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

openmc-dev / openmc / 17707962129

14 Sep 2025 07:15AM UTC coverage: 85.074% (-0.1%) from 85.218%
17707962129

Pull #3547

github

web-flow
Merge 07e1f2aef into afd9d0607
Pull Request #3547: Tally spectrum of secondary particles

23 of 58 new or added lines in 8 files covered. (39.66%)

31 existing lines in 2 files now uncovered.

52790 of 62052 relevant lines covered (85.07%)

38251650.85 hits per line

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

81.23
/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_particleout_analog_tallies;
62
vector<int> active_tracklength_tallies;
63
vector<int> active_timed_tracklength_tallies;
64
vector<int> active_collision_tallies;
65
vector<int> active_meshsurf_tallies;
66
vector<int> active_surface_tallies;
67
vector<int> active_pulse_height_tallies;
68
vector<int> pulse_height_cells;
69
vector<double> time_grid;
70
} // namespace model
71

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

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

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

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

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

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

104
  if (check_for_node(node, "name"))
24,487✔
105
    name_ = get_node_value(node, "name");
2,414✔
106

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

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

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

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

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

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

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

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

154
    auto pf = dynamic_cast<ParticleFilter*>(f);
48,735✔
155
    if (pf)
48,735✔
156
      particle_filter_index = i_filter;
438✔
157

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

176
  // =======================================================================
177
  // READ DATA FOR NUCLIDES
178

179
  this->set_nuclides(node);
24,487✔
180

181
  // =======================================================================
182
  // READ DATA FOR SCORES
183

184
  this->set_scores(node);
24,487✔
185

186
  if (!check_for_node(node, "scores")) {
24,487✔
187
    fatal_error(fmt::format("No scores specified on tally {}.", id_));
×
188
  }
189

190
  // Set IFP if needed
191
  if (!settings::ifp_on) {
24,487✔
192
    // Determine if this tally has an IFP score
193
    bool has_ifp_score = false;
24,487✔
194
    for (int score : scores_) {
58,187✔
195
      if (score == SCORE_IFP_TIME_NUM || score == SCORE_IFP_BETA_NUM ||
33,734✔
196
          score == SCORE_IFP_DENOM) {
197
        has_ifp_score = true;
34✔
198
        break;
34✔
199
      }
200
    }
201

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

225
  // Set IFP parameters if needed
226
  if (settings::ifp_on) {
24,469✔
227
    for (int score : scores_) {
64✔
228
      switch (score) {
48✔
229
      case SCORE_IFP_TIME_NUM:
16✔
230
        if (settings::ifp_parameter == IFPParameter::None) {
16✔
231
          settings::ifp_parameter = IFPParameter::GenerationTime;
16✔
232
        } else if (settings::ifp_parameter == IFPParameter::BetaEffective) {
×
233
          settings::ifp_parameter = IFPParameter::Both;
×
234
        }
235
        break;
16✔
236
      case SCORE_IFP_BETA_NUM:
32✔
237
      case SCORE_IFP_DENOM:
238
        if (settings::ifp_parameter == IFPParameter::None) {
32✔
239
          settings::ifp_parameter = IFPParameter::BetaEffective;
×
240
        } else if (settings::ifp_parameter == IFPParameter::GenerationTime) {
32✔
241
          settings::ifp_parameter = IFPParameter::Both;
16✔
242
        }
243
        break;
32✔
244
      }
245
    }
246
  }
247

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

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

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

313
    deriv_ = it->second;
320✔
314

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

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

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

346
  // =======================================================================
347
  // SET TALLY ESTIMATOR
348

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

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

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

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

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

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

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

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

412
void Tally::set_id(int32_t id)
27,587✔
413
{
414
  assert(id >= 0 || id == C_NONE);
22,060✔
415

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

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

428
  // If no ID specified, auto-assign next ID in sequence
429
  if (id == C_NONE) {
27,587✔
430
    id = 0;
1,605✔
431
    for (const auto& t : model::tallies) {
5,154✔
432
      id = std::max(id, t->id_);
3,549✔
433
    }
434
    ++id;
1,605✔
435
  }
436

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

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

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

456
    filter_indices[f->type()] = i;
741✔
457
  }
458
  return filter_indices;
263✔
459
}
×
460

461
bool Tally::has_filter(FilterType filter_type) const
212,827✔
462
{
463
  for (auto idx : this->filters()) {
614,482✔
464
    if (model::tally_filters[idx]->type() == filter_type)
402,372✔
465
      return true;
717✔
466
  }
467
  return false;
212,110✔
468
}
469

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

476
  // Copy in the given filter indices.
477
  auto n = filters.size();
27,816✔
478
  filters_.reserve(n);
27,816✔
479

480
  for (auto* filter : filters) {
79,019✔
481
    add_filter(filter);
51,203✔
482
  }
483
}
27,816✔
484

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

492
  // Keep track of indices for special filters
493
  if (filter->type() == FilterType::ENERGY_OUT) {
51,460✔
494
    energyout_filter_ = filters_.size();
4,717✔
495
  } else if (filter->type() == FilterType::DELAYED_GROUP) {
46,743✔
496
    delayedgroup_filter_ = filters_.size();
800✔
497
  }
498
  filters_.push_back(filter_idx);
51,460✔
499
}
500

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

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

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

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

531
  // Check for the presence of certain restrictive filters.
532
  bool energyout_present = energyout_filter_ != C_NONE;
26,094✔
533
  bool legendre_present = false;
26,094✔
534
  bool cell_present = false;
26,094✔
535
  bool cellfrom_present = false;
26,094✔
536
  bool surface_present = false;
26,094✔
537
  bool meshsurface_present = false;
26,094✔
538
  bool non_cell_energy_present = false;
26,094✔
539
  for (auto i_filt : filters_) {
76,147✔
540
    const auto* filt {model::tally_filters[i_filt].get()};
50,053✔
541
    // Checking for only cell and energy filters for pulse-height tally
542
    if (!(filt->type() == FilterType::CELL ||
99,025✔
543
          filt->type() == FilterType::ENERGY)) {
48,972✔
544
      non_cell_energy_present = true;
31,923✔
545
    }
546
    if (filt->type() == FilterType::LEGENDRE) {
50,053✔
547
      legendre_present = true;
2,045✔
548
    } else if (filt->type() == FilterType::CELLFROM) {
48,008✔
549
      cellfrom_present = true;
304✔
550
    } else if (filt->type() == FilterType::CELL) {
47,704✔
551
      cell_present = true;
1,081✔
552
    } else if (filt->type() == FilterType::SURFACE) {
46,623✔
553
      surface_present = true;
193✔
554
    } else if (filt->type() == FilterType::MESH_SURFACE) {
46,430✔
555
      meshsurface_present = true;
367✔
556
    }
557
  }
558

559
  // Iterate over the given scores.
560
  for (auto score_str : scores) {
62,069✔
561
    // Make sure a delayed group filter wasn't used with an incompatible
562
    // score.
563
    if (delayedgroup_filter_ != C_NONE) {
35,975✔
564
      if (score_str != "delayed-nu-fission" && score_str != "decay-rate")
816✔
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);
35,975✔
570

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

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

588
    case SCORE_TOTAL:
6,890✔
589
    case SCORE_SCATTER:
590
      if (legendre_present)
6,890✔
591
        estimator_ = TallyEstimator::ANALOG;
1,362✔
592
    case SCORE_NU_FISSION:
593
    case SCORE_DELAYED_NU_FISSION:
594
    case SCORE_PROMPT_NU_FISSION:
595
      if (energyout_present)
11,631✔
596
        estimator_ = TallyEstimator::ANALOG;
3,571✔
597
      break;
11,631✔
598

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

608
    case SCORE_CURRENT:
592✔
609
      // Check which type of current is desired: mesh or surface currents.
610
      if (surface_present || cell_present || cellfrom_present) {
592✔
611
        if (meshsurface_present)
225✔
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) {
367✔
617
        type_ = TallyType::MESH_SURFACE;
367✔
618
      } else {
619
        fatal_error("Cannot tally currents without surface type filters");
×
620
      }
621
      break;
592✔
622

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

628
    case SCORE_PULSE_HEIGHT:
16✔
629
      if (non_cell_energy_present) {
16✔
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:
102✔
653
    case SCORE_IFP_BETA_NUM:
654
    case SCORE_IFP_DENOM:
655
      estimator_ = TallyEstimator::COLLISION;
102✔
656
      break;
102✔
657
    }
658

659
    scores_.push_back(score);
35,975✔
660
  }
