Coverage for kwave/kWaveSimulation_helper/set_sound_speed_ref.py: 28%

1 from kwave import kWaveMedium

2 import numpy as np

3

4

5 def set_sound_speed_ref(medium: kWaveMedium, elastic_code: bool, kspace_elastic_code: bool):

6 """

7 select the reference sound speed used in the k-space operator

8 based on the heterogeneous sound speed map

9 Args:

10 medium:

11 elastic_code:

12 kspace_elastic_code:

13

14 Returns:

15

16 """

17 if not elastic_code:

18 return get_ordinary_sound_speed_ref(medium)

19 elif not kspace_elastic_code: # pragma: no cover

20 return get_pstd_elastic_sound_speed_ref(medium)

21 else: # pragma: no cover

22 return get_kspace_elastic_sound_speed_ref(medium)

23

24

25 def get_ordinary_sound_speed_ref(medium):

26 """

27 calculate the reference sound speed for the fluid code, using the

28 maximum by default which ensures the model is unconditionally stable

29 Args:

30 medium:

31

32 Returns:

33

34 """

35 c_ref = _get_sound_speed_ref(medium.sound_speed_ref, medium.sound_speed)

36 print(' reference sound speed: ', c_ref, 'm/s')

37 return c_ref, None, None

38

39

40 def get_pstd_elastic_sound_speed_ref(medium: kWaveMedium): # pragma: no cover

41 """

42 in the pstd elastic case, the reference sound speed is only used to

43 calculate the PML absorption, so just use the compressional wave speed

44 Args:

45 medium:

46

47 Returns:

48

49 """

50 c_ref = _get_sound_speed_ref(medium.sound_speed_ref, medium.sound_speed_compression)

51 print(' reference sound speed: ', c_ref, 'm/s')

52 return c_ref, None, None

53

54

55 def get_kspace_elastic_sound_speed_ref(medium: kWaveMedium): # pragma: no cover

56 """

57 in the k-space elastic case, there are two reference sound speeds for

58 the compressional and shear waves, so compute them seperately

59 Args:

60 medium:

61

62 Returns:

63

64 """

65 c_ref_compression = _get_sound_speed_ref(medium.sound_speed_ref_compression, medium.sound_speed_compression)

66 print(' reference sound speed (compression): ', c_ref_compression, 'm/s')

67

68 c_ref_shear = _get_sound_speed_ref(medium.sound_speed_ref_shear, medium.sound_speed_shear)

69 print(' reference sound speed (shear): ', c_ref_shear, 'm/s')

70

71 return None, c_ref_compression, c_ref_shear

72

73

74 def _get_sound_speed_ref(reference, speed):

75 reductions = {

76 'min': np.min,

77 'max': np.max,

78 'mean': np.mean

79 }

80

81 if reference is not None:

82 # if reference is defined, check whether it is a scalar or 'reduction'

83 if np.isscalar(reference):

84 c_ref = reference

85 else:

86 c_ref = reductions[reference](speed)

87 else:

88 c_ref = reductions['max'](speed)

89

90 print(' reference sound speed: ', c_ref, 'm/s')

91 return float(c_ref)