Skip to content

Commit 46ad023

Browse files
Merge pull request #354 from isaacsas/jumpsys-maj-fix
Fix MassActionJump parameter evaluation
2 parents a2d62ab + 7ba902f commit 46ad023

File tree

5 files changed

+11
-8
lines changed

5 files changed

+11
-8
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
1515
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1616
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
1717
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
18+
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
1819
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
1920
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2021
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"

src/ModelingToolkit.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import GeneralizedGenerated
1313
using DocStringExtensions
1414
using Base: RefValue
1515

16+
using RecursiveArrayTools
17+
1618
import SymbolicUtils
1719
import SymbolicUtils: to_symbolic, FnType
1820

src/systems/jumps/jumpsystem.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ function assemble_crj(js, crj, statetoid)
4343
end
4444

4545
function assemble_maj(js, maj::MassActionJump{U,Vector{Pair{V,W}},Vector{Pair{V2,W2}}},
46-
statetoid, ptoid, parammap) where {U,V,W,V2,W2}
46+
statetoid, parammap) where {U,V,W,V2,W2}
4747
sr = maj.scaled_rates
4848
if sr isa Operation
49-
pval = substitute(sr,parammap)
49+
pval = simplify(substitute(sr,parammap)).value
5050
elseif sr isa Variable
5151
pval = Dict(parammap)[sr()]
5252
else
@@ -101,7 +101,7 @@ function DiffEqJump.JumpProblem(js::JumpSystem, prob, aggregator; kwargs...)
101101
majs = Vector{MassActionJump}()
102102
pvars = parameters(js)
103103
statetoid = Dict(convert(Variable,state) => i for (i,state) in enumerate(states(js)))
104-
parammap = map(Pair,pvars,prob.p)
104+
parammap = map((x,y)->Pair(x(),y),pvars,prob.p)
105105

106106
for j in equations(js)
107107
if j isa ConstantRateJump

test/jumpsystem.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ integrator = TestInt(u,p,tf)
4444
@test abs(mtjump2.rate(u,p,tf) - jump2.rate(u,p,tf)) < 10*eps()
4545
mtjump1.affect!(mtintegrator)
4646
jump1.affect!(integrator)
47-
@test all(integrator.u .== mtintegrator.u)
47+
@test all(integrator.u .== mtintegrator.u)
4848
mtintegrator.u .= u; integrator.u .= u
4949
mtjump2.affect!(mtintegrator)
5050
jump2.affect!(integrator)
@@ -60,7 +60,7 @@ u₀map = [S => 999, I => 1, R => 0]
6060
parammap ==> .1/1000, γ => .01]
6161
dprob = DiscreteProblem(js2, u₀map, tspan, parammap)
6262
jprob = JumpProblem(js2, dprob, Direct(), save_positions=(false,false))
63-
Nsims = 10000
63+
Nsims = 30000
6464
function getmean(jprob,Nsims)
6565
m = 0.0
6666
for i = 1:Nsims
@@ -79,7 +79,7 @@ mtjumps = jprob.discrete_jump_aggregation
7979
@test abs(mtjumps.rates[2](u,p,tf) - jump2.rate(u,p,tf)) < 10*eps()
8080
mtjumps.affects![1](mtintegrator)
8181
jump1.affect!(integrator)
82-
@test all(integrator.u .== mtintegrator.u)
82+
@test all(integrator.u .== mtintegrator.u)
8383
mtintegrator.u .= u; integrator.u .= u
8484
mtjumps.affects![2](mtintegrator)
8585
jump2.affect!(integrator)
@@ -107,7 +107,6 @@ m2 = getmean(jprob,Nsims)
107107
# test JumpSystem solution agrees with direct version
108108
@test abs(m-m2)/m < .01
109109

110-
111110
# mass action jump tests for SIR model
112111
maj1 = MassActionJump(2*β/2, [S => 1, I => 1], [S => -1, I => 1])
113112
maj2 = MassActionJump(γ, [I => 1], [I => -1, R => 1])
@@ -130,7 +129,7 @@ jprob = JumpProblem(js4, dprob, Direct())
130129
m4 = getmean(jprob,Nsims)
131130
@test abs(m4 - 2.0/.01)*.01/2.0 < .01
132131

133-
# test second order rx runs
132+
# test second order rx runs
134133
maj1 = MassActionJump(2.0, [0 => 1], [S => 1])
135134
maj2 = MassActionJump(γ, [S => 2], [S => -1])
136135
js4 = JumpSystem([maj1,maj2], t, [S], [β,γ])

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ using SafeTestsets, Test
1212
@safetestset "NonlinearSystem Test" begin include("nonlinearsystem.jl") end
1313
@safetestset "OptimizationSystem Test" begin include("optimizationsystem.jl") end
1414
@safetestset "ReactionSystem Test" begin include("reactionsystem.jl") end
15+
@safetestset "JumpSystem Test" begin include("jumpsystem.jl") end
1516
@safetestset "Build Targets Test" begin include("build_targets.jl") end
1617
@safetestset "Domain Test" begin include("domains.jl") end
1718
@safetestset "Constraints Test" begin include("constraints.jl") end

0 commit comments

Comments
 (0)