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

openmc-dev / openmc / 18542382223

15 Oct 2025 08:54PM UTC coverage: 81.921% (-3.3%) from 85.204%
18542382223

Pull #3404

github

web-flow
Merge 578501b17 into e9077b137
Pull Request #3404: Ability to source electron/positrons directly

16594 of 23101 branches covered (71.83%)

Branch coverage included in aggregate %.

16 of 19 new or added lines in 4 files covered. (84.21%)

2215 existing lines in 79 files now uncovered.

53716 of 62726 relevant lines covered (85.64%)

43526789.79 hits per line

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

75.28
/src/tallies/tally.cpp
1
#include "openmc/tallies/tally.h"
2

3
#include "openmc/array.h"
4
#include "openmc/capi.h"
5
#include "openmc/constants.h"
6
#include "openmc/container_util.h"
7
#include "openmc/error.h"
8
#include "openmc/file_utils.h"
9
#include "openmc/mesh.h"
10
#include "openmc/message_passing.h"
11
#include "openmc/mgxs_interface.h"
12
#include "openmc/nuclide.h"
13
#include "openmc/particle.h"
14
#include "openmc/reaction.h"
15
#include "openmc/reaction_product.h"
16
#include "openmc/settings.h"
17
#include "openmc/simulation.h"
18
#include "openmc/source.h"
19
#include "openmc/tallies/derivative.h"
20
#include "openmc/tallies/filter.h"
21
#include "openmc/tallies/filter_cell.h"
22
#include "openmc/tallies/filter_cellborn.h"
23
#include "openmc/tallies/filter_cellfrom.h"
24
#include "openmc/tallies/filter_collision.h"
25
#include "openmc/tallies/filter_delayedgroup.h"
26
#include "openmc/tallies/filter_energy.h"
27
#include "openmc/tallies/filter_legendre.h"
28
#include "openmc/tallies/filter_mesh.h"
29
#include "openmc/tallies/filter_meshborn.h"
30
#include "openmc/tallies/filter_meshmaterial.h"
31
#include "openmc/tallies/filter_meshsurface.h"
32
#include "openmc/tallies/filter_particle.h"
33
#include "openmc/tallies/filter_sph_harm.h"
34
#include "openmc/tallies/filter_surface.h"
35
#include "openmc/tallies/filter_time.h"
36
#include "openmc/xml_interface.h"
37

38
#include "xtensor/xadapt.hpp"
39
#include "xtensor/xbuilder.hpp" // for empty_like
40
#include "xtensor/xview.hpp"
41
#include <fmt/core.h>
42

43
#include <algorithm> // for max, set_union
44
#include <cassert>
45
#include <cstddef>  // for size_t
46
#include <iterator> // for back_inserter
47
#include <string>
48

