Skip to content

Commit 85d8d10

Browse files
Merge pull request #3085 from ArnoStrouwen/docs3
Start rewriting programmatically generating systems docs
2 parents c0d1112 + 39a63f9 commit 85d8d10

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

docs/src/tutorials/programmatically_generating.md

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
11
# [Programmatically Generating and Scripting ODESystems](@id programmatically)
22

3-
In the following tutorial we will discuss how to programmatically generate `ODESystem`s.
4-
This is for cases where one is writing functions that generating `ODESystem`s, for example
5-
if implementing a reader which parses some file format to generate an `ODESystem` (for example,
6-
SBML), or for writing functions that transform an `ODESystem` (for example, if you write a
7-
function that log-transforms a variable in an `ODESystem`).
3+
In the following tutorial, we will discuss how to programmatically generate `ODESystem`s.
4+
This is useful for functions that generate `ODESystem`s, for example
5+
when you implement a reader that parses some file format, such as SBML, to generate an `ODESystem`.
6+
It is also useful for functions that transform an `ODESystem`, for example
7+
when you write a function that log-transforms a variable in an `ODESystem`.
88

99
## The Representation of a ModelingToolkit System
1010

1111
ModelingToolkit is built on [Symbolics.jl](https://symbolics.juliasymbolics.org/dev/),
1212
a symbolic Computer Algebra System (CAS) developed in Julia. As such, all CAS functionality
13-
is available on ModelingToolkit systems, such as symbolic differentiation, Groebner basis
13+
is also available to be used on ModelingToolkit systems, such as symbolic differentiation, Groebner basis
1414
calculations, and whatever else you can think of. Under the hood, all ModelingToolkit
1515
variables and expressions are Symbolics.jl variables and expressions. Thus when scripting
1616
a ModelingToolkit system, one simply needs to generate Symbolics.jl variables and equations
1717
as demonstrated in the Symbolics.jl documentation. This looks like:
1818

1919
```@example scripting
20-
using Symbolics
21-
using ModelingToolkit: t_nounits as t, D_nounits as D
22-
23-
@variables x(t) y(t) # Define variables
20+
using ModelingToolkit # reexports Symbolics
21+
@variables t x(t) y(t) # Define variables
22+
D = Differential(t)
2423
eqs = [D(x) ~ y
2524
D(y) ~ x] # Define an array of equations
2625
```
2726

27+
However, ModelingToolkit has many higher-level features which will make scripting ModelingToolkit systems more convenient.
28+
For example, as shown in the next section, defining your own independent variables and differentials is rarely needed.
29+
2830
## The Non-DSL (non-`@mtkmodel`) Way of Defining an ODESystem
2931

30-
Using `@mtkmodel` is the preferred way of defining ODEs with MTK. However, let us
31-
look at how we can define the same system without `@mtkmodel`. This is useful for
32-
defining PDESystem etc.
32+
Using `@mtkmodel`, like in the [getting started tutorial](@ref getting_started),
33+
is the preferred way of defining ODEs with MTK.
34+
However generating the contents of a `@mtkmodel` programmatically can be tedious.
35+
Let us look at how we can define the same system without `@mtkmodel`.
3336

3437
```@example scripting
3538
using ModelingToolkit
3639
using ModelingToolkit: t_nounits as t, D_nounits as D
37-
38-
@variables x(t) # independent and dependent variables
39-
@parameters τ # parameters
40+
@variables x(t) = 0.0 # independent and dependent variables
41+
@parameters τ = 3.0 # parameters
4042
@constants h = 1 # constants
4143
eqs = [D(x) ~ (h - x) / τ] # create an array of equations
4244
@@ -45,10 +47,16 @@ eqs = [D(x) ~ (h - x) / τ] # create an array of equations
4547
4648
# Perform the standard transformations and mark the model complete
4749
# Note: Complete models cannot be subsystems of other models!
48-
fol_model = structural_simplify(model)
50+
fol = structural_simplify(model)
51+
prob = ODEProblem(fol, [], (0.0, 10.0), [])
52+
using DifferentialEquations: solve
53+
sol = solve(prob)
54+
55+
using Plots
56+
plot(sol)
4957
```
5058

51-
As you can see, generating an ODESystem is as simple as creating the array of equations
59+
As you can see, generating an ODESystem is as simple as creating an array of equations
5260
and passing it to the `ODESystem` constructor.
5361

5462
## Understanding the Difference Between the Julia Variable and the Symbolic Variable

0 commit comments

Comments
 (0)