Skip to content

Commit

Permalink
Update to MTK v9
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisRackauckas committed Feb 27, 2024
1 parent b76b087 commit 752138b
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 41 deletions.
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ DataInterpolations = "4"
DifferentialEquations = "7"
Documenter = "1"
ForwardDiff = "0.10"
ModelingToolkit = "8"
ModelingToolkit = "9"
ModelingToolkitStandardLibrary = "2"
OrdinaryDiffEq = "6"
Plots = "1"
Expand Down
4 changes: 2 additions & 2 deletions docs/src/lectures/lecture1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ eqs = [
D(ẋ) ~
d*+ k*x^1.5 ~ F
]
@named odesys = ODESystem(eqs, t, vars, [])
@mtkbuild odesys = ODESystem(eqs, t, vars, [])
sys = structural_simplify(odesys)
prob = ODEProblem(sys, [], (0.0, 0.01))
sol = solve(prob; abstol=tol)
Expand Down Expand Up @@ -507,7 +507,7 @@ eqs =[
D(x) ~ (F - k*x^1.5)/d
]

@named odesys = ODESystem(eqs, t, vars, [])
@mtkbuild odesys = ODESystem(eqs, t, vars, [])

aesys = DAE2AE.dae_to_ae(odesys, Δt)
sys = structural_simplify(aesys)
Expand Down
8 changes: 3 additions & 5 deletions docs/src/lectures/lecture1.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ ModelingToolkit.jl uses symbolic math from Symbolics.jl to provide automatic ind

```@example l1
using ModelingToolkit
@variables t
D = Differential(t)
using ModelingToolkit: t_nounits as t, D_nounits as D
nothing # hide
```

Expand All @@ -213,8 +212,7 @@ nothing # hide
Note the variables are defined as a function of the independent variable `t` and given initial conditions which are captured in the variable `vars`. The equations are then defined using the tilde `~` operator, which represents the equation equality. This information is then fed to an `ODESystem` constructor and simplified using the `structural_simplify` function.

```@example l1
@named odesys = ODESystem(eqs, t, vars, pars)
sys = structural_simplify(odesys)
@mtkbuild odesys = ODESystem(eqs, t, vars, pars)
```

As can be seen, the 3 equation system is simplified down to 1 equation. To see the solved states and equations we can use the respective functions
Expand Down Expand Up @@ -586,7 +584,7 @@ eqs = [
D(dx) ~ ddx
m*ddx + d*dx + k*x ~ F
]
@named odesys = ODESystem(eqs, t, vars, pars)
@mtkbuild odesys = ODESystem(eqs, t, vars, pars)
sys = structural_simplify(odesys)
prob = ODEProblem(sys, [], (0,10))
sol = solve(prob)
Expand Down
9 changes: 3 additions & 6 deletions docs/src/lectures/lecture2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,19 @@ eqs_x = [
x ~ x_fun(t,amp,f)
]

@named odesys_x = ODESystem(eqs_x, t, vars, pars)
sys_x = structural_simplify(odesys_x)
@mtkbuild odesys_x = ODESystem(eqs_x, t, vars, pars)
prob_x = ODEProblem(sys_x, [], (0, t_end))
sol_x = solve(prob_x; saveat=time)

@named odesys_ṁ1 = ODESystem(eqs_ṁ1, t, vars, pars)
sys_ṁ1 = structural_simplify(odesys_ṁ1)
@mtkbuild odesys_ṁ1 = ODESystem(eqs_ṁ1, t, vars, pars)
u0 = [sol_x[s][1] for s in states(sys_ṁ1)]
prob_ṁ1 = ODEProblem(sys_ṁ1, u0, (0, t_end))
sol_ṁ1 = solve(prob_ṁ1)

plot(sol_ṁ1; idxs=ṁ, label="guess", ylabel="")
plot!(sol_x; idxs=ṁ, label="solution")

@named odesys_ṁ2 = ODESystem(eqs_ṁ2, t, vars, pars)
sys_ṁ2 = structural_simplify(odesys_ṁ2)
@mtkbuild odesys_ṁ2 = ODESystem(eqs_ṁ2, t, vars, pars)
prob_ṁ2 = ODEProblem(sys_ṁ2, u0, (0, t_end))
sol_ṁ2 = solve(prob_ṁ2)

