Skip to content

Handle dummy derivatives and more in initialize_equations #3030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/structural_transformation/symbolics_tearing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ function tearing_reassemble(state::TearingState, var_eq_matching,
# convert it into the mass matrix form.
# We cannot solve the differential variable like D(x)
if isdervar(iv)
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])")
order, lv = var_order(iv)
dx = D(simplify_shifts(lower_varname_withshift(
fullvars[lv], idep, order - 1)))
Expand Down
13 changes: 8 additions & 5 deletions src/systems/nonlinear/initializesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,16 @@ function generate_initializesystem(sys::ODESystem;
end
end

pars = [parameters(sys); get_iv(sys)]
nleqs = if algebraic_only
[eqs_ics; observed(sys)]
else
[eqs_ics; get_initialization_eqs(sys); initialization_eqs; observed(sys)]
if !algebraic_only
for eq in [get_initialization_eqs(sys); initialization_eqs]
_eq = ModelingToolkit.fixpoint_sub(eq, full_diffmap)
push!(eqs_ics, _eq)
end
end

pars = [parameters(sys); get_iv(sys)]
nleqs = [eqs_ics; observed(sys)]

sys_nl = NonlinearSystem(nleqs,
full_states,
pars;
Expand Down
14 changes: 14 additions & 0 deletions test/initializationsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,17 @@ end
sol2 = solve(prob2, Tsit5())
@test all(sol2[x] .== 2) && all(sol2[y] .== 2)
end

# https://github.com/SciML/ModelingToolkit.jl/issues/3029
@testset "Derivatives in Initialization Equations" begin
@variables x(t)
sys = ODESystem(
[D(D(x)) ~ 0], t; initialization_eqs = [x ~ 0, D(x) ~ 1], name = :sys) |>
structural_simplify
@test_nowarn ODEProblem(sys, [], (0.0, 1.0), [])

sys = ODESystem(
[D(D(x)) ~ 0], t; initialization_eqs = [x ~ 0, D(D(x)) ~ 0], name = :sys) |>
structural_simplify
@test_nowarn ODEProblem(sys, [D(x) => 1.0], (0.0, 1.0), [])
end
Loading