Skip to content

Read scalar values from OF #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 7, 2025
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,6 @@ cython_debug/

*.bp
.vscode
*_version.py
*_version.py

*Zone.Identifier
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description = "Convert OpenFOAM files to dolfinx functions"
readme = "README/md"
requires-python = ">=3.9"
license = { file = "LICENSE" }
dependencies = ["fenics-dolfinx==0.9.0", "pyvista"]
dependencies = ["fenics-dolfinx>=0.9.0", "pyvista"]
classifiers = [
"Natural Language :: English",
"Topic :: Scientific/Engineering",
Expand Down
22 changes: 16 additions & 6 deletions src/foam2dolfinx/open_foam_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,15 @@ def _create_dolfinx_mesh(self):
)
degree = 1 # Set polynomial degree
cell = ufl.Cell(shape)
self.mesh_element = basix.ufl.element(
self.mesh_vector_element = basix.ufl.element(
"Lagrange", cell.cellname(), degree, shape=(3,)
)
self.mesh_scalar_element = basix.ufl.element(
"Lagrange", cell.cellname(), degree, shape=()
)

# Create dolfinx Mesh
mesh_ufl = ufl.Mesh(self.mesh_element)
mesh_ufl = ufl.Mesh(self.mesh_vector_element)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could probably add a small inline comment here stating how the type of element for creating the mesh is not important

self.dolfinx_mesh = create_mesh(
MPI.COMM_WORLD, self.connectivity, self.OF_mesh.points, mesh_ufl
)
Expand All @@ -127,10 +130,17 @@ def create_dolfinx_function(
the dolfinx function
"""
self._read_with_pyvista(t=t)
self._create_dolfinx_mesh()
self.function_space = dolfinx.fem.functionspace(
self.dolfinx_mesh, self.mesh_element
)

# Create dolfinx mesh if it doesn't exist
if not hasattr(self, "dolfinx_mesh"):
self._create_dolfinx_mesh()

if name == "U":
element = self.mesh_vector_element
else:
element = self.mesh_scalar_element
Comment on lines +138 to +141
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again could add small comment, in the case of reading for "U" a velocity field, a vector element is used etc


self.function_space = dolfinx.fem.functionspace(self.dolfinx_mesh, element)
u = dolfinx.fem.Function(self.function_space)

num_vertices = (
Expand Down
Binary file added test/data/baby_example.zip
Binary file not shown.
25 changes: 25 additions & 0 deletions test/test_example.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
from foam2dolfinx import OpenFOAMReader
import dolfinx
from pyvista import examples
import zipfile
from pathlib import Path


def test_reading_and_writing_cavity_example():
my_of_reader = OpenFOAMReader(filename=examples.download_cavity(load=False))
vel = my_of_reader.create_dolfinx_function(t=2.5)

assert isinstance(vel, dolfinx.fem.Function)


def test_baby_example(tmpdir):
time = 2812.0

zip_path = Path("test/data/baby_example.zip")
extract_path = Path(tmpdir) / "baby_example"

# Unzip the file
with zipfile.ZipFile(zip_path, "r") as zip_ref:
zip_ref.extractall(extract_path)

# Construct the path to the .foam file
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)

vel = my_of_reader.create_dolfinx_function(t=time, name="U")
T = my_of_reader.create_dolfinx_function(t=time, name="T")

assert isinstance(vel, dolfinx.fem.Function)
assert isinstance(T, dolfinx.fem.Function)
Loading