Make the pointwise 2M+P3 tendencies ForwardDiff-differentiable#725
Open
haakon-e wants to merge 2 commits into
Open
Make the pointwise 2M+P3 tendencies ForwardDiff-differentiable#725haakon-e wants to merge 2 commits into
haakon-e wants to merge 2 commits into
Conversation
`get_state_from_prognostic` is not defined anywhere; the path was unexercised by tests.
4759ee8 to
374c47c
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #725 +/- ##
==========================================
+ Coverage 92.74% 92.92% +0.18%
==========================================
Files 56 56
Lines 2701 2728 +27
==========================================
+ Hits 2505 2535 +30
+ Misses 196 193 -3
🚀 New features to boost your workflow:
|
374c47c to
bdd359d
Compare
This was referenced Jun 12, 2026
Member
Author
|
This change is part of the following stack:
Change managed by git-spice. |
7514748 to
415fe44
Compare
This was referenced Jun 14, 2026
Implicit substepping needs Jacobians of the pointwise tendencies w.r.t. the 8 prognostic species (logλ held fixed) — mixed Dual/plain arguments, frozen thermodynamic context. Primal values and allocations are unchanged (uniform-float promotion is the identity). - Type flexibility for mixed Dual/plain inputs: P3State, Γ_incl, the gamma_inc_inv wrapper, ice_mass_coeffs (uniform branch return — was boxing ~105 MiB per Jacobian), logistic_function_integral, Chen2022_exponential_pdf, LclRaiRates, the size-distribution closures. - gamma_inc_inv: add an implicit-function-theorem Dual rule (none upstream), exact in the quantile slots; the shape slot differences the smaller of (P, Q), the larger saturating in the tails. - SB2006 autoconversion: ϕ_au ∝ τ^0.7 has a vertical tangent at τ = 0, so ∂/∂q_rai is NaN at zero rain with cloud present. Gate the enhancement below ϵ_numerics_2M_M; value at q_rai = 0 unchanged. - sgs_weight_function: guard the lower tail where atanh → -Inf poisons Float32 rime-ratio partials. - Sentinel returns: typing from one argument (eltype(q_tot)-style) makes the return union-typed and heap-boxed under mixed-type callers — silently, since the box lives in the abstract return convention (JET and the optimized IR show nothing). Add Utilities.promote_typeof and apply it across the verified hits in the 2M, NonEq, P3, and BMT code. Tests: per-row Jacobian-vs-FD oracle; AD coverage for the regularised-ratio band, the tail shape slots, and mixed arguments; sentinel_type_tests assert concrete returns per signature. Deferred: the logλ shape solve (needs ∂/∂a of forward gamma_inc) and the 1M sentinel leaves.
415fe44 to
b150213
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Implicit (Rosenbrock-type) microphysics substepping needs Jacobians of the pointwise 2M+P3 tendencies w.r.t. the 8 prognostic species, so
ForwardDiff.jacobianmust work throughbulk_microphysics_tendencies(logλis held fixed). This PR does some groundwork to enable this.P3StateandΓ_inclto accept a mix ofDualand floating point arguments.Dualmethod forSF.gamma_inc_inv(What aboutgamma_inc_inv's derivative ? JuliaMath/SpecialFunctions.jl#452) locally. Ideally this should be added toSpecialFunctions, and I might do so later, but adding here to move forward locally for now. The implementation uses a mix of analytical and central differences of the forward map.ice_mass_coeffsreturns one tuple type (needed for efficient AD).Utilities.promote_typeof), so a caller mixingDualand float arguments does not get a union-typed, heap-boxed return.sgs_weight_function(lower tail rounds 1 - a to 1, so atanh(-1) = -Inf) and SB2006 autoconversion (vertical tangent ofτ^0.7at zero rain), which previously could produce NaNs.get_distribution_logλ_from_prognosticcalled an undefined function.I might try to differentiate through the
logλshape solve later; which needs ∂/∂a of the forwardgamma_inc(JuliaMath/SpecialFunctions.jl#317).Tests cover Jacobians at four regimes for both float types against per-row central finite differences, the
gamma_inc_invrule into theq = eps()tail, the regularised-ratio band, and that the touched functions stay concretely typed under single-Dualand all-Dualargument mixes.