microstructpy.meshing.trimesh module

Triangle/Tetrahedron Meshing

This module contains the class definition for the TriMesh class.

class microstructpy.meshing.trimesh.TriMesh(points, elements, element_attributes=None, facets=None, facet_attributes=None)[source]

Bases: object

Triangle/Tetrahedron mesh.

The TriMesh class contains the points, facets, and elements in a triangle/ tetrahedron mesh, also called an unstructured grid.

The points attribute is an Nx2 or Nx3 list of points in the mesh. The elements attribute contains the Nx3 or Nx4 list of the points at the corners of each triangle/tetrahedron. A list of facets can also be included, though it is optional and does not need to include every facet in the mesh. Attributes can also be assigned to the elements and facets, though they are also optional.

Parameters:
  • points (list or numpy.ndarray) – List of coordinates in the mesh.
  • elements (list or numpy.ndarray) – List of indices of the points at the corners of each element. The shape should be Nx3 in 2D or Nx4 in 3D.
  • element_attributes (list or numpy.ndarray, optional) – A number associated with each element.
  • facets (list or numy.ndarray, optional) – A list of facets in the mesh. The shape should be Nx2 in 2D or Nx3 in 3D.
  • facet_attributes (list or numpy.ndarray, optional) – A number associated with each facet.
classmethod from_file(filename)[source]

Read TriMesh from file.

This function reads in a triangular mesh from a file and creates an instance from that file. Currently the only supported file type is the output from write() with the format='str' option.

Parameters:filename (str) – Name of file to read from.
Returns:An instance of the class.
Return type:TriMesh
classmethod from_polymesh(polymesh, phases=None, min_angle=0, max_volume=inf, max_edge_length=inf)[source]

Create TriMesh from PolyMesh.

This constuctor creates a triangle/tetrahedron mesh from a polygon mesh (PolyMesh). Polygons of the same seed number are merged and the element attribute is set to the seed number it is within. The facets between seeds are saved to the mesh and the index of the facet is stored in the facet attributes.

Since the PolyMesh can include phase numbers for each region, additional information about the phases can be included as an input. The “phases” input should be a list of material phase dictionaries, formatted according to the Phase Dictionaries guide.

The minimum angle, maximum volume, and maximum edge length options provide quality controls for the mesh. The phase type option can take several values, described below.

Phase Type Options
Value Description
crystalline, granular, solid These options all create a mesh where cells of the same seed number are merged, but cells are not merged across seeds. (default)
amorphous, glass, matrix These options create a mesh where cells of the same phase number are merged, creating an amorphous region in the mesh.
crack, hole, void These options are the same as the amorphous options described above, except that these regions are treated as holes in the mesh.
Parameters:
  • polymesh (PolyMesh) – A polygon/polyhedron mesh.
  • phases (list) – A list of dictionaries containing options for each phase.
  • min_angle (float) – The minimum interior angle of an element.
  • max_volume (float) – The default maximum cell volume, used if one is not set for each phase.
  • max_edge_length (float) – The maximum edge length of elements along grain boundaries. Currently only supported in 2D.
plot(**kwargs)[source]

Plot the mesh.

This method plots the mesh using matplotlib. In 2D, the elements are plotted using a matplotlib PolyCollection. In 3D, the facets are plotted using a Poly3DCollection.

Parameters:**kwargs – Keyword arguments that are passed through to matplotlib.
write(filename, format='str', seeds=None, polymesh=None)[source]

Write mesh to file.

This function writes the contents of the mesh to a file. There are some options for the format of the file, described below.

TriMesh Write Formats
Format format kD Description
Abaqus abaqus ND An Abaqus input file.
Text String str ND The results of __str__
TetGen / Triangle tet/tri ND Node and element files formatted according to the TetGen and Triangle file standard. The filename input above should be the basename without extensions. The extensions .node and .ele will be added. See the links below for more information about the file formats.
VTK Legacy vtk 3D A VTK file
Parameters:
  • filename (str) – The name of the file to write. In the cases of TetGen/Triangle, this is the basename of the files.
  • format (str) – The format of the output.
  • seeds (SeedList) – List of seeds. If given, will also write phase number to VTK files. This assumes the element_attributes field contains the seed number of each element.
  • polymesh (PolyMesh) – Polygonal mesh used for generating the the triangular mesh. If given, will add surface unions to Abaqus files - for easier specification of boundary conditions.