From c86c9b6b22f664aa03fe409f141bd1e0942a338e Mon Sep 17 00:00:00 2001 From: jhdark Date: Fri, 7 Mar 2025 17:04:59 -0500 Subject: [PATCH 1/5] simplify variable name, new property and setter --- src/foam2dolfinx/open_foam_reader.py | 34 ++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/foam2dolfinx/open_foam_reader.py b/src/foam2dolfinx/open_foam_reader.py index e2885e2..b38c8f4 100644 --- a/src/foam2dolfinx/open_foam_reader.py +++ b/src/foam2dolfinx/open_foam_reader.py @@ -19,11 +19,11 @@ class OpenFOAMReader: Args: filename: the filename - OF_mesh_cell_type_value: cell type id (12 corresponds to HEXAHEDRON) + cell_type: cell type id (12 corresponds to HEXAHEDRON) Attributes: filename: the filename - OF_mesh_cell_type_value: cell type id (12 corresponds to HEXAHEDRON) + cell_type: cell type id (12 corresponds to HEXAHEDRON) reader: pyvista OpenFOAM reader for .foam files OF_mesh: the mesh from the openfoam file OF_cells: an array of the cells with associated vertices @@ -33,10 +33,14 @@ class OpenFOAMReader: dolfinx_mesh: the dolfinx mesh function_space: the function space of the dolfinx function returned in create_dolfinx_function() + + Notes: + The cell type refers to the VTK cell type, a full list of cells and their + respective integers can be found at: https://vtk.org/doc/nightly/html/vtkCellType_8h_source.html """ filename: str - OF_mesh_cell_type_value: int + cell_type: int reader: pyvista.POpenFOAMReader OF_mesh: pyvista.pyvista_ndarray | pyvista.DataSet @@ -46,12 +50,22 @@ class OpenFOAMReader: dolfinx_mesh: dolfinx.mesh.Mesh function_space: dolfinx.fem.FunctionSpace - def __init__(self, filename, OF_mesh_cell_type_value: int = 12): + def __init__(self, filename, cell_type: int = 12): self.filename = filename - self.OF_mesh_cell_type_value = OF_mesh_cell_type_value + self.cell_type = cell_type self.reader = pyvista.POpenFOAMReader(self.filename) + @property + def cell_type(self): + return self._cell_type + + @cell_type.setter + def cell_type(self, value): + if not isinstance(value, int): + raise TypeError("cell_type value should be an int") + self._cell_type = value + def _read_with_pyvista(self, t: float): """reads the filename dolfinx.fem.Function from the OpenFOAM file. @@ -70,7 +84,7 @@ def _read_with_pyvista(self, t: float): assert hasattr(self.OF_mesh, "cells_dict") # Ensure the mesh has cell data OF_cells_dict = self.OF_mesh.cells_dict # Get the cell dictionary - self.OF_cells = OF_cells_dict.get(self.OF_mesh_cell_type_value) + self.OF_cells = OF_cells_dict.get(self.cell_type) # Raise error if OF_mesh is mixed topology if len(OF_cells_dict.keys()) > 1: @@ -79,7 +93,7 @@ def _read_with_pyvista(self, t: float): # Raise error if no cells of the specified type are found in the OF_mesh if self.OF_cells is None: raise ValueError( - f"No {self.OF_mesh_cell_type_value} cells found in the mesh. Found " + f"No {self.cell_type} cells found in the mesh. Found " f"{OF_cells_dict.keys()}" ) @@ -92,13 +106,13 @@ def _create_dolfinx_mesh(self): self.connectivity = self.OF_cells[rows, args_conn] # Reorder connectivity # Define mesh element - if self.OF_mesh_cell_type_value == 12: + if self.cell_type == 12: shape = "hexahedron" - elif self.OF_mesh_cell_type_value == 10: + elif self.cell_type == 10: shape = "tetrahedron" else: raise ValueError( - f"Cell type: {self.OF_mesh_cell_type_value}, not supported, please use" + f"Cell type: {self.cell_type}, not supported, please use" " either 12 (hexahedron) or 10 (tetrahedron) cells in OF mesh" ) degree = 1 # Set polynomial degree From 10406f547d606a241e42bced18fc9892bf4d22a6 Mon Sep 17 00:00:00 2001 From: jhdark Date: Fri, 7 Mar 2025 17:05:11 -0500 Subject: [PATCH 2/5] update tests with new variable name --- test/test_create_dolfinx_mesh.py | 2 +- test/test_read_with_pyvista.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_create_dolfinx_mesh.py b/test/test_create_dolfinx_mesh.py index 36aab1a..45d942f 100644 --- a/test/test_create_dolfinx_mesh.py +++ b/test/test_create_dolfinx_mesh.py @@ -10,7 +10,7 @@ def test_error_rasied_when_using_mixed_topology_mesh(): test_value = 1 my_reader = OpenFOAMReader( filename=examples.download_cavity(load=False), - OF_mesh_cell_type_value=test_value, + cell_type=test_value, ) # Create a random number generator diff --git a/test/test_read_with_pyvista.py b/test/test_read_with_pyvista.py index 6e5be86..fd2552b 100644 --- a/test/test_read_with_pyvista.py +++ b/test/test_read_with_pyvista.py @@ -15,7 +15,7 @@ def test_error_rasied_when_using_mixed_topology_mesh(): def test_error_rasied_when_cells_wanted_are_not_in_file_provided(): my_reader = OpenFOAMReader( - filename=examples.download_cavity(load=False), OF_mesh_cell_type_value=1 + filename=examples.download_cavity(load=False), cell_type=1 ) with pytest.raises( From e2b7709a591fb330097b0619405b47d691c0e4c0 Mon Sep 17 00:00:00 2001 From: jhdark Date: Fri, 7 Mar 2025 17:17:25 -0500 Subject: [PATCH 3/5] additioanl test --- test/test_open_foam_reader.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 test/test_open_foam_reader.py diff --git a/test/test_open_foam_reader.py b/test/test_open_foam_reader.py new file mode 100644 index 0000000..6632477 --- /dev/null +++ b/test/test_open_foam_reader.py @@ -0,0 +1,13 @@ +import numpy as np +import pytest +from pyvista import examples + +from foam2dolfinx import OpenFOAMReader + + +@pytest.mark.parametrize("value", ["tetra", 1.0, np.array([1.0])]) +def test_error_raised_when_cell_type_not_int(value): + "test that an error is raised when an integer is not given as an arg to cell type" + + with pytest.raises(TypeError): + OpenFOAMReader(filename=examples.download_cavity(load=False), cell_type=value) From b014d4473b9efaafe1f1c74b7067f8929b4c606a Mon Sep 17 00:00:00 2001 From: jhdark Date: Fri, 7 Mar 2025 17:23:24 -0500 Subject: [PATCH 4/5] remove fetch depth --- .github/workflows/ci_conda.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ci_conda.yml b/.github/workflows/ci_conda.yml index 5b9179f..753244f 100644 --- a/.github/workflows/ci_conda.yml +++ b/.github/workflows/ci_conda.yml @@ -8,8 +8,6 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v4 - with: - fetch-depth: 0 - name: Set up Conda uses: conda-incubator/setup-miniconda@v3 From 7d3571102a5de46e8ddacbc26a14ad9938ccc205 Mon Sep 17 00:00:00 2001 From: jhdark Date: Fri, 7 Mar 2025 17:32:46 -0500 Subject: [PATCH 5/5] fix test --- test/test_example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_example.py b/test/test_example.py index 1f891d8..f660ec6 100644 --- a/test/test_example.py +++ b/test/test_example.py @@ -26,7 +26,7 @@ def test_baby_example(tmpdir): foam_file = extract_path / "baby_example/pv.foam" # read the .foam file - my_of_reader = OpenFOAMReader(filename=str(foam_file), OF_mesh_cell_type_value=10) + my_of_reader = OpenFOAMReader(filename=str(foam_file), cell_type=10) vel = my_of_reader.create_dolfinx_function(t=time, name="U") T = my_of_reader.create_dolfinx_function(t=time, name="T")