Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/ci_conda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 24 additions & 10 deletions src/foam2dolfinx/open_foam_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.

Expand All @@ -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:
Expand All @@ -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()}"
)

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/test_create_dolfinx_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
13 changes: 13 additions & 0 deletions test/test_open_foam_reader.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion test/test_read_with_pyvista.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading