Skip to content

Commit 9fc98a6

Browse files
committed
update constraint tutorial
1 parent fa43296 commit 9fc98a6

File tree

1 file changed

+71
-35
lines changed

1 file changed

+71
-35
lines changed

docs/src/model_creation/constraint_equations.md

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [Constraint Equations and Events](@id constraint_equations)
1+
# [Coupled ODEs, Algebraic Equations, and Events](@id constraint_equations)
22
In many applications one has additional algebraic or differential equations for
33
non-chemical species that can be coupled to a chemical reaction network model.
44
Catalyst supports coupled differential and algebraic equations, and currently
@@ -8,24 +8,81 @@ condition is reached, such as providing a drug treatment at a specified time, or
88
turning off production of cells once the population reaches a given level.
99
Catalyst supports the event representation provided by ModelingToolkit, see
1010
[here](https://docs.sciml.ai/ModelingToolkit/stable/basics/Events/), allowing
11-
for both continuous and discrete events (though only the latter are supported
12-
when converting to `JumpSystem`s currently).
11+
for both continuous and discrete events.
12+
13+
In this tutorial we'll illustrate how to make use of coupled constraint (i.e.
14+
ODE/algebraic) equations and events. Let's consider a model of a cell with
15+
volume $V(t)$ that grows at a rate $\lambda$. For now we'll assume the cell can
16+
grow indefinitely. We'll also keep track of one protein $P(t)$, which is
17+
produced at a rate proportional $V$ and can be degraded.
18+
19+
## [Coupling ODE constraints via the DSL](@id constraint_equations_dsl)
20+
The easiest way to include ODEs and algebraic equations is to just include them
21+
when using the DSL to specify a model. Here we include an ODE for $V(t)$ along
22+
with degradation and production reactions for $P(t)$:
23+
```@example ceq1
24+
rn = @reaction_network dynamic_cell begin
25+
# the growth rate
26+
@parameters λ = 1.0
27+
28+
# assume there is no protein initially
29+
@species P(t) = 0.0
30+
31+
# set the initial volume to 1.0
32+
@variables V(t) = 1.0
33+
34+
# the reactions
35+
V, 0 --> P
36+
1.0, P --> 0
37+
38+
# the coupled ODE for V(t)
39+
@equations begin
40+
D(V) ~ λ * V
41+
end
42+
end
43+
```
44+
We can now create an `ODEProblem` from our model and solve it to see how $V(t)$
45+
and $P(t)$ evolve in time:
46+
```@example ceq1
47+
oprob = ODEProblem(growing_cell, [], (0.0, 1.0))
48+
sol = solve(oprob, Tsit5())
49+
plot(sol)
50+
```
51+
52+
## Coupling ODE constraints via directly building a `ReactionSystem`
53+
As an alternative to the previous approach, we could have also constructed our
54+
`ReactionSystem` all at once using the symbolic interface:
55+
```@example ceq2
56+
using Catalyst, OrdinaryDiffEqTsit5, Plots
57+
t = default_t()
58+
D = default_time_deriv()
59+
60+
@parameters λ = 1.0
61+
@variables V(t) = 1.0
62+
eq = D(V) ~ λ * V
63+
rx1 = @reaction $V, 0 --> P
64+
rx2 = @reaction 1.0, P --> 0
65+
@named growing_cell = ReactionSystem([rx1, rx2, eq], t)
66+
setdefaults!(growing_cell, [:P => 0.0])
67+
growing_cell = complete(growing_cell)
68+
69+
oprob = ODEProblem(growing_cell, [], (0.0, 1.0))
70+
sol = solve(oprob, Tsit5())
71+
plot(sol)
72+
```
1373

14-
In this tutorial we'll illustrate how to make use of constraint equations and
15-
events. Let's consider a model of a cell with volume $V(t)$ that grows at a rate
16-
$\lambda$. For now we'll assume the cell can grow indefinitely. We'll also keep
17-
track of one protein $P(t)$, which is produced at a rate proportional $V$ and
18-
can be degraded.
1974

2075
## [Coupling ODE constraints via extending a system](@id constraint_equations_coupling_constraints)
2176

22-
There are several ways we can create our Catalyst model with the two reactions
23-
and ODE for $V(t)$. One approach is to use compositional modeling, create
24-
separate `ReactionSystem`s and `ODESystem`s with their respective components,
25-
and then extend the `ReactionSystem` with the `ODESystem`. Let's begin by
26-
creating these two systems.
77+
Finally, we could also construct our model by using compositional modeling. Here
78+
we create separate `ReactionSystem`s and `ODESystem`s with their respective
79+
components, and then extend the `ReactionSystem` with the `ODESystem`. Let's
80+
begin by creating these two systems.
2781

28-
Here, to create differentials with respect to time (for our differential equations), we must import the time differential operator from Catalyst. We do this through `D = default_time_deriv()`. Here, `D(V)` denotes the differential of the variable `V` with respect to time.
82+
Here, to create differentials with respect to time (for our differential
83+
equations), we must import the time differential operator from Catalyst. We do
84+
this through `D = default_time_deriv()`. Here, `D(V)` denotes the differential
85+
of the variable `V` with respect to time.
2986

3087
```@example ceq1
3188
using Catalyst, OrdinaryDiffEqTsit5, Plots
@@ -72,27 +129,6 @@ sol = solve(oprob, Tsit5())
72129
plot(sol)
73130
```
74131

75-
## Coupling ODE constraints via directly building a `ReactionSystem`
76-
As an alternative to the previous approach, we could have constructed our
77-
`ReactionSystem` all at once by directly using the symbolic interface:
78-
```@example ceq2
79-
using Catalyst, OrdinaryDiffEqTsit5, Plots
80-
t = default_t()
81-
D = default_time_deriv()
82-
83-
@parameters λ = 1.0
84-
@variables V(t) = 1.0
85-
eq = D(V) ~ λ * V
86-
rx1 = @reaction $V, 0 --> P
87-
rx2 = @reaction 1.0, P --> 0
88-
@named growing_cell = ReactionSystem([rx1, rx2, eq], t)
89-
setdefaults!(growing_cell, [:P => 0.0])
90-
growing_cell = complete(growing_cell)
91-
92-
oprob = ODEProblem(growing_cell, [], (0.0, 1.0))
93-
sol = solve(oprob, Tsit5())
94-
plot(sol)
95-
```
96132

97133
## [Adding events](@id constraint_equations_events)
98134
Our current model is unrealistic in assuming the cell will grow exponentially

0 commit comments

Comments
 (0)