Skip to content

Commit bf8f210

Browse files
committed
redo read_with_pyvista
1 parent 6eb54fc commit bf8f210

1 file changed

Lines changed: 23 additions & 65 deletions

File tree

src/foam2dolfinx/open_foam_reader.py

Lines changed: 23 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -89,90 +89,48 @@ def cell_type(self, value):
8989
raise TypeError("cell_type value should be an int")
9090
self._cell_type = value
9191

92-
def _read_with_pyvista(self, t: float, subdomain: str | None = "default"):
93-
"""
94-
Reads the OpenFOAM data in the filename provided, passes details of the
95-
OpenFOAM mesh to OF_mesh and details of the cells to OF_cells.
92+
def _read_with_pyvista(self, t: float, subdomain: str | None = None):
93+
"""Reads the OpenFOAM multiblock data at time t.
94+
95+
Populates OF_multiblock and OF_meshes_dict for all subdomains. If
96+
subdomain is given, validates it exists and populates OF_cells_dict for
97+
it. In single-domain files, OF_cells_dict["default"] is always populated.
98+
In multidomain files with no subdomain given, cell data is populated
99+
lazily by _create_global_dolfinx_mesh.
96100
97101
Args:
98102
t: timestamp of the data to read
99-
subdomain: Name of the subdmain in the OpenFOAM file, from which a field is
100-
extracted
101-
103+
subdomain: if given, validate this subdomain exists and populate its
104+
OF_cells_dict entry. Pass None when reading all subdomains
105+
without targeting a specific one.
102106
"""
103-
self.reader.set_active_time_value(t) # Set the time value to read data from
104-
self.OF_multiblock = self.reader.read() # Read the data from the OpenFOAM file
107+
self.reader.set_active_time_value(t)
108+
self.OF_multiblock = self.reader.read()
105109

106-
# Check if the reader has a multiblock dataset block named "internalMesh"
107110
if "internalMesh" not in self.OF_multiblock.keys():
108111
self.multidomain = True
109112
named_subdomains = [
110113
k for k in self.OF_multiblock.keys() if k != "defaultRegion"
111114
]
112-
if subdomain not in named_subdomains:
115+
if subdomain is not None and subdomain not in named_subdomains:
113116
raise ValueError(
114117
f"Subdomain {subdomain} not found in the OpenFOAM file. "
115118
f"Available subdomains: {named_subdomains}"
116119
)
117-
118-
# Extract the internal mesh
119-
if self.multidomain:
120-
for cell_array_name in self.OF_multiblock.keys():
121-
if cell_array_name == "defaultRegion":
122-
continue
123-
self.OF_meshes_dict[cell_array_name] = self.OF_multiblock[
124-
cell_array_name
125-
]["internalMesh"]
126-
else:
127-
self.OF_meshes_dict[subdomain] = self.OF_multiblock["internalMesh"]
128-
129-
# obtain dictionary of cell types in OF_mesh
130-
OF_cell_type_dict = self.OF_meshes_dict[subdomain].cells_dict
131-
132-
cell_types_in_mesh = [int(k) for k in OF_cell_type_dict.keys()]
133-
134-
# Raise error if OF_mesh is mixed topology
135-
if len(cell_types_in_mesh) > 1:
136-
raise NotImplementedError("Cannot support mixed-topology meshes")
137-
138-
self.OF_cells_dict[subdomain] = OF_cell_type_dict.get(self.cell_type)
139-
140-
# Raise error if no cells of the specified type are found in the OF_mesh
141-
if self.OF_cells_dict[subdomain] is None:
142-
raise ValueError(
143-
f"No cell type {self.cell_type} found in the mesh. Found "
144-
f"{cell_types_in_mesh}"
145-
)
146-
147-
def _read_with_pyvista_all(self, t: float):
148-
"""Reads the OpenFOAM multiblock data at time t for all subdomains.
149-
150-
Populates OF_multiblock and OF_meshes_dict for every subdomain without
151-
requiring a specific subdomain to be named. For single-domain files,
152-
also populates OF_cells_dict["default"]. For multidomain files, cell
153-
data for each subdomain is read lazily by _create_global_dolfinx_mesh.
154-
155-
Args:
156-
t: timestamp of the data to read
157-
"""
158-
self.reader.set_active_time_value(t)
159-
self.OF_multiblock = self.reader.read()
160-
161-
if "internalMesh" not in self.OF_multiblock.keys():
162-
self.multidomain = True
163-
for name in self.OF_multiblock.keys():
164-
if name == "defaultRegion":
165-
continue
120+
for name in named_subdomains:
166121
self.OF_meshes_dict[name] = self.OF_multiblock[name]["internalMesh"]
167122
else:
168123
self.multidomain = False
169124
self.OF_meshes_dict["default"] = self.OF_multiblock["internalMesh"]
170-
OF_cell_type_dict = self.OF_meshes_dict["default"].cells_dict
125+
subdomain = "default"
126+
127+
if subdomain is not None:
128+
OF_cell_type_dict = self.OF_meshes_dict[subdomain].cells_dict
171129
cell_types = [int(k) for k in OF_cell_type_dict.keys()]
172130
if len(cell_types) > 1:
173131
raise NotImplementedError("Cannot support mixed-topology meshes")
174-
self.OF_cells_dict["default"] = OF_cell_type_dict.get(self.cell_type)
175-
if self.OF_cells_dict["default"] is None:
132+
self.OF_cells_dict[subdomain] = OF_cell_type_dict.get(self.cell_type)
133+
if self.OF_cells_dict[subdomain] is None:
176134
raise ValueError(
177135
f"No cell type {self.cell_type} found in the mesh. "
178136
f"Found {cell_types}"
@@ -418,7 +376,7 @@ def create_facet_meshtags(self, t: float | None = None) -> dolfinx.mesh.MeshTags
418376
the dolfinx MeshTags for the facets of the mesh
419377
"""
420378
t = t if t is not None else next(tv for tv in self.times if tv != 0)
421-
self._read_with_pyvista_all(t=t)
379+
self._read_with_pyvista(t=t)
422380

423381
if self.multidomain:
424382
if "_global" not in self.dolfinx_meshes_dict:
@@ -547,7 +505,7 @@ def create_cell_meshtags(self, t: float | None = None) -> dolfinx.mesh.MeshTags:
547505
the dolfinx MeshTags for the cells of the mesh
548506
"""
549507
t = t if t is not None else next(tv for tv in self.times if tv != 0)
550-
self._read_with_pyvista_all(t=t)
508+
self._read_with_pyvista(t=t)
551509

552510
if self.multidomain:
553511
if "_global" not in self.dolfinx_meshes_dict:

0 commit comments

Comments
 (0)