Coverage for tests/test_pscattering.py: 100%

1 import numpy as np

2 import pytest

3

4 import flexfrac1d.lib.phase_shift as ps

5

6 random_handlers = (

7 ps.UniformScatteringHandler,

8 ps.PerturbationScatteringHandler,

9)

10

11 edge_amplitudes = (

12 np.array(1.5 + 1.8j),

13 np.array([0.11833799 + 0.63103069j, 0.78585931 + 0.84839417j]),

14)

15 c_wavenumbers = (

16 np.array(0.78032224 + 0.85889267j),

17 np.array([0.10716051 + 0.7798643j, 0.54648262 + 0.94570875j]),

18)

19 xfs = (

20 np.array([11.1]),

21 np.array([4.7, 8.3]),

22 np.array([2.2, 6.4, 9.3]),

23)

24

25

26@pytest.mark.parametrize(

27 "edge_amplitudes, c_wavenumbers", zip(edge_amplitudes, c_wavenumbers)

28)

29@pytest.mark.parametrize("xf", xfs)

30 def test_continuity(

31 edge_amplitudes: np.ndarray, c_wavenumbers: np.ndarray, xf: np.ndarray

32):

33 # Verify that when no scattering is used, the right edge of the left floe

34 # is in the same state as the left edge of the right floe. That is, the

35 # complex amplitude advected from the left edge of the left floe is equal to the

36 # amplitude at the left edge of the right floe.

37 post_amplitudes = ps.ContinuousScatteringHandler.compute_edge_amplitudes(

38 edge_amplitudes, c_wavenumbers, xf

39 )

40 for xl, xr, pl, pr in zip(

41 xf[:-1], xf[1:], post_amplitudes[:-1], post_amplitudes[1:]

42 ):

43 assert np.allclose(pl * np.exp(1j * c_wavenumbers * (xr - xl)) - pr, 0)

44

45

46@pytest.mark.parametrize("handler_type", random_handlers)

47@pytest.mark.parametrize(

48 "edge_amplitudes, c_wavenumbers", zip(edge_amplitudes, c_wavenumbers)

49)

50@pytest.mark.parametrize("xf", xfs)

51 def test_absolute_value_continuity(

52 handler_type: type,

53 edge_amplitudes: np.ndarray,

54 c_wavenumbers: np.ndarray,

55 xf: np.ndarray,

56):

57 # Verify that when scattering is used, the amplitude at the right edge of

58 # the left floe is equal, in amplitude, and different, in phase, from the

59 # amplitude at the left edge of the right floe.

60 seed = 3103

61 random_handler: ps.ScatteringHandler = handler_type.from_seed(seed)

62 pa_continuous = ps.ContinuousScatteringHandler.compute_edge_amplitudes(

63 edge_amplitudes, c_wavenumbers, xf

64 )

65 pa_random = random_handler.compute_edge_amplitudes(

66 edge_amplitudes, c_wavenumbers, xf

67 )

68 for pac, par in zip(pa_continuous[1:], pa_random[1:]):

69 assert not np.allclose(pac - par, 0)

70 assert np.allclose(np.abs(pac) - np.abs(par), 0)

71

72

73@pytest.mark.parametrize("handler_type", random_handlers)

74@pytest.mark.parametrize(

75 "edge_amplitudes, c_wavenumbers", zip(edge_amplitudes, c_wavenumbers)

76)

77@pytest.mark.parametrize("xf", xfs)

78 def test_repeatability(handler_type: type, edge_amplitudes, c_wavenumbers, xf):

79 # Verify that two random scattering handlers return the same result when

80 # computing the amplitudes from a generator seeded with the same number.

81 seed = 83

82

83 handler1: ps.ScatteringHandler = handler_type.from_seed(seed)

84 test1 = handler1.compute_edge_amplitudes(edge_amplitudes, c_wavenumbers, xf)

85

86 handler2: ps.ScatteringHandler = handler_type.from_seed(seed)

87 test2 = handler2.compute_edge_amplitudes(edge_amplitudes, c_wavenumbers, xf)

88

89 assert np.allclose(test1 - test2, 0)