Skip to content

Commit 968831c

Browse files
committed
Add stream connector tests
1 parent b96121e commit 968831c

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ using SafeTestsets, Test
2222
@safetestset "Constraints Test" begin include("constraints.jl") end
2323
@safetestset "Reduction Test" begin include("reduction.jl") end
2424
@safetestset "Components Test" begin include("components.jl") end
25+
@safetestset "Stream Connnect Test" begin include("stream_connectors.jl") end
2526
@safetestset "PDE Construction Test" begin include("pde.jl") end
2627
@safetestset "Lowering Integration Test" begin include("lowering_solving.jl") end
2728
@safetestset "Test Big System Usage" begin include("bigsystem.jl") end

test/stream_connectors.jl

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
using Test
2+
using ModelingToolkit
3+
@variables t
4+
5+
@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 [connect=Flow] P(t)=P
7+
ODESystem(Equation[], t, vars, []; name=name)
8+
end
9+
10+
function MassFlowSource_h(;name,
11+
h_in=420e3,
12+
m_flow_in=-0.01,
13+
)
14+
15+
pars = @parameters begin
16+
h_in=h_in
17+
m_flow_in=m_flow_in
18+
end
19+
20+
vars = @variables begin
21+
P(t)
22+
end
23+
24+
@named port = TwoPhaseFluidPort()
25+
26+
subs = [port]
27+
28+
eqns = Equation[]
29+
30+
push!(eqns, port.P ~ P)
31+
push!(eqns, port.m_flow ~ -m_flow_in)
32+
push!(eqns, port.h_outflow ~ h_in)
33+
34+
compose(ODESystem(eqns, t, vars, pars; name=name), subs)
35+
end
36+
37+
# Simplified components.
38+
function AdiabaticStraightPipe(;name,
39+
kwargs...,
40+
)
41+
42+
vars = []
43+
pars = []
44+
45+
@named port_a = TwoPhaseFluidPort()
46+
@named port_b = TwoPhaseFluidPort()
47+
48+
subs = [port_a; port_b]
49+
50+
eqns = Equation[]
51+
52+
#=
53+
push!(eqns, port_a.P ~ port_b.P)
54+
push!(eqns, 0 ~ port_a.m_flow + port_b.m_flow)
55+
push!(eqns, port_b.h_outflow ~ instream(port_a.h_outflow))
56+
push!(eqns, port_a.h_outflow ~ instream(port_b.h_outflow))
57+
=#
58+
59+
push!(eqns, connect(port_a, port_b))
60+
sys = ODESystem(eqns, t, vars, pars; name=name)
61+
sys = compose(sys, subs)
62+
end
63+
64+
65+
function SmallBoundary_Ph(;name,
66+
P_in=1e6,
67+
h_in=400e3,
68+
)
69+
70+
vars = []
71+
72+
pars = @parameters begin
73+
P=P_in
74+
h=h_in
75+
end
76+
77+
@named port1 = TwoPhaseFluidPort()
78+
79+
subs = [port1]
80+
81+
eqns = Equation[]
82+
83+
push!(eqns, port1.P ~ P)
84+
push!(eqns, port1.h_outflow ~ h)
85+
86+
compose(ODESystem(eqns, t, vars, pars; name=name), subs)
87+
end
88+
89+
90+
# N1M1 model and test code.
91+
function N1M1(;name,
92+
P_in=1e6,
93+
h_in=400e3,
94+
kwargs...,
95+
)
96+
97+
@named port_a = TwoPhaseFluidPort()
98+
@named source = SmallBoundary_Ph(P_in=P_in, h_in=h_in)
99+
100+
subs = [port_a; source]
101+
102+
eqns = Equation[]
103+
104+
push!(eqns, connect(source.port1, port_a))
105+
106+
sys = ODESystem(eqns, t, [], [], name=name)
107+
sys = compose(sys, subs)
108+
end
109+
110+
@named n1m1 = N1M1()
111+
@named pipe = AdiabaticStraightPipe()
112+
@named sink = MassFlowSource_h(m_flow_in=-0.01, h_in=400e3)
113+
114+
streams_a = [n1m1.port_a, pipe.port_a]
115+
streams_b = [pipe.port_b, sink.port]
116+
117+
eqns = [
118+
connect(n1m1.port_a, pipe.port_a)
119+
connect(pipe.port_b, sink.port)
120+
]
121+
122+
@named sys = ODESystem(eqns, t)
123+
@named n1m1Test = compose(sys, n1m1, pipe, sink)
124+
@test_nowarn structural_simplify(n1m1Test)
125+
126+
127+
# N1M2 model and test code.
128+
function N1M2(;name,
129+
P_in=1e6,
130+
h_in=400e3,
131+
kwargs...,
132+
)
133+
134+
@named port_a = TwoPhaseFluidPort()
135+
@named port_b = TwoPhaseFluidPort()
136+
137+
@named source = SmallBoundary_Ph(P_in=P_in, h_in=h_in)
138+
139+
subs = [port_a; port_b; source]
140+
141+
eqns = Equation[]
142+
143+
push!(eqns, connect(source.port1, port_a))
144+
push!(eqns, connect(source.port1, port_b))
145+
146+
sys = ODESystem(eqns, t, [], [], name=name)
147+
sys = compose(sys, subs)
148+
end
149+
150+
@named n1m2 = N1M2()
151+
@named sink1 = MassFlowSource_h(m_flow_in=-0.01, h_in=400e3)
152+
@named sink2 = MassFlowSource_h(m_flow_in=-0.01, h_in=400e3)
153+
154+
eqns = [
155+
connect(n1m2.port_a, sink1.port)
156+
connect(n1m2.port_b, sink2.port)
157+
]
158+
159+
@named sys = ODESystem(eqns, t)
160+
@named n1m2Test = compose(sys, n1m2, sink1, sink2)
161+
@test_nowarn structural_simplify(n1m2Test)
162+
163+
164+
@named n1m2 = N1M2()
165+
@named pipe1 = AdiabaticStraightPipe()
166+
@named pipe2 = AdiabaticStraightPipe()
167+
@named sink1 = MassFlowSource_h(m_flow_in=-0.01, h_in=400e3)
168+
@named sink2 = MassFlowSource_h(m_flow_in=-0.01, h_in=400e3)
169+
170+
eqns = [
171+
connect(n1m2.port_a, pipe1.port_a)
172+
connect(pipe1.port_b, sink1.port)
173+
174+
connect(n1m2.port_b, pipe2.port_a)
175+
connect(pipe2.port_b, sink2.port)
176+
]
177+
178+
@named sys = ODESystem(eqns, t)
179+
@named n1m2AltTest = compose(sys, n1m2, pipe1, pipe2, sink1, sink2)
180+
@test_nowarn structural_simplify(n1m2AltTest)
181+
182+
183+
# N2M2 model and test code.
184+
function N2M2(;name,
185+
kwargs...,
186+
)
187+
188+
@named port_a = TwoPhaseFluidPort()
189+
@named port_b = TwoPhaseFluidPort()
190+
@named pipe = AdiabaticStraightPipe()
191+
192+
streams_a = [port_a, pipe.port_a]
193+
streams_b = [pipe.port_b, port_b]
194+
195+
subs = [port_a; port_b; pipe]
196+
197+
eqns = Equation[]
198+
199+
push!(eqns, connect(port_a, pipe.port_a))
200+
push!(eqns, connect(pipe.port_b, port_b))
201+
202+
sys = ODESystem(eqns, t, [], [], name=name)
203+
sys = compose(sys, subs)
204+
end
205+
206+
@named n2m2 = N2M2()
207+
@named source = MassFlowSource_h(m_flow_in=-0.01, h_in=400e3)
208+
@named sink = SmallBoundary_Ph(P_in=1e6, h_in=400e3)
209+
210+
eqns = [
211+
connect(source.port, n2m2.port_a)
212+
connect(n2m2.port_b, sink.port1)
213+
]
214+
215+
@named sys = ODESystem(eqns, t)
216+
@named n2m2Test = compose(sys, n2m2, source, sink)
217+
@test_nowarn structural_simplify(n2m2Test)

0 commit comments

Comments
 (0)