Note
Go to the end to download the full example code.
Advanced Examples#
In this section, we demonstrate how to go from the inner product to the discrete approximation for some special cases. We also show how all necessary operators are constructed for each case.
Import Packages#
Here we import the packages required for this tutorial
from discretize.utils import sdiag
from discretize import TensorMesh
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng(4321)
Constitive Relations and Differential Operators#
Where
These cases effectively combine what was learned in the previous two tutorials. For each case, we must:
Define discretized quantities at the appropriate mesh locations
Define an inner product matrix that depends on a single constitutive parameter (
) or a tensor ( ) Construct differential operators that may require you to define boundary conditions
Where
Where
Where
With the operators constructed below, you can compute all of the aforementioned inner products.
# Make basic mesh
h = np.ones(10)
mesh = TensorMesh([h, h, h])
sig = rng.random(mesh.nC) # isotropic
Sig = rng.random((mesh.nC, 6)) # anisotropic
# Inner product matricies
Mc = sdiag(mesh.cell_volumes * sig) # Inner product matrix (centers)
# Mn = mesh.getNodalInnerProduct(sig) # Inner product matrix (nodes) (*functionality pending*)
Me = mesh.get_edge_inner_product(sig) # Inner product matrix (edges)
Mf = mesh.get_face_inner_product(sig) # Inner product matrix for tensor (faces)
# Differential operators
Gne = mesh.nodal_gradient # Nodes to edges gradient
mesh.set_cell_gradient_BC(
["neumann", "dirichlet", "neumann"]
) # Set boundary conditions
Gcf = mesh.cell_gradient # Cells to faces gradient
D = mesh.face_divergence # Faces to centers divergence
Cef = mesh.edge_curl # Edges to faces curl
Cfe = mesh.edge_curl.T # Faces to edges curl
# EXAMPLE: (u, sig*Curl*v)
fig = plt.figure(figsize=(9, 5))
ax1 = fig.add_subplot(121)
ax1.spy(Mf * Cef, markersize=0.5)
ax1.set_title("Me(sig)*Cef (Isotropic)", pad=10)
Mf_tensor = mesh.get_face_inner_product(Sig) # inner product matrix for tensor
ax2 = fig.add_subplot(122)
ax2.spy(Mf_tensor * Cef, markersize=0.5)
ax2.set_title("Me(sig)*Cef (Anisotropic)", pad=10)

Text(0.5, 1.082305057745918, 'Me(sig)*Cef (Anisotropic)')
Divergence of a Scalar and a Vector Field#
Where
Scalar and vector quantities are generally discretized to lie on
different locations on the mesh. As result, it is better to use the
identity
Term 1:
If the vector field
Where
Term 2:
Let
With the operators constructed below, you can compute all of the inner products.
# Make basic mesh
h = np.ones(10)
mesh = TensorMesh([h, h, h])
# Inner product matricies
Mc = sdiag(mesh.cell_volumes * sig) # Inner product matrix (centers)
# Differential operators
mesh.set_cell_gradient_BC(
["neumann", "dirichlet", "neumann"]
) # Set boundary conditions
Gcf = mesh.cell_gradient # Cells to faces gradient
Dfc = mesh.face_divergence # Faces to centers divergence
# Averaging and summing matrix
Afc = mesh.dim * mesh.aveF2CC
Total running time of the script: (0 minutes 0.299 seconds)