Skip to content

feat(P3): Gauss-Legendre quadrature, integrand breakpoints -> GL(6) default#741

Open
haakon-e wants to merge 3 commits into
mainfrom
he/ice-selfcol-triangle
Open

feat(P3): Gauss-Legendre quadrature, integrand breakpoints -> GL(6) default#741
haakon-e wants to merge 3 commits into
mainfrom
he/ice-selfcol-triangle

Conversation

@haakon-e

@haakon-e haakon-e commented Jun 22, 2026

Copy link
Copy Markdown
Member

Set the default P3 quadrature rule to Gauss-Legendre of order 6, and place every known feature of the P3 size-distribution integrands on a quadrature subinterval boundary so that a low-order rule suffices.

Stacked on #757 (mechanical refactors and small fixes).


Each P3 size-distribution integrand has a small set of known non-smooth features: the mass-regime thresholds, the terminal-velocity regime break, the ice-liquid fall-speed crossing, the exponential decay of the distribution tail, and the wet-growth onset. Placing each feature on a subinterval boundary leaves the integrand smooth within every subinterval, where Gauss-Legendre converges geometrically. Order 6 then meets (a handwavily reasonable) target accuracy (see below).

API

  • The quadrature rule lives on P3IceParams. build_quadrature and the stored quadrature_order field are removed; the parameter constructors take a quadrature_order (or quad) keyword.
  • integrate and the P3 process functions require quad.
  • VolumetricCollisionRate stores the ice and liquid terminal-velocity closures as fields, so the collision integrals read the velocities and their structure from the integrand itself: the crossing bounds, the closed-form rain integrals (coefficients read from the closure), and the wet-growth onset balance.
  • velocity_breakpoints(v_term) returns the diameters where a terminal-velocity closure changes functional form. velocity_integral_bounds consumes the closure, so the integral bounds no longer assume the Chen 2022 cutoff field.

Breakpoints

  • Terminal-velocity regime break (Chen small/large ice): a subinterval boundary (velocity_integral_bounds) for the velocity-weighted integrals, ice_melt (through its ventilation factor), the outer collision integral, and ice self-collection.
  • Ice-liquid fall-speed crossing v_l(D) = v_i(Dᵢ): a subinterval boundary of the inner collision integrals (crossing_integral_bounds). The rain path computes the crossover diameter once per outer node and shares it across the closed-form N and M components and the B_rim quadrature. With the crossing on a boundary, the collision-efficiency family (collision sources and ice self-collection) converges with order rather than saturating at a fixed error: worst-case relative error falls from 1.2e-2 at order 6 to 5.0e-6 at order 32.
  • Distribution tail at three decay lengths (3/λ): the upper bound is the (1 - p) quantile, so without this breakpoint the last subinterval spans most of the log(1/p) ≈ 11.5 decay lengths and sets the low-order error in large-mean-size states. With the breakpoint, ice self-collection in the hail-core states reaches 7.1e-5 relative error at order 12 (1.1e-2 at order 6).
  • Wet-growth onset (where collected liquid mass balances the Musil freeze limit): a step in the outer integrand, through the freeze/shed min() partition and the wet-growth indicator. wet_growth_onset_diameter locates up to two balance points (the window closes again at large sizes) from a closed-form balance refined by bracketed root-finding, and places them on outer subinterval boundaries. At order 12 the shedding component reaches ~1e-7 relative error; the wet-growth-indicator component sits near 2e-4 and is nearly order-independent (2.5e-4 at order 6, 2.3e-4 at order 12), set by the onset placement rather than the node count.
  • Ice self-collection integrates over the upper triangle D₁ ≤ D₂, which places the D₁ = D₂ derivative discontinuity of the relative fall speed on a boundary and halves the integrand evaluations. A test cross-checks the triangle against the full-square double integral with the ½ pair-counting factor.

Accuracy and the default order

I wrote a script that let's me test the accuracy for different orders (since I suspect I'll revisit this in the future).
Over each P3 integral, the collision source vector, and the full 2M+P3 bulk tendency vector, across 17 "regime" states including hail cores. Baseline ("truth") is a GL(128) reference (converged). The main column runs the same script on main against main's own GL(128) reference (both without breakpoints). The bulk-tendency error separates into transport components (sedimentation velocities, melt) and components proportional to the collision efficiency (collision sources, ice self-collection, rime):

main (GL(16) default) GL(5) GL(6) (default) GL(7) GL(8)
transport p95 / max 1.7e-3 / 2.0e-2 7.1e-4 / 4.5e-3 3.2e-4 / 1.2e-3 4.4e-5 / 3.9e-4
E-family p95 / max 3.9e-2 / 6.9e-2 1.1e-2 / 1.2e-2 2.0e-3 / 7.8e-3 6.1e-4 / 1.1e-3
bulk p95 / max 1.4e-3 / 6.2e-2 2.8e-2 / 6.9e-2 8.9e-3 / 1.4e-2 1.5e-3 / 7.8e-3 2.9e-4 / 9.0e-4

Every bulk error above 0.45% is in a collision-efficiency component, which sets E = 1. The package's own single-moment scheme assigns collision efficiencies from 0.1 to 1.0 for the analogous liquid-frozen and rain-frozen collisions (cloud_liquid_snow_collision_efficiency = 0.1, cloud_ice_rain_collision_efficiency = 1.0), so the E = 1 choice is an upper bound uncertain to a factor of order ten. That uncertainty exceeds the ~1% quadrature error on those rates, so resolving them below ~1% adds no physical accuracy while E is fixed. The transport components, which do not include the E = 1 assumption, converge to at most 0.45% (worst-case 4.5e-3 at order 6). Order 5 reaches 2.0e-2 in the transport family and is rejected. Order 7 matches main's bulk p95 and is far below main on the worst-case bulk error; order 8 is below main on both. Both remain one-keyword changes (quadrature_order). Parameterizing the collision efficiency is the top follow-up, and this table should be revisited when it lands.

Runtime

Then testing the runtime with these changes in Float64 on Julia 1.12.

cell main (GL(16)) this PR (GL(6) + breakpoints) ratio
warm (ice-free) 0.48 µs 0.81 µs 1.7
mixed-phase 5011 µs 2063 µs 0.41
hail core 4824 µs 2143 µs 0.44

The warm cell performs no size-distribution integrals; its sub-microsecond time differs between the two by about 0.3 µs, reproducibly across runs and negligible against the ice-path cost. The he/p3-refactors base (the PR base) gives the same GL(16) baseline as main (5011 / 4824 µs here versus 5017 / 4869 µs for main in a separate run, for the two ice cells), so the speedup is attributable to the order reduction, the closed-form rain integrals, and the triangle self-collection, net of the onset cost. The onset location costs about a quarter of the collision call (a log-spaced scan, RootSolvers Brent refinements of the closed-form balance, and two additional outer subintervals). Removing the step also removes a state-dependent discontinuity from the tendencies that the implicit solver's Jacobian must differentiate.

The absolute GL(16) baseline depends on the Julia version: on Julia 1.12 the 16-node collision double integral runs at ~5000 µs, roughly 1.8× the figure a lower Julia version reports, while the GL(6) time is stable across versions. The ratio above is therefore the Julia 1.12 result (on lower versions the ratio is closer to 0.75). The measurement ran on the clima cluster.


Tests:

  • test/p3_quadrature_error_study.jl (run_quadrature_error_study(); usage in the developer guide) regenerates the accuracy table when the quadrature machinery or the process integrands change.
  • test/bulk_tendencies_quadrature_tests.jl locks orders 6 through 12 as a regression test against a GL(200) reference, with graupel and hail-core states in the state set.
  • The P3 and 2M integral tests use Gauss-Legendre in place of Chebyshev-Gauss, matching the default: scheme-path calls use GL(6), and the standalone reference integrals use the smallest order meeting their tolerance. On these integrands Gauss-Legendre is 1-2 orders of magnitude more accurate than Chebyshev-Gauss at matched order and cost; the endpoint behavior that originally motivated Chebyshev-Gauss was the unresolved crossing and onset structure, which the breakpoints remove.

@haakon-e
haakon-e force-pushed the he/ice-selfcol-triangle branch from 9c0a58a to 09ba907 Compare June 22, 2026 23:58
@haakon-e
haakon-e force-pushed the he/p3-aspect-ratio-fix branch from 69f01fa to abdbd39 Compare June 23, 2026 00:32
@haakon-e
haakon-e force-pushed the he/ice-selfcol-triangle branch from 09ba907 to e4595a3 Compare June 23, 2026 00:32
@haakon-e
haakon-e force-pushed the he/p3-aspect-ratio-fix branch from abdbd39 to 7f9b793 Compare June 23, 2026 21:32
@haakon-e
haakon-e force-pushed the he/ice-selfcol-triangle branch from e4595a3 to 2068d98 Compare June 23, 2026 21:32
@haakon-e
haakon-e force-pushed the he/p3-aspect-ratio-fix branch from 7f9b793 to 3aa2995 Compare June 23, 2026 22:26
@haakon-e
haakon-e force-pushed the he/ice-selfcol-triangle branch from 2068d98 to 49695f3 Compare June 23, 2026 22:26
@haakon-e
haakon-e force-pushed the he/p3-aspect-ratio-fix branch from 3aa2995 to 22a3865 Compare June 24, 2026 21:56
@haakon-e
haakon-e force-pushed the he/ice-selfcol-triangle branch from 49695f3 to 2aabe66 Compare June 24, 2026 21:56
@haakon-e
haakon-e force-pushed the he/p3-aspect-ratio-fix branch from 22a3865 to f42aeb2 Compare June 25, 2026 18:01
@haakon-e
haakon-e force-pushed the he/ice-selfcol-triangle branch from 2aabe66 to 90f0aea Compare June 25, 2026 18:01
@codecov

codecov Bot commented Jun 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.66667% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.81%. Comparing base (cd6581c) to head (355b541).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #741      +/-   ##
==========================================
- Coverage   92.92%   92.81%   -0.11%     
==========================================
  Files          56       56              
  Lines        2896     2952      +56     
==========================================
+ Hits         2691     2740      +49     
- Misses        205      212       +7     
Components Coverage Δ
src 93.59% <96.66%> (-0.13%) ⬇️
ext 69.47% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@haakon-e
haakon-e force-pushed the he/p3-aspect-ratio-fix branch from f42aeb2 to b4b9090 Compare June 26, 2026 00:38
@haakon-e
haakon-e force-pushed the he/ice-selfcol-triangle branch from 90f0aea to 7e848a2 Compare June 26, 2026 00:38
@haakon-e
haakon-e force-pushed the he/p3-aspect-ratio-fix branch from b4b9090 to aeb57d4 Compare June 26, 2026 01:22
@haakon-e
haakon-e force-pushed the he/ice-selfcol-triangle branch from 7e848a2 to dfa0e14 Compare June 26, 2026 01:22
@haakon-e
haakon-e force-pushed the he/p3-aspect-ratio-fix branch from aeb57d4 to 93c9f4a Compare June 26, 2026 01:31
@haakon-e
haakon-e force-pushed the he/ice-selfcol-triangle branch from dfa0e14 to 0604b2c Compare June 26, 2026 01:31
@haakon-e
haakon-e force-pushed the he/p3-aspect-ratio-fix branch from 93c9f4a to 22beeec Compare July 1, 2026 05:35
@haakon-e
haakon-e force-pushed the he/ice-selfcol-triangle branch 3 times, most recently from 2bd64ea to c5474cc Compare July 3, 2026 03:53
@haakon-e
haakon-e changed the base branch from he/p3-refactors to he/rosenbrock-modes July 7, 2026 19:07
@haakon-e
haakon-e force-pushed the he/rosenbrock-modes branch from 090fd49 to 043da95 Compare July 8, 2026 04:34
@haakon-e
haakon-e changed the base branch from he/rosenbrock-modes to he/p3-refactors July 8, 2026 04:39
@haakon-e
haakon-e force-pushed the he/ice-selfcol-triangle branch 2 times, most recently from 4453b1c to 2293db0 Compare July 9, 2026 04:16
Base automatically changed from he/p3-refactors to main July 10, 2026 00:26
@trontrytel trontrytel added the Needs review Please review my pull request label Jul 10, 2026
Comment thread src/P3_processes.jl
Comment thread src/P3_processes.jl
Comment thread src/P3_processes.jl
# fallback values typed by the promotion of the node and the captured state
# (mixed plain/Dual under differentiation)
FT = UT.promote_typeof(Dᵢ, ΔT, Δρᵥ_sat, denom)
Tₐ ≥ T_frz && return zero(FT) # No collisional freezing above the freezing temperature

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

just rewriting without branches here an a few other places.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Claude is asking if ForwardDiff will propagate the non-finite rate through FT(rate) before ifelse discards it. Will Dual arithmetic with Inf work for the derivative?

Comment thread src/P3_processes.jl
Comment thread src/P3_processes.jl
Comment thread src/P3_processes.jl
Comment thread src/P3_processes.jl
@trontrytel trontrytel added Approved 🍀 and removed Needs review Please review my pull request labels Jul 10, 2026
@haakon-e
haakon-e force-pushed the he/ice-selfcol-triangle branch from 8ef7fe6 to bbb3da8 Compare July 10, 2026 23:15
@haakon-e
haakon-e enabled auto-merge July 10, 2026 23:21
@haakon-e
haakon-e force-pushed the he/ice-selfcol-triangle branch from bbb3da8 to 8a2faf4 Compare July 11, 2026 00:58
haakon-e added 3 commits July 10, 2026 20:23
…lt order 6

Set the default P3 size-distribution quadrature to Gauss-Legendre of order 6, and place
each known feature of the integrands on a quadrature subinterval boundary: the
terminal-velocity regime break, the ice-liquid fall-speed crossing, the distribution-tail
decay scale, and the wet-growth onset. The integrands are smooth per subinterval, so a
low-order rule converges. Ice self-collection integrates the upper triangle D1 <= D2, and
the collision integrals read the velocity closures and their breakpoints from the
VolumetricCollisionRate. The quadrature order is a Microphysics2MParams parameter.
Add test/p3_quadrature_error_study.jl, which regenerates the accuracy table over the
quadrature-consuming P3 integrals and the 2M+P3 bulk tendency. Switch the P3 and 2M integral
tests to Gauss-Legendre to match the default, cross-check ice self-collection against the
full-square double integral, cover the x_min = 0 mass limit, add a full-tendency benchmark,
and set zero-allocation budgets for the 2M+P3 GPU kernel.
…collision cross-sections

Add the quadrature_order keyword to the Microphysics2MParams docstring signature, document
the error-study usage in the developer guide, list ice_self_collection and the ice-ice and
ice-liquid collision cross-sections in the API @docs block so their references resolve, and
update the melting and terminal-velocity plot scripts.
@haakon-e
haakon-e force-pushed the he/ice-selfcol-triangle branch from dc1db96 to 355b541 Compare July 11, 2026 03:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants