Coverage for yasfpy/parameters.py: 62%
37 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-15 20:36 +0100
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-15 20:36 +0100
1import numpy as np
3from yasfpy.particles import Particles
4from yasfpy.initial_field import InitialField
7import numpy as np
10class Parameters:
11 """
12 The Parameters class represents the parameters for a simulation, including wavelength, refractive
13 indices, scattering particles, and initial field, and provides methods for computing angular
14 frequency and wave vectors.
15 """
17 def __init__(
18 self,
19 wavelength: np.array,
20 medium_refractive_index: np.array,
21 particles: Particles,
22 initial_field: InitialField,
23 ):
24 """Initializes the class with the given parameters and sets up the necessary variables.
26 Args:
27 wavelength (np.array): An array that represents the wavelengths of the light being used.
28 It contains the values of the wavelengths at which the simulation will be performed.
29 medium_refractive_index (np.array): An array that represents the refractive index of the
30 medium in which the particles are located. It contains the refractive index values at different
31 wavelengths.
32 particles (Particles): An instance of the "Particles" class. It represents the particles
33 present in the medium.
34 initial_field (InitialField): An object of the `InitialField` class. It represents the
35 initial field configuration for the simulation.
36 """
37 self.wavelength = wavelength
38 self.medium_refractive_index = medium_refractive_index
39 self.wavelengths_number = wavelength.size
40 self.particles = particles
41 self.initial_field = initial_field
43 self.__setup()
45 def __setup(self):
46 """The function sets up the necessary computations for omega and ks."""
47 self.__compute_omega()
48 self.__compute_ks()
50 def __compute_omega(self):
51 """The function calculates the value of omega using the wavelength."""
52 self.omega = 2 * np.pi / self.wavelength
54 def __interpolate_refractive_index_from_table(self):
55 """Interpolates the refractive index values from a table for different wavelengths.
57 Returns:
58 refractive_index_interpolated (np.array): An array that contains the interpolated refractive index values for the particles
59 at different wavelengths.
60 """
61 refractive_index_interpolated = np.zeros(
62 (self.particles.num_unique_refractive_indices, self.wavelength.size),
63 dtype=complex,
64 )
65 for idx, data in enumerate(self.particles.refractive_index_table):
66 table = data["ref_idx"].to_numpy().astype(float)
67 n = np.interp(
68 self.wavelength / 1e3,
69 table[:, 0],
70 table[:, 1],
71 left=table[0, 1],
72 right=table[-1, 1],
73 )
74 k = np.interp(
75 self.wavelength / 1e3,
76 table[:, 0],
77 table[:, 2],
78 left=table[0, 2],
79 right=table[-1, 2],
80 )
81 refractive_index_interpolated[idx, :] = n + 1j * k
82 return refractive_index_interpolated
84 def __index_to_table(self):
85 # TODO: do all the idx to value conversion here
86 pass
88 def __compute_ks(self):
89 """Computes the values of k_medium and k_particle based on the refractive index of the
90 medium and particles.
91 """
92 self.k_medium = self.omega * self.medium_refractive_index
93 if self.particles.refractive_index_table is None:
94 self.k_particle = np.outer(self.particles.refractive_index, self.omega)
95 else:
96 table = self.__interpolate_refractive_index_from_table()
97 self.k_particle = (
98 np.take(table, self.particles.refractive_index, axis=0)
99 * self.omega[np.newaxis, :]
100 )
102 unique_radius_index_pairs = np.zeros(
103 (
104 self.particles.unique_radius_index_pairs.shape[0],
105 self.wavelength.size + 1,
106 ),
107 dtype=complex,
108 )
109 unique_radius_index_pairs[:, 0] = self.particles.unique_radius_index_pairs[
110 :, 0
111 ]
112 unique_radius_index_pairs[:, 1:] = np.take(
113 table,
114 self.particles.unique_radius_index_pairs[:, 1].astype(int),
115 axis=0,
116 )
118 self.particles.unique_radius_index_pairs = unique_radius_index_pairs