PyAMG: Algebraic Multigrid Solvers in Python
PyAMG is a Python library providing implementations of Algebraic Multigrid (AMG) solvers and supporting tools for approximating solutions to large, sparse linear systems of algebraic equations (Ax=b). It is actively maintained, with the current version being 5.3.0, and receives regular updates to support newer Python and SciPy versions and introduce new features.
Common errors
-
ModuleNotFoundError: No module named 'pyamg.amg_core.evolution_strength'
cause This error often indicates an incomplete or incorrect installation of PyAMG, particularly if installed in isolated or non-standard Python environments (e.g., custom Spyder installers or virtual environments without proper build tools). The underlying C++ extensions (`amg_core`) were not compiled or linked correctly.fixReinstall PyAMG in a clean virtual environment using `pip install pyamg`. Ensure `numpy` and `scipy` are installed first. If issues persist, try installing from source (refer to PyAMG's GitHub for build instructions). For Anaconda users, `conda install -c anaconda pyamg` is an alternative. -
Error in TokenizeString(): two or more options before an '=' sign in the configuration file.
cause This error is typically encountered when PyAMG is integrated with external software like SU2 for mesh adaptation, indicating a syntax error in the configuration file used by the calling application. It implies incorrect parsing of input parameters or missing required options (e.g., `PYADAP_COMPLEXITY`).fixCarefully review the configuration file (e.g., `.cfg` file for SU2) for any malformed lines, duplicate options, or missing parameters required by the PyAMG integration. Ensure all options are correctly formatted (e.g., `OPTION = VALUE`). Refer to the specific integration's documentation for required parameters. -
MemoryError: Unable to allocate ...
cause PyAMG solvers can consume significant memory for large-scale problems, especially when generating coarse grids. This error occurs when the system runs out of available memory.fixFor `smoothed_aggregation_solver`, try reducing the `max_levels` parameter to limit the number of coarse grids, or increasing `max_coarse` to allow coarser grids to be larger, reducing the total memory footprint. Using the CSR matrix format is also recommended for efficiency.
Warnings
- breaking In PyAMG v5.3.0, the internal matrix storage naming convention changed from `[bc]sr_matrix` to `[bc]sr_array`. Additionally, the matrix-vector multiplication operator should now use `@` instead of `*` for NumPy compatibility.
- breaking Starting from PyAMG v5.1.0, the minimum required Python version is 3.8 and the minimum required SciPy version is 1.8.0. Codebases using older versions of Python or SciPy may encounter compatibility issues.
- gotcha For optimal performance, especially with iterative solvers, it's best practice to create the `MultilevelSolver` object once and then call its `solve()` method multiple times for different right-hand sides, rather than re-creating the solver for each solve operation.
Install
-
pip install pyamg
Imports
- pyamg
import pyamg
- smoothed_aggregation_solver
from pyamg import smoothed_aggregation_solver
- gallery
import pyamg.gallery
from pyamg import gallery
Quickstart
import numpy as np
from scipy.sparse import csr_matrix
from pyamg import gallery
from pyamg import smoothed_aggregation_solver
# 1. Create a sample sparse matrix (e.g., 2D Poisson problem)
A = gallery.poisson((100, 100), format='csr')
# 2. Create a right-hand side vector
b = np.random.rand(A.shape[0])
# 3. Construct the Algebraic Multigrid solver
# max_coarse controls the size of the coarsest grid
ml = smoothed_aggregation_solver(A, max_coarse=10)
# 4. Solve the linear system Ax = b
x = ml.solve(b, tol=1e-10)
print(f"Matrix shape: {A.shape}")
print(f"Number of nonzeros: {A.nnz}")
print(f"Solution vector norm: {np.linalg.norm(x)}")
print(f"Residual norm: {np.linalg.norm(b - A @ x)}")