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_material(label, imat)[source]#

Add a material name.

Parameters:
  • label (str) – Material name.

  • imat (int) – Material ID.

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.

property cell_data#

Return cell data arrays.

cell_data_to_point_data()[source]#

Interpolate cell data to point data.

property cell_sets#

Return sets of cells.

property cells#

Return connectivity of cells.

property centers#

Return node centers of cell in mesh.

property 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.

property dim#

Return mesh dimension.

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

property face_areas#

Return areas of faces in mesh.

property face_normals#

Return normal vectors of faces in mesh.

property faces#

Return connectivity of faces of cell in mesh.

property field_data#

Return field data names.

property filter#

Filter mesh.

Parameters:

filter (str, optional, default 'box') – Filter method.

Returns:

Indices of cells filtered.

Return type:

array_like

property label_length#

Return length of labels.

property labels#

Return labels of cell in mesh.

property materials#

Return materials of cell in mesh.

property n_cells#

Return number of cells.

property n_points#

Return number of points.

near(points)[source]#

Return indices of cells nearest to query points.

Parameters:

points (array_like) – Coordinates of points to query.

Returns:

Index of cell or indices of cells.

Return type:

int or array_like

plot(*args, **kwargs)[source]#

Display mesh using pyvista.UnstructuredGrid.plot().

property point_data#

Return point data arrays.

point_data_to_cell_data()[source]#

Interpolate point data to cell data.

property point_sets#

Return sets of points.

property 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.

property 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, labels_order=None, connection=False)[source]#

Import TOUGH results to the mesh.

Parameters:
  • file_or_output (str, pathlike, buffer, toughio.ElementOutput, toughio.ConnectionOutput, sequence of toughio.ElementOutput or sequence of toughio.ConnectionOutput) – Input file name or buffer, or output data.

  • time_step (int, optional, default -1) – Data for given time step to import. Default is last time step.

  • labels_order (sequence of array_like or None, optional, default None) – List of labels. If None, output will be assumed ordered.

  • connection (bool, optional, default False) – Only for standard TOUGH output file. If True, read data related to connections.

set_cell_labels(labels)[source]#

Set labels to cells.

Parameters:

labels (array_like) – Cell labels.

Note

If labels are not set, a custom labeler with a naming convention different that of TOUGH is used.

set_material(material, cells)[source]#

Set material to cells.

Parameters:
  • material (str) – Material name.

  • cells (array_like) – Indices of cells or array of booleans.

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

property volumes#

Return volumes of cell in mesh.

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

Write mesh to file.

Parameters:
  • filename (str, pathlike or buffer) – Output file name or buffer.

  • 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).

  • 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', eos=None)[source]#

Write TOUGH INCON file.

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

  • eos (str or None, optional, default None) – Equation of State.

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, pathlike or buffer, optional, default 'MESH') – Output file name or buffer.

  • 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.

  • eos (str or None, optional, default None) – Equation of State.

  • gravity (array_like or None, optional, default None) – Gravity direction vector.

I/O#

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

Read unstructured mesh from file.

Parameters:
  • filename (str, pathlike or buffer) – Input file name or buffer.

  • file_format (str or None, optional, default None) – Input file format.

  • 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, pathlike or buffer) – Output file name or buffer.

  • mesh (toughio.Mesh) – Mesh to export.

  • file_format (str or None, optional, default None) – Output file format.

  • 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.

  • eos (str or None, optional, default None) – Only if file_format = "tough". Equation of State.

  • gravity (array_like or None, optional, default None) – Only if file_format = "tough". Gravity direction vector.

  • protocol (integer, optional, default pickle.HIGHEST_PROTOCOL) – Only if file_format = "pickle". pickle protocol version.

toughio.register_mesh(file_format, extensions, reader, writer=None)#

Register a new mesh format.

Parameters:
  • file_format (str) – File format to register.

  • extensions (array_like) – List of extensions to associate to the new format.

  • reader (callable) – Read fumction.

  • writer (callable or None, optional, default None) – Write function.

toughio.read_time_series(filename)[source]#

Read time series from XDMF file.

Parameters:

filename (str, pathlike or buffer) – 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, pathlike or buffer) – 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, material='dfalt')[source]#

Convert a meshio.Mesh to toughio.Mesh.

Parameters:
  • mesh (meshio.Mesh) – Input mesh.

  • material (str, optional, default 'dfalt') – Default material name.

Returns:

Output mesh.

Return type:

toughio.Mesh

toughio.from_pyvista(mesh, material='dfalt')[source]#

Convert a pyvista.UnstructuredGrid to toughio.Mesh.

Parameters:
  • mesh (pyvista.UnstructuredGrid) – Input mesh.

  • material (str, optional, default 'dfalt') – Default material name.

Returns:

Output mesh.

Return type:

toughio.Mesh

Basic generators#

toughio.meshmaker.cylindric_grid(dr, dz, origin_z=None, layer=False, 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.

  • layer (bool, optional, default False) – If True, mesh will be generated by layers.

  • material (str, optional, default 'dfalt') – Default material name.

Returns:

Output cylindric mesh.

Return type:

toughio.Mesh

toughio.meshmaker.voxelize(points, origin, layer=False, material='dfalt')[source]#

Generate a 3D non-uniform structured grid from cloud points.

Input points must form a non-uniform structured grid.

Parameters:
  • points (array_like) – Cooordinates of points.

  • origin (array_like) – Origin point coordinate.

  • layer (bool, optional, default False) – If True, mesh will be generated by layers.

  • material (str, optional, default 'dfalt') – Default material name.

Returns:

Output non-uniform structured mesh.

Return type:

toughio.Mesh

toughio.meshmaker.structured_grid(dx, dy, dz=None, origin=None, layer=False, 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.

  • layer (bool, optional, default False) – If True, mesh will be generated by layers.

  • 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) – Cooordinates of points.

  • material (str, optional, default 'dfalt') – Default material name.

Returns:

Output triangulated mesh.

Return type:

toughio.Mesh

toughio.meshmaker.from_meshmaker(filename_or_dict, material='dfalt')[source]#

Generate a mesh from a block MESHM.

Parameters:
  • filename_or_dict (str, pathlike, buffer or dict) – Input file name, buffer or parameters dict with key “meshmaker”.

  • material (str, optional, default 'dfalt') – Default material name.