# Saving workspace to file

**URL:** <https://discourse.julialang.org/t/saving-workspace-to-file/10781>\
**Category:** General Usage\
**Tags:** diffeq\
**Created:** [May 8, 2018, 2:45pm UTC](https://discourse.julialang.org/t/saving-workspace-to-file/10781 "2018-05-08T14:45:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [May 8, 2018, 2:45pm UTC](https://discourse.julialang.org/t/saving-workspace-to-file/10781/1 "2018-05-08T14:45:16Z")

</div>

Commonly I run a large set of simulations over night/weekend. All of this information exists in a Jupyter notebook and if the browser/jupyter crashes everything is lost and needs to be redone for the next day. After this happened this morning (again) I’ve decided I should be more robust n my approach.

The best would be if I could save my workspace, like I used to do in matlab. Then in case things go wrong I have a copy on the disk which I can use load up and use.

I have tried to understand how to do it, but found it surprisingly hard. There is a package called JLD, however I have read the documentation and it only describes saving variables, and not the entire workspace. Also it seems hard to use with non standard types, like the solutions to ODE/SDE problems, or the types I save my models in.

Is this possible to do, and if so, how?  
cheers

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 8, 2018, 5:01pm UTC](https://discourse.julialang.org/t/saving-workspace-to-file/10781/2 "2018-05-08T17:01:27Z")

</div>

You could use JLD2:

[https://github.com/simonster/JLD2.jl](https://github.com/simonster/JLD2.jl)

`@save <filename>`

saves all variables in the global namespace to the file; `@load` loads them again.

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [May 9, 2018, 12:48pm UTC](https://discourse.julialang.org/t/saving-workspace-to-file/10781/3 "2018-05-09T12:48:52Z")

</div>

Thank you, that is very helpful.

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [May 9, 2018, 4:00pm UTC](https://discourse.julialang.org/t/saving-workspace-to-file/10781/4 "2018-05-09T16:00:48Z")

</div>

I have been trying through the day to get this to work, however I have had problems with non-trivial types. Typically what I want to save are solutions to Ordinary Differential Equations or Stochastic Differential Equations. I run this code

```julia
using JLD2
using DifferentialEquations
f(u,p,t) = 1.01*u
u0=1/2
tspan = (0.0,1.0)
prob = ODEProblem(f,u0,tspan)
sol = solve(prob,Tsit5(),reltol=1e-8,abstol=1e-8);
@lsave "tmpfile.jld"

```

and then turns the kernel off (I do everything in Jupyter by the way). So far everything is fine. Now I run:

```julia
using JLD2
using DifferentialEquations
using Plots
@load "tmpfile.jld"

```

This time I get this warning:

```julia
WARNING: type #f does not exist in workspace; reconstructing
WARNING: some parameters could not be resolved for type DiffEqBase.ODEProblem{Float64,Float64,false,Void,#f,Void,Void,UniformScaling{Int64},DiffEqBase.StandardODEProblem}; reconstructing
WARNING: some parameters could not be resolved for type OrdinaryDiffEq.InterpolationData{#f,Array{Float64,1},Array{Float64,1},Array{Array{Float64,1},1},OrdinaryDiffEq.Tsit5ConstantCache{Float64,Float64}}; reconstructing

```

which seems ominous. Finally I try

```julia
plot(sol)

```

and gets this plain error

```julia
type ##DiffEqBase.ODEProblem{Float64,Float64,false,Void,#f,Void,Void,UniformScaling{Int64},DiffEqBase.StandardODEProblem}#666 has no field p

Stacktrace:
 [1] macro expansion at /home/Torkel.Loman/.julia/v0.6/DiffEqBase/src/solutions/solution_interface.jl:99 [inlined]
 [2] apply_recipe(::Dict{Symbol,Any}, ::DiffEqBase.ODESolution{Float64,1,Array{Float64,1},Void,Void,Array{Float64,1},Array{Array{Float64,1},1},JLD2.ReconstructedTypes.##DiffEqBase.ODEProblem{Float64,Float64,false,Void,#f,Void,Void,UniformScaling{Int64},DiffEqBase.StandardODEProblem}#666,OrdinaryDiffEq.Tsit5,JLD2.ReconstructedTypes.##OrdinaryDiffEq.InterpolationData{#f,Array{Float64,1},Array{Float64,1},Array{Array{Float64,1},1},OrdinaryDiffEq.Tsit5ConstantCache{Float64,Float64}}#667}) at /home/Torkel.Loman/.julia/v0.6/RecipesBase/src/RecipesBase.jl:291
 [3] _process_userrecipes(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{DiffEqBase.ODESolution{Float64,1,Array{Float64,1},Void,Void,Array{Float64,1},Array{Array{Float64,1},1},JLD2.ReconstructedTypes.##DiffEqBase.ODEProblem{Float64,Float64,false,Void,#f,Void,Void,UniformScaling{Int64},DiffEqBase.StandardODEProblem}#666,OrdinaryDiffEq.Tsit5,JLD2.ReconstructedTypes.##OrdinaryDiffEq.InterpolationData{#f,Array{Float64,1},Array{Float64,1},Array{Array{Float64,1},1},OrdinaryDiffEq.Tsit5ConstantCache{Float64,Float64}}#667}}) at /home/Torkel.Loman/.julia/v0.6/Plots/src/pipeline.jl:81
 [4] _plot!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{DiffEqBase.ODESolution{Float64,1,Array{Float64,1},Void,Void,Array{Float64,1},Array{Array{Float64,1},1},JLD2.ReconstructedTypes.##DiffEqBase.ODEProblem{Float64,Float64,false,Void,#f,Void,Void,UniformScaling{Int64},DiffEqBase.StandardODEProblem}#666,OrdinaryDiffEq.Tsit5,JLD2.ReconstructedTypes.##OrdinaryDiffEq.InterpolationData{#f,Array{Float64,1},Array{Float64,1},Array{Array{Float64,1},1},OrdinaryDiffEq.Tsit5ConstantCache{Float64,Float64}}#667}}) at /home/Torkel.Loman/.julia/v0.6/Plots/src/plot.jl:179
 [5] plot(::DiffEqBase.ODESolution{Float64,1,Array{Float64,1},Void,Void,Array{Float64,1},Array{Array{Float64,1},1},JLD2.ReconstructedTypes.##DiffEqBase.ODEProblem{Float64,Float64,false,Void,#f,Void,Void,UniformScaling{Int64},DiffEqBase.StandardODEProblem}#666,OrdinaryDiffEq.Tsit5,JLD2.ReconstructedTypes.##OrdinaryDiffEq.InterpolationData{#f,Array{Float64,1},Array{Float64,1},Array{Array{Float64,1},1},OrdinaryDiffEq.Tsit5ConstantCache{Float64,Float64}}#667}) at /home/Torkel.Loman/.julia/v0.6/Plots/src/plot.jl:52

```

Further more, I repeat my test but using another special type, the reaction networks produced by the DiffEqBiological package. I use this code

```julia
using JLD2
using DifferentialEquations
rn_model = @reaction_network rnType begin
    1.0, X --> 0
end
@save "tmpfile.jld"

```

and get this response:

```julia
WARNING: skipping rn_model because it contains a pointer

```

How limited is JLD2 in what parts of the works pace I can actually successfully save? I succeed with the simpler types, but there should be some way to store also this kinds of stuff. Or is there nothing and I am running into a dead end?
