Skip to content

Commit 2a553a7

Browse files
committed
Be careful with string sorting
1 parent 2e4c1bb commit 2a553a7

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

test/odesystem.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ eqs = [D(x) ~ σ * (y - x),
2323
ModelingToolkit.toexpr.(eqs)[1]
2424
@named de = ODESystem(eqs; defaults = Dict(x => 1))
2525
subed = substitute(de, [σ => k])
26-
@test isequal(sort(parameters(subed), by = string), [k, β, ρ])
26+
ssort(eqs) = sort(eqs, by = string)
27+
@test isequal(ssort(parameters(subed)), [k, β, ρ])
2728
@test isequal(equations(subed),
2829
[D(x) ~ k * (y - x)
2930
D(y) ~- z) * x - y
@@ -348,14 +349,14 @@ eqs = [0 ~ x + z
348349
D(accumulation_y) ~ y
349350
D(accumulation_z) ~ z
350351
D(x) ~ y]
351-
@test sort(equations(asys), by = string) == eqs
352+
@test ssort(equations(asys)) == ssort(eqs)
352353
@variables ac(t)
353354
asys = add_accumulations(sys, [ac => (x + y)^2])
354355
eqs = [0 ~ x + z
355356
0 ~ x - y
356357
D(ac) ~ (x + y)^2
357358
D(x) ~ y]
358-
@test sort(equations(asys), by = string) == eqs
359+
@test ssort(equations(asys)) == ssort(eqs)
359360

360361
sys2 = ode_order_lowering(sys)
361362
M = ModelingToolkit.calculate_massmatrix(sys2)

test/stream_connectors.jl

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -136,27 +136,28 @@ eqns = [domain_connect(fluid, n1m1.port_a)
136136

137137
@test_nowarn structural_simplify(n1m1Test)
138138
@unpack source, port_a = n1m1
139-
@test sort(equations(expand_connections(n1m1)), by = string) == [0 ~ port_a.m_flow
139+
ssort(eqs) = sort(eqs, by = string)
140+
@test ssort(equations(expand_connections(n1m1))) == ssort([0 ~ port_a.m_flow
140141
0 ~ source.port1.m_flow - port_a.m_flow
141142
source.port1.P ~ port_a.P
142143
source.port1.P ~ source.P
143144
source.port1.h_outflow ~ port_a.h_outflow
144-
source.port1.h_outflow ~ source.h]
145+
source.port1.h_outflow ~ source.h])
145146
@unpack port_a, port_b = pipe
146-
@test sort(equations(expand_connections(pipe)), by = string) ==
147-
[0 ~ -port_a.m_flow - port_b.m_flow
147+
@test ssort(equations(expand_connections(pipe))) ==
148+
ssort([0 ~ -port_a.m_flow - port_b.m_flow
148149
0 ~ port_a.m_flow
149150
0 ~ port_b.m_flow
150151
port_a.P ~ port_b.P
151152
port_a.h_outflow ~ instream(port_b.h_outflow)
152-
port_b.h_outflow ~ instream(port_a.h_outflow)]
153-
@test sort(equations(expand_connections(sys)), by = string) ==
154-
[0 ~ n1m1.port_a.m_flow + pipe.port_a.m_flow
153+
port_b.h_outflow ~ instream(port_a.h_outflow)])
154+
@test ssort(equations(expand_connections(sys))) ==
155+
ssort([0 ~ n1m1.port_a.m_flow + pipe.port_a.m_flow
155156
0 ~ pipe.port_b.m_flow + sink.port.m_flow
156157
n1m1.port_a.P ~ pipe.port_a.P
157-
pipe.port_b.P ~ sink.port.P]
158-
@test sort(equations(expand_connections(n1m1Test)), by = string) ==
159-
[0 ~ -pipe.port_a.m_flow - pipe.port_b.m_flow
158+
pipe.port_b.P ~ sink.port.P])
159+
@test ssort(equations(expand_connections(n1m1Test))) ==
160+
ssort([0 ~ -pipe.port_a.m_flow - pipe.port_b.m_flow
160161
0 ~ n1m1.source.port1.m_flow - n1m1.port_a.m_flow
161162
0 ~ n1m1.port_a.m_flow + pipe.port_a.m_flow
162163
0 ~ pipe.port_b.m_flow + sink.port.m_flow
@@ -172,7 +173,7 @@ eqns = [domain_connect(fluid, n1m1.port_a)
172173
pipe.port_b.h_outflow ~ n1m1.port_a.h_outflow
173174
sink.port.P ~ sink.P
174175
sink.port.h_outflow ~ sink.h_in
175-
sink.port.m_flow ~ -sink.m_flow_in]
176+
sink.port.m_flow ~ -sink.m_flow_in])
176177

177178
# N1M2 model and test code.
178179
function N1M2(; name,
@@ -255,12 +256,12 @@ eqns = [connect(source.port, n2m2.port_a)
255256
@named sp2 = TwoPhaseFluidPort()
256257
@named sys = ODESystem([connect(sp1, sp2)], t)
257258
sys_exp = expand_connections(compose(sys, [sp1, sp2]))
258-
@test sort(equations(sys_exp), by = string) == [0 ~ -sp1.m_flow - sp2.m_flow
259+
@test ssort(equations(sys_exp)) == ssort([0 ~ -sp1.m_flow - sp2.m_flow
259260
0 ~ sp1.m_flow
260261
0 ~ sp2.m_flow
261262
sp1.P ~ sp2.P
262263
sp1.h_outflow ~ ModelingToolkit.instream(sp2.h_outflow)
263-
sp2.h_outflow ~ ModelingToolkit.instream(sp1.h_outflow)]
264+
sp2.h_outflow ~ ModelingToolkit.instream(sp1.h_outflow)])
264265

265266
# array var
266267
@connector function VecPin(; name)
@@ -274,15 +275,15 @@ end
274275

275276
@named simple = ODESystem([connect(vp1, vp2, vp3)], t)
276277
sys = expand_connections(compose(simple, [vp1, vp2, vp3]))
277-
@test sort(equations(sys), by = string) == sort([0 .~ collect(vp1.i)
278-
0 .~ collect(vp2.i)
279-
0 .~ collect(vp3.i)
280-
vp1.v[1] ~ vp2.v[1]
281-
vp1.v[2] ~ vp2.v[2]
282-
vp1.v[1] ~ vp3.v[1]
283-
vp1.v[2] ~ vp3.v[2]
284-
0 ~ -vp1.i[1] - vp2.i[1] - vp3.i[1]
285-
0 ~ -vp1.i[2] - vp2.i[2] - vp3.i[2]], by = string)
278+
@test ssort(equations(sys)) == ssort([0 .~ collect(vp1.i)
279+
0 .~ collect(vp2.i)
280+
0 .~ collect(vp3.i)
281+
vp1.v[1] ~ vp2.v[1]
282+
vp1.v[2] ~ vp2.v[2]
283+
vp1.v[1] ~ vp3.v[1]
284+
vp1.v[2] ~ vp3.v[2]
285+
0 ~ -vp1.i[1] - vp2.i[1] - vp3.i[1]
286+
0 ~ -vp1.i[2] - vp2.i[2] - vp3.i[2]])
286287

287288
@connector function VectorHeatPort(; name, N = 100, T0 = 0.0, Q0 = 0.0)
288289
@variables (T(t))[1:N]=T0 (Q(t))[1:N]=Q0 [connect = Flow]

0 commit comments

Comments
 (0)