Skip to content

Commit 8dea9b1

Browse files
feat: improve error message when system contains array equations
1 parent 7d7316e commit 8dea9b1

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/systems/abstractsystem.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,6 +2878,15 @@ function Base.eltype(::Type{<:TreeIterator{ModelingToolkit.AbstractSystem}})
28782878
ModelingToolkit.AbstractSystem
28792879
end
28802880

2881+
function check_array_equations_unknowns(eqs, dvs)
2882+
if any(eq -> Symbolics.isarraysymbolic(eq.lhs), eqs)
2883+
throw(ArgumentError("The system has array equations. Call `structural_simplify` to handle such equations or scalarize them manually."))
2884+
end
2885+
if any(x -> Symbolics.isarraysymbolic(x), dvs)
2886+
throw(ArgumentError("The system has array unknowns. Call `structural_simplify` to handle this or scalarize them manually."))
2887+
end
2888+
end
2889+
28812890
function check_eqs_u0(eqs, dvs, u0; check_length = true, kwargs...)
28822891
if u0 !== nothing
28832892
if check_length

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ function process_DEProblem(constructor, sys::AbstractODESystem, u0map, parammap;
815815
ps = parameters(sys)
816816
iv = get_iv(sys)
817817

818+
check_array_equations_unknowns(eqs, dvs)
818819
# TODO: Pass already computed information to varmap_to_vars call
819820
# in process_u0? That would just be a small optimization
820821
varmap = u0map === nothing || isempty(u0map) || eltype(u0map) <: Number ?

test/odesystem.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,3 +1404,34 @@ end
14041404
@test !ModelingToolkit.is_dde(sys)
14051405
@test is_markovian(sys)
14061406
end
1407+
1408+
@testset "Issue #2597" begin
1409+
@variables x(t)[1:2]=ones(2) y(t)=1.0
1410+
1411+
for eqs in [D(x) ~ x, collect(D(x) .~ x)]
1412+
for dvs in [[x], collect(x)]
1413+
@named sys = ODESystem(eqs, t, dvs, [])
1414+
sys = complete(sys)
1415+
if eqs isa Vector && length(eqs) == 2 && length(dvs) == 2
1416+
@test_nowarn ODEProblem(sys, [], (0.0, 1.0))
1417+
else
1418+
@test_throws [
1419+
r"array (equations|unknowns)", "structural_simplify", "scalarize"] ODEProblem(
1420+
sys, [], (0.0, 1.0))
1421+
end
1422+
end
1423+
end
1424+
for eqs in [[D(x) ~ x, D(y) ~ y], [collect(D(x) .~ x); D(y) ~ y]]
1425+
for dvs in [[x, y], [x..., y]]
1426+
@named sys = ODESystem(eqs, t, dvs, [])
1427+
sys = complete(sys)
1428+
if eqs isa Vector && length(eqs) == 3 && length(dvs) == 3
1429+
@test_nowarn ODEProblem(sys, [], (0.0, 1.0))
1430+
else
1431+
@test_throws [
1432+
r"array (equations|unknowns)", "structural_simplify", "scalarize"] ODEProblem(
1433+
sys, [], (0.0, 1.0))
1434+
end
1435+
end
1436+
end
1437+
end

0 commit comments

Comments
 (0)