Convert most uses of iostreams in output.cpp to fmt::print

This commit is contained in:
Paul Romano 2020-01-19 16:18:48 -06:00
parent 570433b917
commit 843c136d36

View file

@ -11,6 +11,7 @@
#include <unordered_map>
#include <utility> // for pair
#include <fmt/format.h>
#ifdef _OPENMP
#include <omp.h>
#endif
@ -44,53 +45,53 @@ namespace openmc {
void title()
{
std::cout <<
" %%%%%%%%%%%%%%%\n" <<
" %%%%%%%%%%%%%%%%%%%%%%%%\n" <<
" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" <<
" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" <<
" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" <<
" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" <<
" %%%%%%%%%%%%%%%%%%%%%%%%\n" <<
" %%%%%%%%%%%%%%%%%%%%%%%%\n" <<
" ############### %%%%%%%%%%%%%%%%%%%%%%%%\n" <<
" ################## %%%%%%%%%%%%%%%%%%%%%%%\n" <<
" ################### %%%%%%%%%%%%%%%%%%%%%%%\n" <<
" #################### %%%%%%%%%%%%%%%%%%%%%%\n" <<
" ##################### %%%%%%%%%%%%%%%%%%%%%\n" <<
" ###################### %%%%%%%%%%%%%%%%%%%%\n" <<
" ####################### %%%%%%%%%%%%%%%%%%\n" <<
" ####################### %%%%%%%%%%%%%%%%%\n" <<
" ###################### %%%%%%%%%%%%%%%%%\n" <<
" #################### %%%%%%%%%%%%%%%%%\n" <<
" ################# %%%%%%%%%%%%%%%%%\n" <<
" ############### %%%%%%%%%%%%%%%%\n" <<
" ############ %%%%%%%%%%%%%%%\n" <<
" ######## %%%%%%%%%%%%%%\n" <<
" %%%%%%%%%%%\n\n";
fmt::print(
" %%%%%%%%%%%%%%%\n"
" %%%%%%%%%%%%%%%%%%%%%%%%\n"
" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
" %%%%%%%%%%%%%%%%%%%%%%%%\n"
" %%%%%%%%%%%%%%%%%%%%%%%%\n"
" ############### %%%%%%%%%%%%%%%%%%%%%%%%\n"
" ################## %%%%%%%%%%%%%%%%%%%%%%%\n"
" ################### %%%%%%%%%%%%%%%%%%%%%%%\n"
" #################### %%%%%%%%%%%%%%%%%%%%%%\n"
" ##################### %%%%%%%%%%%%%%%%%%%%%\n"
" ###################### %%%%%%%%%%%%%%%%%%%%\n"
" ####################### %%%%%%%%%%%%%%%%%%\n"
" ####################### %%%%%%%%%%%%%%%%%\n"
" ###################### %%%%%%%%%%%%%%%%%\n"
" #################### %%%%%%%%%%%%%%%%%\n"
" ################# %%%%%%%%%%%%%%%%%\n"
" ############### %%%%%%%%%%%%%%%%\n"
" ############ %%%%%%%%%%%%%%%\n"
" ######## %%%%%%%%%%%%%%\n"
" %%%%%%%%%%%\n\n");
// Write version information
std::cout <<
" | The OpenMC Monte Carlo Code\n" <<
" Copyright | 2011-2020 MIT and OpenMC contributors\n" <<
" License | http://openmc.readthedocs.io/en/latest/license.html\n" <<
" Version | " << VERSION_MAJOR << '.' << VERSION_MINOR << '.'
<< VERSION_RELEASE << (VERSION_DEV ? "-dev" : "") << '\n';
fmt::print(
" | The OpenMC Monte Carlo Code\n"
" Copyright | 2011-2020 MIT and OpenMC contributors\n"
" License | http://openmc.readthedocs.io/en/latest/license.html\n"
" Version | {}.{}.{}{}\n", VERSION_MAJOR, VERSION_MINOR,
VERSION_RELEASE, VERSION_DEV ? "-dev" : "");
#ifdef GIT_SHA1
std::cout << " Git SHA1 | " << GIT_SHA1 << '\n';
fmt::print(" Git SHA1 | {}\n", GIT_SHA1);
#endif
// Write the date and time
std::cout << " Date/Time | " << time_stamp() << '\n';
fmt::print(" Date/Time | {}\n", time_stamp());
#ifdef OPENMC_MPI
// Write number of processors
std::cout << " MPI Processes | " << mpi::n_procs << '\n';
fmt::print(" MPI Processes | {}\n", mpi::n_procs);
#endif
#ifdef _OPENMP
// Write number of OpenMP threads
std::cout << " OpenMP Threads | " << omp_get_max_threads() << '\n';
fmt::print(" OpenMP Threads | {}\n", omp_get_max_threads());
#endif
std::cout << std::endl;
}
@ -146,64 +147,61 @@ extern "C" void print_particle(Particle* p)
// Display particle type and ID.
switch (p->type_) {
case Particle::Type::neutron:
std::cout << "Neutron ";
fmt::print("Neutron ");
break;
case Particle::Type::photon:
std::cout << "Photon ";
fmt::print("Photon ");
break;
case Particle::Type::electron:
std::cout << "Electron ";
fmt::print("Electron ");
break;
case Particle::Type::positron:
std::cout << "Positron ";
fmt::print("Positron ");
break;
default:
std::cout << "Unknown Particle ";
fmt::print("Unknown Particle ");
}
std::cout << p->id_ << "\n";
fmt::print("{}\n", p->id_);
// Display particle geometry hierarchy.
for (auto i = 0; i < p->n_coord_; i++) {
std::cout << " Level " << i << "\n";
fmt::print(" Level {}\n", i);
if (p->coord_[i].cell != C_NONE) {
const Cell& c {*model::cells[p->coord_[i].cell]};
std::cout << " Cell = " << c.id_ << "\n";
fmt::print(" Cell = {}\n", c.id_);
}
if (p->coord_[i].universe != C_NONE) {
const Universe& u {*model::universes[p->coord_[i].universe]};
std::cout << " Universe = " << u.id_ << "\n";
fmt::print(" Universe = {}\n", u.id_);
}
if (p->coord_[i].lattice != C_NONE) {
const Lattice& lat {*model::lattices[p->coord_[i].lattice]};
std::cout << " Lattice = " << lat.id_ << "\n";
std::cout << " Lattice position = (" << p->coord_[i].lattice_x
<< "," << p->coord_[i].lattice_y << ","
<< p->coord_[i].lattice_z << ")\n";
fmt::print(" Lattice = {}\n", lat.id_);
fmt::print(" Lattice position = ({},{},{})\n", p->coord_[i].lattice_x,
p->coord_[i].lattice_y, p->coord_[i].lattice_z);
}
std::cout << " r = (" << p->coord_[i].r.x << ", "
<< p->coord_[i].r.y << ", " << p->coord_[i].r.z << ")\n";
std::cout << " u = (" << p->coord_[i].u.x << ", "
<< p->coord_[i].u.y << ", " << p->coord_[i].u.z << ")\n";
fmt::print(" r = ");
std::cout << p->coord_[i].r << '\n';
fmt::print(" u = ");
std::cout << p->coord_[i].u << '\n';
}
// Display miscellaneous info.
if (p->surface_ != 0) {
const Surface& surf {*model::surfaces[std::abs(p->surface_)-1]};
std::cout << " Surface = " << std::copysign(surf.id_, p->surface_) << "\n";
fmt::print(" Surface = {}\n", std::copysign(surf.id_, p->surface_));
}
std::cout << " Weight = " << p->wgt_ << "\n";
fmt::print(" Weight = {}\n", p->wgt_);
if (settings::run_CE) {
std::cout << " Energy = " << p->E_ << "\n";
fmt::print(" Energy = {}\n", p->E_);
} else {
std::cout << " Energy Group = " << p->g_ << "\n";
fmt::print(" Energy Group = {}\n", p->g_);
}
std::cout << " Delayed Group = " << p->delayed_group_ << "\n";
std::cout << "\n";
fmt::print(" Delayed Group = {}\n\n", p->delayed_group_);
}
//==============================================================================
@ -215,65 +213,53 @@ void print_plot()
for (auto pl : model::plots) {
// Plot id
std::cout << "Plot ID: " << pl.id_ << "\n";
fmt::print("Plot ID: {}\n", pl.id_);
// Plot filename
std::cout << "Plot file: " << pl.path_plot_ << "\n";
fmt::print("Plot file: {}\n", pl.path_plot_);
// Plot level
std::cout << "Universe depth: " << pl.level_ << "\n";
fmt::print("Universe depth: {}\n", pl.level_);
// Plot type
if (PlotType::slice == pl.type_) {
std::cout << "Plot Type: Slice" << "\n";
fmt::print("Plot Type: Slice\n");
} else if (PlotType::voxel == pl.type_) {
std::cout << "Plot Type: Voxel" << "\n";
fmt::print("Plot Type: Voxel\n");
}
// Plot parameters
std::cout << "Origin: " << pl.origin_[0] << " "
<< pl.origin_[1] << " "
<< pl.origin_[2] << "\n";
fmt::print("Origin: {} {} {}\n", pl.origin_[0], pl.origin_[1], pl.origin_[2]);
if (PlotType::slice == pl.type_) {
std::cout << std::setprecision(4)
<< "Width: "
<< pl.width_[0] << " "
<< pl.width_[1] << "\n";
fmt::print("Width: {:4} {:4}\n", pl.width_[0], pl.width_[1]);
} else if (PlotType::voxel == pl.type_) {
std::cout << std::setprecision(4)
<< "Width: "
<< pl.width_[0] << " "
<< pl.width_[1] << " "
<< pl.width_[2] << "\n";
fmt::print("Width: {:4} {:4} {:4}\n", pl.width_[0], pl.width_[1],
pl.width_[2]);
}
if (PlotColorBy::cells == pl.color_by_) {
std::cout << "Coloring: Cells" << "\n";
fmt::print("Coloring: Cells\n");
} else if (PlotColorBy::mats == pl.color_by_) {
std::cout << "Coloring: Materials" << "\n";
fmt::print("Coloring: Materials\n");
}
if (PlotType::slice == pl.type_) {
switch(pl.basis_) {
case PlotBasis::xy:
std::cout << "Basis: XY" << "\n";
fmt::print("Basis: XY\n");
break;
case PlotBasis::xz:
std::cout << "Basis: XZ" << "\n";
fmt::print("Basis: XZ\n");
break;
case PlotBasis::yz:
std::cout << "Basis: YZ" << "\n";
fmt::print("Basis: YZ\n");
break;
}
std::cout << "Pixels: " << pl.pixels_[0] << " "
<< pl.pixels_[1] << " " << "\n";
fmt::print("Pixels: {} {}\n", pl.pixels_[0], pl.pixels_[1]);
} else if (PlotType::voxel == pl.type_) {
std::cout << "Voxels: " << pl.pixels_[0] << " "
<< pl.pixels_[1] << " "
<< pl.pixels_[2] << "\n";
fmt::print("Voxels: {} {} {}\n", pl.pixels_[0], pl.pixels_[1], pl.pixels_[2]);
}
std::cout << "\n";
fmt::print("\n");
}
}
@ -291,23 +277,22 @@ print_overlap_check()
if (mpi::master) {
header("cell overlap check summary", 1);
std::cout << " Cell ID No. Overlap Checks\n";
fmt::print(" Cell ID No. Overlap Checks\n");
std::vector<int32_t> sparse_cell_ids;
for (int i = 0; i < model::cells.size(); i++) {
std::cout << " " << std::setw(8) << model::cells[i]->id_ << std::setw(17)
<< model::overlap_check_count[i] << "\n";
fmt::print(" {:8}{:17}\n", model::cells[i]->id_, model::overlap_check_count[i]);
if (model::overlap_check_count[i] < 10) {
sparse_cell_ids.push_back(model::cells[i]->id_);
}
}
std::cout << "\n There were " << sparse_cell_ids.size()
<< " cells with less than 10 overlap checks\n";
fmt::print("\n There were {} cells with less than 10 overlap checks\n",
sparse_cell_ids.size());
for (auto id : sparse_cell_ids) {
std::cout << " " << id;
fmt::print(" {}", id);
}
std::cout << "\n";
fmt::print("\n");
}
}
@ -316,7 +301,7 @@ print_overlap_check()
void print_usage()
{
if (mpi::master) {
std::cout <<
fmt::print(
"Usage: openmc [options] [directory]\n\n"
"Options:\n"
" -c, --volume Run in stochastic volume calculation mode\n"
@ -329,7 +314,7 @@ void print_usage()
" -t, --track Write tracks for all particles\n"
" -e, --event Run using event-based parallelism\n"
" -v, --version Show version information\n"
" -h, --help Show this message\n";
" -h, --help Show this message\n");
}
}
@ -338,14 +323,14 @@ void print_usage()
void print_version()
{
if (mpi::master) {
std::cout << "OpenMC version " << VERSION_MAJOR << '.' << VERSION_MINOR
<< '.' << VERSION_RELEASE << '\n';
fmt::print("OpenMC version {}.{}.{}\n", VERSION_MAJOR, VERSION_MINOR,
VERSION_RELEASE);
#ifdef GIT_SHA1
std::cout << "Git SHA1: " << GIT_SHA1 << '\n';
fmt::print("Git SHA1: {}\n", GIT_SHA1);
#endif
std::cout << "Copyright (c) 2011-2019 Massachusetts Institute of "
fmt::print("Copyright (c) 2011-2019 Massachusetts Institute of "
"Technology and OpenMC contributors\nMIT/X license at "
"<http://openmc.readthedocs.io/en/latest/license.html>\n";
"<http://openmc.readthedocs.io/en/latest/license.html>\n");
}
}
@ -354,13 +339,13 @@ void print_version()
void print_columns()
{
if (settings::entropy_on) {
std::cout <<
fmt::print(
" Bat./Gen. k Entropy Average k \n"
" ========= ======== ======== ====================\n";
" ========= ======== ======== ====================\n");
} else {
std::cout <<
fmt::print(
" Bat./Gen. k Average k\n"
" ========= ======== ====================\n";
" ========= ======== ====================\n");
}
}
@ -368,85 +353,63 @@ void print_columns()
void print_generation()
{
// Save state of cout
auto f {std::cout.flags()};
// Determine overall generation and number of active generations
int i = overall_generation() - 1;
int n = simulation::current_batch > settings::n_inactive ?
settings::gen_per_batch*simulation::n_realizations + simulation::current_gen : 0;
// Set format for values
std::cout << std::fixed << std::setprecision(5);
// write out information batch and option independent output
std::cout << " " << std::setw(9) << std::to_string(simulation::current_batch)
+ "/" + std::to_string(simulation::current_gen) << " " << std::setw(8)
<< simulation::k_generation[i];
auto batch_and_gen = std::to_string(simulation::current_batch) + "/" +
std::to_string(simulation::current_gen);
fmt::print(" {:>9} {:8.5f}", batch_and_gen, simulation::k_generation[i]);
// write out entropy info
if (settings::entropy_on) {
std::cout << " " << std::setw(8) << simulation::entropy[i];
fmt::print(" {:8.5f}", simulation::entropy[i]);
}
if (n > 1) {
std::cout << " " << std::setw(8) << simulation::keff << " +/-"
<< std::setw(8) << simulation::keff_std;
fmt::print(" {:8.5f} +/-{:8.5f}", simulation::keff, simulation::keff_std);
}
std::cout << '\n';
// Restore state of cout
std::cout.flags(f);
std::cout << std::endl;
}
//==============================================================================
void print_batch_keff()
{
// Save state of cout
auto f {std::cout.flags()};
// Determine overall generation and number of active generations
int i = simulation::current_batch*settings::gen_per_batch - 1;
int n = simulation::n_realizations*settings::gen_per_batch;
// Set format for values
std::cout << std::fixed << std::setprecision(5);
// write out information batch and option independent output
std::cout << " " << std::setw(9) << std::to_string(simulation::current_batch)
+ "/" + std::to_string(settings::gen_per_batch) << " " << std::setw(8)
<< simulation::k_generation[i];
auto batch_and_gen = std::to_string(simulation::current_batch) + "/" +
std::to_string(settings::gen_per_batch);
fmt::print(" {:>9} {:8.5f}", batch_and_gen, simulation::k_generation[i]);
// write out entropy info
if (settings::entropy_on) {
std::cout << " " << std::setw(8) << simulation::entropy[i];
fmt::print(" {:8.5f}", simulation::entropy[i]);
}
if (n > 1) {
std::cout << " " << std::setw(8) << simulation::keff << " +/-"
<< std::setw(8) << simulation::keff_std;
fmt::print(" {:8.5f} +/-{:8.5f}", simulation::keff, simulation::keff_std);
}
std::cout << std::endl;
// Restore state of cout
std::cout.flags(f);
}
//==============================================================================
void show_time(const char* label, double secs, int indent_level=0)
{
std::cout << std::string(2*indent_level, ' ');
int width = 33 - indent_level*2;
std::cout << " " << std::setw(width) << std::left << label << " = "
<< std::setw(10) << std::right << secs << " seconds\n";
fmt::print("{0:{1}} {2:<{3}} = {4:>10.4e} seconds\n",
"", 2*indent_level, label, width, secs);
}
void show_rate(const char* label, double particles_per_sec)
{
std::cout << " " << std::setw(33) << std::left << label << " = " <<
particles_per_sec << " particles/second\n";
fmt::print(" {:<33} = {:.6} particles/second\n", label, particles_per_sec);
}
void print_runtime()
@ -457,11 +420,7 @@ void print_runtime()
header("Timing Statistics", 6);
if (settings::verbosity < 6) return;
// Save state of cout
auto f {std::cout.flags()};
// display time elapsed for various sections
std::cout << std::scientific << std::setprecision(4);
show_time("Total time for initialization", time_initialize.elapsed());
show_time("Reading cross sections", time_read_xs.elapsed(), 1);
show_time("Total time in simulation", time_inactive.elapsed() +
@ -490,9 +449,6 @@ void print_runtime()
show_time("Total time for finalization", time_finalize.elapsed());
show_time("Total time elapsed", time_total.elapsed());
// Restore state of cout
std::cout.flags(f);
// Calculate particle rate in active/inactive batches
int n_active = simulation::current_batch - settings::n_inactive;
double speed_inactive = 0.0;
@ -519,15 +475,11 @@ void print_runtime()
}
// display calculation rate
std::cout << std::setprecision(6) << std::showpoint;
if (!(settings::restart_run && (simulation::restart_batch >= settings::n_inactive))
&& settings::n_inactive > 0) {
show_rate("Calculation Rate (inactive)", speed_inactive);
}
show_rate("Calculation Rate (active)", speed_active);
// Restore state of cout
std::cout.flags(f);
}
//==============================================================================
@ -545,9 +497,6 @@ mean_stdev(const double* x, int n)
void print_results()
{
// Save state of cout
auto f {std::cout.flags()};
// display header block for results
header("Results", 4);
if (settings::verbosity < 4) return;
@ -564,52 +513,46 @@ void print_results()
t_n3 = 1.0;
}
// Set formatting for floats
std::cout << std::fixed << std::setprecision(5);
// write global tallies
const auto& gt = simulation::global_tallies;
double mean, stdev;
if (n > 1) {
if (settings::run_mode == RunMode::EIGENVALUE) {
std::tie(mean, stdev) = mean_stdev(&gt(GlobalTally::K_COLLISION, 0), n);
std::cout << " k-effective (Collision) = "
<< mean << " +/- " << t_n1 * stdev << '\n';
fmt::print(" k-effective (Collision) = {:.5f} +/- {:.5f}\n",
mean, t_n1 * stdev);
std::tie(mean, stdev) = mean_stdev(&gt(GlobalTally::K_TRACKLENGTH, 0), n);
std::cout << " k-effective (Track-length) = "
<< mean << " +/- " << t_n1 * stdev << '\n';
fmt::print(" k-effective (Track-length) = {:.5f} +/- {:.5f}\n",
mean, t_n1 * stdev);
std::tie(mean, stdev) = mean_stdev(&gt(GlobalTally::K_ABSORPTION, 0), n);
std::cout << " k-effective (Absorption) = "
<< mean << " +/- " << t_n1 * stdev << '\n';
fmt::print(" k-effective (Absorption) = {:.5f} +/- {:.5f}\n",
mean, t_n1 * stdev);
if (n > 3) {
double k_combined[2];
openmc_get_keff(k_combined);
std::cout << " Combined k-effective = "
<< k_combined[0] << " +/- " << t_n3 * k_combined[1] << '\n';
fmt::print(" Combined k-effective = {:.5f} +/- {:.5f}\n",
k_combined[0], k_combined[1]);
}
}
std::tie(mean, stdev) = mean_stdev(&gt(GlobalTally::LEAKAGE, 0), n);
std::cout << " Leakage Fraction = "
<< mean << " +/- " << t_n1 * stdev << '\n';
fmt::print(" Leakage Fraction = {:.5f} +/- {:.5f}\n",
mean, t_n1 * stdev);
} else {
if (mpi::master) warning("Could not compute uncertainties -- only one "
"active batch simulated!");
if (settings::run_mode == RunMode::EIGENVALUE) {
std::cout << " k-effective (Collision) = "
<< gt(GlobalTally::K_COLLISION, TallyResult::SUM) / n << '\n';
std::cout << " k-effective (Track-length) = "
<< gt(GlobalTally::K_TRACKLENGTH, TallyResult::SUM) / n << '\n';
std::cout << " k-effective (Absorption) = "
<< gt(GlobalTally::K_ABSORPTION, TallyResult::SUM) / n << '\n';
fmt::print(" k-effective (Collision) = {:.5f}\n",
gt(GlobalTally::K_COLLISION, TallyResult::SUM) / n);
fmt::print(" k-effective (Track-length) = {:.5f}\n",
gt(GlobalTally::K_TRACKLENGTH, TallyResult::SUM) / n);
fmt::print(" k-effective (Absorption) = {:.5f}\n",
gt(GlobalTally::K_ABSORPTION, TallyResult::SUM) / n);
}
std::cout << " Leakage Fraction = "
<< gt(GlobalTally::LEAKAGE, TallyResult::SUM) / n << '\n';
fmt::print(" Leakage Fraction = {:.5f}\n",
gt(GlobalTally::LEAKAGE, TallyResult::SUM) / n);
}
std::cout << '\n';
// Restore state of cout
std::cout.flags(f);
fmt::print("\n");
}
//==============================================================================