Coverage for tests/test_attenuation.py: 100%

56 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-08-30 14:00 +0200

1from hypothesis import given 

2import hypothesis.extra.numpy as npst 

3import numpy as np 

4 

5import flexfrac1d.lib.att as att 

6from flexfrac1d.model.model import FloatingIce, WavesUnderElasticPlate, WavesUnderIce 

7 

8from .conftest import coupled_ocean_ice, physical_strategies 

9 

10wavenumbers_strategy = npst.arrays( 

11 float, 

12 npst.array_shapes(min_dims=1, max_dims=1, min_side=1, max_side=10), 

13 elements=physical_strategies["wave"]["wavenumber"], 

14) 

15 

16 

17class TestNoAttenuation: 

18 @staticmethod 

19 def test_unit(): 

20 assert att.no_attenuation() == 0 

21 

22 @staticmethod 

23 @given(**coupled_ocean_ice, wavenumbers=wavenumbers_strategy) 

24 def test_integrated(ice, ocean, gravity, wavenumbers): 

25 fi = FloatingIce.from_ice_ocean(ice, ocean, gravity) 

26 wup = WavesUnderElasticPlate(fi, wavenumbers) 

27 wui = WavesUnderIce.without_attenuation(wup) 

28 assert wui.attenuations == 0 

29 

30 

31class TestParam01: 

32 @staticmethod 

33 @given( 

34 thickness=physical_strategies["ice"]["thickness"], 

35 wavenumbers=wavenumbers_strategy, 

36 ) 

37 def test_unit(thickness, wavenumbers): 

38 attenuations = wavenumbers**2 * thickness / 4 

39 assert np.allclose( 

40 attenuations - att.parameterisation_01(thickness, wavenumbers), 0 

41 ) 

42 

43 @staticmethod 

44 @given(**coupled_ocean_ice, wavenumbers=wavenumbers_strategy) 

45 def test_integrated(ice, ocean, gravity, wavenumbers): 

46 fi = FloatingIce.from_ice_ocean(ice, ocean, gravity) 

47 wup = WavesUnderElasticPlate(fi, wavenumbers) 

48 wui = WavesUnderIce.with_attenuation_01(wup) 

49 assert np.allclose( 

50 wui.attenuations - att.parameterisation_01(ice.thickness, wavenumbers), 0 

51 ) 

52 

53 

54class TestGeneric: 

55 # Test passing a generic attenuation function against the existing 

56 # parameterisations. 

57 

58 @staticmethod 

59 @given(**coupled_ocean_ice, wavenumbers=wavenumbers_strategy) 

60 def test_no_attenuation(ice, ocean, gravity, wavenumbers): 

61 fi = FloatingIce.from_ice_ocean(ice, ocean, gravity) 

62 wup = WavesUnderElasticPlate(fi, wavenumbers) 

63 wui = WavesUnderIce.with_generic_attenuation(wup, lambda: 0) 

64 wui_ref = WavesUnderIce.without_attenuation(wup) 

65 assert wui.attenuations == wui_ref.attenuations 

66 

67 @staticmethod 

68 @given(**coupled_ocean_ice, wavenumbers=wavenumbers_strategy) 

69 def test_param01_args(ice, ocean, gravity, wavenumbers): 

70 # Test Param01 providing arguments as a string 

71 fi = FloatingIce.from_ice_ocean(ice, ocean, gravity) 

72 wup = WavesUnderElasticPlate(fi, wavenumbers) 

73 wui = WavesUnderIce.with_generic_attenuation( 

74 wup, 

75 lambda thickness, wavenumbers: wavenumbers**2 * thickness / 4, 

76 "ice.thickness wavenumbers", 

77 ) 

78 wui_ref = WavesUnderIce.with_attenuation_01(wup) 

79 assert np.allclose(wui.attenuations - wui_ref.attenuations, 0) 

80 

81 @staticmethod 

82 @given(**coupled_ocean_ice, wavenumbers=wavenumbers_strategy) 

83 def test_param01_kwargs(ice, ocean, gravity, wavenumbers): 

84 # Test Param01 providing arguments as a dict 

85 fi = FloatingIce.from_ice_ocean(ice, ocean, gravity) 

86 wup = WavesUnderElasticPlate(fi, wavenumbers) 

87 wui = WavesUnderIce.with_generic_attenuation( 

88 wup, 

89 lambda thickness, wavenumbers: wavenumbers**2 * thickness / 4, 

90 **{"thickness": ice.thickness, "wavenumbers": wavenumbers}, 

91 ) 

92 wui_ref = WavesUnderIce.with_attenuation_01(wup) 

93 assert np.allclose(wui.attenuations - wui_ref.attenuations, 0)