Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ Lightweight numerical utility functions shared across modules.
```@docs
Utilities
Utilities.clamp_to_nonneg
Utilities.promote_typeof
Utilities.ϵ_numerics
Utilities.ϵ_numerics_2M_M
Utilities.ϵ_numerics_2M_N
Expand Down
4 changes: 3 additions & 1 deletion src/Microphysics2M.jl
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,9 @@ function rain_evaporation(
(; pdf_r, evap)::CMP.SB2006, aps::CMP.AirProperties, tps::TDI.PS,
q_tot, q_lcl, q_icl, q_rai, q_sno, ρ, N_rai, T,
)
FT = eltype(q_tot)
# the early return below must match the main path's type for any mix of
# plain-float and Dual arguments
FT = UT.promote_typeof(q_tot, q_lcl, q_icl, q_rai, q_sno, ρ, N_rai, T)
ϵₘ = UT.ϵ_numerics_2M_M(FT)
ϵₙ = UT.ϵ_numerics_2M_N(FT)

Expand Down
14 changes: 14 additions & 0 deletions src/Utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ module Utilities
import UnrolledUtilities as UU

export clamp_to_nonneg, ϵ_numerics, ϵ_numerics_2M_M, ϵ_numerics_2M_N, ϵ_numerics_P3_B
export promote_typeof

"""
promote_typeof(args...)

The common promoted type of the arguments' types.

Use it to type early returns and sentinel values from all the arguments the
main-path result derives from. Typing them from a single argument
(`FT = eltype(q_tot)`-style) makes the function's return a union when a
caller mixes plain floats with `ForwardDiff.Dual`s (or float widths) across
arguments — non-concrete, heap-boxed, and silent.
"""
@inline promote_typeof(args...) = promote_type(map(typeof, args)...)
export unrolled_logsumexp
export sgs_weight_function, rime_mass_fraction, rime_density

Expand Down
16 changes: 16 additions & 0 deletions test/ad_compat_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ function test_ad_compatibility(FT)
@test CM2.Γ_incl(FT(-0.25), FT(0.5)) isa FT
end

@testset "rain_evaporation is concretely typed for mixed arguments ($FT)" begin
# the early return (no rain / supersaturated) must have the same type
# as the main path when species are Duals over a plain-float q_tot —
# a union here heap-boxes every call in the Jacobian hot loop
sb = mp.warm_rain.seifert_beheng
aps = mp.warm_rain.air_properties
z = D(0, 0)
# subsaturated (main path) and supersaturated (early return) states
for (q_tot, T) in ((FT(0.005), FT(288)), (FT(0.02), FT(288)))
t = @inferred CM2.rain_evaporation(
sb, aps, tps, q_tot, D(2e-4, 1), z, D(1e-4, 1), z, FT(1.05), D(4e4, 1), T,
)
@test all(v -> v isa FD.Dual, values(t))
end
end

@testset "BMT 2M+P3 Jacobian w.r.t. the 8 species ($FT)" begin
function rhs(x, ρ, T, q_tot, logλ)
t = BMT.bulk_microphysics_tendencies(
Expand Down
Loading