diff --git a/src/interpolation/PlanetaryEphemerisSerialization.jl b/src/interpolation/PlanetaryEphemerisSerialization.jl new file mode 100644 index 0000000..67dd4bc --- /dev/null +++ b/src/interpolation/PlanetaryEphemerisSerialization.jl @@ -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 diff --git a/src/interpolation/TaylorInterpolantNSerialization.jl b/src/interpolation/TaylorInterpolantNSerialization.jl new file mode 100644 index 0000000..88c5924 --- /dev/null +++ b/src/interpolation/TaylorInterpolantNSerialization.jl @@ -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 \ No newline at end of file