difftools.fidop2d

difftools.fidop2d(N, drt='xy', fds='c')[source]

Compute 2D finite difference operators

Parameters:

N : int

Positive integer holding the grid dimension (has to be square, i.e. N-by-N!)

drt : str

String determining the direction of the discrete derivatives. Can be either ‘x’, ‘y’ or ‘xy’.

fds : str

String determining the employed finite difference scheme. Can be either ‘c’ for centered, ‘f’ for forward or ‘b’ for backward.

Returns:

D : SciPy sparse matrix/matrices

Depending on the given input one or two sparse matrices corresponding to the wanted discrete derivative operators are returned.

Notes

For clarity here some comments on the rationale behind the code.

The Laplacian

If Dxf and Dyf are computed using forward differences, i.e.

>>> Dxf,Dyf = fidop2d(N,'xy','f')

then the discrete Laplacian Lh based on centered differences is obtained by

>>> Lh = -(Dxf.T*Dxf + Dyf.T*Dyf)

or analogously for backward differences,

>>> Dxb,Dyb = fidop2d(N,'xy','b')

then

>>> Lh = Dxb.T*Dxb + Dyb.T*Dyb

The Divergence

Note adjoints: for \(p\) in \(H_0(\mathrm{div}):=\{p \in L^2(\Omega)\) | div \((p) \in L^2(\Omega), pn = 0\) on the boundary of \(\Omega\}\) we have

\[\begin{split}\begin{align} \int_{\Omega} \nabla u\cdot p dx &= \int_{\Omega} u p \cdot n dS - \int_{\Omega} u \textrm{ div }(p) dx\\ & = 0 -\int_{\Omega} u \textrm{ div }(p) dx \end{align}\end{split}\]

or in short \(\textrm{div}^\ast = -\nabla\). Hence for a spatial discretization take Div_h ~ -Dh.T as approximation for the divergence.

Examples

The command

>>> Dx,Dy = fidop2d(N) 

returns the sparse N**2-by-N**2 matrices Dx and Dy such that Dh defined by

>>> import numpy as np
>>> Dh = np.hstack([Dx,Dy])

is the discrete gradient operator in lexicographic ordering for a function F given on a grid of size N-by-N. The matrix Dx is a discrete approximation of the first-order derivative operator in x-direction, Analogously, Dy discretizes the first order derivative operator in y-direction. The spacing between points in each direction is assumed to be one (i.e. the step size h = 1).

The command

>>> D = fidop2d(N,drt) 

returns the sparse N**2-by-N**2 matrix D corresponding to the discrete derivative operator in direction drt in lexicographic ordering. The string drt has to be either ‘x’ (default) , ‘y’ or ‘xy’. Note: only ‘xy’ will return two matrices, namely Dx, Dy.

The command

>>> D = fidop2d(N,drt,fds) 

returns sparse the N**2-by-N**2 matrix D corresponding to the discrete derivative operator in direction drt in lexicographic ordering using the finite difference scheme fds. The string fds can either be ‘c’ (centered differences, in cells, default), ‘f’ (forward differences, in interfaces) or ‘b’ (backward differences, in interfaces).

To discretize first order differential operators on a 4-by-4 grid in both x- and y-directions using forward differences use

>>> Dx,Dy = fidop2d(4,fds='f')

To get only the discrete first order differential operator in y-direction on a grid of size N use

>>> Dy = fidop2d(N,'y')