Coverage for yasfpy/initial_field.py: 81%

27 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-15 20:36 +0100

1import yasfpy.log as log 

2 

3import numpy as np 

4 

5 

6class InitialField: 

7 """Represents an object with various parameters for a beam of light.""" 

8 

9 def __init__( 

10 self, 

11 beam_width, 

12 focal_point, 

13 field_type: str = "gaussian", 

14 amplitude: float = 1, 

15 polar_angle: float = 0, 

16 azimuthal_angle: float = 0, 

17 polarization: str = "TE", 

18 ): 

19 """Initializes an object with various parameters for a beam of light. 

20 

21 Args: 

22 beam_width (float): The beam width parameter represents the width of the beam. It determines the spread of the beam 

23 and is typically measured as the full width at half maximum (FWHM) of the beam intensity 

24 distribution. 

25 focal_point (tuple): The focal point is the point in space where the beam is focused. It is a coordinate in 

26 three-dimensional space (x, y, z) that represents the position of the focal point. 

27 field_type (str, optional): The `field_type` parameter specifies the type of field for the beam. It can be set to 

28 "gaussian" or any other type of field that is supported by the code. Defaults to "gaussian". 

29 amplitude (float, optional): The amplitude parameter represents the maximum value or intensity of the beam. It determines 

30 the overall strength or power of the beam. Defaults to 1. 

31 polar_angle (float, optional): The `polar_angle` parameter represents the angle between the positive z-axis and the direction 

32 of propagation of the beam. It is measured in radians. Defaults to 0. 

33 azimuthal_angle (float, optional): The azimuthal angle is a measure of the angle between the projection of the vector onto the 

34 xy-plane and the positive x-axis. It determines the orientation of the beam in the xy-plane. Defaults to 0. 

35 polarization (str, optional): The "polarization" parameter determines the polarization of the beam. It can have two possible 

36 values: "TE" for transverse electric polarization and "TM" for transverse magnetic polarization. Defaults to "TE". 

37 """ 

38 self.field_type = field_type 

39 self.amplitude = amplitude 

40 self.polar_angle = polar_angle 

41 self.azimuthal_angle = azimuthal_angle 

42 self.polarization = polarization 

43 self.beam_width = beam_width 

44 self.focal_point = focal_point 

45 

46 self.log = log.scattering_logger(__name__) 

47 self.__setup() 

48 

49 def __set_pol_idx(self): 

50 """ 

51 Sets the polarization index based on the polarization type. 

52 

53 The polarization index is determined based on the value of the `polarization` attribute. 

54 If the `polarization` is "unp" or 0, the polarization index is set to 0. 

55 If the `polarization` is "te" or 1, the polarization index is set to 1. 

56 If the `polarization` is "tm" or 2, the polarization index is set to 2. 

57 If the `polarization` is not a valid value, the polarization index is set to 0 and a warning message is logged. 

58 """ 

59 if ( 

60 isinstance(self.polarization, str) and self.polarization.lower() == "unp" 

61 ) or (isinstance(self.polarization, int) and self.polarization == 0): 

62 # Unpolarized is also present in the MSTM output 

63 self.pol = 0 

64 elif ( 

65 isinstance(self.polarization, str) and self.polarization.lower() == "te" 

66 ) or (isinstance(self.polarization, int) and self.polarization == 1): 

67 # Coresponds to the perpendicular value found in MSTM 

68 self.pol = 1 

69 elif ( 

70 isinstance(self.polarization, str) and self.polarization.lower() == "tm" 

71 ) or (isinstance(self.polarization, int) and self.polarization == 2): 

72 # Coresponds to the parallel value found in MSTM 

73 self.pol = 2 

74 else: 

75 self.pol = 0 

76 self.log.warning( 

77 "%s is not a valid polarization type. Please use TE or TM. Reverting to unpolarized", 

78 self.polarization, 

79 ) 

80 

81 def __set_normal_incidence(self): 

82 """ 

83 Sets the normal incidence flag based on the polar angle. 

84 

85 This method checks the value of the polar angle and determines if it is close to zero. 

86 If the absolute value of the sine of the polar angle is less than 1e-5, the normal incidence flag is set to True. 

87 Otherwise, the normal incidence flag is set to False. 

88 """ 

89 self.normal_incidence = np.abs(np.sin(self.polar_angle)) < 1e-5 

90 

91 def __setup(self): 

92 """ 

93 Performs the initial setup of the field. 

94 

95 This method sets the polarization index and normal incidence for the field. 

96 """ 

97 self.__set_pol_idx() 

98 self.__set_normal_incidence()