Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,31 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
ModelingToolkitCourse = "d0ecabdd-fe99-481e-9814-e16fa9c541df"
ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739"
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
OrdinaryDiffEqBDF = "6ad6398a-0878-4a85-9266-38940aa047c8"
OrdinaryDiffEqNonlinearSolve = "127b3ac7-2247-4354-8eb6-78cf4e7c58e8"
OrdinaryDiffEqSDIRK = "2d112036-d095-4a1e-ab9a-08536f3ecdbf"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
SciCompDSL = "91a8cdf1-4ca6-467b-a780-87fda3fff15e"
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"

[compat]
DataInterpolations = "5, 6, 8.10, 9.0"
DifferentialEquations = "7, 8.0"
DifferentialEquations = "8"
Documenter = "1"
ForwardDiff = "0.10, 1.4"
ModelingToolkit = "9, 11.28"
ModelingToolkitStandardLibrary = "2"
OrdinaryDiffEq = "=6.74.1, 7.1"
ModelingToolkit = "11.28"
ModelingToolkitStandardLibrary = "2.29"
NonlinearSolve = "4"
OrdinaryDiffEq = "7.1"
OrdinaryDiffEqBDF = "2"
OrdinaryDiffEqNonlinearSolve = "2"
OrdinaryDiffEqSDIRK = "2"
Plots = "1"
SciCompDSL = "1"
Setfield = "1"
Sundials = "4, 6.2"
Symbolics = "5, 6, 7.28"
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Documenter, ModelingToolkitCourse
# NOTE: OrdinaryDiffEq limited to v6.74.1 because of bug https://github.com/SciML/OrdinaryDiffEq.jl/issues/2250

