Skip to content

Commit 271d4cd

Browse files
Merge pull request #3030 from SciML/initialize_eqs_dd
Handle dummy derivatives and more in initialize_equations
2 parents 741ade3 + 9f535c7 commit 271d4cd

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/structural_transformation/symbolics_tearing.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ 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) &&
457+
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])")
456458
order, lv = var_order(iv)
457459
dx = D(simplify_shifts(lower_varname_withshift(
458460
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,17 @@ 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(
516+
[D(D(x)) ~ 0], t; initialization_eqs = [x ~ 0, D(x) ~ 1], name = :sys) |>
517+
structural_simplify
518+
@test_nowarn ODEProblem(sys, [], (0.0, 1.0), [])
519+
520+
sys = ODESystem(
521+
[D(D(x)) ~ 0], t; initialization_eqs = [x ~ 0, D(D(x)) ~ 0], name = :sys) |>
522+
structural_simplify
523+
@test_nowarn ODEProblem(sys, [D(x) => 1.0], (0.0, 1.0), [])
524+
end

0 commit comments

Comments
 (0)