4545
4646function JumpSystem (eqs, iv, states, ps; systems = JumpSystem[],
4747 name = gensym (:JumpSystem ))
48-
4948 ap = ArrayPartition (MassActionJump[], ConstantRateJump[], VariableRateJump[])
5049 for eq in eqs
5150 if eq isa MassActionJump
@@ -62,6 +61,9 @@ function JumpSystem(eqs, iv, states, ps; systems = JumpSystem[],
6261 JumpSystem {typeof(ap)} (ap, convert (Variable,iv), convert .(Variable, states), convert .(Variable, ps), name, systems)
6362end
6463
64+ JumpSystem (eqs:: ArrayPartition , iv, states, ps; systems = JumpSystem[], name = gensym (:JumpSystem )) =
65+ JumpSystem {typeof(eqs)} (eqs, convert (Variable,iv), convert .(Variable, states), convert .(Variable, ps), name, systems)
66+
6567
6668generate_rate_function (js, rate) = build_function (rate, states (js), parameters (js),
6769 independent_variable (js),
@@ -114,45 +116,64 @@ function assemble_crj_expr(js, crj, statetoid)
114116 end
115117end
116118
117- function assemble_maj (js, maj:: MassActionJump{U,Vector{Pair{V,W}},Vector{Pair{V2,W2}}} ,
118- statetoid, subber, invttype) where {U,V,W,V2,W2}
119- sr = maj. scaled_rates
120- if sr isa Operation
121- pval = subber (sr). value
122- elseif sr isa Variable
123- pval = subber (sr ()). value
119+ function numericrate (rate, subber)
120+ if rate isa Operation
121+ rval = subber (rate). value
122+ elseif rate isa Variable
123+ rval = subber (rate ()). value
124124 else
125- pval = maj . scaled_rates
125+ rval = rate
126126 end
127+ rval
128+ end
127129
130+ function numericrstoich (mtrs:: Vector{Pair{V,W}} , statetoid) where {V,W}
128131 rs = Vector {Pair{Int,W}} ()
129- for (spec,stoich) in maj . reactant_stoch
130- if iszero (spec)
132+ for (spec,stoich) in mtrs
133+ if ! (spec isa Operation) && iszero (spec)
131134 push! (rs, 0 => stoich)
132135 else
133136 push! (rs, statetoid[convert (Variable,spec)] => stoich)
134137 end
135138 end
136139 sort! (rs)
140+ rs
141+ end
137142
138- ns = Vector {Pair{Int,W2}} ()
139- for (spec,stoich) in maj. net_stoch
140- iszero (spec) && error (" Net stoichiometry can not have a species labelled 0." )
143+ function numericnstoich (mtrs:: Vector{Pair{V,W}} , statetoid) where {V,W}
144+ ns = Vector {Pair{Int,W}} ()
145+ for (spec,stoich) in mtrs
146+ ! (spec isa Operation) && iszero (spec) && error (" Net stoichiometry can not have a species labelled 0." )
141147 push! (ns, statetoid[convert (Variable,spec)] => stoich)
142148 end
143149 sort! (ns)
150+ end
144151
145- maj = MassActionJump (convert (invttype, pval), rs, ns, scale_rates = false )
146- return maj
152+ # assemble a numeric MassActionJump from a MT MassActionJump representing one rx.
153+ function assemble_maj (maj:: MassActionJump , statetoid, subber, invttype)
154+ rval = numericrate (maj. scaled_rates, subber)
155+ rs = numericrstoich (maj. reactant_stoch, statetoid)
156+ ns = numericnstoich (maj. net_stoch, statetoid)
157+ maj = MassActionJump (convert (invttype, rval), rs, ns, scale_rates = false )
158+ maj
147159end
148160
161+ # For MassActionJumps that contain many reactions
162+ # function assemble_maj(maj::MassActionJump{U,V,W}, statetoid, subber,
163+ # invttype) where {U <: AbstractVector,V,W}
164+ # rval = [convert(invttype,numericrate(sr, subber)) for sr in maj.scaled_rates]
165+ # rs = [numericrstoich(rs, statetoid) for rs in maj.reactant_stoch]
166+ # ns = [numericnstoich(ns, statetoid) for ns in maj.net_stoch]
167+ # maj = MassActionJump(rval, rs, ns, scale_rates = false)
168+ # maj
169+ # end
149170"""
150171```julia
151172function DiffEqBase.DiscreteProblem(sys::JumpSystem, u0map, tspan,
152173 parammap=DiffEqBase.NullParameters; kwargs...)
153174```
154175
155- Generates a blank DiscreteProblem for a pure jump JumpSystem to utilize as
176+ Generates a blank DiscreteProblem for a pure jump JumpSystem to utilize as
156177its `prob.prob`. This is used in the case where there are no ODEs
157178and no SDEs associated with the system.
158179
@@ -167,9 +188,17 @@ dprob = DiscreteProblem(js, u₀map, tspan, parammap)
167188"""
168189function DiffEqBase. DiscreteProblem (sys:: JumpSystem , u0map, tspan:: Tuple ,
169190 parammap= DiffEqBase. NullParameters (); kwargs... )
170- u0 = varmap_to_vars (u0map, states (sys))
171- p = varmap_to_vars (parammap, parameters (sys))
172- # identity function to make syms works
191+
192+ (u0map isa AbstractVector) || error (" For DiscreteProblems u0map must be an AbstractVector." )
193+ u0d = Dict ( convert (Variable,u[1 ]) => u[2 ] for u in u0map)
194+ u0 = [u0d[u] for u in states (sys)]
195+ if parammap != DiffEqBase. NullParameters ()
196+ (parammap isa AbstractVector) || error (" For DiscreteProblems parammap must be an AbstractVector." )
197+ pd = Dict ( convert (Variable,u[1 ]) => u[2 ] for u in parammap)
198+ p = [pd[u] for u in parameters (sys)]
199+ else
200+ p = parammap
201+ end
173202 # EvalFunc because we know that the jump functions are generated via eval
174203 f = DiffEqBase. EvalFunc (DiffEqBase. DISCRETE_INPLACE_DEFAULT)
175204 df = DiscreteFunction (f, syms= Symbol .(states (sys)))
@@ -235,7 +264,7 @@ function DiffEqJump.JumpProblem(js::JumpSystem, prob, aggregator; kwargs...)
235264 parammap = map ((x,y)-> Pair (x (),y), parameters (js), p)
236265 subber = substituter (parammap)
237266
238- majs = MassActionJump[assemble_maj (js, j, statetoid, subber, invttype) for j in eqs. x[1 ]]
267+ majs = MassActionJump[assemble_maj (j, statetoid, subber, invttype) for j in eqs. x[1 ]]
239268 crjs = ConstantRateJump[assemble_crj (js, j, statetoid) for j in eqs. x[2 ]]
240269 vrjs = VariableRateJump[assemble_vrj (js, j, statetoid) for j in eqs. x[3 ]]
241270 ((prob isa DiscreteProblem) && ! isempty (vrjs)) && error (" Use continuous problems such as an ODEProblem or a SDEProblem with VariableRateJumps" )
0 commit comments