35,975✔
661

662
  // Make sure that no duplicate scores exist.
663
  for (auto it1 = scores_.begin(); it1 != scores_.end(); ++it1) {
62,069✔
664
    for (auto it2 = it1 + 1; it2 != scores_.end(); ++it2) {
83,347✔
665
      if (*it1 == *it2)
47,372✔
666
        fatal_error(
×
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,094✔
674
    for (auto sc : scores_)
6,158✔
675
      if (sc > 0)
4,551✔
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,094✔
683
    if (scores_.size() != 1)
592✔
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,094✔
688
    fatal_error("Cannot tally score other than 'current' when using a surface "
×
689
                "or mesh-surface filter.");
690
}
26,094✔
691

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

696
  // By default, we tally just the total material rates.
697
  if (!check_for_node(node, "nuclides")) {
24,487✔
698
    nuclides_.push_back(-1);
6,190✔
699
    return;
6,190✔
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,297✔
705
  this->set_nuclides(words);
18,297✔
706
}
18,297✔
707

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

712
  for (const auto& nuc : nuclides) {
47,292✔
713
    if (nuc == "total") {
28,995✔
714
      nuclides_.push_back(-1);
14,768✔
715
    } else {
716
      auto search = data::nuclide_map.find(nuc);
14,227✔
717
      if (search == data::nuclide_map.end()) {
14,227✔
718
        int err = openmc_load_nuclide(nuc.c_str(), nullptr, 0);
33✔
719
        if (err < 0)
33✔
720
          throw std::runtime_error {openmc_err_msg};
×
721
      }
722
      nuclides_.push_back(data::nuclide_map.at(nuc));
14,227✔
723
    }
724
  }
725
}
18,297✔
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✔
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 {
741
        fatal_error(fmt::format(
×
742
          "Unknown trigger type \"{}\" in tally {}", type_str, id_));
×
743
      }
744
    } else {
71✔
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✔
754
        fatal_error("Tally trigger threshold must be positive");
×
755
      }
