Skip to content

Commit ccf4af6

Browse files
authored
Merge pull request #1425 from SciML/myb/check
Fix #1413 and #1389; check the validity of the equations before building the function
2 parents 018cfa0 + a0f87f1 commit ccf4af6

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

src/systems/abstractsystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ function structural_simplify(sys::AbstractSystem; simplify=false)
911911
sys = initialize_system_structure(alias_elimination(sys))
912912
check_consistency(sys)
913913
if sys isa ODESystem
914-
sys = initialize_system_structure(dae_index_lowering(sys))
914+
sys = initialize_system_structure(dae_index_lowering(ode_order_lowering(sys)))
915915
end
916916
sys = tearing(sys, simplify=simplify)
917917
fullstates = [map(eq->eq.lhs, observed(sys)); states(sys)]

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ function generate_function(
9090

9191
eqs = [eq for eq in equations(sys) if !isdifferenceeq(eq)]
9292
foreach(check_derivative_variables, eqs)
93+
check_lhs(eqs, Differential, Set(dvs))
9394
# substitute x(t) by just x
9495
rhss = implicit_dae ? [_iszero(eq.lhs) ? eq.rhs : eq.rhs - eq.lhs for eq in eqs] :
9596
[eq.rhs for eq in eqs]

src/utils.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ function check_variables(dvs, iv)
130130
end
131131
end
132132

133+
function check_lhs(eq::Equation, op, dvs::Set)
134+
v = unwrap(eq.lhs)
135+
_iszero(v) && return
136+
(operation(v) isa op && only(arguments(v)) in dvs) && return
137+
error("$v is not a valid LHS. Please run structural_simplify before simulation.")
138+
end
139+
check_lhs(eqs, op, dvs::Set) = for eq in eqs
140+
check_lhs(eq, op, dvs)
141+
end
142+
133143
"""
134144
collect_ivs(eqs, op = Differential)
135145

test/odesystem.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,21 @@ RHS2 = RHS
517517
@unpack RHS = fol
518518
@test isequal(RHS, RHS2)
519519

520+
#1413 and 1389
521+
@parameters t α β
522+
@variables x(t) y(t) z(t)
523+
D = Differential(t)
524+
525+
eqs = [
526+
D(x) ~ 0.1x + 0.9y,
527+
D(y) ~ 0.5x + 0.5y,
528+
z ~ α*x - β*y
529+
]
530+
531+
@named sys = ODESystem(eqs, t, [x,y,z],[α,β])
532+
@test_throws Any ODEFunction(sys)
533+
534+
eqs = copy(eqs)
535+
eqs[end] = D(D(z)) ~ α*x - β*y
536+
@named sys = ODESystem(eqs, t, [x,y,z],[α,β])
537+
@test_throws Any ODEFunction(sys)

0 commit comments

Comments
 (0)