Repository navigation
Expand file tree
/
Copy pathgpu_direct_nucleation.py
More file actions
206 lines (185 loc) · 6.33 KB
/
Copy pathgpu_direct_nucleation.py
File metadata and controls
206 lines (185 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""Run the bounded direct-Warp nucleation step with explicit transfers.
The package-exported ``nucleation_step_gpu`` is used for execution, while
configuration and sidecar records are imported from their concrete modules.
Warp is required; this example intentionally has no CPU-physics fallback.
"""
import numpy as np
import numpy.testing as npt
import warp as wp
from particula.gas import EnvironmentData, GasData
from particula.gpu import (
from_warp_environment_data,
from_warp_gas_data,
from_warp_particle_data,
to_warp_environment_data,
to_warp_gas_data,
to_warp_particle_data,
)
from particula.gpu.kernels import nucleation_step_gpu
from particula.gpu.kernels.exhaustion import ResamplingBuffers
from particula.gpu.kernels.nucleation import (
NucleationConfig,
NucleationDiagnosticBuffers,
NucleationExhaustionBuffers,
NucleationExhaustionControls,
NucleationFinalizedDemandBuffers,
NucleationScratchBuffers,
)
from particula.particles import ParticleData
from particula.util.constants import AVOGADRO_NUMBER
def _buffers(
device: str,
) -> tuple[
NucleationScratchBuffers,
NucleationFinalizedDemandBuffers,
NucleationDiagnosticBuffers,
NucleationExhaustionBuffers,
]:
"""Allocate caller-owned sidecars for the B=1, N=2, S=1 fixture.
Args:
device: Warp device that owns every allocated sidecar.
Returns:
Scratch, finalized-demand, diagnostic, and exhaustion sidecars on
``device``.
"""
def f64(shape: tuple[int, ...]) -> object:
"""Allocate a float64 sidecar on the selected Warp device.
Args:
shape: Dimensions of the sidecar.
Returns:
A zero-initialized Warp float64 array.
"""
return wp.zeros(shape, dtype=wp.float64, device=device)
def ones(shape: tuple[int, ...]) -> object:
"""Allocate a float64 sidecar initialized to one.
Args:
shape: Dimensions of the sidecar.
Returns:
A one-initialized Warp float64 array.
"""
return wp.ones(shape, dtype=wp.float64, device=device)
def i32(shape: tuple[int, ...]) -> object:
"""Allocate an int32 sidecar on the selected Warp device.
Args:
shape: Dimensions of the sidecar.
Returns:
A zero-initialized Warp int32 array.
"""
return wp.zeros(shape, dtype=wp.int32, device=device)
resampling = ResamplingBuffers(
i32((1,)),
i32((1,)),
i32((1, 2)),
i32((1, 2)),
i32((1, 2)),
f64((1, 2, 1)),
f64((1, 2)),
f64((1, 2)),
f64((1, 2)),
f64((1,)),
f64((1,)),
f64((1,)),
f64((1,)),
i32((1,)),
)
return (
NucleationScratchBuffers(f64((1,)), f64((1,)), f64((1,))),
NucleationFinalizedDemandBuffers(i32((1,)), f64((1,)), f64((1, 1))),
NucleationDiagnosticBuffers(
i32((1,)), i32((1, 2)), i32((1, 2)), i32((1,)), i32((1,))
),
NucleationExhaustionBuffers(
resampling,
f64((1,)),
f64((1,)),
ones((1,)),
ones((1,)),
ones((1,)),
f64((1,)),
i32((1,)),
i32((1,)),
i32((1,)),
i32((1,)),
i32((1, 2)),
),
)
def run_example() -> tuple[ParticleData, GasData, EnvironmentData]:
"""Run one deterministic direct-Warp nucleation event on Warp CPU.
Returns:
Restored CPU particle, gas, and environment data after synchronization.
"""
device = "cpu"
particles = ParticleData(
masses=np.zeros((1, 2, 1), dtype=np.float64),
concentration=np.zeros((1, 2), dtype=np.float64),
charge=np.zeros((1, 2), dtype=np.float64),
density=np.array([1000.0], dtype=np.float64),
volume=np.array([1.0], dtype=np.float64),
)
gas = GasData(
name=["precursor"],
molar_mass=np.array([0.1]),
concentration=np.array([[1.0]], dtype=np.float64),
partitioning=np.array([True]),
)
environment = EnvironmentData(
temperature=np.array([300.0]),
pressure=np.array([101325.0]),
saturation_ratio=np.array([[1.0]]),
)
initial_inventory = gas.concentration.copy()
gpu_particles = to_warp_particle_data(particles, device=device)
gpu_gas = to_warp_gas_data(gas, device=device)
gpu_environment = to_warp_environment_data(environment, device=device)
scratch, finalized, diagnostics, exhaustion = _buffers(device)
config = NucleationConfig(
rate_law="activation",
coefficient=0.1 / AVOGADRO_NUMBER,
survival_factor=1.0,
precursor_index=0,
molecule_counts=(1,),
formation_diameter=1.0e-9,
precursor_number_concentration_lower=0.0,
precursor_number_concentration_upper=1.0e30,
temperature_lower=200.0,
temperature_upper=400.0,
)
returned_particles, returned_gas = nucleation_step_gpu(
gpu_particles,
gpu_gas,
config,
1.0,
environment=gpu_environment,
scratch=scratch,
finalized_demand=finalized,
diagnostics=diagnostics,
exhaustion_controls=NucleationExhaustionControls(False, False),
exhaustion_buffers=exhaustion,
)
assert returned_particles is gpu_particles and returned_gas is gpu_gas
wp.synchronize()
restored_particles = from_warp_particle_data(gpu_particles, sync=False)
restored_gas = from_warp_gas_data(gpu_gas, name=gas.name, sync=False)
restored_environment = from_warp_environment_data(
gpu_environment,
sync=False,
)
assert restored_particles.masses.shape == (1, 2, 1)
assert restored_gas.concentration.shape == (1, 1)
assert int(np.count_nonzero(restored_particles.concentration)) == 1
assert restored_gas.concentration[0, 0] <= gas.concentration[0, 0]
particle_inventory = np.sum(
restored_particles.masses
* restored_particles.concentration[:, :, None],
axis=1,
)
npt.assert_allclose(
particle_inventory + restored_gas.concentration,
initial_inventory,
rtol=1e-12,
atol=1e-30,
)
return restored_particles, restored_gas, restored_environment
if __name__ == "__main__":
run_example()
print("Direct Warp nucleation example completed on device=cpu.")