Tensor Mesh

class discretize.TensorMesh(h=None, x0=None, **kwargs)[source]

Bases: discretize.TensorMesh.BaseTensorMesh, discretize.BaseMesh.BaseRectangularMesh, discretize.View.TensorView, discretize.DiffOperators.DiffOperators, discretize.InnerProducts.InnerProducts, discretize.MeshIO.TensorMeshIO

TensorMesh is a mesh class that deals with tensor product meshes.

Any Mesh that has a constant width along the entire axis such that it can defined by a single width vector, called ‘h’.

hx = np.array([1, 1, 1])
hy = np.array([1, 2])
hz = np.array([1, 1, 1, 1])

mesh = Mesh.TensorMesh([hx, hy, hz])

Example of a padded tensor mesh using discretize.utils.meshutils.meshTensor():

import discretize
M = discretize.TensorMesh([
    [(10, 10, -1.3), (10, 40), (10, 10, 1.3)],
    [(10, 10, -1.3), (10, 20)]
])
M.plotGrid()

(Source code, png, hires.png, pdf)

../_images/mesh_tensor-1.png

For a quick tensor mesh on a (10x12x15) unit cube:

mesh = discretize.TensorMesh([10, 12, 15])

Required Properties:

  • h (a list of Array): h is a list containing the cell widths of the tensor mesh in each dimension., a list (each item is a list or numpy array of <class ‘float’> with shape (*)) with length between 0 and 3
  • x0 (Array): origin of the mesh (dim, ), a list or numpy array of <class ‘float’> with shape (*)
vol

Construct cell volumes of the 3D model as 1d array.

areaFx

Area of the x-faces

areaFy

Area of the y-faces

areaFz

Area of the z-faces

area

Construct face areas of the 3D model as 1d array.

edgeEx

x-edge lengths

edgeEy

y-edge lengths

edgeEz

z-edge lengths

edge

Construct edge legnths of the 3D model as 1d array.

faceBoundaryInd

Find indices of boundary faces in each direction

cellBoundaryInd

Find indices of boundary faces in each direction

Mesh IO

class discretize.MeshIO.TensorMeshIO[source]

Bases: object

classmethod readUBC(TensorMesh, fileName, directory='')[source]

Wrapper to Read UBC GIF 2D and 3D tensor mesh and generate same dimension TensorMesh.

Input: :param str fileName: path to the UBC GIF mesh file or just its name if directory is specified :param str directory: directory where the UBC GIF file lives

Output: :rtype: TensorMesh :return: The tensor mesh for the fileName.

classmethod readVTK(TensorMesh, fileName, directory='')[source]

Read VTK Rectilinear (vtr xml file) and return Tensor mesh and model

Input: :param str fileName: path to the vtr model file to read or just its name if directory is specified :param str directory: directory where the UBC GIF file lives

Output: :rtype: tuple :return: (TensorMesh, modelDictionary)

writeVTK(mesh, fileName, models=None, directory='')[source]

Makes and saves a VTK rectilinear file (vtr) for a Tensor mesh and model.

Input: :param str fileName: path to the output vtk file or just its name if directory is specified :param str directory: directory where the UBC GIF file lives :param dict models: dictionary of numpy.array - Name(‘s) and array(‘s). Match number of cells

readModelUBC(mesh, fileName, directory='')[source]
Read UBC 2D or 3D Tensor mesh model
and generate Tensor mesh model

Input: :param str fileName: path to the UBC GIF mesh file to read or just its name if directory is specified :param str directory: directory where the UBC GIF file lives

Output: :rtype: numpy.ndarray :return: model with TensorMesh ordered

writeModelUBC(mesh, fileName, model, directory='')[source]

Writes a model associated with a TensorMesh to a UBC-GIF format model file.

Input: :param str fileName: File to write to or just its name if directory is specified :param str directory: directory where the UBC GIF file lives :param numpy.ndarray model: The model

writeUBC(mesh, fileName, models=None, directory='', comment_lines='')[source]

Writes a TensorMesh to a UBC-GIF format mesh file.

Input: :param str fileName: File to write to :param str directory: directory where to save model :param dict models: A dictionary of the models :param str comment_lines: comment lines preceded with ‘!’ to add

Mesh Viewing

class discretize.View.TensorView[source]

Bases: object

Provides viewing functions for TensorMesh

This class is inherited by TensorMesh

plotImage(v)[source]

Plots scalar fields on the given mesh.

Input:

Parameters:v (numpy.array) – vector

Optional Inputs:

Parameters:
  • vType (str) – type of vector (‘CC’, ‘N’, ‘F’, ‘Fx’, ‘Fy’, ‘Fz’, ‘E’, ‘Ex’, ‘Ey’, ‘Ez’)
  • ax (matplotlib.axes.Axes) – axis to plot to
  • showIt (bool) – call plt.show()

3D Inputs:

Parameters:
  • numbering (bool) – show numbering of slices, 3D only
  • annotationColor (str) – color of annotation, e.g. ‘w’, ‘k’, ‘b’
import discretize
import numpy as np
M = discretize.TensorMesh([20, 20])
v = np.sin(M.gridCC[:, 0]*2*np.pi)*np.sin(M.gridCC[:, 1]*2*np.pi)
M.plotImage(v, showIt=True)

(Source code, png, hires.png, pdf)

../_images/mesh_tensor-2.png
import discretize
import numpy as np
M = discretize.TensorMesh([20, 20, 20])
v = np.sin(M.gridCC[:, 0]*2*np.pi)*np.sin(M.gridCC[:, 1]*2*np.pi)*np.sin(M.gridCC[:, 2]*2*np.pi)
M.plotImage(v, annotationColor='k', showIt=True)

(Source code, png, hires.png, pdf)

../_images/mesh_tensor-3.png
plotSlice(v, vType='CC', normal='Z', ind=None, grid=False, view='real', ax=None, clim=None, showIt=False, pcolorOpts=None, streamOpts=None, gridOpts=None, range_x=None, range_y=None, sample_grid=None, stream_threshold=None)[source]

Plots a slice of a 3D mesh.

(Source code, png, hires.png, pdf)

../_images/mesh_tensor-4.png
plotGrid(ax=None, nodes=False, faces=False, centers=False, edges=False, lines=True, showIt=False)[source]

Plot the nodal, cell-centered and staggered grids for 1,2 and 3 dimensions.

Parameters:
  • nodes (bool) – plot nodes
  • faces (bool) – plot faces
  • centers (bool) – plot centers
  • edges (bool) – plot edges
  • lines (bool) – plot lines connecting nodes
  • showIt (bool) – call plt.show()
import discretize
import numpy as np
h1 = np.linspace(.1, .5, 3)
h2 = np.linspace(.1, .5, 5)
mesh = discretize.TensorMesh([h1, h2])
mesh.plotGrid(nodes=True, faces=True, centers=True, lines=True, showIt=True)

(Source code, png, hires.png, pdf)

../_images/mesh_tensor-5.png
import discretize
import numpy as np
h1 = np.linspace(.1, .5, 3)
h2 = np.linspace(.1, .5, 5)
h3 = np.linspace(.1, .5, 3)
mesh = discretize.TensorMesh([h1, h2, h3])
mesh.plotGrid(nodes=True, faces=True, centers=True, lines=True, showIt=True)

(Source code, png, hires.png, pdf)

../_images/mesh_tensor-6.png