• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmc-dev / openmc / 23291766092

19 Mar 2026 11:01AM UTC coverage: 80.712% (-0.7%) from 81.447%
23291766092

Pull #3886

github

web-flow
Merge 7b45738dd into 3ce6cbfdd
Pull Request #3886: Implement python tally types

16322 of 23494 branches covered (69.47%)

Branch coverage included in aggregate %.

213 of 262 new or added lines in 11 files covered. (81.3%)

710 existing lines in 49 files now uncovered.

56450 of 66668 relevant lines covered (84.67%)

7838586.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.49
/src/output.cpp
1
#include "openmc/output.h"
2

3
#include <algorithm> // for transform, max
4
#include <cstdio>    // for stdout
5
#include <cstring>   // for strlen
6
#include <ctime>     // for time, localtime
7
#include <fstream>
8
#include <iomanip> // for setw, setprecision, put_time
9
#include <ios>     // for fixed, scientific, left
10
#include <iostream>
11
#include <sstream>
12
#include <unordered_map>
13
#include <utility> // for pair
14

15
#include <fmt/core.h>
16
#include <fmt/ostream.h>
17
#ifdef _OPENMP
18
#include <omp.h>
19
#endif
20
#include "openmc/tensor.h"
21

22
#include "openmc/capi.h"
23
#include "openmc/cell.h"
24
#include "openmc/constants.h"
25
#include "openmc/eigenvalue.h"
26
#include "openmc/error.h"
27
#include "openmc/geometry.h"
28
#include "openmc/lattice.h"
29
#include "openmc/math_functions.h"
30
#include "openmc/message_passing.h"
31
#include "openmc/mgxs_interface.h"
32
#include "openmc/nuclide.h"
33
#include "openmc/plot.h"
34
#include "openmc/random_ray/flat_source_domain.h"
35
#include "openmc/reaction.h"
36
#include "openmc/settings.h"
37
#include "openmc/simulation.h"
38
#include "openmc/surface.h"
39
#include "openmc/tallies/derivative.h"
40
#include "openmc/tallies/filter.h"
41
#include "openmc/tallies/tally.h"
42
#include "openmc/tallies/tally_scoring.h"
43
#include "openmc/timer.h"
44

