Skip to content

Commit dadc4fe

Browse files
GuyStenpaulromano
andauthored
Fix no serialization of periodic_surface_id bug (#3421)
Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
1 parent e14bb88 commit dadc4fe

4 files changed

Lines changed: 70 additions & 0 deletions

File tree

include/openmc/boundary_condition.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ class PeriodicBC : public BoundaryCondition {
111111

112112
std::string type() const override { return "periodic"; }
113113

114+
int i_surf() const { return i_surf_; }
115+
116+
int j_surf() const { return j_surf_; }
117+
114118
protected:
115119
int i_surf_;
116120
int j_surf_;

openmc/summary.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,23 @@ def _read_materials(self):
127127
self._fast_materials[material.id] = material
128128

129129
def _read_surfaces(self):
130+
periodic_surface_ids = set()
130131
for group in self._f['geometry/surfaces'].values():
131132
surface = openmc.Surface.from_hdf5(group)
132133
# surface may be None for DAGMC surfaces
133134
if surface:
134135
self._fast_surfaces[surface.id] = surface
136+
if surface.boundary_type == "periodic":
137+
periodic_surface_ids.add(surface.id)
138+
139+
# Assign periodic surfaces when information is in file
140+
for surface_id in periodic_surface_ids:
141+
group = self._f[f'geometry/surfaces/surface {surface_id}']
142+
surface = self._fast_surfaces[surface_id]
143+
if 'periodic_surface_id' in group:
144+
periodic_surface_id = int(group['periodic_surface_id'][()])
145+
surface.periodic_surface = self._fast_surfaces[periodic_surface_id]
146+
135147

136148
def _read_cells(self):
137149

src/surface.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,19 @@ void Surface::to_hdf5(hid_t group_id) const
172172
if (bc_) {
173173
write_string(surf_group, "boundary_type", bc_->type(), false);
174174
bc_->to_hdf5(surf_group);
175+
176+
// write periodic surface ID
177+
if (bc_->type() == "periodic") {
178+
auto pbc = dynamic_cast<PeriodicBC*>(bc_.get());
179+
Surface& surf1 {*model::surfaces[pbc->i_surf()]};
180+
Surface& surf2 {*model::surfaces[pbc->j_surf()]};
181+
182+
if (id_ == surf1.id_) {
183+
write_dataset(surf_group, "periodic_surface_id", surf2.id_);
184+
} else {
185+
write_dataset(surf_group, "periodic_surface_id", surf1.id_);
186+
}
187+
}
175188
} else {
176189
write_string(surf_group, "boundary_type", "transmission", false);
177190
}

tests/unit_tests/test_summary.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import openmc
2+
3+
4+
def test_periodic_surface_roundtrip(run_in_tmpdir):
5+
# Create a simple model with periodic surfaces
6+
mat = openmc.Material()
7+
mat.add_nuclide('H1', 1.0)
8+
mat.set_density('g/cm3', 1.0)
9+
cyl = openmc.ZCylinder(r=1.0)
10+
x0 = openmc.XPlane(-5.0, boundary_type='periodic')
11+
y0 = openmc.YPlane(-5.0, boundary_type='periodic')
12+
z0 = openmc.ZPlane(-5.0, boundary_type='periodic')
13+
x1 = openmc.XPlane(5.0, boundary_type='periodic')
14+
y1 = openmc.YPlane(5.0, boundary_type='periodic')
15+
z1 = openmc.ZPlane(5.0, boundary_type='periodic')
16+
x0.periodic_surface = x1
17+
y0.periodic_surface = y1
18+
z0.periodic_surface = z1
19+
cell1 = openmc.Cell(fill=mat, region=-cyl)
20+
cell2 = openmc.Cell(fill=mat, region=+cyl & +x0 & -x1 & +y0 & -y1 & +z0 & -z1)
21+
model = openmc.Model()
22+
model.geometry = openmc.Geometry([cell1, cell2])
23+
model.settings.particles = 100
24+
model.settings.batches = 1
25+
model.settings.run_mode = 'fixed source'
26+
model.settings.source = openmc.IndependentSource(
27+
energy=openmc.stats.delta_function(1.0e4)
28+
)
29+
30+
# Run model
31+
model.run()
32+
33+
# Load summary data and check periodic surfaces
34+
summary = openmc.Summary('summary.h5')
35+
surfs = summary.geometry.get_all_surfaces()
36+
for s in [x0, y0, z0, x1, y1, z1]:
37+
assert surfs[s.id].boundary_type == 'periodic'
38+
pairs = [(x0, x1), (y0, y1), (z0, z1)]
39+
for s0, s1 in pairs:
40+
assert surfs[s0.id].periodic_surface == surfs[s1.id]
41+
assert surfs[s1.id].periodic_surface == surfs[s0.id]

0 commit comments

Comments
 (0)