microstructpy.meshing package

class microstructpy.meshing.PolyMesh(points, facets, regions, seed_numbers=None, phase_numbers=None, facet_neighbors=None, volumes=None)[source]

Bases: object

Polygonal/Polyhedral mesh.

The PolyMesh class contains the points, edges, regions, etc. in a polygon (2D) or polyhedron (3D) mesh.

The points attribute is a numpy array containing the (x, y) or (x, y, z) coordinates of each point in the mesh. This is the only attribute that contains floating point numbers. The rest contain indices/integers.

The facets attribute describes the interfaces between the polygons/ polyhedra. In 2D, these interfaces are line segments and each facet contains the indices of the points at each end of the line segment. These indices are unorderd. In 3D, the interfaces are polygons so each facet contains the indices of the points on that polygon. These indices are ordered such that neighboring keypoints are connected by line segments that form the polygon.

The regions attribute contains the area (2D) or volume (3D). In 2D, a region is given by an ordered list of facets, or edges, that enclose the polygon. In 3D, the region is given by an un-ordered list of facets, or polygons, that enclose the polyhedron.

For each region, there is also an associated seed number and material phase. These data are stored in the seed_number and phase_number attributes, which have the same length as the regions list.

Parameters:
  • points (numpy.ndarray) – An Nx2 or Nx3 array of coordinates in the mesh.
  • facets (list) – List of facets between regions. In 2D, this is a list of edges (Nx2). In 3D, this is a list of 3D polygons.
  • regions (list) – A list of polygons (2D) or polyhedra (3D), with each element of the list being a list of facet indices.
  • seed_numbers (list, optional) – The seed number associated with each region.
  • phase_numbers (list, optional) – The phase number associated with each region.
  • facet_neighbors (list, optional) – The region numbers on either side of each facet
  • volumes (list, optional) – The area/volume of each region.
classmethod from_file(filename)[source]

Read PolyMesh from file.

This function reads in a polygon 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:The instance of the class written to the file.
Return type:PolyMesh
classmethod from_seeds(seedlist, domain)[source]

Create from SeedList and Domain.

This function creates a polygon/polyhedron mesh from a seed list and a domain. It relies on the pyvoro package, which wraps Voro++. The mesh is a Voronoi power diagram / Laguerre tessellationself.

The pyvoro package operates on rectangular domains, so other domains are meshed by reflecting seeds across their boundaries and meshing a larger domain. The reflection process will double the number of points in the voro++ input, which may cause a noticable slow-down for high-population microstructures. This reflection process is unnecessary for rectangular and box domains.

Parameters:
  • seedlist (SeedList) – A list of seeds in the microstructure.
  • domain (Domain) – The domain to be filled by the seeds.
Returns:

A polygon/polyhedron mesh.

Return type:

PolyMesh

plot(**kwargs)[source]

Plot the mesh.

This function plots the polygon mesh. The keyword arguments are passed though to matplotlib.

Parameters:**kwargs – Keyword arguments for matplotlib.
plot_facets(**kwargs)[source]

Plot PolyMesh facets.

This function plots the facets of the polygon mesh, rather than the regions.

Parameters:**kwargs (dict) – Keyword arguments for matplotlib.
write(filename, format='txt')[source]

Write the mesh to a fileself.

This function writes the polygon/polyhedron mesh to a file. The format of the file can be specified, with the options described in the table below.

PolyMesh Write Formats
Format format kD Description
Text String txt ND The Python string representation of the mesh. Human readable, but not in a standard file format.
POLY File poly 2D A poly file that contains a planar straight line graph (PSLG). This file can be read by the Triangle program from J. Shewchuk.
PLY File ply ND A PLY file containing the mesh facets.
VTK Legacy vtk 3D A VTK file containing the mesh as a POLYDATA dataset. Note: seed number and phase number information are not written to the VTK file.

The text string output file is meant solely for saving the polygon/ polyhedron mesh as an intermediate step in the meshing process. The other file types lists are meant for processing and interpretation by other programs. The format for the text string file is:

Mesh Points: <numPoints>
    x1, y1(, z1)      <- tab character at line start
    x2, y2(, z2)
    ...
    xn, yn(, zn)
Mesh Facets: <numFacets>
    f1_1, f1_2, f1_3, ...
    f2_1, f2_2, f2_3, ...
    ...
    fn_1, fn_2, fn_3, ...
Mesh Regions: <numRegions>
    r1_1, r1_2, r1_3, ...
    r2_1, r2_2, r2_3, ...
    ...
    rn_1, rn_2, rn_3, ...
Seed Numbers: <numRegions>
    s1
    s2
    ...
    sn
Phase Numbers: <numRegions>
    p1
    p2
    ...
    pn

For example:

Mesh Points: 4
    0.0, 0.0
    1.0, 0.0
    3.0, 2.0
    2.0, 2.0
Mesh Facets: 5
    0, 1
    1, 2
    2, 3
    3, 0
    1, 3
Mesh Regions: 2
    0, 4, 3
    1, 2, 4
Seed Numbers: 2
    0
    1
Phase Numbers: 2
    0
    0

Note that everything is indexed from 0 since this is produced in Python. In this example, the polygon mesh contains a parallelogram that has been divided into two triangles. In general, the regions do not need to have the same number of facets.

See also

.poly files Description and examples of poly files.
PLY - Polygon File Format Description and examples of ply files.
File Formats for VTK Version 4.2 PDF guide for VTK legacy format.
Parameters:
  • filename (str) – Name of the file to be written.
  • format (str) – Format of the data in the file.
class microstructpy.meshing.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.