Skip to content

Commit 47486fb

Browse files
committed
Add conservationlaw HC tutorial
1 parent d3d264b commit 47486fb

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

docs/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Catalyst = "479239e8-5488-4da2-87a7-35f2df7eef83"
33
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
44
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
55
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
6+
HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327"
67
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
78
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
89
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"

docs/src/tutorials/homotopy_continuation_tutorial.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,27 @@ using Catalyst
3737
two_state_model = @reaction_network begin
3838
(k1,k2), X1 <--> X2
3939
end k1 k2
40-
p = [:k1 => 2.0, :k2 => 1.0]
4140
```
42-
However, the conservation laws can be computed using the `conservationlaws` function. By supplying these, as well as fixed concentrations (in this case the total amount of *X*, that is *X1+X2*), steady states can be found. Tutorial for this is currently a WIP.
43-
44-
41+
However, the conservation laws can be computed using the `conservationlaws` function. By supplying these, as well as fixed concentrations (in this case the total amount of *X*, that is *X1+X2*), steady states can be found. First, we set the default values of the system's initial conditions and steady states. This will allow the system to automatically find the conserved amounts.
42+
```@example hc3
43+
setdefaults!(two_state_model, [:X1 => 1.0, :X2 => 1.0, :k1 => 2.0, :k2 => 1.0])
44+
```
45+
Next, we create a `NonlinearSystem`, while also removing the redundant equation.
46+
```@example hc3
47+
ns = convert(NonlinearSystem,two_state_model; remove_conserved=true)
48+
```
49+
Again, we will create the dictionary for parameter values that we will sub in. However, we will do it slightly differently so that the conserved quantitites are accoutned for.
50+
```@example hc3
51+
const MT = ModelingToolkit
52+
subs = Dict(MT.parameters(ns) .=> MT.varmap_to_vars([], MT.parameters(ns); defaults=MT.defaults(ns)))
53+
```
54+
We now extract the equation produced by the conservation law, and then sub in the parameter values creating a final set of equations (like previously). Unlike previously, we have to do `eq.rhs-eq.lhs`, as `cons_eq` may contain important information on both the lhs and rhs.
55+
```@example hc3
56+
cons_eq = conservedequations(two_state_model)
57+
new_eqs = map(eq -> substitute(eq.rhs-eq.lhs,subs), [equations(ns)...;cons_eq...])
58+
```
59+
Finally, we compute the solution:
60+
```@example hc3
61+
using HomotopyContinuation
62+
sols = real_solutions(as_polynomial((f, x...) -> HomotopyContinuation.solve(collect(x)), new_eqs...))
63+
```

0 commit comments

Comments
 (0)