I/O: Saving and Loading Solution Data
The ability to save and load solutions is important for handling large datasets and analyzing the results over multiple Julia sessions. This page explains the existing functionality for doing so.
Tabular Data: IterableTables
An interface to IterableTables.jl is provided. This IterableTables link allows you to use a solution type as the data source to convert to other tabular data formats. For example, let's solve a 4x2 system of ODEs and get the DataFrame:
import OrdinaryDiffEq as ODE, DataFrames
f_2dlinear = (du, u, p, t) -> du .= 1.01u;
tspan = (0.0, 1.0)
prob = ODE.ODEProblem(f_2dlinear, rand(2, 2), tspan);
sol = ODE.solve(prob, ODE.Euler(); dt = 1 // 2^(4));
df = DataFrames.DataFrame(sol)
Row | timestamp | value1 | value2 | value3 | value4 |
---|---|---|---|---|---|
Float64 | Float64 | Float64 | Float64 | Float64 | |
1 | 0.0 | 0.286961 | 0.214631 | 0.500158 | 0.0100907 |
2 | 0.0625 | 0.305076 | 0.22818 | 0.53173 | 0.0107276 |
3 | 0.125 | 0.324334 | 0.242583 | 0.565295 | 0.0114048 |
4 | 0.1875 | 0.344807 | 0.257896 | 0.60098 | 0.0121247 |
5 | 0.25 | 0.366573 | 0.274176 | 0.638917 | 0.0128901 |
6 | 0.3125 | 0.389713 | 0.291484 | 0.679248 | 0.0137038 |
7 | 0.375 | 0.414314 | 0.309883 | 0.722126 | 0.0145689 |
8 | 0.4375 | 0.440467 | 0.329445 | 0.76771 | 0.0154885 |
9 | 0.5 | 0.468272 | 0.350241 | 0.816172 | 0.0164662 |
10 | 0.5625 | 0.497832 | 0.37235 | 0.867692 | 0.0175057 |
11 | 0.625 | 0.529257 | 0.395855 | 0.922466 | 0.0186107 |
12 | 0.6875 | 0.562667 | 0.420843 | 0.980696 | 0.0197855 |
13 | 0.75 | 0.598185 | 0.447409 | 1.0426 | 0.0210345 |
14 | 0.8125 | 0.635945 | 0.475651 | 1.10842 | 0.0223623 |
15 | 0.875 | 0.676089 | 0.505677 | 1.17839 | 0.0237739 |
16 | 0.9375 | 0.718768 | 0.537598 | 1.25277 | 0.0252746 |
17 | 1.0 | 0.76414 | 0.571533 | 1.33185 | 0.0268701 |
If we set syms
in the DiffEqFunction, then those names will be used:
f = ODE.ODEFunction(f_2dlinear, syms = [:a, :b, :c, :d])
prob = ODE.ODEProblem(f, rand(2, 2), (0.0, 1.0));
sol = ODE.solve(prob, ODE.Euler(); dt = 1 // 2^(4));
df = DataFrames.DataFrame(sol)
Row | timestamp | a | b | c | d |
---|---|---|---|---|---|
Float64 | Float64 | Float64 | Float64 | Float64 | |
1 | 0.0 | 0.0781444 | 0.434769 | 0.989895 | 0.567102 |
2 | 0.0625 | 0.0830773 | 0.462214 | 1.05238 | 0.6029 |
3 | 0.125 | 0.0883215 | 0.491391 | 1.11881 | 0.640958 |
4 | 0.1875 | 0.0938968 | 0.52241 | 1.18944 | 0.681419 |
5 | 0.25 | 0.0998241 | 0.555387 | 1.26452 | 0.724433 |
6 | 0.3125 | 0.106125 | 0.590446 | 1.34435 | 0.770163 |
7 | 0.375 | 0.112825 | 0.627718 | 1.42921 | 0.818779 |
8 | 0.4375 | 0.119947 | 0.667342 | 1.51943 | 0.870465 |
9 | 0.5 | 0.127518 | 0.709468 | 1.61534 | 0.925413 |
10 | 0.5625 | 0.135568 | 0.754254 | 1.71731 | 0.98383 |
11 | 0.625 | 0.144126 | 0.801866 | 1.82571 | 1.04593 |
12 | 0.6875 | 0.153224 | 0.852484 | 1.94096 | 1.11196 |
13 | 0.75 | 0.162896 | 0.906297 | 2.06348 | 1.18215 |
14 | 0.8125 | 0.173179 | 0.963507 | 2.19374 | 1.25677 |
15 | 0.875 | 0.184111 | 1.02433 | 2.33222 | 1.33611 |
16 | 0.9375 | 0.195732 | 1.08899 | 2.47944 | 1.42045 |
17 | 1.0 | 0.208088 | 1.15773 | 2.63596 | 1.51012 |
Many modeling frameworks will automatically set syms
for this feature. Additionally, this data can be saved to a CSV:
import CSV
CSV.write("out.csv", df)
"out.csv"
JLD2 and BSON.jl
JLD2.jl and BSON.jl will work with the full solution type if you bring the required functions back into scope before loading. For example, if we save the solution:
sol = ODE.solve(prob, ODE.Euler(); dt = 1 // 2^(4))
import JLD2
JLD2.@save "out.jld2" sol
┌ Warning: Attempting to store Main.var"#1#2".
│ JLD2 only stores functions by name.
│ This may not be useful for anonymous functions.
└ @ JLD2 ~/.cache/julia-buildkite-plugin/depots/0185fce3-4489-413a-a934-123dd653ef61/packages/JLD2/SgtOb/src/data/writing_datatypes.jl:447
┌ Warning: Attempting to store ODEFunction{true, SciMLBase.FullSpecialize, Main.var"#1#2", LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing, SymbolicIndexingInterface.SymbolCache{Dict{Symbol, Int64}, Nothing, Nothing, Nothing, Dict{Any, Any}}, Nothing, Nothing}.
│ JLD2 only stores functions by name.
│ This may not be useful for anonymous functions.
└ @ JLD2 ~/.cache/julia-buildkite-plugin/depots/0185fce3-4489-413a-a934-123dd653ef61/packages/JLD2/SgtOb/src/data/writing_datatypes.jl:447
┌ Warning: Attempting to store Main.var"#1#2".
│ JLD2 only stores functions by name.
│ This may not be useful for anonymous functions.
└ @ JLD2 ~/.cache/julia-buildkite-plugin/depots/0185fce3-4489-413a-a934-123dd653ef61/packages/JLD2/SgtOb/src/data/writing_datatypes.jl:447
then we can get the full solution type back, interpolations and all, if we load the dependent functions first:
# New session
import JLD2
import OrdinaryDiffEq as ODE
JLD2.@load "out.jld2" sol
1-element Vector{Symbol}:
:sol
The example with BSON.jl is:
sol = ODE.solve(prob, ODE.Euler(); dt = 1 // 2^(4))
import BSON
BSON.bson("test.bson", Dict(:sol => sol))
# New session
import OrdinaryDiffEq as ODE
import BSON
# BSON.load("test.bson") # currently broken: https://github.com/JuliaIO/BSON.jl/issues/109
If you load it without the DE function then for some algorithms the interpolation may not work, and for all algorithms you'll need at least a solver package or SciMLBase.jl in scope in order for the solution interface (plot recipes, array indexing, etc.) to work. If none of these are put into scope, the solution type will still load and hold all of the values (so sol.u
and sol.t
will work), but none of the interface will be available.
If you want a copy of the solution that contains no function information you can use the function SciMLBase.strip_solution(sol)
. This will return a copy of the solution that doesn't have any functions, which you can serialize and deserialize without having any of the problems that typically come with serializing functions.
JLD
Don't use JLD. It's dead. Julia types can be saved via JLD.jl. However, they cannot save types which have functions, which means that the solution type is currently not compatible with JLD.
import JLD
JLD.save("out.jld", "sol", sol)