Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1472,7 +1472,11 @@ RegularMesh::RegularMesh(hid_t group) : StructuredMesh {group}

int RegularMesh::get_index_in_direction(double r, int i) const
{
return std::ceil((r - lower_left_[i]) / width_[i]);
if (r == lower_left_[i]) {
Comment thread
GuySten marked this conversation as resolved.
return 1;
} else {
return std::ceil((r - lower_left_[i]) / width_[i]);
}
}

const std::string RegularMesh::mesh_type = "regular";
Expand Down
54 changes: 54 additions & 0 deletions tests/cpp_unit_tests/test_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,57 @@ TEST_CASE("Test multiple meshes HDF5 roundtrip - spherical")
REQUIRE(regular_mesh_hdf5->lower_left() == regular_mesh_xml->lower_left());
REQUIRE(regular_mesh_hdf5->upper_right() == regular_mesh_xml->upper_right());
}

TEST_CASE("Test get_index_in_direction - regular mesh")
{
// The XML data as a string
std::string xml_string = R"(
<mesh id="1">
<dimension>3 4 5</dimension>
<lower_left>-2 -3 -5</lower_left>
<upper_right>2 3 5</upper_right>
</mesh>
)";

// Create a pugixml document object
pugi::xml_document doc;

// Load the XML from the string
pugi::xml_parse_result result = doc.load_string(xml_string.c_str());

pugi::xml_node root = doc.child("mesh");

auto mesh = RegularMesh(root);

REQUIRE(mesh.get_index_in_direction(-2.0, 0) == 1);
REQUIRE(mesh.get_index_in_direction(-1.99, 0) == 1);
REQUIRE(mesh.get_index_in_direction(1.99, 0) == 3);
REQUIRE(mesh.get_index_in_direction(2.0, 0) == 3);
}

TEST_CASE("Test get_index_in_direction - rectilinear mesh")
{
// The XML data as a string
std::string xml_string = R"(
<mesh id="1" type="rectilinear">
<x_grid>0.0 1.0 5.0 10.0</x_grid>
<y_grid>-10.0 -5.0 0.0</y_grid>
<z_grid>-100.0 0.0 100.0</z_grid>
</mesh>
)";

// Create a pugixml document object
pugi::xml_document doc;

// Load the XML from the string
pugi::xml_parse_result result = doc.load_string(xml_string.c_str());

pugi::xml_node root = doc.child("mesh");

auto mesh = RectilinearMesh(root);

REQUIRE(mesh.get_index_in_direction(0.0, 0) == 1);
REQUIRE(mesh.get_index_in_direction(0.01, 0) == 1);
REQUIRE(mesh.get_index_in_direction(9.99, 0) == 3);
REQUIRE(mesh.get_index_in_direction(10.0, 0) == 3);
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
386e507008ed3c72c6e1a101aafc01cfaaff2c0b10555558d1eab6332441f7b17264b55002d721151bac2f3e7c1a8aac4c5ed243f5270533d171850f985206ed
0d7b17d4e364bda2be21feb2e5b2aae4be69fc8ed634c4f45062e8efc61b7bd24dcf448837692fd603afe3756501fe8821d134f2d6289179618f85178ddb8793
Original file line number Diff line number Diff line change
@@ -1 +1 @@
d17a437262d3316985fba4b48e21a7fcd5f32ac2d96ef0e66849f567deaeadc1e0f18d5d0bf5e9cab4246dbf823e7f43f249a1f67e928b27ae8c70b89f3cefbf
2f1c8efbf6ab0b0c7319fadd7ee8f27b545c1ebab94ebe15cf1133b54227487542966b6d798970c8b37bc19e7041e687687d5285803241bb83f4c8dc40a4d276
Loading