From ee2917ae59f35225f3c7ae0a31997b793d9b52b9 Mon Sep 17 00:00:00 2001 From: jhdark Date: Mon, 23 Jun 2025 16:43:57 -0400 Subject: [PATCH] add times attribute, remove optional --- src/foam2dolfinx/open_foam_reader.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/foam2dolfinx/open_foam_reader.py b/src/foam2dolfinx/open_foam_reader.py index 48329a2..4c77b09 100644 --- a/src/foam2dolfinx/open_foam_reader.py +++ b/src/foam2dolfinx/open_foam_reader.py @@ -1,5 +1,3 @@ -from typing import Optional - from mpi4py import MPI import basix @@ -25,8 +23,9 @@ class OpenFOAMReader: filename: the filename cell_type: cell type id (12 corresponds to HEXAHEDRON) reader: pyvista OpenFOAM reader for .foam files + times: list of time values in the OpenFOAM file multidomain: boolean indicating if the mesh is multi-domain - OF_meshes_dict: dictionary of meshes from the openfoam file + OF_meshes_dict: dictionary of meshes from the OpenFOAM file OF_cells_dict: dictionary of arrays of the cells with associated vertices connectivities_dict: dictionary of the OpenFOAM mesh cell connectivity with vertices reordered in a sorted order for mapping with the dolfinx mesh. @@ -44,6 +43,7 @@ class OpenFOAMReader: cell_type: int reader: pyvista.POpenFOAMReader + times: list[float] multidomain: bool OF_meshes_dict: dict[str, pyvista.pyvista_ndarray | pyvista.DataSet] OF_cells_dict: dict[str, np.ndarray] @@ -55,6 +55,7 @@ def __init__(self, filename, cell_type: int = 12): self.cell_type = cell_type self.reader = pyvista.POpenFOAMReader(self.filename) + self.times = self.reader.time_values self.multidomain = False self.OF_meshes_dict = {} self.OF_cells_dict = {} @@ -71,14 +72,14 @@ def cell_type(self, value): raise TypeError("cell_type value should be an int") self._cell_type = value - def _read_with_pyvista(self, t: float, subdomain: Optional[str] = "default"): + def _read_with_pyvista(self, t: float, subdomain: str | None = "default"): """ - Reads the openfoam data in the filename provided, passes details of the - openfoam mesh to OF_mesh and details of the cells to OF_cells. + Reads the OpenFOAM data in the filename provided, passes details of the + OpenFOAM mesh to OF_mesh and details of the cells to OF_cells. Args: t: timestamp of the data to read - subdomain: Name of the subdmain in the openfoam file, from which a field is + subdomain: Name of the subdmain in the OpenFOAM file, from which a field is extracted """ @@ -124,7 +125,7 @@ def _read_with_pyvista(self, t: float, subdomain: Optional[str] = "default"): f"{cell_types_in_mesh}" ) - def _create_dolfinx_mesh(self, subdomain: Optional[str] = "default"): + def _create_dolfinx_mesh(self, subdomain: str | None = "default"): """Creates a dolfinx.mesh.Mesh based on the elements within the OpenFOAM mesh""" # Define mesh element and define args conn based on the OF cell type @@ -184,21 +185,21 @@ def _create_dolfinx_mesh(self, subdomain: Optional[str] = "default"): ) def create_dolfinx_function( - self, t: float, name: str = "U", subdomain: Optional[str] = "default" + self, t: float, name: str = "U", subdomain: str | None = "default" ) -> dolfinx.fem.Function: """Creates a dolfinx.fem.Function from the OpenFOAM file. Args: t: timestamp of the data to read - name: Name of the field in the openfoam file, defaults to "U" for velocity - subdomain: Name of the subdmain in the openfoam file, from which a field is + name: Name of the field in the OpenFOAM file, defaults to "U" for velocity + subdomain: Name of the subdmain in the OpenFOAM file, from which a field is extracted Returns: the dolfinx function """ - # read the openfoam data in the filename provided + # read the OpenFOAM data in the filename provided self._read_with_pyvista(t=t, subdomain=subdomain) # create the dolfinx mesh