#include #include #include #include #include #include "openmc/hdf5_interface.h" #include "openmc/mesh.h" using namespace openmc; TEST_CASE("Test mesh hdf5 roundtrip - regular") { // The XML data as a string std::string xml_string = R"( 3 4 5 -2 -3 -5 2 3 5 )"; // 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); hid_t file_id = file_open("mesh.h5", 'w'); mesh.to_hdf5(file_id); file_close(file_id); hid_t file_id2 = file_open("mesh.h5", 'r'); hid_t group = open_group(file_id2, "mesh 1"); auto mesh2 = RegularMesh(group); file_close(file_id2); remove("mesh.h5"); REQUIRE(mesh2.shape_ == mesh.shape_); REQUIRE(mesh2.lower_left() == mesh.lower_left()); REQUIRE(mesh2.upper_right() == mesh.upper_right()); } TEST_CASE("Test mesh hdf5 roundtrip - rectilinear") { // The XML data as a string std::string xml_string = R"( 0.0 1.0 5.0 10.0 -10.0 -5.0 0.0 -100.0 0.0 100.0 )"; // 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); hid_t file_id = file_open("mesh.h5", 'w'); mesh.to_hdf5(file_id); file_close(file_id); hid_t file_id2 = file_open("mesh.h5", 'r'); hid_t group = open_group(file_id2, "mesh 1"); auto mesh2 = RectilinearMesh(group); file_close(file_id2); remove("mesh.h5"); REQUIRE(mesh2.shape_ == mesh.shape_); REQUIRE(mesh2.grid_ == mesh.grid_); } TEST_CASE("Test mesh hdf5 roundtrip - cylindrical") { // The XML data as a string std::string xml_string = R"( 0.1 0.2 0.5 1.0 0.0 6.283185307179586 0.1 0.2 0.4 0.6 1.0 0 0 0 )"; // 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 = CylindricalMesh(root); hid_t file_id = file_open("mesh.h5", 'w'); mesh.to_hdf5(file_id); file_close(file_id); hid_t file_id2 = file_open("mesh.h5", 'r'); hid_t group = open_group(file_id2, "mesh 1"); auto mesh2 = CylindricalMesh(group); file_close(file_id2); remove("mesh.h5"); REQUIRE(mesh2.shape_ == mesh.shape_); REQUIRE(mesh2.grid_ == mesh.grid_); } TEST_CASE("Test mesh hdf5 roundtrip - spherical") { // The XML data as a string std::string xml_string = R"( 0.1 0.2 0.5 1.0 0.0 3.141592653589793 0.0 6.283185307179586 0.0 0.0 0.0 ' )"; // 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 = SphericalMesh(root); hid_t file_id = file_open("mesh.h5", 'w'); mesh.to_hdf5(file_id); file_close(file_id); hid_t file_id2 = file_open("mesh.h5", 'r'); hid_t group = open_group(file_id2, "mesh 1"); auto mesh2 = SphericalMesh(group); file_close(file_id2); remove("mesh.h5"); REQUIRE(mesh2.shape_ == mesh.shape_); REQUIRE(mesh2.grid_ == mesh.grid_); } TEST_CASE("Test multiple meshes HDF5 roundtrip - spherical") { // The XML data as a string std::string xml_string = R"( 0.1 0.2 0.5 1.0 0.0 3.141592653589793 0.0 6.283185307179586 0.0 0.0 0.0 3 4 5 -2 -3 -5 2 3 5 )"; // 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("meshes"); read_meshes(root); const auto spherical_mesh_xml = dynamic_cast(model::meshes[0].get()); const auto regular_mesh_xml = dynamic_cast(model::meshes[1].get()); hid_t file_id = file_open("meshes.h5", 'w'); hid_t root_group = create_group(file_id, "root"); open_group(file_id, "root"); meshes_to_hdf5(root_group); close_group(root_group); file_close(file_id); hid_t file_id2 = file_open("meshes.h5", 'r'); hid_t root_group_read = open_group(file_id2, "root"); hid_t mesh_group_read = open_group(root_group_read, "meshes"); read_meshes(mesh_group_read); // increment mesh IDs to avoid collision during read for (auto& mesh : model::meshes) { mesh->set_id(mesh->id() + 10); } const auto spherical_mesh_hdf5 = dynamic_cast( model::meshes[model::mesh_map[spherical_mesh_xml->id_]].get()); const auto regular_mesh_hdf5 = dynamic_cast( model::meshes[model::mesh_map[regular_mesh_xml->id_]].get()); remove("meshes.h5"); REQUIRE(spherical_mesh_hdf5->shape_ == spherical_mesh_xml->shape_); REQUIRE(spherical_mesh_hdf5->grid_ == spherical_mesh_xml->grid_); REQUIRE(regular_mesh_hdf5->shape_ == regular_mesh_xml->shape_); REQUIRE(regular_mesh_hdf5->lower_left() == regular_mesh_xml->lower_left()); REQUIRE(regular_mesh_hdf5->upper_right() == regular_mesh_xml->upper_right()); }