Skip to content

Commit 938dc2a

Browse files
Handle dummy derivatives and more in initialize_equations
Also throws a better error for the case where a differential is not caught in the initialization system handling. Fixes #3029
1 parent 741ade3 commit 938dc2a

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/structural_transformation/symbolics_tearing.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching,
453453
# convert it into the mass matrix form.
454454
# We cannot solve the differential variable like D(x)
455455
if isdervar(iv)
456+
isnothing(D) && error("Differential found in a non-differential system. Likely this is a bug in the construction of an initialization system. Please report this issue with a reproducible example. Offending equation: $(equations(sys)[ieq])")
456457
order, lv = var_order(iv)
457458
dx = D(simplify_shifts(lower_varname_withshift(
458459
fullvars[lv], idep, order - 1)))

src/systems/nonlinear/initializesystem.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,16 @@ function generate_initializesystem(sys::ODESystem;
9191
end
9292
end
9393

94-
pars = [parameters(sys); get_iv(sys)]
95-
nleqs = if algebraic_only
96-
[eqs_ics; observed(sys)]
97-
else
98-
[eqs_ics; get_initialization_eqs(sys); initialization_eqs; observed(sys)]
94+
if !algebraic_only
95+
for eq in [get_initialization_eqs(sys); initialization_eqs]
96+
_eq = ModelingToolkit.fixpoint_sub(eq, full_diffmap)
97+
push!(eqs_ics,_eq)
98+
end
9999
end
100100

101+
pars = [parameters(sys); get_iv(sys)]
102+
nleqs = [eqs_ics; observed(sys)]
103+
101104
sys_nl = NonlinearSystem(nleqs,
102105
full_states,
103106
pars;

test/initializationsystem.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,13 @@ end
508508
sol2 = solve(prob2, Tsit5())
509509
@test all(sol2[x] .== 2) && all(sol2[y] .== 2)
510510
end
511+
512+
# https://github.com/SciML/ModelingToolkit.jl/issues/3029
513+
@testset "Derivatives in Initialization Equations" begin
514+
@variables x(t)
515+
sys = ODESystem([D(D(x)) ~ 0], t; initialization_eqs = [x ~ 0, D(x) ~ 1], name = :sys) |> structural_simplify
516+
@test_nowarn ODEProblem(sys, [], (0.0, 1.0), []);
517+
518+
sys = ODESystem([D(D(x)) ~ 0], t; initialization_eqs = [x ~ 0, D(D(x)) ~ 0], name = :sys) |> structural_simplify
519+
@test_nowarn ODEProblem(sys, [D(x) => 1.0], (0.0, 1.0), []);
520+
end

0 commit comments

Comments
 (0)