-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathcomponents.jl
408 lines (368 loc) · 13.1 KB
/
components.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
using Test
using ModelingToolkit, OrdinaryDiffEq
using ModelingToolkit: get_component_type
using ModelingToolkit.BipartiteGraphs
using ModelingToolkit.StructuralTransformations
using ModelingToolkit: t_nounits as t, D_nounits as D
include("../examples/rc_model.jl")
function check_contract(sys)
state = ModelingToolkit.get_tearing_state(sys)
graph = state.structure.graph
fullvars = state.fullvars
sys = tearing_substitution(sys)
eqs = equations(sys)
for (i, eq) in enumerate(eqs)
actual = union(ModelingToolkit.vars(eq.lhs), ModelingToolkit.vars(eq.rhs))
actual = filter(!ModelingToolkit.isparameter, collect(actual))
current = Set(fullvars[𝑠neighbors(graph, i)])
@test isempty(setdiff(actual, current))
end
end
function check_rc_sol(sol)
rpi = sol[rc_model.resistor.p.i]
rpifun = sol.prob.f.observed(rc_model.resistor.p.i)
@test rpifun.(sol.u, (sol.prob.p,), sol.t) == rpi
@test any(!isequal(rpi[1]), rpi) # test that we don't have a constant system
@test sol[rc_model.resistor.p.i] == sol[resistor.p.i] == sol[capacitor.p.i]
@test sol[rc_model.resistor.n.i] == sol[resistor.n.i] == -sol[capacitor.p.i]
@test sol[rc_model.capacitor.n.i] == sol[capacitor.n.i] == -sol[capacitor.p.i]
@test iszero(sol[rc_model.ground.g.i])
@test iszero(sol[rc_model.ground.g.v])
@test sol[rc_model.resistor.v] == sol[resistor.v] ==
sol[source.p.v] - sol[capacitor.p.v]
end
@named pin = Pin()
@test get_component_type(pin).name == :Pin
@test get_component_type(rc_model.resistor).name == :Resistor
completed_rc_model = complete(rc_model)
@test isequal(completed_rc_model.resistor.n.i, resistor.n.i)
@test ModelingToolkit.n_expanded_connection_equations(capacitor) == 2
@test length(equations(structural_simplify(rc_model, allow_parameter = false))) == 2
sys = structural_simplify(rc_model)
@test_throws ModelingToolkit.RepeatedStructuralSimplificationError structural_simplify(sys)
@test length(equations(sys)) == 1
check_contract(sys)
@test !isempty(ModelingToolkit.defaults(sys))
u0 = [capacitor.v => 0.0]
prob = ODEProblem(sys, u0, (0, 10.0))
sol = solve(prob, Rodas4())
check_rc_sol(sol)
# https://discourse.julialang.org/t/using-optimization-parameters-in-modelingtoolkit/82099
let
@parameters param_r1 param_c1
@named resistor = Resistor(R = param_r1)
@named capacitor = Capacitor(C = param_c1)
@named source = ConstantVoltage(V = 1.0)
@named ground = Ground()
rc_eqs = [connect(source.p, resistor.p)
connect(resistor.n, capacitor.p)
connect(capacitor.n, source.n)
connect(capacitor.n, ground.g)]
@named _rc_model = ODESystem(rc_eqs, t)
@named rc_model = compose(_rc_model,
[resistor, capacitor, source, ground])
sys = structural_simplify(rc_model)
u0 = [
capacitor.v => 0.0
]
params = [param_r1 => 1.0, param_c1 => 1.0]
tspan = (0.0, 10.0)
prob = ODEProblem(sys, u0, tspan, params)
@test solve(prob, Tsit5()).retcode == ReturnCode.Success
end
let
# 1478
@named resistor2 = Resistor(R = R)
rc_eqs2 = [connect(source.p, resistor.p)
connect(resistor.n, resistor2.p)
connect(resistor2.n, capacitor.p)
connect(capacitor.n, source.n)
connect(capacitor.n, ground.g)]
@named _rc_model2 = ODESystem(rc_eqs2, t)
@named rc_model2 = compose(_rc_model2,
[resistor, resistor2, capacitor, source, ground])
sys2 = structural_simplify(rc_model2)
prob2 = ODEProblem(sys2, [source.p.i => 0.0], (0, 10.0), guesses = u0)
sol2 = solve(prob2, Rosenbrock23())
@test sol2[source.p.i] ≈ sol2[rc_model2.source.p.i] ≈ -sol2[capacitor.i]
prob3 = ODEProblem(sys2, [], (0, 10.0), guesses = u0)
sol3 = solve(prob2, Rosenbrock23())
@test sol3[unknowns(rc_model2), end] ≈ sol2[unknowns(rc_model2), end]
end
# Outer/inner connections
function rc_component(; name, R = 1, C = 1)
@parameters R=R C=C
@named p = Pin()
@named n = Pin()
@named resistor = Resistor(R = R) # test parent scope default of @named
@named capacitor = Capacitor(C = ParentScope(C))
eqs = [connect(p, resistor.p);
connect(resistor.n, capacitor.p);
connect(capacitor.n, n)]
@named sys = ODESystem(eqs, t, [], [R, C])
compose(sys, [p, n, resistor, capacitor]; name = name)
end
@named ground = Ground()
@named source = ConstantVoltage(V = 1)
@named rc_comp = rc_component()
eqs = [connect(source.p, rc_comp.p)
connect(source.n, rc_comp.n)
connect(source.n, ground.g)]
@named sys′ = ODESystem(eqs, t)
@named sys_inner_outer = compose(sys′, [ground, source, rc_comp])
@test_nowarn show(IOBuffer(), MIME"text/plain"(), sys_inner_outer)
expand_connections(sys_inner_outer, debug = true)
sys_inner_outer = structural_simplify(sys_inner_outer)
@test !isempty(ModelingToolkit.defaults(sys_inner_outer))
u0 = [rc_comp.capacitor.v => 0.0]
prob = ODEProblem(sys_inner_outer, u0, (0, 10.0), sparse = true)
sol_inner_outer = solve(prob, Rodas4())
@test sol[capacitor.v] ≈ sol_inner_outer[rc_comp.capacitor.v]
u0 = [
capacitor.v => 0.0
]
prob = ODEProblem(sys, u0, (0, 10.0))
sol = solve(prob, Tsit5())
@test sol[resistor.p.i] == sol[capacitor.p.i]
@test sol[resistor.n.i] == -sol[capacitor.p.i]
@test sol[capacitor.n.i] == -sol[capacitor.p.i]
@test iszero(sol[ground.g.i])
@test iszero(sol[ground.g.v])
@test sol[resistor.v] == sol[source.p.v] - sol[capacitor.p.v]
#using Plots
#plot(sol)
include("../examples/serial_inductor.jl")
sys = structural_simplify(ll_model)
@test length(equations(sys)) == 2
u0 = unknowns(sys) .=> 0
@test_nowarn ODEProblem(
sys, [], (0, 10.0), guesses = u0, warn_initialize_determined = false)
prob = DAEProblem(sys, D.(unknowns(sys)) .=> 0, [], (0, 0.5), guesses = u0)
sol = solve(prob, DFBDF())
@test sol.retcode == SciMLBase.ReturnCode.Success
sys2 = structural_simplify(ll2_model)
@test length(equations(sys2)) == 3
u0 = unknowns(sys) .=> 0
prob = ODEProblem(sys, u0, (0, 10.0))
@test_nowarn sol = solve(prob, FBDF())
@variables x1(t) x2(t) x3(t) x4(t)
@named sys1_inner = ODESystem([D(x1) ~ x1], t)
@named sys1_partial = compose(ODESystem([D(x2) ~ x2], t; name = :foo), sys1_inner)
@named sys1 = extend(ODESystem([D(x3) ~ x3], t; name = :foo), sys1_partial)
@named sys2 = compose(ODESystem([D(x4) ~ x4], t; name = :foo), sys1)
@test_nowarn sys2.sys1.sys1_inner.x1 # test the correct nesting
# compose tests
function record_fun(; name)
pars = @parameters a=10 b=100
ODESystem(Equation[], t, [], pars; name)
end
function first_model(; name)
@named foo = record_fun()
defs = Dict()
defs[foo.a] = 3
defs[foo.b] = 300
pars = @parameters x=2 y=20
compose(ODESystem(Equation[], t, [], pars; name, defaults = defs), foo)
end
@named goo = first_model()
@unpack foo = goo
@test ModelingToolkit.defaults(goo)[foo.a] == 3
@test ModelingToolkit.defaults(goo)[foo.b] == 300
#=
model Circuit
Ground ground;
Load load;
Resistor resistor;
equation
connect(load.p , ground.p);
connect(resistor.p, ground.p);
end Circuit;
model Load
extends TwoPin;
Resistor resistor;
equation
connect(p, resistor.p);
connect(resistor.n, n);
end Load;
=#
function Load(; name)
R = 1
@named p = Pin()
@named n = Pin()
@named resistor = Resistor(R = R)
eqs = [connect(p, resistor.p);
connect(resistor.n, n)]
@named sys = ODESystem(eqs, t)
compose(sys, [p, n, resistor]; name = name)
end
function Circuit(; name)
R = 1
@named ground = Ground()
@named load = Load()
@named resistor = Resistor(R = R)
eqs = [connect(load.p, ground.g);
connect(resistor.p, ground.g)]
@named sys = ODESystem(eqs, t)
compose(sys, [ground, resistor, load]; name = name)
end
@named foo = Circuit()
@test structural_simplify(foo) isa ModelingToolkit.AbstractSystem
# BLT tests
using LinearAlgebra
function parallel_rc_model(i; name, source, ground, R, C)
resistor = HeatingResistor(name = Symbol(:resistor, i), R = R)
capacitor = Capacitor(name = Symbol(:capacitor, i), C = C)
heat_capacitor = HeatCapacitor(name = Symbol(:heat_capacitor, i))
rc_eqs = [connect(source.p, resistor.p)
connect(resistor.n, capacitor.p)
connect(capacitor.n, source.n, ground.g)
connect(resistor.h, heat_capacitor.h)]
compose(ODESystem(rc_eqs, t, name = Symbol(name, i)),
[resistor, capacitor, source, ground, heat_capacitor])
end
V = 2.0
@named source = ConstantVoltage(V = V)
@named ground = Ground()
N = 50
Rs = 10 .^ range(0, stop = -4, length = N)
Cs = 10 .^ range(-3, stop = 0, length = N)
rc_systems = map(1:N) do i
parallel_rc_model(i; name = :rc, source = source, ground = ground, R = Rs[i], C = Cs[i])
end;
@variables E(t) = 0.0
eqs = [
D(E) ~ sum(((i, sys),) -> getproperty(sys, Symbol(:resistor, i)).h.Q_flow,
enumerate(rc_systems))
]
@named _big_rc = ODESystem(eqs, t, [E], [])
@named big_rc = compose(_big_rc, rc_systems)
ts = TearingState(expand_connections(big_rc))
@test istriu(but_ordered_incidence(ts)[1])
# Test using constants inside subsystems
function FixedResistor(; name, R = 1.0)
@named oneport = OnePort()
@unpack v, i = oneport
@constants R = R
eqs = [
v ~ i * R
]
extend(ODESystem(eqs, t, [], []; name = name), oneport)
end
capacitor = Capacitor(; name = :c1)
resistor = FixedResistor(; name = :r1)
ground = Ground(; name = :ground)
rc_eqs = [connect(capacitor.n, resistor.p)
connect(resistor.n, capacitor.p)
connect(capacitor.n, ground.g)]
@named _rc_model = ODESystem(rc_eqs, t)
@named rc_model = compose(_rc_model,
[resistor, capacitor, ground])
sys = structural_simplify(rc_model)
prob = ODEProblem(sys, u0, (0, 10.0))
sol = solve(prob, Tsit5())
@testset "docstrings (#1155)" begin
"""
Hey there, Pin1!
"""
@connector function Pin1(; name)
@independent_variables t
sts = @variables v(t)=1.0 i(t)=1.0
ODESystem(Equation[], t, sts, []; name = name)
end
@test string(Base.doc(Pin1)) == "Hey there, Pin1!\n"
"""
Hey there, Pin2!
"""
@component function Pin2(; name)
@independent_variables t
sts = @variables v(t)=1.0 i(t)=1.0
ODESystem(Equation[], t, sts, []; name = name)
end
@test string(Base.doc(Pin2)) == "Hey there, Pin2!\n"
end
@testset "Issue#3016 Hierarchical indexing" begin
@mtkmodel Inner begin
@parameters begin
p
end
end
@mtkmodel Outer begin
@components begin
inner = Inner()
end
@variables begin
x(t)
end
@equations begin
x ~ inner.p
end
end
@named outer = Outer()
simp = structural_simplify(outer)
@test sort(propertynames(outer)) == [:inner, :t, :x]
@test propertynames(simp) == propertynames(outer)
@test sort(propertynames(outer.inner)) == [:p, :t]
@test propertynames(simp.inner) == propertynames(outer.inner)
for sym in (:t, :x)
@test_nowarn getproperty(simp, sym)
@test_nowarn getproperty(outer, sym)
end
@test_nowarn simp.inner.p
@test_nowarn outer.inner.p
@test_throws ArgumentError simp.inner₊p
@test_throws ArgumentError outer.inner₊p
end
@testset "`getproperty` on `structural_simplify(complete(sys))`" begin
@mtkmodel Foo begin
@variables begin
x(t)
end
end
@mtkmodel Bar begin
@components begin
foo = Foo()
end
@equations begin
D(foo.x) ~ foo.x
end
end
@named bar = Bar()
cbar = complete(bar)
ss = structural_simplify(cbar)
@test isequal(cbar.foo.x, ss.foo.x)
end
@testset "Issue#3275: Metadata retained on `complete`" begin
@variables x(t) y(t)
@testset "ODESystem" begin
@named inner = ODESystem(D(x) ~ x, t)
@named outer = ODESystem(D(y) ~ y, t; systems = [inner], metadata = "test")
@test ModelingToolkit.get_metadata(outer) == "test"
sys = complete(outer)
@test ModelingToolkit.get_metadata(sys) == "test"
end
@testset "NonlinearSystem" begin
@named inner = NonlinearSystem([0 ~ x^2 + 4x + 4], [x], [])
@named outer = NonlinearSystem(
[0 ~ x^3 - y^3], [x, y], []; systems = [inner], metadata = "test")
@test ModelingToolkit.get_metadata(outer) == "test"
sys = complete(outer)
@test ModelingToolkit.get_metadata(sys) == "test"
end
k = ShiftIndex(t)
@testset "DiscreteSystem" begin
@named inner = DiscreteSystem([x(k) ~ x(k - 1) + x(k - 2)], t, [x], [])
@named outer = DiscreteSystem([y(k) ~ y(k - 1) + y(k - 2)], t, [x, y],
[]; systems = [inner], metadata = "test")
@test ModelingToolkit.get_metadata(outer) == "test"
sys = complete(outer)
@test ModelingToolkit.get_metadata(sys) == "test"
end
@testset "OptimizationSystem" begin
@named inner = OptimizationSystem(x^2 + y^2 - 3, [x, y], [])
@named outer = OptimizationSystem(
x^3 - y, [x, y], []; systems = [inner], metadata = "test")
@test ModelingToolkit.get_metadata(outer) == "test"
sys = complete(outer)
@test ModelingToolkit.get_metadata(sys) == "test"
end
end