Skip to content

Commit a895879

Browse files
authored
Merge pull request #895 from SciML/myb/errmsg
Throw an error when D(x) appears in the RHS
2 parents ed2de3e + 4624c8a commit a895879

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

src/systems/abstractsystem.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,3 +537,8 @@ end
537537
end
538538

539539
Base.show(io::IO, ::MIME"text/latex", x::AbstractSystem) = print(io, latexify(x))
540+
541+
struct InvalidSystemException <: Exception
542+
msg::String
543+
end
544+
Base.showerror(io::IO, e::InvalidSystemException) = print(io, "InvalidSystemException: ", e.msg)

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,29 @@ function generate_jacobian(sys::AbstractODESystem, dvs = states(sys), ps = param
4747
return build_function(jac, dvs, ps, get_iv(sys); kwargs...)
4848
end
4949

50+
@noinline function throw_invalid_derivative(dervar, eq)
51+
msg = "The derivative variable must be isolated to the left-hand " *
52+
"side of the equation like `$dervar ~ ...`.\n Got $eq."
53+
throw(InvalidSystemException(msg))
54+
end
55+
56+
function check_derivative_variables(eq, expr=eq.rhs)
57+
istree(expr) || return nothing
58+
if operation(expr) isa Differential
59+
throw_invalid_derivative(expr, eq)
60+
end
61+
foreach(Base.Fix1(check_derivative_variables, eq), arguments(expr))
62+
end
63+
5064
function generate_function(sys::AbstractODESystem, dvs = states(sys), ps = parameters(sys); kwargs...)
5165
# optimization
5266
#obsvars = map(eq->eq.lhs, observed(sys))
5367
#fulldvs = [dvs; obsvars]
5468

69+
eqs = equations(sys)
70+
foreach(check_derivative_variables, eqs)
5571
# substitute x(t) by just x
56-
rhss = [deq.rhs for deq equations(sys)]
72+
rhss = [deq.rhs for deq in eqs]
5773
#obss = [makesym(value(eq.lhs)) ~ substitute(eq.rhs, sub) for eq ∈ observed(sys)]
5874
#rhss = Let(obss, rhss)
5975

src/systems/systemstructure.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module SystemStructures
33
using DataStructures
44
using SymbolicUtils: istree, operation, arguments, Symbolic
55
using ..ModelingToolkit
6-
import ..ModelingToolkit: isdiffeq, var_from_nested_derivative, vars!, flatten, value
6+
import ..ModelingToolkit: isdiffeq, var_from_nested_derivative, vars!, flatten,
7+
value, InvalidSystemException
78
using ..BipartiteGraphs
89
using UnPack
910
using Setfield
@@ -130,7 +131,9 @@ function init_graph(sys)
130131
if istree(var) && operation(var) isa Differential
131132
isalgeq = false
132133
diffvar = arguments(var)[1]
133-
@assert !(diffvar isa Differential) "The equation [ $eq ] is not first order"
134+
if diffvar isa Differential
135+
throw(InvalidSystemException("The equation [ $eq ] is not first order"))
136+
end
134137
push!(dervars, var)
135138
push!(diffvars, diffvar)
136139
end

test/reduction.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ let
167167
@test ref_eqs == equations(reduced_sys)
168168
end
169169

170+
# issue #889
171+
let
172+
@parameters t
173+
D = Differential(t)
174+
@variables x(t)
175+
@named sys = ODESystem([0 ~ D(x) + x], t, [x], [])
176+
sys = structural_simplify(sys)
177+
@test_throws ModelingToolkit.InvalidSystemException ODEProblem(sys, [1.0], (0, 10.0))
178+
end
179+
170180
# NonlinearSystem
171181
@parameters t
172182
@variables u1(t) u2(t) u3(t)

0 commit comments

Comments
 (0)