Skip to content

Particles

Particles

The Particles class represents a collection of particles with various properties such as position, radius, and refractive index, and provides methods for computing unique properties and characteristics of the particles.

Initializes an object with position, radius, refractive index, refractive index table, and shape type attributes.

Parameters:

Name Type Description Default
position array

A numpy array representing the position of the shape.

required
r array

A numpy array containing the radius values for each shape in the system.

required
refractive_index array

A numpy array representing the refractive index of the shape. It can be either a complex number or a two-column matrix.

required
refractive_index_table list

A list containing the refractive index values for different materials. Each element in the list represents a material, and the refractive index values for that material are stored as a complex number.

None
shape_type str

A string specifying the type of shape for the object. Defaults to "sphere" or any other supported shape type.

'sphere'
Source code in yasfpy/particles.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def __init__(
    self,
    position: np.ndarray,
    r: np.ndarray,
    refractive_index: np.ndarray,
    refractive_index_table: list = None,
    shape_type: str = "sphere",
):
    """Initializes an object with position, radius, refractive index, refractive index table, and shape type attributes.

    Args:
        position (np.array): A numpy array representing the position of the shape.
        r (np.array): A numpy array containing the radius values for each shape in the system.
        refractive_index (np.array): A numpy array representing the refractive index of the shape.
            It can be either a complex number or a two-column matrix.
        refractive_index_table (list): A list containing the refractive index values for different materials.
            Each element in the list represents a material, and the refractive index values for that material are stored as a complex number.
        shape_type (str, optional): A string specifying the type of shape for the object.
            Defaults to "sphere" or any other supported shape type.

    """
    self.position = position
    self.r = r
    self.refractive_index = refractive_index
    self.type = shape_type

    # self.log = log.scattering_logger(__name__)
    self.log = logging.getLogger(self.__class__.__module__)

    # TODO: Keep it for now, remove later...
    self.refractive_index_table = refractive_index_table

    if refractive_index_table is None:
        if len(refractive_index.shape) > 2:
            raise Exception(
                "Refractive index should be either an integer array, complex array, or a two column float matrix!"
            )
        elif (len(refractive_index.shape) == 2) and (
            self.refractive_index.shape[1] > 2
        ):
            raise Exception(
                "Refractive index should be either an integer array, complex array, or a two column float matrix!"
            )

        elif (len(refractive_index.shape) > 1) and (refractive_index.shape[1] == 2):
            self.refractive_index = (
                refractive_index[:, 0] + 1j * refractive_index[:, 1]
            )
    else:
        self.refractive_index = refractive_index.astype(int)

    self.number = r.shape[0]
    self.__setup_impl()

position = position instance-attribute

r = r instance-attribute

refractive_index = refractive_index instance-attribute

type = shape_type instance-attribute

log = logging.getLogger(self.__class__.__module__) instance-attribute

refractive_index_table = refractive_index_table instance-attribute

number = r.shape[0] instance-attribute

compute_unique_refractive_indices

Computes the unique refractive indices and their indices.

Source code in yasfpy/particles.py
90
91
92
93
94
95
def compute_unique_refractive_indices(self):
    """Computes the unique refractive indices and their indices."""
    self.unique_refractive_indices, self.refractive_index_array_idx = np.unique(
        self.refractive_index, return_inverse=True, axis=0
    )
    self.num_unique_refractive_indices = self.unique_refractive_indices.shape[0]

compute_unique_radii

The function computes the unique radii from an array and stores them in a variable.

Source code in yasfpy/particles.py
 97
 98
 99
100
101
102
def compute_unique_radii(self):
    """The function computes the unique radii from an array and stores them in a variable."""
    self.unqiue_radii, self.radius_array_idx = np.unique(
        self.r, return_inverse=True, axis=0
    )
    self.num_unique_radii = self.unqiue_radii.shape[0]

compute_unique_radii_index_pairs

The function computes unique pairs of radii and refractive indices and stores them in different arrays.

Source code in yasfpy/particles.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def compute_unique_radii_index_pairs(self):
    """The function computes unique pairs of radii and refractive indices and stores them in different
    arrays.

    """
    self.unique_radius_index_pairs, self.single_unique_array_idx = np.unique(
        np.column_stack((self.r, self.refractive_index)),
        return_inverse=True,
        axis=0,
    )
    self.unique_single_radius_index_pairs = np.unique(
        np.column_stack((self.radius_array_idx, self.refractive_index_array_idx)),
        axis=0,
    )

compute_single_unique_idx

The function computes a single unique index based on the sum of pairs of values and their corresponding indices.

Source code in yasfpy/particles.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
def compute_single_unique_idx(self):
    """The function computes a single unique index based on the sum of pairs of values and their
    corresponding indices.

    """
    self.single_unique_idx = (
        np.sum(self.unique_single_radius_index_pairs, axis=1)
        * (np.sum(self.unique_single_radius_index_pairs, axis=1) + 1)
    ) // 2 + self.unique_single_radius_index_pairs[:, 1]

    # pairedArray = (
    #   self.radius_array_idx + self.refractive_index_array_idx *
    #   (self.radius_array_idx + self.refractive_index_array_idx + 1)
    # ) // 2 + self.refractive_index_array_idx

    # self.single_unique_idx, self.single_unique_array_idx = np.unique(
    #   pairedArray,
    #   return_inverse=True,
    #   axis=0)

    self.num_unique_pairs = self.unique_radius_index_pairs.shape[0]

compute_maximal_particle_distance

The function computes the maximum distance between particles using the ConvexHull algorithm.

Source code in yasfpy/particles.py
141
142
143
144
145
146
147
148
149
150
151
152
def compute_maximal_particle_distance(self):
    """The function computes the maximum distance between particles using the ConvexHull algorithm."""
    if len(self.position) < 4:
        if len(self.position) == 1:
            self.max_particle_distance = 0
        else:
            self.max_particle_distance = max(pdist(self.position))

    else:
        hull = ConvexHull(self.position)
        vert = self.position[hull.vertices, :]
        self.max_particle_distance = max(pdist(vert))

compute_volume_equivalent_area

The function computes the volume equivalent area by calculating the geometric projection.

Source code in yasfpy/particles.py
154
155
156
157
def compute_volume_equivalent_area(self):
    """The function computes the volume equivalent area by calculating the geometric projection."""
    r3 = np.power(self.r, 3)
    self.geometric_projection = np.pi * np.power(np.sum(r3), 2 / 3)

__setup_impl

The function sets up various computations related to refractive indices, radii, and particle distances.

Source code in yasfpy/particles.py
159
160
161
162
163
164
165
166
167
168
169
def __setup_impl(self):
    """The function sets up various computations related to refractive indices, radii, and particle
    distances.

    """
    self.compute_unique_refractive_indices()
    self.compute_unique_radii()
    self.compute_unique_radii_index_pairs()
    self.compute_single_unique_idx()
    self.compute_maximal_particle_distance()
    self.compute_volume_equivalent_area()

Comments