diff --git a/src/utils.jl b/src/utils.jl index dc239e34c1..4f77715d2a 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -171,7 +171,7 @@ end """ check_equations(eqs, iv) -Assert that equations are well-formed when building ODE, i.e., only containing a single independent variable. +Assert that ODE equations are well-formed. """ function check_equations(eqs, iv) ivs = collect_ivs(eqs) @@ -183,6 +183,17 @@ function check_equations(eqs, iv) isequal(single_iv, iv) || throw(ArgumentError("Differential w.r.t. variable ($single_iv) other than the independent variable ($iv) are not allowed.")) end + + for eq in eqs + vars, pars = collect_vars(eq, iv) + if isempty(vars) + if isempty(pars) + throw(ArgumentError("Equation $eq contains no variables or parameters.")) + else + throw(ArgumentError("Equation $eq contains only parameters, but relationships between parameters should be specified with defaults or parameter_dependencies.")) + end + end + end end """ Get all the independent variables with respect to which differentials are taken. @@ -439,6 +450,12 @@ function find_derivatives!(vars, expr, f) return vars end +function collect_vars(args...; kwargs...) + unknowns, parameters = [], [] + collect_vars!(unknowns, parameters, args...; kwargs...) + return unknowns, parameters +end + function collect_vars!(unknowns, parameters, expr, iv; op = Differential) if issym(expr) collect_var!(unknowns, parameters, expr, iv) diff --git a/test/odesystem.jl b/test/odesystem.jl index ab28f5cb47..04331008a1 100644 --- a/test/odesystem.jl +++ b/test/odesystem.jl @@ -420,7 +420,14 @@ der = Differential(w) eqs = [ der(u1) ~ t ] -@test_throws ArgumentError ModelingToolkit.ODESystem(eqs, t, vars, pars, name = :foo) +@test_throws ArgumentError ODESystem(eqs, t, vars, pars, name = :foo) + + # equations without variables are forbidden + # https://github.com/SciML/ModelingToolkit.jl/issues/2727 +@parameters p q +@test_throws ArgumentError ODESystem([p ~ q], t; name = :foo) +@test_throws ArgumentError ODESystem([p ~ 1], t; name = :foo) +@test_throws ArgumentError ODESystem([1 ~ 2], t; name = :foo) @variables x(t) @parameters M b k