More use of fmt::print and fmt::format

This commit is contained in:
Paul Romano 2020-01-20 13:43:02 -06:00
parent 7383a52f1f
commit 2a230cd739
29 changed files with 219 additions and 302 deletions

View file

@ -11,6 +11,7 @@
#include "openmc/position.h"
#include "openmc/constants.h"
#include "openmc/cell.h"
#include "openmc/error.h"
#include "openmc/geometry.h"
#include "openmc/particle.h"
#include "openmc/xml_interface.h"
@ -154,10 +155,8 @@ T PlotBase::get_map() const {
in_i = 1;
out_i = 2;
break;
#ifdef __GNUC__
default:
__builtin_unreachable();
#endif
UNREACHABLE();
}
// set initial position

View file

@ -7,6 +7,7 @@
#include "pugixml.hpp"
#include "xtensor/xtensor.hpp"
#include <array>
#include <string>
#include <vector>
#include <gsl/gsl>

View file

@ -2,9 +2,9 @@
#include <cmath> // for ceil, pow
#include <limits> // for numeric_limits
#include <sstream>
#include <string>
#include <fmt/format.h>
#ifdef _OPENMP
#include <omp.h>
#endif
@ -130,7 +130,7 @@ void get_run_parameters(pugi::xml_node node_base)
if (n_particles == -1) {
n_particles = std::stoll(get_node_value(node_base, "particles"));
}
// Get maximum number of in flight particles for event-based mode
if (check_for_node(node_base, "max_particles_in_flight")) {
max_particles_in_flight = std::stoll(get_node_value(node_base,
@ -195,13 +195,13 @@ void read_settings_xml()
std::string filename = path_input + "settings.xml";
if (!file_exists(filename)) {
if (run_mode != RunMode::PLOTTING) {
std::stringstream msg;
msg << "Settings XML file '" << filename << "' does not exist! In order "
fatal_error(fmt::format(
"Settings XML file '{}' does not exist! In order "
"to run OpenMC, you first need a set of input files; at a minimum, this "
"includes settings.xml, geometry.xml, and materials.xml. Please consult "
"the user's guide at http://openmc.readthedocs.io for further "
"information.";
fatal_error(msg);
"the user's guide at https://docs.openmc.org for further "
"information.", filename
));
} else {
// The settings.xml file is optional if we just want to make a plot.
return;
@ -494,9 +494,8 @@ void read_settings_xml()
if (check_for_node(root, "entropy_mesh")) {
int temp = std::stoi(get_node_value(root, "entropy_mesh"));
if (model::mesh_map.find(temp) == model::mesh_map.end()) {
std::stringstream msg;
msg << "Mesh " << temp << " specified for Shannon entropy does not exist.";
fatal_error(msg);
fatal_error(fmt::format(
"Mesh {} specified for Shannon entropy does not exist.", temp));
}
index_entropy_mesh = model::mesh_map.at(temp);
@ -544,10 +543,8 @@ void read_settings_xml()
if (check_for_node(root, "ufs_mesh")) {
auto temp = std::stoi(get_node_value(root, "ufs_mesh"));
if (model::mesh_map.find(temp) == model::mesh_map.end()) {
std::stringstream msg;
msg << "Mesh " << temp << " specified for uniform fission site method "
"does not exist.";
fatal_error(msg);
fatal_error(fmt::format("Mesh {} specified for uniform fission site "
"method does not exist.", temp));
}
i_ufs_mesh = model::mesh_map.at(temp);
@ -792,7 +789,7 @@ void read_settings_xml()
if (check_for_node(root, "delayed_photon_scaling")) {
delayed_photon_scaling = get_node_value_bool(root, "delayed_photon_scaling");
}
// Check whether to use event-based parallelism
if (check_for_node(root, "event_based")) {
event_based = get_node_value_bool(root, "event_based");

View file

@ -1,8 +1,8 @@
#include "openmc/source.h"
#include <algorithm> // for move
#include <sstream> // for stringstream
#include <fmt/format.h>
#include "xtensor/xadapt.hpp"
#include "openmc/bank.h"
@ -68,9 +68,8 @@ SourceDistribution::SourceDistribution(pugi::xml_node node)
// Check if source file exists
if (!file_exists(settings::path_source)) {
std::stringstream msg;
msg << "Source file '" << settings::path_source << "' does not exist.";
fatal_error(msg);
fatal_error(fmt::format("Source file '{}' does not exist.",
settings::path_source));
}
} else {
@ -97,9 +96,8 @@ SourceDistribution::SourceDistribution(pugi::xml_node node)
} else if (type == "point") {
space_ = UPtrSpace{new SpatialPoint(node_space)};
} else {
std::stringstream msg;
msg << "Invalid spatial distribution for external source: " << type;
fatal_error(msg);
fatal_error(fmt::format(
"Invalid spatial distribution for external source: {}", type));
}
} else {
@ -123,9 +121,8 @@ SourceDistribution::SourceDistribution(pugi::xml_node node)
} else if (type == "mu-phi") {
angle_ = UPtrAngle{new PolarAzimuthal(node_angle)};
} else {
std::stringstream msg;
msg << "Invalid angular distribution for external source: " << type;
fatal_error(msg);
fatal_error(fmt::format(
"Invalid angular distribution for external source: {}", type));
}
} else {
@ -244,9 +241,8 @@ void initialize_source()
// Read the source from a binary file instead of sampling from some
// assumed source distribution
std::stringstream msg;
msg << "Reading source file from " << settings::path_source << "...";
write_message(msg, 6);
write_message(fmt::format("Reading source file from {}...",
settings::path_source), 6);
// Open the binary file
hid_t file_id = file_open(settings::path_source, 'r', true);

View file

@ -2,10 +2,10 @@
#include <algorithm>
#include <cstdint> // for int64_t
#include <iomanip> // for setfill, setw
#include <string>
#include <vector>
#include <fmt/format.h>
#include "xtensor/xbuilder.hpp" // for empty_like
#include "xtensor/xview.hpp"
@ -41,10 +41,8 @@ openmc_statepoint_write(const char* filename, bool* write_source)
int w = std::to_string(settings::n_max_batches).size();
// Set filename for state point
std::stringstream ss;
ss << settings::path_output << "statepoint." << std::setfill('0')
<< std::setw(w) << simulation::current_batch << ".h5";
filename_ = ss.str();
filename_ = fmt::format("{0}statepoint.{1:0{2}}.h5",
settings::path_output, simulation::current_batch, w);
}
// Determine whether or not to write the source bank
@ -523,10 +521,8 @@ write_source_point(const char* filename)
// Determine width for zero padding
int w = std::to_string(settings::n_max_batches).size();
std::stringstream s;
s << settings::path_output << "source." << std::setfill('0')
<< std::setw(w) << simulation::current_batch << ".h5";
filename_ = s.str();
filename_ = fmt::format("{0}source.{1:0{2}}.h5",
settings::path_output, simulation::current_batch, w);
}
hid_t file_id;

View file

@ -2,9 +2,10 @@
#include <array>
#include <cmath>
#include <sstream>
#include <utility>
#include <fmt/format.h>
#include "openmc/error.h"
#include "openmc/dagmc.h"
#include "openmc/hdf5_interface.h"
@ -35,10 +36,8 @@ void read_coeffs(pugi::xml_node surf_node, int surf_id, double &c1)
std::string coeffs = get_node_value(surf_node, "coeffs");
int n_words = word_count(coeffs);
if (n_words != 1) {
std::stringstream err_msg;
err_msg << "Surface " << surf_id << " expects 1 coeff but was given "
<< n_words;
fatal_error(err_msg);
fatal_error(fmt::format("Surface {} expects 1 coeff but was given {}",
surf_id, n_words));
}
// Parse the coefficients.
@ -55,10 +54,8 @@ void read_coeffs(pugi::xml_node surf_node, int surf_id, double &c1, double &c2,
std::string coeffs = get_node_value(surf_node, "coeffs");
int n_words = word_count(coeffs);
if (n_words != 3) {
std::stringstream err_msg;
err_msg << "Surface " << surf_id << " expects 3 coeffs but was given "
<< n_words;
fatal_error(err_msg);
fatal_error(fmt::format("Surface {} expects 3 coeffs but was given {}",
surf_id, n_words));
}
// Parse the coefficients.
@ -75,10 +72,8 @@ void read_coeffs(pugi::xml_node surf_node, int surf_id, double &c1, double &c2,
std::string coeffs = get_node_value(surf_node, "coeffs");
int n_words = word_count(coeffs);
if (n_words != 4) {
std::stringstream err_msg;
err_msg << "Surface " << surf_id << " expects 4 coeffs but was given "
<< n_words;
fatal_error(err_msg);
fatal_error(fmt::format("Surface {} expects 4 coeffs but was given ",
surf_id, n_words));
}
// Parse the coefficients.
@ -96,10 +91,8 @@ void read_coeffs(pugi::xml_node surf_node, int surf_id, double &c1, double &c2,
std::string coeffs = get_node_value(surf_node, "coeffs");
int n_words = word_count(coeffs);
if (n_words != 10) {
std::stringstream err_msg;
err_msg << "Surface " << surf_id << " expects 10 coeffs but was given "
<< n_words;
fatal_error(err_msg);
fatal_error(fmt::format("Surface {} expects 10 coeffs but was given {}",
surf_id, n_words));
}
// Parse the coefficients.
@ -145,10 +138,8 @@ Surface::Surface(pugi::xml_node surf_node)
} else if (surf_bc == "periodic") {
bc_ = BoundaryType::PERIODIC;
} else {
std::stringstream err_msg;
err_msg << "Unknown boundary condition \"" << surf_bc
<< "\" specified on surface " << id_;
fatal_error(err_msg);
fatal_error(fmt::format("Unknown boundary condition \"{}\" specified "
"on surface {}", surf_bc, id_));
}
} else {
@ -1170,9 +1161,7 @@ void read_surfaces(pugi::xml_node node)
model::surfaces.push_back(std::make_unique<SurfaceQuadric>(surf_node));
} else {
std::stringstream err_msg;
err_msg << "Invalid surface type, \"" << surf_type << "\"";
fatal_error(err_msg);
fatal_error(fmt::format("Invalid surface type, \"{}\"", surf_type));
}
}
}
@ -1184,9 +1173,8 @@ void read_surfaces(pugi::xml_node node)
if (in_map == model::surface_map.end()) {
model::surface_map[id] = i_surf;
} else {
std::stringstream err_msg;
err_msg << "Two or more surfaces use the same unique ID: " << id;
fatal_error(err_msg);
fatal_error(fmt::format(
"Two or more surfaces use the same unique ID: {}", id));
}
}
@ -1202,11 +1190,9 @@ void read_surfaces(pugi::xml_node node)
// Make sure this surface inherits from PeriodicSurface.
if (!surf) {
std::stringstream err_msg;
err_msg << "Periodic boundary condition not supported for surface "
<< surf_base->id_
<< ". Periodic BCs are only supported for planar surfaces.";
fatal_error(err_msg);
fatal_error(fmt::format(
"Periodic boundary condition not supported for surface {}. Periodic "
"BCs are only supported for planar surfaces.", surf_base->id_));
}
// See if this surface makes part of the global bounding box.
@ -1278,10 +1264,8 @@ void read_surfaces(pugi::xml_node node)
// This is a SurfacePlane. We won't try to find it's partner if the
// user didn't specify one.
if (surf->i_periodic_ == C_NONE) {
std::stringstream err_msg;
err_msg << "No matching periodic surface specified for periodic "
"boundary condition on surface " << surf->id_;
fatal_error(err_msg);
fatal_error(fmt::format("No matching periodic surface specified for "
"periodic boundary condition on surface {}", surf->id_));
} else {
// Convert the surface id to an index.
surf->i_periodic_ = model::surface_map[surf->i_periodic_];
@ -1290,10 +1274,8 @@ void read_surfaces(pugi::xml_node node)
// Make sure the opposite surface is also periodic.
if (model::surfaces[surf->i_periodic_]->bc_ != Surface::BoundaryType::PERIODIC) {
std::stringstream err_msg;
err_msg << "Could not find matching surface for periodic boundary "
"condition on surface " << surf->id_;
fatal_error(err_msg);
fatal_error(fmt::format("Could not find matching surface for periodic "
"boundary condition on surface {}", surf->id_));
}
}
}

View file

@ -7,7 +7,7 @@
#include "openmc/tallies/tally.h"
#include "openmc/xml_interface.h"
#include <sstream>
#include <fmt/format.h>
template class std::vector<openmc::TallyDerivative>;
@ -55,20 +55,16 @@ TallyDerivative::TallyDerivative(pugi::xml_node node)
}
}
if (!found) {
std::stringstream out;
out << "Could not find the nuclide \"" << nuclide_name
<< "\" specified in derivative " << id << " in any material.";
fatal_error(out);
fatal_error(fmt::format("Could not find the nuclide \"{}\" specified in "
"derivative {} in any material.", nuclide_name, id));
}
} else if (variable_str == "temperature") {
variable = DerivativeVariable::TEMPERATURE;
} else {
std::stringstream out;
out << "Unrecognized variable \"" << variable_str
<< "\" on derivative " << id;
fatal_error(out);
fatal_error(fmt::format("Unrecognized variable \"{}\" on derivative {}",
variable_str, id));
}
diff_material = std::stoi(get_node_value(node, "material"));
@ -568,7 +564,7 @@ score_track_derivative(Particle* p, double distance)
// A void material cannot be perturbed so it will not affect flux derivatives.
if (p->material_ == MATERIAL_VOID) return;
const Material& material {*model::materials[p->material_]};
for (auto idx = 0; idx < model::tally_derivs.size(); idx++) {
const auto& deriv = model::tally_derivs[idx];
auto& flux_deriv = p->flux_derivs_[idx];
@ -641,11 +637,9 @@ void score_collision_derivative(Particle* p)
if (material.nuclide_[i] == deriv.diff_nuclide) break;
// Make sure we found the nuclide.
if (material.nuclide_[i] != deriv.diff_nuclide) {
std::stringstream err_msg;
err_msg << "Could not find nuclide "
<< data::nuclides[deriv.diff_nuclide]->name_ << " in material "
<< material.id_ << " for tally derivative " << deriv.id;
fatal_error(err_msg);
fatal_error(fmt::format(
"Could not find nuclide {} in material {} for tally derivative {}",
data::nuclides[deriv.diff_nuclide]->name_, material.id_, deriv.id));
}
// phi is proportional to Sigma_s
// (1 / phi) * (d_phi / d_N) = (d_Sigma_s / d_N) / Sigma_s

View file

@ -1,7 +1,8 @@
#include "openmc/tallies/filter_azimuthal.h"
#include <cmath>
#include <sstream>
#include <fmt/format.h>
#include "openmc/constants.h"
#include "openmc/error.h"
@ -77,9 +78,7 @@ AzimuthalFilter::to_statepoint(hid_t filter_group) const
std::string
AzimuthalFilter::text_label(int bin) const
{
std::stringstream out;
out << "Azimuthal Angle [" << bins_[bin] << ", " << bins_[bin+1] << ")";
return out.str();
return fmt::format("Azimuthal Angle [{}, {})", bins_[bin], bins_[bin+1]);
}
} // namespace openmc