49
namespace openmc {
50

51
//==============================================================================
52
// Global variable definitions
53
//==============================================================================
54

55
namespace model {
56
//! a mapping of tally ID to index in the tallies vector
57
std::unordered_map<int, int> tally_map;
58
vector<unique_ptr<Tally>> tallies;
59
vector<int> active_tallies;
60
vector<int> active_analog_tallies;
61
vector<int> active_tracklength_tallies;
62
vector<int> active_timed_tracklength_tallies;
63
vector<int> active_collision_tallies;
64
vector<int> active_meshsurf_tallies;
65
vector<int> active_surface_tallies;
66
vector<int> active_pulse_height_tallies;
67
vector<int> pulse_height_cells;
68
vector<double> time_grid;
69
} // namespace model
70

71
namespace simulation {
72
xt::xtensor_fixed<double, xt::xshape<N_GLOBAL_TALLIES, 3>> global_tallies;
73
int32_t n_realizations {0};
74
} // namespace simulation
75

76
double global_tally_absorption;
77
double global_tally_collision;
78
double global_tally_tracklength;
79
double global_tally_leakage;
80

81
//==============================================================================
82
// Tally object implementation
83
//==============================================================================
84

85
Tally::Tally(int32_t id)
1,692✔
86
{
87
  index_ = model::tallies.size(); // Avoids warning about narrowing
1,692✔
88
  this->set_id(id);
1,692✔
89
  this->set_filters({});
1,692✔
90
}
1,692✔
91

92
Tally::Tally(pugi::xml_node node)
24,820✔
93
{
94
  index_ = model::tallies.size(); // Avoids warning about narrowing
24,820✔
95

96
  // Copy and set tally id
97
  if (!check_for_node(node, "id")) {
24,820!
UNCOV
98
    throw std::runtime_error {"Must specify id for tally in tally XML file."};
×
99
  }
100
  int32_t id = std::stoi(get_node_value(node, "id"));
24,820✔
101
  this->set_id(id);
24,820✔
102

103
  if (check_for_node(node, "name"))
24,820✔
104
    name_ = get_node_value(node, "name");
2,601✔
105

106
  if (check_for_node(node, "multiply_density")) {
24,820✔
107
    multiply_density_ = get_node_value_bool(node, "multiply_density");
33✔
108
  }
109

110
  // =======================================================================
111
  // READ DATA FOR FILTERS
112

113
  // Check if user is using old XML format and throw an error if so
114
  if (check_for_node(node, "filter")) {
24,820!
115
    throw std::runtime_error {
×
116
      "Tally filters must be specified independently of "
117
      "tallies in a <filter> element. The <tally> element itself should "
118
      "have a list of filters that apply, e.g., <filters>1 2</filters> "
119
      "where 1 and 2 are the IDs of filters specified outside of "
UNCOV
120
      "<tally>."};
×
121
  }
122

123
  // Determine number of filters
124
  vector<int> filter_ids;
24,820✔
125
  if (check_for_node(node, "filters")) {
24,820✔
126
    filter_ids = get_node_array<int>(node, "filters");
23,731✔
127
  }
128

129
  // Allocate and store filter user ids
130
  vector<Filter*> filters;
24,820✔
131
  for (int filter_id : filter_ids) {
73,750✔
132
    // Determine if filter ID is valid
133
    auto it = model::filter_map.find(filter_id);
48,930✔
134
    if (it == model::filter_map.end()) {
48,930!
UNCOV
135
      throw std::runtime_error {fmt::format(
×
UNCOV
136
        "Could not find filter {} specified on tally {}", filter_id, id_)};
×
137
    }
138

139
    // Store the index of the filter
140
    filters.push_back(model::tally_filters[it->second].get());
48,930✔
141
  }
142

143
  // Set the filters
144
  this->set_filters(filters);
24,820✔
145

146
  // Check for the presence of certain filter types
147
  bool has_energyout = energyout_filter_ >= 0;
24,820✔
148
  int particle_filter_index = C_NONE;
24,820✔
149
  for (int64_t j = 0; j < filters_.size(); ++j) {
73,750✔
150
    int i_filter = filters_[j];
48,930✔
151
    const auto& f = model::tally_filters[i_filter].get();
48,930✔
152

153
    auto pf = dynamic_cast<ParticleFilter*>(f);
48,930!
154
    if (pf)
48,930✔
155
      particle_filter_index = i_filter;
449✔
156

157
    // Change the tally estimator if a filter demands it
158
    FilterType filt_type = f->type();
48,930✔
159
    if (filt_type == FilterType::ENERGY_OUT ||
48,930✔
160
        filt_type == FilterType::LEGENDRE) {
161
      estimator_ = TallyEstimator::ANALOG;
6,558✔
162
    } else if (filt_type == FilterType::SPHERICAL_HARMONICS) {
42,372✔
163
      auto sf = dynamic_cast<SphericalHarmonicsFilter*>(f);
70!
164
      if (sf->cosine() == SphericalHarmonicsCosine::scatter) {
70✔
165
        estimator_ = TallyEstimator::ANALOG;
11✔
166
      }
167
    } else if (filt_type == FilterType::SPATIAL_LEGENDRE ||
42,302✔
168
               filt_type == FilterType::ZERNIKE ||
42,238!
169
               filt_type == FilterType::ZERNIKE_RADIAL) {
170
      estimator_ = TallyEstimator::COLLISION;
64✔
171
    }
172
  }
173

174
  // =======================================================================
175
  // READ DATA FOR NUCLIDES
176

177
  this->set_nuclides(node);
24,820✔
178

179
  // =======================================================================
180
  // READ DATA FOR SCORES
181

182
  this->set_scores(node);
24,820✔
183

184
  if (!check_for_node(node, "scores")) {
24,820!
UNCOV
185
    fatal_error(fmt::format("No scores specified on tally {}.", id_));
×
186
  }
187

188
  // Set IFP if needed
189
  if (!settings::ifp_on) {
24,820✔
190
    // Determine if this tally has an IFP score
191
    bool has_ifp_score = false;
24,700✔
192
    for (int score : scores_) {
58,566✔
193
      if (score == SCORE_IFP_TIME_NUM || score == SCORE_IFP_BETA_NUM ||
33,960!
194
          score == SCORE_IFP_DENOM) {
195
        has_ifp_score = true;
94✔
196
        break;
94✔
197
      }
198
    }
199

200
    // Check for errors
201
    if (has_ifp_score) {
24,700✔
202
      if (settings::run_mode == RunMode::EIGENVALUE) {
94✔
203
        if (settings::ifp_n_generation < 0) {
85✔
204
          settings::ifp_n_generation = DEFAULT_IFP_N_GENERATION;
9✔
205
          warning(fmt::format(
9!
206
            "{} generations will be used for IFP (default value). It can be "
207
            "changed using the 'ifp_n_generation' settings.",
208
            settings::ifp_n_generation));
209
        }
210
        if (settings::ifp_n_generation > settings::n_inactive) {
85✔
211
          fatal_error("'ifp_n_generation' must be lower than or equal to the "
9✔
212
                      "number of inactive cycles.");
213
        }
214
        settings::ifp_on = true;
76✔
215
      } else {
216
        fatal_error(
9✔
217
          "Iterated Fission Probability can only be used in an eigenvalue "
218
          "calculation.");
219
      }
220
    }
221
  }
222

223
  // Set IFP parameters if needed
224
  if (settings::ifp_on) {
24,802✔
225
    for (int score : scores_) {
424✔
226
      switch (score) {
228!
227
      case SCORE_IFP_TIME_NUM:
76✔
228
        if (settings::ifp_parameter == IFPParameter::None) {
76!
229
          settings::ifp_parameter = IFPParameter::GenerationTime;
76✔
UNCOV
230
        } else if (settings::ifp_parameter == IFPParameter::BetaEffective) {
×
UNCOV
231
          settings::ifp_parameter = IFPParameter::Both;
×
232
        }
233
        break;
76✔
234
      case SCORE_IFP_BETA_NUM:
152✔
235
      case SCORE_IFP_DENOM:
236
        if (settings::ifp_parameter == IFPParameter::None) {
152!
UNCOV
237
          settings::ifp_parameter = IFPParameter::BetaEffective;
×
238
        } else if (settings::ifp_parameter == IFPParameter::GenerationTime) {
152✔
239
          settings::ifp_parameter = IFPParameter::Both;
76✔
240
        }
241
        break;
152✔
242
      }
243
    }
244
  }
245

246
  // Check if tally is compatible with particle type
247
  if (!settings::photon_transport) {
24,802✔
248
    for (int score : scores_) {
57,784✔
249
      switch (score) {
33,410!
UNCOV
250
      case SCORE_PULSE_HEIGHT:
×
UNCOV
251
        fatal_error("For pulse-height tallies, photon transport needs to be "
×
252
                    "activated.");
253
        break;
254
      }
255
    }
256
  }
257
  if (settings::photon_transport) {
24,802✔
258
    if (particle_filter_index == C_NONE) {
428✔
259
      for (int score : scores_) {
218✔
260
        switch (score) {
109!
UNCOV
261
        case SCORE_INVERSE_VELOCITY:
×
UNCOV
262
          fatal_error("Particle filter must be used with photon "
×
263
                      "transport on and inverse velocity score");
264
          break;
265
        case SCORE_FLUX:
44✔
266
        case SCORE_TOTAL:
267
        case SCORE_SCATTER:
268
        case SCORE_NU_SCATTER:
269
        case SCORE_ABSORPTION:
270
        case SCORE_FISSION:
271
        case SCORE_NU_FISSION:
272
        case SCORE_CURRENT:
273
        case SCORE_EVENTS:
274
        case SCORE_DELAYED_NU_FISSION:
275
        case SCORE_PROMPT_NU_FISSION:
276
        case SCORE_DECAY_RATE:
277
          warning("You are tallying the '" + reaction_name(score) +
44✔
278
                  "' score and haven't used a particle filter. This score will "
279
                  "include contributions from all particles.");
280
          break;
44✔
281
        }
282
      }
283
    }
284
  } else {
285
    if (particle_filter_index >= 0) {
24,374✔
286
      const auto& f = model::tally_filters[particle_filter_index].get();
130✔
287
      auto pf = dynamic_cast<ParticleFilter*>(f);
130!
288
      for (auto p : pf->particles()) {
390✔
289
        if (p != ParticleType::neutron) {
260✔
290
          warning(fmt::format(
130✔
291
            "Particle filter other than NEUTRON used with "
292
            "photon transport turned off. All tallies for particle type {}"
293
            " will have no scores",
294
            static_cast<int>(p)));
260✔
295
        }
296
      }
297
    }
298
  }
299

300
  // Check for a tally derivative.
301
  if (check_for_node(node, "derivative")) {
24,802✔
302
    int deriv_id = std::stoi(get_node_value(node, "derivative"));
320✔
303

304
    // Find the derivative with the given id, and store it's index.
305
    auto it = model::tally_deriv_map.find(deriv_id);
320✔
306
    if (it == model::tally_deriv_map.end()) {
320!
UNCOV
307
      fatal_error(fmt::format(
×
UNCOV
308
        "Could not find derivative {} specified on tally {}", deriv_id, id_));
×
309
    }
310

311
    deriv_ = it->second;
320✔
312

313
    // Only analog or collision estimators are supported for differential
314
    // tallies.
315
    if (estimator_ == TallyEstimator::TRACKLENGTH) {
320✔
316
      estimator_ = TallyEstimator::COLLISION;
240✔
317
    }
318

319
    const auto& deriv = model::tally_derivs[deriv_];
320✔
320
    if (deriv.variable == DerivativeVariable::NUCLIDE_DENSITY ||
320✔
321
        deriv.variable == DerivativeVariable::TEMPERATURE) {
192✔
322
      for (int i_nuc : nuclides_) {
432✔
323
        if (has_energyout && i_nuc == -1) {
240!
324
          fatal_error(fmt::format(
×
325
            "Error on tally {}: Cannot use a "
326
            "'nuclide_density' or 'temperature' derivative on a tally with "
327
            "an "
328
            "outgoing energy filter and 'total' nuclide rate. Instead, tally "
329
            "each nuclide in the material individually.",
UNCOV
330
            id_));
×
331
          // Note that diff tallies with these characteristics would work
332
          // correctly if no tally events occur in the perturbed material
333
          // (e.g. pertrubing moderator but only tallying fuel), but this
334
          // case would be hard to check for by only reading inputs.
335
        }
336
      }
337
    }
338
  }
339

340
  // If settings.xml trigger is turned on, create tally triggers
341
  if (settings::trigger_on) {
24,802✔
342
    this->init_triggers(node);
183✔
343
  }
344

345
  // =======================================================================
346
  // SET TALLY ESTIMATOR
347

348
  // Check if user specified estimator
349
  if (check_for_node(node, "estimator")) {
24,802✔
350
    std::string est = get_node_value(node, "estimator");
19,809✔
351
    if (est == "analog") {
19,809✔
352
      estimator_ = TallyEstimator::ANALOG;
8,143✔
353
    } else if (est == "tracklength" || est == "track-length" ||
12,494!
354
               est == "pathlength" || est == "path-length") {
12,494!
355
      // If the estimator was set to an analog estimator, this means the
356
      // tally needs post-collision information
357
      if (estimator_ == TallyEstimator::ANALOG ||
11,252!
358
          estimator_ == TallyEstimator::COLLISION) {
11,252!
UNCOV
359
        throw std::runtime_error {fmt::format("Cannot use track-length "
×
360
                                              "estimator for tally {}",
UNCOV
361
          id_)};
×
362
      }
363

364
      // Set estimator to track-length estimator
365
      estimator_ = TallyEstimator::TRACKLENGTH;
11,252✔
366

367
    } else if (est == "collision") {
414!
368
      // If the estimator was set to an analog estimator, this means the
369
      // tally needs post-collision information
370
      if (estimator_ == TallyEstimator::ANALOG) {
414!
UNCOV
371
        throw std::runtime_error {fmt::format("Cannot use collision estimator "
×
372
                                              "for tally ",
UNCOV
373
          id_)};
×
374
      }
375

376
      // Set estimator to collision estimator
377
      estimator_ = TallyEstimator::COLLISION;
414✔
378

379
    } else {
UNCOV
380
      throw std::runtime_error {
×
UNCOV
381
        fmt::format("Invalid estimator '{}' on tally {}", est, id_)};
×
382
    }
383
  }
19,809✔
384

385
#ifdef OPENMC_LIBMESH_ENABLED
386
  // ensure a tracklength tally isn't used with a libMesh filter
387
  for (auto i : this->filters_) {
13,755✔
388
    auto df = dynamic_cast<MeshFilter*>(model::tally_filters[i].get());
9,130!
389
    if (df) {
9,130✔
390
      auto lm = dynamic_cast<LibMesh*>(model::meshes[df->mesh()].get());
1,094!
391
      if (lm && estimator_ == TallyEstimator::TRACKLENGTH) {
1,094!
392
        fatal_error("A tracklength estimator cannot be used with "
393
                    "an unstructured LibMesh tally.");
394
      }
395
    }
396
  }
397
#endif
398
}
24,802✔
399

400
Tally::~Tally()
26,494✔
401
{
402
  model::tally_map.erase(id_);
26,494✔
403
}
26,494✔
404

405
Tally* Tally::create(int32_t id)
109✔
406
{
407
  model::tallies.push_back(make_unique<Tally>(id));
109✔
408
  return model::tallies.back().get();
109✔
409
}
410

411
void Tally::set_id(int32_t id)
28,094✔
412
{
413
  assert(id >= 0 || id == C_NONE);
22,893!
414

415
  // Clear entry in tally map if an ID was already assigned before
416
  if (id_ != C_NONE) {
28,094✔
417
    model::tally_map.erase(id_);
1,582✔
418
    id_ = C_NONE;
1,582✔
419
  }
420

421
  // Make sure no other tally has the same ID
422
  if (model::tally_map.find(id) != model::tally_map.end()) {
28,094!
UNCOV
423
    throw std::runtime_error {
×
UNCOV
424
      fmt::format("Two tallies have the same ID: {}", id)};
×
425
  }
426

427
  // If no ID specified, auto-assign next ID in sequence
428
  if (id == C_NONE) {
28,094✔
429
    id = 0;
1,692✔
430
    for (const auto& t : model::tallies) {
5,413✔
431
      id = std::max(id, t->id_);
3,721✔
432
    }
433
    ++id;
1,692✔
434
  }
435

436
  // Update ID and entry in tally map
437
  id_ = id;
28,094✔
438
  model::tally_map[id] = index_;
28,094✔
439
}
28,094✔
440

441
std::vector<FilterType> Tally::filter_types() const
274✔
442
{
443
  std::vector<FilterType> filter_types;
274✔
444
  for (auto idx : this->filters())
1,044✔
445
    filter_types.push_back(model::tally_filters[idx]->type());
770✔
446
  return filter_types;
274✔
UNCOV
447
}
×
448

449
std::unordered_map<FilterType, int32_t> Tally::filter_indices() const
274✔
450
{
451
  std::unordered_map<FilterType, int32_t> filter_indices;
274✔
452
  for (int i = 0; i < this->filters().size(); i++) {
1,044✔
453
    const auto& f = model::tally_filters[this->filters(i)];
770✔
454

455
    filter_indices[f->type()] = i;
770✔
456
  }
457
  return filter_indices;
274✔
UNCOV
458
}
×
459

460
bool Tally::has_filter(FilterType filter_type) const
822✔
461
{
462
  for (auto idx : this->filters()) {
1,971✔
463
    if (model::tally_filters[idx]->type() == filter_type)
1,893✔
464
      return true;
744✔
465
  }
466
  return false;
78✔
467
}
468

469
void Tally::set_filters(span<Filter*> filters)
28,322✔
470
{
471
  // Clear old data.
472
  filters_.clear();
28,322✔
473
  strides_.clear();
28,322✔
474

475
  // Copy in the given filter indices.
476
  auto n = filters.size();
28,322✔
477
  filters_.reserve(n);
28,322✔
478

479
  for (auto* filter : filters) {
79,837✔
480
    add_filter(filter);
51,515✔
481
  }
482
}
28,322✔
483

484
void Tally::add_filter(Filter* filter)
51,794✔
485
{
486
  int32_t filter_idx = model::filter_map.at(filter->id());
51,794✔
487
  // if this filter is already present, do nothing and return
488
  if (std::find(filters_.begin(), filters_.end(), filter_idx) != filters_.end())
51,794✔
489
    return;
22✔
490

491
  // Keep track of indices for special filters
492
  if (filter->type() == FilterType::ENERGY_OUT) {
51,772✔
493
    energyout_filter_ = filters_.size();
4,719✔
494
  } else if (filter->type() == FilterType::DELAYED_GROUP) {
47,053✔
495
    delayedgroup_filter_ = filters_.size();
838✔
496
  }
497
  filters_.push_back(filter_idx);
51,772✔
498
}
499

500
void Tally::set_strides()
26,990✔
501
{
502
  // Set the strides.  Filters are traversed in reverse so that the last
503
  // filter has the shortest stride in memory and the first filter has the
504
  // longest stride.
505
  auto n = filters_.size();
26,990✔
506
  strides_.resize(n, 0);
26,990✔
507
  int stride = 1;
26,990✔
508
  for (int i = n - 1; i >= 0; --i) {
78,957✔
509
    strides_[i] = stride;
51,967✔
510
    stride *= model::tally_filters[filters_[i]]->n_bins();
51,967✔
511
  }
512
  n_filter_bins_ = stride;
26,990✔
513
}
26,990✔
514

515
void Tally::set_scores(pugi::xml_node node)
24,820✔
516
{
517
  if (!check_for_node(node, "scores"))
24,820!
UNCOV
518
    fatal_error(fmt::format("No scores specified on tally {}", id_));
×
519

520
  auto scores = get_node_array<std::string>(node, "scores");
24,820✔
521
  set_scores(scores);
24,820✔
522
}
24,820✔
523

524
void Tally::set_scores(const vector<std::string>& scores)
26,515✔
525
{
526
  // Reset state and prepare for the new scores.
527
  scores_.clear();
26,515✔
528
  scores_.reserve(scores.size());
26,515✔
529

530
  // Check for the presence of certain restrictive filters.
531
  bool energyout_present = energyout_filter_ != C_NONE;
26,515✔
532
  bool legendre_present = false;
26,515✔
533
  bool cell_present = false;
26,515✔
534
  bool cellfrom_present = false;
26,515✔
535
  bool surface_present = false;
26,515✔
536
  bool meshsurface_present = false;
26,515✔
537
  bool non_cell_energy_present = false;
26,515✔
538
  for (auto i_filt : filters_) {
76,861✔
539
    const auto* filt {model::tally_filters[i_filt].get()};
50,346✔
540
    // Checking for only cell and energy filters for pulse-height tally
541
    if (!(filt->type() == FilterType::CELL ||
99,594✔
542
          filt->type() == FilterType::ENERGY)) {
49,248✔
543
      non_cell_energy_present = true;
32,171✔
544
    }
545
    if (filt->type() == FilterType::LEGENDRE) {
50,346✔
546
      legendre_present = true;
2,060✔
547
    } else if (filt->type() == FilterType::CELLFROM) {
48,286✔
548
      cellfrom_present = true;
304✔
549
    } else if (filt->type() == FilterType::CELL) {
47,982✔
550
      cell_present = true;
1,098✔
551
    } else if (filt->type() == FilterType::SURFACE) {
46,884✔
552
      surface_present = true;
193✔
553
    } else if (filt->type() == FilterType::MESH_SURFACE) {
46,691✔
554
      meshsurface_present = true;
382✔
555
    }
556
  }
557

558
  // Iterate over the given scores.
559
  for (auto score_str : scores) {
62,968✔
560
    // Make sure a delayed group filter wasn't used with an incompatible
561
    // score.
562
    if (delayedgroup_filter_ != C_NONE) {
36,453✔
563
      if (score_str != "delayed-nu-fission" && score_str != "decay-rate" &&
892!
564
          score_str != "ifp-beta-numerator")
38✔
UNCOV
565
        fatal_error("Cannot tally " + score_str + "with a delayedgroup filter");
×
566
    }
567

568
    // Determine integer code for score
569
    int score = reaction_type(score_str);
36,453✔
570

571
    switch (score) {
36,453✔
572
    case SCORE_FLUX:
11,103✔
573
      if (!nuclides_.empty())
11,103!
574
        if (!(nuclides_.size() == 1 && nuclides_[0] == -1))
11,103!
UNCOV
575
          fatal_error("Cannot tally flux for an individual nuclide.");
×
576
      if (energyout_present)
11,103!
UNCOV
577
        fatal_error("Cannot tally flux with an outgoing energy filter.");
×
578
      break;
11,103✔
579

580
    case SCORE_TOTAL:
6,982✔
581
    case SCORE_ABSORPTION:
582
    case SCORE_FISSION:
583
      if (energyout_present)
6,982!
UNCOV
584
        fatal_error("Cannot tally " + score_str +
×
585
                    " reaction rate with an "
586
                    "outgoing energy filter");
587
      break;
6,982✔
588

589
    case SCORE_SCATTER:
3,915✔
590
      if (legendre_present)
3,915✔
591
        estimator_ = TallyEstimator::ANALOG;
1,377✔
592
    case SCORE_NU_FISSION:
593
    case SCORE_DELAYED_NU_FISSION:
594
    case SCORE_PROMPT_NU_FISSION:
595
      if (energyout_present)
8,672✔
596
        estimator_ = TallyEstimator::ANALOG;
3,573✔
597
      break;
8,672✔
598

599
    case SCORE_NU_SCATTER:
2,114✔
600
      if (settings::run_CE) {
2,114✔
601
        estimator_ = TallyEstimator::ANALOG;
1,986✔
602
      } else {
603
        if (energyout_present || legendre_present)
128!
604
          estimator_ = TallyEstimator::ANALOG;
64✔
605
      }
606
      break;
2,114✔
607

608
    case SCORE_CURRENT:
607✔
609
      // Check which type of current is desired: mesh or surface currents.
610
      if (surface_present || cell_present || cellfrom_present) {
607!
611
        if (meshsurface_present)
225!
UNCOV
612
          fatal_error("Cannot tally mesh surface currents in the same tally as "
×
613
                      "normal surface currents");
614
        type_ = TallyType::SURFACE;
225✔
615
        estimator_ = TallyEstimator::ANALOG;
225✔
616
      } else if (meshsurface_present) {
382!
617
        type_ = TallyType::MESH_SURFACE;
382✔
618
      } else {
UNCOV
619
        fatal_error("Cannot tally currents without surface type filters");
×
620
      }
621
      break;
607✔
622

623
    case HEATING:
370✔
624
      if (settings::photon_transport)
370✔
625
        estimator_ = TallyEstimator::COLLISION;
199✔
626
      break;
370✔
627

628
    case SCORE_PULSE_HEIGHT:
16✔
629
      if (non_cell_energy_present) {
16!
UNCOV
630
        fatal_error("Pulse-height tallies are not compatible with filters "
×
631
                    "other than CellFilter and EnergyFilter");
632
      }
633
      type_ = TallyType::PULSE_HEIGHT;
16✔
634

635
      // Collecting indices of all cells covered by the filters in the pulse
636
      // height tally in global variable pulse_height_cells
637
      for (const auto& i_filt : filters_) {
48✔
638
        auto cell_filter =
639
          dynamic_cast<CellFilter*>(model::tally_filters[i_filt].get());
32!
640
        if (cell_filter) {
32✔
641
          const auto& cells = cell_filter->cells();
16✔
642
          for (int i = 0; i < cell_filter->n_bins(); i++) {
32✔
643
            int cell_index = cells[i];
16✔
644
            if (!contains(model::pulse_height_cells, cell_index)) {
16!
645
              model::pulse_height_cells.push_back(cell_index);
16✔
646
            }
647
          }
648
        }
649
      }
650
      break;
16✔
651

652
    case SCORE_IFP_TIME_NUM:
282✔
653
    case SCORE_IFP_BETA_NUM:
654
    case SCORE_IFP_DENOM:
655
      estimator_ = TallyEstimator::COLLISION;
282✔
656
      break;
282✔
657
    }
658

659
    scores_.push_back(score);
36,453✔
660
  }
36,453✔
661

662
  // Make sure that no duplicate scores exist.
663
  for (auto it1 = scores_.begin(); it1 != scores_.end(); ++it1) {
62,968✔
664
    for (auto it2 = it1 + 1; it2 != scores_.end(); ++it2) {
83,888✔
665
      if (*it1 == *it2)
47,435!
UNCOV
666
        fatal_error(
×
UNCOV
667
          fmt::format("Duplicate score of type \"{}\" found in tally {}",
×
668
            reaction_name(*it1), id_));
×
669
    }
670
  }
671

672
  // Make sure all scores are compatible with multigroup mode.
673
  if (!settings::run_CE) {
26,515✔
674
    for (auto sc : scores_)
6,254✔
675
      if (sc > 0)
4,599!
676
        fatal_error("Cannot tally " + reaction_name(sc) +
×
677
                    " reaction rate "
678
                    "in multi-group mode");
679
  }
680

681
  // Make sure current scores are not mixed in with volumetric scores.
682
  if (type_ == TallyType::SURFACE || type_ == TallyType::MESH_SURFACE) {
26,515✔
683
    if (scores_.size() != 1)
607!
UNCOV
684
      fatal_error("Cannot tally other scores in the same tally as surface "
×
685
                  "currents.");
686
  }
687
  if ((surface_present || meshsurface_present) && scores_[0] != SCORE_CURRENT)
26,515!
UNCOV
688
    fatal_error("Cannot tally score other than 'current' when using a surface "
×
689
                "or mesh-surface filter.");
690
}
26,515✔
691

692
void Tally::set_nuclides(pugi::xml_node node)
24,820✔
693
{
694
  nuclides_.clear();
24,820✔
695

696
  // By default, we tally just the total material rates.
697
  if (!check_for_node(node, "nuclides")) {
24,820✔
698
    nuclides_.push_back(-1);
6,520✔
699
    return;
6,520✔
700
  }
701

702
  // The user provided specifics nuclides.  Parse it as an array with either
703
  // "total" or a nuclide name like "U235" in each position.
704
  auto words = get_node_array<std::string>(node, "nuclides");
18,300✔
705
  this->set_nuclides(words);
18,300✔
706
}
18,300✔
707

708
void Tally::set_nuclides(const vector<std::string>& nuclides)
18,300✔
709
{
710
  nuclides_.clear();
18,300✔
711

712
  for (const auto& nuc : nuclides) {
47,301✔
713
    if (nuc == "total") {
29,001✔
714
      nuclides_.push_back(-1);
14,768✔
715
    } else {
716
      auto search = data::nuclide_map.find(nuc);
14,233✔
717
      if (search == data::nuclide_map.end()) {
14,233✔
718
        int err = openmc_load_nuclide(nuc.c_str(), nullptr, 0);
33✔
719
        if (err < 0)
33!
UNCOV
720
          throw std::runtime_error {openmc_err_msg};
×
721
      }
722
      nuclides_.push_back(data::nuclide_map.at(nuc));
14,233✔
723
    }
724
  }
725
}
18,300✔
726

727
void Tally::init_triggers(pugi::xml_node node)
183✔
728
{
729
  for (auto trigger_node : node.children("trigger")) {
254✔
730
    // Read the trigger type.
731
    TriggerMetric metric;
732
    if (check_for_node(trigger_node, "type")) {
71!
733
      auto type_str = get_node_value(trigger_node, "type");
71✔
734
      if (type_str == "std_dev") {
71!
UNCOV
735
        metric = TriggerMetric::standard_deviation;
×
736
      } else if (type_str == "variance") {
71!
737
        metric = TriggerMetric::variance;
×
738
      } else if (type_str == "rel_err") {
71!
739
        metric = TriggerMetric::relative_error;
71✔
740
      } else {
UNCOV
741
        fatal_error(fmt::format(
×
UNCOV
742
          "Unknown trigger type \"{}\" in tally {}", type_str, id_));
×
743
      }
744
    } else {
71✔
UNCOV
745
      fatal_error(fmt::format(
×
746
        "Must specify trigger type for tally {} in tally XML file", id_));
×
747
    }
748

749
    // Read the trigger threshold.
750
    double threshold;
751
    if (check_for_node(trigger_node, "threshold")) {
71!
752
      threshold = std::stod(get_node_value(trigger_node, "threshold"));
71✔
753
      if (threshold <= 0) {
71!
UNCOV
754
        fatal_error("Tally trigger threshold must be positive");
×
755
      }
756
    } else {
UNCOV
757
      fatal_error(fmt::format(
×
UNCOV
758
        "Must specify trigger threshold for tally {} in tally XML file", id_));
×
759
    }
760

761
    // Read whether to allow zero-tally bins to be ignored.
762
    bool ignore_zeros = false;
71✔
763
    if (check_for_node(trigger_node, "ignore_zeros")) {
71✔
764
      ignore_zeros = get_node_value_bool(trigger_node, "ignore_zeros");
11✔
765
    }
766

767
    // Read the trigger scores.
768
    vector<std::string> trigger_scores;
71✔
769
    if (check_for_node(trigger_node, "scores")) {
71!
770
      trigger_scores = get_node_array<std::string>(trigger_node, "scores");
71✔
771
    } else {
UNCOV
772
      trigger_scores.push_back("all");
×
773
    }
774

775
    // Parse the trigger scores and populate the triggers_ vector.
776
    for (auto score_str : trigger_scores) {
142✔
777
      if (score_str == "all") {
71✔
778
        triggers_.reserve(triggers_.size() + this->scores_.size());
16✔
779
        for (auto i_score = 0; i_score < this->scores_.size(); ++i_score) {
80✔
780
          triggers_.push_back({metric, threshold, ignore_zeros, i_score});
64✔
781
        }
782
      } else {
783
        int i_score = 0;
55✔
784
        for (; i_score < this->scores_.size(); ++i_score) {
77!
785
          if (this->scores_[i_score] == reaction_type(score_str))
77✔
786
            break;
55✔
787
        }
788
        if (i_score == this->scores_.size()) {
55!
UNCOV
789
          fatal_error(
×
UNCOV
790
            fmt::format("Could not find the score \"{}\" in tally "
×
791
                        "{} but it was listed in a trigger on that tally",
UNCOV
792
              score_str, id_));
×
793
        }
794
        triggers_.push_back({metric, threshold, ignore_zeros, i_score});
55✔
795
      }
796
    }
71✔
797
  }
71✔
798
}
183✔
799

800
void Tally::init_results()
26,990✔
801
{
802
  int n_scores = scores_.size() * nuclides_.size();
26,990✔
803
  results_ = xt::empty<double>({n_filter_bins_, n_scores, 3});
26,990✔
804
}
26,990✔
805

806
void Tally::reset()
58,709✔
807
{
808
  n_realizations_ = 0;
58,709✔
809
  if (results_.size() != 0) {
58,709✔
810
    xt::view(results_, xt::all()) = 0.0;
57,104✔
811
  }
812
}
58,709✔
813

814
void Tally::accumulate()
217,302✔
815
{
816
  // Increment number of realizations
817
  n_realizations_ += settings::reduce_tallies ? 1 : mpi::n_procs;
217,302!
818

819
  if (mpi::master || !settings::reduce_tallies) {
217,302!
820
    // Calculate total source strength for normalization
821
    double total_source = 0.0;
179,392✔
822
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
179,392✔
823
      total_source = model::external_sources_probability.integral();
50,767✔
824
    } else {
825
      total_source = 1.0;
128,625✔
826
    }
827

828
    // Account for number of source particles in normalization
829
    double norm =
179,392✔
830
      total_source / (settings::n_particles * settings::gen_per_batch);
179,392✔
831

832
    if (settings::solver_type == SolverType::RANDOM_RAY) {
179,392✔
833
      norm = 1.0;
11,391✔
834
    }
835

836
// Accumulate each result
837
#pragma omp parallel for
94,974✔
838
    for (int i = 0; i < results_.shape()[0]; ++i) {
50,396,492✔
839
      for (int j = 0; j < results_.shape()[1]; ++j) {
118,939,780✔
840
        double val = results_(i, j, TallyResult::VALUE) * norm;
68,627,706✔
841
        results_(i, j, TallyResult::VALUE) = 0.0;
68,627,706✔
842
        results_(i, j, TallyResult::SUM) += val;
68,627,706✔
843
        results_(i, j, TallyResult::SUM_SQ) += val * val;
68,627,706✔
844
      }
845
    }
846
  }
847
}
217,302✔
848

