Coverage for kwave/recorder.py: 18%

1 from dataclasses import dataclass

2 from typing import List

3

4 from kwave.data import Array

5 from kwave.kgrid import kWaveGrid

6

7

8@dataclass

9 class Recorder(object):

10

11 def __init__(self):

12 # flags which control which parameters are recorded

13 self.p = True #: time-varying acoustic pressure

14 self.p_max = False #: maximum pressure over simulation

15 self.p_min = False #: minimum pressure over simulation

16 self.p_rms = False #: root-mean-squared pressure over simulation

17 self.p_max_all = False #: maximum pressure over simulation at all grid points

18 self.p_min_all = False #: minimum pressure over simulation at all grid points

19 self.p_final = False #: final pressure field at all grid points

20 self.u = False #: time-varying particle velocity

21 self.u_split_field = False #: compressional and shear components of time-varying particle velocity

22 self.u_non_staggered = False #: time-varying particle velocity on non-staggered grid

23 self.u_max = False #: maximum particle velocity over simulation

24 self.u_min = False #: minimum particle velocity over simulation

25 self.u_rms = False #: root-mean-squared particle velocity over simulation

26 self.u_max_all = False #: maximum particle velocity over simulation at all grid points

27 self.u_min_all = False #: minimum particle velocity over simulation at all grid points

28 self.u_final = False #: final particle velocity field at all grid points

29 self.I = False #: time-varying acoustic intensity

30 self.I_avg = False #: time-averaged acoustic intensity

31

32 self.cuboid_corners_list = None

33

34 self.x1_inside, self.x2_inside = None, None

35 self.y1_inside, self.y2_inside = None, None

36 self.z1_inside, self.z2_inside = None, None

37

38 def set_flags_from_list(self, flags_list: List[str], is_elastic_code: bool) -> None:

39 """

40 Set Recorder flags that are present in the string list to True

41

42 Args:

43 flags_list: String list of flags that should be set to True

44 is_elastic_code: Is the simulation elastic

45

46 Returns:

47 None

48 """

49 # check the contents of the cell array are valid inputs

50 allowed_flags = self.get_allowed_flags(is_elastic_code)

51 for record_element in flags_list:

52 assert record_element in allowed_flags, f'{record_element} is not a valid input for sensor.record'

53

54 if record_element == 'p': # custom logic for 'p'

55 continue

56 else:

57 setattr(self, record_element, True)

58

59 # set self.record_p to false if a user input for sensor.record

60 # is given and 'p' is not set (default is true)

61 self.p = ('p' in flags_list)

62

63 def set_index_variables(self, kgrid: kWaveGrid, pml_size: Array, is_pml_inside: bool, is_axisymmetric: bool) -> None:

64 """

65 Assign the index variables

66

67 Args:

68 kgrid: kWaveGrid instance

69 pml_size: Size of the PML

70 is_pml_inside: Whether the PML is inside the grid defined by the user

71 is_axisymmetric: Whether the simulation is axisymmetric

72

73 Returns:

74 None

75 """

76 if not is_pml_inside:

77 self.x1_inside = pml_size.x + 1.0

78 self.x2_inside = kgrid.Nx - pml_size.x

79 if kgrid.dim == 2:

80 if is_axisymmetric:

81 self.y1_inside = 1

82 else:

83 self.y1_inside = pml_size.y + 1.0

84 self.y2_inside = kgrid.Ny - pml_size.y

85 elif kgrid.dim == 3:

86 self.y1_inside = pml_size.y + 1.0

87 self.y2_inside = kgrid.Ny - pml_size.y

88 self.z1_inside = pml_size.z + 1.0

89 self.z2_inside = kgrid.Nz - pml_size.z

90 else:

91 self.x1_inside = 1.0

92 self.x2_inside = kgrid.Nx

93 if kgrid.dim == 2:

94 self.y1_inside = 1.0

95 self.y2_inside = kgrid.Ny

96 if kgrid.dim == 3:

97 self.z1_inside = 1.0

98 self.z2_inside = kgrid.Nz

99

100 @staticmethod

101 def get_allowed_flags(is_elastic_code):

102 """

103 Get the list of allowed flags for a given simulation type

104

105 Args:

106 is_elastic_code: Whether the simulation is axisymmetric

107

108 Returns:

109 List of allowed flags for a given simulation type

110 """

111 allowed_flags = ['p', 'p_max', 'p_min', 'p_rms', 'p_max_all', 'p_min_all', 'p_final',

112 'u', 'u_max', 'u_min', 'u_rms', 'u_max_all', 'u_min_all', 'u_final',

113 'u_non_staggered', 'I', 'I_avg']

114 if is_elastic_code: # pragma: no cover

115 allowed_flags += ['u_split_field']

116 return allowed_flags

117

118 def is_set(self, attrs: List[str]) -> List[bool]:

119 """

120 Check if the attributes are set

121

122 Args:

123 attrs: Attributes to check

124

125 Returns:

126 List of individual boolean results

127 """

128 return [getattr(self, a) for a in attrs]