Skip to content

Commit

Permalink
Fix runtime dispatch and alloc caused by temperature_residual
Browse files Browse the repository at this point in the history
  • Loading branch information
bgroenks96 committed Jan 11, 2023
1 parent fb20af1 commit aec9825
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FreezeCurves"
uuid = "71e4ad71-e4f2-45a3-aa0a-91ffaa9676be"
authors = ["Brian Groenke <brian.groenke@awi.de> and contributors"]
version = "0.5.1"
version = "0.5.2"

[deps]
Flatten = "4c728ea3-d9ee-5c9a-9642-b6f7d7dc04fa"
Expand Down Expand Up @@ -34,10 +34,11 @@ Unitful = "1"
julia = "1.6"

[extras]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"

[targets]
test = ["Test", "NonlinearSolve", "Optim", "Turing"]
test = ["Test", "BenchmarkTools", "NonlinearSolve", "Optim", "Turing"]
2 changes: 1 addition & 1 deletion src/Solvers/Solvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module Solvers
H::TH
ψ₀::Tψ = nothing
end
@inline (obj::SFCCInverseEnthalpyObjective)(T) = FreezeCurves.temperature_residual(obj.f, obj.f_kwargs, obj.hc, obj.L, adstrip(obj.H), T, adstrip(obj.ψ₀), Val{false}())
@inline (obj::SFCCInverseEnthalpyObjective)(T) = first(FreezeCurves.temperature_residual(obj.f, obj.f_kwargs, obj.hc, obj.L, adstrip(obj.H), T, adstrip(obj.ψ₀)))

"""
initialize!(solver::SFCCSolver, fc::SFCCFunction, hc; fc_kwargs...)
Expand Down
6 changes: 3 additions & 3 deletions src/sfcc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ Returns the analytical inflection point (i.e. where ∂²θ/∂T^2 = 0), if avai
"""
inflectionpoint(f::SFCCFunction) = error("not implemented for $f")
"""
temperature_residual(f::F, f_args::Fargs, hc, L, H, T, ::Val{return_all}=Val{true}()) where {F<:SFCCFunction,Fargs<:Tuple,return_all}
temperature_residual(f::F, f_args::Fargs, hc, L, H, T, ψ₀=nothing) where {F<:SFCCFunction}
Helper function for updating θw, C, and the residual. `hc` should be a function `θw -> C`
which computes the heat capacity from liquid water content (`θw`).
"""
@inline function temperature_residual(f::F, f_kwargs::NamedTuple, hc, L, H, T, ψ₀=nothing, ::Val{return_all}=Val{true}()) where {F<:SFCCFunction,return_all}
@inline function temperature_residual(f::F, f_kwargs::NamedTuple, hc, L, H, T, ψ₀=nothing) where {F<:SFCCFunction}
# helper function to handle case where SFCC has no SWRC and thus does not accept ψ₀ as an argument
_invoke_f(::Nothing, T, ψ₀) = f(T; f_kwargs...)
_invoke_f(::SWRCFunction, T, ψ₀) = f(T, ψ₀; f_kwargs...)
θw = _invoke_f(swrc(f), T, ψ₀)
C = hc(θw)
Tres = T - (H - θw*L) / C
return return_all ? (;Tres, θw, C) : Tres
return Tres, θw, C
end
"""
PainterKarra{Tftp,Tβ,Tω,Tg,Tswrc<:SWRCFunction} <: SFCCFunction
Expand Down
16 changes: 16 additions & 0 deletions test/solver_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ using Test

using Base.Iterators

@testset "temperature_residual" begin
freezecurves = (ustrip(McKenzie()), ustrip(Westermann()), ustrip(DallAmico()))
L = 3.34e8
T₀ = -1.0
for fc in freezecurves
props = SoilWaterProperties(fc)
θtot, θsat = props.θtot, props.θsat
hc = heatcapacity(1.9e6, 4.2e6; θtot)
θw_true = fc(-0.01; θtot, θsat)
C_true = hc(θw_true)
H_true = FreezeCurves.enthalpy(-0.01, C_true, L, θw_true)
T_resid, θw, C = @inferred FreezeCurves.temperature_residual(fc, (;), hc, L, H_true, -0.1, nothing)
@test abs(T_resid) > 0
end
end

@testset "Solvers" begin
freezecurves = (ustrip(McKenzie()), ustrip(Westermann()), ustrip(DallAmico()))
T_cases = [1.0,-0.01,-0.1,-10.0]
Expand Down

2 comments on commit aec9825

@bgroenks96
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/75512

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.2 -m "<description of version>" aec9825d41594bbb23b8ed1f4ad8d6ec973bd792
git push origin v0.5.2

Please sign in to comment.