Adaptive cloud coagulation on CUDA
View code · Download Python script
Cloud scavenging over a fixed observation interval
Section titled “Cloud scavenging over a fixed observation interval”How many native collision passes does a cloud box need between observations? Follow two synthetic, bimodal populations for 600 s, observing every 100 s. The sparse mode contains 10% water droplets. Smaller aerosol particles and droplets each carry one represented particle. The sampler combines Brownian motion, differential settling (SP2016) and turbulent shear (ST1956). Run on a CUDA device. See the prepared quick start for the upload and checkpoint lifecycle.
Python · section 1
"""Recover an evolving CUDA coagulation study from a bounded work rejection."""
from time import perf_counter
import matplotlib.pyplot as pltimport numpy as npimport warp as wpfrom particula import Aerosol, plottingfrom particula.dynamics import ( BrownianCoagulationStrategy, Coagulation, CombineCoagulationStrategy, SedimentationCoagulationStrategy, TurbulentShearCoagulationStrategy,)from particula.gas import EnvironmentData, GasDatafrom particula.gpu.kernels.coagulation import ( CoagulationSubcyclingConfig, CoagulationSubcyclingError, CoagulationWorkConfig,)from particula.particles import ParticleData
def cloud(slots): """Build a one-box mixture at 2,000 particles per cubic centimetre.""" collectors = slots // 10 radii = np.r_[ np.geomspace(50e-9, 200e-9, slots - collectors), np.geomspace(2e-6, 8e-6, collectors), ] particles = ParticleData( masses=(4 * np.pi / 3 * 1000 * radii**3)[None, :, None], population=np.ones((1, slots)), charge=np.zeros((1, slots)), density=np.array([1000.0]), volume=np.array([slots / 2e9]), distribution_type="particle_resolved", ) return Aerosol( particles=particles, gas=GasData( ["air"], np.array([0.029]), np.zeros((1, 1)), np.array([False]) ), environment=EnvironmentData( np.array([283.15]), np.array([101325.0]), np.zeros((1, 1)) ), )Start with a one-pass limit. If a live interval needs more work, checkpoint the committed state and raise the pass allowance. Replay the same observation interval on a newly captured owner. Inspect the number of accepted passes, collision totals and the wall time of the synchronous step call. An extra status check on the idle stream measures the observation overhead separately.
Python · section 2
def simulate(): """Run two physical resolutions at the same density and observation times.""" model = "particle_resolved" process = Coagulation( CombineCoagulationStrategy( [ BrownianCoagulationStrategy(model), SedimentationCoagulationStrategy(model), TurbulentShearCoagulationStrategy(model, 1e-4, 1.2), ] ) ) ceiling = 64 * 1024**2 rows = [] for slots in (5000, 10000): initial = cloud(slots) options = dict( time_step=100.0, device="cuda:0", root_seed=17, coagulation_work_config=CoagulationWorkConfig( max_trials_per_box=256 ), coagulation_subcycling=CoagulationSubcyclingConfig( 0.01, 1, ceiling ), ) sim = process.prepare(initial, **options) retried = False events = [] try: sim.capture() while sim.completed_steps < 6: start = perf_counter() try: sim.step() except CoagulationSubcyclingError: if retried: raise saved = sim.checkpoint() sim.close() policy = CoagulationSubcyclingConfig(0.01, 128, ceiling) sim = process.resume(saved, retry_subcycling=policy) sim.capture() retried = True continue seconds = perf_counter() - start start = perf_counter() sim.synchronize() idle_sync_seconds = perf_counter() - start diag = sim.coagulation_diagnostics() events.append( dict( time_s=sim.simulated_time, passes=diag["substep_count"], seconds=seconds, idle_sync_seconds=idle_sync_seconds, collisions=int(diag["accepted_collisions"].sum()), ) ) final = sim.snapshot().particles np.testing.assert_array_equal( final.population.sum(), slots - sum(e["collisions"] for e in events), ) np.testing.assert_array_equal( final.population[final.population > 0], 1.0 ) np.testing.assert_array_equal(final.charge, 0.0) relative_drift = ( np.sum(final.masses * final.population[..., None]) / np.sum(initial.particles.masses) - 1 ) assert ( abs(relative_drift) < 2e-14 and sim.simulated_time == 600 and retried ) rows.append( dict( slots=slots, events=events, allocation=diag["allocation_bytes"], relative_mass_drift=float(relative_drift), ) ) finally: sim.close() return dict(device=wp.get_device("cuda:0").name, rows=rows)A step in the first panel counts how often the live sampler had to recompute rates. The second panel tracks the accepted mergers. Compare their shapes: trial demand is set by the fastest possible pair in a box, while acceptance also depends on the particular pairs drawn.
Python · section 3
@plt.style.context("particula.default")def plot_results(report): """Plot observed subcycle counts and cumulative accepted mergers.""" fig, axes = plotting.subplots(1, 2) for row in report["rows"]: events = row["events"] times = [e["time_s"] for e in events] label = f"{row['slots']:,} initial particles" axes[0].plot( times, [e["passes"] for e in events], marker="o", label=label ) axes[1].plot( times, np.cumsum([e["collisions"] for e in events]), marker="o", label=label, ) for ax in axes: ax.set_xlabel("Simulated time (s)") axes[0].set_ylabel("Passes per 100 s interval") axes[1].set_ylabel("Cumulative mergers") plotting.legend(axes[0]) plotting.fit(fig) return fig
if __name__ == "__main__": report = simulate() print(report) plot_results(report) plt.show()Explicit cuda invocation
report = simulate()print(report)figure = plot_results(report)plt.show()Warp 1.11.1 initialized:
CUDA Toolkit 12.9, Driver 13.4
Devices:
"cpu" : "CPU"
"cuda:0" : "NVIDIA GeForce RTX 5060" (8 GiB, sm_120, mempool enabled)
Kernel cache:
~/.cache/warp/1.11.1
Module particula.gpu.kernels.coagulation 54feb45 load on device 'cuda:0' took 278.89 ms (cached)
Module particula.gpu.kernels.coagulation 358b13f load on device 'cuda:0' took 513.79 ms (cached)
Module particula.gpu.kernels.environment e11375e load on device 'cuda:0' took 3.78 ms (cached)
Module particula.gpu.kernels._coagulation_native f93a244 load on device 'cuda:0' took 3.41 ms (cached)
Module warp.utils f35afd3 load on device 'cuda:0' took 2.78 ms (cached)
Module warp.math edde159 load on device 'cuda:0' took 2.08 ms (cached)
Module warp.marching_cubes fb6e712 load on device 'cuda:0' took 2.82 ms (cached)
Module particula.gpu.properties.gas_properties edde159 load on device 'cuda:0' took 2.06 ms (cached)
Module particula.gpu.properties.particle_properties edde159 load on device 'cuda:0' took 2.10 ms (cached)
Module particula.gpu.dynamics.coagulation_funcs edde159 load on device 'cuda:0' took 2.06 ms (cached)
Module particula.gpu.dynamics.condensation_funcs edde159 load on device 'cuda:0' took 2.08 ms (cached)
Module particula.gpu.dynamics.ion_pair_data edde159 load on device 'cuda:0' took 4.91 ms (cached)
Module particula.gpu.dynamics.van_der_waals edde159 load on device 'cuda:0' took 6.94 ms (cached)
Module particula.gpu.kernels._coagulation_budget b4a3978 load on device 'cuda:0' took 6.91 ms (cached)
Module particula.gpu.kernels._coagulation_subcycling edde159 load on device 'cuda:0' took 6.64 ms (cached)
Module particula.execution.communication 2909959 load on device 'cuda:0' took 13.23 ms (cached)
Module particula.gpu.kernels._population_validation edde159 load on device 'cuda:0' took 2.05 ms (cached)
Module particula.gpu.kernels.communication ac9b2a2 load on device 'cuda:0' took 12.65 ms (cached)
Module particula.gpu.kernels._condensation_native edde159 load on device 'cuda:0' took 9.36 ms (cached)
Module particula.gpu.kernels._condensation_parallel edde159 load on device 'cuda:0' took 2.01 ms (cached)
Module particula.gpu.kernels.thermodynamics edde159 load on device 'cuda:0' took 4.42 ms (cached)
Module particula.gpu.kernels.condensation edde159 load on device 'cuda:0' took 4.87 ms (cached)
Module particula.gpu.kernels.exhaustion 616f81d load on device 'cuda:0' took 6.66 ms (cached)
Module particula.gpu.kernels.slot_management 0375e2c load on device 'cuda:0' took 4.96 ms (cached)
Module particula.gpu.kernels.nucleation 1124f09 load on device 'cuda:0' took 9.31 ms (cached)
Module particula.execution.gpu_resources 30f296c load on device 'cuda:0' took 5.10 ms (cached)
Module particula.gpu.warp_types edde159 load on device 'cuda:0' took 2.05 ms (cached)
Module particula.execution.prepared_diagnostics f551690 load on device 'cuda:0' took 4.22 ms (cached)
Module particula.gpu.kernels._brownian_parallel 5433dfc load on device 'cuda:0' took 10.25 ms (cached)
Module particula.execution.prepared_guards 5366864 load on device 'cuda:0' took 6.61 ms (cached)
Module particula.gpu.kernels._brownian_parallel f84feee load on device 'cuda:0' took 9.06 ms (cached)
Module particula.gpu.kernels._coagulation_subcycling 93efbd4 load on device 'cuda:0' took 6.80 ms (cached)
Module particula.gpu.kernels._brownian_parallel c0acfec load on device 'cuda:0' took 17.83 ms (cached)
Module particula.gpu.kernels.coagulation 2feff1b load on device 'cuda:0' took 514.33 ms (cached)
Module particula.gpu.kernels.coagulation 93d340b load on device 'cuda:0' took 460.72 ms (cached)
Module particula.gpu.kernels._coagulation_subcycling ca6b3ba load on device 'cuda:0' took 5.36 ms (cached)
Module particula.gpu.kernels._coagulation_subcycling a49d377 load on device 'cuda:0' took 9.02 ms (cached)
{'device': 'NVIDIA GeForce RTX 5060', 'rows': [{'slots': 5000, 'events': [{'time_s': 100.0, 'passes': 8, 'seconds': 0.57500971108675, 'idle_sync_seconds': 0.00013103289529681206, 'collisions': 30}, {'time_s': 200.0, 'passes': 9, 'seconds': 0.6414334280416369, 'idle_sync_seconds': 0.00012795696966350079, 'collisions': 24}, {'time_s': 300.0, 'passes': 10, 'seconds': 0.7012007699813694, 'idle_sync_seconds': 0.00013537099584937096, 'collisions': 32}, {'time_s': 400.0, 'passes': 10, 'seconds': 0.6948769600130618, 'idle_sync_seconds': 0.0001311739906668663, 'collisions': 36}, {'time_s': 500.0, 'passes': 10, 'seconds': 0.6873223069123924, 'idle_sync_seconds': 0.00012321886606514454, 'collisions': 34}, {'time_s': 600.0, 'passes': 10, 'seconds': 0.6823172860313207, 'idle_sync_seconds': 0.0001364140771329403, 'collisions': 28}], 'allocation': {'live': 503876, 'rollback': 503876}, 'relative_mass_drift': 0.0}, {'slots': 10000, 'events': [{'time_s': 100.0, 'passes': 16, 'seconds': 4.200548965018243, 'idle_sync_seconds': 0.0001904941163957119, 'collisions': 72}, {'time_s': 200.0, 'passes': 18, 'seconds': 4.648463296005502, 'idle_sync_seconds': 0.00015683192759752274, 'collisions': 73}, {'time_s': 300.0, 'passes': 18, 'seconds': 4.568113900022581, 'idle_sync_seconds': 0.00018772808834910393, 'collisions': 76}, {'time_s': 400.0, 'passes': 19, 'seconds': 4.760749721899629, 'idle_sync_seconds': 0.00014064181596040726, 'collisions': 67}, {'time_s': 500.0, 'passes': 22, 'seconds': 5.420935877831653, 'idle_sync_seconds': 0.00014405813999474049, 'collisions': 73}, {'time_s': 600.0, 'passes': 22, 'seconds': 5.346477173967287, 'idle_sync_seconds': 0.00013800710439682007, 'collisions': 72}], 'allocation': {'live': 964516, 'rollback': 964516}, 'relative_mass_drift': 0.0}]}

Both curves represent one stochastic realization at each resolution. The conservation and count checks address bookkeeping. To estimate uncertainty in scavenging rates, repeat across independent seeds and refine the observation interval while keeping concentration and the size distribution fixed.
Study resources
Section titled “Study resources”- Adaptive CUDA coagulation trajectories
- How to use this · Prepared GPU simulations
- Qualification record · GPU Coagulation Validation Record
Explore this topic: Prepared simulations · Direct kernels