Skip to content

Commit 2037a92

Browse files
Merge pull request #357 from TorkelE/fixconvert(JumpSystem,ReactionSystem)
Fix to making JumpSystems from ReactionSystems
2 parents 61aa190 + 0a9e73a commit 2037a92

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/systems/reaction/reactionsystem.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,13 @@ end
148148
function jumpratelaw(rx)
149149
@unpack rate, substrates, substoich, only_use_rate = rx
150150
rl = deepcopy(rate)
151-
foreach(op -> substitute_expr!(rl,op=>var2op(op.op)), get_variables(rx.rate))
151+
for op in get_variables(rx.rate)
152+
rl = substitute_expr!(rl,op=>var2op(op.op))
153+
end
152154
if !only_use_rate
153-
coef = one(eltype(substoich))
154155
for (i,stoich) in enumerate(substoich)
155-
coef *= factorial(stoich)
156-
rl *= isone(stoich) ? var2op(substrates[i].op) : var2op(substrates[i].op)^stoich
156+
rl *= isone(stoich) ? var2op(substrates[i].op) : Operation(binomial,[var2op(substrates[i].op),stoich])
157157
end
158-
(!isone(coef)) && (rl /= coef)
159158
end
160159
rl
161160
end

test/reactionsystem.jl

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using ModelingToolkit, LinearAlgebra, Test
22

3-
@parameters t k[1:15]
3+
@parameters t k[1:20]
44
@variables A(t) B(t) C(t) D(t)
55
rxs = [Reaction(k[1], nothing, [A]), # 0 -> A
66
Reaction(k[2], [B], nothing), # B -> 0
@@ -13,11 +13,16 @@ rxs = [Reaction(k[1], nothing, [A]), # 0 -> A
1313
Reaction(k[9], [A,B], [C,D]), # A + B -> C + D
1414
Reaction(k[10], [A], [C,D], [2], [1,1]), # 2A -> C + D
1515
Reaction(k[11], [A], [A,B], [2], [1,1]), # 2A -> A + B
16-
Reaction(k[12], [A,B,C], [C,D], [1,3,4], [2, 3]), # A+3B+4C -> 2C + 3D
17-
Reaction(k[13], [A,B], nothing, [3,1], nothing), # 3A+B -> 0
18-
Reaction(k[14], nothing, [A], nothing, [2]), # 0 -> 2A
19-
Reaction(k[15]*A/(2+A), [A], nothing; only_use_rate=true) # A -> 0 with custom rate
20-
]
16+
Reaction(k[12], [A,B,C], [C,D], [1,3,4], [2, 3]), # A+3B+4C -> 2C + 3D
17+
Reaction(k[13], [A,B], nothing, [3,1], nothing), # 3A+B -> 0
18+
Reaction(k[14], nothing, [A], nothing, [2]), # 0 -> 2A
19+
Reaction(k[15]*A/(2+A), [A], nothing; only_use_rate=true), # A -> 0 with custom rate
20+
Reaction(k[16], [A], [B]; only_use_rate=true), # A -> B with custom rate.
21+
Reaction(k[17]*A*exp(B), [C], [D], [2], [1]), # 2C -> D with non constant rate.
22+
Reaction(k[18]*B, nothing, [B], nothing, [2]), # 0 -> 2B with non constant rate.
23+
Reaction(k[19]*t, [A], [B]), # A -> B with non constant rate.
24+
Reaction(k[20]*t*A, [B,C], [D],[2,1],[2]) # 2A +B -> 2C with non constant rate.
25+
]
2126
rs = ReactionSystem(rxs,t,[A,B,C,D],k)
2227
odesys = convert(ODESystem,rs)
2328
sdesys = convert(SDESystem,rs)
@@ -26,10 +31,10 @@ sdesys = convert(SDESystem,rs)
2631
function oderhs(u,k,t)
2732
A = u[1]; B = u[2]; C = u[3]; D = u[4];
2833
du = zeros(eltype(u),4)
29-
du[1] = k[1] - k[3]*A + k[4]*C + 2*k[5]*C - k[6]*A*B + k[7]*B^2/2 - k[9]*A*B - k[10]*A^2 - k[11]*A^2/2 - k[12]*A*B^3*C^4/144 - 3*k[13]*A^3*B/6 + 2*k[14] - k[15]*A/(2+A)
30-
du[2] = -k[2]*B + k[4]*C - k[6]*A*B - k[7]*B^2 - k[8]*A*B - k[9]*A*B + k[11]*A^2/2 - 3*k[12]*A*B^3*C^4/144 - k[13]*A^3*B/6
31-
du[3] = k[3]*A - k[4]*C - k[5]*C + k[6]*A*B + k[8]*A*B + k[9]*A*B + k[10]*A^2/2 - 2*k[12]*A*B^3*C^4/144
32-
du[4] = k[9]*A*B + k[10]*A^2/2 + 3*k[12]*A*B^3*C^4/144
34+
du[1] = k[1] - k[3]*A + k[4]*C + 2*k[5]*C - k[6]*A*B + k[7]*B^2/2 - k[9]*A*B - k[10]*A^2 - k[11]*A^2/2 - k[12]*A*B^3*C^4/144 - 3*k[13]*A^3*B/6 + 2*k[14] - k[15]*A/(2+A) - k[16] - k[19]*t*A
35+
du[2] = -k[2]*B + k[4]*C - k[6]*A*B - k[7]*B^2 - k[8]*A*B - k[9]*A*B + k[11]*A^2/2 - 3*k[12]*A*B^3*C^4/144 - k[13]*A^3*B/6 + k[16] + 2*k[18]*B + k[19]*t*A - 2*k[20]*t*A*B^2*C
36+
du[3] = k[3]*A - k[4]*C - k[5]*C + k[6]*A*B + k[8]*A*B + k[9]*A*B + k[10]*A^2/2 - 2*k[12]*A*B^3*C^4/144 - 2*k[17]*A*exp(B)*C^2/2 - k[20]*t*A*B^2*C
37+
du[4] = k[9]*A*B + k[10]*A^2/2 + 3*k[12]*A*B^3*C^4/144 + k[17]*A*exp(B)*C^2/2 + 2*k[20]*t*A*B^2*C
3338
du
3439
end
3540

