From 16ba307317d7a6d2e75723f271382d7e0243f512 Mon Sep 17 00:00:00 2001 From: oscarddssmith Date: Tue, 6 Feb 2024 11:12:35 -0500 Subject: [PATCH 1/2] Fix IDA for callbacks that change p --- Project.toml | 3 ++- src/common_interface/integrator_utils.jl | 2 ++ test/common_interface/ida.jl | 11 +++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index fcd757f..51d3892 100644 --- a/Project.toml +++ b/Project.toml @@ -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" @@ -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"] diff --git a/src/common_interface/integrator_utils.jl b/src/common_interface/integrator_utils.jl index 87642bf..b6004e9 100644 --- a/src/common_interface/integrator_utils.jl +++ b/src/common_interface/integrator_utils.jl @@ -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 diff --git a/test/common_interface/ida.jl b/test/common_interface/ida.jl index 75c14cc..481f49d 100644 --- a/test/common_interface/ida.jl +++ b/test/common_interface/ida.jl @@ -133,3 +133,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] From 3024c4ab324925772bdd47574c39adeea4031044 Mon Sep 17 00:00:00 2001 From: oscarddssmith Date: Wed, 7 Feb 2024 10:02:12 -0500 Subject: [PATCH 2/2] actually use DiffEqCallbacks --- test/common_interface/ida.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/common_interface/ida.jl b/test/common_interface/ida.jl index 481f49d..7932c28 100644 --- a/test/common_interface/ida.jl +++ b/test/common_interface/ida.jl @@ -1,6 +1,7 @@ using Sundials, Test using SciMLBase: NoInit using DAEProblemLibrary: prob_dae_resrob +using DiffEqCallbacks # Test DAE mutable struct precflags