849
int Tally::score_index(const std::string& score) const
274✔
850
{
851
  for (int i = 0; i < scores_.size(); i++) {
274!
852
    if (this->score_name(i) == score)
274!
853
      return i;
274✔
854
  }
UNCOV
855
  return -1;
×
856
}
857

858
xt::xarray<double> Tally::get_reshaped_data() const
×
859
{
UNCOV
860
  std::vector<uint64_t> shape;
×
861
  for (auto f : filters()) {
×
862
    shape.push_back(model::tally_filters[f]->n_bins());
×
863
  }
864

865
  // add number of scores and nuclides to tally
UNCOV
866
  shape.push_back(results_.shape()[1]);
×
UNCOV
867
  shape.push_back(results_.shape()[2]);
×
868

869
  xt::xarray<double> reshaped_results = results_;
×
UNCOV
870
  reshaped_results.reshape(shape);
×
UNCOV
871
  return reshaped_results;
×
UNCOV
872
}
×
873

874
std::string Tally::score_name(int score_idx) const
304✔
875
{
876
  if (score_idx < 0 || score_idx >= scores_.size()) {
304!
877
    fatal_error("Index in scores array is out of bounds.");
×
878
  }
879
  return reaction_name(scores_[score_idx]);
304✔
880
}
881

UNCOV
882
std::vector<std::string> Tally::scores() const
×
883
{
UNCOV
884
  std::vector<std::string> score_names;
×
885
  for (int score : scores_)
×
UNCOV
886
    score_names.push_back(reaction_name(score));
×
UNCOV
887
  return score_names;
×
UNCOV
888
}
×
889

