Skip to content

Commit f47c5d6

Browse files
feat: improve error message when system contains array equations
1 parent 07701c4 commit f47c5d6

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
@@ -2855,6 +2855,15 @@ function Base.eltype(::Type{<:TreeIterator{ModelingToolkit.AbstractSystem}})
28552855
ModelingToolkit.AbstractSystem
28562856
end
28572857

2858+
function check_array_equations_unknowns(eqs, dvs)
2859+
if any(eq -> Symbolics.isarraysymbolic(eq.lhs), eqs)
2860+
throw(ArgumentError("The system has array equations. Call `structural_simplify` to handle such equations or scalarize them manually."))
2861+
end
2862+
if any(x -> Symbolics.isarraysymbolic(x), dvs)
2863+
throw(ArgumentError("The system has array unknowns. Call `structural_simplify` to handle this or scalarize them manually."))
2864+
end
2865+
end
2866+
28582867
function check_eqs_u0(eqs, dvs, u0; check_length = true, kwargs...)
28592868
if u0 !== nothing
28602869
if check_length

src/systems/diffeqs/abstractodesystem.jl

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

792+
check_array_equations_unknowns(eqs, dvs)
792793
# TODO: Pass already computed information to varmap_to_vars call
793794
# in process_u0? That would just be a small optimization
794795
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
@@ -1394,3 +1394,34 @@ end
13941394
prob = @test_nowarn ODEProblem(sys, nothing, (0.0, 1.0))
13951395
@test_nowarn solve(prob)
13961396
end
1397+
1398+
@testset "Issue #2597" begin
1399+
@variables x(t)[1:2]=ones(2) y(t)=1.0
1400+
1401+
for eqs in [D(x) ~ x, collect(D(x) .~ x)]
1402+
for dvs in [[x], collect(x)]
1403+
@named sys = ODESystem(eqs, t, dvs, [])
1404+
sys = complete(sys)
1405+
if eqs isa Vector && length(eqs) == 2 && length(dvs) == 2
1406+
@test_nowarn ODEProblem(sys, [], (0.0, 1.0))
1407+
else
1408+
@test_throws [
1409+
r"array (equations|unknowns)", "structural_simplify", "scalarize"] ODEProblem(
1410+
sys, [], (0.0, 1.0))
1411+
end
1412+
end
1413+
end
1414+
for eqs in [[D(x) ~ x, D(y) ~ y], [collect(D(x) .~ x); D(y) ~ y]]
1415+
for dvs in [[x, y], [x..., y]]
1416+
@named sys = ODESystem(eqs, t, dvs, [])
1417+
sys = complete(sys)
1418+
if eqs isa Vector && length(eqs) == 3 && length(dvs) == 3
1419+
@test_nowarn ODEProblem(sys, [], (0.0, 1.0))
1420+
else
1421+
@test_throws [
1422+
r"array (equations|unknowns)", "structural_simplify", "scalarize"] ODEProblem(
1423+
sys, [], (0.0, 1.0))
1424+
end
1425+
end
1426+
end
1427+
end

0 commit comments

Comments
 (0)