Skip to content

Commit 53397ae

Browse files
committed
Update docs
1 parent c10bc9e commit 53397ae

File tree

9 files changed

+74
-80
lines changed

9 files changed

+74
-80
lines changed

docs/src/basics/Composition.md

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ end
3232

3333
@parameters t
3434
D = Differential(t)
35-
@named connected = ODESystem([
35+
@named connected = compose(ODESystem([
3636
decay2.f ~ decay1.x
3737
D(decay1.f) ~ 0
38-
], t, systems=[decay1, decay2])
38+
], t), decay1, decay2)
3939

4040
equations(connected)
4141

@@ -81,7 +81,7 @@ subsystems. A model is the composition of itself and its subsystems.
8181
For example, if we have:
8282

8383
```julia
84-
@named sys = ODESystem(eqs,indepvar,states,ps,system=[subsys])
84+
@named sys = compose(ODESystem(eqs,indepvar,states,ps),subsys)
8585
```
8686

8787
the `equations` of `sys` is the concatenation of `get_eqs(sys)` and
@@ -191,7 +191,7 @@ N = S + I + R
191191
@named ieqn = ODESystem([D(I) ~ β*S*I/N-γ*I])
192192
@named reqn = ODESystem([D(R) ~ γ*I])
193193

194-
@named sir = ODESystem([
194+
@named sir = compose(ODESystem([
195195
S ~ ieqn.S,
196196
I ~ seqn.I,
197197
R ~ ieqn.R,
@@ -200,13 +200,12 @@ N = S + I + R
200200
seqn.R ~ reqn.R,
201201
ieqn.R ~ reqn.R,
202202
reqn.I ~ ieqn.I], t, [S,I,R], [β,γ],
203-
systems=[seqn,ieqn,reqn],
204-
default_p = [
203+
defaults = [
205204
seqn.β => β
206205
ieqn.β => β
207206
ieqn.γ => γ
208207
reqn.γ => γ
209-
])
208+
]), seqn, ieqn, reqn)
210209
```
211210

212211
Note that the states are forwarded by an equality relationship, while
@@ -241,14 +240,6 @@ sol = solve(prob,Tsit5())
241240
sol[reqn.R]
242241
```
243242

244-
However, one can similarly simplify this process of inheritance by
245-
using `combine` which concatenates all of the vectors within the
246-
systems. For example, we could equivalently have done:
247-
248-
```julia
249-
@named sir = combine([seqn,ieqn,reqn])
250-
```
251-
252243
## Tearing Problem Construction
253244

254245
Some system types, specifically `ODESystem` and `NonlinearSystem`, can be further
@@ -259,12 +250,3 @@ strongly connected components calculated during the process of simplification
259250
as the basis for building pre-simplified nonlinear systems in the implicit
260251
solving. In summary: these problems are structurally modified, but could be
261252
more efficient and more stable.
262-
263-
## Automatic Model Promotion (TODO)
264-
265-
In many cases one might want to compose models of different types. For example,
266-
one may want to include a `NonlinearSystem` as a set of algebraic equations
267-
within an `ODESystem`, or one may want to use an `ODESystem` as a subsystem of
268-
an `SDESystem`. In these cases, the compostion works automatically by promoting
269-
the model via `promote_system`. System promotions exist in the cases where a
270-
mathematically-trivial definition of the promotion exists.

docs/src/tutorials/acausal_components.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ end
3535
function Ground(;name)
3636
@named g = Pin()
3737
eqs = [g.v ~ 0]
38-
ODESystem(eqs, t, [], [], systems=[g]; name=name)
38+
compose(ODESystem(eqs, t, [], []; name=name), g)
3939
end
4040

4141
function OnePort(;name)
@@ -47,7 +47,7 @@ function OnePort(;name)
4747
0 ~ p.i + n.i
4848
i ~ p.i
4949
]
50-
ODESystem(eqs, t, sts, [], systems=[p, n]; name=name)
50+
compose(ODESystem(eqs, t, sts, []; name=name), p, n)
5151
end
5252

