Saving and loading

If you would like to split your workflow into multiple scripts (e.g. separately generating initial conditions and running dynamics) it is necessary to be able to store intermediate data in files.

When this data is atomic configurations or trajectories, it can be useful to use standard file formats such as those mentioned in the Atoms section previously. However, often it is more convenient to directly save and load Julia objects between sessions. For this purpose, we recommend using FileIO.jl with JLD2.jl.

Note

JLD2 can be used independently of FileIO. However, FileIO provides a unified interface for many file types and allows you to save data to lots of formats with consistent syntax.

As a simple example, suppose that we want the same system parameters across multiple scripts:

using NQCDynamics

atoms = Atoms([:H, :H, :C, :C])
cell = PeriodicCell([10.0 0 0; 0 10.0 0; 0 0 10.0])
model = Harmonic()

Instead of redefining these in every script, we can save them to a file, then load them back in whenever we need them using FileIO.

This creates a file "parameters.jld2" containing all of our system parameters:

using FileIO
save("parameters.jld2", Dict("atoms"=>atoms, "cell"=>cell, "model"=>model))

In a separate Julia session we can re-load these parameters. As detailed in the JLD2 documentation we can select the data to load by specifying extra arguments to load.

julia> using NQCDynamics, FileIO
julia> parameters = load("parameters.jld2")Dict{String, Any} with 3 entries: "model" => Harmonic{Float64, Float64, Float64}(1.0, 1.0, 0.0, 1) "atoms" => Atoms{Float64}([:H, :H, :C, :C], [1, 1, 6, 6], [1837.47, 1837.47, … "cell" => PeriodicCell{Float64}([10.0 0.0 0.0; 0.0 10.0 0.0; 0.0 0.0 10.0], …
julia> atoms = load("parameters.jld2", "atoms")Atoms{Float64}([:H, :H, :C, :C], [1, 1, 6, 6], [1837.4715941070515, 1837.4715941070515, 21894.713607956142, 21894.713607956142])
julia> cell = load("parameters.jld2", "cell")PeriodicCell{Float64}([10.0 0.0 0.0; 0.0 10.0 0.0; 0.0 0.0 10.0], [0.1 0.0 0.0; 0.0 0.1 0.0; 0.0 0.0 0.1], Bool[1, 1, 1], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0], Bool[0, 0, 0])
julia> model = load("parameters.jld2", "model")Harmonic{Float64, Float64, Float64} m: Float64 1.0 ω: Float64 1.0 r₀: Float64 0.0 dofs: Int64 1

JLD2 is compatible with any Julia type so it widely usable for most of the types you encounter is NQCDynamics.jl and across all Julia packages.