yasfpy.preconditioner ===================== .. py:module:: yasfpy.preconditioner .. autoapi-nested-parse:: Block-diagonal preconditioner for iterative solvers. This module implements a block-diagonal preconditioner based on spatial partitioning of particles, following the CELES approach. The preconditioner approximates the full system matrix M = I - TW by block-diagonal components, where each block corresponds to particles within a spatial cuboid. Key features: - Spatial partitioning using configurable cuboid sizes - LU factorization of each block - Custom triangular solvers (Numba JIT-compiled) - Significantly reduces iteration count for large systems Classes ------- .. autoapisummary:: yasfpy.preconditioner.BlockDiagonalPreconditioner Functions --------- .. autoapisummary:: yasfpy.preconditioner.sph_bessel yasfpy.preconditioner.make_particle_partition yasfpy.preconditioner.compute_pairwise_geometry yasfpy.preconditioner.build_master_matrix_from_geometry yasfpy.preconditioner.build_master_matrix_from_geometry_wrapper yasfpy.preconditioner.lu_factorize yasfpy.preconditioner.solve_lower_triangular yasfpy.preconditioner.solve_upper_triangular Module Contents --------------- .. py:function:: sph_bessel(nu, l, z) Spherical Bessel or Hankel function. :param nu: Type selector (1=Bessel j_l, 3=Hankel h_l^(1)) :param l: Order :param z: Argument(s) :returns: Spherical Bessel/Hankel function values .. py:class:: BlockDiagonalPreconditioner(partition_edge_sizes) Block-diagonal preconditioner for electromagnetic scattering problems. The preconditioner divides particles into spatial blocks and pre-computes LU factorizations of block-diagonal approximations to the system matrix. This dramatically improves convergence for large particle systems. :ivar partition_edge_sizes: np.ndarray of shape (3,) containing [dx, dy, dz] for cuboid dimensions :ivar partitioning: List of particle index arrays for each block :ivar partitioning_idcs: List of SVWF coefficient index arrays for each block :ivar factorized_matrices: List of dicts containing 'L', 'U', 'P' for each block .. py:attribute:: partition_edge_sizes .. py:attribute:: partitioning :type: Optional[List[numpy.ndarray]] :value: None .. py:attribute:: partitioning_idcs :type: Optional[List[numpy.ndarray]] :value: None .. py:attribute:: factorized_matrices :type: Optional[List[dict]] :value: None .. py:attribute:: log .. py:method:: prepare(simulation) Prepare the preconditioner by computing block partitions and LU factorizations. This is a one-time setup step that: 1. Partitions particles into spatial blocks 2. Builds master matrix M = I - TW for each block 3. Computes LU factorization of each block :param simulation: Simulation object containing parameters, numerics, and tables :returns: Updated simulation object (for chaining) :rtype: simulation .. py:method:: apply(rhs) Apply the preconditioner to a right-hand side vector. Solves M^{-1} @ rhs where M is the block-diagonal approximation. For each block: solves P @ L @ U @ x = rhs via forward/back substitution. :param rhs: Right-hand side vector (complex array) :returns: Preconditioned vector M^{-1} @ rhs :rtype: result .. py:function:: make_particle_partition(positions, edge_sizes) Partition particles into spatial cuboids. Creates a grid of cuboids with specified edge sizes and assigns each particle to a cuboid based on its position. :param positions: Array of shape (N, 3) with particle positions [x, y, z] :param edge_sizes: Array of shape (3,) with cuboid dimensions [dx, dy, dz] :returns: List of arrays, each containing particle indices in one cuboid .. py:function:: compute_pairwise_geometry(positions, k_medium, lmax) Pre-compute all pairwise geometric quantities for a block of particles. This computes distances, angles, Legendre polynomials, and spherical Hankel functions for all particle pairs, which are then used to build the master matrix efficiently. :param positions: Particle positions, shape (N, 3) :param k_medium: Wave number in medium :param lmax: Maximum multipole order :returns: - distances: (N, N) distance matrix - cos_theta: (N, N) cosine of polar angle - sin_theta: (N, N) sine of polar angle - phi: (N, N) azimuthal angle - legendre: dict mapping (p, abs(m)) -> (N, N) Legendre polynomial values - sph_hankel: dict mapping p -> (N, N) spherical Hankel values :rtype: Dictionary containing .. py:function:: build_master_matrix_from_geometry(geometry_dict, mie_coefficients, translation_table, lmax) Build master matrix ``M = I - T W`` using pre-computed geometry. .. rubric:: Notes This function is kept as an API placeholder for a future Numba-friendly implementation. Numba does not support ordinary Python ``dict`` objects in ``nopython`` mode, so the current implementation lives in :func:`build_master_matrix_from_geometry_wrapper`. .. py:function:: build_master_matrix_from_geometry_wrapper(geometry, mie_coefficients, translation_table, lmax) Build the master matrix M = I - TW for a block of particles. This wrapper unpacks pre-computed geometry and builds the matrix. Not JIT-compiled due to dict handling. :param geometry: Pre-computed geometric quantities from compute_pairwise_geometry :param mie_coefficients: Mie coefficients for particles, shape (N_block, nmax) :param translation_table: Translation table ab5 :param lmax: Maximum multipole order :returns: Master matrix of shape (N_block * nmax, N_block * nmax) :rtype: M .. py:function:: lu_factorize(M) Compute LU factorization with partial pivoting: P @ M = L @ U Uses scipy's LU factorization and extracts L, U, and permutation vector P. :param M: Square matrix to factorize :returns: Lower triangular matrix with unit diagonal U: Upper triangular matrix P: Permutation indices (not matrix!) such that M[P, :] = L @ U :rtype: L .. py:function:: solve_lower_triangular(L, b) Solve L @ x = b for lower triangular matrix L. Uses forward substitution algorithm. L must have unit diagonal (from LU). :param L: Lower triangular matrix (n x n) :param b: Right-hand side vector (n,) :returns: Solution vector (n,) :rtype: x .. py:function:: solve_upper_triangular(U, b) Solve U @ x = b for upper triangular matrix U. Uses backward substitution algorithm. :param U: Upper triangular matrix (n x n) :param b: Right-hand side vector (n,) :returns: Solution vector (n,) :rtype: x