@@ -186,23 +186,23 @@ function numericnstoich(mtrs::Vector{Pair{V,W}}, statetoid) where {V,W}
186
186
end
187
187
188
188
# assemble a numeric MassActionJump from a MT MassActionJump representing one rx.
189
- function assemble_maj (maj:: MassActionJump , statetoid, subber, invttype)
190
- rval = subber (maj. scaled_rates)
191
- rs = numericrstoich (maj. reactant_stoch, statetoid)
192
- ns = numericnstoich (maj. net_stoch, statetoid)
193
- maj = MassActionJump (convert (invttype, value (rval)), rs, ns, scale_rates = false )
194
- maj
195
- end
196
-
197
- # For MassActionJumps that contain many reactions
198
- # function assemble_maj(maj::MassActionJump{U,V,W}, statetoid, subber,
199
- # invttype) where {U <: AbstractVector,V,W}
200
- # rval = [convert(invttype,numericrate(sr, subber)) for sr in maj.scaled_rates]
201
- # rs = [numericrstoich(rs, statetoid) for rs in maj.reactant_stoch]
202
- # ns = [numericnstoich(ns, statetoid) for ns in maj.net_stoch]
203
- # maj = MassActionJump(rval, rs, ns, scale_rates = false)
189
+ # function assemble_maj(maj::MassActionJump, statetoid, subber, invttype)
190
+ # rval = subber(maj.scaled_rates)
191
+ # rs = numericrstoich(maj.reactant_stoch, statetoid)
192
+ # ns = numericnstoich(maj.net_stoch, statetoid)
193
+ # maj = MassActionJump(convert(invttype, value(rval)), rs, ns, scale_rates = false)
204
194
# maj
205
195
# end
196
+
197
+ rstype (:: MassActionJump{U,Vector{Pair{V,W}},X,Y} ) where {U,V,W,X,Y} = W
198
+
199
+ # assemble a numeric MassActionJump from a MT symbolics MassActionJumps
200
+ function assemble_maj (majv:: Vector{U} , statetoid, pmapper, params) where {U <: MassActionJump }
201
+ rs = [numericrstoich (maj. reactant_stoch, statetoid) for maj in majv]
202
+ ns = [numericnstoich (maj. net_stoch, statetoid) for maj in majv]
203
+ MassActionJump (rs, ns; param_mapper = pmapper, params= params, scale_rates= false , nocopy= true )
204
+ end
205
+
206
206
"""
207
207
```julia
208
208
function DiffEqBase.DiscreteProblem(sys::JumpSystem, u0map, tspan,
@@ -287,14 +287,13 @@ function DiffEqJump.JumpProblem(js::JumpSystem, prob, aggregator; kwargs...)
287
287
288
288
# handling parameter substition and empty param vecs
289
289
p = (prob. p isa DiffEqBase. NullParameters || prob. p === nothing ) ? Num[] : prob. p
290
- parammap = map ((x,y)-> Pair (x,y), parameters (js), p)
291
- subber = substituter (parammap)
292
290
293
- majs = MassActionJump[assemble_maj (j, statetoid, subber, invttype) for j in eqs. x[1 ]]
291
+ majpmapper = JumpSysMajParamMapper (js, p; jseqs= eqs, rateconsttype= invttype)
292
+ majs = isempty (eqs. x[1 ]) ? nothing : assemble_maj (eqs. x[1 ], statetoid, majpmapper, p)
294
293
crjs = ConstantRateJump[assemble_crj (js, j, statetoid) for j in eqs. x[2 ]]
295
294
vrjs = VariableRateJump[assemble_vrj (js, j, statetoid) for j in eqs. x[3 ]]
296
295
((prob isa DiscreteProblem) && ! isempty (vrjs)) && error (" Use continuous problems such as an ODEProblem or a SDEProblem with VariableRateJumps" )
297
- jset = JumpSet (Tuple (vrjs), Tuple (crjs), nothing , isempty (majs) ? nothing : majs)
296
+ jset = JumpSet (Tuple (vrjs), Tuple (crjs), nothing , majs)
298
297
299
298
if needs_vartojumps_map (aggregator) || needs_depgraph (aggregator)
300
299
jdeps = asgraph (js)
@@ -338,3 +337,44 @@ function modified_states!(mstates, jump::MassActionJump, sts)
338
337
any (isequal (state), sts) && push! (mstates, state)
339
338
end
340
339
end
340
+
341
+
342
+
343
+ # ##################### parameter mapper ###########################
344
+ struct JumpSysMajParamMapper{U,V,W}
345
+ paramexprs:: U # the parameter expressions to use for each jump rate constant
346
+ sympars:: V # parameters(sys) from the underlying JumpSystem
347
+ subdict # mapping from an element of parameters(sys) to its current numerical value
348
+ end
349
+
350
+ function JumpSysMajParamMapper (js:: JumpSystem , p; jseqs= nothing , rateconsttype= Float64)
351
+ eqs = (jseqs === nothing ) ? equations (js) : jseqs
352
+ psyms = parameters (js)
353
+ parammap = map ((x,y)-> Pair (x,y), psyms, p)
354
+ paramdict = Dict (value (k) => value (v) for (k, v) in parammap)
355
+ paramexprs = [maj. scaled_rates for maj in eqs. x[1 ]]
356
+ JumpSysMajParamMapper {typeof(paramexprs),typeof(psyms),rateconsttype} (paramexprs, psyms, paramdict)
357
+ end
358
+
359
+ function updateparams! (ratemap:: JumpSysMajParamMapper{U,V,W} , params) where {U <: AbstractArray , V <: AbstractArray , W}
360
+ for (i,p) in enumerate (params)
361
+ sympar = ratemap. sympars[i]
362
+ ratemap. subdict[sympar] = p
363
+ end
364
+ end
365
+
366
+ # create the initial parameter vector for use in a MassActionJump
367
+ function (ratemap:: JumpSysMajParamMapper{U,V,W} )(params) where {U <: AbstractArray , V <: AbstractArray , W}
368
+ updateparams! (ratemap, params)
369
+ [convert (W,value (substitute (paramexpr, ratemap. subdict))) for paramexpr in ratemap. paramexprs]
370
+ end
371
+
372
+ # update a maj with parameter vectors
373
+ function (ratemap:: JumpSysMajParamMapper{U,V,W} )(maj:: MassActionJump , newparams; scale_rates, kwargs... ) where {U <: AbstractArray , V <: AbstractArray , W}
374
+ updateparams! (ratemap, newparams)
375
+ for i in 1 : get_num_majumps (maj)
376
+ maj. scaled_rates[i] = convert (W,value (substitute (ratemap. paramexprs[i], ratemap. subdict)))
377
+ end
378
+ scale_rates && DiffEqJump. scalerates! (maj. scaled_rates, maj. reactant_stoch)
379
+ nothing
380
+ end
0 commit comments