microstructpy.meshing.PolyMesh¶
- 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 (list or 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 or numpy.ndarray) – (optional) The seed number associated with each region. Defaults to 0 for all regions.
phase_numbers (list or numpy.ndarray) – (optional) The phase number associated with each region. Defaults to 0 for all regions.
facet_neighbors (list or numpy.ndarray) – (optional) The region numbers on either side of each facet. If not givien, a neighbor list is computed from
regions
.volumes (list or numpy.ndarray) – (optional) The area/volume of each region. If not given, region volumes are calculated based on
points
,facets
, andregions
.
- 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 theformat='txt'
option.
- classmethod from_seeds(seedlist, domain, edge_opt=False, n_iter=100, verbose=False)[source]¶
Create from
SeedList
and a 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 in 2D by meshing in a bounding box then the boundary cells are clipped to the domain boundary. Currently non-rectangular domains in 3D are not supported.
This function also includes the option to maximize the shortest edges in the polygonal/polyhedral mesh. Short edges cause numerical issues in finite element analysis - setting edge_opt to True can improve mesh quality with minimal changes to the microstructure.
- Parameters
seedlist (SeedList) – A list of seeds in the microstructure.
domain (from
microstructpy.geometry
) – The domain to be filled by the seed.edge_opt (bool) – (optional) This option will maximize the minimum edge length in the PolyMesh. The seeds associated with the shortest edge are displaced randomly to find improvement and this process iterates until n_iter attempts have been made for a given edge. Defaults to False.
n_iter (int) – (optional) Maximum number of iterations per edge during optimization. Ignored if edge_opt set to False. Defaults to 100.
verbose (bool) – (optional) Print status of edge optimization to screen. Defaults to False.
- Returns
A polygon/polyhedron mesh.
- Return type
- plot(index_by='seed', material=[], loc=0, **kwargs)[source]¶
Plot the mesh.
This function plots the polygon mesh. In 2D, this creates a class:matplotlib.collections.PolyCollection and adds it to the current axes. In 3D, it creates a
mpl_toolkits.mplot3d.art3d.Poly3DCollection
and adds it to the current axes. The keyword arguments are passed though to matplotlib.- Parameters
index_by (str) – (optional) {‘facet’ | ‘material’ | ‘seed’} Flag for indexing into the other arrays passed into the function. For example,
plot(index_by='material', color=['blue', 'red'])
will plot the regions withphase_number
equal to 0 in blue, and regions withphase_number
equal to 1 in red. The facet option is only available for 3D plots. Defaults to ‘seed’.material (list) – (optional) Names of material phases. One entry per material phase (the
index_by
argument is ignored). If this argument is set, a legend is added to the plot with one entry per material.loc (int or str) – (optional) The location of the legend, if ‘material’ is specified. This argument is passed directly through to
matplotlib.pyplot.legend()
. Defaults to 0, which is ‘best’ in matplotlib.**kwargs – Keyword arguments for matplotlib.
- plot_facets(index_by='seed', hide_interior=True, **kwargs)[source]¶
Plot PolyMesh facets.
This function plots the facets of the polygon mesh, rather than the regions. In 2D, it adds a
matplotlib.collections.LineCollection
to the current axes. In 3D, it adds ampl_toolkits.mplot3d.art3d.Poly3DCollection
withfacecolors='none'
. The keyword arguments are passed though to matplotlib.- Parameters
index_by (str) – (optional) {‘facet’ | ‘material’ | ‘seed’} Flag for indexing into the other arrays passed into the function. For example,
plot(index_by='material', color=['blue', 'red'])
will plot the regions withphase_number
equal to 0 in blue, and regions withphase
equal to 1 in red. The facet option is only available for 3D plots. Defaults to ‘seed’.hide_interior (bool) – If True, removes interior facets from the output plot. This avoids occasional matplotlib issue where interior facets are shown in output plots.
**kwargs (dict) – Keyword arguments for matplotlib.
- write(filename, format='txt')[source]¶
Write the mesh to a file.
This function writes the polygon/polyhedron mesh to a file. See the Polygonal Mesh section of the Output File Formats guide for more information about the available output file formats.