View file

@ -1,6 +1,6 @@
#include "openmc/tallies/filter_cell.h"
#include <sstream>
#include <fmt/format.h>
#include "openmc/capi.h"
#include "openmc/cell.h"
@ -17,10 +17,8 @@ CellFilter::from_xml(pugi::xml_node node)
for (auto& c : cells) {
auto search = model::cell_map.find(c);
if (search == model::cell_map.end()) {
std::stringstream err_msg;
err_msg << "Could not find cell " << c
<< " specified on tally filter.";
throw std::runtime_error{err_msg.str()};
throw std::runtime_error{fmt::format(
"Could not find cell {} specified on tally filter.", c)};
}
c = search->second;
}
@ -72,7 +70,7 @@ CellFilter::to_statepoint(hid_t filter_group) const
std::string
CellFilter::text_label(int bin) const
{
return "Cell " + std::to_string(model::cells[cells_[bin]]->id_);
return fmt::format("Cell {}", model::cells[cells_[bin]]->id_);
}
//==============================================================================

View file

@ -1,8 +1,9 @@
#include "openmc/tallies/filter_cell_instance.h"
#include <sstream>
#include <string>
#include <fmt/format.h>
#include "openmc/capi.h"
#include "openmc/cell.h"
#include "openmc/error.h"
@ -29,10 +30,8 @@ CellInstanceFilter::from_xml(pugi::xml_node node)
gsl::index instance = cells[2*i + 1];
auto search = model::cell_map.find(cell_id);
if (search == model::cell_map.end()) {
std::stringstream err_msg;
err_msg << "Could not find cell " << cell_id
<< " specified on tally filter.";
throw std::runtime_error{err_msg.str()};
throw std::runtime_error{fmt::format(
"Could not find cell {} specified on tally filter.", cell_id)};
}
gsl::index index = search->second;
instances.push_back({index, instance});
@ -55,9 +54,9 @@ CellInstanceFilter::set_cell_instances(gsl::span<CellInstance> instances)
Expects(x.index_cell < model::cells.size());
const auto& c {model::cells[x.index_cell]};
if (c->type_ != Fill::MATERIAL) {
throw std::invalid_argument{"Cell " + std::to_string(c->id_) + " is not "
"filled with a material. Only material cells can be used in a cell "
"instance filter."};
throw std::invalid_argument{fmt::format(
"Cell {} is not filled with a material. Only material cells can be "
"used in a cell instance filter.", c->id_)};
}
cell_instances_.push_back(x);
map_[x] = cell_instances_.size() - 1;

