Skip to content

Commit 5dd6ff6

Browse files
authored
Fix negative distances from bins_crossed for CylindricalMesh (#3370)
1 parent a113440 commit 5dd6ff6

File tree

6 files changed

+29
-33
lines changed

6 files changed

+29
-33
lines changed

include/openmc/mesh.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,6 @@ class Mesh {
138138
//! Perform any preparation needed to support point location within the mesh
139139
virtual void prepare_for_point_location() {};
140140

141-
//! Update a position to the local coordinates of the mesh
142-
virtual void local_coords(Position& r) const {};
143-
144141
//! Return a position in the local coordinates of the mesh
145142
virtual Position local_coords(const Position& r) const { return r; };
146143

@@ -427,8 +424,6 @@ class PeriodicStructuredMesh : public StructuredMesh {
427424
PeriodicStructuredMesh() = default;
428425
PeriodicStructuredMesh(pugi::xml_node node) : StructuredMesh {node} {};
429426

430-
void local_coords(Position& r) const override { r -= origin_; };
431-
432427
Position local_coords(const Position& r) const override
433428
{
434429
return r - origin_;

src/mesh.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,11 @@ void StructuredMesh::raytrace_mesh(
915915
if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
916916
return;
917917

918+
// keep a copy of the original global position to pass to get_indices,
919+
// which performs its own transformation to local coordinates
920+
Position global_r = r0;
921+
Position local_r = local_coords(r0);
922+
918923
const int n = n_dimension_;
919924

920925
// Flag if position is inside the mesh
@@ -925,7 +930,7 @@ void StructuredMesh::raytrace_mesh(
925930

926931
// Calculate index of current cell. Offset the position a tiny bit in
927932
// direction of flight
928-
MeshIndex ijk = get_indices(r0 + TINY_BIT * u, in_mesh);
933+
MeshIndex ijk = get_indices(global_r + TINY_BIT * u, in_mesh);
929934

930935
// if track is very short, assume that it is completely inside one cell.
931936
// Only the current cell will score and no surfaces
@@ -936,16 +941,10 @@ void StructuredMesh::raytrace_mesh(
936941
return;
937942
}
938943

939-
// translate start and end positions,
940-
// this needs to come after the get_indices call because it does its own
941-
// translation
942-
local_coords(r0);
943-
local_coords(r1);
944-
945944
// Calculate initial distances to next surfaces in all three dimensions
946945
std::array<MeshDistance, 3> distances;
947946
for (int k = 0; k < n; ++k) {
948-
distances[k] = distance_to_grid_boundary(ijk, k, r0, u, 0.0);
947+
distances[k] = distance_to_grid_boundary(ijk, k, local_r, u, 0.0);
949948
}
950949

951950
// Loop until r = r1 is eventually reached
@@ -975,7 +974,7 @@ void StructuredMesh::raytrace_mesh(
975974
// The two other directions are still valid!
976975
ijk[k] = distances[k].next_index;
977976
distances[k] =
978-
distance_to_grid_boundary(ijk, k, r0, u, traveled_distance);
977+
distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
979978

980979
// Check if we have left the interior of the mesh
981980
in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
@@ -1005,10 +1004,10 @@ void StructuredMesh::raytrace_mesh(
10051004

10061005
// Calculate the new cell index and update all distances to next
10071006
// surfaces.
1008-
ijk = get_indices(r0 + (traveled_distance + TINY_BIT) * u, in_mesh);
1007+
ijk = get_indices(global_r + (traveled_distance + TINY_BIT) * u, in_mesh);
10091008
for (int k = 0; k < n; ++k) {
10101009
distances[k] =
1011-
distance_to_grid_boundary(ijk, k, r0, u, traveled_distance);
1010+
distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
10121011
}
10131012

10141013
// If inside the mesh, Tally inward current
@@ -1482,7 +1481,7 @@ std::string CylindricalMesh::get_mesh_type() const
14821481
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
14831482
Position r, bool& in_mesh) const
14841483
{
1485-
local_coords(r);
1484+
r = local_coords(r);
14861485

14871486
Position mapped_r;
14881487
mapped_r[0] = std::hypot(r.x, r.y);
@@ -1630,23 +1629,21 @@ StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
16301629
const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
16311630
double l) const
16321631
{
1633-
Position r = r0 - origin_;
1634-
16351632
if (i == 0) {
16361633

16371634
return std::min(
1638-
MeshDistance(ijk[i] + 1, true, find_r_crossing(r, u, l, ijk[i])),
1639-
MeshDistance(ijk[i] - 1, false, find_r_crossing(r, u, l, ijk[i] - 1)));
1635+
MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
1636+
MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
16401637

16411638
} else if (i == 1) {
16421639

16431640
return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
1644-
find_phi_crossing(r, u, l, ijk[i])),
1641+
find_phi_crossing(r0, u, l, ijk[i])),
16451642
MeshDistance(sanitize_phi(ijk[i] - 1), false,
1646-
find_phi_crossing(r, u, l, ijk[i] - 1)));
1643+
find_phi_crossing(r0, u, l, ijk[i] - 1)));
16471644

16481645
} else {
1649-
return find_z_crossing(r, u, l, ijk[i]);
1646+
return find_z_crossing(r0, u, l, ijk[i]);
16501647
}
16511648
}
16521649

@@ -1762,7 +1759,7 @@ std::string SphericalMesh::get_mesh_type() const
17621759
StructuredMesh::MeshIndex SphericalMesh::get_indices(
17631760
Position r, bool& in_mesh) const
17641761
{
1765-
local_coords(r);
1762+
r = local_coords(r);
17661763

17671764
Position mapped_r;
17681765
mapped_r[0] = r.norm();

tests/regression_tests/filter_mesh/inputs_true.dat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
<mesh id="5" type="cylindrical">
5555
<r_grid>0.0 0.4411764705882353 0.8823529411764706 1.3235294117647058 1.7647058823529411 2.2058823529411766 2.6470588235294117 3.0882352941176467 3.5294117647058822 3.9705882352941178 4.411764705882353 4.852941176470588 5.294117647058823 5.735294117647059 6.1764705882352935 6.617647058823529 7.0588235294117645 7.5</r_grid>
5656
<phi_grid>0.0 0.3490658503988659 0.6981317007977318 1.0471975511965976 1.3962634015954636 1.7453292519943295 2.0943951023931953 2.443460952792061 2.792526803190927 3.141592653589793 3.490658503988659 3.839724354387525 4.1887902047863905 4.537856055185257 4.886921905584122 5.235987755982989 5.585053606381854 5.93411945678072 6.283185307179586</phi_grid>
57-
<z_grid>-7.5 -6.5625 -5.625 -4.6875 -3.75 -2.8125 -1.875 -0.9375 0.0 0.9375 1.875 2.8125 3.75 4.6875 5.625 6.5625 7.5</z_grid>
58-
<origin>0.0 0.0 0.0</origin>
57+
<z_grid>0.0 0.9375 1.875 2.8125 3.75 4.6875 5.625 6.5625 7.5 8.4375 9.375 10.3125 11.25 12.1875 13.125 14.0625 15.0</z_grid>
58+
<origin>0.0 0.0 -7.5</origin>
5959
</mesh>
6060
<mesh id="6" type="spherical">
6161
<r_grid>0.0 0.4411764705882353 0.8823529411764706 1.3235294117647058 1.7647058823529411 2.2058823529411766 2.6470588235294117 3.0882352941176467 3.5294117647058822 3.9705882352941178 4.411764705882353 4.852941176470588 5.294117647058823 5.735294117647059 6.1764705882352935 6.617647058823529 7.0588235294117645 7.5</r_grid>

tests/regression_tests/filter_mesh/test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ def model():
6161
np.testing.assert_allclose(recti_mesh.volumes, recti_mesh_exp_vols)
6262

6363
cyl_mesh = openmc.CylindricalMesh(
64+
origin=(0, 0, -7.5),
6465
r_grid=np.linspace(0, 7.5, 18),
6566
phi_grid=np.linspace(0, 2*pi, 19),
66-
z_grid=np.linspace(-7.5, 7.5, 17),
67+
z_grid=np.linspace(0, 15, 17),
6768
)
6869
dr = 0.5 * np.diff(np.linspace(0, 7.5, 18)**2)
6970
dp = np.full(cyl_mesh.dimension[1], 2*pi / 18)

tests/unit_tests/test_cylindrical_mesh.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def model():
4949

5050
return openmc.Model(geometry=geom, settings=settings, tallies=tallies)
5151

52+
5253
def test_origin_read_write_to_xml(run_in_tmpdir, model):
5354
"""Tests that the origin attribute can be written and read back to XML
5455
"""
@@ -63,8 +64,9 @@ def test_origin_read_write_to_xml(run_in_tmpdir, model):
6364
np.testing.assert_equal(new_mesh.origin, mesh.origin)
6465

6566
estimators = ('tracklength', 'collision')
66-
origins = set(permutations((-geom_size, 0, 0)))
67-
origins |= set(permutations((geom_size, 0, 0)))
67+
offset = geom_size + 0.001
68+
origins = set(permutations((-offset , 0, 0)))
69+
origins |= set(permutations((offset, 0, 0)))
6870

6971
test_cases = product(estimators, origins)
7072

@@ -74,8 +76,9 @@ def label(p):
7476
if isinstance(p, str):
7577
return f'estimator:{p}'
7678

79+
7780
@pytest.mark.parametrize('estimator,origin', test_cases, ids=label)
78-
def test_offset_mesh(run_in_tmpdir, model, estimator, origin):
81+
def test_offset_mesh(model, estimator, origin):
7982
"""Tests that the mesh has been moved based on tally results
8083
"""
8184
mesh = model.tallies[0].filters[0].mesh
@@ -103,6 +106,7 @@ def test_offset_mesh(run_in_tmpdir, model, estimator, origin):
103106
else:
104107
mean[i, j, k] != 0.0
105108

109+
106110
@pytest.fixture()
107111
def void_coincident_geom_model():
108112
"""A model with many geometric boundaries coincident with mesh boundaries
@@ -155,6 +159,7 @@ def _check_void_cylindrical_tally(statepoint_filename):
155159
d_r = mesh.r_grid[1] - mesh.r_grid[0]
156160
assert neutron_flux == pytest.approx(d_r)
157161

162+
158163
def test_void_geom_pnt_src(run_in_tmpdir, void_coincident_geom_model):
159164
src = openmc.IndependentSource()
160165
src.space = openmc.stats.Point()

tests/unit_tests/test_spherical_mesh.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ def test_origin_read_write_to_xml(run_in_tmpdir, model):
6363
np.testing.assert_equal(new_mesh.origin, mesh.origin)
6464

6565
estimators = ('tracklength', 'collision')
66-
# TODO: determine why this is needed for spherical mesh
67-
# but not cylindrical mesh
6866
offset = geom_size + 0.001
6967

7068
origins = set(permutations((-offset, 0, 0)))

0 commit comments

Comments
 (0)