Skip to content

Commit 018ac8d

Browse files
authored
Merge pull request #1555 from SciML/myb/error_check
Fix consistency checks
2 parents 41cc6ea + 3516e71 commit 018ac8d

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/bipartite_graph.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,18 @@ end
6868

6969
function complete(m::Matching{U}) where {U}
7070
m.inv_match !== nothing && return m
71-
inv_match = Union{U, Int}[unassigned for _ = 1:length(m.match)]
71+
N = length(m.match)
72+
inv_match = Union{U, Int}[unassigned for _ = 1:N]
7273
for (i, eq) in enumerate(m.match)
7374
isa(eq, Int) || continue
74-
inv_match[eq] = i
75+
if eq <= N
76+
inv_match[eq] = i
77+
else # this only happens when the system is imbalanced
78+
for k in N+1:eq-1
79+
push!(inv_match, unassigned)
80+
end
81+
push!(inv_match, i)
82+
end
7583
end
7684
return Matching{U}(collect(m.match), inv_match)
7785
end

src/structural_transformation/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function check_consistency(state::TearingState)
5959
iseqs = n_highest_vars < neqs
6060
if iseqs
6161
eq_var_matching = invview(complete(var_eq_matching)) # extra equations
62-
bad_idxs = findall(isnothing, @view eq_var_matching[1:nsrcs(graph)])
62+
bad_idxs = findall(isequal(unassigned), @view eq_var_matching[1:nsrcs(graph)])
6363
else
6464
bad_idxs = findall(isequal(unassigned), var_eq_matching)
6565
end

test/odesystem.jl

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ let
683683
@test length(equations(structural_simplify(sys))) == 2
684684
end
685685

686-
let
686+
let
687687
eq_to_lhs(eq) = eq.lhs - eq.rhs ~ 0
688688
eqs_to_lhs(eqs) = eq_to_lhs.(eqs)
689689

@@ -708,7 +708,7 @@ let
708708
D2(u[2]) ~ u[1] * (rho - u[3]) - u[2],
709709
D2(u[3]) ~ u[1] * u[2] - beta * u[3]]
710710
eqs3 = eqs_to_lhs(eqs3)
711-
711+
712712
eqs4 = [
713713
D2(y2) ~ x2 * (rho - z2) - y2,
714714
D2(x2) ~ sigma * (y2 - x2),
@@ -724,9 +724,24 @@ let
724724
@test ModelingToolkit.isisomorphic(sys1, sys2)
725725
@test !ModelingToolkit.isisomorphic(sys1, sys3)
726726
@test ModelingToolkit.isisomorphic(sys1, ssys3) # I don't call structural_simplify in isisomorphic
727-
@test !ModelingToolkit.isisomorphic(sys1, sys4)
727+
@test !ModelingToolkit.isisomorphic(sys1, sys4)
728728

729729
# 1281
730730
iv2 = only(independent_variables(sys2))
731731
@test isequal(only(independent_variables(convert_system(ODESystem, sys1, iv2))), iv2)
732-
end
732+
end
733+
734+
let
735+
@variables t
736+
vars = @variables sP(t) spP(t) spm(t) sph(t)
737+
pars = @parameters a b
738+
eqs = [
739+
sP ~ 1
740+
spP ~ sP
741+
spm ~ a
742+
sph ~ b
743+
spm ~ 0
744+
]
745+
@named sys = ODESystem(eqs, t, vars, pars)
746+
@test_throws ModelingToolkit.ExtraEquationsSystemException structural_simplify(sys)
747+
end

0 commit comments

Comments
 (0)