Navigation

  • index
  • modules |
  • modules |
  • next |
  • previous |
  • PyAMG 1.1 documentation »
  • Project Documentation »
  • Pyamg Documentation »

Gallery Documentation¶

This page contains the Gallery Package documentation.

The demo Module¶

Basic PyAMG demo showing AMG standalone convergence versus preconditioned CG with AMG

pyamg.gallery.demo.demo()[source]¶

The 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).

-div Q A Q^T grad u

Q = [cos(theta) -sin(theta)]
[sin(theta) cos(theta)]
A = [1 0 ]
[0 eps ]
pyamg.gallery.diffusion.diffusion_stencil_2d(epsilon=1.0, theta=0.0, type='FE')[source]¶
Parameters :

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

Returns :

stencil : numpy array

A 3x3 diffusion stencil

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]]

The 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

Parameters :

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.)

Returns :

A : {csr_matrix}

FE Q1 stiffness matrix

See also

linear_elasticity_p1

Notes

  • only 2d for now

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))
pyamg.gallery.elasticity.linear_elasticity_p1(vertices, elements, E=100000.0, nu=0.3, format=None)[source]¶

P1 elements in 2 or 3 dimensions

Parameters :

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’

Returns :

A : {csr_matrix}

FE Q1 stiffness matrix

Notes

  • works in both 2d and in 3d

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)

The example Module¶

Examples stored in files

pyamg.gallery.example.load_example(name)[source]¶

Load an example problem by name

Parameters :

name : string (e.g. ‘airfoil’)

Name of the example to load

Notes

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

Examples

>>> from pyamg.gallery import load_example
>>> ex = load_example('knot')

The 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.

Parameters :

grid : tuple of integers

grid dimensions e.g. (100,100)

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.]])
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.

Parameters :

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

Returns :

A : {csr matrix}

A is Hermitian positive definite for beta > 0.0 A is Symmetric semi-definite for beta = 0.0

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)

The mesh Module¶

Generates simple meshes

pyamg.gallery.mesh.regular_triangle_mesh(nx, ny)[source]¶

Construct a regular triangular mesh in the unit square

Parameters :

nx : int

Number of nodes in the x-direction

ny : int

Number of nodes in the y-direction

Returns :

Vert : array

nx*ny x 2 vertex list

E2V : array

Nex x 3 element list

Examples

>>> E2V,Vert = regular_triangle_mesh(3, 2)

The random_sparse Module¶

Random sparse matrices

pyamg.gallery.random_sparse.sprand(m, n, density, format='csr')[source]¶

Returns a random sparse matrix.

Parameters :

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.

Returns :

A : sparse matrix

m x n sparse matrix

Examples

>>> import numpy
>>> A = sprand(5,5,3/5.0)

The setup Module¶

pyamg.gallery.setup.configuration(parent_package='', top_path=None)[source]¶

The 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

Parameters :

S : ndarray

matrix stencil stored in rank N 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.

Returns :

A : sparse matrix

Sparse matrix which represents the operator given by applying stencil S at each vertex of a regular grid with given dimensions.

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.]])

Logo

Table Of Contents

  • Gallery Documentation
    • The demo Module
    • The diffusion Module
    • The elasticity Module
    • The example Module
    • The laplacian Module
    • The mesh Module
    • The random_sparse Module
    • The setup Module
    • The stencil Module

Previous topic

Classical Documentation

Next topic

Krylov Documentation

This Page

  • Show Source

Quick search

Enter search terms or a module, class or function name.

Navigation

  • index
  • modules |
  • modules |
  • next |
  • previous |
  • PyAMG 1.1 documentation »
  • Project Documentation »
  • Pyamg Documentation »
© Copyright 2009, Nathan Bell, Luke Olson, Jacob Schroder. Created using Sphinx 1.1.3.