Expand Down
13 changes: 4 additions & 9 deletions docs/src/lectures/lecture2.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ using ModelingToolkit
using DifferentialEquations
using Symbolics
using Plots
@parameters t
D = Differential(t)
using ModelingToolkit: t_nounits as t, D_nounits as D
# parameters -------
pars = @parameters begin
Expand Down Expand Up @@ -253,8 +251,7 @@ nothing # hide
Now we have 3 sets of equations, let's construct the systems and solve. If we start with case 3 with the target ``x`` input, notice that the `structural_simplify` step outputs a system with 0 equations!

```@example l2
@named odesys_x = ODESystem(eqs_x, t, vars, pars)
sys_x = structural_simplify(odesys_x)
@mtkbuild odesys_x = ODESystem(eqs_x, t, vars, pars)
nothing # hide
```

Expand Down Expand Up @@ -282,8 +279,7 @@ plot(sol_x; idxs=ṁ)
Now let's solve the other system and compare the results.

```@example l2
@named odesys_ṁ1 = ODESystem(eqs_ṁ1, t, vars, pars)
sys_ṁ1 = structural_simplify(odesys_ṁ1)
@mtkbuild odesys_ṁ1 = ODESystem(eqs_ṁ1, t, vars, pars)
nothing # hide
```

Expand All @@ -310,8 +306,7 @@ plot!(sol_x; idxs=ṁ, label="solution")
If we now solve for case 2, we can study the impact the compressibility derivation

