# Tensor Mesh¶

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

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() For a quick tensor mesh on a (10x12x15) unit cube:

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


Required Properties:

• axis_u (Vector3): Vector orientation of u-direction. For more details see the docs for the rotation_matrix property., a 3D Vector of <class ‘float’> with shape (3), Default: X
• axis_v (Vector3): Vector orientation of v-direction. For more details see the docs for the rotation_matrix property., a 3D Vector of <class ‘float’> with shape (3), Default: Y
• axis_w (Vector3): Vector orientation of w-direction. For more details see the docs for the rotation_matrix property., a 3D Vector of <class ‘float’> with shape (3), Default: Z
• 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
• reference_system (String): The type of coordinate reference frame. Can take on the values cartesian, cylindrical, or spherical. Abbreviations of these are allowed., a unicode string, Default: cartesian
• 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]
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.

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.ndarray) – 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) 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) 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, stream_thickness=None)[source]

Plots a slice of a 3D mesh. plotGrid(ax=None, nodes=False, faces=False, centers=False, edges=False, lines=True, showIt=False, **kwargs)[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) 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) plot_3d_slicer(v, xslice=None, yslice=None, zslice=None, vType='CC', view='real', axis='xy', transparent=None, clim=None, aspect='auto', grid=[2, 2, 1], pcolorOpts=None)[source]

Plot slices of a 3D volume, interactively (scroll wheel).

If called from a notebook, make sure to set

%matplotlib notebook

See the class discretize.View.Slicer for more information.

It returns nothing. However, if you need the different figure handles you can get it via

fig = plt.gcf()

and subsequently its children via

fig.get_children()

and recursively deeper, e.g.,

fig.get_children().get_children().