discretize.CurvilinearMesh¶

class discretize.CurvilinearMesh(nodes=None, **kwargs)[source]

Bases: discretize.base.base_mesh.BaseRectangularMesh, discretize.DiffOperators.DiffOperators, discretize.InnerProducts.InnerProducts, discretize.View.CurviView

CurvilinearMesh is a mesh class that deals with curvilinear meshes.

Example of a curvilinear mesh:

import discretize
X, Y = discretize.utils.exampleLrmGrid([3,3],'rotate')
mesh = discretize.CurvilinearMesh([X, Y])
mesh.plotGrid(showIt=True)


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
• nodes (a list of Array): List of arrays describing the node locations, a list (each item is a list or numpy array of <class ‘float’>, <class ‘int’> with shape (*, *) or (*, *, *)) with length between 2 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’>, <class ‘int’> with shape (*)
Attributes: area Area of the faces aveCC2F Construct the averaging operator on cell centers to faces. aveCCV2F Construct the averaging operator on cell centers to faces as a vector. aveE2CC Construct the averaging operator on cell edges to cell centers. aveE2CCV Construct the averaging operator on cell edges to cell centers. aveEx2CC Construct the averaging operator on cell edges in the x direction to cell centers. aveEy2CC Construct the averaging operator on cell edges in the y direction to cell centers. aveEz2CC Construct the averaging operator on cell edges in the z direction to cell centers. aveF2CC Construct the averaging operator on cell faces to cell centers. aveF2CCV Construct the averaging operator on cell faces to cell centers. aveFx2CC Construct the averaging operator on cell faces in the x direction to cell centers. aveFy2CC Construct the averaging operator on cell faces in the y direction to cell centers. aveFz2CC Construct the averaging operator on cell faces in the z direction to cell centers. aveN2CC Construct the averaging operator on cell nodes to cell centers. aveN2E Construct the averaging operator on cell nodes to cell edges, keeping each dimension separate. aveN2F Construct the averaging operator on cell nodes to cell faces, keeping each dimension separate. axis_u axis_u (Vector3): Vector orientation of u-direction. For more details see the docs for the rotation_matrix property., a 3D Vector of with shape (3), Default: X axis_v axis_v (Vector3): Vector orientation of v-direction. For more details see the docs for the rotation_matrix property., a 3D Vector of with shape (3), Default: Y axis_w axis_w (Vector3): Vector orientation of w-direction. For more details see the docs for the rotation_matrix property., a 3D Vector of with shape (3), Default: Z cellGrad The cell centered Gradient, takes you to cell faces. cellGradBC The cell centered Gradient boundary condition matrix cellGradx Cell centered Gradient in the x dimension. cellGrady cellGradz Cell centered Gradient in the x dimension. dim The dimension of the mesh (1, 2, or 3). edge Edge lengths edgeCurl Construct the 3D curl operator. faceDiv Construct divergence operator (face-stg to cell-centres). faceDivx Construct divergence operator in the x component (face-stg to cell-centres). faceDivy faceDivz Construct divergence operator in the z component (face-stg to cell-centers). gridCC Cell-centered grid gridEx Edge staggered grid in the x direction. gridEy Edge staggered grid in the y direction. gridEz Edge staggered grid in the z direction. gridFx Face staggered grid in the x direction. gridFy Face staggered grid in the y direction. gridFz Face staggered grid in the y direction. gridN Nodal grid. nC Total number of cells nCx Number of cells in the x direction nCy Number of cells in the y direction nCz Number of cells in the z direction nE Total number of edges. nEx Number of x-edges nEy Number of y-edges nEz Number of z-edges nF Total number of faces. nFx Number of x-faces nFy Number of y-faces nFz Number of z-faces nN Total number of nodes nNx Number of nodes in the x-direction nNy Number of nodes in the y-direction nNz Number of nodes in the z-direction nodalGrad Construct gradient operator (nodes to edges). nodalLaplacian Construct laplacian operator (nodes to edges). nodes nodes (a list of Array): List of arrays describing the node locations, a list (each item is a list or numpy array of , with shape (*, *) or (*, *, *)) with length between 2 and 3 normals Face normals: calling this will average the computed normals so that there is one per face. reference_is_rotated True if the axes are rotated from the traditional system reference_system 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 rotation_matrix Builds a rotation matrix to transform coordinates from their coordinate system into a conventional cartesian system. tangents Edge tangents vnC Total number of cells in each direction vnE Total number of edges in each direction vnEx Number of x-edges in each direction vnEy Number of y-edges in each direction vnEz Number of z-edges in each direction vnF Total number of faces in each direction vnFx Number of x-faces in each direction vnFy Number of y-faces in each direction vnFz Number of z-faces in each direction vnN Total number of nodes in each direction vol Construct cell volumes of the 3D model as 1d array x0 x0 (Array): origin of the mesh (dim, ), a list or numpy array of , with shape (*)

