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:
using OrdinaryDiffEq, DataFrames
f_2dlinear = (du, u, p, t) -> du .= 1.01u;
tspan = (0.0, 1.0)
prob = ODEProblem(f_2dlinear, rand(2, 2), tspan);
sol = solve(prob, Euler(); dt = 1 // 2^(4));
df = DataFrame(sol)
Row | timestamp | value1 | value2 | value3 | value4 |
---|---|---|---|---|---|
Float64 | Float64 | Float64 | Float64 | Float64 | |
1 | 0.0 | 0.0438883 | 0.463407 | 0.122069 | 0.761485 |
2 | 0.0625 | 0.0466588 | 0.49266 | 0.129775 | 0.809553 |
3 | 0.125 | 0.0496041 | 0.523759 | 0.137967 | 0.860656 |
4 | 0.1875 | 0.0527353 | 0.556821 | 0.146676 | 0.914985 |
5 | 0.25 | 0.0560643 | 0.591971 | 0.155935 | 0.972744 |
6 | 0.3125 | 0.0596033 | 0.629339 | 0.165778 | 1.03415 |
7 | 0.375 | 0.0633658 | 0.669066 | 0.176243 | 1.09943 |
8 | 0.4375 | 0.0673657 | 0.711301 | 0.187368 | 1.16883 |
9 | 0.5 | 0.0716182 | 0.756201 | 0.199196 | 1.24261 |
10 | 0.5625 | 0.0761391 | 0.803937 | 0.21177 | 1.32105 |
11 | 0.625 | 0.0809454 | 0.854685 | 0.225138 | 1.40444 |
12 | 0.6875 | 0.0860551 | 0.908637 | 0.23935 | 1.4931 |
13 | 0.75 | 0.0914873 | 0.965995 | 0.254459 | 1.58735 |
14 | 0.8125 | 0.0972624 | 1.02697 | 0.270522 | 1.68755 |
15 | 0.875 | 0.103402 | 1.0918 | 0.287599 | 1.79408 |
16 | 0.9375 | 0.109929 | 1.16072 | 0.305753 | 1.90733 |
17 | 1.0 | 0.116869 | 1.23399 | 0.325054 | 2.02773 |
If we set syms
in the DiffEqFunction, then those names will be used:
f = ODEFunction(f_2dlinear, syms = [:a, :b, :c, :d])
prob = ODEProblem(f, rand(2, 2), (0.0, 1.0));
sol = solve(prob, Euler(); dt = 1 // 2^(4));
df = DataFrame(sol)
Row | timestamp | a | b | c | d |
---|---|---|---|---|---|
Float64 | Float64 | Float64 | Float64 | Float64 | |
1 | 0.0 | 0.484137 | 0.992583 | 0.0121201 | 0.451722 |
2 | 0.0625 | 0.514699 | 1.05524 | 0.0128852 | 0.480237 |
3 | 0.125 | 0.547189 | 1.12185 | 0.0136986 | 0.510552 |
4 | 0.1875 | 0.58173 | 1.19267 | 0.0145633 | 0.542781 |
5 | 0.25 | 0.618452 | 1.26796 | 0.0154826 | 0.577044 |
6 | 0.3125 | 0.657492 | 1.348 | 0.0164599 | 0.61347 |
7 | 0.375 | 0.698996 | 1.43309 | 0.017499 | 0.652195 |
8 | 0.4375 | 0.74312 | 1.52355 | 0.0186036 | 0.693365 |
9 | 0.5 | 0.790029 | 1.61973 | 0.019778 | 0.737134 |
10 | 0.5625 | 0.8399 | 1.72197 | 0.0210264 | 0.783665 |
11 | 0.625 | 0.892919 | 1.83067 | 0.0223537 | 0.833134 |
12 | 0.6875 | 0.949284 | 1.94623 | 0.0237648 | 0.885726 |
13 | 0.75 | 1.00921 | 2.06909 | 0.025265 | 0.941637 |
14 | 0.8125 | 1.07291 | 2.1997 | 0.0268598 | 1.00108 |
15 | 0.875 | 1.14064 | 2.33855 | 0.0285553 | 1.06427 |
16 | 0.9375 | 1.21264 | 2.48618 | 0.0303579 | 1.13145 |
17 | 1.0 | 1.28919 | 2.64312 | 0.0322742 | 1.20288 |
Many modeling frameworks will automatically set syms
for this feature. Additionally, this data can be saved to a CSV:
using 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 = solve(prob, Euler(); dt = 1 // 2^(4))
using 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/phaon/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/phaon/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/phaon/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
using JLD2
using OrdinaryDiffEq
JLD2.@load "out.jld2" sol
1-element Vector{Symbol}:
:sol
The example with BSON.jl is:
sol = solve(prob, Euler(); dt = 1 // 2^(4))
using BSON
bson("test.bson", Dict(:sol => sol))
# New session
using OrdinaryDiffEq
using 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.
using JLD
JLD.save("out.jld", "sol", sol)