@@ -42,6 +42,34 @@ function assemble_crj(js, crj, statetoid)
4242 ConstantRateJump (rate, affect)
4343end
4444
45+ function assemble_maj (js, maj:: MassActionJump{U,Vector{Pair{V,W}},Vector{Pair{V2,W2}}} ,
46+ statetoid, ptoid, p, pcontext) where {U,V,W,V2,W2}
47+ sr = maj. scaled_rates
48+ if sr isa Operation || sr isa Variable
49+ pval = Base. eval (pcontext, Expr (maj. scaled_rates))
50+ else
51+ pval = maj. scaled_rates
52+ end
53+
54+ rs = Vector {Pair{Int,W}} ()
55+ for (spec,stoich) in maj. reactant_stoch
56+ if iszero (spec)
57+ push! (rs, 0 => stoich)
58+ else
59+ push! (rs, statetoid[convert (Variable,spec)] => stoich)
60+ end
61+ end
62+ sort! (rs)
63+
64+ ns = Vector {Pair{Int,W2}} ()
65+ for (spec,stoich) in maj. net_stoch
66+ iszero (spec) && error (" Net stoichiometry can not have a species labelled 0." )
67+ push! (ns, statetoid[convert (Variable,spec)] => stoich)
68+ end
69+ sort! (ns)
70+
71+ MassActionJump (pval, rs, ns, scale_rates = false )
72+ end
4573
4674"""
4775```julia
@@ -68,17 +96,32 @@ Generates a JumpProblem from a JumpSystem.
6896function DiffEqJump. JumpProblem (js:: JumpSystem , prob, aggregator; kwargs... )
6997 vrjs = Vector {VariableRateJump} ()
7098 crjs = Vector {ConstantRateJump} ()
99+ majs = Vector {MassActionJump} ()
100+ pvars = parameters (js)
71101 statetoid = Dict (convert (Variable,state) => i for (i,state) in enumerate (states (js)))
102+ ptoid = Dict (convert (Variable,par) => i for (i,par) in enumerate (parameters (js)))
103+
104+ # for mass action jumps might need to evaluate parameter expressions
105+ # populate dummy module with params as local variables
106+ # (for eval-ing parameter expressions)
107+ param_context = Module ()
108+ for (i, pval) in enumerate (prob. p)
109+ psym = Symbol (pvars[i])
110+ Base. eval (param_context, :($ psym = $ pval))
111+ end
112+
72113 for j in equations (js)
73114 if j isa ConstantRateJump
74115 push! (crjs, assemble_crj (js, j, statetoid))
75116 elseif j isa VariableRateJump
76117 push! (vrjs, assemble_vrj (js, j, statetoid))
118+ elseif j isa MassActionJump
119+ push! (majs, assemble_maj (js, j, statetoid, ptoid, prob. p, param_context))
77120 else
78- (j isa MassActionJump) && error (" Generation of JumpProblems with MassActionJumps is not yet supported ." )
121+ error (" JumpSystems should only contain Constant, Variable or Mass Action Jumps ." )
79122 end
80123 end
81124 ((prob isa DiscreteProblem) && ! isempty (vrjs)) && error (" Use continuous problems such as an ODEProblem or a SDEProblem with VariableRateJumps" )
82- jset = JumpSet (Tuple (vrjs), Tuple (crjs), nothing , nothing )
125+ jset = JumpSet (Tuple (vrjs), Tuple (crjs), nothing , isempty (majs) ? nothing : majs )
83126 JumpProblem (prob, aggregator, jset)
84127end
0 commit comments