View file

@ -1,5 +1,7 @@
#include "openmc/tallies/filter_distribcell.h"
#include <fmt/format.h>
#include "openmc/cell.h"
#include "openmc/error.h"
#include "openmc/geometry_aux.h" // For distribcell_path
@ -19,10 +21,8 @@ DistribcellFilter::from_xml(pugi::xml_node node)
// Find index in global cells vector corresponding to cell ID
auto search = model::cell_map.find(cells[0]);
if (search == model::cell_map.end()) {
std::stringstream err_msg;
err_msg << "Could not find cell " << cell_
<< " specified on tally filter.";
throw std::runtime_error{err_msg.str()};
throw std::runtime_error{fmt::format(
"Could not find cell {} specified on tally filter.", cell_)};
}
this->set_cell(search->second);

View file

@ -1,5 +1,7 @@
#include "openmc/tallies/filter_energy.h"
#include <fmt/format.h>
#include "openmc/capi.h"
#include "openmc/constants.h" // For F90_NONE
#include "openmc/mgxs_interface.h"
@ -90,9 +92,7 @@ EnergyFilter::to_statepoint(hid_t filter_group) const
std::string
EnergyFilter::text_label(int bin) const
{
std::stringstream out;
out << "Incoming Energy [" << bins_[bin] << ", " << bins_[bin+1] << ")";
return out.str();
return fmt::format("Incoming Energy [{}, {})", bins_[bin], bins_[bin+1]);
}
//==============================================================================
@ -119,9 +119,7 @@ EnergyoutFilter::get_all_bins(const Particle* p, TallyEstimator estimator,
std::string
EnergyoutFilter::text_label(int bin) const
{
std::stringstream out;
out << "Outgoing Energy [" << bins_[bin] << ", " << bins_[bin+1] << ")";
return out.str();
return fmt::format("Outgoing Energy [{}, {})", bins_[bin], bins_[bin+1]);
}
//==============================================================================

View file

@ -1,8 +1,6 @@
#include "openmc/tallies/filter_energyfunc.h"
#include <iomanip> // for setprecision
#include <ios> // for scientific
#include <sstream>
#include <fmt/format.h>
#include "openmc/error.h"
#include "openmc/search.h"
@ -82,12 +80,9 @@ EnergyFunctionFilter::to_statepoint(hid_t filter_group) const
std::string
EnergyFunctionFilter::text_label(int bin) const
{
std::stringstream out;
out << std::scientific << std::setprecision(1)
<< "Energy Function f"
<< "([ " << energy_.front() << ", ..., " << energy_.back() << "]) = "
<< "[" << y_.front() << ", ..., " << y_.back() << "]";
return out.str();
return fmt::format(
"Energy Function f([{:.1e}, ..., {:.1e}]) = [{:.1e}, ..., {:.1e}]",
energy_.front(), energy_.back(), y_.front(), y_.back());
}
//==============================================================================

View file

@ -1,6 +1,6 @@
#include "openmc/tallies/filter_material.h"
#include <sstream>
#include <fmt/format.h>
#include "openmc/capi.h"
#include "openmc/material.h"
@ -16,10 +16,8 @@ MaterialFilter::from_xml(pugi::xml_node node)
for (auto& m : mats) {
auto search = model::material_map.find(m);
if (search == model::material_map.end()) {
std::stringstream err_msg;
err_msg << "Could not find material " << m
<< " specified on tally filter.";
throw std::runtime_error{err_msg.str()};
throw std::runtime_error{fmt::format(
"Could not find material {} specified on tally filter.", m)};
}
m = search->second;
}
@ -69,7 +67,7 @@ MaterialFilter::to_statepoint(hid_t filter_group) const
std::string
MaterialFilter::text_label(int bin) const
{
return "Material " + std::to_string(model::materials[materials_[bin]]->id_);
return fmt::format("Material {}", model::materials[materials_[bin]]->id_);
}
//==============================================================================

View file

@ -1,6 +1,6 @@
#include "openmc/tallies/filter_mesh.h"
#include <sstream>
#include <fmt/format.h>
#include "openmc/capi.h"
#include "openmc/constants.h"
@ -24,9 +24,8 @@ MeshFilter::from_xml(pugi::xml_node node)
if (search != model::mesh_map.end()) {
set_mesh(search->second);
} else{
std::stringstream err_msg;
err_msg << "Could not find mesh " << id << " specified on tally filter.";
fatal_error(err_msg);
fatal_error(fmt::format(
"Could not find mesh {} specified on tally filter.", id));
}
}
@ -61,13 +60,13 @@ MeshFilter::text_label(int bin) const
std::vector<int> ijk(n_dim);
mesh.get_indices_from_bin(bin, ijk.data());
std::stringstream out;
out << "Mesh Index (" << ijk[0];
if (n_dim > 1) out << ", " << ijk[1];
if (n_dim > 2) out << ", " << ijk[2];
out << ")";
return out.str();
if (n_dim > 2) {
return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
} else if (n_dim > 1) {
return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
} else {
return fmt::format("Mesh Index ({})", ijk[0]) ;
}
}
void

