Coverage for tests/test_physical_handlers.py: 0%

1 import numpy as np

2 import pytest

3

4 import swiift.lib.physics as ph

5 from swiift.model.model import FloatingIce, Ice, Ocean, WavesUnderFloe, WavesUnderIce

6 from tests.helpers import growth_params_bool, make_growth_params, wave_params

7

8 floe_params = (0.36, 7.8)

9 handlers = [

10 ph.FluidSurfaceHandler,

11 ph.DisplacementHandler,

12 ph.CurvatureHandler,

13 ph.StrainHandler,

14]

15 vectorised_methods = ("displacement", "curvature")

16 scalar_methods = ("energy",)

17 x_as_real = (0, 1, 1.0, 3.18, np.array(1.8))

18 x_as_list_or_tuple = (

19 (0, 1),

20 [0, 1],

21 (0.3, 1.8),

22 [0.3, 1.8],

23 (2.5,),

24 [2.5],

25)

26 x_as_array = tuple([np.asarray(_x) for _x in x_as_list_or_tuple[1::2]])

27 thickness = 0.5

28

29

30 def prepare_instance(growth_params_bool, handler, wave_params):

31 growth_params = make_growth_params(growth_params_bool, wave_params)

32 if handler == ph.FluidSurfaceHandler:

33 handler_instance = handler(wave_params, growth_params)

34 elif handler == ph.StrainHandler:

35 handler_instance = handler(

36 ph.CurvatureHandler(floe_params, wave_params, growth_params), thickness

37 )

38 else:

39 handler_instance = handler(floe_params, wave_params, growth_params)

40 return handler_instance

41

42

43@pytest.mark.parametrize("wave_params", wave_params)

44@pytest.mark.parametrize("growth_params", growth_params_bool)

45@pytest.mark.parametrize("x", x_as_real)

46@pytest.mark.parametrize("handler", handlers)

47 def test_shape_real(wave_params, growth_params, x, handler):

48 handler_instance = prepare_instance(growth_params, handler, wave_params)

49 res = handler_instance.compute(x)

50 with pytest.raises(TypeError):

51 len(res)

52

53

54@pytest.mark.parametrize("wave_params", wave_params)

55@pytest.mark.parametrize("growth_params", growth_params_bool)

56@pytest.mark.parametrize("x", x_as_list_or_tuple + x_as_array)

57@pytest.mark.parametrize("handler", handlers)

58 def test_shape_array(wave_params, growth_params, x, handler):

59 handler_instance = prepare_instance(growth_params, handler, wave_params)

60 res = handler_instance.compute(x)

61 assert len(x) == res.size

62

63

64 def setup_wuf(wave_params):

65 gravity = 9.8

66 left_edge = 12.6

67 length = 100.3

68 amplitudes, c_wavenumbers = wave_params

69 wavenumbers, attenuations = (func(c_wavenumbers) for func in (np.real, np.imag))

70 wui = WavesUnderIce(

71 FloatingIce.from_ice_ocean(Ice(), Ocean(), gravity),

72 wavenumbers,

73 attenuations,

74 )

75 return WavesUnderFloe(

76 left_edge=left_edge, length=length, wui=wui, edge_amplitudes=amplitudes

77 )

78

79

80@pytest.mark.parametrize("wave_params", wave_params)

81@pytest.mark.parametrize("growth_params", growth_params_bool)

82@pytest.mark.parametrize("x", x_as_list_or_tuple + x_as_array)

83@pytest.mark.parametrize("method", vectorised_methods)

84 def test_from_wuf_object(wave_params, growth_params, x, method):

85 wuf = setup_wuf(wave_params)

86 growth_params = make_growth_params(growth_params, wave_params)

87 res = getattr(wuf, method)(x, growth_params)

88 assert len(x) == res.size

89

90

91@pytest.mark.parametrize("wave_params", wave_params)

92@pytest.mark.parametrize("growth_params", growth_params_bool)

93@pytest.mark.parametrize("method", scalar_methods)

94 def test_from_wuf_object_scalar(wave_params, growth_params, method):

95 wuf = setup_wuf(wave_params)

96 growth_params = make_growth_params(growth_params, wave_params)

97 getattr(wuf, method)(growth_params)