Skip to content

Commit

Permalink
Add Taylor expansions to TaylorIntegration cache types
Browse files Browse the repository at this point in the history
  • Loading branch information
PerezHz committed Dec 20, 2024
1 parent 3523978 commit 4a01df0
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 222 deletions.
236 changes: 153 additions & 83 deletions src/integrator/cache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,108 @@ struct ScalarCache{TV, XV, PSOL, T, X} <: AbstractTaylorIntegrationCache
x::X
end

function init_cache(cachetype::Type{ScalarCache}, dense::Val{D}, t0::T, x0::U, maxsteps::Int, order::Int) where {D, U, T}
# Initialize the Taylor1 expansions
# VectorCache

struct VectorCache{TV, XV, PSOL, XAUX, T, X, DX} <: AbstractVectorCache
tv::TV
xv::XV
psol::PSOL
xaux::XAUX
t::T
x::X
dx::DX
end

# VectorTRangeCache

struct VectorTRangeCache{TV, XV, PSOL, XAUX, X0, X1, T, X, DX} <: AbstractVectorCache
tv::TV
xv::XV
psol::PSOL
xaux::XAUX
x0::X0
x1::X1
t::T
x::X
dx::DX
end

# LyapunovSpectrumCache

struct LyapunovSpectrumCache{TV, XV, PSOL, XAUX, X0, Λ, ΛTSUM, ΔX, DΔX, JAC, VARSAUX, QQH, RRH, AJ, QI, VJ, T, X, DX, JT, DVARS} <: AbstractVectorCache
tv::TV
xv::XV
psol::PSOL
xaux::XAUX
x0::X0
λ::Λ
λtsum::ΛTSUM
δx::ΔX
dδx::DΔX
jac::JAC
varsaux::VARSAUX
QH::QQH
RH::RRH
aⱼ::AJ
qᵢ::QI
vⱼ::VJ
t::T
x::X
dx::DX
jt::JT
dvars::DVARS
end

# LyapunovSpectrumTRangeCache

struct LyapunovSpectrumTRangeCache{TV, XV, PSOL, XAUX, X0, Q1, Λ, ΛTSUM, ΔX, DΔX, JAC, VARSAUX, QQH, RRH, AJ, QI, VJ, T, X, DX, JT, DVARS} <: AbstractVectorCache
tv::TV
xv::XV
psol::PSOL
xaux::XAUX
x0::X0
q1::Q1
λ::Λ
λtsum::ΛTSUM
δx::ΔX
dδx::DΔX
jac::JAC
varsaux::VARSAUX
QH::QQH
RH::RRH
aⱼ::AJ
qᵢ::QI
vⱼ::VJ
t::T
x::X
dx::DX
jt::JT
dvars::DVARS
end

# init_expansions

function init_expansions(t0::T, x0::U, order::Int) where {T, U}
t = t0 + Taylor1( T, order )
x = Taylor1( x0, order )
return t, x
end

function init_expansions(t0::T, q0::Vector{U}, order::Int) where {T, U}
dof = length(q0)
t = t0 + Taylor1( T, order )
x = Array{Taylor1{U}}(undef, dof)
dx = Array{Taylor1{U}}(undef, dof)
x .= Taylor1.( q0, order )
dx .= Taylor1.( zero.(q0), order )
return t, x, dx
end

# init_cache

