Skip to content

Commit 01c0646

Browse files
shashiYingboMa
andcommitted
FIXED ismassaction FINALLY
Co-authored-by: "Yingbo Ma" <[email protected]>
1 parent 25d0acd commit 01c0646

File tree

3 files changed

+7
-12
lines changed

3 files changed

+7
-12
lines changed

src/systems/reaction/reactionsystem.jl

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,7 @@ Notes:
260260
function jumpratelaw(rx; rxvars=get_variables(rx.rate), combinatoric_ratelaw=true)
261261
@unpack rate, substrates, substoich, only_use_rate = rx
262262
rl = rate
263-
for op in rxvars
264-
rl = substitute(rl, op => var2op(op))
265-
end
263+
rl = substitute(rl, Dict(rxvars .=> var2op.(rxvars)))
266264
if !only_use_rate
267265
coef = one(eltype(substoich))
268266
for (i,stoich) in enumerate(substoich)
@@ -299,7 +297,7 @@ explicitly on the independent variable (usually time).
299297
- Optional: `stateset`, set of states which if the rxvars are within mean rx is non-mass action.
300298
"""
301299
function ismassaction(rx, rs; rxvars = get_variables(rx.rate),
302-
haveivdep = any(var -> isequal(rs.iv,convert(Variable,var)), rxvars),
300+
haveivdep,
303301
stateset = Set(states(rs)))
304302
# if no dependencies must be zero order
305303
(length(rxvars)==0) && return true
@@ -316,13 +314,13 @@ end
316314
zeroorder = (length(substoich) == 0)
317315
reactant_stoch = Vector{Pair{Any,eltype(substoich)}}(undef, length(substoich))
318316
@inbounds for i = 1:length(reactant_stoch)
319-
reactant_stoch[i] = var2op(substrates[i]) => substoich[i]
317+
reactant_stoch[i] = substrates[i] => substoich[i]
320318
end
321319
#push!(rstoich, reactant_stoch)
322320
coef = (zeroorder || (!combinatoric_ratelaw)) ? one(eltype(substoich)) : prod(stoich -> factorial(stoich), substoich)
323321
(!isone(coef)) && (rate /= coef)
324322
#push!(rates, rate)
325-
net_stoch = [Pair(var2op(p[1]),p[2]) for p in netstoich]
323+
net_stoch = [Pair(p[1],p[2]) for p in netstoich]
326324
#push!(nstoich, net_stoch)
327325
MassActionJump(Num(rate), reactant_stoch, net_stoch, scale_rates=false, useiszero=false)
328326
end
@@ -337,18 +335,14 @@ function assemble_jumps(rs; combinatoric_ratelaws=true)
337335
isempty(equations(rs)) && error("Must give at least one reaction before constructing a JumpSystem.")
338336
for rx in equations(rs)
339337
empty!(rxvars)
340-
@show rx.rate
341-
@show typeof(rx.rate)
342-
(rx.rate isa Term) && get_variables!(rxvars, rx.rate)
338+
(rx.rate isa Symbolic) && get_variables!(rxvars, rx.rate)
343339
haveivdep = false
344340
@inbounds for i = 1:length(rxvars)
345341
if isequal(rxvars[i], rs.iv)
346342
haveivdep = true
347343
break
348344
end
349345
end
350-
@show rx.rate
351-
@show haveivdep
352346
if ismassaction(rx, rs; rxvars=rxvars, haveivdep=haveivdep, stateset=stateset)
353347
push!(meqs, makemajump(rx, combinatoric_ratelaw=combinatoric_ratelaws))
354348
else

src/utils.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ substitute(expr::Operation, s::Vector)
100100
Performs the substitution `Operation => val` on the `expr` Operation.
101101
"""
102102
substitute(expr::Num, s::Union{Pair, Vector, Dict}; kw...) = Num(substituter(s)(value(expr); kw...))
103+
# TODO: move this to SymbolicUtils
103104
substitute(expr::Term, s::Pair; kw...) = substituter([s[1] => s[2]])(expr; kw...)
104105
substitute(expr::Term, s::Vector; kw...) = substituter(s)(expr; kw...)
105106

test/reactionsystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ jumps[19] = VariableRateJump((u,p,t) -> p[19]*u[1]*t, integrator -> (integrator.
150150
jumps[20] = VariableRateJump((u,p,t) -> p[20]*t*u[1]*binomial(u[2],2)*u[3], integrator -> (integrator.u[2] -= 2; integrator.u[3] -= 1; integrator.u[4] += 2))
151151

152152
statetoid = Dict(state => i for (i,state) in enumerate(states(js)))
153-
parammap = map((x,y)->Pair(x(),y),parameters(js),pars)
153+
parammap = map((x,y)->Pair(x,y),parameters(js),pars)
154154
for i in midxs
155155
maj = MT.assemble_maj(js.eqs[i], statetoid, ModelingToolkit.substituter(parammap),eltype(pars))
156156
@test abs(jumps[i].scaled_rates - maj.scaled_rates) < 100*eps()

0 commit comments

Comments
 (0)