From b3b6f6fedbba91e988e39e3773f6421b6c9b18d2 Mon Sep 17 00:00:00 2001 From: Shikhar Kumar Date: Fri, 12 Feb 2021 19:18:57 -0500 Subject: [PATCH] Use dynamic_cast to get type of sub-class instead of storing type as variable --- include/openmc/mesh.h | 10 ---------- src/mesh.cpp | 45 ++++++++++++++++++++++++++++++------------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/include/openmc/mesh.h b/include/openmc/mesh.h index b2e35819c6..b2d919d30a 100644 --- a/include/openmc/mesh.h +++ b/include/openmc/mesh.h @@ -46,8 +46,6 @@ public: Mesh(pugi::xml_node node); virtual ~Mesh() = default; - virtual std::string type() const = 0; - // Methods //! Determine which bins were crossed by a particle @@ -196,8 +194,6 @@ public: // Overriden methods - std::string type() const override {return "regular";} - void surface_bins_crossed(const Particle& p, std::vector& bins) const override; @@ -228,8 +224,6 @@ public: // Overriden methods - std::string type() const override {return "rectilinear";} - void surface_bins_crossed(const Particle& p, std::vector& bins) const override; @@ -256,8 +250,6 @@ public: UnstructuredMesh(pugi::xml_node); ~UnstructuredMesh() = default; - std::string type() const override {return "unstructured";} - void bins_crossed(const Particle& p, std::vector& bins, std::vector& lengths) const override; @@ -430,8 +422,6 @@ void read_meshes(pugi::xml_node root); //! \param[in] group HDF5 group void meshes_to_hdf5(hid_t group); -RegularMesh* get_regular_mesh(int32_t index); - void free_memory_mesh(); } // namespace openmc diff --git a/src/mesh.cpp b/src/mesh.cpp index 576ae3e772..a4b93e9af4 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -865,7 +865,7 @@ void RegularMesh::to_hdf5(hid_t group) const { hid_t mesh_group = create_group(group, "mesh " + std::to_string(id_)); - write_dataset(mesh_group, "type", type()); + write_dataset(mesh_group, "type", "regular"); write_dataset(mesh_group, "dimension", shape_); write_dataset(mesh_group, "lower_left", lower_left_); write_dataset(mesh_group, "upper_right", upper_right_); @@ -1122,7 +1122,7 @@ void RectilinearMesh::to_hdf5(hid_t group) const { hid_t mesh_group = create_group(group, "mesh " + std::to_string(id_)); - write_dataset(mesh_group, "type", type()); + write_dataset(mesh_group, "type", "rectilinear"); write_dataset(mesh_group, "x_grid", grid_[0]); write_dataset(mesh_group, "y_grid", grid_[1]); write_dataset(mesh_group, "z_grid", grid_[2]); @@ -1148,10 +1148,23 @@ int check_mesh_type(int32_t index, const std::string& mesh_compare_type) { if (int err = check_mesh(index)) return err; - StructuredMesh* mesh = dynamic_cast(model::meshes[index].get()); - auto mesh_type = mesh->type(); - if (mesh_compare_type != mesh_type) { - set_errmsg("This function is only valid for " + mesh_type + " meshes."); + + if (mesh_compare_type == "regular") { + RegularMesh* mesh = dynamic_cast(model::meshes[index].get()); + if (!mesh) { + set_errmsg("This function is only valid for regular meshes."); + return OPENMC_E_INVALID_TYPE; + } + } + else if (mesh_compare_type == "rectilinear") { + RectilinearMesh* mesh = dynamic_cast(model::meshes[index].get()); + if (!mesh) { + set_errmsg("This function is only valid for rectilinear meshes."); + return OPENMC_E_INVALID_TYPE; + } + } + else { + set_errmsg("Mesh type " + mesh_compare_type + " is not supported."); return OPENMC_E_INVALID_TYPE; } return 0; @@ -1167,21 +1180,27 @@ openmc_mesh_get_type(int32_t index, char* type) { if (int err = check_mesh(index)) return err; - StructuredMesh* mesh = dynamic_cast(model::meshes[index].get()); - std::strcpy(type, mesh->type().c_str()); + RegularMesh* mesh = dynamic_cast(model::meshes[index].get()); + if (mesh) { + std::strcpy(type, "regular"); + } + else { + RectilinearMesh* mesh = dynamic_cast(model::meshes[index].get()); + if (mesh) { + std::strcpy(type, "rectilinear"); + } + } return 0; } -RegularMesh* get_regular_mesh(int32_t index) { - return dynamic_cast(model::meshes[index].get()); -} - //! Extend the meshes array by n elements extern "C" int openmc_extend_meshes(int32_t n, const char* type, int32_t* index_start, int32_t* index_end) { if (index_start) *index_start = model::meshes.size(); + std::string mesh_type; + for (int i = 0; i < n; ++i) { if (std::strcmp(type, "regular") == 0) { @@ -1670,7 +1689,7 @@ UnstructuredMesh::to_hdf5(hid_t group) const { hid_t mesh_group = create_group(group, fmt::format("mesh {}", id_)); - write_dataset(mesh_group, "type", type()); + write_dataset(mesh_group, "type", "unstructured"); write_dataset(mesh_group, "filename", filename_); // write volume and centroid of each tet