ApproxFun.jl icon indicating copy to clipboard operation
ApproxFun.jl copied to clipboard

How can we save variables on disk?

Open BoundaryValueProblems opened this issue 7 years ago • 8 comments

Hello! I am wondering how we can save the results of our computations using ApproxFun functions on a hard disk. Is there anything like save function in MALTAB? Also, how about load function once I could save the results? Thanks! BVPs

BoundaryValueProblems avatar Nov 06 '16 02:11 BoundaryValueProblems

Hmm, this isn't implemented yet. You could do

writecsv(filename, f.coefficients)

Then you'd have to remember the space (probably Space(Interval()^2) or similar) to recover:

cfs=readcsv(filename)
d=Interval()^2
sp = Space(d)
f=Fun(cfs,sp)

I'll look into the "proper" way of writing Julia types.

dlfivefifty avatar Nov 07 '16 00:11 dlfivefifty

An alternative is JLD.jl.

vchuravy avatar Nov 07 '16 02:11 vchuravy

JLD works for me, so maybe I'll add to the docs the following example:

using ApproxFun, JLD
f=Fun(exp)
save("fun.jld","f",f)
d=load("fun.jld")
f=d["f"]

dlfivefifty avatar Nov 07 '16 05:11 dlfivefifty

Thanks a lot, vchuravy and dlfivefify! If JLD.jl works for any constructs generated by ApproxFun functions, that would be great! I tried myself to use MAT.jl to save matrices and arrays created by sampling functions generated by ApproxFun. Of course, it worked, but it was not clear to me whether I could save genuine ApproxFun constructs other than matrices and arrays as a MATLAB mat file...

BoundaryValueProblems avatar Nov 07 '16 05:11 BoundaryValueProblems

It looks like JLD would work for any (pure) Julia types, which is the case for all of ApproxFun.

On 7 Nov. 2016, at 4:54 pm, BoundaryValueProblems [email protected] wrote:

Thanks a lot, vchuravy and dlfivefify! If JLD.jl https://github.com/JuliaIO/JLD.jl works for any constructs generated by ApproxFun functions, that would be great! I tried myself to use MAT.jl https://github.com/simonster/MAT.jlto save matrices and arrays created by sampling functions generated by ApproxFun. Of course, it worked, but it was not clear to me whether I could save genuine ApproxFun constructs other than matrices and arrays as a MATLAB mat file...

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ApproxFun/ApproxFun.jl/issues/415#issuecomment-258753086, or mute the thread https://github.com/notifications/unsubscribe-auth/ABLDqRYytzSTT9xbQN8FC4VA22yypbVQks5q7r0DgaJpZM4Kqche.

dlfivefifty avatar Nov 07 '16 05:11 dlfivefifty

Well, I store the constructs without generating error using JLD:

julia> D = Interval()^2 # The domain is [-1,1]x[-1,1].
julia> Δ = Laplacian(D)
julia> g = Fun((x,y)->cos(5pi*x+7pi*y)) # an example 2D function on D
julia> f = Fun(z->(x=real(z);y=imag(z);g(x,y)),∂(D)) # boundary values of g
julia> u = [dirichlet(D); Δ + 500I] \ [f; 0.0]
julia> save("helmexp.jld","D",D,"Δ",Δ,"g",g,"f",f)

But when I try to load the save file via

julia> d=load("helmexp.jld")

it generated the following error:

stored type ApproxFun.TensorSpace{Core.Tuple{ApproxFun.Chebyshev{ApproxFun.Interval{Core.Float64}},ApproxFun.Chebyshev{ApproxFun.Interval{Core.Float64}}},ApproxFun.RealBasis,2} does not match currently loaded type in jldatatype(::JLD.JldFile, ::HDF5.HDF5Datatype) at /Users/xxx/.julia/v0.5/JLD/src/jld_types.jl:749 in jldatatype(::JLD.JldFile, ::HDF5.HDF5Datatype) at /Users/xxx/.julia/v0.5/JLD/src/jld_types.jl:739 in read(::JLD.JldDataset) at /Users/xxx/.julia/v0.5/JLD/src/JLD.jl:390 in read(::JLD.JldFile, ::String) at /Users/xxx/.julia/v0.5/JLD/src/JLD.jl:366 in collect_to!(::Array{Tuple{String,Any},1}, ::Base.Generator{Array{String,1},JLD.##38#40{JLD.JldFile}}, ::Int64, ::Int64) at ./array.jl:340 in collect_to!(::Array{Tuple{String,ApproxFun.ProductDomain{Tuple{ApproxFun.Interval{Float64},ApproxFun.Interval{Float64}},Float64,2}},1}, ::Base.Generator{Array{String,1},JLD.##38#40{JLD.JldFile}}, ::Int64, ::Int64) at ./array.jl:350 ......

Thanks a lot for your help! BVPs

BoundaryValueProblems avatar Nov 07 '16 20:11 BoundaryValueProblems

The following example produces the same kind of error even in 1D whereas the example of @dlfivefifty above is ok.

julia> S = Fourier(0..2π) ;
julia> f = Fun(x-> cos(x), S)
julia> save("fun.jld","f",f)
julia> d=load("fun.jld")

ERROR: stored type ApproxFun.SumSpace{Core.Tuple{ApproxFun.CosSpace{ApproxFun.PeriodicInterval{Core.Float64},Core.Float64},ApproxFun.SinSpace{ApproxFun.PeriodicInterval{Core.Float64},Core.Float64}},ApproxFun.PeriodicInterval{Core.Float64},Core.Float64} does not match currently loaded type
Stacktrace:
 [1] jldatatype(::JLD.JldFile, ::HDF5.HDF5Datatype) at ...

edljk avatar Dec 12 '17 17:12 edljk

Hmm, I'm not sure JLD.jl is the best choice for this anyways because it's "unsafe": it can execute code. But I don't know what a better alternative is (making a .fun file type feels excessive).

dlfivefifty avatar Dec 12 '17 19:12 dlfivefifty