fol.mesh_input_output#
Mesh input/output utilities provided by FoLax.
Base mesh class#
Authors: Reza Najian Asl, RezaNajian Date: June, 2024 License: FOL/LICENSE
- class fol.mesh_input_output.mesh.Mesh(io_name, file_name, case_dir='.', scale_factor=1)[source]#
Bases:
ABCBase mesh input/output class for finite element simulations.
This class provides a unified interface for reading, storing, querying, and exporting finite element meshes used throughout FoLax. It supports both general-purpose mesh formats via the
meshioPython library and Kratos Multiphysics.mdpamesh files via a custom parser.When the input file format is not
.mdpa, the mesh is read usingmeshio.read. In this case,meshiois responsible for parsing node coordinates, element connectivities, and mesh metadata. Node sets are constructed frommeshiopoint tags, and element connectivities are stored by element type in a dictionary. This allows FoLax to support a wide range of mesh formats such as VTK, Gmsh, Salome med, and others supported bymeshio.When the input file format is
.mdpa, the mesh is read using a custom parser implemented in this class. Nodes, elements, and sub-model parts are extracted directly from the file. The resulting data are then wrapped in ameshio.Meshobject so that downstream operations (such as export) can use the same interface as meshes read viameshio.After reading the mesh, element orientations are checked using the reference finite element definitions in
fe_element_dict. Elements with negative Jacobian determinants at the reference integration point are automatically re-oriented to ensure consistent element orientation.The mesh object stores: - node identifiers and coordinates, - element connectivity grouped by element type, - node sets defined by boundary or sub-model-part names, - optional point data accessible through
mesh_io.- Parameters:
io_name (str) – Name identifier for the mesh object.
file_name (str) – Name of the mesh file to be read. The file extension determines whether
meshioor the Kratos.mdpaparser is used.case_dir (str, optional) – Directory containing the mesh file. Default is the current directory.
scale_factor (float, optional) – Scaling factor applied to node coordinates after reading the mesh. Default is
1.
Notes
The
meshiolibrary is used as a backend for reading and writing standard mesh formats and for managing point data. FoLax relies onmeshioto provide a consistent representation of nodes and elements across different file formats.The
Finalizemethod exports the mesh usingmeshio.writeand can be used to write results (including point data added during a simulation) to formats such as VTK.This class does not perform any FE assembly itself; it only provides mesh topology and geometry to losses, solvers, and controls.
- CheckAndOrientElements()[source]#
Check element orientation and swap nodes for inverted elements.
For each element type present in
self.elements_nodesthat also exists infe_element_dict, this method computes the Jacobian determinant at the reference integration point. If the determinant is negative, the element is considered inverted and its first two node indices are swapped (a minimal correction strategy).This method updates
self.elements_nodesin place for affected element types.Notes
Only element types recognized by
fe_element_dictare processed.If swapping does not fix some elements, a warning is emitted.
- Finalize(export_dir='.', export_format='vtk')[source]#
Export the mesh (and any attached point data) to disk using
meshio.The output file name is derived from the original
file_nameby replacing the extension withexport_format.- Parameters:
export_dir – Output directory.
export_format – Output file format supported by
meshio(e.g.vtk).
- Returns:
None
- GetElementsIds(element_type)[source]#
Return element ids for a given element type.
- Parameters:
element_type – Element type key used in
self.elements_nodes(meshio type).- Returns:
Array of element ids with shape
(num_elements,).- Raises:
KeyError – If
element_typeis not present in the mesh.
- GetElementsNodes(element_type)[source]#
Return element connectivity for a given element type.
- Parameters:
element_type – Element type key used in
self.elements_nodes.- Returns:
Connectivity array of shape
(num_elements, num_nodes_per_element).- Raises:
KeyError – If
element_typeis not present in the mesh.
- GetNodeSet(set_name)[source]#
Return node ids belonging to a named node set.
- Parameters:
set_name – Name of the node set.
- Returns:
Array of node indices in the set.
- Raises:
KeyError – If
set_namedoes not exist inself.node_sets.
- GetNodesCoordinates()[source]#
Return nodal coordinates.
- Returns:
Array of nodal coordinates with shape
(num_nodes, dim).
- GetNodesIds()[source]#
Return the node id array.
- Returns:
Array of node ids with shape
(num_nodes,).
- GetNodesX()[source]#
Return x-coordinates of all nodes.
- Returns:
Array of x-coordinates with shape
(num_nodes,).
- GetNodesY()[source]#
Return y-coordinates of all nodes.
- Returns:
Array of y-coordinates with shape
(num_nodes,).
- GetNodesZ()[source]#
Return z-coordinates of all nodes.
This assumes the mesh is 3D or that the third coordinate exists.
- Returns:
Array of z-coordinates with shape
(num_nodes,).
- GetNumberOfElements(element_type)[source]#
Return number of elements for a given element type.
- Parameters:
element_type – Element type key used in
self.elements_nodes.- Returns:
Number of elements of that type.
- Raises:
KeyError – If
element_typeis not present in the mesh.
- GetNumberOfNodes()[source]#
Return the number of nodes in the mesh.
- Returns:
Total number of mesh nodes.
- HasPointData(data_name)[source]#
Check whether point data with the given name exists in
mesh_io.- Parameters:
data_name – Name of the point data field.
- Returns:
True if the point data exists, otherwise False.
- Initialize()[source]#
Read and initialize the mesh from disk.
If the mesh has already been initialized, this method returns immediately without modifying the internal state.
The behavior depends on the mesh file extension. For files that are not in
mdpaformat, the mesh is read using themeshiolibrary. Node coordinates, element connectivity, and tag-based node sets are extracted from themeshioobject and stored internally.For files in
mdpaformat, a custom parser is used to read nodes, elements, and sub-model parts directly from the file. The parsed data are then wrapped into ameshio.Meshobject so that export and point data handling use the same interface as meshes read viameshio.After reading the mesh, element orientations are checked and corrected when possible to ensure consistent element Jacobians.