Skip to content

Commit

Permalink
Be careful with string sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
YingboMa committed Oct 2, 2023
1 parent 2e4c1bb commit 360ddb3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
7 changes: 4 additions & 3 deletions test/odesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ eqs = [D(x) ~ σ * (y - x),
ModelingToolkit.toexpr.(eqs)[1]
@named de = ODESystem(eqs; defaults = Dict(x => 1))
subed = substitute(de, [σ => k])
@test isequal(sort(parameters(subed), by = string), [k, β, ρ])
ssort(eqs) = sort(eqs, by = string)
@test isequal(ssort(parameters(subed)), [k, β, ρ])
@test isequal(equations(subed),
[D(x) ~ k * (y - x)
D(y) ~- z) * x - y
Expand Down Expand Up @@ -348,14 +349,14 @@ eqs = [0 ~ x + z
D(accumulation_y) ~ y
D(accumulation_z) ~ z
D(x) ~ y]
@test sort(equations(asys), by = string) == eqs
@test ssort(equations(asys)) == ssort(eqs)
@variables ac(t)
asys = add_accumulations(sys, [ac => (x + y)^2])
eqs = [0 ~ x + z
0 ~ x - y
D(ac) ~ (x + y)^2
D(x) ~ y]
@test sort(equations(asys), by = string) == eqs
@test ssort(equations(asys)) == ssort(eqs)

sys2 = ode_order_lowering(sys)
M = ModelingToolkit.calculate_massmatrix(sys2)
Expand Down
31 changes: 16 additions & 15 deletions test/stream_connectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,27 +136,28 @@ eqns = [domain_connect(fluid, n1m1.port_a)

@test_nowarn structural_simplify(n1m1Test)
@unpack source, port_a = n1m1
@test sort(equations(expand_connections(n1m1)), by = string) == [0 ~ port_a.m_flow
ssort(eqs) = sort(eqs, by = string)
@test ssort(equations(expand_connections(n1m1))) == ssort([0 ~ port_a.m_flow
0 ~ source.port1.m_flow - port_a.m_flow
source.port1.P ~ port_a.P
source.port1.P ~ source.P
source.port1.h_outflow ~ port_a.h_outflow
source.port1.h_outflow ~ source.h]
source.port1.h_outflow ~ source.h])
@unpack port_a, port_b = pipe
@test sort(equations(expand_connections(pipe)), by = string) ==
[0 ~ -port_a.m_flow - port_b.m_flow
@test ssort(equations(expand_connections(pipe))) ==
ssort([0 ~ -port_a.m_flow - port_b.m_flow
0 ~ port_a.m_flow
0 ~ port_b.m_flow
port_a.P ~ port_b.P
port_a.h_outflow ~ instream(port_b.h_outflow)
port_b.h_outflow ~ instream(port_a.h_outflow)]
@test sort(equations(expand_connections(sys)), by = string) ==
[0 ~ n1m1.port_a.m_flow + pipe.port_a.m_flow
port_b.h_outflow ~ instream(port_a.h_outflow)])
@test ssort(equations(expand_connections(sys))) ==
ssort([0 ~ n1m1.port_a.m_flow + pipe.port_a.m_flow
0 ~ pipe.port_b.m_flow + sink.port.m_flow
n1m1.port_a.P ~ pipe.port_a.P
pipe.port_b.P ~ sink.port.P]
@test sort(equations(expand_connections(n1m1Test)), by = string) ==
[0 ~ -pipe.port_a.m_flow - pipe.port_b.m_flow
pipe.port_b.P ~ sink.port.P])
@test ssort(equations(expand_connections(n1m1Test))) ==
ssort([0 ~ -pipe.port_a.m_flow - pipe.port_b.m_flow
0 ~ n1m1.source.port1.m_flow - n1m1.port_a.m_flow
0 ~ n1m1.port_a.m_flow + pipe.port_a.m_flow
0 ~ pipe.port_b.m_flow + sink.port.m_flow
Expand All @@ -172,7 +173,7 @@ eqns = [domain_connect(fluid, n1m1.port_a)
pipe.port_b.h_outflow ~ n1m1.port_a.h_outflow
sink.port.P ~ sink.P
sink.port.h_outflow ~ sink.h_in
sink.port.m_flow ~ -sink.m_flow_in]
sink.port.m_flow ~ -sink.m_flow_in])

# N1M2 model and test code.
function N1M2(; name,
Expand Down Expand Up @@ -255,12 +256,12 @@ eqns = [connect(source.port, n2m2.port_a)
@named sp2 = TwoPhaseFluidPort()
@named sys = ODESystem([connect(sp1, sp2)], t)
sys_exp = expand_connections(compose(sys, [sp1, sp2]))
@test sort(equations(sys_exp), by = string) == [0 ~ -sp1.m_flow - sp2.m_flow
@test ssort(equations(sys_exp)) == ssort([0 ~ -sp1.m_flow - sp2.m_flow
0 ~ sp1.m_flow
0 ~ sp2.m_flow
sp1.P ~ sp2.P
sp1.h_outflow ~ ModelingToolkit.instream(sp2.h_outflow)
sp2.h_outflow ~ ModelingToolkit.instream(sp1.h_outflow)]
sp2.h_outflow ~ ModelingToolkit.instream(sp1.h_outflow)])

# array var
@connector function VecPin(; name)
Expand All @@ -274,15 +275,15 @@ end

@named simple = ODESystem([connect(vp1, vp2, vp3)], t)
sys = expand_connections(compose(simple, [vp1, vp2, vp3]))
@test sort(equations(sys), by = string) == sort([0 .~ collect(vp1.i)
@test ssort(equations(sys)) == ssort([0 .~ collect(vp1.i)
0 .~ collect(vp2.i)
0 .~ collect(vp3.i)
vp1.v[1] ~ vp2.v[1]
vp1.v[2] ~ vp2.v[2]
vp1.v[1] ~ vp3.v[1]
vp1.v[2] ~ vp3.v[2]
0 ~ -vp1.i[1] - vp2.i[1] - vp3.i[1]
0 ~ -vp1.i[2] - vp2.i[2] - vp3.i[2]], by = string)
0 ~ -vp1.i[2] - vp2.i[2] - vp3.i[2]])

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

0 comments on commit 360ddb3

Please sign in to comment.