View file

@ -1,6 +1,6 @@
#include "openmc/tallies/filter_mu.h"
#include <sstream>
#include <fmt/format.h>
#include "openmc/error.h"
#include "openmc/search.h"
@ -69,9 +69,7 @@ MuFilter::to_statepoint(hid_t filter_group) const
std::string
MuFilter::text_label(int bin) const
{
std::stringstream out;
out << "Change-in-Angle [" << bins_[bin] << ", " << bins_[bin+1] << ")";
return out.str();
return fmt::format("Change-in-Angle [{}, {})", bins_[bin], bins_[bin+1]);
}
} // namespace openmc

View file

@ -1,6 +1,6 @@
#include "openmc/tallies/filter_polar.h"
#include <sstream>
#include <fmt/format.h>
#include "openmc/constants.h"
#include "openmc/error.h"
@ -77,9 +77,7 @@ PolarFilter::to_statepoint(hid_t filter_group) const
std::string
PolarFilter::text_label(int bin) const
{
std::stringstream out;
out << "Polar Angle [" << bins_[bin] << ", " << bins_[bin+1] << ")";
return out.str();
return fmt::format("Polar Angle [{}, {})", bins_[bin], bins_[bin+1]);
}
} // namespace openmc

View file

@ -2,6 +2,9 @@
#include <utility> // For pair
#include <fmt/format.h>
#include <gsl/gsl>
#include "openmc/capi.h"
#include "openmc/error.h"
#include "openmc/math_functions.h"
@ -36,10 +39,8 @@ SphericalHarmonicsFilter::set_cosine(gsl::cstring_span cosine)
} else if (cosine == "particle") {
cosine_ = SphericalHarmonicsCosine::particle;
} else {
std::stringstream err_msg;
err_msg << "Unrecognized cosine type, \"" << cosine
<< "\" in spherical harmonics filter";
throw std::invalid_argument{err_msg.str()};
throw std::invalid_argument{fmt::format("Unrecognized cosine type, \"{}\" "
"in spherical harmonics filter", gsl::to_string(cosine))};
}
}
@ -88,15 +89,14 @@ SphericalHarmonicsFilter::to_statepoint(hid_t filter_group) const
std::string
SphericalHarmonicsFilter::text_label(int bin) const
{
std::stringstream out;
Expects(bin >= 0 && bin < n_bins_);
for (int n = 0; n < order_ + 1; n++) {
if (bin < (n + 1) * (n + 1)) {
int m = (bin - n*n) - n;
out << "Spherical harmonic expansion, Y" << n << "," << m;
break;
return fmt::format("Spherical harmonic expansion, Y{},{}", n, m);
}
}
return out.str();
UNREACHABLE();
}
//==============================================================================

View file

@ -2,6 +2,8 @@
#include <utility> // For pair
#include <fmt/format.h>
#include "openmc/capi.h"
#include "openmc/error.h"
#include "openmc/math_functions.h"
@ -107,17 +109,13 @@ SpatialLegendreFilter::to_statepoint(hid_t filter_group) const
std::string
SpatialLegendreFilter::text_label(int bin) const
{
std::stringstream out;
out << "Legendre expansion, ";
if (axis_ == LegendreAxis::x) {
out << "x";
return fmt::format("Legendre expansion, x axis, P{}", bin);
} else if (axis_ == LegendreAxis::y) {
out << "y";
return fmt::format("Legendre expansion, y axis, P{}", bin);
} else {
out << "z";
return fmt::format("Legendre expansion, z axis, P{}", bin);
}
out << " axis, P" << std::to_string(bin);
return out.str();
}
//==============================================================================

View file

@ -1,6 +1,6 @@
#include "openmc/tallies/filter_surface.h"
#include <sstream>
#include <fmt/format.h>
#include "openmc/error.h"
#include "openmc/surface.h"
@ -17,10 +17,8 @@ SurfaceFilter::from_xml(pugi::xml_node node)
for (auto& s : surfaces) {
auto search = model::surface_map.find(s);
if (search == model::surface_map.end()) {
std::stringstream err_msg;
err_msg << "Could not find surface " << s
<< " specified on tally filter.";
throw std::runtime_error{err_msg.str()};
throw std::runtime_error{fmt::format(
"Could not find surface {} specified on tally filter.", s)};
}
s = search->second;
@ -75,7 +73,7 @@ SurfaceFilter::to_statepoint(hid_t filter_group) const
std::string
SurfaceFilter::text_label(int bin) const
{
return "Surface " + std::to_string(model::surfaces[surfaces_[bin]]->id_);
return fmt::format("Surface {}", model::surfaces[surfaces_[bin]]->id_);
}
} // namespace openmc

View file

@ -1,6 +1,6 @@
#include "openmc/tallies/filter_universe.h"
#include <sstream>
#include <fmt/format.h>
#include "openmc/cell.h"
#include "openmc/error.h"
@ -16,10 +16,8 @@ UniverseFilter::from_xml(pugi::xml_node node)
for (auto& u : universes) {
auto search = model::universe_map.find(u);
if (search == model::universe_map.end()) {
std::stringstream err_msg;
err_msg << "Could not find universe " << u
<< " specified on tally filter.";
throw std::runtime_error{err_msg.str()};
throw std::runtime_error{fmt::format(
"Could not find universe {} specified on tally filter.", u)};
}
u = search->second;
}
@ -71,7 +69,7 @@ UniverseFilter::to_statepoint(hid_t filter_group) const
std::string
UniverseFilter::text_label(int bin) const
{
return "Universe " + std::to_string(model::universes[universes_[bin]]->id_);
return fmt::format("Universe {}", model::universes[universes_[bin]]->id_);
}
} // namespace openmc

View file

@ -4,6 +4,9 @@
#include <sstream>
#include <utility> // For pair
#include <fmt/format.h>
#include <gsl/gsl>
#include "openmc/capi.h"
#include "openmc/error.h"
#include "openmc/math_functions.h"
@ -58,17 +61,16 @@ ZernikeFilter::to_statepoint(hid_t filter_group) const
std::string
ZernikeFilter::text_label(int bin) const
{
std::stringstream out;
Expects(bin >= 0 && bin < n_bins_);
for (int n = 0; n < order_+1; n++) {
int last = (n + 1) * (n + 2) / 2;
if (bin < last) {
int first = last - (n + 1);
int m = -n + (bin - first) * 2;
out << "Zernike expansion, Z" << n << "," << m;
break;
return fmt::format("Zernike expansion, Z{},{}", n, m);
}
}
return out.str();
UNREACHABLE();
}
void

View file

