-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TaylorInterpolant JLD2 serialization methods (#36)
* Update CI.yml * Move TaylorInterpolantNSerialization over from NEOs * Add missing files * Update tests * Scattered fixes * Uncomment tests * Update tests
- Loading branch information
Showing
9 changed files
with
242 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# 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} | ||
function writeas(::Type{<:TaylorInterpolant{T, T, 2, Vector{T}, Matrix{Taylor1{T}}}}) where {T<:Real} | ||
return PlanetaryEphemerisSerialization{T} | ||
end | ||
|
||
# Convert method to write .jld2 files | ||
function convert(::Type{PlanetaryEphemerisSerialization{T}}, eph::TaylorInterpolant{T, T, 2, Vector{T}, Matrix{Taylor1{T}}}) 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, Vector{T}, Matrix{Taylor1{T}}}}, 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 | ||
|
||
function convert(::Type{TaylorInterpolant{T, T, 2}}, eph::PlanetaryEphemerisSerialization{T}) where {T<:Real} | ||
return convert(TaylorInterpolant{T, T, 2, Vector{T}, Matrix{Taylor1{T}}}, eph) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
@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} | ||
function writeas(::Type{<:TaylorInterpolant{T, TaylorN{T}, 2, Vector{T}, Matrix{Taylor1{TaylorN{T}}}}}) where {T<:Real} | ||
return TaylorInterpolantNSerialization{T} | ||
end | ||
|
||
# Convert method to write .jld2 files | ||
function convert(::Type{TaylorInterpolantNSerialization{T}}, eph::TaylorInterpolant{T, TaylorN{T}, 2, Vector{T}, Matrix{Taylor1{TaylorN{T}}}}) 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) | ||
|
||
# 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, Vector{T}, Matrix{Taylor1{TaylorN{T}}}}}, 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 | ||
|
||
function convert(::Type{TaylorInterpolant{T, TaylorN{T}, 2}}, eph::PlanetaryEphemerisSerialization{T}) where {T<:Real} | ||
return convert(TaylorInterpolant{T, TaylorN{T}, 2, Vector{T}, Matrix{Taylor1{TaylorN{T}}}}, eph) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ed4165e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
ed4165e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/104068
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: