Skip to content

Commit 47cdd3b

Browse files
committed
Check that equations contain variables
1 parent a3ad348 commit 47cdd3b

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/utils.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ end
171171
"""
172172
check_equations(eqs, iv)
173173
174-
Assert that equations are well-formed when building ODE, i.e., only containing a single independent variable.
174+
Assert that ODE equations are well-formed.
175175
"""
176176
function check_equations(eqs, iv)
177177
ivs = collect_ivs(eqs)
@@ -183,6 +183,17 @@ function check_equations(eqs, iv)
183183
isequal(single_iv, iv) ||
184184
throw(ArgumentError("Differential w.r.t. variable ($single_iv) other than the independent variable ($iv) are not allowed."))
185185
end
186+
187+
for eq in eqs
188+
vars, pars = collect_vars(eq, iv)
189+
if isempty(vars)
190+
if isempty(pars)
191+
throw(ArgumentError("Equation $eq contains no variables or parameters."))
192+
else
193+
throw(ArgumentError("Equation $eq contains only parameters, but relationships between parameters should be specified with defaults or parameter_dependencies."))
194+
end
195+
end
196+
end
186197
end
187198
"""
188199
Get all the independent variables with respect to which differentials are taken.
@@ -439,6 +450,12 @@ function find_derivatives!(vars, expr, f)
439450
return vars
440451
end
441452

453+
function collect_vars(args...; kwargs...)
454+
unknowns, parameters = [], []
455+
collect_vars!(unknowns, parameters, args...; kwargs...)
456+
return unknowns, parameters
457+
end
458+
442459
function collect_vars!(unknowns, parameters, expr, iv; op = Differential)
443460
if issym(expr)
444461
collect_var!(unknowns, parameters, expr, iv)

test/odesystem.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,14 @@ der = Differential(w)
420420
eqs = [
421421
der(u1) ~ t
422422
]
423-
@test_throws ArgumentError ModelingToolkit.ODESystem(eqs, t, vars, pars, name = :foo)
423+
@test_throws ArgumentError ODESystem(eqs, t, vars, pars, name = :foo)
424+
425+
# equations without variables are forbidden
426+
# https://github.com/SciML/ModelingToolkit.jl/issues/2727
427+
@parameters p q
428+
@test_throws ArgumentError ODESystem([p ~ q], t; name = :foo)
429+
@test_throws ArgumentError ODESystem([p ~ 1], t; name = :foo)
430+
@test_throws ArgumentError ODESystem([1 ~ 2], t; name = :foo)
424431

425432
@variables x(t)
426433
@parameters M b k

0 commit comments

Comments
 (0)