@ -28,6 +28,7 @@
#include "openmc/tallies/filter_surface.h"
#include "openmc/xml_interface.h"
#include <fmt/format.h>
#include "xtensor/xadapt.hpp"
#include "xtensor/xbuilder.hpp" // for empty_like
#include "xtensor/xview.hpp"
@ -35,7 +36,6 @@
#include <algorithm> // for max
#include <array>
#include <cstddef> // for size_t
#include <sstream>
#include <string>
namespace openmc {
@ -287,8 +287,8 @@ Tally::Tally(pugi::xml_node node)
// Determine if filter ID is valid
auto it = model::filter_map.find(filter_id);
if (it == model::filter_map.end()) {
throw std::runtime_error{"Could not find filter " + std::to_string(filter_id)
+ " specified on tally " + std::to_string(id_)};
throw std::runtime_error{fmt::format(
"Could not find filter {} specified on tally {}", filter_id, id_)};
}
// Store the index of the filter
@ -334,8 +334,7 @@ Tally::Tally(pugi::xml_node node)
this->set_scores(node);
if (!check_for_node(node, "scores")) {
fatal_error("No scores specified on tally " + std::to_string(id_)
+ ".");
fatal_error(fmt::format("No scores specified on tally {}.", id_));
}
// Check if tally is compatible with particle type
@ -371,9 +370,9 @@ Tally::Tally(pugi::xml_node node)
auto pf = dynamic_cast<ParticleFilter*>(f);
for (auto p : pf->particles()) {
if (p != Particle::Type::neutron) {
warning("Particle filter other than NEUTRON used with photon "
"transport turned off. All tallies for particle type " +
std::to_string(static_cast<int>(p)) + " will have no scores");
warning(fmt::format("Particle filter other than NEUTRON used with "
"photon transport turned off. All tallies for particle type {}"
" will have no scores", static_cast<int>(p)));
}
}
}
@ -386,8 +385,8 @@ Tally::Tally(pugi::xml_node node)
// Find the derivative with the given id, and store it's index.
auto it = model::tally_deriv_map.find(deriv_id);
if (it == model::tally_deriv_map.end()) {
fatal_error("Could not find derivative " + std::to_string(deriv_id)
+ " specified on tally " + std::to_string(id_));
fatal_error(fmt::format(
"Could not find derivative {} specified on tally {}", deriv_id, id_));
}
deriv_ = it->second;
@ -403,11 +402,10 @@ Tally::Tally(pugi::xml_node node)
|| deriv.variable == DerivativeVariable::TEMPERATURE) {
for (int i_nuc : nuclides_) {
if (has_energyout && i_nuc == -1) {
fatal_error("Error on tally " + std::to_string(id_)
+ ": Cannot use a 'nuclide_density' or 'temperature' "
"derivative on a tally with an outgoing energy filter and "
"'total' nuclide rate. Instead, tally each nuclide in the "
"material individually.");
fatal_error(fmt::format("Error on tally {}: Cannot use a "
"'nuclide_density' or 'temperature' derivative on a tally with an "
"outgoing energy filter and 'total' nuclide rate. Instead, tally "
"each nuclide in the material individually.", id_));
// Note that diff tallies with these characteristics would work
// correctly if no tally events occur in the perturbed material
// (e.g. pertrubing moderator but only tallying fuel), but this
@ -432,11 +430,11 @@ Tally::Tally(pugi::xml_node node)
estimator_ = TallyEstimator::ANALOG;
} else if (est == "tracklength" || est == "track-length"
|| est == "pathlength" || est == "path-length") {
// If the estimator was set to an analog/collision estimator, this means
// the tally needs post-collision information
// If the estimator was set to an analog estimator, this means the
// tally needs post-collision information
if (estimator_ == TallyEstimator::ANALOG || estimator_ == TallyEstimator::COLLISION) {
throw std::runtime_error{"Cannot use track-length estimator for tally "
+ std::to_string(id_)};
throw std::runtime_error{fmt::format("Cannot use track-length "
"estimator for tally {}", id_)};
}
// Set estimator to track-length estimator
@ -446,16 +444,16 @@ Tally::Tally(pugi::xml_node node)
// If the estimator was set to an analog estimator, this means the
// tally needs post-collision information
if (estimator_ == TallyEstimator::ANALOG) {
throw std::runtime_error{"Cannot use collision estimator for tally " +
std::to_string(id_)};
throw std::runtime_error{fmt::format("Cannot use collision estimator "
"for tally ", id_)};
}
// Set estimator to collision estimator
estimator_ = TallyEstimator::COLLISION;
} else {
throw std::runtime_error{"Invalid estimator '" + est + "' on tally " +
std::to_string(id_)};
throw std::runtime_error{fmt::format(
"Invalid estimator '{}' on tally {}", est, id_)};
}
}
}
@ -485,7 +483,7 @@ Tally::set_id(int32_t id)
// Make sure no other tally has the same ID
if (model::tally_map.find(id) != model::tally_map.end()) {
throw std::runtime_error{"Two tallies have the same ID: " + std::to_string(id)};
throw std::runtime_error{fmt::format("Two tallies have the same ID: {}", id)};
}
// If no ID specified, auto-assign next ID in sequence
@ -542,7 +540,7 @@ void
Tally::set_scores(pugi::xml_node node)
{
if (!check_for_node(node, "scores"))
fatal_error("No scores specified on tally " + std::to_string(id_));
fatal_error(fmt::format("No scores specified on tally {}", id_));
auto scores = get_node_array<std::string>(node, "scores");
set_scores(scores);
@ -659,8 +657,9 @@ Tally::set_scores(const std::vector<std::string>& scores)
for (auto it1 = scores_.begin(); it1 != scores_.end(); ++it1) {
for (auto it2 = it1 + 1; it2 != scores_.end(); ++it2) {
if (*it1 == *it2)
fatal_error("Duplicate score of type \"" + reaction_name(*it1)
+ "\" found in tally " + std::to_string(id_));
fatal_error(fmt::format(
"Duplicate score of type \"{}\" found in tally {}",
reaction_name(*it1), id_));
}
}
@ -720,9 +719,8 @@ Tally::set_nuclides(const std::vector<std::string>& nuclides)
} else {
auto search = data::nuclide_map.find(nuc);
if (search == data::nuclide_map.end())
fatal_error("Could not find the nuclide " + nuc
+ " specified in tally " + std::to_string(id_)
+ " in any material");
fatal_error(fmt::format("Could not find the nuclide {} specified in "
"tally {} in any material", nuc, id_));
nuclides_.push_back(search->second);
}
}
@ -743,15 +741,12 @@ Tally::init_triggers(pugi::xml_node node)
} else if (type_str == "rel_err") {
metric = TriggerMetric::relative_error;
} else {
std::stringstream msg;
msg << "Unknown trigger type \"" << type_str << "\" in tally " << id_;
fatal_error(msg);
fatal_error(fmt::format("Unknown trigger type \"{}\" in tally {}",
type_str, id_));
}
} else {
std::stringstream msg;
msg << "Must specify trigger type for tally " << id_
<< " in tally XML file";
fatal_error(msg);
fatal_error(fmt::format(
"Must specify trigger type for tally {} in tally XML file", id_));
}
// Read the trigger threshold.
@ -759,10 +754,8 @@ Tally::init_triggers(pugi::xml_node node)
if (check_for_node(trigger_node, "threshold")) {
threshold = std::stod(get_node_value(trigger_node, "threshold"));
} else {
std::stringstream msg;
msg << "Must specify trigger threshold for tally " << id_
<< " in tally XML file";
fatal_error(msg);
fatal_error(fmt::format(
"Must specify trigger threshold for tally {} in tally XML file", id_));
}
// Read the trigger scores.
@ -786,10 +779,8 @@ Tally::init_triggers(pugi::xml_node node)
if (reaction_name(this->scores_[i_score]) == score_str) break;
}
if (i_score == this->scores_.size()) {
std::stringstream msg;
msg << "Could not find the score \"" << score_str << "\" in tally "
<< id_ << " but it was listed in a trigger on that tally";
fatal_error(msg);
fatal_error(fmt::format("Could not find the score \"{}\" in tally "
"{} but it was listed in a trigger on that tally", score_str, id_));
}
triggers_.push_back({metric, threshold, i_score});
}
@ -1078,7 +1069,7 @@ openmc_get_tally_index(int32_t id, int32_t* index)
{
auto it = model::tally_map.find(id);
if (it == model::tally_map.end()) {
set_errmsg("No tally exists with ID=" + std::to_string(id) + ".");
set_errmsg(fmt::format("No tally exists with ID={}.", id));
return OPENMC_E_INVALID_ID;
}
@ -1182,9 +1173,7 @@ openmc_tally_set_type(int32_t index, const char* type)
} else if (strcmp(type, "surface") == 0) {
model::tallies[index]->type_ = TallyType::SURFACE;
} else {
std::stringstream errmsg;
errmsg << "Unknown tally type: " << type;
set_errmsg(errmsg);
set_errmsg(fmt::format("Unknown tally type: {}", type));
return OPENMC_E_INVALID_ARGUMENT;
}

