Hi Scott,

Dierckx.Spline2D is simply a type that holds Vectors of spline
coefficients, so it should work with JLD like any other type (but I admit
that I’m not that familiar with JLD). I’d probably do something like this:

using Dierckx

type LogPTGridEOS
    logP::LinSpace{Float64}
    logT::LinSpace{Float64}
    densities::Matrix{Float64}
    spline::Spline2D

    # calculate the spline coefficients when you create a new instance
    LogPTGridEOS(logP, logT, densities) = new(logP, logT, densities,
Spline2D(logP, logT, densities))
end

grid = LogPTGridEOS(linspace(0.0, 1.0, 10), linspace(0.0, 1.0, 10),
ones(10, 10))

using JLD
save("myfile.jld", "grid", grid)

Then your call method would simply be

call(grid::LogPTGridEOS, p, t) = evaluate(grid.spline, log10(p), log10(t))

Note that the latest version of Dierckx can accept LinSpace, Range or any
AbstractVector as inputs (internally collected into a Vector).

— Kyle
​

On Thu, Aug 13, 2015 at 3:41 PM, Scott T <[email protected]> wrote:

> That's a really nice solution, thanks.
>
> Scott
>
>
> On Thursday, 13 August 2015 22:19:35 UTC+1, Tim Holy wrote:
>>
>> There are several possible solutions, but one is to use the new custom
>> serialization facilities to discard/recreate the interp_func when you
>> save/load the object:
>>
>> https://github.com/JuliaLang/JLD.jl/blob/master/doc/jld.md#custom-serialization
>>
>> --Tim
>>
>> On Thursday, August 13, 2015 12:37:59 PM Scott T wrote:
>> > Hi everyone,
>> >
>> > I'm wondering what the most Julian way to handle the following
>> situation
>> > is. I know it can't be complicated, but am not quite sure how to go
>> about
>> > it.
>> >
>> > I'm doing some simple interpolation using Dierckx.jl
>> > <https://github.com/kbarbary/Dierckx.jl>, getting densities of
>> materials
>> > from a pressure-temperature pair.
>> >
>> > I have a type which holds pressure and temperature axes, and density
>> values
>> > on the (logarithmic) grid that these define:
>> >
>> > type LogPTGridEOS
>> >     logP::LinSpace{Float64}
>> >     logT::LinSpace{Float64}
>> >     densities::Matrix{Float64}
>> > end
>> >
>> > I want to be able to easily store and load instances of this type, and
>> am
>> > using JLD.jl <https://github.com/JuliaLang/JLD.jl> for this. This lets
>> me
>> > save and load from HDF5 ".jld" files, which preserve the type
>> information
>> > (nice).
>> >
>> > To interpolate on the grid, I first need to make an interpolating
>> function,
>> > then evaluate it:
>> >
>> > function call(eos::LogPTGridEOS, P, T)
>> >     interp_func = Dierckx.Spline2D(collect(eos.logP),
>> collect(eos.logT), eos
>> > .densities)  # collect because Dierckx needs arrays
>> >     interpolated_density = Dierckx.evaluate(interp_func, log10(P),
>> log10(T))
>> > # remember this is a log-log grid
>> > end
>> >
>> > But the interpolating function made in that first line could just be
>> called
>> > over and over again, avoiding any overhead from reconstructing it (the
>> slow
>> > bit). How can I best cache this function for re-use? I considered
>> storing
>> > it in the type itself by adding an "interp_func" field, but this messes
>> up
>> > saving it via JLD since JLD can't save a pointer. Am I right in
>> thinking I
>> > should make a wrapper type which holds the original type plus a
>> reference
>> > to the interpolation function, or is there a nicer way to do this?
>> >
>> > Cheers,
>> > Scott
>>
>>

Reply via email to