Skip to content

Commit 8f09b38

Browse files
authored
Merge pull request #23 from SWIFTSIM/cosmo_quantity
Changes for swiftsimio v10 support.
2 parents 72ac01b + a04677e commit 8f09b38

File tree

6 files changed

+191
-153
lines changed

6 files changed

+191
-153
lines changed

swiftgalaxy/halo_catalogues.py

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,7 @@ def _get_user_spatial_mask(self, snapshot_filename: str) -> SWIFTMask:
215215
if region is not None:
216216
for ax in range(3):
217217
region[ax] = (
218-
[
219-
self.centre[ax] + region[ax][0],
220-
self.centre[ax] + region[ax][1],
221-
]
218+
[self.centre[ax] + region[ax][0], self.centre[ax] + region[ax][1]]
222219
if region[ax] is not None
223220
else None
224221
)
@@ -659,10 +656,7 @@ def _load(self) -> None:
659656
sm.constrain_indices(self._soap_index)
660657
else:
661658
sm.constrain_index(self._soap_index)
662-
self._catalogue = SWIFTDataset(
663-
self.soap_file,
664-
mask=sm,
665-
)
659+
self._catalogue = SWIFTDataset(self.soap_file, mask=sm)
666660

667661
@property
668662
def soap_index(self) -> Union[int, List[int]]:
@@ -1303,7 +1297,14 @@ def centre(self) -> cosmo_array:
13031297
if self.centre_type in ("_gas", "_stars"):
13041298
# {XYZ}c_gas and {XYZ}c_stars are relative to {XYZ}c
13051299
relative_to = np.hstack(
1306-
[getattr(self._catalogue.positions, "{:s}c".format(c)) for c in "xyz"]
1300+
[
1301+
cosmo_array(
1302+
getattr(self._catalogue.positions, "{:s}c".format(c)),
1303+
comoving=self._catalogue.units.comoving,
1304+
cosmo_factor=cosmo_factor(a**1, self._catalogue.units.a),
1305+
)
1306+
for c in "xyz"
1307+
]
13071308
).T
13081309
else:
13091310
# {XYZ}cmbp, {XYZ}cminpot and {XYZ}c are absolute
@@ -1319,9 +1320,13 @@ def centre(self) -> cosmo_array:
13191320
relative_to
13201321
+ np.hstack(
13211322
[
1322-
getattr(
1323-
self._catalogue.positions,
1324-
"{:s}c{:s}".format(c, self.centre_type),
1323+
cosmo_array(
1324+
getattr(
1325+
self._catalogue.positions,
1326+
"{:s}c{:s}".format(c, self.centre_type),
1327+
),
1328+
comoving=self._catalogue.units.comoving,
1329+
cosmo_factor=cosmo_factor(a**1, self._catalogue.units.a),
13251330
)
13261331
for c in "xyz"
13271332
]
@@ -1356,7 +1361,14 @@ def velocity_centre(self) -> cosmo_array:
13561361
if self.centre_type in ("_gas", "_stars"):
13571362
# V{XYZ}c_gas and V{XYZ}c_stars are relative to {XYZ}c
13581363
relative_to = np.hstack(
1359-
[getattr(self._catalogue.velocities, "v{:s}c".format(c)) for c in "xyz"]
1364+
[
1365+
cosmo_array(
1366+
getattr(self._catalogue.velocities, "v{:s}c".format(c)),
1367+
comoving=self._catalogue.units.comoving,
1368+
cosmo_factor=cosmo_factor(a**0, self._catalogue.units.a),
1369+
)
1370+
for c in "xyz"
1371+
]
13601372
).T
13611373
else:
13621374
# V{XYZ}cmbp, V{XYZ}cminpot and V{XYZ}c are absolute
@@ -1371,9 +1383,13 @@ def velocity_centre(self) -> cosmo_array:
13711383
relative_to
13721384
+ np.hstack(
13731385
[
1374-
getattr(
1375-
self._catalogue.velocities,
1376-
"v{:s}c{:s}".format(c, self.centre_type),
1386+
cosmo_array(
1387+
getattr(
1388+
self._catalogue.velocities,
1389+
"v{:s}c{:s}".format(c, self.centre_type),
1390+
),
1391+
comoving=self._catalogue.units.comoving,
1392+
cosmo_factor=cosmo_factor(a**0, self._catalogue.units.a),
13771393
)
13781394
for c in "xyz"
13791395
]
@@ -1641,8 +1657,7 @@ def _generate_bound_only_mask(self, sg: "SWIFTGalaxy") -> MaskCollection:
16411657
"""
16421658

16431659
def in_one_of_ranges(
1644-
ints: NDArray[np.int_],
1645-
int_ranges: NDArray[np.int_],
1660+
ints: NDArray[np.int_], int_ranges: NDArray[np.int_]
16461661
) -> NDArray[np.bool_]:
16471662
"""
16481663
Produces a boolean mask corresponding to `ints`.

swiftgalaxy/iterator.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,10 @@ def _eval_dense_optimized_solution(self):
373373
# grid should be at least 1 cell in size so that we efficiently group targets
374374
# in the same grid location
375375
grid_element_dim = (
376-
np.max(
377-
target_sizes // sm.cell_size[np.newaxis] + 1,
378-
axis=0,
379-
)
376+
np.max(target_sizes // sm.cell_size[np.newaxis], axis=0)
380377
.to_value(u.dimensionless)
381378
.astype(int)
379+
+ 1
382380
)
383381
# Cells are not guaranteed to have a vertex at box coordinate (0, 0, 0)
384382
# but usually are. At least one of our grids is misaligned with the cells
@@ -393,16 +391,18 @@ def _eval_dense_optimized_solution(self):
393391
cells_dim % grid_element_dim > 0
394392
).astype(int)
395393
target_grid_offsets_aligned, target_grid_indices_aligned = np.modf(
396-
target_centres / (grid_element_dim * sm.cell_size)
394+
(target_centres / (grid_element_dim * sm.cell_size)).to_value(
395+
u.dimensionless
396+
)
397397
)
398398
target_grid_offsets_offset, target_grid_indices_offset = np.modf(
399-
(target_centres + 0.5 * grid_element_dim * sm.cell_size)
400-
/ (grid_element_dim * sm.cell_size)
399+
(
400+
(target_centres + 0.5 * grid_element_dim * sm.cell_size)
401+
/ (grid_element_dim * sm.cell_size)
402+
).to_value(u.dimensionless)
401403
- 1
402404
)
403-
target_grid_indices_aligned = target_grid_indices_aligned.to_value(
404-
u.dimensionless
405-
).astype(int)
405+
target_grid_indices_aligned = target_grid_indices_aligned.astype(int)
406406
# need a "box wrap" of targets to the far side of the grid for the offset grid:
407407
target_grid_indices_offset = np.where(
408408
np.logical_and(
@@ -411,9 +411,7 @@ def _eval_dense_optimized_solution(self):
411411
grid_dim - 1,
412412
target_grid_indices_offset,
413413
) # complains about missing cosmo_array info
414-
target_grid_indices_offset = target_grid_indices_offset.to_value(
415-
u.dimensionless
416-
).astype(int)
414+
target_grid_indices_offset = target_grid_indices_offset.astype(int)
417415
target_grid_distances_aligned = np.sqrt(
418416
np.sum(
419417
np.power(
@@ -603,8 +601,7 @@ def __iter__(self) -> Generator:
603601
boost=False,
604602
)
605603
swift_galaxy._transform(
606-
self.coordinate_frame_from._velocity_like_transform,
607-
boost=True,
604+
self.coordinate_frame_from._velocity_like_transform, boost=True
608605
)
609606
elif self.auto_recentre:
610607
swift_galaxy.recentre(self.halo_catalogue.centre)
@@ -757,8 +754,6 @@ def dm_median_position(
757754
kwargs = [dict()] * len(self.iteration_order)
758755
for sg, iteration_location in zip(self, self.iteration_order):
759756
result[sg.halo_catalogue._multi_galaxy_catalogue_mask] = func(
760-
sg,
761-
*args[iteration_location],
762-
**kwargs[iteration_location],
757+
sg, *args[iteration_location], **kwargs[iteration_location]
763758
)
764759
return result

swiftgalaxy/reader.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ def _apply_box_wrap(coords: cosmo_array, boxsize: Optional[cosmo_array]) -> cosm
5555
out : :class:`~swiftsimio.objects.cosmo_array`
5656
The coordinates wrapped to lie within the box dimensions.
5757
"""
58-
if boxsize is None:
59-
return coords
60-
retval = (coords + boxsize / 2.0) % boxsize - boxsize / 2.0
61-
return retval
58+
return (
59+
(coords + boxsize / 2.0) % boxsize - boxsize / 2.0
60+
if boxsize is not None
61+
else coords
62+
)
6263

6364

6465
def _apply_translation(coords: cosmo_array, offset: cosmo_array) -> cosmo_array:
@@ -1573,9 +1574,7 @@ def __init__(
15731574
getattr(self.metadata, f"{particle_name}_properties").group
15741575
]
15751576
TypeDatasetHelper = type(
1576-
f"{nice_name}DatasetHelper",
1577-
(_SWIFTGroupDatasetHelper, object),
1578-
dict(),
1577+
f"{nice_name}DatasetHelper", (_SWIFTGroupDatasetHelper, object), dict()
15791578
)
15801579
self._particle_dataset_helpers[particle_name] = TypeDatasetHelper(
15811580
super().__getattribute__(particle_name), self

tests/test_coordinate_transformations.py

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
import numpy as np
33
import unyt as u
44
from unyt.testing import assert_allclose_units
5-
from swiftsimio.objects import cosmo_array, cosmo_factor, a
5+
from swiftsimio.objects import cosmo_array, cosmo_factor, a, cosmo_quantity
66
from scipy.spatial.transform import Rotation
77
from toysnap import present_particle_types, toysnap_filename, ToyHF
88
from swiftgalaxy import SWIFTGalaxy
99

1010
reltol = 1.01 # allow some wiggle room for floating point roundoff
11-
abstol_c = 10 * u.pc # less than this is ~0
12-
abstol_v = 10 * u.m / u.s # less than this is ~0
11+
abstol_c = cosmo_quantity(
12+
10, u.pc, comoving=True, cosmo_factor=cosmo_factor(a**1, 1.0)
13+
) # less than this is ~0
14+
abstol_v = cosmo_quantity(
15+
10, u.m / u.s, comoving=True, cosmo_factor=cosmo_factor(a**0, 1.0)
16+
) # less than this is ~0
1317

1418
expected_xy = {
1519
"gas": cosmo_array(
@@ -227,8 +231,24 @@ def test_auto_recentering_off(
227231
Positions should still be offcentre.
228232
"""
229233
xyz = getattr(sg_autorecentre_off, particle_name).coordinates
230-
assert (np.abs(xyz[:, :2] - 2 * u.Mpc) <= expected_xy).all()
231-
assert (np.abs(xyz[:, 2] - 2 * u.Mpc) <= expected_z).all()
234+
assert (
235+
np.abs(
236+
xyz[:, :2]
237+
- cosmo_quantity(
238+
2, u.Mpc, comoving=True, cosmo_factor=cosmo_factor(a**1, 1.0)
239+
)
240+
)
241+
<= expected_xy
242+
).all()
243+
assert (
244+
np.abs(
245+
xyz[:, 2]
246+
- cosmo_quantity(
247+
2, u.Mpc, comoving=True, cosmo_factor=cosmo_factor(a**1, 1.0)
248+
)
249+
)
250+
<= expected_z
251+
).all()
232252

233253
@pytest.mark.parametrize(
234254
"particle_name, expected_vxy, expected_vz",
@@ -241,8 +261,30 @@ def test_auto_recentering_off_velocity(
241261
Velocities should still be offcentre.
242262
"""
243263
vxyz = getattr(sg_autorecentre_off, particle_name).velocities
244-
assert (np.abs(vxyz[:, :2] - 200 * u.km / u.s) <= expected_vxy).all()
245-
assert (np.abs(vxyz[:, 2] - 200 * u.km / u.s) <= expected_vz).all()
264+
assert (
265+
np.abs(
266+
vxyz[:, :2]
267+
- cosmo_quantity(
268+
200,
269+
u.km / u.s,
270+
comoving=True,
271+
cosmo_factor=cosmo_factor(a**0, 1.0),
272+
)
273+
)
274+
<= expected_vxy
275+
).all()
276+
assert (
277+
np.abs(
278+
vxyz[:, 2]
279+
- cosmo_quantity(
280+
200,
281+
u.km / u.s,
282+
comoving=True,
283+
cosmo_factor=cosmo_factor(a**0, 1.0),
284+
)
285+
)
286+
<= expected_vz
287+
).all()
246288

247289

248290
class TestManualCoordinateTransformations:

0 commit comments

Comments
 (0)