Skip to content

Commit 3400f96

Browse files
committed
13 02 part 2
1 parent de3b2c3 commit 3400f96

2 files changed

Lines changed: 95 additions & 17 deletions

File tree

PySDM/backends/impl_jax/methods/moments_methods.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,14 @@ def body(
120120
attr_data,
121121
cell_id,
122122
idx,
123-
length,
124123
rank,
125-
x_bins,
126-
x_attr,
127124
weighting_attribute,
128125
weighting_rank,
129126
bin_to_count,
130127
idx_i
131128
):
132129

133130
i = idx[idx_i]
134-
135131
moment_0 = moment_0.at[bin_to_count, cell_id[i]].add(multiplicity[i] * weighting_attribute[i] ** weighting_rank)
136132
moments = moments.at[bin_to_count, cell_id[i]].add(multiplicity[i] * weighting_attribute[i] ** weighting_rank * attr_data[i] ** rank)
137133

@@ -170,34 +166,35 @@ def cond_fun(k):
170166
return bin_to_calculate
171167
# TODO: what happens if k == x_bins.shape[0] - 1
172168

173-
174-
moment_0.data = moment_0.data.at[:, :].set(0)
175-
moments.data = moments.data.at[:,:].set(0)
169+
new_moment_0 = jax.numpy.zeros((moment_0.shape[0]+1, moment_0.shape[1]))
170+
new_moments = jax.numpy.zeros((moment_0.shape[0]+1, moment_0.shape[1]))
176171
idx_idxs = jax.numpy.arange(length)
177172

178173
count_bins_func = jax.vmap(spectrum_moments_helper, (None, None, None, 0))
179174
bins_to_count = count_bins_func(x_bins.data, x_attr.data, idx.data, idx_idxs)
180-
mapped_spectrum = jax.vmap(self._spectrum_moments_body, (None, None, None, None, None, None, None, None, None, None, None, None, 0, 0))
181-
182-
moment_0.data, moments.data = mapped_spectrum(
183-
moment_0.data,
184-
moments.data,
175+
# TODO: bins_to_count > len()-1 is not handled
176+
print(bins_to_count)
177+
assert all(bins_to_count < new_moments.shape[0])
178+
mapped_spectrum = jax.vmap(self._spectrum_moments_body, (None, None, None, None, None, None, None, None, None, 0, 0))
179+
180+
new_moment_0, new_moments = mapped_spectrum(
181+
new_moment_0,
182+
new_moments,
185183
multiplicity.data,
186184
attr_data.data,
187185
cell_id.data,
188186
idx.data,
189-
length,
190187
rank,
191-
x_bins.data,
192-
x_attr.data,
193188
weighting_attribute.data,
194189
weighting_rank,
195190
bins_to_count,
196191
idx_idxs
197192
)
198193

199-
moments.data = moments.data.sum(0)
200-
moment_0.data = moment_0.data.sum(0)
194+
# moments.data = new_moments.sum(0)
195+
# moment_0.data = new_moment_0.sum(0)
196+
moments.data = new_moments.at[:-1, :].sum(0)
197+
moment_0.data = new_moment_0.at[:-1, :].sum(0)
201198

202199
if not skip_division_by_m0:
203200
moments.data = jax.numpy.where(moment_0.data != 0, moments.data / moment_0.data, 0.0)

tests/unit_tests/impl/test_moments.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,84 @@ def test_spectrum_moment_0d(backend_class_with_jax, skip_division_by_m0):
135135
actual=sum(actual if skip_division_by_m0 else actual * spectrum_moment_0.to_ndarray()),
136136
significant=4,
137137
)
138+
139+
140+
@staticmethod
141+
@pytest.mark.parametrize('skip_division_by_m0', (True, False))
142+
def test_spectrum_moment_0d_one_drop_outside_of_bins(backend_class_with_jax, skip_division_by_m0):
143+
# Arrange
144+
particulator = DummyParticulator(backend_class_with_jax, n_sd=1)
145+
attribute = {
146+
"multiplicity": np.ones(1),
147+
"volume": np.ones(1),
148+
}
149+
particulator.build(attribute)
150+
151+
v_bins = np.linspace(0, .5, num=2, endpoint=True)
152+
153+
assert len(v_bins) == 2
154+
spectrum_moment_0 = particulator.backend.Storage.empty(
155+
(len(v_bins) - 1, 1), dtype=float
156+
)
157+
spectrum_moments = particulator.backend.Storage.empty(
158+
(len(v_bins) - 1, 1), dtype=float
159+
)
160+
v_bins_edges = particulator.backend.Storage.from_ndarray(v_bins)
161+
162+
# Act
163+
particulator.spectrum_moments(
164+
moment_0=spectrum_moment_0,
165+
moments=spectrum_moments,
166+
attr="volume",
167+
attr_name='volume',
168+
rank=1,
169+
attr_bins=v_bins_edges,
170+
skip_division_by_m0=skip_division_by_m0,
171+
)
172+
173+
# Assert
174+
assert spectrum_moment_0.to_ndarray() == [0]
175+
assert spectrum_moments.to_ndarray() == [0]
176+
177+
@staticmethod
178+
def test_moment_1d(backend_class_with_jax):
179+
# Arrange
180+
grid=(2,)
181+
particulator = DummyParticulator(
182+
backend_class_with_jax,
183+
n_sd=1,
184+
grid=grid
185+
)
186+
187+
attribute = {
188+
"multiplicity": np.ones(1),
189+
"volume": np.ones(1),
190+
"cell id": np.ones(1, dtype=int),
191+
}
192+
particulator.build(attribute)
193+
194+
v_bins = np.linspace(0, 2, num=2, endpoint=True)
195+
196+
assert len(v_bins) == 2
197+
spectrum_moment_0 = particulator.backend.Storage.empty(
198+
(len(v_bins) - 1, np.prod(grid)), dtype=float
199+
)
200+
spectrum_moments = particulator.backend.Storage.empty(
201+
(len(v_bins) - 1, np.prod(grid)), dtype=float
202+
)
203+
v_bins_edges = particulator.backend.Storage.from_ndarray(v_bins)
204+
205+
# Act
206+
particulator.spectrum_moments(
207+
moment_0=spectrum_moment_0,
208+
moments=spectrum_moments,
209+
attr="volume",
210+
attr_name='volume',
211+
rank=1,
212+
attr_bins=v_bins_edges,
213+
skip_division_by_m0=True,
214+
)
215+
216+
# Assert
217+
assert np.prod(spectrum_moment_0.to_ndarray() == [0, 1])
218+
assert np.prod(spectrum_moments.to_ndarray() == [0, 1])

0 commit comments

Comments
 (0)