From 70ba317eff7c00b06c0dba8db5de7fcaa5d7aa83 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Mon, 2 Sep 2024 01:28:54 +0530 Subject: [PATCH 01/56] Perform_step! refactor for Rosenbrock part 2 --- .../src/rosenbrock_caches.jl | 47 +--- .../src/rosenbrock_perform_step.jl | 240 ++++-------------- 2 files changed, 53 insertions(+), 234 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl index 4b6841a1bc..6b12bd9172 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl @@ -795,47 +795,6 @@ struct Rosenbrock5ConstantCache{TF, UF, Tab, JType, WType, F} <: RosenbrockConst linsolve::F end -@cache mutable struct Rosenbrock5Cache{ - uType, rateType, uNoUnitsType, JType, WType, TabType, - TFType, UFType, F, JCType, GCType, RTolType, A, StepLimiter, StageLimiter} <: - RosenbrockMutableCache - u::uType - uprev::uType - dense1::rateType - dense2::rateType - dense3::rateType - du::rateType - du1::rateType - du2::rateType - k1::rateType - k2::rateType - k3::rateType - k4::rateType - k5::rateType - k6::rateType - k7::rateType - k8::rateType - fsalfirst::rateType - fsallast::rateType - dT::rateType - J::JType - W::WType - tmp::rateType - atmp::uNoUnitsType - weight::uNoUnitsType - tab::TabType - tf::TFType - uf::UFType - linsolve_tmp::rateType - linsolve::F - jac_config::JCType - grad_config::GCType - reltol::RTolType - alg::A - step_limiter!::StepLimiter - stage_limiter!::StageLimiter -end - function alg_cache(alg::Rodas5, u, rate_prototype, ::Type{uEltypeNoUnits}, ::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck, @@ -877,8 +836,7 @@ function alg_cache(alg::Rodas5, u, rate_prototype, ::Type{uEltypeNoUnits}, assumptions = LinearSolve.OperatorAssumptions(true)) grad_config = build_grad_config(alg, f, tf, du1, t) jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, du2) - Rosenbrock5Cache(u, uprev, dense1, dense2, dense3, du, du1, du2, k1, k2, k3, k4, - k5, k6, k7, k8, + RosenbrockCache(u, uprev, dense, du, du1, du2, ks, fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp, linsolve, jac_config, grad_config, reltol, alg, alg.step_limiter!, @@ -941,8 +899,7 @@ function alg_cache( assumptions = LinearSolve.OperatorAssumptions(true)) grad_config = build_grad_config(alg, f, tf, du1, t) jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, du2) - Rosenbrock5Cache(u, uprev, dense1, dense2, dense3, du, du1, du2, k1, k2, k3, k4, - k5, k6, k7, k8, + RosenbrockCache(u, uprev, dense, du, du1, du2, ks, fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp, linsolve, jac_config, grad_config, reltol, alg, alg.step_limiter!, diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index 51c938ee39..549913e2ff 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -1600,19 +1600,19 @@ end return nothing end -function initialize!(integrator, cache::Rosenbrock5Cache) +function initialize!(integrator, cache::RosenbrockCache) integrator.kshortsize = 3 - @unpack dense1, dense2, dense3 = cache + dense1, dense2, dense3 = dense[1], dense[2], dense[3] resize!(integrator.k, integrator.kshortsize) integrator.k[1] = dense1 integrator.k[2] = dense2 integrator.k[3] = dense3 end -@muladd function perform_step!(integrator, cache::Rosenbrock5Cache, repeat_step = false) - @unpack t, dt, uprev, u, f, p = integrator - @unpack du, du1, du2, k1, k2, k3, k4, k5, k6, k7, k8, dT, J, W, uf, tf, linsolve_tmp, jac_config, atmp, weight, stage_limiter!, step_limiter! = cache - @unpack a21, a31, a32, a41, a42, a43, a51, a52, a53, a54, a61, a62, a63, a64, a65, C21, C31, C32, C41, C42, C43, C51, C52, C53, C54, C61, C62, C63, C64, C65, C71, C72, C73, C74, C75, C76, C81, C82, C83, C84, C85, C86, C87, gamma, d1, d2, d3, d4, d5, c2, c3, c4, c5 = cache.tab +@muladd function perform_step!(integrator, cache::RosenbrockCache, repeat_step = false) + (;t, dt, uprev, u, f, p) = integrator + (;du, du1, du2, dT, J, W, uf, tf, ks, linsolve_tmp, jac_config, atmp, weight, stage_limiter!, step_limiter!) = cache + (;A, C, gamma, c, d, H) = cache.tab # Assignments sizeu = size(u) @@ -1620,46 +1620,14 @@ end mass_matrix = integrator.f.mass_matrix # Precalculations - dtC21 = C21 / dt - dtC31 = C31 / dt - dtC32 = C32 / dt - dtC41 = C41 / dt - dtC42 = C42 / dt - dtC43 = C43 / dt - dtC51 = C51 / dt - dtC52 = C52 / dt - dtC53 = C53 / dt - dtC54 = C54 / dt - dtC61 = C61 / dt - dtC62 = C62 / dt - dtC63 = C63 / dt - dtC64 = C64 / dt - dtC65 = C65 / dt - dtC71 = C71 / dt - dtC72 = C72 / dt - dtC73 = C73 / dt - dtC74 = C74 / dt - dtC75 = C75 / dt - dtC76 = C76 / dt - dtC81 = C81 / dt - dtC82 = C82 / dt - dtC83 = C83 / dt - dtC84 = C84 / dt - dtC85 = C85 / dt - dtC86 = C86 / dt - dtC87 = C87 / dt - - dtd1 = dt * d1 - dtd2 = dt * d2 - dtd3 = dt * d3 - dtd4 = dt * d4 - dtd5 = dt * d5 + dtC = C .* inv(dt) + dtd = dt .* d dtgamma = dt * gamma f(cache.fsalfirst, uprev, p, t) # used in calc_rosenbrock_differentiation! OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - calc_rosenbrock_differentiation!(integrator, cache, dtd1, dtgamma, repeat_step, true) + calc_rosenbrock_differentiation!(integrator, cache, dtd[1], dtgamma, repeat_step, true) calculate_residuals!(weight, fill!(weight, one(eltype(u))), uprev, uprev, integrator.opts.abstol, integrator.opts.reltol, @@ -1676,156 +1644,49 @@ end solverdata = (; gamma = dtgamma)) end - vecu = _vec(linres.u) - veck1 = _vec(k1) - - @.. broadcast=false veck1=-vecu - integrator.stats.nsolve += 1 - - @.. broadcast=false u=uprev + a21 * k1 - stage_limiter!(u, integrator, p, t + c2 * dt) - f(du, u, p, t + c2 * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + dtd2 * dT + dtC21 * k1 - else - @.. broadcast=false du1=dtC21 * k1 - mul!(_vec(du2), mass_matrix, _vec(du1)) - @.. broadcast=false linsolve_tmp=du + dtd2 * dT + du2 - end - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - veck2 = _vec(k2) - @.. broadcast=false veck2=-vecu - integrator.stats.nsolve += 1 - - @.. broadcast=false u=uprev + a31 * k1 + a32 * k2 - stage_limiter!(u, integrator, p, t + c3 * dt) - f(du, u, p, t + c3 * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + dtd3 * dT + (dtC31 * k1 + dtC32 * k2) - else - @.. broadcast=false du1=dtC31 * k1 + dtC32 * k2 - mul!(_vec(du2), mass_matrix, _vec(du1)) - @.. broadcast=false linsolve_tmp=du + dtd3 * dT + du2 - end - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - veck3 = _vec(k3) - @.. broadcast=false veck3=-vecu - integrator.stats.nsolve += 1 - - @.. broadcast=false u=uprev + a41 * k1 + a42 * k2 + a43 * k3 - stage_limiter!(u, integrator, p, t + c4 * dt) - f(du, u, p, t + c4 * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + dtd4 * dT + - (dtC41 * k1 + dtC42 * k2 + dtC43 * k3) - else - @.. broadcast=false du1=dtC41 * k1 + dtC42 * k2 + dtC43 * k3 - mul!(_vec(du2), mass_matrix, _vec(du1)) - @.. broadcast=false linsolve_tmp=du + dtd4 * dT + du2 - end - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - veck4 = _vec(k4) - @.. broadcast=false veck4=-vecu - integrator.stats.nsolve += 1 - - @.. broadcast=false u=uprev + a51 * k1 + a52 * k2 + a53 * k3 + a54 * k4 - stage_limiter!(u, integrator, p, t + c5 * dt) - f(du, u, p, t + c5 * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + dtd5 * dT + - (dtC52 * k2 + dtC54 * k4 + dtC51 * k1 + dtC53 * k3) - else - @.. broadcast=false du1=dtC52 * k2 + dtC54 * k4 + dtC51 * k1 + dtC53 * k3 - mul!(_vec(du2), mass_matrix, _vec(du1)) - @.. broadcast=false linsolve_tmp=du + dtd5 * dT + du2 - end - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - veck5 = _vec(k5) - @.. broadcast=false veck5=-vecu - integrator.stats.nsolve += 1 - - @.. broadcast=false u=uprev + a61 * k1 + a62 * k2 + a63 * k3 + a64 * k4 + a65 * k5 - stage_limiter!(u, integrator, p, t + dt) - f(du, u, p, t + dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + (dtC61 * k1 + dtC62 * k2 + dtC63 * k3 + - dtC64 * k4 + dtC65 * k5) - else - @.. broadcast=false du1=dtC61 * k1 + dtC62 * k2 + dtC63 * k3 + dtC64 * k4 + - dtC65 * k5 - mul!(_vec(du2), mass_matrix, _vec(du1)) - @.. broadcast=false linsolve_tmp=du + du2 - end - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - veck6 = _vec(k6) - @.. broadcast=false veck6=-vecu + @.. $(_vec(ks[1]))=-linres.u integrator.stats.nsolve += 1 - u .+= k6 - stage_limiter!(u, integrator, p, t + dt) - f(du, u, p, t + dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + (dtC71 * k1 + dtC72 * k2 + dtC73 * k3 + - dtC74 * k4 + dtC75 * k5 + dtC76 * k6) - else - @.. broadcast=false du1=dtC71 * k1 + dtC72 * k2 + dtC73 * k3 + dtC74 * k4 + - dtC75 * k5 + dtC76 * k6 - mul!(_vec(du2), mass_matrix, _vec(du1)) - @.. broadcast=false linsolve_tmp=du + du2 - end + for stage in 2:length(ks) + u .= uprev + for i in 1:stage-1 + @.. u += A[stage, i] * ks[i] + end - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - veck7 = _vec(k7) - @.. broadcast=false veck7=-vecu - integrator.stats.nsolve += 1 + stage_limiter!(u, integrator, p, t + c[stage] * dt) + f(du, u, p, t + c[stage] * dt) + OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - u .+= k7 - f(du, u, p, t + dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) + du1 .= 0 + if mass_matrix === I + for i in 1:stage-1 + @.. du1 += dtC[stage, i] * ks[i] + end + else + for i in 1:stage-1 + @.. du1 += dtC[stage, i] * ks[i] + end + mul!(_vec(du2), mass_matrix, _vec(du1)) + du1 .= du2 + end + @.. linsolve_tmp = du + dtd[stage] * dT + du1 - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + (dtC81 * k1 + dtC82 * k2 + dtC83 * k3 + - dtC84 * k4 + dtC85 * k5 + dtC86 * k6 + dtC87 * k7) - else - @.. broadcast=false du1=dtC81 * k1 + dtC82 * k2 + dtC83 * k3 + dtC84 * k4 + - dtC85 * k5 + dtC86 * k6 + dtC87 * k7 - mul!(_vec(du2), mass_matrix, _vec(du1)) - @.. broadcast=false linsolve_tmp=du + du2 + linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) + @.. $(_vec(ks[stage]))=-linres.u + integrator.stats.nsolve += 1 end - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - veck8 = _vec(k8) - @.. broadcast=false veck8=-vecu - integrator.stats.nsolve += 1 - - du .= k8 - u .+= k8 + du .= ks[end] + u .+= ks[end] step_limiter!(u, integrator, p, t + dt) if integrator.opts.adaptive if (integrator.alg isa Rodas5Pe) - @. du = 0.2606326497975715 * k1 - 0.005158627295444251 * k2 + - 1.3038988631109731 * k3 + 1.235000722062074 * k4 + - -0.7931985603795049 * k5 - 1.005448461135913 * k6 - - 0.18044626132120234 * k7 + 0.17051519239113755 * k8 + @. du = 0.2606326497975715 * ks[1] - 0.005158627295444251 * ks[2] + + 1.3038988631109731 * ks[3] + 1.235000722062074 * ks[4] + + -0.7931985603795049 * ks[5] - 1.005448461135913 * ks[6] - + 0.18044626132120234 * ks[7] + 0.17051519239113755 * ks[8] end calculate_residuals!(atmp, du, uprev, u, integrator.opts.abstol, integrator.opts.reltol, integrator.opts.internalnorm, t) @@ -1833,19 +1694,20 @@ end end if integrator.opts.calck - @unpack h21, h22, h23, h24, h25, h26, h27, h28, h31, h32, h33, h34, h35, h36, h37, h38, h41, h42, h43, h44, h45, h46, h47, h48 = cache.tab - @.. broadcast=false integrator.k[1]=h21 * k1 + h22 * k2 + h23 * k3 + h24 * k4 + - h25 * k5 + h26 * k6 + h27 * k7 + h28 * k8 - @.. broadcast=false integrator.k[2]=h31 * k1 + h32 * k2 + h33 * k3 + h34 * k4 + - h35 * k5 + h36 * k6 + h37 * k7 + h38 * k8 - @.. broadcast=false integrator.k[3]=h41 * k1 + h42 * k2 + h43 * k3 + h44 * k4 + - h45 * k5 + h46 * k6 + h47 * k7 + h48 * k8 + for j in eachindex(integrator.k) + integrator.k[j] .= 0 + end + for i in eachindex(ks) + for j in eachindex(integrator.k) + @.. integrator.k[j] += H[j, i] * ks[i] + end + end if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && (integrator.EEst < 1.0) - k2 = 0.5 * (uprev + u + + ks[2] = 0.5 * (uprev + u + 0.5 * (integrator.k[1] + 0.5 * (integrator.k[2] + 0.5 * integrator.k[3]))) du1 = (0.25 * (integrator.k[2] + integrator.k[3]) - uprev + u) / dt - f(du, k2, p, t + dt / 2) + f(du, ks[2], p, t + dt / 2) OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) if mass_matrix === I du2 = du1 - du @@ -1861,4 +1723,4 @@ end end @RosenbrockW6S4OS(:init) -@RosenbrockW6S4OS(:performstep) +@RosenbrockW6S4OS(:performstep) \ No newline at end of file From d50d9fe5ebc9a39c6b7c33ae52803328ecaedda3 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Mon, 2 Sep 2024 01:41:04 +0530 Subject: [PATCH 02/56] Fixes --- .../src/interp_func.jl | 2 +- .../src/rosenbrock_interpolants.jl | 34 +-- .../src/stiff_addsteps.jl | 204 +++--------------- 3 files changed, 52 insertions(+), 188 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl b/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl index 652b7ea6af..8862e82bf3 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl @@ -21,7 +21,7 @@ function DiffEqBase.interp_summary(::Type{cacheType}, dense::Bool) where { cacheType <: Union{Rosenbrock5ConstantCache, - Rosenbrock5Cache}} + RosenbrockCache}} dense ? "specialized 4rd order \"free\" stiffness-aware interpolation" : "1st order linear" end diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl index 4d7e659410..5d5e331abd 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl @@ -5,7 +5,7 @@ ROSENBROCKS_WITH_INTERPOLATIONS = Union{Rosenbrock23ConstantCache, Rosenbrock23C Rodas23WConstantCache, Rodas3PConstantCache, Rodas23WCache, Rodas3PCache, Rodas4ConstantCache, Rosenbrock5ConstantCache, - RosenbrockCache, Rosenbrock5Cache} + RosenbrockCache} function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::ROSENBROCKS_WITH_INTERPOLATIONS, @@ -220,14 +220,14 @@ end @inbounds Θ1 * y₀ + Θ * (y₁ + Θ1 * (k[1] + Θ * (k[2] + Θ * k[3]))) end -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Rosenbrock5Cache, idxs::Nothing, +@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::RosenbrockCache, idxs::Nothing, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ @inbounds @.. broadcast=false Θ1 * y₀+Θ * (y₁ + Θ1 * (k[1] + Θ * (k[2] + Θ * k[3]))) end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, Rosenbrock5Cache}, + cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, idxs, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ @.. broadcast=false Θ1 * @@ -236,7 +236,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, Rosenbrock5Cache}, + cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, idxs::Nothing, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ @.. broadcast=false out=Θ1 * y₀ + Θ * (y₁ + Θ1 * (k[1] + Θ * (k[2] + Θ * k[3]))) @@ -244,7 +244,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, Rosenbrock5Cache}, + cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, idxs, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ @views @.. broadcast=false out=Θ1 * y₀[idxs] + @@ -261,7 +261,7 @@ end y₁) / dt end -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Rosenbrock5Cache, idxs::Nothing, +@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::RosenbrockCache, idxs::Nothing, T::Type{Val{1}}, differential_vars) @inbounds @.. broadcast=false (k[1] + Θ * (-2 * k[1] + 2 * k[2] + @@ -269,7 +269,7 @@ end end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, Rosenbrock5Cache}, + cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, idxs, T::Type{Val{1}}, differential_vars) @.. broadcast=false (k[1][idxs] + Θ * (-2 * k[1][idxs] + 2 * k[2][idxs] + @@ -278,7 +278,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, Rosenbrock5Cache}, + cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, idxs::Nothing, T::Type{Val{1}}, differential_vars) @.. broadcast=false out=(k[1] + Θ * (-2 * k[1] + 2 * k[2] + @@ -287,7 +287,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, Rosenbrock5Cache}, + cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, idxs, T::Type{Val{1}}, differential_vars) @views @.. broadcast=false out=(k[1][idxs] + Θ * (-2 * k[1][idxs] + 2 * k[2][idxs] + @@ -303,21 +303,21 @@ end @inbounds (-2 * k[1] + 2 * k[2] + Θ * (-6 * k[2] + 6 * k[3] - 12 * Θ * k[3])) / dt^2 end -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Rosenbrock5Cache, idxs::Nothing, +@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::RosenbrockCache, idxs::Nothing, T::Type{Val{2}}, differential_vars) @inbounds @.. broadcast=false (-2 * k[1] + 2 * k[2] + Θ * (-6 * k[2] + 6 * k[3] - 12 * Θ * k[3]))/dt^2 end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, Rosenbrock5Cache}, + cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, idxs, T::Type{Val{2}}, differential_vars) @.. broadcast=false (-2 * k[1][idxs] + 2 * k[2][idxs] + Θ * (-6 * k[2][idxs] + 6 * k[3][idxs] - 12 * Θ * k[3][idxs]))/dt^2 end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, Rosenbrock5Cache}, + cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, idxs::Nothing, T::Type{Val{2}}, differential_vars) @.. broadcast=false out=(-2 * k[1] + 2 * k[2] + Θ * (-6 * k[2] + 6 * k[3] - 12 * Θ * k[3])) / dt^2 @@ -325,7 +325,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, Rosenbrock5Cache}, + cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, idxs, T::Type{Val{2}}, differential_vars) @views @.. broadcast=false out=(-2 * k[1][idxs] + 2 * k[2][idxs] + Θ * @@ -340,26 +340,26 @@ end @inbounds (-6 * k[2] + 6 * k[3] - 24 * Θ * k[3]) / dt^3 end -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Rosenbrock5Cache, idxs::Nothing, +@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::RosenbrockCache, idxs::Nothing, T::Type{Val{3}}, differential_vars) @inbounds @.. broadcast=false (-6 * k[2] + 6 * k[3] - 24 * Θ * k[3])/dt^3 end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, Rosenbrock5Cache}, + cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, idxs, T::Type{Val{3}}, differential_vars) @.. broadcast=false (-6 * k[2][idxs] + 6 * k[3][idxs] - 24 * Θ * k[3][idxs])/dt^3 end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, Rosenbrock5Cache}, + cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, idxs::Nothing, T::Type{Val{3}}, differential_vars) @.. broadcast=false out=(-6 * k[2] + 6 * k[3] - 24 * Θ * k[3]) / dt^3 out end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, Rosenbrock5Cache}, + cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, idxs, T::Type{Val{3}}, differential_vars) @views @.. broadcast=false out=(-6 * k[2][idxs] + 6 * k[3][idxs] - 24 * Θ * k[3][idxs]) / dt^3 diff --git a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl index 57a643afc6..ad81ab4772 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl @@ -558,54 +558,22 @@ function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::Rosenbrock5ConstantCach nothing end -function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::Rosenbrock5Cache, +function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::RosenbrockCache, always_calc_begin = false, allow_calc_end = true, force_calc_end = false) if length(k) < 3 || always_calc_begin - @unpack du, du1, du2, tmp, k1, k2, k3, k4, k5, k6, k7, k8, dT, J, W, uf, tf, linsolve_tmp, jac_config, fsalfirst, weight = cache - @unpack a21, a31, a32, a41, a42, a43, a51, a52, a53, a54, a61, a62, a63, a64, a65, C21, C31, C32, C41, C42, C43, C51, C52, C53, C54, C61, C62, C63, C64, C65, C71, C72, C73, C74, C75, C76, C81, C82, C83, C84, C85, C86, C87, gamma, d1, d2, d3, d4, d5, c2, c3, c4, c5 = cache.tab + (;du, du1, du2, tmp, ks, dT, J, W, uf, tf, linsolve_tmp, jac_config, fsalfirst, weight) = cache + (;A, C, gamma, c, d , H) = cache.tab # Assignments sizeu = size(u) uidx = eachindex(uprev) mass_matrix = f.mass_matrix - tmp = k8 # integrator.tmp === linsolve_tmp, aliasing fails due to linsolve mutation + tmp = ks[8] # integrator.tmp === linsolve_tmp, aliasing fails due to linsolve mutation # Precalculations - dtC21 = C21 / dt - dtC31 = C31 / dt - dtC32 = C32 / dt - dtC41 = C41 / dt - dtC42 = C42 / dt - dtC43 = C43 / dt - dtC51 = C51 / dt - dtC52 = C52 / dt - dtC53 = C53 / dt - dtC54 = C54 / dt - dtC61 = C61 / dt - dtC62 = C62 / dt - dtC63 = C63 / dt - dtC64 = C64 / dt - dtC65 = C65 / dt - dtC71 = C71 / dt - dtC72 = C72 / dt - dtC73 = C73 / dt - dtC74 = C74 / dt - dtC75 = C75 / dt - dtC76 = C76 / dt - dtC81 = C81 / dt - dtC82 = C82 / dt - dtC83 = C83 / dt - dtC84 = C84 / dt - dtC85 = C85 / dt - dtC86 = C86 / dt - dtC87 = C87 / dt - - dtd1 = dt * d1 - dtd2 = dt * d2 - dtd3 = dt * d3 - dtd4 = dt * d4 - dtd5 = dt * d5 + dtC = C ./ dt + dtd = dt .* d dtgamma = dt * gamma @.. broadcast=false linsolve_tmp=@muladd fsalfirst + dtgamma * dT @@ -618,148 +586,44 @@ function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::Rosenbrock5Cache, linres = dolinsolve(cache, linsolve; A = W, b = _vec(linsolve_tmp), reltol = cache.reltol) - vecu = _vec(linres.u) - veck1 = _vec(k1) - - @.. broadcast=false veck1=-vecu - @.. broadcast=false tmp=uprev + a21 * k1 - f(du, tmp, p, t + c2 * dt) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + dtd2 * dT + dtC21 * k1 - else - @.. broadcast=false du1=dtC21 * k1 - mul!(du2, mass_matrix, du1) - @.. broadcast=false linsolve_tmp=du + dtd2 * dT + du2 - end - - linres = dolinsolve(cache, linres.cache; b = _vec(linsolve_tmp), - reltol = cache.reltol) - vecu = _vec(linres.u) - veck2 = _vec(k2) - @.. broadcast=false veck2=-vecu - @.. broadcast=false tmp=uprev + a31 * k1 + a32 * k2 - f(du, tmp, p, t + c3 * dt) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + dtd3 * dT + (dtC31 * k1 + dtC32 * k2) - else - @.. broadcast=false du1=dtC31 * k1 + dtC32 * k2 - mul!(du2, mass_matrix, du1) - @.. broadcast=false linsolve_tmp=du + dtd3 * dT + du2 - end - - linres = dolinsolve(cache, linres.cache; b = _vec(linsolve_tmp), - reltol = cache.reltol) - vecu = _vec(linres.u) - veck3 = _vec(k3) - @.. broadcast=false veck3=-vecu - @.. broadcast=false tmp=uprev + a41 * k1 + a42 * k2 + a43 * k3 - f(du, tmp, p, t + c4 * dt) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + dtd4 * dT + - (dtC41 * k1 + dtC42 * k2 + dtC43 * k3) - else - @.. broadcast=false du1=dtC41 * k1 + dtC42 * k2 + dtC43 * k3 - mul!(du2, mass_matrix, du1) - @.. broadcast=false linsolve_tmp=du + dtd4 * dT + du2 - end - - linres = dolinsolve(cache, linres.cache; b = _vec(linsolve_tmp), - reltol = cache.reltol) - vecu = _vec(linres.u) - veck4 = _vec(k4) - @.. broadcast=false veck4=-vecu - @.. broadcast=false tmp=uprev + a51 * k1 + a52 * k2 + a53 * k3 + a54 * k4 - f(du, tmp, p, t + c5 * dt) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + dtd5 * dT + - (dtC52 * k2 + dtC54 * k4 + dtC51 * k1 + - dtC53 * k3) - else - @.. broadcast=false du1=dtC52 * k2 + dtC54 * k4 + dtC51 * k1 + dtC53 * k3 - mul!(du2, mass_matrix, du1) - @.. broadcast=false linsolve_tmp=du + dtd5 * dT + du2 - end - - linres = dolinsolve(cache, linres.cache; b = _vec(linsolve_tmp), - reltol = cache.reltol) - vecu = _vec(linres.u) - veck5 = _vec(k5) - @.. broadcast=false veck5=-vecu - @.. broadcast=false tmp=uprev + a61 * k1 + a62 * k2 + a63 * k3 + a64 * k4 + a65 * k5 - f(du, tmp, p, t + dt) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + (dtC62 * k2 + dtC64 * k4 + dtC61 * k1 + - dtC63 * k3 + dtC65 * k5) - else - @.. broadcast=false du1=dtC62 * k2 + dtC64 * k4 + dtC61 * k1 + dtC63 * k3 + - dtC65 * k5 - mul!(du2, mass_matrix, du1) - @.. broadcast=false linsolve_tmp=du + du2 - end - - linres = dolinsolve(cache, linres.cache; b = _vec(linsolve_tmp), - reltol = cache.reltol) - vecu = _vec(linres.u) - veck6 = _vec(k6) - @.. broadcast=false veck6=-vecu - @.. broadcast=false tmp+=k6 - f(du, tmp, p, t + dt) + @.. $(_vec(ks[1]))=-linres.u - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + (dtC71 * k1 + dtC72 * k2 + dtC73 * k3 + - dtC74 * k4 + dtC75 * k5 + dtC76 * k6) - else - @.. broadcast=false du1=dtC72 * k2 + dtC74 * k4 + dtC71 * k1 + dtC73 * k3 + - dtC75 * k5 + dtC76 * k6 - mul!(du2, mass_matrix, du1) - @.. broadcast=false linsolve_tmp=du + du2 - end + for stage in 2:length(ks)-1 + tmp .= uprev + for i in 1:stage-1 + @.. tmp += A[stage, i] * _vec(ks[i]) + end + f(du, tmp, p, t + c[stage] * dt) - linres = dolinsolve(cache, linres.cache; b = _vec(linsolve_tmp), - reltol = cache.reltol) - vecu = _vec(linres.u) - veck7 = _vec(k7) - @.. broadcast=false veck7=-vecu - @.. broadcast=false tmp+=k7 - f(du, tmp, p, t + dt) + if mass_matrix === I + @.. linsolve_tmp = du + dtd[stage] * dT + for i in 1:stage-1 + @.. linsolve_tmp += dtC[stage, i] * _vec(ks[i]) + end + else + du1 .= du + for i in 1:stage-1 + @.. du1 += dtC[stage, i] * _vec(ks[i]) + end + mul!(_vec(du2), mass_matrix, _vec(du1)) + @.. linsolve_tmp = du + dtd[stage] * dT + du2 + end - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + (dtC81 * k1 + dtC82 * k2 + dtC83 * k3 + - dtC84 * k4 + dtC85 * k5 + dtC86 * k6 + - dtC87 * k7) - else - @.. broadcast=false du1=dtC81 * k1 + dtC82 * k2 + dtC83 * k3 + dtC84 * k4 + - dtC85 * k5 + dtC86 * k6 + dtC87 * k7 - mul!(du2, mass_matrix, du1) - @.. broadcast=false linsolve_tmp=du + du2 + linres = dolinsolve(cache, linres.cache; b = _vec(linsolve_tmp), reltol = cache.reltol) + @.. $(_vec(ks[stage]))=-linres.u end - linres = dolinsolve(cache, linres.cache; b = _vec(linsolve_tmp), - reltol = cache.reltol) - vecu = _vec(linres.u) - veck8 = _vec(k8) - @.. broadcast=false veck8=-vecu - # https://github.com/SciML/OrdinaryDiffEq.jl/issues/2055 tmp = linsolve_tmp - - @unpack h21, h22, h23, h24, h25, h26, h27, h28, h31, h32, h33, h34, h35, h36, h37, h38, h41, h42, h43, h44, h45, h46, h47, h48 = cache.tab - @.. broadcast=false tmp=h21 * k1 + h22 * k2 + h23 * k3 + h24 * k4 + h25 * k5 + - h26 * k6 + h27 * k7 + h28 * k8 copyat_or_push!(k, 1, copy(tmp)) - - @.. broadcast=false tmp=h31 * k1 + h32 * k2 + h33 * k3 + h34 * k4 + h35 * k5 + - h36 * k6 + h37 * k7 + h38 * k8 copyat_or_push!(k, 2, copy(tmp)) - - @.. broadcast=false tmp=h41 * k1 + h42 * k2 + h43 * k3 + h44 * k4 + h45 * k5 + - h46 * k6 + h47 * k7 + h48 * k8 copyat_or_push!(k, 3, copy(tmp)) + + for i in 1:length(ks)-1 + @.. k[1] += H[1, i] * _vec(ks[i]) + @.. k[2] += H[2, i] * _vec(ks[i]) + @.. k[3] += H[3, i] * _vec(ks[i]) + end end nothing end From 9438d925afe765b5d077a5e768e0e5888b02c764 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Mon, 2 Sep 2024 01:45:57 +0530 Subject: [PATCH 03/56] Fixes --- lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index 549913e2ff..d40388ba4f 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -1602,6 +1602,7 @@ end function initialize!(integrator, cache::RosenbrockCache) integrator.kshortsize = 3 + dense = cache.dense dense1, dense2, dense3 = dense[1], dense[2], dense[3] resize!(integrator.k, integrator.kshortsize) integrator.k[1] = dense1 From 0cb9dc789e94d20e20cb5b4ec3de2e09fa5e3b48 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Mon, 2 Sep 2024 02:01:26 +0530 Subject: [PATCH 04/56] Rosenbrock5Cache merged --- .../src/rosenbrock_perform_step.jl | 136 +++--------------- 1 file changed, 23 insertions(+), 113 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index d40388ba4f..79aeda21df 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -1362,11 +1362,18 @@ end @.. $(_vec(ks[stage]))=-linres.u integrator.stats.nsolve += 1 end + du .= ks[end] u .+= ks[end] step_limiter!(u, integrator, p, t + dt) if integrator.opts.adaptive + if (integrator.alg isa Rodas5Pe) + @. du = 0.2606326497975715 * ks[1] - 0.005158627295444251 * ks[2] + + 1.3038988631109731 * ks[3] + 1.235000722062074 * ks[4] + + -0.7931985603795049 * ks[5] - 1.005448461135913 * ks[6] - + 0.18044626132120234 * ks[7] + 0.17051519239113755 * ks[8] + end calculate_residuals!(atmp, ks[end], uprev, u, integrator.opts.abstol, integrator.opts.reltol, integrator.opts.internalnorm, t) integrator.EEst = integrator.opts.internalnorm(atmp, t) @@ -1381,6 +1388,22 @@ end @.. integrator.k[j] += H[j, i] * ks[i] end end + if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && + (integrator.EEst < 1.0) + ks[2] = 0.5 * (uprev + u + + 0.5 * (integrator.k[1] + 0.5 * (integrator.k[2] + 0.5 * integrator.k[3]))) + du1 = (0.25 * (integrator.k[2] + integrator.k[3]) - uprev + u) / dt + f(du, ks[2], p, t + dt / 2) + OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) + if mass_matrix === I + du2 = du1 - du + else + mul!(_vec(du2), mass_matrix, _vec(du1)) + du2 = du2 - du + end + EEst = norm(du2) / norm(integrator.opts.abstol .+ integrator.opts.reltol .* k2) + integrator.EEst = max(EEst, integrator.EEst) + end end cache.linsolve = linres.cache end @@ -1610,118 +1633,5 @@ function initialize!(integrator, cache::RosenbrockCache) integrator.k[3] = dense3 end -@muladd function perform_step!(integrator, cache::RosenbrockCache, repeat_step = false) - (;t, dt, uprev, u, f, p) = integrator - (;du, du1, du2, dT, J, W, uf, tf, ks, linsolve_tmp, jac_config, atmp, weight, stage_limiter!, step_limiter!) = cache - (;A, C, gamma, c, d, H) = cache.tab - - # Assignments - sizeu = size(u) - uidx = eachindex(integrator.uprev) - mass_matrix = integrator.f.mass_matrix - - # Precalculations - dtC = C .* inv(dt) - dtd = dt .* d - dtgamma = dt * gamma - - f(cache.fsalfirst, uprev, p, t) # used in calc_rosenbrock_differentiation! - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - calc_rosenbrock_differentiation!(integrator, cache, dtd[1], dtgamma, repeat_step, true) - - calculate_residuals!(weight, fill!(weight, one(eltype(u))), uprev, uprev, - integrator.opts.abstol, integrator.opts.reltol, - integrator.opts.internalnorm, t) - - if repeat_step - linres = dolinsolve( - integrator, cache.linsolve; A = nothing, b = _vec(linsolve_tmp), - du = cache.fsalfirst, u = u, p = p, t = t, weight = weight, - solverdata = (; gamma = dtgamma)) - else - linres = dolinsolve(integrator, cache.linsolve; A = W, b = _vec(linsolve_tmp), - du = cache.fsalfirst, u = u, p = p, t = t, weight = weight, - solverdata = (; gamma = dtgamma)) - end - - @.. $(_vec(ks[1]))=-linres.u - integrator.stats.nsolve += 1 - - for stage in 2:length(ks) - u .= uprev - for i in 1:stage-1 - @.. u += A[stage, i] * ks[i] - end - - stage_limiter!(u, integrator, p, t + c[stage] * dt) - f(du, u, p, t + c[stage] * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - du1 .= 0 - if mass_matrix === I - for i in 1:stage-1 - @.. du1 += dtC[stage, i] * ks[i] - end - else - for i in 1:stage-1 - @.. du1 += dtC[stage, i] * ks[i] - end - mul!(_vec(du2), mass_matrix, _vec(du1)) - du1 .= du2 - end - @.. linsolve_tmp = du + dtd[stage] * dT + du1 - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - @.. $(_vec(ks[stage]))=-linres.u - integrator.stats.nsolve += 1 - end - - du .= ks[end] - u .+= ks[end] - - step_limiter!(u, integrator, p, t + dt) - - if integrator.opts.adaptive - if (integrator.alg isa Rodas5Pe) - @. du = 0.2606326497975715 * ks[1] - 0.005158627295444251 * ks[2] + - 1.3038988631109731 * ks[3] + 1.235000722062074 * ks[4] + - -0.7931985603795049 * ks[5] - 1.005448461135913 * ks[6] - - 0.18044626132120234 * ks[7] + 0.17051519239113755 * ks[8] - end - calculate_residuals!(atmp, du, uprev, u, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - integrator.EEst = integrator.opts.internalnorm(atmp, t) - end - - if integrator.opts.calck - for j in eachindex(integrator.k) - integrator.k[j] .= 0 - end - for i in eachindex(ks) - for j in eachindex(integrator.k) - @.. integrator.k[j] += H[j, i] * ks[i] - end - end - if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && - (integrator.EEst < 1.0) - ks[2] = 0.5 * (uprev + u + - 0.5 * (integrator.k[1] + 0.5 * (integrator.k[2] + 0.5 * integrator.k[3]))) - du1 = (0.25 * (integrator.k[2] + integrator.k[3]) - uprev + u) / dt - f(du, ks[2], p, t + dt / 2) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - if mass_matrix === I - du2 = du1 - du - else - mul!(_vec(du2), mass_matrix, _vec(du1)) - du2 = du2 - du - end - EEst = norm(du2) / norm(integrator.opts.abstol .+ integrator.opts.reltol .* k2) - integrator.EEst = max(EEst, integrator.EEst) - end - end - cache.linsolve = linres.cache -end - @RosenbrockW6S4OS(:init) @RosenbrockW6S4OS(:performstep) \ No newline at end of file From 8d1fa9b348a2521463b4efccc2c2e38bf2300fee Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Mon, 2 Sep 2024 07:51:23 +0530 Subject: [PATCH 05/56] Fixes --- .../src/rosenbrock_perform_step.jl | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index 79aeda21df..e43e5cd343 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -1623,15 +1623,5 @@ end return nothing end -function initialize!(integrator, cache::RosenbrockCache) - integrator.kshortsize = 3 - dense = cache.dense - dense1, dense2, dense3 = dense[1], dense[2], dense[3] - resize!(integrator.k, integrator.kshortsize) - integrator.k[1] = dense1 - integrator.k[2] = dense2 - integrator.k[3] = dense3 -end - @RosenbrockW6S4OS(:init) @RosenbrockW6S4OS(:performstep) \ No newline at end of file From 01afc209e7e9a825825838fc22419797e2c5c0ed Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Mon, 2 Sep 2024 08:27:29 +0530 Subject: [PATCH 06/56] Fixes --- .../src/rosenbrock_caches.jl | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl index 6b12bd9172..acebdf1100 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl @@ -799,20 +799,11 @@ function alg_cache(alg::Rodas5, u, rate_prototype, ::Type{uEltypeNoUnits}, ::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck, ::Val{true}) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits} - dense1 = zero(rate_prototype) - dense2 = zero(rate_prototype) - dense3 = zero(rate_prototype) + dense = [zero(rate_prototype) for _ in 1:3] du = zero(rate_prototype) du1 = zero(rate_prototype) du2 = zero(rate_prototype) - k1 = zero(rate_prototype) - k2 = zero(rate_prototype) - k3 = zero(rate_prototype) - k4 = zero(rate_prototype) - k5 = zero(rate_prototype) - k6 = zero(rate_prototype) - k7 = zero(rate_prototype) - k8 = zero(rate_prototype) + ks = [zero(rate_prototype) for _ in 1:8] fsalfirst = zero(rate_prototype) fsallast = zero(rate_prototype) dT = zero(rate_prototype) From 50def24d60c9536814e4511e121ea8d800c68935 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Mon, 2 Sep 2024 09:21:07 +0530 Subject: [PATCH 07/56] Fixes --- lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl index ad81ab4772..eb6701e21c 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl @@ -569,7 +569,7 @@ function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::RosenbrockCache, sizeu = size(u) uidx = eachindex(uprev) mass_matrix = f.mass_matrix - tmp = ks[8] # integrator.tmp === linsolve_tmp, aliasing fails due to linsolve mutation + tmp = ks[end] # integrator.tmp === linsolve_tmp, aliasing fails due to linsolve mutation # Precalculations dtC = C ./ dt From 02c7140e7ffbf943d196cc6c5b7bfbad4df9d8fe Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Mon, 2 Sep 2024 20:12:48 +0530 Subject: [PATCH 08/56] fixes --- .../src/stiff_addsteps.jl | 73 +------------------ 1 file changed, 2 insertions(+), 71 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl index eb6701e21c..13b201fb94 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl @@ -376,6 +376,7 @@ function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::RosenbrockCache, sizeu = size(u) uidx = eachindex(uprev) mass_matrix = f.mass_matrix + tmp = ks[end] # Precalculations dtC = C ./ dt @@ -556,74 +557,4 @@ function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::Rosenbrock5ConstantCach copyat_or_push!(k, 3, k₃) end nothing -end - -function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::RosenbrockCache, - always_calc_begin = false, allow_calc_end = true, - force_calc_end = false) - if length(k) < 3 || always_calc_begin - (;du, du1, du2, tmp, ks, dT, J, W, uf, tf, linsolve_tmp, jac_config, fsalfirst, weight) = cache - (;A, C, gamma, c, d , H) = cache.tab - - # Assignments - sizeu = size(u) - uidx = eachindex(uprev) - mass_matrix = f.mass_matrix - tmp = ks[end] # integrator.tmp === linsolve_tmp, aliasing fails due to linsolve mutation - - # Precalculations - dtC = C ./ dt - dtd = dt .* d - dtgamma = dt * gamma - - @.. broadcast=false linsolve_tmp=@muladd fsalfirst + dtgamma * dT - - ### Jacobian does not need to be re-evaluated after an event - ### Since it's unchanged - jacobian2W!(W, mass_matrix, dtgamma, J, true) - - linsolve = cache.linsolve - - linres = dolinsolve(cache, linsolve; A = W, b = _vec(linsolve_tmp), - reltol = cache.reltol) - @.. $(_vec(ks[1]))=-linres.u - - for stage in 2:length(ks)-1 - tmp .= uprev - for i in 1:stage-1 - @.. tmp += A[stage, i] * _vec(ks[i]) - end - f(du, tmp, p, t + c[stage] * dt) - - if mass_matrix === I - @.. linsolve_tmp = du + dtd[stage] * dT - for i in 1:stage-1 - @.. linsolve_tmp += dtC[stage, i] * _vec(ks[i]) - end - else - du1 .= du - for i in 1:stage-1 - @.. du1 += dtC[stage, i] * _vec(ks[i]) - end - mul!(_vec(du2), mass_matrix, _vec(du1)) - @.. linsolve_tmp = du + dtd[stage] * dT + du2 - end - - linres = dolinsolve(cache, linres.cache; b = _vec(linsolve_tmp), reltol = cache.reltol) - @.. $(_vec(ks[stage]))=-linres.u - end - - # https://github.com/SciML/OrdinaryDiffEq.jl/issues/2055 - tmp = linsolve_tmp - copyat_or_push!(k, 1, copy(tmp)) - copyat_or_push!(k, 2, copy(tmp)) - copyat_or_push!(k, 3, copy(tmp)) - - for i in 1:length(ks)-1 - @.. k[1] += H[1, i] * _vec(ks[i]) - @.. k[2] += H[2, i] * _vec(ks[i]) - @.. k[3] += H[3, i] * _vec(ks[i]) - end - end - nothing -end +end \ No newline at end of file From 35aee13a81b2f8a8b5c6d585dcb470b8538df642 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Mon, 2 Sep 2024 20:25:42 +0530 Subject: [PATCH 09/56] Fixes --- .../src/rosenbrock_caches.jl | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl index acebdf1100..267a57b1f9 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl @@ -853,20 +853,11 @@ function alg_cache( ::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck, ::Val{true}) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits} - dense1 = zero(rate_prototype) - dense2 = zero(rate_prototype) - dense3 = zero(rate_prototype) + dense = [zero(rate_prototype) for _ in 1:3] du = zero(rate_prototype) du1 = zero(rate_prototype) du2 = zero(rate_prototype) - k1 = zero(rate_prototype) - k2 = zero(rate_prototype) - k3 = zero(rate_prototype) - k4 = zero(rate_prototype) - k5 = zero(rate_prototype) - k6 = zero(rate_prototype) - k7 = zero(rate_prototype) - k8 = zero(rate_prototype) + ks = [zero(rate_prototype) for _ in 1:8] fsalfirst = zero(rate_prototype) fsallast = zero(rate_prototype) dT = zero(rate_prototype) From fe1057bd511c3105e38719b66bba518c2f6656bb Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 3 Sep 2024 00:20:53 +0530 Subject: [PATCH 10/56] Fixes --- .../src/rosenbrock_tableaus.jl | 319 ++++-------------- 1 file changed, 61 insertions(+), 258 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl index 19e7a989d7..fa20bff4b5 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl @@ -339,167 +339,43 @@ function Rodas4P2Tableau(T, T2) end struct Rodas5Tableau{T, T2} - a21::T - a31::T - a32::T - a41::T - a42::T - a43::T - a51::T - a52::T - a53::T - a54::T - a61::T - a62::T - a63::T - a64::T - a65::T - C21::T - C31::T - C32::T - C41::T - C42::T - C43::T - C51::T - C52::T - C53::T - C54::T - C61::T - C62::T - C63::T - C64::T - C65::T - C71::T - C72::T - C73::T - C74::T - C75::T - C76::T - C81::T - C82::T - C83::T - C84::T - C85::T - C86::T - C87::T + A::Matrix{T} + C::Matrix{T} gamma::T2 - d1::T - d2::T - d3::T - d4::T - d5::T - c2::T2 - c3::T2 - c4::T2 - c5::T2 - h21::T - h22::T - h23::T - h24::T - h25::T - h26::T - h27::T - h28::T - h31::T - h32::T - h33::T - h34::T - h35::T - h36::T - h37::T - h38::T - h41::T - h42::T - h43::T - h44::T - h45::T - h46::T - h47::T - h48::T + d::Vector{T} + c::Vector{T2} + H::Matrix{T} end function Rodas5Tableau(T, T2) gamma = convert(T2, 0.19) - a21 = convert(T, 2.0) - a31 = convert(T, 3.040894194418781) - a32 = convert(T, 1.041747909077569) - a41 = convert(T, 2.576417536461461) - a42 = convert(T, 1.622083060776640) - a43 = convert(T, -0.9089668560264532) - a51 = convert(T, 2.760842080225597) - a52 = convert(T, 1.446624659844071) - a53 = convert(T, -0.3036980084553738) - a54 = convert(T, 0.2877498600325443) - a61 = convert(T, -14.09640773051259) - a62 = convert(T, 6.925207756232704) - a63 = convert(T, -41.47510893210728) - a64 = convert(T, 2.343771018586405) - a65 = convert(T, 24.13215229196062) - C21 = convert(T, -10.31323885133993) - C31 = convert(T, -21.04823117650003) - C32 = convert(T, -7.234992135176716) - C41 = convert(T, 32.22751541853323) - C42 = convert(T, -4.943732386540191) - C43 = convert(T, 19.44922031041879) - C51 = convert(T, -20.69865579590063) - C52 = convert(T, -8.816374604402768) - C53 = convert(T, 1.260436877740897) - C54 = convert(T, -0.7495647613787146) - C61 = convert(T, -46.22004352711257) - C62 = convert(T, -17.49534862857472) - C63 = convert(T, -289.6389582892057) - C64 = convert(T, 93.60855400400906) - C65 = convert(T, 318.3822534212147) - C71 = convert(T, 34.20013733472935) - C72 = convert(T, -14.15535402717690) - C73 = convert(T, 57.82335640988400) - C74 = convert(T, 25.83362985412365) - C75 = convert(T, 1.408950972071624) - C76 = convert(T, -6.551835421242162) - C81 = convert(T, 42.57076742291101) - C82 = convert(T, -13.80770672017997) - C83 = convert(T, 93.98938432427124) - C84 = convert(T, 18.77919633714503) - C85 = convert(T, -31.58359187223370) - C86 = convert(T, -6.685968952921985) - C87 = convert(T, -5.810979938412932) - c2 = convert(T2, 0.38) - c3 = convert(T2, 0.3878509998321533) - c4 = convert(T2, 0.4839718937873840) - c5 = convert(T2, 0.4570477008819580) - d1 = convert(T, gamma) - d2 = convert(T, -0.1823079225333714636) - d3 = convert(T, -0.319231832186874912) - d4 = convert(T, 0.3449828624725343) - d5 = convert(T, -0.377417564392089818) - - h21 = convert(T, 27.354592673333357) - h22 = convert(T, -6.925207756232857) - h23 = convert(T, 26.40037733258859) - h24 = convert(T, 0.5635230501052979) - h25 = convert(T, -4.699151156849391) - h26 = convert(T, -1.6008677469422725) - h27 = convert(T, -1.5306074446748028) - h28 = convert(T, -1.3929872940716344) - - h31 = convert(T, 44.19024239501722) - h32 = convert(T, 1.3677947663381929e-13) - h33 = convert(T, 202.93261852171622) - h34 = convert(T, -35.5669339789154) - h35 = convert(T, -181.91095152160645) - h36 = convert(T, 3.4116351403665033) - h37 = convert(T, 2.5793540257308067) - h38 = convert(T, 2.2435122582734066) - - h41 = convert(T, -44.0988150021747) - h42 = convert(T, -5.755396159656812e-13) - h43 = convert(T, -181.26175034586677) - h44 = convert(T, 56.99302194811676) - h45 = convert(T, 183.21182741427398) - h46 = convert(T, -7.480257918273637) - h47 = convert(T, -5.792426076169686) - h48 = convert(T, -5.32503859794143) - + A = T[ + 0 0 0 0 0 0 + 2.0 0 0 0 0 0 + 3.040894194418781 1.041747909077569 0 0 0 0 + 2.576417536461461 1.622083060776640 -0.9089668560264532 0 0 0 + 2.760842080225597 1.446624659844071 -0.3036980084553738 0.2877498600325443 0 0 + -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 0 + ] + C = T[ + 0 0 0 0 0 0 0 0 + -10.31323885133993 0 0 0 0 0 0 0 + -21.04823117650003 -7.234992135176716 0 0 0 0 0 0 + 32.22751541853323 -4.943732386540191 19.44922031041879 0 0 0 0 0 + -20.69865579590063 -8.816374604402768 1.260436877740897 -0.7495647613787146 0 0 0 0 + -46.22004352711257 -17.49534862857472 -289.6389582892057 93.60855400400906 318.3822534212147 0 0 0 + 34.20013733472935 -14.15535402717690 57.82335640988400 25.83362985412365 1.408950972071624 -6.551835421242162 0 0 + 42.57076742291101 -13.80770672017997 93.98938432427124 18.77919633714503 -31.58359187223370 -6.685968952921985 -5.810979938412932 0 + ] + c = T2[0, 0.38, 0.3878509998321533, 0.4839718937873840, 0.4570477008819580] + d = T[gamma, -0.1823079225333714636, -0.319231832186874912, 0.3449828624725343, -0.377417564392089818] + + H = T[ + 0 0 0 0 0 0 0 0 + 27.354592673333357 -6.925207756232857 26.40037733258859 0.5635230501052979 -4.699151156849391 -1.6008677469422725 -1.5306074446748028 -1.3929872940716344 + 44.19024239501722 1.3677947663381929e-13 202.93261852171622 -35.5669339789154 -181.91095152160645 3.4116351403665033 2.5793540257308067 2.2435122582734066 + -44.0988150021747 -5.755396159656812e-13 -181.26175034586677 56.99302194811676 183.21182741427398 -7.480257918273637 -5.792426076169686 -5.32503859794143 + ] # println("---Rodas5---") #= @@ -525,111 +401,38 @@ function Rodas5Tableau(T, T2) b7 = convert(T,1) b8 = convert(T,1) =# - - Rodas5Tableau(a21, a31, a32, a41, a42, a43, a51, a52, a53, a54, - a61, a62, a63, a64, a65, - C21, C31, C32, C41, C42, C43, C51, C52, C53, C54, - C61, C62, C63, C64, C65, C71, C72, C73, C74, C75, C76, - C81, C82, C83, C84, C85, C86, C87, - gamma, d1, d2, d3, d4, d5, c2, c3, c4, c5, - h21, h22, h23, h24, h25, h26, h27, h28, h31, h32, h33, h34, h35, h36, h37, - h38, h41, h42, h43, h44, h45, h46, h47, h48) + Rodas5Tableau(A, C, gamma, d, c, H) end function Rodas5PTableau(T, T2) gamma = convert(T2, 0.21193756319429014) - - a21 = convert(T, 3.0) - a31 = convert(T, 2.849394379747939) - a32 = convert(T, 0.45842242204463923) - a41 = convert(T, -6.954028509809101) - a42 = convert(T, 2.489845061869568) - a43 = convert(T, -10.358996098473584) - a51 = convert(T, 2.8029986275628964) - a52 = convert(T, 0.5072464736228206) - a53 = convert(T, -0.3988312541770524) - a54 = convert(T, -0.04721187230404641) - a61 = convert(T, -7.502846399306121) - a62 = convert(T, 2.561846144803919) - a63 = convert(T, -11.627539656261098) - a64 = convert(T, -0.18268767659942256) - a65 = convert(T, 0.030198172008377946) - - C21 = convert(T, -14.155112264123755) - C31 = convert(T, -17.97296035885952) - C32 = convert(T, -2.859693295451294) - C41 = convert(T, 147.12150275711716) - C42 = convert(T, -1.41221402718213) - C43 = convert(T, 71.68940251302358) - C51 = convert(T, 165.43517024871676) - C52 = convert(T, -0.4592823456491126) - C53 = convert(T, 42.90938336958603) - C54 = convert(T, -5.961986721573306) - C61 = convert(T, 24.854864614690072) - C62 = convert(T, -3.0009227002832186) - C63 = convert(T, 47.4931110020768) - C64 = convert(T, 5.5814197821558125) - C65 = convert(T, -0.6610691825249471) - C71 = convert(T, 30.91273214028599) - C72 = convert(T, -3.1208243349937974) - C73 = convert(T, 77.79954646070892) - C74 = convert(T, 34.28646028294783) - C75 = convert(T, -19.097331116725623) - C76 = convert(T, -28.087943162872662) - C81 = convert(T, 37.80277123390563) - C82 = convert(T, -3.2571969029072276) - C83 = convert(T, 112.26918849496327) - C84 = convert(T, 66.9347231244047) - C85 = convert(T, -40.06618937091002) - C86 = convert(T, -54.66780262877968) - C87 = convert(T, -9.48861652309627) - - c2 = convert(T2, 0.6358126895828704) - c3 = convert(T2, 0.4095798393397535) - c4 = convert(T2, 0.9769306725060716) - c5 = convert(T2, 0.4288403609558664) - - d1 = convert(T, 0.21193756319429014) - d2 = convert(T, -0.42387512638858027) - d3 = convert(T, -0.3384627126235924) - d4 = convert(T, 1.8046452872882734) - d5 = convert(T, 2.325825639765069) - - h21 = convert(T, 25.948786856663858) - h22 = convert(T, -2.5579724845846235) - h23 = convert(T, 10.433815404888879) - h24 = convert(T, -2.3679251022685204) - h25 = convert(T, 0.524948541321073) - h26 = convert(T, 1.1241088310450404) - h27 = convert(T, 0.4272876194431874) - h28 = convert(T, -0.17202221070155493) - - h31 = convert(T, -9.91568850695171) - h32 = convert(T, -0.9689944594115154) - h33 = convert(T, 3.0438037242978453) - h34 = convert(T, -24.495224566215796) - h35 = convert(T, 20.176138334709044) - h36 = convert(T, 15.98066361424651) - h37 = convert(T, -6.789040303419874) - h38 = convert(T, -6.710236069923372) - - h41 = convert(T, 11.419903575922262) - h42 = convert(T, 2.8879645146136994) - h43 = convert(T, 72.92137995996029) - h44 = convert(T, 80.12511834622643) - h45 = convert(T, -52.072871366152654) - h46 = convert(T, -59.78993625266729) - h47 = convert(T, -0.15582684282751913) - h48 = convert(T, 4.883087185713722) - - Rodas5Tableau(a21, a31, a32, a41, a42, a43, a51, a52, a53, a54, - a61, a62, a63, a64, a65, - C21, C31, C32, C41, C42, C43, C51, C52, C53, C54, - C61, C62, C63, C64, C65, C71, C72, C73, C74, C75, C76, - C81, C82, C83, C84, C85, C86, C87, - gamma, d1, d2, d3, d4, d5, c2, c3, c4, c5, - h21, h22, h23, h24, h25, h26, h27, h28, h31, h32, h33, h34, h35, h36, h37, - h38, h41, h42, h43, h44, h45, h46, h47, h48) + A = T[ + 0 0 0 0 0 0 + 3.0 0 0 0 0 0 + 2.849394379747939 0.45842242204463923 0 0 0 0 + -6.954028509809101 2.489845061869568 -10.358996098473584 0 0 0 + 2.8029986275628964 0.5072464736228206 -0.3988312541770524 -0.04721187230404641 0 0 + -7.502846399306121 2.561846144803919 -11.627539656261098 -0.18268767659942256 0.030198172008377946 0 + ] + C = T[ + 0 0 0 0 0 0 0 0 + -14.155112264123755 0 0 0 0 0 0 0 + -17.97296035885952 -2.859693295451294 0 0 0 0 0 0 + 147.12150275711716 -1.41221402718213 71.68940251302358 0 0 0 0 0 + 165.43517024871676 -0.4592823456491126 42.90938336958603 -5.961986721573306 0 0 0 0 + 24.854864614690072 -3.0009227002832186 47.4931110020768 5.5814197821558125 -0.6610691825249471 0 0 0 + 30.91273214028599 -3.1208243349937974 77.79954646070892 34.28646028294783 -19.097331116725623 -28.087943162872662 0 0 + 37.80277123390563 -3.2571969029072276 112.26918849496327 66.9347231244047 -40.06618937091002 -54.66780262877968 -9.48861652309627 0 + ] + c = T2[0, 0.6358126895828704, 0.4095798393397535, 0.9769306725060716, 0.4288403609558664] + d = T[0.21193756319429014, -0.42387512638858027, -0.3384627126235924, 1.8046452872882734, 2.325825639765069] + H = T[ + 0 0 0 0 0 0 0 0 + 25.948786856663858 -2.5579724845846235 10.433815404888879 -2.3679251022685204 0.524948541321073 1.1241088310450404 0.4272876194431874 -0.17202221070155493 + -9.91568850695171 -0.9689944594115154 3.0438037242978453 -24.495224566215796 20.176138334709044 15.98066361424651 -6.789040303419874 -6.710236069923372 + 11.419903575922262 2.8879645146136994 72.92137995996029 80.12511834622643 -52.072871366152654 -59.78993625266729 -0.15582684282751913 4.883087185713722 + ] + Rodas5Tableau(A, C, gamma, d, c, H) end @RosenbrockW6S4OS(:tableau) @@ -679,4 +482,4 @@ beta3 = 6.8619167645278386e-2 beta4 = 0.8289547562599182 beta5 = 7.9630136489868164e-2 alpha64 = -0.2076823627400282 -=# +=# \ No newline at end of file From bece07904cb0143f7dfd5984cfc16cf05819f698 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 3 Sep 2024 00:40:29 +0530 Subject: [PATCH 11/56] Fixes --- .../src/rosenbrock_tableaus.jl | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl index fa20bff4b5..94a04687da 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl @@ -350,25 +350,25 @@ end function Rodas5Tableau(T, T2) gamma = convert(T2, 0.19) A = T[ - 0 0 0 0 0 0 - 2.0 0 0 0 0 0 - 3.040894194418781 1.041747909077569 0 0 0 0 - 2.576417536461461 1.622083060776640 -0.9089668560264532 0 0 0 - 2.760842080225597 1.446624659844071 -0.3036980084553738 0.2877498600325443 0 0 + 0 0 0 0 0 0 + 2.0 0 0 0 0 0 + 3.040894194418781 1.041747909077569 0 0 0 0 + 2.576417536461461 1.622083060776640 -0.9089668560264532 0 0 0 + 2.760842080225597 1.446624659844071 -0.3036980084553738 0.2877498600325443 0 0 -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 0 ] C = T[ - 0 0 0 0 0 0 0 0 - -10.31323885133993 0 0 0 0 0 0 0 - -21.04823117650003 -7.234992135176716 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 + -10.31323885133993 0 0 0 0 0 0 0 + -21.04823117650003 -7.234992135176716 0 0 0 0 0 0 32.22751541853323 -4.943732386540191 19.44922031041879 0 0 0 0 0 -20.69865579590063 -8.816374604402768 1.260436877740897 -0.7495647613787146 0 0 0 0 -46.22004352711257 -17.49534862857472 -289.6389582892057 93.60855400400906 318.3822534212147 0 0 0 34.20013733472935 -14.15535402717690 57.82335640988400 25.83362985412365 1.408950972071624 -6.551835421242162 0 0 42.57076742291101 -13.80770672017997 93.98938432427124 18.77919633714503 -31.58359187223370 -6.685968952921985 -5.810979938412932 0 ] - c = T2[0, 0.38, 0.3878509998321533, 0.4839718937873840, 0.4570477008819580] - d = T[gamma, -0.1823079225333714636, -0.319231832186874912, 0.3449828624725343, -0.377417564392089818] + c = T2[0, 0.38, 0.3878509998321533, 0.4839718937873840, 0.4570477008819580, 1, 1] + d = T[gamma, -0.1823079225333714636, -0.319231832186874912, 0.3449828624725343, -0.377417564392089818, 0, 0, 0] H = T[ 0 0 0 0 0 0 0 0 @@ -407,22 +407,23 @@ end function Rodas5PTableau(T, T2) gamma = convert(T2, 0.21193756319429014) A = T[ - 0 0 0 0 0 0 - 3.0 0 0 0 0 0 - 2.849394379747939 0.45842242204463923 0 0 0 0 - -6.954028509809101 2.489845061869568 -10.358996098473584 0 0 0 - 2.8029986275628964 0.5072464736228206 -0.3988312541770524 -0.04721187230404641 0 0 + 0 0 0 0 0 0 + 3.0 0 0 0 0 0 + 2.849394379747939 0.45842242204463923 0 0 0 0 + -6.954028509809101 2.489845061869568 -10.358996098473584 0 0 0 + 2.8029986275628964 0.5072464736228206 -0.3988312541770524 -0.04721187230404641 0 0 -7.502846399306121 2.561846144803919 -11.627539656261098 -0.18268767659942256 0.030198172008377946 0 + -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 1 0 ] C = T[ - 0 0 0 0 0 0 0 0 - -14.155112264123755 0 0 0 0 0 0 0 - -17.97296035885952 -2.859693295451294 0 0 0 0 0 0 - 147.12150275711716 -1.41221402718213 71.68940251302358 0 0 0 0 0 - 165.43517024871676 -0.4592823456491126 42.90938336958603 -5.961986721573306 0 0 0 0 - 24.854864614690072 -3.0009227002832186 47.4931110020768 5.5814197821558125 -0.6610691825249471 0 0 0 - 30.91273214028599 -3.1208243349937974 77.79954646070892 34.28646028294783 -19.097331116725623 -28.087943162872662 0 0 - 37.80277123390563 -3.2571969029072276 112.26918849496327 66.9347231244047 -40.06618937091002 -54.66780262877968 -9.48861652309627 0 + 0 0 0 0 0 0 0 0 + -14.155112264123755 0 0 0 0 0 0 0 + -17.97296035885952 -2.859693295451294 0 0 0 0 0 0 + 147.12150275711716 -1.41221402718213 71.68940251302358 0 0 0 0 0 + 165.43517024871676 -0.4592823456491126 42.90938336958603 -5.961986721573306 0 0 0 0 + 24.854864614690072 -3.0009227002832186 47.4931110020768 5.5814197821558125 -0.6610691825249471 0 0 0 + 30.91273214028599 -3.1208243349937974 77.79954646070892 34.28646028294783 -19.097331116725623 -28.087943162872662 0 0 + 37.80277123390563 -3.2571969029072276 112.26918849496327 66.9347231244047 -40.06618937091002 -54.66780262877968 -9.48861652309627 0 ] c = T2[0, 0.6358126895828704, 0.4095798393397535, 0.9769306725060716, 0.4288403609558664] d = T[0.21193756319429014, -0.42387512638858027, -0.3384627126235924, 1.8046452872882734, 2.325825639765069] From a584c7510bddfc23899cb27f9cf99507b8d66c96 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 3 Sep 2024 00:59:25 +0530 Subject: [PATCH 12/56] Fixes --- .../src/rosenbrock_tableaus.jl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl index 94a04687da..aaefb5411a 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl @@ -358,13 +358,13 @@ function Rodas5Tableau(T, T2) -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 0 ] C = T[ - 0 0 0 0 0 0 0 0 - -10.31323885133993 0 0 0 0 0 0 0 - -21.04823117650003 -7.234992135176716 0 0 0 0 0 0 - 32.22751541853323 -4.943732386540191 19.44922031041879 0 0 0 0 0 - -20.69865579590063 -8.816374604402768 1.260436877740897 -0.7495647613787146 0 0 0 0 - -46.22004352711257 -17.49534862857472 -289.6389582892057 93.60855400400906 318.3822534212147 0 0 0 - 34.20013733472935 -14.15535402717690 57.82335640988400 25.83362985412365 1.408950972071624 -6.551835421242162 0 0 + 0 0 0 0 0 0 0 0 + -10.31323885133993 0 0 0 0 0 0 0 + -21.04823117650003 -7.234992135176716 0 0 0 0 0 0 + 32.22751541853323 -4.943732386540191 19.44922031041879 0 0 0 0 0 + -20.69865579590063 -8.816374604402768 1.260436877740897 -0.7495647613787146 0 0 0 0 + -46.22004352711257 -17.49534862857472 -289.6389582892057 93.60855400400906 318.3822534212147 0 0 0 + 34.20013733472935 -14.15535402717690 57.82335640988400 25.83362985412365 1.408950972071624 -6.551835421242162 0 0 42.57076742291101 -13.80770672017997 93.98938432427124 18.77919633714503 -31.58359187223370 -6.685968952921985 -5.810979938412932 0 ] c = T2[0, 0.38, 0.3878509998321533, 0.4839718937873840, 0.4570477008819580, 1, 1] @@ -413,7 +413,7 @@ function Rodas5PTableau(T, T2) -6.954028509809101 2.489845061869568 -10.358996098473584 0 0 0 2.8029986275628964 0.5072464736228206 -0.3988312541770524 -0.04721187230404641 0 0 -7.502846399306121 2.561846144803919 -11.627539656261098 -0.18268767659942256 0.030198172008377946 0 - -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 1 0 + -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 1 ] C = T[ 0 0 0 0 0 0 0 0 From 4249302b94c7579bfed3e7dff0634485ef98a41c Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 3 Sep 2024 01:11:48 +0530 Subject: [PATCH 13/56] Fixes --- lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl index aaefb5411a..c29ba978df 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl @@ -425,8 +425,8 @@ function Rodas5PTableau(T, T2) 30.91273214028599 -3.1208243349937974 77.79954646070892 34.28646028294783 -19.097331116725623 -28.087943162872662 0 0 37.80277123390563 -3.2571969029072276 112.26918849496327 66.9347231244047 -40.06618937091002 -54.66780262877968 -9.48861652309627 0 ] - c = T2[0, 0.6358126895828704, 0.4095798393397535, 0.9769306725060716, 0.4288403609558664] - d = T[0.21193756319429014, -0.42387512638858027, -0.3384627126235924, 1.8046452872882734, 2.325825639765069] + c = T2[0, 0.6358126895828704, 0.4095798393397535, 0.9769306725060716, 0.4288403609558664, 1, 1] + d = T[0.21193756319429014, -0.42387512638858027, -0.3384627126235924, 1.8046452872882734, 2.325825639765069, 0, 0, 0] H = T[ 0 0 0 0 0 0 0 0 25.948786856663858 -2.5579724845846235 10.433815404888879 -2.3679251022685204 0.524948541321073 1.1241088310450404 0.4272876194431874 -0.17202221070155493 From 875b2c4340e243c1b5e5a0adf91684a5d27d6ace Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 3 Sep 2024 01:18:45 +0530 Subject: [PATCH 14/56] Fixes --- .../src/rosenbrock_tableaus.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl index c29ba978df..a7f8f634da 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl @@ -407,13 +407,13 @@ end function Rodas5PTableau(T, T2) gamma = convert(T2, 0.21193756319429014) A = T[ - 0 0 0 0 0 0 - 3.0 0 0 0 0 0 - 2.849394379747939 0.45842242204463923 0 0 0 0 - -6.954028509809101 2.489845061869568 -10.358996098473584 0 0 0 - 2.8029986275628964 0.5072464736228206 -0.3988312541770524 -0.04721187230404641 0 0 - -7.502846399306121 2.561846144803919 -11.627539656261098 -0.18268767659942256 0.030198172008377946 0 - -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 1 + 0 0 0 0 0 0 0 + 3.0 0 0 0 0 0 0 + 2.849394379747939 0.45842242204463923 0 0 0 0 0 + -6.954028509809101 2.489845061869568 -10.358996098473584 0 0 0 0 + 2.8029986275628964 0.5072464736228206 -0.3988312541770524 -0.04721187230404641 0 0 0 + -7.502846399306121 2.561846144803919 -11.627539656261098 -0.18268767659942256 0.030198172008377946 0 0 + -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 1 0 ] C = T[ 0 0 0 0 0 0 0 0 From 574d049b5047b0c1ccd6e50fe659c98b4bd4286b Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 3 Sep 2024 01:23:20 +0530 Subject: [PATCH 15/56] Fixes --- lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl index a7f8f634da..be341755a8 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl @@ -413,7 +413,8 @@ function Rodas5PTableau(T, T2) -6.954028509809101 2.489845061869568 -10.358996098473584 0 0 0 0 2.8029986275628964 0.5072464736228206 -0.3988312541770524 -0.04721187230404641 0 0 0 -7.502846399306121 2.561846144803919 -11.627539656261098 -0.18268767659942256 0.030198172008377946 0 0 - -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 1 0 + -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 1 0 + -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 1 0 ] C = T[ 0 0 0 0 0 0 0 0 From 27c8b5a6b40e48d072f7a69f5e48e8c11f6f29df Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 3 Sep 2024 01:24:42 +0530 Subject: [PATCH 16/56] Changes --- lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl index be341755a8..4c61a793da 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl @@ -413,7 +413,7 @@ function Rodas5PTableau(T, T2) -6.954028509809101 2.489845061869568 -10.358996098473584 0 0 0 0 2.8029986275628964 0.5072464736228206 -0.3988312541770524 -0.04721187230404641 0 0 0 -7.502846399306121 2.561846144803919 -11.627539656261098 -0.18268767659942256 0.030198172008377946 0 0 - -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 1 0 + -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 0 0 -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 1 0 ] C = T[ From 9f9c928e6865e2d8f0fc313e6968def32b8b74c6 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 3 Sep 2024 01:31:13 +0530 Subject: [PATCH 17/56] Fixes --- lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl index 4c61a793da..8f52b25568 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl @@ -367,7 +367,7 @@ function Rodas5Tableau(T, T2) 34.20013733472935 -14.15535402717690 57.82335640988400 25.83362985412365 1.408950972071624 -6.551835421242162 0 0 42.57076742291101 -13.80770672017997 93.98938432427124 18.77919633714503 -31.58359187223370 -6.685968952921985 -5.810979938412932 0 ] - c = T2[0, 0.38, 0.3878509998321533, 0.4839718937873840, 0.4570477008819580, 1, 1] + c = T2[0, 0.38, 0.3878509998321533, 0.4839718937873840, 0.4570477008819580, 1, 1, 1] d = T[gamma, -0.1823079225333714636, -0.319231832186874912, 0.3449828624725343, -0.377417564392089818, 0, 0, 0] H = T[ @@ -426,7 +426,7 @@ function Rodas5PTableau(T, T2) 30.91273214028599 -3.1208243349937974 77.79954646070892 34.28646028294783 -19.097331116725623 -28.087943162872662 0 0 37.80277123390563 -3.2571969029072276 112.26918849496327 66.9347231244047 -40.06618937091002 -54.66780262877968 -9.48861652309627 0 ] - c = T2[0, 0.6358126895828704, 0.4095798393397535, 0.9769306725060716, 0.4288403609558664, 1, 1] + c = T2[0, 0.6358126895828704, 0.4095798393397535, 0.9769306725060716, 0.4288403609558664, 1, 1, 1] d = T[0.21193756319429014, -0.42387512638858027, -0.3384627126235924, 1.8046452872882734, 2.325825639765069, 0, 0, 0] H = T[ 0 0 0 0 0 0 0 0 From 904d33285d22e26215ed3b933f90ddc88131e054 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 3 Sep 2024 23:17:01 +0530 Subject: [PATCH 18/56] Changes --- .../src/rosenbrock_caches.jl | 37 +++++-------- .../src/rosenbrock_interpolants.jl | 52 +++++++++---------- 2 files changed, 40 insertions(+), 49 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl index 267a57b1f9..97747db6b5 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl @@ -46,6 +46,16 @@ function full_cache(c::RosenbrockCache) c.ks..., c.fsalfirst, c.fsallast, c.dT, c.tmp, c.atmp, c.weight, c.linsolve_tmp] end +struct RosenbrockCombinedConstantCache{TF, UF, Tab, JType, WType, F, AD} <: RosenbrockConstantCache + tf::TF + uf::UF + tab::Tab + J::JType + W::WType + linsolve::F + autodiff::AD +end + @cache mutable struct Rosenbrock23Cache{uType, rateType, uNoUnitsType, JType, WType, TabType, TFType, UFType, F, JCType, GCType, RTolType, A, AV, StepLimiter, StageLimiter} <: RosenbrockMutableCache @@ -701,16 +711,6 @@ end ### Rodas4 methods -struct Rodas4ConstantCache{TF, UF, Tab, JType, WType, F, AD} <: RosenbrockConstantCache - tf::TF - uf::UF - tab::Tab - J::JType - W::WType - linsolve::F - autodiff::AD -end - tabtype(::Rodas4) = Rodas4Tableau tabtype(::Rodas42) = Rodas42Tableau tabtype(::Rodas4P) = Rodas4PTableau @@ -725,7 +725,7 @@ function alg_cache(alg::Union{Rodas4, Rodas42, Rodas4P, Rodas4P2}, u, rate_proto J, W = build_J_W(alg, u, uprev, p, t, dt, f, uEltypeNoUnits, Val(false)) linprob = nothing #LinearProblem(W,copy(u); u0=copy(u)) linsolve = nothing #init(linprob,alg.linsolve,alias_A=true,alias_b=true) - Rodas4ConstantCache(tf, uf, + RosenbrockCombinedConstantCache(tf, uf, tabtype(alg)(constvalue(uBottomEltypeNoUnits), constvalue(tTypeNoUnits)), J, W, linsolve, alg_autodiff(alg)) @@ -786,15 +786,6 @@ end ### Rosenbrock5 -struct Rosenbrock5ConstantCache{TF, UF, Tab, JType, WType, F} <: RosenbrockConstantCache - tf::TF - uf::UF - tab::Tab - J::JType - W::WType - linsolve::F -end - function alg_cache(alg::Rodas5, u, rate_prototype, ::Type{uEltypeNoUnits}, ::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck, @@ -843,9 +834,9 @@ function alg_cache(alg::Rodas5, u, rate_prototype, ::Type{uEltypeNoUnits}, J, W = build_J_W(alg, u, uprev, p, t, dt, f, uEltypeNoUnits, Val(false)) linprob = nothing #LinearProblem(W,copy(u); u0=copy(u)) linsolve = nothing #init(linprob,alg.linsolve,alias_A=true,alias_b=true) - Rosenbrock5ConstantCache(tf, uf, + RosenbrockCombinedConstantCache(tf, uf, Rodas5Tableau(constvalue(uBottomEltypeNoUnits), - constvalue(tTypeNoUnits)), J, W, linsolve) + constvalue(tTypeNoUnits)), J, W, linsolve, alg_autodiff(alg)) end function alg_cache( @@ -898,7 +889,7 @@ function alg_cache( J, W = build_J_W(alg, u, uprev, p, t, dt, f, uEltypeNoUnits, Val(false)) linprob = nothing #LinearProblem(W,copy(u); u0=copy(u)) linsolve = nothing #init(linprob,alg.linsolve,alias_A=true,alias_b=true) - Rosenbrock5ConstantCache(tf, uf, + RosenbrockConstantCache(tf, uf, Rodas5PTableau(constvalue(uBottomEltypeNoUnits), constvalue(tTypeNoUnits)), J, W, linsolve) end diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl index 5d5e331abd..c986c96fb2 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl @@ -4,7 +4,7 @@ ROSENBROCKS_WITH_INTERPOLATIONS = Union{Rosenbrock23ConstantCache, Rosenbrock23C Rosenbrock32ConstantCache, Rosenbrock32Cache, Rodas23WConstantCache, Rodas3PConstantCache, Rodas23WCache, Rodas3PCache, - Rodas4ConstantCache, Rosenbrock5ConstantCache, + RosenbrockCombinedConstantCache, RosenbrockCache} function _ode_interpolant(Θ, dt, y₀, y₁, k, @@ -128,7 +128,7 @@ end From MATLAB ODE Suite by Shampine """ @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rodas4ConstantCache, Rodas23WConstantCache, Rodas3PConstantCache}, + cache::Union{RosenbrockCombinedConstantCache, Rodas23WConstantCache, Rodas3PConstantCache}, idxs::Nothing, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ @inbounds Θ1 * y₀ + Θ * (y₁ + Θ1 * (k[1] + Θ * k[2])) @@ -142,7 +142,7 @@ end end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rodas4ConstantCache, RosenbrockCache, Rodas23WConstantCache, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ @@ -150,7 +150,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rodas4ConstantCache, RosenbrockCache, Rodas23WConstantCache, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs::Nothing, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ @@ -159,7 +159,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rodas4ConstantCache, RosenbrockCache, Rodas23WConstantCache, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ @@ -170,7 +170,7 @@ end # First Derivative @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rodas4ConstantCache, Rodas23WConstantCache, Rodas3PConstantCache}, + cache::Union{RosenbrockCombinedConstantCache, Rodas23WConstantCache, Rodas3PConstantCache}, idxs::Nothing, T::Type{Val{1}}, differential_vars) @inbounds (k[1] + Θ * (-2 * k[1] + 2 * k[2] - 3 * k[2] * Θ) - y₀ + y₁) / dt end @@ -183,7 +183,7 @@ end end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rodas4ConstantCache, RosenbrockCache, Rodas23WConstantCache, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs, T::Type{Val{1}}, differential_vars) @.. broadcast=false (k[1][idxs] + @@ -192,7 +192,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rodas4ConstantCache, RosenbrockCache, Rodas23WConstantCache, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs::Nothing, T::Type{Val{1}}, differential_vars) @.. broadcast=false out=(k[1] + Θ * (-2 * k[1] + 2 * k[2] - 3 * k[2] * Θ) - y₀ + y₁) / @@ -201,7 +201,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rodas4ConstantCache, RosenbrockCache, Rodas23WConstantCache, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs, T::Type{Val{1}}, differential_vars) @views @.. broadcast=false out=(k[1][idxs] + @@ -227,7 +227,7 @@ end end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ @.. broadcast=false Θ1 * @@ -236,7 +236,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs::Nothing, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ @.. broadcast=false out=Θ1 * y₀ + Θ * (y₁ + Θ1 * (k[1] + Θ * (k[2] + Θ * k[3]))) @@ -244,7 +244,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ @views @.. broadcast=false out=Θ1 * y₀[idxs] + @@ -254,14 +254,14 @@ end end # First Derivative -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Rosenbrock5ConstantCache, +@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::RosenbrockCombinedConstantCache, idxs::Nothing, T::Type{Val{1}}, differential_vars) @inbounds (k[1] + Θ * (-2 * k[1] + 2 * k[2] + Θ * (-3 * k[2] + 3 * k[3] - 4 * Θ * k[3])) - y₀ + y₁) / dt end -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::RosenbrockCache, idxs::Nothing, +@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::RosenbrockCombinedConstantCache, idxs::Nothing, T::Type{Val{1}}, differential_vars) @inbounds @.. broadcast=false (k[1] + Θ * (-2 * k[1] + 2 * k[2] + @@ -269,7 +269,7 @@ end end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs, T::Type{Val{1}}, differential_vars) @.. broadcast=false (k[1][idxs] + Θ * (-2 * k[1][idxs] + 2 * k[2][idxs] + @@ -278,7 +278,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs::Nothing, T::Type{Val{1}}, differential_vars) @.. broadcast=false out=(k[1] + Θ * (-2 * k[1] + 2 * k[2] + @@ -287,7 +287,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs, T::Type{Val{1}}, differential_vars) @views @.. broadcast=false out=(k[1][idxs] + Θ * (-2 * k[1][idxs] + 2 * k[2][idxs] + @@ -298,7 +298,7 @@ end end # Second Derivative -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Rosenbrock5ConstantCache, +@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::RosenbrockCombinedConstantCache, idxs::Nothing, T::Type{Val{2}}, differential_vars) @inbounds (-2 * k[1] + 2 * k[2] + Θ * (-6 * k[2] + 6 * k[3] - 12 * Θ * k[3])) / dt^2 end @@ -310,14 +310,14 @@ end end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs, T::Type{Val{2}}, differential_vars) @.. broadcast=false (-2 * k[1][idxs] + 2 * k[2][idxs] + Θ * (-6 * k[2][idxs] + 6 * k[3][idxs] - 12 * Θ * k[3][idxs]))/dt^2 end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs::Nothing, T::Type{Val{2}}, differential_vars) @.. broadcast=false out=(-2 * k[1] + 2 * k[2] + Θ * (-6 * k[2] + 6 * k[3] - 12 * Θ * k[3])) / dt^2 @@ -325,7 +325,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs, T::Type{Val{2}}, differential_vars) @views @.. broadcast=false out=(-2 * k[1][idxs] + 2 * k[2][idxs] + Θ * @@ -335,7 +335,7 @@ end end # Third Derivative -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Rosenbrock5ConstantCache, +@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::RosenbrockCombinedConstantCache, idxs::Nothing, T::Type{Val{3}}, differential_vars) @inbounds (-6 * k[2] + 6 * k[3] - 24 * Θ * k[3]) / dt^3 end @@ -346,22 +346,22 @@ end end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs, T::Type{Val{3}}, differential_vars) @.. broadcast=false (-6 * k[2][idxs] + 6 * k[3][idxs] - 24 * Θ * k[3][idxs])/dt^3 end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs::Nothing, T::Type{Val{3}}, differential_vars) @.. broadcast=false out=(-6 * k[2] + 6 * k[3] - 24 * Θ * k[3]) / dt^3 out end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock5ConstantCache, RosenbrockCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs, T::Type{Val{3}}, differential_vars) @views @.. broadcast=false out=(-6 * k[2][idxs] + 6 * k[3][idxs] - 24 * Θ * k[3][idxs]) / dt^3 out -end +end \ No newline at end of file From 37546da3de105e8b602c2e3ade3691faf376849f Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 3 Sep 2024 23:21:09 +0530 Subject: [PATCH 19/56] Changes --- lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl b/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl index 8862e82bf3..07f4bbeee7 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl @@ -11,7 +11,7 @@ end function DiffEqBase.interp_summary(::Type{cacheType}, dense::Bool) where { cacheType <: - Union{Rodas4ConstantCache, Rodas23WConstantCache, Rodas3PConstantCache, + Union{RosenbrockCombinedConstantCache, Rodas23WConstantCache, Rodas3PConstantCache, RosenbrockCache, Rodas23WCache, Rodas3PCache}} dense ? "specialized 3rd order \"free\" stiffness-aware interpolation" : "1st order linear" @@ -20,7 +20,7 @@ end function DiffEqBase.interp_summary(::Type{cacheType}, dense::Bool) where { cacheType <: - Union{Rosenbrock5ConstantCache, + Union{RosenbrockCombinedConstantCache, RosenbrockCache}} dense ? "specialized 4rd order \"free\" stiffness-aware interpolation" : "1st order linear" From d842e88cdfc3b3b8382e1595e88cde647f2db398 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 3 Sep 2024 23:24:21 +0530 Subject: [PATCH 20/56] Fixes --- lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl index c986c96fb2..7df6d5dc87 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl @@ -214,7 +214,7 @@ end #- -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Rosenbrock5ConstantCache, +@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::RosenbrockCombinedConstantCache, idxs::Nothing, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ @inbounds Θ1 * y₀ + Θ * (y₁ + Θ1 * (k[1] + Θ * (k[2] + Θ * k[3]))) From 1897edb4133a5f4350d38864d1aa4a5c6b084223 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 3 Sep 2024 23:34:53 +0530 Subject: [PATCH 21/56] Minor fixes done --- .../src/rosenbrock_perform_step.jl | 8 ++++---- lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index e43e5cd343..2632371fe4 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -1198,7 +1198,7 @@ end #### Rodas4 type method -function initialize!(integrator, cache::Rodas4ConstantCache) +function initialize!(integrator, cache::RosenbrockCombinedConstantCache) integrator.kshortsize = 2 integrator.k = typeof(integrator.k)(undef, integrator.kshortsize) # Avoid undefined entries if k is an array of arrays @@ -1206,7 +1206,7 @@ function initialize!(integrator, cache::Rodas4ConstantCache) integrator.k[2] = zero(integrator.u) end -@muladd function perform_step!(integrator, cache::Rodas4ConstantCache, repeat_step = false) +@muladd function perform_step!(integrator, cache::RosenbrockCombinedConstantCache, repeat_step = false) (;t, dt, uprev, u, f, p) = integrator (;tf, uf) = cache (;A, C, gamma, c, d, H) = cache.tab @@ -1412,7 +1412,7 @@ end ### Rodas5 Method -function initialize!(integrator, cache::Rosenbrock5ConstantCache) +function initialize!(integrator, cache::RosenbrockCombinedConstantCache) integrator.kshortsize = 3 integrator.k = typeof(integrator.k)(undef, integrator.kshortsize) # Avoid undefined entries if k is an array of arrays @@ -1421,7 +1421,7 @@ function initialize!(integrator, cache::Rosenbrock5ConstantCache) integrator.k[3] = zero(integrator.u) end -@muladd function perform_step!(integrator, cache::Rosenbrock5ConstantCache, +@muladd function perform_step!(integrator, cache::RosenbrockCombinedConstantCache, repeat_step = false) @unpack t, dt, uprev, u, f, p = integrator @unpack tf, uf = cache diff --git a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl index 13b201fb94..ac61f6757c 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl @@ -287,7 +287,7 @@ function _ode_addsteps!( nothing end -function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::Rodas4ConstantCache, +function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::RosenbrockCombinedConstantCache, always_calc_begin = false, allow_calc_end = true, force_calc_end = false) if length(k) < 2 || always_calc_begin @@ -429,7 +429,7 @@ function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::RosenbrockCache, nothing end -function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::Rosenbrock5ConstantCache, +function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::RosenbrockCombinedConstantCache, always_calc_begin = false, allow_calc_end = true, force_calc_end = false) if length(k) < 3 || always_calc_begin From e69b28dc94adb68a5298120529b706a7a28f318b Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 3 Sep 2024 23:49:41 +0530 Subject: [PATCH 22/56] Changes --- .../src/rosenbrock_perform_step.jl | 230 ++---------------- 1 file changed, 15 insertions(+), 215 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index 2632371fe4..6e33275ee9 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -1280,6 +1280,21 @@ end integrator.k[j] = @.. integrator.k[j] + H[j, i] * ks[i] end end + if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && + (integrator.EEst < 1.0) + k2 = 0.5 * (uprev + u + + 0.5 * (integrator.k[1] + 0.5 * (integrator.k[2] + 0.5 * integrator.k[3]))) + du1 = (0.25 * (integrator.k[2] + integrator.k[3]) - uprev + u) / dt + du = f(k2, p, t + dt / 2) + OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) + if mass_matrix === I + du2 = du1 - du + else + du2 = mass_matrix * du1 - du + end + EEst = norm(du2) / norm(integrator.opts.abstol .+ integrator.opts.reltol .* k2) + integrator.EEst = max(EEst, integrator.EEst) + end end integrator.u = u @@ -1408,220 +1423,5 @@ end cache.linsolve = linres.cache end -############################################################################### - -### Rodas5 Method - -function initialize!(integrator, cache::RosenbrockCombinedConstantCache) - integrator.kshortsize = 3 - integrator.k = typeof(integrator.k)(undef, integrator.kshortsize) - # Avoid undefined entries if k is an array of arrays - integrator.k[1] = zero(integrator.u) - integrator.k[2] = zero(integrator.u) - integrator.k[3] = zero(integrator.u) -end - -@muladd function perform_step!(integrator, cache::RosenbrockCombinedConstantCache, - repeat_step = false) - @unpack t, dt, uprev, u, f, p = integrator - @unpack tf, uf = cache - @unpack a21, a31, a32, a41, a42, a43, a51, a52, a53, a54, a61, a62, a63, a64, a65, C21, C31, C32, C41, C42, C43, C51, C52, C53, C54, C61, C62, C63, C64, C65, C71, C72, C73, C74, C75, C76, C81, C82, C83, C84, C85, C86, C87, gamma, d1, d2, d3, d4, d5, c2, c3, c4, c5 = cache.tab - - # Precalculations - dtC21 = C21 / dt - dtC31 = C31 / dt - dtC32 = C32 / dt - dtC41 = C41 / dt - dtC42 = C42 / dt - dtC43 = C43 / dt - dtC51 = C51 / dt - dtC52 = C52 / dt - dtC53 = C53 / dt - dtC54 = C54 / dt - dtC61 = C61 / dt - dtC62 = C62 / dt - dtC63 = C63 / dt - dtC64 = C64 / dt - dtC65 = C65 / dt - dtC71 = C71 / dt - dtC72 = C72 / dt - dtC73 = C73 / dt - dtC74 = C74 / dt - dtC75 = C75 / dt - dtC76 = C76 / dt - dtC81 = C81 / dt - dtC82 = C82 / dt - dtC83 = C83 / dt - dtC84 = C84 / dt - dtC85 = C85 / dt - dtC86 = C86 / dt - dtC87 = C87 / dt - - dtd1 = dt * d1 - dtd2 = dt * d2 - dtd3 = dt * d3 - dtd4 = dt * d4 - dtd5 = dt * d5 - dtgamma = dt * gamma - - mass_matrix = integrator.f.mass_matrix - - # Time derivative - dT = calc_tderivative(integrator, cache) - - W = calc_W(integrator, cache, dtgamma, repeat_step, true) - if !issuccess_W(W) - integrator.EEst = 2 - return nothing - end - - du1 = f(uprev, p, t) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - linsolve_tmp = du1 + dtd1 * dT - - k1 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - u = uprev + a21 * k1 - du = f(u, p, t + c2 * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - linsolve_tmp = du + dtd2 * dT + dtC21 * k1 - else - linsolve_tmp = du + dtd2 * dT + mass_matrix * (dtC21 * k1) - end - - k2 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - u = uprev + a31 * k1 + a32 * k2 - du = f(u, p, t + c3 * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - linsolve_tmp = du + dtd3 * dT + (dtC31 * k1 + dtC32 * k2) - else - linsolve_tmp = du + dtd3 * dT + mass_matrix * (dtC31 * k1 + dtC32 * k2) - end - - k3 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - u = uprev + a41 * k1 + a42 * k2 + a43 * k3 - du = f(u, p, t + c4 * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - linsolve_tmp = du + dtd4 * dT + (dtC41 * k1 + dtC42 * k2 + dtC43 * k3) - else - linsolve_tmp = du + dtd4 * dT + mass_matrix * (dtC41 * k1 + dtC42 * k2 + dtC43 * k3) - end - - k4 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - u = uprev + a51 * k1 + a52 * k2 + a53 * k3 + a54 * k4 - du = f(u, p, t + c5 * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - linsolve_tmp = du + dtd5 * dT + (dtC52 * k2 + dtC54 * k4 + dtC51 * k1 + dtC53 * k3) - else - linsolve_tmp = du + dtd5 * dT + - mass_matrix * (dtC52 * k2 + dtC54 * k4 + dtC51 * k1 + dtC53 * k3) - end - - k5 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - u = uprev + a61 * k1 + a62 * k2 + a63 * k3 + a64 * k4 + a65 * k5 - du = f(u, p, t + dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - linsolve_tmp = du + (dtC61 * k1 + dtC62 * k2 + dtC63 * k3 + dtC64 * k4 + dtC65 * k5) - else - linsolve_tmp = du + - mass_matrix * - (dtC61 * k1 + dtC62 * k2 + dtC63 * k3 + dtC64 * k4 + dtC65 * k5) - end - - k6 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - u = u + k6 - du = f(u, p, t + dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - linsolve_tmp = du + - (dtC71 * k1 + dtC72 * k2 + dtC73 * k3 + dtC74 * k4 + dtC75 * k5 + - dtC76 * k6) - else - linsolve_tmp = du + - mass_matrix * - (dtC71 * k1 + dtC72 * k2 + dtC73 * k3 + dtC74 * k4 + dtC75 * k5 + - dtC76 * k6) - end - - k7 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - u = u + k7 - du = f(u, p, t + dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - linsolve_tmp = du + - (dtC81 * k1 + dtC82 * k2 + dtC83 * k3 + dtC84 * k4 + dtC85 * k5 + - dtC86 * k6 + dtC87 * k7) - else - linsolve_tmp = du + - mass_matrix * - (dtC81 * k1 + dtC82 * k2 + dtC83 * k3 + dtC84 * k4 + dtC85 * k5 + - dtC86 * k6 + dtC87 * k7) - end - - k8 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - u = u + k8 - linsolve_tmp = k8 - - if integrator.opts.adaptive - if (integrator.alg isa Rodas5Pe) - linsolve_tmp = 0.2606326497975715 * k1 - 0.005158627295444251 * k2 + - 1.3038988631109731 * k3 + 1.235000722062074 * k4 + - -0.7931985603795049 * k5 - 1.005448461135913 * k6 - - 0.18044626132120234 * k7 + 0.17051519239113755 * k8 - end - atmp = calculate_residuals(linsolve_tmp, uprev, u, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - integrator.EEst = integrator.opts.internalnorm(atmp, t) - end - - if integrator.opts.calck - @unpack h21, h22, h23, h24, h25, h26, h27, h28, h31, h32, h33, h34, h35, h36, h37, h38, h41, h42, h43, h44, h45, h46, h47, h48 = cache.tab - integrator.k[1] = h21 * k1 + h22 * k2 + h23 * k3 + h24 * k4 + h25 * k5 + h26 * k6 + - h27 * k7 + h28 * k8 - integrator.k[2] = h31 * k1 + h32 * k2 + h33 * k3 + h34 * k4 + h35 * k5 + h36 * k6 + - h37 * k7 + h38 * k8 - integrator.k[3] = h41 * k1 + h42 * k2 + h43 * k3 + h44 * k4 + h45 * k5 + h46 * k6 + - h47 * k7 + h48 * k8 - if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && - (integrator.EEst < 1.0) - k2 = 0.5 * (uprev + u + - 0.5 * (integrator.k[1] + 0.5 * (integrator.k[2] + 0.5 * integrator.k[3]))) - du1 = (0.25 * (integrator.k[2] + integrator.k[3]) - uprev + u) / dt - du = f(k2, p, t + dt / 2) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - if mass_matrix === I - du2 = du1 - du - else - du2 = mass_matrix * du1 - du - end - EEst = norm(du2) / norm(integrator.opts.abstol .+ integrator.opts.reltol .* k2) - integrator.EEst = max(EEst, integrator.EEst) - end - end - - integrator.u = u - return nothing -end - @RosenbrockW6S4OS(:init) @RosenbrockW6S4OS(:performstep) \ No newline at end of file From f3a2fb726fbe07b8f5983453d1567c319db2c9b0 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Wed, 4 Sep 2024 00:44:13 +0530 Subject: [PATCH 23/56] Fixes --- .../src/stiff_addsteps.jl | 131 ------------------ 1 file changed, 131 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl index ac61f6757c..dd0702236d 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl @@ -318,7 +318,6 @@ function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::RosenbrockCombinedConst W = 1 / dtgamma - J end - num_stages = size(A,1) du = f(u, p, t) linsolve_tmp = @.. du + dtd[1] * dT @@ -427,134 +426,4 @@ function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::RosenbrockCache, end end nothing -end - -function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::RosenbrockCombinedConstantCache, - always_calc_begin = false, allow_calc_end = true, - force_calc_end = false) - if length(k) < 3 || always_calc_begin - @unpack tf, uf = cache - @unpack a21, a31, a32, a41, a42, a43, a51, a52, a53, a54, a61, a62, a63, a64, a65, C21, C31, C32, C41, C42, C43, C51, C52, C53, C54, C61, C62, C63, C64, C65, C71, C72, C73, C74, C75, C76, C81, C82, C83, C84, C85, C86, C87, gamma, d1, d2, d3, d4, d5, c2, c3, c4, c5 = cache.tab - - # Precalculations - dtC21 = C21 / dt - dtC31 = C31 / dt - dtC32 = C32 / dt - dtC41 = C41 / dt - dtC42 = C42 / dt - dtC43 = C43 / dt - dtC51 = C51 / dt - dtC52 = C52 / dt - dtC53 = C53 / dt - dtC54 = C54 / dt - dtC61 = C61 / dt - dtC62 = C62 / dt - dtC63 = C63 / dt - dtC64 = C64 / dt - dtC65 = C65 / dt - dtC71 = C71 / dt - dtC72 = C72 / dt - dtC73 = C73 / dt - dtC74 = C74 / dt - dtC75 = C75 / dt - dtC76 = C76 / dt - dtC81 = C81 / dt - dtC82 = C82 / dt - dtC83 = C83 / dt - dtC84 = C84 / dt - dtC85 = C85 / dt - dtC86 = C86 / dt - dtC87 = C87 / dt - - dtd1 = dt * d1 - dtd2 = dt * d2 - dtd3 = dt * d3 - dtd4 = dt * d4 - dtd5 = dt * d5 - dtgamma = dt * gamma - mass_matrix = f.mass_matrix - - # Time derivative - tf.u = uprev - # if cache.autodiff isa AutoForwardDiff - # dT = ForwardDiff.derivative(tf, t) - # else - dT = FiniteDiff.finite_difference_derivative(tf, t, dir = sign(dt)) - # end - - # Jacobian - uf.t = t - if uprev isa AbstractArray - J = ForwardDiff.jacobian(uf, uprev) - W = mass_matrix / dtgamma - J - else - J = ForwardDiff.derivative(uf, uprev) - W = 1 / dtgamma - J - end - - du = f(uprev, p, t) - - linsolve_tmp = du + dtd1 * dT - - k1 = W \ linsolve_tmp - u = uprev + a21 * k1 - du = f(u, p, t + c2 * dt) - - linsolve_tmp = du + dtd2 * dT + dtC21 * k1 - - k2 = W \ linsolve_tmp - u = uprev + a31 * k1 + a32 * k2 - du = f(u, p, t + c3 * dt) - - linsolve_tmp = du + dtd3 * dT + (dtC31 * k1 + dtC32 * k2) - - k3 = W \ linsolve_tmp - u = uprev + a41 * k1 + a42 * k2 + a43 * k3 - du = f(u, p, t + c4 * dt) - - linsolve_tmp = du + dtd4 * dT + (dtC41 * k1 + dtC42 * k2 + dtC43 * k3) - - k4 = W \ linsolve_tmp - u = uprev + a51 * k1 + a52 * k2 + a53 * k3 + a54 * k4 - du = f(u, p, t + c5 * dt) - - linsolve_tmp = du + dtd5 * dT + (dtC52 * k2 + dtC54 * k4 + dtC51 * k1 + dtC53 * k3) - - k5 = W \ linsolve_tmp - u = uprev + a61 * k1 + a62 * k2 + a63 * k3 + a64 * k4 + a65 * k5 - du = f(u, p, t + dt) - - linsolve_tmp = du + (dtC61 * k1 + dtC62 * k2 + dtC63 * k3 + dtC64 * k4 + dtC65 * k5) - - k6 = W \ linsolve_tmp - u = u + k6 - du = f(u, p, t + dt) - - linsolve_tmp = du + - (dtC71 * k1 + dtC72 * k2 + dtC73 * k3 + dtC74 * k4 + dtC75 * k5 + - dtC76 * k6) - - k7 = W \ linsolve_tmp - - u = u + k7 - du = f(u, p, t + dt) - - linsolve_tmp = du + - (dtC81 * k1 + dtC82 * k2 + dtC83 * k3 + dtC84 * k4 + dtC85 * k5 + - dtC86 * k6 + dtC87 * k7) - - k8 = W \ linsolve_tmp - - @unpack h21, h22, h23, h24, h25, h26, h27, h28, h31, h32, h33, h34, h35, h36, h37, h38, h41, h42, h43, h44, h45, h46, h47, h48 = cache.tab - k₁ = h21 * k1 + h22 * k2 + h23 * k3 + h24 * k4 + h25 * k5 + h26 * k6 + h27 * k7 + - h28 * k8 - k₂ = h31 * k1 + h32 * k2 + h33 * k3 + h34 * k4 + h35 * k5 + h36 * k6 + h37 * k7 + - h38 * k8 - k₃ = h41 * k1 + h42 * k2 + h43 * k3 + h44 * k4 + h45 * k5 + h46 * k6 + h47 * k7 + - h48 * k8 - copyat_or_push!(k, 1, k₁) - copyat_or_push!(k, 2, k₂) - copyat_or_push!(k, 3, k₃) - end - nothing end \ No newline at end of file From eaf579141396097cf84fc22d8ec44a600edbd743 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Wed, 4 Sep 2024 01:34:13 +0530 Subject: [PATCH 24/56] Extra row added --- .../src/rosenbrock_tableaus.jl | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl index 8f52b25568..33da49d76d 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl @@ -238,12 +238,12 @@ function Rodas4Tableau(T, T2) #BET3P=0.0635D0 #BET4P=0.3438D0 A = T[ - 0 0 0 0 0 0 - 1.544 0 0 0 0 0 - 0.9466785280815826 0.2557011698983284 0 0 0 0 - 3.314825187068521 2.896124015972201 0.9986419139977817 0 0 0 - 1.221224509226641 6.019134481288629 12.53708332932087 -0.6878860361058950 0 0 - 1.221224509226641 6.019134481288629 12.53708332932087 -0.6878860361058950 1 0 + 0 0 0 0 0 0 + 1.544 0 0 0 0 0 + 0.9466785280815826 0.2557011698983284 0 0 0 0 + 3.314825187068521 2.896124015972201 0.9986419139977817 0 0 0 + 1.221224509226641 6.019134481288629 12.53708332932087 -0.6878860361058950 0 0 + 1.221224509226641 6.019134481288629 12.53708332932087 -0.6878860361058950 1 0 ] C = T[ 0 0 0 0 0 @@ -356,6 +356,7 @@ function Rodas5Tableau(T, T2) 2.576417536461461 1.622083060776640 -0.9089668560264532 0 0 0 2.760842080225597 1.446624659844071 -0.3036980084553738 0.2877498600325443 0 0 -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 0 + -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 0 ] C = T[ 0 0 0 0 0 0 0 0 From 58cf9a5561a923215703388d526a56a2118ca077 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Wed, 4 Sep 2024 01:34:57 +0530 Subject: [PATCH 25/56] Fixes --- .../src/rosenbrock_tableaus.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl index 33da49d76d..b5615b0afe 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl @@ -350,13 +350,13 @@ end function Rodas5Tableau(T, T2) gamma = convert(T2, 0.19) A = T[ - 0 0 0 0 0 0 - 2.0 0 0 0 0 0 - 3.040894194418781 1.041747909077569 0 0 0 0 - 2.576417536461461 1.622083060776640 -0.9089668560264532 0 0 0 - 2.760842080225597 1.446624659844071 -0.3036980084553738 0.2877498600325443 0 0 - -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 0 - -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 0 + 0 0 0 0 0 0 0 + 2.0 0 0 0 0 0 0 + 3.040894194418781 1.041747909077569 0 0 0 0 0 + 2.576417536461461 1.622083060776640 -0.9089668560264532 0 0 0 0 + 2.760842080225597 1.446624659844071 -0.3036980084553738 0.2877498600325443 0 0 0 + -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 0 0 + -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 1 0 ] C = T[ 0 0 0 0 0 0 0 0 From a7a536105100356f781b4c1b9e54b0f9ac773abd Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Wed, 4 Sep 2024 00:24:04 -0400 Subject: [PATCH 26/56] fixes --- .../src/rosenbrock_caches.jl | 20 ++--- .../src/rosenbrock_interpolants.jl | 73 ++++++++----------- .../src/rosenbrock_perform_step.jl | 18 ++--- .../src/rosenbrock_tableaus.jl | 7 +- 4 files changed, 52 insertions(+), 66 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl index 97747db6b5..8b77c9170e 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl @@ -40,6 +40,7 @@ mutable struct RosenbrockCache{uType, rateType, uNoUnitsType, JType, WType, TabT alg::A step_limiter!::StepLimiter stage_limiter!::StageLimiter + order::Int end function full_cache(c::RosenbrockCache) return [c.u, c.uprev, c.dense..., c.du, c.du1, c.du2, @@ -54,6 +55,7 @@ struct RosenbrockCombinedConstantCache{TF, UF, Tab, JType, WType, F, AD} <: Rose W::WType linsolve::F autodiff::AD + order::Int end @cache mutable struct Rosenbrock23Cache{uType, rateType, uNoUnitsType, JType, WType, @@ -728,7 +730,7 @@ function alg_cache(alg::Union{Rodas4, Rodas42, Rodas4P, Rodas4P2}, u, rate_proto RosenbrockCombinedConstantCache(tf, uf, tabtype(alg)(constvalue(uBottomEltypeNoUnits), constvalue(tTypeNoUnits)), J, W, linsolve, - alg_autodiff(alg)) + alg_autodiff(alg), 4) end function alg_cache(alg::Union{Rodas4, Rodas42, Rodas4P, Rodas4P2}, u, rate_prototype, ::Type{uEltypeNoUnits}, @@ -779,7 +781,7 @@ function alg_cache(alg::Union{Rodas4, Rodas42, Rodas4P, Rodas4P2}, u, rate_proto u, uprev, dense, du, du1, du2, ks, fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp, linsolve, jac_config, grad_config, reltol, alg, - alg.step_limiter!, alg.stage_limiter!) + alg.step_limiter!, alg.stage_limiter!, 4) end ################################################################################ @@ -794,7 +796,7 @@ function alg_cache(alg::Rodas5, u, rate_prototype, ::Type{uEltypeNoUnits}, du = zero(rate_prototype) du1 = zero(rate_prototype) du2 = zero(rate_prototype) - ks = [zero(rate_prototype) for _ in 1:8] + ks = [zero(rate_prototype) for _ in 1:7] fsalfirst = zero(rate_prototype) fsallast = zero(rate_prototype) dT = zero(rate_prototype) @@ -822,7 +824,7 @@ function alg_cache(alg::Rodas5, u, rate_prototype, ::Type{uEltypeNoUnits}, fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp, linsolve, jac_config, grad_config, reltol, alg, alg.step_limiter!, - alg.stage_limiter!) + alg.stage_limiter!, 5) end function alg_cache(alg::Rodas5, u, rate_prototype, ::Type{uEltypeNoUnits}, @@ -836,7 +838,7 @@ function alg_cache(alg::Rodas5, u, rate_prototype, ::Type{uEltypeNoUnits}, linsolve = nothing #init(linprob,alg.linsolve,alias_A=true,alias_b=true) RosenbrockCombinedConstantCache(tf, uf, Rodas5Tableau(constvalue(uBottomEltypeNoUnits), - constvalue(tTypeNoUnits)), J, W, linsolve, alg_autodiff(alg)) + constvalue(tTypeNoUnits)), J, W, linsolve, alg_autodiff(alg), 5) end function alg_cache( @@ -848,7 +850,7 @@ function alg_cache( du = zero(rate_prototype) du1 = zero(rate_prototype) du2 = zero(rate_prototype) - ks = [zero(rate_prototype) for _ in 1:8] + ks = [zero(rate_prototype) for _ in 1:7] fsalfirst = zero(rate_prototype) fsallast = zero(rate_prototype) dT = zero(rate_prototype) @@ -876,7 +878,7 @@ function alg_cache( fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp, linsolve, jac_config, grad_config, reltol, alg, alg.step_limiter!, - alg.stage_limiter!) + alg.stage_limiter!, 5) end function alg_cache( @@ -889,9 +891,9 @@ function alg_cache( J, W = build_J_W(alg, u, uprev, p, t, dt, f, uEltypeNoUnits, Val(false)) linprob = nothing #LinearProblem(W,copy(u); u0=copy(u)) linsolve = nothing #init(linprob,alg.linsolve,alias_A=true,alias_b=true) - RosenbrockConstantCache(tf, uf, + RosenbrockCombinedConstantCache(tf, uf, Rodas5PTableau(constvalue(uBottomEltypeNoUnits), - constvalue(tTypeNoUnits)), J, W, linsolve) + constvalue(tTypeNoUnits)), J, W, linsolve, alg_autodiff(alg), 5) end function get_fsalfirstlast( diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl index 7df6d5dc87..4b787b8a6a 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl @@ -50,7 +50,7 @@ end cache::Union{Rosenbrock23Cache, Rosenbrock32Cache}, idxs::Nothing, T::Type{Val{0}}, differential_vars) @rosenbrock2332pre0 - @inbounds @.. broadcast=false y₀+dt * (c1 * k[1] + c2 * k[2]) + @inbounds @.. y₀+dt * (c1 * k[1] + c2 * k[2]) end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, @@ -58,7 +58,7 @@ end Rosenbrock32ConstantCache, Rosenbrock32Cache }, idxs, T::Type{Val{0}}, differential_vars) @rosenbrock2332pre0 - @.. broadcast=false y₀[idxs]+dt * (c1 * k[1][idxs] + c2 * k[2][idxs]) + @.. y₀[idxs]+dt * (c1 * k[1][idxs] + c2 * k[2][idxs]) end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, @@ -67,7 +67,7 @@ end Rosenbrock32ConstantCache, Rosenbrock32Cache }, idxs::Nothing, T::Type{Val{0}}, differential_vars) @rosenbrock2332pre0 - @inbounds @.. broadcast=false out=y₀ + dt * (c1 * k[1] + c2 * k[2]) + @inbounds @.. out=y₀ + dt * (c1 * k[1] + c2 * k[2]) out end @@ -77,7 +77,7 @@ end Rosenbrock32ConstantCache, Rosenbrock32Cache }, idxs, T::Type{Val{0}}, differential_vars) @rosenbrock2332pre0 - @views @.. broadcast=false out=y₀[idxs] + dt * (c1 * k[1][idxs] + c2 * k[2][idxs]) + @views @.. out=y₀[idxs] + dt * (c1 * k[1][idxs] + c2 * k[2][idxs]) out end @@ -93,7 +93,7 @@ end Rosenbrock32ConstantCache, Rosenbrock32Cache }, idxs::Nothing, T::Type{Val{1}}, differential_vars) @rosenbrock2332pre1 - @.. broadcast=false c1diff * k[1]+c2diff * k[2] + @.. c1diff * k[1]+c2diff * k[2] end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, @@ -101,7 +101,7 @@ end Rosenbrock32ConstantCache, Rosenbrock32Cache }, idxs, T::Type{Val{1}}, differential_vars) @rosenbrock2332pre1 - @.. broadcast=false c1diff * k[1][idxs]+c2diff * k[2][idxs] + @.. c1diff * k[1][idxs]+c2diff * k[2][idxs] end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, @@ -110,7 +110,7 @@ end Rosenbrock32ConstantCache, Rosenbrock32Cache }, idxs::Nothing, T::Type{Val{1}}, differential_vars) @rosenbrock2332pre1 - @.. broadcast=false out=c1diff * k[1] + c2diff * k[2] + @.. out=c1diff * k[1] + c2diff * k[2] out end @@ -120,7 +120,7 @@ end Rosenbrock32ConstantCache, Rosenbrock32Cache }, idxs, T::Type{Val{1}}, differential_vars) @rosenbrock2332pre1 - @views @.. broadcast=false out=c1diff * k[1][idxs] + c2diff * k[2][idxs] + @views @.. out=c1diff * k[1][idxs] + c2diff * k[2][idxs] out end @@ -138,7 +138,7 @@ end Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCache, Rodas23WCache, Rodas3PCache}, idxs::Nothing, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ - @inbounds @.. broadcast=false Θ1 * y₀+Θ * (y₁ + Θ1 * (k[1] + Θ * k[2])) + @inbounds @.. Θ1 * y₀+Θ * (y₁ + Θ1 * (k[1] + Θ * k[2])) end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, @@ -146,7 +146,7 @@ end Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ - @.. broadcast=false Θ1 * y₀[idxs]+Θ * (y₁[idxs] + Θ1 * (k[1][idxs] + Θ * k[2][idxs])) + @.. Θ1 * y₀[idxs]+Θ * (y₁[idxs] + Θ1 * (k[1][idxs] + Θ * k[2][idxs])) end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, @@ -154,7 +154,7 @@ end Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs::Nothing, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ - @.. broadcast=false out=Θ1 * y₀ + Θ * (y₁ + Θ1 * (k[1] + Θ * k[2])) + @.. out=Θ1 * y₀ + Θ * (y₁ + Θ1 * (k[1] + Θ * k[2])) out end @@ -163,8 +163,7 @@ end Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ - @views @.. broadcast=false out=Θ1 * y₀[idxs] + - Θ * (y₁[idxs] + Θ1 * (k[1][idxs] + Θ * k[2][idxs])) + @views @.. out=Θ1 * y₀[idxs] + Θ * (y₁[idxs] + Θ1 * (k[1][idxs] + Θ * k[2][idxs])) out end @@ -178,25 +177,22 @@ end @muladd function _ode_interpolant( Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCache, Rodas23WCache, Rodas3PCache}, idxs::Nothing, T::Type{Val{1}}, differential_vars) - @inbounds @.. broadcast=false (k[1] + Θ * (-2 * k[1] + 2 * k[2] - 3 * k[2] * Θ) - y₀ + - y₁)/dt + @inbounds @.. (k[1] + Θ * (-2 * k[1] + 2 * k[2] - 3 * k[2] * Θ) - y₀ + y₁)/dt end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs, T::Type{Val{1}}, differential_vars) - @.. broadcast=false (k[1][idxs] + - Θ * (-2 * k[1][idxs] + 2 * k[2][idxs] - 3 * k[2][idxs] * Θ) - - y₀[idxs] + y₁[idxs])/dt + @.. (k[1][idxs] + Θ * (-2 * k[1][idxs] + 2 * k[2][idxs] - 3 * k[2][idxs] * Θ) - + y₀[idxs] + y₁[idxs])/dt end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs::Nothing, T::Type{Val{1}}, differential_vars) - @.. broadcast=false out=(k[1] + Θ * (-2 * k[1] + 2 * k[2] - 3 * k[2] * Θ) - y₀ + y₁) / - dt + @.. out=(k[1] + Θ * (-2 * k[1] + 2 * k[2] - 3 * k[2] * Θ) - y₀ + y₁) / dt out end @@ -204,11 +200,11 @@ end cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs, T::Type{Val{1}}, differential_vars) - @views @.. broadcast=false out=(k[1][idxs] + - Θ * - (-2 * k[1][idxs] + 2 * k[2][idxs] - - 3 * k[2][idxs] * Θ) - - y₀[idxs] + y₁[idxs]) / dt + @views @.. out=(k[1][idxs] + + Θ * + (-2 * k[1][idxs] + 2 * k[2][idxs] - + 3 * k[2][idxs] * Θ) - + y₀[idxs] + y₁[idxs]) / dt out end @@ -223,23 +219,22 @@ end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::RosenbrockCache, idxs::Nothing, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ - @inbounds @.. broadcast=false Θ1 * y₀+Θ * (y₁ + Θ1 * (k[1] + Θ * (k[2] + Θ * k[3]))) + @inbounds @.. Θ1 * y₀+Θ * (y₁ + Θ1 * (k[1] + Θ * (k[2] + Θ * k[3]))) end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ - @.. broadcast=false Θ1 * - y₀[idxs]+Θ * (y₁[idxs] + - Θ1 * (k[1][idxs] + Θ * (k[2][idxs] + Θ * k[3][idxs]))) + @.. Θ1 * y₀[idxs]+Θ * (y₁[idxs] + + Θ1 * (k[1][idxs] + Θ * (k[2][idxs] + Θ * k[3][idxs]))) end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs::Nothing, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ - @.. broadcast=false out=Θ1 * y₀ + Θ * (y₁ + Θ1 * (k[1] + Θ * (k[2] + Θ * k[3]))) + @.. out=Θ1 * y₀ + Θ * (y₁ + Θ1 * (k[1] + Θ * (k[2] + Θ * k[3]))) out end @@ -247,25 +242,17 @@ end cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ - @views @.. broadcast=false out=Θ1 * y₀[idxs] + - Θ * (y₁[idxs] + - Θ1 * (k[1][idxs] + Θ * (k[2][idxs] + Θ * k[3][idxs]))) + @views @.. out= Θ1 * y₀[idxs] + Θ * (y₁[idxs] + + Θ1 * (k[1][idxs] + Θ * (k[2][idxs] + Θ * k[3][idxs]))) out end # First Derivative -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::RosenbrockCombinedConstantCache, - idxs::Nothing, T::Type{Val{1}}, differential_vars) - @inbounds (k[1] + - Θ * (-2 * k[1] + 2 * k[2] + Θ * (-3 * k[2] + 3 * k[3] - 4 * Θ * k[3])) - y₀ + - y₁) / dt -end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::RosenbrockCombinedConstantCache, idxs::Nothing, T::Type{Val{1}}, differential_vars) - @inbounds @.. broadcast=false (k[1] + - Θ * (-2 * k[1] + 2 * k[2] + - Θ * (-3 * k[2] + 3 * k[3] - 4 * Θ * k[3])) - y₀ + y₁)/dt + @inbounds @.. (k[1] + Θ * (-2 * k[1] + 2 * k[2] + + Θ * (-3 * k[2] + 3 * k[3] - 4 * Θ * k[3])) - y₀ + y₁)/dt end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, @@ -364,4 +351,4 @@ end @views @.. broadcast=false out=(-6 * k[2][idxs] + 6 * k[3][idxs] - 24 * Θ * k[3][idxs]) / dt^3 out -end \ No newline at end of file +end diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index 6e33275ee9..cc59d7bc12 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -1199,11 +1199,12 @@ end #### Rodas4 type method function initialize!(integrator, cache::RosenbrockCombinedConstantCache) - integrator.kshortsize = 2 + integrator.kshortsize = cache.order == 5 ? 3 : 2 integrator.k = typeof(integrator.k)(undef, integrator.kshortsize) # Avoid undefined entries if k is an array of arrays - integrator.k[1] = zero(integrator.u) - integrator.k[2] = zero(integrator.u) + for i in 1:integrator.kshortsize + integrator.k[i] = zero(integrator.u) + end end @muladd function perform_step!(integrator, cache::RosenbrockCombinedConstantCache, repeat_step = false) @@ -1302,12 +1303,11 @@ end end function initialize!(integrator, cache::RosenbrockCache) - dense = cache.dense - dense1, dense2 = dense[1], dense[2] - integrator.kshortsize = 2 + integrator.kshortsize = cache.order == 5 ? 3 : 2 resize!(integrator.k, integrator.kshortsize) - integrator.k[1] = dense1 - integrator.k[2] = dense2 + for i in 1:integrator.kshortsize + integrator.k[i] = cache.dense[i] + end end @@ -1424,4 +1424,4 @@ end end @RosenbrockW6S4OS(:init) -@RosenbrockW6S4OS(:performstep) \ No newline at end of file +@RosenbrockW6S4OS(:performstep) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl index b5615b0afe..beccc563d6 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl @@ -372,7 +372,6 @@ function Rodas5Tableau(T, T2) d = T[gamma, -0.1823079225333714636, -0.319231832186874912, 0.3449828624725343, -0.377417564392089818, 0, 0, 0] H = T[ - 0 0 0 0 0 0 0 0 27.354592673333357 -6.925207756232857 26.40037733258859 0.5635230501052979 -4.699151156849391 -1.6008677469422725 -1.5306074446748028 -1.3929872940716344 44.19024239501722 1.3677947663381929e-13 202.93261852171622 -35.5669339789154 -181.91095152160645 3.4116351403665033 2.5793540257308067 2.2435122582734066 -44.0988150021747 -5.755396159656812e-13 -181.26175034586677 56.99302194811676 183.21182741427398 -7.480257918273637 -5.792426076169686 -5.32503859794143 @@ -414,8 +413,7 @@ function Rodas5PTableau(T, T2) -6.954028509809101 2.489845061869568 -10.358996098473584 0 0 0 0 2.8029986275628964 0.5072464736228206 -0.3988312541770524 -0.04721187230404641 0 0 0 -7.502846399306121 2.561846144803919 -11.627539656261098 -0.18268767659942256 0.030198172008377946 0 0 - -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 0 0 - -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 1 0 + -7.502846399306121 2.561846144803919 -11.627539656261098 -0.18268767659942256 0.030198172008377946 1 0 ] C = T[ 0 0 0 0 0 0 0 0 @@ -430,7 +428,6 @@ function Rodas5PTableau(T, T2) c = T2[0, 0.6358126895828704, 0.4095798393397535, 0.9769306725060716, 0.4288403609558664, 1, 1, 1] d = T[0.21193756319429014, -0.42387512638858027, -0.3384627126235924, 1.8046452872882734, 2.325825639765069, 0, 0, 0] H = T[ - 0 0 0 0 0 0 0 0 25.948786856663858 -2.5579724845846235 10.433815404888879 -2.3679251022685204 0.524948541321073 1.1241088310450404 0.4272876194431874 -0.17202221070155493 -9.91568850695171 -0.9689944594115154 3.0438037242978453 -24.495224566215796 20.176138334709044 15.98066361424651 -6.789040303419874 -6.710236069923372 11.419903575922262 2.8879645146136994 72.92137995996029 80.12511834622643 -52.072871366152654 -59.78993625266729 -0.15582684282751913 4.883087185713722 @@ -485,4 +482,4 @@ beta3 = 6.8619167645278386e-2 beta4 = 0.8289547562599182 beta5 = 7.9630136489868164e-2 alpha64 = -0.2076823627400282 -=# \ No newline at end of file +=# From c9d94e34be78a7175fb43c07bd6f0f00443e8b86 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Wed, 4 Sep 2024 00:40:36 -0400 Subject: [PATCH 27/56] fix tableaus --- .../src/rosenbrock_caches.jl | 2 +- .../src/rosenbrock_tableaus.jl | 62 ++++++++++--------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl index 8b77c9170e..14b24b0ccb 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl @@ -850,7 +850,7 @@ function alg_cache( du = zero(rate_prototype) du1 = zero(rate_prototype) du2 = zero(rate_prototype) - ks = [zero(rate_prototype) for _ in 1:7] + ks = [zero(rate_prototype) for _ in 1:8] fsalfirst = zero(rate_prototype) fsallast = zero(rate_prototype) dT = zero(rate_prototype) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl index beccc563d6..d6fe9bc752 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl @@ -350,23 +350,24 @@ end function Rodas5Tableau(T, T2) gamma = convert(T2, 0.19) A = T[ - 0 0 0 0 0 0 0 - 2.0 0 0 0 0 0 0 - 3.040894194418781 1.041747909077569 0 0 0 0 0 - 2.576417536461461 1.622083060776640 -0.9089668560264532 0 0 0 0 - 2.760842080225597 1.446624659844071 -0.3036980084553738 0.2877498600325443 0 0 0 - -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 0 0 - -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 1 0 + 0 0 0 0 0 0 0 0 + 2.0 0 0 0 0 0 0 0 + 3.040894194418781 1.041747909077569 0 0 0 0 0 0 + 2.576417536461461 1.622083060776640 -0.9089668560264532 0 0 0 0 0 + 2.760842080225597 1.446624659844071 -0.3036980084553738 0.2877498600325443 0 0 0 0 + -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 0 0 0 + -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 1 0 0 + -14.09640773051259 6.925207756232704 -41.47510893210728 2.343771018586405 24.13215229196062 1 1 0 ] C = T[ - 0 0 0 0 0 0 0 0 - -10.31323885133993 0 0 0 0 0 0 0 - -21.04823117650003 -7.234992135176716 0 0 0 0 0 0 - 32.22751541853323 -4.943732386540191 19.44922031041879 0 0 0 0 0 - -20.69865579590063 -8.816374604402768 1.260436877740897 -0.7495647613787146 0 0 0 0 - -46.22004352711257 -17.49534862857472 -289.6389582892057 93.60855400400906 318.3822534212147 0 0 0 - 34.20013733472935 -14.15535402717690 57.82335640988400 25.83362985412365 1.408950972071624 -6.551835421242162 0 0 - 42.57076742291101 -13.80770672017997 93.98938432427124 18.77919633714503 -31.58359187223370 -6.685968952921985 -5.810979938412932 0 + 0 0 0 0 0 0 0 + -10.31323885133993 0 0 0 0 0 0 + -21.04823117650003 -7.234992135176716 0 0 0 0 0 + 32.22751541853323 -4.943732386540191 19.44922031041879 0 0 0 0 + -20.69865579590063 -8.816374604402768 1.260436877740897 -0.7495647613787146 0 0 0 + -46.22004352711257 -17.49534862857472 -289.6389582892057 93.60855400400906 318.3822534212147 0 0 + 34.20013733472935 -14.15535402717690 57.82335640988400 25.83362985412365 1.408950972071624 -6.551835421242162 0 + 42.57076742291101 -13.80770672017997 93.98938432427124 18.77919633714503 -31.58359187223370 -6.685968952921985 -5.810979938412932 ] c = T2[0, 0.38, 0.3878509998321533, 0.4839718937873840, 0.4570477008819580, 1, 1, 1] d = T[gamma, -0.1823079225333714636, -0.319231832186874912, 0.3449828624725343, -0.377417564392089818, 0, 0, 0] @@ -407,23 +408,24 @@ end function Rodas5PTableau(T, T2) gamma = convert(T2, 0.21193756319429014) A = T[ - 0 0 0 0 0 0 0 - 3.0 0 0 0 0 0 0 - 2.849394379747939 0.45842242204463923 0 0 0 0 0 - -6.954028509809101 2.489845061869568 -10.358996098473584 0 0 0 0 - 2.8029986275628964 0.5072464736228206 -0.3988312541770524 -0.04721187230404641 0 0 0 - -7.502846399306121 2.561846144803919 -11.627539656261098 -0.18268767659942256 0.030198172008377946 0 0 - -7.502846399306121 2.561846144803919 -11.627539656261098 -0.18268767659942256 0.030198172008377946 1 0 + 0 0 0 0 0 0 0 0 + 3.0 0 0 0 0 0 0 0 + 2.849394379747939 0.45842242204463923 0 0 0 0 0 0 + -6.954028509809101 2.489845061869568 -10.358996098473584 0 0 0 0 0 + 2.8029986275628964 0.5072464736228206 -0.3988312541770524 -0.04721187230404641 0 0 0 0 + -7.502846399306121 2.561846144803919 -11.627539656261098 -0.18268767659942256 0.030198172008377946 0 0 0 + -7.502846399306121 2.561846144803919 -11.627539656261098 -0.18268767659942256 0.030198172008377946 1 0 0 + -7.502846399306121 2.561846144803919 -11.627539656261098 -0.18268767659942256 0.030198172008377946 1 1 0 ] C = T[ - 0 0 0 0 0 0 0 0 - -14.155112264123755 0 0 0 0 0 0 0 - -17.97296035885952 -2.859693295451294 0 0 0 0 0 0 - 147.12150275711716 -1.41221402718213 71.68940251302358 0 0 0 0 0 - 165.43517024871676 -0.4592823456491126 42.90938336958603 -5.961986721573306 0 0 0 0 - 24.854864614690072 -3.0009227002832186 47.4931110020768 5.5814197821558125 -0.6610691825249471 0 0 0 - 30.91273214028599 -3.1208243349937974 77.79954646070892 34.28646028294783 -19.097331116725623 -28.087943162872662 0 0 - 37.80277123390563 -3.2571969029072276 112.26918849496327 66.9347231244047 -40.06618937091002 -54.66780262877968 -9.48861652309627 0 + 0 0 0 0 0 0 0 + -14.155112264123755 0 0 0 0 0 0 + -17.97296035885952 -2.859693295451294 0 0 0 0 0 + 147.12150275711716 -1.41221402718213 71.68940251302358 0 0 0 0 + 165.43517024871676 -0.4592823456491126 42.90938336958603 -5.961986721573306 0 0 0 + 24.854864614690072 -3.0009227002832186 47.4931110020768 5.5814197821558125 -0.6610691825249471 0 0 + 30.91273214028599 -3.1208243349937974 77.79954646070892 34.28646028294783 -19.097331116725623 -28.087943162872662 0 + 37.80277123390563 -3.2571969029072276 112.26918849496327 66.9347231244047 -40.06618937091002 -54.66780262877968 -9.48861652309627 ] c = T2[0, 0.6358126895828704, 0.4095798393397535, 0.9769306725060716, 0.4288403609558664, 1, 1, 1] d = T[0.21193756319429014, -0.42387512638858027, -0.3384627126235924, 1.8046452872882734, 2.325825639765069, 0, 0, 0] From f72dbbfea9776959b2ee61f11edf25ae71b2352a Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Thu, 5 Sep 2024 08:05:23 +0530 Subject: [PATCH 28/56] Changes --- lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl index 4b787b8a6a..4b5cf8fb28 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl @@ -1,5 +1,4 @@ ### Fallbacks to capture - ROSENBROCKS_WITH_INTERPOLATIONS = Union{Rosenbrock23ConstantCache, Rosenbrock23Cache, Rosenbrock32ConstantCache, Rosenbrock32Cache, Rodas23WConstantCache, Rodas3PConstantCache, @@ -351,4 +350,4 @@ end @views @.. broadcast=false out=(-6 * k[2][idxs] + 6 * k[3][idxs] - 24 * Θ * k[3][idxs]) / dt^3 out -end +end \ No newline at end of file From 144c7033d6f8357719f3c9695e40beb17508e020 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Thu, 5 Sep 2024 08:22:24 +0530 Subject: [PATCH 29/56] Changes --- .../src/rosenbrock_interpolants.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl index 4b5cf8fb28..4fb9fb6881 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl @@ -37,6 +37,17 @@ end c2 = Θ * (Θ - 2d) / (1 - 2d) end +@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, idxs, T::Type{Val{0}}, differential_vars, alg) + order = alg_order(alg) + + Θ1 = 1 - Θ + + if order == 2 + @rosenbrock2332pre0 + @inbounds y₀ + dt * (c1 * k[1] + c2 * k[2]) + end +end + @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Union{Rosenbrock23ConstantCache, Rosenbrock32ConstantCache}, idxs::Nothing, From 34fe9bdb7041ea418a6a90071252326983158881 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Thu, 5 Sep 2024 14:26:51 +0530 Subject: [PATCH 30/56] cache to alg_order --- .../src/rosenbrock_interpolants.jl | 40 +++++++------------ 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl index 4fb9fb6881..0e1b7102b6 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl @@ -42,33 +42,26 @@ end Θ1 = 1 - Θ - if order == 2 + if order == 2 && typeof(idxs) == Nothing @rosenbrock2332pre0 @inbounds y₀ + dt * (c1 * k[1] + c2 * k[2]) end -end -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock23ConstantCache, - Rosenbrock32ConstantCache}, idxs::Nothing, - T::Type{Val{0}}, differential_vars) - @rosenbrock2332pre0 - @inbounds y₀ + dt * (c1 * k[1] + c2 * k[2]) -end + if order == 2 && typeof(idxs) != Nothing + @rosenbrock2332pre0 + @views @.. out=y₀[idxs] + dt * (c1 * k[1][idxs] + c2 * k[2][idxs]) + out + end -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock23Cache, Rosenbrock32Cache}, - idxs::Nothing, T::Type{Val{0}}, differential_vars) - @rosenbrock2332pre0 - @inbounds @.. y₀+dt * (c1 * k[1] + c2 * k[2]) -end + if order == 3 && typeof(idxs) == Nothing + @rosenbrock2332pre0 + @inbounds y₀ + dt * (c1 * k[1] + c2 * k[2]) + end -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock23ConstantCache, Rosenbrock23Cache, - Rosenbrock32ConstantCache, Rosenbrock32Cache - }, idxs, T::Type{Val{0}}, differential_vars) - @rosenbrock2332pre0 - @.. y₀[idxs]+dt * (c1 * k[1][idxs] + c2 * k[2][idxs]) + if order == 3 & typeof(idxs) != Nothing + @rosenbrock2332pre0 + @.. y₀[idxs]+dt * (c1 * k[1][idxs] + c2 * k[2][idxs]) + end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, @@ -82,10 +75,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock23ConstantCache, - Rosenbrock23Cache, - Rosenbrock32ConstantCache, Rosenbrock32Cache - }, idxs, T::Type{Val{0}}, differential_vars) + cache::Union{Rosenbrock32ConstantCache, Rosenbrock32Cache}, idxs, T::Type{Val{0}}, differential_vars) @rosenbrock2332pre0 @views @.. out=y₀[idxs] + dt * (c1 * k[1][idxs] + c2 * k[2][idxs]) out From 625d8889c9d1fb6a0faae40b2bbe477eb3f0bb86 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Fri, 6 Sep 2024 01:57:29 +0530 Subject: [PATCH 31/56] Also sees order now --- .../src/rosenbrock_interpolants.jl | 71 +++++++++---------- 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl index 0e1b7102b6..156b1622ed 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl @@ -37,31 +37,27 @@ end c2 = Θ * (Θ - 2d) / (1 - 2d) end -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, idxs, T::Type{Val{0}}, differential_vars, alg) - order = alg_order(alg) - - Θ1 = 1 - Θ - - if order == 2 && typeof(idxs) == Nothing - @rosenbrock2332pre0 - @inbounds y₀ + dt * (c1 * k[1] + c2 * k[2]) - end - - if order == 2 && typeof(idxs) != Nothing - @rosenbrock2332pre0 - @views @.. out=y₀[idxs] + dt * (c1 * k[1][idxs] + c2 * k[2][idxs]) - out - end +@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, + cache::Union{Rosenbrock23ConstantCache, + Rosenbrock32ConstantCache}, idxs::Nothing, + T::Type{Val{0}}, differential_vars) + @rosenbrock2332pre0 + @inbounds y₀ + dt * (c1 * k[1] + c2 * k[2]) +end - if order == 3 && typeof(idxs) == Nothing - @rosenbrock2332pre0 - @inbounds y₀ + dt * (c1 * k[1] + c2 * k[2]) - end +@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, + cache::Union{Rosenbrock23Cache, Rosenbrock32Cache}, + idxs::Nothing, T::Type{Val{0}}, differential_vars) + @rosenbrock2332pre0 + @inbounds @.. y₀+dt * (c1 * k[1] + c2 * k[2]) +end - if order == 3 & typeof(idxs) != Nothing - @rosenbrock2332pre0 - @.. y₀[idxs]+dt * (c1 * k[1][idxs] + c2 * k[2][idxs]) - end +@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, + cache::Union{Rosenbrock23ConstantCache, Rosenbrock23Cache, + Rosenbrock32ConstantCache, Rosenbrock32Cache + }, idxs, T::Type{Val{0}}, differential_vars) + @rosenbrock2332pre0 + @.. y₀[idxs]+dt * (c1 * k[1][idxs] + c2 * k[2][idxs]) end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, @@ -75,7 +71,10 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock32ConstantCache, Rosenbrock32Cache}, idxs, T::Type{Val{0}}, differential_vars) + cache::Union{Rosenbrock23ConstantCache, + Rosenbrock23Cache, + Rosenbrock32ConstantCache, Rosenbrock32Cache + }, idxs, T::Type{Val{0}}, differential_vars) @rosenbrock2332pre0 @views @.. out=y₀[idxs] + dt * (c1 * k[1][idxs] + c2 * k[2][idxs]) out @@ -136,9 +135,16 @@ end @muladd function _ode_interpolant( Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCache, Rodas23WCache, Rodas3PCache}, - idxs::Nothing, T::Type{Val{0}}, differential_vars) + idxs::Nothing, T::Type{Val{0}}, differential_vars, alg) + order = alg_order(alg) Θ1 = 1 - Θ - @inbounds @.. Θ1 * y₀+Θ * (y₁ + Θ1 * (k[1] + Θ * k[2])) + + if order == 4 || order == 3 + @inbounds @.. Θ1 * y₀+Θ * (y₁ + Θ1 * (k[1] + Θ * k[2])) + elseif order == 5 + Θ1 = 1 - Θ + @inbounds @.. Θ1 * y₀+Θ * (y₁ + Θ1 * (k[1] + Θ * (k[2] + Θ * k[3]))) + end end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, @@ -209,19 +215,6 @@ end end #- - -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::RosenbrockCombinedConstantCache, - idxs::Nothing, T::Type{Val{0}}, differential_vars) - Θ1 = 1 - Θ - @inbounds Θ1 * y₀ + Θ * (y₁ + Θ1 * (k[1] + Θ * (k[2] + Θ * k[3]))) -end - -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::RosenbrockCache, idxs::Nothing, - T::Type{Val{0}}, differential_vars) - Θ1 = 1 - Θ - @inbounds @.. Θ1 * y₀+Θ * (y₁ + Θ1 * (k[1] + Θ * (k[2] + Θ * k[3]))) -end - @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs, T::Type{Val{0}}, differential_vars) From 737180f70208e27fb747fe7015bef3c3b0df8758 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Thu, 5 Sep 2024 16:38:32 -0400 Subject: [PATCH 32/56] interpolation fixes --- .../src/rosenbrock_interpolants.jl | 156 +++++++----------- .../src/rosenbrock_perform_step.jl | 2 +- .../src/rosenbrock_tableaus.jl | 15 +- .../test/ode_rosenbrock_tests.jl | 3 +- 4 files changed, 62 insertions(+), 114 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl index 156b1622ed..d72eca9f66 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl @@ -126,24 +126,15 @@ end """ From MATLAB ODE Suite by Shampine """ -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{RosenbrockCombinedConstantCache, Rodas23WConstantCache, Rodas3PConstantCache}, - idxs::Nothing, T::Type{Val{0}}, differential_vars) - Θ1 = 1 - Θ - @inbounds Θ1 * y₀ + Θ * (y₁ + Θ1 * (k[1] + Θ * k[2])) -end @muladd function _ode_interpolant( - Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCache, Rodas23WCache, Rodas3PCache}, - idxs::Nothing, T::Type{Val{0}}, differential_vars, alg) - order = alg_order(alg) + Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, Rodas23WConstantCache, Rodas3PConstantCache, RosenbrockCache, Rodas23WCache, Rodas3PCache}, + idxs::Nothing, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ - - if order == 4 || order == 3 - @inbounds @.. Θ1 * y₀+Θ * (y₁ + Θ1 * (k[1] + Θ * k[2])) - elseif order == 5 - Θ1 = 1 - Θ - @inbounds @.. Θ1 * y₀+Θ * (y₁ + Θ1 * (k[1] + Θ * (k[2] + Θ * k[3]))) + if !isdefined(cache, :order) || cache.order == 4 + @.. Θ1 * y₀+Θ * (y₁ + Θ1 * (k[1] + Θ * k[2])) + else + @.. Θ1 * y₀+Θ * (y₁ + Θ1 * (k[1] + Θ * (k[2] + Θ * k[3]))) end end @@ -152,7 +143,11 @@ end Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ - @.. Θ1 * y₀[idxs]+Θ * (y₁[idxs] + Θ1 * (k[1][idxs] + Θ * k[2][idxs])) + if !isdefined(cache, :order) || cache.order == 4 + @views @.. Θ1 * y₀[idxs]+Θ * (y₁[idxs] + Θ1 * (k[1][idxs] + Θ * k[2][idxs])) + else + @views @.. Θ1 * y₀[idxs]+Θ * (y₁[idxs] + Θ1 * (k[1][idxs] + Θ * (k[2][idxs] + Θ * k[3][idxs]))) + end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, @@ -160,7 +155,11 @@ end Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs::Nothing, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ - @.. out=Θ1 * y₀ + Θ * (y₁ + Θ1 * (k[1] + Θ * k[2])) + if !isdefined(cache, :order) || cache.order == 4 + @.. out=Θ1 * y₀ + Θ * (y₁ + Θ1 * (k[1] + Θ * k[2])) + else + @.. out=Θ1 * y₀ + Θ * (y₁ + Θ1 * (k[1] + Θ * (k[2] + Θ * k[3]))) + end out end @@ -169,36 +168,50 @@ end Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ - @views @.. out=Θ1 * y₀[idxs] + Θ * (y₁[idxs] + Θ1 * (k[1][idxs] + Θ * k[2][idxs])) + if !isdefined(cache, :order) || cache.order == 4 + @views @.. out=Θ1 * y₀[idxs] + Θ * (y₁[idxs] + Θ1 * (k[1][idxs] + Θ * k[2][idxs])) + else + @views @.. Θ1 * y₀[idxs]+Θ * (y₁[idxs] + + Θ1 * (k[1][idxs] + Θ * (k[2][idxs] + Θ * k[3][idxs]))) + end out end # First Derivative -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{RosenbrockCombinedConstantCache, Rodas23WConstantCache, Rodas3PConstantCache}, - idxs::Nothing, T::Type{Val{1}}, differential_vars) - @inbounds (k[1] + Θ * (-2 * k[1] + 2 * k[2] - 3 * k[2] * Θ) - y₀ + y₁) / dt -end - @muladd function _ode_interpolant( - Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCache, Rodas23WCache, Rodas3PCache}, + Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCache, Rodas23WCache, Rodas3PCache, RosenbrockCombinedConstantCache, Rodas23WConstantCache, Rodas3PConstantCache}, idxs::Nothing, T::Type{Val{1}}, differential_vars) - @inbounds @.. (k[1] + Θ * (-2 * k[1] + 2 * k[2] - 3 * k[2] * Θ) - y₀ + y₁)/dt + if !isdefined(cache, :order) || cache.order == 4 + @.. (k[1] + Θ * (-2 * k[1] + 2 * k[2] - 3 * k[2] * Θ) - y₀ + y₁)/dt + else + @.. (k[1][idxs] + Θ * (-2 * k[1][idxs] + 2 * k[2][idxs] + + Θ * (-3 * k[2][idxs] + 3 * k[3][idxs] - 4 * Θ * k[3][idxs])) - + y₀[idxs] + y₁[idxs])/dt + end end - @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs, T::Type{Val{1}}, differential_vars) - @.. (k[1][idxs] + Θ * (-2 * k[1][idxs] + 2 * k[2][idxs] - 3 * k[2][idxs] * Θ) - - y₀[idxs] + y₁[idxs])/dt + if !isdefined(cache, :order) || cache.order == 4 + @views @.. (k[1][idxs] + Θ * (-2 * k[1][idxs] + 2 * k[2][idxs] - 3 * k[2][idxs] * Θ) - + y₀[idxs] + y₁[idxs])/dt + else + @views @.. (k[1] + Θ * (-2 * k[1] + 2 * k[2] + + Θ * (-3 * k[2] + 3 * k[3] - 4 * Θ * k[3])) - y₀ + y₁)/dt + end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs::Nothing, T::Type{Val{1}}, differential_vars) - @.. out=(k[1] + Θ * (-2 * k[1] + 2 * k[2] - 3 * k[2] * Θ) - y₀ + y₁) / dt + if !isdefined(cache, :order) || cache.order == 4 + @.. out=(k[1] + Θ * (-2 * k[1] + 2 * k[2] - 3 * k[2] * Θ) - y₀ + y₁) / dt + else + @.. out=(k[1] + Θ * (-2 * k[1] + 2 * k[2] + + Θ * (-3 * k[2] + 3 * k[3] - 4 * Θ * k[3])) - y₀ + y₁) / dt + end out end @@ -206,74 +219,19 @@ end cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, idxs, T::Type{Val{1}}, differential_vars) - @views @.. out=(k[1][idxs] + - Θ * - (-2 * k[1][idxs] + 2 * k[2][idxs] - - 3 * k[2][idxs] * Θ) - - y₀[idxs] + y₁[idxs]) / dt - out -end - -#- -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, - idxs, T::Type{Val{0}}, differential_vars) - Θ1 = 1 - Θ - @.. Θ1 * y₀[idxs]+Θ * (y₁[idxs] + - Θ1 * (k[1][idxs] + Θ * (k[2][idxs] + Θ * k[3][idxs]))) -end - -@muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, - idxs::Nothing, T::Type{Val{0}}, differential_vars) - Θ1 = 1 - Θ - @.. out=Θ1 * y₀ + Θ * (y₁ + Θ1 * (k[1] + Θ * (k[2] + Θ * k[3]))) - out -end - -@muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, - idxs, T::Type{Val{0}}, differential_vars) - Θ1 = 1 - Θ - @views @.. out= Θ1 * y₀[idxs] + Θ * (y₁[idxs] + - Θ1 * (k[1][idxs] + Θ * (k[2][idxs] + Θ * k[3][idxs]))) - out -end - -# First Derivative - -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::RosenbrockCombinedConstantCache, idxs::Nothing, - T::Type{Val{1}}, differential_vars) - @inbounds @.. (k[1] + Θ * (-2 * k[1] + 2 * k[2] + - Θ * (-3 * k[2] + 3 * k[3] - 4 * Θ * k[3])) - y₀ + y₁)/dt -end - -@muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, - idxs, T::Type{Val{1}}, differential_vars) - @.. broadcast=false (k[1][idxs] + - Θ * (-2 * k[1][idxs] + 2 * k[2][idxs] + - Θ * (-3 * k[2][idxs] + 3 * k[3][idxs] - 4 * Θ * k[3][idxs])) - - y₀[idxs] + y₁[idxs])/dt -end - -@muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, - idxs::Nothing, T::Type{Val{1}}, differential_vars) - @.. broadcast=false out=(k[1] + - Θ * (-2 * k[1] + 2 * k[2] + - Θ * (-3 * k[2] + 3 * k[3] - 4 * Θ * k[3])) - y₀ + y₁) / dt - out -end - -@muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, - idxs, T::Type{Val{1}}, differential_vars) - @views @.. broadcast=false out=(k[1][idxs] + - Θ * (-2 * k[1][idxs] + 2 * k[2][idxs] + - Θ * - (-3 * k[2][idxs] + 3 * k[3][idxs] - 4 * Θ * k[3][idxs])) - - y₀[idxs] + y₁[idxs]) / dt + if !isdefined(cache, :order) || cache.order == 4 + @views @.. out=(k[1][idxs] + + Θ * + (-2 * k[1][idxs] + 2 * k[2][idxs] - + 3 * k[2][idxs] * Θ) - + y₀[idxs] + y₁[idxs]) / dt + else + @views @.. broadcast=false out=(k[1][idxs] + + Θ * (-2 * k[1][idxs] + 2 * k[2][idxs] + + Θ * + (-3 * k[2][idxs] + 3 * k[3][idxs] - 4 * Θ * k[3][idxs])) - + y₀[idxs] + y₁[idxs]) / dt + end out end @@ -344,4 +302,4 @@ end @views @.. broadcast=false out=(-6 * k[2][idxs] + 6 * k[3][idxs] - 24 * Θ * k[3][idxs]) / dt^3 out -end \ No newline at end of file +end diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index cc59d7bc12..f3412f8bcd 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -1416,7 +1416,7 @@ end mul!(_vec(du2), mass_matrix, _vec(du1)) du2 = du2 - du end - EEst = norm(du2) / norm(integrator.opts.abstol .+ integrator.opts.reltol .* k2) + EEst = norm(du2) / norm(integrator.opts.abstol .+ integrator.opts.reltol .* ks[2]) integrator.EEst = max(EEst, integrator.EEst) end end diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl index d6fe9bc752..fcf883b492 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl @@ -338,15 +338,6 @@ function Rodas4P2Tableau(T, T2) RodasTableau(A, C, gamma, c, d, H) end -struct Rodas5Tableau{T, T2} - A::Matrix{T} - C::Matrix{T} - gamma::T2 - d::Vector{T} - c::Vector{T2} - H::Matrix{T} -end - function Rodas5Tableau(T, T2) gamma = convert(T2, 0.19) A = T[ @@ -369,7 +360,7 @@ function Rodas5Tableau(T, T2) 34.20013733472935 -14.15535402717690 57.82335640988400 25.83362985412365 1.408950972071624 -6.551835421242162 0 42.57076742291101 -13.80770672017997 93.98938432427124 18.77919633714503 -31.58359187223370 -6.685968952921985 -5.810979938412932 ] - c = T2[0, 0.38, 0.3878509998321533, 0.4839718937873840, 0.4570477008819580, 1, 1, 1] + c = T2[0, 0.38, 0.3878509998321533, 0.4839718937873840, 0.4570477008819580, 1, 1, 1] d = T[gamma, -0.1823079225333714636, -0.319231832186874912, 0.3449828624725343, -0.377417564392089818, 0, 0, 0] H = T[ @@ -402,7 +393,7 @@ function Rodas5Tableau(T, T2) b7 = convert(T,1) b8 = convert(T,1) =# - Rodas5Tableau(A, C, gamma, d, c, H) + RodasTableau(A, C, gamma, d, c, H) end function Rodas5PTableau(T, T2) @@ -434,7 +425,7 @@ function Rodas5PTableau(T, T2) -9.91568850695171 -0.9689944594115154 3.0438037242978453 -24.495224566215796 20.176138334709044 15.98066361424651 -6.789040303419874 -6.710236069923372 11.419903575922262 2.8879645146136994 72.92137995996029 80.12511834622643 -52.072871366152654 -59.78993625266729 -0.15582684282751913 4.883087185713722 ] - Rodas5Tableau(A, C, gamma, d, c, H) + RodasTableau(A, C, gamma, d, c, H) end @RosenbrockW6S4OS(:tableau) diff --git a/lib/OrdinaryDiffEqRosenbrock/test/ode_rosenbrock_tests.jl b/lib/OrdinaryDiffEqRosenbrock/test/ode_rosenbrock_tests.jl index d2e5d0608f..d384937120 100644 --- a/lib/OrdinaryDiffEqRosenbrock/test/ode_rosenbrock_tests.jl +++ b/lib/OrdinaryDiffEqRosenbrock/test/ode_rosenbrock_tests.jl @@ -609,7 +609,7 @@ import LinearSolve prob = prob_ode_linear - dts = (1 / 2) .^ (6:-1:3) + dts = (1 / 2) .^ (5:-1:2) sim = test_convergence(dts, prob, Rodas5(), dense_errors = true) @test sim.𝒪est[:final]≈5 atol=testTol @test sim.𝒪est[:L2]≈5 atol=testTol @@ -630,7 +630,6 @@ import LinearSolve prob = prob_ode_linear - dts = (1 / 2) .^ (5:-1:2) sim = test_convergence(dts, prob, Rodas5P(), dense_errors = true) #@test sim.𝒪est[:final]≈5 atol=testTol #-- observed order > 6 @test sim.𝒪est[:L2]≈5 atol=testTol From c606e6746b61a3ffa986999b580ebb22fae8b5f0 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Sun, 8 Sep 2024 01:08:06 +0530 Subject: [PATCH 33/56] Rosenbrock 3rd order perform_step! refactor --- .../src/rosenbrock_caches.jl | 165 +----- .../src/rosenbrock_perform_step.jl | 526 ++---------------- .../src/rosenbrock_tableaus.jl | 238 +++----- 3 files changed, 147 insertions(+), 782 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl index 14b24b0ccb..bbb33250d9 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl @@ -11,7 +11,6 @@ end ################################################################################ # Shampine's Low-order Rosenbrocks - mutable struct RosenbrockCache{uType, rateType, uNoUnitsType, JType, WType, TabType, TFType, UFType, F, JCType, GCType, RTolType, A, StepLimiter, StageLimiter} <: RosenbrockMutableCache u::uType @@ -276,49 +275,6 @@ end ### 3rd order specialized Rosenbrocks -struct Rosenbrock33ConstantCache{TF, UF, Tab, JType, WType, F} <: - RosenbrockConstantCache - tf::TF - uf::UF - tab::Tab - J::JType - W::WType - linsolve::F -end - -@cache mutable struct Rosenbrock33Cache{uType, rateType, uNoUnitsType, JType, WType, - TabType, TFType, UFType, F, JCType, GCType, - RTolType, A, StepLimiter, StageLimiter} <: RosenbrockMutableCache - u::uType - uprev::uType - du::rateType - du1::rateType - du2::rateType - k1::rateType - k2::rateType - k3::rateType - k4::rateType - fsalfirst::rateType - fsallast::rateType - dT::rateType - J::JType - W::WType - tmp::rateType - atmp::uNoUnitsType - weight::uNoUnitsType - tab::TabType - tf::TFType - uf::UFType - linsolve_tmp::rateType - linsolve::F - jac_config::JCType - grad_config::GCType - reltol::RTolType - alg::A - step_limiter!::StepLimiter - stage_limiter!::StageLimiter -end - function alg_cache(alg::ROS3P, u, rate_prototype, ::Type{uEltypeNoUnits}, ::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck, @@ -326,10 +282,8 @@ function alg_cache(alg::ROS3P, u, rate_prototype, ::Type{uEltypeNoUnits}, du = zero(rate_prototype) du1 = zero(rate_prototype) du2 = zero(rate_prototype) - k1 = zero(rate_prototype) - k2 = zero(rate_prototype) - k3 = zero(rate_prototype) - k4 = zero(rate_prototype) + dense = Vector{rate_prototype}(undef, 0) + ks = [zero(rate_prototype) for _ in 1:4] fsalfirst = zero(rate_prototype) fsallast = zero(rate_prototype) dT = zero(rate_prototype) @@ -352,11 +306,10 @@ function alg_cache(alg::ROS3P, u, rate_prototype, ::Type{uEltypeNoUnits}, assumptions = LinearSolve.OperatorAssumptions(true)) grad_config = build_grad_config(alg, f, tf, du1, t) jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, du2) - Rosenbrock33Cache(u, uprev, du, du1, du2, k1, k2, k3, k4, - fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, + RosenbrockCache(u, uprev, dense, du, du1, du2, ks, fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp, linsolve, jac_config, grad_config, reltol, alg, alg.step_limiter!, - alg.stage_limiter!) + alg.stage_limiter!, 3) end function alg_cache(alg::ROS3P, u, rate_prototype, ::Type{uEltypeNoUnits}, @@ -368,40 +321,9 @@ function alg_cache(alg::ROS3P, u, rate_prototype, ::Type{uEltypeNoUnits}, J, W = build_J_W(alg, u, uprev, p, t, dt, f, uEltypeNoUnits, Val(false)) linprob = nothing #LinearProblem(W,copy(u); u0=copy(u)) linsolve = nothing #init(linprob,alg.linsolve,alias_A=true,alias_b=true) - Rosenbrock33ConstantCache(tf, uf, + RosenbrockConstantCache(tf, uf, ROS3PTableau(constvalue(uBottomEltypeNoUnits), - constvalue(tTypeNoUnits)), J, W, linsolve) -end - -@cache mutable struct Rosenbrock34Cache{uType, rateType, uNoUnitsType, JType, WType, - TabType, TFType, UFType, F, JCType, GCType, StepLimiter, StageLimiter} <: - RosenbrockMutableCache - u::uType - uprev::uType - du::rateType - du1::rateType - du2::rateType - k1::rateType - k2::rateType - k3::rateType - k4::rateType - fsalfirst::rateType - fsallast::rateType - dT::rateType - J::JType - W::WType - tmp::rateType - atmp::uNoUnitsType - weight::uNoUnitsType - tab::TabType - tf::TFType - uf::UFType - linsolve_tmp::rateType - linsolve::F - jac_config::JCType - grad_config::GCType - step_limiter!::StepLimiter - stage_limiter!::StageLimiter + constvalue(tTypeNoUnits)), J, W, linsolve, alg_autodiff(alg), 3) end function alg_cache(alg::Rodas3, u, rate_prototype, ::Type{uEltypeNoUnits}, @@ -411,10 +333,8 @@ function alg_cache(alg::Rodas3, u, rate_prototype, ::Type{uEltypeNoUnits}, du = zero(rate_prototype) du1 = zero(rate_prototype) du2 = zero(rate_prototype) - k1 = zero(rate_prototype) - k2 = zero(rate_prototype) - k3 = zero(rate_prototype) - k4 = zero(rate_prototype) + ks = [zero(rate_prototype) for _ in 1:4] + dense = Vector{rate_prototype}(undef, 0) fsalfirst = zero(rate_prototype) fsallast = zero(rate_prototype) dT = zero(rate_prototype) @@ -438,11 +358,11 @@ function alg_cache(alg::Rodas3, u, rate_prototype, ::Type{uEltypeNoUnits}, assumptions = LinearSolve.OperatorAssumptions(true)) grad_config = build_grad_config(alg, f, tf, du1, t) jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, du2) - Rosenbrock34Cache(u, uprev, du, du1, du2, k1, k2, k3, k4, + RosenbrockCache(u, uprev, dense, du, du1, du2, ks, fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp, linsolve, jac_config, grad_config, alg.step_limiter!, - alg.stage_limiter!) + alg.stage_limiter!, 3) end struct Rosenbrock34ConstantCache{TF, UF, Tab, JType, WType, F} <: @@ -509,16 +429,6 @@ struct Rodas23WConstantCache{TF, UF, Tab, JType, WType, F, AD} <: autodiff::AD end -struct Rodas3PConstantCache{TF, UF, Tab, JType, WType, F, AD} <: RosenbrockConstantCache - tf::TF - uf::UF - tab::Tab - J::JType - W::WType - linsolve::F - autodiff::AD -end - @cache mutable struct Rodas23WCache{uType, rateType, uNoUnitsType, JType, WType, TabType, TFType, UFType, F, JCType, GCType, RTolType, A, StepLimiter, StageLimiter} <: RosenbrockMutableCache @@ -556,43 +466,6 @@ end stage_limiter!::StageLimiter end -@cache mutable struct Rodas3PCache{uType, rateType, uNoUnitsType, JType, WType, TabType, - TFType, UFType, F, JCType, GCType, RTolType, A, StepLimiter, StageLimiter} <: - RosenbrockMutableCache - u::uType - uprev::uType - dense1::rateType - dense2::rateType - dense3::rateType - du::rateType - du1::rateType - du2::rateType - k1::rateType - k2::rateType - k3::rateType - k4::rateType - k5::rateType - fsalfirst::rateType - fsallast::rateType - dT::rateType - J::JType - W::WType - tmp::rateType - atmp::uNoUnitsType - weight::uNoUnitsType - tab::TabType - tf::TFType - uf::UFType - linsolve_tmp::rateType - linsolve::F - jac_config::JCType - grad_config::GCType - reltol::RTolType - alg::A - step_limiter!::StepLimiter - stage_limiter!::StageLimiter -end - function alg_cache(alg::Rodas23W, u, rate_prototype, ::Type{uEltypeNoUnits}, ::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck, @@ -641,17 +514,11 @@ function alg_cache(alg::Rodas3P, u, rate_prototype, ::Type{uEltypeNoUnits}, ::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck, ::Val{true}) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits} - dense1 = zero(rate_prototype) - dense2 = zero(rate_prototype) - dense3 = zero(rate_prototype) + dense = [zero(rate_prototype) for _ in 1:3] du = zero(rate_prototype) du1 = zero(rate_prototype) du2 = zero(rate_prototype) - k1 = zero(rate_prototype) - k2 = zero(rate_prototype) - k3 = zero(rate_prototype) - k4 = zero(rate_prototype) - k5 = zero(rate_prototype) + ks = [zero(rate_prototype) for _ in 1:5] fsalfirst = zero(rate_prototype) fsallast = zero(rate_prototype) dT = zero(rate_prototype) @@ -675,10 +542,10 @@ function alg_cache(alg::Rodas3P, u, rate_prototype, ::Type{uEltypeNoUnits}, assumptions = LinearSolve.OperatorAssumptions(true)) grad_config = build_grad_config(alg, f, tf, du1, t) jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, du2) - Rodas3PCache(u, uprev, dense1, dense2, dense3, du, du1, du2, k1, k2, k3, k4, k5, + RosenbrockCache(u, uprev, dense, du, du1, du2, ks, fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp, linsolve, jac_config, grad_config, reltol, alg, alg.step_limiter!, - alg.stage_limiter!) + alg.stage_limiter!, 3) end function alg_cache(alg::Rodas23W, u, rate_prototype, ::Type{uEltypeNoUnits}, @@ -705,10 +572,10 @@ function alg_cache(alg::Rodas3P, u, rate_prototype, ::Type{uEltypeNoUnits}, J, W = build_J_W(alg, u, uprev, p, t, dt, f, uEltypeNoUnits, Val(false)) linprob = nothing #LinearProblem(W,copy(u); u0=copy(u)) linsolve = nothing #init(linprob,alg.linsolve,alias_A=true,alias_b=true) - Rodas3PConstantCache(tf, uf, + RosenbrockCombinedConstantCache(tf, uf, Rodas3PTableau(constvalue(uBottomEltypeNoUnits), constvalue(tTypeNoUnits)), J, W, linsolve, - alg_autodiff(alg)) + alg_autodiff(alg), 3) end ### Rodas4 methods diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index f3412f8bcd..e6f2dda374 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -392,7 +392,7 @@ end end function initialize!(integrator, - cache::Union{Rosenbrock33ConstantCache, + cache::Union{RosenbrockCombinedConstantCache, Rosenbrock34ConstantCache, Rosenbrock4ConstantCache}) integrator.kshortsize = 2 @@ -407,7 +407,7 @@ function initialize!(integrator, end function initialize!(integrator, - cache::Union{Rosenbrock33Cache, + cache::Union{RosenbrockCache, Rosenbrock34Cache, Rosenbrock4Cache}) integrator.kshortsize = 2 @@ -419,179 +419,6 @@ function initialize!(integrator, OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) end -@muladd function perform_step!(integrator, cache::Rosenbrock33ConstantCache, - repeat_step = false) - @unpack t, dt, uprev, u, f, p = integrator - @unpack tf, uf = cache - @unpack a21, a31, a32, C21, C31, C32, b1, b2, b3, btilde1, btilde2, btilde3, gamma, c2, c3, d1, d2, d3 = cache.tab - - # Precalculations - dtC21 = C21 / dt - dtC31 = C31 / dt - dtC32 = C32 / dt - - dtd1 = dt * d1 - dtd2 = dt * d2 - dtd3 = dt * d3 - dtgamma = dt * gamma - - mass_matrix = integrator.f.mass_matrix - - # Time derivative - dT = calc_tderivative(integrator, cache) - - W = calc_W(integrator, cache, dtgamma, repeat_step, true) - if !issuccess_W(W) - integrator.EEst = 2 - return nothing - end - - linsolve_tmp = integrator.fsalfirst + dtd1 * dT - - k1 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - u = uprev + a21 * k1 - du = f(u, p, t + c2 * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - linsolve_tmp = du + dtd2 * dT + dtC21 * k1 - else - linsolve_tmp = du + dtd2 * dT + mass_matrix * (dtC21 * k1) - end - - k2 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - u = uprev + a31 * k1 + a32 * k2 - du = f(u, p, t + c3 * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - linsolve_tmp = du + dtd3 * dT + dtC31 * k1 + dtC32 * k2 - else - linsolve_tmp = du + dtd3 * dT + mass_matrix * (dtC31 * k1 + dtC32 * k2) - end - - k3 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - u = uprev + b1 * k1 + b2 * k2 + b3 * k3 - integrator.fsallast = f(u, p, t + dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if integrator.opts.adaptive - utilde = btilde1 * k1 + btilde2 * k2 + btilde3 * k3 - atmp = calculate_residuals(utilde, uprev, u, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - integrator.EEst = integrator.opts.internalnorm(atmp, t) - end - - integrator.k[1] = integrator.fsalfirst - integrator.k[2] = integrator.fsallast - integrator.u = u - return nothing -end - -@muladd function perform_step!(integrator, cache::Rosenbrock33Cache, repeat_step = false) - @unpack t, dt, uprev, u, f, p = integrator - @unpack du, du1, du2, fsalfirst, fsallast, k1, k2, k3, dT, J, W, uf, tf, linsolve_tmp, jac_config, atmp, weight, stage_limiter!, step_limiter! = cache - @unpack a21, a31, a32, C21, C31, C32, b1, b2, b3, btilde1, btilde2, btilde3, gamma, c2, c3, d1, d2, d3 = cache.tab - - # Assignments - mass_matrix = integrator.f.mass_matrix - sizeu = size(u) - utilde = du - - # Precalculations - dtC21 = C21 / dt - dtC31 = C31 / dt - dtC32 = C32 / dt - - dtd1 = dt * d1 - dtd2 = dt * d2 - dtd3 = dt * d3 - dtgamma = dt * gamma - - calc_rosenbrock_differentiation!(integrator, cache, dtd1, dtgamma, repeat_step, true) - - calculate_residuals!(weight, fill!(weight, one(eltype(u))), uprev, uprev, - integrator.opts.abstol, integrator.opts.reltol, - integrator.opts.internalnorm, t) - - if repeat_step - linres = dolinsolve( - integrator, cache.linsolve; A = nothing, b = _vec(linsolve_tmp), - du = integrator.fsalfirst, u = u, p = p, t = t, weight = weight, - solverdata = (; gamma = dtgamma)) - else - linres = dolinsolve(integrator, cache.linsolve; A = W, b = _vec(linsolve_tmp), - du = integrator.fsalfirst, u = u, p = p, t = t, weight = weight, - solverdata = (; gamma = dtgamma)) - end - - vecu = _vec(linres.u) - veck1 = _vec(k1) - - @.. broadcast=false veck1=-vecu - integrator.stats.nsolve += 1 - - @.. broadcast=false u=uprev + a21 * k1 - stage_limiter!(u, integrator, p, t + c2 * dt) - f(du, u, p, t + c2 * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + dtd2 * dT + dtC21 * k1 - else - @.. broadcast=false du1=dtC21 * k1 - mul!(_vec(du2), mass_matrix, _vec(du1)) - @.. broadcast=false linsolve_tmp=du + dtd2 * dT + du2 - end - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - vecu = _vec(linres.u) - veck2 = _vec(k2) - - @.. broadcast=false veck2=-vecu - - integrator.stats.nsolve += 1 - - @.. broadcast=false u=uprev + a31 * k1 + a32 * k2 - stage_limiter!(u, integrator, p, t + c3 * dt) - f(du, u, p, t + c3 * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + dtd3 * dT + dtC31 * k1 + dtC32 * k2 - else - @.. broadcast=false du1=dtC31 * k1 + dtC32 * k2 - mul!(_vec(du2), mass_matrix, _vec(du1)) - @.. broadcast=false linsolve_tmp=du + dtd3 * dT + du2 - end - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - vecu = _vec(linres.u) - veck3 = _vec(k3) - - @.. broadcast=false veck3=-vecu - - integrator.stats.nsolve += 1 - - @.. broadcast=false u=uprev + b1 * k1 + b2 * k2 + b3 * k3 - - step_limiter!(u, integrator, p, t + dt) - - f(fsallast, u, p, t + dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if integrator.opts.adaptive - @.. broadcast=false utilde=btilde1 * k1 + btilde2 * k2 + btilde3 * k3 - calculate_residuals!(atmp, utilde, uprev, u, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - integrator.EEst = integrator.opts.internalnorm(atmp, t) - end - cache.linsolve = linres.cache -end - ################################################################################ @muladd function perform_step!(integrator, cache::Rosenbrock34ConstantCache, @@ -833,308 +660,22 @@ end #### Rodas3P type method -function initialize!(integrator, cache::Union{Rodas23WConstantCache, Rodas3PConstantCache}) +function initialize!(integrator, cache::RosenbrockCombinedConstantCache) integrator.kshortsize = 3 integrator.k = typeof(integrator.k)(undef, integrator.kshortsize) # Avoid undefined entries if k is an array of arrays - integrator.k[1] = zero(integrator.u) - integrator.k[2] = zero(integrator.u) - integrator.k[3] = zero(integrator.u) -end - -@muladd function perform_step!( - integrator, cache::Union{Rodas23WConstantCache, Rodas3PConstantCache}, - repeat_step = false) - @unpack t, dt, uprev, u, f, p = integrator - @unpack tf, uf = cache - @unpack a21, a41, a42, a43, C21, C31, C32, C41, C42, C43, C51, C52, C53, C54, gamma, c2, c3, d1, d2, d3 = cache.tab - - # Precalculations - dtC21 = C21 / dt - dtC31 = C31 / dt - dtC32 = C32 / dt - dtC41 = C41 / dt - dtC42 = C42 / dt - dtC43 = C43 / dt - dtC51 = C51 / dt - dtC52 = C52 / dt - dtC53 = C53 / dt - dtC54 = C54 / dt - - dtd1 = dt * d1 - dtd2 = dt * d2 - dtd3 = dt * d3 - dtgamma = dt * gamma - - mass_matrix = integrator.f.mass_matrix - - # Time derivative - tf.u = uprev - dT = calc_tderivative(integrator, cache) - - W = calc_W(integrator, cache, dtgamma, repeat_step, true) - if !issuccess_W(W) - integrator.EEst = 2 - return nothing - end - - du = f(uprev, p, t) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - k3 = copy(du) #-- save for stage 3 - - linsolve_tmp = du + dtd1 * dT - - k1 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - u = uprev + a21 * k1 - du = f(u, p, t + c2 * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - linsolve_tmp = du + dtd2 * dT + dtC21 * k1 - else - linsolve_tmp = du + dtd2 * dT + mass_matrix * (dtC21 * k1) - end - - k2 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - - if mass_matrix === I - linsolve_tmp = k3 + dtd3 * dT + (dtC31 * k1 + dtC32 * k2) - else - linsolve_tmp = k3 + dtd3 * dT + mass_matrix * (dtC31 * k1 + dtC32 * k2) - end - - k3 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - u = uprev + a41 * k1 + a42 * k2 + a43 * k3 - du = f(u, p, t + dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - linsolve_tmp = du + (dtC41 * k1 + dtC42 * k2 + dtC43 * k3) - else - linsolve_tmp = du + mass_matrix * (dtC41 * k1 + dtC42 * k2 + dtC43 * k3) - end - - k4 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - - if mass_matrix === I - linsolve_tmp = du + (dtC52 * k2 + dtC54 * k4 + dtC51 * k1 + dtC53 * k3) - else - linsolve_tmp = du + - mass_matrix * (dtC52 * k2 + dtC54 * k4 + dtC51 * k1 + dtC53 * k3) - end - - k5 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - du = u + k4 #-- solution p=2 - u = u + k5 #-- solution p=3 - - EEst = 0.0 - if integrator.opts.calck - @unpack h21, h22, h23, h24, h25, h31, h32, h33, h34, h35, h2_21, h2_22, h2_23, h2_24, h2_25 = cache.tab - integrator.k[1] = h21 * k1 + h22 * k2 + h23 * k3 + h24 * k4 + h25 * k5 - integrator.k[2] = h31 * k1 + h32 * k2 + h33 * k3 + h34 * k4 + h35 * k5 - integrator.k[3] = h2_21 * k1 + h2_22 * k2 + h2_23 * k3 + h2_24 * k4 + h2_25 * k5 - if integrator.opts.adaptive - if isa(linsolve_tmp, AbstractFloat) - u_int, u_diff = calculate_interpoldiff( - uprev, du, u, integrator.k[1], integrator.k[2], integrator.k[3]) - else - u_int = linsolve_tmp - u_diff = linsolve_tmp .+ 0 - calculate_interpoldiff!(u_int, u_diff, uprev, du, u, integrator.k[1], - integrator.k[2], integrator.k[3]) - end - atmp = calculate_residuals(u_diff, uprev, u_int, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - EEst = max(EEst, integrator.opts.internalnorm(atmp, t)) #-- role of t unclear - end - end - - if (integrator.alg isa Rodas23W) - k1 = u .+ 0 - u = du .+ 0 - du = k1 .+ 0 - if integrator.opts.calck - integrator.k[1] = integrator.k[3] .+ 0 - integrator.k[2] = 0 * integrator.k[2] - end - end - - if integrator.opts.adaptive - atmp = calculate_residuals(u - du, uprev, u, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - integrator.EEst = max(EEst, integrator.opts.internalnorm(atmp, t)) + for i in 1:integrator.kshortsize + integrator.k[i] = zero(integrator.u) end - - integrator.u = u - return nothing end -function initialize!(integrator, cache::Union{Rodas23WCache, Rodas3PCache}) +function initialize!(integrator, cache::RosenbrockCache) integrator.kshortsize = 3 @unpack dense1, dense2, dense3 = cache resize!(integrator.k, integrator.kshortsize) - integrator.k[1] = dense1 - integrator.k[2] = dense2 - integrator.k[3] = dense3 -end - -@muladd function perform_step!( - integrator, cache::Union{Rodas23WCache, Rodas3PCache}, repeat_step = false) - @unpack t, dt, uprev, u, f, p = integrator - @unpack du, du1, du2, dT, J, W, uf, tf, k1, k2, k3, k4, k5, linsolve_tmp, jac_config, atmp, weight, stage_limiter!, step_limiter! = cache - @unpack a21, a41, a42, a43, C21, C31, C32, C41, C42, C43, C51, C52, C53, C54, gamma, c2, c3, d1, d2, d3 = cache.tab - - # Assignments - sizeu = size(u) - uidx = eachindex(integrator.uprev) - mass_matrix = integrator.f.mass_matrix - - # Precalculations - dtC21 = C21 / dt - dtC31 = C31 / dt - dtC32 = C32 / dt - dtC41 = C41 / dt - dtC42 = C42 / dt - dtC43 = C43 / dt - dtC51 = C51 / dt - dtC52 = C52 / dt - dtC53 = C53 / dt - dtC54 = C54 / dt - - dtd1 = dt * d1 - dtd2 = dt * d2 - dtd3 = dt * d3 - dtgamma = dt * gamma - - f(cache.fsalfirst, uprev, p, t) # used in calc_rosenbrock_differentiation! - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - calc_rosenbrock_differentiation!(integrator, cache, dtd1, dtgamma, repeat_step, true) - - calculate_residuals!(weight, fill!(weight, one(eltype(u))), uprev, uprev, - integrator.opts.abstol, integrator.opts.reltol, - integrator.opts.internalnorm, t) - - if repeat_step - linres = dolinsolve( - integrator, cache.linsolve; A = nothing, b = _vec(linsolve_tmp), - du = cache.fsalfirst, u = u, p = p, t = t, weight = weight, - solverdata = (; gamma = dtgamma)) - else - linres = dolinsolve(integrator, cache.linsolve; A = W, b = _vec(linsolve_tmp), - du = cache.fsalfirst, u = u, p = p, t = t, weight = weight, - solverdata = (; gamma = dtgamma)) - end - - @.. broadcast=false $(_vec(k1))=-linres.u - - integrator.stats.nsolve += 1 - - @.. broadcast=false u=uprev + a21 * k1 - stage_limiter!(u, integrator, p, t + c2 * dt) - f(du, u, p, t + c2 * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + dtd2 * dT + dtC21 * k1 - else - @.. broadcast=false du1=dtC21 * k1 - mul!(_vec(du2), mass_matrix, _vec(du1)) - @.. broadcast=false linsolve_tmp=du + dtd2 * dT + du2 - end - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - @.. broadcast=false $(_vec(k2))=-linres.u - integrator.stats.nsolve += 1 - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=cache.fsalfirst + dtd3 * dT + - (dtC31 * k1 + dtC32 * k2) - else - @.. broadcast=false du1=dtC31 * k1 + dtC32 * k2 - mul!(_vec(du2), mass_matrix, _vec(du1)) - @.. broadcast=false linsolve_tmp=cache.fsalfirst + dtd3 * dT + du2 - end - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - @.. broadcast=false $(_vec(k3))=-linres.u - integrator.stats.nsolve += 1 - - @.. broadcast=false u=uprev + a41 * k1 + a42 * k2 + a43 * k3 - stage_limiter!(u, integrator, p, t + c2 * dt) - f(du, u, p, t + dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + - (dtC41 * k1 + dtC42 * k2 + dtC43 * k3) - else - @.. broadcast=false du1=dtC41 * k1 + dtC42 * k2 + dtC43 * k3 - mul!(_vec(du2), mass_matrix, _vec(du1)) - @.. broadcast=false linsolve_tmp=du + du2 - end - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - @.. broadcast=false $(_vec(k4))=-linres.u - integrator.stats.nsolve += 1 - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + - (dtC52 * k2 + dtC54 * k4 + dtC51 * k1 + dtC53 * k3) - else - @.. broadcast=false du1=dtC52 * k2 + dtC54 * k4 + dtC51 * k1 + dtC53 * k3 - mul!(_vec(du2), mass_matrix, _vec(du1)) - @.. broadcast=false linsolve_tmp=du + du2 - end - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - @.. broadcast=false $(_vec(k5))=-linres.u - integrator.stats.nsolve += 1 - - du = u + k4 #-- p=2 solution - u .+= k5 - - step_limiter!(u, integrator, p, t + dt) - - EEst = 0.0 - if integrator.opts.calck - @unpack h21, h22, h23, h24, h25, h31, h32, h33, h34, h35, h2_21, h2_22, h2_23, h2_24, h2_25 = cache.tab - @.. broadcast=false integrator.k[1]=h21 * k1 + h22 * k2 + h23 * k3 + h24 * k4 + - h25 * k5 - @.. broadcast=false integrator.k[2]=h31 * k1 + h32 * k2 + h33 * k3 + h34 * k4 + - h35 * k5 - @.. broadcast=false integrator.k[3]=h2_21 * k1 + h2_22 * k2 + h2_23 * k3 + - h2_24 * k4 + h2_25 * k5 - if integrator.opts.adaptive - calculate_interpoldiff!( - du1, du2, uprev, du, u, integrator.k[1], integrator.k[2], integrator.k[3]) - calculate_residuals!(atmp, du2, uprev, du1, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - EEst = max(EEst, integrator.opts.internalnorm(atmp, t)) #-- role of t unclear - end - end - - if (integrator.alg isa Rodas23W) - du1[:] = u[:] - u[:] = du[:] - du[:] = du1[:] - if integrator.opts.calck - integrator.k[1][:] = integrator.k[3][:] - integrator.k[2][:] .= 0.0 - end - end - - if integrator.opts.adaptive - calculate_residuals!(atmp, u - du, uprev, u, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - integrator.EEst = max(EEst, integrator.opts.internalnorm(atmp, t)) + for i in 1:integrator.kshortsize + integrator.k[i] = cache.dense[i] end - cache.linsolve = linres.cache end function calculate_interpoldiff(uprev, up2, up3, c_koeff, d_koeff, c2_koeff) @@ -1272,6 +813,7 @@ end integrator.EEst = integrator.opts.internalnorm(atmp, t) end + EEst = 0.0 if integrator.opts.calck for j in eachindex(integrator.k) integrator.k[j] = zero(integrator.k[1]) @@ -1281,6 +823,7 @@ end integrator.k[j] = @.. integrator.k[j] + H[j, i] * ks[i] end end + integrator.k[3] = h2_21 * k1 + h2_22 * k2 + h2_23 * k3 + h2_24 * k4 + h2_25 * k5 if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && (integrator.EEst < 1.0) k2 = 0.5 * (uprev + u + @@ -1296,8 +839,35 @@ end EEst = norm(du2) / norm(integrator.opts.abstol .+ integrator.opts.reltol .* k2) integrator.EEst = max(EEst, integrator.EEst) end + + if integrator.opts.adaptive + if isa(linsolve_tmp, AbstractFloat) + u_int, u_diff = calculate_interpoldiff( + uprev, du, u, integrator.k[1], integrator.k[2], integrator.k[3]) + else + u_int = linsolve_tmp + u_diff = linsolve_tmp .+ 0 + calculate_interpoldiff!(u_int, u_diff, uprev, du, u, integrator.k[1], + integrator.k[2], integrator.k[3]) + end + atmp = calculate_residuals(u_diff, uprev, u_int, integrator.opts.abstol, + integrator.opts.reltol, integrator.opts.internalnorm, t) + EEst = max(EEst, integrator.opts.internalnorm(atmp, t)) #-- role of t unclear + end + + if (integrator.alg isa Rodas23W) + k1 = u .+ 0 + u = du .+ 0 + du = k1 .+ 0 + if integrator.opts.calck + integrator.k[1] = integrator.k[3] .+ 0 + integrator.k[2] = 0 * integrator.k[2] + end + end end + integrator.k[1] = integrator.fsalfirst + integrator.k[2] = integrator.fsallast integrator.u = u return nothing end @@ -1314,12 +884,13 @@ end @muladd function perform_step!(integrator, cache::RosenbrockCache, repeat_step = false) (;t, dt, uprev, u, f, p) = integrator (;du, du1, du2, dT, J, W, uf, tf, ks, linsolve_tmp, jac_config, atmp, weight, stage_limiter!, step_limiter!) = cache - (;A, C, gamma, c, d, H) = cache.tab + (;A, C, gamma, b, btilde, c, d, H) = cache.tab # Assignments sizeu = size(u) uidx = eachindex(integrator.uprev) mass_matrix = integrator.f.mass_matrix + utilde = du # Precalculations dtC = C .* inv(dt) @@ -1380,9 +951,12 @@ end du .= ks[end] u .+= ks[end] + @.. u=uprev + b[1] * ks[1] + b[2] * ks[2] + b[3] * ks[3] + step_limiter!(u, integrator, p, t + dt) if integrator.opts.adaptive + @.. utilde=btilde[1] * ks[1] + btilde[2] * ks[2] + btilde[3] * ks[3] if (integrator.alg isa Rodas5Pe) @. du = 0.2606326497975715 * ks[1] - 0.005158627295444251 * ks[2] + 1.3038988631109731 * ks[3] + 1.235000722062074 * ks[4] + @@ -1403,6 +977,22 @@ end @.. integrator.k[j] += H[j, i] * ks[i] end end + if (integrator.alg isa Rodas23W) + du1[:] = u[:] + u[:] = du[:] + du[:] = du1[:] + if integrator.opts.calck + integrator.k[1][:] = integrator.k[3][:] + integrator.k[2][:] .= 0.0 + end + end + if integrator.opts.adaptive + calculate_interpoldiff!( + du1, du2, uprev, du, u, integrator.k[1], integrator.k[2], integrator.k[3]) + calculate_residuals!(atmp, du2, uprev, du1, integrator.opts.abstol, + integrator.opts.reltol, integrator.opts.internalnorm, t) + EEst = max(EEst, integrator.opts.internalnorm(atmp, t)) #-- role of t unclear + end if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && (integrator.EEst < 1.0) ks[2] = 0.5 * (uprev + u + diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl index fcf883b492..479cf4a0ed 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl @@ -21,197 +21,105 @@ function Rosenbrock32Tableau(T) end struct ROS3PTableau{T, T2} - a21::T - a31::T - a32::T - C21::T - C31::T - C32::T - b1::T - b2::T - b3::T - btilde1::T - btilde2::T - btilde3::T + A::Matrix{T} + C::Matrix{T} + b::Vector{T} + btilde::Vector{T} gamma::T2 - c2::T2 - c3::T2 - d1::T - d2::T - d3::T + c::Vector{T2} + d::Vector{T} end function ROS3PTableau(T, T2) gamma = convert(T, 1 / 2 + sqrt(3) / 6) igamma = inv(gamma) - a21 = convert(T, igamma) - a31 = convert(T, igamma) - a32 = convert(T, 0) - C21 = convert(T, -igamma^2) + A = T[ + 0 0 0 + convert(T, igamma) 0 0 + convert(T, igamma) convert(T, 0) 0 + ] tmp = -igamma * (convert(T, 2) - convert(T, 1 / 2) * igamma) - C31 = -igamma * (convert(T, 1) - tmp) - C32 = tmp + C = T[ + 0 0 0 + convert(T, -igamma^2) 0 0 + -igamma * (convert(T, 1) - tmp) tmp 0 + ] tmp = igamma * (convert(T, 2 / 3) - convert(T, 1 / 6) * igamma) - b1 = igamma * (convert(T, 1) + tmp) - b2 = tmp - b3 = convert(T, 1 / 3) * igamma + b = [(igamma * (convert(T, 1) + tmp)), (tmp), (convert(T, 1 / 3) * igamma)] # btilde1 = convert(T,2.113248654051871) # btilde2 = convert(T,1.000000000000000) # btilde3 = convert(T,0.4226497308103742) - btilde1 = b1 - convert(T, 2.113248654051871) - btilde2 = b2 - convert(T, 1.000000000000000) - btilde3 = b3 - convert(T, 0.4226497308103742) - c2 = convert(T, 1) - c3 = convert(T, 1) - d1 = convert(T, 0.7886751345948129) - d2 = convert(T, -0.2113248654051871) - d3 = convert(T, -1.077350269189626) - ROS3PTableau( - a21, a31, a32, C21, C31, C32, b1, b2, b3, btilde1, btilde2, btilde3, gamma, - c2, c3, d1, d2, d3) + btilde = [(b1 - convert(T, 2.113248654051871)), (b2 - convert(T, 1.000000000000000)), (b3 - convert(T, 0.4226497308103742))] + c = T[1, 1] + d = T[0.7886751345948129, -0.2113248654051871, -1.077350269189626] + ROS3PTableau(A, C, b, btilde, gamma, c, d) end struct Rodas3Tableau{T, T2} - a21::T - a31::T - a32::T - a41::T - a42::T - a43::T - C21::T - C31::T - C32::T - C41::T - C42::T - C43::T - b1::T - b2::T - b3::T - b4::T - btilde1::T - btilde2::T - btilde3::T - btilde4::T + A::Matrix{T} + C::Matrix{T} + b::Vector{T} + btilde::Vector{T} gamma::T2 - c2::T2 - c3::T2 - d1::T - d2::T - d3::T - d4::T + c::Vector{T2} + d::Vector{T} end function Rodas3Tableau(T, T2) gamma = convert(T, 1 // 2) - a21 = convert(T, 0) - a31 = convert(T, 2) - a32 = convert(T, 0) - a41 = convert(T, 2) - a42 = convert(T, 0) - a43 = convert(T, 1) - C21 = convert(T, 4) - C31 = convert(T, 1) - C32 = convert(T, -1) - C41 = convert(T, 1) - C42 = convert(T, -1) - C43 = convert(T, -8 // 3) - b1 = convert(T, 2) - b2 = convert(T, 0) - b3 = convert(T, 1) - b4 = convert(T, 1) - btilde1 = convert(T, 0.0) - btilde2 = convert(T, 0.0) - btilde3 = convert(T, 0.0) - btilde4 = convert(T, 1.0) - c2 = convert(T, 0.0) - c3 = convert(T, 1.0) - c4 = convert(T, 1.0) - d1 = convert(T, 1 // 2) - d2 = convert(T, 3 // 2) - d3 = convert(T, 0) - d4 = convert(T, 0) - Rodas3Tableau(a21, a31, a32, a41, a42, a43, C21, C31, C32, C41, C42, C43, b1, b2, b3, - b4, btilde1, btilde2, btilde3, btilde4, gamma, c2, c3, d1, d2, d3, d4) + A = T[ + 0 0 0 + 0 0 0 + 2 0 0 + 2 0 1 + ] + C = T[ + 0 0 0 + 4 0 0 + 1 -1 0 + 1 -1 -8 // 3 + ] + b = T[2, 0, 1, 1] + btilde = T[0.0, 0.0, 0.0, 1.0] + c = T[0.0, 1.0, 1.0] + d = T[1 // 2, 3 // 2, 0, 0] + Rodas3Tableau(A, C, b, btilde, gamma, c, d) end struct Rodas3PTableau{T, T2} - a21::T - a41::T - a42::T - a43::T - C21::T - C31::T - C32::T - C41::T - C42::T - C43::T - C51::T - C52::T - C53::T - C54::T + A::Matrix{T} + C::Matrix{T} gamma::T - c2::T2 - c3::T2 - d1::T - d2::T - d3::T - h21::T - h22::T - h23::T - h24::T - h25::T - h31::T - h32::T - h33::T - h34::T - h35::T - h2_21::T - h2_22::T - h2_23::T - h2_24::T - h2_25::T + c::Vector{T2} + d::Vector{T} + h::Matrix{T} + h2::Vector{T} end function Rodas3PTableau(T, T2) gamma = convert(T, 1 // 3) - a21 = convert(T, 4.0 / 3.0) - a41 = convert(T, 2.90625) - a42 = convert(T, 3.375) - a43 = convert(T, 0.40625) - C21 = -convert(T, 4.0) - C31 = convert(T, 8.25) - C32 = convert(T, 6.75) - C41 = convert(T, 1.21875) - C42 = -convert(T, 5.0625) - C43 = -convert(T, 1.96875) - C51 = convert(T, 4.03125) - C52 = -convert(T, 15.1875) - C53 = -convert(T, 4.03125) - C54 = convert(T, 6.0) - c2 = convert(T2, 4.0 / 9.0) - c3 = convert(T2, 0.0) - d1 = convert(T, 1.0 / 3.0) - d2 = -convert(T, 1.0 / 9.0) - d3 = convert(T, 1.0) - h21 = convert(T, 1.78125) - h22 = convert(T, 6.75) - h23 = convert(T, 0.15625) - h24 = -convert(T, 6.0) - h25 = -convert(T, 1.0) - h31 = convert(T, 4.21875) - h32 = -convert(T, 15.1875) - h33 = -convert(T, 3.09375) - h34 = convert(T, 9.0) - h35 = convert(T, 0.0) - h2_21 = convert(T, 4.21875) - h2_22 = -convert(T, 2.025) - h2_23 = -convert(T, 1.63125) - h2_24 = -convert(T, 1.7) - h2_25 = -convert(T, 0.1) - Rodas3PTableau(a21, a41, a42, a43, - C21, C31, C32, C41, C42, C43, C51, C52, C53, C54, - gamma, c2, c3, d1, d2, d3, - h21, h22, h23, h24, h25, h31, h32, h33, h34, h35, h2_21, h2_22, h2_23, h2_24, h2_25) + A = T[ + 0 0 0 0 + 4.0 / 3.0 0 0 0 + 0 0 0 0 + 2.90625 3.375 0.40625 0 + ] + C = T[ + 0 0 0 0 + 4.0 0 0 0 + 8.25 6.75 0 0 + 1.21875 5.0625 1.96875 0 + 4.03125 15.1875 4.03125 6.0 + ] + c = T2[4.0 / 9.0, 0.0] + d = T[1.0 / 3.0, 1.0 / 9.0, 1.0] + H = T[ + 0 0 0 0 0 + 1.78125 6.75 0.15625 6.0 1.0 + 4.21875 15.1875 3.09375 9.0 0.0 + ] + h2_2 = T[4.21875, 2.025, 1.63125, 1.7, 0.1] + Rodas3PTableau(A, C, gamma, c, d, H, h2_2) end @ROS2(:tableau) @@ -475,4 +383,4 @@ beta3 = 6.8619167645278386e-2 beta4 = 0.8289547562599182 beta5 = 7.9630136489868164e-2 alpha64 = -0.2076823627400282 -=# +=# \ No newline at end of file From c683f897b71e60e5df70ba0defa4e17b3a0d4074 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Sun, 8 Sep 2024 01:11:55 +0530 Subject: [PATCH 34/56] Fixes --- lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl index bbb33250d9..c7ecf45a96 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl @@ -764,7 +764,7 @@ function alg_cache( end function get_fsalfirstlast( - cache::Union{Rosenbrock23Cache, Rosenbrock32Cache, Rosenbrock33Cache, + cache::Union{Rosenbrock23Cache, Rosenbrock32Cache, RosenbrockCache, Rosenbrock34Cache, Rosenbrock4Cache}, u) From de0b440828fe99c2384c6aa0d5729adf88424aea Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Sun, 8 Sep 2024 01:18:14 +0530 Subject: [PATCH 35/56] Fixes --- .../src/rosenbrock_caches.jl | 1 - .../src/rosenbrock_perform_step.jl | 122 ------------------ 2 files changed, 123 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl index c7ecf45a96..ed291955b4 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl @@ -765,7 +765,6 @@ end function get_fsalfirstlast( cache::Union{Rosenbrock23Cache, Rosenbrock32Cache, RosenbrockCache, - Rosenbrock34Cache, Rosenbrock4Cache}, u) (cache.fsalfirst, cache.fsallast) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index e6f2dda374..e47a1e6f0f 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -408,7 +408,6 @@ end function initialize!(integrator, cache::Union{RosenbrockCache, - Rosenbrock34Cache, Rosenbrock4Cache}) integrator.kshortsize = 2 @unpack fsalfirst, fsallast = cache @@ -508,127 +507,6 @@ end return nothing end -@muladd function perform_step!(integrator, cache::Rosenbrock34Cache, repeat_step = false) - @unpack t, dt, uprev, u, f, p = integrator - @unpack du, du1, du2, fsalfirst, fsallast, k1, k2, k3, k4, dT, J, W, uf, tf, linsolve_tmp, jac_config, atmp, weight, stage_limiter!, step_limiter! = cache - @unpack a21, a31, a32, a41, a42, a43, C21, C31, C32, C41, C42, C43, b1, b2, b3, b4, btilde1, btilde2, btilde3, btilde4, gamma, c2, c3, d1, d2, d3, d4 = cache.tab - - # Assignments - uidx = eachindex(integrator.uprev) - sizeu = size(u) - mass_matrix = integrator.f.mass_matrix - utilde = du - - # Precalculations - dtC21 = C21 / dt - dtC31 = C31 / dt - dtC32 = C32 / dt - dtC41 = C41 / dt - dtC42 = C42 / dt - dtC43 = C43 / dt - - dtd1 = dt * d1 - dtd2 = dt * d2 - dtd3 = dt * d3 - dtd4 = dt * d4 - dtgamma = dt * gamma - - calc_rosenbrock_differentiation!(integrator, cache, dtd1, dtgamma, repeat_step, true) - - calculate_residuals!(weight, fill!(weight, one(eltype(u))), uprev, uprev, - integrator.opts.abstol, integrator.opts.reltol, - integrator.opts.internalnorm, t) - - if repeat_step - linres = dolinsolve( - integrator, cache.linsolve; A = nothing, b = _vec(linsolve_tmp), - du = integrator.fsalfirst, u = u, p = p, t = t, weight = weight, - solverdata = (; gamma = dtgamma)) - else - linres = dolinsolve(integrator, cache.linsolve; A = W, b = _vec(linsolve_tmp), - du = integrator.fsalfirst, u = u, p = p, t = t, weight = weight, - solverdata = (; gamma = dtgamma)) - end - - vecu = _vec(linres.u) - veck1 = _vec(k1) - - @.. broadcast=false veck1=-vecu - integrator.stats.nsolve += 1 - - #= - a21 == 0 and c2 == 0 - so du = integrator.fsalfirst! - @.. broadcast=false u = uprev + a21*k1 - - f(du, u, p, t+c2*dt) - =# - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=fsalfirst + dtd2 * dT + dtC21 * k1 - else - @.. broadcast=false du1=dtC21 * k1 - mul!(_vec(du2), mass_matrix, _vec(du1)) - @.. broadcast=false linsolve_tmp=fsalfirst + dtd2 * dT + du2 - end - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - veck2 = _vec(k2) - @.. broadcast=false veck2=-vecu - integrator.stats.nsolve += 1 - - @.. broadcast=false u=uprev + a31 * k1 + a32 * k2 - stage_limiter!(u, integrator, p, t + c3 * dt) - f(du, u, p, t + c3 * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + dtd3 * dT + dtC31 * k1 + dtC32 * k2 - else - @.. broadcast=false du1=dtC31 * k1 + dtC32 * k2 - mul!(_vec(du2), mass_matrix, _vec(du1)) - @.. broadcast=false linsolve_tmp=du + dtd3 * dT + du2 - end - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - veck3 = _vec(k3) - @.. broadcast=false veck3=-vecu - integrator.stats.nsolve += 1 - @.. broadcast=false u=uprev + a41 * k1 + a42 * k2 + a43 * k3 - stage_limiter!(u, integrator, p, t + dt) - f(du, u, p, t + dt) #-- c4 = 1 - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + dtd4 * dT + dtC41 * k1 + dtC42 * k2 + - dtC43 * k3 - else - @.. broadcast=false du1=dtC41 * k1 + dtC42 * k2 + dtC43 * k3 - mul!(_vec(du2), mass_matrix, _vec(du1)) - @.. broadcast=false linsolve_tmp=du + dtd4 * dT + du2 - end - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - veck4 = _vec(k4) - @.. broadcast=false veck4=-vecu - integrator.stats.nsolve += 1 - - @.. broadcast=false u=uprev + b1 * k1 + b2 * k2 + b3 * k3 + b4 * k4 - - step_limiter!(u, integrator, p, t + dt) - - f(fsallast, u, p, t + dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if integrator.opts.adaptive - @.. broadcast=false utilde=btilde1 * k1 + btilde2 * k2 + btilde3 * k3 + btilde4 * k4 - calculate_residuals!(atmp, utilde, uprev, u, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - integrator.EEst = integrator.opts.internalnorm(atmp, t) - end - cache.linsolve = linres.cache -end - ################################################################################ #### ROS2 type method From a6ef729468416322327271ad213b1acc01c8021b Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Sun, 8 Sep 2024 01:26:43 +0530 Subject: [PATCH 36/56] Fixes --- .../src/interp_func.jl | 2 +- .../src/rosenbrock_interpolants.jl | 18 +- .../src/stiff_addsteps.jl | 198 ------------------ 3 files changed, 10 insertions(+), 208 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl b/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl index 07f4bbeee7..2977c22c2e 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl @@ -11,7 +11,7 @@ end function DiffEqBase.interp_summary(::Type{cacheType}, dense::Bool) where { cacheType <: - Union{RosenbrockCombinedConstantCache, Rodas23WConstantCache, Rodas3PConstantCache, + Union{RosenbrockCombinedConstantCache, Rodas23WConstantCache, RosenbrockCache, Rodas23WCache, Rodas3PCache}} dense ? "specialized 3rd order \"free\" stiffness-aware interpolation" : "1st order linear" diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl index d72eca9f66..cf2e1cf9f9 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl @@ -1,7 +1,7 @@ ### Fallbacks to capture ROSENBROCKS_WITH_INTERPOLATIONS = Union{Rosenbrock23ConstantCache, Rosenbrock23Cache, Rosenbrock32ConstantCache, Rosenbrock32Cache, - Rodas23WConstantCache, Rodas3PConstantCache, + Rodas23WConstantCache, Rodas23WCache, Rodas3PCache, RosenbrockCombinedConstantCache, RosenbrockCache} @@ -128,7 +128,7 @@ From MATLAB ODE Suite by Shampine """ @muladd function _ode_interpolant( - Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, Rodas23WConstantCache, Rodas3PConstantCache, RosenbrockCache, Rodas23WCache, Rodas3PCache}, + Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, Rodas23WConstantCache, RosenbrockCache, Rodas23WCache, Rodas3PCache}, idxs::Nothing, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ if !isdefined(cache, :order) || cache.order == 4 @@ -140,7 +140,7 @@ end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, - Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, + Rodas23WCache, Rodas3PCache}, idxs, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ if !isdefined(cache, :order) || cache.order == 4 @@ -152,7 +152,7 @@ end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, - Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, + Rodas23WCache, Rodas3PCache}, idxs::Nothing, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ if !isdefined(cache, :order) || cache.order == 4 @@ -165,7 +165,7 @@ end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, - Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, + Rodas23WCache, Rodas3PCache}, idxs, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ if !isdefined(cache, :order) || cache.order == 4 @@ -179,7 +179,7 @@ end # First Derivative @muladd function _ode_interpolant( - Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCache, Rodas23WCache, Rodas3PCache, RosenbrockCombinedConstantCache, Rodas23WConstantCache, Rodas3PConstantCache}, + Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCache, Rodas23WCache, Rodas3PCache, RosenbrockCombinedConstantCache, Rodas23WConstantCache}, idxs::Nothing, T::Type{Val{1}}, differential_vars) if !isdefined(cache, :order) || cache.order == 4 @.. (k[1] + Θ * (-2 * k[1] + 2 * k[2] - 3 * k[2] * Θ) - y₀ + y₁)/dt @@ -191,7 +191,7 @@ end end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, - Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, + Rodas23WCache, Rodas3PCache}, idxs, T::Type{Val{1}}, differential_vars) if !isdefined(cache, :order) || cache.order == 4 @views @.. (k[1][idxs] + Θ * (-2 * k[1][idxs] + 2 * k[2][idxs] - 3 * k[2][idxs] * Θ) - @@ -204,7 +204,7 @@ end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, - Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, + Rodas23WCache, Rodas3PCache}, idxs::Nothing, T::Type{Val{1}}, differential_vars) if !isdefined(cache, :order) || cache.order == 4 @.. out=(k[1] + Θ * (-2 * k[1] + 2 * k[2] - 3 * k[2] * Θ) - y₀ + y₁) / dt @@ -217,7 +217,7 @@ end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, - Rodas23WCache, Rodas3PConstantCache, Rodas3PCache}, + Rodas23WCache, Rodas3PCache}, idxs, T::Type{Val{1}}, differential_vars) if !isdefined(cache, :order) || cache.order == 4 @views @.. out=(k[1][idxs] + diff --git a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl index dd0702236d..a6e503c3bf 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl @@ -89,204 +89,6 @@ function _ode_addsteps!(k, t, uprev, u, dt, f, p, nothing end -function _ode_addsteps!( - k, t, uprev, u, dt, f, p, cache::Union{Rodas23WConstantCache, Rodas3PConstantCache}, - always_calc_begin = false, allow_calc_end = true, - force_calc_end = false) - if length(k) < 2 || always_calc_begin - @unpack tf, uf = cache - @unpack a21, a41, a42, a43, C21, C31, C32, C41, C42, C43, C51, C52, C53, C54, gamma, c2, c3, d1, d2, d3 = cache.tab - - # Precalculations - dtC21 = C21 / dt - dtC31 = C31 / dt - dtC32 = C32 / dt - dtC41 = C41 / dt - dtC42 = C42 / dt - dtC43 = C43 / dt - dtC51 = C51 / dt - dtC52 = C52 / dt - dtC53 = C53 / dt - dtC54 = C54 / dt - - dtd1 = dt * d1 - dtd2 = dt * d2 - dtd3 = dt * d3 - dtgamma = dt * gamma - mass_matrix = f.mass_matrix - - # Time derivative - tf.u = uprev - if cache.autodiff isa AutoForwardDiff - dT = ForwardDiff.derivative(tf, t) - else - dT = FiniteDiff.finite_difference_derivative(tf, t, dir = sign(dt)) - end - - # Jacobian - uf.t = t - if uprev isa AbstractArray - J = ForwardDiff.jacobian(uf, uprev) - W = mass_matrix / dtgamma - J - else - J = ForwardDiff.derivative(uf, uprev) - W = 1 / dtgamma - J - end - - du = f(uprev, p, t) - k3 = copy(du) - - linsolve_tmp = du + dtd1 * dT - - k1 = W \ linsolve_tmp - u = uprev + a21 * k1 - du = f(u, p, t + c2 * dt) - - linsolve_tmp = du + dtd2 * dT + dtC21 * k1 - - k2 = W \ linsolve_tmp - - linsolve_tmp = k3 + dtd3 * dT + (dtC31 * k1 + dtC32 * k2) - - k3 = W \ linsolve_tmp - u = uprev + a41 * k1 + a42 * k2 + a43 * k3 - du = f(u, p, t + dt) - - linsolve_tmp = du + (dtC41 * k1 + dtC42 * k2 + dtC43 * k3) - - k4 = W \ linsolve_tmp - - linsolve_tmp = du + (dtC52 * k2 + dtC54 * k4 + dtC51 * k1 + dtC53 * k3) - - k5 = W \ linsolve_tmp - - @unpack h21, h22, h23, h24, h25, h31, h32, h33, h34, h35, h2_21, h2_22, h2_23, h2_24, h2_25 = cache.tab - k₁ = h21 * k1 + h22 * k2 + h23 * k3 + h24 * k4 + h25 * k5 - k₂ = h31 * k1 + h32 * k2 + h33 * k3 + h34 * k4 + h35 * k5 - #k₃ = h2_21 * k1 + h2_22 * k2 + h2_23 * k3 + h2_24 * k4 + h2_25 * k5 - copyat_or_push!(k, 1, k₁) - copyat_or_push!(k, 2, k₂) - #copyat_or_push!(k, 3, k₃) - end - nothing -end - -function _ode_addsteps!( - k, t, uprev, u, dt, f, p, cache::Union{Rodas23WCache, Rodas3PCache}, - always_calc_begin = false, allow_calc_end = true, - force_calc_end = false) - if length(k) < 2 || always_calc_begin - @unpack du, du1, du2, tmp, k1, k2, k3, k4, k5, dT, J, W, uf, tf, linsolve_tmp, jac_config, fsalfirst, weight = cache - @unpack a21, a41, a42, a43, C21, C31, C32, C41, C42, C43, C51, C52, C53, C54, gamma, c2, c3, d1, d2, d3 = cache.tab - - # Assignments - sizeu = size(u) - uidx = eachindex(uprev) - mass_matrix = f.mass_matrix - - # Precalculations - dtC21 = C21 / dt - dtC31 = C31 / dt - dtC32 = C32 / dt - dtC41 = C41 / dt - dtC42 = C42 / dt - dtC43 = C43 / dt - dtC51 = C51 / dt - dtC52 = C52 / dt - dtC53 = C53 / dt - dtC54 = C54 / dt - - dtd1 = dt * d1 - dtd2 = dt * d2 - dtd3 = dt * d3 - dtgamma = dt * gamma - - @.. broadcast=false linsolve_tmp=@muladd fsalfirst + dtgamma * dT - - ### Jacobian does not need to be re-evaluated after an event - ### Since it's unchanged - jacobian2W!(W, mass_matrix, dtgamma, J, true) - - linsolve = cache.linsolve - - linres = dolinsolve(cache, linsolve; A = W, b = _vec(linsolve_tmp), - reltol = cache.reltol) - vecu = _vec(linres.u) - veck1 = _vec(k1) - - @.. broadcast=false veck1=-vecu - @.. broadcast=false tmp=uprev + a21 * k1 - f(du, tmp, p, t + c2 * dt) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + dtd2 * dT + dtC21 * k1 - else - @.. broadcast=false du1=dtC21 * k1 - mul!(du2, mass_matrix, du1) - @.. broadcast=false linsolve_tmp=du + dtd2 * dT + du2 - end - - linres = dolinsolve(cache, linres.cache; b = _vec(linsolve_tmp), - reltol = cache.reltol) - vecu = _vec(linres.u) - veck2 = _vec(k2) - @.. broadcast=false veck2=-vecu - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=fsalfirst + dtd3 * dT + - (dtC31 * k1 + dtC32 * k2) - else - @.. broadcast=false du1=dtC31 * k1 + dtC32 * k2 - mul!(du2, mass_matrix, du1) - @.. broadcast=false linsolve_tmp=fsalfirst + dtd3 * dT + du2 - end - - linres = dolinsolve(cache, linres.cache; b = _vec(linsolve_tmp), - reltol = cache.reltol) - vecu = _vec(linres.u) - veck3 = _vec(k3) - @.. broadcast=false veck3=-vecu - @.. broadcast=false tmp=uprev + a41 * k1 + a42 * k2 + a43 * k3 - f(du, tmp, p, t + dt) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + (dtC41 * k1 + dtC42 * k2 + dtC43 * k3) - else - @.. broadcast=false du1=dtC41 * k1 + dtC42 * k2 + dtC43 * k3 - mul!(du2, mass_matrix, du1) - @.. broadcast=false linsolve_tmp=du + du2 - end - - linres = dolinsolve(cache, linres.cache; b = _vec(linsolve_tmp), - reltol = cache.reltol) - vecu = _vec(linres.u) - veck4 = _vec(k4) - @.. broadcast=false veck4=-vecu - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=du + (dtC52 * k2 + dtC54 * k4 + dtC51 * k1 + - dtC53 * k3) - else - @.. broadcast=false du1=dtC52 * k2 + dtC54 * k4 + dtC51 * k1 + dtC53 * k3 - mul!(du2, mass_matrix, du1) - @.. broadcast=false linsolve_tmp=du + du2 - end - - linres = dolinsolve(cache, linres.cache; b = _vec(linsolve_tmp), - reltol = cache.reltol) - vecu = _vec(linres.u) - veck5 = _vec(k5) - @.. broadcast=false veck5=-vecu - @unpack h21, h22, h23, h24, h25, h31, h32, h33, h34, h35, h2_21, h2_22, h2_23, h2_24, h2_25 = cache.tab - @.. broadcast=false du=h21 * k1 + h22 * k2 + h23 * k3 + h24 * k4 + h25 * k5 - copyat_or_push!(k, 1, copy(du)) - - @.. broadcast=false du=h31 * k1 + h32 * k2 + h33 * k3 + h34 * k4 + h35 * k5 - copyat_or_push!(k, 2, copy(du)) - end - nothing -end - function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::RosenbrockCombinedConstantCache, always_calc_begin = false, allow_calc_end = true, force_calc_end = false) From f0a2ee329565d3a56aae658f39d526d9ba25c942 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Sun, 8 Sep 2024 01:28:20 +0530 Subject: [PATCH 37/56] Fixes further --- lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl b/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl index 2977c22c2e..57d5494282 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl @@ -11,8 +11,8 @@ end function DiffEqBase.interp_summary(::Type{cacheType}, dense::Bool) where { cacheType <: - Union{RosenbrockCombinedConstantCache, Rodas23WConstantCache, - RosenbrockCache, Rodas23WCache, Rodas3PCache}} + Union{RosenbrockCombinedConstantCache, + RosenbrockCache}} dense ? "specialized 3rd order \"free\" stiffness-aware interpolation" : "1st order linear" end From 31238d41e81ececb0370e0fc6972a41cb3242c50 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Sun, 8 Sep 2024 01:35:02 +0530 Subject: [PATCH 38/56] Some more fixes --- .../src/rosenbrock_interpolants.jl | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl index cf2e1cf9f9..479c1b8fe5 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl @@ -1,8 +1,6 @@ ### Fallbacks to capture ROSENBROCKS_WITH_INTERPOLATIONS = Union{Rosenbrock23ConstantCache, Rosenbrock23Cache, Rosenbrock32ConstantCache, Rosenbrock32Cache, - Rodas23WConstantCache, - Rodas23WCache, Rodas3PCache, RosenbrockCombinedConstantCache, RosenbrockCache} @@ -128,7 +126,7 @@ From MATLAB ODE Suite by Shampine """ @muladd function _ode_interpolant( - Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, Rodas23WConstantCache, RosenbrockCache, Rodas23WCache, Rodas3PCache}, + Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCombinedConstantCache, Rodas23WConstantCache, RosenbrockCache}, idxs::Nothing, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ if !isdefined(cache, :order) || cache.order == 4 @@ -139,8 +137,7 @@ From MATLAB ODE Suite by Shampine end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, - Rodas23WCache, Rodas3PCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ if !isdefined(cache, :order) || cache.order == 4 @@ -151,8 +148,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, - Rodas23WCache, Rodas3PCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs::Nothing, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ if !isdefined(cache, :order) || cache.order == 4 @@ -164,8 +160,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, - Rodas23WCache, Rodas3PCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs, T::Type{Val{0}}, differential_vars) Θ1 = 1 - Θ if !isdefined(cache, :order) || cache.order == 4 @@ -179,7 +174,7 @@ end # First Derivative @muladd function _ode_interpolant( - Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCache, Rodas23WCache, Rodas3PCache, RosenbrockCombinedConstantCache, Rodas23WConstantCache}, + Θ, dt, y₀, y₁, k, cache::Union{RosenbrockCache, RosenbrockCombinedConstantCache}, idxs::Nothing, T::Type{Val{1}}, differential_vars) if !isdefined(cache, :order) || cache.order == 4 @.. (k[1] + Θ * (-2 * k[1] + 2 * k[2] - 3 * k[2] * Θ) - y₀ + y₁)/dt @@ -190,8 +185,7 @@ end end end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, - Rodas23WCache, Rodas3PCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs, T::Type{Val{1}}, differential_vars) if !isdefined(cache, :order) || cache.order == 4 @views @.. (k[1][idxs] + Θ * (-2 * k[1][idxs] + 2 * k[2][idxs] - 3 * k[2][idxs] * Θ) - @@ -203,8 +197,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, - Rodas23WCache, Rodas3PCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs::Nothing, T::Type{Val{1}}, differential_vars) if !isdefined(cache, :order) || cache.order == 4 @.. out=(k[1] + Θ * (-2 * k[1] + 2 * k[2] - 3 * k[2] * Θ) - y₀ + y₁) / dt @@ -216,8 +209,7 @@ end end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, - cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache, Rodas23WConstantCache, - Rodas23WCache, Rodas3PCache}, + cache::Union{RosenbrockCombinedConstantCache, RosenbrockCache}, idxs, T::Type{Val{1}}, differential_vars) if !isdefined(cache, :order) || cache.order == 4 @views @.. out=(k[1][idxs] + From 13273d3b1529641f07df8b59b1486c0dbc1d625f Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Sun, 8 Sep 2024 07:34:34 +0530 Subject: [PATCH 39/56] Fixes --- .../src/rosenbrock_perform_step.jl | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index e47a1e6f0f..03c496dc8b 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -762,7 +762,7 @@ end @muladd function perform_step!(integrator, cache::RosenbrockCache, repeat_step = false) (;t, dt, uprev, u, f, p) = integrator (;du, du1, du2, dT, J, W, uf, tf, ks, linsolve_tmp, jac_config, atmp, weight, stage_limiter!, step_limiter!) = cache - (;A, C, gamma, b, btilde, c, d, H) = cache.tab + (;A, C, gamma, c, d, H) = cache.tab # Assignments sizeu = size(u) @@ -770,6 +770,16 @@ end mass_matrix = integrator.f.mass_matrix utilde = du + b_exists = hasproperty(cache.tab, :b) + btilde_exists = hasproperty(cache.tab, :btilde) + + if b_exists + b = cache.tab.b + end + if btilde_exists + btilde = cache.tab.btilde + end + # Precalculations dtC = C .* inv(dt) dtd = dt .* d From 48b5fa3d9d55a12bbd6d9ce10e5cc6631b6bfe15 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Sun, 8 Sep 2024 17:28:04 +0530 Subject: [PATCH 40/56] Fixes --- .../src/rosenbrock_perform_step.jl | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index 03c496dc8b..5886266209 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -701,7 +701,7 @@ end integrator.k[j] = @.. integrator.k[j] + H[j, i] * ks[i] end end - integrator.k[3] = h2_21 * k1 + h2_22 * k2 + h2_23 * k3 + h2_24 * k4 + h2_25 * k5 + integrator.k[3] = h2_2[1] * k1 + h2_2[2] * k2 + h2_2[3] * k3 + h2_2[4] * k4 + h2_2[5] * k5 if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && (integrator.EEst < 1.0) k2 = 0.5 * (uprev + u + @@ -770,14 +770,11 @@ end mass_matrix = integrator.f.mass_matrix utilde = du - b_exists = hasproperty(cache.tab, :b) - btilde_exists = hasproperty(cache.tab, :btilde) - - if b_exists - b = cache.tab.b + if hasproperty(cache.tab, :b) + (;b) = cache.tab.b end - if btilde_exists - btilde = cache.tab.btilde + if hasproperty(cache.tab, :btilde) + (;btilde) = cache.tab.btilde end # Precalculations From 8e0d88c95a797d81214b3a6bda21589e1ac96cbf Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 10 Sep 2024 00:56:43 +0530 Subject: [PATCH 41/56] Updates --- .../src/rosenbrock_perform_step.jl | 18 ++++++++++++++---- .../src/rosenbrock_tableaus.jl | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index 5886266209..2bc382069d 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -631,6 +631,12 @@ end (;tf, uf) = cache (;A, C, gamma, c, d, H) = cache.tab + if !isdefined(cache, :order) || cache.order == 3 + (;A, C, gamma, h2_2, c, d, H) = cache.tab + else + (;A, C, gamma, c, d, H) = cache.tab + end + # Precalculations dtC = C ./ dt dtd = dt .* d @@ -701,7 +707,7 @@ end integrator.k[j] = @.. integrator.k[j] + H[j, i] * ks[i] end end - integrator.k[3] = h2_2[1] * k1 + h2_2[2] * k2 + h2_2[3] * k3 + h2_2[4] * k4 + h2_2[5] * k5 + integrator.k[3] = h2_21 * k1 + h2_22 * k2 + h2_23 * k3 + h2_24 * k4 + h2_25 * k5 if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && (integrator.EEst < 1.0) k2 = 0.5 * (uprev + u + @@ -762,8 +768,12 @@ end @muladd function perform_step!(integrator, cache::RosenbrockCache, repeat_step = false) (;t, dt, uprev, u, f, p) = integrator (;du, du1, du2, dT, J, W, uf, tf, ks, linsolve_tmp, jac_config, atmp, weight, stage_limiter!, step_limiter!) = cache - (;A, C, gamma, c, d, H) = cache.tab + if !isdefined(cache, :order) || cache.order == 3 + (;A, C, gamma, b, c, d, H) = cache.tab + else + (;A, C, gamma, c, d, H) = cache.tab + end # Assignments sizeu = size(u) uidx = eachindex(integrator.uprev) @@ -771,10 +781,10 @@ end utilde = du if hasproperty(cache.tab, :b) - (;b) = cache.tab.b + b = cache.tab.b end if hasproperty(cache.tab, :btilde) - (;btilde) = cache.tab.btilde + btilde = cache.tab.btilde end # Precalculations diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl index 479cf4a0ed..8077d83873 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl @@ -93,7 +93,7 @@ struct Rodas3PTableau{T, T2} c::Vector{T2} d::Vector{T} h::Matrix{T} - h2::Vector{T} + h2_2::Vector{T} end function Rodas3PTableau(T, T2) From f325a99a107a7b05cd07d8d94f19f1d80976a371 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 10 Sep 2024 01:07:06 +0530 Subject: [PATCH 42/56] Fixes --- .../src/rosenbrock_perform_step.jl | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index 2bc382069d..614c218a1e 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -768,24 +768,21 @@ end @muladd function perform_step!(integrator, cache::RosenbrockCache, repeat_step = false) (;t, dt, uprev, u, f, p) = integrator (;du, du1, du2, dT, J, W, uf, tf, ks, linsolve_tmp, jac_config, atmp, weight, stage_limiter!, step_limiter!) = cache - - if !isdefined(cache, :order) || cache.order == 3 - (;A, C, gamma, b, c, d, H) = cache.tab - else - (;A, C, gamma, c, d, H) = cache.tab - end - # Assignments - sizeu = size(u) - uidx = eachindex(integrator.uprev) - mass_matrix = integrator.f.mass_matrix - utilde = du + (;A, C, gamma, c, d, H) = cache.tab if hasproperty(cache.tab, :b) b = cache.tab.b end + if hasproperty(cache.tab, :btilde) btilde = cache.tab.btilde end + + # Assignments + sizeu = size(u) + uidx = eachindex(integrator.uprev) + mass_matrix = integrator.f.mass_matrix + utilde = du # Precalculations dtC = C .* inv(dt) From f2da4bfec6cda994ec8755ac37c2390cc07b4188 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 10 Sep 2024 01:25:15 +0530 Subject: [PATCH 43/56] Fixes --- lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index 614c218a1e..8e9513ce5c 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -777,7 +777,6 @@ end if hasproperty(cache.tab, :btilde) btilde = cache.tab.btilde end - # Assignments sizeu = size(u) uidx = eachindex(integrator.uprev) @@ -843,7 +842,9 @@ end du .= ks[end] u .+= ks[end] - @.. u=uprev + b[1] * ks[1] + b[2] * ks[2] + b[3] * ks[3] + if hasproperty(cache.tab, :b) + @.. u=uprev + b[1] * ks[1] + b[2] * ks[2] + b[3] * ks[3] + end step_limiter!(u, integrator, p, t + dt) From 58860fb3b5d6c30f5baaa1035da51da4929ceb5c Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 10 Sep 2024 01:31:13 +0530 Subject: [PATCH 44/56] Fixes --- lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index 8e9513ce5c..dcb0047e28 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -707,7 +707,7 @@ end integrator.k[j] = @.. integrator.k[j] + H[j, i] * ks[i] end end - integrator.k[3] = h2_21 * k1 + h2_22 * k2 + h2_23 * k3 + h2_24 * k4 + h2_25 * k5 + integrator.k[3] = h2_2[1] * k1 + h2_2[2] * k2 + h2_2[3] * k3 + h2_2[4] * k4 + h2_2[5] * k5 if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && (integrator.EEst < 1.0) k2 = 0.5 * (uprev + u + From 77412b84b72bb8199aa45a3ec86b338f70344cc9 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 10 Sep 2024 01:37:18 +0530 Subject: [PATCH 45/56] Fixes --- .../src/rosenbrock_perform_step.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index dcb0047e28..4ad64d0b00 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -631,10 +631,8 @@ end (;tf, uf) = cache (;A, C, gamma, c, d, H) = cache.tab - if !isdefined(cache, :order) || cache.order == 3 - (;A, C, gamma, h2_2, c, d, H) = cache.tab - else - (;A, C, gamma, c, d, H) = cache.tab + if hasproperty(cache.tab, :h2_2) + h2_2 = cache.tab.h2_2 end # Precalculations @@ -707,7 +705,9 @@ end integrator.k[j] = @.. integrator.k[j] + H[j, i] * ks[i] end end - integrator.k[3] = h2_2[1] * k1 + h2_2[2] * k2 + h2_2[3] * k3 + h2_2[4] * k4 + h2_2[5] * k5 + if hasproperty(cache.tab, :h2_2) + integrator.k[3] = h2_2[1] * k1 + h2_2[2] * k2 + h2_2[3] * k3 + h2_2[4] * k4 + h2_2[5] * k5 + end if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && (integrator.EEst < 1.0) k2 = 0.5 * (uprev + u + From 669260c110e674bc8bce3b381bdebefa492d8ec0 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 10 Sep 2024 01:43:12 +0530 Subject: [PATCH 46/56] Fixes --- lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index 4ad64d0b00..f7cfce33e7 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -849,7 +849,10 @@ end step_limiter!(u, integrator, p, t + dt) if integrator.opts.adaptive - @.. utilde=btilde[1] * ks[1] + btilde[2] * ks[2] + btilde[3] * ks[3] + if hasproperty(cache.tab, :btilde) + @.. utilde=btilde[1] * ks[1] + btilde[2] * ks[2] + btilde[3] * ks[3] + end + if (integrator.alg isa Rodas5Pe) @. du = 0.2606326497975715 * ks[1] - 0.005158627295444251 * ks[2] + 1.3038988631109731 * ks[3] + 1.235000722062074 * ks[4] + From f68187b38a4f25c41ffc23ceb587c0b07933e02e Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 10 Sep 2024 08:07:58 +0530 Subject: [PATCH 47/56] Fixes --- .../src/rosenbrock_perform_step.jl | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index f7cfce33e7..dccfeef9f3 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -705,11 +705,11 @@ end integrator.k[j] = @.. integrator.k[j] + H[j, i] * ks[i] end end - if hasproperty(cache.tab, :h2_2) + if hasproperty(cache.tab, :h2_2) && integrator.kshortsize == 3 integrator.k[3] = h2_2[1] * k1 + h2_2[2] * k2 + h2_2[3] * k3 + h2_2[4] * k4 + h2_2[5] * k5 end if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && - (integrator.EEst < 1.0) + (integrator.EEst < 1.0) && integrator.kshortsize == 3 k2 = 0.5 * (uprev + u + 0.5 * (integrator.k[1] + 0.5 * (integrator.k[2] + 0.5 * integrator.k[3]))) du1 = (0.25 * (integrator.k[2] + integrator.k[3]) - uprev + u) / dt @@ -724,7 +724,7 @@ end integrator.EEst = max(EEst, integrator.EEst) end - if integrator.opts.adaptive + if integrator.opts.adaptive && integrator.kshortsize == 3 if isa(linsolve_tmp, AbstractFloat) u_int, u_diff = calculate_interpoldiff( uprev, du, u, integrator.k[1], integrator.k[2], integrator.k[3]) @@ -739,7 +739,7 @@ end EEst = max(EEst, integrator.opts.internalnorm(atmp, t)) #-- role of t unclear end - if (integrator.alg isa Rodas23W) + if (integrator.alg isa Rodas23W) && integrator.kshortsize == 3 k1 = u .+ 0 u = du .+ 0 du = k1 .+ 0 @@ -883,14 +883,16 @@ end end end if integrator.opts.adaptive - calculate_interpoldiff!( - du1, du2, uprev, du, u, integrator.k[1], integrator.k[2], integrator.k[3]) + if integrator.kshortsize == 3 + calculate_interpoldiff!( + du1, du2, uprev, du, u, integrator.k[1], integrator.k[2], integrator.k[3]) + end calculate_residuals!(atmp, du2, uprev, du1, integrator.opts.abstol, integrator.opts.reltol, integrator.opts.internalnorm, t) EEst = max(EEst, integrator.opts.internalnorm(atmp, t)) #-- role of t unclear end if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && - (integrator.EEst < 1.0) + (integrator.EEst < 1.0) && integrator.kshortsize == 3 ks[2] = 0.5 * (uprev + u + 0.5 * (integrator.k[1] + 0.5 * (integrator.k[2] + 0.5 * integrator.k[3]))) du1 = (0.25 * (integrator.k[2] + integrator.k[3]) - uprev + u) / dt From 865d8c4a58e68c56bdf1d9f2763a554f3201982f Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Tue, 10 Sep 2024 17:02:53 +0530 Subject: [PATCH 48/56] Fixes --- lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index dccfeef9f3..fe3f5e0ed5 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -854,7 +854,7 @@ end end if (integrator.alg isa Rodas5Pe) - @. du = 0.2606326497975715 * ks[1] - 0.005158627295444251 * ks[2] + + @.. du = 0.2606326497975715 * ks[1] - 0.005158627295444251 * ks[2] + 1.3038988631109731 * ks[3] + 1.235000722062074 * ks[4] + -0.7931985603795049 * ks[5] - 1.005448461135913 * ks[6] - 0.18044626132120234 * ks[7] + 0.17051519239113755 * ks[8] @@ -889,7 +889,7 @@ end end calculate_residuals!(atmp, du2, uprev, du1, integrator.opts.abstol, integrator.opts.reltol, integrator.opts.internalnorm, t) - EEst = max(EEst, integrator.opts.internalnorm(atmp, t)) #-- role of t unclear + integrator.EEst = max(integrator.EEst, integrator.opts.internalnorm(atmp, t)) #-- role of t unclear end if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && (integrator.EEst < 1.0) && integrator.kshortsize == 3 From 8c3afc4a9f86bde11753a70c2c923a6d9db56ee1 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Sat, 14 Sep 2024 09:10:18 +0530 Subject: [PATCH 49/56] Fixes --- lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl index 8077d83873..4b700cd4a0 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_tableaus.jl @@ -49,7 +49,7 @@ function ROS3PTableau(T, T2) # btilde1 = convert(T,2.113248654051871) # btilde2 = convert(T,1.000000000000000) # btilde3 = convert(T,0.4226497308103742) - btilde = [(b1 - convert(T, 2.113248654051871)), (b2 - convert(T, 1.000000000000000)), (b3 - convert(T, 0.4226497308103742))] + btilde = [(convert(T, 2.113248654051871)), (convert(T, 1.000000000000000)), (convert(T, 0.4226497308103742))] c = T[1, 1] d = T[0.7886751345948129, -0.2113248654051871, -1.077350269189626] ROS3PTableau(A, C, b, btilde, gamma, c, d) From b7e57fe0bde4e8292350d43313506ea1b94d6ec9 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Sun, 15 Sep 2024 09:08:07 +0530 Subject: [PATCH 50/56] Fixes done --- .../src/rosenbrock_caches.jl | 101 ++++++++---------- 1 file changed, 43 insertions(+), 58 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl index f5e0a45f7f..e55b7a4d2e 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl @@ -12,13 +12,14 @@ end # Shampine's Low-order Rosenbrocks mutable struct RosenbrockCache{uType, rateType, uNoUnitsType, JType, WType, TabType, - TFType, UFType, F, JCType, GCType, RTolType, A, StepLimiter, StageLimiter} <: RosenbrockMutableCache + TFType, UFType, F, JCType, GCType, RTolType, A, AV, StepLimiter, StageLimiter} <: RosenbrockMutableCache u::uType uprev::uType dense::Vector{rateType} du::rateType du1::rateType du2::rateType + f₁::rateType ks::Vector{rateType} fsalfirst::rateType fsallast::rateType @@ -37,6 +38,7 @@ mutable struct RosenbrockCache{uType, rateType, uNoUnitsType, JType, WType, TabT grad_config::GCType reltol::RTolType alg::A + algebraic_vars::AV step_limiter!::StepLimiter stage_limiter!::StageLimiter order::Int @@ -57,39 +59,6 @@ struct RosenbrockCombinedConstantCache{TF, UF, Tab, JType, WType, F, AD} <: Rose order::Int end -@cache mutable struct Rosenbrock23Cache{uType, rateType, uNoUnitsType, JType, WType, - TabType, TFType, UFType, F, JCType, GCType, - RTolType, A, AV, StepLimiter, StageLimiter} <: RosenbrockMutableCache - u::uType - uprev::uType - k₁::rateType - k₂::rateType - k₃::rateType - du1::rateType - du2::rateType - f₁::rateType - fsalfirst::rateType - fsallast::rateType - dT::rateType - J::JType - W::WType - tmp::rateType - atmp::uNoUnitsType - weight::uNoUnitsType - tab::TabType - tf::TFType - uf::UFType - linsolve_tmp::rateType - linsolve::F - jac_config::JCType - grad_config::GCType - reltol::RTolType - alg::A - algebraic_vars::AV - step_limiter!::StepLimiter - stage_limiter!::StageLimiter -end - @cache mutable struct Rosenbrock32Cache{uType, rateType, uNoUnitsType, JType, WType, TabType, TFType, UFType, F, JCType, GCType, RTolType, A, AV, StepLimiter, StageLimiter} <: RosenbrockMutableCache @@ -127,11 +96,10 @@ function alg_cache(alg::Rosenbrock23, u, rate_prototype, ::Type{uEltypeNoUnits}, ::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck, ::Val{true}) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits} - k₁ = zero(rate_prototype) - k₂ = zero(rate_prototype) - k₃ = zero(rate_prototype) + ks = [zero(rate_prototype) for _ in 1:3] du1 = zero(rate_prototype) du2 = zero(rate_prototype) + dense = Vector{rate_prototype}(undef, 0) # f₀ = zero(u) fsalfirst f₁ = zero(rate_prototype) fsalfirst = zero(rate_prototype) @@ -161,11 +129,11 @@ function alg_cache(alg::Rosenbrock23, u, rate_prototype, ::Type{uEltypeNoUnits}, algebraic_vars = f.mass_matrix === I ? nothing : [all(iszero, x) for x in eachcol(f.mass_matrix)] - Rosenbrock23Cache(u, uprev, k₁, k₂, k₃, du1, du2, f₁, + RosenbrockCache(u, uprev, dense, du, ks, du1, du2, f₁, fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp, linsolve, jac_config, grad_config, reltol, alg, algebraic_vars, alg.step_limiter!, - alg.stage_limiter!) + alg.stage_limiter!, 2) end function alg_cache(alg::Rosenbrock32, u, rate_prototype, ::Type{uEltypeNoUnits}, @@ -283,6 +251,7 @@ function alg_cache(alg::ROS3P, u, rate_prototype, ::Type{uEltypeNoUnits}, du1 = zero(rate_prototype) du2 = zero(rate_prototype) dense = Vector{rate_prototype}(undef, 0) + f₁ = zero(rate_prototype) ks = [zero(rate_prototype) for _ in 1:4] fsalfirst = zero(rate_prototype) fsallast = zero(rate_prototype) @@ -306,9 +275,12 @@ function alg_cache(alg::ROS3P, u, rate_prototype, ::Type{uEltypeNoUnits}, assumptions = LinearSolve.OperatorAssumptions(true)) grad_config = build_grad_config(alg, f, tf, du1, t) jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, du2) - RosenbrockCache(u, uprev, dense, du, du1, du2, ks, fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, + algebraic_vars = f.mass_matrix === I ? nothing : + [all(iszero, x) for x in eachcol(f.mass_matrix)] + + RosenbrockCache(u, uprev, dense, du, du1, du2, ks, f₁, fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp, - linsolve, jac_config, grad_config, reltol, alg, alg.step_limiter!, + linsolve, jac_config, grad_config, reltol, alg, algebraic_vars, alg.step_limiter!, alg.stage_limiter!, 3) end @@ -335,6 +307,7 @@ function alg_cache(alg::Rodas3, u, rate_prototype, ::Type{uEltypeNoUnits}, du2 = zero(rate_prototype) ks = [zero(rate_prototype) for _ in 1:4] dense = Vector{rate_prototype}(undef, 0) + f₁ = zero(rate_prototype) fsalfirst = zero(rate_prototype) fsallast = zero(rate_prototype) dT = zero(rate_prototype) @@ -358,10 +331,12 @@ function alg_cache(alg::Rodas3, u, rate_prototype, ::Type{uEltypeNoUnits}, assumptions = LinearSolve.OperatorAssumptions(true)) grad_config = build_grad_config(alg, f, tf, du1, t) jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, du2) - RosenbrockCache(u, uprev, dense, du, du1, du2, ks, - fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, + + algebraic_vars = f.mass_matrix === I ? nothing : + [all(iszero, x) for x in eachcol(f.mass_matrix)] + RosenbrockCache(u, uprev, dense, du, du1, du2, ks, f₁, fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp, - linsolve, jac_config, grad_config, alg.step_limiter!, + linsolve, jac_config, grad_config, reltol, alg, algebraic_vars, alg.step_limiter!, alg.stage_limiter!, 3) end @@ -518,6 +493,7 @@ function alg_cache(alg::Rodas3P, u, rate_prototype, ::Type{uEltypeNoUnits}, du = zero(rate_prototype) du1 = zero(rate_prototype) du2 = zero(rate_prototype) + f₁ = zero(rate_prototype) ks = [zero(rate_prototype) for _ in 1:5] fsalfirst = zero(rate_prototype) fsallast = zero(rate_prototype) @@ -542,9 +518,9 @@ function alg_cache(alg::Rodas3P, u, rate_prototype, ::Type{uEltypeNoUnits}, assumptions = LinearSolve.OperatorAssumptions(true)) grad_config = build_grad_config(alg, f, tf, du1, t) jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, du2) - RosenbrockCache(u, uprev, dense, du, du1, du2, ks, - fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp, - linsolve, jac_config, grad_config, reltol, alg, alg.step_limiter!, + RosenbrockCache(u, uprev, dense, du, du1, du2, ks, f₁, fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, + linsolve_tmp, + linsolve, jac_config, grad_config, reltol, alg, algebraic_vars, alg.step_limiter!, alg.stage_limiter!, 3) end @@ -611,6 +587,8 @@ function alg_cache(alg::Union{Rodas4, Rodas42, Rodas4P, Rodas4P2}, u, rate_proto du1 = zero(rate_prototype) du2 = zero(rate_prototype) + f₁ = zero(rate_prototype) + # Initialize other variables fsalfirst = zero(rate_prototype) fsallast = zero(rate_prototype) @@ -643,12 +621,14 @@ function alg_cache(alg::Union{Rodas4, Rodas42, Rodas4P, Rodas4P2}, u, rate_proto grad_config = build_grad_config(alg, f, tf, du1, t) jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, du2) + algebraic_vars = f.mass_matrix === I ? nothing : + [all(iszero, x) for x in eachcol(f.mass_matrix)] + # Return the cache struct with vectors - RosenbrockCache( - u, uprev, dense, du, du1, du2, ks, fsalfirst, fsallast, - dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp, - linsolve, jac_config, grad_config, reltol, alg, - alg.step_limiter!, alg.stage_limiter!, 4) + RosenbrockCache(u, uprev, dense, du, du1, du2, ks, f₁, fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, + linsolve_tmp, + linsolve, jac_config, grad_config, reltol, alg, algebraic_vars, alg.step_limiter!, + alg.stage_limiter!, 4) end ################################################################################ @@ -664,6 +644,7 @@ function alg_cache(alg::Rodas5, u, rate_prototype, ::Type{uEltypeNoUnits}, du1 = zero(rate_prototype) du2 = zero(rate_prototype) ks = [zero(rate_prototype) for _ in 1:7] + f₁ = zero(rate_prototype) fsalfirst = zero(rate_prototype) fsallast = zero(rate_prototype) dT = zero(rate_prototype) @@ -687,10 +668,11 @@ function alg_cache(alg::Rodas5, u, rate_prototype, ::Type{uEltypeNoUnits}, assumptions = LinearSolve.OperatorAssumptions(true)) grad_config = build_grad_config(alg, f, tf, du1, t) jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, du2) - RosenbrockCache(u, uprev, dense, du, du1, du2, ks, - fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, + algebraic_vars = f.mass_matrix === I ? nothing : + [all(iszero, x) for x in eachcol(f.mass_matrix)] + RosenbrockCache(u, uprev, dense, du, du1, du2, ks, f₁, fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp, - linsolve, jac_config, grad_config, reltol, alg, alg.step_limiter!, + linsolve, jac_config, grad_config, reltol, alg, algebraic_vars, alg.step_limiter!, alg.stage_limiter!, 5) end @@ -718,6 +700,8 @@ function alg_cache( du1 = zero(rate_prototype) du2 = zero(rate_prototype) ks = [zero(rate_prototype) for _ in 1:8] + + f₁ = zero(rate_prototype) fsalfirst = zero(rate_prototype) fsallast = zero(rate_prototype) dT = zero(rate_prototype) @@ -741,10 +725,11 @@ function alg_cache( assumptions = LinearSolve.OperatorAssumptions(true)) grad_config = build_grad_config(alg, f, tf, du1, t) jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, du2) - RosenbrockCache(u, uprev, dense, du, du1, du2, ks, - fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, + algebraic_vars = f.mass_matrix === I ? nothing : + [all(iszero, x) for x in eachcol(f.mass_matrix)] + RosenbrockCache(u, uprev, dense, du, du1, du2, ks, f₁, fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp, - linsolve, jac_config, grad_config, reltol, alg, alg.step_limiter!, + linsolve, jac_config, grad_config, reltol, alg, algebraic_vars, alg.step_limiter!, alg.stage_limiter!, 5) end From e7463ac1341a28aa6026f333467271f9a8a5ff5f Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Sun, 15 Sep 2024 09:14:13 +0530 Subject: [PATCH 51/56] Fixes --- .../src/interp_func.jl | 2 +- .../src/rosenbrock_caches.jl | 2 +- .../src/rosenbrock_interpolants.jl | 18 +++++++++--------- .../src/rosenbrock_perform_step.jl | 4 ++-- .../src/stiff_addsteps.jl | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl b/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl index 57d5494282..0fcf67e4d8 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl @@ -3,7 +3,7 @@ function DiffEqBase.interp_summary(::Type{cacheType}, cacheType <: Union{Rosenbrock23ConstantCache, Rosenbrock32ConstantCache, - Rosenbrock23Cache, + RosenbrockCache, Rosenbrock32Cache}} dense ? "specialized 2nd order \"free\" stiffness-aware interpolation" : "1st order linear" diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl index e55b7a4d2e..3ccdd81fd2 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl @@ -749,7 +749,7 @@ function alg_cache( end function get_fsalfirstlast( - cache::Union{Rosenbrock23Cache, Rosenbrock32Cache, RosenbrockCache, + cache::Union{RosenbrockCache, Rosenbrock32Cache, RosenbrockCache, Rosenbrock4Cache}, u) (cache.fsalfirst, cache.fsallast) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl index 479c1b8fe5..aad93ff71e 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl @@ -1,5 +1,5 @@ ### Fallbacks to capture -ROSENBROCKS_WITH_INTERPOLATIONS = Union{Rosenbrock23ConstantCache, Rosenbrock23Cache, +ROSENBROCKS_WITH_INTERPOLATIONS = Union{Rosenbrock23ConstantCache, RosenbrockCache, Rosenbrock32ConstantCache, Rosenbrock32Cache, RosenbrockCombinedConstantCache, RosenbrockCache} @@ -44,14 +44,14 @@ end end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock23Cache, Rosenbrock32Cache}, + cache::Union{RosenbrockCache, Rosenbrock32Cache}, idxs::Nothing, T::Type{Val{0}}, differential_vars) @rosenbrock2332pre0 @inbounds @.. y₀+dt * (c1 * k[1] + c2 * k[2]) end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock23ConstantCache, Rosenbrock23Cache, + cache::Union{Rosenbrock23ConstantCache, RosenbrockCache, Rosenbrock32ConstantCache, Rosenbrock32Cache }, idxs, T::Type{Val{0}}, differential_vars) @rosenbrock2332pre0 @@ -60,7 +60,7 @@ end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, cache::Union{Rosenbrock23ConstantCache, - Rosenbrock23Cache, + RosenbrockCache, Rosenbrock32ConstantCache, Rosenbrock32Cache }, idxs::Nothing, T::Type{Val{0}}, differential_vars) @rosenbrock2332pre0 @@ -70,7 +70,7 @@ end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, cache::Union{Rosenbrock23ConstantCache, - Rosenbrock23Cache, + RosenbrockCache, Rosenbrock32ConstantCache, Rosenbrock32Cache }, idxs, T::Type{Val{0}}, differential_vars) @rosenbrock2332pre0 @@ -86,7 +86,7 @@ end end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock23ConstantCache, Rosenbrock23Cache, + cache::Union{Rosenbrock23ConstantCache, RosenbrockCache, Rosenbrock32ConstantCache, Rosenbrock32Cache }, idxs::Nothing, T::Type{Val{1}}, differential_vars) @rosenbrock2332pre1 @@ -94,7 +94,7 @@ end end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{Rosenbrock23ConstantCache, Rosenbrock23Cache, + cache::Union{Rosenbrock23ConstantCache, RosenbrockCache, Rosenbrock32ConstantCache, Rosenbrock32Cache }, idxs, T::Type{Val{1}}, differential_vars) @rosenbrock2332pre1 @@ -103,7 +103,7 @@ end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, cache::Union{Rosenbrock23ConstantCache, - Rosenbrock23Cache, + RosenbrockCache, Rosenbrock32ConstantCache, Rosenbrock32Cache }, idxs::Nothing, T::Type{Val{1}}, differential_vars) @rosenbrock2332pre1 @@ -113,7 +113,7 @@ end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, cache::Union{Rosenbrock23ConstantCache, - Rosenbrock23Cache, + RosenbrockCache, Rosenbrock32ConstantCache, Rosenbrock32Cache }, idxs, T::Type{Val{1}}, differential_vars) @rosenbrock2332pre1 diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index 8510a68aeb..aea4bfe1a6 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -1,4 +1,4 @@ -function initialize!(integrator, cache::Union{Rosenbrock23Cache, +function initialize!(integrator, cache::Union{RosenbrockCache, Rosenbrock32Cache}) integrator.kshortsize = 2 @unpack k₁, k₂, fsalfirst, fsallast = cache @@ -23,7 +23,7 @@ function initialize!(integrator, integrator.k[2] = zero(integrator.fsalfirst) end -@muladd function perform_step!(integrator, cache::Rosenbrock23Cache, repeat_step = false) +@muladd function perform_step!(integrator, cache::RosenbrockCache, repeat_step = false) @unpack t, dt, uprev, u, f, p, opts = integrator @unpack k₁, k₂, k₃, du1, du2, f₁, fsalfirst, fsallast, dT, J, W, tmp, uf, tf, linsolve_tmp, jac_config, atmp, weight, stage_limiter!, step_limiter! = cache @unpack c₃₂, d = cache.tab diff --git a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl index 47eb570c01..220fa9f936 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl @@ -44,7 +44,7 @@ function _ode_addsteps!(k, t, uprev, u, dt, f, p, end function _ode_addsteps!(k, t, uprev, u, dt, f, p, - cache::Union{Rosenbrock23Cache, Rosenbrock32Cache}, + cache::Union{RosenbrockCache, Rosenbrock32Cache}, always_calc_begin = false, allow_calc_end = true, force_calc_end = false) if length(k) < 2 || always_calc_begin From 6880a2d3bb509770ea8b601c24dd277846d739a5 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Mon, 16 Sep 2024 00:01:24 +0530 Subject: [PATCH 52/56] Fixes --- lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl index 3ccdd81fd2..a5b24047a5 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl @@ -129,7 +129,8 @@ function alg_cache(alg::Rosenbrock23, u, rate_prototype, ::Type{uEltypeNoUnits}, algebraic_vars = f.mass_matrix === I ? nothing : [all(iszero, x) for x in eachcol(f.mass_matrix)] - RosenbrockCache(u, uprev, dense, du, ks, du1, du2, f₁, + RosenbrockCache(u, uprev, dense, du, du1, du2, f₁, + ks, fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp, linsolve, jac_config, grad_config, reltol, alg, algebraic_vars, alg.step_limiter!, From 9fa571a2b207694c6d5e499271d9fca08ab38f09 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Mon, 16 Sep 2024 19:31:06 +0530 Subject: [PATCH 53/56] Combined caches --- .../src/rosenbrock_caches.jl | 105 +- .../src/rosenbrock_interpolants.jl | 19 +- .../src/rosenbrock_perform_step.jl | 1362 ++++++++--------- 3 files changed, 666 insertions(+), 820 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl index 6339394467..51b7a1f6fb 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_caches.jl @@ -59,39 +59,6 @@ struct RosenbrockCombinedConstantCache{TF, UF, Tab, JType, WType, F, AD} <: Rose order::Int end -@cache mutable struct Rosenbrock32Cache{uType, rateType, uNoUnitsType, JType, WType, - TabType, TFType, UFType, F, JCType, GCType, - RTolType, A, AV, StepLimiter, StageLimiter} <: RosenbrockMutableCache - u::uType - uprev::uType - k₁::rateType - k₂::rateType - k₃::rateType - du1::rateType - du2::rateType - f₁::rateType - fsalfirst::rateType - fsallast::rateType - dT::rateType - J::JType - W::WType - tmp::rateType - atmp::uNoUnitsType - weight::uNoUnitsType - tab::TabType - tf::TFType - uf::UFType - linsolve_tmp::rateType - linsolve::F - jac_config::JCType - grad_config::GCType - reltol::RTolType - alg::A - algebraic_vars::AV - step_limiter!::StepLimiter - stage_limiter!::StageLimiter -end - function alg_cache(alg::Rosenbrock23, u, rate_prototype, ::Type{uEltypeNoUnits}, ::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck, @@ -141,11 +108,10 @@ function alg_cache(alg::Rosenbrock32, u, rate_prototype, ::Type{uEltypeNoUnits}, ::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck, ::Val{true}) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits} - k₁ = zero(rate_prototype) - k₂ = zero(rate_prototype) - k₃ = zero(rate_prototype) + ks = [zero(rate_prototype) for _ in 1:3] du1 = zero(rate_prototype) du2 = zero(rate_prototype) + dense = Vector{rate_prototype}(undef, 0) # f₀ = zero(u) fsalfirst f₁ = zero(rate_prototype) fsalfirst = zero(rate_prototype) @@ -175,9 +141,12 @@ function alg_cache(alg::Rosenbrock32, u, rate_prototype, ::Type{uEltypeNoUnits}, algebraic_vars = f.mass_matrix === I ? nothing : [all(iszero, x) for x in eachcol(f.mass_matrix)] - Rosenbrock32Cache(u, uprev, k₁, k₂, k₃, du1, du2, f₁, fsalfirst, fsallast, dT, J, W, - tmp, atmp, weight, tab, tf, uf, linsolve_tmp, linsolve, jac_config, - grad_config, reltol, alg, algebraic_vars, alg.step_limiter!, alg.stage_limiter!) + RosenbrockCache(u, uprev, dense, du, du1, du2, f₁, + ks, + fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, + linsolve_tmp, + linsolve, jac_config, grad_config, reltol, alg, algebraic_vars, alg.step_limiter!, + alg.stage_limiter!, 3) end struct Rosenbrock23ConstantCache{T, TF, UF, JType, WType, F, AD} <: @@ -251,6 +220,7 @@ function alg_cache(alg::ROS3P, u, rate_prototype, ::Type{uEltypeNoUnits}, du = zero(rate_prototype) du1 = zero(rate_prototype) du2 = zero(rate_prototype) + ks = [zero(rate_prototype) for _ in 1:4] dense = Vector{rate_prototype}(undef, 0) f₁ = zero(rate_prototype) ks = [zero(rate_prototype) for _ in 1:4] @@ -405,58 +375,15 @@ struct Rodas23WConstantCache{TF, UF, Tab, JType, WType, F, AD} <: autodiff::AD end -@cache mutable struct Rodas23WCache{uType, rateType, uNoUnitsType, JType, WType, TabType, - TFType, UFType, F, JCType, GCType, RTolType, A, StepLimiter, StageLimiter} <: - RosenbrockMutableCache - u::uType - uprev::uType - dense1::rateType - dense2::rateType - dense3::rateType - du::rateType - du1::rateType - du2::rateType - k1::rateType - k2::rateType - k3::rateType - k4::rateType - k5::rateType - fsalfirst::rateType - fsallast::rateType - dT::rateType - J::JType - W::WType - tmp::rateType - atmp::uNoUnitsType - weight::uNoUnitsType - tab::TabType - tf::TFType - uf::UFType - linsolve_tmp::rateType - linsolve::F - jac_config::JCType - grad_config::GCType - reltol::RTolType - alg::A - step_limiter!::StepLimiter - stage_limiter!::StageLimiter -end - function alg_cache(alg::Rodas23W, u, rate_prototype, ::Type{uEltypeNoUnits}, ::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits}, uprev, uprev2, f, t, dt, reltol, p, calck, ::Val{true}) where {uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits} - dense1 = zero(rate_prototype) - dense2 = zero(rate_prototype) - dense3 = zero(rate_prototype) + dense = [zero(rate_prototype) for _ in 1:3] du = zero(rate_prototype) du1 = zero(rate_prototype) du2 = zero(rate_prototype) - k1 = zero(rate_prototype) - k2 = zero(rate_prototype) - k3 = zero(rate_prototype) - k4 = zero(rate_prototype) - k5 = zero(rate_prototype) + ks = [zero(rate_prototype) for _ in 1:5] fsalfirst = zero(rate_prototype) fsallast = zero(rate_prototype) dT = zero(rate_prototype) @@ -480,10 +407,10 @@ function alg_cache(alg::Rodas23W, u, rate_prototype, ::Type{uEltypeNoUnits}, assumptions = LinearSolve.OperatorAssumptions(true)) grad_config = build_grad_config(alg, f, tf, du1, t) jac_config = build_jac_config(alg, f, uf, du1, uprev, u, tmp, du2) - Rodas23WCache(u, uprev, dense1, dense2, dense3, du, du1, du2, k1, k2, k3, k4, k5, - fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, linsolve_tmp, - linsolve, jac_config, grad_config, reltol, alg, alg.step_limiter!, - alg.stage_limiter!) + RosenbrockCache(u, uprev, dense, du, du1, du2, ks, f₁, fsalfirst, fsallast, dT, J, W, tmp, atmp, weight, tab, tf, uf, + linsolve_tmp, + linsolve, jac_config, grad_config, reltol, alg, algebraic_vars, alg.step_limiter!, + alg.stage_limiter!, 3) end function alg_cache(alg::Rodas3P, u, rate_prototype, ::Type{uEltypeNoUnits}, @@ -753,7 +680,7 @@ function alg_cache( end function get_fsalfirstlast( - cache::Union{RosenbrockCache, Rosenbrock32Cache, RosenbrockCache, + cache::Union{RosenbrockCache, Rosenbrock4Cache}, u) (cache.fsalfirst, cache.fsallast) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl index aad93ff71e..69ee62f03c 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl @@ -1,8 +1,7 @@ ### Fallbacks to capture ROSENBROCKS_WITH_INTERPOLATIONS = Union{Rosenbrock23ConstantCache, RosenbrockCache, - Rosenbrock32ConstantCache, Rosenbrock32Cache, - RosenbrockCombinedConstantCache, - RosenbrockCache} + Rosenbrock32ConstantCache, + RosenbrockCombinedConstantCache} function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::ROSENBROCKS_WITH_INTERPOLATIONS, @@ -44,7 +43,7 @@ end end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, - cache::Union{RosenbrockCache, Rosenbrock32Cache}, + cache::Union{RosenbrockCache}, idxs::Nothing, T::Type{Val{0}}, differential_vars) @rosenbrock2332pre0 @inbounds @.. y₀+dt * (c1 * k[1] + c2 * k[2]) @@ -52,7 +51,7 @@ end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Union{Rosenbrock23ConstantCache, RosenbrockCache, - Rosenbrock32ConstantCache, Rosenbrock32Cache + Rosenbrock32ConstantCache }, idxs, T::Type{Val{0}}, differential_vars) @rosenbrock2332pre0 @.. y₀[idxs]+dt * (c1 * k[1][idxs] + c2 * k[2][idxs]) @@ -61,7 +60,7 @@ end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, cache::Union{Rosenbrock23ConstantCache, RosenbrockCache, - Rosenbrock32ConstantCache, Rosenbrock32Cache + Rosenbrock32ConstantCache }, idxs::Nothing, T::Type{Val{0}}, differential_vars) @rosenbrock2332pre0 @inbounds @.. out=y₀ + dt * (c1 * k[1] + c2 * k[2]) @@ -71,7 +70,7 @@ end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, cache::Union{Rosenbrock23ConstantCache, RosenbrockCache, - Rosenbrock32ConstantCache, Rosenbrock32Cache + Rosenbrock32ConstantCache }, idxs, T::Type{Val{0}}, differential_vars) @rosenbrock2332pre0 @views @.. out=y₀[idxs] + dt * (c1 * k[1][idxs] + c2 * k[2][idxs]) @@ -87,7 +86,7 @@ end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Union{Rosenbrock23ConstantCache, RosenbrockCache, - Rosenbrock32ConstantCache, Rosenbrock32Cache + Rosenbrock32ConstantCache }, idxs::Nothing, T::Type{Val{1}}, differential_vars) @rosenbrock2332pre1 @.. c1diff * k[1]+c2diff * k[2] @@ -95,7 +94,7 @@ end @muladd function _ode_interpolant(Θ, dt, y₀, y₁, k, cache::Union{Rosenbrock23ConstantCache, RosenbrockCache, - Rosenbrock32ConstantCache, Rosenbrock32Cache + Rosenbrock32ConstantCache }, idxs, T::Type{Val{1}}, differential_vars) @rosenbrock2332pre1 @.. c1diff * k[1][idxs]+c2diff * k[2][idxs] @@ -104,7 +103,7 @@ end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, cache::Union{Rosenbrock23ConstantCache, RosenbrockCache, - Rosenbrock32ConstantCache, Rosenbrock32Cache + Rosenbrock32ConstantCache }, idxs::Nothing, T::Type{Val{1}}, differential_vars) @rosenbrock2332pre1 @.. out=c1diff * k[1] + c2diff * k[2] diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index 5116ab9147..b25b63e35e 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -1,371 +1,189 @@ function initialize!(integrator, cache::Union{RosenbrockCache, - Rosenbrock32Cache}) - integrator.kshortsize = 2 - @unpack k₁, k₂, fsalfirst, fsallast = cache - resize!(integrator.k, integrator.kshortsize) - integrator.k[1] = k₁ - integrator.k[2] = k₂ - integrator.f(integrator.fsalfirst, integrator.uprev, integrator.p, integrator.t) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) + Rosenbrock32Cache}) +integrator.kshortsize = 2 +@unpack k₁, k₂, fsalfirst, fsallast = cache +resize!(integrator.k, integrator.kshortsize) +integrator.k[1] = k₁ +integrator.k[2] = k₂ +integrator.f(integrator.fsalfirst, integrator.uprev, integrator.p, integrator.t) +OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) end function initialize!(integrator, - cache::Union{Rosenbrock23ConstantCache, - Rosenbrock32ConstantCache}) - integrator.kshortsize = 2 - integrator.k = typeof(integrator.k)(undef, integrator.kshortsize) - integrator.fsalfirst = integrator.f(integrator.uprev, integrator.p, integrator.t) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - # Avoid undefined entries if k is an array of arrays - integrator.fsallast = zero(integrator.fsalfirst) - integrator.k[1] = zero(integrator.fsalfirst) - integrator.k[2] = zero(integrator.fsalfirst) + cache::Union{Rosenbrock23ConstantCache, + Rosenbrock32ConstantCache}) +integrator.kshortsize = 2 +integrator.k = typeof(integrator.k)(undef, integrator.kshortsize) +integrator.fsalfirst = integrator.f(integrator.uprev, integrator.p, integrator.t) +OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) + +# Avoid undefined entries if k is an array of arrays +integrator.fsallast = zero(integrator.fsalfirst) +integrator.k[1] = zero(integrator.fsalfirst) +integrator.k[2] = zero(integrator.fsalfirst) end @muladd function perform_step!(integrator, cache::RosenbrockCache, repeat_step = false) - @unpack t, dt, uprev, u, f, p, opts = integrator - @unpack k₁, k₂, k₃, du1, du2, f₁, fsalfirst, fsallast, dT, J, W, tmp, uf, tf, linsolve_tmp, jac_config, atmp, weight, stage_limiter!, step_limiter! = cache - @unpack c₃₂, d = cache.tab - - # Assignments - sizeu = size(u) - mass_matrix = integrator.f.mass_matrix - - # Precalculations - dtγ = dt * d - neginvdtγ = -inv(dtγ) - dto2 = dt / 2 - dto6 = dt / 6 - - if repeat_step - f(integrator.fsalfirst, uprev, p, t) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - end - - calc_rosenbrock_differentiation!(integrator, cache, dtγ, dtγ, repeat_step, true) - - calculate_residuals!(weight, fill!(weight, one(eltype(u))), uprev, uprev, - integrator.opts.abstol, integrator.opts.reltol, - integrator.opts.internalnorm, t) - - if repeat_step - linres = dolinsolve( - integrator, cache.linsolve; A = nothing, b = _vec(linsolve_tmp), - du = integrator.fsalfirst, u = u, p = p, t = t, weight = weight, - solverdata = (; gamma = dtγ)) - else - linres = dolinsolve(integrator, cache.linsolve; A = W, b = _vec(linsolve_tmp), - du = integrator.fsalfirst, u = u, p = p, t = t, weight = weight, - solverdata = (; gamma = dtγ)) - end - - vecu = _vec(linres.u) - veck₁ = _vec(k₁) - - @.. veck₁ = vecu * neginvdtγ - integrator.stats.nsolve += 1 - - @.. u = uprev + dto2 * k₁ - stage_limiter!(u, integrator, p, t + dto2) - f(f₁, u, p, t + dto2) +@unpack t, dt, uprev, u, f, p, opts = integrator +@unpack k₁, k₂, k₃, du1, du2, f₁, fsalfirst, fsallast, dT, J, W, tmp, uf, tf, linsolve_tmp, jac_config, atmp, weight, stage_limiter!, step_limiter! = cache +@unpack c₃₂, d = cache.tab + +# Assignments +sizeu = size(u) +mass_matrix = integrator.f.mass_matrix + +# Precalculations +dtγ = dt * d +neginvdtγ = -inv(dtγ) +dto2 = dt / 2 +dto6 = dt / 6 + +if repeat_step + f(integrator.fsalfirst, uprev, p, t) OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) +end - if mass_matrix === I - copyto!(tmp, k₁) - else - mul!(_vec(tmp), mass_matrix, _vec(k₁)) - end - - @.. linsolve_tmp = f₁ - tmp - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - vecu = _vec(linres.u) - veck₂ = _vec(k₂) - - @.. veck₂ = vecu * neginvdtγ + veck₁ - integrator.stats.nsolve += 1 - - @.. u = uprev + dt * k₂ - stage_limiter!(u, integrator, p, t + dt) - step_limiter!(u, integrator, p, t + dt) - - if integrator.opts.adaptive - f(fsallast, u, p, t + dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=fsallast - c₃₂ * (k₂ - f₁) - - 2(k₁ - fsalfirst) + dt * dT - else - @.. broadcast=false du2=c₃₂ * k₂ + 2k₁ - mul!(_vec(du1), mass_matrix, _vec(du2)) - @.. broadcast=false linsolve_tmp=fsallast - du1 + c₃₂ * f₁ + 2fsalfirst + - dt * dT - end - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - vecu = _vec(linres.u) - veck3 = _vec(k₃) - @.. veck3 = vecu * neginvdtγ - - integrator.stats.nsolve += 1 - - if mass_matrix === I - @.. broadcast=false tmp=dto6 * (k₁ - 2 * k₂ + k₃) - else - veck₁ = _vec(k₁) - veck₂ = _vec(k₂) - veck₃ = _vec(k₃) - vectmp = _vec(tmp) - @.. broadcast=false vectmp=ifelse(cache.algebraic_vars, - false, dto6 * (veck₁ - 2 * veck₂ + veck₃)) - end - calculate_residuals!(atmp, tmp, uprev, u, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - integrator.EEst = integrator.opts.internalnorm(atmp, t) - - if mass_matrix !== I - algvar = reshape(cache.algebraic_vars, size(u)) - invatol = inv(integrator.opts.abstol) - @.. atmp = ifelse(algvar, fsallast, false) * invatol - integrator.EEst += integrator.opts.internalnorm(atmp, t) - end - end - cache.linsolve = linres.cache +calc_rosenbrock_differentiation!(integrator, cache, dtγ, dtγ, repeat_step, true) + +calculate_residuals!(weight, fill!(weight, one(eltype(u))), uprev, uprev, + integrator.opts.abstol, integrator.opts.reltol, + integrator.opts.internalnorm, t) + +if repeat_step + linres = dolinsolve( + integrator, cache.linsolve; A = nothing, b = _vec(linsolve_tmp), + du = integrator.fsalfirst, u = u, p = p, t = t, weight = weight, + solverdata = (; gamma = dtγ)) +else + linres = dolinsolve(integrator, cache.linsolve; A = W, b = _vec(linsolve_tmp), + du = integrator.fsalfirst, u = u, p = p, t = t, weight = weight, + solverdata = (; gamma = dtγ)) end -@muladd function perform_step!(integrator, cache::Rosenbrock32Cache, repeat_step = false) - @unpack t, dt, uprev, u, f, p, opts = integrator - @unpack k₁, k₂, k₃, du1, du2, f₁, fsalfirst, fsallast, dT, J, W, tmp, uf, tf, linsolve_tmp, jac_config, atmp, weight, stage_limiter!, step_limiter! = cache - @unpack c₃₂, d = cache.tab +vecu = _vec(linres.u) +veck₁ = _vec(k₁) - # Assignments - sizeu = size(u) - mass_matrix = integrator.f.mass_matrix +@.. veck₁ = vecu * neginvdtγ +integrator.stats.nsolve += 1 - # Precalculations - dtγ = dt * d - neginvdtγ = -inv(dtγ) - dto2 = dt / 2 - dto6 = dt / 6 +@.. u = uprev + dto2 * k₁ +stage_limiter!(u, integrator, p, t + dto2) +f(f₁, u, p, t + dto2) +OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - if repeat_step - f(integrator.fsalfirst, uprev, p, t) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - end +if mass_matrix === I + copyto!(tmp, k₁) +else + mul!(_vec(tmp), mass_matrix, _vec(k₁)) +end - calc_rosenbrock_differentiation!(integrator, cache, dtγ, dtγ, repeat_step, true) +@.. linsolve_tmp = f₁ - tmp - calculate_residuals!(weight, fill!(weight, one(eltype(u))), uprev, uprev, - integrator.opts.abstol, integrator.opts.reltol, - integrator.opts.internalnorm, t) +linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) +vecu = _vec(linres.u) +veck₂ = _vec(k₂) - if repeat_step - linres = dolinsolve( - integrator, cache.linsolve; A = nothing, b = _vec(linsolve_tmp), - du = integrator.fsalfirst, u = u, p = p, t = t, weight = weight, - solverdata = (; gamma = dtγ)) - else - linres = dolinsolve(integrator, cache.linsolve; A = W, b = _vec(linsolve_tmp), - du = integrator.fsalfirst, u = u, p = p, t = t, weight = weight, - solverdata = (; gamma = dtγ)) - end +@.. veck₂ = vecu * neginvdtγ + veck₁ +integrator.stats.nsolve += 1 - vecu = _vec(linres.u) - veck₁ = _vec(k₁) - - @.. veck₁ = vecu * neginvdtγ - integrator.stats.nsolve += 1 +@.. u = uprev + dt * k₂ +stage_limiter!(u, integrator, p, t + dt) +step_limiter!(u, integrator, p, t + dt) - @.. broadcast=false u=uprev + dto2 * k₁ - stage_limiter!(u, integrator, p, t + dto2) - f(f₁, u, p, t + dto2) +if integrator.opts.adaptive + f(fsallast, u, p, t + dt) OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) if mass_matrix === I - tmp .= k₁ - else - mul!(_vec(tmp), mass_matrix, _vec(k₁)) - end - - @.. broadcast=false linsolve_tmp=f₁ - tmp - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - vecu = _vec(linres.u) - veck₂ = _vec(k₂) - - @.. veck₂ = vecu * neginvdtγ + veck₁ - integrator.stats.nsolve += 1 - - @.. tmp = uprev + dt * k₂ - stage_limiter!(u, integrator, p, t + dt) - f(fsallast, tmp, p, t + dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - @.. broadcast=false linsolve_tmp=fsallast - c₃₂ * (k₂ - f₁) - 2(k₁ - fsalfirst) + - dt * dT + @.. broadcast=false linsolve_tmp=fsallast - c₃₂ * (k₂ - f₁) - + 2(k₁ - fsalfirst) + dt * dT else @.. broadcast=false du2=c₃₂ * k₂ + 2k₁ mul!(_vec(du1), mass_matrix, _vec(du2)) - @.. broadcast=false linsolve_tmp=fsallast - du1 + c₃₂ * f₁ + 2fsalfirst + dt * dT + @.. broadcast=false linsolve_tmp=fsallast - du1 + c₃₂ * f₁ + 2fsalfirst + + dt * dT end linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) vecu = _vec(linres.u) veck3 = _vec(k₃) - @.. veck3 = vecu * neginvdtγ - integrator.stats.nsolve += 1 - - @.. broadcast=false u=uprev + dto6 * (k₁ + 4k₂ + k₃) - step_limiter!(u, integrator, p, t + dt) + integrator.stats.nsolve += 1 - if integrator.opts.adaptive + if mass_matrix === I @.. broadcast=false tmp=dto6 * (k₁ - 2 * k₂ + k₃) - calculate_residuals!(atmp, tmp, uprev, u, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - integrator.EEst = integrator.opts.internalnorm(atmp, t) - - if mass_matrix !== I - invatol = inv(integrator.opts.abstol) - @.. atmp = ifelse(cache.algebraic_vars, fsallast, false) * invatol - integrator.EEst += integrator.opts.internalnorm(atmp, t) - end + else + veck₁ = _vec(k₁) + veck₂ = _vec(k₂) + veck₃ = _vec(k₃) + vectmp = _vec(tmp) + @.. broadcast=false vectmp=ifelse(cache.algebraic_vars, + false, dto6 * (veck₁ - 2 * veck₂ + veck₃)) + end + calculate_residuals!(atmp, tmp, uprev, u, integrator.opts.abstol, + integrator.opts.reltol, integrator.opts.internalnorm, t) + integrator.EEst = integrator.opts.internalnorm(atmp, t) + + if mass_matrix !== I + algvar = reshape(cache.algebraic_vars, size(u)) + invatol = inv(integrator.opts.abstol) + @.. atmp = ifelse(algvar, fsallast, false) * invatol + integrator.EEst += integrator.opts.internalnorm(atmp, t) end - cache.linsolve = linres.cache +end +cache.linsolve = linres.cache end @muladd function perform_step!(integrator, cache::Rosenbrock23ConstantCache, - repeat_step = false) - @unpack t, dt, uprev, u, f, p = integrator - @unpack c₃₂, d, tf, uf = cache - - # Precalculations - dtγ = dt * d - neginvdtγ = -inv(dtγ) - dto2 = dt / 2 - dto6 = dt / 6 - - if repeat_step - integrator.fsalfirst = f(uprev, p, t) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - end - - mass_matrix = integrator.f.mass_matrix - - # Time derivative - dT = calc_tderivative(integrator, cache) - - W = calc_W(integrator, cache, dtγ, repeat_step, true) - if !issuccess_W(W) - integrator.EEst = 2 - return nothing - end - - k₁ = _reshape(W \ _vec((integrator.fsalfirst + dtγ * dT)), axes(uprev)) * neginvdtγ - integrator.stats.nsolve += 1 - tmp = @.. uprev + dto2 * k₁ - f₁ = f(tmp, p, t + dto2) + repeat_step = false) +@unpack t, dt, uprev, u, f, p = integrator +@unpack c₃₂, d, tf, uf = cache + +# Precalculations +dtγ = dt * d +neginvdtγ = -inv(dtγ) +dto2 = dt / 2 +dto6 = dt / 6 + +if repeat_step + integrator.fsalfirst = f(uprev, p, t) OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) +end - if mass_matrix === I - k₂ = _reshape(W \ _vec(f₁ - k₁), axes(uprev)) - else - k₂ = _reshape(W \ _vec(f₁ - mass_matrix * k₁), axes(uprev)) - end - k₂ = @.. k₂ * neginvdtγ + k₁ - integrator.stats.nsolve += 1 - u = uprev + dt * k₂ - - if integrator.opts.adaptive - integrator.fsallast = f(u, p, t + dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - linsolve_tmp = @.. (integrator.fsallast - c₃₂ * (k₂ - f₁) - - 2 * (k₁ - integrator.fsalfirst) + dt * dT) - else - linsolve_tmp = mass_matrix * (@.. c₃₂ * k₂ + 2 * k₁) - linsolve_tmp = @.. (integrator.fsallast - linsolve_tmp + - c₃₂ * f₁ + 2 * integrator.fsalfirst + dt * dT) - end - k₃ = _reshape(W \ _vec(linsolve_tmp), axes(uprev)) * neginvdtγ - integrator.stats.nsolve += 1 +mass_matrix = integrator.f.mass_matrix - if u isa Number - utilde = dto6 * f.mass_matrix[1, 1] * (k₁ - 2 * k₂ + k₃) - else - utilde = f.mass_matrix * (@.. dto6 * (k₁ - 2 * k₂ + k₃)) - end - atmp = calculate_residuals(utilde, uprev, u, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - integrator.EEst = integrator.opts.internalnorm(atmp, t) +# Time derivative +dT = calc_tderivative(integrator, cache) - if mass_matrix !== I - invatol = inv(integrator.opts.abstol) - atmp = @. ifelse(integrator.differential_vars, false, integrator.fsallast) * - invatol - integrator.EEst += integrator.opts.internalnorm(atmp, t) - end - end - integrator.k[1] = k₁ - integrator.k[2] = k₂ - integrator.u = u +W = calc_W(integrator, cache, dtγ, repeat_step, true) +if !issuccess_W(W) + integrator.EEst = 2 return nothing end -@muladd function perform_step!(integrator, cache::Rosenbrock32ConstantCache, - repeat_step = false) - @unpack t, dt, uprev, u, f, p = integrator - @unpack c₃₂, d, tf, uf = cache - - # Precalculations - dtγ = dt * d - neginvdtγ = -inv(dtγ) - dto2 = dt / 2 - dto6 = dt / 6 - - mass_matrix = integrator.f.mass_matrix - - if repeat_step - integrator.fsalfirst = f(uprev, p, t) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - end +k₁ = _reshape(W \ _vec((integrator.fsalfirst + dtγ * dT)), axes(uprev)) * neginvdtγ +integrator.stats.nsolve += 1 +tmp = @.. uprev + dto2 * k₁ +f₁ = f(tmp, p, t + dto2) +OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - # Time derivative - dT = calc_tderivative(integrator, cache) - - W = calc_W(integrator, cache, dtγ, repeat_step, true) - if !issuccess_W(W) - integrator.EEst = 2 - return nothing - end - - k₁ = _reshape(W \ -_vec((integrator.fsalfirst + dtγ * dT)), axes(uprev)) / dtγ - integrator.stats.nsolve += 1 - tmp = @.. uprev + dto2 * k₁ - f₁ = f(tmp, p, t + dto2) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - if mass_matrix === I - k₂ = _reshape(W \ _vec(f₁ - k₁), axes(uprev)) - else - linsolve_tmp = f₁ - mass_matrix * k₁ - k₂ = _reshape(W \ _vec(linsolve_tmp), axes(uprev)) - end - k₂ = @.. k₂ * neginvdtγ + k₁ +if mass_matrix === I + k₂ = _reshape(W \ _vec(f₁ - k₁), axes(uprev)) +else + k₂ = _reshape(W \ _vec(f₁ - mass_matrix * k₁), axes(uprev)) +end +k₂ = @.. k₂ * neginvdtγ + k₁ +integrator.stats.nsolve += 1 +u = uprev + dt * k₂ - integrator.stats.nsolve += 1 - tmp = @.. uprev + dt * k₂ - integrator.fsallast = f(tmp, p, t + dt) +if integrator.opts.adaptive + integrator.fsallast = f(u, p, t + dt) OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) if mass_matrix === I linsolve_tmp = @.. (integrator.fsallast - c₃₂ * (k₂ - f₁) - - 2(k₁ - integrator.fsalfirst) + dt * dT) + 2 * (k₁ - integrator.fsalfirst) + dt * dT) else linsolve_tmp = mass_matrix * (@.. c₃₂ * k₂ + 2 * k₁) linsolve_tmp = @.. (integrator.fsallast - linsolve_tmp + @@ -373,142 +191,221 @@ end end k₃ = _reshape(W \ _vec(linsolve_tmp), axes(uprev)) * neginvdtγ integrator.stats.nsolve += 1 - u = @.. uprev + dto6 * (k₁ + 4k₂ + k₃) - if integrator.opts.adaptive - utilde = @.. dto6 * (k₁ - 2k₂ + k₃) - atmp = calculate_residuals(utilde, uprev, u, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - integrator.EEst = integrator.opts.internalnorm(atmp, t) + if u isa Number + utilde = dto6 * f.mass_matrix[1, 1] * (k₁ - 2 * k₂ + k₃) + else + utilde = f.mass_matrix * (@.. dto6 * (k₁ - 2 * k₂ + k₃)) + end + atmp = calculate_residuals(utilde, uprev, u, integrator.opts.abstol, + integrator.opts.reltol, integrator.opts.internalnorm, t) + integrator.EEst = integrator.opts.internalnorm(atmp, t) - if mass_matrix !== I - invatol = inv(integrator.opts.abstol) - atmp = ifelse(integrator.differential_vars, false, integrator.fsallast) .* - invatol - integrator.EEst += integrator.opts.internalnorm(atmp, t) - end + if mass_matrix !== I + invatol = inv(integrator.opts.abstol) + atmp = @. ifelse(integrator.differential_vars, false, integrator.fsallast) * + invatol + integrator.EEst += integrator.opts.internalnorm(atmp, t) end +end +integrator.k[1] = k₁ +integrator.k[2] = k₂ +integrator.u = u +return nothing +end + +@muladd function perform_step!(integrator, cache::Rosenbrock32ConstantCache, + repeat_step = false) +@unpack t, dt, uprev, u, f, p = integrator +@unpack c₃₂, d, tf, uf = cache + +# Precalculations +dtγ = dt * d +neginvdtγ = -inv(dtγ) +dto2 = dt / 2 +dto6 = dt / 6 + +mass_matrix = integrator.f.mass_matrix + +if repeat_step + integrator.fsalfirst = f(uprev, p, t) + OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) +end + +# Time derivative +dT = calc_tderivative(integrator, cache) - integrator.k[1] = k₁ - integrator.k[2] = k₂ - integrator.u = u +W = calc_W(integrator, cache, dtγ, repeat_step, true) +if !issuccess_W(W) + integrator.EEst = 2 return nothing end -function initialize!(integrator, - cache::Union{RosenbrockCombinedConstantCache, - Rosenbrock34ConstantCache, - Rosenbrock4ConstantCache}) - integrator.kshortsize = 2 - integrator.k = typeof(integrator.k)(undef, integrator.kshortsize) - integrator.fsalfirst = integrator.f(integrator.uprev, integrator.p, integrator.t) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) +k₁ = _reshape(W \ -_vec((integrator.fsalfirst + dtγ * dT)), axes(uprev)) / dtγ +integrator.stats.nsolve += 1 +tmp = @.. uprev + dto2 * k₁ +f₁ = f(tmp, p, t + dto2) +OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) + +if mass_matrix === I + k₂ = _reshape(W \ _vec(f₁ - k₁), axes(uprev)) +else + linsolve_tmp = f₁ - mass_matrix * k₁ + k₂ = _reshape(W \ _vec(linsolve_tmp), axes(uprev)) +end +k₂ = @.. k₂ * neginvdtγ + k₁ + +integrator.stats.nsolve += 1 +tmp = @.. uprev + dt * k₂ +integrator.fsallast = f(tmp, p, t + dt) +OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) + +if mass_matrix === I + linsolve_tmp = @.. (integrator.fsallast - c₃₂ * (k₂ - f₁) - + 2(k₁ - integrator.fsalfirst) + dt * dT) +else + linsolve_tmp = mass_matrix * (@.. c₃₂ * k₂ + 2 * k₁) + linsolve_tmp = @.. (integrator.fsallast - linsolve_tmp + + c₃₂ * f₁ + 2 * integrator.fsalfirst + dt * dT) +end +k₃ = _reshape(W \ _vec(linsolve_tmp), axes(uprev)) * neginvdtγ +integrator.stats.nsolve += 1 +u = @.. uprev + dto6 * (k₁ + 4k₂ + k₃) + +if integrator.opts.adaptive + utilde = @.. dto6 * (k₁ - 2k₂ + k₃) + atmp = calculate_residuals(utilde, uprev, u, integrator.opts.abstol, + integrator.opts.reltol, integrator.opts.internalnorm, t) + integrator.EEst = integrator.opts.internalnorm(atmp, t) + + if mass_matrix !== I + invatol = inv(integrator.opts.abstol) + atmp = ifelse(integrator.differential_vars, false, integrator.fsallast) .* + invatol + integrator.EEst += integrator.opts.internalnorm(atmp, t) + end +end - # Avoid undefined entries if k is an array of arrays - integrator.fsallast = zero(integrator.fsalfirst) - integrator.k[1] = integrator.fsalfirst - integrator.k[2] = integrator.fsallast +integrator.k[1] = k₁ +integrator.k[2] = k₂ +integrator.u = u +return nothing end function initialize!(integrator, - cache::Union{RosenbrockCache, - Rosenbrock4Cache}) - integrator.kshortsize = 2 - @unpack fsalfirst, fsallast = cache - resize!(integrator.k, integrator.kshortsize) - integrator.k[1] = fsalfirst - integrator.k[2] = fsallast - integrator.f(integrator.fsalfirst, integrator.uprev, integrator.p, integrator.t) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) + cache::Union{RosenbrockCombinedConstantCache, + Rosenbrock34ConstantCache, + Rosenbrock4ConstantCache}) +integrator.kshortsize = 2 +integrator.k = typeof(integrator.k)(undef, integrator.kshortsize) +integrator.fsalfirst = integrator.f(integrator.uprev, integrator.p, integrator.t) +OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) + +# Avoid undefined entries if k is an array of arrays +integrator.fsallast = zero(integrator.fsalfirst) +integrator.k[1] = integrator.fsalfirst +integrator.k[2] = integrator.fsallast +end + +function initialize!(integrator, + cache::Union{RosenbrockCache, + Rosenbrock4Cache}) +integrator.kshortsize = 2 +@unpack fsalfirst, fsallast = cache +resize!(integrator.k, integrator.kshortsize) +integrator.k[1] = fsalfirst +integrator.k[2] = fsallast +integrator.f(integrator.fsalfirst, integrator.uprev, integrator.p, integrator.t) +OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) end ################################################################################ @muladd function perform_step!(integrator, cache::Rosenbrock34ConstantCache, - repeat_step = false) - @unpack t, dt, uprev, u, f, p = integrator - @unpack tf, uf = cache - @unpack a21, a31, a32, a41, a42, a43, C21, C31, C32, C41, C42, C43, b1, b2, b3, b4, btilde1, btilde2, btilde3, btilde4, gamma, c2, c3, d1, d2, d3, d4 = cache.tab - - # Precalculations - dtC21 = C21 / dt - dtC31 = C31 / dt - dtC32 = C32 / dt - dtC41 = C41 / dt - dtC42 = C42 / dt - dtC43 = C43 / dt - - dtd1 = dt * d1 - dtd2 = dt * d2 - dtd3 = dt * d3 - dtd4 = dt * d4 - dtgamma = dt * gamma - - mass_matrix = integrator.f.mass_matrix - # Time derivative - tf.u = uprev - dT = calc_tderivative(integrator, cache) - - W = calc_W(integrator, cache, dtgamma, repeat_step, true) - if !issuccess_W(W) - integrator.EEst = 2 - return nothing - end + repeat_step = false) +@unpack t, dt, uprev, u, f, p = integrator +@unpack tf, uf = cache +@unpack a21, a31, a32, a41, a42, a43, C21, C31, C32, C41, C42, C43, b1, b2, b3, b4, btilde1, btilde2, btilde3, btilde4, gamma, c2, c3, d1, d2, d3, d4 = cache.tab + +# Precalculations +dtC21 = C21 / dt +dtC31 = C31 / dt +dtC32 = C32 / dt +dtC41 = C41 / dt +dtC42 = C42 / dt +dtC43 = C43 / dt + +dtd1 = dt * d1 +dtd2 = dt * d2 +dtd3 = dt * d3 +dtd4 = dt * d4 +dtgamma = dt * gamma + +mass_matrix = integrator.f.mass_matrix +# Time derivative +tf.u = uprev +dT = calc_tderivative(integrator, cache) + +W = calc_W(integrator, cache, dtgamma, repeat_step, true) +if !issuccess_W(W) + integrator.EEst = 2 + return nothing +end - linsolve_tmp = integrator.fsalfirst + dtd1 * dT +linsolve_tmp = integrator.fsalfirst + dtd1 * dT - k1 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - u = uprev # +a21*k1 a21 == 0 - # du = f(u, p, t+c2*dt) c2 == 0 and a21 == 0 => du = f(uprev, p, t) == fsalfirst +k1 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) +integrator.stats.nsolve += 1 +u = uprev # +a21*k1 a21 == 0 +# du = f(u, p, t+c2*dt) c2 == 0 and a21 == 0 => du = f(uprev, p, t) == fsalfirst - if mass_matrix === I - linsolve_tmp = integrator.fsalfirst + dtd2 * dT + dtC21 * k1 - else - linsolve_tmp = integrator.fsalfirst + dtd2 * dT + mass_matrix * (dtC21 * k1) - end +if mass_matrix === I + linsolve_tmp = integrator.fsalfirst + dtd2 * dT + dtC21 * k1 +else + linsolve_tmp = integrator.fsalfirst + dtd2 * dT + mass_matrix * (dtC21 * k1) +end - k2 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - u = uprev + a31 * k1 + a32 * k2 - du = f(u, p, t + c3 * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) +k2 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) +integrator.stats.nsolve += 1 +u = uprev + a31 * k1 + a32 * k2 +du = f(u, p, t + c3 * dt) +OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - if mass_matrix === I - linsolve_tmp = du + dtd3 * dT + dtC31 * k1 + dtC32 * k2 - else - linsolve_tmp = du + dtd3 * dT + mass_matrix * (dtC31 * k1 + dtC32 * k2) - end - - k3 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - u = uprev + a41 * k1 + a42 * k2 + a43 * k3 - du = f(u, p, t + dt) #-- c4 = 1 - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) +if mass_matrix === I + linsolve_tmp = du + dtd3 * dT + dtC31 * k1 + dtC32 * k2 +else + linsolve_tmp = du + dtd3 * dT + mass_matrix * (dtC31 * k1 + dtC32 * k2) +end - if mass_matrix === I - linsolve_tmp = du + dtd4 * dT + dtC41 * k1 + dtC42 * k2 + dtC43 * k3 - else - linsolve_tmp = du + dtd4 * dT + mass_matrix * (dtC41 * k1 + dtC42 * k2 + dtC43 * k3) - end +k3 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) +integrator.stats.nsolve += 1 +u = uprev + a41 * k1 + a42 * k2 + a43 * k3 +du = f(u, p, t + dt) #-- c4 = 1 +OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - k4 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - integrator.stats.nsolve += 1 - u = uprev + b1 * k1 + b2 * k2 + b3 * k3 + b4 * k4 - integrator.fsallast = f(u, p, t + dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) +if mass_matrix === I + linsolve_tmp = du + dtd4 * dT + dtC41 * k1 + dtC42 * k2 + dtC43 * k3 +else + linsolve_tmp = du + dtd4 * dT + mass_matrix * (dtC41 * k1 + dtC42 * k2 + dtC43 * k3) +end - if integrator.opts.adaptive - utilde = btilde1 * k1 + btilde2 * k2 + btilde3 * k3 + btilde4 * k4 - atmp = calculate_residuals(utilde, uprev, u, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - integrator.EEst = integrator.opts.internalnorm(atmp, t) - end +k4 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) +integrator.stats.nsolve += 1 +u = uprev + b1 * k1 + b2 * k2 + b3 * k3 + b4 * k4 +integrator.fsallast = f(u, p, t + dt) +OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) + +if integrator.opts.adaptive + utilde = btilde1 * k1 + btilde2 * k2 + btilde3 * k3 + btilde4 * k4 + atmp = calculate_residuals(utilde, uprev, u, integrator.opts.abstol, + integrator.opts.reltol, integrator.opts.internalnorm, t) + integrator.EEst = integrator.opts.internalnorm(atmp, t) +end - integrator.k[1] = integrator.fsalfirst - integrator.k[2] = integrator.fsallast - integrator.u = u - return nothing +integrator.k[1] = integrator.fsalfirst +integrator.k[2] = integrator.fsallast +integrator.u = u +return nothing end ################################################################################ @@ -543,32 +440,61 @@ end #### Rodas3P type method function initialize!(integrator, cache::RosenbrockCombinedConstantCache) - integrator.kshortsize = 3 - integrator.k = typeof(integrator.k)(undef, integrator.kshortsize) - # Avoid undefined entries if k is an array of arrays - for i in 1:integrator.kshortsize - integrator.k[i] = zero(integrator.u) - end +integrator.kshortsize = 3 +integrator.k = typeof(integrator.k)(undef, integrator.kshortsize) +# Avoid undefined entries if k is an array of arrays +for i in 1:integrator.kshortsize + integrator.k[i] = zero(integrator.u) +end end function initialize!(integrator, cache::RosenbrockCache) - integrator.kshortsize = 3 - @unpack dense1, dense2, dense3 = cache - resize!(integrator.k, integrator.kshortsize) - for i in 1:integrator.kshortsize - integrator.k[i] = cache.dense[i] - end +integrator.kshortsize = 3 +@unpack dense1, dense2, dense3 = cache +resize!(integrator.k, integrator.kshortsize) +for i in 1:integrator.kshortsize + integrator.k[i] = cache.dense[i] +end end function calculate_interpoldiff(uprev, up2, up3, c_koeff, d_koeff, c2_koeff) - u_int = 0.0 - u_diff = 0.0 - a1 = up3 + c_koeff - up2 - c2_koeff - a2 = d_koeff - c_koeff + c2_koeff - a3 = -d_koeff +u_int = 0.0 +u_diff = 0.0 +a1 = up3 + c_koeff - up2 - c2_koeff +a2 = d_koeff - c_koeff + c2_koeff +a3 = -d_koeff +dis = a2^2 - 3 * a1 * a3 +u_int = up3 +u_diff = 0.0 +if dis > 0.0 #-- Min/Max occurs + tau1 = (-a2 - sqrt(dis)) / (3 * a3) + tau2 = (-a2 + sqrt(dis)) / (3 * a3) + if tau1 > tau2 + tau1, tau2 = tau2, tau1 + end + for tau in (tau1, tau2) + if (tau > 0.0) && (tau < 1.0) + y_tau = (1 - tau) * uprev + + tau * (up3 + (1 - tau) * (c_koeff + tau * d_koeff)) + dy_tau = ((a3 * tau + a2) * tau + a1) * tau + if abs(dy_tau) > abs(u_diff) + u_diff = dy_tau + u_int = y_tau + end + end + end +end +return u_int, u_diff +end + +function calculate_interpoldiff!(u_int, u_diff, uprev, up2, up3, c_koeff, d_koeff, c2_koeff) +for i in eachindex(up2) + a1 = up3[i] + c_koeff[i] - up2[i] - c2_koeff[i] + a2 = d_koeff[i] - c_koeff[i] + c2_koeff[i] + a3 = -d_koeff[i] dis = a2^2 - 3 * a1 * a3 - u_int = up3 - u_diff = 0.0 + u_int[i] = up3[i] + u_diff[i] = 0.0 if dis > 0.0 #-- Min/Max occurs tau1 = (-a2 - sqrt(dis)) / (3 * a3) tau2 = (-a2 + sqrt(dis)) / (3 * a3) @@ -577,342 +503,336 @@ function calculate_interpoldiff(uprev, up2, up3, c_koeff, d_koeff, c2_koeff) end for tau in (tau1, tau2) if (tau > 0.0) && (tau < 1.0) - y_tau = (1 - tau) * uprev + - tau * (up3 + (1 - tau) * (c_koeff + tau * d_koeff)) + y_tau = (1 - tau) * uprev[i] + + tau * (up3[i] + (1 - tau) * (c_koeff[i] + tau * d_koeff[i])) dy_tau = ((a3 * tau + a2) * tau + a1) * tau - if abs(dy_tau) > abs(u_diff) - u_diff = dy_tau - u_int = y_tau + if abs(dy_tau) > abs(u_diff[i]) + u_diff[i] = dy_tau + u_int[i] = y_tau end end end end - return u_int, u_diff end - -function calculate_interpoldiff!(u_int, u_diff, uprev, up2, up3, c_koeff, d_koeff, c2_koeff) - for i in eachindex(up2) - a1 = up3[i] + c_koeff[i] - up2[i] - c2_koeff[i] - a2 = d_koeff[i] - c_koeff[i] + c2_koeff[i] - a3 = -d_koeff[i] - dis = a2^2 - 3 * a1 * a3 - u_int[i] = up3[i] - u_diff[i] = 0.0 - if dis > 0.0 #-- Min/Max occurs - tau1 = (-a2 - sqrt(dis)) / (3 * a3) - tau2 = (-a2 + sqrt(dis)) / (3 * a3) - if tau1 > tau2 - tau1, tau2 = tau2, tau1 - end - for tau in (tau1, tau2) - if (tau > 0.0) && (tau < 1.0) - y_tau = (1 - tau) * uprev[i] + - tau * (up3[i] + (1 - tau) * (c_koeff[i] + tau * d_koeff[i])) - dy_tau = ((a3 * tau + a2) * tau + a1) * tau - if abs(dy_tau) > abs(u_diff[i]) - u_diff[i] = dy_tau - u_int[i] = y_tau - end - end - end - end - end end #### Rodas4 type method function initialize!(integrator, cache::RosenbrockCombinedConstantCache) - integrator.kshortsize = cache.order == 5 ? 3 : 2 - integrator.k = typeof(integrator.k)(undef, integrator.kshortsize) - # Avoid undefined entries if k is an array of arrays - for i in 1:integrator.kshortsize - integrator.k[i] = zero(integrator.u) - end +integrator.kshortsize = cache.order == 5 ? 3 : 2 +integrator.k = typeof(integrator.k)(undef, integrator.kshortsize) +# Avoid undefined entries if k is an array of arrays +for i in 1:integrator.kshortsize + integrator.k[i] = zero(integrator.u) +end end @muladd function perform_step!(integrator, cache::RosenbrockCombinedConstantCache, repeat_step = false) - (;t, dt, uprev, u, f, p) = integrator - (;tf, uf) = cache - (;A, C, gamma, c, d, H) = cache.tab +(;t, dt, uprev, u, f, p) = integrator +(;tf, uf) = cache +(;A, C, gamma, c, d, H) = cache.tab - if hasproperty(cache.tab, :h2_2) - h2_2 = cache.tab.h2_2 - end +if hasproperty(cache.tab, :h2_2) + h2_2 = cache.tab.h2_2 +end - # Precalculations - dtC = C ./ dt - dtd = dt .* d - dtgamma = dt * gamma +# Precalculations +dtC = C ./ dt +dtd = dt .* d +dtgamma = dt * gamma - mass_matrix = integrator.f.mass_matrix +mass_matrix = integrator.f.mass_matrix - # Time derivative - tf.u = uprev - dT = calc_tderivative(integrator, cache) +# Time derivative +tf.u = uprev +dT = calc_tderivative(integrator, cache) - W = calc_W(integrator, cache, dtgamma, repeat_step, true) - if !issuccess_W(W) - integrator.EEst = 2 - return nothing - end +W = calc_W(integrator, cache, dtgamma, repeat_step, true) +if !issuccess_W(W) + integrator.EEst = 2 + return nothing +end - # Initialize ks - num_stages = size(A, 1) - du = f(uprev, p, t) - linsolve_tmp = @.. du + dtd[1] * dT - k1 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) - # constant number for type stability make sure this is greater than num_stages - ks = ntuple(Returns(k1), 10) - # Loop for stages - for stage in 2:num_stages - u = uprev +# Initialize ks +num_stages = size(A, 1) +du = f(uprev, p, t) +linsolve_tmp = @.. du + dtd[1] * dT +k1 = _reshape(W \ -_vec(linsolve_tmp), axes(uprev)) +# constant number for type stability make sure this is greater than num_stages +ks = ntuple(Returns(k1), 10) +# Loop for stages +for stage in 2:num_stages + u = uprev + for i in 1:(stage - 1) + u = @.. u + A[stage, i] * ks[i] + end + + du = f(u, p, t + c[stage] * dt) + OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) + + # Compute linsolve_tmp for current stage + linsolve_tmp = zero(du) + if mass_matrix === I for i in 1:(stage - 1) - u = @.. u + A[stage, i] * ks[i] + linsolve_tmp = @.. linsolve_tmp + dtC[stage, i] * ks[i] end - - du = f(u, p, t + c[stage] * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - # Compute linsolve_tmp for current stage - linsolve_tmp = zero(du) - if mass_matrix === I - for i in 1:(stage - 1) - linsolve_tmp = @.. linsolve_tmp + dtC[stage, i] * ks[i] - end - else - for i in 1:(stage - 1) - linsolve_tmp = @.. linsolve_tmp + dtC[stage, i] * ks[i] - end - linsolve_tmp = mass_matrix * linsolve_tmp + else + for i in 1:(stage - 1) + linsolve_tmp = @.. linsolve_tmp + dtC[stage, i] * ks[i] end - linsolve_tmp = @.. du + dtd[stage] * dT + linsolve_tmp - - ks = Base.setindex(ks, _reshape(W \ -_vec(linsolve_tmp), axes(uprev)), stage) - integrator.stats.nsolve += 1 + linsolve_tmp = mass_matrix * linsolve_tmp end - #@show ks - u = u .+ ks[num_stages] + linsolve_tmp = @.. du + dtd[stage] * dT + linsolve_tmp - if integrator.opts.adaptive - atmp = calculate_residuals(ks[num_stages], uprev, u, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - integrator.EEst = integrator.opts.internalnorm(atmp, t) - end + ks = Base.setindex(ks, _reshape(W \ -_vec(linsolve_tmp), axes(uprev)), stage) + integrator.stats.nsolve += 1 +end +#@show ks +u = u .+ ks[num_stages] + +if integrator.opts.adaptive + atmp = calculate_residuals(ks[num_stages], uprev, u, integrator.opts.abstol, + integrator.opts.reltol, integrator.opts.internalnorm, t) + integrator.EEst = integrator.opts.internalnorm(atmp, t) +end - EEst = 0.0 - if integrator.opts.calck +EEst = 0.0 +if integrator.opts.calck + for j in eachindex(integrator.k) + integrator.k[j] = zero(integrator.k[1]) + end + for i in 1:num_stages for j in eachindex(integrator.k) - integrator.k[j] = zero(integrator.k[1]) - end - for i in 1:num_stages - for j in eachindex(integrator.k) - integrator.k[j] = @.. integrator.k[j] + H[j, i] * ks[i] - end - end - if hasproperty(cache.tab, :h2_2) && integrator.kshortsize == 3 - integrator.k[3] = h2_2[1] * k1 + h2_2[2] * k2 + h2_2[3] * k3 + h2_2[4] * k4 + h2_2[5] * k5 + integrator.k[j] = @.. integrator.k[j] + H[j, i] * ks[i] end - if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && - (integrator.EEst < 1.0) && integrator.kshortsize == 3 - k2 = 0.5 * (uprev + u + - 0.5 * (integrator.k[1] + 0.5 * (integrator.k[2] + 0.5 * integrator.k[3]))) - du1 = (0.25 * (integrator.k[2] + integrator.k[3]) - uprev + u) / dt - du = f(k2, p, t + dt / 2) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - if mass_matrix === I - du2 = du1 - du - else - du2 = mass_matrix * du1 - du - end - EEst = norm(du2) / norm(integrator.opts.abstol .+ integrator.opts.reltol .* k2) - integrator.EEst = max(EEst, integrator.EEst) + end + if hasproperty(cache.tab, :h2_2) && integrator.kshortsize == 3 + integrator.k[3] = h2_2[1] * k1 + h2_2[2] * k2 + h2_2[3] * k3 + h2_2[4] * k4 + h2_2[5] * k5 + end + if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && + (integrator.EEst < 1.0) && integrator.kshortsize == 3 + k2 = 0.5 * (uprev + u + + 0.5 * (integrator.k[1] + 0.5 * (integrator.k[2] + 0.5 * integrator.k[3]))) + du1 = (0.25 * (integrator.k[2] + integrator.k[3]) - uprev + u) / dt + du = f(k2, p, t + dt / 2) + OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) + if mass_matrix === I + du2 = du1 - du + else + du2 = mass_matrix * du1 - du end - - if integrator.opts.adaptive && integrator.kshortsize == 3 - if isa(linsolve_tmp, AbstractFloat) - u_int, u_diff = calculate_interpoldiff( - uprev, du, u, integrator.k[1], integrator.k[2], integrator.k[3]) - else - u_int = linsolve_tmp - u_diff = linsolve_tmp .+ 0 - calculate_interpoldiff!(u_int, u_diff, uprev, du, u, integrator.k[1], - integrator.k[2], integrator.k[3]) - end - atmp = calculate_residuals(u_diff, uprev, u_int, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - EEst = max(EEst, integrator.opts.internalnorm(atmp, t)) #-- role of t unclear + EEst = norm(du2) / norm(integrator.opts.abstol .+ integrator.opts.reltol .* k2) + integrator.EEst = max(EEst, integrator.EEst) + end + + if integrator.opts.adaptive && integrator.kshortsize == 3 + if isa(linsolve_tmp, AbstractFloat) + u_int, u_diff = calculate_interpoldiff( + uprev, du, u, integrator.k[1], integrator.k[2], integrator.k[3]) + else + u_int = linsolve_tmp + u_diff = linsolve_tmp .+ 0 + calculate_interpoldiff!(u_int, u_diff, uprev, du, u, integrator.k[1], + integrator.k[2], integrator.k[3]) end + atmp = calculate_residuals(u_diff, uprev, u_int, integrator.opts.abstol, + integrator.opts.reltol, integrator.opts.internalnorm, t) + EEst = max(EEst, integrator.opts.internalnorm(atmp, t)) #-- role of t unclear + end - if (integrator.alg isa Rodas23W) && integrator.kshortsize == 3 - k1 = u .+ 0 - u = du .+ 0 - du = k1 .+ 0 - if integrator.opts.calck - integrator.k[1] = integrator.k[3] .+ 0 - integrator.k[2] = 0 * integrator.k[2] - end + if (integrator.alg isa Rodas23W) && integrator.kshortsize == 3 + k1 = u .+ 0 + u = du .+ 0 + du = k1 .+ 0 + if integrator.opts.calck + integrator.k[1] = integrator.k[3] .+ 0 + integrator.k[2] = 0 * integrator.k[2] end end +end - integrator.k[1] = integrator.fsalfirst - integrator.k[2] = integrator.fsallast - integrator.u = u - return nothing +integrator.k[1] = integrator.fsalfirst +integrator.k[2] = integrator.fsallast +integrator.u = u +return nothing end function initialize!(integrator, cache::RosenbrockCache) - integrator.kshortsize = cache.order == 5 ? 3 : 2 - resize!(integrator.k, integrator.kshortsize) - for i in 1:integrator.kshortsize - integrator.k[i] = cache.dense[i] - end +integrator.kshortsize = cache.order == 5 ? 3 : 2 +resize!(integrator.k, integrator.kshortsize) +for i in 1:integrator.kshortsize + integrator.k[i] = cache.dense[i] +end end @muladd function perform_step!(integrator, cache::RosenbrockCache, repeat_step = false) - (; t, dt, uprev, u, f, p) = integrator - (; du, du1, du2, dT, J, W, uf, tf, ks, linsolve_tmp, jac_config, atmp, weight, stage_limiter!, step_limiter!) = cache - (; A, C, gamma, c, d, H) = cache.tab +(; t, dt, uprev, u, f, p) = integrator +(; du, du1, du2, dT, J, W, uf, tf, ks, linsolve_tmp, jac_config, atmp, weight, stage_limiter!, step_limiter!) = cache +(; A, C, gamma, c, d, H) = cache.tab - if hasproperty(cache.tab, :b) - b = cache.tab.b - end +if hasproperty(cache.tab, :b) + b = cache.tab.b +end - if hasproperty(cache.tab, :btilde) - btilde = cache.tab.btilde - end - # Assignments - sizeu = size(u) - uidx = eachindex(integrator.uprev) - mass_matrix = integrator.f.mass_matrix - utilde = du - - # Precalculations - dtC = C .* inv(dt) - dtd = dt .* d - dtgamma = dt * gamma - - f(cache.fsalfirst, uprev, p, t) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) +if hasproperty(cache.tab, :c₃₂) + c₃₂ = cache.tab.c₃₂ +end - calc_rosenbrock_differentiation!(integrator, cache, dtd[1], dtgamma, repeat_step, true) +if hasproperty(cache.tab, :btilde) + btilde = cache.tab.btilde +end +# Assignments +sizeu = size(u) +uidx = eachindex(integrator.uprev) +mass_matrix = integrator.f.mass_matrix +utilde = du + +# Precalculations +dtC = C .* inv(dt) +dtd = dt .* d +dtgamma = dt * gamma + +dtγ = dt * d +neginvdtγ = -inv(dtγ) +dto2 = dt / 2 +dto6 = dt / 6 + +f(cache.fsalfirst, uprev, p, t) +OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) + +calc_rosenbrock_differentiation!(integrator, cache, dtd[1], dtgamma, repeat_step, true) + +calculate_residuals!(weight, fill!(weight, one(eltype(u))), uprev, uprev, + integrator.opts.abstol, integrator.opts.reltol, + integrator.opts.internalnorm, t) + +if repeat_step + linres = dolinsolve( + integrator, cache.linsolve; A = nothing, b = _vec(linsolve_tmp), + du = cache.fsalfirst, u = u, p = p, t = t, weight = weight, + solverdata = (; gamma = dtgamma)) +else + linres = dolinsolve(integrator, cache.linsolve; A = W, b = _vec(linsolve_tmp), + du = cache.fsalfirst, u = u, p = p, t = t, weight = weight, + solverdata = (; gamma = dtgamma)) +end - calculate_residuals!(weight, fill!(weight, one(eltype(u))), uprev, uprev, - integrator.opts.abstol, integrator.opts.reltol, - integrator.opts.internalnorm, t) +@.. $(_vec(ks[1])) = -linres.u +integrator.stats.nsolve += 1 - if repeat_step - linres = dolinsolve( - integrator, cache.linsolve; A = nothing, b = _vec(linsolve_tmp), - du = cache.fsalfirst, u = u, p = p, t = t, weight = weight, - solverdata = (; gamma = dtgamma)) - else - linres = dolinsolve(integrator, cache.linsolve; A = W, b = _vec(linsolve_tmp), - du = cache.fsalfirst, u = u, p = p, t = t, weight = weight, - solverdata = (; gamma = dtgamma)) +for stage in 2:length(ks) + u .= uprev + for i in 1:(stage - 1) + @.. u += A[stage, i] * ks[i] end - @.. $(_vec(ks[1])) = -linres.u - integrator.stats.nsolve += 1 + stage_limiter!(u, integrator, p, t + c[stage] * dt) + f(du, u, p, t + c[stage] * dt) + OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - for stage in 2:length(ks) - u .= uprev + du1 .= 0 + if mass_matrix === I for i in 1:(stage - 1) - @.. u += A[stage, i] * ks[i] + @.. du1 += dtC[stage, i] * ks[i] end - - stage_limiter!(u, integrator, p, t + c[stage] * dt) - f(du, u, p, t + c[stage] * dt) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - - du1 .= 0 - if mass_matrix === I - for i in 1:(stage - 1) - @.. du1 += dtC[stage, i] * ks[i] - end - else - for i in 1:(stage - 1) - @.. du1 += dtC[stage, i] * ks[i] - end - mul!(_vec(du2), mass_matrix, _vec(du1)) - du1 .= du2 + else + for i in 1:(stage - 1) + @.. du1 += dtC[stage, i] * ks[i] end - @.. linsolve_tmp = du + dtd[stage] * dT + du1 - - linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) - @.. $(_vec(ks[stage])) = -linres.u - integrator.stats.nsolve += 1 + mul!(_vec(du2), mass_matrix, _vec(du1)) + du1 .= du2 end - du .= ks[end] - u .+= ks[end] + @.. linsolve_tmp = du + dtd[stage] * dT + du1 - if hasproperty(cache.tab, :b) - @.. u=uprev + b[1] * ks[1] + b[2] * ks[2] + b[3] * ks[3] - end + linres = dolinsolve(integrator, linres.cache; b = _vec(linsolve_tmp)) + @.. $(_vec(ks[stage])) = -linres.u + integrator.stats.nsolve += 1 +end +du .= ks[end] +u .+= ks[end] - step_limiter!(u, integrator, p, t + dt) +if hasproperty(cache.tab, :b) + @.. u=uprev + b[1] * ks[1] + b[2] * ks[2] + b[3] * ks[3] +end - if integrator.opts.adaptive - if hasproperty(cache.tab, :btilde) - @.. utilde=btilde[1] * ks[1] + btilde[2] * ks[2] + btilde[3] * ks[3] - end - - if (integrator.alg isa Rodas5Pe) - @.. du = 0.2606326497975715 * ks[1] - 0.005158627295444251 * ks[2] + - 1.3038988631109731 * ks[3] + 1.235000722062074 * ks[4] + - -0.7931985603795049 * ks[5] - 1.005448461135913 * ks[6] - - 0.18044626132120234 * ks[7] + 0.17051519239113755 * ks[8] - end - calculate_residuals!(atmp, ks[end], uprev, u, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - integrator.EEst = integrator.opts.internalnorm(atmp, t) - end +step_limiter!(u, integrator, p, t + dt) - if integrator.opts.calck +if integrator.opts.adaptive + if hasproperty(cache.tab, :btilde) + @.. utilde=btilde[1] * ks[1] + btilde[2] * ks[2] + btilde[3] * ks[3] + end + + if (integrator.alg isa Rodas5Pe) + @.. du = 0.2606326497975715 * ks[1] - 0.005158627295444251 * ks[2] + + 1.3038988631109731 * ks[3] + 1.235000722062074 * ks[4] + + -0.7931985603795049 * ks[5] - 1.005448461135913 * ks[6] - + 0.18044626132120234 * ks[7] + 0.17051519239113755 * ks[8] + end + calculate_residuals!(atmp, ks[end], uprev, u, integrator.opts.abstol, + integrator.opts.reltol, integrator.opts.internalnorm, t) + integrator.EEst = integrator.opts.internalnorm(atmp, t) +end + +if integrator.opts.calck + for j in eachindex(integrator.k) + integrator.k[j] .= 0 + end + for i in eachindex(ks) for j in eachindex(integrator.k) - integrator.k[j] .= 0 + @.. integrator.k[j] += H[j, i] * ks[i] end - for i in eachindex(ks) - for j in eachindex(integrator.k) - @.. integrator.k[j] += H[j, i] * ks[i] - end + end + if (integrator.alg isa Rodas23W) + du1[:] = u[:] + u[:] = du[:] + du[:] = du1[:] + if integrator.opts.calck + integrator.k[1][:] = integrator.k[3][:] + integrator.k[2][:] .= 0.0 end - if (integrator.alg isa Rodas23W) - du1[:] = u[:] - u[:] = du[:] - du[:] = du1[:] - if integrator.opts.calck - integrator.k[1][:] = integrator.k[3][:] - integrator.k[2][:] .= 0.0 - end + end + if integrator.opts.adaptive + if integrator.kshortsize == 3 + calculate_interpoldiff!( + du1, du2, uprev, du, u, integrator.k[1], integrator.k[2], integrator.k[3]) end - if integrator.opts.adaptive - if integrator.kshortsize == 3 - calculate_interpoldiff!( - du1, du2, uprev, du, u, integrator.k[1], integrator.k[2], integrator.k[3]) - end - calculate_residuals!(atmp, du2, uprev, du1, integrator.opts.abstol, - integrator.opts.reltol, integrator.opts.internalnorm, t) - integrator.EEst = max(integrator.EEst, integrator.opts.internalnorm(atmp, t)) #-- role of t unclear + calculate_residuals!(atmp, du2, uprev, du1, integrator.opts.abstol, + integrator.opts.reltol, integrator.opts.internalnorm, t) + integrator.EEst = max(integrator.EEst, integrator.opts.internalnorm(atmp, t)) #-- role of t unclear + end + + if integrator.opts.adaptive + @.. broadcast=false tmp=dto6 * (k₁ - 2 * k₂ + k₃) + calculate_residuals!(atmp, tmp, uprev, u, integrator.opts.abstol, + integrator.opts.reltol, integrator.opts.internalnorm, t) + integrator.EEst = integrator.opts.internalnorm(atmp, t) + + if mass_matrix !== I + invatol = inv(integrator.opts.abstol) + @.. atmp = ifelse(cache.algebraic_vars, fsallast, false) * invatol + integrator.EEst += integrator.opts.internalnorm(atmp, t) end - if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && - (integrator.EEst < 1.0) && integrator.kshortsize == 3 - ks[2] = 0.5 * (uprev + u + - 0.5 * (integrator.k[1] + 0.5 * (integrator.k[2] + 0.5 * integrator.k[3]))) - du1 = (0.25 * (integrator.k[2] + integrator.k[3]) - uprev + u) / dt - f(du, ks[2], p, t + dt / 2) - OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) - if mass_matrix === I - du2 = du1 - du - else - mul!(_vec(du2), mass_matrix, _vec(du1)) - du2 = du2 - du - end - EEst = norm(du2) / norm(integrator.opts.abstol .+ integrator.opts.reltol .* ks[2]) - integrator.EEst = max(EEst, integrator.EEst) - end end - cache.linsolve = linres.cache + + if (integrator.alg isa Rodas5Pr) && integrator.opts.adaptive && + (integrator.EEst < 1.0) && integrator.kshortsize == 3 + ks[2] = 0.5 * (uprev + u + + 0.5 * (integrator.k[1] + 0.5 * (integrator.k[2] + 0.5 * integrator.k[3]))) + du1 = (0.25 * (integrator.k[2] + integrator.k[3]) - uprev + u) / dt + f(du, ks[2], p, t + dt / 2) + OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) + if mass_matrix === I + du2 = du1 - du + else + mul!(_vec(du2), mass_matrix, _vec(du1)) + du2 = du2 - du + end + EEst = norm(du2) / norm(integrator.opts.abstol .+ integrator.opts.reltol .* ks[2]) + integrator.EEst = max(EEst, integrator.EEst) + end +end +cache.linsolve = linres.cache end @RosenbrockW6S4OS(:init) -@RosenbrockW6S4OS(:performstep) +@RosenbrockW6S4OS(:performstep) \ No newline at end of file From 6e21bc36a0685e91f95d9b137066a4b51f6f1a8c Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Mon, 16 Sep 2024 19:32:00 +0530 Subject: [PATCH 54/56] Fixes --- lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl | 2 +- lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl | 2 +- lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl b/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl index 0fcf67e4d8..44156fdde5 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/interp_func.jl @@ -4,7 +4,7 @@ function DiffEqBase.interp_summary(::Type{cacheType}, Union{Rosenbrock23ConstantCache, Rosenbrock32ConstantCache, RosenbrockCache, - Rosenbrock32Cache}} + RosenbrockCache}} dense ? "specialized 2nd order \"free\" stiffness-aware interpolation" : "1st order linear" end diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl index 69ee62f03c..c5966d4c94 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_interpolants.jl @@ -113,7 +113,7 @@ end @muladd function _ode_interpolant!(out, Θ, dt, y₀, y₁, k, cache::Union{Rosenbrock23ConstantCache, RosenbrockCache, - Rosenbrock32ConstantCache, Rosenbrock32Cache + Rosenbrock32ConstantCache, RosenbrockCache }, idxs, T::Type{Val{1}}, differential_vars) @rosenbrock2332pre1 @views @.. out=c1diff * k[1][idxs] + c2diff * k[2][idxs] diff --git a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl index 3c9c29da2e..d6d2127639 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl @@ -44,7 +44,7 @@ function _ode_addsteps!(k, t, uprev, u, dt, f, p, end function _ode_addsteps!(k, t, uprev, u, dt, f, p, - cache::Union{RosenbrockCache, Rosenbrock32Cache}, + cache::Union{RosenbrockCache}, always_calc_begin = false, allow_calc_end = true, force_calc_end = false) if length(k) < 2 || always_calc_begin From 579d9938877447fa479d2b2d9c7ae017befa5983 Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Mon, 16 Sep 2024 20:05:18 +0530 Subject: [PATCH 55/56] Fixes --- lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl index b25b63e35e..c5723a2b40 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/rosenbrock_perform_step.jl @@ -1,5 +1,5 @@ function initialize!(integrator, cache::Union{RosenbrockCache, - Rosenbrock32Cache}) + RosenbrockCache}) integrator.kshortsize = 2 @unpack k₁, k₂, fsalfirst, fsallast = cache resize!(integrator.k, integrator.kshortsize) From 3e9427ba1c1781a7b6a5d8dcda90f789e8d2913c Mon Sep 17 00:00:00 2001 From: ParamThakkar123 Date: Wed, 18 Sep 2024 19:52:19 +0530 Subject: [PATCH 56/56] Fixes --- lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl index 51ec9a84f6..079598a7d7 100644 --- a/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl +++ b/lib/OrdinaryDiffEqRosenbrock/src/stiff_addsteps.jl @@ -240,4 +240,4 @@ function _ode_addsteps!(k, t, uprev, u, dt, f, p, cache::RosenbrockCache, end end nothing -end +end \ No newline at end of file