Skip to content
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

Fix IDA for callbacks that change p #448

Merged
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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ julia = "1.9"
[extras]
AlgebraicMultigrid = "2169fc97-5a83-5252-b627-83903c6c433c"
DAEProblemLibrary = "dfb8ca35-80a1-48ba-a605-84916a45b4f8"
DiffEqCallbacks = "459566f4-90b8-5000-8ac3-15dfb0a30def"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
IncompleteLU = "40713840-3770-5561-ab4c-a76e7d0d7895"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
Expand All @@ -38,4 +39,4 @@ SparsityTracing = "06eadbd4-12ad-4cbc-ab6e-10f8370940a5"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "AlgebraicMultigrid", "ODEProblemLibrary", "DAEProblemLibrary", "ForwardDiff", "SparseDiffTools", "SparsityTracing", "IncompleteLU", "ModelingToolkit"]
test = ["Test", "AlgebraicMultigrid", "DiffEqCallbacks", "ODEProblemLibrary", "DAEProblemLibrary", "ForwardDiff", "SparseDiffTools", "SparsityTracing", "IncompleteLU", "ModelingToolkit"]
2 changes: 2 additions & 0 deletions src/common_interface/integrator_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,11 @@ end
end

function DiffEqBase.reeval_internals_due_to_modification!(integrator::AbstractSundialsIntegrator)
integrator.userfun.p = integrator.p
nothing
end
function DiffEqBase.reeval_internals_due_to_modification!(integrator::IDAIntegrator)
integrator.userfun.p = integrator.p
handle_callback_modifiers!(integrator::IDAIntegrator)
end

Expand Down
12 changes: 12 additions & 0 deletions test/common_interface/ida.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Sundials, Test
using SciMLBase: NoInit
using DAEProblemLibrary: prob_dae_resrob
using DiffEqCallbacks

# Test DAE
mutable struct precflags
Expand Down Expand Up @@ -133,3 +134,14 @@ sol = solve(prob, IDA())
# If this is one, it incorrectly saved u, if it is 0., it incorrectly solved
# the pre-init value rather than the post-init one.
@test sol(0.0, Val{1})[1] ≈ 11.0

# test that callbacks modifying p get the new p
daefun = (du, u, p, t) -> [du[1] - u[2], u[2] - p]
callback = PresetTimeCallback(.5, integ->(integ.p=-integ.p;))
prob = DAEProblem(daefun, [0.0, 0.0], [0.0,-1.0], (0., 1), 1;
differential_vars = [true, false], callback)
sol = solve(prob, IDA())
@test sol.retcode == ReturnCode.Success
# test that the callback flipping p caused u[2] to get flipped.
first_t = findfirst(isequal(0.5), sol.t)
@test sol.u[first_t][2] == -sol.u[first_t+1][2]
Loading