This page contains the Gallery Package documentation.
Basic PyAMG demo showing AMG standalone convergence versus preconditioned CG with AMG
Generate a diffusion stencil
Supports isotropic diffusion (FE,FD), anisotropic diffusion (FE, FD), and rotated anisotropic diffusion (FD).
The stencils include redundancy to maintain readability for simple cases (e.g. isotropic diffusion).
-div Q A Q^T grad u
Parameters : | epsilon : float, optional
theta : float, optional
type : {‘FE’,’FD’}
|
---|---|
Returns : | stencil : numpy array
|
See also
stencil_grid, poisson
Notes
Not all combinations are supported.
Examples
>>> import scipy
>>> from pyamg.gallery.diffusion import diffusion_stencil_2d
>>> sten = diffusion_stencil_2d(epsilon=0.0001,theta=scipy.pi/6,type='FD')
>>> print sten
[[-0.2164847 -0.750025 0.2164847]
[-0.250075 2.0002 -0.250075 ]
[ 0.2164847 -0.750025 -0.2164847]]
Constructs linear elasticity problems for first-order elements in 2D and 3D
Linear elasticity problem discretizes with Q1 finite elements on a regular rectangular grid
Parameters : | grid : tuple
spacing : tuple
E : float
nu : float
format : string
|
---|---|
Returns : | A : {csr_matrix}
|
See also
Notes
References
[R16] | J. Alberty, C. Carstensen, S. A. Funken, and R. KloseDOI “Matlab implementation of the finite element method in elasticity” Computing, Volume 69, Issue 3 (November 2002) Pages: 239 - 263 http://www.math.hu-berlin.de/~cc/ |
Examples
>>> from pyamg.gallery import linear_elasticity
>>> A,B = linear_elasticity((4,4))
P1 elements in 2 or 3 dimensions
Parameters : | vertices : array_like
elements : array_like
E : float
nu : float
format : string
|
---|---|
Returns : | A : {csr_matrix}
|
Notes
References
[R17] | J. Alberty, C. Carstensen, S. A. Funken, and R. KloseDOI “Matlab implementation of the finite element method in elasticity” Computing, Volume 69, Issue 3 (November 2002) Pages: 239 - 263 http://www.math.hu-berlin.de/~cc/ |
Examples
>>> from pyamg.gallery import linear_elasticity_p1
>>> E = array([[0,1,2],[1,3,2]])
>>> V = array([[0.0,0.0],[1.0,0.0],[0.0,1.0],[1.0,1.0]])
>>> A,B = linear_elasticity_p1(V,E)
Examples stored in files
Load an example problem by name
Parameters : | name : string (e.g. ‘airfoil’)
|
---|
Notes
Examples
>>> from pyamg.gallery import load_example
>>> ex = load_example('knot')
Discretizations of the Poisson problem
Returns a sparse matrix for the N-dimensional Poisson problem
The matrix represents a finite Difference approximation to the Poisson problem on a regular n-dimensional grid with unit grid spacing and Dirichlet boundary conditions.
Parameters : | grid : tuple of integers
|
---|
Notes
The matrix is symmetric and positive definite (SPD).
Examples
>>> # 4 nodes in one dimension
>>> poisson( (4,) ).todense()
matrix([[ 2., -1., 0., 0.],
[-1., 2., -1., 0.],
[ 0., -1., 2., -1.],
[ 0., 0., -1., 2.]])
>>> # rectangular two dimensional grid
>>> poisson( (2,3) ).todense()
matrix([[ 4., -1., 0., -1., 0., 0.],
[-1., 4., -1., 0., -1., 0.],
[ 0., -1., 4., 0., 0., -1.],
[-1., 0., 0., 4., -1., 0.],
[ 0., -1., 0., -1., 4., -1.],
[ 0., 0., -1., 0., -1., 4.]])
Construct a Gauge Laplacian from Quantum Chromodynamics for regular 2D grids
Note that this function is not written efficiently, but should be fine for N x N grids where N is in the low hundreds.
Parameters : | npts : {int}
spacing : {float}
beta : {float}
|
---|---|
Returns : | A : {csr matrix}
|
References
[R18] | MacLachlan, S. and Oosterlee, C., “Algebraic Multigrid Solvers for Complex-Valued Matrices”, Vol. 30, SIAM J. Sci. Comp, 2008 |
Examples
>>> A = gauge_laplacian(10)
Generates simple meshes
Construct a regular triangular mesh in the unit square
Parameters : | nx : int
ny : int
|
---|---|
Returns : | Vert : array
E2V : array
|
Examples
>>> E2V,Vert = regular_triangle_mesh(3, 2)
Random sparse matrices
Returns a random sparse matrix.
Parameters : | m, n : int
density : float
format : string
|
---|---|
Returns : | A : sparse matrix
|
Examples
>>> import numpy
>>> A = sprand(5,5,3/5.0)
Construct sparse matrix from a local stencil
Construct a sparse matrix form a local matrix stencil
Parameters : | S : ndarray
grid : tuple
dtype : :
format : string
|
---|---|
Returns : | A : sparse matrix
|
Notes
The grid vertices are enumerated as arange(prod(grid)).reshape(grid). This implies that the last grid dimension cycles fastest, while the first dimension cycles slowest. For example, if grid=(2,3) then the grid vertices are ordered as (0,0), (0,1), (0,2), (1,0), (1,1), (1,2).
This coincides with the ordering used by the NumPy functions ndenumerate() and mgrid().
Examples
>>> stencil = [-1,2,-1] # 1D Poisson stencil
>>> grid = (5,) # 1D grid with 5 vertices
>>> A = stencil_grid(stencil, grid, dtype=float, format='csr')
>>> A.todense()
matrix([[ 2., -1., 0., 0., 0.],
[-1., 2., -1., 0., 0.],
[ 0., -1., 2., -1., 0.],
[ 0., 0., -1., 2., -1.],
[ 0., 0., 0., -1., 2.]])
>>> stencil = [[0,-1,0],[-1,4,-1],[0,-1,0]] # 2D Poisson stencil
>>> grid = (3,3) # 2D grid with shape 3x3
>>> A = stencil_grid(stencil, grid, dtype=float, format='csr')
>>> A.todense()
matrix([[ 4., -1., 0., -1., 0., 0., 0., 0., 0.],
[-1., 4., -1., 0., -1., 0., 0., 0., 0.],
[ 0., -1., 4., 0., 0., -1., 0., 0., 0.],
[-1., 0., 0., 4., -1., 0., -1., 0., 0.],
[ 0., -1., 0., -1., 4., -1., 0., -1., 0.],
[ 0., 0., -1., 0., -1., 4., 0., 0., -1.],
[ 0., 0., 0., -1., 0., 0., 4., -1., 0.],
[ 0., 0., 0., 0., -1., 0., -1., 4., -1.],
[ 0., 0., 0., 0., 0., -1., 0., -1., 4.]])