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

openmc-dev / openmc / 23494067097

24 Mar 2026 02:14PM UTC coverage: 81.299% (-0.03%) from 81.326%
23494067097

Pull #3863

github

web-flow
Merge fc121e706 into 6cd39073b
Pull Request #3863: Shared Secondary Particle Bank

17806 of 25682 branches covered (69.33%)

Branch coverage included in aggregate %.

418 of 435 new or added lines in 17 files covered. (96.09%)

130 existing lines in 4 files now uncovered.

58438 of 68100 relevant lines covered (85.81%)

46091568.19 hits per line

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

85.36
/src/particle.cpp
1
#include "openmc/particle.h"
2

3
#include <algorithm> // copy, min
4
#include <cmath>     // log, abs
5

6
#include <fmt/core.h>
7

8
#include "openmc/bank.h"
9
#include "openmc/capi.h"
10
#include "openmc/cell.h"
11
#include "openmc/collision_track.h"
12
#include "openmc/constants.h"
13
#include "openmc/dagmc.h"
14
#include "openmc/error.h"
15
#include "openmc/geometry.h"
16
#include "openmc/hdf5_interface.h"
17
#include "openmc/lattice.h"
18
#include "openmc/material.h"
19
#include "openmc/message_passing.h"
20
#include "openmc/mgxs_interface.h"
21
#include "openmc/nuclide.h"
22
#include "openmc/particle_data.h"
23
#include "openmc/photon.h"
24
#include "openmc/physics.h"
25
#include "openmc/physics_mg.h"
26
#include "openmc/random_lcg.h"
27
#include "openmc/settings.h"
28
#include "openmc/simulation.h"
29
#include "openmc/source.h"
30
#include "openmc/surface.h"
31
#include "openmc/tallies/derivative.h"
32
#include "openmc/tallies/tally.h"
33
#include "openmc/tallies/tally_scoring.h"
34
#include "openmc/track_output.h"
35
#include "openmc/weight_windows.h"
36

37
#ifdef OPENMC_DAGMC_ENABLED
38
#include "DagMC.hpp"
39
#endif
40

