|
| 1 | +from mpi4py import MPI |
| 2 | + |
| 3 | +import basix |
| 4 | +import dolfinx |
| 5 | +import numpy as np |
| 6 | +import pyvista |
| 7 | +import pyvista.core.pointset |
| 8 | +import ufl |
| 9 | +from dolfinx.mesh import create_mesh |
| 10 | + |
| 11 | +__all__ = ["StructuredGridReader", "UnstructuredMeshReader"] |
| 12 | + |
| 13 | + |
| 14 | +class OpenMC2dolfinx(pyvista.VTKDataSetReader): |
| 15 | + """ |
| 16 | + Base OpenMC2Dolfinx Mesh Reader |
| 17 | +
|
| 18 | + Converts OpenMC results data into a dolfinx.fem.Function |
| 19 | +
|
| 20 | + Args: |
| 21 | + path: the path to the OpenMC .vtk file |
| 22 | +
|
| 23 | + Attributes: |
| 24 | + data: the mesh and results from the OpenMC .vtk file |
| 25 | + connectivity: The OpenMC mesh cell connectivity |
| 26 | + dolfinx_mesh: the dolfinx mesh |
| 27 | + """ |
| 28 | + |
| 29 | + data: pyvista.core.pointset.UnstructuredGrid | pyvista.core.pointset.StructuredGrid |
| 30 | + connectivity: np.ndarray |
| 31 | + dolfinx_mesh: dolfinx.mesh.Mesh = None |
| 32 | + |
| 33 | + def create_dolfinx_mesh(self): |
| 34 | + """Creates the dolfinx mesh depending on the type of cell provided |
| 35 | +
|
| 36 | + args: |
| 37 | + cell_type: the cell type for the dolfinx mesh, defaults to "tetrahedron" |
| 38 | + """ |
| 39 | + |
| 40 | + # TODO find a way to fix this with abstractmethod and property |
| 41 | + if not hasattr(self, "cell_type"): |
| 42 | + raise AttributeError("cell_type must be defined in the child class") |
| 43 | + |
| 44 | + self.data = self.read() |
| 45 | + if not hasattr(self, "cell_connectivity"): |
| 46 | + raise AttributeError("cell_connectivity must be defined in the child class") |
| 47 | + |
| 48 | + degree = 1 # Set polynomial degree |
| 49 | + |
| 50 | + cell = ufl.Cell(f"{self.cell_type}") |
| 51 | + mesh_element = basix.ufl.element( |
| 52 | + "Lagrange", cell.cellname(), degree, shape=(3,) |
| 53 | + ) |
| 54 | + |
| 55 | + # Create dolfinx Mesh |
| 56 | + mesh_ufl = ufl.Mesh(mesh_element) |
| 57 | + self.dolfinx_mesh = create_mesh( |
| 58 | + MPI.COMM_WORLD, self.cell_connectivity, self.data.points, mesh_ufl |
| 59 | + ) |
| 60 | + |
| 61 | + def create_dolfinx_function(self, data: str = "mean") -> dolfinx.fem.Function: |
| 62 | + """reads the filename of the OpenMC file |
| 63 | +
|
| 64 | + Arguments: |
| 65 | + data: the name of the data to extract from the vtk file |
| 66 | +
|
| 67 | + Returns: |
| 68 | + dolfinx function with openmc results mapped |
| 69 | + """ |
| 70 | + |
| 71 | + if not self.dolfinx_mesh: |
| 72 | + self.create_dolfinx_mesh() |
| 73 | + |
| 74 | + function_space = dolfinx.fem.functionspace(self.dolfinx_mesh, ("DG", 0)) |
| 75 | + u = dolfinx.fem.Function(function_space) |
| 76 | + |
| 77 | + u.x.array[:] = self.data.cell_data[f"{data}"][ |
| 78 | + self.dolfinx_mesh.topology.original_cell_index |
| 79 | + ] |
| 80 | + |
| 81 | + return u |
| 82 | + |
| 83 | + |
| 84 | +class UnstructuredMeshReader(OpenMC2dolfinx): |
| 85 | + """ |
| 86 | + Unstructured Mesh Reader |
| 87 | +
|
| 88 | + Reads an OpenMC .vtk results file with unstructured meshes and converts the data |
| 89 | + into a dolfinx.fem.Function |
| 90 | +
|
| 91 | + Args: |
| 92 | + path: the path to the OpenMC .vtk file |
| 93 | +
|
| 94 | + Example: |
| 95 | + .. code-block:: python |
| 96 | + reader = UnstructuredMeshReader("path/to/file.vtk") |
| 97 | + dolfinx_function = reader.create_dolfinx_function() |
| 98 | + """ |
| 99 | + |
| 100 | + cell_type = "tetrahedron" |
| 101 | + |
| 102 | + @property |
| 103 | + def cell_connectivity(self): |
| 104 | + return self.data.cells_dict[10] |
| 105 | + |
| 106 | + |
| 107 | +class StructuredGridReader(OpenMC2dolfinx): |
| 108 | + """ |
| 109 | + Structured Mesh Reader |
| 110 | +
|
| 111 | + Reads an OpenMC .vtk results file with Structured meshes and converts the data |
| 112 | + into a dolfinx.fem.Function |
| 113 | +
|
| 114 | + Args: |
| 115 | + path: the path to the OpenMC .vtk file |
| 116 | +
|
| 117 | + Example: |
| 118 | + .. code-block:: python |
| 119 | + reader = StructuredGridReader("path/to/file.vtk") |
| 120 | + dolfinx_function = reader.create_dolfinx_function() |
| 121 | + """ |
| 122 | + |
| 123 | + cell_type = "hexahedron" |
| 124 | + _cell_connectivity = None |
| 125 | + |
| 126 | + def get_connectivity(self): |
| 127 | + num_cells = self.data.GetNumberOfCells() |
| 128 | + assert self.data.GetCellType(0) == 12, "Only hexahedron cells are supported" |
| 129 | + |
| 130 | + # Extract connectivity information |
| 131 | + ordering = [0, 1, 3, 2, 4, 5, 7, 6] |
| 132 | + |
| 133 | + self._cell_connectivity = [] |
| 134 | + |
| 135 | + # TODO numpify this |
| 136 | + # Extract all cell connectivity data at once |
| 137 | + for i in range(num_cells): |
| 138 | + cell = self.data.GetCell(i) # Get the i-th cell |
| 139 | + point_ids = [cell.GetPointId(j) for j in ordering] # Extract connectivity |
| 140 | + self._cell_connectivity.append(point_ids) |
| 141 | + |
| 142 | + @property |
| 143 | + def cell_connectivity(self): |
| 144 | + if self._cell_connectivity is None: |
| 145 | + self.get_connectivity() |
| 146 | + return self._cell_connectivity |
0 commit comments