Coverage for tests/test_analytical_energy.py: 100%

1 from collections.abc import Callable (empty)

2 import pathlib (empty)

3

4 import numpy as np (empty)

5 import pytest (empty)

6

7 import swiift.lib.physics as ph (empty)

8

9# TODO: fiture these tests instead of running loops in individual functions

10

11# Test configurations visually examined against solution from scipy.solve_bvp

12 PATH_DIS = pathlib.Path("tests/target/displacement") (empty)

13 PATH_CUR = pathlib.Path("tests/target/curvature") (empty)

14 PATH_EGY = pathlib.Path("tests/target/energy/") (empty)

15 PATH_PLY = pathlib.Path("tests/target/poly_analytical/") (empty)

16

17

18 def format_to_pack( (empty)

19 red_num, length, wave_params_real

20) -> tuple[tuple[float], tuple[np.ndarray]]:

21 # format raw floats to fields of Handlers

22 floe_params = red_num, length 4 ctx 1cade

23 wave_params = tuple( 4 ctx 1cade

24 map(

25 np.atleast_1d,

26 (

27 wave_params_real[0] * np.exp(1j * wave_params_real[3]),

28 wave_params_real[1] + 1j * wave_params_real[2],

29 ),

30 )

31 )

32 return floe_params, wave_params 4 ctx 1cade

33

34

35 def read_header(handle: pathlib.Path): (empty)

36 with open(handle, "r") as file: 2 ctx 1cd

37 header = file.readline() 2 ctx 1cd

38 # remove trailing '# ' and split

39 red_num, length, *wave_params_real = map(float, header[2:-1].split(",")) 2 ctx 1cd

40 return red_num, length, wave_params_real 2 ctx 1cd

41

42

43 def _test_analytical(root_dir: pathlib.Path, func: Callable): (empty)

44 sentinel = 0 # make sure no error in path and at least one test was run 2 ctx 1cd

45 for handle in root_dir.glob("*ssv"): 2 ctx 1cd

46 sentinel += 1 2 ctx 1cd

47 loaded = np.loadtxt(handle) 2 ctx 1cd

48 # loaded[0]: along-floe space variable x

49 # loaded[1]: reference values for func(x)

50 floe_params, wave_params = format_to_pack(*read_header(handle)) 2 ctx 1cd

51 handler = func(floe_params, wave_params) 2 ctx 1cd

52

53 # test func(x) against existing displacement

54 assert np.allclose(loaded[1] - handler.compute(loaded[0]), 0) 2 ctx 1cd

55 assert sentinel > 0 2 ctx 1cd

56

57

58 def test_displacement(): (empty)

59 _test_analytical(PATH_DIS, ph.DisplacementHandler) 1 ctx 1d

60

61

62# @pytest.mark.parametrize

63# def test_displacement_wuf():

64# # TODO:

65# # * instantiate wui from parameters

66# # * instantiate wuf from parameters and wui

67# # * call `displacement`

68# # * profit

69# pass

70

71

72 def test_curvature(): (empty)

73 _test_analytical(PATH_CUR, ph.CurvatureHandler) 1 ctx 1c

74

75

76 def test_dce_poly(): (empty)

77 sentinel = 0 1 ctx 1a

78 for handle in PATH_PLY.glob("*"): 1 ctx 1a

79 sentinel += 1 1 ctx 1a

80 loaded = np.loadtxt(handle.joinpath("values.ssv")) 1 ctx 1a

81 x, dis, cur = loaded 1 ctx 1a

82 egy = float(np.loadtxt(handle.joinpath("energy"))) 1 ctx 1a

83 floe_params = np.loadtxt(handle.joinpath("floe_params.ssv")) 1 ctx 1a

84 wave_params_real = np.loadtxt(handle.joinpath("wave_params.ssv")) 1 ctx 1a

85 assert len(wave_params_real.shape) == 2 1 ctx 1a

86 floe_params, wave_params = format_to_pack(*floe_params, wave_params_real) 1 ctx 1a

87 displacement = ph.DisplacementHandler(floe_params, wave_params).compute 1 ctx 1a

88 curvature = ph.CurvatureHandler(floe_params, wave_params).compute 1 ctx 1a

89 energy = ph.EnergyHandler(floe_params, wave_params).compute 1 ctx 1a

90

91 _test_poly(dis, displacement, x, floe_params, wave_params) 1 ctx 1a

92 _test_poly(cur, curvature, x, floe_params, wave_params) 1 ctx 1a

93 _test_poly(egy, energy, floe_params, wave_params) 1 ctx 1a

94 assert sentinel > 1 1 ctx 1a

95

96

97 def _test_poly(ref_val, function, *args): (empty)

98 assert np.allclose(ref_val - function(*args), 0) 1 ctx 1a

99

100

101 def test_energy(): (empty)

102 loaded = np.loadtxt(PATH_EGY.joinpath("energy_mono.ssv")) 1 ctx 1e

103 for vars in loaded.T: 1 ctx 1e

104 red_num, length, *wave_params_real = vars[:-1] 1 ctx 1e

105 floe_params, wave_params = format_to_pack(red_num, length, wave_params_real) 1 ctx 1e

106 handler = ph.EnergyHandler(floe_params, wave_params) 1 ctx 1e

107 assert np.isclose(vars[-1] - handler.compute(), 0) 1 ctx 1e

108

109

110@pytest.mark.filterwarnings("error::RuntimeWarning") (empty)

111 def test_energy_no_attenuation(): (empty)

112 floe_params = 0.34, 126.12 1 ctx 1f

113 wave_params = ( 1 ctx 1f

114 np.array([0.14808142 + 0.34891663j, 0.08581965 + 0.54191726j]),

115 np.array([0.02674772 + 0.0j, 0.09422177 + 0.0j]),

116 )

117 ph.EnergyHandler(floe_params, wave_params).compute() 1 ctx 1f