Skip to content

Commit 99a9753

Browse files
committed
init
1 parent 5277a2c commit 99a9753

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

src/reaction.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ end
163163
function Reaction(rate, subs, prods, substoich, prodstoich;
164164
netstoich = nothing, metadata = Pair{Symbol, Any}[],
165165
only_use_rate = metadata_only_use_rate_check(metadata), kwargs...)
166+
# Handles empty/nothing vectors.
167+
isnothing(subs) || isempty(subs) && (subs = nothing)
168+
isnothing(prods) || isempty(prods) && (prods = nothing)
169+
isnothing(substoich) || isempty(substoich) && (substoich = nothing)
170+
isnothing(prodstoich) || isempty(prodstoich) && (prodstoich = nothing)
171+
166172
(isnothing(prods) && isnothing(subs)) &&
167173
throw(ArgumentError("A reaction requires a non-nothing substrate or product vector."))
168174
(isnothing(prodstoich) && isnothing(substoich)) &&

test/reactionsystem_core/reaction.jl

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,89 @@ using ModelingToolkit: value, get_variables!
88
# Sets the default `t` to use.
99
t = default_t()
1010

11+
### Reaction Constructor Tests ###
12+
13+
# Checks that `Reaction`s can be successfully created using various complicated inputs.
14+
# Checks that the `Reaction`s have the correct type, and the correct net stoichiometries are generated.
15+
let
16+
# Declare symbolic variables.
17+
@parameters k n1 n2::Int32 x [isconstantspecies=true]
18+
@species X(t) Y(t) Z(t)
19+
@variables A(t)
20+
21+
# Tries for different types of rates (should not matter).
22+
for rate in (k, k*A, 2, 3.0, 4//3)
23+
# Creates `Reaction`s.
24+
rx1 = Reaction(rate, [X], [])
25+
rx2 = Reaction(rate, [x], [Y], [1.5], [1])
26+
rx3 = Reaction(rate, [x, X], [], [n1 + n2, n2], [])
27+
rx4 = Reaction(rate, [X, Y], [X, Y, Z], [2//3, 3], [1//3, 1, 2])
28+
rx5 = Reaction(rate, [X, Y], [X, Y, Z], [2, 3], [1, n1, n2])
29+
rx6 = Reaction(rate, [X], [x], [n1], [1])
30+
31+
# Check `Reaction` types.
32+
@test rx1 isa Reaction{Any,Int64}
33+
@test rx2 isa Reaction{Any,Float64}
34+
@test rx3 isa Reaction{Any,Any}
35+
@test rx4 isa Reaction{Any,Rational{Int64}}
36+
@test rx5 isa Reaction{Any,Any}
37+
@test rx6 isa Reaction{Any,Any}
38+
39+
# Check `Reaction` net stoichiometries.
40+
issetequal(rx1.netstoich, [X => -1])
41+
issetequal(rx2.netstoich, [x => -1.5, Y => 1.0])
42+
issetequal(rx3.netstoich, [x => -n1 - n2, X => -n2])
43+
issetequal(rx4.netstoich, [X => -1//3, Y => -2//1, Z => 2//1])
44+
issetequal(rx5.netstoich, [X => -1, Y => n1 - 3, Z => n2])
45+
issetequal(rx6.netstoich, [X => -n1, x => 1])
46+
end
47+
end
48+
49+
# Tests that various `Reaction` constructors gives identical inputs.
50+
let
51+
# Declare symbolic variables.
52+
@parameters k n1 n2::Int32
53+
@species X(t) Y(t) Z(t)
54+
@variables A(t)
55+
56+
# Tests that the three-argument constructor generates correct result.
57+
@test Reaction(k*A, [X], [Y, Z]) == Reaction(k*A, [X], [Y, Z], [1], [1, 1])
58+
59+
# Tests that `[]` and `nothing` can be used interchangeably.
60+
@test Reaction(k*A, [X, Z], nothing) == Reaction(k*A, [X, Z], [])
61+
@test Reaction(k*A, nothing, [Y, Z]) == Reaction(k*A, [], [Y, Z])
62+
@test Reaction(k*A, [X, Z], nothing, [n1 + n2, 2], nothing) == Reaction(k*A, [X, Z], [], [n1 + n2, 2], [])
63+
@test Reaction(k*A, nothing, [Y, Z], nothing, [n1 + n2, 2]) == Reaction(k*A, [], [Y, Z], [], [n1 + n2, 2])
64+
end
65+
66+
# Tests that various incorrect inputs yields errors.
67+
let
68+
# Declare symbolic variables.
69+
@parameters k n1 n2::Int32
70+
@species X(t) Y(t) Z(t)
71+
@variables A(t)
72+
73+
# Neither substrates nor products.
74+
@test_throws ArgumentError Reaction(k*A, [], [])
75+
76+
# Substrate vector not of equal length to substrate stoichiometry vector.
77+
@test_throws ArgumentError Reaction(k*A, [X, X, Z], [], [1, 2], [])
78+
79+
# Product vector not of equal length to product stoichiometry vector.
80+
@test_throws ArgumentError Reaction(k*A, [], [X, X, Z], [], [1, 2])
81+
82+
# Repeated substrates.
83+
@test_throws ArgumentError Reaction(k*A, [X, X, Z], [])
84+
85+
# Repeated products.
86+
@test_throws ArgumentError Reaction(k*A, [], [Y, Z, Z])
87+
88+
# Non-valid reactants (parameter or variable).
89+
@test_throws ArgumentError Reaction(k*A, [], [A])
90+
@test_throws ArgumentError Reaction(k*A, [], [k])
91+
end
92+
93+
1194
### Test Basic Accessors ###
1295

1396
# Tests the `get_variables` function.
@@ -42,7 +125,6 @@ end
42125
# Tests basic accessor functions.
43126
# Tests that repeated metadata entries are not permitted.
44127
let
45-
@variables t
46128
@parameters k
47129
@species X(t) X2(t)
48130

@@ -60,7 +142,6 @@ end
60142

61143
# Tests accessors for system without metadata.
62144
let
63-
@variables t
64145
@parameters k
65146
@species X(t) X2(t)
66147

@@ -77,7 +158,6 @@ end
77158
# Tests basic accessor functions.
78159
# Tests various metadata types.
79160
let
80-
@variables t
81161
@parameters k
82162
@species X(t) X2(t)
83163

@@ -109,7 +189,6 @@ end
109189

110190
# Tests the noise scaling metadata.
111191
let
112-
@variables t
113192
@parameters k η
114193
@species X(t) X2(t)
115194

0 commit comments

Comments
 (0)