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)
17×5 DataFrame
Rowtimestampvalue1value2value3value4
Float64Float64Float64Float64Float64
10.00.9646380.6451020.1161440.833915
20.06251.025530.6858240.1234750.886556
30.1251.090270.7291160.131270.94252
40.18751.159090.7751420.1395561.00202
50.251.232260.8240730.1483661.06527
60.31251.310040.8760920.1577311.13251
70.3751.392740.9313950.1676881.204
80.43751.480660.990190.1782731.28001
90.51.574121.05270.1895271.36081
100.56251.673491.119150.2014911.44671
110.6251.779131.189790.214211.53803
120.68751.891441.26490.2277321.63512
130.752.010831.344750.2421071.73834
140.81252.137771.429630.257391.84807
150.8752.272721.519880.2736381.96473
160.93752.416181.615820.2909122.08875
171.02.56871.717820.3092752.2206

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)
17×5 DataFrame
Rowtimestampabcd
Float64Float64Float64Float64Float64
10.00.8131660.4498280.166490.871129
20.06250.8644970.4782240.1770.926119
30.1250.9190680.5084120.1881730.98458
40.18750.9770850.5405050.2000521.04673
50.251.038760.5746250.212681.11281
60.31251.104330.6108980.2261051.18305
70.3751.174050.6494610.2403781.25773
80.43751.248160.6904580.2555521.33713
90.51.326950.7340430.2716841.42153
100.56251.410710.780380.2888341.51127
110.6251.499760.8296410.3070661.60667
120.68751.594430.8820120.326451.70809
130.751.695080.9376890.3470571.81591
140.81251.802090.9968810.3689651.93054
150.8751.915841.059810.3922562.05241
160.93752.036781.126710.4170172.18196
171.02.165351.197830.4433422.3197

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"#2#3".
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/hbsZG/src/data/writing_datatypes.jl:447
┌ Warning: Attempting to store Main.var"#2#3".
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/hbsZG/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)