Skip to content

Commit 4c52bc5

Browse files
authored
Merge pull request #2126 from SciML/myb/domain
Domain feature unit tests and fixes
2 parents b32de83 + 1911363 commit 4c52bc5

File tree

2 files changed

+230
-6
lines changed

2 files changed

+230
-6
lines changed

src/systems/connectors.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ 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+
265266
_, domainset = merge(connectionsets, true)
266267
sys, (merge(domain_free_connectionsets), domainset)
267268
end
@@ -395,7 +396,7 @@ function Graphs.outneighbors(g::SystemDomainGraph, n::Int)
395396
for s in g.csets[i].set
396397
sys = s.sys.sys
397398
is_domain_connector(s.sys.sys) && continue
398-
ts = TearingState(s.sys.namespace)
399+
ts = TearingState(expand_connections(s.sys.namespace))
399400
graph = ts.structure.graph
400401
mm = linear_subsys_adjmat!(ts)
401402
lineqs = BitSet(mm.nzrows)

test/stream_connectors.jl

Lines changed: 228 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[m_flow ~ 0]
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
@@ -125,6 +156,7 @@ eqns = [connect(n1m1.port_a, pipe.port_a)
125156
0 ~ n1m1.port_a.m_flow + pipe.port_a.m_flow
126157
0 ~ n1m1.source.port1.m_flow - n1m1.port_a.m_flow
127158
0 ~ pipe.port_b.m_flow + sink.port.m_flow
159+
fluid.m_flow ~ 0
128160
n1m1.port_a.P ~ pipe.port_a.P
129161
n1m1.source.port1.P ~ n1m1.port_a.P
130162
n1m1.source.port1.P ~ n1m1.source.P
@@ -254,3 +286,194 @@ sys = expand_connections(compose(simple, [vp1, vp2, vp3]))
254286
end
255287

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

0 commit comments

Comments
 (0)