CliMA / Thermodynamics.jl

A package containing a library of moist thermodynamic relations.
https://clima.github.io/Thermodynamics.jl/dev/
Apache License 2.0
61 stars 2 forks source link

Remove dispatch on FT for Dual number support #197

Closed dennisYatunin closed 7 months ago

dennisYatunin commented 7 months ago

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.

codecov[bot] commented 7 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

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

Additional details and impacted files ```diff @@ 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 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.