gallery Package¶
gallery Package¶
Matrix Gallery for Multigrid Solvers
Functions¶
- poisson() : Poisson problem using Finite Differences
- linear_elasticity() : Linear Elasticity using Finite Elements
- stencil_grid() : General stencil generation from 1D, 2D, and 3D
- diffusion_stencil_2d() : 2D rotated anisotropic FE/FD stencil
- pyamg.gallery.demo()¶
- pyamg.gallery.diffusion_stencil_2d(epsilon=1.0, theta=0.0, type='FE')¶
Rotated Anisotropic diffusion in 2d of the form:
-div Q A Q^T grad u
- Q = [cos(theta) -sin(theta)]
- [sin(theta) cos(theta)]
- A = [1 0 ]
- [0 eps ]
- epsilon : float, optional
- Anisotropic diffusion coefficient: -div A grad u, where A = [1 0; 0 epsilon]. The default is isotropic, epsilon=1.0
- theta : float, optional
- Rotation angle theta in radians defines -div Q A Q^T grad, where Q = [cos(theta) -sin(theta); sin(theta) cos(theta)].
- type : {‘FE’,’FD’}
- Specifies the discretization as Q1 finite element (FE) or 2nd order finite difference (FD) The default is theta = 0.0
- stencil : numpy array
- A 3x3 diffusion stencil
stencil_grid, poisson
Not all combinations are supported.
>>> 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]]
- pyamg.gallery.gauge_laplacian(npts, spacing=1.0, beta=0.1)¶
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.
- npts : {int}
- number of pts in x and y directions
- spacing : {float}
- grid spacing between points
- beta : {float}
- temperature Note that if beta=0, then we get the typical 5pt Laplacian stencil
- A : {csr matrix}
- A is Hermitian positive definite for beta > 0.0 A is Symmetric semi-definite for beta = 0.0
>>> from pyamg.gallery import gauge_laplacian >>> A = gauge_laplacian(10)
[1] MacLachlan, S. and Oosterlee, C., “Algebraic Multigrid Solvers for Complex-Valued Matrices”, Vol. 30, SIAM J. Sci. Comp, 2008
- pyamg.gallery.linear_elasticity(grid, spacing=None, E=100000.0, nu=0.3, format=None)¶
Linear elasticity problem discretizes with Q1 finite elements on a regular rectangular grid
- grid : tuple
- length 2 tuple of grid sizes, e.g. (10, 10)
- spacing : tuple
- length 2 tuple of grid spacings, e.g. (1.0, 0.1)
- E : float
- Young’s modulus
- nu : float
- Poisson’s ratio
- format : string
- Format of the returned sparse matrix (eg. ‘csr’, ‘bsr’, etc.)
- A : {csr_matrix}
- FE Q1 stiffness matrix
linear_elasticity_p1
- only 2d for now
>>> from pyamg.gallery import linear_elasticity >>> A, B = linear_elasticity((4, 4))
[1] 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/
- pyamg.gallery.linear_elasticity_p1(vertices, elements, E=100000.0, nu=0.3, format=None)¶
P1 elements in 2 or 3 dimensions
- vertices : array_like
- array of vertices of a triangle or tets
- elements : array_like
- array of vertex indices for tri or tet elements
- E : float
- Young’s modulus
- nu : float
- Poisson’s ratio
- format : string
- ‘csr’, ‘csc’, ‘coo’, ‘bsr’
- A : {csr_matrix}
- FE Q1 stiffness matrix
- works in both 2d and in 3d
>>> from pyamg.gallery import linear_elasticity_p1 >>> from numpy import array >>> 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)
[1] 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/
- pyamg.gallery.load_example(name)¶
Load an example problem by name
- name : string (e.g. ‘airfoil’)
- Name of the example to load
- Each example is stored in a dictionary with the following keys:
- ‘A’ : sparse matrix
- ‘B’ : near-nullspace candidates
- ‘vertices’ : dense array of nodal coordinates
- ‘elements’ : dense array of element indices
- Current example names are:
- airfoil bar helmholtz_2D knot local_disc_galerkin_diffusion recirc_flow unit_cube unit_square
>>> from pyamg.gallery import load_example >>> ex = load_example('knot')
- pyamg.gallery.poisson(grid, spacing=None, dtype=<type 'float'>, format=None)¶
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.
- grid : tuple of integers
- grid dimensions e.g. (100,100)
The matrix is symmetric and positive definite (SPD).
>>> from pyamg.gallery import poisson >>> # 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.]])
- pyamg.gallery.regular_triangle_mesh(nx, ny)¶
Construct a regular triangular mesh in the unit square
- nx : int
- Number of nodes in the x-direction
- ny : int
- Number of nodes in the y-direction
- Vert : array
- nx*ny x 2 vertex list
- E2V : array
- Nex x 3 element list
>>> from pyamg.gallery import regular_triangle_mesh >>> E2V,Vert = regular_triangle_mesh(3, 2)
- pyamg.gallery.sprand(m, n, density, format='csr')¶
Returns a random sparse matrix.
- m, n : int
- shape of the result
- density : float
- target a matrix with nnz(A) = m*n*density, 0<=density<=1
- format : string
- sparse matrix format to return, e.g. ‘csr’, ‘coo’, etc.
- A : sparse matrix
- m x n sparse matrix
>>> from pyamg.gallery import sprand >>> A = sprand(5,5,3/5.0)
- pyamg.gallery.stencil_grid(S, grid, dtype=None, format=None)¶
Construct a sparse matrix form a local matrix stencil
- S : ndarray
- matrix stencil stored in N-d array
- grid : tuple
- tuple containing the N grid dimensions
- dtype :
- data type of the result
- format : string
- sparse matrix format to return, e.g. “csr”, “coo”, etc.
- A : sparse matrix
- Sparse matrix which represents the operator given by applying stencil S at each vertex of a regular grid with given dimensions.
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().
>>> from pyamg.gallery import stencil_grid >>> 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.]])
demo Module¶
Basic PyAMG demo showing AMG standalone convergence versus preconditioned CG with AMG
diffusion Module¶
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).
- pyamg.gallery.diffusion.diffusion_stencil_2d(epsilon=1.0, theta=0.0, type='FE')[source]¶
Rotated Anisotropic diffusion in 2d of the form:
-div Q A Q^T grad u
- Q = [cos(theta) -sin(theta)]
- [sin(theta) cos(theta)]
- A = [1 0 ]
- [0 eps ]
- epsilon : float, optional
- Anisotropic diffusion coefficient: -div A grad u, where A = [1 0; 0 epsilon]. The default is isotropic, epsilon=1.0
- theta : float, optional
- Rotation angle theta in radians defines -div Q A Q^T grad, where Q = [cos(theta) -sin(theta); sin(theta) cos(theta)].
- type : {‘FE’,’FD’}
- Specifies the discretization as Q1 finite element (FE) or 2nd order finite difference (FD) The default is theta = 0.0
- stencil : numpy array
- A 3x3 diffusion stencil
stencil_grid, poisson
Not all combinations are supported.
>>> 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]]
elasticity Module¶
Constructs linear elasticity problems for first-order elements in 2D and 3D
- pyamg.gallery.elasticity.linear_elasticity(grid, spacing=None, E=100000.0, nu=0.3, format=None)[source]¶
Linear elasticity problem discretizes with Q1 finite elements on a regular rectangular grid
- grid : tuple
- length 2 tuple of grid sizes, e.g. (10, 10)
- spacing : tuple
- length 2 tuple of grid spacings, e.g. (1.0, 0.1)
- E : float
- Young’s modulus
- nu : float
- Poisson’s ratio
- format : string
- Format of the returned sparse matrix (eg. ‘csr’, ‘bsr’, etc.)
- A : {csr_matrix}
- FE Q1 stiffness matrix
linear_elasticity_p1
- only 2d for now
>>> from pyamg.gallery import linear_elasticity >>> A, B = linear_elasticity((4, 4))
[1] 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/
- pyamg.gallery.elasticity.linear_elasticity_p1(vertices, elements, E=100000.0, nu=0.3, format=None)[source]¶
P1 elements in 2 or 3 dimensions
- vertices : array_like
- array of vertices of a triangle or tets
- elements : array_like
- array of vertex indices for tri or tet elements
- E : float
- Young’s modulus
- nu : float
- Poisson’s ratio
- format : string
- ‘csr’, ‘csc’, ‘coo’, ‘bsr’
- A : {csr_matrix}
- FE Q1 stiffness matrix
- works in both 2d and in 3d
>>> from pyamg.gallery import linear_elasticity_p1 >>> from numpy import array >>> 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)
[1] 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/
example Module¶
Examples stored in files
- pyamg.gallery.example.load_example(name)[source]¶
Load an example problem by name
- name : string (e.g. ‘airfoil’)
- Name of the example to load
- Each example is stored in a dictionary with the following keys:
- ‘A’ : sparse matrix
- ‘B’ : near-nullspace candidates
- ‘vertices’ : dense array of nodal coordinates
- ‘elements’ : dense array of element indices
- Current example names are:
- airfoil bar helmholtz_2D knot local_disc_galerkin_diffusion recirc_flow unit_cube unit_square
>>> from pyamg.gallery import load_example >>> ex = load_example('knot')
info Module¶
Matrix Gallery for Multigrid Solvers
Functions¶
- poisson() : Poisson problem using Finite Differences
- linear_elasticity() : Linear Elasticity using Finite Elements
- stencil_grid() : General stencil generation from 1D, 2D, and 3D
- diffusion_stencil_2d() : 2D rotated anisotropic FE/FD stencil
laplacian Module¶
Discretizations of the Poisson problem
- pyamg.gallery.laplacian.poisson(grid, spacing=None, dtype=<type 'float'>, format=None)[source]¶
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.
- grid : tuple of integers
- grid dimensions e.g. (100,100)
The matrix is symmetric and positive definite (SPD).
>>> from pyamg.gallery import poisson >>> # 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.]])
- pyamg.gallery.laplacian.gauge_laplacian(npts, spacing=1.0, beta=0.1)[source]¶
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.
- npts : {int}
- number of pts in x and y directions
- spacing : {float}
- grid spacing between points
- beta : {float}
- temperature Note that if beta=0, then we get the typical 5pt Laplacian stencil
- A : {csr matrix}
- A is Hermitian positive definite for beta > 0.0 A is Symmetric semi-definite for beta = 0.0
>>> from pyamg.gallery import gauge_laplacian >>> A = gauge_laplacian(10)
[1] MacLachlan, S. and Oosterlee, C., “Algebraic Multigrid Solvers for Complex-Valued Matrices”, Vol. 30, SIAM J. Sci. Comp, 2008
mesh Module¶
Generates simple meshes
- pyamg.gallery.mesh.regular_triangle_mesh(nx, ny)[source]¶
Construct a regular triangular mesh in the unit square
- nx : int
- Number of nodes in the x-direction
- ny : int
- Number of nodes in the y-direction
- Vert : array
- nx*ny x 2 vertex list
- E2V : array
- Nex x 3 element list
>>> from pyamg.gallery import regular_triangle_mesh >>> E2V,Vert = regular_triangle_mesh(3, 2)
random_sparse Module¶
Random sparse matrices
- pyamg.gallery.random_sparse.sprand(m, n, density, format='csr')[source]¶
Returns a random sparse matrix.
- m, n : int
- shape of the result
- density : float
- target a matrix with nnz(A) = m*n*density, 0<=density<=1
- format : string
- sparse matrix format to return, e.g. ‘csr’, ‘coo’, etc.
- A : sparse matrix
- m x n sparse matrix
>>> from pyamg.gallery import sprand >>> A = sprand(5,5,3/5.0)
stencil Module¶
Construct sparse matrix from a local stencil
- pyamg.gallery.stencil.stencil_grid(S, grid, dtype=None, format=None)[source]¶
Construct a sparse matrix form a local matrix stencil
- S : ndarray
- matrix stencil stored in N-d array
- grid : tuple
- tuple containing the N grid dimensions
- dtype :
- data type of the result
- format : string
- sparse matrix format to return, e.g. “csr”, “coo”, etc.
- A : sparse matrix
- Sparse matrix which represents the operator given by applying stencil S at each vertex of a regular grid with given dimensions.
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().
>>> from pyamg.gallery import stencil_grid >>> 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.]])