Skip to content

Commit b18c969

Browse files
Merge pull request #46 from Gaussia/regular_jumps
Regular jumps
2 parents c88da83 + 23cb534 commit b18c969

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/maketype.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ function maketype(name,
55
g,
66
g_func,
77
jumps,
8+
regular_jumps,
89
jump_rate_expr,
910
jump_affect_expr,
1011
p_matrix,
@@ -22,6 +23,7 @@ function maketype(name,
2223
g::Function
2324
g_func::Vector{Any}
2425
jumps::Tuple{AbstractJump,Vararg{AbstractJump}}
26+
regular_jumps::RegularJump
2527
jump_rate_expr::Tuple{Any,Vararg{Any}}
2628
jump_affect_expr::Tuple{Vector{Expr},Vararg{Vector{Expr}}}
2729
p_matrix::Array{Float64,2}
@@ -37,6 +39,7 @@ function maketype(name,
3739
$(Expr(:kw,:g,g)),
3840
$(Expr(:kw,:g_func,g_func)),
3941
$(Expr(:kw,:jumps,jumps)),
42+
$(Expr(:kw,:regular_jumps,regular_jumps)),
4043
$(Expr(:kw,:jump_rate_expr,jump_rate_expr)),
4144
$(Expr(:kw,:jump_affect_expr,jump_affect_expr)),
4245
$(Expr(:kw,:p_matrix,p_matrix)),
@@ -52,6 +55,7 @@ function maketype(name,
5255
g,
5356
g_func,
5457
jumps,
58+
regular_jumps,
5559
jump_rate_expr,
5660
jump_affect_expr,
5761
p_matrix,

src/reaction_network.jl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function coordinate(name, ex::Expr, p, scale_noise)
9292
g = make_func(g_expr, reactants, parameters)
9393
p_matrix = zeros(length(reactants), length(reactions))
9494

95-
(jump_rate_expr, jump_affect_expr, jumps) = get_jumps(reactions, reactants,parameters)
95+
(jump_rate_expr, jump_affect_expr, jumps, regular_jumps) = get_jumps(reactions, reactants, parameters)
9696

9797
f_rhs = [element.args[2] for element in f_expr]
9898
#symjac = Expr(:quote, calculate_jac(f_rhs, syms))
@@ -105,7 +105,7 @@ function coordinate(name, ex::Expr, p, scale_noise)
105105
f_funcs = [element.args[2] for element in f_expr]
106106
g_funcs = [element.args[2] for element in g_expr]
107107

108-
typeex,constructorex = maketype(name, f, f_funcs, f_symfuncs, g, g_funcs, jumps, Meta.quot(jump_rate_expr), Meta.quot(jump_affect_expr), p_matrix, syms; params=params, reactions=reactions)
108+
typeex,constructorex = maketype(name, f, f_funcs, f_symfuncs, g, g_funcs, jumps, regular_jumps, Meta.quot(jump_rate_expr), Meta.quot(jump_affect_expr), p_matrix, syms; params=params, reactions=reactions)
109109

110110
push!(exprs,typeex)
111111
push!(exprs,constructorex)
@@ -313,12 +313,15 @@ function get_jumps(reactions::Vector{ReactionStruct}, reactants::OrderedDict{Sym
313313
rates = Vector{Any}(length(reactions))
314314
affects = Vector{Vector{Expr}}(length(reactions))
315315
jumps = Expr(:tuple)
316+
reg_rates = Expr(:block)
317+
reg_c = Expr(:block)
316318
idx = 0
317319
for reaction in deepcopy(reactions)
318320
rates[idx += 1] = recursive_clean!(reaction.rate_SSA)
319321
affects[idx] = Vector{Expr}(0)
320-
foreach(prod -> push!(affects[idx],:(@inbounds integrator.u[$(reactants[prod.reactant])] += $(prod.stoichiometry))), reaction.products)
321-
foreach(sub -> push!(affects[idx],:(@inbounds integrator.u[$(reactants[sub.reactant])] -= $(sub.stoichiometry))), reaction.substrates)
322+
reactant_set = union(getfield.(reaction.products, :reactant),getfield.(reaction.substrates, :reactant))
323+
foreach(r -> push!(affects[idx],:(@inbounds integrator.u[$(reactants[r])] += $(get_stoch_diff(reaction,r)))), reactant_set)
324+
syntax_rate = recursive_replace!(deepcopy(rates[idx]), (reactants,:internal_var___u), (parameters, :internal_var___p))
322325
#if reaction.is_pure_mass_action
323326
# ma_sub_stoch = :(reactant_stoich = [[]])
324327
# ma_stoch_change = :(reactant_stoich = [[]])
@@ -327,11 +330,14 @@ function get_jumps(reactions::Vector{ReactionStruct}, reactants::OrderedDict{Sym
327330
# push!(jumps.args,:(MassActionJump($(reaction.rate_org),$(ma_sub_stoch),$(ma_stoch_change))))
328331
#else
329332
recursive_contains(:t,rates[idx]) ? push!(jumps.args,Expr(:call,:VariableRateJump)) : push!(jumps.args,Expr(:call,:ConstantRateJump))
330-
push!(jumps.args[idx].args, :((internal_var___u,internal_var___p,t) -> $(recursive_replace!(deepcopy(rates[idx]), (reactants,:internal_var___u), (parameters, :internal_var___p)))))
333+
push!(jumps.args[idx].args, :((internal_var___u,internal_var___p,t) -> $syntax_rate))
331334
push!(jumps.args[idx].args, :(integrator -> $(expr_arr_to_block(deepcopy(affects[idx])))))
332335
#end
336+
push!(reg_rates.args,:(internal_var___out[$idx]=$syntax_rate))
337+
foreach(r -> push!(reg_c.args,:(internal_var___dc[$(reactants[r]),$idx]=$(get_stoch_diff(reaction,r)))), reactant_set)
333338
end
334-
return (Tuple(rates),Tuple(affects),jumps)
339+
reg_jumps = :(RegularJump((internal_var___out,internal_var___u,internal_var___p,t)->$reg_rates,(internal_var___dc,internal_var___u,internal_var___p,t,internal_var___mark)->$reg_c,zeros($(length(reactants)),$(length(reactions)));constant_c=true))
340+
return (Tuple(rates),Tuple(affects),jumps,reg_jumps)
335341
end
336342

337343
#Recursively traverses an expression and removes things like X^1, 1*X. Will not actually have any affect on the expression when used as a function, but will make it much easier to look at it for debugging, as well as if it is transformed to LaTeX code.

0 commit comments

Comments
 (0)