Skip to content

Commit

Permalink
Merge pull request #1275 from ranocha/resize
Browse files Browse the repository at this point in the history
support resize! with non-Int size; help wanted
  • Loading branch information
ChrisRackauckas authored Sep 24, 2020
2 parents 66ed00b + 6a35432 commit 821fc83
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ DiffEqDevTools = "f3b72e0c-5b89-59e1-b016-84e28bfd966d"
DiffEqOperators = "9fdde737-9c7f-55bf-ade8-46b3f136cc48"
DiffEqProblemLibrary = "a077e3f3-b75c-5d7f-a0c6-6bc4c8ec64a9"
DiffEqSensitivity = "41bf760c-e81c-5289-8e54-58b1f1f8abe2"
ElasticArrays = "fdbdab4c-e67f-52f5-8c3f-e7b388dad3d4"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Expand All @@ -66,4 +67,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[targets]
test = ["Calculus", "DelayDiffEq", "DiffEqCallbacks", "DiffEqDevTools", "DiffEqOperators", "DiffEqProblemLibrary", "DiffEqSensitivity", "InteractiveUtils", "PoissonRandom", "Printf", "Random", "SafeTestsets", "SparseArrays", "Statistics", "Test", "Unitful", "ModelingToolkit", "Pkg"]
test = ["Calculus", "DelayDiffEq", "DiffEqCallbacks", "DiffEqDevTools", "DiffEqOperators", "DiffEqProblemLibrary", "DiffEqSensitivity", "ElasticArrays", "InteractiveUtils", "PoissonRandom", "Printf", "Random", "SafeTestsets", "SparseArrays", "Statistics", "Test", "Unitful", "ModelingToolkit", "Pkg"]
12 changes: 12 additions & 0 deletions src/integrators/integrator_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ function resize!(integrator::ODEIntegrator, i::Int)
resize_J_W!(cache, integrator, i)
resize_non_user_cache!(integrator, cache, i)
end
# we can't use resize!(..., i::Union{Int, NTuple{N,Int}}) where {N} because of method ambiguities with DiffEqBase
function resize!(integrator::ODEIntegrator, i::NTuple{N,Int}) where {N}
@unpack cache = integrator

for c in full_cache(cache)
resize!(c,i)
end
# TODO the parts below need to be adapted for implicit methods
isdefined(integrator.cache, :nlsolver) && resize_nlsolver!(integrator, i)
resize_J_W!(cache, integrator, i)
resize_non_user_cache!(integrator, cache, i)
end

function resize_J_W!(cache, integrator, i)
(isdefined(cache, :J) && isdefined(cache, :W)) || return
Expand Down
20 changes: 20 additions & 0 deletions test/integrators/ode_cache_tests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using OrdinaryDiffEq, DiffEqBase, DiffEqCallbacks, Test
using Random
using ElasticArrays
Random.seed!(213)
CACHE_TEST_ALGS = [Euler(),Midpoint(),RK4(),SSPRK22(),SSPRK33(),
CarpenterKennedy2N54(), HSLDDRK64(),
Expand Down Expand Up @@ -67,6 +68,25 @@ for alg in broken_CACHE_TEST_ALGS
@test_broken length(solve(prob,alg,callback=callback,dt=1/2)[end]) > 1
end

# cache tests resizing multidimensional arrays
u0_matrix = ElasticArray(ones(2, 2))
f_matrix = (du, u, p, t) -> du .= u
prob_matrix = ODEProblem(f_matrix, u0_matrix, (0.0, 2.0))
condition_matrix = (u, t, integrator) -> t - 1
affect_matrix! = function (integrator)
resize!(integrator, (2,3))
integrator.u .= 1
nothing
end
callback_matrix = ContinuousCallback(condition_matrix, affect_matrix!)

for alg in CACHE_TEST_ALGS
OrdinaryDiffEq.isimplicit(alg) && continue # this restriction should be removed in the future
@show alg
sol = solve(prob_matrix, alg,callback=callback_matrix, dt=1/2)
@test size(sol[end]) == (2,3)
end


sol = solve(prob,Rodas4(chunk_size=1),callback=callback,dt=1/2)
@test length(sol[end]) > 1
Expand Down

0 comments on commit 821fc83

Please sign in to comment.