|
| 1 | +"""Tests for helicon.postprocess.moments — moment computation from particle data.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pytest |
| 7 | + |
| 8 | +from helicon.postprocess.moments import MomentData |
| 9 | + |
| 10 | + |
| 11 | +class TestMomentDataclass: |
| 12 | + def test_fields_accessible(self): |
| 13 | + nr, nz = 8, 16 |
| 14 | + m = MomentData( |
| 15 | + species="D_plus", |
| 16 | + density=np.zeros((nr, nz)), |
| 17 | + vr_mean=np.zeros((nr, nz)), |
| 18 | + vz_mean=np.zeros((nr, nz)), |
| 19 | + pressure_rr=np.zeros((nr, nz)), |
| 20 | + pressure_zz=np.zeros((nr, nz)), |
| 21 | + r_grid=np.linspace(0, 0.1, nr), |
| 22 | + z_grid=np.linspace(-0.3, 1.0, nz), |
| 23 | + ) |
| 24 | + assert m.species == "D_plus" |
| 25 | + assert m.density.shape == (nr, nz) |
| 26 | + assert m.vr_mean.shape == (nr, nz) |
| 27 | + assert m.vz_mean.shape == (nr, nz) |
| 28 | + assert m.pressure_rr.shape == (nr, nz) |
| 29 | + assert m.pressure_zz.shape == (nr, nz) |
| 30 | + assert m.r_grid.shape == (nr,) |
| 31 | + assert m.z_grid.shape == (nz,) |
| 32 | + |
| 33 | + def test_with_nonzero_values(self): |
| 34 | + nr, nz = 4, 8 |
| 35 | + density = np.random.default_rng(42).random((nr, nz)) * 1e18 |
| 36 | + m = MomentData( |
| 37 | + species="He_2plus", |
| 38 | + density=density, |
| 39 | + vr_mean=np.zeros((nr, nz)), |
| 40 | + vz_mean=np.ones((nr, nz)) * 50000.0, |
| 41 | + pressure_rr=np.zeros((nr, nz)), |
| 42 | + pressure_zz=np.zeros((nr, nz)), |
| 43 | + r_grid=np.linspace(0, 0.2, nr), |
| 44 | + z_grid=np.linspace(0.0, 2.0, nz), |
| 45 | + ) |
| 46 | + assert np.all(m.density >= 0) |
| 47 | + assert np.allclose(m.vz_mean, 50000.0) |
| 48 | + |
| 49 | + |
| 50 | +class TestBinIndexNumpyPath: |
| 51 | + """Test the numpy bin-index logic that underpins moment binning.""" |
| 52 | + |
| 53 | + def _bin_indices(self, pos, edges, n_bins): |
| 54 | + """Replicate the numpy path from compute_moments.""" |
| 55 | + return np.clip(np.searchsorted(edges, pos) - 1, 0, n_bins - 1) |
| 56 | + |
| 57 | + def test_center_of_first_bin(self): |
| 58 | + edges = np.linspace(0.0, 1.0, 11) # 10 bins |
| 59 | + pos = np.array([0.05]) # midpoint of first bin |
| 60 | + idx = self._bin_indices(pos, edges, 10) |
| 61 | + assert idx[0] == 0 |
| 62 | + |
| 63 | + def test_center_of_last_bin(self): |
| 64 | + edges = np.linspace(0.0, 1.0, 11) |
| 65 | + pos = np.array([0.95]) # midpoint of last bin |
| 66 | + idx = self._bin_indices(pos, edges, 10) |
| 67 | + assert idx[0] == 9 |
| 68 | + |
| 69 | + def test_below_minimum_clips_to_zero(self): |
| 70 | + edges = np.linspace(0.0, 1.0, 11) |
| 71 | + pos = np.array([-0.5]) |
| 72 | + idx = self._bin_indices(pos, edges, 10) |
| 73 | + assert idx[0] == 0 |
| 74 | + |
| 75 | + def test_above_maximum_clips_to_last(self): |
| 76 | + edges = np.linspace(0.0, 1.0, 11) |
| 77 | + pos = np.array([2.0]) |
| 78 | + idx = self._bin_indices(pos, edges, 10) |
| 79 | + assert idx[0] == 9 |
| 80 | + |
| 81 | + def test_all_particles_binned_in_range(self): |
| 82 | + rng = np.random.default_rng(0) |
| 83 | + n = 1000 |
| 84 | + edges = np.linspace(0.0, 5.0, 101) # 100 bins |
| 85 | + pos = rng.uniform(0.0, 5.0, size=n) |
| 86 | + idx = self._bin_indices(pos, edges, 100) |
| 87 | + assert np.all(idx >= 0) |
| 88 | + assert np.all(idx < 100) |
| 89 | + |
| 90 | + |
| 91 | +class TestComputeMomentsErrors: |
| 92 | + def test_raises_on_missing_h5_files(self, tmp_path): |
| 93 | + """compute_moments raises FileNotFoundError if no HDF5 files present.""" |
| 94 | + from helicon.postprocess.moments import compute_moments |
| 95 | + |
| 96 | + with pytest.raises(FileNotFoundError, match="No HDF5 files"): |
| 97 | + compute_moments(tmp_path) |
0 commit comments