Coverage for kwave/kWaveSimulation_helper/data_cast.py: 4%

1 from kwave import kWaveMedium, kWaveGrid, SimulationOptions

2 from kwave.utils import cast_to_type

3 from kwave.utils import dotdict

4

5

6 def dataCast(data_cast, medium: kWaveMedium, kgrid: kWaveGrid, opt: SimulationOptions, values: dotdict, flags: dotdict):

7 # NOTE from Farid: this method is not used now.

8 # Therefore, there still exists 'self' calls at the end.

9 # Originally, self was referring to the kWaveSimulation class

10

11 # update command line status

12 print(f' casting variables to {data_cast} type...')

13

14 # create list of variable names used in all dimensions

15 if flags.elastic_code: # pragma: no cover

16 cast_variables = ['dt', 'mu', 'lambda']

17 else:

18 cast_variables = ['dt', 'kappa', 'c0', 'rho0']

19

20 # create a separate list for indexing variables

21 cast_index_variables = []

22

23 # add variables specific to simulations in certain dimensions

24 if kgrid.dim == 1:

25 # variables used in the fluid code

26 cast_variables = cast_variables + ['ddx_k', 'shift_pos', 'shift_neg','pml_x', 'pml_x_sgx', 'rho0_sgx_inv']

27

28 elif kgrid.dim == 2:

29 # variables used in both fluid and elastic codes

30 cast_variables = cast_variables + ['ddx_k_shift_pos', 'ddx_k_shift_neg', 'pml_x', 'pml_y',

31 'pml_x_sgx', 'pml_y_sgy', 'rho0_sgx_inv', 'rho0_sgy_inv']

32

33 # y-dimension shift and derivative variables (these differ between the fluid/elastic code and the axisymmetric code)

34 if flags.axisymmetric:

35

36 # y-axis variables

37 cast_variables = cast_variables + ['y_vec', 'y_vec_sg']

38

39 # derivative and shift variables

40 if opt.radial_symmetry in ['WSWA-FFT','WSWS-FFT','WS-FFT']:

41 cast_variables = cast_variables + ['ddy_k', 'y_shift_pos', 'y_shift_neg']

42

43 elif opt.radial_symmetry == 'WSWA':

44 cast_variables = cast_variables + ['ddy_k_wswa', 'ddy_k_hahs']

45

46 elif opt.radial_symmetry == 'WSWS':

47 cast_variables = cast_variables + ['ddy_k_wsws', 'ddy_k_haha']

48

49 else:

50 # derivative and shift variables in the regular code

51 cast_variables = cast_variables + ['ddy_k_shift_pos', 'ddy_k_shift_neg']

52

53 # extra variables only used in elastic code

54 if flags.elastic_code: # pragma: no cover

55

56 # variables used in both lossless and lossy case

57 cast_variables = cast_variables + ['mu_sgxy', 'mpml_x', 'mpml_y', 'mpml_x_sgx', 'mpml_y_sgy']

58

59 # extra variables only used in the lossy case

60 if flags.kelvin_voigt_model:

61 cast_variables = cast_variables + ['chi', 'eta', 'eta_sgxy']

62

63 elif kgrid.dim == 3:

64 # variables used in both fluid and elastic codes

65 cast_variables = cast_variables + ['ddx_k_shift_pos', 'ddy_k_shift_pos', 'ddz_k_shift_pos',

66 'ddx_k_shift_neg', 'ddy_k_shift_neg', 'ddz_k_shift_neg',

67 'pml_x', 'pml_y', 'pml_z',

68 'pml_x_sgx', 'pml_y_sgy', 'pml_z_sgz',

69 'rho0_sgx_inv', 'rho0_sgy_inv', 'rho0_sgz_inv']

70

71 # extra variables only used in elastic code

72 if flags.elastic_code: # pragma: no cover

73 # variables used in both lossless and lossy case

74 cast_variables = cast_variables + ['mu_sgxy', 'mu_sgxz', 'mu_sgyz',

75 'mpml_x', 'mpml_y', 'mpml_z',

76 'mpml_x_sgx', 'mpml_y_sgy', 'mpml_z_sgz']

77

78 # extra variables only used in the lossy case

79 if flags.kelvin_voigt_model:

80 cast_variables = cast_variables + ['chi', 'eta', 'eta_sgxy', 'eta_sgxz', 'eta_sgyz']

81

82 # add sensor mask variables

83 if flags.use_sensor:

84 cast_index_variables += ['sensor_mask_index']

85 if flags.binary_sensor_mask and (values.record.u_non_staggered or values.record.I or values.record.I_avg):

