-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.jl
288 lines (240 loc) · 6.84 KB
/
demo.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
using ModelingToolkit
using DifferentialEquations
using Plots
# ------------------------------------------------
# Part 1: Steady State Modeling ------------------
# ------------------------------------------------
pars = @parameters A=0.1 ẋ=1 c=1000 pₛ=300e5 pᵣ=0 ρ=1000 Cₒ=2.7 m=100 ẍ=0
vars = @variables p₁=300e5 p₂=0e5 Aₒ=0.001
# symbolic expressions
u = ẋ * (A/Aₒ)
# equations
eqs = [
pₛ - p₁ ~ (1/2)*ρ*u^2*Cₒ
p₂ - pᵣ ~ (1/2)*ρ*u^2*Cₒ
m*ẍ ~ (p₂ - p₁)*A - c*ẋ
]
@named nlsys = NonlinearSystem(eqs, vars, pars)
sys = structural_simplify(nlsys)
prob = NonlinearProblem(sys, [], []) # [initial conditions], [parameters]
sol = solve(prob)
sol[Aₒ] #<-- solution!
# how to quickly make a new solution
orifices = []
velocity_limits = 1.0:0.1:2.0
for velocity_limit in velocity_limits
prob′ = remake(prob; p=[ẋ => velocity_limit])
sol′ = solve(prob′)
push!(orifices, sol′[Aₒ])
end
plot(velocity_limits, orifices; xlabel="velocity limit [m/s]", ylabel="orifice size [m^2]")
# ------------------------------------------------
# Part 2: Dynamic Modeling (DAEs) ----------------
# ------------------------------------------------
@parameters t
D = Differential(t)
pars = @parameters A=0.1 pₛ=300e5 pᵣ=0 ρ=1000 C₀=2.7 m=100 Aₒ=0.00094 c=1000
vars = @variables x(t)=0 ẋ(t)=0 p₁(t)=300e5 p₂(t)=0e5 ẍ(t)=(p₂-p₁)*A/m
# symbolic expressions
u = ẋ * (A/Aₒ)
# equations
eqs = [
D(x) ~ ẋ
D(ẋ) ~ ẍ
pₛ - p₁ ~ (1/2)*ρ*u^2*C₀
p₂ - pᵣ ~ (1/2)*ρ*u^2*C₀
m*ẍ ~ (p₂-p₁)*A - c*ẋ
]
@named odesys = ODESystem(eqs, t, vars, pars)
sys = structural_simplify(odesys)
prob = ODEProblem(sys, [], (0.0, 0.0001), [])
sol = solve(prob)
# explain sol object...
plot(sol.t, sol[x]; marker=:circle, ylabel="position [m]")
plot(sol, idxs=[x]; ylabel="position [m]")
plot(sol, idxs=[ẋ]; ylabel="velocity [m/s]")
plot(sol, idxs=[ẍ]; ylabel="acceleration [m/s^2]")
plot(sol, idxs=[p₁, p₂]; ylabel="pressure [Pa]")
# for comparison with compressible system
prob′ = remake(prob, tspan=(0, 0.1))
sol_ic = solve(prob′)
# ------------------------------------------------
# Part 3: Component Based Modeling ---------------
# ------------------------------------------------
# Connectors ----
# https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/connectors/connections/
@connector Port begin
p(t)
ṁ(t)=0, [connect = Flow]
end
@connector Flange begin
ẋ(t)=0
f(t), [connect = Flow]
end
# Components ----
@mtkmodel Orifice begin
@parameters begin
Cₒ=2.7
Aₒ=0.00094
ρ₀=1000
p′=0
end
@variables begin
ṁ(t)=0
p₁(t)=p′
p₂(t)=p′
end
@components begin
port₁ = Port(p=p′)
port₂ = Port(p=p′)
end
begin
u = ṁ/(ρ₀*Aₒ)
end
@equations begin
ṁ ~ +port₁.ṁ
ṁ ~ -port₂.ṁ
p₁ ~ port₁.p
p₂ ~ port₂.p
p₁ - p₂ ~ (1/2)*ρ₀*u^2*Cₒ
end
end
@mtkmodel Volume begin
@parameters begin
A=0.1
ρ₀=1000
β=2e9
direction=+1
p′
x′
end
@variables begin
p(t)=p′
x(t)=x′
ṁ(t)=0
f(t)=p′ * A
ẋ(t)=0
r(t)=ρ₀*(1 + p′/β)
ṙ(t)=0
end
@components begin
port = Port(p=p′)
flange = Flange(f=-p′ * A * direction)
end
@equations begin
D(x) ~ ẋ
D(r) ~ ṙ
p ~ +port.p
ṁ ~ +port.ṁ # mass is entering
f ~ -flange.f * direction # force is leaving
ẋ ~ flange.ẋ * direction
r ~ ρ₀*(1 + p/β)
ṁ ~ (r*ẋ*A) + (ṙ*x*A)
f ~ p * A
end
end
@mtkmodel Mass begin
@parameters begin
m = 100
f′
end
@variables begin
f(t)=f′
x(t)=0
ẋ(t)=0
ẍ(t)=f′/m
end
@components begin
flange = Flange(f=f′)
end
@equations begin
D(x) ~ ẋ
D(ẋ) ~ ẍ
f ~ flange.f
ẋ ~ flange.ẋ
m*ẍ ~ f
end
end
@mtkmodel Actuator begin
@parameters begin
p₁′
p₂′
end
begin #constants
x′=0.5
A=0.1
end
@components begin
port₁ = Port(p=p₁′)
port₂ = Port(p=p₂′)
vol₁ = Volume(p′=p₁′, x′=x′, direction=-1)
vol₂ = Volume(p′=p₂′, x′=x′, direction=+1)
mass = Mass(f′=(p₂′ - p₁′)*A)
flange = Flange(f=0)
end
@equations begin
connect(port₁, vol₁.port)
connect(port₂, vol₂.port)
connect(vol₁.flange, vol₂.flange, mass.flange, flange)
end
end
@mtkmodel Source begin
@parameters begin
p′
end
@components begin
port = Port(p=p′)
end
@equations begin
port.p ~ p′
end
end
@mtkmodel Damper begin
@parameters begin
c = 1000
end
@components begin
flange = Flange(f=0)
end
@equations begin
flange.f ~ c*flange.ẋ
end
end
@mtkmodel System begin
@components begin
res₁ = Orifice(p′=300e5)
res₂ = Orifice(p′=0)
act = Actuator(p₁′=300e5, p₂′=0)
src = Source(p′=300e5)
snk = Source(p′=0)
dmp = Damper()
end
@equations begin
connect(src.port, res₁.port₁)
connect(res₁.port₂, act.port₁)
connect(act.port₂, res₂.port₁)
connect(res₂.port₂, snk.port)
connect(dmp.flange, act.flange)
end
end
@mtkbuild sys = System()
prob = ODEProblem(sys, [], (0, 0.1), [])
# Solving with ImplicitEuler ---------------------------------------------------------
sol_ie = solve(prob, ImplicitEuler(nlsolve = NLNewton(check_div=false, always_new=true)))
# Solving with Initialization Hack ---------------------------------------------------
dt = 1e-7
prob = ODEProblem(sys, [], (0, dt))
sol = solve(prob, ImplicitEuler(nlsolve=NLNewton(check_div=false, always_new=true, relax=4/10, max_iter=100)); dt, adaptive=false)
# update u0 with the ImplicitEuler non-adaptive step
prob′ = ODEProblem(sys, sol[2], (0, 0.1))
sol_r = solve(prob′);
# velocity comparison (incompressible vs. compressible)
plot(sol_ie, idxs=[sys.act.mass.ẋ]; ylabel="velocity [m/s]", label="Compressible (ImplicitEuler)")
plot!(sol_r, idxs=[sys.act.mass.ẋ]; ylabel="velocity [m/s]", label="Compressible (Rodas5P)")
plot!(sol_ic, idxs=[ẋ], label="Incompressible")
# What's Next --> Using the ModelingToolkitStandardLibrary
# https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/
# RC Circuit
# https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/tutorials/rc_circuit/
# DC Motor
# https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/tutorials/dc_motor_pi/