Workflow utilities

Tools and helper methods for working with Py2DM.

This module includes independent utilities that may be useful when working with Py2DM as part of a larger workflow. This includes compatibility modes, format converters and the likes.

py2dm.utils.convert_random_nodes(filepath, export_conversion_tables=False, encoding='utf-8')

Compatibility parser for 2DM files with invalid IDs.

This parser will honour the semantics of any existing IDs (i.e. which nodes are connected to what element), but the ID value is generated internally. This allows reading node files whose IDs are no longer consecutive, which can occur when modifying 2DM meshes in external programs.

The generated output file will be placed next to the input and named <input-filename>_converted.2dm.

Note that this will affect the ID of every node and element in the mesh. Auxiliary files that refer to these IDs will no longer work. You can set the export_conversion_tables flag to export CSV files containing the old and new ID of every entity in the mesh. These tables can be used to update external files to match the new IDs.

Note

These conversion tables are only useful if there were no duplicate IDs in the input, which is not validated as part of this utility.

Parameters
  • filepath (str) – Input 2DM file to parse.

  • export_conversion_tables (bool) – Whether to export CSV files representing the generated ID conversion tables, defaults to False.

  • encoding (str) – The encoding to use for input file.

py2dm.utils.convert_unsorted_nodes(filepath, encoding='utf-8')

Compatibility parser for 2DM files with unsorted IDs.

The nodes and elements must still produce a consecutive block of IDs, without gaps or duplicates. This parser will fix non-standard ID ordering, but will not change any IDs.

The generated output file will be placed next to the input and named <input-filename>_converted.2dm.

If your node or elements IDs do have gaps or duplicates, please use the py2dm.utils.compat.convert_random_nodes() converter instead.

Parameters
  • filepath (str) – Input 2DM file to parse.

  • encoding (str) – The encoding to use for input file.

py2dm.utils.merge_meshes(mesh1, mesh2, output='', encoding='utf-8')

Merge two meshes using their shared vertices.

This utility will merge two meshes by first merging them at their shared vertices, then reconnecting all elements.

Note that this function does not check for mesh topology and may create self-intersections if the input meshes are not properly aligned. Likewise, this may create duplicate elements if the input meshes overlap exactly.

Parameters
  • mesh1 (str | pathlib.Path) – Base mesh to extend (all IDs are preserved).

  • mesh2 (str | pathlib.Path) – Mesh to add (IDs may change).

  • output (str | pathlib.Path) – The output file to write. Defaults to <mesh1>_<mesh2>.2dm.

  • encoding (str) – Text encoding to use for all file operations.

py2dm.utils.triangle_to_2dm(node_file, ele_file, output='', encoding='utf-8')

Create a 2DM mesh from “Triangle” output files.

This converter allows the creation of a Py2DM-compatible mesh from the output files of the Triangle 2D mesh generator: https://www.cs.cmu.edu/~quake/triangle.html.

Parameters
  • node_file (str | pathlib.Path) – The Triangle NODE file to read.

  • ele_file (str | pathlib.Path) – The Triangle ELE file to read.

  • output (str | pathlib.Path) – The output file to write. Defaults to the name of the Triangle output, minus the iteration number.

  • encoding (str) – The encoding to use for input and output files.