discretize.TreeMesh#
- class discretize.TreeMesh(h=None, origin=None, diagonal_balance=None, **kwargs)[source]#
Class for QuadTree (2D) and OcTree (3D) meshes.
Tree meshes are numerical grids where the dimensions of each cell are powers of 2 larger than some base cell dimension. Unlike the
TensorMesh
class, gridded locations and numerical operators for instances ofTreeMesh
cannot be simply constructed using tensor products. Furthermore, each cell is an instance ofTreeCell
.Each cell of a TreeMesh has a certain level associated with it which describes its height in the structure of the tree. The tree mesh is refined by specifying what level you want in a given location. They start at level 0, defining the largest possible cell on the mesh, and go to a level of max_level describing the finest possible cell on the mesh. TreeMesh` contains several refinement functions used to design the mesh, starting with a general purpose refinement function, along with several faster specialized refinement functions based on fundamental geometric entities:
refine -> The general purpose refinement function supporting arbitrary defined functions
insert_cells
refine_ball
refine_line
refine_triangle
refine_box
refine_tetrahedron
refine_vertical_trianglular_prism
refine_bounding_box
refine_points
refine_surface
Like array indexing in python, you can also supply negative indices as a level arguments to these functions to index levels in a reveresed order (i.e. -1 is equivalent to max_level).
- Parameters:
- h(
dim
) iterableof
int
,numpy.ndarray
,or
tuple
Defines the cell widths of the underlying tensor mesh along each axis. The length of the iterable object is equal to the dimension of the mesh (2 or 3). For a 3D mesh, the list would have the form [hx, hy, hz]. The number of cells along each axis must be a power of 2 .
Along each axis, the user has 3 choices for defining the cells widths for the underlying tensor mesh:
int
-> A unit interval is equally discretized into N cells.numpy.ndarray
-> The widths are explicity given for each cellthe widths are defined as a
list
oftuple
of the form (dh, nc, [npad]) where dh is the cell width, nc is the number of cells, and npad (optional) is a padding factor denoting exponential increase/decrease in the cell width for each cell; e.g. [(2., 10, -1.3), (2., 50), (2., 10, 1.3)]
- origin(
dim
) iterable, default: 0 Define the origin or ‘anchor point’ of the mesh; i.e. the bottom-left-frontmost corner. By default, the mesh is anchored such that its origin is at [0, 0, 0].
For each dimension (x, y or z), The user may set the origin 2 ways:
a
scalar
which explicitly defines origin along that dimension.{‘0’, ‘C’, ‘N’} a
str
specifying whether the zero coordinate along each axis is the first node location (‘0’), in the center (‘C’) or the last node location (‘N’) (see Examples).
- diagonal_balancebool,
optional
Whether to balance cells along the diagonal of the tree during construction. This will affect all calls to refine the tree.
- h(
Attributes
area has been deprecated.
areaFx has been deprecated.
areaFy has been deprecated.
areaFz has been deprecated.
Averaging operator from cell centers to edges (scalar quantities).
Averaging operator from cell centers to faces (scalar quantities).
Averaging operator from cell centers to x faces (scalar quantities).
Averaging operator from cell centers to y faces (scalar quantities).
Averaging operator from cell centers to z faces (scalar quantities).
Averaging operator from cell centers to faces (vector quantities).
Averaging operator from edges to cell centers (scalar quantities).
Averaging operator from edges to cell centers (vector quantities).
Averaging operator from edges to faces.
Averaging operator from x-edges to cell centers (scalar quantities).
Averaging operator from y-edges to cell centers (scalar quantities).
Averaging operator from z-edges to cell centers (scalar quantities).
Averaging operator from faces to cell centers (scalar quantities).
Averaging operator from faces to cell centers (vector quantities).
Averaging operator from x-faces to cell centers (scalar quantities).
Averaging operator from y-faces to cell centers (scalar quantities).
Averaging operator from z-faces to cell centers (scalar quantities).
Averaging operator from nodes to cell centers (scalar quantities).
Averaging operator from nodes to edges (scalar quantities).
Averaging operator from nodes to x edges (scalar quantities).
Averaging operator from nodes to y edges (scalar quantities).
Averaging operator from nodes to z edges (scalar quantities).
Averaging operator from nodes to faces (scalar quantities).
Averaging operator from nodes to x faces (scalar quantities).
Averaging operator from nodes to y faces (scalar quantities).
Averaging operator from nodes to z faces (scalar quantities).
Orientation of the first axis.
Orientation of the second axis.
Orientation of the third axis.
Integrate a vector function on the boundary.
Gridded boundary edge locations.
Outward normals of boundary faces.
Represent the operation of integrating a scalar function on the boundary.
Gridded boundary face locations.
Integrate a vector function dotted with the boundary normal.
Gridded boundary node locations.
cellBoundaryInd has been deprecated.
cellGrad has been deprecated.
cellGradBC has been deprecated.
cellGradStencil has been deprecated.
cellGradx has been deprecated.
cellGrady has been deprecated.
cellGradz has been deprecated.
Returns the indices of the x, y (and z) boundary cells.
Gridded cell center locations.
Return x-coordinates of the cell centers along the x-direction.
Return y-coordinates of the cell centers along the y-direction.
Return z-coordinates of the cell centers along the z-direction.
Cell gradient operator (cell centers to faces).
Boundary conditions matrix for the cell gradient operator (Deprecated).
X-derivative operator (cell centers to x-faces).
Y-derivative operator (cell centers to y-faces).
Z-derivative operator (cell centers to z-faces).
The index of all nodes for each cell.
The current state of the cells on the mesh.
Return cell volumes.
The dimension of the mesh (1, 2, or 3).
edge has been deprecated.
edgeCurl has been deprecated.
edgeEx has been deprecated.
edgeEy has been deprecated.
edgeEz has been deprecated.
Edge curl operator (edges to faces)
Returns the lengths of all edges in the mesh.
The index of nodes for every edge.
Unit tangent vectors for all mesh edges.
Gridded edge locations.
Gridded locations of non-hanging x-edges.
Gridded locations of non-hanging y-edges.
Gridded locations of non-hanging z-edges.
faceBoundaryInd has been deprecated.
faceDiv has been deprecated.
faceDivx has been deprecated.
faceDivy has been deprecated.
faceDivz has been deprecated.
Returns the areas of all cell faces.
Returns the indices of the x, y (and z) boundary faces.
Face divergence operator (faces to cell-centres).
Unit normal vectors for all mesh faces.
X-derivative operator (x-faces to cell-centres).
Y-derivative operator (y-faces to cell-centres).
Z-derivative operator (z-faces to cell-centres).
Gridded face locations.
Gridded locations of non-hanging x-faces.
Gridded locations of non-hanging y-faces.
Gridded locations of non-hanging z-faces.
How 'filled' the tree mesh is compared to the underlying tensor mesh.
Whether tree mesh is finalized.
Cell widths along each axis direction.
Gridded cell dimensions.
Gridded locations of hanging x-edges.
Gridded locations of hanging y-edges.
Gridded locations of hanging z-edges.
Gridded locations of hanging x-faces.
Gridded locations of hanging y-faces.
Gridded locations of hanging z-faces.
Gridded hanging node locations.
Width of cells in the x direction.
Width of cells in the y direction.
Width of cells in the z direction.
maxLevel has been deprecated.
Maximum allowable refinement level for the mesh.
Maximum refinement level used.
Total number of cells in the mesh.
Total number of edges in the mesh.
The number of edges in each direction.
Number of x-edges in the mesh.
Number of y-edges in the mesh.
Number of z-edges in the mesh.
Total number of faces in the mesh.
The number of faces in each axis direction.
Number of x-faces in the mesh.
Number of y-faces in the mesh.
Number of z-faces in the mesh.
Total number of hanging edges in all dimensions.
Number of hanging x-edges in the mesh.
Number of hanging y-edges in the mesh.
Number of hanging z-edges in the mesh.
Total number of hanging faces in the mesh.
Number of hanging x-faces in the mesh.
Number of hanging y-faces in the mesh.
Number of hanging z-faces in the mesh.
Number of hanging nodes.
Total number of nodes in the mesh.
Total number of hanging and non-hanging edges in all dimensions.
Number of hanging and non-hanging x-edges in the mesh.
Number of hanging and non-hanging y-edges in the mesh.
Number of hanging and non-hanging z-edges in the mesh.
Total number of hanging and non-hanging faces in the mesh.
Number of hanging and non-hanging x-faces in the mesh.
Number of hanging and non-hanging y-faces in the mesh.
Number of hanging and non-hanging z-faces in the mesh.
Number of hanging and non-hanging nodes.
nodalGrad has been deprecated.
nodalLaplacian has been deprecated.
Nodal gradient operator (nodes to edges)
Not implemented on the TreeMesh.
Gridded non-hanging nodes locations.
Return x-coordinates of the nodes along the x-direction.
Return y-coordinates of the nodes along the y-direction.
Return z-coordinates of the nodes along the z-direction.
normals has been deprecated.
Rotation matrix defining mesh axes relative to Cartesian.
Origin or 'anchor point' of the mesh.
permuteCC has been deprecated.
permuteE has been deprecated.
permuteF has been deprecated.
Permutation matrix re-ordering of cells sorted by x, then y, then z.
Permutation matrix re-ordering of edges sorted by x, then y, then z.
Permutation matrix re-ordering of faces sorted by x, then y, then z.
Projection matrix from all edges to boundary edges.
Projection matrix from all faces to boundary faces.
Projection matrix from all nodes to boundary nodes.
Indicate whether mesh uses standard coordinate axes.
Coordinate reference system.
Alias for
orientation
.Number of cells in each coordinate direction.
Stencil for cell gradient operator (cell centers to faces).
Differencing operator along x-direction to total (including hanging) x faces.
Differencing operator along y-direction to total (including hanging) y faces.
Differencing operator along z-direction to total (including hanging) z faces.
tangents has been deprecated.
Gridded hanging and non-hanging nodes locations.
vectorCCx has been deprecated.
vectorCCy has been deprecated.
vectorCCz has been deprecated.
vectorNx has been deprecated.
vectorNy has been deprecated.
vectorNz has been deprecated.
Vector number of total edges along each axis.
Vector number of total faces along each axis.
vol has been deprecated.
Alias for the
origin
.cell_bounds
Methods
Average matrix for cell center to total (including hanging) x faces.
Average matrix for cell center to total (including hanging) y faces.
Average matrix for cell center to total (including hanging) z faces.
cell_gradient_weak_form_robin
([alpha, beta, ...])Create Robin conditions pieces for weak form of the cell gradient operator (cell centers to faces).
cell_levels_by_index
(indices)Fast function to return a list of levels for the given cell indices.
closest_points_index
(locations[, grid_loc, ...])Find the indicies for the nearest grid location for a set of points.
copy
()Make a copy of the current mesh.
deserialize
(items, **kwargs)Create this mesh from a dictionary of attributes.
edge_divergence_weak_form_robin
([alpha, ...])Create Robin conditions pieces for weak form of the edge divergence operator (edges to nodes).
equals
(other)Compare the current mesh with another mesh to determine if they are identical.
finalize
(self)Finalize the
TreeMesh
.from_omf
(element)Convert an
omf
object to adiscretize
mesh.getBCProjWF
(*args, **kwargs)getBCProjWF has been removed.
getBCProjWF_simple
(*args, **kwargs)getBCProjWF_simple has been removed.
getEdgeInnerProduct
(*args, **kwargs)getEdgeInnerProduct has been removed.
getEdgeInnerProductDeriv
(*args, **kwargs)getEdgeInnerProductDeriv has been removed.
getFaceInnerProduct
(*args, **kwargs)getFaceInnerProduct has been removed.
getFaceInnerProductDeriv
(*args, **kwargs)getFaceInnerProductDeriv has been removed.
getInterpolationMat
(*args, **kwargs)getInterpolationMat has been removed.
getTensor
(*args, **kwargs)getTensor has been removed.
get_BC_projections
(BC[, discretization])Create the weak form boundary condition projection matrices.
get_BC_projections_simple
([discretization])Create weak form boundary condition projection matrices for mixed boundary condition.
get_boundary_cells
(self[, active_ind, direction])Return the indices of boundary cells in a given direction given an active index array.
get_cells_along_line
(self, x0, x1)Find the cells in order along a line segment.
get_cells_in_aabb
(self, x_min, ...)Find the indices of cells that intersect an axis aligned bounding box (aabb)
get_cells_in_ball
(self, center, double radius)Find the indices of cells that intersect a ball
get_cells_in_tetrahedron
(self, tetra)Find the indices of cells that intersect a tetrahedron.
get_cells_in_triangle
(self, triangle)Find the indices of cells that intersect a triangle.
Find the indices of cells that intersect a vertical triangular prism.
get_cells_on_line
(self, segment)Find the cells intersecting a line segment.
get_cells_on_plane
(self, origin, normal)Find the indices of cells that intersect a plane.
get_containing_cells
(self, points)Return the cells containing the given points.
get_edge_inner_product
([model, ...])Generate the edge inner product matrix or its inverse.
get_edge_inner_product_deriv
(model[, ...])Get a function handle to multiply vector with derivative of edge inner product matrix (or its inverse).
get_edge_inner_product_line
([model, ...])Generate the edge inner product line matrix or its inverse.
get_edge_inner_product_line_deriv
(model[, ...])Get a function handle to multiply a vector with derivative of edge inner product line matrix (or its inverse).
get_edge_inner_product_surface
([model, ...])Generate the edge inner product surface matrix or its inverse.
Get a function handle to multiply a vector with derivative of edge inner product surface matrix (or its inverse).
get_face_inner_product
([model, ...])Generate the face inner product matrix or its inverse.
get_face_inner_product_deriv
(model[, ...])Get a function handle to multiply a vector with derivative of face inner product matrix (or its inverse).
get_face_inner_product_surface
([model, ...])Generate the face inner product matrix or its inverse.
Get a function handle to multiply a vector with derivative of face inner product surface matrix (or its inverse).
get_interpolation_matrix
(locs[, ...])Construct a linear interpolation matrix from mesh.
get_overlapping_cells
(self, rectangle)Find the indicis of cells that overlap the given rectangle
get_tensor
(key)Return the base 1D arrays for a specified mesh tensor.
insert_cells
(self, points, levels[, ...])Insert cells into the
TreeMesh
that contain given points.isInside
(*args, **kwargs)isInside has been removed.
is_inside
(pts[, location_type])Determine which points lie within the mesh.
number
(self)Number the cells, nodes, faces, and edges of the TreeMesh.
plotGrid
(*args, **kwargs)plotGrid has been deprecated.
plotImage
(*args, **kwargs)plotImage has been deprecated.
plotSlice
(*args, **kwargs)plotSlice has been deprecated.
plot_3d_slicer
(v[, xslice, yslice, zslice, ...])Plot slices of a 3D volume, interactively (scroll wheel).
plot_grid
([ax, nodes, faces, centers, ...])Plot the grid for nodal, cell-centered and staggered grids.
plot_image
(v[, v_type, grid, view, ax, ...])Plot quantities defined on a given mesh.
plot_slice
(v[, v_type, normal, ind, ...])Plot a slice of fields on the given 3D mesh.
point2index
(locs)Find cells that contain the given points.
projectEdgeVector
(*args, **kwargs)projectEdgeVector has been removed.
projectFaceVector
(*args, **kwargs)projectFaceVector has been removed.
project_edge_vector
(edge_vectors)Project vectors to the edges of the mesh.
project_face_vector
(face_vectors)Project vectors onto the faces of the mesh.
readModelUBC
(*args, **kwargs)readModelUBC has been removed.
readUBC
(file_name[, directory])Read 3D Tree mesh from UBC-GIF formatted file.
read_UBC
(file_name[, directory])Read 3D tree mesh (OcTree mesh) from UBC-GIF formatted file.
read_model_UBC
(file_name[, directory])Read UBC-GIF formatted file model file for 3D tree mesh (OcTree).
refine
(self, function[, finalize, ...])Refine
TreeMesh
with user-defined function.refine_ball
(self, points, radii, levels[, ...])Refine
TreeMesh
using radial distance (ball) and refinement level for a cluster of points.refine_bounding_box
(points[, level, ...])Refine within a bounding box based on the maximum and minimum extent of scattered points.
refine_box
(self, x0s, x1s, levels[, ...])Refine the
TreeMesh
within the axis aligned boxes to the desired level.refine_line
(self, path, levels[, finalize, ...])Refine the
TreeMesh
along the line segment to the desired level.refine_plane
(self, origins, normals, levels)Refine the
TreeMesh
along a plane to the desired level.refine_points
(points[, level, ...])Refine the mesh at given points to the prescribed level.
refine_surface
(xyz[, level, ...])Refine along a surface triangulated from xyz to the prescribed level.
refine_tetrahedron
(self, tetra, levels[, ...])Refine the
TreeMesh
along the tetrahedron to the desired level.refine_triangle
(self, triangle, levels[, ...])Refine the
TreeMesh
along the triangle to the desired level.refine_vertical_trianglular_prism
(self, ...)Refine the
TreeMesh
along the trianglular prism to the desired level.save
([file_name, verbose])Save the mesh to json.
Represent the mesh's attributes as a dictionary.
setCellGradBC
(*args, **kwargs)setCellGradBC has been removed.
Set boundary conditions for derivative operators acting on cell-centered quantities.
toVTK
([models])Convert mesh (and models) to corresponding VTK or PyVista data object.
to_dict
()Represent the mesh's attributes as a dictionary.
to_omf
([models])Convert to an
omf
data object.to_vtk
([models])Convert mesh (and models) to corresponding VTK or PyVista data object.
validate
()Return the validation state of the mesh.
writeModelUBC
(*args, **kwargs)writeModelUBC has been removed.
writeUBC
(*args, **kwargs)writeUBC has been removed.
writeVTK
(file_name[, models, directory])Convert mesh (and models) to corresponding VTK or PyVista data object then writes to file.
write_UBC
(file_name[, models, directory])Write OcTree mesh (and models) to UBC-GIF formatted files.
write_model_UBC
(file_name, model[, directory])Write 3D tree model (OcTree) to UBC-GIF formatted file.
write_vtk
(file_name[, models, directory])Convert mesh (and models) to corresponding VTK or PyVista data object then writes to file.
Examples
Here we generate a basic 2D tree mesh.
>>> from discretize import TreeMesh >>> import numpy as np >>> import matplotlib.pyplot as plt
Define base mesh (domain and finest discretization),
>>> dh = 5 # minimum cell width (base mesh cell width) >>> nbc = 64 # number of base mesh cells >>> h = dh * np.ones(nbc) >>> mesh = TreeMesh([h, h])
Define corner points for a rectangular box, and subdived the mesh within the box to the maximum refinement level.
>>> x0s = [120.0, 80.0] >>> x1s = [240.0, 160.0] >>> levels = [mesh.max_level] >>> mesh.refine_box(x0s, x1s, levels)
>>> mesh.plot_grid() >>> plt.show()
(
Source code
,png
,pdf
)