890
std::string Tally::nuclide_name(int nuclide_idx) const
30✔
891
{
892
  if (nuclide_idx < 0 || nuclide_idx >= nuclides_.size()) {
30!
UNCOV
893
    fatal_error("Index in nuclides array is out of bounds");
×
894
  }
895

896
  int nuclide = nuclides_.at(nuclide_idx);
30✔
897
  if (nuclide == -1) {
30!
898
    return "total";
30!
899
  }
UNCOV
900
  return data::nuclides.at(nuclide)->name_;
×
901
}
902

903
//==============================================================================
904
// Non-member functions
905
//==============================================================================
906

907
void read_tallies_xml()
1,725✔
908
{
909
  // Check if tallies.xml exists. If not, just return since it is optional
910
  std::string filename = settings::path_input + "tallies.xml";
1,725✔
911
  if (!file_exists(filename))
1,725✔
912
    return;
1,158✔
913

914
  write_message("Reading tallies XML file...", 5);
567✔
915

916
  // Parse tallies.xml file
917
  pugi::xml_document doc;
567✔
918
  doc.load_file(filename.c_str());
567✔
919
  pugi::xml_node root = doc.document_element();
567✔
920

921
  read_tallies_xml(root);
567✔
922
}
1,725✔
923

924
void read_tallies_xml(pugi::xml_node root)
4,222✔
925
{
926
  // Check for <assume_separate> setting
927
  if (check_for_node(root, "assume_separate")) {
4,222✔
928
    settings::assume_separate = get_node_value_bool(root, "assume_separate");
16✔
929
  }
930

931
  // Check for user meshes and allocate
932
  read_meshes(root);
4,222✔
933

934
  // We only need the mesh info for plotting
935
  if (settings::run_mode == RunMode::PLOTTING)
4,222✔
936
    return;
33✔
937

938
  // Read data for tally derivatives
939
  read_tally_derivatives(root);
4,189✔
940

941
  // ==========================================================================
942
  // READ FILTER DATA
943

944
  // Check for user filters and allocate
945
  for (auto node_filt : root.children("filter")) {
12,366✔
946
    auto f = Filter::create(node_filt);
8,177✔
947
  }
948

949
  // ==========================================================================
950
  // READ TALLY DATA
951

952
  // Check for user tallies
953
  int n = 0;
4,189✔
954
  for (auto node : root.children("tally"))
29,009✔
955
    ++n;
24,820✔
956
  if (n == 0 && mpi::master) {
4,189!
UNCOV
957
    warning("No tallies present in tallies.xml file.");
×
958
  }
959

960
  for (auto node_tal : root.children("tally")) {
28,991✔
961
    model::tallies.push_back(make_unique<Tally>(node_tal));
24,820✔
962
  }
963
}
964

