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.04388830.4634070.1220690.761485
20.06250.04665880.492660.1297750.809553
30.1250.04960410.5237590.1379670.860656
40.18750.05273530.5568210.1466760.914985
50.250.05606430.5919710.1559350.972744
60.31250.05960330.6293390.1657781.03415
70.3750.06336580.6690660.1762431.09943
80.43750.06736570.7113010.1873681.16883
90.50.07161820.7562010.1991961.24261
100.56250.07613910.8039370.211771.32105
110.6250.08094540.8546850.2251381.40444
120.68750.08605510.9086370.239351.4931
130.750.09148730.9659950.2544591.58735
140.81250.09726241.026970.2705221.68755
150.8750.1034021.09180.2875991.79408
160.93750.1099291.160720.3057531.90733
171.00.1168691.233990.3250542.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)
17×5 DataFrame
Rowtimestampabcd
Float64Float64Float64Float64Float64
10.00.4841370.9925830.01212010.451722
20.06250.5146991.055240.01288520.480237
30.1250.5471891.121850.01369860.510552
40.18750.581731.192670.01456330.542781
50.250.6184521.267960.01548260.577044
60.31250.6574921.3480.01645990.61347
70.3750.6989961.433090.0174990.652195
80.43750.743121.523550.01860360.693365
90.50.7900291.619730.0197780.737134
100.56250.83991.721970.02102640.783665
110.6250.8929191.830670.02235370.833134
120.68750.9492841.946230.02376480.885726
130.751.009212.069090.0252650.941637
140.81251.072912.19970.02685981.00108
150.8751.140642.338550.02855531.06427
160.93751.212642.486180.03035791.13145
171.01.289192.643120.03227421.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)