@@ -51,10 +56,14 @@ function sdenoise(u,k,t)
5156
-2*sqrt(k[10]*A^2/2) z sqrt(k[10]*A^2/2) sqrt(k[10]*A^2/2);
5257
-sqrt(k[11]*A^2/2) sqrt(k[11]*A^2/2) z z;
5358
-sqrt(k[12]*A*B^3*C^4/144) -3*sqrt(k[12]*A*B^3*C^4/144) -2*sqrt(k[12]*A*B^3*C^4/144) 3*sqrt(k[12]*A*B^3*C^4/144);
54-
-3*sqrt(k[13]*A^3*B/6) -sqrt(k[13]*A^3*B/6) z z
59+
-3*sqrt(k[13]*A^3*B/6) -sqrt(k[13]*A^3*B/6) z z;
5560
2*sqrt(k[14]) z z z;
56-
-sqrt(k[15]*A/(2+A)) z z z]'
57-
61+
-sqrt(k[15]*A/(2+A)) z z z;
62+
-sqrt(k[16]) sqrt(k[16]) z z;
63+
z z -2*sqrt(k[17]*A*exp(B)*C^2/2) sqrt(k[17]*A*exp(B)*C^2/2);
64+
z 2*sqrt(k[18]*B) z z;
65+
-sqrt(k[19]*t*A) sqrt(k[19]*t*A) z z;
66+
z -2*sqrt(k[20]*t*A*B^2*C) -sqrt(k[20]*t*A*B^2*C) +2*sqrt(k[20]*t*A*B^2*C)]'
5867
return G
5968
end
6069

0 commit comments

Comments
 (0)