Skip to content

Commit e035534

Browse files
authored
Merge pull request #69 from spcornelius/fix_ma_rateconsts
Allow mass action rate consts involving parameter expressions
2 parents 716f922 + 90d8f7a commit e035534

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/massaction_jump_utils.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function get_net_stoich(rs, specmap)
5252
end
5353

5454
# given a ReactionStruct and a species map construct a MassActionJump
55-
function make_majump(rs, specmap, ratemap, params)
55+
function make_majump(rs, specmap, ratemap, params, param_context)
5656
reactant_stoich = get_substrate_stoich(rs, specmap)
5757
net_stoich = get_net_stoich(rs, specmap)
5858
if isempty(net_stoich)
@@ -62,7 +62,8 @@ function make_majump(rs, specmap, ratemap, params)
6262
if typeof(rs.rate_org) == Symbol
6363
rateconst = params[ratemap[rs.rate_org]]
6464
elseif typeof(rs.rate_org) == Expr
65-
rateconst = eval(rs.rate_org)
65+
# eval in param_context, in case Expr depends on params
66+
rateconst = Base.eval(param_context, rs.rate_org)
6667
elseif typeof(rs.rate_org) <: Number
6768
rateconst = rs.rate_org
6869
else
@@ -79,9 +80,16 @@ function network_to_jumpset(rn, specmap, ratemap, params)
7980
majumpvec = Vector{typeof(empty_majump)}()
8081
cjumpvec = Vector{ConstantRateJump}()
8182

83+
# populate dummy module with params as local variables
84+
# (for eval-ing parameter expressions)
85+
param_context = Module()
86+
for (param, index) in ratemap
87+
Base.eval(param_context, :($param = $(params[index])))
88+
end
89+
8290
for (i,rs) in enumerate(rn.reactions)
8391
if rs.is_pure_mass_action
84-
push!(majumpvec, make_majump(rs, specmap, ratemap, params))
92+
push!(majumpvec, make_majump(rs, specmap, ratemap, params, param_context))
8593
else
8694
push!(cjumpvec, rn.jumps[i])
8795
end

test/mass_act_jump_tests.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,22 @@ for method in algs
113113
jump_prob = JumpProblem(prob, method, network)
114114
sol = solve(jump_prob,SSAStepper());
115115
end
116+
117+
# make sure problem instantiation works when mass action jumps
118+
# rate consts are a nontrivial expression on the params
119+
network = @reaction_network begin
120+
1, X --> 2*X
121+
1/K, 2X --> X
122+
end K
123+
p = [1000.]
124+
prob = DiscreteProblem([500.], (0.,100.), p)
125+
jump_prob = JumpProblem(prob, Direct(), network)
126+
127+
128+
# same as above
129+
network = @reaction_network begin
130+
p1*p2, X --> Y
131+
end p1 p2
132+
p = [1.,2.]
133+
prob = DiscreteProblem([10.,10.],(0.,10.), p)
134+
jump_prob = JumpProblem(prob, Direct(), network)

0 commit comments

Comments
 (0)