Methods

 plotImage

Attributes¶

CurvilinearMesh.area

Area of the faces

CurvilinearMesh.aveCC2F

Construct the averaging operator on cell centers to faces.

CurvilinearMesh.aveCCV2F

Construct the averaging operator on cell centers to faces as a vector.

CurvilinearMesh.aveE2CC

Construct the averaging operator on cell edges to cell centers.

CurvilinearMesh.aveE2CCV

Construct the averaging operator on cell edges to cell centers.

CurvilinearMesh.aveEx2CC

Construct the averaging operator on cell edges in the x direction to cell centers.

CurvilinearMesh.aveEy2CC

Construct the averaging operator on cell edges in the y direction to cell centers.

CurvilinearMesh.aveEz2CC

Construct the averaging operator on cell edges in the z direction to cell centers.

CurvilinearMesh.aveF2CC

Construct the averaging operator on cell faces to cell centers.

CurvilinearMesh.aveF2CCV

Construct the averaging operator on cell faces to cell centers.

CurvilinearMesh.aveFx2CC

Construct the averaging operator on cell faces in the x direction to cell centers.

CurvilinearMesh.aveFy2CC

Construct the averaging operator on cell faces in the y direction to cell centers.

CurvilinearMesh.aveFz2CC

Construct the averaging operator on cell faces in the z direction to cell centers.

CurvilinearMesh.aveN2CC

Construct the averaging operator on cell nodes to cell centers.

CurvilinearMesh.aveN2E

Construct the averaging operator on cell nodes to cell edges, keeping each dimension separate.

CurvilinearMesh.aveN2F

Construct the averaging operator on cell nodes to cell faces, keeping each dimension separate.

CurvilinearMesh.axis_u

X

Type: axis_u (Vector3) Vector orientation of u-direction. For more details see the docs for the rotation_matrix property., a 3D Vector of with shape (3), Default
CurvilinearMesh.axis_v

Y

Type: axis_v (Vector3) Vector orientation of v-direction. For more details see the docs for the rotation_matrix property., a 3D Vector of with shape (3), Default
CurvilinearMesh.axis_w

Z

Type: axis_w (Vector3) Vector orientation of w-direction. For more details see the docs for the rotation_matrix property., a 3D Vector of with shape (3), Default
CurvilinearMesh.cellGrad

The cell centered Gradient, takes you to cell faces.

CurvilinearMesh.cellGradBC

The cell centered Gradient boundary condition matrix

CurvilinearMesh.cellGradx

Cell centered Gradient in the x dimension. Has neumann boundary conditions.

CurvilinearMesh.cellGrady
CurvilinearMesh.cellGradz

Cell centered Gradient in the x dimension. Has neumann boundary conditions.

CurvilinearMesh.dim

The dimension of the mesh (1, 2, or 3).

Returns: dimension of the mesh int
CurvilinearMesh.edge

Edge lengths

CurvilinearMesh.edgeCurl

Construct the 3D curl operator.

CurvilinearMesh.faceDiv

Construct divergence operator (face-stg to cell-centres).

CurvilinearMesh.faceDivx

Construct divergence operator in the x component (face-stg to cell-centres).

CurvilinearMesh.faceDivy
CurvilinearMesh.faceDivz

Construct divergence operator in the z component (face-stg to cell-centers).

CurvilinearMesh.gridCC

Cell-centered grid

CurvilinearMesh.gridEx

Edge staggered grid in the x direction.

CurvilinearMesh.gridEy

Edge staggered grid in the y direction.

CurvilinearMesh.gridEz

Edge staggered grid in the z direction.

CurvilinearMesh.gridFx

Face staggered grid in the x direction.

CurvilinearMesh.gridFy

Face staggered grid in the y direction.

CurvilinearMesh.gridFz

Face staggered grid in the y direction.

CurvilinearMesh.gridN

Nodal grid.

CurvilinearMesh.nC

Total number of cells

Return type: int nC
CurvilinearMesh.nCx

Number of cells in the x direction

Return type: int nCx
CurvilinearMesh.nCy

Number of cells in the y direction

Return type: int nCy or None if dim < 2
CurvilinearMesh.nCz

Number of cells in the z direction

