# discretize.operators.DiffOperators¶

class discretize.operators.DiffOperators

Bases: object

Class creates the differential operators that you need!

Attributes
average_cell_to_edge
average_cell_to_face

Construct the averaging operator on cell centers to faces.

average_cell_vector_to_face

Construct the averaging operator on cell centers to faces as a vector.

average_edge_to_cell

Construct the averaging operator on cell edges to cell centers.

average_edge_to_cell_vector

Construct the averaging operator on cell edges to cell centers.

average_edge_to_face_vector
average_edge_x_to_cell

Construct the averaging operator on cell edges in the x direction to cell centers.

average_edge_y_to_cell

Construct the averaging operator on cell edges in the y direction to cell centers.

average_edge_z_to_cell

Construct the averaging operator on cell edges in the z direction to cell centers.

average_face_to_cell

Construct the averaging operator on cell faces to cell centers.

average_face_to_cell_vector

Construct the averaging operator on cell faces to cell centers.

average_face_x_to_cell

Construct the averaging operator on cell faces in the x direction to cell centers.

average_face_y_to_cell

Construct the averaging operator on cell faces in the y direction to cell centers.

average_face_z_to_cell

Construct the averaging operator on cell faces in the z direction to cell centers.

average_node_to_cell

Construct the averaging operator on cell nodes to cell centers.

average_node_to_edge

Construct the averaging operator on cell nodes to cell edges, keeping each dimension separate.

average_node_to_face

Construct the averaging operator on cell nodes to cell faces, keeping each dimension separate.

boundary_edge_vector_integral

Represents the operation of integrating a vector function on the boundary

boundary_face_scalar_integral

Represents the operation of integrating a scalar function on the boundary

boundary_node_vector_integral

Represents the operation of integrating a vector function dotted with the boundary normal

cellGrad

cellGradBC

cellGradx

cellGrady

cellGradz

cell_gradient

The cell centered Gradient, takes you to cell faces.

cell_gradient_BC

The cell centered Gradient boundary condition matrix

cell_gradient_x

Cell centered Gradient in the x dimension.

cell_gradient_z

Cell centered Gradient in the x dimension.

edgeCurl

edgeCurl has been deprecated. See edge_curl for documentation

edge_curl

Construct the 3D curl operator.

faceDiv

faceDiv has been deprecated. See face_divergence for documentation

faceDivx

faceDivx has been deprecated. See face_x_divergence for documentation

faceDivy

faceDivy has been deprecated. See face_y_divergence for documentation

faceDivz

faceDivz has been deprecated. See face_z_divergence for documentation

face_divergence

Construct divergence operator (face-stg to cell-centres).

face_x_divergence

Construct divergence operator in the x component (face-stg to cell-centres).

face_y_divergence
face_z_divergence

Construct divergence operator in the z component (face-stg to cell-centers).

nodalGrad

nodalLaplacian

nodalLaplacian has been deprecated. See nodal_laplacian for documentation

nodal_gradient

Construct gradient operator (nodes to edges).

nodal_laplacian

Construct laplacian operator (nodes to edges).

project_edge_to_boundary_edge

Projects values defined on all edges to the boundary edges

project_face_to_boundary_face

Projects values defined on all faces to the boundary faces

project_node_to_boundary_node

Projects values defined on all edges to the boundary edges

Methods

 cell_gradient_weak_form_robin([alpha, beta, …]) Robin boundary condition for the weak formulation of the cell gradient edge_divergence_weak_form_robin([alpha, …]) Robin boundary condition for the weak formulation of the edge divergence getBCProjWF(*args, **kwargs) getBCProjWF has been deprecated. getBCProjWF_simple(*args, **kwargs) getBCProjWF_simple has been deprecated. get_BC_projections(BC[, discretization]) The weak form boundary condition projection matrices. get_BC_projections_simple([discretization]) The weak form boundary condition projection matrices when mixed boundary condition is used setCellGradBC(*args, **kwargs) setCellGradBC has been deprecated. Function that sets the boundary conditions for cell-centred derivative operators.

## Attributes¶

DiffOperators.average_cell_to_edge
DiffOperators.average_cell_to_face

Construct the averaging operator on cell centers to faces.

DiffOperators.average_cell_vector_to_face

Construct the averaging operator on cell centers to faces as a vector.

DiffOperators.average_edge_to_cell

Construct the averaging operator on cell edges to cell centers.

