Skip to content

Commit 21bea51

Browse files
committed
Enable partial loading with swift snapshots
1 parent 9a0ed0c commit 21bea51

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

src/topsy/__init__.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,19 @@ def parse_args(args=None):
3030
argparser.add_argument("--particle", "-p", help="Specify the particle type to visualise",
3131
default="dm", type=str)
3232
argparser.add_argument("--center", "-c", help="Specify the centering method: 'halo-<N>', 'all', 'zoom' or 'none'",
33-
default="halo-1", type=str)
33+
default="none", type=str)
3434
argparser.add_argument("--quantity", "-q", help="Specify a quantity to render instead of density",
3535
default=None, type=str)
3636
argparser.add_argument("--tile", "-t", help="Wrap and tile the simulation box using its periodicity",
3737
default=False, action="store_true")
3838

39+
argparser.add_argument("--load-sphere", nargs=4, help="Load a sphere of particles with the given "
40+
"radius and centre in simulation units, "
41+
"e.g. --load-sphere 0.2 0.3 0.4 0.5 to load a sphere of "
42+
"particles centre (0.3, 0.4, 0.5), radius 0.2. "
43+
"Supported only for swift simulations",
44+
default=None, type=float)
45+
3946
if args is None:
4047
args = sys.argv[1:]
4148
arg_batches = []
@@ -77,8 +84,13 @@ def main():
7784
logger.info(f"Using test data with {n_part} particles")
7885
loader_args = (n_part,)
7986
else:
87+
import pynbody
8088
loader_class = loader.PynbodyDataLoader
81-
loader_args = (args.filename, args.center, args.particle)
89+
if args.load_sphere is not None:
90+
loader_args = (args.filename, args.center, args.particle,
91+
pynbody.filt.Sphere(args.load_sphere[0], args.load_sphere[1:]))
92+
else:
93+
loader_args = (args.filename, args.center, args.particle)
8294

8395

8496
vis = visualizer.Visualizer(data_loader_class=loader_class,

src/topsy/loader.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import wgpu
33
import logging
44
import pynbody
5-
import pickle
5+
import pickle
6+
7+
from typing import Optional
68

79
from . import config
810
# ABC support:
@@ -126,10 +128,15 @@ def get_filename(self):
126128

127129
class PynbodyDataLoader(PynbodyDataInMemory):
128130
"""Literal data loader for pynbody (starts from just a filename)"""
129-
def __init__(self, device: wgpu.GPUDevice, filename: str, center: str, particle: str):
131+
def __init__(self, device: wgpu.GPUDevice, filename: str, center: str, particle: str,
132+
take_region: Optional[pynbody.filt.Filter] = None):
130133

131134
logger.info(f"Data filename = {filename}, center = {center}, particle = {particle}")
132-
snapshot = pynbody.load(filename)
135+
if take_region is None:
136+
snapshot = pynbody.load(filename)
137+
else:
138+
snapshot = pynbody.load(filename, take_region=take_region)
139+
133140
snapshot.physical_units()
134141
self.filename = filename
135142

src/topsy/sph.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,11 @@ def encode_render_pass(self, command_encoder):
255255

256256
def _get_kernel_at_resolution(self, n_samples):
257257
if self._kernel is None:
258-
self._kernel = pynbody.sph.Kernel2D()
258+
try:
259+
self._kernel = pynbody.sph.Kernel2D()
260+
except AttributeError:
261+
# pynbody v2:
262+
self._kernel = pynbody.sph.kernels.Kernel2D()
259263

260264
# sph kernel is sampled at the centre of the pixels, and the full grid ranges from -2 to 2.
261265
# thus the left hand most pixel is at -2+2/n_samples, and the right hand most pixel is at 2-2/n_samples.

0 commit comments

Comments
 (0)