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.5710420.06264190.9400750.250768
20.06250.6070890.06659620.9994170.266598
30.1250.6454110.07080011.062510.283427
40.18750.6861530.07526931.129580.301318
50.250.7294660.08002071.200880.320339
60.31250.7755140.0850721.276690.340561
70.3750.8244680.09044221.357280.362059
80.43750.8765130.09615141.442960.384913
90.50.9318420.1022211.534040.409211
100.56250.9906650.1086741.630880.435043
110.6251.05320.1155341.733830.462505
120.68751.119680.1228271.843280.4917
130.751.190360.130581.959630.522739
140.81251.265510.1388232.083330.555737
150.8751.345390.1475862.214840.590818
160.93751.430320.1569032.354660.628113
171.01.520610.1668072.503290.667763

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.2140210.6064690.007914580.177972
20.06250.2275310.6447520.008414190.189206
30.1250.2418940.6854520.008945330.20115
40.18750.2571630.7287210.009510010.213848
50.250.2733970.7747220.01011030.227347
60.31250.2906550.8236260.01074850.241698
70.3750.3090030.8756180.0114270.256955
80.43750.3285090.9308910.01214840.273176
90.50.3492460.9896530.01291520.29042
100.56250.3712921.052130.01373050.308753
110.6250.394731.118540.01459730.328243
120.68750.4196471.189150.01551870.348963
130.750.4461371.264210.01649830.370991
140.81250.4742991.344020.01753980.39441
150.8750.504241.428860.0186470.419307
160.93750.536071.519050.01982410.445776
171.00.5699091.614950.02107550.473915

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)