pages = [
"Home" => "index.md",
Expand All @@ -23,6 +22,7 @@ makedocs(
linkcheck_ignore = [
"https://epubs.siam.org/doi/10.1137/0903023",
"https://link.springer.com/book/10.1007/978-3-642-05221-7",
"https://www.mathworks.com/help/simscape/ref/variablehydraulicchamber.html",
"http://www.siam.org/journals/auth-info.php",
],
format = Documenter.HTML(
Expand Down
19 changes: 10 additions & 9 deletions docs/src/lectures/lecture1.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Then we can solve by specifying the method, in this case we specify `NewtonRaphs

```@example l1
using DifferentialEquations
using NonlinearSolve

p = xᵢ₋₁ = 0.0 # initial condition if i=2, x[1]=0
u0 = xᵢ = xᵢ₋₁ # guess value for x[i]
Expand Down Expand Up @@ -186,6 +187,7 @@ ModelingToolkit.jl uses symbolic math from Symbolics.jl to provide automatic ind
```@example l1
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
using SciCompDSL # provides the `@connector`/`@mtkmodel` component DSL
nothing # hide
```

Expand Down Expand Up @@ -238,9 +240,8 @@ Notice how the 2nd derivative term `ẍ(t)` has been automatically determined fr
We can now assemble a problem and solve it. The initial conditions do not need to be supplied here because the `sys` contains the variable defaults from `vars`. The solution object `sol` can now be indexed symbolically from any symbol of the system regardless if it's a solved variable, observable, or even a parameter. This way, if for example doing a batch of simulations, each respective solution object can easily retrieve all respective information about the simulation.

```@example l1
u0 = [] # <-- used to override defaults of ODESystem variables
p = [] # <-- used to override defaults of ODESystem parameters
prob = ODEProblem(odesys, u0, tspan, p)
op = [] # <-- symbolic map used to override defaults of ODESystem variables and parameters
prob = ODEProblem(odesys, op, tspan)
sol = solve(prob; abstol=tol)
plot(sol; idxs=ẍ, xlabel="time [s]", ylabel="ẍ [m/s^2]")
```
Expand Down Expand Up @@ -368,7 +369,7 @@ nothing # hide
Now the `Mass` and `Damper` components can be assembled in a system and connected together (note: the `connect` equation). Also note the parameters `v`, `m`, and `d` are defined to expose the properties which can be set as keyword arguments of the same name.

```@example l1
@mtkmodel System begin
@mtkmodel MassDamperSystem begin
@parameters begin
v
m
Expand All @@ -383,7 +384,7 @@ Now the `Mass` and `Damper` components can be assembled in a system and connecte
end
end

@mtkbuild sys = System(;v=100, m=5, d=3)
@mtkbuild sys = MassDamperSystem(;v=100, m=5, d=3)
nothing # hide
```

Expand Down Expand Up @@ -533,7 +534,7 @@ As with the `Reference` component, the force is a boundary condition and is leav
Now let's assemble a *mass-spring-damper* system with the full collection of components.

```@example l1
@mtkmodel System begin
@mtkmodel MassSpringDamperSystem begin
@parameters begin
v=0
x=0
Expand All @@ -555,7 +556,7 @@ Now let's assemble a *mass-spring-damper* system with the full collection of com
end
end

@mtkbuild sys = System()
@mtkbuild sys = MassSpringDamperSystem()
```

![mass-spring-damper](../img/System2.png)
Expand Down Expand Up @@ -638,7 +639,7 @@ nothing # hide
As an example, the `MassSpringDamper` component can be connected in series to make a complex system. One can imagine then how this enables easy construction of complex models that can be quickly modified, extremely useful for the application of model based design.

```@example l1
@mtkmodel System begin
@mtkmodel SeriesSystem begin
@parameters begin
v = 0
x = 0
Expand All @@ -658,7 +659,7 @@ As an example, the `MassSpringDamper` component can be connected in series to ma
end
end

@mtkbuild sys = System()
@mtkbuild sys = SeriesSystem()
nothing #hide
```

Expand Down
25 changes: 14 additions & 11 deletions docs/src/lectures/lecture2.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,9 @@ odesys_ṁ1
Notice that now, with a simple change of the system input variable, `structural_simplify()` outputs a system with 4 states to be solved. We can find the initial conditions needed for these states from `sol_x` and solve.

```@example l2
u0 = [sol_x[s][1] for s in unknowns(odesys_ṁ1)]
prob_ṁ1 = ODEProblem(odesys_ṁ1, u0, (0, t_end))
@time sol_ṁ1 = solve(prob_ṁ1; initializealg=NoInit());
u0 = [s => sol_x[s][1] for s in unknowns(odesys_ṁ1)]
prob_ṁ1 = ODEProblem(odesys_ṁ1, u0, (0, t_end); build_initializeprob = false)
@time sol_ṁ1 = solve(prob_ṁ1; initializealg=SciMLBase.NoInit());
nothing # hide
```

Expand All @@ -307,8 +307,8 @@ If we now solve for case 2, we can study the impact the compressibility derivati

```@example l2
@mtkbuild odesys_ṁ2 = ODESystem(eqs_ṁ2, t, vars, pars)
prob_ṁ2 = ODEProblem(odesys_ṁ2, u0, (0, t_end))
@time sol_ṁ2 = solve(prob_ṁ2; initializealg=NoInit());
prob_ṁ2 = ODEProblem(odesys_ṁ2, u0, (0, t_end); build_initializeprob = false)
@time sol_ṁ2 = solve(prob_ṁ2; initializealg=SciMLBase.NoInit());
nothing # hide
```

Expand All @@ -333,13 +333,13 @@ plot(time, (sol_ṁ1(time)[x] .- sol_ṁ2(time)[x])/1e-3,
Also note the difference in computation.

```@repl l2
sol_ṁ1.destats
sol_ṁ1.stats
```

As can be seen, including the detail of full compressibility resulted in more computation: more function evaluations, Jacobians, solves, and steps.

```@repl l2
sol_ṁ2.destats
sol_ṁ2.stats
```

### ModelingToolkitStandardLibrary.jl
Expand All @@ -350,6 +350,8 @@ import ModelingToolkitStandardLibrary.Mechanical.Translational as T
import ModelingToolkitStandardLibrary.Hydraulic.IsothermalCompressible as IC
import ModelingToolkitStandardLibrary.Blocks as B

include("volume.jl") # <-- moving-wall `Volume` component

using DataInterpolations
mass_flow_fun = LinearInterpolation(sol_x[ṁ], sol_x.t)

Expand All @@ -370,9 +372,9 @@ function MassVolume(; name, dx, drho, dm)
vars = []
systems = @named begin
fluid = IC.HydraulicFluid(; density = 876, bulk_modulus = 1.2e9)
mass = T.Mass(;v=dx,m=M,g=-g)
vol = IC.Volume(;area=A, x=x₀, p=p_int, dx, drho, dm)
mass_flow = IC.MassFlow(;p_int)
mass = T.Mass(;m=M,g=-g)
vol = Volume(;area=A, x=x₀, p=p_int, dx, drho, dm)
mass_flow = IC.MassFlow()
mass_flow_input = B.TimeVaryingFunction(;f = mass_flow_fun)
end

Expand All @@ -383,7 +385,8 @@ function MassVolume(; name, dx, drho, dm)
connect(mass_flow.port, fluid)
]

return ODESystem(eqs, t, vars, pars; systems, name)
return ODESystem(eqs, t, vars, pars; systems, name,
initial_conditions = [mass.v => dx, mass.s => x₀])
end

dx = sol_x[ẋ][1]
Expand Down
Loading