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)
17×5 DataFrame
Rowtimestampvalue1value2value3value4
Float64Float64Float64Float64Float64
10.00.8595870.2330410.754650.297755
20.06250.9138480.2477510.8022880.31655
30.1250.9715350.2633910.8529320.336533
40.18751.032860.2800170.9067730.357776
50.251.098060.2976930.9640130.380361
60.31251.167380.3164851.024870.404371
70.3751.241070.3364631.089560.429897
80.43751.319410.3577021.158340.457034
90.51.40270.3802821.231460.485885
100.56251.491240.4042881.30920.516556
110.6251.585380.4298081.391840.549164
120.68751.685460.456941.47970.58383
130.751.791850.4857841.57310.620684
140.81251.904960.5164491.672410.659865
150.8752.025210.549051.777980.701519
160.93752.153050.5837091.890210.745802
171.02.288960.6205562.009530.792881

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)
17×5 DataFrame
Rowtimestampabcd
Float64Float64Float64Float64Float64
10.00.264550.6340640.737760.657121
20.06250.281250.6740890.7843310.698602
30.1250.2990040.7166410.8338420.742701
40.18750.3178780.7618790.8864780.789584
50.250.3379450.8099730.9424370.839427
60.31250.3592770.8611021.001930.892415
70.3750.3819570.9154591.065180.948749
80.43750.4060680.9732481.132411.00864
90.50.4317011.034681.20391.07231
100.56250.4589521.11.279891.14
110.6250.4879231.169441.360691.21196
120.68750.5187231.243261.446581.28847
130.750.5514681.321741.53791.3698
140.81250.5862791.405171.634981.45627
150.8750.6232881.493871.738181.5482
160.93750.6626331.588171.847911.64593
171.00.7044621.688431.964561.74983

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/pdSa4/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/pdSa4/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/pdSa4/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)