DiffOperators.average_edge_to_cell_vector

Construct the averaging operator on cell edges to cell centers.

DiffOperators.average_edge_to_face_vector
DiffOperators.average_edge_x_to_cell

Construct the averaging operator on cell edges in the x direction to cell centers.

DiffOperators.average_edge_y_to_cell

Construct the averaging operator on cell edges in the y direction to cell centers.

DiffOperators.average_edge_z_to_cell

Construct the averaging operator on cell edges in the z direction to cell centers.

DiffOperators.average_face_to_cell

Construct the averaging operator on cell faces to cell centers.

DiffOperators.average_face_to_cell_vector

Construct the averaging operator on cell faces to cell centers.

DiffOperators.average_face_x_to_cell

Construct the averaging operator on cell faces in the x direction to cell centers.

DiffOperators.average_face_y_to_cell

Construct the averaging operator on cell faces in the y direction to cell centers.

DiffOperators.average_face_z_to_cell

Construct the averaging operator on cell faces in the z direction to cell centers.

DiffOperators.average_node_to_cell

Construct the averaging operator on cell nodes to cell centers.

DiffOperators.average_node_to_edge

Construct the averaging operator on cell nodes to cell edges, keeping each dimension separate.

DiffOperators.average_node_to_face

Construct the averaging operator on cell nodes to cell faces, keeping each dimension separate.

DiffOperators.boundary_edge_vector_integral

Represents the operation of integrating a vector function on the boundary

This matrix represents the boundary surface integral of a vector function multiplied with a finite volume test function on the mesh.

In 1D and 2D, the operation assumes that the right array contains only a single component of the vector u. In 3D, however, we must assume that u will contain each of the three vector components, and it must be ordered as, [edges_1_x, ... ,edge_N_x, edge_1_y, ..., edge_N_y, edge_1_z, ..., edge_N_z] , where N is the number of boundary edges.

Returns
scipy.sparse.csr_matrix

Sparse matrix of shape (n_edges, n_boundary_edges) for 1D or 2D mesh, (n_edges, 3*n_boundary_edges) for a 3D mesh.

Notes

The integral we are representing on the boundary of the mesh is

$\int_{\Omega} \vec{w} \cdot (\vec{u} \times \hat{n}) \partial \Omega$

In discrete form this is:

$w^T * P * u_b$

where w is defined on all edges, and u_b is all three components defined on boundary edges.

DiffOperators.boundary_face_scalar_integral

Represents the operation of integrating a scalar function on the boundary

This matrix represents the boundary surface integral of a scalar function multiplied with a finite volume test function on the mesh.

Returns
scipy.sparse.csr_matrix

Sparse matrix of shape (n_faces, n_boundary_faces)

Notes

The integral we are representing on the boundary of the mesh is

$\int_{\Omega} u\vec{w} \cdot \hat{n} \partial \Omega$

In discrete form this is:

$w^T * P * u_b$

where w is defined on all faces, and u_b is defined on boundary faces.

DiffOperators.boundary_node_vector_integral

Represents the operation of integrating a vector function dotted with the boundary normal

This matrix represents the boundary surface integral of a vector function dotted with the boundary normal and multiplied with a scalar finite volume test function on the mesh.

Returns
scipy.sparse.csr_matrix

Sparse matrix of shape (n_nodes, ndim * n_boundary_nodes).

Notes

The integral we are representing on the boundary of the mesh is

$\int_{\Omega} (w \vec{u}) \cdot \hat{n} \partial \Omega$

In discrete form this is:

$w^T * P * u_b$

where w is defined on all nodes, and u_b is all three components defined on boundary nodes.

DiffOperators.cellGrad

DiffOperators.cellGradBC

DiffOperators.cellGradx

DiffOperators.cellGrady

DiffOperators.cellGradz

DiffOperators.cell_gradient

The cell centered Gradient, takes you to cell faces.

DiffOperators.cell_gradient_BC

The cell centered Gradient boundary condition matrix

DiffOperators.cell_gradient_x

Cell centered Gradient in the x dimension. Has neumann boundary conditions.

DiffOperators.cell_gradient_y
DiffOperators.cell_gradient_z

Cell centered Gradient in the x dimension. Has neumann boundary conditions.

DiffOperators.edgeCurl

edgeCurl has been deprecated. See edge_curl for documentation

DiffOperators.edge_curl

Construct the 3D curl operator.

DiffOperators.faceDiv

faceDiv has been deprecated. See face_divergence for documentation

DiffOperators.faceDivx

faceDivx has been deprecated. See face_x_divergence for documentation

DiffOperators.faceDivy

faceDivy has been deprecated. See face_y_divergence for documentation

DiffOperators.faceDivz

faceDivz has been deprecated. See face_z_divergence for documentation

DiffOperators.face_divergence

Construct divergence operator (face-stg to cell-centres).

DiffOperators.face_x_divergence

Construct divergence operator in the x component (face-stg to cell-centres).

DiffOperators.face_y_divergence
DiffOperators.face_z_divergence

Construct divergence operator in the z component (face-stg to cell-centers).

DiffOperators.nodalGrad

DiffOperators.nodalLaplacian

nodalLaplacian has been deprecated. See nodal_laplacian for documentation

DiffOperators.nodal_gradient

Construct gradient operator (nodes to edges).

DiffOperators.nodal_laplacian

Construct laplacian operator (nodes to edges).

DiffOperators.project_edge_to_boundary_edge

Projects values defined on all edges to the boundary edges

Returns
scipy.sparse.csr_matrix

Projection matrix with shape (n_boundary_edges, n_edges)

DiffOperators.project_face_to_boundary_face

Projects values defined on all faces to the boundary faces

Returns
scipy.sparse.csr_matrix

Projection matrix with shape (n_boundary_faces, n_faces)

DiffOperators.project_node_to_boundary_node

Projects values defined on all edges to the boundary edges

Returns
scipy.sparse.csr_matrix

Projection matrix with shape (n_boundary_nodes, n_nodes)

DiffOperators.stencil_cell_gradient
DiffOperators.stencil_cell_gradient_x
DiffOperators.stencil_cell_gradient_y
DiffOperators.stencil_cell_gradient_z

## Methods¶

DiffOperators.cell_gradient_weak_form_robin(alpha=1.0, beta=0.0, gamma=0.0)[source]

Robin boundary condition for the weak formulation of the cell gradient

This function returns the necessary parts for the weak form of the cell gradient operator to represent the Robin boundary conditions.

The implementation assumes a ghost cell that mirrors the boundary cells across the boundary faces, with a piecewise linear approximation to the values at the ghost cell centers.

The parameters can either be defined as a constant applied to the entire boundary, or as arrays that represent those values on the discretize.base.BaseTensorMesh.boundary_faces().

The returned arrays represent the proper boundary conditions on a solution u such that the inner product of the gradient of u with a test function y would be <y, gradient*u> = y.dot((-face_divergence.T*cell_volumes + A)*u + y.dot(b).

The default values will produce a zero-dirichlet boundary condition.

Parameters
alpha, betascalar or array_like

Parameters for the Robin boundary condition. array_like must be defined on each boundary face.

gamma: scalar or array_like

right hand side boundary conditions. If this parameter is array like, it can be fed either a (n_boundary_faces,) shape array or an (n_boundary_faces, n_rhs) shape array if multiple systems have the same alpha and beta parameters.

Returns
Ascipy.sparse.csr_matrix

Matrix to add to (-face_divergence.T * cell_volumes)

bnumpy.ndarray

Array to add to the result of the (-face_divergence.T * cell_volumes + A) @ u.

Notes

The weak form is obtained by multiplying the gradient by a (piecewise-constant) test function, and integrating over the cell, i.e.

(1)$\int_V \vec{y} \cdot \nabla u \partial V$

This equation can be transformed to reduce the differentiability requirement on u to be,

(2)$-\int_V u (\nabla \cdot \vec{y}) \partial V + \int_{dV} u \vec{y} \partial V.$

The first term in equation :eq:transformed is constructed using the matrix operators defined on this mesh as D = discretize.operators.DiffOperators.face_divergence() and V, a diagonal matrix of discretize.base.BaseMesh.cell_volumes(), as

$-(D*y)^T*V*u.$

This function returns the necessary matrices to complete the transformation of equation :eq:transformed. The second part of equation :eq:transformed becomes,

(3)$\int_V \nabla \cdot (\phi u) \partial V = \int_{\partial\Omega} \phi\vec{u}\cdot\hat{n} \partial a$

which is then approximated with the matrices returned here such that the full form of the weak formulation in a discrete form would be.

$y^T(-D^T V + B)u + y^Tb$

Examples

We first create a very simple 2D tensor mesh on the [0, 1] boundary:

>>> import matplotlib.pyplot as plt
>>> import scipy.sparse as sp
>>> import discretize
>>> mesh = discretize.TensorMesh([32, 32])


Define the alpha, beta, and gamma parameters for a zero - Dirichlet condition on the boundary, this corresponds to setting:

>>> alpha = 1.0
>>> beta = 0.0
>>> gamma = 0.0
>>> A, b = mesh.cell_gradient_weak_form_robin(alpha, beta, gamma)


We can then represent the operation of taking the weak form of the gradient of a function defined on cell centers with appropriate robin boundary conditions as:

>>> V = sp.diags(mesh.cell_volumes)
>>> D = mesh.face_divergence
>>> phi = np.sin(np.pi * mesh.cell_centers[:, 0]) * np.sin(np.pi * mesh.cell_centers[:, 1])
>>> phi_grad = (-D.T @ V + A) @ phi + b

DiffOperators.edge_divergence_weak_form_robin(alpha=0.0, beta=1.0, gamma=0.0)[source]

Robin boundary condition for the weak formulation of the edge divergence

This function returns the necessary parts to form the full weak form of the edge divergence using the nodal gradient with appropriate boundary conditions.

The alpha, beta, and gamma parameters can be scalars, or arrays. If they are arrays, they can either be the same length as the number of boundary faces, or boundary nodes. If multiple parameters are arrays, they must all be the same length.

beta can not be 0.

It is assumed here that quantity that is approximated on the boundary is the gradient of another quantity. See the Notes section for explicit details.

Parameters
alpha, betascalar or array_like

Parameters for the Robin boundary condition. array_like must be defined on either boundary faces or boundary nodes.

gamma: scalar or array_like

right hand side boundary conditions. If this parameter is array like, it can be fed either a (n_boundary_XXX,) shape array or an (n_boundary_XXX, n_rhs) shape array if multiple systems have the same alpha and beta parameters.

Notes

For these returned operators, it is assumed that the quantity on the boundary is related to the gradient of some other quantity.

The weak form is obtained by multiplying the divergence by a (piecewise-constant) test function, and integrating over the cell, i.e.

(4)$\int_V y \nabla \cdot \vec{u} \partial V$

This equation can be transformed to reduce the differentiability requirement on $$\vec{u}$$ to be,

(5)$-\int_V \vec{u} \cdot (\nabla y) \partial V + \int_{dV} y \vec{u} \cdot \hat{n} \partial S.$

Furthermore, when applying these types of transformations, the unknown vector $$\vec{u}$$ is usually related to some scalar potential as:

(6)$\vec{u} = \nabla \phi$

Thus the robin conditions returned by these matrices apply to the quantity of $$\phi$$.

\begin{align}\begin{aligned}\alpha \phi + \beta \nabla \phi \cdot \hat{n} = \gamma\\\alpha \phi + \beta \vec{u} \cdot \hat{n} = \gamma\end{aligned}\end{align}

The returned operators cannot be used to impose a Dirichlet condition on $$\phi$$.

DiffOperators.getBCProjWF(*args, **kwargs)

getBCProjWF has been deprecated. See get_BC_projections for documentation

DiffOperators.getBCProjWF_simple(*args, **kwargs)

getBCProjWF_simple has been deprecated. See get_BC_projections_simple for documentation

DiffOperators.get_BC_projections(BC, discretization='CC')[source]

The weak form boundary condition projection matrices.

Examples

# Neumann in all directions
BC = 'neumann'

# 3D, Dirichlet in y Neumann else
BC = ['neumann', 'dirichlet', 'neumann']

# 3D, Neumann in x on bottom of domain, Dirichlet else
BC = [['neumann', 'dirichlet'], 'dirichlet', 'dirichlet']

DiffOperators.get_BC_projections_simple(discretization='CC')[source]

The weak form boundary condition projection matrices when mixed boundary condition is used

DiffOperators.setCellGradBC(*args, **kwargs)

DiffOperators.set_cell_gradient_BC(BC)[source]

Function that sets the boundary conditions for cell-centred derivative operators.

Examples

..code:: python

# Neumann in all directions BC = ‘neumann’

# 3D, Dirichlet in y Neumann else BC = [‘neumann’, ‘dirichlet’, ‘neumann’]

# 3D, Neumann in x on bottom of domain, Dirichlet else BC = [[‘neumann’, ‘dirichlet’], ‘dirichlet’, ‘dirichlet’]