Skip to content

Commit 91427ef

Browse files
author
Brad Carman
committed
added tests for domain feature
1 parent c7a1140 commit 91427ef

File tree

2 files changed

+232
-5
lines changed

2 files changed

+232
-5
lines changed

src/systems/connectors.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,13 @@ function generate_connection_set(sys::AbstractSystem, find = nothing, replace =
262262
domain_free_connectionsets = filter(connectionsets) do cset
263263
!any(s -> is_domain_connector(s.sys.sys), cset.set)
264264
end
265+
266+
# Calling merge(connectionsets, true)
267+
# needs to run with sys expanded equations, if we have more connectionsets then domain_free, then a domain exits, run again...
268+
if length(connectionsets) > length(domain_free_connectionsets)
269+
# connectionsets_ = ConnectionSet[]
270+
sys = generate_connection_set!(connectionsets, sys, find, replace)
271+
end
265272
_, domainset = merge(connectionsets, true)
266273
sys, (merge(domain_free_connectionsets), domainset)
267274
end

test/stream_connectors.jl

Lines changed: 225 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,34 @@ using ModelingToolkit
33
@variables t
44

55
@connector function TwoPhaseFluidPort(; name, P = 0.0, m_flow = 0.0, h_outflow = 0.0)
6-
vars = @variables h_outflow(t)=h_outflow [connect = Stream] m_flow(t)=m_flow [
7-
connect = Flow,
8-
] P(t)=P
9-
ODESystem(Equation[], t, vars, []; name = name)
6+
pars = @parameters begin
7+
rho
8+
bulk
9+
viscosity
10+
end
11+
12+
vars = @variables begin
13+
(h_outflow(t) = h_outflow), [connect = Stream]
14+
(m_flow(t) = m_flow), [connect = Flow]
15+
P(t) = P
16+
end
17+
18+
ODESystem(Equation[], t, vars, pars; name = name)
19+
end
20+
21+
@connector function TwoPhaseFluid(; name, R, B, V)
22+
pars = @parameters begin
23+
rho = R
24+
bulk = B
25+
viscosity = V
26+
end
27+
28+
vars = @variables begin m_flow(t), [connect = Flow] end
29+
30+
# equations ---------------------------
31+
eqs = Equation[]
32+
33+
ODESystem(eqs, t, vars, pars; name)
1034
end
1135

1236
function MassFlowSource_h(; name,
@@ -90,6 +114,7 @@ function N1M1(; name,
90114
sys = compose(sys, subs)
91115
end
92116

117+
@named fluid = TwoPhaseFluid(; R = 876, B = 1.2e9, V = 0.034)
93118
@named n1m1 = N1M1()
94119
@named pipe = AdiabaticStraightPipe()
95120
@named sink = MassFlowSource_h(m_flow_in = -0.01, h_in = 400e3)
@@ -98,7 +123,13 @@ eqns = [connect(n1m1.port_a, pipe.port_a)
98123
connect(pipe.port_b, sink.port)]
99124

100125
@named sys = ODESystem(eqns, t)
101-
@named n1m1Test = compose(sys, n1m1, pipe, sink)
126+
127+
eqns = [connect(fluid, n1m1.port_a)
128+
connect(n1m1.port_a, pipe.port_a)
129+
connect(pipe.port_b, sink.port)]
130+
131+
@named n1m1Test = ODESystem(eqns, t, [], []; systems = [fluid, n1m1, pipe, sink])
132+
102133
@test_nowarn structural_simplify(n1m1Test)
103134
@unpack source, port_a = n1m1
104135
@test sort(equations(expand_connections(n1m1)), by = string) == [0 ~ port_a.m_flow
@@ -254,3 +285,192 @@ sys = expand_connections(compose(simple, [vp1, vp2, vp3]))
254285
end
255286

256287
@test_nowarn @named a = VectorHeatPort()
288+
289+
# --------------------------------------------------
290+
# Test the new Domain feature
291+
292+
sys_ = expand_connections(n1m1Test)
293+
sys_defs = ModelingToolkit.defaults(sys_)
294+
csys = complete(n1m1Test)
295+
@test Symbol(sys_defs[csys.pipe.port_a.rho]) == Symbol(csys.fluid.rho)
296+
297+
#TODO: This test fails...Is the AdiabaticStraightPipe really a valid component?
298+
# @test Symbol(sys_defs[csys.pipe.port_b.rho]) == Symbol(csys.fluid.rho)
299+
300+
# Testing the domain feature with non-stream system...
301+
302+
@connector function HydraulicPort(; P, name)
303+
pars = @parameters begin
304+
p_int = P
305+
rho
306+
bulk
307+
viscosity
308+
end
309+
310+
vars = @variables begin
311+
p(t) = p_int
312+
dm(t), [connect = Flow]
313+
end
314+
315+
# equations ---------------------------
316+
eqs = Equation[]
317+
318+
ODESystem(eqs, t, vars, pars; name, defaults = [dm => 0])
319+
end
320+
321+
@connector function Fluid(; name, R, B, V)
322+
pars = @parameters begin
323+
rho = R
324+
bulk = B
325+
viscosity = V
326+
end
327+
328+
vars = @variables begin dm(t), [connect = Flow] end
329+
330+
# equations ---------------------------
331+
eqs = Equation[]
332+
333+
ODESystem(eqs, t, vars, pars; name)
334+
end
335+
336+
function StepSource(; P, name)
337+
pars = @parameters begin p_int = P end
338+
339+
vars = []
340+
341+
# nodes -------------------------------
342+
systems = @named begin H = HydraulicPort(; P = p_int) end
343+
344+
# equations ---------------------------
345+
eqs = [
346+
H.p ~ ifelse(t < 0.1, 0, p_int),
347+
]
348+
349+
ODESystem(eqs, t, vars, pars; name, systems)
350+
end
351+
352+
function Pipe(; P, R, name)
353+
pars = @parameters begin
354+
p_int = P
355+
resistance = R
356+
end
357+
358+
vars = []
359+
360+
# nodes -------------------------------
361+
systems = @named begin
362+
HA = HydraulicPort(; P = p_int)
363+
HB = HydraulicPort(; P = p_int)
364+
end
365+
366+
# equations ---------------------------
367+
eqs = [HA.p - HB.p ~ HA.dm * resistance / HA.viscosity
368+
0 ~ HA.dm + HB.dm]
369+
370+
ODESystem(eqs, t, vars, pars; name, systems)
371+
end
372+
373+
function StaticVolume(; P, V, name)
374+
pars = @parameters begin
375+
p_int = P
376+
vol = V
377+
end
378+
379+
vars = @variables begin
380+
p(t) = p_int
381+
vrho(t)
382+
drho(t) = 0
383+
end
384+
385+
# nodes -------------------------------
386+
systems = @named begin H = HydraulicPort(; P = p_int) end
387+
388+
# fluid props ------------------------
389+
rho_0 = H.rho
390+
391+
# equations ---------------------------
392+
eqs = [D(vrho) ~ drho
393+
vrho ~ rho_0 * (1 + p / H.bulk)
394+
H.p ~ p
395+
H.dm ~ drho * V]
396+
397+
ODESystem(eqs, t, vars, pars; name, systems,
398+
defaults = [vrho => rho_0 * (1 + p_int / H.bulk)])
399+
end
400+
401+
function TwoFluidSystem(; name)
402+
pars = []
403+
vars = []
404+
405+
# nodes -------------------------------
406+
systems = @named begin
407+
fluid_a = Fluid(; R = 876, B = 1.2e9, V = 0.034)
408+
source_a = StepSource(; P = 10e5)
409+
pipe_a = Pipe(; P = 0, R = 1e6)
410+
volume_a = StaticVolume(; P = 0, V = 0.1)
411+
412+
fluid_b = Fluid(; R = 1000, B = 2.5e9, V = 0.00034)
413+
source_b = StepSource(; P = 10e5)
414+
pipe_b = Pipe(; P = 0, R = 1e6)
415+
volume_b = StaticVolume(; P = 0, V = 0.1)
416+
end
417+
418+
# equations ---------------------------
419+
eqs = [connect(fluid_a, source_a.H)
420+
connect(source_a.H, pipe_a.HA)
421+
connect(pipe_a.HB, volume_a.H)
422+
connect(fluid_b, source_b.H)
423+
connect(source_b.H, pipe_b.HA)
424+
connect(pipe_b.HB, volume_b.H)]
425+
426+
ODESystem(eqs, t, vars, pars; name, systems)
427+
end
428+
429+
@named two_fluid_system = TwoFluidSystem()
430+
sys = expand_connections(two_fluid_system)
431+
432+
sys_defs = ModelingToolkit.defaults(sys)
433+
csys = complete(two_fluid_system)
434+
435+
@test Symbol(sys_defs[csys.volume_a.H.rho]) == Symbol(csys.fluid_a.rho)
436+
@test Symbol(sys_defs[csys.volume_b.H.rho]) == Symbol(csys.fluid_b.rho)
437+
438+
@test_nowarn structural_simplify(two_fluid_system)
439+
440+
function OneFluidSystem(; name)
441+
pars = []
442+
vars = []
443+
444+
# nodes -------------------------------
445+
systems = @named begin
446+
fluid = Fluid(; R = 876, B = 1.2e9, V = 0.034)
447+
448+
source_a = StepSource(; P = 10e5)
449+
pipe_a = Pipe(; P = 0, R = 1e6)
450+
volume_a = StaticVolume(; P = 0, V = 0.1)
451+
452+
source_b = StepSource(; P = 20e5)
453+
pipe_b = Pipe(; P = 0, R = 1e6)
454+
volume_b = StaticVolume(; P = 0, V = 0.1)
455+
end
456+
457+
# equations ---------------------------
458+
eqs = [connect(fluid, source_a.H, source_b.H)
459+
connect(source_a.H, pipe_a.HA)
460+
connect(pipe_a.HB, volume_a.H)
461+
connect(source_b.H, pipe_b.HA)
462+
connect(pipe_b.HB, volume_b.H)]
463+
464+
ODESystem(eqs, t, vars, pars; name, systems)
465+
end
466+
467+
@named one_fluid_system = OneFluidSystem()
468+
sys = expand_connections(one_fluid_system)
469+
470+
sys_defs = ModelingToolkit.defaults(sys)
471+
csys = complete(one_fluid_system)
472+
473+
@test Symbol(sys_defs[csys.volume_a.H.rho]) == Symbol(csys.fluid.rho)
474+
@test Symbol(sys_defs[csys.volume_b.H.rho]) == Symbol(csys.fluid.rho)
475+
476+
@test_nowarn structural_simplify(one_fluid_system)

0 commit comments

Comments
 (0)