yasfpy.functions.cpu_numba ========================== .. py:module:: yasfpy.functions.cpu_numba .. autoapi-nested-parse:: CPU kernels accelerated with Numba. This module contains CPU implementations of inner loops used for coupling and field/polarization calculations. Attributes ---------- .. autoapisummary:: yasfpy.functions.cpu_numba.prange Functions --------- .. autoapisummary:: yasfpy.functions.cpu_numba.particle_interaction_scalar yasfpy.functions.cpu_numba.particle_interaction yasfpy.functions.cpu_numba.particle_interaction_tiled yasfpy.functions.cpu_numba.particle_interaction_sparse yasfpy.functions.cpu_numba.compute_idx_lookups yasfpy.functions.cpu_numba.compute_scattering_cross_section yasfpy.functions.cpu_numba.compute_radial_independent_scattered_field_legacy yasfpy.functions.cpu_numba.compute_electric_field_angle_components yasfpy.functions.cpu_numba.compute_polarization_components yasfpy.functions.cpu_numba.compute_radial_independent_scattered_field yasfpy.functions.cpu_numba.compute_lookup_tables yasfpy.functions.cpu_numba.compute_field Module Contents --------------- .. py:data:: prange .. py:function:: 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. .. py:function:: particle_interaction(lmax, particle_number, idx, x, translation_table, plm, sph_h, e_j_dm_phi) Compute Wx = coupling_matrix @ x. .. rubric:: 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 .. py:function:: 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`. :param tile_s1_start/tile_s1_end: Global particle index range for output rows. :param tile_s2_start/tile_s2_end: Global particle index range for input columns. .. rubric:: 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). .. py:function:: 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. .. py:function:: compute_idx_lookups(lmax, particle_number) Create the (particle, n, tau, l, m) lookup table. .. rubric:: Notes YASF flattens the unknown vector by stacking all vector-spherical-wave-function (VSWF) coefficients for each particle. For a fixed particle ``s`` we enumerate the modes using ``(tau, l, m)`` with ``tau in {1, 2}``, ``l = 1..lmax``, and ``m = -l..l``. The per-particle mode index ``n`` follows the CELES ordering .. math:: n = (\tau - 1)\,l_{\max}(l_{\max}+2) + (l-1)(l+1) + l + m. The global flat index is then ``i = s * nmax + n`` with ``nmax = 2 * lmax * (lmax + 2)``. .. py:function:: 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. .. py:function:: 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. :param lmax: The maximum value of the angular momentum quantum number `l`. It determines the maximum order of the spherical harmonics used in the computation. :type lmax: int :param particles_position: 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. :type particles_position: np.ndarray :param idx: 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: :type idx: np.ndarray :param sfc: A 3-dimensional array representing the scattering form factors. It has dimensions `(s, n, w)`, where: :type sfc: np.ndarray :param k_medium: An array representing the wave number in the medium. It is used in the calculation of the scattered field. :type k_medium: np.ndarray :param azimuthal_angles: An array of azimuthal angles, representing the angles at which the scattered field is computed. :type azimuthal_angles: np.ndarray :param e_r: 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. :type e_r: np.ndarray :param e_phi: 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. :type e_phi: np.ndarray :param e_theta: 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. :type e_theta: np.ndarray :param pilm: 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`. :type pilm: np.ndarray :param taulm: 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. :type taulm: np.ndarray :returns: An array of complex numbers representing the scattered field. :rtype: e_1_sca (np.ndarray) .. py:function:: 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. :param lmax: The maximum value of the angular momentum quantum number `l`. It determines the maximum value of `l` for which the calculations will be performed. :type lmax: int :param particles_position: 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). :type particles_position: np.ndarray :param idx: 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)`. :type idx: np.ndarray :param sfc: A 3-dimensional numpy array representing the scattering form factors. It has dimensions `(s, n, w)`. :type sfc: np.ndarray :param k_medium: The wave vector in the medium. It is a numpy array that contains the wave vector values for different frequencies or wavelengths. :type k_medium: np.ndarray :param azimuthal_angles: 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. :type azimuthal_angles: np.ndarray :param e_r: 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. :type e_r: np.ndarray :param pilm: 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. :type pilm: np.ndarray :param taulm: 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. :type taulm: np.ndarray :returns: The electric field component in the theta direction. e_field_phi (np.ndarray): The electric field component in the phi direction. :rtype: e_field_theta (np.ndarray) .. py:function:: compute_polarization_components(number_of_wavelengths, number_of_angles, e_field_theta, e_field_phi) Compute the polarization components of electromagnetic fields. :param number_of_wavelengths: The number of wavelengths. :type number_of_wavelengths: int :param number_of_angles: The number of angles. :type number_of_angles: int :param e_field_theta: The electric field component in the theta direction. :type e_field_theta: np.ndarray :param e_field_phi: The electric field component in the phi direction. :type e_field_phi: np.ndarray :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. :rtype: degree_of_polarization_tuple (tuple) .. py:function:: 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. :param number_of_wavelengths: The number of wavelengths. :type number_of_wavelengths: int :param number_of_angles: The number of angles. :type number_of_angles: int :param e_phi: The electric field in the phi direction. :type e_phi: np.ndarray :param e_theta: The electric field in the theta direction. :type e_theta: np.ndarray :param e_field_theta: The electric field theta component. :type e_field_theta: np.ndarray :param e_field_phi: The electric field phi component. :type e_field_phi: np.ndarray :returns: The computed radial independent scattered field. :rtype: e_1_sca (np.ndarray) .. py:function:: compute_lookup_tables(lmax, size_parameter, phi, cosine_theta) Compute lookup tables for spherical computations. :param lmax: The maximum degree of the spherical harmonics. :type lmax: int :param size_parameter: Array of size parameters. :type size_parameter: np.ndarray :param phi: Array of azimuthal angles. :type phi: np.ndarray :param cosine_theta: Array of cosine of polar angles. :type cosine_theta: np.ndarray :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. :rtype: spherical_bessel (np.ndarray) .. py:function:: 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.