1111[ ![ SciML Code Style] ( https://img.shields.io/static/v1?label=code%20style&message=SciML&color=9558b2&labelColor=389826 )] ( https://github.com/SciML/SciMLStyle )
1212
1313ModelingToolkit.jl is a modeling framework for high-performance symbolic-numeric computation
14- in scientific computing and scientific machine learning.
14+ in scientific computing and scientific machine learning.
1515It allows for users to give a high-level description of a model for
1616symbolic preprocessing to analyze and enhance the model. ModelingToolkit can
1717automatically generate fast functions for model components like Jacobians
@@ -36,88 +36,95 @@ lower it to a first order system, symbolically generate the Jacobian function
3636for the numerical integrator, and solve it.
3737
3838``` julia
39- using OrdinaryDiffEqDefault, ModelingToolkit
39+ using ModelingToolkit
4040using ModelingToolkit: t_nounits as t, D_nounits as D
4141
42+ # Defines a ModelingToolkit `System` model.
4243@parameters σ ρ β
4344@variables x (t) y (t) z (t)
44-
45- eqs = [ D (D (x)) ~ σ * (y - x),
45+ eqs = [
46+ D (D (x)) ~ σ * (y - x),
4647 D (y) ~ x * (ρ - z) - y,
47- D (z) ~ x * y - β * z]
48-
48+ D (z) ~ x * y - β * z
49+ ]
4950@mtkcompile sys = System (eqs, t)
5051
51- u0 = [D (x) => 2.0 ,
52+ # Simulate the model for a specific condition (initial condition and parameter values).
53+ using OrdinaryDiffEqDefault
54+ sim_cond = [
55+ D (x) => 2.0 ,
5256 x => 1.0 ,
5357 y => 0.0 ,
54- z => 0.0 ]
55-
56- p = [σ => 28.0 ,
58+ z => 0.0 ,
59+ σ => 28.0 ,
5760 ρ => 10.0 ,
58- β => 8 / 3 ]
59-
60- tspan = ( 0.0 , 100.0 )
61- prob = ODEProblem (sys, u0, tspan, p, jac = true )
61+ β => 8 / 3
62+ ]
63+ tend = 100.0
64+ prob = ODEProblem (sys, sim_cond, tend; jac = true )
6265sol = solve (prob)
66+
67+ # Plot the solution in phase-space.
6368using Plots
6469plot (sol, idxs = (x, y))
6570```
6671
67- ![ Lorenz2] ( https://user-images.githubusercontent. com/1814174/79118645-744eb580-7d5c-11ea-9c37-13c4efd585ca.png )
72+ ![ Lorenz2] ( https://github. com/user-attachments/assets/e82fb2ce-97b7-4f56-b272-85653c88bdb3 )
6873
69- This automatically will have generated fast Jacobian functions, making
74+ This will have automatically generated fast Jacobian functions, making
7075it more optimized than directly building a function. In addition, we can then
7176use ModelingToolkit to compose multiple ODE subsystems. Now, let's define two
7277interacting Lorenz equations and simulate the resulting Differential-Algebraic
7378Equation (DAE):
7479
7580``` julia
76- using DifferentialEquations, ModelingToolkit
81+ using ModelingToolkit
7782using ModelingToolkit: t_nounits as t, D_nounits as D
7883
79- @parameters σ ρ β
80- @variables x (t) y (t) z (t)
81-
82- eqs = [D (x) ~ σ * (y - x),
84+ # Defines two lorenz system models.
85+ eqs = [
86+ D (x) ~ σ * (y - x),
8387 D (y) ~ x * (ρ - z) - y,
84- D (z) ~ x * y - β * z]
85-
88+ D (z) ~ x * y - β * z
89+ ]
8690@named lorenz1 = System (eqs, t)
8791@named lorenz2 = System (eqs, t)
8892
93+ # Connect the two models, creating a single model.
8994@variables a (t)
9095@parameters γ
9196connections = [0 ~ lorenz1. x + lorenz2. y + a * γ]
92- @mtkcompile connected = System (connections, t, systems = [lorenz1, lorenz2])
97+ @mtkcompile connected_lorenz = System (connections, t; systems = [lorenz1, lorenz2])
9398
94- u0 = [lorenz1. x => 1.0 ,
99+ # Simulate the model for a specific condition (initial condition and parameter values).
100+ using OrdinaryDiffEqDefault
101+ sim_cond = [
102+ lorenz1. x => 1.0 ,
95103 lorenz1. y => 0.0 ,
96104 lorenz1. z => 0.0 ,
97105 lorenz2. x => 0.0 ,
98- lorenz2. y => 1.0 ,
99106 lorenz2. z => 0.0 ,
100- a => 2.0 ]
101-
102- p = [lorenz1. σ => 10.0 ,
107+ a => 2.0 ,
108+ lorenz1. σ => 10.0 ,
103109 lorenz1. ρ => 28.0 ,
104110 lorenz1. β => 8 / 3 ,
105111 lorenz2. σ => 10.0 ,
106112 lorenz2. ρ => 28.0 ,
107113 lorenz2. β => 8 / 3 ,
108- γ => 2.0 ]
109-
110- tspan = ( 0.0 , 100.0 )
111- prob = ODEProblem (connected, u0, tspan, p )
114+ γ => 2.0
115+ ]
116+ tend = 100.0
117+ prob = ODEProblem (connected_lorenz, sim_cond, tend )
112118sol = solve (prob)
113119
120+ # Plot the solution in phase-space.
114121using Plots
115122plot (sol, idxs = (a, lorenz1. x, lorenz2. z))
116123```
117124
118- ![ ] ( https://user-images.githubusercontent. com/17304743/187790221-528046c3-dbdb-4853-b977-799596c147f3.png )
125+ ![ LorenzConnected ] ( https://github. com/user-attachments/assets/ef65d812-c10e-42c6-945d-e61515e3b6a1 )
119126
120- # Citation
127+ ## Citation
121128
122129If you use ModelingToolkit.jl in your research, please cite [ this paper] ( https://arxiv.org/abs/2103.05244 ) :
123130
0 commit comments