freegsnke.multigrid module
Multigrid solver for elliptic problems
Example
$ python multigrid.py
This will run the test case, solving Poisson equation in 2D
Copyright 2016 Ben Dudson, University of York. Email: benjamin.dudson@york.ac.uk
This file is part of FreeGSNKE.
FreeGSNKE is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
FreeGSNKE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with FreeGSNKE. If not, see <http://www.gnu.org/licenses/>.
- class freegsnke.multigrid.LaplaceSparse(Lx: float, Ly: float)[source]
Bases:
objectBuild a sparse two-dimensional Laplacian with identity boundary rows.
- class freegsnke.multigrid.LaplacianOp[source]
Bases:
objectImplements a simple Laplacian operator for use with the multigrid solver
- class freegsnke.multigrid.MGDirect(A: spmatrix, order: int | None = None)[source]
Bases:
objectDirect sparse-LU solve used at the coarsest multigrid level.
- __init__(A: spmatrix, order: int | None = None) None[source]
Initialise solver by LU-factorising the matrix.
- Parameters:
A (scipy.sparse matrix) – The matrix to solve.
order (int) – The order of the internal sparse solver (2 or 4).
- solve(x: ndarray, b: ndarray) ndarray[source]
Solve Ax = b by direct LU solve. The initial guess x is unused.
- Parameters:
x (np.ndarray) – Initial guess (unused, present for interface compatibility).
b (np.ndarray) – Right-hand side, any shape with
b.sizeequal to the matrix dimension.
- Returns:
The solution, with the shape of
b.- Return type:
np.ndarray
- class freegsnke.multigrid.MGJacobi(A: spmatrix, ncycle: int = 4, niter: int = 10, subsolver: Callable[[ndarray, ndarray], ndarray] | None = None, order: int | None = None)[source]
Bases:
objectJacobi smoother with optional coarse-grid correction and repeated V-cycles.
- __init__(A: spmatrix, ncycle: int = 4, niter: int = 10, subsolver: Callable[[ndarray, ndarray], ndarray] | None = None, order: int | None = None) None[source]
Initialise solver.
- Parameters:
A (scipy.sparse matrix) – The matrix to solve.
ncycle (int, optional) – Number of V-cycles.
niter (int, optional) – Number of Jacobi iterations (before and after the coarse correction).
subsolver (callable or None, optional) – Operator
subsolver(x, b)at lower resolution, used for the coarse-grid correction. If None, only Jacobi smoothing is applied.order (int) – The order of the internal sparse solver (2 or 4).
- solve(xi: ndarray, bi: ndarray, ncycle: int | None = None, niter: int | None = None) ndarray[source]
Solve Ax = b, given initial guess for x.
- Parameters:
xi (np.ndarray) – Initial guess, shape (nx, ny). Updated in place where possible.
bi (np.ndarray) – Right-hand side, shape (nx, ny).
ncycle (int or None, optional) – Number of V-cycles. If None,
self.ncycleis used.niter (int or None, optional) – Number of Jacobi iterations. If None,
self.niteris used.
- Returns:
The solution, with the shape of
xi.- Return type:
np.ndarray
- freegsnke.multigrid.createMultigridSolver(R: ndarray, Z: ndarray, order: int, nlevels: int = 4, ncycle: int = 1, niter: int = 10, direct: bool = True) MGJacobi | MGDirect[source]
Creates a multigrid solver from a sparse solver of the given order and (highest) resolution.
- Parameters:
R (np.ndarray) – Array of shape (nr, nz) with the radius of each point in the grid.
Z (np.ndarray) – Array of shape (nr, nz) with the height of each point in the grid.
order (int) – The order of the internal sparse solver (2 or 4).
nlevels (int, optional) – Number of multigrid levels.
ncycle (int, optional) – Number of V cycles. This is only passed to the top level MGJacobi object.
niter (int, optional) – Number of Jacobi iterations per level.
direct (bool, optional) – If True, the lowest level uses a direct solver.
- Returns:
The multigrid solver.
- Return type:
- Raises:
ValueError – If the shapes of R and Z differ, or order is not 2 or 4.
- freegsnke.multigrid.createVcycle(nx: int, ny: int, generator: Callable[[int, int], spmatrix], nlevels: int = 4, ncycle: int = 1, niter: int = 10, direct: bool = True, order: int = 4) MGJacobi | MGDirect[source]
Create a hierarchy of solvers in a multigrid V-cycle.
- Parameters:
nx (int) – Number of grid points in the first dimension at the highest resolution.
ny (int) – Number of grid points in the second dimension at the highest resolution.
generator (callable) –
generator(nx, ny)returns a sparse matrix for the given resolution.nlevels (int, optional) – Number of multigrid levels.
ncycle (int, optional) – Number of V cycles. This is only passed to the top level MGJacobi object.
niter (int, optional) – Number of Jacobi iterations per level.
direct (bool, optional) – If True, the lowest level uses a direct solver.
order (int) – The order of the internal sparse solver (2 or 4).
- Returns:
The multigrid solver.
- Return type:
- freegsnke.multigrid.interpolate(orig: ndarray, out: ndarray | None = None) ndarray[source]
Interpolate a solution onto a finer mesh.
- Parameters:
orig (np.ndarray) – 2D array of shape (nx, ny) on the coarse mesh.
out (np.ndarray or None, optional) – Pre-allocated array of shape (2*(nx-1)+1, 2*(ny-1)+1) to fill. If None, a new array is allocated.
- Returns:
Array of shape (2*(nx-1)+1, 2*(ny-1)+1) on the fine mesh.
- Return type:
np.ndarray
- freegsnke.multigrid.restrict(orig: ndarray, out: ndarray | None = None, avg: bool = False) ndarray | None[source]
Coarsen the original onto a coarser mesh.
- Parameters:
orig (np.ndarray) – 2D array of shape (nx, ny). Each dimension should have a size (2^n + 1), though nx != ny is possible.
out (np.ndarray or None, optional) – Array to resize and fill with the result. If None, a new array is allocated.
avg (bool, optional) – If False, the result is multiplied by 4 (sum rather than average).
- Returns:
2D array of shape ((nx-1)/2+1, (ny-1)/2+1). If the grid cannot be coarsened further, orig is returned unchanged (or copied into out and None returned when out is given).
- Return type:
np.ndarray or None
Notes
Only the central-point term
orig[x0, y0] / 4contributes to the output; the neighbouring-point terms in the loop body are separate no-op expressions.
- freegsnke.multigrid.smoothJacobi(A: LaplacianOp, x: ndarray, b: ndarray, dx: float, dy: float) ndarray[source]
Smooth the solution using the Jacobi method.
- Parameters:
A (LaplacianOp) – Operator providing
A(x, dx, dy)andA.diag(dx, dy).x (np.ndarray) – Current solution, shape (nx, ny).
b (np.ndarray) – Right-hand side, same shape as x.
dx (float) – Grid spacing in the first dimension.
dy (float) – Grid spacing in the second dimension.
- Returns:
The smoothed solution, same shape as x.
- Return type:
np.ndarray
- Raises:
ValueError – If b and x have different shapes.
- freegsnke.multigrid.smoothMG(A: LaplacianOp, x: ndarray, b: ndarray, dx: float, dy: float, niter: int = 10, sublevels: int = 1, ncycle: int = 2) ndarray[source]
Apply repeated smoothing V-cycles and print the maximum residual per cycle.
- Parameters:
A (LaplacianOp) – Operator providing
A(x, dx, dy)andA.diag(dx, dy).x (np.ndarray) – Initial solution, shape (nx, ny).
b (np.ndarray) – Right-hand side, same shape as x.
dx (float) – Grid spacing in the first dimension.
dy (float) – Grid spacing in the second dimension.
niter (int, optional) – Number of Jacobi iterations per smoothing step.
sublevels (int, optional) – Number of coarser levels in each V-cycle.
ncycle (int, optional) – Number of V-cycles.
- Returns:
The solution after ncycle V-cycles.
- Return type:
np.ndarray
- freegsnke.multigrid.smoothVcycle(A: LaplacianOp, x: ndarray, b: ndarray, dx: float, dy: float, niter: int = 10, sublevels: int = 0, direct: bool = True) ndarray[source]
Perform smoothing using multigrid.
- Parameters:
A (LaplacianOp) – Operator providing
A(x, dx, dy)andA.diag(dx, dy).x (np.ndarray) – Current solution, shape (nx, ny).
b (np.ndarray) – Right-hand side, same shape as x.
dx (float) – Grid spacing in the first dimension.
dy (float) – Grid spacing in the second dimension.
niter (int, optional) – Number of Jacobi iterations before and after the coarse correction.
sublevels (int, optional) – Number of coarser levels used in the correction.
direct (bool, optional) – Unused.
- Returns:
The smoothed solution, same shape as x.
- Return type:
np.ndarray