From 128faa49b03fd633b3bf858d8c844a655261d642 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Thu, 23 Apr 2020 14:23:15 -0500 Subject: [PATCH] Add particle_type_to_str and str_to_particle_type functions --- include/openmc/particle.h | 16 +++++++++---- src/particle.cpp | 30 +++++++++++++++++++++++++ src/physics.cpp | 2 +- src/reaction_product.cpp | 7 ++---- src/tallies/filter_particle.cpp | 40 +++++---------------------------- 5 files changed, 51 insertions(+), 44 deletions(-) diff --git a/include/openmc/particle.h b/include/openmc/particle.h index 2917b45e4..85641f7c4 100644 --- a/include/openmc/particle.h +++ b/include/openmc/particle.h @@ -157,7 +157,7 @@ public: }; //! Saved ("banked") state of a particle - //! NOTE: This structure's MPI type is built in initialize_mpi() of + //! NOTE: This structure's MPI type is built in initialize_mpi() of //! initialize.cpp. Any changes made to the struct here must also be //! made when building the Bank MPI type in initialize_mpi(). //! NOTE: This structure is also used on the python side, and is defined @@ -173,7 +173,7 @@ public: int64_t parent_id; int64_t progeny_id; }; - + //! Saved ("banked") state of a particle, for nu-fission tallying struct NuBank { double E; //!< particle energy @@ -311,7 +311,7 @@ public: int cell_born_ {-1}; //!< index for cell particle was born in int material_ {-1}; //!< index for current material int material_last_ {-1}; //!< index for last material - + // Boundary information BoundaryInfo boundary_; @@ -328,7 +328,7 @@ public: // Current PRNG state uint64_t seeds_[N_STREAMS]; // current seeds int stream_; // current RNG stream - + // Secondary particle bank std::vector secondary_bank_; @@ -363,6 +363,14 @@ public: int64_t n_progeny_ {0}; // Number of progeny produced by this particle }; +//============================================================================ +//! Functions +//============================================================================ + +std::string particle_type_to_str(Particle::Type type); + +Particle::Type str_to_particle_type(std::string str); + } // namespace openmc #endif // OPENMC_PARTICLE_H diff --git a/src/particle.cpp b/src/particle.cpp index 481d7755a..c4458a1e4 100644 --- a/src/particle.cpp +++ b/src/particle.cpp @@ -707,4 +707,34 @@ Particle::write_restart() const } // #pragma omp critical } +std::string particle_type_to_str(Particle::Type type) +{ + switch (type) { + case Particle::Type::neutron: + return "neutron"; + case Particle::Type::photon: + return "photon"; + case Particle::Type::electron: + return "electron"; + case Particle::Type::positron: + return "positron"; + } + UNREACHABLE(); +} + +Particle::Type str_to_particle_type(std::string str) +{ + if (str == "neutron") { + return Particle::Type::neutron; + } else if (str == "photon") { + return Particle::Type::photon; + } else if (str == "electron") { + return Particle::Type::electron; + } else if (str == "positron") { + return Particle::Type::positron; + } else { + throw std::invalid_argument{fmt::format("Invalid particle name: {}", str)}; + } +} + } // namespace openmc diff --git a/src/physics.cpp b/src/physics.cpp index 7cff949ab..b8f2667b1 100644 --- a/src/physics.cpp +++ b/src/physics.cpp @@ -38,7 +38,7 @@ void collision(Particle& p) ++(p.n_collision_); // Sample reaction for the material the particle is in - switch (static_cast(p.type_)) { + switch (p.type_) { case Particle::Type::neutron: sample_neutron_reaction(p); break; diff --git a/src/reaction_product.cpp b/src/reaction_product.cpp index 50870a3d1..495e4dfa5 100644 --- a/src/reaction_product.cpp +++ b/src/reaction_product.cpp @@ -5,6 +5,7 @@ #include "openmc/endf.h" #include "openmc/hdf5_interface.h" +#include "openmc/particle.h" #include "openmc/random_lcg.h" #include "openmc/secondary_correlated.h" #include "openmc/secondary_kalbach.h" @@ -22,11 +23,7 @@ ReactionProduct::ReactionProduct(hid_t group) // Read particle type std::string temp; read_attribute(group, "particle", temp); - if (temp == "neutron") { - particle_ = Particle::Type::neutron; - } else if (temp == "photon") { - particle_ = Particle::Type::photon; - } + particle_ = str_to_particle_type(temp); // Read emission mode and decay rate read_attribute(group, "emission_mode", temp); diff --git a/src/tallies/filter_particle.cpp b/src/tallies/filter_particle.cpp index 8354f0a2d..c2b045a67 100644 --- a/src/tallies/filter_particle.cpp +++ b/src/tallies/filter_particle.cpp @@ -1,5 +1,7 @@ #include "openmc/tallies/filter_particle.h" +#include + #include "openmc/xml_interface.h" namespace openmc { @@ -12,15 +14,7 @@ ParticleFilter::from_xml(pugi::xml_node node) // Convert to vector of Particle::Type std::vector types; for (auto& p : particles) { - if (p == "neutron") { - types.push_back(Particle::Type::neutron); - } else if (p == "photon") { - types.push_back(Particle::Type::photon); - } else if (p == "electron") { - types.push_back(Particle::Type::electron); - } else if (p == "positron") { - types.push_back(Particle::Type::positron); - } + types.push_back(str_to_particle_type(p)); } this->set_particles(types); } @@ -57,20 +51,7 @@ ParticleFilter::to_statepoint(hid_t filter_group) const Filter::to_statepoint(filter_group); std::vector particles; for (auto p : particles_) { - switch (p) { - case Particle::Type::neutron: - particles.push_back("neutron"); - break; - case Particle::Type::photon: - particles.push_back("photon"); - break; - case Particle::Type::electron: - particles.push_back("electron"); - break; - case Particle::Type::positron: - particles.push_back("positron"); - break; - } + particles.push_back(particle_type_to_str(p)); } write_dataset(filter_group, "bins", particles); } @@ -78,17 +59,8 @@ ParticleFilter::to_statepoint(hid_t filter_group) const std::string ParticleFilter::text_label(int bin) const { - switch (particles_[bin]) { - case Particle::Type::neutron: - return "Particle: neutron"; - case Particle::Type::photon: - return "Particle: photon"; - case Particle::Type::electron: - return "Particle: electron"; - case Particle::Type::positron: - return "Particle: positron"; - } - UNREACHABLE(); + const auto& p = particles_.at(bin); + return fmt::format("Particle: {}", particle_type_to_str(p)); } } // namespace openmc