Skip to content

Commit

Permalink
Merge pull request #3050 from hersle/fix_derivative_guesses
Browse files Browse the repository at this point in the history
Respect D(x)-like guesses
  • Loading branch information
ChrisRackauckas authored Sep 16, 2024
2 parents 39b6221 + 556d09e commit b84b1b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/systems/nonlinear/initializesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function generate_initializesystem(sys::ODESystem;

full_states = unique([sts; getfield.((observed(sys)), :lhs)])
set_full_states = Set(full_states)
guesses = todict(guesses)
guesses = merge(get_guesses(sys), todict(guesses))
schedule = getfield(sys, :schedule)

if schedule !== nothing
Expand Down Expand Up @@ -69,7 +69,6 @@ function generate_initializesystem(sys::ODESystem;
end

defs = merge(defaults(sys), filtered_u0)
guesses = merge(get_guesses(sys), todict(guesses), dd_guess)

for st in full_states
if st keys(defs)
Expand Down
16 changes: 15 additions & 1 deletion test/initializationsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ end
end

# https://github.com/SciML/ModelingToolkit.jl/issues/3029
@testset "Derivatives in Initialization Equations" begin
@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) |>
Expand All @@ -523,6 +523,20 @@ end
@test_nowarn ODEProblem(sys, [D(x) => 1.0], (0.0, 1.0), [])
end

# https://github.com/SciML/ModelingToolkit.jl/issues/3049
@testset "Derivatives in initialization guesses" begin
for sign in [-1.0, +1.0]
@variables x(t)
sys = ODESystem(
[D(D(x)) ~ 0], t;
initialization_eqs = [D(x)^2 ~ 1, x ~ 0], guesses = [D(x) => sign], name = :sys
) |> structural_simplify
prob = ODEProblem(sys, [], (0.0, 1.0), [])
sol = solve(prob, Tsit5())
@test sol(1.0, idxs = sys.x) sign # system with D(x(0)) = ±1 should solve to x(1) = ±1
end
end

# https://github.com/SciML/ModelingToolkit.jl/issues/2619
@parameters k1 k2 ω
@variables X(t) Y(t)
Expand Down

0 comments on commit b84b1b5

Please sign in to comment.