Skip to content

Commit aec9825

Browse files
committed
Fix runtime dispatch and alloc caused by temperature_residual
1 parent fb20af1 commit aec9825

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

Project.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "FreezeCurves"
22
uuid = "71e4ad71-e4f2-45a3-aa0a-91ffaa9676be"
33
authors = ["Brian Groenke <[email protected]> and contributors"]
4-
version = "0.5.1"
4+
version = "0.5.2"
55

66
[deps]
77
Flatten = "4c728ea3-d9ee-5c9a-9642-b6f7d7dc04fa"
@@ -34,10 +34,11 @@ Unitful = "1"
3434
julia = "1.6"
3535

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

4243
[targets]
43-
test = ["Test", "NonlinearSolve", "Optim", "Turing"]
44+
test = ["Test", "BenchmarkTools", "NonlinearSolve", "Optim", "Turing"]

src/Solvers/Solvers.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module Solvers
4545
H::TH
4646
ψ₀::Tψ = nothing
4747
end
48-
@inline (obj::SFCCInverseEnthalpyObjective)(T) = FreezeCurves.temperature_residual(obj.f, obj.f_kwargs, obj.hc, obj.L, adstrip(obj.H), T, adstrip(obj.ψ₀), Val{false}())
48+
@inline (obj::SFCCInverseEnthalpyObjective)(T) = first(FreezeCurves.temperature_residual(obj.f, obj.f_kwargs, obj.hc, obj.L, adstrip(obj.H), T, adstrip(obj.ψ₀)))
4949

5050
"""
5151
initialize!(solver::SFCCSolver, fc::SFCCFunction, hc; fc_kwargs...)

src/sfcc.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,19 @@ Returns the analytical inflection point (i.e. where ∂²θ/∂T^2 = 0), if avai
6868
"""
6969
inflectionpoint(f::SFCCFunction) = error("not implemented for $f")
7070
"""
71-
temperature_residual(f::F, f_args::Fargs, hc, L, H, T, ::Val{return_all}=Val{true}()) where {F<:SFCCFunction,Fargs<:Tuple,return_all}
71+
temperature_residual(f::F, f_args::Fargs, hc, L, H, T, ψ₀=nothing) where {F<:SFCCFunction}
7272
7373
Helper function for updating θw, C, and the residual. `hc` should be a function `θw -> C`
7474
which computes the heat capacity from liquid water content (`θw`).
7575
"""
76-
@inline function temperature_residual(f::F, f_kwargs::NamedTuple, hc, L, H, T, ψ₀=nothing, ::Val{return_all}=Val{true}()) where {F<:SFCCFunction,return_all}
76+
@inline function temperature_residual(f::F, f_kwargs::NamedTuple, hc, L, H, T, ψ₀=nothing) where {F<:SFCCFunction}
7777
# helper function to handle case where SFCC has no SWRC and thus does not accept ψ₀ as an argument
7878
_invoke_f(::Nothing, T, ψ₀) = f(T; f_kwargs...)
7979
_invoke_f(::SWRCFunction, T, ψ₀) = f(T, ψ₀; f_kwargs...)
8080
θw = _invoke_f(swrc(f), T, ψ₀)
8181
C = hc(θw)
8282
Tres = T - (H - θw*L) / C
83-
return return_all ? (;Tres, θw, C) : Tres
83+
return Tres, θw, C
8484
end
8585
"""
8686
PainterKarra{Tftp,Tβ,Tω,Tg,Tswrc<:SWRCFunction} <: SFCCFunction

test/solver_tests.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ using Test
55

66
using Base.Iterators
77

8+
@testset "temperature_residual" begin
9+
freezecurves = (ustrip(McKenzie()), ustrip(Westermann()), ustrip(DallAmico()))
10+
L = 3.34e8
11+
T₀ = -1.0
12+
for fc in freezecurves
13+
props = SoilWaterProperties(fc)
14+
θtot, θsat = props.θtot, props.θsat
15+
hc = heatcapacity(1.9e6, 4.2e6; θtot)
16+
θw_true = fc(-0.01; θtot, θsat)
17+
C_true = hc(θw_true)
18+
H_true = FreezeCurves.enthalpy(-0.01, C_true, L, θw_true)
19+
T_resid, θw, C = @inferred FreezeCurves.temperature_residual(fc, (;), hc, L, H_true, -0.1, nothing)
20+
@test abs(T_resid) > 0
21+
end
22+
end
23+
824
@testset "Solvers" begin
925
freezecurves = (ustrip(McKenzie()), ustrip(Westermann()), ustrip(DallAmico()))
1026
T_cases = [1.0,-0.01,-0.1,-10.0]

0 commit comments

Comments
 (0)