yasfpy.functions.cpu_numba#
CPU kernels accelerated with Numba.
This module contains CPU implementations of inner loops used for coupling and field/polarization calculations.
Attributes#
Functions#
|
Compute Wx = coupling_matrix @ x for a single channel. |
|
Compute Wx = coupling_matrix @ x. |
|
Accumulate a coupling-matvec block into wx. |
|
Accumulate a coupling-matvec block into wx for sparse targets. |
|
Create the (particle, n, tau, l, m) lookup table. |
|
Compute the scattering cross section. |
Calculates the scattered field for a given set of parameters and returns the result. |
|
|
Calculates the electric field components in the theta and phi directions for given input parameters. |
|
Compute the polarization components of electromagnetic fields. |
Compute the radial independent scattered field. |
|
|
Compute lookup tables for spherical computations. |
|
Compute the field for scattered or initial coefficients. |
Module Contents#
- yasfpy.functions.cpu_numba.prange#
- yasfpy.functions.cpu_numba.particle_interaction_scalar(lmax, particle_number, idx, x, translation_table, plm, sph_h, e_j_dm_phi)#
Compute Wx = coupling_matrix @ x for a single channel.
This specialization avoids per-row array allocations and inner loops over the wavelength axis, which noticeably helps the common channels==1 case.
- yasfpy.functions.cpu_numba.particle_interaction(lmax, particle_number, idx, x, translation_table, plm, sph_h, e_j_dm_phi)#
Compute Wx = coupling_matrix @ x.
Notes
This routine is performance-critical. The implementation is structured so that the outer prange writes to disjoint output rows, which allows Numba to parallelize safely.
The implementation avoids redundant work by: - computing per-(j1, j2) scalars once (not once per channel) - skipping same-particle blocks by construction
- yasfpy.functions.cpu_numba.particle_interaction_tiled(lmax, tile_s1_start, tile_s1_end, tile_s2_start, tile_s2_end, idx, x, translation_table, plm_tile, sph_h_tile, e_j_dm_phi_tile, wx)#
Accumulate a coupling-matvec block into wx.
This is the tiled counterpart to particle_interaction.
- Parameters:
tile_s1_start/tile_s1_end – Global particle index range for output rows.
tile_s2_start/tile_s2_end – Global particle index range for input columns.
Notes
plm_tile, sph_h_tile, and e_j_dm_phi_tile must be computed for the cross product of the two particle ranges (same ordering as mutual_lookup).
- yasfpy.functions.cpu_numba.particle_interaction_sparse(lmax, tile_s1_start, tile_s1_end, s2_indices, near_mask, idx, x, translation_table, plm_tile, sph_h_tile, e_j_dm_phi_tile, wx)#
Accumulate a coupling-matvec block into wx for sparse targets.
s2_indices lists which global particle blocks are included. near_mask controls which (s1_local, s2_local) pairs contribute.
- yasfpy.functions.cpu_numba.compute_idx_lookups(lmax, particle_number)#
Create the (particle, n, tau, l, m) lookup table.
Notes
YASF flattens the unknown vector by stacking all vector-spherical-wave-function (VSWF) coefficients for each particle. For a fixed particle
swe enumerate the modes using(tau, l, m)withtau in {1, 2},l = 1..lmax, andm = -l..l.The per-particle mode index
nfollows the CELES ordering\[n = (\tau - 1)\,l_{\max}(l_{\max}+2) + (l-1)(l+1) + l + m.\]The global flat index is then
i = s * nmax + nwithnmax = 2 * lmax * (lmax + 2).
- yasfpy.functions.cpu_numba.compute_scattering_cross_section(lmax, particle_number, idx, sfc, translation_table, plm, sph_h, e_j_dm_phi)#
Compute the scattering cross section.
The original implementation performed a parallel loop over all (j1, j2) pairs and accumulated into a shared output array, which is not a safe parallel reduction.
This version keeps the parallelism over j1 (large axis, important for performance) by computing per-j1 partial sums and reducing at the end.
- yasfpy.functions.cpu_numba.compute_radial_independent_scattered_field_legacy(lmax, particles_position, idx, sfc, k_medium, azimuthal_angles, e_r, e_phi, e_theta, pilm, taulm)#
Calculates the scattered field for a given set of parameters and returns the result.
- Parameters:
lmax (int) – The maximum value of the angular momentum quantum number l. It determines the maximum order of the spherical harmonics used in the computation.
particles_position (np.ndarray) – An array representing the positions of particles. It has shape (num_particles, 3), where num_particles is the number of particles and each row represents the x, y, and z coordinates of a particle.
idx (np.ndarray) – An array containing the indices of the particles. It has shape (jmax, 5) where jmax is the total number of particles. Each row of idx represents a particle and contains the following information:
sfc (np.ndarray) – A 3-dimensional array representing the scattering form factors. It has dimensions (s, n, w), where:
k_medium (np.ndarray) – An array representing the wave number in the medium. It is used in the calculation of the scattered field.
azimuthal_angles (np.ndarray) – An array of azimuthal angles, representing the angles at which the scattered field is computed.
e_r (np.ndarray) – An array representing the radial component of the electric field. It has shape (azimuthal_angles.size, 3), where azimuthal_angles.size is the number of azimuthal angles and 3 represents the three Cartesian components of the electric field.
e_phi (np.ndarray) – An array representing the electric field component in the azimuthal direction. It has a shape of (azimuthal_angles.size, 3), where azimuthal_angles.size is the number of azimuthal angles and 3 represents the three components of the electric field.
e_theta (np.ndarray) – An array representing the electric field component in the theta direction. It has a shape of (azimuthal_angles.size, 3), where azimuthal_angles.size is the number of azimuthal angles and 3 represents the three components of the electric field.
pilm (np.ndarray) – An array representing the matrix of spherical harmonics coefficients. It has a shape of (lmax+1, lmax+1, azimuthal_angles.size). Each element pilm[l, m, a] represents the coefficient of the spherical harmonics for a given l, m, and azimuthal angle a.
taulm (np.ndarray) – An array representing the scattering coefficients for each combination of l, m, and azimuthal angle a. It has a shape of (lmax+1, lmax+1, azimuthal_angles.size). The values in taulm represent the scattering coefficients.
- Returns:
An array of complex numbers representing the scattered field.
- Return type:
e_1_sca (np.ndarray)
- yasfpy.functions.cpu_numba.compute_electric_field_angle_components(lmax, particles_position, idx, sfc, k_medium, azimuthal_angles, e_r, pilm, taulm)#
Calculates the electric field components in the theta and phi directions for given input parameters.
- Parameters:
lmax (int) – The maximum value of the angular momentum quantum number l. It determines the maximum value of l for which the calculations will be performed.
particles_position (np.ndarray) – The positions of particles. It has shape (num_particles, 3), where num_particles is the number of particles and each particle has 3 coordinates (x, y, z).
idx (np.ndarray) – A numpy array of shape (jmax, 5), where jmax is the total number of particles multiplied by 2 * lmax * (lmax + 2). Each row of idx represents the indices (s, n, tau, l, m).
sfc (np.ndarray) – A 3-dimensional numpy array representing the scattering form factors. It has dimensions (s, n, w).
k_medium (np.ndarray) – The wave vector in the medium. It is a numpy array that contains the wave vector values for different frequencies or wavelengths.
azimuthal_angles (np.ndarray) – An array representing the azimuthal angles at which the electric field components are computed. It specifies the angles at which the electric field is measured in the azimuthal direction.
e_r (np.ndarray) – The unit vector pointing in the direction of the electric field. It is a numpy array of shape (azimuthal_angles.size, 3), where each row corresponds to a different azimuthal angle and the three columns represent the x, y, and z components.
pilm (np.ndarray) – A 3-dimensional numpy array of shape (lmax+1, lmax+1, azimuthal_angles.size). It represents the matrix elements of the electric field expansion coefficients for the theta component. The indices (l, m, a) correspond to the spherical harmonics.
taulm (np.ndarray) – A numpy array that represents the angular momentum coupling coefficients. It has a shape of (lmax+1, lmax+1, azimuthal_angles.size). The first dimension represents the value of l, the second dimension represents the value of m, and the third dimension represents the azimuthal angle.
- Returns:
The electric field component in the theta direction. e_field_phi (np.ndarray): The electric field component in the phi direction.
- Return type:
e_field_theta (np.ndarray)
- yasfpy.functions.cpu_numba.compute_polarization_components(number_of_wavelengths, number_of_angles, e_field_theta, e_field_phi)#
Compute the polarization components of electromagnetic fields.
- Parameters:
- Returns:
- A tuple containing the following polarization components:
I (np.ndarray): The total intensity.
degree_of_polarization (np.ndarray): The degree of polarization.
degree_of_linear_polarization (np.ndarray): The degree of linear polarization.
degree_of_linear_polarization_q (np.ndarray): The degree of linear polarization in the Q direction.
degree_of_linear_polarization_u (np.ndarray): The degree of linear polarization in the U direction.
degree_of_circular_polarization (np.ndarray): The degree of circular polarization.
- Return type:
degree_of_polarization_tuple (tuple)
- yasfpy.functions.cpu_numba.compute_radial_independent_scattered_field(number_of_wavelengths, number_of_angles, e_phi, e_theta, e_field_theta, e_field_phi)#
Compute the radial independent scattered field.
- Parameters:
number_of_wavelengths (int) – The number of wavelengths.
number_of_angles (int) – The number of angles.
e_phi (np.ndarray) – The electric field in the phi direction.
e_theta (np.ndarray) – The electric field in the theta direction.
e_field_theta (np.ndarray) – The electric field theta component.
e_field_phi (np.ndarray) – The electric field phi component.
- Returns:
The computed radial independent scattered field.
- Return type:
e_1_sca (np.ndarray)
- yasfpy.functions.cpu_numba.compute_lookup_tables(lmax, size_parameter, phi, cosine_theta)#
Compute lookup tables for spherical computations.
- Parameters:
lmax (int) – The maximum degree of the spherical harmonics.
size_parameter (np.ndarray) – Array of size parameters.
phi (np.ndarray) – Array of azimuthal angles.
cosine_theta (np.ndarray) – Array of cosine of polar angles.
- Returns:
Array of spherical Bessel functions. spherical_hankel (np.ndarray): Array of spherical Hankel functions. e_j_dm_phi (np.ndarray): Array of exponential terms. p_lm (np.ndarray): Array of associated Legendre polynomials.
- Return type:
spherical_bessel (np.ndarray)
- yasfpy.functions.cpu_numba.compute_field(lmax, idx, size_parameter, sph_h, derivative, e_j_dm_phi, p_lm, pi_lm, tau_lm, e_r, e_theta, e_phi, scattered_field_coefficients=None, initial_field_coefficients=None, scatter_to_internal=None)#
Compute the field for scattered or initial coefficients.
This is a lightweight Python dispatcher around a Numba-jitted core. Numba doesn’t reliably support np.ndarray | None arguments in nopython mode, so the jitted function always receives a concrete coefficient array.
scatter_to_internal is currently unused but kept for API compatibility.