Coverage for tests/test_physical_handlers.py: 100%

38 statements  

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

1import numpy as np 

2import pytest 

3 

4import flexfrac1d.lib.physics as ph 

5 

6floe_params = (0.36, 7.8) 

7wave_params = ( 

8 ( 

9 np.array([0.00950574 + 0.13669057j]), 

10 np.array([0.02660581 + 0.02685434j]), 

11 ), # monochromatic 

12 ( 

13 ( 

14 np.array([0.00950574 + 0.13669057j, 0.02660581 + 0.0265434j]), 

15 np.array([0.03552382 + 0.05215654j, 0.06214718 + 0.1250975j]), 

16 ) # polychromatic 

17 ), 

18) 

19growth_params = (None, True) 

20 

21handlers = [ 

22 ph.FluidSurfaceHandler, 

23 ph.DisplacementHandler, 

24 ph.CurvatureHandler, 

25 ph.StrainHandler, 

26] 

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

28x_as_list_or_tuple = ( 

29 (0, 1), 

30 [0, 1], 

31 (0.3, 1.8), 

32 [0.3, 1.8], 

33 (2.5,), 

34 [2.5], 

35) 

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

37thickness = 0.5 

38 

39 

40def prepare_instance(growth_params, handler, wave_params): 

41 if growth_params is not None: 

42 one_and_maybe_two = np.linspace(1, 2, len(wave_params[0])) 

43 # Set arbitrary growth kernel with correct shape. Setting the mean to a 

44 # negative number ensures a numerical solution is used. 

45 growth_params = (-3 * one_and_maybe_two[:, None], 20) 

46 

47 if handler == ph.FluidSurfaceHandler: 

48 handler_instance = handler(wave_params, growth_params) 

49 elif handler == ph.StrainHandler: 

50 handler_instance = handler( 

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

52 ) 

53 else: 

54 handler_instance = handler(floe_params, wave_params, growth_params) 

55 return handler_instance 

56 

57 

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

59@pytest.mark.parametrize("growth_params", growth_params) 

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

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

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

63 handler_instance = prepare_instance(growth_params, handler, wave_params) 

64 res = handler_instance.compute(x) 

65 with pytest.raises(TypeError): 

66 len(res) 

67 

68 

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

70@pytest.mark.parametrize("growth_params", growth_params) 

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

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

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

74 handler_instance = prepare_instance(growth_params, handler, wave_params) 

75 res = handler_instance.compute(x) 

76 assert len(x) == res.size