Return type: int nCz or None if dim < 3
CurvilinearMesh.nE

Total number of edges.

Returns: nE int = sum([nEx, nEy, nEz])
CurvilinearMesh.nEx

Number of x-edges

Return type: int nEx
CurvilinearMesh.nEy

Number of y-edges

Return type: int nEy
CurvilinearMesh.nEz

Number of z-edges

Return type: int nEz
CurvilinearMesh.nF

Total number of faces.

Return type: int sum([nFx, nFy, nFz])
CurvilinearMesh.nFx

Number of x-faces

Return type: int nFx
CurvilinearMesh.nFy

Number of y-faces

Return type: int nFy
CurvilinearMesh.nFz

Number of z-faces

Return type: int nFz
CurvilinearMesh.nN

Total number of nodes

Return type: int nN
CurvilinearMesh.nNx

Number of nodes in the x-direction

Return type: int nNx
CurvilinearMesh.nNy

Number of nodes in the y-direction

Return type: int nNy or None if dim < 2
CurvilinearMesh.nNz

Number of nodes in the z-direction

Return type: int nNz or None if dim < 3
CurvilinearMesh.nodalGrad

Construct gradient operator (nodes to edges).

CurvilinearMesh.nodalLaplacian

Construct laplacian operator (nodes to edges).

CurvilinearMesh.nodes

List of arrays describing the node locations, a list (each item is a list or numpy array of <class ‘float’>, <class ‘int’> with shape (*, *) or (*, *, *)) with length between 2 and 3

Type: nodes (a list of Array)
CurvilinearMesh.normals

calling this will average the computed normals so that there is one per face. This is especially relevant in 3D, as there are up to 4 different normals for each face that will be different.

To reshape the normals into a matrix and get the y component:

NyX, NyY, NyZ = M.r(M.normals, 'F', 'Fy', 'M')

Type: Face normals
CurvilinearMesh.reference_is_rotated

True if the axes are rotated from the traditional <X,Y,Z> system with vectors of $$(1,0,0)$$, $$(0,1,0)$$, and $$(0,0,1)$$

CurvilinearMesh.reference_system

cartesian

Type: 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
CurvilinearMesh.rotation_matrix

Builds a rotation matrix to transform coordinates from their coordinate system into a conventional cartesian system. This is built off of the three axis_u, axis_v, and axis_w properties; these mapping coordinates use the letters U, V, and W (the three letters preceding X, Y, and Z in the alphabet) to define the projection of the X, Y, and Z durections. These UVW vectors describe the placement and transformation of the mesh’s coordinate sytem assuming at most 3 directions.

Why would you want to use these UVW mapping vectors the this rotation_matrix property? They allow us to define the realationship between local and global coordinate systems and provide a tool for switching between the two while still maintaing the connectivity of the mesh’s cells. For a visual example of this, please see the figure in the docs for the InterfaceVTK.

CurvilinearMesh.tangents

Edge tangents

CurvilinearMesh.vnC

Total number of cells in each direction

Return type: numpy.ndarray [nCx, nCy, nCz]
CurvilinearMesh.vnE

Total number of edges in each direction

Returns: vnE (numpy.ndarray = [nEx, nEy, nEz], (dim, )) .. plot:: – :include-source:import discretize import numpy as np M = discretize.TensorMesh([np.ones(n) for n in [2,3]]) M.plotGrid(edges=True, showIt=True)
CurvilinearMesh.vnEx

Number of x-edges in each direction

Return type: numpy.ndarray vnEx
CurvilinearMesh.vnEy

Number of y-edges in each direction

Return type: numpy.ndarray vnEy or None if dim < 2
CurvilinearMesh.vnEz

Number of z-edges in each direction

Return type: numpy.ndarray vnEz or None if dim < 3
CurvilinearMesh.vnF

Total number of faces in each direction

Return type: numpy.ndarray [nFx, nFy, nFz], (dim, )
import discretize
import numpy as np
M = discretize.TensorMesh([np.ones(n) for n in [2,3]])
M.plotGrid(faces=True, showIt=True)

CurvilinearMesh.vnFx

Number of x-faces in each direction

Return type: numpy.ndarray vnFx
CurvilinearMesh.vnFy

Number of y-faces in each direction

Return type: numpy.ndarray vnFy or None if dim < 2
CurvilinearMesh.vnFz

Number of z-faces in each direction

Return type: numpy.ndarray vnFz or None if dim < 3
CurvilinearMesh.vnN

Total number of nodes in each direction

Return type: numpy.ndarray [nNx, nNy, nNz]
CurvilinearMesh.vol

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

