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

Remove dispatch on FT for Dual number support #197

Closed
wants to merge 1 commit into from

Conversation

dennisYatunin
Copy link
Member

@dennisYatunin dennisYatunin commented Feb 14, 2024

In order to enable automatic differentiation in ClimaAtmos.jl, we need to get rid of dispatch on FT. For example, consider the definition

@inline function moist_static_energy(
    param_set::APS,
    ts::ThermodynamicState{FT},
    e_pot::FT,
) where {FT <: Real}
    return specific_enthalpy(param_set, ts) + e_pot
end

This does not work when ts contains Dual numbers and e_pot is a regular Float32 or Float64, which occurs when ts depends on the prognostic state while e_pot is a constant. In general, it is hard to anticipate which of the values that come from ClimaAtmos.jl will be Dual numbers, since the same value can be either prescribed or inferred from the prognostic state, depending on the model configuration. This is particularly significant for surface conditions, where the chain of computations can vary a lot between different surface parameterizations.

In order to make this change easier to review, I'll split it up into several PRs. This PR removes all dispatch on FT from relations.jl. In places where FT is necessary for type-stability (i.e., when we need to return FT(0) or FT(1) from a conditional statement) or for obtaining machine epsilon (eps(FT)), it is now inferred from the parameter set. As long as the parameter set does not contain any Dual numbers, this ensures that "constant" values will always be represented as Float32s or Float64s.

@dennisYatunin dennisYatunin changed the title Remove diagonal dispatch on FT Remove dispatch on FT for Dual number support Feb 14, 2024
Copy link

codecov bot commented Feb 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (1dc23bb) 93.04% compared to head (c8575c2) 92.98%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #197      +/-   ##
==========================================
- Coverage   93.04%   92.98%   -0.06%     
==========================================
  Files          10       10              
  Lines        1150     1141       -9     
==========================================
- Hits         1070     1061       -9     
  Misses         80       80              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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.

1 participant