From af7c2db0c2dbf5ceb75b0c54251af5b24af1c0d9 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Wed, 7 Oct 2026 12:42:38 -0400 Subject: [PATCH 1/2] Preallocate Magnus NC6/Gauss4 stage and commutator matrices MagnusNC6 and MagnusGauss4 rebuilt every stage snapshot, intermediate B/commutator matrix, and the final generator from scratch each step via deepcopy, matrix products with `*`, and repeated subexpressions, allocating ~1.4 MB/step (N=50, time-dependent) and ~22.7 MB/step (N=200, constant) for NC6, and ~223 KB/~3.5 MB/step for Gauss4. The caches now preallocate every stage/commutator buffer and the perform_step! bodies use copyto!/mul!/@. to fill them in place, cutting step allocation to ~61 KB (N=50) and ~962 KB (N=200) for both algorithms. The remaining bytes come from ExponentialUtilities.exponential!'s internal Pade scratch matrices, which the `ExpMethodGeneric` cache argument does not reuse; that is a separate, upstream-scoped allocation source. Every rewritten expression keeps the original's literal operator grouping (e.g. `3 * B0 / 2`, not `1.5 * B0`) so the floating-point rounding is unchanged; sol.u hashes are bitwise identical to master on a time-dependent N=50 and a constant N=200 problem at fixed dt. Adds a runtime allocation-bound regression test that fails on unfixed master (1,431,464 and 222,664 bytes/step against a 150,000 bound) and passes after the fix. Co-Authored-By: Chris Rackauckas Co-Authored-By: Claude Sonnet 5.5 Agent-Harness: Claude Code Agent-Model: claude-sonnet-5-5 Agent-Session: https://claude.ai/code/session_01LPHREnnonfLg1VcE1EJovv --- lib/OrdinaryDiffEqLinear/src/linear_caches.jl | 42 ++++++++- .../src/linear_perform_step.jl | 92 +++++++++++++------ .../test/qa/allocation_tests.jl | 44 +++++++++ 3 files changed, 146 insertions(+), 32 deletions(-) diff --git a/lib/OrdinaryDiffEqLinear/src/linear_caches.jl b/lib/OrdinaryDiffEqLinear/src/linear_caches.jl index f482308450a..db96a3a8a61 100644 --- a/lib/OrdinaryDiffEqLinear/src/linear_caches.jl +++ b/lib/OrdinaryDiffEqLinear/src/linear_caches.jl @@ -401,7 +401,8 @@ function alg_cache( return MagnusGL8ConstantCache() end -@cache struct MagnusNC6Cache{uType, rateType, WType, expType} <: LinearMutableCache +@cache struct MagnusNC6Cache{uType, rateType, WType, expType, matType} <: + LinearMutableCache u::uType uprev::uType uprev2::uType @@ -410,6 +411,23 @@ end W::WType k::rateType exp_cache::expType + A0::matType + A1::matType + A2::matType + A3::matType + B0::matType + B1::matType + B2::matType + C1::matType + D::matType + E::matType + F::matType + G::matType + P1::matType + P2::matType + Ω2::matType + Ω3_4::matType + Ωtot::matType end function alg_cache( @@ -422,7 +440,15 @@ function alg_cache( k = zero(rate_prototype) fsalfirst = zero(rate_prototype) exp_cache = ExponentialUtilities.alloc_mem(f, ExpMethodGeneric()) - return MagnusNC6Cache(u, uprev, uprev2, zero(u), fsalfirst, W, k, exp_cache) + matbuf() = similar(W) + return MagnusNC6Cache( + u, uprev, uprev2, zero(u), fsalfirst, W, k, exp_cache, + matbuf(), matbuf(), matbuf(), matbuf(), # A0, A1, A2, A3 + matbuf(), matbuf(), matbuf(), # B0, B1, B2 + matbuf(), matbuf(), matbuf(), matbuf(), matbuf(), # C1, D, E, F, G + matbuf(), matbuf(), # P1, P2 + matbuf(), matbuf(), matbuf() # Ω2, Ω3_4, Ωtot + ) end struct MagnusNC6ConstantCache <: OrdinaryDiffEqConstantCache @@ -472,7 +498,7 @@ function alg_cache( ) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits} return MagnusGL6ConstantCache() end -@cache struct MagnusGauss4Cache{uType, rateType, WType, expType} <: +@cache struct MagnusGauss4Cache{uType, rateType, WType, expType, matType} <: LinearMutableCache u::uType uprev::uType @@ -482,6 +508,10 @@ end W::WType k::rateType exp_cache::expType + A::matType + P1::matType + P2::matType + G::matType end function alg_cache( @@ -494,7 +524,11 @@ function alg_cache( k = zero(rate_prototype) fsalfirst = zero(rate_prototype) exp_cache = ExponentialUtilities.alloc_mem(f, ExpMethodGeneric()) - return MagnusGauss4Cache(u, uprev, uprev2, zero(u), fsalfirst, W, k, exp_cache) + matbuf() = similar(W) + return MagnusGauss4Cache( + u, uprev, uprev2, zero(u), fsalfirst, W, k, exp_cache, + matbuf(), matbuf(), matbuf(), matbuf() + ) end struct MagnusGauss4ConstantCache <: OrdinaryDiffEqConstantCache diff --git a/lib/OrdinaryDiffEqLinear/src/linear_perform_step.jl b/lib/OrdinaryDiffEqLinear/src/linear_perform_step.jl index 08a34bcdf04..544e543e0fb 100644 --- a/lib/OrdinaryDiffEqLinear/src/linear_perform_step.jl +++ b/lib/OrdinaryDiffEqLinear/src/linear_perform_step.jl @@ -1,3 +1,12 @@ +# Writes the matrix commutator [X, Y] = X*Y - Y*X into `dest`, using `tmp1`/`tmp2` +# as scratch for the two products so no new matrix is allocated. +@inline function _magnus_commutator!(dest, X, Y, tmp1, tmp2) + mul!(tmp1, X, Y) + mul!(tmp2, Y, X) + @. dest = tmp1 - tmp2 + return dest +end + function initialize!(integrator, cache::MagnusMidpointCache) integrator.kshortsize = 2 @@ -565,39 +574,63 @@ end function perform_step!(integrator, cache::MagnusNC6Cache, repeat_step = false) (; t, dt, uprev, u, p) = integrator alg = unwrap_alg(integrator, nothing) - (; W, k, tmp, exp_cache) = cache + (; exp_cache) = cache mass_matrix = integrator.f.mass_matrix exp_method = ExpMethodGeneric() L = integrator.f.f update_coefficients!(L, uprev, p, t) - A0 = deepcopy(convert(AbstractMatrix, L)) + A0 = cache.A0 + copyto!(A0, convert(AbstractMatrix, L)) update_coefficients!(L, uprev, p, t + dt / 4) - A1 = deepcopy(convert(AbstractMatrix, L)) + A1 = cache.A1 + copyto!(A1, convert(AbstractMatrix, L)) update_coefficients!(L, uprev, p, t + dt / 2) - A2 = deepcopy(convert(AbstractMatrix, L)) + A2 = cache.A2 + copyto!(A2, convert(AbstractMatrix, L)) update_coefficients!(L, uprev, p, t + 3 * dt / 4) - A3 = deepcopy(convert(AbstractMatrix, L)) + A3 = cache.A3 + copyto!(A3, convert(AbstractMatrix, L)) update_coefficients!(L, uprev, p, t + dt) A4 = convert(AbstractMatrix, L) - B0 = (1 / 90) * (7 * (A0 + A4) + 32 * (A1 + A3) + 12 * (A2)) - B1 = (1 / 90) * ((7 / 2) * (A4 - A0) + 8 * (A3 - A1)) - B2 = (1 / 90) * ((7 / 4) * (A0 + A4) + 2 * (A1 + A3)) - Ω1 = dt * B0 - Ω2 = (dt * dt) * (B1 * (3 * B0 / 2 - 6 * B2) - (3 * B0 / 2 - 6 * B2) * B1) - Ω3_4 = (dt * dt) * - ( - B0 * (B0 * (dt * B2 / 2 - Ω2 / 60) - (dt * B2 / 2 - Ω2 / 60) * B0) - - (B0 * (dt * B2 / 2 - Ω2 / 60) - (dt * B2 / 2 - Ω2 / 60) * B0) * B0 - ) + - (3 * dt / 5) * (B1 * Ω2 - Ω2 * B1) + + B0 = cache.B0 + B1 = cache.B1 + B2 = cache.B2 + @. B0 = (1 / 90) * (7 * (A0 + A4) + 32 * (A1 + A3) + 12 * (A2)) + @. B1 = (1 / 90) * ((7 / 2) * (A4 - A0) + 8 * (A3 - A1)) + @. B2 = (1 / 90) * ((7 / 4) * (A0 + A4) + 2 * (A1 + A3)) + + P1 = cache.P1 + P2 = cache.P2 + C1 = cache.C1 + @. C1 = 3 * B0 / 2 - 6 * B2 + Ω2 = cache.Ω2 + _magnus_commutator!(Ω2, B1, C1, P1, P2) + @. Ω2 = (dt * dt) * Ω2 + + D = cache.D + @. D = dt * B2 / 2 - Ω2 / 60 + E = cache.E + _magnus_commutator!(E, B0, D, P1, P2) + F = cache.F + _magnus_commutator!(F, B0, E, P1, P2) + G = cache.G + _magnus_commutator!(G, B1, Ω2, P1, P2) + Ω3_4 = cache.Ω3_4 + @. Ω3_4 = (dt * dt) * F + (3 * dt / 5) * G + + Ωtot = cache.Ωtot + @. Ωtot = dt * B0 + Ω2 + Ω3_4 + if alg.krylov u .= expv( - 1.0, Ω1 + Ω2 + Ω3_4, uprev; m = min(alg.m, size(L, 1)), + 1.0, Ωtot, uprev; m = min(alg.m, size(L, 1)), opnorm = integrator.opts.internalopnorm, alg.iop ) else - u .= exponential!(Ω1 + Ω2 + Ω3_4, exp_method, exp_cache) * uprev + expΩ = exponential!(Ωtot, exp_method, exp_cache) + mul!(u, expΩ, uprev) end integrator.f(integrator.fsallast, u, p, t + dt) return OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) @@ -663,28 +696,31 @@ end function perform_step!(integrator, cache::MagnusGauss4Cache, repeat_step = false) (; t, dt, uprev, u, p) = integrator alg = unwrap_alg(integrator, nothing) - (; W, k, tmp, exp_cache) = cache + (; exp_cache) = cache mass_matrix = integrator.f.mass_matrix exp_method = ExpMethodGeneric() L = integrator.f.f update_coefficients!(L, uprev, p, t + dt * (1 / 2 + sqrt(3) / 6)) - A = deepcopy(convert(AbstractMatrix, L)) + A = cache.A + copyto!(A, convert(AbstractMatrix, L)) update_coefficients!(L, uprev, p, t + dt * (1 / 2 - sqrt(3) / 6)) B = convert(AbstractMatrix, L) + P1 = cache.P1 + P2 = cache.P2 + G = cache.G + mul!(P1, B, A) + mul!(P2, A, B) if alg.krylov + @. G = (A + B) / 2 + (dt * sqrt(3)) * (P1 - P2) / 12 u .= expv( - dt, (A + B) ./ 2 + (dt * sqrt(3)) .* (B * A - A * B) ./ 12, u; - m = min(alg.m, size(L, 1)), opnorm = integrator.opts.internalopnorm, + dt, G, u; m = min(alg.m, size(L, 1)), opnorm = integrator.opts.internalopnorm, alg.iop ) else - u .= exponential!( - (dt / 2) .* (A + B) + - ((dt^2) * (sqrt(3) / 12)) .* (B * A - A * B), - exp_method, - exp_cache - ) * uprev + @. G = (dt / 2) * (A + B) + ((dt^2) * (sqrt(3) / 12)) * (P1 - P2) + expG = exponential!(G, exp_method, exp_cache) + mul!(u, expG, uprev) end integrator.f(integrator.fsallast, u, p, t + dt) return OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) diff --git a/lib/OrdinaryDiffEqLinear/test/qa/allocation_tests.jl b/lib/OrdinaryDiffEqLinear/test/qa/allocation_tests.jl index 735d6c7724e..ee975546560 100644 --- a/lib/OrdinaryDiffEqLinear/test/qa/allocation_tests.jl +++ b/lib/OrdinaryDiffEqLinear/test/qa/allocation_tests.jl @@ -5,6 +5,50 @@ using AllocCheck using LinearAlgebra using Test +# Regression bounds for the per-step allocation of the dense (non-Krylov) Magnus +# exponential integrators, which build several NxN stage/commutator matrices every +# step. `linear_perform_step.jl` preallocates those matrices in the cache rather than +# forming new ones on each call; these bounds are well below what master allocates +# (MagnusNC6/MagnusGauss4 at N=50: 1,431,528 / 222,728 bytes/step; at N=200 constant: +# 22,730,984 / 3,524,648 bytes/step) and comfortably above what the preallocated +# version allocates (~60,900 and ~961,900 bytes/step respectively, dominated by the +# scratch matrices ExponentialUtilities.exponential! allocates internally). +@testset "Magnus dense stage-matrix allocation regression" begin + function magnus_step_alloc(prob, alg; dt = 0.01) + integrator = init( + prob, alg; dt = dt, adaptive = false, save_everystep = false + ) + step!(integrator) + step!(integrator) + return @allocated step!(integrator) + end + + function timedep_update!(A, u, p, t) + @inbounds for j in axes(A, 2), i in axes(A, 1) + A[i, j] = (i == j ? -0.1i : 0.001 * sin(t + i * j)) + end + return nothing + end + + N = 50 + op = MatrixOperator(zeros(Float64, N, N); update_func! = timedep_update!) + prob_timedep = ODEProblem(op, ones(Float64, N), (0.0, 0.2)) + + Nc = 200 + op_const = MatrixOperator(-0.1 * Matrix{Float64}(I, Nc, Nc)) + prob_const = ODEProblem(op_const, ones(Float64, Nc), (0.0, 0.2)) + + @testset "$(typeof(alg)) step allocation bound" for (alg, prob, bound) in ( + (MagnusNC6(), prob_timedep, 150_000), + (MagnusGauss4(), prob_timedep, 150_000), + (MagnusNC6(), prob_const, 2_000_000), + (MagnusGauss4(), prob_const, 2_000_000), + ) + a = magnus_step_alloc(prob, alg) + @test a < bound + end +end + @testset "Linear Allocation Tests" begin A = MatrixOperator([-1.0 0.5; 0.0 -2.0]) prob = ODEProblem(A, [1.0, 1.0], (0.0, 1.0)) From 8d42b7114660184a78580e69b79b5b9d5d8f2f21 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Fri, 16 Oct 2026 10:51:39 -0400 Subject: [PATCH 2/2] Keep Magnus NC6/Gauss4 stages in the operator's matrix type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The preallocated stage buffers were `similar(W)`, where `W` is an outer product of `rate_prototype`. That gave them the state's units squared (DimensionError for a unitful state with a dimensionless operator), the state's element type (complex or Float32 states changed rounding), and a dense layout for sparse operators (17 / 4 dense N×N matrices per cache on the Krylov path). The buffers are now `zero(A)` of the operator's own matrix, and only when that matrix is a `StridedMatrix` whose element type is unchanged by the stage arithmetic (`promote_type(T, Float64, typeof(dt)) === T`). Every other operator (sparse, structured, mixed precision, unitful time) forms its stages out of place exactly as on master. The final update is again `u .= exp(Ω) * uprev`: `mul!` into `u` accumulated in `eltype(u)` and changed rounding for Float32 states. Adds Core tests for a unitful state and for sparse/Tridiagonal operators (cache stays below two dense N×N matrices, solution matches the dense operator), with Unitful and SparseArrays as test-only dependencies. Co-Authored-By: Chris Rackauckas Co-Authored-By: Claude Opus 5.5 Agent-Harness: Claude Code 2.1.301 Agent-Model: claude-opus-5-5 Agent-Session: local session ID 152619c5-78a8-4ae5-a06c-12cc1a58a70b (headless claude -p, no URL) --- lib/OrdinaryDiffEqLinear/Project.toml | 6 +- lib/OrdinaryDiffEqLinear/src/linear_caches.jl | 84 ++++++++------ .../src/linear_perform_step.jl | 108 +++++++++++------- .../test/linear_method_tests.jl | 40 ++++++- 4 files changed, 162 insertions(+), 76 deletions(-) diff --git a/lib/OrdinaryDiffEqLinear/Project.toml b/lib/OrdinaryDiffEqLinear/Project.toml index 514b1477108..39708e4de45 100644 --- a/lib/OrdinaryDiffEqLinear/Project.toml +++ b/lib/OrdinaryDiffEqLinear/Project.toml @@ -23,6 +23,8 @@ OrdinaryDiffEqVerner = "79d7bb75-1356-48c1-b8c0-6832512096c2" OrdinaryDiffEqRosenbrock = "43230ef6-c299-4910-a778-202eb28ce4ce" SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" SciMLTesting = "09d9d899-5365-40a9-917a-5f67fddea283" +SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" +Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" [sources] DiffEqBase = {path = "../DiffEqBase"} @@ -47,9 +49,11 @@ DiffEqBase = "7" SafeTestsets = "0.1.0" OrdinaryDiffEqRosenbrock = "2" Reexport = "1.2.2" +SparseArrays = "1.10" +Unitful = "1.25.1" [targets] -test = ["DiffEqDevTools", "Random", "OrdinaryDiffEqRosenbrock", "SafeTestsets", "Test", "OrdinaryDiffEqVerner", "OrdinaryDiffEqTsit5", "Pkg", "SciMLTesting"] +test = ["DiffEqDevTools", "Random", "OrdinaryDiffEqRosenbrock", "SafeTestsets", "Test", "OrdinaryDiffEqVerner", "OrdinaryDiffEqTsit5", "Pkg", "SciMLTesting", "SparseArrays", "Unitful"] [sources.OrdinaryDiffEqTsit5] path = "../OrdinaryDiffEqTsit5" diff --git a/lib/OrdinaryDiffEqLinear/src/linear_caches.jl b/lib/OrdinaryDiffEqLinear/src/linear_caches.jl index db96a3a8a61..5d5aeafa01b 100644 --- a/lib/OrdinaryDiffEqLinear/src/linear_caches.jl +++ b/lib/OrdinaryDiffEqLinear/src/linear_caches.jl @@ -401,7 +401,41 @@ function alg_cache( return MagnusGL8ConstantCache() end -@cache struct MagnusNC6Cache{uType, rateType, WType, expType, matType} <: +# Preallocated stage buffers only for a dense operator matrix whose element type survives +# the stage arithmetic (`Float64` coefficients, `dt` scaling). Otherwise `nothing`: the +# stages are then formed out of place and keep the operator's own matrix type. +function magnus_dense_stages(::Type{S}, f, dt) where {S} + A = convert(AbstractMatrix, f.f) + T = eltype(A) + if A isa StridedMatrix && T <: Number && promote_type(T, Float64, typeof(dt)) === T + return S(A) + end + return nothing +end + +struct MagnusNC6Stages{M} + A0::M + A1::M + A2::M + A3::M + B0::M + B1::M + B2::M + C1::M + D::M + E::M + F::M + G::M + P1::M + P2::M + Ω2::M + Ω3_4::M + Ωtot::M +end + +MagnusNC6Stages(A::AbstractMatrix) = MagnusNC6Stages(ntuple(_ -> zero(A), Val(17))...) + +@cache struct MagnusNC6Cache{uType, rateType, WType, expType, stagesType} <: LinearMutableCache u::uType uprev::uType @@ -411,23 +445,7 @@ end W::WType k::rateType exp_cache::expType - A0::matType - A1::matType - A2::matType - A3::matType - B0::matType - B1::matType - B2::matType - C1::matType - D::matType - E::matType - F::matType - G::matType - P1::matType - P2::matType - Ω2::matType - Ω3_4::matType - Ωtot::matType + stages::stagesType end function alg_cache( @@ -440,14 +458,9 @@ function alg_cache( k = zero(rate_prototype) fsalfirst = zero(rate_prototype) exp_cache = ExponentialUtilities.alloc_mem(f, ExpMethodGeneric()) - matbuf() = similar(W) + stages = magnus_dense_stages(MagnusNC6Stages, f, dt) return MagnusNC6Cache( - u, uprev, uprev2, zero(u), fsalfirst, W, k, exp_cache, - matbuf(), matbuf(), matbuf(), matbuf(), # A0, A1, A2, A3 - matbuf(), matbuf(), matbuf(), # B0, B1, B2 - matbuf(), matbuf(), matbuf(), matbuf(), matbuf(), # C1, D, E, F, G - matbuf(), matbuf(), # P1, P2 - matbuf(), matbuf(), matbuf() # Ω2, Ω3_4, Ωtot + u, uprev, uprev2, zero(u), fsalfirst, W, k, exp_cache, stages ) end @@ -498,7 +511,16 @@ function alg_cache( ) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits} return MagnusGL6ConstantCache() end -@cache struct MagnusGauss4Cache{uType, rateType, WType, expType, matType} <: +struct MagnusGauss4Stages{M} + A::M + P1::M + P2::M + G::M +end + +MagnusGauss4Stages(A::AbstractMatrix) = MagnusGauss4Stages(zero(A), zero(A), zero(A), zero(A)) + +@cache struct MagnusGauss4Cache{uType, rateType, WType, expType, stagesType} <: LinearMutableCache u::uType uprev::uType @@ -508,10 +530,7 @@ end W::WType k::rateType exp_cache::expType - A::matType - P1::matType - P2::matType - G::matType + stages::stagesType end function alg_cache( @@ -524,10 +543,9 @@ function alg_cache( k = zero(rate_prototype) fsalfirst = zero(rate_prototype) exp_cache = ExponentialUtilities.alloc_mem(f, ExpMethodGeneric()) - matbuf() = similar(W) + stages = magnus_dense_stages(MagnusGauss4Stages, f, dt) return MagnusGauss4Cache( - u, uprev, uprev2, zero(u), fsalfirst, W, k, exp_cache, - matbuf(), matbuf(), matbuf(), matbuf() + u, uprev, uprev2, zero(u), fsalfirst, W, k, exp_cache, stages ) end diff --git a/lib/OrdinaryDiffEqLinear/src/linear_perform_step.jl b/lib/OrdinaryDiffEqLinear/src/linear_perform_step.jl index 544e543e0fb..f4fd13f112b 100644 --- a/lib/OrdinaryDiffEqLinear/src/linear_perform_step.jl +++ b/lib/OrdinaryDiffEqLinear/src/linear_perform_step.jl @@ -571,66 +571,77 @@ function initialize!(integrator, cache::MagnusNC6Cache) return OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) end -function perform_step!(integrator, cache::MagnusNC6Cache, repeat_step = false) - (; t, dt, uprev, u, p) = integrator - alg = unwrap_alg(integrator, nothing) - (; exp_cache) = cache - mass_matrix = integrator.f.mass_matrix - exp_method = ExpMethodGeneric() +function magnus_nc6_exponent!(::Nothing, L, uprev, p, t, dt) + update_coefficients!(L, uprev, p, t) + A0 = deepcopy(convert(AbstractMatrix, L)) + update_coefficients!(L, uprev, p, t + dt / 4) + A1 = deepcopy(convert(AbstractMatrix, L)) + update_coefficients!(L, uprev, p, t + dt / 2) + A2 = deepcopy(convert(AbstractMatrix, L)) + update_coefficients!(L, uprev, p, t + 3 * dt / 4) + A3 = deepcopy(convert(AbstractMatrix, L)) + update_coefficients!(L, uprev, p, t + dt) + A4 = convert(AbstractMatrix, L) + B0 = (1 / 90) * (7 * (A0 + A4) + 32 * (A1 + A3) + 12 * (A2)) + B1 = (1 / 90) * ((7 / 2) * (A4 - A0) + 8 * (A3 - A1)) + B2 = (1 / 90) * ((7 / 4) * (A0 + A4) + 2 * (A1 + A3)) + Ω1 = dt * B0 + Ω2 = (dt * dt) * (B1 * (3 * B0 / 2 - 6 * B2) - (3 * B0 / 2 - 6 * B2) * B1) + Ω3_4 = (dt * dt) * + ( + B0 * (B0 * (dt * B2 / 2 - Ω2 / 60) - (dt * B2 / 2 - Ω2 / 60) * B0) - + (B0 * (dt * B2 / 2 - Ω2 / 60) - (dt * B2 / 2 - Ω2 / 60) * B0) * B0 + ) + + (3 * dt / 5) * (B1 * Ω2 - Ω2 * B1) + return Ω1 + Ω2 + Ω3_4 +end - L = integrator.f.f +function magnus_nc6_exponent!(stages::MagnusNC6Stages, L, uprev, p, t, dt) + (; A0, A1, A2, A3, B0, B1, B2, C1, D, E, F, G, P1, P2, Ω2, Ω3_4, Ωtot) = stages update_coefficients!(L, uprev, p, t) - A0 = cache.A0 copyto!(A0, convert(AbstractMatrix, L)) update_coefficients!(L, uprev, p, t + dt / 4) - A1 = cache.A1 copyto!(A1, convert(AbstractMatrix, L)) update_coefficients!(L, uprev, p, t + dt / 2) - A2 = cache.A2 copyto!(A2, convert(AbstractMatrix, L)) update_coefficients!(L, uprev, p, t + 3 * dt / 4) - A3 = cache.A3 copyto!(A3, convert(AbstractMatrix, L)) update_coefficients!(L, uprev, p, t + dt) A4 = convert(AbstractMatrix, L) - B0 = cache.B0 - B1 = cache.B1 - B2 = cache.B2 @. B0 = (1 / 90) * (7 * (A0 + A4) + 32 * (A1 + A3) + 12 * (A2)) @. B1 = (1 / 90) * ((7 / 2) * (A4 - A0) + 8 * (A3 - A1)) @. B2 = (1 / 90) * ((7 / 4) * (A0 + A4) + 2 * (A1 + A3)) - P1 = cache.P1 - P2 = cache.P2 - C1 = cache.C1 @. C1 = 3 * B0 / 2 - 6 * B2 - Ω2 = cache.Ω2 _magnus_commutator!(Ω2, B1, C1, P1, P2) @. Ω2 = (dt * dt) * Ω2 - D = cache.D @. D = dt * B2 / 2 - Ω2 / 60 - E = cache.E _magnus_commutator!(E, B0, D, P1, P2) - F = cache.F _magnus_commutator!(F, B0, E, P1, P2) - G = cache.G _magnus_commutator!(G, B1, Ω2, P1, P2) - Ω3_4 = cache.Ω3_4 @. Ω3_4 = (dt * dt) * F + (3 * dt / 5) * G - Ωtot = cache.Ωtot @. Ωtot = dt * B0 + Ω2 + Ω3_4 + return Ωtot +end +function perform_step!(integrator, cache::MagnusNC6Cache, repeat_step = false) + (; t, dt, uprev, u, p) = integrator + alg = unwrap_alg(integrator, nothing) + (; exp_cache, stages) = cache + exp_method = ExpMethodGeneric() + + L = integrator.f.f + Ω = magnus_nc6_exponent!(stages, L, uprev, p, t, dt) if alg.krylov u .= expv( - 1.0, Ωtot, uprev; m = min(alg.m, size(L, 1)), + 1.0, Ω, uprev; m = min(alg.m, size(L, 1)), opnorm = integrator.opts.internalopnorm, alg.iop ) else - expΩ = exponential!(Ωtot, exp_method, exp_cache) - mul!(u, expΩ, uprev) + u .= exponential!(Ω, exp_method, exp_cache) * uprev end integrator.f(integrator.fsallast, u, p, t + dt) return OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) @@ -693,34 +704,49 @@ function initialize!(integrator, cache::MagnusGauss4Cache) return OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) end +# The Krylov path takes the generator scaled by `1/dt`, since `expv` applies the `dt`. +function magnus_gauss4_exponent!(::Nothing, A, B, dt, krylov) + if krylov + return (A + B) ./ 2 + (dt * sqrt(3)) .* (B * A - A * B) ./ 12 + else + return (dt / 2) .* (A + B) + ((dt^2) * (sqrt(3) / 12)) .* (B * A - A * B) + end +end + +function magnus_gauss4_exponent!(stages::MagnusGauss4Stages, A, B, dt, krylov) + (; P1, P2, G) = stages + mul!(P1, B, A) + mul!(P2, A, B) + if krylov + @. G = (A + B) / 2 + (dt * sqrt(3)) * (P1 - P2) / 12 + else + @. G = (dt / 2) * (A + B) + ((dt^2) * (sqrt(3) / 12)) * (P1 - P2) + end + return G +end + +magnus_snapshot!(::Nothing, A) = deepcopy(A) +magnus_snapshot!(stages::MagnusGauss4Stages, A) = copyto!(stages.A, A) + function perform_step!(integrator, cache::MagnusGauss4Cache, repeat_step = false) (; t, dt, uprev, u, p) = integrator alg = unwrap_alg(integrator, nothing) - (; exp_cache) = cache - mass_matrix = integrator.f.mass_matrix + (; exp_cache, stages) = cache exp_method = ExpMethodGeneric() L = integrator.f.f update_coefficients!(L, uprev, p, t + dt * (1 / 2 + sqrt(3) / 6)) - A = cache.A - copyto!(A, convert(AbstractMatrix, L)) + A = magnus_snapshot!(stages, convert(AbstractMatrix, L)) update_coefficients!(L, uprev, p, t + dt * (1 / 2 - sqrt(3) / 6)) B = convert(AbstractMatrix, L) - P1 = cache.P1 - P2 = cache.P2 - G = cache.G - mul!(P1, B, A) - mul!(P2, A, B) + Ω = magnus_gauss4_exponent!(stages, A, B, dt, alg.krylov) if alg.krylov - @. G = (A + B) / 2 + (dt * sqrt(3)) * (P1 - P2) / 12 u .= expv( - dt, G, u; m = min(alg.m, size(L, 1)), opnorm = integrator.opts.internalopnorm, + dt, Ω, u; m = min(alg.m, size(L, 1)), opnorm = integrator.opts.internalopnorm, alg.iop ) else - @. G = (dt / 2) * (A + B) + ((dt^2) * (sqrt(3) / 12)) * (P1 - P2) - expG = exponential!(G, exp_method, exp_cache) - mul!(u, expG, uprev) + u .= exponential!(Ω, exp_method, exp_cache) * uprev end integrator.f(integrator.fsallast, u, p, t + dt) return OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) diff --git a/lib/OrdinaryDiffEqLinear/test/linear_method_tests.jl b/lib/OrdinaryDiffEqLinear/test/linear_method_tests.jl index 027a8b6096e..70282f0d1f7 100644 --- a/lib/OrdinaryDiffEqLinear/test/linear_method_tests.jl +++ b/lib/OrdinaryDiffEqLinear/test/linear_method_tests.jl @@ -1,6 +1,6 @@ using OrdinaryDiffEqLinear, Test, DiffEqDevTools using OrdinaryDiffEqRosenbrock, OrdinaryDiffEqVerner, OrdinaryDiffEqTsit5 -using LinearAlgebra, Random +using LinearAlgebra, Random, SparseArrays, Unitful using SciMLOperators: MatrixOperator # Linear exponential solvers @@ -308,3 +308,41 @@ test_setup = Dict(:alg => Vern9(), :reltol => 1.0e-14, :abstol => 1.0e-14) sim = analyticless_test_convergence(dts, prob, CayleyEuler(), test_setup) @test sim.𝒪est[:l2] ≈ 1 atol = 0.2 + +@testset "MagnusNC6 and MagnusGauss4 operator matrix types" begin + Au = [-0.1 0.0; 0.0 -0.2] + prob_unitful = ODEProblem(MatrixOperator(Au), [1.0, 2.0]u"m", (0.0, 1.0)) + @testset "unitful state, $(nameof(typeof(alg)))" for alg in (MagnusNC6(), MagnusGauss4()) + sol = solve(prob_unitful, alg; dt = 0.1, adaptive = false) + @test sol.retcode == ReturnCode.Success + @test ustrip.(u"m", sol.u[end]) ≈ exp(Au) * [1.0, 2.0] + end + + function tridiag_update!(A, u, p, t) + for j in axes(A, 2), i in max(1, j - 1):min(size(A, 1), j + 1) + A[i, j] = i == j ? -0.1 * i : (i < j ? 0.01 : 0.01 * sin(t)) + end + return nothing + end + N = 300 + pattern = spdiagm(-1 => ones(N - 1), 0 => ones(N), 1 => ones(N - 1)) + tridiag_prob(A) = ODEProblem( + MatrixOperator(A; update_func! = tridiag_update!), ones(N), (0.0, 0.1) + ) + prob_dense = tridiag_prob(Matrix(pattern)) + @testset "$(nameof(typeof(A))) operator, $(nameof(typeof(alg))) krylov=$(alg.krylov)" for (A, alg) in ( + (pattern, MagnusNC6(krylov = true)), + (pattern, MagnusGauss4(krylov = true)), + (Tridiagonal(Matrix(pattern)), MagnusNC6()), + (Tridiagonal(Matrix(pattern)), MagnusGauss4()), + ) + integrator = init(tridiag_prob(A), alg; dt = 0.01, adaptive = false) + if A isa SparseMatrixCSC + # The cache already holds one dense N×N matrix (`W`); the stages must not add more. + @test Base.summarysize(integrator.cache) < 2 * N^2 * sizeof(Float64) + end + solve!(integrator) + sol_dense = solve(prob_dense, alg; dt = 0.01, adaptive = false) + @test integrator.sol.u[end] ≈ sol_dense.u[end] rtol = 1.0e-12 + end +end