Add particle_type_to_str and str_to_particle_type functions

This commit is contained in:
Paul Romano 2020-04-23 14:23:15 -05:00
parent a6c7b54d8a
commit 128faa49b0
5 changed files with 51 additions and 44 deletions

View file

@ -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<Particle::Bank> 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

View file

@ -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

View file

@ -38,7 +38,7 @@ void collision(Particle& p)
++(p.n_collision_);
// Sample reaction for the material the particle is in
switch (static_cast<Particle::Type>(p.type_)) {
switch (p.type_) {
case Particle::Type::neutron:
sample_neutron_reaction(p);
break;

View file

@ -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);

View file

@ -1,5 +1,7 @@
#include "openmc/tallies/filter_particle.h"
#include <fmt/core.h>
#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<Particle::Type> 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<std::string> 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