86 if kgrid.dim == 1:

87 cast_index_variables += ['record.x_shift_neg']

88 elif kgrid.dim == 2:

89 cast_index_variables += ['record.x_shift_neg', 'record.y_shift_neg']

90 elif kgrid.dim == 3:

91 cast_index_variables += ['record.x_shift_neg', 'record.y_shift_neg', 'record.z_shift_neg']

92

93 # additional variables only used if the medium is absorbing

94 if values.equation_of_state == 'absorbing':

95 cast_variables += ['absorb_nabla1', 'absorb_nabla2', 'absorb_eta', 'absorb_tau']

96

97 # additional variables only used if the propagation is nonlinear

98 if medium.is_nonlinear():

99 cast_variables += ['medium.BonA']

100

101 # additional variables only used if there is an initial pressure source

102 if flags.source_p0:

103 cast_variables += ['source.p0']

104

105 # additional variables only used if there is a time varying pressure source term

106 if flags.source_p:

107 cast_variables += ['source.p']

108 cast_index_variables += ['p_source_pos_index']

109 if flags.source_p_labelled:

110 cast_index_variables += ['p_source_sig_index']

111

112 # additional variables only used if there is a time varying velocity source term

113 if flags.source_ux or flags.source_uy or flags.source_uz:

114 cast_index_variables += ['u_source_pos_index']

115 if self.source_u_labelled:

116 cast_index_variables += ['u_source_sig_index']

117

118 if flags.source_ux:

119 cast_variables += ['source.ux']

120 if flags.source_uy:

121 cast_variables += ['source.uy']

122 if flags.source_uz:

123 cast_variables += ['source.uz']

124

125 # additional variables only used if there is a time varying stress source term

126 if flags.source_sxx or flags.source_syy or flags.source_szz or flags.source_sxy or flags.source_sxz or flags.source_syz:

127 cast_index_variables += ['s_source_pos_index']

128 if flags.source_s_labelled:

129 cast_index_variables += ['s_source_sig_index']

130

131 if flags.source_sxx:

132 cast_variables += ['source.sxx']

133 if flags.source_syy:

134 cast_variables += ['source.syy']

135 if flags.source_szz:

136 cast_variables += ['source.szz']

137 if flags.source_sxy:

138 cast_variables += ['source.sxy']

139 if flags.source_sxz:

140 cast_variables += ['source.sxz']

141 if flags.source_syz:

142 cast_variables += ['source.syz']

143

144 # addition variables only used if there is a transducer source

145 if flags.transducer_source:

146 cast_variables += ['transducer_input_signal']

147 cast_index_variables += ['u_source_pos_index', 'delay_mask', 'self.transducer_source', 'transducer_transmit_apodization']

148

149 # addition variables only used if there is a transducer sensor with an elevation focus

150 if flags.transducer_sensor and flags.transducer_receive_elevation_focus:

151 cast_index_variables += ['sensor_data_buffer', 'transducer_receive_mask']

152

153 # additional variables only used with nonuniform grids

154 if flags.nonuniform_grid:

155 if kgrid.dim == 1:

156 cast_index_variables += ['kgrid.dxudxn']

157 elif kgrid.dim == 2:

158 cast_index_variables += ['kgrid.dxudxn', 'kgrid.dyudyn']

159 elif kgrid.dim == 3:

160 cast_index_variables += ['kgrid.dxudxn', 'kgrid.dyudyn', 'kgrid.dzudzn']

161

162 # additional variables only used for Cartesian sensor masks with linear interpolation

163 if flags.use_sensor and not flags.binary_sensor_mask and not flags.time_rev:

164 if kgrid.dim == 1:

165 cast_variables += ['record.grid_x', 'record.sensor_x']

166 else:

167 cast_variables += ['record.tri', 'record.bc']

168

169 # additional variables only used in 2D if sensor directivity is defined

170 if flags.compute_directivity:

171 cast_variables += ['sensor.directivity_angle', 'sensor.directivity_unique_angles', 'sensor.directivity_wavenumbers']

172

173 # cast variables

174 for cast_var in cast_variables:

175 if '.' in cast_var:

176 part1, part2 = cast_var.split('.')

177 subdict = getattr(self, part1)

178 subdict[part2] = cast_to_type(subdict[part2], data_cast)

179 else:

180 setattr(self, cast_var, cast_to_type(getattr(self, cast_var), data_cast))

181

182 # cast index variables only if casting to the GPU

183 if data_cast.startswith('kWaveGPU'):

184 raise NotImplementedError