45
namespace openmc {
46

47
#ifdef OPENMC_ENABLE_STRICT_FP
48
const bool STRICT_FP_ENABLED = true;
49
#else
50
const bool STRICT_FP_ENABLED = false;
51
#endif
52

53
//==============================================================================
54

55
void title()
643✔
56
{
57
  fmt::print("                                %%%%%%%%%%%%%%%\n"
643✔
58
             "                           %%%%%%%%%%%%%%%%%%%%%%%%\n"
59
             "                        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
60
             "                      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
61
             "                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
62
             "                   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
63
             "                                    %%%%%%%%%%%%%%%%%%%%%%%%\n"
64
             "                                     %%%%%%%%%%%%%%%%%%%%%%%%\n"
65
             "                 ###############      %%%%%%%%%%%%%%%%%%%%%%%%\n"
66
             "                ##################     %%%%%%%%%%%%%%%%%%%%%%%\n"
67
             "                ###################     %%%%%%%%%%%%%%%%%%%%%%%\n"
68
             "                ####################     %%%%%%%%%%%%%%%%%%%%%%\n"
69
             "                #####################     %%%%%%%%%%%%%%%%%%%%%\n"
70
             "                ######################     %%%%%%%%%%%%%%%%%%%%\n"
71
             "                #######################     %%%%%%%%%%%%%%%%%%\n"
72
             "                 #######################     %%%%%%%%%%%%%%%%%\n"
73
             "                 ######################     %%%%%%%%%%%%%%%%%\n"
74
             "                  ####################     %%%%%%%%%%%%%%%%%\n"
75
             "                    #################     %%%%%%%%%%%%%%%%%\n"
76
             "                     ###############     %%%%%%%%%%%%%%%%\n"
77
             "                       ############     %%%%%%%%%%%%%%%\n"
78
             "                          ########     %%%%%%%%%%%%%%\n"
79
             "                                      %%%%%%%%%%%\n\n");
80

81
  // Write version information
82
  fmt::print(
1,286✔
83
    "                 | The OpenMC Monte Carlo Code\n"
84
    "       Copyright | 2011-2026 MIT, UChicago Argonne LLC, and contributors\n"
85
    "         License | https://docs.openmc.org/en/latest/license.html\n"
86
    "         Version | {}.{}.{}{}{}\n",
87
    VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE, VERSION_DEV ? "-dev" : "",
643✔
88
    VERSION_COMMIT_COUNT);
89
  fmt::print("     Commit Hash | {}\n", VERSION_COMMIT_HASH);
643✔
90

91
  // Write the date and time
92
  fmt::print("       Date/Time | {}\n", time_stamp());
643✔
93

94
#ifdef OPENMC_MPI
95
  // Write number of processors
96
  fmt::print("   MPI Processes | {}\n", mpi::n_procs);
97
#endif
98

99
#ifdef _OPENMP
100
  // Write number of OpenMP threads
101
  fmt::print("  OpenMP Threads | {}\n", omp_get_max_threads());
102
#endif
103
  fmt::print("\n");
643✔
104
  std::fflush(stdout);
643✔
105
}
643✔
106

107
//==============================================================================
108

109
std::string header(const char* msg)
3,482✔
110
{
111
  // Determine how many times to repeat the '=' character.
112
  int n_prefix = (63 - strlen(msg)) / 2;
3,482✔
113
  int n_suffix = n_prefix;
3,482✔
114
  if ((strlen(msg) % 2) == 0)
3,482✔
115
    ++n_suffix;
704✔
116

117
  // Convert to uppercase.
118
  std::string upper(msg);
3,482✔
119
  std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
3,482✔
120

121
  // Add ===>  <=== markers.
122
  std::stringstream out;
3,482✔
123
  out << ' ';
3,482✔
124
  for (int i = 0; i < n_prefix; i++)
88,613✔
125
    out << '=';
85,131✔
126
  out << ">     " << upper << "     <";
3,482✔
127
  for (int i = 0; i < n_suffix; i++)
89,317✔
128
    out << '=';
85,835✔
129

130
  return out.str();
3,482✔
131
}
3,482✔
132

133
std::string header(const std::string& msg)
1,742✔
134
{
135
  return header(msg.c_str());
1,742✔
136
}
137

138
void header(const char* msg, int level)
1,740✔
139
{
140
  auto out = header(msg);
1,740✔
141

142
  // Print header based on verbosity level.
143
  if (settings::verbosity >= level) {
1,740✔
144
    fmt::print("\n{}\n\n", out);
1,715✔
145
    std::fflush(stdout);
1,715✔
146
  }
147
}
1,740✔
148

149
//==============================================================================
150

151
std::string time_stamp()
1,927✔
152
{
153
  std::stringstream ts;
1,927✔
154
  std::time_t t = std::time(nullptr); // get time now
1,927✔
155
  ts << std::put_time(std::localtime(&t), "%Y-%m-%d %H:%M:%S");
1,927✔
156
  return ts.str();
3,854✔
157
}
1,927✔
158

159
//==============================================================================
160

161
void print_particle(Particle& p)
3✔
162
{
163
  // Display particle type and ID.
164
  switch (p.type().pdg_number()) {
3!
165
  case PDG_NEUTRON:
3✔
166
    fmt::print("Neutron ");
3✔
167
    break;
3✔
168
  case PDG_PHOTON:
×
169
    fmt::print("Photon ");
×
170
    break;
×
171
  case PDG_ELECTRON:
×
172
    fmt::print("Electron ");
×
173
    break;
×
174
  case PDG_POSITRON:
×
175
    fmt::print("Positron ");
×
176
    break;
×
177
  default:
×
178
    fmt::print("Particle {} ", p.type().str());
×
179
  }
180
  fmt::print("{}\n", p.id());
3✔
181

182
  // Display particle geometry hierarchy.
183
  for (auto i = 0; i < p.n_coord(); i++) {
6✔
184
    fmt::print("  Level {}\n", i);
3✔
185

186
    if (p.coord(i).cell() != C_NONE) {
3!
187
      const Cell& c {*model::cells[p.coord(i).cell()]};
3✔
188
      fmt::print("    Cell             = {}\n", c.id_);
3✔
189
    }
190

191
    if (p.coord(i).universe() != C_NONE) {
3!
192
      const Universe& u {*model::universes[p.coord(i).universe()]};
3✔
193
      fmt::print("    Universe         = {}\n", u.id_);
3✔
194
    }
195

196
    if (p.coord(i).lattice() != C_NONE) {
3!
197
      const Lattice& lat {*model::lattices[p.coord(i).lattice()]};
×
198
      fmt::print("    Lattice          = {}\n", lat.id_);
×
199
      fmt::print("    Lattice position = ({},{},{})\n",
×
200
        p.coord(i).lattice_index()[0], p.coord(i).lattice_index()[1],
×
201
        p.coord(i).lattice_index()[2]);
×
202
    }
203

204
    fmt::print("    r = {}\n", p.coord(i).r());
3✔
205
    fmt::print("    u = {}\n", p.coord(i).u());
3✔
206
  }
207

208
  // Display miscellaneous info.
209
  if (p.surface() != SURFACE_NONE) {
3!
210
    // Surfaces identifiers are >= 1, but indices are >= 0 so we need -1
211
    const Surface& surf {*model::surfaces[p.surface_index()]};
×
212
    fmt::print("  Surface = {}\n", (p.surface() > 0) ? surf.id_ : -surf.id_);
×
213
  }
214
  fmt::print("  Weight = {}\n", p.wgt());
3✔
215
  if (settings::run_CE) {
3!
216
    fmt::print("  Energy = {}\n", p.E());
3✔
217
  } else {
218
    fmt::print("  Energy Group = {}\n", p.g());
×
219
  }
220
  fmt::print("  Delayed Group = {}\n\n", p.delayed_group());
3✔
221
}
3✔
222

223
//==============================================================================
224

225
void print_plot()
8✔
226
{
227
  header("PLOTTING SUMMARY", 5);
8✔
228
  if (settings::verbosity < 5)
8!
229
    return;
230

231
  for (const auto& pl : model::plots) {
30✔
232
    fmt::print("Plot ID: {}\n", pl->id());
22✔
233
    fmt::print("Plot file: {}\n", pl->path_plot());
22✔
234
    fmt::print("Universe depth: {}\n", pl->level());
22✔
235
    pl->print_info(); // prints type-specific plot info
22✔
236
    fmt::print("\n");
22✔
237
  }
238
}
239

240
//==============================================================================
241

242
void print_overlap_check()
×
243
{
244
#ifdef OPENMC_MPI
245
  vector<int64_t> temp(model::overlap_check_count);
246
  MPI_Reduce(temp.data(), model::overlap_check_count.data(),
247
    model::overlap_check_count.size(), MPI_INT64_T, MPI_SUM, 0, mpi::intracomm);
248
#endif
249

250
  if (mpi::master) {
×
251
    header("cell overlap check summary", 1);
×
252
    fmt::print(" Cell ID      No. Overlap Checks\n");
×
253

254
    vector<int32_t> sparse_cell_ids;
×
255
    for (int i = 0; i < model::cells.size(); i++) {
×
256
      fmt::print(
×
257
        " {:8} {:17}\n", model::cells[i]->id_, model::overlap_check_count[i]);
×
258
      if (model::overlap_check_count[i] < 10) {
×
259
        sparse_cell_ids.push_back(model::cells[i]->id_);
×
260
      }
261
    }
262

263
    fmt::print("\n There were {} cells with less than 10 overlap checks\n",
×
264
      sparse_cell_ids.size());
×
265
    for (auto id : sparse_cell_ids) {
×
266
      fmt::print(" {}", id);
×
267
    }
268
    fmt::print("\n");
×
269
  }
×
270
}
×
271

272
//==============================================================================
273

274
void print_usage()
×
275
{
276
  if (mpi::master) {
×
277
    fmt::print(
×
278
      "Usage: openmc [options] [path]\n\n"
279
      "Options:\n"
280
      "  -c, --volume           Run in stochastic volume calculation mode\n"
281
      "  -g, --geometry-debug   Run with geometry debugging on\n"
282
      "  -n, --particles        Number of particles per generation\n"
283
      "  -p, --plot             Run in plotting mode\n"
284
      "  -r, --restart          Restart a previous run from a state point\n"
285
      "                         or a particle restart file\n"
286
      "  -s, --threads          Number of OpenMP threads\n"
287
      "  -t, --track            Write tracks for all particles (up to "
288
      "max_tracks)\n"
289
      "  -e, --event            Run using event-based parallelism\n"
290
      "  -q, --verbosity        Output verbosity\n"
291
      "  -v, --version          Show version information\n"
292
      "  -h, --help             Show this message\n");
293
  }
294
}
×
295

296
//==============================================================================
297

298
void print_version()
1✔
299
{
300
  if (mpi::master) {
1!
301
    fmt::print("OpenMC version {}.{}.{}{}{}\n", VERSION_MAJOR, VERSION_MINOR,
2✔
302
      VERSION_RELEASE, VERSION_DEV ? "-dev" : "", VERSION_COMMIT_COUNT);
1✔
303
    fmt::print("Commit hash: {}\n", VERSION_COMMIT_HASH);
1✔
304
    fmt::print("Copyright (c) 2011-2026 MIT, UChicago Argonne LLC, and "
1✔
305
               "contributors\nMIT/X license at "
306
               "<https://docs.openmc.org/en/latest/license.html>\n");
307
  }
308
}
1✔
309

310
//==============================================================================
311

312
void print_build_info()
1✔
313
{
314
  const std::string n("no");
1✔
315
  const std::string y("yes");
1✔
316

317
  std::string mpi(n);
1✔
318
  std::string phdf5(n);
1✔
319
  std::string dagmc(n);
1✔
320
  std::string libmesh(n);
1✔
321
  std::string png(n);
1✔
322
  std::string profiling(n);
1✔
323
  std::string coverage(n);
1✔
324
  std::string mcpl(n);
1✔
325
  std::string uwuw(n);
1✔
326
  std::string strict_fp(n);
1✔
327

328
#ifdef PHDF5
329
  phdf5 = y;
330
#endif
331
#ifdef OPENMC_MPI
332
  mpi = y;
333
#endif
334
#ifdef OPENMC_DAGMC_ENABLED
335
  dagmc = y;
336
#endif
337
#ifdef OPENMC_LIBMESH_ENABLED
338
  libmesh = y;
339
#endif
340
#ifdef OPENMC_MCPL
341
  mcpl = y;
342
#endif
343
#ifdef USE_LIBPNG
344
  png = y;
1✔
345
#endif
346
#ifdef PROFILINGBUILD
347
  profiling = y;
348
#endif
349
#ifdef COVERAGEBUILD
350
  coverage = y;
1✔
351
#endif
352
#ifdef OPENMC_UWUW_ENABLED
353
  uwuw = y;
354
#endif
355
#ifdef OPENMC_ENABLE_STRICT_FP
356
  strict_fp = y;
1✔
357
#endif
358

359
  // Wraps macro variables in quotes
360
#define STRINGIFY(x) STRINGIFY2(x)
361
#define STRINGIFY2(x) #x
362

363
  if (mpi::master) {
1!
364
    fmt::print("Build type:            {}\n", STRINGIFY(BUILD_TYPE));
1✔
365
    fmt::print("Compiler ID:           {} {}\n", STRINGIFY(COMPILER_ID),
1✔
366
      STRINGIFY(COMPILER_VERSION));
367
    fmt::print("MPI enabled:           {}\n", mpi);
1✔
368
    fmt::print("Parallel HDF5 enabled: {}\n", phdf5);
1✔
369
    fmt::print("PNG support:           {}\n", png);
1✔
370
    fmt::print("DAGMC support:         {}\n", dagmc);
1✔
371
    fmt::print("libMesh support:       {}\n", libmesh);
1✔
372
    fmt::print("MCPL support:          {}\n", mcpl);
1✔
373
    fmt::print("Coverage testing:      {}\n", coverage);
1✔
374
    fmt::print("Profiling flags:       {}\n", profiling);
1✔
375
    fmt::print("UWUW support:          {}\n", uwuw);
1✔
376
    fmt::print("Strict FP:             {}\n", strict_fp);
1✔
377
  }
378
}
1✔
379

380
//==============================================================================
381

382
void print_columns()
309✔
383
{
384
  if (settings::entropy_on) {
309✔
385
    fmt::print("  Bat./Gen.      k       Entropy         Average k \n"
45✔
386
               "  =========   ========   ========   ====================\n");
387
  } else {
388
    fmt::print("  Bat./Gen.      k            Average k\n"
264✔
389
               "  =========   ========   ====================\n");
390
  }
391
}
309✔
392

393
//==============================================================================
394

395
void print_generation()
6,903✔
396
{
397
  // Determine overall generation index and number of active generations
398
  int idx = overall_generation() - 1;
6,903✔
399
  int n = simulation::current_batch > settings::n_inactive
13,806✔
400
            ? settings::gen_per_batch * simulation::n_realizations +
6,903✔
401
                simulation::current_gen
402
            : 0;
403

404
  // write out batch/generation and generation k-effective
405
  auto batch_and_gen = std::to_string(simulation::current_batch) + "/" +
13,806✔
406
                       std::to_string(simulation::current_gen);
13,806✔
407
  fmt::print("  {:>9}   {:8.5f}", batch_and_gen, simulation::k_generation[idx]);
6,903✔
408

409
  // write out entropy info
410
  if (settings::entropy_on) {
6,903✔
411
    fmt::print("   {:8.5f}", simulation::entropy[idx]);
1,205✔
412
  }
413

414
  if (n > 1) {
6,903✔
415
    fmt::print("   {:8.5f} +/-{:8.5f}", simulation::keff, simulation::keff_std);
4,892✔
416
  }
417
  fmt::print("\n");
6,903✔
418
  std::fflush(stdout);
6,903✔
419
}
6,903✔
420

421
//==============================================================================
422

423
void show_time(const char* label, double secs, int indent_level)
6,234✔
424
{
425
  int width = 33 - indent_level * 2;
6,234✔
426
  fmt::print("{0:{1}} {2:<{3}} = {4:>10.4e} seconds\n", "", 2 * indent_level,
6,234✔
427
    label, width, secs);
428
}
6,234✔
429

430
void show_rate(const char* label, double particles_per_sec)
693✔
431
{
432
  fmt::print(" {:<33} = {:.6} particles/second\n", label, particles_per_sec);
693✔
433
}
693✔
434

435
void print_runtime()
489✔
436
{
437
  using namespace simulation;
489✔
438

439
  // display header block
440
  header("Timing Statistics", 6);
489✔
441
  if (settings::verbosity < 6)
489!
442
    return;
443

444
  // display time elapsed for various sections
445
  show_time("Total time for initialization", time_initialize.elapsed());
489✔
446
  show_time("Reading cross sections", time_read_xs.elapsed(), 1);
489✔
447
  show_time("Total time in simulation",
489✔
448
    time_inactive.elapsed() + time_active.elapsed());
489✔
449
  show_time("Time in transport only", time_transport.elapsed(), 1);
489✔
450
  if (settings::event_based) {
489!
UNCOV
451
    show_time("Particle initialization", time_event_init.elapsed(), 2);
×
UNCOV
452
    show_time("XS lookups", time_event_calculate_xs.elapsed(), 2);
×
UNCOV
453
    show_time("Advancing", time_event_advance_particle.elapsed(), 2);
×
UNCOV
454
    show_time("Surface crossings", time_event_surface_crossing.elapsed(), 2);
×
UNCOV
455
    show_time("Collisions", time_event_collision.elapsed(), 2);
×
UNCOV
456
    show_time("Particle death", time_event_death.elapsed(), 2);
×
457
  }
458
  if (settings::run_mode == RunMode::EIGENVALUE) {
489✔
459
    show_time("Time in inactive batches", time_inactive.elapsed(), 1);
284✔
460
  }
461
  show_time("Time in active batches", time_active.elapsed(), 1);
489✔
462
  if (settings::run_mode == RunMode::EIGENVALUE) {
489✔
463
    show_time("Time synchronizing fission bank", time_bank.elapsed(), 1);
284✔
464
    show_time("Sampling source sites", time_bank_sample.elapsed(), 2);
284✔
465
    show_time("SEND/RECV source sites", time_bank_sendrecv.elapsed(), 2);
284✔
466
  }
467
  show_time("Time accumulating tallies", time_tallies.elapsed(), 1);
489✔
468
  show_time("Time writing statepoints", time_statepoint.elapsed(), 1);
489✔
469
  show_time("Total time for finalization", time_finalize.elapsed());
489✔
470
  show_time("Total time elapsed", time_total.elapsed());
489✔
471

472
  // Calculate particle rate in active/inactive batches
473
  int n_active = simulation::current_batch - settings::n_inactive;
489✔
474
  double speed_inactive = 0.0;
489✔
475
  double speed_active;
489✔
476
  if (settings::restart_run) {
489✔
477
    if (simulation::restart_batch < settings::n_inactive) {
3!
478
      speed_inactive = (settings::n_particles *
×
479
                         (settings::n_inactive - simulation::restart_batch) *
×
480
                         settings::gen_per_batch) /
×
481
                       time_inactive.elapsed();
×
482
      speed_active =
×
483
        (settings::n_particles * n_active * settings::gen_per_batch) /
×
484
        time_active.elapsed();
×
485
    } else {
486
      speed_active = (settings::n_particles *
3✔
487
                       (settings::n_batches - simulation::restart_batch) *
3✔
488
                       settings::gen_per_batch) /
3✔
489
                     time_active.elapsed();
3✔
490
    }
491
  } else {
492
    if (settings::n_inactive > 0) {
486✔
493
      speed_inactive = (settings::n_particles * settings::n_inactive *
204✔
494
                         settings::gen_per_batch) /
204✔
495
                       time_inactive.elapsed();
204✔
496
    }
497
    speed_active =
486✔
498
      (settings::n_particles * n_active * settings::gen_per_batch) /
486✔
499
      time_active.elapsed();
486✔
500
  }
501

502
  // display calculation rate
503
  if (!(settings::restart_run &&
489✔
504
        (simulation::restart_batch >= settings::n_inactive)) &&
3!
505
      settings::n_inactive > 0) {
486✔
506
    show_rate("Calculation Rate (inactive)", speed_inactive);
204✔
507
  }
508
  show_rate("Calculation Rate (active)", speed_active);
489✔
509
}
510

511
//==============================================================================
512

513
std::pair<double, double> mean_stdev(const double* x, int n)
1,231,899✔
514
{
515
  double mean = x[static_cast<int>(TallyResult::SUM)] / n;
1,231,899✔
516
  double stdev =
1,231,899✔
517
    n > 1 ? std::sqrt(std::max(0.0,
2,463,052✔
518
              (x[static_cast<int>(TallyResult::SUM_SQ)] / n - mean * mean) /
2,463,052✔
519
                (n - 1)))
1,231,153✔
520
          : 0.0;
1,231,899✔
521
  return {mean, stdev};
1,231,899✔
522
}
523

524
//==============================================================================
525

526
void print_results()
489✔
527
{
528
  // display header block for results
529
  header("Results", 4);
489✔
530
  if (settings::verbosity < 4)
489!
531
    return;
×
532

533
  // Calculate t-value for confidence intervals
534
  int n = simulation::n_realizations;
489✔
535
  double alpha, t_n1, t_n3;
489✔
536
  if (settings::confidence_intervals) {
489✔
537
    alpha = 1.0 - CONFIDENCE_LEVEL;
1✔
538
    t_n1 = t_percentile(1.0 - alpha / 2.0, n - 1);
1✔
539
    t_n3 = t_percentile(1.0 - alpha / 2.0, n - 3);
1✔
540
  } else {
541
    t_n1 = 1.0;
542
    t_n3 = 1.0;
543
  }
544

545
  // write global tallies
546
  const auto& gt = simulation::global_tallies;
489✔
547
  double mean, stdev;
489✔
548
  if (n > 1) {
489✔
549
    if (settings::run_mode == RunMode::EIGENVALUE) {
462✔
550
      std::tie(mean, stdev) = mean_stdev(&gt(GlobalTally::K_COLLISION, 0), n);
281✔
551
      fmt::print(" k-effective (Collision)     = {:.5f} +/- {:.5f}\n", mean,
562✔
552
        t_n1 * stdev);
281✔
553
      std::tie(mean, stdev) = mean_stdev(&gt(GlobalTally::K_TRACKLENGTH, 0), n);
281✔
554
      fmt::print(" k-effective (Track-length)  = {:.5f} +/- {:.5f}\n", mean,
562✔
555
        t_n1 * stdev);
281✔
556
      std::tie(mean, stdev) = mean_stdev(&gt(GlobalTally::K_ABSORPTION, 0), n);
281✔
557
      fmt::print(" k-effective (Absorption)    = {:.5f} +/- {:.5f}\n", mean,
562✔
558
        t_n1 * stdev);
281✔
559
      if (n > 3) {
281✔
560
        double k_combined[2];
275✔
561
        openmc_get_keff(k_combined);
275✔
562
        fmt::print(" Combined k-effective        = {:.5f} +/- {:.5f}\n",
275✔
563
          k_combined[0], k_combined[1]);
564
      }
565
    }
566
    std::tie(mean, stdev) = mean_stdev(&gt(GlobalTally::LEAKAGE, 0), n);
462✔
567
    fmt::print(
924✔
568
      " Leakage Fraction            = {:.5f} +/- {:.5f}\n", mean, t_n1 * stdev);
462✔
569
  } else {
570
    if (mpi::master)
27!
571
      warning("Could not compute uncertainties -- only one "
54✔
572
              "active batch simulated!");
573

574
    if (settings::run_mode == RunMode::EIGENVALUE) {
27✔
575
      fmt::print(" k-effective (Collision)    = {:.5f}\n",
6✔
576
        gt(GlobalTally::K_COLLISION, TallyResult::SUM) / n);
3✔
577
      fmt::print(" k-effective (Track-length) = {:.5f}\n",
6✔
578
        gt(GlobalTally::K_TRACKLENGTH, TallyResult::SUM) / n);
3✔
579
      fmt::print(" k-effective (Absorption)   = {:.5f}\n",
6✔
580
        gt(GlobalTally::K_ABSORPTION, TallyResult::SUM) / n);
3✔
581
    }
582
    fmt::print(" Leakage Fraction           = {:.5f}\n",
54✔
583
      gt(GlobalTally::LEAKAGE, TallyResult::SUM) / n);
27✔
584
  }
585
  fmt::print("\n");
489✔
586
  std::fflush(stdout);
489✔
587
}
588

589
//==============================================================================
590

591
const std::unordered_map<int, const char*> score_names = {
592
  {SCORE_FLUX, "Flux"},
593
  {SCORE_TOTAL, "Total Reaction Rate"},
594
  {SCORE_SCATTER, "Scattering Rate"},
595
  {SCORE_NU_SCATTER, "Scattering Production Rate"},
596
  {SCORE_ABSORPTION, "Absorption Rate"},
597
  {SCORE_FISSION, "Fission Rate"},
598
  {SCORE_NU_FISSION, "Nu-Fission Rate"},
599
  {SCORE_KAPPA_FISSION, "Kappa-Fission Rate"},
600
  {SCORE_EVENTS, "Events"},
601
  {SCORE_DECAY_RATE, "Decay Rate"},
602
  {SCORE_DELAYED_NU_FISSION, "Delayed-Nu-Fission Rate"},
603
  {SCORE_PROMPT_NU_FISSION, "Prompt-Nu-Fission Rate"},
604
  {SCORE_INVERSE_VELOCITY, "Flux-Weighted Inverse Velocity"},
605
  {SCORE_FISS_Q_PROMPT, "Prompt fission power"},
606
  {SCORE_FISS_Q_RECOV, "Recoverable fission power"},
607
  {SCORE_CURRENT, "Current"},
608
  {SCORE_PULSE_HEIGHT, "pulse-height"},
609
  {SCORE_IFP_TIME_NUM, "IFP lifetime numerator"},
610
  {SCORE_IFP_BETA_NUM, "IFP delayed fraction numerator"},
611
  {SCORE_IFP_DENOM, "IFP common denominator"},
612
};
613

614
//! Create an ASCII output file showing all tally results.
615

616
void write_tallies()
534✔
617
{
618
  if (model::tallies.empty())
534✔
619
    return;
185✔
620

621
  // Set filename for tallies_out
622
  std::string filename = fmt::format("{}tallies.out", settings::path_output);
349✔
623

624
  // Open the tallies.out file.
625
  std::ofstream tallies_out;
349✔
626
  tallies_out.open(filename, std::ios::out | std::ios::trunc);
349✔
627

628
  // Loop over each tally.
629
  for (auto i_tally = 0; i_tally < model::tallies.size(); ++i_tally) {
2,091✔
630
    const auto& tally {*model::tallies[i_tally]};
1,742✔
631

632
    // Write header block.
633
    std::string tally_header("TALLY " + std::to_string(tally.id_));
1,742✔
634
    if (!tally.name_.empty())
1,742✔
635
      tally_header += ": " + tally.name_;
380✔
636
    fmt::print(tallies_out, "{}\n\n", header(tally_header));
1,742✔
637

638
    if (!tally.writable_) {
1,742✔
639
      fmt::print(tallies_out, " Internal\n\n");
38✔
640
      continue;
38✔
641
    }
642

643
    // Calculate t-value for confidence intervals
644
    double t_value = 1;
1,704✔
645
    if (settings::confidence_intervals) {
1,704✔
646
      auto alpha = 1 - CONFIDENCE_LEVEL;
1✔
647
      t_value = t_percentile(1 - alpha * 0.5, tally.n_realizations_ - 1);
1✔
648
    }
649

650
    // Write derivative information.
651
    if (tally.deriv_ != C_NONE) {
1,704✔
652
      const auto& deriv {model::tally_derivs[tally.deriv_]};
20!
653
      switch (deriv.variable) {
20!
654
      case DerivativeVariable::DENSITY:
8✔
655
        fmt::print(tallies_out, " Density derivative Material {}\n",
16✔
656
          deriv.diff_material);
8✔
657
        break;
8✔
658
      case DerivativeVariable::NUCLIDE_DENSITY:
8✔
659
        fmt::print(tallies_out,
16✔
660
          " Nuclide density derivative Material {} Nuclide {}\n",
661
          deriv.diff_material, data::nuclides[deriv.diff_nuclide]->name_);
8✔
662
        break;
8✔
663
      case DerivativeVariable::TEMPERATURE:
4✔
664
        fmt::print(tallies_out, " Temperature derivative Material {}\n",
8✔
665
          deriv.diff_material);
4✔
666
        break;
4✔
667
      default:
×
668
        fatal_error(fmt::format("Differential tally dependent variable for "
×
669
                                "tally {} not defined in output.cpp",
670
          tally.id_));
×
671
      }
672
    }
673

674
    // Initialize Filter Matches Object
675
    vector<FilterMatch> filter_matches;
3,408✔
676
    // Allocate space for tally filter matches
677
    filter_matches.resize(model::tally_filters.size());
1,704✔
678

679
    // Loop over all filter bin combinations.
680
    auto filter_iter = FilterBinIter(tally, false, &filter_matches);
1,704✔
681
    auto end = FilterBinIter(tally, true, &filter_matches);
1,704✔
682
    for (; filter_iter != end; ++filter_iter) {
841,207✔
683
      auto filter_index = filter_iter.index_;
684

685
      // Print info about this combination of filter bins.  The stride check
686
      // prevents redundant output.
687
      int indent = 0;
688
      for (auto i = 0; i < tally.filters().size(); ++i) {
2,671,482✔
689
        if (filter_index % tally.strides(i) == 0) {
1,831,979✔
690
          auto i_filt = tally.filters(i);
1,273,794✔
691
          const auto& filt {*model::tally_filters[i_filt]};
1,273,794✔
692
          auto& match {filter_matches[i_filt]};
1,273,794✔
693
          fmt::print(tallies_out, "{0:{1}}{2}\n", "", indent + 1,
1,273,794✔
694
            filt.text_label(match.i_bin_));
2,547,588✔
695
        }
696
        indent += 2;
1,831,979✔
697
      }
698

699
      // Loop over all nuclide and score combinations.
700
      int score_index = 0;
839,503✔
701
      for (auto i_nuclide : tally.nuclides_) {
1,808,781✔
702
        // Write label for this nuclide bin.
703
        if (i_nuclide == -1) {
969,278✔
704
          fmt::print(tallies_out, "{0:{1}}Total Material\n", "", indent + 1);
711,451✔
705
        } else {
706
          if (settings::run_CE) {
257,827✔
707
            fmt::print(tallies_out, "{0:{1}}{2}\n", "", indent + 1,
257,793✔
708
              data::nuclides[i_nuclide]->name_);
257,793✔
709
          } else {
710
            fmt::print(tallies_out, "{0:{1}}{2}\n", "", indent + 1,
34✔
711
              data::mg.nuclides_[i_nuclide].name);
34✔
712
          }
713
        }
714

715
        // Write the score, mean, and uncertainty.
716
        indent += 2;
969,278✔
717
        for (auto score : tally.scores_) {
2,199,872✔
718
          std::string score_name =
1,230,594✔
719
            score > 0 ? reaction_name(score) : score_names.at(score);
1,230,594✔
720
          double mean, stdev;
1,230,594✔
721
          std::tie(mean, stdev) =
1,230,594✔
722
            mean_stdev(&tally.results_(filter_index, score_index, 0),
2,461,188✔
723
              tally.n_realizations_);
1,230,594✔
724
          fmt::print(tallies_out, "{0:{1}}{2:<36} {3:.6} +/- {4:.6}\n", "",
2,461,188✔
725
            indent + 1, score_name, mean, t_value * stdev);
1,230,594✔
726
          score_index += 1;
1,230,594✔
727
        }
1,230,594✔
728
        indent -= 2;
969,278✔
729
      }
730
    }
731
  }
1,742✔
732
}
349✔
733

734
} // namespace openmc
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc