Skip to content

Commit 1b8134c

Browse files
committed
Fix and clean-up tests
1 parent da56c3f commit 1b8134c

File tree

3 files changed

+11
-57
lines changed

3 files changed

+11
-57
lines changed

src/structural_transformation/utils.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ end
5050
function error_reporting(sys, bad_idxs, n_highest_vars, iseqs)
5151
io = IOBuffer()
5252
if iseqs
53-
error_title = "More equations than variables:\n"
53+
error_title = "More equations than variables, here are the potential extra equation(s):\n"
5454
out_arr = equations(sys)[bad_idxs]
5555
else
56-
error_title = "More variables than equations:\n"
56+
error_title = "More variables than equations, here are the potential extra variable(s):\n"
5757
out_arr = structure(sys).fullvars[bad_idxs]
5858
end
5959

@@ -94,12 +94,11 @@ function check_consistency(sys::AbstractSystem)
9494
assign = matching(graph, varwhitelist) # not assigned
9595
# Just use `error_reporting` to do conditional
9696
iseqs = n_highest_vars < neqs
97-
9897
if iseqs
99-
bad_idxs = findall(isequal(UNASSIGNED), assign)
100-
else
10198
inv_assign = inverse_mapping(assign) # extra equations
102-
bad_idxs = findall(iszero, inv_assign)
99+
bad_idxs = findall(iszero, @view inv_assign[1:nsrcs(graph)])
100+
else
101+
bad_idxs = findall(isequal(UNASSIGNED), assign)
103102
end
104103
error_reporting(sys, bad_idxs, n_highest_vars, iseqs)
105104
end

test/error_handling.jl

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
using Test
2-
using ModelingToolkit#, OrdinaryDiffEq
2+
using ModelingToolkit
33
import ModelingToolkit: ExtraVariablesSystemException, ExtraEquationsSystemException
44

5-
@parameters t
6-
function Pin(;name)
7-
@variables v(t) i(t)
8-
ODESystem(Equation[], t, [v, i], [], name=name, defaults=[v=>1.0, i=>1.0])
9-
end
5+
include("../examples/electrical_components.jl")
106

117
function UnderdefinedConstantVoltage(;name, V = 1.0)
128
val = V
@@ -15,7 +11,8 @@ function UnderdefinedConstantVoltage(;name, V = 1.0)
1511
@parameters V
1612
eqs = [
1713
V ~ p.v - n.v
18-
#0 ~ p.i + n.i
14+
# Remove equation
15+
# 0 ~ p.i + n.i
1916
]
2017
ODESystem(eqs, t, [], [V], systems=[p, n], defaults=Dict(V => val), name=name)
2118
end
@@ -28,53 +25,13 @@ function OverdefinedConstantVoltage(;name, V = 1.0, I = 1.0)
2825
@parameters V I
2926
eqs = [
3027
V ~ p.v - n.v
28+
# Overdefine p.i and n.i
3129
n.i ~ I
3230
p.i ~ I
3331
]
3432
ODESystem(eqs, t, [], [V], systems=[p, n], defaults=Dict(V => val, I => val2), name=name)
3533
end
3634

37-
function Resistor(;name, R = 1.0)
38-
val = R
39-
@named p = Pin()
40-
@named n = Pin()
41-
@variables v(t)
42-
@parameters R
43-
eqs = [
44-
v ~ p.v - n.v
45-
0 ~ p.i + n.i
46-
v ~ p.i * R
47-
]
48-
ODESystem(eqs, t, [v], [R], systems=[p, n], defaults=Dict(R => val), name=name)
49-
end
50-
51-
function Capacitor(;name, C = 1.0)
52-
val = C
53-
@named p = Pin()
54-
@named n = Pin()
55-
@variables v(t)
56-
@parameters C
57-
D = Differential(t)
58-
eqs = [
59-
v ~ p.v - n.v
60-
0 ~ p.i + n.i
61-
D(v) ~ p.i / C
62-
]
63-
ODESystem(eqs, t, [v], [C], systems=[p, n], defaults=Dict(C => val), name=name)
64-
end
65-
66-
function ModelingToolkit.connect(ps...)
67-
eqs = [
68-
0 ~ sum(p->p.i, ps) # KCL
69-
]
70-
# KVL
71-
for i in 1:length(ps)-1
72-
push!(eqs, ps[i].v ~ ps[i+1].v)
73-
end
74-
75-
return eqs
76-
end
77-
7835
R = 1.0
7936
C = 1.0
8037
V = 1.0
@@ -89,8 +46,6 @@ rc_eqs = [
8946
]
9047

9148
@named rc_model = ODESystem(rc_eqs, t, systems=[resistor, capacitor, source])
92-
93-
9449
@test_throws ModelingToolkit.ExtraVariablesSystemException structural_simplify(rc_model)
9550

9651

test/reduction.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ eqs = [
233233
]
234234

235235
@named sys = ODESystem(eqs, t, [E, C, S, P], [k₁, k₂, k₋₁, E₀])
236-
@test_throws ModelingToolkit.InvalidSystemException structural_simplify(sys)
236+
@test_throws ModelingToolkit.ExtraEquationsSystemException structural_simplify(sys)
237237

238238
# Example 5 from Pantelides' original paper
239239
@parameters t

0 commit comments

Comments
 (0)