5353
function Resistor(;name, R = 1.0)
@@ -57,7 +57,7 @@ function Resistor(;name, R = 1.0)
5757
eqs = [
5858
v ~ i * R
5959
]
60-
extend(oneport, ODESystem(eqs, t, [], ps; name=name); name=name)
60+
extend(ODESystem(eqs, t, [], ps; name=name), oneport)
6161
end
6262

6363
function Capacitor(;name, C = 1.0)
@@ -68,7 +68,7 @@ function Capacitor(;name, C = 1.0)
6868
eqs = [
6969
D(v) ~ i / C
7070
]
71-
extend(oneport, ODESystem(eqs, t, [], ps; name=name); name=name)
71+
extend(ODESystem(eqs, t, [], ps; name=name), oneport)
7272
end
7373

7474
function ConstantVoltage(;name, V = 1.0)
@@ -78,7 +78,7 @@ function ConstantVoltage(;name, V = 1.0)
7878
eqs = [
7979
V ~ v
8080
]
81-
extend(oneport, ODESystem(eqs, t, [], ps; name=name); name=name)
81+
extend(ODESystem(eqs, t, [], ps; name=name), oneport)
8282
end
8383

8484
R = 1.0
@@ -153,7 +153,7 @@ that the voltage in such a `Pin` is equal to zero. This gives:
153153
function Ground(;name)
154154
@named g = Pin()
155155
eqs = [g.v ~ 0]
156-
ODESystem(eqs, t, [], [], systems=[g]; name=name)
156+
compose(ODESystem(eqs, t, [], []; name=name), g)
157157
end
158158
```
159159

@@ -173,7 +173,7 @@ function OnePort(;name)
173173
0 ~ p.i + n.i
174174
i ~ p.i
175175
]
176-
ODESystem(eqs, t, sts, [], systems=[p, n]; name=name)
176+
compose(ODESystem(eqs, t, sts, []; name=name), p, n)
177177
end
178178
```
179179

