1
1
# [ Programmatically Generating and Scripting ODESystems] (@id programmatically)
2
2
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 ` .
8
8
9
9
## The Representation of a ModelingToolkit System
10
10
11
11
ModelingToolkit is built on [ Symbolics.jl] ( https://symbolics.juliasymbolics.org/dev/ ) ,
12
12
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
14
14
calculations, and whatever else you can think of. Under the hood, all ModelingToolkit
15
15
variables and expressions are Symbolics.jl variables and expressions. Thus when scripting
16
16
a ModelingToolkit system, one simply needs to generate Symbolics.jl variables and equations
17
17
as demonstrated in the Symbolics.jl documentation. This looks like:
18
18
19
19
``` @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)
24
23
eqs = [D(x) ~ y
25
24
D(y) ~ x] # Define an array of equations
26
25
```
27
26
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
+
28
30
## The Non-DSL (non-` @mtkmodel ` ) Way of Defining an ODESystem
29
31
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 ` .
33
36
34
37
``` @example scripting
35
38
using ModelingToolkit
36
39
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
40
42
@constants h = 1 # constants
41
43
eqs = [D(x) ~ (h - x) / τ] # create an array of equations
42
44
@@ -45,10 +47,16 @@ eqs = [D(x) ~ (h - x) / τ] # create an array of equations
45
47
46
48
# Perform the standard transformations and mark the model complete
47
49
# 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)
49
57
```
50
58
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
52
60
and passing it to the ` ODESystem ` constructor.
53
61
54
62
## Understanding the Difference Between the Julia Variable and the Symbolic Variable
0 commit comments