Skip to content

Commit 44f0967

Browse files
committed
implement to_hdf5 for unstructured meshes using a new c api function
1 parent f1c85fc commit 44f0967

4 files changed

Lines changed: 46 additions & 19 deletions

File tree

openmc/lib/mesh.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Mapping, Sequence
2-
from ctypes import (c_int, c_int32, c_char_p, c_double, POINTER, c_void_p,
2+
from ctypes import (c_int, c_int32, c_int64, c_char_p, c_double, POINTER, c_void_p,
33
create_string_buffer, c_size_t)
44
from math import sqrt
55
import sys
@@ -18,7 +18,7 @@
1818

1919
__all__ = [
2020
'Mesh', 'RegularMesh', 'RectilinearMesh', 'CylindricalMesh',
21-
'SphericalMesh', 'UnstructuredMesh', 'meshes', 'MeshMaterialVolumes'
21+
'SphericalMesh', 'UnstructuredMesh', 'meshes', 'MeshMaterialVolumes', 'export_unstructured_mesh'
2222
]
2323

2424

@@ -108,6 +108,10 @@
108108
_dll.openmc_spherical_mesh_set_grid.restype = c_int
109109
_dll.openmc_spherical_mesh_set_grid.errcheck = _error_handler
110110

111+
_dll.openmc_unstructured_mesh_export_hdf5.argtypes = [c_int32, c_int64]
112+
_dll.openmc_unstructured_mesh_export_hdf5.restype = c_int
113+
_dll.openmc_unstructured_mesh_export_hdf5.errcheck = _error_handler
114+
111115

112116
class Mesh(_FortranObjectWithID):
113117
"""Base class to represent mesh objects
@@ -740,6 +744,12 @@ class UnstructuredMesh(Mesh):
740744
}
741745

742746

747+
def export_unstructured_mesh(mesh, group):
748+
index = c_int32()
749+
_dll.openmc_get_mesh_index(mesh.id, index)
750+
_dll.openmc_unstructured_mesh_export_hdf5(index.value, int(group.id.id))
751+
752+
743753
def _get_mesh(index):
744754
mesh_type = create_string_buffer(20)
745755
_dll.openmc_mesh_get_type(index, mesh_type)

openmc/mesh.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3295,11 +3295,20 @@ def from_hdf5(cls, group: h5py.Group, mesh_id: int, name: str):
32953295
return mesh
32963296

32973297
def to_hdf5(self, group: h5py.Group):
3298-
raise NotImplementedError(
3299-
"UnstructuredMesh.to_hdf5 is not implemented in Python. "
3300-
"Use openmc.lib.export_weight_windows() to export weight "
3301-
"windows on unstructured meshes."
3298+
import openmc.lib
3299+
mesh_group = super().to_hdf5(group)
3300+
model = openmc.Model()
3301+
sph = openmc.Sphere(boundary_type='vacuum')
3302+
model.geometry = openmc.Geometry([openmc.Cell(region=-sph)])
3303+
model.settings.particles = 100
3304+
model.settings.batches = 1
3305+
wwg = openmc.WeightWindowGenerator(
3306+
method='magic',
3307+
mesh=self,
33023308
)
3309+
model.settings.weight_window_generators = [wwg]
3310+
with openmc.lib.TemporarySession(model):
3311+
openmc.lib.export_unstructured_mesh(self, mesh_group)
33033312

33043313
def to_xml_element(self):
33053314
"""Return XML representation of the mesh

openmc/weight_windows.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,19 +1081,6 @@ def export_to_hdf5(self, path: PathLike = 'weight_windows.h5', **init_kwargs):
10811081
cv.check_type('path', path, PathLike)
10821082
path = Path(path).resolve()
10831083

1084-
# Any unstructured mesh forces the whole list onto the lib fallback.
1085-
if any(isinstance(ww.mesh, UnstructuredMesh) for ww in self):
1086-
import openmc.lib
1087-
model = openmc.Model()
1088-
sph = openmc.Sphere(boundary_type='vacuum')
1089-
model.geometry = openmc.Geometry([openmc.Cell(region=-sph)])
1090-
model.settings.weight_windows = self
1091-
model.settings.particles = 100
1092-
model.settings.batches = 1
1093-
with openmc.lib.TemporarySession(model, **init_kwargs):
1094-
openmc.lib.export_weight_windows(path)
1095-
return
1096-
10971084
with h5py.File(path, 'w') as f:
10981085
f.attrs['filetype'] = np.bytes_('weight_windows')
10991086
f.attrs['version'] = np.asarray([1, 0], dtype=np.int32)

src/mesh.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2843,6 +2843,27 @@ extern "C" int openmc_spherical_mesh_set_grid(int32_t index,
28432843
index, grid_x, nx, grid_y, ny, grid_z, nz);
28442844
}
28452845

2846+
extern "C" int openmc_unstructured_mesh_export_hdf5(
2847+
int32_t index, hid_t mesh_group)
2848+
{
2849+
if (int err = check_mesh_type<UnstructuredMesh>(index))
2850+
return err;
2851+
UnstructuredMesh* m =
2852+
dynamic_cast<UnstructuredMesh*>(model::meshes[index].get());
2853+
2854+
// Write mesh type
2855+
write_dataset(mesh_group, "type", m->get_mesh_type());
2856+
2857+
// Write mesh ID
2858+
write_attribute(mesh_group, "id", m->id_);
2859+
2860+
// Write mesh name
2861+
write_dataset(mesh_group, "name", m->name_);
2862+
2863+
m->to_hdf5_inner(mesh_group);
2864+
return 0;
2865+
}
2866+
28462867
#ifdef OPENMC_DAGMC_ENABLED
28472868

28482869
const std::string MOABMesh::mesh_lib_type = "moab";

0 commit comments

Comments
 (0)