yasfpy.preconditioner#
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#
Block-diagonal preconditioner for electromagnetic scattering problems. |
Functions#
|
Spherical Bessel or Hankel function. |
|
Partition particles into spatial cuboids. |
|
Pre-compute all pairwise geometric quantities for a block of particles. |
|
Build master matrix |
|
Build the master matrix M = I - TW for a block of particles. |
|
Compute LU factorization with partial pivoting: P @ M = L @ U |
|
Solve L @ x = b for lower triangular matrix L. |
|
Solve U @ x = b for upper triangular matrix U. |
Module Contents#
- yasfpy.preconditioner.sph_bessel(nu, l, z)#
Spherical Bessel or Hankel function.
- Parameters:
nu – Type selector (1=Bessel j_l, 3=Hankel h_l^(1))
l – Order
z – Argument(s)
- Returns:
Spherical Bessel/Hankel function values
- class yasfpy.preconditioner.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.
- Variables:
partition_edge_sizes – np.ndarray of shape (3,) containing [dx, dy, dz] for cuboid dimensions
partitioning – List of particle index arrays for each block
partitioning_idcs – List of SVWF coefficient index arrays for each block
factorized_matrices – List of dicts containing ‘L’, ‘U’, ‘P’ for each block
- partition_edge_sizes#
- partitioning: List[numpy.ndarray] | None = None#
- partitioning_idcs: List[numpy.ndarray] | None = None#
- log#
- 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
- Parameters:
simulation – Simulation object containing parameters, numerics, and tables
- Returns:
Updated simulation object (for chaining)
- Return type:
simulation
- 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.
- Parameters:
rhs – Right-hand side vector (complex array)
- Returns:
Preconditioned vector M^{-1} @ rhs
- Return type:
result
- yasfpy.preconditioner.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.
- Parameters:
positions – Array of shape (N, 3) with particle positions [x, y, z]
edge_sizes – Array of shape (3,) with cuboid dimensions [dx, dy, dz]
- Returns:
List of arrays, each containing particle indices in one cuboid
- yasfpy.preconditioner.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.
- Parameters:
positions – Particle positions, shape (N, 3)
k_medium – Wave number in medium
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
- Return type:
Dictionary containing
- yasfpy.preconditioner.build_master_matrix_from_geometry(geometry_dict, mie_coefficients, translation_table, lmax)#
Build master matrix
M = I - T Wusing pre-computed geometry.Notes
This function is kept as an API placeholder for a future Numba-friendly implementation. Numba does not support ordinary Python
dictobjects innopythonmode, so the current implementation lives inbuild_master_matrix_from_geometry_wrapper().
- yasfpy.preconditioner.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.
- Parameters:
geometry – Pre-computed geometric quantities from compute_pairwise_geometry
mie_coefficients – Mie coefficients for particles, shape (N_block, nmax)
translation_table – Translation table ab5
lmax – Maximum multipole order
- Returns:
Master matrix of shape (N_block * nmax, N_block * nmax)
- Return type:
M
- yasfpy.preconditioner.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.
- Parameters:
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
- Return type:
L
- yasfpy.preconditioner.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).
- Parameters:
L – Lower triangular matrix (n x n)
b – Right-hand side vector (n,)
- Returns:
Solution vector (n,)
- Return type:
x
- yasfpy.preconditioner.solve_upper_triangular(U, b)#
Solve U @ x = b for upper triangular matrix U.
Uses backward substitution algorithm.
- Parameters:
U – Upper triangular matrix (n x n)
b – Right-hand side vector (n,)
- Returns:
Solution vector (n,)
- Return type:
x