Coverage for tests/test_domain.py: 100%

1 from hypothesis import given

2 import numpy as np

3 import pytest

4

5 import flexfrac1d.lib.att as att

6 from flexfrac1d.model.model import (

7 DiscreteSpectrum,

8 Domain,

9 Floe,

10 FreeSurfaceWaves,

11 Ocean,

12)

13

14 from .conftest import ocean_and_spectrum, simple_objects

15

16 growth_params = (None, (-13, None), (-28, 75), (np.array([-45]), None))

17

18

19 def instantiate_domain(att_spec, is_mono) -> None:

20 ocean = simple_objects["ocean"]

21 if is_mono:

22 spectrum = simple_objects["spec_mono"]

23 else:

24 spectrum = simple_objects["spec_poly"]

25 gravity = simple_objects["gravity"]

26 return Domain.from_discrete(gravity, spectrum, ocean, attenuation=att_spec)

27

28

29@given(**ocean_and_spectrum)

30 def test_initialisation(gravity: float, spectrum: DiscreteSpectrum, ocean: Ocean):

31 domain = Domain.from_discrete(gravity, spectrum, ocean)

32 fsw = FreeSurfaceWaves.from_ocean(ocean, spectrum, gravity)

33

34 assert domain.gravity == gravity

35

36 assert domain.fsw.ocean == ocean

37 assert np.all(domain.fsw.wavenumbers == fsw.wavenumbers)

38

39 # TODO: to reenable when DiscreteSpectrum has been attrs'd

40 # assert np.all(domain.spectrum.amplitudes == spectrum.amplitudes)

41 # assert np.all(domain.spectrum.frequencies == spectrum.frequencies)

42 # assert np.all(domain.spectrum.phases == spectrum.phases)

43

44 assert domain.growth_params is None

45 assert domain.attenuation == att.AttenuationParameterisation.PARAM_01

46

47 assert len(domain.cached_wuis) == 0

48 assert len(domain.cached_phases) == 0

49

50

51@given(**ocean_and_spectrum)

52 def test_failing(gravity: float, spectrum: DiscreteSpectrum, ocean: Ocean):

53 with pytest.raises(TypeError):

54 Domain.from_discrete(gravity, spectrum, ocean, growth_params=1)

55

56 with pytest.raises(ValueError):

57 Domain.from_discrete(gravity, spectrum, ocean, growth_params=(1, 1, 1))

58

59 nf = spectrum.nf

60 if nf in (1, 2):

61 nf = 3

62 else:

63 nf -= 1

64 means = np.zeros(nf)

65 with pytest.raises(ValueError):

66 Domain.with_growth_means(gravity, spectrum, ocean, growth_means=means)

67

68

69 def instantiate_floe() -> None:

70 ice = simple_objects["ice"]

71 return Floe(

72 left_edge=simple_objects["left_edge"],

73 length=simple_objects["length"],

74 ice=ice,

75 )

76

77

78@pytest.mark.parametrize("is_mono", (True, False))

79@pytest.mark.parametrize("att_spec", att.AttenuationParameterisation)

80 def test_att_parameterisations(att_spec, is_mono):

81 floe = instantiate_floe()

82 domain = instantiate_domain(att_spec, is_mono)

83 domain.add_floes(floe)

84 assert len(domain.cached_wuis) == 1

85 assert floe.ice in domain.cached_wuis

86

87

88 def test_promote():

89 floe = instantiate_floe()

90 res = Domain._promote_floe(floe)

91 assert isinstance(res, tuple)

92 assert len(res) == 1

93 assert res[0] == floe

94

95 floes = [floe]

96 res = Domain._promote_floe(floes)

97 assert res == floes

98

99 with pytest.raises(ValueError):

100 Domain._promote_floe(1)