Skip to content

Commit

Permalink
add @constantvalue macro
Browse files Browse the repository at this point in the history
  • Loading branch information
juliasloan25 committed Nov 13, 2024
1 parent be3889a commit f164f7e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/shared_utilities/Parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion src/shared_utilities/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SciMLBase
import ClimaDiagnostics.Schedules: EveryCalendarDtSchedule
import Dates

export FTfromY
export FTfromY, @ConstantValue

"""
heaviside(x::FT)::FT where {FT}
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions src/standalone/Soil/retention_models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit f164f7e

Please sign in to comment.