41
namespace openmc {
42

43
//==============================================================================
44
// Particle implementation
45
//==============================================================================
46

47
double Particle::speed() const
2,147,483,647✔
48
{
49
  if (settings::run_CE) {
2,147,483,647✔
50
    // Determine mass in eV/c^2
51
    double mass;
2,147,483,647✔
52
    switch (type().pdg_number()) {
2,147,483,647✔
53
    case PDG_NEUTRON:
54
      mass = MASS_NEUTRON_EV;
55
    case PDG_ELECTRON:
56
    case PDG_POSITRON:
57
      mass = MASS_ELECTRON_EV;
2,147,483,647✔
58
    default:
2,147,483,647✔
59
      mass = this->type().mass() * AMU_EV;
2,147,483,647✔
60
    }
61

62
    // Equivalent to C * sqrt(1-(m/(m+E))^2) without problem at E<<m:
63
    return C_LIGHT * std::sqrt(this->E() * (this->E() + 2 * mass)) /
2,147,483,647✔
64
           (this->E() + mass);
2,147,483,647✔
65
  } else {
66
    auto& macro_xs = data::mg.macro_xs_[this->material()];
2,081,363,471✔
67
    int macro_t = this->mg_xs_cache().t;
2,081,363,471✔
68
    int macro_a = macro_xs.get_angle_index(this->u());
2,081,363,471✔
69
    return 1.0 / macro_xs.get_xs(MgxsType::INVERSE_VELOCITY, this->g(), nullptr,
2,081,363,471✔
70
                   nullptr, nullptr, macro_t, macro_a);
2,081,363,471✔
71
  }
72
}
73

74
bool Particle::create_secondary(
179,054,175✔
75
  double wgt, Direction u, double E, ParticleType type)
76
{
77
  // If energy is below cutoff for this particle, don't create secondary
78
  // particle
79
  int idx = type.transport_index();
179,054,175✔
80
  if (idx == C_NONE) {
179,054,175!
81
    return false;
82
  }
83
  if (E < settings::energy_cutoff[idx]) {
179,054,175✔
84
    return false;
85
  }
86

87
  // Increment number of secondaries created (for ParticleProductionFilter)
88
  n_secondaries()++;
93,353,675✔
89

90
  SourceSite bank;
93,353,675✔
91
  bank.particle = type;
93,353,675✔
92
  bank.wgt = wgt;
93,353,675✔
93
  bank.r = r();
93,353,675!
94
  bank.u = u;
93,353,675✔
95
  bank.E = settings::run_CE ? E : g();
93,353,675!
96
  bank.time = time();
93,353,675✔
97
  bank_second_E() += bank.E;
93,353,675✔
98
  bank.parent_id = current_work();
93,353,675✔
99
  if (settings::use_shared_secondary_bank) {
93,353,675✔
100
    bank.progeny_id = n_progeny()++;
15,377,814✔
101
  }
102
  bank.wgt_born = wgt_born();
93,353,675✔
103
  bank.wgt_ww_born = wgt_ww_born();
93,353,675✔
104
  bank.n_split = n_split();
93,353,675✔
105

106
  // In shared secondary mode, subtract secondary photon energy from parent's
107
  // pulse-height storage now, since the secondary will be transported as a
108
  // separate Particle object and won't have access to the parent's pht_storage.
109
  if (settings::use_shared_secondary_bank &&
108,731,489✔
110
      !model::active_pulse_height_tallies.empty() && type.is_photon()) {
93,353,675!
NEW
111
    auto it = std::find(model::pulse_height_cells.begin(),
×
NEW
112
      model::pulse_height_cells.end(), lowest_coord().cell());
×
NEW
113
    if (it != model::pulse_height_cells.end()) {
×
NEW
114
      int index = std::distance(model::pulse_height_cells.begin(), it);
×
NEW
115
      pht_storage()[index] -= bank.E;
×
116
    }
117
  }
118

119
  local_secondary_bank().emplace_back(bank);
93,353,675✔
120
  return true;
121
}
122

123
void Particle::split(double wgt)
10,373,211✔
124
{
125
  SourceSite bank;
10,373,211✔
126
  bank.particle = type();
10,373,211✔
127
  bank.wgt = wgt;
10,373,211✔
128
  bank.r = r();
10,373,211✔
129
  bank.u = u();
10,373,211✔
130
  bank.E = settings::run_CE ? E() : g();
10,373,211✔
131
  bank.time = time();
10,373,211✔
132

133
  // Convert signed index to a signed surface ID
134
  if (surface() == SURFACE_NONE) {
10,373,211✔
135
    bank.surf_id = SURFACE_NONE;
10,369,339✔
136
  } else {
137
    int surf_id = model::surfaces[surface_index()]->id_;
3,872✔
138
    bank.surf_id = (surface() > 0) ? surf_id : -surf_id;
3,872✔
139
  }
140

141
  bank.wgt_born = wgt_born();
10,373,211✔
142
  bank.wgt_ww_born = wgt_ww_born();
10,373,211✔
143
  bank.n_split = n_split();
10,373,211✔
144
  bank.parent_id = current_work();
10,373,211✔
145
  if (settings::use_shared_secondary_bank) {
10,373,211✔
146
    bank.progeny_id = n_progeny()++;
5,205,367✔
147
  }
148

149
  local_secondary_bank().emplace_back(bank);
10,373,211✔
150
}
10,373,211✔
151

152
void Particle::from_source(const SourceSite* src)
282,520,827✔
153
{
154
  // Reset some attributes
155
  clear();
282,520,827✔
156
  surface() = SURFACE_NONE;
282,520,827✔
157
  cell_born() = C_NONE;
282,520,827✔
158
  material() = C_NONE;
282,520,827✔
159
  n_collision() = 0;
282,520,827✔
160
  fission() = false;
282,520,827✔
161
  zero_flux_derivs();
282,520,827✔
162
  lifetime() = 0.0;
282,520,827✔
163
#ifdef OPENMC_DAGMC_ENABLED
164
  history().reset();
25,840,170✔
165
#endif
166

167
  // Copy attributes from source bank site
168
  type() = src->particle;
282,520,827✔
169
  wgt() = src->wgt;
282,520,827✔
170
  wgt_last() = src->wgt;
282,520,827✔
171
  r() = src->r;
282,520,827✔
172
  u() = src->u;
282,520,827✔
173
  r_born() = src->r;
282,520,827✔
174
  r_last_current() = src->r;
282,520,827✔
175
  r_last() = src->r;
282,520,827✔
176
  u_last() = src->u;
282,520,827✔
177
  if (settings::run_CE) {
282,520,827✔
178
    E() = src->E;
165,831,293✔
179
    g() = 0;
165,831,293✔
180
  } else {
181
    g() = static_cast<int>(src->E);
116,689,534✔
182
    g_last() = static_cast<int>(src->E);
116,689,534✔
183
    E() = data::mg.energy_bin_avg_[g()];
116,689,534✔
184
  }
185
  E_last() = E();
282,520,827✔
186
  time() = src->time;
282,520,827✔
187
  time_last() = src->time;
282,520,827✔
188
  parent_nuclide() = src->parent_nuclide;
282,520,827✔
189
  delayed_group() = src->delayed_group;
282,520,827✔
190

191
  // Convert signed surface ID to signed index
192
  if (src->surf_id != SURFACE_NONE) {
282,520,827✔
193
    int index_plus_one = model::surface_map[std::abs(src->surf_id)] + 1;
113,872✔
194
    surface() = (src->surf_id > 0) ? index_plus_one : -index_plus_one;
113,872✔
195
  }
196

197
  wgt_born() = src->wgt_born;
282,520,827✔
198
  wgt_ww_born() = src->wgt_ww_born;
282,520,827✔
199
  n_split() = src->n_split;
282,520,827✔
200
}
282,520,827✔
201

202
void Particle::event_calculate_xs()
2,147,483,647✔
203
{
204
  // Set the random number stream
205
  stream() = STREAM_TRACKING;
2,147,483,647✔
206

207
  // Store pre-collision particle properties
208
  wgt_last() = wgt();
2,147,483,647✔
209
  E_last() = E();
2,147,483,647✔
210
  u_last() = u();
2,147,483,647✔
211
  r_last() = r();
2,147,483,647✔
212
  time_last() = time();
2,147,483,647✔
213

214
  // Reset event variables
215
  event() = TallyEvent::KILL;
2,147,483,647✔
216
  event_nuclide() = NUCLIDE_NONE;
2,147,483,647✔
217
  event_mt() = REACTION_NONE;
2,147,483,647✔
218

219
  // If the cell hasn't been determined based on the particle's location,
220
  // initiate a search for the current cell. This generally happens at the
221
  // beginning of the history and again for any secondary particles
222
  if (lowest_coord().cell() == C_NONE) {
2,147,483,647✔
223
    if (!exhaustive_find_cell(*this)) {
273,848,114!
224
      mark_as_lost(
×
225
        "Could not find the cell containing particle " + std::to_string(id()));
×
226
      return;
×
227
    }
228

229
    // Set birth cell attribute
230
    if (cell_born() == C_NONE)
273,848,114!
231
      cell_born() = lowest_coord().cell();
273,848,114✔
232

233
    // Initialize last cells from current cell
234
    for (int j = 0; j < n_coord(); ++j) {
565,091,647✔
235
      cell_last(j) = coord(j).cell();
291,243,533✔
236
    }
237
    n_coord_last() = n_coord();
273,848,114✔
238
  }
239

240
  // Write particle track.
241
  if (write_track())
2,147,483,647✔
242
    write_particle_track(*this);
10,313✔
243

244
  if (settings::check_overlaps)
2,147,483,647!
245
    check_cell_overlap(*this);
×
246

247
  // Calculate microscopic and macroscopic cross sections
248
  if (material() != MATERIAL_VOID) {
2,147,483,647✔
249
    if (settings::run_CE) {
2,147,483,647✔
250
      if (material() != material_last() || sqrtkT() != sqrtkT_last() ||
2,147,483,647✔
251
          density_mult() != density_mult_last()) {
385,409,811✔
252
        // If the material is the same as the last material and the
253
        // temperature hasn't changed, we don't need to lookup cross
254
        // sections again.
255
        model::materials[material()]->calculate_xs(*this);
1,971,746,277✔
256
      }
257
    } else {
258
      // Get the MG data; unlike the CE case above, we have to re-calculate
259
      // cross sections for every collision since the cross sections may
260
      // be angle-dependent
261
      data::mg.macro_xs_[material()].calculate_xs(*this);
2,081,363,471✔
262

263
      // Update the particle's group while we know we are multi-group
264
      g_last() = g();
2,081,363,471✔
265
    }
266
  } else {
267
    macro_xs().total = 0.0;
111,862,112✔
268
    macro_xs().absorption = 0.0;
111,862,112✔
269
    macro_xs().fission = 0.0;
111,862,112✔
270
    macro_xs().nu_fission = 0.0;
111,862,112✔
271
  }
272
}
273

274
void Particle::event_advance()
2,147,483,647✔
275
{
276
  // Find the distance to the nearest boundary
277
  boundary() = distance_to_boundary(*this);
2,147,483,647✔
278

279
  // Sample a distance to collision
280
  if (type() == ParticleType::electron() ||
2,147,483,647✔
281
      type() == ParticleType::positron()) {
2,147,483,647✔
282
    collision_distance() = material() == MATERIAL_VOID ? INFINITY : 0.0;
172,341,226!
283
  } else if (macro_xs().total == 0.0) {
2,147,483,647✔
284
    collision_distance() = INFINITY;
111,862,112✔
285
  } else {
286
    collision_distance() = -std::log(prn(current_seed())) / macro_xs().total;
2,147,483,647✔
287
  }
288

289
  double speed = this->speed();
2,147,483,647✔
290
  double time_cutoff = settings::time_cutoff[type().transport_index()];
2,147,483,647✔
291
  double distance_cutoff =
2,147,483,647✔
292
    (time_cutoff < INFTY) ? (time_cutoff - time()) * speed : INFTY;
2,147,483,647✔
293

294
  // Select smaller of the three distances
295
  double distance =
2,147,483,647✔
296
    std::min({boundary().distance(), collision_distance(), distance_cutoff});
2,147,483,647✔
297

298
  // Advance particle in space and time
299
  this->move_distance(distance);
2,147,483,647✔
300
  double dt = distance / speed;
2,147,483,647✔
301
  this->time() += dt;
2,147,483,647✔
302
  this->lifetime() += dt;
2,147,483,647✔
303

304
  // Score timed track-length tallies
305
  if (!model::active_timed_tracklength_tallies.empty()) {
2,147,483,647✔
306
    score_timed_tracklength_tally(*this, distance);
3,628,317✔
307
  }
308

309
  // Score track-length tallies
310
  if (!model::active_tracklength_tallies.empty()) {
2,147,483,647✔
311
    score_tracklength_tally(*this, distance);
1,929,727,574✔
312
  }
313

314
  // Score track-length estimate of k-eff
315
  if (settings::run_mode == RunMode::EIGENVALUE && type().is_neutron()) {
2,147,483,647✔
316
    keff_tally_tracklength() += wgt() * distance * macro_xs().nu_fission;
2,147,483,647✔
317
  }
318

319
  // Score flux derivative accumulators for differential tallies.
320
  if (!model::active_tallies.empty()) {
2,147,483,647✔
321
    score_track_derivative(*this, distance);
2,100,110,642✔
322
  }
323

324
  // Set particle weight to zero if it hit the time boundary
325
  if (distance == distance_cutoff) {
2,147,483,647✔
326
    wgt() = 0.0;
224,928✔
327
  }
328
}
2,147,483,647✔
329

330
void Particle::event_cross_surface()
2,147,483,647✔
331
{
332
  // Saving previous cell data
333
  for (int j = 0; j < n_coord(); ++j) {
2,147,483,647✔
334
    cell_last(j) = coord(j).cell();
2,147,483,647✔
335
  }
336
  n_coord_last() = n_coord();
2,147,483,647✔
337

338
  // Set surface that particle is on and adjust coordinate levels
339
  surface() = boundary().surface();
2,147,483,647✔
340
  n_coord() = boundary().coord_level();
2,147,483,647✔
341

342
  if (boundary().lattice_translation()[0] != 0 ||
2,147,483,647✔
343
      boundary().lattice_translation()[1] != 0 ||
2,147,483,647✔
344
      boundary().lattice_translation()[2] != 0) {
1,861,001,512✔
345
    // Particle crosses lattice boundary
346

347
    bool verbose = settings::verbosity >= 10 || trace();
749,891,403!
348
    cross_lattice(*this, boundary(), verbose);
749,891,403✔
349
    event() = TallyEvent::LATTICE;
749,891,403✔
350

351
    // Score cell to cell partial currents
352
    if (!model::active_surface_tallies.empty()) {
749,891,403!
353
      auto& lat {*model::lattices[lowest_coord().lattice()]};
×
354
      bool is_valid;
×
355
      Direction normal =
×
356
        lat.get_normal(boundary().lattice_translation(), is_valid);
×
357
      if (is_valid) {
×
358
        normal /= normal.norm();
×
359
        score_surface_tally(*this, model::active_surface_tallies, normal);
×
360
      }
361
    }
362

363
  } else {
364

365
    const auto& surf {*model::surfaces[surface_index()].get()};
1,672,561,262✔
366

367
    // Particle crosses surface
368
    // If BC, add particle to surface source before crossing surface
369
    if (surf.surf_source_ && surf.bc_) {
1,672,561,262✔
370
      add_surf_source_to_bank(*this, surf);
720,435,033✔
371
    }
372
    this->cross_surface(surf);
1,672,561,262✔
373
    // If no BC, add particle to surface source after crossing surface
374
    if (surf.surf_source_ && !surf.bc_) {
1,672,561,253✔
375
      add_surf_source_to_bank(*this, surf);
950,888,393✔
376
    }
377
    if (settings::weight_window_checkpoint_surface) {
1,672,561,253✔
378
      apply_weight_windows(*this);
173,985✔
379
    }
380
    event() = TallyEvent::SURFACE;
1,672,561,253✔
381

382
    // Score cell to cell partial currents
383
    if (!model::active_surface_tallies.empty()) {
1,672,561,253✔
384
      Direction normal = surf.normal(r());
34,931,567✔
385
      normal /= normal.norm();
34,931,567✔
386
      score_surface_tally(*this, model::active_surface_tallies, normal);
34,931,567✔
387
    }
388
  }
389
}
2,147,483,647✔
390

391
void Particle::event_collide()
2,147,483,647✔
392
{
393

394
  // Score collision estimate of keff
395
  if (settings::run_mode == RunMode::EIGENVALUE && type().is_neutron()) {
2,147,483,647✔
396
    keff_tally_collision() += wgt() * macro_xs().nu_fission / macro_xs().total;
2,139,390,664✔
397
  }
398

399
  // Score surface current tallies -- this has to be done before the collision
400
  // since the direction of the particle will change and we need to use the
401
  // pre-collision direction to figure out what mesh surfaces were crossed
402

403
  if (!model::active_meshsurf_tallies.empty())
2,147,483,647✔
404
    score_meshsurface_tally(*this, model::active_meshsurf_tallies);
63,098,926✔
405

406
  // Clear surface component
407
  surface() = SURFACE_NONE;
2,147,483,647✔
408

409
  if (settings::run_CE) {
2,147,483,647✔
410
    collision(*this);
1,261,539,588✔
411
  } else {
412
    collision_mg(*this);
1,800,818,030✔
413
  }
414

415
  // Collision track feature to recording particle interaction
416
  if (settings::collision_track) {
2,147,483,647✔
417
    collision_track_record(*this);
150,087✔
418
  }
419

420
  // Score collision estimator tallies -- this is done after a collision
421
  // has occurred rather than before because we need information on the
422
  // outgoing energy for any tallies with an outgoing energy filter
423
  if (!model::active_collision_tallies.empty())
2,147,483,647✔
424
    score_collision_tally(*this);
108,955,046✔
425
  if (!model::active_analog_tallies.empty()) {
2,147,483,647✔
426
    if (settings::run_CE) {
406,059,376✔
427
      score_analog_tally_ce(*this);
404,851,114✔
428
    } else {
429
      score_analog_tally_mg(*this);
1,208,262✔
430
    }
431
  }
432

433
  if (!model::active_pulse_height_tallies.empty() && type().is_photon()) {
2,147,483,647✔
434
    pht_collision_energy();
8,096✔
435
  }
436

437
  // Reset banked weight during collision
438
  n_bank() = 0;
2,147,483,647✔
439
  bank_second_E() = 0.0;
2,147,483,647✔
440
  wgt_bank() = 0.0;
2,147,483,647✔
441

442
  // Clear number of secondaries in this collision. This is
443
  // distinct from the number of created neutrons n_bank() above!
444
  n_secondaries() = 0;
2,147,483,647✔
445

446
  zero_delayed_bank();
2,147,483,647✔
447

448
  // Reset fission logical
449
  fission() = false;
2,147,483,647✔
450

451
  // Save coordinates for tallying purposes
452
  r_last_current() = r();
2,147,483,647✔
453

454
  // Set last material to none since cross sections will need to be
455
  // re-evaluated
456
  material_last() = C_NONE;
2,147,483,647✔
457

458
  // Set all directions to base level -- right now, after a collision, only
459
  // the base level directions are changed
460
  for (int j = 0; j < n_coord() - 1; ++j) {
2,147,483,647✔
461
    if (coord(j + 1).rotated()) {
161,148,739✔
462
      // If next level is rotated, apply rotation matrix
463
      const auto& m {model::cells[coord(j).cell()]->rotation_};
10,426,614✔
464
      const auto& u {coord(j).u()};
10,426,614✔
465
      coord(j + 1).u() = u.rotate(m);
10,426,614✔
466
    } else {
467
      // Otherwise, copy this level's direction
468
      coord(j + 1).u() = coord(j).u();
150,722,125✔
469
    }
470
  }
471

472
  // Score flux derivative accumulators for differential tallies.
473
  if (!model::active_tallies.empty())
2,147,483,647✔
474
    score_collision_derivative(*this);
1,143,977,257✔
475

476
#ifdef OPENMC_DAGMC_ENABLED
477
  history().reset();
280,272,046✔
478
#endif
479
}
2,147,483,647✔
480

481
void Particle::event_revive_from_secondary(SourceSite& site)
104,741,083✔
482
{
483
  // Write final position for the previous track (skip if this is a freshly
484
  // constructed particle with no prior track, e.g., Phase 2 of shared
485
  // secondary transport)
486
  if (write_track() && n_event() > 0) {
104,741,083!
487
    write_particle_track(*this);
5,234✔
488
  }
489

490
  from_source(&site);
104,741,083✔
491

492
  n_event() = 0;
104,741,083✔
493
  if (!settings::use_shared_secondary_bank) {
104,741,083✔
494
    n_tracks()++;
83,352,240✔
495
  }
496
  bank_second_E() = 0.0;
104,741,083✔
497

498
  // Subtract secondary particle energy from interim pulse-height results.
499
  // In shared secondary mode, this subtraction was already done on the parent
500
  // particle during create_secondary(), so skip it here.
501
  if (!settings::use_shared_secondary_bank &&
188,093,323✔
502
      !model::active_pulse_height_tallies.empty() && this->type().is_photon()) {
104,741,083✔
503
    // Since the birth cell of the particle has not been set we
504
    // have to determine it before the energy of the secondary particle can be
505
    // removed from the pulse-height of this cell.
506
    if (lowest_coord().cell() == C_NONE) {
2,420!
507
      bool verbose = settings::verbosity >= 10 || trace();
2,420!
508
      if (!exhaustive_find_cell(*this, verbose)) {
2,420!
NEW
509
        mark_as_lost("Could not find the cell containing particle " +
×
NEW
510
                     std::to_string(id()));
×
NEW
511
        return;
×
512
      }
513
      // Set birth cell attribute
514
      if (cell_born() == C_NONE)
2,420!
515
        cell_born() = lowest_coord().cell();
2,420✔
516

517
      // Initialize last cells from current cell
518
      for (int j = 0; j < n_coord(); ++j) {
4,840✔
519
        cell_last(j) = coord(j).cell();
2,420✔
520
      }
521
      n_coord_last() = n_coord();
2,420✔
522
    }
523
    pht_secondary_particles();
2,420✔
524
  }
525

526
  // Enter new particle in particle track file
527
  if (write_track())
104,741,083✔
528
    add_particle_track(*this);
5,234✔
529
}
530

531
void Particle::event_check_limit_and_revive()
2,147,483,647✔
532
{
533
  // If particle has too many events, display warning and kill it
534
  n_event()++;
2,147,483,647✔
535
  if (n_event() == settings::max_particle_events) {
2,147,483,647!
NEW
536
    warning("Particle " + std::to_string(id()) +
×
537
            " underwent maximum number of events.");
NEW
538
    wgt() = 0.0;
×
539
  }
540

541
  // In non-shared-secondary mode, revive from local secondary bank
542
  if (!alive() && !settings::use_shared_secondary_bank &&
2,147,483,647✔
543
      !local_secondary_bank().empty()) {
251,676,186✔
544
    SourceSite& site = local_secondary_bank().back();
83,352,240✔
545
    event_revive_from_secondary(site);
83,352,240✔
546
    local_secondary_bank().pop_back();
83,352,240✔
547
  }
548
}
2,147,483,647✔
549

550
void Particle::event_death()
190,498,285✔
551
{
552
#ifdef OPENMC_DAGMC_ENABLED
553
  history().reset();
17,399,677✔
554
#endif
555

556
  // Finish particle track output.
557
  if (write_track()) {
190,498,285✔
558
    write_particle_track(*this);
1,010✔
559
    finalize_particle_track(*this);
1,010✔
560
  }
561

562
// Contribute tally reduction variables to global accumulator
563
#pragma omp atomic
104,132,184✔
564
  global_tally_absorption += keff_tally_absorption();
190,498,285✔
565
#pragma omp atomic
104,051,163✔
566
  global_tally_collision += keff_tally_collision();
190,498,285✔
567
#pragma omp atomic
104,112,915✔
568
  global_tally_tracklength += keff_tally_tracklength();
190,498,285✔
569
#pragma omp atomic
104,171,684✔
570
  global_tally_leakage += keff_tally_leakage();
190,498,285✔
571

572
  // Reset particle tallies once accumulated
573
  keff_tally_absorption() = 0.0;
190,498,285✔
574
  keff_tally_collision() = 0.0;
190,498,285✔
575
  keff_tally_tracklength() = 0.0;
190,498,285✔
576
  keff_tally_leakage() = 0.0;
190,498,285✔
577

578
  if (!model::active_pulse_height_tallies.empty()) {
190,498,285✔
579
    score_pulse_height_tally(*this, model::active_pulse_height_tallies);
22,000✔
580
  }
581

582
  // Accumulate track count for this particle history
583
  if (!settings::use_shared_secondary_bank) {
190,498,285✔
584
#pragma omp atomic
91,893,936✔
585
    simulation::simulation_tracks_completed += n_tracks();
168,324,946✔
586
  }
587

588
  // Record the number of progeny created by this particle.
589
  // This data will be used to efficiently sort the fission bank.
590
  if (settings::run_mode == RunMode::EIGENVALUE ||
190,498,285✔
591
      settings::use_shared_secondary_bank) {
592
    simulation::progeny_per_particle[current_work()] = n_progeny();
163,708,039✔
593
  }
594
}
190,498,285✔
595

596
void Particle::pht_collision_energy()
8,096✔
597
{
598
  // Adds the energy particles lose in a collision to the pulse-height
599

600
  // determine index of cell in pulse_height_cells
601
  auto it = std::find(model::pulse_height_cells.begin(),
8,096✔
602
    model::pulse_height_cells.end(), lowest_coord().cell());
8,096!
603

604
  if (it != model::pulse_height_cells.end()) {
8,096!
605
    int index = std::distance(model::pulse_height_cells.begin(), it);
8,096✔
606
    pht_storage()[index] += E_last() - E();
8,096✔
607

608
    // If the energy of the particle is below the cutoff, it will not be sampled
609
    // so its energy is added to the pulse-height in the cell
610
    int photon = ParticleType::photon().transport_index();
8,096✔
611
    if (E() < settings::energy_cutoff[photon]) {
8,096✔
612
      pht_storage()[index] += E();
3,300✔
613
    }
614
  }
615
}
8,096✔
616

617
void Particle::pht_secondary_particles()
2,420✔
618
{
619
  // Removes the energy of secondary produced particles from the pulse-height
620

621
  // determine index of cell in pulse_height_cells
622
  auto it = std::find(model::pulse_height_cells.begin(),
2,420✔
623
    model::pulse_height_cells.end(), cell_born());
2,420!
624

625
  if (it != model::pulse_height_cells.end()) {
2,420!
626
    int index = std::distance(model::pulse_height_cells.begin(), it);
2,420✔
627
    pht_storage()[index] -= E();
2,420✔
628
  }
629
}
2,420✔
630

631
void Particle::cross_surface(const Surface& surf)
1,674,454,536✔
632
{
633

634
  if (settings::verbosity >= 10 || trace()) {
1,674,454,536✔
635
    write_message(1, "    Crossing surface {}", surf.id_);
88✔
636
  }
637

638
// if we're crossing a CSG surface, make sure the DAG history is reset
639
#ifdef OPENMC_DAGMC_ENABLED
640
  if (surf.geom_type() == GeometryType::CSG)
152,888,356✔
641
    history().reset();
152,830,674✔
642
#endif
643

644
  // Handle any applicable boundary conditions.
645
  if (surf.bc_ && settings::run_mode != RunMode::PLOTTING &&
1,674,454,536!
646
      settings::run_mode != RunMode::VOLUME) {
647
    surf.bc_->handle_particle(*this, surf);
720,787,141✔
648
    return;
720,787,141✔
649
  }
650

651
  // ==========================================================================
652
  // SEARCH NEIGHBOR LISTS FOR NEXT CELL
653

654
#ifdef OPENMC_DAGMC_ENABLED
655
  // in DAGMC, we know what the next cell should be
656
  if (surf.geom_type() == GeometryType::DAG) {
86,919,715✔
657
    int32_t i_cell = next_cell(surface_index(), cell_last(n_coord() - 1),
46,742✔
658
                       lowest_coord().universe()) -
46,742✔
659
                     1;
46,742✔
660
    // save material, temperature, and density multiplier
661
    material_last() = material();
46,742✔
662
    sqrtkT_last() = sqrtkT();
46,742✔
663
    density_mult_last() = density_mult();
46,742✔
664
    // set new cell value
665
    lowest_coord().cell() = i_cell;
46,742✔
666
    auto& cell = model::cells[i_cell];
46,742✔
667

668
    cell_instance() = 0;
46,742✔
669
    if (cell->distribcell_index_ >= 0)
46,742✔
670
      cell_instance() = cell_instance_at_level(*this, n_coord() - 1);
45,718✔
671

672
    material() = cell->material(cell_instance());
46,742!
673
    sqrtkT() = cell->sqrtkT(cell_instance());
46,742!
674
    density_mult() = cell->density_mult(cell_instance());
46,742✔
675
    return;
46,742✔
676
  }
677
#endif
678

679
  bool verbose = settings::verbosity >= 10 || trace();
953,620,653!
680
  if (neighbor_list_find_cell(*this, verbose)) {
953,620,653✔
681
    return;
682
  }
683

684
  // ==========================================================================
685
  // COULDN'T FIND PARTICLE IN NEIGHBORING CELLS, SEARCH ALL CELLS
686

687
  // Remove lower coordinate levels
688
  n_coord() = 1;
29,977✔
689
  bool found = exhaustive_find_cell(*this, verbose);
29,977✔
690

691
  if (settings::run_mode != RunMode::PLOTTING && (!found)) {
29,977!
692
    // If a cell is still not found, there are two possible causes: 1) there is
693
    // a void in the model, and 2) the particle hit a surface at a tangent. If
694
    // the particle is really traveling tangent to a surface, if we move it
695
    // forward a tiny bit it should fix the problem.
696

697
    surface() = SURFACE_NONE;
5,865✔
698
    n_coord() = 1;
5,865✔
699
    r() += TINY_BIT * u();
5,865✔
700

701
    // Couldn't find next cell anywhere! This probably means there is an actual
702
    // undefined region in the geometry.
703

704
    if (!exhaustive_find_cell(*this, verbose)) {
5,865!
705
      mark_as_lost("After particle " + std::to_string(id()) +
17,586✔
706
                   " crossed surface " + std::to_string(surf.id_) +
17,586✔
707
                   " it could not be located in any cell and it did not leak.");
708
      return;
5,856✔
709
    }
710
  }
711
}
712

713
void Particle::cross_vacuum_bc(const Surface& surf)
35,135,776✔
714
{
715
  // Score any surface current tallies -- note that the particle is moved
716
  // forward slightly so that if the mesh boundary is on the surface, it is
717
  // still processed
718

719
  if (!model::active_meshsurf_tallies.empty()) {
35,135,776✔
720
    // TODO: Find a better solution to score surface currents than
721
    // physically moving the particle forward slightly
722

723
    r() += TINY_BIT * u();
937,222✔
724
    score_meshsurface_tally(*this, model::active_meshsurf_tallies);
937,222✔
725
  }
726

727
  // Score to global leakage tally
728
  keff_tally_leakage() += wgt();
35,135,776✔
729

730
  // Kill the particle
731
  wgt() = 0.0;
35,135,776✔
732

733
  // Display message
734
  if (settings::verbosity >= 10 || trace()) {
35,135,776!
735
    write_message(1, "    Leaked out of surface {}", surf.id_);
22✔
736
  }
737
}
35,135,776✔
738

739
void Particle::cross_reflective_bc(const Surface& surf, Direction new_u)
684,413,855✔
740
{
741
  // Do not handle reflective boundary conditions on lower universes
742
  if (n_coord() != 1) {
684,413,855!
743
    mark_as_lost("Cannot reflect particle " + std::to_string(id()) +
×
744
                 " off surface in a lower universe.");
745
    return;
×
746
  }
747

748
  // Score surface currents since reflection causes the direction of the
749
  // particle to change. For surface filters, we need to score the tallies
750
  // twice, once before the particle's surface attribute has changed and
751
  // once after. For mesh surface filters, we need to artificially move
752
  // the particle slightly back in case the surface crossing is coincident
753
  // with a mesh boundary
754

755
  if (!model::active_surface_tallies.empty()) {
684,413,855✔
756
    Direction normal = surf.normal(r());
285,021✔
757
    normal /= normal.norm();
285,021✔
758
    score_surface_tally(*this, model::active_surface_tallies, normal);
285,021✔
759
  }
760

761
  if (!model::active_meshsurf_tallies.empty()) {
684,413,855✔
762
    Position r {this->r()};
46,885,487✔
763
    this->r() -= TINY_BIT * u();
46,885,487✔
764
    score_meshsurface_tally(*this, model::active_meshsurf_tallies);
46,885,487✔
765
    this->r() = r;
46,885,487✔
766
  }
767

768
  // Set the new particle direction
769
  u() = new_u;
684,413,855✔
770

771
  // Reassign particle's cell and surface
772
  coord(0).cell() = cell_last(0);
684,413,855✔
773
  surface() = -surface();
684,413,855✔
774

775
  // If a reflective surface is coincident with a lattice or universe
776
  // boundary, it is necessary to redetermine the particle's coordinates in
777
  // the lower universes.
778
  // (unless we're using a dagmc model, which has exactly one universe)
779
  n_coord() = 1;
684,413,855✔
780
  if (surf.geom_type() != GeometryType::DAG &&
1,368,824,952!
781
      !neighbor_list_find_cell(*this)) {
684,411,097✔
782
    mark_as_lost("Couldn't find particle after reflecting from surface " +
×
783
                 std::to_string(surf.id_) + ".");
×
784
    return;
×
785
  }
786

787
  // Set previous coordinate going slightly past surface crossing
788
  r_last_current() = r() + TINY_BIT * u();
684,413,855✔
789

790
  // Diagnostic message
791
  if (settings::verbosity >= 10 || trace()) {
684,413,855!
792
    write_message(1, "    Reflected from surface {}", surf.id_);
×
793
  }
794
}
795

796
void Particle::cross_periodic_bc(
2,242,976✔
797
  const Surface& surf, Position new_r, Direction new_u, int new_surface)
798
{
799
  // Do not handle periodic boundary conditions on lower universes
800
  if (n_coord() != 1) {
2,242,976!
801
    mark_as_lost(
×
802
      "Cannot transfer particle " + std::to_string(id()) +
×
803
      " across surface in a lower universe. Boundary conditions must be "
804
      "applied to root universe.");
805
    return;
×
806
  }
807

808
  // Score surface currents since reflection causes the direction of the
809
  // particle to change -- artificially move the particle slightly back in
810
  // case the surface crossing is coincident with a mesh boundary
811
  if (!model::active_meshsurf_tallies.empty()) {
2,242,976!
812
    Position r {this->r()};
×
813
    this->r() -= TINY_BIT * u();
×
814
    score_meshsurface_tally(*this, model::active_meshsurf_tallies);
×
815
    this->r() = r;
×
816
  }
817

818
  // Adjust the particle's location and direction.
819
  r() = new_r;
2,242,976✔
820
  u() = new_u;
2,242,976✔
821

822
  // Reassign particle's surface
823
  surface() = new_surface;
2,242,976✔
824

825
  // Figure out what cell particle is in now
826
  n_coord() = 1;
2,242,976✔
827

828
  if (!neighbor_list_find_cell(*this)) {
2,242,976!
829
    mark_as_lost("Couldn't find particle after hitting periodic "
×
830
                 "boundary on surface " +
×
831
                 std::to_string(surf.id_) + ".");
×
832
    return;
×
833
  }
834

835
  // Set previous coordinate going slightly past surface crossing
836
  r_last_current() = r() + TINY_BIT * u();
2,242,976✔
837

838
  // Diagnostic message
839
  if (settings::verbosity >= 10 || trace()) {
2,242,976!
840
    write_message(1, "    Hit periodic boundary on surface {}", surf.id_);
×
841
  }
842
}
843

844
void Particle::mark_as_lost(const char* message)
5,865✔
845
{
846
  // Print warning and write lost particle file
847
  warning(message);
5,865✔
848
  if (settings::max_write_lost_particles < 0 ||
5,865✔
849
      simulation::n_lost_particles < settings::max_write_lost_particles) {
5,500✔
850
    write_restart();
440✔
851
  }
852
  // Increment number of lost particles
853
  wgt() = 0.0;
5,865✔
854
#pragma omp atomic
3,190✔
855
  simulation::n_lost_particles += 1;
2,675✔
856

857
  // Count the total number of simulated particles (on this processor)
858
  auto n = simulation::current_batch * settings::gen_per_batch *
5,865✔
859
           simulation::work_per_rank;
860

861
  // Abort the simulation if the maximum number of lost particles has been
862
  // reached
863
  if (simulation::n_lost_particles >= settings::max_lost_particles &&
5,865✔
864
      simulation::n_lost_particles >= settings::rel_max_lost_particles * n) {
9!
865
    fatal_error("Maximum number of lost particles has been reached.");
9✔
866
  }
867
}
5,856✔
868

869
void Particle::write_restart() const
440✔
870
{
871
  // Dont write another restart file if in particle restart mode
872
  if (settings::run_mode == RunMode::PARTICLE)
440✔
873
    return;
33✔
874

875
  // Set up file name
876
  auto filename = fmt::format("{}particle_{}_{}.h5", settings::path_output,
407✔
877
    simulation::current_batch, id());
407✔
878

879
#pragma omp critical(WriteParticleRestart)
217✔
880
  {
407✔
881
    // Create file
882
    hid_t file_id = file_open(filename, 'w');
407✔
883

884
    // Write filetype and version info
885
    write_attribute(file_id, "filetype", "particle restart");
407✔
886
    write_attribute(file_id, "version", VERSION_PARTICLE_RESTART);
407✔
887
    write_attribute(file_id, "openmc_version", VERSION);
407✔
888
#ifdef GIT_SHA1
889
    write_attr_string(file_id, "git_sha1", GIT_SHA1);
890
#endif
891

892
    // Write data to file
893
    write_dataset(file_id, "current_batch", simulation::current_batch);
407✔
894
    write_dataset(file_id, "generations_per_batch", settings::gen_per_batch);
407✔
895
    write_dataset(file_id, "current_generation", simulation::current_gen);
407✔
896
    write_dataset(file_id, "n_particles", settings::n_particles);
407✔
897
    switch (settings::run_mode) {
407!
898
    case RunMode::FIXED_SOURCE:
275✔
899
      write_dataset(file_id, "run_mode", "fixed source");
275✔
900
      break;
145✔
901
    case RunMode::EIGENVALUE:
132✔
902
      write_dataset(file_id, "run_mode", "eigenvalue");
132✔
903
      break;
72✔
904
    case RunMode::PARTICLE:
×
905
      write_dataset(file_id, "run_mode", "particle restart");
×
906
      break;
907
    default:
908
      break;
909
    }
910
    write_dataset(file_id, "id", id());
407✔
911
    write_dataset(file_id, "type", type().pdg_number());
407✔
912

913
    // Get source site data for the particle that got lost
914
    int64_t i = current_work();
407✔
915
    SourceSite site;
407✔
916
    if (settings::run_mode == RunMode::EIGENVALUE) {
407✔
917
      site = simulation::source_bank[i];
132✔
918
    } else if (settings::run_mode == RunMode::FIXED_SOURCE &&
275✔
919
               settings::use_shared_secondary_bank &&
275!
920
               i < simulation::shared_secondary_bank_read.size()) {
55!
NEW
921
      site = simulation::shared_secondary_bank_read[i];
×
922
    } else if (settings::run_mode == RunMode::FIXED_SOURCE) {
275!
923
      // Re-sample using the same seed used to generate the source particle.
924
      // current_work() is 0-indexed, compute_particle_id expects 1-indexed.
925
      int64_t id = compute_transport_seed(compute_particle_id(i + 1));
275✔
926
      uint64_t seed = init_seed(id, STREAM_SOURCE);
275✔
927
      site = sample_external_source(&seed);
275✔
928
    }
929
    write_dataset(file_id, "weight", site.wgt);
407✔
930
    write_dataset(file_id, "energy", site.E);
407✔
931
    write_dataset(file_id, "xyz", site.r);
407✔
932
    write_dataset(file_id, "uvw", site.u);
407✔
933
    write_dataset(file_id, "time", site.time);
407✔
934

935
    // Close file
936
    file_close(file_id);
407✔
937
  } // #pragma omp critical
938
}
407✔
939

940
void Particle::update_neutron_xs(
2,147,483,647✔
941
  int i_nuclide, int i_grid, int i_sab, double sab_frac, double ncrystal_xs)
942
{
943
  // Get microscopic cross section cache
944
  auto& micro = this->neutron_xs(i_nuclide);
2,147,483,647✔
945

946
  // If the cache doesn't match, recalculate micro xs
947
  if (this->E() != micro.last_E || this->sqrtkT() != micro.last_sqrtkT ||
2,147,483,647✔
948
      i_sab != micro.index_sab || sab_frac != micro.sab_frac ||
2,147,483,647✔
949
      ncrystal_xs != micro.ncrystal_xs) {
2,147,483,647!
950
    data::nuclides[i_nuclide]->calculate_xs(i_sab, i_grid, sab_frac, *this);
2,147,483,647✔
951

952
    // If NCrystal is being used, update micro cross section cache
953
    micro.ncrystal_xs = ncrystal_xs;
2,147,483,647✔
954
    if (ncrystal_xs >= 0.0) {
2,147,483,647✔
955
      data::nuclides[i_nuclide]->calculate_elastic_xs(*this);
11,018,953✔
956
      ncrystal_update_micro(ncrystal_xs, micro);
11,018,953✔
957
    }
958
  }
959
}
2,147,483,647✔
960

961
//==============================================================================
962
// Non-method functions
963
//==============================================================================
964
void add_surf_source_to_bank(Particle& p, const Surface& surf)
1,671,323,426✔
965
{
966
  if (simulation::current_batch <= settings::n_inactive ||
1,671,323,426✔
967
      simulation::surf_source_bank.full()) {
1,285,856,756✔
968
    return;
1,671,193,773✔
969
  }
970

971
  // If a cell/cellfrom/cellto parameter is defined
972
  if (settings::ssw_cell_id != C_NONE) {
337,081✔
973

974
    // Retrieve cell index and storage type
975
    int cell_idx = model::cell_map[settings::ssw_cell_id];
254,436✔
976

977
    if (surf.bc_) {
254,436✔
978
      // Leave if cellto with vacuum boundary condition
979
      if (surf.bc_->type() == "vacuum" &&
298,918✔
980
          settings::ssw_cell_type == SSWCellType::To) {
33,099✔
981
        return;
982
      }
983

984
      // Leave if other boundary condition than vacuum
985
      if (surf.bc_->type() != "vacuum") {
274,646✔
986
        return;
987
      }
988
    }
989

990
    // Check if the cell of interest has been exited
991
    bool exited = false;
992
    for (int i = 0; i < p.n_coord_last(); ++i) {
333,669✔
993
      if (p.cell_last(i) == cell_idx) {
207,729✔
994
        exited = true;
73,765✔
995
      }
996
    }
997

998
    // Check if the cell of interest has been entered
999
    bool entered = false;
1000
    for (int i = 0; i < p.n_coord(); ++i) {
297,971✔
1001
      if (p.coord(i).cell() == cell_idx) {
172,031✔
1002
        entered = true;
57,515✔
1003
      }
1004
    }
1005

1006
    // Vacuum boundary conditions: return if cell is not exited
1007
    if (surf.bc_) {
125,940✔
1008
      if (surf.bc_->type() == "vacuum" && !exited) {
41,926!
1009
        return;
1010
      }
1011
    } else {
1012

1013
      // If we both enter and exit the cell of interest
1014
      if (entered && exited) {
104,977✔
1015
        return;
1016
      }
1017

1018
      // If we did not enter nor exit the cell of interest
1019
      if (!entered && !exited) {
77,774✔
1020
        return;
1021
      }
1022

1023
      // If cellfrom and the cell before crossing is not the cell of
1024
      // interest
1025
      if (settings::ssw_cell_type == SSWCellType::From && !exited) {
64,274✔
1026
        return;
1027
      }
1028

1029
      // If cellto and the cell after crossing is not the cell of interest
1030
      if (settings::ssw_cell_type == SSWCellType::To && !entered) {
52,733✔
1031
        return;
1032
      }
1033
    }
1034
  }
1035

1036
  SourceSite site;
129,653✔
1037
  site.r = p.r();
129,653✔
1038
  site.u = p.u();
129,653✔
1039
  site.E = p.E();
129,653✔
1040
  site.time = p.time();
129,653✔
1041
  site.wgt = p.wgt();
129,653✔
1042
  site.delayed_group = p.delayed_group();
129,653✔
1043
  site.surf_id = surf.id_;
129,653✔
1044
  site.particle = p.type();
129,653✔
1045
  site.parent_id = p.id();
129,653✔
1046
  site.progeny_id = p.n_progeny();
129,653✔
1047
  int64_t idx = simulation::surf_source_bank.thread_safe_append(site);
129,653✔
1048
}
1049

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