CurvilinearMesh.x0

origin of the mesh (dim, ), a list or numpy array of <class ‘float’>, <class ‘int’> with shape (*)

Type: x0 (Array)

Methods¶

CurvilinearMesh.copy(self)

Make a copy of the current mesh

classmethod CurvilinearMesh.deserialize(value, trusted=False, strict=False, assert_valid=False, **kwargs)

Creates HasProperties instance from serialized dictionary

This uses the Property deserializers to deserialize all JSON-compatible dictionary values into their corresponding Property values on a new instance of a HasProperties class. Extra keys in the dictionary that do not correspond to Properties will be ignored.

Parameters:

• value - Dictionary to deserialize new instance from.
• trusted - If True (and if the input dictionary has '__class__' keyword and this class is in the registry), the new HasProperties class will come from the dictionary. If False (the default), only the HasProperties class this method is called on will be constructed.
• strict - Requires '__class__', if present on the input dictionary, to match the deserialized instance’s class. Also disallows unused properties in the input dictionary. Default is False.
• assert_valid - Require deserialized instance to be valid. Default is False.
• Any other keyword arguments will be passed through to the Property deserializers.
CurvilinearMesh.equal(self, other)

Determine if two HasProperties instances are equivalent

Equivalence is determined by checking if all Property values on two instances are equal, using Property.equal.

static CurvilinearMesh.from_omf(element)

Convert an OMF element to it’s proper discretize type. Automatically determines the output type. Returns both the mesh and a dictionary of model arrays.

CurvilinearMesh.getBCProjWF(self, BC, discretization='CC')

The weak form boundary condition projection matrices.

Example

# Neumann in all directions
BC = 'neumann'

# 3D, Dirichlet in y Neumann else
BC = ['neumann', 'dirichlet', 'neumann']

# 3D, Neumann in x on bottom of domain, Dirichlet else
BC = [['neumann', 'dirichlet'], 'dirichlet', 'dirichlet']

CurvilinearMesh.getBCProjWF_simple(self, discretization='CC')

The weak form boundary condition projection matrices when mixed boundary condition is used

CurvilinearMesh.getEdgeInnerProduct(self, prop=None, invProp=False, invMat=False, doFast=True)

Generate the edge inner product matrix

Parameters: prop (numpy.ndarray) – material property (tensor properties are possible) at each cell center (nC, (1, 3, or 6)) invProp (bool) – inverts the material property invMat (bool) – inverts the matrix doFast (bool) – do a faster implementation if available. M, the inner product matrix (nE, nE) scipy.sparse.csr_matrix
CurvilinearMesh.getEdgeInnerProductDeriv(self, prop, doFast=True, invProp=False, invMat=False)
Parameters: prop (numpy.ndarray) – material property (tensor properties are possible) at each cell center (nC, (1, 3, or 6)) doFast (bool) – do a faster implementation if available. invProp (bool) – inverts the material property invMat (bool) – inverts the matrix dMdm, the derivative of the inner product matrix (nE, nC*nA) scipy.sparse.csr_matrix
CurvilinearMesh.getFaceInnerProduct(self, prop=None, invProp=False, invMat=False, doFast=True)

Generate the face inner product matrix

Parameters: prop (numpy.ndarray) – material property (tensor properties are possible) at each cell center (nC, (1, 3, or 6)) invProp (bool) – inverts the material property invMat (bool) – inverts the matrix doFast (bool) – do a faster implementation if available. M, the inner product matrix (nF, nF) scipy.sparse.csr_matrix
CurvilinearMesh.getFaceInnerProductDeriv(self, prop, doFast=True, invProp=False, invMat=False)
Parameters: prop (numpy.ndarray) – material property (tensor properties are possible) at each cell center (nC, (1, 3, or 6)) doFast – bool do a faster implementation if available. invProp (bool) – inverts the material property invMat (bool) – inverts the matrix dMdmu(u), the derivative of the inner product matrix for a certain u scipy.sparse.csr_matrix
CurvilinearMesh.plotGrid(self, ax=None, nodes=False, faces=False, centers=False, edges=False, lines=True, showIt=False, **kwargs)

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

import discretize
X, Y = discretize.utils.exampleLrmGrid([3, 3], 'rotate')
M = discretize.CurvilinearMesh([X, Y])
M.plotGrid(showIt=True)

CurvilinearMesh.plotImage(self, I, ax=None, showIt=False, grid=False, clim=None)
CurvilinearMesh.projectEdgeVector(self, eV)

Given a vector, eV, in cartesian coordinates, this will project it onto the mesh using the tangents

Parameters: eV (numpy.ndarray) – edge vector with shape (nE, dim) numpy.ndarray projected edge vector, (nE, )
CurvilinearMesh.projectFaceVector(self, fV)

Given a vector, fV, in cartesian coordinates, this will project it onto the mesh using the normals

Parameters: fV (numpy.ndarray) – face vector with shape (nF, dim) numpy.ndarray projected face vector, (nF, )
CurvilinearMesh.r(self, x, xType='CC', outType='CC', format='V')

r is a quick reshape command that will do the best it can at giving you what you want.

For example, you have a face variable, and you want the x component of it reshaped to a 3D matrix.

r can fulfil your dreams:

mesh.r(V, 'F', 'Fx', 'M')
|   |     |    |
|   |     |    {
|   |     |      How: 'M' or ['V'] for a matrix
|   |     |      (ndgrid style) or a vector (n x dim)
|   |     |    }
|   |     {
|   |       What you want: ['CC'], 'N',
|   |                       'F', 'Fx', 'Fy', 'Fz',
|   |                       'E', 'Ex', 'Ey', or 'Ez'
|   |     }
|   {
|     What is it: ['CC'], 'N',
|                  'F', 'Fx', 'Fy', 'Fz',
|                  'E', 'Ex', 'Ey', or 'Ez'
|   }
{
The input: as a list or ndarray
}


For example:

# Separates each component of the Ex grid into 3 matrices
Xex, Yex, Zex = r(mesh.gridEx, 'Ex', 'Ex', 'M')

# Given an edge vector, return just the x edges as a vector
XedgeVector = r(edgeVector, 'E', 'Ex', 'V')

# Separates each component of the edgeVector into 3 vectors
eX, eY, eZ = r(edgeVector, 'E', 'E', 'V')

CurvilinearMesh.save(self, filename='mesh.json', verbose=False)

Save the mesh to json :param str file: filename for saving the casing properties :param str directory: working directory for saving the file

CurvilinearMesh.serialize(self, include_class=True, save_dynamic=False, **kwargs)

Serializes a HasProperties instance to dictionary

This uses the Property serializers to serialize all Property values to a JSON-compatible dictionary. Properties that are undefined are not included. If the HasProperties instance contains a reference to itself, a properties.SelfReferenceError will be raised.

Parameters:

• include_class - If True (the default), the name of the class will also be saved to the serialized dictionary under key '__class__'
• save_dynamic - If True, dynamic properties are written to the serialized dict (default: False).
• Any other keyword arguments will be passed through to the Property serializers.
CurvilinearMesh.setCellGradBC(self, BC)

Function that sets the boundary conditions for cell-centred derivative operators.

Example

..code:: python

# Neumann in all directions BC = ‘neumann’

# 3D, Dirichlet in y Neumann else BC = [‘neumann’, ‘dirichlet’, ‘neumann’]

# 3D, Neumann in x on bottom of domain, Dirichlet else BC = [[‘neumann’, ‘dirichlet’], ‘dirichlet’, ‘dirichlet’]

CurvilinearMesh.toVTK(mesh, models=None)

Convert this mesh object to it’s proper VTK or pyvista data object with the given model dictionary as the cell data of that dataset.

Parameters: models (dict(numpy.ndarray)) – Name(‘s) and array(‘s). Match number of cells
CurvilinearMesh.to_omf(mesh, models=None)

Convert this mesh object to it’s proper omf data object with the given model dictionary as the cell data of that dataset.

Parameters: models (dict(numpy.ndarray)) – Name(‘s) and array(‘s). Match number of cells
CurvilinearMesh.to_vtk(mesh, models=None)

Convert this mesh object to it’s proper VTK or pyvista data object with the given model dictionary as the cell data of that dataset.

Parameters: models (dict(numpy.ndarray)) – Name(‘s) and array(‘s). Match number of cells
CurvilinearMesh.validate(self)

Call all registered class validator methods

These are all methods decorated with @properties.validator. Validator methods are expected to raise a ValidationError if they fail.

CurvilinearMesh.writeVTK(mesh, filename, models=None, directory='')

Makes and saves a VTK object from this mesh and given models

Parameters: filename (str) – path to the output vtk file or just its name if directory is specified models (dict) – dictionary of numpy.array - Name(‘s) and array(‘s). Match number of cells directory (str) – directory where the UBC GIF file lives
CurvilinearMesh.write_vtk(mesh, filename, models=None, directory='')

Makes and saves a VTK object from this mesh and given models

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