function init_cache(cachetype::Type{ScalarCache}, dense::Val{D}, t0::T, x0::U, maxsteps::Int, order::Int) where {D, U, T}
# Initialize the Taylor1 expansions
t, x = init_expansions(t0, x0, order)
# Initialize cache
return cachetype(
Array{T}(undef, maxsteps + 1),
Expand All @@ -40,8 +138,7 @@ end
function init_cache(cachetype::Type{ScalarCache}, ::Val{false}, trange::AbstractVector{T}, x0::U, maxsteps::Int, order::Int) where {U, T}
# Initialize the Taylor1 expansions
t0 = trange[1]
t = t0 + Taylor1( T, order )
x = Taylor1( x0, order )
t, x = init_expansions(t0, x0, order)
# Initialize cache
nn = length(trange)
cache = cachetype(
Expand All @@ -54,82 +151,60 @@ function init_cache(cachetype::Type{ScalarCache}, ::Val{false}, trange::Abstract
return cache
end

# VectorCache

struct VectorCache{TV, XV, PSOL, XAUX} <: AbstractVectorCache
tv::TV
xv::XV
psol::PSOL
xaux::XAUX
end

function init_cache(cachetype::Type{VectorCache}, dense::Val{D}, t0::T, x::Vector{Taylor1{U}}, maxsteps::Int) where {D, U, T}
dof = length(x)
function init_cache(cachetype::Type{VectorCache}, dense::Val{D}, t0::T, q0::Vector{U}, maxsteps::Int, order::Int) where {D, U, T}
# Initialize the vector of Taylor1 expansions
t, x, dx = init_expansions(t0, q0, order)
# Initialize cache
dof = length(q0)
return cachetype(
Array{T}(undef, maxsteps + 1),
Array{U}(undef, dof, maxsteps + 1),
init_psol(dense, maxsteps, dof, x),
Array{Taylor1{U}}(undef, dof))
end

# VectorTRangeCache

struct VectorTRangeCache{TV, XV, PSOL, XAUX, X0, X1} <: AbstractVectorCache
tv::TV
xv::XV
psol::PSOL
xaux::XAUX
x0::X0
x1::X1
Array{Taylor1{U}}(undef, dof),
t,
x,
dx)
end

function init_cache(cachetype::Type{VectorTRangeCache}, ::Val{false}, trange::AbstractVector{T}, x::Vector{Taylor1{U}}, maxsteps::Int) where {U, T}
function init_cache(cachetype::Type{VectorTRangeCache}, ::Val{false}, trange::AbstractVector{T}, q0::Vector{U}, maxsteps::Int, order::Int) where {U, T}
# Initialize the vector of Taylor1 expansions
t0 = trange[1]
t, x, dx = init_expansions(t0, q0, order)
# Initialize cache
nn = length(trange)
dof = length(x)
dof = length(q0)
cache = cachetype(
trange,
Array{U}(undef, dof, nn),
init_psol(Val(false), maxsteps, dof, x),
Array{Taylor1{U}}(undef, dof),
similar(constant_term.(x)),
similar(constant_term.(x)))
similar(q0),
similar(q0),
t,
x,
dx)
fill!(cache.x0, T(NaN))
for ind in 1:nn
@inbounds cache.xv[:,ind] .= cache.x0
end
return cache
end

# LyapunovSpectrumCache

struct LyapunovSpectrumCache{TV, XV, PSOL, XAUX, X0, Λ, ΛTSUM, ΔX, DΔX, JAC, VARSAUX, QQH, RRH, AJ, QI, VJ} <: AbstractVectorCache
tv::TV
xv::XV
psol::PSOL
xaux::XAUX
x0::X0
λ::Λ
λtsum::ΛTSUM
δx::ΔX
dδx::DΔX
jac::JAC
varsaux::VARSAUX
QH::QQH
RH::RRH
aⱼ::AJ
qᵢ::QI
vⱼ::VJ
end

function init_cache(cachetype::Type{LyapunovSpectrumCache}, dense, t0::T, x::Vector{Taylor1{U}}, maxsteps::Int) where {U, T}
nx0 = length(x) # equals dof + dof^2
dof = Int(sqrt(nx0 + 1/4) - 1/2)
function init_cache(cachetype::Type{LyapunovSpectrumCache}, dense, t0::T, q0::Vector{U}, maxsteps::Int, order::Int) where {U, T}
# Initialize the vector of Taylor1 expansions
dof = length(q0)
jt = Matrix{U}(I, dof, dof)
x0 = vcat(q0, reshape(jt, dof*dof))
t, x, dx = init_expansions(t0, x0, order)
# Initialize cache
nx0 = length(x0)
dvars = Array{TaylorN{Taylor1{U}}}(undef, dof)
cache = cachetype(
Array{T}(undef, maxsteps+1),
Array{U}(undef, dof, maxsteps+1),
nothing,
Array{Taylor1{U}}(undef, nx0),
getcoeff.(x, 0),
x0,
Array{U}(undef, dof, maxsteps+1),
similar(constant_term.(x[1:dof])),
Array{TaylorN{Taylor1{U}}}(undef, dof),
Expand All @@ -140,38 +215,28 @@ function init_cache(cachetype::Type{LyapunovSpectrumCache}, dense, t0::T, x::Vec
Array{U}(undef, dof, dof),
Array{U}(undef, dof),
Array{U}(undef, dof),
Array{U}(undef, dof)
Array{U}(undef, dof),
t,
x,
dx,
jt,
dvars
)
fill!(cache.jac, zero(x[1]))
return cache
end

# LyapunovSpectrumTRangeCache

struct LyapunovSpectrumTRangeCache{TV, XV, PSOL, XAUX, X0, Q1, Λ, ΛTSUM, ΔX, DΔX, JAC, VARSAUX, QQH, RRH, AJ, QI, VJ} <: AbstractVectorCache
tv::TV
xv::XV
psol::PSOL
xaux::XAUX
x0::X0
q1::Q1
λ::Λ
λtsum::ΛTSUM
δx::ΔX
dδx::DΔX
jac::JAC
varsaux::VARSAUX
QH::QQH
RH::RRH
aⱼ::AJ
qᵢ::QI
vⱼ::VJ
end

function init_cache(cachetype::Type{LyapunovSpectrumTRangeCache}, dense, trange::AbstractVector{T}, x::Vector{Taylor1{U}}, maxsteps::Int) where {U, T}
nx0 = length(x) # equals dof + dof^2
dof = Int(sqrt(nx0 + 1/4) - 1/2)
function init_cache(cachetype::Type{LyapunovSpectrumTRangeCache}, dense, trange::AbstractVector{T}, q0::Vector{U}, maxsteps::Int, order::Int) where {U, T}
# Initialize the vector of Taylor1 expansions
t0 = trange[1]
dof = length(q0)
jt = Matrix{U}(I, dof, dof)
x0 = vcat(q0, reshape(jt, dof*dof))
t, x, dx = init_expansions(t0, x0, order)
# Initialize cache
nn = length(trange)
nx0 = length(x0)
dvars = Array{TaylorN{Taylor1{U}}}(undef, dof)
cache = cachetype(
trange,
Array{U}(undef, dof, nn),
Expand All @@ -189,7 +254,12 @@ function init_cache(cachetype::Type{LyapunovSpectrumTRangeCache}, dense, trange:
Array{U}(undef, dof, dof),
Array{U}(undef, dof),
Array{U}(undef, dof),
Array{U}(undef, dof)
Array{U}(undef, dof),
t,
x,
dx,
jt,
dvars
)
fill!(cache.xv, U(NaN))
fill!(cache.λ, U(NaN))
Expand Down
Loading

0 comments on commit 4a01df0

Please sign in to comment.