-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add JLD2 extension (move over from NEOs) * Add missing files * Bump patch version * Update Project.toml (order fields alphabetically) * Cleanup * Remove leftover comment
- Loading branch information
Showing
6 changed files
with
117 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
module TaylorSeriesJLD2Ext | ||
|
||
import Base: convert | ||
using TaylorSeries | ||
|
||
if isdefined(Base, :get_extension) | ||
import JLD2: writeas | ||
else | ||
import ..JLD2: writeas | ||
end | ||
|
||
@doc raw""" | ||
TaylorNSerialization{T} | ||
Custom serialization struct to save a `TaylorN{T}` to a `.jld2` file. | ||
# Fields | ||
- `vars::Vector{String}`: jet transport variables. | ||
- `varorder::Int`: order of jet transport perturbations. | ||
- `x::Vector{T}`: vector of coefficients. | ||
""" | ||
struct TaylorNSerialization{T} | ||
vars::Vector{String} | ||
varorder::Int | ||
x::Vector{T} | ||
end | ||
|
||
# Tell JLD2 to save TaylorN{T} as TaylorNSerialization{T} | ||
writeas(::Type{TaylorN{T}}) where {T} = TaylorNSerialization{T} | ||
|
||
# Convert method to write .jld2 files | ||
function convert(::Type{TaylorNSerialization{T}}, eph::TaylorN{T}) where {T} | ||
# Variables | ||
vars = TS.get_variable_names() | ||
# Number of variables | ||
n = length(vars) | ||
# TaylorN order | ||
varorder = eph.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, M) | ||
|
||
# Save coefficients | ||
i = 1 | ||
for i_1 in 0:varorder | ||
# Iterate over i_1 order HomogeneousPolynomial | ||
for i_2 in 1:binomial(n + i_1 - 1, i_1) | ||
x[i] = eph.coeffs[i_1+1].coeffs[i_2] | ||
i += 1 | ||
end | ||
end | ||
|
||
return TaylorNSerialization{T}(vars, varorder, x) | ||
end | ||
|
||
# Convert method to read .jld2 files | ||
function convert(::Type{TaylorN{T}}, eph::TaylorNSerialization{T}) where {T} | ||
# Variables | ||
vars = eph.vars | ||
# Number of variables | ||
n = length(vars) | ||
# 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) | ||
|
||
# Set variables | ||
if TS.get_variable_names() != vars | ||
TS.set_variables(T, vars, order = varorder) | ||
end | ||
|
||
# Reconstruct TaylorN | ||
i = 1 | ||
TaylorN_coeffs = Vector{HomogeneousPolynomial{T}}(undef, L) | ||
for i_1 in 0:varorder | ||
# Reconstruct HomogeneousPolynomials | ||
TaylorN_coeffs[i_1 + 1] = HomogeneousPolynomial(eph.x[i : i + binomial(n + i_1 - 1, i_1)-1], i_1) | ||
i += binomial(n + i_1 - 1, i_1) | ||
end | ||
x = TaylorN{T}(TaylorN_coeffs, varorder) | ||
|
||
return x | ||
end | ||
|
||
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,15 @@ | ||
# This file is part of TaylorSeries.jl, MIT licensed | ||
# | ||
|
||
using TaylorSeries, JLD2 | ||
|
||
using Test | ||
|
||
@testset "Test TaylorSeries JLD2 extension" begin | ||
dq = set_variables("q", order=4, numvars=6) | ||
random_TaylorN = [cos(sum(dq .* rand(6))), sin(sum(dq .* rand(6))), tan(sum(dq .* rand(6)))] | ||
jldsave("test.jld2"; random_TaylorN = random_TaylorN) | ||
recovered_taylorN = JLD2.load("test.jld2", "random_TaylorN") | ||
@test recovered_taylorN == random_TaylorN | ||
rm("test.jld2") | ||
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
87116db
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
87116db
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/104025
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: