2. Meshing

class toughio.Mesh(points, cells, point_data=None, cell_data=None, field_data=None, point_sets=None, cell_sets=None)[source]

Unstructured toughio mesh.

This class is updated following the latest meshio version and brings backward compatibility with its previous versions.

Parameters:
  • points (array_like (n_points, 3)) – Cooordinates of points.
  • cells (list of tuples (cell_type, data)) – Connectivity of cells.
  • point_data (dict or None, optional, default None) – Point data arrays.
  • cell_data (dict or None, optional, default None) – Cell data arrays.
  • field_data (dict or None, optional, default None) – Field data names.
  • point_sets (dict or None, optional, default None) – Sets of points.
  • cell_sets (dict or None, optional, default None) – Sets of cells.
add_cell_data(label, data)[source]

Add a new cell data array.

Parameters:
  • label (str) – Cell data array name.
  • data (array_like) – Cell data array.
add_point_data(label, data)[source]

Add a new point data array.

Parameters:
  • label (str) – Point data array name.
  • data (array_like) – Point data array.
cell_data

Return cell data arrays.

cell_sets

Return sets of cells.

cells

Return connectivity of cells.

cells_dict

Return connectivity of cells (meshio < 4.0.0).

centers

Return node centers of cell in mesh.

connections

Return mesh connections.

Assume conformity and that points and cells are uniquely defined in mesh.

Note

Only for 3D meshes and first order cells.

extrude_to_3d(height=1.0, axis=2, inplace=True)[source]

Convert a 2D mesh to 3D by extruding cells along given axis.

Parameters:
  • height (scalar or array_like, optional, default 1.0) – Height of extrusion.
  • axis (int (0, 1 or 2), optional, default 2) – Axis along which extrusion is performed.
  • inplace (bool, optional, default True) – If False, return a new toughio.Mesh.
Returns:

Extruded mesh (only if inplace == False).

Return type:

toughio.Mesh

face_areas

Return areas of faces in mesh.

face_normals

Return normal vectors of faces in mesh.

faces

Return connectivity of faces of cell in mesh.

field_data

Return field data names.

label_length

Return length of labels.

labels

Return labels of cell in mesh.

materials

Return materials of cell in mesh.

n_cells

Return number of cells.

n_points

Return number of points.

near(point)[source]

Return index of cell nearest to query point.

Parameters:point (array_like) – Coordinates of point to query.
Returns:Index of cell.
Return type:tuple
plot(*args, **kwargs)[source]

Display mesh using pyvista.UnstructuredGrid.plot().

point_data

Return point data arrays.

point_sets

Return sets of points.

points

Return coordinates of points.

prune_duplicates(inplace=True)[source]

Delete duplicate points and cells.

Parameters:inplace (bool, optional, default True) – If False, return a new toughio.Mesh.
Returns:Pruned mesh (only if inplace == False).
Return type:toughio.Mesh

Note

Does not preserve points order from original array in mesh.

qualities

Return qualities of cells in mesh.

The quality of a cell is measured as the minimum cosine angle between the connection line and the interface normal vectors.

read_output(file_or_output, time_step=-1, connection=False)[source]

Import TOUGH results to the mesh.

Parameters:
  • file_or_output (str, namedtuple or list of namedtuple) – Input file name or output data.
  • time_step (int, optional, default -1) – Data for given time step to import. Default is last time step.
  • connection (bool, optional, default False) – Only for standard TOUGH output file. If True, read data related to connections.
set_material(material, xlim=None, ylim=None, zlim=None)[source]

Set material to cells in box.

Set material for cells within box selection defined by xlim, ylim and zlim.

Parameters:
  • material (str) – Material name.
  • xlim (array_like or None, optional, default None) – Minimum and maximum values in X direction.
  • ylim (array_like or None, optional, default None) – Minimum and maximum values in Y direction.
  • zlim (array_like or None, optional, default None) – Minimum and maximum values in Z direction.
Raises:

AssertionError – If any input argument is not valid.

split(arr)[source]

Split input array into subarrays for each cell block in mesh.

Parameters:arr (array_like) – Input array.
Returns:List of subarrays.
Return type:list of array_like
to_meshio()[source]

Convert mesh to meshio.Mesh.

Returns:Output mesh.
Return type:meshio.Mesh
to_pyvista()[source]

Convert mesh to pyvista.UnstructuredGrid.

Returns:Output mesh.
Return type:pyvista.UnstructuredGrid
volumes

Return volumes of cell in mesh.

write(filename, file_format=None, **kwargs)[source]

Write mesh to file.

Parameters:
  • filename (str) – Output file name.
  • file_format (str or None, optional, default None) – Output file format. If None, it will be guessed from file’s extension. To write TOUGH MESH, file_format must be specified as ‘tough’ (no specific extension exists for TOUGH MESH).
Other Parameters:
 
  • nodal_distance (str (‘line’ or ‘orthogonal’), optional, default ‘line’) – Only if file_format = "tough". Method to calculate connection nodal distances: - ‘line’: distance between node and common face along connecting line (distance is not normal), - ‘orthogonal’ : distance between node and its orthogonal projection onto common face (shortest distance).
  • material_name (dict or None, default None) – Only if file_format = "tough". Rename cell material.
  • material_end (str, array_like or None, default None) – Only if file_format = "tough". Move cells to bottom of block ‘ELEME’ if their materials is in material_end.
  • incon (bool, optional, default False) – Only if file_format = "tough". If True, initial conditions will be written in file INCON.
  • protocol (integer, optional, default pickle.HIGHEST_PROTOCOL) – Only if file_format = "pickle". pickle protocol version.
write_incon(filename='INCON')[source]

Write TOUGH INCON file.

Parameters:filename (str, optional, default 'INCON') – Output file name.

Note

Mostly useful to restart a simulation with other initial conditions but with the same mesh.

write_tough(filename='MESH', **kwargs)[source]

Write TOUGH MESH file.

Parameters:

filename (str, optional, default 'MESH') – Output file name.

Other Parameters:
 
  • nodal_distance (str (‘line’ or ‘orthogonal’), optional, default ‘line’) – Method to calculate connection nodal distances: - ‘line’: distance between node and common face along connecting line (distance is not normal), - ‘orthogonal’ : distance between node and its orthogonal projection onto common face (shortest distance).
  • material_name (dict or None, default None) – Rename cell material.
  • material_end (str, array_like or None, default None) – Move cells to bottom of block ‘ELEME’ if their materials is in material_end.
  • incon (bool, optional, default False) – If True, initial conditions will be written in file INCON.

2.1. I/O

toughio.read_mesh(filename, file_format=None, **kwargs)

Read unstructured mesh from file.

Parameters:
  • filename (str) – Input file name.
  • file_format (str or None, optional, default None) – Input file format.
Other Parameters:
 

label_length (int or None, optional, default None) – Only if file_format = "tough". Number of characters in cell labels.

Returns:

Imported mesh.

Return type:

toughio.Mesh

toughio.write_mesh(filename, mesh, file_format=None, **kwargs)

Write unstructured mesh to file.

Parameters:
  • filename (str) – Output file name.
  • mesh (toughio.Mesh) – Mesh to export.
  • file_format (str or None, optional, default None) – Output file format.
Other Parameters:
 
  • nodal_distance (str (‘line’ or ‘orthogonal’), optional, default ‘line’) – Only if file_format = "tough". Method to calculate connection nodal distances: - ‘line’: distance between node and common face along connecting line (distance is not normal), - ‘orthogonal’ : distance between node and its orthogonal projection onto common face (shortest distance).
  • material_name (dict or None, default None) – Only if file_format = "tough". Rename cell material.
  • material_end (str, array_like or None, default None) – Only if file_format = "tough". Move cells to bottom of block ‘ELEME’ if their materials is in material_end.
  • incon (bool, optional, default False) – Only if file_format = "tough". If True, initial conditions will be written in file INCON.
  • protocol (integer, optional, default pickle.HIGHEST_PROTOCOL) – Only if file_format = "pickle". pickle protocol version.
toughio.read_time_series(filename)[source]

Read time series from XDMF file.

Parameters:filename (str) – Input file name.
Returns:
  • list of namedtuple (type, data) – Grid cell data.
  • list of dict – Data associated to grid points for each time step.
  • list of dict – Data associated to grid cells for each time step.
  • array_like – Time step values.
toughio.write_time_series(filename, points, cells, point_data=None, cell_data=None, time_steps=None)[source]

Write time series given points and cells data.

Parameters:
  • filename (str) – Output file name.
  • points (ndarray) – Grid points array.
  • cells (list of namedtuple (type, data)) – Grid cell data.
  • point_data (list of dict or None, optional, default None) – Data associated to grid points for each time step.
  • cell_data (list of dict or None, optional, default None) – Data associated to grid cells for each time step.
  • time_steps (array_like, optional, default None) – Time step values.
toughio.from_meshio(mesh)[source]

Convert a meshio.Mesh to toughio.Mesh.

Parameters:mesh (meshio.Mesh) – Input mesh.
Returns:Output mesh.
Return type:toughio.Mesh
toughio.from_pyvista(mesh)[source]

Convert a pyvista.UnstructuredGrid to toughio.Mesh.

Parameters:mesh (pyvista.UnstructuredGrid) – Input mesh.
Returns:Output mesh.
Return type:toughio.Mesh

2.2. Basic generators

toughio.meshmaker.cylindric_grid(dr, dz, origin_z=None, material='dfalt')[source]

Generate a cylindric mesh as a radial XZ structured grid.

Parameters:
  • dr (array_like) – Grid spacing along X axis.
  • dz (array_like) – Grid spacing along Z axis.
  • origin_z (scalar, optional, default None) – Depth of origin point.
  • material (str, optional, default 'dfalt') – Default material name.
Returns:

Output cylindric mesh.

Return type:

toughio.Mesh

toughio.meshmaker.voxelize(points, material='dfalt')[source]

Generate a 1D hexahedral mesh from 1D point coordinates.

Parameters:
  • points (array_like (n_points,)) – Cooordinates of points.
  • material (str, optional, default 'dfalt') – Default material name.
Returns:

Output hexahedral mesh.

Return type:

toughio.Mesh

toughio.meshmaker.structured_grid(dx, dy, dz=None, origin=None, material='dfalt')[source]

Generate 2D or 3D non-uniform structured grid.

Parameters:
  • dx (array_like) – Grid spacing along X axis.
  • dy (array_like) – Grid spacing along Y axis.
  • dz (array_like or None, optional, default None) – Grid spacing along Z axis. If None, generate 2D grid.
  • origin (array_like or None, optional, default None) – Origin point coordinate.
  • material (str, optional, default 'dfalt') – Default material name.
Returns:

Output non-uniform structured mesh.

Return type:

toughio.Mesh

toughio.meshmaker.triangulate(points, material='dfalt')[source]

Generate a triangulated mesh from cloud points.

Parameters:
  • points (array_like (n_points, 3)) – Cooordinates of points.
  • material (str, optional, default 'dfalt') – Default material name.
Returns:

Output triangulated mesh.

Return type:

toughio.Mesh