Skip to content

Commit 49bfb76

Browse files
Unit test for the distance to next boundary method
1 parent 76cdf20 commit 49bfb76

3 files changed

Lines changed: 42 additions & 6 deletions

File tree

include/openmc/mesh.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class Mesh {
181181
//! \param[in] r Position of the particle
182182
//! \param[in] u Particle direction
183183
//! \return Distance to the next boundary
184-
virtual double distance_to_next_boundary(Position r, Position u) const = 0;
184+
virtual double distance_to_next_boundary(Position r, Direction u) const = 0;
185185

186186
//! Get bin at a given position in space
187187
//
@@ -312,7 +312,7 @@ class StructuredMesh : public Mesh {
312312
void surface_bins_crossed(Position r0, Position r1, const Direction& u,
313313
vector<int>& bins) const override;
314314

315-
double distance_to_next_boundary(Position r, Position u) const override;
315+
double distance_to_next_boundary(Position r, Direction u) const override;
316316

317317
//! Determine which cell or surface bins were crossed by a particle
318318
//
@@ -696,7 +696,7 @@ class UnstructuredMesh : public Mesh {
696696
UnstructuredMesh(pugi::xml_node node);
697697
UnstructuredMesh(hid_t group);
698698

699-
double distance_to_next_boundary(Position r, Position u) const override;
699+
double distance_to_next_boundary(Position r, Direction u) const override;
700700

701701
static const std::string mesh_type;
702702
virtual std::string get_mesh_type() const override;

src/mesh.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ UnstructuredMesh::UnstructuredMesh(hid_t group) : Mesh(group)
720720
}
721721
}
722722

723-
double UnstructuredMesh::distance_to_next_boundary(Position r, Position u) const
723+
double UnstructuredMesh::distance_to_next_boundary(Position r, Direction u) const
724724
{
725725
fatal_error("Not implemented");
726726
return -1.0;
@@ -1181,12 +1181,12 @@ void StructuredMesh::surface_bins_crossed(
11811181
raytrace_mesh(r0, r1, u, SurfaceAggregator(this, bins));
11821182
}
11831183

1184-
double StructuredMesh::distance_to_next_boundary(Position r, Position u) const
1184+
double StructuredMesh::distance_to_next_boundary(Position r, Direction u) const
11851185
{
11861186
Position global_r = r;
11871187
Position local_r = local_coords(r);
11881188

1189-
double distance = INFTY;
1189+
double distance = 0.0;
11901190
const int n = n_dimension_;
11911191
bool in_mesh;
11921192

tests/cpp_unit_tests/test_mesh.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,39 @@ TEST_CASE("Test multiple meshes HDF5 roundtrip - spherical")
255255
REQUIRE(regular_mesh_hdf5->lower_left() == regular_mesh_xml->lower_left());
256256
REQUIRE(regular_mesh_hdf5->upper_right() == regular_mesh_xml->upper_right());
257257
}
258+
259+
TEST_CASE("Test distance_to_next_boundary() method for regular mesh")
260+
{
261+
// The XML data as a string
262+
std::string xml_string = R"(
263+
<mesh id="1">
264+
<dimension>2 2 2</dimension>
265+
<lower_left>-1 -1 -1</lower_left>
266+
<upper_right>1 1 1</upper_right>
267+
</mesh>
268+
)";
269+
270+
// Create the mesh from a file
271+
pugi::xml_document doc;
272+
pugi::xml_parse_result result = doc.load_string(xml_string.c_str());
273+
pugi::xml_node root = doc.child("mesh");
274+
auto mesh = RegularMesh(root);
275+
276+
Position r;
277+
Position u;
278+
279+
// Test inside the mesh
280+
r = Position(0.0, 0.0, 0.0);
281+
u = Position(1.0, 0.0, 0.0);
282+
REQUIRE(mesh.distance_to_next_boundary(r, u) == 1.0);
283+
284+
// Test outside the mesh, going toward the mesh
285+
r = Position(-1.5, 0.0, 0.0);
286+
u = Position(1.0, 0.0, 0.0);
287+
REQUIRE(mesh.distance_to_next_boundary(r, u) == 0.5);
288+
289+
// Test outside the mesh, not going toward the mesh
290+
r = Position(-2.0, 0.0, 0.0);
291+
u = Position(-1.0, 0.0, 0.0);
292+
REQUIRE(mesh.distance_to_next_boundary(r, u) == INFTY);
293+
}

0 commit comments

Comments
 (0)