Skip to content

Commit bac3407

Browse files
jtbuchslayoo
andauthored
spatial sampling: option to limit sampling along X-direction to a given range (#1316)
Co-authored-by: Sylwester Arabas <[email protected]>
1 parent e9e760f commit bac3407

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

PySDM/initialisation/sampling/spatial_sampling.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,36 @@
88

99
class Pseudorandom: # pylint: disable=too-few-public-methods
1010
@staticmethod
11-
def sample(*, backend, grid, n_sd, z_part=None):
12-
dimension = len(grid)
13-
n_elements = dimension * n_sd
11+
def sample(*, backend, grid, n_sd, z_part=None, x_part=None):
12+
n_dims = len(grid)
13+
scale_factors = []
14+
affine_factors = []
1415

15-
storage = backend.Storage.empty(n_elements, dtype=float)
16-
backend.Random(seed=backend.formulae.seed, size=n_elements)(storage)
17-
positions = storage.to_ndarray().reshape(dimension, n_sd)
16+
storage = backend.Storage.empty(n_dims * n_sd, dtype=float)
17+
backend.Random(seed=backend.formulae.seed, size=n_dims * n_sd)(storage)
18+
positions = storage.to_ndarray().reshape(n_dims, n_sd)
1819

1920
if z_part is None:
20-
for dim in range(dimension):
21-
positions[dim, :] *= grid[dim]
21+
scale_factors.append(grid[0])
22+
affine_factors.append(0)
2223
else:
23-
assert dimension == 1
2424
iz_min = int(grid[0] * z_part[0])
2525
iz_max = int(grid[0] * z_part[1])
26-
for dim in range(dimension):
27-
positions[dim, :] *= iz_max - iz_min
28-
positions[dim, :] += iz_min
26+
scale_factors.append(iz_max - iz_min)
27+
affine_factors.append(iz_min)
28+
29+
if x_part is not None:
30+
ix_min = int(grid[1] * x_part[0])
31+
ix_max = int(grid[1] * x_part[1])
32+
scale_factors.append(ix_max - ix_min)
33+
affine_factors.append(ix_min)
34+
else:
35+
if n_dims == 2:
36+
scale_factors.append(grid[1])
37+
affine_factors.append(0)
38+
39+
for dim in range(n_dims):
40+
positions[dim, :] *= scale_factors[dim]
41+
positions[dim, :] += affine_factors[dim]
2942

3043
return positions

tests/unit_tests/initialisation/test_spatial_discretisation.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,33 @@ def test_pseudorandom_zrange(z_range, backend_class):
5555
# assert
5656
np.testing.assert_array_less(positions, comp * z_range[1] * grid[0])
5757
np.testing.assert_array_less(comp * z_range[0] * grid[0], positions)
58+
59+
60+
@pytest.mark.parametrize(
61+
"z_range, x_range",
62+
(
63+
pytest.param((0.0, 1.0), (0.0, 1.0), id="full range"),
64+
pytest.param((0.5, 0.75), (0.5, 0.75), id="partial range"),
65+
),
66+
)
67+
def test_pseudorandom_x_z_range(z_range, x_range, backend_class):
68+
# arrange
69+
assert len(z_range) == 2
70+
assert len(x_range) == 2
71+
backend = backend_class(Formulae())
72+
grid = (8, 8)
73+
n_sd = 100
74+
75+
# act
76+
positions = Pseudorandom.sample(
77+
backend=backend, grid=grid, n_sd=n_sd, z_part=z_range, x_part=x_range
78+
)
79+
80+
for droplet in range(n_sd):
81+
# assert z positions
82+
np.testing.assert_array_less(positions[0][droplet], z_range[1] * grid[0])
83+
np.testing.assert_array_less(z_range[0] * grid[0], positions[0][droplet])
84+
85+
# assert x positions
86+
np.testing.assert_array_less(positions[1][droplet], x_range[1] * grid[1])
87+
np.testing.assert_array_less(x_range[0] * grid[1], positions[1][droplet])

0 commit comments

Comments
 (0)