forked from SciML/ModelingToolkit.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_parsing.jl
299 lines (265 loc) · 8.21 KB
/
model_parsing.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
using ModelingToolkit, Test
using ModelingToolkit: get_gui_metadata,
VariableDescription, getdefault, RegularConnector, get_ps
using URIs: URI
using Distributions
using Unitful
ENV["MTK_ICONS_DIR"] = "$(@__DIR__)/icons"
@testset "Comprehensive Test of Parsing Models (with an RC Circuit)" begin
@connector RealInput begin
u(t), [input = true, unit = u"V"]
end
@connector RealOutput begin
u(t), [output = true, unit = u"V"]
end
@mtkmodel Constant begin
@components begin
output = RealOutput()
end
@parameters begin
k, [description = "Constant output value of block"]
end
@equations begin
output.u ~ k
end
end
@variables t [unit = u"s"]
D = Differential(t)
@connector Pin begin
v(t), [unit = u"V"] # Potential at the pin [V]
i(t), [connect = Flow, unit = u"A"] # Current flowing into the pin [A]
@icon "pin.png"
end
@named p = Pin(; v = π)
@test getdefault(p.v) == π
@test Pin.isconnector == true
@mtkmodel OnePort begin
@components begin
p = Pin()
n = Pin()
end
@variables begin
v(t), [unit = u"V"]
i(t), [unit = u"A"]
end
@icon "oneport.png"
@equations begin
v ~ p.v - n.v
0 ~ p.i + n.i
i ~ p.i
end
end
@test OnePort.isconnector == false
@mtkmodel Ground begin
@components begin
g = Pin()
end
@icon begin
read(abspath(ENV["MTK_ICONS_DIR"], "ground.svg"), String)
end
@equations begin
g.v ~ 0
end
end
resistor_log = "$(@__DIR__)/logo/resistor.svg"
@mtkmodel Resistor begin
@extend v, i = oneport = OnePort()
@parameters begin
R, [unit = u"Ω"]
end
@icon begin
"""<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="80" height="30">
<path d="M10 15
l15 0
l2.5 -5
l5 10
l5 -10
l5 10
l5 -10
l5 10
l2.5 -5
l15 0" stroke="black" stroke-width="1" stroke-linejoin="bevel" fill="none"></path>
</svg>
"""
end
@equations begin
v ~ i * R
end
end
@mtkmodel Capacitor begin
@parameters begin
C, [unit = u"F"]
end
@extend v, i = oneport = OnePort(; v = 0.0)
@icon "https://upload.wikimedia.org/wikipedia/commons/7/78/Capacitor_symbol.svg"
@equations begin
D(v) ~ i / C
end
end
@named capacitor = Capacitor(C = 10, v = 10.0)
@test getdefault(capacitor.v) == 10.0
@mtkmodel Voltage begin
@extend v, i = oneport = OnePort()
@components begin
V = RealInput()
end
@equations begin
v ~ V.u
end
end
@mtkmodel RC begin
@structural_parameters begin
R_val = 10
C_val = 10
k_val = 10
end
@components begin
resistor = Resistor(; R = R_val)
capacitor = Capacitor(; C = C_val)
source = Voltage()
constant = Constant(; k = k_val)
ground = Ground()
end
@equations begin
connect(constant.output, source.V)
connect(source.p, resistor.p)
connect(resistor.n, capacitor.p)
connect(capacitor.n, source.n, ground.g)
end
end
C_val = 20
R_val = 20
res__R = 100
@named rc = RC(; C_val, R_val, resistor.R = res__R)
# Test that `resistor.R` overrides `R_val` in the argument.
@test getdefault(rc.resistor.R) == res__R != R_val
# Test that `C_val` passed via argument is set as default of C.
@test getdefault(rc.capacitor.C) == C_val
# Test that `k`'s default value is unchanged.
@test getdefault(rc.constant.k) == RC.structure[:kwargs][:k_val]
@test getdefault(rc.capacitor.v) == 0.0
@test get_gui_metadata(rc.resistor).layout == Resistor.structure[:icon] ==
read(joinpath(ENV["MTK_ICONS_DIR"], "resistor.svg"), String)
@test get_gui_metadata(rc.ground).layout ==
read(abspath(ENV["MTK_ICONS_DIR"], "ground.svg"), String)
@test get_gui_metadata(rc.capacitor).layout ==
URI("https://upload.wikimedia.org/wikipedia/commons/7/78/Capacitor_symbol.svg")
@test OnePort.structure[:icon] ==
URI("file:///" * abspath(ENV["MTK_ICONS_DIR"], "oneport.png"))
@test ModelingToolkit.get_gui_metadata(rc.resistor.p).layout == Pin.structure[:icon] ==
URI("file:///" * abspath(ENV["MTK_ICONS_DIR"], "pin.png"))
@test length(equations(structural_simplify(rc))) == 1
end
@testset "Parameters and Structural parameters in various modes" begin
@mtkmodel MockModel begin
@parameters begin
a
b(t)
cval
jval
kval
c(t) = cval + jval
d = 2
e, [description = "e"]
f = 3, [description = "f"]
h(t), [description = "h(t)"]
i(t) = 4, [description = "i(t)"]
j(t) = jval, [description = "j(t)"]
k = kval, [description = "k"]
end
@structural_parameters begin
l = 1
func
end
end
kval = 5
@named model = MockModel(; kval, cval = 1, func = identity)
@test hasmetadata(model.e, VariableDescription)
@test hasmetadata(model.f, VariableDescription)
@test hasmetadata(model.h, VariableDescription)
@test hasmetadata(model.i, VariableDescription)
@test hasmetadata(model.j, VariableDescription)
@test hasmetadata(model.k, VariableDescription)
model = complete(model)
@test getdefault(model.cval) == 1
@test isequal(getdefault(model.c), model.cval + model.jval)
@test getdefault(model.d) == 2
@test_throws KeyError getdefault(model.e)
@test getdefault(model.f) == 3
@test getdefault(model.i) == 4
@test isequal(getdefault(model.j), model.jval)
@test isequal(getdefault(model.k), model.kval)
end
@testset "Defaults of subcomponents MTKModel" begin
@mtkmodel A begin
@parameters begin
p
end
@components begin
b = B(i = p, j = 1 / p, k = 1)
end
end
@mtkmodel B begin
@parameters begin
i
j
k
end
end
@named a = A(p = 10)
params = get_ps(a)
@test isequal(getdefault(a.b.i), params[1])
@test isequal(getdefault(a.b.j), 1 / params[1])
@test getdefault(a.b.k) == 1
@named a = A(p = 10, b.i = 20, b.j = 30, b.k = 40)
@test getdefault(a.b.i) == 20
@test getdefault(a.b.j) == 30
@test getdefault(a.b.k) == 40
end
@testset "Metadata in variables" begin
metadata = Dict(:description => "Variable to test metadata in the Model.structure",
:input => true, :bounds => (-1, 1), :connection_type => :Flow, :integer => true,
:binary => false, :tunable => false, :disturbance => true, :dist => Normal(1, 1))
@connector MockMeta begin
m(t),
[description = "Variable to test metadata in the Model.structure",
input = true, bounds = (-1, 1), connect = Flow, integer = true,
binary = false, tunable = false, disturbance = true, dist = Normal(1, 1)]
end
for (k, v) in metadata
@test MockMeta.structure[:variables][:m][k] == v
end
end
@testset "Connector with parameters, equations..." begin
@connector A begin
@extend (e,) = extended_e = E()
@icon "pin.png"
@parameters begin
p
end
@variables begin
v(t)
end
@components begin
cc = C()
end
@equations begin
e ~ 0
end
end
@connector C begin
c(t)
end
@connector E begin
e(t)
end
@named aa = A()
@test aa.connector_type == RegularConnector()
@test A.isconnector == true
@test A.structure[:parameters] == Dict(:p => Dict())
@test A.structure[:extend] == [[:e], :extended_e, :E]
@test A.structure[:equations] == ["e ~ 0"]
@test A.structure[:kwargs] == Dict(:p => nothing, :v => nothing)
@test A.structure[:components] == [[:cc, :C]]
end