From 459230e8c0aec788a0b460bbaeda620ee0669957 Mon Sep 17 00:00:00 2001 From: Ben Keller Date: Sun, 1 Feb 2026 22:33:25 -0600 Subject: [PATCH] Converting to physical units before building the KDTree can cause spurious, roundoff-caused problems with periodic boxes. In a snapshot where the boxsize was 1.0, and two or more particles are at -0.5 and 0.5 in code units, these same particles may fail pynbody's boundary check in smCheckFits, and calculations will end up falling back to assuming no periodicity. --- tangos/input_handlers/pynbody.py | 1 + tangos/parallel_tasks/pynbody_server/snapshot_queue.py | 1 + 2 files changed, 2 insertions(+) diff --git a/tangos/input_handlers/pynbody.py b/tangos/input_handlers/pynbody.py index 8bdaf23e..3508b207 100644 --- a/tangos/input_handlers/pynbody.py +++ b/tangos/input_handlers/pynbody.py @@ -98,6 +98,7 @@ def load_timestep_without_caching(self, ts_extension, mode=None) -> pynbody.snap raise NotImplementedError("Load mode %r is not implemented"%mode) def _build_kdtree(self, timestep, mode): + timestep.wrap() # Because we have converted pos to kpc, FP roundoff may place particles at the boundaries outside the period of the box. timestep.build_tree() def load_region(self, ts_extension, region_specification, mode=None, expected_number_of_queries=None) -> pynbody.snapshot.simsnap.SimSnap: diff --git a/tangos/parallel_tasks/pynbody_server/snapshot_queue.py b/tangos/parallel_tasks/pynbody_server/snapshot_queue.py index 9982d1c5..b4fdf46a 100644 --- a/tangos/parallel_tasks/pynbody_server/snapshot_queue.py +++ b/tangos/parallel_tasks/pynbody_server/snapshot_queue.py @@ -110,6 +110,7 @@ def build_tree(self): num_threads = multiprocessing.cpu_count() else: num_threads = None + self.current_snapshot.wrap() # Because we have converted pos to kpc, FP roundoff may place particles at the boundaries outside the period of the box. self.current_snapshot.build_tree(num_threads=num_threads, shared_mem=self.current_shared_mem_flag)