@@ -3,25 +3,36 @@ struct Reaction{S <: Variable, T <: Number}
3
3
substrates:: Vector{Operation}
4
4
products:: Vector{Operation}
5
5
substoich:: Vector{T}
6
- prodstoich:: Vector{T}
6
+ prodstoich:: Vector{T}
7
7
netstoich:: Vector{Pair{S,T}}
8
8
only_use_rate:: Bool
9
9
end
10
10
11
- function Reaction (rate, subs, prods, substoich, prodstoich;
11
+ function Reaction (rate, subs, prods, substoich, prodstoich;
12
12
netstoich= nothing , only_use_rate= false , kwargs... )
13
13
14
- subsv = isnothing (subs) ? Vector {Operation} () : subs
15
- prodsv = isnothing (prods) ? Vector {Operation} () : prods
16
- ns = isnothing (netstoich) ? get_netstoich (subsv, prodsv, substoich, prodstoich) : netstoich
17
- Reaction (rate, subsv, prodsv, substoich, prodstoich, ns, only_use_rate)
14
+ (isnothing (prods)&& isnothing (subs)) && error (" A reaction requires a non-nothing substrate or product vector." )
15
+ (isnothing (prodstoich)&& isnothing (substoich)) && error (" Both substrate and product stochiometry inputs cannot be nothing." )
16
+ if isnothing (subs)
17
+ subs = Vector {Operation} ()
18
+ (substoich!= nothing ) && error (" If substrates are nothing, substrate stiocihometries have to be so too." )
19
+ substoich = typeof (prodstoich)()
20
+ end
21
+ if isnothing (prods)
22
+ prods = Vector {Operation} ()
23
+ (prodstoich!= nothing ) && error (" If products are nothing, product stiocihometries have to be so too." )
24
+ prodstoich = typeof (substoich)()
25
+ end
26
+ ns = isnothing (netstoich) ? get_netstoich (subs, prods, substoich, prodstoich) : netstoich
27
+ Reaction (rate, subs, prods, substoich, prodstoich, ns, only_use_rate)
18
28
end
19
29
30
+
20
31
# three argument constructor assumes stoichiometric coefs are one and integers
21
- function Reaction (rate, subs, prods; kwargs... )
32
+ function Reaction (rate, subs, prods; kwargs... )
22
33
23
- sstoich = isnothing (subs) ? Int[] : ones (Int,length (subs))
24
- pstoich = isnothing (prods) ? Int[] : ones (Int,length (prods))
34
+ sstoich = isnothing (subs) ? nothing : ones (Int,length (subs))
35
+ pstoich = isnothing (prods) ? nothing : ones (Int,length (prods))
25
36
Reaction (rate, subs, prods, sstoich, pstoich; kwargs... )
26
37
end
27
38
54
65
function ReactionSystem (eqs, iv, species, params; systems = ReactionSystem[],
55
66
name = gensym (:ReactionSystem ))
56
67
57
- ReactionSystem (eqs, iv, convert .(Variable,species), convert .(Variable,params),
68
+ ReactionSystem (eqs, iv, convert .(Variable,species), convert .(Variable,params),
58
69
name, systems)
59
70
end
60
71
61
72
# Calculate the ODE rate law
62
73
function oderatelaw (rx)
63
- @unpack rate, substrates, substoich, only_use_rate = rx
74
+ @unpack rate, substrates, substoich, only_use_rate = rx
64
75
rl = rate
65
76
if ! only_use_rate
66
77
coef = one (eltype (substoich))
67
78
for (i,stoich) in enumerate (substoich)
68
- coef *= factorial (stoich)
79
+ coef *= factorial (stoich)
69
80
rl *= isone (stoich) ? substrates[i] : substrates[i]^ stoich
70
81
end
71
82
(! isone (coef)) && (rl /= coef)
@@ -78,15 +89,15 @@ function assemble_drift(rs)
78
89
eqs = [D (x (rs. iv ())) ~ 0 for x in rs. states]
79
90
species_to_idx = Dict ((x => i for (i,x) in enumerate (rs. states)))
80
91
81
- for rx in rs. eqs
92
+ for rx in rs. eqs
82
93
rl = oderatelaw (rx)
83
94
for (spec,stoich) in rx. netstoich
84
95
i = species_to_idx[spec]
85
96
if iszero (eqs[i]. rhs)
86
97
signedrl = (stoich > zero (stoich)) ? rl : - rl
87
- rhs = isone (abs (stoich)) ? signedrl : stoich * rl
98
+ rhs = isone (abs (stoich)) ? signedrl : stoich * rl
88
99
else
89
- Δspec = isone (abs (stoich)) ? rl : abs (stoich) * rl
100
+ Δspec = isone (abs (stoich)) ? rl : abs (stoich) * rl
90
101
rhs = (stoich > zero (stoich)) ? (eqs[i]. rhs + Δspec) : (eqs[i]. rhs - Δspec)
91
102
end
92
103
eqs[i] = Equation (eqs[i]. lhs, rhs)
@@ -104,7 +115,7 @@ function assemble_diffusion(rs)
104
115
for (spec,stoich) in rx. netstoich
105
116
i = species_to_idx[spec]
106
117
signedrlsqrt = (stoich > zero (stoich)) ? rlsqrt : - rlsqrt
107
- eqs[i,j] = isone (abs (stoich)) ? signedrlsqrt : stoich * rlsqrt
118
+ eqs[i,j] = isone (abs (stoich)) ? signedrlsqrt : stoich * rlsqrt
108
119
end
109
120
end
110
121
eqs
0 commit comments