Skip to content

Commit

Permalink
Rewrite v2p
Browse files Browse the repository at this point in the history
as mentioned in #21 (comment)
  • Loading branch information
singularitti committed Dec 30, 2020
1 parent ab3bf4f commit 8bd6716
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/Thermodyn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@ function v2p(fₜ₀ᵥ::AbstractDimVector{<:Energy,<:Tuple{Vol}}, param::Parame
end
end
function v2p(fₜ₀ᵥ::AbstractDimVector{T,<:Tuple{Vol}}, param::Parameters) where {T}
p = sortperm(val(dims(fₜ₀ᵥ, Vol)))
volumes = val(dims(fₜ₀ᵥ, Vol))[p]
min, max = extrema(volumes)
y = collect(fₜ₀ᵥ)[p]
perm = sortperm(val(dims(fₜ₀ᵥ, Vol)))
volumes = val(dims(fₜ₀ᵥ, Vol))[perm]
ps = map(PressureEquation(param), volumes)
y = fₜ₀ᵥ[perm]
return function (pressures)
fₜ₀ₚ = map(pressures) do pressure
v = inverse(PressureEquation(param))(pressure)
if min <= v <= max
interpolate((volumes,), y, Gridded(Linear()))(v)
else
extrapolate(interpolate((volumes,), fₜ₀ᵥ, Gridded(Linear())), Periodic())(v)
end
pmin, pmax = extrema(pressures)
interp = if minimum(ps) < pmin <= pmax < maximum(ps)
interpolate((ps,), y, Gridded(Linear()))
else
extrapolate(interpolate((ps,), y, Gridded(Linear())), Periodic())
end
fₜ₀ₚ = map(interp, pressures)
return rebuild(fₜ₀ᵥ, fₜ₀ₚ, (Press(pressures),))
end
end
Expand Down

0 comments on commit 8bd6716

Please sign in to comment.