diff --git a/utu/spectrum/_synthesis.py b/utu/spectrum/_synthesis.py index fd0586b..6f32763 100644 --- a/utu/spectrum/_synthesis.py +++ b/utu/spectrum/_synthesis.py @@ -151,9 +151,10 @@ def synthesize( : The spectral radiance of each line in each bin of velocity, along `axis`, `axis_velocity`, and every axis of the other arguments but - `axis_temperature` and `axis_shift`. The inputs are the vertices of - the bins, as - wavelengths with the rest wavelength of each line beside them. + `axis_temperature` and `axis_shift`, in the precision of + `emission_measure`, or single precision if that is narrower. The + inputs are the vertices of the bins, as wavelengths with the rest + wavelength of each line beside them. Raises ------ @@ -190,6 +191,13 @@ def synthesize( `width_nonthermal` varies along `axis`, it is computed once for all of them. + The kernel is computed in double precision, and rounded to the precision + of `emission_measure` before the sum. A matrix product of two precisions + would copy the emission measure into the wider one, so a map in single + precision, as the emission measure of a simulation often is, is summed in + single precision instead, and takes no more memory than itself, the + kernel, and the result. + Examples -------- The :math:`\mathrm{Ne\,VII}\;465\,\AA` line of plasma near half a million @@ -388,6 +396,13 @@ def synthesize( unit_kernel = unit / na.unit_normalized(emission_measure) emission_measure = emission_measure.value + # The kernel is computed in double precision, and rounded to the + # precision of the emission measure before the sum. A matrix product of + # two precisions copies the emission measure into the wider one, so a + # map of single precision, as a simulation often is, would otherwise be + # copied whole into double precision, twice its own size. + dtype = np.result_type(na.get_dtype(emission_measure), np.float32) + # The kernel varies along every axis the contribution function and the # profile do, which are axes of the map only where one of them varies # from pixel to pixel. If the kernel of every line in every bin of @@ -465,6 +480,7 @@ def synthesize( # axis of the kernel, is multiplied in. factor = g[index] / width / (4 * np.pi * u.sr) kernel = factor.to(unit_kernel).value * fraction + kernel = kernel.astype(dtype, copy=False) radiance = na.vecdot( kernel, @@ -483,7 +499,7 @@ def synthesize( # no lines, or no bins of velocity if result is None: result = na.ScalarArray( - ndarray=np.zeros(tuple(shape.values())) << unit, + ndarray=np.zeros(tuple(shape.values()), dtype=dtype) << unit, axes=tuple(shape), ) diff --git a/utu/spectrum/_tests/test_synthesis.py b/utu/spectrum/_tests/test_synthesis.py index 84ff87c..9551703 100644 --- a/utu/spectrum/_tests/test_synthesis.py +++ b/utu/spectrum/_tests/test_synthesis.py @@ -480,6 +480,70 @@ def test_synthesize_blocks( assert np.allclose(result.outputs, expected.outputs, rtol=1e-12) +@pytest.mark.parametrize( + argnames="em", + argvalues=[_em_map, _em_map_uncertain], + ids=["scalar", "uncertain"], +) +@pytest.mark.parametrize( + argnames="size_block", + argvalues=[ + None, + # Both lines in seven bins at a time, as above. + (6 * 3 + 3) * 2 * 7, + ], + ids=["whole", "blocks"], +) +def test_synthesize_precision( + monkeypatch: pytest.MonkeyPatch, + em: na.AbstractScalar, + size_block: None | int, +) -> None: + """ + An emission measure in single precision is summed in single precision. + + Its kernel is rounded to single precision, so that the matrix product + does not copy the emission measure into double precision, which would + give a spectrum in double precision. The spectrum agrees with the one + summed in double precision to within the rounding of single precision. + + The bins span no more than a few thermal widths of the hottest plasma, + so that every bin holds far more of each line than the least number + single precision can hold. + """ + shift = na.ScalarArray([-20, 0, 30] * u.km / u.s, axes="x") + v = na.linspace(-100, 100, axis="velocity", num=41) * u.km / u.s + + if size_block is not None: + monkeypatch.setattr(utu.spectrum._synthesis, "_size_block", size_block) + + em_single = em.astype(np.float32) + em_double = em_single.astype(np.float64) + + result = _synthesize(emission_measure=em_single, shift=shift, velocity=v) + expected = _synthesize(emission_measure=em_double, shift=shift, velocity=v) + + assert type(result.outputs) is type(expected.outputs) + assert result.outputs.dtype == np.float32 + assert expected.outputs.dtype == np.float64 + assert na.shape(result.outputs) == dict(line=2, velocity=40, x=3) + assert np.allclose(result.outputs, expected.outputs, rtol=1e-6) + + +def test_synthesize_precision_empty() -> None: + """No lines from an emission measure in single precision are single too.""" + index = {"line": slice(0, 0)} + + result = _synthesize( + contribution=contribution[index], + mass=mass[index], + emission_measure=emission_measure.astype(np.float32), + ) + + assert na.shape(result.outputs) == dict(line=0, velocity=800) + assert result.outputs.dtype == np.float32 + + @pytest.mark.parametrize( argnames="size_block", argvalues=[