Add promoting constructors for Dual number support #199
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a method to every user-facing
ThermodynamicState
constructor, in which all input arguments are promoted to the same number type. In addition,q_vap_saturation_generic
is given a new method that promotes its input arguments.With only three exceptions, every function from
Thermodynamics.jl
that gets called while computing the precomputed quantities and implicit tendencies ofClimaAtmos.jl
only takes two input arguments: a parameter set and aThermodynamicState
. The exceptions areThermodynamicState
s, where some inputs can be prescribed by the conditions att = 0
orz = 0
while others depend on the prognostic statetotal_specific_enthalpy(param_set, ts, e_tot)
, wherets
ande_tot
both depend on the prognostic stateq_vap_saturation_generic(param_set, T, ρ, phase)
, whereT
is prescribed by the conditions atz = 0
whileρ
depends on the prognostic state in the bottom elementSo, this PR constitutes the minimum set of additions to
Thermodynamics.jl
that will enable us to use automatic differentiation inClimaAtmos.jl
, which involves replacing values in the prognostic state with numbers of typeForwardDiff.Dual
.This is a simpler alternative to #197, which I'm going to close for now. In the future, we may want to come back to the changes in that PR, since they are much more generalizable. That is, they will not require to continue adding new methods to every function that relies on promotion during automatic differentiation in other repositories. The downside to those changes is that they would allow developers to silently introduce
Float32
/Float64
type instabilities within internal functions, and we would probably want to add more unit tests to ensure that those are avoided.