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
import OrdinaryDiffEqLowOrderRK: Euler
import DataFrames: DataFrame
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, Euler(); dt = 1 // 2^(4));
df = DataFrame(sol)
17×5 DataFrame
Rowtimestampvalue1value2value3value4
Float64Float64Float64Float64Float64
10.00.7468610.9725490.8465050.376994
20.06250.7940071.033940.8999410.400791
30.1250.8441281.099210.956750.426091
40.18750.8974141.16861.017140.452988
50.250.9540631.242361.081350.481583
60.31251.014291.320791.149610.511983
70.3751.078321.404161.222180.544302
80.43751.146381.49281.299330.578661
90.51.218751.587031.381350.615189
100.56251.295681.687221.468550.654023
110.6251.377471.793721.561250.695308
120.68751.464431.906951.659810.739199
130.751.556872.027331.764580.785861
140.81251.655152.15531.875970.835469
150.8751.759632.291351.994390.888208
160.93751.87072.4362.120290.944276
171.01.988792.589772.254131.00388

If we attach an ModelingToolkit / SciML symbolic system via the problem's sys, then those names will be used:

df = DataFrame(sol)
17×5 DataFrame
Rowtimestampvalue1value2value3value4
Float64Float64Float64Float64Float64
10.00.7468610.9725490.8465050.376994
20.06250.7940071.033940.8999410.400791
30.1250.8441281.099210.956750.426091
40.18750.8974141.16861.017140.452988
50.250.9540631.242361.081350.481583
60.31251.014291.320791.149610.511983
70.3751.078321.404161.222180.544302
80.43751.146381.49281.299330.578661
90.51.218751.587031.381350.615189
100.56251.295681.687221.468550.654023
110.6251.377471.793721.561250.695308
120.68751.464431.906951.659810.739199
130.751.556872.027331.764580.785861
140.81251.655152.15531.875970.835469
150.8751.759632.291351.994390.888208
160.93751.87072.4362.120290.944276
171.01.988792.589772.254131.00388

Modeling frameworks like ModelingToolkit automatically attach a sys, which provides the state/parameter names used here. 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, 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 ~/.julia/packages/JLD2/r2pdD/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 ~/.julia/packages/JLD2/r2pdD/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, 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)