Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add FT wrapper for scalars #3015

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

add FT wrapper for scalars #3015

wants to merge 1 commit into from

Conversation

haakon-e
Copy link
Member

Purpose

Add macro @convert_scalars to conveniently wrap scalars (e.g. integers, floats) with the provided FT, e.g.:

FT = Float32
@convert_scalars FT 1 + 2.0 + 3.0f0 * π + sin(pi)
# expands to: FT(1) + FT(2.0) + FT(3.0f0) * FT(π) + sin(FT(pi))

Copy link
Member

@charleskawczynski charleskawczynski left a comment

Choose a reason for hiding this comment

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

Where is this intended to be used?

@haakon-e
Copy link
Member Author

haakon-e commented May 15, 2024

I wanted this for some functions I'll be writing that provide forcing / boundary conditions for the LES. They will look similar to (but mine have more terms needing to be wrapped in FT) e.g.

draft_area(::Type{FT}) where {FT} =
z -> z < 0.7e4 ? FT(0.5) * exp(-(z - FT(4e3))^2 / 2 / FT(1e3)^2) : FT(0)
edmfx_q_tot(::Type{FT}) where {FT} =
z -> z < 0.7e4 ? FT(1e-3) * exp(-(z - FT(4e3))^2 / 2 / FT(1e3)^2) : FT(0)

which (in the case of the example above), would become something like

edmfx_q_tot(::Type{FT}) where {FT} = @convert_scalars FT z ->
    z < 0.7e4 ? 1e-3 * exp(-(z - 4e3)^2 / 2 / 1e3^2) : 0

edmfx_q_tot(::Type{FT}) where {FT} = @convert_scalars FT z ->
    z < 0.7e4 ? 1e-3 * exp(-(z - 4e3)^2 / 2 / 1e3^2) : 0

An example of what I will actually do is:

FT = eltype(params)
θ = @convert_scalars FT z -> if z < 400 # m
    265 + 0.004 * (z - 400)
elseif 400  z < 825
    265
elseif 825  z < 2045
    266 + (z - 825)^0.3
elseif 2045  z
    271 + (z - 2000)^0.33
end


# Helper function to process expressions
function process_expr(expr, FT_expr)
if isa(expr, Number) || expr in (:π, :pi)
Copy link
Member

Choose a reason for hiding this comment

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

I'd recommend using something like names(MathConstants)[2:end] instead of manually listing out , :pi, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants