Skip to content

Conversation

haakon-e
Copy link
Member

@haakon-e haakon-e commented Jul 25, 2025

Purpose

This pull request introduces several improvements and refactorings to the P3 microphysics scheme and its related utilities, focusing on code clarity, numerical stability, and performance benchmarking. The most significant changes are grouped below by theme.

P3 Scheme Refactoring and Utility Improvements

  • Added the get_bounded_thresholds utility to consistently clamp and sanitize particle size thresholds, and refactored related functions (integral_bounds, get_segments) to use this for more robust interval handling. This improves the reliability of segment and bounds calculations for particle distributions. [1] [2]
  • Refactored particle property functions (ice_mass, ice_density, get_∂mass_∂D_coeffs, ∂ice_mass_∂D, ϕᵢ) to accept explicit arguments or state objects rather than variadic arguments, improving type stability and readability. [1] [2] [3] [4]

Numerical Integration and Terminal Velocity

  • Improved numerical integration routines by adding input validation (returning zero for invalid bounds) and using unrolled_sum for efficient summation in particle_terminal_velocity, enhancing both correctness and performance. [1] [2] [3] [4]

Consistency and Readability Updates

  • Standardized unit conversion factors in ice nucleation routines for clarity (e.g., using 1000 and 1_000_000 instead of scientific notation). [1] [2]
  • Various code style improvements in test and benchmark files, including argument formatting and variable naming for clarity and maintainability. [1] [2] [3] [4] [5] [6] [7]

Testing and Benchmarking Enhancements

  • Expanded and updated performance benchmarks to cover new and refactored P3 routines, including state construction, distribution parameter calculation, terminal velocities, and ice nucleation/melt processes. Also added type stability checks in test cases. [1] [2] [3]

These changes collectively improve the robustness, maintainability, and test coverage of the P3 microphysics codebase.

@haakon-e
Copy link
Member Author

haakon-e commented Jul 25, 2025

@haakon-e haakon-e force-pushed the he/feat-ice-terminal-velocity branch from a656b96 to b95a0b7 Compare July 25, 2025 20:35
@haakon-e haakon-e force-pushed the he/fix-remove-allocations-in-p3 branch from 465789a to f246c9a Compare July 25, 2025 20:35
@haakon-e haakon-e force-pushed the he/feat-ice-terminal-velocity branch from b95a0b7 to b1b7058 Compare July 25, 2025 20:42
@haakon-e haakon-e force-pushed the he/fix-remove-allocations-in-p3 branch from f246c9a to 34838bc Compare July 25, 2025 20:42
@haakon-e haakon-e force-pushed the he/feat-ice-terminal-velocity branch from b1b7058 to 5d5af66 Compare July 25, 2025 22:05
@haakon-e haakon-e force-pushed the he/fix-remove-allocations-in-p3 branch 5 times, most recently from 6e14234 to 07f1c02 Compare July 26, 2025 04:21
Copy link

codecov bot commented Jul 26, 2025

Codecov Report

❌ Patch coverage is 94.28571% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.79%. Comparing base (37b9712) to head (f834e76).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #621      +/-   ##
==========================================
+ Coverage   91.65%   92.79%   +1.14%     
==========================================
  Files          48       48              
  Lines        1953     1957       +4     
==========================================
+ Hits         1790     1816      +26     
+ Misses        163      141      -22     
Components Coverage Δ
src 93.98% <94.28%> (+1.19%) ⬆️
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/feat-ice-terminal-velocity branch from 5d5af66 to f024268 Compare July 30, 2025 21:28
@haakon-e haakon-e force-pushed the he/feat-ice-terminal-velocity branch from f024268 to 37b9712 Compare August 14, 2025 17:53
Base automatically changed from he/feat-ice-terminal-velocity to main August 14, 2025 18:23
@haakon-e haakon-e force-pushed the he/fix-remove-allocations-in-p3 branch 3 times, most recently from c96aaf3 to f7364a3 Compare August 14, 2025 22:48
@@ -337,7 +338,7 @@ Needed for numerical integrals in the P3 scheme.
function particle_terminal_velocity(velocity_params::CMP.TerminalVelocityType, ρs...)
(ai, bi, ci) = Chen2022_vel_coeffs(velocity_params, ρs...)
v_terms = Chen2022_monodisperse_pdf.(ai, bi, ci) # tuple of functions
v_term(D) = sum(@. D |> v_terms)
v_term(D) = unrolled_sum(v_term -> v_term(D), v_terms)
Copy link
Member Author

Choose a reason for hiding this comment

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

Adding UnrolledUtilities and the unrolled_sum statement is needed for inference on julia 1.10 only. This is a small package with no dependencies, so it should be fine. Further, it is a dependency of ClimaCore, so the addition doesn't matter from the perspective of ClimaAtmos/KiD anyways. If we ever drop support for julia 1.10, then this could potentially be revisited.

Comment on lines +1249 to +1257
ref_val = if FT == Float64
FT(0.0002736160475969029)
else
# loss of precision due to
# `exp(-B * V_l_converted * Δt * exp(a * Tₛ))` -> 0.9999999 (Float32)
# instead of 0.9999998631919762 (Float64).
FT(0.00023841858f0)
end
TT.@test Array(output)[1] ≈ ref_val
Copy link
Member Author

Choose a reason for hiding this comment

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

This arises from fixing the type instability here

@haakon-e haakon-e force-pushed the he/fix-remove-allocations-in-p3 branch from f7364a3 to f834e76 Compare August 14, 2025 23:32
@haakon-e haakon-e merged commit 566b884 into main Aug 15, 2025
15 checks passed
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