discretize.CurvilinearMesh.nodal_gradient#
- property CurvilinearMesh.nodal_gradient#
Nodal gradient operator (nodes to edges).
This property constructs the 2nd order numerical gradient operator that maps from nodes to edges. The operator is a sparse matrix \(\mathbf{G_n}\) that can be applied as a matrix-vector product to a discrete scalar quantity \(\boldsymbol{\phi}\) that lives on the nodes, i.e.:
grad_phi = Gn @ phi
Once constructed, the operator is stored permanently as a property of the mesh.
- Returns
- (
n_edges
,n_nodes
)scipy.sparse.csr_matrix
The numerical gradient operator from nodes to edges
- (
Notes
In continuous space, the gradient operator is defined as:
\[\vec{u} = \nabla \phi = \frac{\partial \phi}{\partial x}\hat{x} + \frac{\partial \phi}{\partial y}\hat{y} + \frac{\partial \phi}{\partial z}\hat{z}\]Where \(\boldsymbol{\phi}\) is the discrete representation of the continuous variable \(\phi\) on the nodes and \(\mathbf{u}\) is the discrete representation of \(\vec{u}\) on the edges, nodal_gradient constructs a discrete linear operator \(\mathbf{G_n}\) such that:
\[\mathbf{u} = \mathbf{G_n} \, \boldsymbol{\phi}\]The Cartesian components of \(\vec{u}\) are defined on their corresponding edges (x, y or z) as follows; e.g. the x-component of the gradient is defined on x-edges. For edge \(i\) which defines a straight path of length \(h_i\) between adjacent nodes \(n_1\) and \(n_2\):
\[u_i = \frac{\phi_{n_2} - \phi_{n_1}}{h_i}\]Note that \(u_i \in \mathbf{u}\) may correspond to a value on an x, y or z edge. See the example below.
Examples
Below, we demonstrate 1) how to apply the nodal gradient operator to a discrete scalar quantity, and 2) the mapping of the nodal gradient operator and its sparsity. Our example is carried out on a 2D mesh but it can be done equivalently for a 3D mesh.
We start by importing the necessary packages and modules.
>>> from discretize import TensorMesh >>> import numpy as np >>> import matplotlib.pyplot as plt >>> import matplotlib as mpl
For a discrete scalar quantity defined on the nodes, we take the gradient by constructing the gradient operator and multiplying as a matrix-vector product.
Create a uniform grid
>>> h = np.ones(20) >>> mesh = TensorMesh([h, h], "CC")
Create a discrete scalar on nodes
>>> nodes = mesh.nodes >>> phi = np.exp(-(nodes[:, 0] ** 2 + nodes[:, 1] ** 2) / 4 ** 2)
Construct the gradient operator and apply to vector
>>> Gn = mesh.nodal_gradient >>> grad_phi = Gn @ phi
Plot the original function and the gradient
(Source code, png, pdf)
The nodal gradient operator is a sparse matrix that maps from nodes to edges. To demonstrate this, we construct a small 2D mesh. We then show the ordering of the elements in the original discrete quantity \(\boldsymbol{\phi}\) and its discrete gradient as well as a spy plot.