Coverage for tests/test_param_spectra.py: 100%

1 from hypothesis import given, strategies as st (empty)

2 import numpy as np (empty)

3 import pytest (empty)

4 import scipy.integrate as integrate (empty)

5

6 import swiift.api.spectra as spectra (empty)

7 from swiift.lib.constants import PI_2 (empty)

8 from tests.physical_strategies import PHYSICAL_STRATEGIES (empty)

9

10 _freqencies = ( (empty)

11 np.array([1, 2]),

12 np.array([0.5, 1.2]),

13 np.linspace(0.3, 0.7, 10),

14 0.3,

15)

16

17 generic_spectra = ( (empty)

18 spectra.PiersonMoskowitz.from_peak_frequency(0.2, 9.8),

19 spectra.Bretschneider.from_peak_frequency_swh(0.18, 2.1),

20 spectra.JONSWAP.from_parameters(0.21, 3.3, 9.8),

21)

22

23

24@pytest.mark.parametrize("frequencies", _freqencies) (empty)

25@pytest.mark.parametrize("spectrum", generic_spectra) (empty)

26 def test_angular_density(frequencies, spectrum: spectra._ParametrisedSpectrum): (empty)

27 density_from_frequency = spectrum.density(frequencies) 1 ctx 1d

28 angular_frequencies = PI_2 * frequencies 1 ctx 1d

29 density_from_ang_frequency = spectrum._density_ang(angular_frequencies) 1 ctx 1d

30

31 # The density should be equal up to the scaling factor 2pi.

32 assert np.allclose(density_from_frequency, density_from_ang_frequency * PI_2) 1 ctx 1d

33

34 # Integrating the densities wrt their arguments

35 # should yield identical results.

36 if len(np.atleast_1d(frequencies)) > 1: 1 ctx 1d

37 assert np.isclose( 1 ctx 1d

38 spectrum.discrete_energy(frequencies),

39 np.trapezoid(density_from_ang_frequency, angular_frequencies),

40 )

41

42

43@pytest.mark.parametrize("spectrum", generic_spectra) (empty)

44 def test_total_energy(spectrum): (empty)

45 res, _ = integrate.quad(spectrum.density, 0, np.inf) 1 ctx 1c

46 swh_num = 4 * np.sqrt(res) 1 ctx 1c

47 target = spectrum.swh 1 ctx 1c

48

49 if isinstance(spectrum, spectra.JONSWAP): 1 ctx 1c

50 # Twice the greatest absolute error from the ITTC polynomial

51 # approximation, with respect to the density numerically

52 # integrated "in-house".

53 jonswap_atol = 9e-3 1 ctx 1c

54 assert np.isclose(swh_num, target, atol=jonswap_atol) 1 ctx 1c

55 else:

56 assert np.isclose(swh_num, target) 1 ctx 1c

57

58

59@pytest.mark.parametrize("spectrum", generic_spectra) (empty)

60 def test_properties(spectrum): (empty)

61 fp = spectrum.peak_frequency 1 ctx 1e

62

63 assert np.isclose(spectrum.peak_period, 1 / fp) 1 ctx 1e

64 assert np.isclose(spectrum.peak_ang_frequency, PI_2 * fp) 1 ctx 1e

65

66

67 class TestPiersonMoskowitz: (empty)

68 @staticmethod (empty)

69 @given( 2 ctx 1ab

70 swh=PHYSICAL_STRATEGIES[("wave", "amplitude")],

71 gravity=PHYSICAL_STRATEGIES[("gravity",)],

72 )

73 def test_recover_swh(swh: float, gravity: float): (empty)

74 spectrum = spectra.PiersonMoskowitz.from_swh(swh, gravity) 1 ctx 1b

75 assert np.isclose(spectrum.swh, swh) 1 ctx 1b

76

77 @staticmethod (empty)

78 @given( 2 ctx 1ab

79 peak_frequency=PHYSICAL_STRATEGIES[("wave", "frequency")],

80 gravity=PHYSICAL_STRATEGIES[("gravity",)],

81 )

82 def test_recover_peak_frequency(peak_frequency: float, gravity: float): (empty)

83 spectrum = spectra.PiersonMoskowitz.from_peak_frequency(peak_frequency, gravity) 1 ctx 1b

84 assert np.isclose(spectrum.peak_frequency, peak_frequency) 1 ctx 1b

85

86

87 class TestBretschneider: (empty)

88 @staticmethod (empty)

89 @given( 2 ctx 1ab

90 peak_frequency=PHYSICAL_STRATEGIES[("wave", "frequency")],

91 swh=PHYSICAL_STRATEGIES[("wave", "amplitude")],

92 )

93 def test_recover_physicak_values(peak_frequency, swh): (empty)

94 spectrum = spectra.Bretschneider.from_peak_frequency_swh(peak_frequency, swh) 1 ctx 1b

95 assert np.isclose(spectrum.peak_frequency, peak_frequency) 1 ctx 1b

96 assert np.isclose(spectrum.swh, swh) 1 ctx 1b