965
#ifdef OPENMC_MPI
966
void reduce_tally_results()
32,990✔
967
{
968
  // Don't reduce tally is no_reduce option is on
969
  if (settings::reduce_tallies) {
32,990!
970
    for (int i_tally : model::active_tallies) {
98,440✔
971
      // Skip any tallies that are not active
972
      auto& tally {model::tallies[i_tally]};
65,450✔
973

974
      // Get view of accumulated tally values
975
      auto values_view = xt::view(tally->results_, xt::all(), xt::all(),
65,450✔
976
        static_cast<int>(TallyResult::VALUE));
130,900✔
977

978
      // Make copy of tally values in contiguous array
979
      xt::xtensor<double, 2> values = values_view;
65,450✔
980
      xt::xtensor<double, 2> values_reduced = xt::empty_like(values);
65,450✔
981

982
      // Reduce contiguous set of tally results
983
      MPI_Reduce(values.data(), values_reduced.data(), values.size(),
65,450✔
984
        MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
985

986
      // Transfer values on master and reset on other ranks
987
      if (mpi::master) {
65,450✔
988
        values_view = values_reduced;
32,715✔
989
      } else {
990
        values_view = 0.0;
32,735✔
991
      }
992
    }
65,450✔
993
  }
994

995
  // Note that global tallies are *always* reduced even when no_reduce option
996
  // is on.
997

998
  // Get view of global tally values
999
  auto& gt = simulation::global_tallies;
32,990✔
1000
  auto gt_values_view =
1001
    xt::view(gt, xt::all(), static_cast<int>(TallyResult::VALUE));
32,990✔
1002

1003
  // Make copy of values in contiguous array
1004
  xt::xtensor<double, 1> gt_values = gt_values_view;
32,990✔
1005
  xt::xtensor<double, 1> gt_values_reduced = xt::empty_like(gt_values);
32,990✔
1006

1007
  // Reduce contiguous data
1008
  MPI_Reduce(gt_values.data(), gt_values_reduced.data(), N_GLOBAL_TALLIES,
32,990✔
1009
    MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
1010

1011
  // Transfer values on master and reset on other ranks
1012
  if (mpi::master) {
32,990✔
1013
    gt_values_view = gt_values_reduced;
16,490✔
1014
  } else {
1015
    gt_values_view = 0.0;
16,500✔
1016
  }
1017

1018
  // We also need to determine the total starting weight of particles from the
1019
  // last realization
1020
  double weight_reduced;
1021
  MPI_Reduce(&simulation::total_weight, &weight_reduced, 1, MPI_DOUBLE, MPI_SUM,
32,990✔
1022
    0, mpi::intracomm);
1023
  if (mpi::master)
32,990✔
1024
    simulation::total_weight = weight_reduced;
16,490✔
1025
}
32,990✔
1026
#endif
1027

1028
void accumulate_tallies()
117,810✔
1029
{
1030
#ifdef OPENMC_MPI
1031
  // Combine tally results onto master process
1032
  if (mpi::n_procs > 1 && settings::solver_type == SolverType::MONTE_CARLO) {
68,589✔
1033
    reduce_tally_results();
32,990✔
1034
  }
1035
#endif
1036

1037
  // Increase number of realizations (only used for global tallies)
1038
  simulation::n_realizations += 1;
117,810✔
1039

1040
  // Accumulate on master only unless run is not reduced then do it on all
1041
  if (mpi::master || !settings::reduce_tallies) {
117,810!
1042
    auto& gt = simulation::global_tallies;
96,010✔
1043

1044
    if (settings::run_mode == RunMode::EIGENVALUE) {
96,010✔
1045
      if (simulation::current_batch > settings::n_inactive) {
65,882✔
1046
        // Accumulate products of different estimators of k
1047
        double k_col = gt(GlobalTally::K_COLLISION, TallyResult::VALUE) /
50,542✔
1048
                       simulation::total_weight;
50,542✔
1049
        double k_abs = gt(GlobalTally::K_ABSORPTION, TallyResult::VALUE) /
50,542✔
1050
                       simulation::total_weight;
50,542✔
1051
        double k_tra = gt(GlobalTally::K_TRACKLENGTH, TallyResult::VALUE) /
50,542✔
1052
                       simulation::total_weight;
50,542✔
1053
        simulation::k_col_abs += k_col * k_abs;
50,542✔
1054
        simulation::k_col_tra += k_col * k_tra;
50,542✔
1055
        simulation::k_abs_tra += k_abs * k_tra;
50,542✔
1056
      }
1057
    }
1058

1059
    // Accumulate results for global tallies
1060
    for (int i = 0; i < N_GLOBAL_TALLIES; ++i) {
480,050✔
1061
      double val = gt(i, TallyResult::VALUE) / simulation::total_weight;
384,040✔
1062
      gt(i, TallyResult::VALUE) = 0.0;
384,040✔
1063
      gt(i, TallyResult::SUM) += val;
384,040✔
1064
      gt(i, TallyResult::SUM_SQ) += val * val;
384,040✔
1065
    }
1066
  }
1067

1068
  // Accumulate results for each tally
1069
  for (int i_tally : model::active_tallies) {
335,112✔
1070
    auto& tally {model::tallies[i_tally]};
217,302✔
1071
    tally->accumulate();
217,302✔
1072
  }
1073
}
117,810✔
1074

1075
double distance_to_time_boundary(double time, double speed)
7,787,043✔
1076
{
1077
  if (model::time_grid.empty()) {
7,787,043!
UNCOV
1078
    return INFTY;
×
1079
  } else if (time >= model::time_grid.back()) {
7,787,043✔
1080
    return INFTY;
26,620✔
1081
  } else {
1082
    double next_time =
1083
      *std::upper_bound(model::time_grid.begin(), model::time_grid.end(), time);
7,760,423✔
1084
    return (next_time - time) * speed;
7,760,423✔
1085
  }
1086
}
1087

1088
//! Add new points to the global time grid
1089
//
1090
//! \param grid Vector of new time points to add
1091
void add_to_time_grid(vector<double> grid)
220✔
1092
{
1093
  if (grid.empty())
220!
UNCOV
1094
    return;
×
1095

1096
  // Create new vector with enough space to hold old and new grid points
1097
  vector<double> merged;
220✔
1098
  merged.reserve(model::time_grid.size() + grid.size());
220✔
1099

1100
  // Merge and remove duplicates
1101
  std::set_union(model::time_grid.begin(), model::time_grid.end(), grid.begin(),
220✔
1102
    grid.end(), std::back_inserter(merged));
1103

1104
  // Swap in the new grid
1105
  model::time_grid.swap(merged);
220✔
1106
}
220✔
1107

1108
void setup_active_tallies()
117,822✔
1109
{
1110
  model::active_tallies.clear();
117,822✔
1111
  model::active_analog_tallies.clear();
117,822✔
1112
  model::active_tracklength_tallies.clear();
117,822✔
1113
  model::active_timed_tracklength_tallies.clear();
117,822✔
1114
  model::active_collision_tallies.clear();
117,822✔
1115
  model::active_meshsurf_tallies.clear();
117,822✔
1116
  model::active_surface_tallies.clear();
117,822✔
1117
  model::active_pulse_height_tallies.clear();
117,822✔
1118
  model::time_grid.clear();
117,822✔
1119

1120
  for (auto i = 0; i < model::tallies.size(); ++i) {
465,972✔
1121
    const auto& tally {*model::tallies[i]};
348,150✔
1122

1123
    if (tally.active_) {
348,150✔
1124
      model::active_tallies.push_back(i);
217,302✔
1125
      bool mesh_present = (tally.get_filter<MeshFilter>() ||
382,847✔
1126
                           tally.get_filter<MeshMaterialFilter>());
165,545✔
1127
      auto time_filter = tally.get_filter<TimeFilter>();
217,302✔
1128
      switch (tally.type_) {
217,302!
1129

1130
      case TallyType::VOLUME:
210,524✔
1131
        switch (tally.estimator_) {
210,524!
1132
        case TallyEstimator::ANALOG:
82,058✔
1133
          model::active_analog_tallies.push_back(i);
82,058✔
1134
          break;
82,058✔
1135
        case TallyEstimator::TRACKLENGTH:
120,388✔
1136
          if (time_filter && mesh_present) {
120,388✔
1137
            model::active_timed_tracklength_tallies.push_back(i);
220✔
1138
            add_to_time_grid(time_filter->bins());
220✔
1139
          } else {
1140
            model::active_tracklength_tallies.push_back(i);
120,168✔
1141
          }
1142
          break;
120,388✔
1143
        case TallyEstimator::COLLISION:
8,078✔
1144
          model::active_collision_tallies.push_back(i);
8,078✔
1145
        }
1146
        break;
210,524✔
1147

1148
      case TallyType::MESH_SURFACE:
4,596✔
1149
        model::active_meshsurf_tallies.push_back(i);
4,596✔
1150
        break;
4,596✔
1151

1152
      case TallyType::SURFACE:
2,102✔
1153
        model::active_surface_tallies.push_back(i);
2,102✔
1154
        break;
2,102✔
1155

1156
      case TallyType::PULSE_HEIGHT:
80✔
1157
        model::active_pulse_height_tallies.push_back(i);
80✔
1158
        break;
80✔
1159
      }
1160
    }
1161
  }
1162
}
117,822✔
1163

1164
void free_memory_tally()
8,017✔
1165
{
1166
  model::tally_derivs.clear();
8,017✔
1167
  model::tally_deriv_map.clear();
8,017✔
1168

1169
  model::tally_filters.clear();
8,017✔
1170
  model::filter_map.clear();
8,017✔
1171

1172
  model::tallies.clear();
8,017✔
1173

1174
  model::active_tallies.clear();
8,017✔
1175
  model::active_analog_tallies.clear();
8,017✔
1176
  model::active_tracklength_tallies.clear();
8,017✔
1177
  model::active_timed_tracklength_tallies.clear();
8,017✔
1178
  model::active_collision_tallies.clear();
8,017✔
1179
  model::active_meshsurf_tallies.clear();
8,017✔
1180
  model::active_surface_tallies.clear();
8,017✔
1181
  model::active_pulse_height_tallies.clear();
8,017✔
1182
  model::time_grid.clear();
8,017✔
1183

1184
  model::tally_map.clear();
8,017✔
1185
}
8,017✔
1186

1187
//==============================================================================
1188
// C-API functions
1189
//==============================================================================
1190

1191
extern "C" int openmc_extend_tallies(
1,582✔
1192
  int32_t n, int32_t* index_start, int32_t* index_end)
1193
{
1194
  if (index_start)
1,582!
1195
    *index_start = model::tallies.size();
1,582✔
1196
  if (index_end)
1,582!
1197
    *index_end = model::tallies.size() + n - 1;
×
1198
  for (int i = 0; i < n; ++i) {
3,164✔
1199
    model::tallies.push_back(make_unique<Tally>(-1));
1,582✔
1200
  }
1201
  return 0;
1,582✔
1202
}
1203

1204
extern "C" int openmc_get_tally_index(int32_t id, int32_t* index)
24,746✔
1205
{
1206
  auto it = model::tally_map.find(id);
24,746✔
1207
  if (it == model::tally_map.end()) {
24,746✔
1208
    set_errmsg(fmt::format("No tally exists with ID={}.", id));
28✔
1209
    return OPENMC_E_INVALID_ID;
28✔
1210
  }
1211

1212
  *index = it->second;
24,718✔
1213
  return 0;
24,718✔
1214
}
1215

UNCOV
1216
extern "C" void openmc_get_tally_next_id(int32_t* id)
×
1217
{
UNCOV
1218
  int32_t largest_tally_id = 0;
×
UNCOV
1219
  for (const auto& t : model::tallies) {
×
1220
    largest_tally_id = std::max(largest_tally_id, t->id_);
×
1221
  }
UNCOV
1222
  *id = largest_tally_id + 1;
×
UNCOV
1223
}
×
1224

UNCOV
1225
extern "C" int openmc_tally_get_estimator(int32_t index, int* estimator)
×
1226
{
UNCOV
1227
  if (index < 0 || index >= model::tallies.size()) {
×
UNCOV
1228
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1229
    return OPENMC_E_OUT_OF_BOUNDS;
×
1230
  }
1231

1232
  *estimator = static_cast<int>(model::tallies[index]->estimator_);
×
UNCOV
1233
  return 0;
×
1234
}
1235

1236
extern "C" int openmc_tally_set_estimator(int32_t index, const char* estimator)
780✔
1237
{
1238
  if (index < 0 || index >= model::tallies.size()) {
780!
UNCOV
1239
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1240
    return OPENMC_E_OUT_OF_BOUNDS;
×
1241
  }
1242

1243
  auto& t {model::tallies[index]};
780✔
1244

1245
  std::string est = estimator;
780✔
1246
  if (est == "analog") {
780!
1247
    t->estimator_ = TallyEstimator::ANALOG;
780✔
UNCOV
1248
  } else if (est == "collision") {
×
1249
    t->estimator_ = TallyEstimator::COLLISION;
×
1250
  } else if (est == "tracklength") {
×
1251
    t->estimator_ = TallyEstimator::TRACKLENGTH;
×
1252
  } else {
UNCOV
1253
    set_errmsg("Unknown tally estimator: " + est);
×
1254
    return OPENMC_E_INVALID_ARGUMENT;
×
1255
  }
1256
  return 0;
780✔
1257
}
780✔
1258

1259
extern "C" int openmc_tally_get_id(int32_t index, int32_t* id)
34,162✔
1260
{
1261
  if (index < 0 || index >= model::tallies.size()) {
34,162!
UNCOV
1262
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1263
    return OPENMC_E_OUT_OF_BOUNDS;
×
1264
  }
1265

1266
  *id = model::tallies[index]->id_;
34,162✔
1267
  return 0;
34,162✔
1268
}
1269

1270
extern "C" int openmc_tally_set_id(int32_t index, int32_t id)
1,582✔
1271
{
1272
  if (index < 0 || index >= model::tallies.size()) {
1,582!
UNCOV
1273
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1274
    return OPENMC_E_OUT_OF_BOUNDS;
×
1275
  }
1276

1277
  model::tallies[index]->set_id(id);
1,582✔
1278
  return 0;
1,582✔
1279
}
1280

1281
extern "C" int openmc_tally_get_type(int32_t index, int32_t* type)
14✔
1282
{
1283
  if (index < 0 || index >= model::tallies.size()) {
14!
UNCOV
1284
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1285
    return OPENMC_E_OUT_OF_BOUNDS;
×
1286
  }
1287
  *type = static_cast<int>(model::tallies[index]->type_);
14✔
1288

1289
  return 0;
14✔
1290
}
1291

1292
extern "C" int openmc_tally_set_type(int32_t index, const char* type)
780✔
1293
{
1294
  if (index < 0 || index >= model::tallies.size()) {
780!
UNCOV
1295
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1296
    return OPENMC_E_OUT_OF_BOUNDS;
×
1297
  }
1298
  if (strcmp(type, "volume") == 0) {
780✔
1299
    model::tallies[index]->type_ = TallyType::VOLUME;
585✔
1300
  } else if (strcmp(type, "mesh-surface") == 0) {
195!
1301
    model::tallies[index]->type_ = TallyType::MESH_SURFACE;
195✔
UNCOV
1302
  } else if (strcmp(type, "surface") == 0) {
×
UNCOV
1303
    model::tallies[index]->type_ = TallyType::SURFACE;
×
UNCOV
1304
  } else if (strcmp(type, "pulse-height") == 0) {
×
UNCOV
1305
    model::tallies[index]->type_ = TallyType::PULSE_HEIGHT;
×
1306
  } else {
UNCOV
1307
    set_errmsg(fmt::format("Unknown tally type: {}", type));
×
1308
    return OPENMC_E_INVALID_ARGUMENT;
×
1309
  }
1310

1311
  return 0;
780✔
1312
}
1313

1314
extern "C" int openmc_tally_get_active(int32_t index, bool* active)
28✔
1315
{
1316
  if (index < 0 || index >= model::tallies.size()) {
28!
UNCOV
1317
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1318
    return OPENMC_E_OUT_OF_BOUNDS;
×
1319
  }
1320
  *active = model::tallies[index]->active_;
28✔
1321

1322
  return 0;
28✔
1323
}
1324

1325
extern "C" int openmc_tally_set_active(int32_t index, bool active)
794✔
1326
{
1327
  if (index < 0 || index >= model::tallies.size()) {
794!
UNCOV
1328
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1329
    return OPENMC_E_OUT_OF_BOUNDS;
×
1330
  }
1331
  model::tallies[index]->active_ = active;
794✔
1332

1333
  return 0;
794✔
1334
}
1335

1336
extern "C" int openmc_tally_get_writable(int32_t index, bool* writable)
28✔
1337
{
1338
  if (index < 0 || index >= model::tallies.size()) {
28!
UNCOV
1339
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1340
    return OPENMC_E_OUT_OF_BOUNDS;
×
1341
  }
1342
  *writable = model::tallies[index]->writable();
28✔
1343

1344
  return 0;
28✔
1345
}
1346

1347
extern "C" int openmc_tally_set_writable(int32_t index, bool writable)
802✔
1348
{
1349
  if (index < 0 || index >= model::tallies.size()) {
802!
1350
    set_errmsg("Index in tallies array is out of bounds.");
×
1351
    return OPENMC_E_OUT_OF_BOUNDS;
×
1352
  }
1353
  model::tallies[index]->set_writable(writable);
802✔
1354

1355
  return 0;
802✔
1356
}
1357

1358
extern "C" int openmc_tally_get_multiply_density(int32_t index, bool* value)
28✔
1359
{
1360
  if (index < 0 || index >= model::tallies.size()) {
28!
UNCOV
1361
    set_errmsg("Index in tallies array is out of bounds.");
×
1362
    return OPENMC_E_OUT_OF_BOUNDS;
×
1363
  }
1364
  *value = model::tallies[index]->multiply_density();
28✔
1365

1366
  return 0;
28✔
1367
}
1368

1369
extern "C" int openmc_tally_set_multiply_density(int32_t index, bool value)
374✔
1370
{
1371
  if (index < 0 || index >= model::tallies.size()) {
374!
UNCOV
1372
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1373
    return OPENMC_E_OUT_OF_BOUNDS;
×
1374
  }
1375
  model::tallies[index]->set_multiply_density(value);
374✔
1376

1377
  return 0;
374✔
1378
}
1379

1380
extern "C" int openmc_tally_get_scores(int32_t index, int** scores, int* n)
508✔
1381
{
1382
  if (index < 0 || index >= model::tallies.size()) {
508!
UNCOV
1383
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1384
    return OPENMC_E_OUT_OF_BOUNDS;
×
1385
  }
1386

1387
  *scores = model::tallies[index]->scores_.data();
508✔
1388
  *n = model::tallies[index]->scores_.size();
508✔
1389
  return 0;
508✔
1390
}
1391

1392
extern "C" int openmc_tally_set_scores(
1,596✔
1393
  int32_t index, int n, const char** scores)
1394
{
1395
  if (index < 0 || index >= model::tallies.size()) {
1,596!
UNCOV
1396
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1397
    return OPENMC_E_OUT_OF_BOUNDS;
×
1398
  }
1399

1400
  vector<std::string> scores_str(scores, scores + n);
1,596✔
1401
  try {
1402
    model::tallies[index]->set_scores(scores_str);
1,596✔
UNCOV
1403
  } catch (const std::invalid_argument& ex) {
×
UNCOV
1404
    set_errmsg(ex.what());
×
UNCOV
1405
    return OPENMC_E_INVALID_ARGUMENT;
×
UNCOV
1406
  }
×
1407

1408
  return 0;
1,596✔
1409
}
1,596✔
1410

1411
extern "C" int openmc_tally_get_nuclides(int32_t index, int** nuclides, int* n)
648✔
1412
{
1413
  // Make sure the index fits in the array bounds.
1414
  if (index < 0 || index >= model::tallies.size()) {
648!
UNCOV
1415
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1416
    return OPENMC_E_OUT_OF_BOUNDS;
×
1417
  }
1418

1419
  *n = model::tallies[index]->nuclides_.size();
648✔
1420
  *nuclides = model::tallies[index]->nuclides_.data();
648✔
1421

1422
  return 0;
648✔
1423
}
1424

1425
extern "C" int openmc_tally_set_nuclides(
1,891✔
1426
  int32_t index, int n, const char** nuclides)
1427
{
1428
  // Make sure the index fits in the array bounds.
1429
  if (index < 0 || index >= model::tallies.size()) {
1,891!
UNCOV
1430
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1431
    return OPENMC_E_OUT_OF_BOUNDS;
×
1432
  }
1433

1434
  vector<std::string> words(nuclides, nuclides + n);
1,891✔
1435
  vector<int> nucs;
1,891✔
1436
  for (auto word : words) {
7,833✔
1437
    if (word == "total") {
5,956✔
1438
      nucs.push_back(-1);
780✔
1439
    } else {
1440
      auto search = data::nuclide_map.find(word);
5,176✔
1441
      if (search == data::nuclide_map.end()) {
5,176✔
1442
        int err = openmc_load_nuclide(word.c_str(), nullptr, 0);
1,018✔
1443
        if (err < 0) {
1,018✔
1444
          set_errmsg(openmc_err_msg);
14✔
1445
          return OPENMC_E_DATA;
14✔
1446
        }
1447
      }
1448
      nucs.push_back(data::nuclide_map.at(word));
5,162✔
1449
    }
1450
  }
5,956✔
1451

1452
  model::tallies[index]->nuclides_ = nucs;
1,877✔
1453

1454
  return 0;
1,877✔
1455
}
1,891✔
1456

1457
extern "C" int openmc_tally_get_filters(
1,647✔
1458
  int32_t index, const int32_t** indices, size_t* n)
1459
{
1460
  if (index < 0 || index >= model::tallies.size()) {
1,647!
1461
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1462
    return OPENMC_E_OUT_OF_BOUNDS;
×
1463
  }
1464

1465
  *indices = model::tallies[index]->filters().data();
1,647✔
1466
  *n = model::tallies[index]->filters().size();
1,647✔
1467
  return 0;
1,647✔
1468
}
1469

1470
extern "C" int openmc_tally_set_filters(
1,760✔
1471
  int32_t index, size_t n, const int32_t* indices)
1472
{
1473
  // Make sure the index fits in the array bounds.
1474
  if (index < 0 || index >= model::tallies.size()) {
1,760!
1475
    set_errmsg("Index in tallies array is out of bounds.");
×
1476
    return OPENMC_E_OUT_OF_BOUNDS;
×
1477
  }
1478

1479
  // Set the filters.
1480
  try {
1481
    // Convert indices to filter pointers
1482
    vector<Filter*> filters;
1,760✔
1483
    for (int64_t i = 0; i < n; ++i) {
4,273✔
1484
      int32_t i_filt = indices[i];
2,513✔
1485
      filters.push_back(model::tally_filters.at(i_filt).get());
2,513✔
1486
    }
1487
    model::tallies[index]->set_filters(filters);
1,760✔
1488
  } catch (const std::out_of_range& ex) {
1,760!
UNCOV
1489
    set_errmsg("Index in tally filter array out of bounds.");
×
UNCOV
1490
    return OPENMC_E_OUT_OF_BOUNDS;
×
UNCOV
1491
  }
×
1492

1493
  return 0;
1,760✔
1494
}
1495

1496
//! Reset tally results and number of realizations
1497
extern "C" int openmc_tally_reset(int32_t index)
3,172✔
1498
{
1499
  // Make sure the index fits in the array bounds.
1500
  if (index < 0 || index >= model::tallies.size()) {
3,172!
UNCOV
1501
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1502
    return OPENMC_E_OUT_OF_BOUNDS;
×
1503
  }
1504

1505
  model::tallies[index]->reset();
3,172✔
1506
  return 0;
3,172✔
1507
}
1508

1509
extern "C" int openmc_tally_get_n_realizations(int32_t index, int32_t* n)
4,323✔
1510
{
1511
  // Make sure the index fits in the array bounds.
1512
  if (index < 0 || index >= model::tallies.size()) {
4,323!
UNCOV
1513
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1514
    return OPENMC_E_OUT_OF_BOUNDS;
×
1515
  }
1516

1517
  *n = model::tallies[index]->n_realizations_;
4,323✔
1518
  return 0;
4,323✔
1519
}
1520

1521
//! \brief Returns a pointer to a tally results array along with its shape.
1522
//! This allows a user to obtain in-memory tally results from Python directly.
1523
extern "C" int openmc_tally_results(
19,087✔
1524
  int32_t index, double** results, size_t* shape)
1525
{
1526
  // Make sure the index fits in the array bounds.
1527
  if (index < 0 || index >= model::tallies.size()) {
19,087!
UNCOV
1528
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1529
    return OPENMC_E_OUT_OF_BOUNDS;
×
1530
  }
1531

1532
  const auto& t {model::tallies[index]};
19,087✔
1533
  if (t->results_.size() == 0) {
19,087!
UNCOV
1534
    set_errmsg("Tally results have not been allocated yet.");
×
UNCOV
1535
    return OPENMC_E_ALLOCATE;
×
1536
  }
1537

1538
  // Set pointer to results and copy shape
1539
  *results = t->results_.data();
19,087✔
1540
  auto s = t->results_.shape();
19,087✔
1541
  shape[0] = s[0];
19,087✔
1542
  shape[1] = s[1];
19,087✔
1543
  shape[2] = s[2];
19,087✔
1544
  return 0;
19,087✔
1545
}
1546

1547
extern "C" int openmc_global_tallies(double** ptr)
14✔
1548
{
1549
  *ptr = simulation::global_tallies.data();
14✔
1550
  return 0;
14✔
1551
}
1552

1553
extern "C" size_t tallies_size()
1,638✔
1554
{
1555
  return model::tallies.size();
1,638✔
1556
}
1557

1558
// given a tally ID, remove it from the tallies vector
1559
extern "C" int openmc_remove_tally(int32_t index)
14✔
1560
{
1561
  // check that id is in the map
1562
  if (index < 0 || index >= model::tallies.size()) {
14!
UNCOV
1563
    set_errmsg("Index in tallies array is out of bounds.");
×
UNCOV
1564
    return OPENMC_E_OUT_OF_BOUNDS;
×
1565
  }
1566

1567
  // delete the tally via iterator pointing to correct position
1568
  // this calls the Tally destructor, removing the tally from the map as well
1569
  model::tallies.erase(model::tallies.begin() + index);
14✔
1570

1571
  return 0;
14✔
1572
}
1573

1574
} // 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