Skip to content

Commit

Permalink
Check that equations contain variables
Browse files Browse the repository at this point in the history
  • Loading branch information
hersle committed Jul 16, 2024
1 parent a3ad348 commit 47cdd3b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion test/odesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 47cdd3b

Please sign in to comment.