```@example l2
@named odesys_ṁ2 = ODESystem(eqs_ṁ2, t, vars, pars)
sys_ṁ2 = structural_simplify(odesys_ṁ2)
@mtkbuild odesys_ṁ2 = ODESystem(eqs_ṁ2, t, vars, pars)
prob_ṁ2 = ODEProblem(sys_ṁ2, u0, (0, t_end))
@time sol_ṁ2 = solve(prob_ṁ2);
nothing # hide
Expand Down
2 changes: 1 addition & 1 deletion docs/src/lectures/lecture6.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The pendulum problem as described above is derived assuming the following:
If we attempt to solve this system we can see that it only solves up to the point that `x` crosses 0.

```@example l6
sys = structural_simplify(pendulum)
sys = complete(structural_simplify(pendulum))
prob = ODEProblem(sys, ModelingToolkit.missing_variable_defaults(sys), (0, 10))
sol = solve(prob)# gives retcode: DtLessThanMin
plot(sol; idxs=[x,y])
Expand Down
5 changes: 3 additions & 2 deletions docs/src/lectures/lecture7.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ are not enough constraints to uniquely determine ``x`` and ``y``. Let's see how
numerical solvers and ModelingToolkit behave.
```@example l7
using DifferentialEquations, Sundials, ModelingToolkit, Plots, LinearAlgebra
using ModelingToolkit: t_nounits as t, D_nounits as D
function f!(out, du, u, p, t)
# u[1]: x, du[1]: x'
Expand All @@ -32,8 +33,8 @@ step. We call such systems non-integrable or singular. Let's try to use
ModelingToolkit to analyze this problem.

```@example l7
@variables t x(t) y(t) z(t)
D = Differential(t)
@variables x(t) y(t) z(t)
eqs = [
x + y ~ sin(t)
z ~ sin(t)
Expand Down
27 changes: 12 additions & 15 deletions docs/src/lectures/lecture8.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ we cannot break the nonlinear system into smaller subsystems. Let's implement
the differentiated system and solve it numerically.
```@example l8
using ModelingToolkit, OrdinaryDiffEq, Plots, LinearAlgebra
using ModelingToolkit: t_nounits as t, D_nounits as D
function pend_manual(out, u, g, t)
ẋ, x, ẏ, y, λ = u
Expand Down Expand Up @@ -245,14 +246,14 @@ system. We can implement the same system in ModelingToolkit and see the same
behavior.
```@example l8
@parameters g
@variables t x(t) y(t) [state_priority = 10] λ(t)
D = Differential(t)
@variables x(t) y(t) [state_priority = 10] λ(t)
eqs = [
D(D(x)) ~ λ * x
D(D(y)) ~ λ * y - g
x^2 + y^2 ~ 1
]
@named pend = ODESystem(eqs)
@named pend = ODESystem(eqs,t)
pend = complete(pend)
ss = structural_simplify(pend)
prob_ir = ODEProblem(ss, [ModelingToolkit.missing_variable_defaults(ss); x => 1], (0.0, 25.0), [g => 1])
Expand All @@ -274,8 +275,7 @@ To save the hassle of running algorithms by hand, we can just implement the new
system in ModelingToolkit.
```@example l8
@parameters g
@variables t x(t) y(t) λ(t) θ(t) [state_priority = 10] T(t) V(t) E(t)
D = Differential(t)
@variables x(t) y(t) λ(t) θ(t) [state_priority = 10] T(t) V(t) E(t)
eqs = [
D(D(x)) ~ λ * x
D(D(y)) ~ λ * y - g
Expand All @@ -285,7 +285,7 @@ eqs = [
V ~ y * g
E ~ T + V
]
@named pend = ODESystem(eqs)
@named pend = ODESystem(eqs,t)
pend = complete(pend)
ss = structural_simplify(pend)
prob_ir = ODEProblem(ss, ModelingToolkit.missing_variable_defaults(ss), (0.0, 25.0), [g => 1])
Expand Down Expand Up @@ -313,9 +313,8 @@ balancing algorithm is available in the original dummy derivative paper

```@example l8
@parameters g
@variables t x1(t) x2(t) y1(t) y2(t) λ1(t) λ2(t) θ1(t) [state_priority = 10] θ2(t) [state_priority = 10]
@variables x1(t) x2(t) y1(t) y2(t) λ1(t) λ2(t) θ1(t) [state_priority = 10] θ2(t) [state_priority = 10]
@variables T(t) V(t) lx2(t) ly2(t)
D = Differential(t)
eqs = [
D(D(x1)) ~ λ1 * x1 - λ2 * lx2
D(D(y1)) ~ λ1 * y1 - λ2 * ly2 - g
Expand All @@ -331,7 +330,7 @@ eqs = [
V ~ y1 * g + y2 * g
]
@named pend = ODESystem(eqs)
@named pend = ODESystem(eqs,t)
pend = complete(pend)
ss = structural_simplify(pend)
prob_ir = ODEProblem(ss,
Expand Down Expand Up @@ -516,9 +515,7 @@ using ModelingToolkit, OrdinaryDiffEq, LinearAlgebra
import ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible as IC
import ModelingToolkitStandardLibrary.Blocks as B
import ModelingToolkitStandardLibrary.Mechanical.Translational as T

@parameters t
D = Differential(t)
using ModelingToolkit: t_nounits as t, D_nounits as D

function System(use_input, f; name)
@parameters t
Expand Down Expand Up @@ -630,10 +627,10 @@ end

```julia
using ModelingToolkit, OrdinaryDiffEq, Plots
using ModelingToolkit: t_nounits as t, D_nounits as D

@parameters g
@variables t x1(t) x2(t) y1(t) y2(t) λ1(t) λ2(t) θ1(t) [state_priority = 10] θ2(t) [state_priority = 10]
D = Differential(t)
@variables x1(t) x2(t) y1(t) y2(t) λ1(t) λ2(t) θ1(t) [state_priority = 10] θ2(t) [state_priority = 10]
eqs = [
D(D(x1)) ~ λ1 * x1,
D(D(y1)) ~ λ1 * y1 - g,
Expand All @@ -645,7 +642,7 @@ eqs = [
y2 ~ y1 + sin(θ2),
]

@named pend = ODESystem(eqs)
@named pend = ODESystem(eqs,t)
pend = complete(pend)
ss = structural_simplify(pend)
prob_ir = ODEProblem(ss,
Expand Down

0 comments on commit 752138b

Please sign in to comment.