2016-08-04 22:46:02 -07:00
|
|
|
"""
|
2019-11-24 23:47:31 -08:00
|
|
|
Solvers and solver interface for FDFD problems.
|
2016-08-04 22:46:02 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
from typing import List, Callable, Dict, Any
|
2017-05-20 21:23:18 -07:00
|
|
|
import logging
|
2016-08-04 22:46:02 -07:00
|
|
|
|
|
|
|
import numpy
|
|
|
|
from numpy.linalg import norm
|
|
|
|
import scipy.sparse.linalg
|
|
|
|
|
2019-11-27 22:59:52 -08:00
|
|
|
from ..fdmath import dx_lists_t, vfdfield_t
|
2016-08-04 22:46:02 -07:00
|
|
|
from . import operators
|
|
|
|
|
|
|
|
|
2017-05-20 21:23:18 -07:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-08-04 22:46:02 -07:00
|
|
|
def _scipy_qmr(A: scipy.sparse.csr_matrix,
|
2016-10-31 18:43:01 -07:00
|
|
|
b: numpy.ndarray,
|
|
|
|
**kwargs
|
|
|
|
) -> numpy.ndarray:
|
2016-08-04 22:46:02 -07:00
|
|
|
"""
|
|
|
|
Wrapper for scipy.sparse.linalg.qmr
|
|
|
|
|
2019-11-24 23:47:31 -08:00
|
|
|
Args:
|
|
|
|
A: Sparse matrix
|
|
|
|
b: Right-hand-side vector
|
|
|
|
kwargs: Passed as **kwargs to the wrapped function
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Guess for solution (returned even if didn't converge)
|
2016-08-04 22:46:02 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
'''
|
|
|
|
Report on our progress
|
|
|
|
'''
|
|
|
|
iter = 0
|
|
|
|
|
2017-05-20 21:23:18 -07:00
|
|
|
def log_residual(xk):
|
2016-08-04 22:46:02 -07:00
|
|
|
nonlocal iter
|
|
|
|
iter += 1
|
|
|
|
if iter % 100 == 0:
|
2017-05-20 21:23:18 -07:00
|
|
|
logger.info('Solver residual at iteration {} : {}'.format(iter, norm(A @ xk - b)))
|
2016-08-04 22:46:02 -07:00
|
|
|
|
|
|
|
if 'callback' in kwargs:
|
|
|
|
def augmented_callback(xk):
|
2017-05-20 21:23:18 -07:00
|
|
|
log_residual(xk)
|
2016-08-04 22:46:02 -07:00
|
|
|
kwargs['callback'](xk)
|
|
|
|
|
|
|
|
kwargs['callback'] = augmented_callback
|
|
|
|
else:
|
2017-05-20 21:23:18 -07:00
|
|
|
kwargs['callback'] = log_residual
|
2016-08-04 22:46:02 -07:00
|
|
|
|
|
|
|
'''
|
|
|
|
Run the actual solve
|
|
|
|
'''
|
|
|
|
|
|
|
|
x, _ = scipy.sparse.linalg.qmr(A, b, **kwargs)
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
def generic(omega: complex,
|
2019-11-27 22:59:52 -08:00
|
|
|
dxes: dx_lists_t,
|
|
|
|
J: vfdfield_t,
|
|
|
|
epsilon: vfdfield_t,
|
|
|
|
mu: vfdfield_t = None,
|
|
|
|
pec: vfdfield_t = None,
|
|
|
|
pmc: vfdfield_t = None,
|
2016-08-04 22:46:02 -07:00
|
|
|
adjoint: bool = False,
|
|
|
|
matrix_solver: Callable[..., numpy.ndarray] = _scipy_qmr,
|
|
|
|
matrix_solver_opts: Dict[str, Any] = None,
|
2019-11-27 22:59:52 -08:00
|
|
|
) -> vfdfield_t:
|
2016-08-04 22:46:02 -07:00
|
|
|
"""
|
|
|
|
Conjugate gradient FDFD solver using CSR sparse matrices.
|
|
|
|
|
2019-11-24 23:47:31 -08:00
|
|
|
All ndarray arguments should be 1D arrays, as returned by `meanas.vec()`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
omega: Complex frequency to solve at.
|
|
|
|
dxes: `[[dx_e, dy_e, dz_e], [dx_h, dy_h, dz_h]]` (complex cell sizes) as
|
2019-11-27 22:59:52 -08:00
|
|
|
discussed in `meanas.fdmath.types`
|
2019-11-24 23:47:31 -08:00
|
|
|
J: Electric current distribution (at E-field locations)
|
|
|
|
epsilon: Dielectric constant distribution (at E-field locations)
|
|
|
|
mu: Magnetic permeability distribution (at H-field locations)
|
|
|
|
pec: Perfect electric conductor distribution
|
|
|
|
(at E-field locations; non-zero value indicates PEC is present)
|
|
|
|
pmc: Perfect magnetic conductor distribution
|
|
|
|
(at H-field locations; non-zero value indicates PMC is present)
|
|
|
|
adjoint: If true, solves the adjoint problem.
|
|
|
|
matrix_solver: Called as `matrix_solver(A, b, **matrix_solver_opts) -> x`,
|
|
|
|
where `A`: `scipy.sparse.csr_matrix`;
|
|
|
|
`b`: `numpy.ndarray`;
|
|
|
|
`x`: `numpy.ndarray`;
|
|
|
|
Default is a wrapped version of `scipy.sparse.linalg.qmr()`
|
|
|
|
which doesn't return convergence info and logs the residual
|
|
|
|
every 100 iterations.
|
|
|
|
matrix_solver_opts: Passed as kwargs to `matrix_solver(...)`
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
E-field which solves the system.
|
2016-08-04 22:46:02 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
if matrix_solver_opts is None:
|
|
|
|
matrix_solver_opts = dict()
|
|
|
|
|
|
|
|
b0 = -1j * omega * J
|
|
|
|
A0 = operators.e_full(omega, dxes, epsilon=epsilon, mu=mu, pec=pec, pmc=pmc)
|
|
|
|
|
|
|
|
Pl, Pr = operators.e_full_preconditioners(dxes)
|
|
|
|
|
|
|
|
if adjoint:
|
|
|
|
A = (Pl @ A0 @ Pr).H
|
|
|
|
b = Pr.H @ b0
|
|
|
|
else:
|
|
|
|
A = Pl @ A0 @ Pr
|
|
|
|
b = Pl @ b0
|
|
|
|
|
|
|
|
x = matrix_solver(A.tocsr(), b, **matrix_solver_opts)
|
|
|
|
|
|
|
|
if adjoint:
|
|
|
|
x0 = Pl.H @ x
|
|
|
|
else:
|
|
|
|
x0 = Pr @ x
|
|
|
|
|
|
|
|
return x0
|