diff --git a/particles.py b/particles.py index d14918e..d2ee8a3 100644 --- a/particles.py +++ b/particles.py @@ -39,7 +39,8 @@ class Observation: attrs: Tensor # (N, d) coords: Tensor # (N, 2) ids: list[int] - target: Tensor # (N, 2): coords[t+1] - coords[t], wrapped + target: Tensor # (N, 2): raw per-step displacement; equals the wrapped + # coords[t+1] - coords[t] as long as the step() assert holds switched: bool # True on the first step of a new regime regime: int @@ -106,6 +107,11 @@ class ParticleWorld: self.until = self._next_switch(self.t) switched = True delta = self._delta() + # target is the raw delta, so it only matches the wrapped coordinate update while a + # particle moves less than half the torus per step. Default config peaks near 0.06; + # a config that trips this (larger n, dt, or sigma) is physically meaningless, not + # something to silently wrap away. + assert delta.abs().max() < 0.5, "per-step displacement exceeds half the torus" obs = Observation(attrs=self._attrs(switched), coords=self.coords.clone(), ids=list(self.ids), target=delta, switched=switched, regime=self.regime) self.coords = (self.coords + delta) % 1.0 diff --git a/tests/test_particles.py b/tests/test_particles.py index 6958ad0..c6af7ee 100644 --- a/tests/test_particles.py +++ b/tests/test_particles.py @@ -72,6 +72,17 @@ def test_target_is_next_delta(): assert torch.allclose(expected, o1.target, atol=1e-6) +def test_target_matches_wrapped_coordinate_update(): + w = ParticleWorld(ParticleConfig(seed=11)) + prev = w.step() + for _ in range(300): + cur = w.step() + d = cur.coords - prev.coords + wrapped = d - torch.floor(d + 0.5) + assert torch.allclose(prev.target, wrapped, atol=1e-6) + prev = cur + + def test_zero_regime_and_no_noise_is_static(): cfg = ParticleConfig(seed=0, noise=0.0) w = ParticleWorld(cfg)