Skip to content

Commit e610231

Browse files
authored
Merge pull request #1022 from isaacsas/dont-drop-rx-odes-by-default
Have ODEs from reactions include zero ODEs by default
2 parents 8a060dc + 3826da6 commit e610231

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

src/systems/reaction/reactionsystem.jl

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ end
185185
"""
186186
oderatelaw(rx; combinatoric_ratelaw=true)
187187
188-
Given a [`Reaction`](@ref), return the reaction rate law [`Operation`](@ref) used in
188+
Given a [`Reaction`](@ref), return the symbolic reaction rate law used in
189189
generated ODEs for the reaction. Note, for a reaction defined by
190190
191191
`k*X*Y, X+Z --> 2X + Y`
@@ -195,7 +195,7 @@ of the form
195195
196196
`k, 2X+3Y --> Z`
197197
198-
the `Operation` that is returned will be `k * (X(t)^2/2) * (Y(t)^3/6)`.
198+
the expression that is returned will be `k * (X(t)^2/2) * (Y(t)^3/6)`.
199199
200200
Notes:
201201
- Allocates
@@ -240,13 +240,13 @@ function assemble_oderhs(rs; combinatoric_ratelaws=true)
240240
rhsvec
241241
end
242242

243-
function assemble_drift(rs; combinatoric_ratelaws=true, as_odes=true)
243+
function assemble_drift(rs; combinatoric_ratelaws=true, as_odes=true, include_zero_odes=true)
244244
rhsvec = assemble_oderhs(rs; combinatoric_ratelaws=combinatoric_ratelaws)
245245
if as_odes
246246
D = Differential(get_iv(rs))
247-
eqs = [Equation(D(x),rhs) for (x,rhs) in zip(get_states(rs),rhsvec) if (!_iszero(rhs))]
247+
eqs = [Equation(D(x),rhs) for (x,rhs) in zip(get_states(rs),rhsvec) if (include_zero_odes || (!_iszero(rhs)))]
248248
else
249-
eqs = [Equation(0,rhs) for rhs in rhsvec if (!_iszero(rhs))]
249+
eqs = [Equation(0,rhs) for rhs in rhsvec if (include_zero_odes || (!_iszero(rhs)))]
250250
end
251251
eqs
252252
end
@@ -272,7 +272,7 @@ end
272272
"""
273273
jumpratelaw(rx; rxvars=get_variables(rx.rate), combinatoric_ratelaw=true)
274274
275-
Given a [`Reaction`](@ref), return the reaction rate law [`Operation`](@ref) used in
275+
Given a [`Reaction`](@ref), return the symbolic reaction rate law used in
276276
generated stochastic chemical kinetics model SSAs for the reaction. Note,
277277
for a reaction defined by
278278
@@ -283,7 +283,7 @@ the form
283283
284284
`k, 2X+3Y --> Z`
285285
286-
the `Operation` that is returned will be `k * binomial(X,2) *
286+
the expression that is returned will be `k * binomial(X,2) *
287287
binomial(Y,3)`.
288288
289289
Notes:
@@ -411,8 +411,8 @@ law, i.e. for `2S -> 0` at rate `k` the ratelaw would be `k*S^2/2!`. If
411411
ignored.
412412
"""
413413
function Base.convert(::Type{<:ODESystem}, rs::ReactionSystem;
414-
name=nameof(rs), combinatoric_ratelaws=true, kwargs...)
415-
eqs = assemble_drift(rs; combinatoric_ratelaws=combinatoric_ratelaws)
414+
name=nameof(rs), combinatoric_ratelaws=true, include_zero_odes=true, kwargs...)
415+
eqs = assemble_drift(rs; combinatoric_ratelaws=combinatoric_ratelaws, include_zero_odes=include_zero_odes)
416416
systems = map(sys -> (sys isa ODESystem) ? sys : convert(ODESystem, sys), get_systems(rs))
417417
ODESystem(eqs, get_iv(rs), get_states(rs), get_ps(rs); name=name, systems=systems, kwargs...)
418418
end
@@ -431,8 +431,8 @@ law, i.e. for `2S -> 0` at rate `k` the ratelaw would be `k*S^2/2!`. If
431431
ignored.
432432
"""
433433
function Base.convert(::Type{<:NonlinearSystem},rs::ReactionSystem;
434-
name=nameof(rs), combinatoric_ratelaws=true, kwargs...)
435-
eqs = assemble_drift(rs; combinatoric_ratelaws=combinatoric_ratelaws, as_odes=false)
434+
name=nameof(rs), combinatoric_ratelaws=true, include_zero_odes=true, kwargs...)
435+
eqs = assemble_drift(rs; combinatoric_ratelaws=combinatoric_ratelaws, as_odes=false, include_zero_odes=include_zero_odes)
436436
systems = convert.(NonlinearSystem, get_systems(rs))
437437
NonlinearSystem(eqs, get_states(rs), get_ps(rs); name=name, systems=systems, kwargs...)
438438
end
@@ -449,17 +449,18 @@ Notes:
449449
law, i.e. for `2S -> 0` at rate `k` the ratelaw would be `k*S^2/2!`. If
450450
`combinatoric_ratelaws=false` then the ratelaw is `k*S^2`, i.e. the scaling factor is
451451
ignored.
452-
- `noise_scaling=nothing::Union{Vector{Operation},Operation,Nothing}` allows for linear
452+
- `noise_scaling=nothing::Union{Vector{Num},Num,Nothing}` allows for linear
453453
scaling of the noise in the chemical Langevin equations. If `nothing` is given, the default
454-
value as in Gillespie 2000 is used. Alternatively, an `Operation` can be given, this is
454+
value as in Gillespie 2000 is used. Alternatively, a `Num` can be given, this is
455455
added as a parameter to the system (at the end of the parameter array). All noise terms
456456
are linearly scaled with this value. The parameter may be one already declared in the `ReactionSystem`.
457-
Finally, a `Vector{Operation}` can be provided (the length must be equal to the number of reactions).
457+
Finally, a `Vector{Num}` can be provided (the length must be equal to the number of reactions).
458458
Here the noise for each reaction is scaled by the corresponding parameter in the input vector.
459459
This input may contain repeat parameters.
460460
"""
461461
function Base.convert(::Type{<:SDESystem}, rs::ReactionSystem;
462-
noise_scaling=nothing, name=nameof(rs), combinatoric_ratelaws=true, kwargs...)
462+
noise_scaling=nothing, name=nameof(rs), combinatoric_ratelaws=true,
463+
include_zero_odes=true, kwargs...)
463464

464465
if noise_scaling isa Vector
465466
(length(noise_scaling)!=length(equations(rs))) &&
@@ -470,7 +471,8 @@ function Base.convert(::Type{<:SDESystem}, rs::ReactionSystem;
470471
noise_scaling = fill(value(noise_scaling),length(equations(rs)))
471472
end
472473

473-
eqs = assemble_drift(rs; combinatoric_ratelaws=combinatoric_ratelaws)
474+
eqs = assemble_drift(rs; combinatoric_ratelaws=combinatoric_ratelaws,
475+
include_zero_odes=include_zero_odes)
474476
noiseeqs = assemble_diffusion(rs,noise_scaling;
475477
combinatoric_ratelaws=combinatoric_ratelaws)
476478
systems = convert.(SDESystem, get_systems(rs))

test/reactionsystem_components.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ pars = [α₀,α,K,n,δ,β,μ]
1717
@named rs = ReactionSystem(rxs, t, specs, pars)
1818

1919
# using ODESystem components
20-
@named os₁ = convert(ODESystem, rs)
21-
@named os₂ = convert(ODESystem, rs)
22-
@named os₃ = convert(ODESystem, rs)
20+
@named os₁ = convert(ODESystem, rs; include_zero_odes=false)
21+
@named os₂ = convert(ODESystem, rs; include_zero_odes=false)
22+
@named os₃ = convert(ODESystem, rs; include_zero_odes=false)
2323
connections = [os₁.R ~ os₃.P,
2424
os₂.R ~ os₁.P,
2525
os₃.R ~ os₂.P]

0 commit comments

Comments
 (0)