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.395790.2910430.07371270.776633
20.06250.4207750.3094150.07836590.825658
30.1250.4473360.3289470.08331270.877778
40.18750.4755740.3497110.08857180.933188
50.250.5055950.3717870.09416290.992095
60.31250.5375110.3952560.1001071.05472
70.3750.5714410.4202070.1064261.1213
80.43750.6075130.4467320.1131441.19208
90.50.6458620.4749320.1202871.26733
100.56250.6866320.5049120.127881.34733
110.6250.7299760.5367850.1359521.43238
120.68750.7760560.5706690.1445341.5228
130.750.8250440.6066930.1536581.61893
140.81250.8771250.644990.1633571.72112
150.8750.9324940.6857050.1736691.82977
160.93750.9913580.728990.1846321.94528
171.01.053940.7750080.1962872.06807

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.9462580.1075640.3633230.597283
20.06251.005990.1143540.3862580.634986
30.1251.069490.1215730.410640.67507
40.18751.137010.1292470.4365620.717684
50.251.208780.1374060.464120.762987
60.31251.285080.1460790.4934170.811151
70.3751.36620.1553010.5245640.862355
80.43751.452450.1651040.5576770.916791
90.51.544130.1755260.5928810.974663
100.56251.64160.1866060.6303061.03619
110.6251.745230.1983860.6700941.1016
120.68751.85540.2109090.7123941.17114
130.751.972520.2242220.7573641.24506
140.81252.097040.2383760.8051731.32366
150.8752.229410.2534240.8559991.40722
160.93752.370140.2694210.9100341.49605
171.02.519760.2864280.967481.59048

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"#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/SgtOb/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/SgtOb/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)