Coverage for tests/test_pscattering.py: 0%
35 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-09-11 16:23 +0200
« prev ^ index » next coverage.py v7.9.1, created at 2025-09-11 16:23 +0200
1import typing
3import numpy as np
4import pytest
6import swiift.lib.phase_shift as ps
8random_handlers = (
9 ps.UniformScatteringHandler,
10 ps.PerturbationScatteringHandler,
11)
13edge_amplitudes = (
14 np.array(1.5 + 1.8j),
15 np.array([0.11833799 + 0.63103069j, 0.78585931 + 0.84839417j]),
16)
17c_wavenumbers = (
18 np.array(0.78032224 + 0.85889267j),
19 np.array([0.10716051 + 0.7798643j, 0.54648262 + 0.94570875j]),
20)
21xfs = (
22 np.array([11.1]),
23 np.array([4.7, 8.3]),
24 np.array([2.2, 6.4, 9.3]),
25)
28@pytest.mark.parametrize(
29 "edge_amplitudes, c_wavenumbers", zip(edge_amplitudes, c_wavenumbers)
30)
31@pytest.mark.parametrize("xf", xfs)
32def test_continuity(
33 edge_amplitudes: np.ndarray, c_wavenumbers: np.ndarray, xf: np.ndarray
34):
35 # Verify that when no scattering is used, the right edge of the left floe
36 # is in the same state as the left edge of the right floe. That is, the
37 # complex amplitude advected from the left edge of the left floe is equal to the
38 # amplitude at the left edge of the right floe.
39 post_amplitudes = ps.ContinuousScatteringHandler().compute_edge_amplitudes(
40 edge_amplitudes, c_wavenumbers, xf
41 )
42 for xl, xr, pl, pr in zip(
43 xf[:-1], xf[1:], post_amplitudes[:-1], post_amplitudes[1:]
44 ):
45 assert np.allclose(pl * np.exp(1j * c_wavenumbers * (xr - xl)) - pr, 0)
48@pytest.mark.parametrize("handler_type", random_handlers)
49@pytest.mark.parametrize(
50 "edge_amplitudes, c_wavenumbers", zip(edge_amplitudes, c_wavenumbers)
51)
52@pytest.mark.parametrize("xf", xfs)
53def test_absolute_value_continuity(
54 handler_type: typing.Type[ps._RandomScatteringHandler],
55 edge_amplitudes: np.ndarray,
56 c_wavenumbers: np.ndarray,
57 xf: np.ndarray,
58):
59 # Verify that when scattering is used, the amplitude at the right edge of
60 # the left floe is equal, in amplitude, and different, in phase, from the
61 # amplitude at the left edge of the right floe.
62 seed = 3103
63 random_handler = handler_type.from_seed(seed)
64 pa_continuous = ps.ContinuousScatteringHandler().compute_edge_amplitudes(
65 edge_amplitudes, c_wavenumbers, xf
66 )
67 pa_random = random_handler.compute_edge_amplitudes(
68 edge_amplitudes, c_wavenumbers, xf
69 )
70 for pac, par in zip(pa_continuous[1:], pa_random[1:]):
71 assert not np.allclose(pac - par, 0)
72 assert np.allclose(np.abs(pac) - np.abs(par), 0)
75@pytest.mark.parametrize("random_handler", random_handlers)
76@pytest.mark.parametrize(
77 "edge_amplitudes, c_wavenumbers", zip(edge_amplitudes, c_wavenumbers)
78)
79@pytest.mark.parametrize("xf", xfs)
80def test_repeatability(
81 random_handler: typing.Type[ps._RandomScatteringHandler],
82 edge_amplitudes,
83 c_wavenumbers,
84 xf,
85):
86 # Verify that two random scattering handlers return the same result when
87 # computing the amplitudes from a generator seeded with the same number.
88 seed = 83
90 handler1 = random_handler.from_seed(seed)
91 test1 = handler1.compute_edge_amplitudes(edge_amplitudes, c_wavenumbers, xf)
93 handler2 = random_handler.from_seed(seed)
94 test2 = handler2.compute_edge_amplitudes(edge_amplitudes, c_wavenumbers, xf)
96 assert np.allclose(test1 - test2, 0)