Skip to content

Commit

Permalink
Add missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
PerezHz committed Apr 1, 2024
1 parent 46fb102 commit f1995d4
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/interpolation/PlanetaryEphemerisSerialization.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Custom serialization

@doc raw"""
PlanetaryEphemerisSerialization{T}
Custom serialization struct to save a `TaylorInterpolant{T, T, 2}` to a `.jld2` file.
# Fields
- `order::Int`: order of Taylor polynomials.
- `dims::Tuple{Int, Int}`: matrix dimensions.
- `t0::T`: initial time.
- `t::Vector{T}`: vector of times.
- `x::Vector{T}`: vector of coefficients.
"""
struct PlanetaryEphemerisSerialization{T}
order::Int
dims::Tuple{Int, Int}
t0::T
t::Vector{T}
x::Vector{T}
end

# Tell JLD2 to save TaylorInterpolant{T, T, 2} as PlanetaryEphemerisSerialization{T}
writeas(::Type{TaylorInterpolant{T, T, 2}}) where {T<:Real} = PlanetaryEphemerisSerialization{T}

# Convert method to write .jld2 files
function convert(::Type{PlanetaryEphemerisSerialization{T}}, eph::TaylorInterpolant{T, T, 2}) where {T <: Real}
# Taylor polynomials order
order = eph.x[1, 1].order
# Number of coefficients in each polynomial
k = order + 1
# Matrix dimensions
dims = size(eph.x)
# Number of elements in matrix
N = dims[1] * dims[2]
# Vector of coefficients
x = Vector{T}(undef, k * N)
# Save coefficients
for i in 1:N
x[(i-1)*k+1 : i*k] = eph.x[i].coeffs
end

return PlanetaryEphemerisSerialization{T}(order, dims, eph.t0, eph.t, x)
end

# Convert method to read .jld2 files
function convert(::Type{TaylorInterpolant{T, T, 2}}, eph::PlanetaryEphemerisSerialization{T}) where {T<:Real}
# Taylor polynomials order
order = eph.order
# Number of coefficients in each polynomial
k = order + 1
# Matrix dimensions
dims = eph.dims
# Number of elements in matrix
N = dims[1] * dims[2]
# Matrix of Taylor polynomials
x = Matrix{Taylor1{T}}(undef, dims[1], dims[2])
# Reconstruct Taylor polynomials
for i in 1:N
x[i] = Taylor1{T}(eph.x[(i-1)*k+1 : i*k], order)
end

return TaylorInterpolant{T, T, 2}(eph.t0, eph.t, x)
end
123 changes: 123 additions & 0 deletions src/interpolation/TaylorInterpolantNSerialization.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
@doc raw"""
TaylorInterpolantNSerialization{T}
Custom serialization struct to save a `TaylorInterpolant{T, TaylorN{T}, 2}` to a `.jld2` file.
# Fields
- `vars::Vector{String}`: jet transport variables.
- `order::Int`: order of Taylor polynomials w.r.t time.
- `varorder::Int`: order of jet transport perturbations.
- `dims::Tuple{Int, Int}`: matrix dimensions.
- `t0::T`: initial time.
- `t::Vector{T}`: vector of times.
- `x::Vector{T}`: vector of coefficients.
"""
struct TaylorInterpolantNSerialization{T}
vars::Vector{String}
order::Int
varorder::Int
dims::Tuple{Int, Int}
t0::T
t::Vector{T}
x::Vector{T}
end

# Tell JLD2 to save TaylorInterpolant{T, TaylorN{T}, 2} as TaylorInterpolantNSerialization{T}
writeas(::Type{TaylorInterpolant{T, TaylorN{T}, 2}}) where {T} = TaylorInterpolantNSerialization{T}

# Convert method to write .jld2 files
function convert(::Type{TaylorInterpolantNSerialization{T}}, eph::TaylorInterpolant{T, TaylorN{T}, 2}) where {T}
# Variables
vars = TS.get_variable_names()
# Number of variables
n = length(vars)
# Matrix dimensions
dims = size(eph.x)
# Number of elements in matrix
N = length(eph.x)
# Taylor1 order
order = eph.x[1, 1].order
# Number of coefficients in each Taylor1
k = order + 1
# TaylorN order
varorder = eph.x[1, 1].coeffs[1].order
# Number of coefficients in each TaylorN
L = varorder + 1
# Number of coefficients in each HomogeneousPolynomial
M = binomial(n + varorder, varorder)
# M = sum(binomial(n + i_3 - 1, i_3) for i_3 in 0:varorder)

# Vector of coefficients
x = Vector{T}(undef, N * k * M)

# Save coefficients
i = 1
# Iterate over matrix elements
for i_1 in 1:N
# Iterate over Taylor1 coefficients
for i_2 in 1:k
# Iterate over TaylorN coefficients
for i_3 in 0:varorder
# Iterate over i_3 order HomogeneousPolynomial
for i_4 in 1:binomial(n + i_3 - 1, i_3)
x[i] = eph.x[i_1].coeffs[i_2].coeffs[i_3+1].coeffs[i_4]
i += 1
end
end
end
end

return TaylorInterpolantNSerialization{T}(vars, order, varorder, dims, eph.t0, eph.t, x)
end

# Convert method to read .jld2 files
function convert(::Type{TaylorInterpolant{T, TaylorN{T}, 2}}, eph::TaylorInterpolantNSerialization{T}) where {T}
# Variables
vars = eph.vars
# Number of variables
n = length(vars)
# Matrix dimensions
dims = eph.dims
# Number of elements in matrix
N = dims[1] * dims[2]
# Taylor1 order
order = eph.order
# Number of coefficients in each Taylor1
k = order + 1
# TaylorN order
varorder = eph.varorder
# Number of coefficients in each TaylorN
L = varorder + 1
# Number of coefficients in each HomogeneousPolynomial
M = binomial(n + varorder, varorder)
# M = sum(binomial(n + i_3 - 1, i_3) for i_3 in 0:varorder)

# Set variables
if TS.get_variable_names() != vars
TS.set_variables(T, vars, order = varorder)
end

# Matrix of Taylor polynomials
x = Matrix{Taylor1{TaylorN{T}}}(undef, dims[1], dims[2])

# Reconstruct Taylor polynomials
i = 1
# Iterate over matrix elements
for i_1 in 1:N
# Reconstruct Taylor1s
Taylor1_coeffs = Vector{TaylorN{T}}(undef, k)
for i_2 in 1:k
# Reconstruct TaylorNs
TaylorN_coeffs = Vector{HomogeneousPolynomial{T}}(undef, L)
# Reconstruct HomogeneousPolynomials
for i_3 in 0:varorder
TaylorN_coeffs[i_3 + 1] = HomogeneousPolynomial(eph.x[i : i + binomial(n + i_3 - 1, i_3)-1], i_3)
i += binomial(n + i_3 - 1, i_3)
end
Taylor1_coeffs[i_2] = TaylorN(TaylorN_coeffs, varorder)
end
x[i_1] = Taylor1{TaylorN{T}}(Taylor1_coeffs, order)
end

return TaylorInterpolant{T, TaylorN{T}, 2}(eph.t0, eph.t, x)
end

0 comments on commit f1995d4

Please sign in to comment.