@@ -192,7 +192,7 @@ function Resistor(;name, R = 1.0)
192192
eqs = [
193193
v ~ i * R
194194
]
195-
extend(oneport, ODESystem(eqs, t, [], ps; name=name); name=name)
195+
extend(ODESystem(eqs, t, [], ps; name=name, oneport)
196196
end
197197
```
198198
@@ -216,7 +216,7 @@ function Capacitor(;name, C = 1.0)
216216
eqs = [
217217
D(v) ~ i / C
218218
]
219-
extend(oneport, ODESystem(eqs, t, [], ps; name=name); name=name)
219+
extend(ODESystem(eqs, t, [], ps; name=name), oneport)
220220
end
221221
```
222222
@@ -233,7 +233,7 @@ function ConstantVoltage(;name, V = 1.0)
233233
eqs = [
234234
V ~ v
235235
]
236-
extend(oneport, ODESystem(eqs, t, [], ps; name=name); name=name)
236+
extend(ODESystem(eqs, t, [], ps; name=name), oneport)
237237
end
238238
```
239239

docs/src/tutorials/ode_modeling.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ matches the name in the REPL. If omitted, you can directly set the `name` keywor
7171
After construction of the ODE, you can solve it using [DifferentialEquations.jl](https://diffeq.sciml.ai/):
7272

7373
```julia
74-
using DifferentialEquations: solve
75-
using Plots: plot
74+
using DifferentialEquations
75+
using Plots
7676

7777
prob = ODEProblem(fol_model, [x => 0.0], (0.0,10.0), [τ => 3.0])
78-
solve(prob) |> plot
78+
plot(solve(prob))
7979
```
8080

8181
![Simulation result of first-order lag element](https://user-images.githubusercontent.com/13935112/111958369-703f2200-8aed-11eb-8bb4-0abe9652e850.png)
@@ -211,7 +211,7 @@ again are just algebraic relations:
211211
connections = [ fol_1.f ~ 1.5,
212212
fol_2.f ~ fol_1.x ]
213213

214-
@named connected = ODESystem(connections; systems=[fol_1,fol_2])
214+
@named connected = compose(ODESystem(connections), fol_1, fol_2)
215215
# Model connected with 5 equations
216216
# States (5):
217217
# fol_1₊f(t)
@@ -267,7 +267,7 @@ p = [ fol_1.τ => 2.0,
267267
fol_2.τ => 4.0 ]
268268

269269
prob = ODEProblem(connected_simp, u0, (0.0,10.0), p)
270-
solve(prob) |> plot
270+
plot(solve(prob))
271271
```
272272

273273
![Simulation of connected system (two first-order lag elements in series)](https://user-images.githubusercontent.com/13935112/111958439-877e0f80-8aed-11eb-9074-9d35458459a4.png)
@@ -308,9 +308,8 @@ still is the problem using the `connected_simp` system above):
308308

309309
```julia
310310
using BenchmarkTools
311-
using DifferentialEquations: Rodas4
312311

313-
@btime solve(prob, Rodas4());
312+
@btime solve($prob, Rodas4());
314313
# 251.300 μs (873 allocations: 31.18 KiB)
315314
```
316315

@@ -320,7 +319,7 @@ be specified during the construction of the `ODEProblem`:
320319
```julia
321320
prob_an = ODEProblem(connected_simp, u0, (0.0,10.0), p; jac=true, sparse=true)
322321

323-
@btime solve(prob_an, Rodas4());
322+
@btime solve($prob_an, Rodas4());
324323
# 142.899 μs (1297 allocations: 83.96 KiB)
325324
```
326325

docs/src/tutorials/tearing_parallelism.md

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,88 +39,79 @@ end
3939
@parameters t
4040
const D = Differential(t)
4141
function Pin(;name)
42-
@variables v(t) i(t)
43-
ODESystem(Equation[], t, [v, i], [], name=name, defaults=Dict([v=>1.0, i=>1.0]))
42+
@variables v(t)=1.0 i(t)=1.0
43+
ODESystem(Equation[], t, [v, i], [], name=name)
4444
end
4545

4646
function Ground(;name)
4747
@named g = Pin()
4848
eqs = [g.v ~ 0]
49-
ODESystem(eqs, t, [], [], systems=[g], name=name)
49+
compose(ODESystem(eqs, t, [], [], name=name), g)
5050
end
5151

5252
function ConstantVoltage(;name, V = 1.0)
5353
val = V
5454
@named p = Pin()
5555
@named n = Pin()
56-
@parameters V
56+
@parameters V=V
5757
eqs = [
5858
V ~ p.v - n.v
5959
0 ~ p.i + n.i
6060
]
61-
ODESystem(eqs, t, [], [V], systems=[p, n], defaults=Dict(V => val), name=name)
61+
compose(ODESystem(eqs, t, [], [V], name=name), p, n)
6262
end
6363

6464
function HeatPort(;name)
65-
@variables T(t) Q_flow(t)
66-
return ODESystem(Equation[], t, [T, Q_flow], [], defaults=Dict(T=>293.15, Q_flow=>0.0), name=name)
65+
@variables T(t)=293.15 Q_flow(t)=0.0
66+
ODESystem(Equation[], t, [T, Q_flow], [], name=name)
6767
end
6868

6969
function HeatingResistor(;name, R=1.0, TAmbient=293.15, alpha=1.0)
70-
R_val, TAmbient_val, alpha_val = R, TAmbient, alpha
7170
@named p = Pin()
7271
@named n = Pin()
7372
@named h = HeatPort()
7473
@variables v(t) RTherm(t)
75-
@parameters R TAmbient alpha
74+
@parameters R=R TAmbient=TAmbient alpha=alpha
7675
eqs = [
7776
RTherm ~ R*(1 + alpha*(h.T - TAmbient))
7877
v ~ p.i * RTherm
7978
h.Q_flow ~ -v * p.i # -LossPower
8079
v ~ p.v - n.v
8180
0 ~ p.i + n.i
8281
]
83-
ODESystem(
84-
eqs, t, [v, RTherm], [R, TAmbient, alpha], systems=[p, n, h],
85-
defaults=Dict(
86-
R=>R_val, TAmbient=>TAmbient_val, alpha=>alpha_val,
87-
v=>0.0, RTherm=>R_val
88-
),
82+
compose(ODESystem(
83+
eqs, t, [v, RTherm], [R, TAmbient, alpha],
8984
name=name,
90-
)
85+
), p, n, h)
9186
end
9287

9388
function HeatCapacitor(;name, rho=8050, V=1, cp=460, TAmbient=293.15)
94-
rho_val, V_val, cp_val = rho, V, cp
95-
@parameters rho V cp
89+
@parameters rho=rho V=V cp=cp
9690
C = rho*V*cp
9791
@named h = HeatPort()
9892
eqs = [
9993
D(h.T) ~ h.Q_flow / C
10094
]
101-
ODESystem(
102-
eqs, t, [], [rho, V, cp], systems=[h],
103-
defaults=Dict(rho=>rho_val, V=>V_val, cp=>cp_val),
95+
compose(ODESystem(
96+
eqs, t, [], [rho, V, cp],
10497
name=name,
105-
)
98+
), h)
10699
end
107100

108101
function Capacitor(;name, C = 1.0)
109-
val = C
110102
@named p = Pin()
111103
@named n = Pin()
112-
@variables v(t)
113-
@parameters C
104+
@variables v(t)=0.0
105+
@parameters C=C
114106
eqs = [
115107
v ~ p.v - n.v
116108
0 ~ p.i + n.i
117109
D(v) ~ p.i / C
118110
]
119-
ODESystem(
120-
eqs, t, [v], [C], systems=[p, n],
121-
defaults=Dict(v => 0.0, C => val),
111+
compose(ODESystem(
112+
eqs, t, [v], [C],
122113
name=name
123-
)
114+
), p, n)
124115
end
125116

126117
function rc_model(i; name, source, ground, R, C)
@@ -154,12 +145,12 @@ Rs = 10 .^range(0, stop=-4, length=N)
154145
Cs = 10 .^range(-3, stop=0, length=N)
155146
rc_systems = map(1:N) do i
156147
rc_model(i; name=:rc, source=source, ground=ground, R=Rs[i], C=Cs[i])
157-
end
158-
@variables E(t)
148+
end;
149+
@variables E(t)=0.0
159150
eqs = [
160151
D(E) ~ sum(((i, sys),)->getproperty(sys, Symbol(:resistor, i)).h.Q_flow, enumerate(rc_systems))
161152
]
162-
big_rc = ODESystem(eqs, t, [E], [], systems=rc_systems, defaults=Dict(E=>0.0))
153+
big_rc = compose([ODESystem(eqs, t, [E], []); rc_systems])
163154
```
164155

165156
Now let's say we want to expose a bit more parallelism via running tearing.

examples/electrical_components.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ end
2222
function Ground(;name)
2323
@named g = Pin()
2424
eqs = [g.v ~ 0]
25-
ODESystem(eqs, t, [], [], systems=[g]; name=name)
25+
compose(ODESystem(eqs, t, [], []; name=name), g)
2626
end
2727

2828
function OnePort(;name)
@@ -34,7 +34,7 @@ function OnePort(;name)
3434
0 ~ p.i + n.i
3535
i ~ p.i
3636
]
37-
ODESystem(eqs, t, sts, [], systems=[p, n]; name=name)
37+
compose(ODESystem(eqs, t, sts, []; name=name), p, n)
3838
end
3939

4040
function Resistor(;name, R = 1.0)

examples/rc_model.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ rc_eqs = [
1414
connect(capacitor.n, source.n, ground.g)
1515
]
1616

17-
@named rc_model = ODESystem(rc_eqs, t, systems=[resistor, capacitor, source, ground])
17+
@named rc_model = compose(ODESystem(rc_eqs, t), resistor, capacitor, source, ground)

examples/serial_inductor.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ eqs = [
1313
connect(source.n, inductor2.n, ground.g)
1414
]
1515

16-
@named ll_model = ODESystem(eqs, t, systems=[source, resistor, inductor1, inductor2, ground])
16+
@named ll_model = compose(ODESystem(eqs, t), source, resistor, inductor1, inductor2, ground)

src/ModelingToolkit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,6 @@ export simplify, substitute
190190
export build_function
191191
export modelingtoolkitize
192192
export @variables, @parameters
193-
export @named, @nonamespace, @namespace, extend
193+
export @named, @nonamespace, @namespace, extend, compose
194194

195195
end # module

0 commit comments

Comments
 (0)