Skip to content

Commit 5960210

Browse files
committed
Merge branch 'master' into new_default_prob_update_tests
2 parents 1dbe4d0 + 3893ddc commit 5960210

File tree

12 files changed

+110
-355
lines changed

12 files changed

+110
-355
lines changed

docs/src/assets/long_ploting_times/model_creation/mm_kinetics.svg

Lines changed: 0 additions & 128 deletions
This file was deleted.

docs/src/assets/long_ploting_times/model_creation/sir_outbreaks.svg

Lines changed: 0 additions & 128 deletions
This file was deleted.

docs/src/assets/long_ploting_times/model_simulation/incomplete_brusselator_simulation.svg

Lines changed: 0 additions & 54 deletions
This file was deleted.

docs/src/introduction_to_catalyst/introduction_to_catalyst.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,12 @@ u₀map = [:m₁ => 0, :m₂ => 0, :m₃ => 0, :P₁ => 20, :P₂ => 0, :P₃ =>
191191
dprob = DiscreteProblem(repressilator, u₀map, tspan, pmap)
192192
193193
# now, we create a JumpProblem, and specify Gillespie's Direct Method as the solver:
194-
jprob = JumpProblem(repressilator, dprob, Direct(), save_positions=(false,false))
194+
jprob = JumpProblem(repressilator, dprob, Direct())
195195
196196
# now, let's solve and plot the jump process:
197-
sol = solve(jprob, SSAStepper(), saveat=10.)
197+
sol = solve(jprob, SSAStepper())
198198
plot(sol)
199+
plot(sol, plotdensity = 1000, fmt = :png) # hide
199200
```
200201

201202
We see that oscillations remain, but become much noisier. Note, in constructing

docs/src/model_creation/examples/basic_CRN_library.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,13 @@ using Plots
115115
oplt = plot(osol; title = "Reaction rate equation (ODE)")
116116
splt = plot(ssol; title = "Chemical Langevin equation (SDE)")
117117
jplt = plot(jsol; title = "Stochastic chemical kinetics (Jump)")
118-
plot(oplt, splt, jplt; lw = 2, size=(800,800), layout = (3,1))
118+
plot(oplt, splt, jplt; lw = 2, size=(800,800), layout = (3,1))
119+
oplt = plot(osol; title = "Reaction rate equation (ODE)", plotdensity = 1000, fmt = :png) # hide
120+
splt = plot(ssol; title = "Chemical Langevin equation (SDE)", plotdensity = 1000, fmt = :png) # hide
121+
jplt = plot(jsol; title = "Stochastic chemical kinetics (Jump)", plotdensity = 1000, fmt = :png) # hide
122+
plot(oplt, splt, jplt; lw = 2, size=(800,800), layout = (3,1), plotdensity = 1000, fmt = :png) # hide
119123
plot!(bottom_margin = 3Plots.Measures.mm) # hide
120-
nothing # hide
121124
```
122-
![MM Kinetics](../../assets/long_ploting_times/model_creation/mm_kinetics.svg)
123-
Note that, due to the large amounts of the species involved, the stochastic trajectories are very similar to the deterministic one.
124125

125126
## [SIR infection model](@id basic_CRN_library_sir)
126127
The [SIR model](https://en.wikipedia.org/wiki/Compartmental_models_in_epidemiology#The_SIR_model) is the simplest model of the spread of an infectious disease. While the real system is very different from the chemical and cellular processes typically modelled with CRNs, it (and several other epidemiological systems) can be modelled using the same CRN formalism. The SIR model consists of three species: susceptible ($S$), infected ($I$), and removed ($R$) individuals, and two reaction events: infection and recovery.
@@ -160,9 +161,11 @@ jplt1 = plot(jsol1; title = "Outbreak")
160161
jplt2 = plot(jsol2; title = "Outbreak")
161162
jplt3 = plot(jsol3; title = "No outbreak")
162163
plot(jplt1, jplt2, jplt3; lw = 3, size=(800,700), layout = (3,1))
163-
nothing # hide
164+
jplt1 = plot(jsol1; title = "Outbreak", plotdensity = 1000, fmt = :png) # hide
165+
jplt2 = plot(jsol2; title = "Outbreak", plotdensity = 1000, fmt = :png) # hide
166+
jplt3 = plot(jsol3; title = "No outbreak", plotdensity = 1000, fmt = :png) # hide
167+
plot(jplt1, jplt2, jplt3; lw = 3, size=(800,700), layout = (3,1), plotdensity = 1000, fmt = :png) # hide
164168
```
165-
![SIR Outbreak](../../assets/long_ploting_times/model_creation/sir_outbreaks.svg)
166169

167170
## [Chemical cross-coupling](@id basic_CRN_library_cc)
168171
In chemistry, [cross-coupling](https://en.wikipedia.org/wiki/Cross-coupling_reaction) is when a catalyst combines two substrates to form a product. In this example, the catalyst ($C$) first binds one substrate ($S₁$) to form an intermediary complex ($S₁C$). Next, the complex binds the second substrate ($S₂$) to form another complex ($CP$). Finally, the catalyst releases the now-formed product ($P$). This system is an extended version of the [Michaelis-Menten system presented earlier](@ref basic_CRN_library_mm).

docs/src/model_simulation/ode_simulation_performance.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ oprob = ODEProblem(brusselator, u0, tspan, ps)
3131
3232
sol1 = solve(oprob, Tsit5())
3333
plot(sol1)
34-
nothing # hide
34+
plot(sol1, plotdensity = 1000, fmt = :png) # hide
3535
```
36-
![Incomplete Brusselator Simulation](../assets/long_ploting_times/model_simulation/incomplete_brusselator_simulation.svg)
3736

3837
We get a warning, indicating that the simulation was terminated. Furthermore, the resulting plot ends at $t ≈ 12$, meaning that the simulation was not completed (as the simulation's endpoint is $t = 20$). Indeed, we can confirm this by checking the *return code* of the solution object:
3938
```@example ode_simulation_performance_1

ext/CatalystBifurcationKitExtension/bifurcation_kit_extension.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ function BK.BifurcationProblem(rs::ReactionSystem, u0_bif, ps, bif_par, args...;
2222

2323
# Creates NonlinearSystem.
2424
Catalyst.conservationlaw_errorcheck(rs, vcat(ps, u0))
25-
nsys = convert(NonlinearSystem, rs; remove_conserved = true, defaults = Dict(u0))
25+
nsys = convert(NonlinearSystem, rs; defaults = Dict(u0),
26+
remove_conserved = true, remove_conserved_warn = false)
2627
nsys = complete(nsys)
2728

2829
# Makes BifurcationProblem (this call goes through the ModelingToolkit-based BifurcationKit extension).

ext/CatalystHomotopyContinuationExtension/homotopy_continuation_extension.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ end
4949
# For a given reaction system, parameter values, and initial conditions, find the polynomial that HC solves to find steady states.
5050
function steady_state_polynomial(rs::ReactionSystem, ps, u0)
5151
rs = Catalyst.expand_registered_functions(rs)
52-
ns = complete(convert(NonlinearSystem, rs; remove_conserved = true))
52+
ns = complete(convert(NonlinearSystem, rs;
53+
remove_conserved = true, remove_conserved_warn = false))
5354
pre_varmap = [symmap_to_varmap(rs, u0)..., symmap_to_varmap(rs, ps)...]
5455
Catalyst.conservationlaw_errorcheck(rs, pre_varmap)
5556
p_vals = ModelingToolkit.varmap_to_vars(pre_varmap, parameters(ns);

ext/CatalystStructuralIdentifiabilityExtension/structural_identifiability_extension.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function make_osys(rs::ReactionSystem; remove_conserved = true)
167167
error("Identifiability should only be computed for complete systems. A ReactionSystem can be marked as complete using the `complete` function.")
168168
end
169169
rs = complete(Catalyst.expand_registered_functions(flatten(rs)))
170-
osys = complete(convert(ODESystem, rs; remove_conserved))
170+
osys = complete(convert(ODESystem, rs; remove_conserved, remove_conserved_warn = false))
171171
vars = [unknowns(rs); parameters(rs)]
172172

173173
# Computes equations for system conservation laws.

src/reactionsystem_conversions.jl

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,22 @@ diff_2_zero(expr) = (Symbolics.is_derivative(expr) ? 0 : expr)
450450

451451
COMPLETENESS_ERROR = "A ReactionSystem must be complete before it can be converted to other system types. A ReactionSystem can be marked as complete using the `complete` function."
452452

453+
# Used to, when required, display a warning about conservation law removeal and remake.
454+
function check_cons_warning(remove_conserved, remove_conserved_warn)
455+
(remove_conserved && remove_conserved_warn) || return
456+
@warn "You are creating a system or problem while eliminating conserved quantities. Please note,
457+
due to limitations / design choices in ModelingToolkit if you use the created system to
458+
create a problem (e.g. an `ODEProblem`), or are directly creating a problem, you *should not*
459+
modify that problem's initial conditions for species (e.g. using `remake`). Changing initial
460+
conditions must be done by creating a new Problem from your reaction system or the
461+
ModelingToolkit system you converted it into with the new initial condition map.
462+
Modification of parameter values is still possible, *except* for the modification of any
463+
conservation law constants ($CONSERVED_CONSTANT_SYMBOL), which is not possible. You might
464+
get this warning when creating a problem directly.
465+
466+
You can remove this warning by setting `remove_conserved_warn = false`."
467+
end
468+
453469
### System Conversions ###
454470

455471
"""
@@ -467,15 +483,20 @@ Keyword args and default values:
467483
- `remove_conserved=false`, if set to `true` will calculate conservation laws of the
468484
underlying set of reactions (ignoring constraint equations), and then apply them to reduce
469485
the number of equations.
486+
- `remove_conserved_warn = true`: If `true`, if also `remove_conserved = true`, there will be
487+
a warning regarding limitations of modifying problems generated from the created system.
470488
"""
471489
function Base.convert(::Type{<:ODESystem}, rs::ReactionSystem; name = nameof(rs),
472490
combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
473-
include_zero_odes = true, remove_conserved = false, checks = false,
474-
default_u0 = Dict(), default_p = Dict(),
491+
include_zero_odes = true, remove_conserved = false, remove_conserved_warn = true,
492+
checks = false, default_u0 = Dict(), default_p = Dict(),
475493
defaults = _merge(Dict(default_u0), Dict(default_p)),
476494
kwargs...)
495+
# Error checks.
477496
iscomplete(rs) || error(COMPLETENESS_ERROR)
478497
spatial_convert_err(rs::ReactionSystem, ODESystem)
498+
check_cons_warning(remove_conserved, remove_conserved_warn)
499+
479500
fullrs = Catalyst.flatten(rs)
480501
remove_conserved && conservationlaws(fullrs)
481502
ists, ispcs = get_indep_sts(fullrs, remove_conserved)
@@ -509,16 +530,19 @@ Keyword args and default values:
509530
- `remove_conserved=false`, if set to `true` will calculate conservation laws of the
510531
underlying set of reactions (ignoring constraint equations), and then apply them to reduce
511532
the number of equations.
533+
- `remove_conserved_warn = true`: If `true`, if also `remove_conserved = true`, there will be
534+
a warning regarding limitations of modifying problems generated from the created system.
512535
"""
513536
function Base.convert(::Type{<:NonlinearSystem}, rs::ReactionSystem; name = nameof(rs),
514537
combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
515538
include_zero_odes = true, remove_conserved = false, checks = false,
516-
default_u0 = Dict(), default_p = Dict(),
539+
remove_conserved_warn = true, default_u0 = Dict(), default_p = Dict(),
517540
defaults = _merge(Dict(default_u0), Dict(default_p)),
518541
all_differentials_permitted = false, kwargs...)
519542
# Error checks.
520543
iscomplete(rs) || error(COMPLETENESS_ERROR)
521544
spatial_convert_err(rs::ReactionSystem, NonlinearSystem)
545+
check_cons_warning(remove_conserved, remove_conserved_warn)
522546
if !isautonomous(rs)
523547
error("Attempting to convert a non-autonomous `ReactionSystem` (e.g. where some rate depend on $(get_iv(rs))) to a `NonlinearSystem`. This is not possible. if you are intending to compute system steady states, consider creating and solving a `SteadyStateProblem.")
524548
end
@@ -589,17 +613,19 @@ Notes:
589613
- `remove_conserved=false`, if set to `true` will calculate conservation laws of the
590614
underlying set of reactions (ignoring constraint equations), and then apply them to reduce
591615
the number of equations.
592-
- Does not currently support `ReactionSystem`s that include coupled algebraic or
593-
differential equations.
616+
- `remove_conserved_warn = true`: If `true`, if also `remove_conserved = true`, there will be
617+
a warning regarding limitations of modifying problems generated from the created system.
594618
"""
595619
function Base.convert(::Type{<:SDESystem}, rs::ReactionSystem;
596620
name = nameof(rs), combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
597621
include_zero_odes = true, checks = false, remove_conserved = false,
598-
default_u0 = Dict(), default_p = Dict(),
622+
remove_conserved_warn = true, default_u0 = Dict(), default_p = Dict(),
599623
defaults = _merge(Dict(default_u0), Dict(default_p)),
600624
kwargs...)
625+
# Error checks.
601626
iscomplete(rs) || error(COMPLETENESS_ERROR)
602627
spatial_convert_err(rs::ReactionSystem, SDESystem)
628+
check_cons_warning(remove_conserved, remove_conserved_warn)
603629

604630
flatrs = Catalyst.flatten(rs)
605631

@@ -651,7 +677,6 @@ function Base.convert(::Type{<:JumpSystem}, rs::ReactionSystem; name = nameof(rs
651677
kwargs...)
652678
iscomplete(rs) || error(COMPLETENESS_ERROR)
653679
spatial_convert_err(rs::ReactionSystem, JumpSystem)
654-
655680
(remove_conserved !== nothing) &&
656681
throw(ArgumentError("Catalyst does not support removing conserved species when converting to JumpSystems."))
657682

@@ -684,12 +709,12 @@ function DiffEqBase.ODEProblem(rs::ReactionSystem, u0, tspan,
684709
p = DiffEqBase.NullParameters(), args...;
685710
check_length = false, name = nameof(rs),
686711
combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
687-
include_zero_odes = true, remove_conserved = false,
712+
include_zero_odes = true, remove_conserved = false, remove_conserved_warn = true,
688713
checks = false, structural_simplify = false, kwargs...)
689714
u0map = symmap_to_varmap(rs, u0)
690715
pmap = symmap_to_varmap(rs, p)
691716
osys = convert(ODESystem, rs; name, combinatoric_ratelaws, include_zero_odes, checks,
692-
remove_conserved)
717+
remove_conserved, remove_conserved_warn)
693718

694719
# Handles potential differential algebraic equations (which requires `structural_simplify`).
695720
if structural_simplify
@@ -708,12 +733,12 @@ function DiffEqBase.NonlinearProblem(rs::ReactionSystem, u0,
708733
p = DiffEqBase.NullParameters(), args...;
709734
name = nameof(rs), include_zero_odes = true,
710735
combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
711-
remove_conserved = false, checks = false,
736+
remove_conserved = false, remove_conserved_warn = true, checks = false,
712737
check_length = false, all_differentials_permitted = false, kwargs...)
713738
u0map = symmap_to_varmap(rs, u0)
714739
pmap = symmap_to_varmap(rs, p)
715740
nlsys = convert(NonlinearSystem, rs; name, combinatoric_ratelaws, include_zero_odes,
716-
checks, all_differentials_permitted, remove_conserved)
741+
checks, all_differentials_permitted, remove_conserved, remove_conserved_warn)
717742
nlsys = complete(nlsys)
718743
return NonlinearProblem(nlsys, u0map, pmap, args...; check_length,
719744
kwargs...)
@@ -723,12 +748,12 @@ end
723748
function DiffEqBase.SDEProblem(rs::ReactionSystem, u0, tspan,
724749
p = DiffEqBase.NullParameters(), args...;
725750
name = nameof(rs), combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
726-
include_zero_odes = true, checks = false, check_length = false,
727-
remove_conserved = false, structural_simplify = false, kwargs...)
751+
include_zero_odes = true, checks = false, check_length = false, remove_conserved = false,
752+
remove_conserved_warn = true, structural_simplify = false, kwargs...)
728753
u0map = symmap_to_varmap(rs, u0)
729754
pmap = symmap_to_varmap(rs, p)
730755
sde_sys = convert(SDESystem, rs; name, combinatoric_ratelaws,
731-
include_zero_odes, checks, remove_conserved)
756+
include_zero_odes, checks, remove_conserved, remove_conserved_warn)
732757

733758
# Handles potential differential algebraic equations (which requires `structural_simplify`).
734759
if structural_simplify
@@ -772,12 +797,12 @@ function DiffEqBase.SteadyStateProblem(rs::ReactionSystem, u0,
772797
p = DiffEqBase.NullParameters(), args...;
773798
check_length = false, name = nameof(rs),
774799
combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
775-
remove_conserved = false, include_zero_odes = true,
800+
remove_conserved = false, remove_conserved_warn = true, include_zero_odes = true,
776801
checks = false, structural_simplify = false, kwargs...)
777802
u0map = symmap_to_varmap(rs, u0)
778803
pmap = symmap_to_varmap(rs, p)
779804
osys = convert(ODESystem, rs; name, combinatoric_ratelaws, include_zero_odes, checks,
780-
remove_conserved)
805+
remove_conserved, remove_conserved_warn)
781806

782807
# Handles potential differential algebraic equations (which requires `structural_simplify`).
783808
if structural_simplify

0 commit comments

Comments
 (0)