756
    } else {
757
      fatal_error(fmt::format(
×
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 {
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✔
789
          fatal_error(
×
790
            fmt::format("Could not find the score \"{}\" in tally "
×
791
                        "{} but it was listed in a trigger on that tally",
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,543✔
801
{
802
  int n_scores = scores_.size() * nuclides_.size();
26,543✔
803
  results_ = xt::empty<double>({n_filter_bins_, n_scores, 3});
26,543✔
804
}
26,543✔
805

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

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

819
  if (mpi::master || !settings::reduce_tallies) {
212,038✔
820
    // Calculate total source strength for normalization
821
    double total_source = 0.0;
174,273✔
822
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
174,273✔
823
      total_source = model::external_sources_probability.integral();
49,967✔
824
    } else {
825
      total_source = 1.0;
124,306✔
826
    }
827

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

832
    if (settings::solver_type == SolverType::RANDOM_RAY) {
174,273✔
833
      norm = 1.0;
11,226✔
834
    }
835

836
// Accumulate each result
837
#pragma omp parallel for
97,531✔
838
    for (int i = 0; i < results_.shape()[0]; ++i) {
44,467,490✔
839
      for (int j = 0; j < results_.shape()[1]; ++j) {
107,039,356✔
840
        double val = results_(i, j, TallyResult::VALUE) * norm;
62,648,608✔
841
        results_(i, j, TallyResult::VALUE) = 0.0;
62,648,608✔
842
        results_(i, j, TallyResult::SUM) += val;
62,648,608✔
843
        results_(i, j, TallyResult::SUM_SQ) += val * val;
62,648,608✔
844
      }
845
    }
846
  }
847
}
212,038✔
848

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

858
xt::xarray<double> Tally::get_reshaped_data() const
×
859
{
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
866
  shape.push_back(results_.shape()[1]);
×
867
  shape.push_back(results_.shape()[2]);
×
868

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

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

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

890
std::string Tally::nuclide_name(int nuclide_idx) const
30✔
891
{
892
  if (nuclide_idx < 0 || nuclide_idx >= nuclides_.size()) {
30✔
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
  }
900
  return data::nuclides.at(nuclide)->name_;
×
901
}
902

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

907
void read_tallies_xml()
1,670✔
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,670✔
911
  if (!file_exists(filename))
1,670✔
912
    return;
1,121✔
913

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

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

921
  read_tallies_xml(root);
549✔
922
}
1,670✔
923

924
void read_tallies_xml(pugi::xml_node root)
4,058✔
925
{
926
  // Check for <assume_separate> setting
927
  if (check_for_node(root, "assume_separate")) {
4,058✔
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,058✔
933

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

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

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

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

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

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

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

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

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

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

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

986
      // Transfer values on master and reset on other ranks
987
      if (mpi::master) {
65,310✔
988
        values_view = values_reduced;
32,645✔
989
      } else {
990
        values_view = 0.0;
32,665✔
991
      }
992
    }
65,310✔
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,980✔
1000
  auto gt_values_view =
1001
    xt::view(gt, xt::all(), static_cast<int>(TallyResult::VALUE));
32,980✔
1002

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

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

1011
  // Transfer values on master and reset on other ranks
1012
  if (mpi::master) {
32,980✔
1013
    gt_values_view = gt_values_reduced;
16,485✔
1014
  } else {
1015
    gt_values_view = 0.0;
16,495✔
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,980✔
1022
    0, mpi::intracomm);
1023
  if (mpi::master)
32,980✔
1024
    simulation::total_weight = weight_reduced;
16,485✔
1025
}
32,980✔
1026
#endif
1027

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

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

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

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

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

1068
  // Accumulate results for each tally
1069
  for (int i_tally : model::active_tallies) {
327,454✔
1070
    auto& tally {model::tallies[i_tally]};
212,038✔
1071
    tally->accumulate();
212,038✔
1072
  }
1073
}
115,416✔
1074

1075
double distance_to_time_boundary(double time, double speed)
7,787,043✔
1076
{
1077
  if (model::time_grid.empty()) {
7,787,043✔
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✔
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()
115,428✔
1109
{
1110
  model::active_tallies.clear();
115,428✔
1111
  model::active_analog_tallies.clear();
115,428✔
1112
  model::active_particleout_analog_tallies.clear();
115,428✔
1113
  model::active_tracklength_tallies.clear();
115,428✔
1114
  model::active_timed_tracklength_tallies.clear();
115,428✔
1115
  model::active_collision_tallies.clear();
115,428✔
1116
  model::active_meshsurf_tallies.clear();
115,428✔
1117
  model::active_surface_tallies.clear();
115,428✔
1118
  model::active_pulse_height_tallies.clear();
115,428✔
1119
  model::time_grid.clear();
115,428✔
1120

1121
  for (auto i = 0; i < model::tallies.size(); ++i) {
456,852✔
1122
    const auto& tally {*model::tallies[i]};
341,424✔
1123

1124
    if (tally.active_) {
341,424✔
1125
      model::active_tallies.push_back(i);
212,038✔
1126
      bool mesh_present = (tally.get_filter<MeshFilter>() ||
373,836✔
1127
                           tally.get_filter<MeshMaterialFilter>());
161,798✔
1128
      auto time_filter = tally.get_filter<TimeFilter>();
212,038✔
1129
      bool particleout_present = tally.has_filter(FilterType::PARTICLE_OUT);
212,038✔
1130
      switch (tally.type_) {
212,038✔
1131

1132
      case TallyType::VOLUME:
205,545✔
1133
        switch (tally.estimator_) {
205,545✔
1134
        case TallyEstimator::ANALOG:
81,203✔
1135
          model::active_analog_tallies.push_back(i);
81,203✔
1136
          if (particleout_present)
81,203✔
NEW
1137
            model::active_particleout_analog_tallies.push_back(i);
×
1138
          break;
81,203✔
1139
        case TallyEstimator::TRACKLENGTH:
119,064✔
1140
          if (time_filter && mesh_present) {
119,064✔
1141
            model::active_timed_tracklength_tallies.push_back(i);
220✔
1142
            add_to_time_grid(time_filter->bins());
220✔
1143
          } else {
1144
            model::active_tracklength_tallies.push_back(i);
118,844✔
1145
          }
1146
          break;
119,064✔
1147
        case TallyEstimator::COLLISION:
5,278✔
1148
          model::active_collision_tallies.push_back(i);
5,278✔
1149
        }
1150
        break;
205,545✔
1151

1152
      case TallyType::MESH_SURFACE:
4,311✔
1153
        model::active_meshsurf_tallies.push_back(i);
4,311✔
1154
        break;
4,311✔
1155

1156
      case TallyType::SURFACE:
2,102✔
1157
        model::active_surface_tallies.push_back(i);
2,102✔
1158
        break;
2,102✔
1159

1160
      case TallyType::PULSE_HEIGHT:
80✔
1161
        model::active_pulse_height_tallies.push_back(i);
80✔
1162
        break;
80✔
1163
      }
1164
    }
1165
  }
1166
}
115,428✔
1167

1168
void free_memory_tally()
7,733✔
1169
{
1170
  model::tally_derivs.clear();
7,733✔
1171
  model::tally_deriv_map.clear();
7,733✔
1172

1173
  model::tally_filters.clear();
7,733✔
1174
  model::filter_map.clear();
7,733✔
1175

1176
  model::tallies.clear();
7,733✔
1177

1178
  model::active_tallies.clear();
7,733✔
1179
  model::active_analog_tallies.clear();
7,733✔
1180
  model::active_particleout_analog_tallies.clear();
7,733✔
1181
  model::active_tracklength_tallies.clear();
7,733✔
1182
  model::active_timed_tracklength_tallies.clear();
7,733✔
1183
  model::active_collision_tallies.clear();
7,733✔
1184
  model::active_meshsurf_tallies.clear();
7,733✔
1185
  model::active_surface_tallies.clear();
7,733✔
1186
  model::active_pulse_height_tallies.clear();
7,733✔
1187
  model::time_grid.clear();
7,733✔
1188

1189
  model::tally_map.clear();
7,733✔
1190
}
7,733✔
1191

1192
//==============================================================================
1193
// C-API functions
1194
//==============================================================================
1195

1196
extern "C" int openmc_extend_tallies(
1,495✔
1197
  int32_t n, int32_t* index_start, int32_t* index_end)
1198
{
1199
  if (index_start)
1,495✔
1200
    *index_start = model::tallies.size();
1,495✔
1201
  if (index_end)
1,495✔
1202
    *index_end = model::tallies.size() + n - 1;
×
1203
  for (int i = 0; i < n; ++i) {
2,990✔
1204
    model::tallies.push_back(make_unique<Tally>(-1));
1,495✔
1205
  }
1206
  return 0;
1,495✔
1207
}
1208

1209
extern "C" int openmc_get_tally_index(int32_t id, int32_t* index)
22,848✔
1210
{
1211
  auto it = model::tally_map.find(id);
22,848✔
1212
  if (it == model::tally_map.end()) {
22,848✔
1213
    set_errmsg(fmt::format("No tally exists with ID={}.", id));
26✔
1214
    return OPENMC_E_INVALID_ID;
26✔
1215
  }
1216

1217
  *index = it->second;
22,822✔
1218
  return 0;
22,822✔
1219
}
1220

1221
extern "C" void openmc_get_tally_next_id(int32_t* id)
×
1222
{
1223
  int32_t largest_tally_id = 0;
×
1224
  for (const auto& t : model::tallies) {
×
1225
    largest_tally_id = std::max(largest_tally_id, t->id_);
×
1226
  }
1227
  *id = largest_tally_id + 1;
×
1228
}
1229

1230
extern "C" int openmc_tally_get_estimator(int32_t index, int* estimator)
×
1231
{
1232
  if (index < 0 || index >= model::tallies.size()) {
×
1233
    set_errmsg("Index in tallies array is out of bounds.");
×
1234
    return OPENMC_E_OUT_OF_BOUNDS;
×
1235
  }
1236

1237
  *estimator = static_cast<int>(model::tallies[index]->estimator_);
×
1238
  return 0;
×
1239
}
1240

1241
extern "C" int openmc_tally_set_estimator(int32_t index, const char* estimator)
720✔
1242
{
1243
  if (index < 0 || index >= model::tallies.size()) {
720✔
1244
    set_errmsg("Index in tallies array is out of bounds.");
×
1245
    return OPENMC_E_OUT_OF_BOUNDS;
×
1246
  }
1247

1248
  auto& t {model::tallies[index]};
720✔
1249

1250
  std::string est = estimator;
720✔
1251
  if (est == "analog") {
720✔
1252
    t->estimator_ = TallyEstimator::ANALOG;
720✔
1253
  } else if (est == "collision") {
×
1254
    t->estimator_ = TallyEstimator::COLLISION;
×
1255
  } else if (est == "tracklength") {
×
1256
    t->estimator_ = TallyEstimator::TRACKLENGTH;
×
1257
  } else {
1258
    set_errmsg("Unknown tally estimator: " + est);
×
1259
    return OPENMC_E_INVALID_ARGUMENT;
×
1260
  }
1261
  return 0;
720✔
1262
}
720✔
1263

1264
extern "C" int openmc_tally_get_id(int32_t index, int32_t* id)
31,776✔
1265
{
1266
  if (index < 0 || index >= model::tallies.size()) {
31,776✔
1267
    set_errmsg("Index in tallies array is out of bounds.");
×
1268
    return OPENMC_E_OUT_OF_BOUNDS;
×
1269
  }
1270

1271
  *id = model::tallies[index]->id_;
31,776✔
1272
  return 0;
31,776✔
1273
}
1274

1275
extern "C" int openmc_tally_set_id(int32_t index, int32_t id)
1,495✔
1276
{
1277
  if (index < 0 || index >= model::tallies.size()) {
1,495✔
1278
    set_errmsg("Index in tallies array is out of bounds.");
×
1279
    return OPENMC_E_OUT_OF_BOUNDS;
×
1280
  }
1281

1282
  model::tallies[index]->set_id(id);
1,495✔
1283
  return 0;
1,495✔
1284
}
1285

1286
extern "C" int openmc_tally_get_type(int32_t index, int32_t* type)
13✔
1287
{
1288
  if (index < 0 || index >= model::tallies.size()) {
13✔
1289
    set_errmsg("Index in tallies array is out of bounds.");
×
1290
    return OPENMC_E_OUT_OF_BOUNDS;
×
1291
  }
1292
  *type = static_cast<int>(model::tallies[index]->type_);
13✔
1293

1294
  return 0;
13✔
1295
}
1296

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

1316
  return 0;
720✔
1317
}
1318

1319
extern "C" int openmc_tally_get_active(int32_t index, bool* active)
26✔
1320
{
1321
  if (index < 0 || index >= model::tallies.size()) {
26✔
1322
    set_errmsg("Index in tallies array is out of bounds.");
×
1323
    return OPENMC_E_OUT_OF_BOUNDS;
×
1324
  }
1325
  *active = model::tallies[index]->active_;
26✔
1326

1327
  return 0;
26✔
1328
}
1329

1330
extern "C" int openmc_tally_set_active(int32_t index, bool active)
733✔
1331
{
1332
  if (index < 0 || index >= model::tallies.size()) {
733✔
1333
    set_errmsg("Index in tallies array is out of bounds.");
×
1334
    return OPENMC_E_OUT_OF_BOUNDS;
×
1335
  }
1336
  model::tallies[index]->active_ = active;
733✔
1337

1338
  return 0;
733✔
1339
}
1340

1341
extern "C" int openmc_tally_get_writable(int32_t index, bool* writable)
26✔
1342
{
1343
  if (index < 0 || index >= model::tallies.size()) {
26✔
1344
    set_errmsg("Index in tallies array is out of bounds.");
×
1345
    return OPENMC_E_OUT_OF_BOUNDS;
×
1346
  }
1347
  *writable = model::tallies[index]->writable();
26✔
1348

1349
  return 0;
26✔
1350
}
1351

1352
extern "C" int openmc_tally_set_writable(int32_t index, bool writable)
775✔
1353
{
1354
  if (index < 0 || index >= model::tallies.size()) {
775✔
1355
    set_errmsg("Index in tallies array is out of bounds.");
×
1356
    return OPENMC_E_OUT_OF_BOUNDS;
×
1357
  }
1358
  model::tallies[index]->set_writable(writable);
775✔
1359

1360
  return 0;
775✔
1361
}
1362

1363
extern "C" int openmc_tally_get_multiply_density(int32_t index, bool* value)
26✔
1364
{
1365
  if (index < 0 || index >= model::tallies.size()) {
26✔
1366
    set_errmsg("Index in tallies array is out of bounds.");
×
1367
    return OPENMC_E_OUT_OF_BOUNDS;
×
1368
  }
1369
  *value = model::tallies[index]->multiply_density();
26✔
1370

1371
  return 0;
26✔
1372
}
1373

1374
extern "C" int openmc_tally_set_multiply_density(int32_t index, bool value)
353✔
1375
{
1376
  if (index < 0 || index >= model::tallies.size()) {
353✔
1377
    set_errmsg("Index in tallies array is out of bounds.");
×
1378
    return OPENMC_E_OUT_OF_BOUNDS;
×
1379
  }
1380
  model::tallies[index]->set_multiply_density(value);
353✔
1381

1382
  return 0;
353✔
1383
}
1384

1385
extern "C" int openmc_tally_get_scores(int32_t index, int** scores, int* n)
506✔
1386
{
1387
  if (index < 0 || index >= model::tallies.size()) {
506✔
1388
    set_errmsg("Index in tallies array is out of bounds.");
×
1389
    return OPENMC_E_OUT_OF_BOUNDS;
×
1390
  }
1391

1392
  *scores = model::tallies[index]->scores_.data();
506✔
1393
  *n = model::tallies[index]->scores_.size();
506✔
1394
  return 0;
506✔
1395
}
1396

1397
extern "C" int openmc_tally_set_scores(
1,508✔
1398
  int32_t index, int n, const char** scores)
1399
{
1400
  if (index < 0 || index >= model::tallies.size()) {
1,508✔
1401
    set_errmsg("Index in tallies array is out of bounds.");
×
1402
    return OPENMC_E_OUT_OF_BOUNDS;
×
1403
  }
1404

1405
  vector<std::string> scores_str(scores, scores + n);
1,508✔
1406
  try {
1407
    model::tallies[index]->set_scores(scores_str);
1,508✔
1408
  } catch (const std::invalid_argument& ex) {
×
1409
    set_errmsg(ex.what());
×
1410
    return OPENMC_E_INVALID_ARGUMENT;
×
1411
  }
×
1412

1413
  return 0;
1,508✔
1414
}
1,508✔
1415

1416
extern "C" int openmc_tally_get_nuclides(int32_t index, int** nuclides, int* n)
639✔
1417
{
1418
  // Make sure the index fits in the array bounds.
1419
  if (index < 0 || index >= model::tallies.size()) {
639✔
1420
    set_errmsg("Index in tallies array is out of bounds.");
×
1421
    return OPENMC_E_OUT_OF_BOUNDS;
×
1422
  }
1423

1424
  *n = model::tallies[index]->nuclides_.size();
639✔
1425
  *nuclides = model::tallies[index]->nuclides_.data();
639✔
1426

1427
  return 0;
639✔
1428
}
1429

1430
extern "C" int openmc_tally_set_nuclides(
1,788✔
1431
  int32_t index, int n, const char** nuclides)
1432
{
1433
  // Make sure the index fits in the array bounds.
1434
  if (index < 0 || index >= model::tallies.size()) {
1,788✔
1435
    set_errmsg("Index in tallies array is out of bounds.");
×
1436
    return OPENMC_E_OUT_OF_BOUNDS;
×
1437
  }
1438

1439
  vector<std::string> words(nuclides, nuclides + n);
1,788✔
1440
  vector<int> nucs;
1,788✔
1441
  for (auto word : words) {
7,429✔
1442
    if (word == "total") {
5,654✔
1443
      nucs.push_back(-1);
720✔
1444
    } else {
1445
      auto search = data::nuclide_map.find(word);
4,934✔
1446
      if (search == data::nuclide_map.end()) {
4,934✔
1447
        int err = openmc_load_nuclide(word.c_str(), nullptr, 0);
953✔
1448
        if (err < 0) {
953✔
1449
          set_errmsg(openmc_err_msg);
13✔
1450
          return OPENMC_E_DATA;
13✔
1451
        }
1452
      }
1453
      nucs.push_back(data::nuclide_map.at(word));
4,921✔
1454
    }
1455
  }
5,654✔
1456

1457
  model::tallies[index]->nuclides_ = nucs;
1,775✔
1458

1459
  return 0;
1,775✔
1460
}
1,788✔
1461

1462
extern "C" int openmc_tally_get_filters(
1,581✔
1463
  int32_t index, const int32_t** indices, size_t* n)
1464
{
1465
  if (index < 0 || index >= model::tallies.size()) {
1,581✔
1466
    set_errmsg("Index in tallies array is out of bounds.");
×
1467
    return OPENMC_E_OUT_OF_BOUNDS;
×
1468
  }
1469

1470
  *indices = model::tallies[index]->filters().data();
1,581✔
1471
  *n = model::tallies[index]->filters().size();
1,581✔
1472
  return 0;
1,581✔
1473
}
1474

1475
extern "C" int openmc_tally_set_filters(
1,674✔
1476
  int32_t index, size_t n, const int32_t* indices)
1477
{
1478
  // Make sure the index fits in the array bounds.
1479
  if (index < 0 || index >= model::tallies.size()) {
1,674✔
1480
    set_errmsg("Index in tallies array is out of bounds.");
×
1481
    return OPENMC_E_OUT_OF_BOUNDS;
×
1482
  }
1483

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

1498
  return 0;
1,674✔
1499
}
1500

1501
//! Reset tally results and number of realizations
1502
extern "C" int openmc_tally_reset(int32_t index)
2,928✔
1503
{
1504
  // Make sure the index fits in the array bounds.
1505
  if (index < 0 || index >= model::tallies.size()) {
2,928✔
1506
    set_errmsg("Index in tallies array is out of bounds.");
×
1507
    return OPENMC_E_OUT_OF_BOUNDS;
×
1508
  }
1509

1510
  model::tallies[index]->reset();
2,928✔
1511
  return 0;
2,928✔
1512
}
1513

1514
extern "C" int openmc_tally_get_n_realizations(int32_t index, int32_t* n)
4,014✔
1515
{
1516
  // Make sure the index fits in the array bounds.
1517
  if (index < 0 || index >= model::tallies.size()) {
4,014✔
1518
    set_errmsg("Index in tallies array is out of bounds.");
×
1519
    return OPENMC_E_OUT_OF_BOUNDS;
×
1520
  }
1521

1522
  *n = model::tallies[index]->n_realizations_;
4,014✔
1523
  return 0;
4,014✔
1524
}
1525

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

1537
  const auto& t {model::tallies[index]};
17,642✔
1538
  if (t->results_.size() == 0) {
17,642✔
1539
    set_errmsg("Tally results have not been allocated yet.");
×
1540
    return OPENMC_E_ALLOCATE;
×
1541
  }
1542

1543
  // Set pointer to results and copy shape
1544
  *results = t->results_.data();
17,642✔
1545
  auto s = t->results_.shape();
17,642✔
1546
  shape[0] = s[0];
17,642✔
1547
  shape[1] = s[1];
17,642✔
1548
  shape[2] = s[2];
17,642✔
1549
  return 0;
17,642✔
1550
}
1551

1552
extern "C" int openmc_global_tallies(double** ptr)
13✔
1553
{
1554
  *ptr = simulation::global_tallies.data();
13✔
1555
  return 0;
13✔
1556
}
1557

1558
extern "C" size_t tallies_size()
1,547✔
1559
{
1560
  return model::tallies.size();
1,547✔
1561
}
1562

1563
// given a tally ID, remove it from the tallies vector
1564
extern "C" int openmc_remove_tally(int32_t index)
13✔
1565
{
1566
  // check that id is in the map
1567
  if (index < 0 || index >= model::tallies.size()) {
13✔
1568
    set_errmsg("Index in tallies array is out of bounds.");
×
1569
    return OPENMC_E_OUT_OF_BOUNDS;
×
1570
  }
1571

1572
  // delete the tally via iterator pointing to correct position
1573
  // this calls the Tally destructor, removing the tally from the map as well
1574
  model::tallies.erase(model::tallies.begin() + index);
13✔
1575

1576
  return 0;
13✔
1577
}
1578

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