View file

@ -1,9 +1,10 @@
#include "openmc/tallies/trigger.h"
#include <cmath>
#include <sstream>
#include <utility> // for std::pair
#include <fmt/format.h>
#include "openmc/capi.h"
#include "openmc/constants.h"
#include "openmc/error.h"
@ -170,13 +171,14 @@ check_triggers()
// At least one trigger is unsatisfied. Let the user know which one.
simulation::satisfy_triggers = false;
std::stringstream msg;
msg << "Triggers unsatisfied, max unc./thresh. is ";
std::string msg;
if (keff_ratio >= tally_ratio) {
msg << keff_ratio << " for eigenvalue";
msg = fmt::format("Triggers unsatisfied, max unc./thresh. is {} for "
"eigenvalue", keff_ratio);
} else {
msg << tally_ratio << " for " << reaction_name(score) << " in tally "
<< tally_id;
msg = fmt::format(
"Triggers unsatisfied, max unc./thresh. is {} for {} in tally {}",
tally_ratio, reaction_name(score), tally_id);
}
write_message(msg, 7);
@ -189,10 +191,10 @@ check_triggers()
auto n_pred_batches = static_cast<int>(n_active * max_ratio * max_ratio)
+ settings::n_inactive + 1;
std::stringstream msg;
msg << "The estimated number of batches is " << n_pred_batches;
std::string msg = fmt::format("The estimated number of batches is {}",
n_pred_batches);
if (n_pred_batches > settings::n_max_batches) {
msg << " --- greater than max batches";
msg.append(" --- greater than max batches");
warning(msg);
} else {
write_message(msg, 7);

View file

@ -2,8 +2,8 @@
#include <algorithm> // for sort, move, min, max, find
#include <cmath> // for round, sqrt, abs
#include <sstream> // for stringstream
#include <fmt/format.h>
#include "xtensor/xarray.hpp"
#include "xtensor/xbuilder.hpp"
#include "xtensor/xmath.hpp"
@ -88,10 +88,8 @@ ThermalScattering::ThermalScattering(hid_t group, const std::vector<double>& tem
temps_to_read.push_back(std::round(temp_actual));
}
} else {
std::stringstream msg;
msg << "Nuclear data library does not contain cross sections for "
<< name_ << " at or near " << std::round(T) << " K.";
fatal_error(msg);
fatal_error(fmt::format("Nuclear data library does not contain cross "
"sections for {} at or near {} K.", name_, std::round(T)));
}
}
break;
@ -115,10 +113,8 @@ ThermalScattering::ThermalScattering(hid_t group, const std::vector<double>& tem
}
}
if (!found) {
std::stringstream msg;
msg << "Nuclear data library does not contain cross sections for "
<< name_ << " at temperatures that bound " << std::round(T) << " K.";
fatal_error(msg);
fatal_error(fmt::format("Nuclear data library does not contain cross "
"sections for {} at temperatures that bound {} K.", name_, std::round(T)));
}
}
}
@ -132,7 +128,7 @@ ThermalScattering::ThermalScattering(hid_t group, const std::vector<double>& tem
for (auto T : temps_to_read) {
// Get temperature as a string
std::string temp_str = std::to_string(T) + "K";
std::string temp_str = fmt::format("{}K", T);
// Read exact temperature value
double kT;

View file

@ -6,10 +6,10 @@
#include "openmc/settings.h"
#include "openmc/simulation.h"
#include <fmt/format.h>
#include "xtensor/xtensor.hpp"
#include <cstddef> // for size_t
#include <sstream>
#include <string>
#include <vector>
@ -35,9 +35,9 @@ void write_particle_track(Particle& p)
void finalize_particle_track(Particle& p)
{
std::stringstream filename;
filename << settings::path_output << "track_" << simulation::current_batch
<< '_' << simulation::current_gen << '_' << p.id_ << ".h5";
std::string filename = fmt::format("{}track_{}_{}_{}.h5",
settings::path_output, simulation::current_batch, simulation::current_gen,
p.id_);
// Determine number of coordinates for each particle
std::vector<int> n_coords;
@ -47,7 +47,7 @@ void finalize_particle_track(Particle& p)
#pragma omp critical (FinalizeParticleTrack)
{
hid_t file_id = file_open(filename.str().c_str(), 'w');
hid_t file_id = file_open(filename, 'w');
write_attribute(file_id, "filetype", "track");
write_attribute(file_id, "version", VERSION_TRACK);
write_attribute(file_id, "n_particles", p.tracks_.size());
@ -61,7 +61,7 @@ void finalize_particle_track(Particle& p)
data(j, 1) = t[j].y;
data(j, 2) = t[j].z;
}
std::string name = "coordinates_" + std::to_string(i);
std::string name = fmt::format("coordinates_{}", i);
write_dataset(file_id, name.c_str(), data);
}
file_close(file_id);

View file

@ -15,6 +15,7 @@
#include "openmc/timer.h"
#include "openmc/xml_interface.h"
#include <fmt/format.h>
#ifdef _OPENMP
#include <omp.h>
#endif
@ -23,7 +24,6 @@
#include <algorithm> // for copy
#include <cmath> // for pow, sqrt
#include <sstream>
#include <unordered_set>
namespace openmc {
@ -66,9 +66,8 @@ VolumeCalculation::VolumeCalculation(pugi::xml_node node)
threshold_ = std::stod(get_node_value(threshold_node, "threshold"));
if (threshold_ <= 0.0) {
std::stringstream msg;
msg << "Invalid error threshold " << threshold_ << " provided for a volume calculation.";
fatal_error(msg);
fatal_error(fmt::format("Invalid error threshold {} provided for a "
"volume calculation.", threshold_));
}
std::string tmp = get_node_value(threshold_node, "type");
@ -79,9 +78,8 @@ VolumeCalculation::VolumeCalculation(pugi::xml_node node)
} else if ( tmp == "rel_err") {
trigger_type_ = TriggerMetric::relative_error;
} else {
std::stringstream msg;
msg << "Invalid volume calculation trigger type '" << tmp << "' provided.";
fatal_error(msg);
fatal_error(fmt::format(
"Invalid volume calculation trigger type '{}' provided.", tmp));
}
}
@ -394,8 +392,7 @@ void VolumeCalculation::to_hdf5(const std::string& filename,
for (int i = 0; i < domain_ids_.size(); ++i)
{
hid_t group_id = create_group(file_id, "domain_"
+ std::to_string(domain_ids_[i]));
hid_t group_id = create_group(file_id, fmt::format("domain_{}", domain_ids_[i]));
// Write volume for domain
const auto& result {results[i]};
@ -468,7 +465,7 @@ int openmc_calculate_volumes() {
for (int i = 0; i < model::volume_calcs.size(); ++i) {
if (mpi::master) {
write_message("Running volume calculation " + std::to_string(i+1) + "...", 4);
write_message(fmt::format("Running volume calculation {}...", i + 1), 4);
}
// Run volume calculation
@ -487,15 +484,13 @@ int openmc_calculate_volumes() {
// Display domain volumes
for (int j = 0; j < vol_calc.domain_ids_.size(); j++) {
std::stringstream msg;
msg << domain_type << vol_calc.domain_ids_[j] << ": " <<
results[j].volume[0] << " +/- " << results[j].volume[1] << " cm^3";
write_message(msg, 4);
write_message(fmt::format("{}{}: {} +/- {} cm^3", domain_type,
vol_calc.domain_ids_[j], results[j].volume[0], results[j].volume[1]), 4);
}
// Write volumes to HDF5 file
std::string filename = settings::path_output + "volume_"
+ std::to_string(i+1) + ".h5";
std::string filename = fmt::format("{}volume_{}.h5",
settings::path_output, i + 1);
vol_calc.to_hdf5(filename, results);
}
@ -504,8 +499,7 @@ int openmc_calculate_volumes() {
// Show elapsed time
time_volume.stop();
if (mpi::master) {
write_message("Elapsed time: " + std::to_string(time_volume.elapsed())
+ " s", 6);
write_message(fmt::format("Elapsed time: {} s", time_volume.elapsed()), 6);
}
return 0;

View file

@ -6,8 +6,9 @@
#include "openmc/math_functions.h"
#include "openmc/nuclide.h"
#include <fmt/format.h>
#include <cmath>
#include <sstream>
namespace openmc {
@ -201,15 +202,14 @@ void check_wmp_version(hid_t file)
std::array<int, 2> version;
read_attribute(file, "version", version);
if (version[0] != WMP_VERSION[0]) {
std::stringstream msg;
msg << "WMP data format uses version " << version[0] << "." <<
version[1] << " whereas your installation of OpenMC expects version "
<< WMP_VERSION[0] << ".x data.";
fatal_error(msg);
fatal_error(fmt::format(
"WMP data format uses version {}.{} whereas your installation of "
"OpenMC expects version {}.x data.",
version[0], version[1], WMP_VERSION[0]));
}
} else {
fatal_error("WMP data does not indicate a version. Your installation of "
"OpenMC expects version " + std::to_string(WMP_VERSION[0]) + ".x data.");
fatal_error(fmt::format("WMP data does not indicate a version. Your "
"installation of OpenMC expects version {}x data.", WMP_VERSION[0]));
}
}

View file

@ -1,10 +1,9 @@
#include "openmc/xml_interface.h"
#include <algorithm> // for transform
#include <sstream>
#include <fmt/format.h>
#include "openmc/error.h"
#include "openmc/string_utils.h"
namespace openmc {
@ -19,17 +18,13 @@ get_node_value(pugi::xml_node node, const char* name, bool lowercase,
} else if (node.child(name)) {
value_char = node.child_value(name);
} else {
std::stringstream err_msg;
err_msg << "Node \"" << name << "\" is not a member of the \""
<< node.name() << "\" XML node";
fatal_error(err_msg);
fatal_error(fmt::format(
"Node \"{}\" is not a member of the \"{}\" XML node", name, node.name()));
}
std::string value {value_char};
// Convert to lower-case if needed
if (lowercase) {
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
}
if (lowercase) to_lower(value);
// Strip leading/trailing whitespace if needed
if (strip) {
@ -48,10 +43,8 @@ get_node_value_bool(pugi::xml_node node, const char* name)
} else if (node.child(name)) {
return node.child(name).text().as_bool();
} else {
std::stringstream err_msg;
err_msg << "Node \"" << name << "\" is not a member of the \""
<< node.name() << "\" XML node";
fatal_error(err_msg);
fatal_error(fmt::format(
"Node \"{}\" is not a member of the \"{}\" XML node", name, node.name()));
}
return false;
}