diff --git a/src/shared_utilities/Parameters.jl b/src/shared_utilities/Parameters.jl index ca7ca40716..92222a42fe 100644 --- a/src/shared_utilities/Parameters.jl +++ b/src/shared_utilities/Parameters.jl @@ -26,9 +26,8 @@ Base.@kwdef struct LandParameters{FT, TP, SFP, IP} <: ALP surf_flux_params::SFP insol_params::IP end - Base.eltype(::LandParameters{FT}) where {FT} = FT -Base.broadcastable(ps::LandParameters) = tuple(ps) +@ConstantValue LandParameters # wrapper methods: P_ref(ps::ALP) = ps.MSLP diff --git a/src/shared_utilities/utils.jl b/src/shared_utilities/utils.jl index a2eac8ed59..c0de657ba7 100644 --- a/src/shared_utilities/utils.jl +++ b/src/shared_utilities/utils.jl @@ -3,7 +3,7 @@ import SciMLBase import ClimaDiagnostics.Schedules: EveryCalendarDtSchedule import Dates -export FTfromY +export FTfromY, @ConstantValue """ heaviside(x::FT)::FT where {FT} @@ -492,3 +492,24 @@ function isdivisible( # have any common divisor) return isinteger(Dates.Day(1) / dt_small) end + +""" + ConstantValue(T) + +This macro defines functions required for broadcasting over an object. +This is a more complete approach than just defining `broadcastable` for an +object, and avoids type inference failures when broadcasting over multiple +complex objects. + +Usage: +- `@ConstantValue T`, as opposed to e.g. `Base.broadcastable(x::T) = tuple(x)` +""" +macro ConstantValue(T) + quote + Base.broadcastable(x::$T) = x + Base.axes(x::$T) = () + Base.getindex(x::$T, i...) = x + Base.getindex(x::$T, i) = x + Base.ndims(x::Type{<:$T}) = 0 + end +end diff --git a/src/standalone/Soil/retention_models.jl b/src/standalone/Soil/retention_models.jl index b1b545ab85..e2674785c6 100644 --- a/src/standalone/Soil/retention_models.jl +++ b/src/standalone/Soil/retention_models.jl @@ -42,6 +42,7 @@ function vanGenuchten{FT}(; α::FT, n::FT) where {FT} S_c = (1 + ((n - 1) / n)^(1 - 2 * n))^(-m) return vanGenuchten{FT}(α, n, m, S_c) end +@ConstantValue vanGenuchten """ BrooksCorey{FT} <: AbstractSoilHydrologyClosure{FT}