Use dynamic_cast to get type of sub-class instead of storing type as variable

This commit is contained in:
Shikhar Kumar 2021-02-12 19:18:57 -05:00
parent b4e00b7324
commit b3b6f6fedb
2 changed files with 32 additions and 23 deletions

View file

@ -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<int>& 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<int>& 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<int>& bins,
std::vector<double>& 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

View file

@ -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<StructuredMesh*>(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<RegularMesh*>(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<RectilinearMesh*>(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<StructuredMesh*>(model::meshes[index].get());
std::strcpy(type, mesh->type().c_str());
RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
if (mesh) {
std::strcpy(type, "regular");
}
else {
RectilinearMesh* mesh = dynamic_cast<RectilinearMesh*>(model::meshes[index].get());
if (mesh) {
std::strcpy(type, "rectilinear");
}
}
return 0;
}
RegularMesh* get_regular_mesh(int32_t index) {
return dynamic_cast<RegularMesh*>(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