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

openmc-dev / openmc / 30830496237

03 Aug 2026 04:04PM UTC coverage: 81.469% (+0.04%) from 81.429%
30830496237

Pull #3971

github

web-flow
Merge 1d36d1147 into 5982acdf8
Pull Request #3971: Delta tracking

18832 of 27249 branches covered (69.11%)

Branch coverage included in aggregate %.

597 of 645 new or added lines in 20 files covered. (92.56%)

60805 of 70502 relevant lines covered (86.25%)

50421428.58 hits per line

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

85.34
/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/majorant.h"
19
#include "openmc/material.h"
20
#include "openmc/message_passing.h"
21
#include "openmc/mgxs_interface.h"
22
#include "openmc/nuclide.h"
23
#include "openmc/particle_data.h"
24
#include "openmc/photon.h"
25
#include "openmc/physics.h"
26
#include "openmc/physics_mg.h"
27
#include "openmc/random_lcg.h"
28
#include "openmc/settings.h"
29
#include "openmc/simulation.h"
30
#include "openmc/source.h"
31
#include "openmc/surface.h"
32
#include "openmc/tallies/derivative.h"
33
#include "openmc/tallies/tally.h"
34
#include "openmc/tallies/tally_scoring.h"
35
#include "openmc/track_output.h"
36
#include "openmc/weight_windows.h"
37

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

42
namespace openmc {
43

44
//==============================================================================
45
// Particle implementation
46
//==============================================================================
47

48
double Particle::speed() const
2,147,483,647✔
49
{
50
  if (settings::run_CE) {
2,147,483,647✔
51
    // Determine mass in eV/c^2
52
    double mass = this->mass();
2,147,483,647✔
53

54
    // Equivalent to C * sqrt(1-(m/(m+E))^2) without problem at E<<m:
55
    return C_LIGHT * std::sqrt(this->E() * (this->E() + 2 * mass)) /
2,147,483,647✔
56
           (this->E() + mass);
2,147,483,647✔
57
  } else {
58
    auto mat = this->material();
2,082,832,565✔
59
    if (mat == MATERIAL_VOID)
2,082,832,565!
60
      return 1.0 / data::mg.default_inverse_velocity_[this->g()];
×
61
    auto& macro_xs = data::mg.macro_xs_[mat];
2,082,832,565✔
62
    int macro_t = this->mg_xs_cache().t;
2,082,832,565✔
63
    int macro_a = macro_xs.get_angle_index(this->u());
2,082,832,565✔
64
    return 1.0 / macro_xs.get_xs(
2,147,483,647✔
65
                   MgxsType::INVERSE_VELOCITY, this->g(), macro_t, macro_a);
2,082,832,565✔
66
  }
67
}
68

69
double Particle::mass() const
2,147,483,647✔
70
{
71
  switch (type().pdg_number()) {
2,147,483,647✔
72
  case PDG_NEUTRON:
73
    return MASS_NEUTRON_EV;
74
  case PDG_ELECTRON:
110,000✔
75
  case PDG_POSITRON:
110,000✔
76
    return MASS_ELECTRON_EV;
110,000✔
77
  default:
123,033,785✔
78
    return this->type().mass() * AMU_EV;
123,033,785✔
79
  }
80
}
81

82
bool Particle::create_secondary(
262,002,826✔
83
  double wgt, Direction u, double E, ParticleType type)
84
{
85
  // If energy is below cutoff for this particle, don't create secondary
86
  // particle
87
  int idx = type.transport_index();
262,002,826✔
88
  if (idx == C_NONE) {
262,002,826!
89
    return false;
90
  }
91
  if (E < settings::energy_cutoff[idx]) {
262,002,826✔
92
    return false;
93
  }
94

95
  // Increment number of secondaries created (for ParticleProductionFilter)
96
  n_secondaries()++;
23,158,455✔
97

98
  SourceSite bank;
23,158,455✔
99
  bank.particle = type;
23,158,455✔
100
  bank.wgt = wgt;
23,158,455✔
101
  bank.r = r();
23,158,455!
102
  bank.u = u;
23,158,455✔
103
  bank.E = settings::run_CE ? E : g();
23,158,455!
104
  bank.time = time();
23,158,455✔
105
  bank_second_E() += bank.E;
23,158,455✔
106
  bank.parent_id = current_work();
23,158,455✔
107
  if (settings::use_shared_secondary_bank) {
23,158,455✔
108
    bank.progeny_id = n_progeny()++;
932,756✔
109
  }
110
  bank.wgt_born = wgt_born();
23,158,455✔
111
  bank.wgt_ww_born = wgt_ww_born();
23,158,455✔
112
  bank.n_split = n_split();
23,158,455✔
113

114
  local_secondary_bank().emplace_back(bank);
23,158,455✔
115
  return true;
116
}
117

118
void Particle::split(double wgt)
19,031,916✔
119
{
120
  SourceSite bank;
19,031,916✔
121
  bank.particle = type();
19,031,916✔
122
  bank.wgt = wgt;
19,031,916✔
123
  bank.r = r();
19,031,916✔
124
  bank.u = u();
19,031,916✔
125
  bank.E = settings::run_CE ? E() : g();
19,031,916✔
126
  bank.time = time();
19,031,916✔
127

128
  // Convert signed index to a signed surface ID
129
  if (surface() == SURFACE_NONE) {
19,031,916✔
130
    bank.surf_id = SURFACE_NONE;
18,764,986✔
131
  } else {
132
    int surf_id = model::surfaces[surface_index()]->id_;
266,930✔
133
    bank.surf_id = (surface() > 0) ? surf_id : -surf_id;
266,930✔
134
  }
135

136
  bank.wgt_born = wgt_born();
19,031,916✔
137
  bank.wgt_ww_born = wgt_ww_born();
19,031,916✔
138
  bank.n_split = n_split();
19,031,916✔
139
  bank.n_collision = n_collision();
19,031,916✔
140
  bank.parent_id = current_work();
19,031,916✔
141
  if (settings::use_shared_secondary_bank) {
19,031,916✔
142
    bank.progeny_id = n_progeny()++;
13,713,295✔
143
  }
144

145
  local_secondary_bank().emplace_back(bank);
19,031,916✔
146
}
19,031,916✔
147

148
void Particle::from_source(const SourceSite* src)
232,221,893✔
149
{
150
  // Reset some attributes
151
  clear();
232,221,893✔
152
  surface() = SURFACE_NONE;
232,221,893✔
153
  cell_born() = C_NONE;
232,221,893✔
154
  material() = C_NONE;
232,221,893✔
155
  n_collision() = src->n_collision;
232,221,893✔
156
  fission() = false;
232,221,893✔
157
  majorant() = 0.0;
232,221,893✔
158
  zero_flux_derivs();
232,221,893✔
159
  lifetime() = 0.0;
232,221,893✔
160
#ifdef OPENMC_DAGMC_ENABLED
161
  history().reset();
21,273,557✔
162
#endif
163

164
  // Copy attributes from source bank site
165
  type() = src->particle;
232,221,893✔
166
  wgt() = src->wgt;
232,221,893✔
167
  wgt_last() = src->wgt;
232,221,893✔
168
  r() = src->r;
232,221,893✔
169
  u() = src->u;
232,221,893✔
170
  r_born() = src->r;
232,221,893✔
171
  r_last_current() = src->r;
232,221,893✔
172
  r_last() = src->r;
232,221,893✔
173
  u_last() = src->u;
232,221,893✔
174
  if (settings::run_CE) {
232,221,893✔
175
    E() = src->E;
114,476,029✔
176
    g() = 0;
114,476,029✔
177
  } else {
178
    g() = static_cast<int>(src->E);
117,745,864✔
179
    g_last() = static_cast<int>(src->E);
117,745,864✔
180
    E() = data::mg.energy_bin_avg_[g()];
117,745,864✔
181
  }
182
  E_last() = E();
232,221,893✔
183
  time() = src->time;
232,221,893✔
184
  time_last() = src->time;
232,221,893✔
185
  parent_nuclide() = src->parent_nuclide;
232,221,893✔
186
  delayed_group() = src->delayed_group;
232,221,893✔
187

188
  // Convert signed surface ID to signed index
189
  if (src->surf_id != SURFACE_NONE) {
232,221,893✔
190
    auto it = model::surface_map.find(std::abs(src->surf_id));
380,725!
191
    if (it != model::surface_map.end()) {
380,725!
192
      int index_plus_one = it->second + 1;
380,725✔
193
      surface() = (src->surf_id > 0) ? index_plus_one : -index_plus_one;
380,725✔
194
    }
195
  }
196

197
  wgt_born() = src->wgt_born;
232,221,893✔
198
  wgt_ww_born() = src->wgt_ww_born;
232,221,893✔
199
  n_split() = src->n_split;
232,221,893✔
200

201
  if (delta_tracking()) {
232,221,893✔
202
    update_majorant();
16,444,153✔
203
  }
204
}
232,221,893✔
205

206
void Particle::event_calculate_xs()
2,147,483,647✔
207
{
208
  // Set the random number stream
209
  stream() = STREAM_TRACKING;
2,147,483,647✔
210

211
  // Store pre-collision particle properties
212
  wgt_last() = wgt();
2,147,483,647✔
213
  E_last() = E();
2,147,483,647✔
214
  u_last() = u();
2,147,483,647✔
215
  r_last() = r();
2,147,483,647✔
216
  time_last() = time();
2,147,483,647✔
217

218
  // Reset event variables
219
  event() = TallyEvent::KILL;
2,147,483,647✔
220
  event_nuclide() = NUCLIDE_NONE;
2,147,483,647✔
221
  event_mt() = REACTION_NONE;
2,147,483,647✔
222

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

233
    // Set birth cell attribute
234
    if (cell_born() == C_NONE)
205,807,016!
235
      cell_born() = lowest_coord().cell();
205,807,016✔
236

237
    // Initialize last cells from current cell
238
    for (int j = 0; j < n_coord(); ++j) {
429,217,018✔
239
      cell_last(j) = coord(j).cell();
223,410,002✔
240
    }
241
    n_coord_last() = n_coord();
205,807,016✔
242
  }
243

244
  // Write particle track.
245
  if (write_track())
2,147,483,647✔
246
    write_particle_track(*this);
5,624✔
247

248
  if (settings::check_overlaps)
2,147,483,647!
249
    check_cell_overlap(*this);
×
250

251
  // Calculate microscopic and macroscopic cross sections
252
  if (material() != MATERIAL_VOID) {
2,147,483,647✔
253
    if (settings::run_CE) {
2,147,483,647✔
254
      if (material() != material_last() || sqrtkT() != sqrtkT_last() ||
2,147,483,647✔
255
          density_mult() != density_mult_last()) {
905,553,220✔
256
        // If the material is the same as the last material and the
257
        // temperature hasn't changed, we don't need to lookup cross
258
        // sections again.
259
        model::materials[material()]->calculate_xs(*this);
2,147,483,647✔
260
      }
261
    } else {
262
      // Get the MG data; unlike the CE case above, we have to re-calculate
263
      // cross sections for every collision since the cross sections may
264
      // be angle-dependent
265
      data::mg.macro_xs_[material()].calculate_xs(*this);
2,082,832,565✔
266

267
      // Update the particle's group while we know we are multi-group
268
      g_last() = g();
2,082,832,565✔
269
    }
270
  } else {
271
    macro_xs().total = 0.0;
113,932,827✔
272
    macro_xs().absorption = 0.0;
113,932,827✔
273
    macro_xs().fission = 0.0;
113,932,827✔
274
    macro_xs().nu_fission = 0.0;
113,932,827✔
275
  }
276
}
277

278
void Particle::event_advance()
2,147,483,647✔
279
{
280
  // Find the distance to the nearest boundary
281
  boundary() = distance_to_boundary(*this);
2,147,483,647✔
282

283
  // Sample a distance to collision
284
  if (type() == ParticleType::electron() ||
2,147,483,647!
285
      type() == ParticleType::positron()) {
2,147,483,647!
286
    collision_distance() = material() == MATERIAL_VOID ? INFINITY : 0.0;
220,000!
287
  } else if (macro_xs().total == 0.0) {
2,147,483,647✔
288
    collision_distance() = INFINITY;
113,932,827✔
289
  } else {
290
    collision_distance() = -std::log(prn(current_seed())) / macro_xs().total;
2,147,483,647✔
291
  }
292

293
  double speed = this->speed();
2,147,483,647✔
294
  double time_cutoff = settings::time_cutoff[type().transport_index()];
2,147,483,647✔
295
  double distance_cutoff =
2,147,483,647✔
296
    (time_cutoff < INFTY) ? (time_cutoff - time()) * speed : INFTY;
2,147,483,647✔
297

298
  // Select smaller of the three distances
299
  double distance =
2,147,483,647✔
300
    std::min({boundary().distance(), collision_distance(), distance_cutoff});
2,147,483,647✔
301

302
  // Advance particle in space and time
303
  this->move_distance(distance);
2,147,483,647✔
304
  double dt = distance / speed;
2,147,483,647✔
305
  this->time() += dt;
2,147,483,647✔
306
  this->lifetime() += dt;
2,147,483,647✔
307

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

313
  // Score track-length tallies
314
  if (!model::active_tracklength_tallies.empty()) {
2,147,483,647✔
315
    score_tracklength_tally(*this, distance);
2,147,483,647✔
316
  }
317

318
  // Score track-length estimate of k-eff
319
  if (settings::run_mode == RunMode::EIGENVALUE && type().is_neutron() &&
2,147,483,647✔
320
      !delta_tracking()) {
2,147,483,647!
321
    keff_tally_tracklength() += wgt() * distance * macro_xs().nu_fission;
2,147,483,647✔
322
  }
323

324
  // Score flux derivative accumulators for differential tallies.
325
  if (!model::active_tallies.empty()) {
2,147,483,647✔
326
    score_track_derivative(*this, distance);
2,147,483,647✔
327
  }
328

329
  // Set particle weight to zero if it hit the time boundary
330
  if (distance == distance_cutoff) {
2,147,483,647✔
331
    wgt() = 0.0;
224,928✔
332
  }
333
}
2,147,483,647✔
334

335
void Particle::event_delta_advance()
113,028,311✔
336
{
337
  if (E() != E_last()) {
113,028,311✔
338
    update_majorant();
23,514,722✔
339
  }
340

341
  // Sample distance to next position
342
  if (majorant() == 0.0) {
113,028,311!
343
    // For a void majorant (rare but possible for a source in a void),
344
    // the collision distance is infinity.
NEW
345
    collision_distance() = INFINITY;
×
346
  } else {
347
    // Sample collision distance based on the majorant for this energy.
348
    collision_distance() = -std::log(prn(current_seed())) / majorant();
113,028,311✔
349
  }
350

351
  // Update distance to problem boundary. Particles with large majorant
352
  // cross sections will tunnel out of the domain if a floating point
353
  // tolerance is not specified on the boundary distance calculation.
354
  boundary() = distance_to_external_boundary(*this);
113,028,311✔
355
  boundary().distance() -= FP_REL_PRECISION;
113,028,311✔
356

357
  double speed = this->speed();
113,028,311✔
358
  double time_cutoff = settings::time_cutoff[type().transport_index()];
113,028,311!
359
  double distance_cutoff =
113,028,311✔
360
    (time_cutoff < INFTY) ? (time_cutoff - time()) * speed : INFTY;
113,028,311!
361

362
  // Move to the external boundary, delta tracking collision site, or time
363
  // cutoff distance.
364
  double distance =
113,028,311✔
365
    std::min({collision_distance(), boundary().distance(), distance_cutoff});
113,028,311✔
366
  move_distance(distance);
113,028,311✔
367

368
  // Advance particle in time.
369
  double dt = distance / speed;
113,028,311✔
370
  time() += dt;
113,028,311✔
371
  lifetime() += dt;
113,028,311✔
372

373
  // Need to locate the particle at the collision site or boundary.
374
  for (int j = 0; j < n_coord(); ++j) {
322,639,020✔
375
    coord(j).reset();
209,610,709✔
376
  }
377
  if (!exhaustive_find_cell(*this)) {
113,028,311!
378
    // We've lost this particle.
NEW
379
    mark_as_lost(fmt::format(
×
NEW
380
      "Particle {} could not be located while running delta tracking!", id()));
×
NEW
381
    return;
×
382
  }
383

384
  // Force re-calculation of material properties at the collision site.
385
  material_last() = C_NONE;
113,028,311✔
386

387
  // Set particle weight to zero if it hit the time boundary
388
  if (distance == distance_cutoff) {
113,028,311!
NEW
389
    wgt() = 0.0;
×
390
  }
391
}
392

393
void Particle::event_cross_surface()
2,147,483,647✔
394
{
395
  // Saving previous cell data
396
  for (int j = 0; j < n_coord(); ++j) {
2,147,483,647✔
397
    cell_last(j) = coord(j).cell();
2,147,483,647✔
398
  }
399
  n_coord_last() = n_coord();
2,147,483,647✔
400

401
  // Set surface that particle is on and adjust coordinate levels
402
  surface() = boundary().surface();
2,147,483,647✔
403
  n_coord() = boundary().coord_level();
2,147,483,647✔
404

405
  if (boundary().lattice_translation()[0] != 0 ||
2,147,483,647✔
406
      boundary().lattice_translation()[1] != 0 ||
2,147,483,647✔
407
      boundary().lattice_translation()[2] != 0) {
2,147,483,647✔
408
    // Particle crosses lattice boundary
409

410
    int i_lattice = coord(boundary().coord_level() - 1).lattice();
813,765,155!
411
    bool verbose = settings::verbosity >= 10 || trace();
813,765,155!
412
    cross_lattice(*this, boundary(), verbose);
813,765,155✔
413
    event() = TallyEvent::LATTICE;
813,765,155✔
414

415
    // Score cell to cell partial currents
416
    if (!model::active_surface_tallies.empty()) {
813,765,155✔
417
      auto& lat {*model::lattices[i_lattice]};
55✔
418
      bool is_valid;
55✔
419
      Direction normal =
55✔
420
        lat.get_normal(boundary().lattice_translation(), is_valid);
55✔
421
      if (is_valid) {
55!
422
        normal /= normal.norm();
55✔
423
        score_surface_tally(*this, model::active_surface_tallies, normal);
55✔
424
      }
425
    }
426

427
  } else {
428

429
    const auto& surf {*model::surfaces[surface_index()].get()};
2,147,483,647✔
430

431
    // Particle crosses surface
432
    // If BC, add particle to surface source before crossing surface
433
    if (surf.surf_source_ && surf.bc_) {
2,147,483,647✔
434
      add_surf_source_to_bank(*this, surf);
1,031,021,951✔
435
    }
436
    this->cross_surface(surf);
2,147,483,647✔
437
    // If no BC, add particle to surface source after crossing surface
438
    if (surf.surf_source_ && !surf.bc_) {
2,147,483,647✔
439
      add_surf_source_to_bank(*this, surf);
1,855,425,325✔
440
    }
441
    if (settings::weight_window_checkpoint_surface) {
2,147,483,647✔
442
      apply_weight_windows(*this);
13,175,293✔
443
    }
444
    event() = TallyEvent::SURFACE;
2,147,483,647✔
445

446
    // Score cell to cell partial currents
447
    if (!model::active_surface_tallies.empty()) {
2,147,483,647✔
448
      Direction normal = surf.normal(r());
34,933,558✔
449
      normal /= normal.norm();
34,933,558✔
450
      score_surface_tally(*this, model::active_surface_tallies, normal);
34,933,558✔
451
    }
452
  }
453
}
2,147,483,647✔
454

455
void Particle::event_collide()
2,147,483,647✔
456
{
457
  // Score collision estimate of keff
458
  if (settings::run_mode == RunMode::EIGENVALUE && type().is_neutron()) {
2,147,483,647✔
459
    keff_tally_collision() += wgt() * macro_xs().nu_fission / macro_xs().total;
2,147,483,647✔
460
  }
461

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

466
  if (!model::active_meshsurf_tallies.empty())
2,147,483,647✔
467
    score_meshsurface_tally(*this, model::active_meshsurf_tallies);
63,095,989✔
468

469
  // Clear surface component
470
  surface() = SURFACE_NONE;
2,147,483,647✔
471

472
  if (settings::run_CE) {
2,147,483,647✔
473
    collision(*this);
1,622,081,216✔
474
  } else {
475
    collision_mg(*this);
1,801,144,774✔
476
  }
477

478
  // Collision track feature to recording particle interaction
479
  if (settings::collision_track) {
2,147,483,647✔
480
    collision_track_record(*this);
712,734✔
481
  }
482

483
  // Score collision estimator tallies -- this is done after a collision
484
  // has occurred rather than before because we need information on the
485
  // outgoing energy for any tallies with an outgoing energy filter
486
  if (!model::active_collision_tallies.empty())
2,147,483,647✔
487
    score_collision_tally(*this);
90,009,422✔
488
  if (!model::active_analog_tallies.empty()) {
2,147,483,647✔
489
    if (settings::run_CE) {
510,591,848✔
490
      score_analog_tally_ce(*this);
509,383,586✔
491
    } else {
492
      score_analog_tally_mg(*this);
1,208,262✔
493
    }
494
  }
495

496
  if (!model::active_pulse_height_tallies.empty() && type().is_photon()) {
2,147,483,647✔
497
    pht_collision_energy();
102,509✔
498
  }
499

500
  // Reset banked weight during collision
501
  n_bank() = 0;
2,147,483,647✔
502
  bank_second_E() = 0.0;
2,147,483,647✔
503
  wgt_bank() = 0.0;
2,147,483,647✔
504

505
  // Clear number of secondaries in this collision. This is
506
  // distinct from the number of created neutrons n_bank() above!
507
  n_secondaries() = 0;
2,147,483,647✔
508

509
  zero_delayed_bank();
2,147,483,647✔
510

511
  // Reset fission logical
512
  fission() = false;
2,147,483,647✔
513

514
  // Save coordinates for tallying purposes
515
  r_last_current() = r();
2,147,483,647✔
516

517
  // Set last material to none since cross sections will need to be
518
  // re-evaluated
519
  material_last() = C_NONE;
2,147,483,647✔
520

521
  // Set all directions to base level -- right now, after a collision, only
522
  // the base level directions are changed
523
  for (int j = 0; j < n_coord() - 1; ++j) {
2,147,483,647✔
524
    if (coord(j + 1).rotated()) {
333,078,592✔
525
      // If next level is rotated, apply rotation matrix
526
      const auto& m {model::cells[coord(j).cell()]->rotation_};
11,724,229✔
527
      const auto& u {coord(j).u()};
11,724,229✔
528
      coord(j + 1).u() = u.rotate(m);
11,724,229✔
529
    } else {
530
      // Otherwise, copy this level's direction
531
      coord(j + 1).u() = coord(j).u();
321,354,363✔
532
    }
533
  }
534

535
  // Score flux derivative accumulators for differential tallies.
536
  if (!model::active_tallies.empty())
2,147,483,647✔
537
    score_collision_derivative(*this);
1,460,032,007✔
538

539
#ifdef OPENMC_DAGMC_ENABLED
540
  history().reset();
313,385,278✔
541
#endif
542
}
2,147,483,647✔
543

544
void Particle::event_revive_from_secondary(const SourceSite& site)
43,205,120✔
545
{
546
  // Write final position for the previous track (skip if this is a freshly
547
  // constructed particle with no prior track, e.g., Phase 2 of shared
548
  // secondary transport)
549
  if (write_track() && n_event() > 0) {
43,205,120!
550
    write_particle_track(*this);
515✔
551
  }
552

553
  from_source(&site);
43,205,120✔
554

555
  n_event() = 0;
43,205,120✔
556
  if (!settings::use_shared_secondary_bank) {
43,205,120✔
557
    n_tracks()++;
27,753,407✔
558
  }
559
  bank_second_E() = 0.0;
43,205,120✔
560

561
  // Subtract secondary particle energy from interim pulse-height results.
562
  // In shared secondary mode, this subtraction was already done on the parent
563
  // particle during create_secondary(), so skip it here.
564
  if (!settings::use_shared_secondary_bank &&
70,958,527✔
565
      !model::active_pulse_height_tallies.empty() && this->type().is_photon()) {
43,205,120!
566
    // Since the birth cell of the particle has not been set we
567
    // have to determine it before the energy of the secondary particle can be
568
    // removed from the pulse-height of this cell.
569
    if (lowest_coord().cell() == C_NONE) {
33,429!
570
      bool verbose = settings::verbosity >= 10 || trace();
33,429!
571
      if (!exhaustive_find_cell(*this, verbose)) {
33,429!
572
        mark_as_lost("Could not find the cell containing particle " +
×
573
                     std::to_string(id()));
×
574
        return;
×
575
      }
576
      // Set birth cell attribute
577
      if (cell_born() == C_NONE)
33,429!
578
        cell_born() = lowest_coord().cell();
33,429✔
579

580
      // Initialize last cells from current cell
581
      for (int j = 0; j < n_coord(); ++j) {
66,858✔
582
        cell_last(j) = coord(j).cell();
33,429✔
583
      }
584
      n_coord_last() = n_coord();
33,429✔
585
    }
586
    pht_secondary_particles();
33,429✔
587
  }
588

589
  // Enter new particle in particle track file
590
  if (write_track())
43,205,120✔
591
    add_particle_track(*this);
515✔
592
}
593

594
void Particle::event_check_limit_and_revive()
2,147,483,647✔
595
{
596
  // If particle has too many events, display warning and kill it
597
  n_event()++;
2,147,483,647✔
598
  if (n_event() == settings::max_particle_events) {
2,147,483,647!
599
    warning("Particle " + std::to_string(id()) +
×
600
            " underwent maximum number of events.");
601
    wgt() = 0.0;
×
602
  }
603

604
  // In non-shared-secondary mode, revive from local secondary bank
605
  if (!alive() && !settings::use_shared_secondary_bank &&
2,147,483,647✔
606
      !local_secondary_bank().empty()) {
205,934,795✔
607
    SourceSite& site = local_secondary_bank().back();
27,753,407✔
608
    event_revive_from_secondary(site);
27,753,407✔
609
    local_secondary_bank().pop_back();
27,753,407✔
610
  }
611
}
2,147,483,647✔
612

613
void Particle::event_death()
194,532,942✔
614
{
615
#ifdef OPENMC_DAGMC_ENABLED
616
  history().reset();
17,767,606✔
617
#endif
618

619
  // Finish particle track output.
620
  if (write_track()) {
194,532,942✔
621
    write_particle_track(*this);
1,010✔
622
    finalize_particle_track(*this);
1,010✔
623
  }
624

625
  // Contribute tally reduction variables to global accumulator
626
  const auto k_absorption = keff_tally_absorption();
194,532,942✔
627
  const auto k_collision = keff_tally_collision();
194,532,942✔
628
  const auto k_tracklength = keff_tally_tracklength();
194,532,942✔
629
  const auto leakage = keff_tally_leakage();
194,532,942✔
630

631
  if (settings::run_mode == RunMode::EIGENVALUE) {
194,532,942✔
632
    if (k_absorption != 0.0) {
151,037,000✔
633
#pragma omp atomic
74,455,696✔
634
      global_tally_absorption += k_absorption;
61,113,833✔
635
    }
636
    if (k_collision != 0.0) {
151,037,000✔
637
#pragma omp atomic
79,070,865✔
638
      global_tally_collision += k_collision;
65,493,721✔
639
    }
640
    if (k_tracklength != 0.0 && !settings::delta_tracking) {
151,037,000!
641
#pragma omp atomic
82,148,042✔
642
      global_tally_tracklength += k_tracklength;
68,109,415✔
643
    }
644
  }
645
  if (leakage != 0.0) {
194,532,942✔
646
#pragma omp atomic
21,434,269✔
647
    global_tally_leakage += leakage;
17,115,968✔
648
  }
649

650
  // Reset particle tallies once accumulated
651
  keff_tally_absorption() = 0.0;
194,532,942✔
652
  keff_tally_collision() = 0.0;
194,532,942✔
653
  keff_tally_tracklength() = 0.0;
194,532,942✔
654
  keff_tally_leakage() = 0.0;
194,532,942✔
655

656
  if (!model::active_pulse_height_tallies.empty()) {
194,532,942✔
657
    score_pulse_height_tally(*this, model::active_pulse_height_tallies);
143,000✔
658
  }
659

660
  // Accumulate track count for this particle history
661
  if (!settings::use_shared_secondary_bank) {
194,532,942✔
662
#pragma omp atomic
97,270,968✔
663
    simulation::simulation_tracks_completed += n_tracks();
178,182,388✔
664
  }
665

666
  // Record the number of progeny created by this particle.
667
  // This data will be used to efficiently sort the fission bank.
668
  if (settings::run_mode == RunMode::EIGENVALUE ||
194,532,942✔
669
      settings::use_shared_secondary_bank) {
670
    simulation::progeny_per_particle[current_work()] = n_progeny();
167,387,554✔
671
  }
672
}
194,532,942✔
673

674
void Particle::pht_collision_energy()
102,509✔
675
{
676
  // Adds the energy particles lose in a collision to the pulse-height
677

678
  // determine index of cell in pulse_height_cells
679
  auto it = std::find(model::pulse_height_cells.begin(),
102,509✔
680
    model::pulse_height_cells.end(), lowest_coord().cell());
102,509!
681

682
  if (it != model::pulse_height_cells.end()) {
102,509!
683
    int index = std::distance(model::pulse_height_cells.begin(), it);
102,509✔
684
    pht_storage()[index] += E_last() - E();
102,509✔
685

686
    // If the energy of the particle is below the cutoff, it will not be sampled
687
    // so its energy is added to the pulse-height in the cell
688
    int photon = ParticleType::photon().transport_index();
102,509✔
689
    if (E() < settings::energy_cutoff[photon]) {
102,509✔
690
      pht_storage()[index] += E();
45,375✔
691
    }
692
  }
693
}
102,509✔
694

695
void Particle::pht_secondary_particles()
33,429✔
696
{
697
  // Removes the energy of secondary produced particles from the pulse-height
698

699
  // determine index of cell in pulse_height_cells
700
  auto it = std::find(model::pulse_height_cells.begin(),
33,429✔
701
    model::pulse_height_cells.end(), cell_born());
33,429!
702

703
  if (it != model::pulse_height_cells.end()) {
33,429!
704
    int index = std::distance(model::pulse_height_cells.begin(), it);
33,429✔
705
    pht_storage()[index] -= E();
33,429✔
706
  }
707
}
33,429✔
708

709
void Particle::cross_surface(const Surface& surf)
2,147,483,647✔
710
{
711

712
  if (settings::verbosity >= 10 || trace()) {
2,147,483,647✔
713
    write_message(1, "    Crossing surface {}", surf.id_);
88✔
714
  }
715

716
// if we're crossing a CSG surface, make sure the DAG history is reset
717
#ifdef OPENMC_DAGMC_ENABLED
718
  if (surf.geom_type() == GeometryType::CSG)
263,126,872✔
719
    history().reset();
263,069,236✔
720
#endif
721

722
  // Handle any applicable boundary conditions.
723
  if (surf.bc_ && settings::run_mode != RunMode::PLOTTING &&
2,147,483,647!
724
      settings::run_mode != RunMode::VOLUME) {
725
    surf.bc_->handle_particle(*this, surf);
1,031,368,948✔
726
    return;
1,031,368,948✔
727
  }
728

729
  // ==========================================================================
730
  // SEARCH NEIGHBOR LISTS FOR NEXT CELL
731

732
#ifdef OPENMC_DAGMC_ENABLED
733
  // in DAGMC, we know what the next cell should be
734
  if (surf.geom_type() == GeometryType::DAG) {
168,923,478✔
735
    int32_t i_cell = next_cell(surface_index(), cell_last(n_coord() - 1),
46,716✔
736
                       lowest_coord().universe()) -
46,716✔
737
                     1;
46,716✔
738
    // save material, temperature, and density multiplier
739
    material_last() = material();
46,716✔
740
    sqrtkT_last() = sqrtkT();
46,716✔
741
    density_mult_last() = density_mult();
46,716✔
742
    // set new cell value
743
    lowest_coord().cell() = i_cell;
46,716✔
744
    auto& cell = model::cells[i_cell];
46,716✔
745

746
    cell_instance() = 0;
46,716✔
747
    if (cell->distribcell_index_ >= 0)
46,716✔
748
      cell_instance() = cell_instance_at_level(*this, n_coord() - 1);
45,692✔
749

750
    material() = cell->material(cell_instance());
46,716!
751
    sqrtkT() = cell->sqrtkT(cell_instance());
46,716!
752
    density_mult() = cell->density_mult(cell_instance());
46,716✔
753
    return;
46,716✔
754
  }
755
#endif
756

757
  bool verbose = settings::verbosity >= 10 || trace();
1,857,969,591!
758
  if (neighbor_list_find_cell(*this, verbose)) {
1,857,969,591✔
759
    return;
760
  }
761

762
  // ==========================================================================
763
  // COULDN'T FIND PARTICLE IN NEIGHBORING CELLS, SEARCH ALL CELLS
764

765
  // Remove lower coordinate levels
766
  n_coord() = 1;
29,977✔
767
  bool found = exhaustive_find_cell(*this, verbose);
29,977✔
768

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

775
    surface() = SURFACE_NONE;
5,865✔
776
    n_coord() = 1;
5,865✔
777
    r() += TINY_BIT * u();
5,865✔
778

779
    // Couldn't find next cell anywhere! This probably means there is an actual
780
    // undefined region in the geometry.
781

782
    if (!exhaustive_find_cell(*this, verbose)) {
5,865!
783
      mark_as_lost("After particle " + std::to_string(id()) +
17,586✔
784
                   " crossed surface " + std::to_string(surf.id_) +
17,586✔
785
                   " it could not be located in any cell and it did not leak.");
786
      return;
5,856✔
787
    }
788
  }
789
}
790

791
void Particle::cross_vacuum_bc(const Surface& surf)
39,112,189✔
792
{
793
  // Score any surface current tallies -- note that the particle is moved
794
  // forward slightly so that if the mesh boundary is on the surface, it is
795
  // still processed
796

797
  if (!model::active_meshsurf_tallies.empty()) {
39,112,189✔
798
    // TODO: Find a better solution to score surface currents than
799
    // physically moving the particle forward slightly
800

801
    r() += TINY_BIT * u();
936,210✔
802
    score_meshsurface_tally(*this, model::active_meshsurf_tallies);
936,210✔
803
  }
804

805
  // Score to global leakage tally
806
  keff_tally_leakage() += wgt();
39,112,189✔
807

808
  // Kill the particle
809
  wgt() = 0.0;
39,112,189✔
810

811
  // Display message
812
  if (settings::verbosity >= 10 || trace()) {
39,112,189!
813
    write_message(1, "    Leaked out of surface {}", surf.id_);
22✔
814
  }
815
}
39,112,189✔
816

817
void Particle::cross_reflective_bc(const Surface& surf, Direction new_u)
990,095,412✔
818
{
819
  // Do not handle reflective boundary conditions on lower universes
820
  if (n_coord() != 1) {
990,095,412!
821
    mark_as_lost("Cannot reflect particle " + std::to_string(id()) +
×
822
                 " off surface in a lower universe.");
823
    return;
×
824
  }
825

826
  // Score surface currents since reflection causes the direction of the
827
  // particle to change. For surface filters, we need to score the tallies
828
  // twice, once before the particle's surface attribute has changed and
829
  // once after. For mesh surface filters, we need to artificially move
830
  // the particle slightly back in case the surface crossing is coincident
831
  // with a mesh boundary
832

833
  if (!model::active_surface_tallies.empty()) {
990,095,412✔
834
    Direction normal = surf.normal(r());
285,021✔
835
    normal /= normal.norm();
285,021✔
836
    score_surface_tally(*this, model::active_surface_tallies, normal);
285,021✔
837
  }
838

839
  if (!model::active_meshsurf_tallies.empty()) {
990,095,412✔
840
    Position r {this->r()};
46,882,979✔
841
    this->r() -= TINY_BIT * u();
46,882,979✔
842
    score_meshsurface_tally(*this, model::active_meshsurf_tallies);
46,882,979✔
843
    this->r() = r;
46,882,979✔
844
  }
845

846
  // Set the new particle direction
847
  u() = new_u;
990,095,412✔
848

849
  // Reassign particle's cell and surface
850
  coord(0).cell() = cell_last(0);
990,095,412✔
851
  surface() = -surface();
990,095,412✔
852

853
  // If a reflective surface is coincident with a lattice or universe
854
  // boundary, it is necessary to redetermine the particle's coordinates in
855
  // the lower universes.
856
  // (unless we're using a dagmc model, which has exactly one universe)
857
  n_coord() = 1;
990,095,412✔
858
  if (surf.geom_type() != GeometryType::DAG &&
1,980,188,066!
859
      !neighbor_list_find_cell(*this)) {
990,092,654✔
860
    mark_as_lost("Couldn't find particle after reflecting from surface " +
×
861
                 std::to_string(surf.id_) + ".");
×
862
    return;
×
863
  }
864

865
  // Set previous coordinate going slightly past surface crossing
866
  r_last_current() = r() + TINY_BIT * u();
990,095,412✔
867

868
  // Diagnostic message
869
  if (settings::verbosity >= 10 || trace()) {
990,095,412!
870
    write_message(1, "    Reflected from surface {}", surf.id_);
×
871
  }
872
}
873

874
void Particle::cross_periodic_bc(
3,166,813✔
875
  const Surface& surf, Position new_r, Direction new_u, int new_surface)
876
{
877
  // Do not handle periodic boundary conditions on lower universes
878
  if (n_coord() != 1) {
3,166,813!
879
    mark_as_lost(
×
880
      "Cannot transfer particle " + std::to_string(id()) +
×
881
      " across surface in a lower universe. Boundary conditions must be "
882
      "applied to root universe.");
883
    return;
×
884
  }
885

886
  // Score surface currents since reflection causes the direction of the
887
  // particle to change -- artificially move the particle slightly back in
888
  // case the surface crossing is coincident with a mesh boundary
889
  if (!model::active_meshsurf_tallies.empty()) {
3,166,813!
890
    Position r {this->r()};
×
891
    this->r() -= TINY_BIT * u();
×
892
    score_meshsurface_tally(*this, model::active_meshsurf_tallies);
×
893
    this->r() = r;
×
894
  }
895

896
  // Adjust the particle's location and direction.
897
  r() = new_r;
3,166,813✔
898
  u() = new_u;
3,166,813✔
899

900
  // Reassign particle's surface
901
  surface() = new_surface;
3,166,813✔
902

903
  // Figure out what cell particle is in now
904
  n_coord() = 1;
3,166,813✔
905

906
  if (!neighbor_list_find_cell(*this)) {
3,166,813!
907
    mark_as_lost("Couldn't find particle after hitting periodic "
×
908
                 "boundary on surface " +
×
909
                 std::to_string(surf.id_) + ".");
×
910
    return;
×
911
  }
912

913
  // Set previous coordinate going slightly past surface crossing
914
  r_last_current() = r() + TINY_BIT * u();
3,166,813✔
915

916
  // Diagnostic message
917
  if (settings::verbosity >= 10 || trace()) {
3,166,813!
918
    write_message(1, "    Hit periodic boundary on surface {}", surf.id_);
×
919
  }
920
}
921

922
void Particle::update_majorant()
41,058,875✔
923
{
924
  if (type().is_neutron()) {
41,058,875✔
925
    majorant() = NeutronMajorant::safety_factor_ *
21,429,298✔
926
                 data::n_majorant->calculate_neutron_xs(E());
21,429,298✔
927
  } else if (type().is_photon()) {
19,629,577!
928
    majorant() = PhotonMajorant::safety_factor_ *
19,629,577✔
929
                 data::p_majorant->calculate_photon_xs(E());
19,629,577✔
930
  }
931
}
41,058,875✔
932

933
bool Particle::kill_invalid_maj()
98,502,976✔
934
{
935
  if (alive() && (macro_xs().total > majorant())) {
98,502,976!
NEW
936
    mark_as_lost(
×
NEW
937
      fmt::format("Ratio of the total cross section ({}) to the majorant "
×
938
                  "cross section ({}) for particle {} ({}) with energy {} is "
939
                  "greater than unity!",
NEW
940
        macro_xs().total, majorant(), id(), type().str(), E()));
×
NEW
941
    return true;
×
942
  }
943
  return false;
944
}
945

946
void Particle::mark_as_lost(const char* message)
5,865✔
947
{
948
  // Print warning and write lost particle file
949
  warning(message);
5,865✔
950
  if (settings::max_write_lost_particles < 0 ||
5,865✔
951
      simulation::n_lost_particles < settings::max_write_lost_particles) {
5,500✔
952
    write_restart();
440✔
953
  }
954
  // Increment number of lost particles
955
  wgt() = 0.0;
5,865✔
956
#pragma omp atomic
3,190✔
957
  simulation::n_lost_particles += 1;
2,675✔
958

959
  // Count the total number of simulated particles (on this processor)
960
  auto n = simulation::current_batch * settings::gen_per_batch *
5,865✔
961
           simulation::work_per_rank;
962

963
  // Abort the simulation if the maximum number of lost particles has been
964
  // reached
965
  if (simulation::n_lost_particles >= settings::max_lost_particles &&
5,865✔
966
      simulation::n_lost_particles >= settings::rel_max_lost_particles * n) {
9!
967
    fatal_error("Maximum number of lost particles has been reached.");
9✔
968
  }
969
}
5,856✔
970

971
void Particle::write_restart() const
440✔
972
{
973
  // Dont write another restart file if in particle restart mode
974
  if (settings::run_mode == RunMode::PARTICLE)
440✔
975
    return;
33✔
976

977
  // Set up file name
978
  auto filename = fmt::format("{}particle_{}_{}.h5", settings::path_output,
407✔
979
    simulation::current_batch, id());
407✔
980

981
#pragma omp critical(WriteParticleRestart)
217✔
982
  {
407✔
983
    // Create file
984
    hid_t file_id = file_open(filename, 'w');
407✔
985

986
    // Write filetype and version info
987
    write_attribute(file_id, "filetype", "particle restart");
407✔
988
    write_attribute(file_id, "version", VERSION_PARTICLE_RESTART);
407✔
989
    write_attribute(file_id, "openmc_version", VERSION);
407✔
990
#ifdef GIT_SHA1
991
    write_attr_string(file_id, "git_sha1", GIT_SHA1);
992
#endif
993

994
    // Write data to file
995
    write_dataset(file_id, "current_batch", simulation::current_batch);
407✔
996
    write_dataset(file_id, "generations_per_batch", settings::gen_per_batch);
407✔
997
    write_dataset(file_id, "current_generation", simulation::current_gen);
407✔
998
    write_dataset(file_id, "n_particles", settings::n_particles);
407✔
999
    switch (settings::run_mode) {
407!
1000
    case RunMode::FIXED_SOURCE:
275✔
1001
      write_dataset(file_id, "run_mode", "fixed source");
275✔
1002
      break;
145✔
1003
    case RunMode::EIGENVALUE:
132✔
1004
      write_dataset(file_id, "run_mode", "eigenvalue");
132✔
1005
      break;
72✔
1006
    case RunMode::PARTICLE:
×
1007
      write_dataset(file_id, "run_mode", "particle restart");
×
1008
      break;
1009
    default:
1010
      break;
1011
    }
1012
    write_dataset(file_id, "id", id());
407✔
1013
    write_dataset(file_id, "type", type().pdg_number());
407✔
1014

1015
    // Get source site data for the particle that got lost
1016
    int64_t i = current_work();
407✔
1017
    SourceSite site;
407✔
1018
    if (settings::run_mode == RunMode::EIGENVALUE) {
407✔
1019
      site = simulation::source_bank[i];
132✔
1020
    } else if (settings::run_mode == RunMode::FIXED_SOURCE &&
275✔
1021
               settings::use_shared_secondary_bank &&
275!
1022
               i < simulation::shared_secondary_bank_read.size()) {
55!
1023
      site = simulation::shared_secondary_bank_read[i];
×
1024
    } else if (settings::run_mode == RunMode::FIXED_SOURCE) {
275!
1025
      // Re-sample using the same seed used to generate the source particle.
1026
      // current_work() is 0-indexed, compute_particle_id expects 1-indexed.
1027
      int64_t id = compute_transport_seed(compute_particle_id(i + 1));
275✔
1028
      uint64_t seed = init_seed(id, STREAM_SOURCE);
275✔
1029
      site = sample_external_source(&seed);
275✔
1030
    }
1031
    write_dataset(file_id, "weight", site.wgt);
407✔
1032
    write_dataset(file_id, "energy", site.E);
407✔
1033
    write_dataset(file_id, "xyz", site.r);
407✔
1034
    write_dataset(file_id, "uvw", site.u);
407✔
1035
    write_dataset(file_id, "time", site.time);
407✔
1036

1037
    // Close file
1038
    file_close(file_id);
407✔
1039
  } // #pragma omp critical
1040
}
407✔
1041

1042
void Particle::update_neutron_xs(
2,147,483,647✔
1043
  int i_nuclide, int i_grid, int i_sab, double sab_frac, double ncrystal_xs)
1044
{
1045
  // Get microscopic cross section cache
1046
  auto& micro = this->neutron_xs(i_nuclide);
2,147,483,647✔
1047

1048
  // If the cache doesn't match, recalculate micro xs
1049
  if (this->E() != micro.last_E || this->sqrtkT() != micro.last_sqrtkT ||
2,147,483,647✔
1050
      i_sab != micro.index_sab || sab_frac != micro.sab_frac ||
2,147,483,647✔
1051
      ncrystal_xs != micro.ncrystal_xs) {
2,147,483,647!
1052
    data::nuclides[i_nuclide]->calculate_xs(i_sab, i_grid, sab_frac, *this);
2,147,483,647✔
1053

1054
    // If NCrystal is being used, update micro cross section cache
1055
    micro.ncrystal_xs = ncrystal_xs;
2,147,483,647✔
1056
    if (ncrystal_xs >= 0.0) {
2,147,483,647✔
1057
      data::nuclides[i_nuclide]->calculate_elastic_xs(*this);
11,018,953✔
1058
      ncrystal_update_micro(ncrystal_xs, micro);
11,018,953✔
1059
    }
1060
  }
1061
}
2,147,483,647✔
1062

1063
//==============================================================================
1064
// Non-method functions
1065
//==============================================================================
1066
void add_surf_source_to_bank(Particle& p, const Surface& surf)
2,147,483,647✔
1067
{
1068
  if (simulation::current_batch <= settings::n_inactive ||
2,147,483,647✔
1069
      simulation::surf_source_bank.full()) {
2,147,483,647✔
1070
    return;
2,147,483,647✔
1071
  }
1072

1073
  // If a cell/cellfrom/cellto parameter is defined
1074
  if (settings::ssw_cell_id != C_NONE) {
304,424✔
1075

1076
    // Retrieve cell index and storage type
1077
    int cell_idx = model::cell_map[settings::ssw_cell_id];
222,358✔
1078

1079
    if (surf.bc_) {
222,358✔
1080
      // Leave if cellto with vacuum boundary condition
1081
      if (surf.bc_->type() == "vacuum" &&
284,576✔
1082
          settings::ssw_cell_type == SSWCellType::To) {
32,878✔
1083
        return;
1084
      }
1085

1086
      // Leave if other boundary condition than vacuum
1087
      if (surf.bc_->type() != "vacuum") {
260,244✔
1088
        return;
1089
      }
1090
    }
1091

1092
    // Check if the cell of interest has been exited
1093
    bool exited = false;
1094
    for (int i = 0; i < p.n_coord_last(); ++i) {
268,747✔
1095
      if (p.cell_last(i) == cell_idx) {
167,965✔
1096
        exited = true;
59,528✔
1097
      }
1098
    }
1099

1100
    // Check if the cell of interest has been entered
1101
    bool entered = false;
1102
    for (int i = 0; i < p.n_coord(); ++i) {
233,631✔
1103
      if (p.coord(i).cell() == cell_idx) {
132,849✔
1104
        entered = true;
43,894✔
1105
      }
1106
    }
1107

1108
    // Vacuum boundary conditions: return if cell is not exited
1109
    if (surf.bc_) {
100,782✔
1110
      if (surf.bc_->type() == "vacuum" && !exited) {
41,424!
1111
        return;
1112
      }
1113
    } else {
1114

1115
      // If we both enter and exit the cell of interest
1116
      if (entered && exited) {
80,070✔
1117
        return;
1118
      }
1119

1120
      // If we did not enter nor exit the cell of interest
1121
      if (!entered && !exited) {
66,553✔
1122
        return;
1123
      }
1124

1125
      // If cellfrom and the cell before crossing is not the cell of
1126
      // interest
1127
      if (settings::ssw_cell_type == SSWCellType::From && !exited) {
63,788✔
1128
        return;
1129
      }
1130

1131
      // If cellto and the cell after crossing is not the cell of interest
1132
      if (settings::ssw_cell_type == SSWCellType::To && !entered) {
52,314✔
1133
        return;
1134
      }
1135
    }
1136
  }
1137

1138
  SourceSite site;
128,646✔
1139
  site.r = p.r();
128,646✔
1140
  site.u = p.u();
128,646✔
1141
  site.E = p.E();
128,646✔
1142
  site.time = p.time();
128,646✔
1143
  site.wgt = p.wgt();
128,646✔
1144
  site.delayed_group = p.delayed_group();
128,646✔
1145
  site.surf_id = surf.id_;
128,646✔
1146
  site.particle = p.type();
128,646✔
1147
  site.parent_id = p.id();
128,646✔
1148
  site.progeny_id = p.n_progeny();
128,646✔
1149
  int64_t idx = simulation::surf_source_bank.thread_safe_append(site);
128,646✔
1150
}
1151

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

© 2026 Coveralls, Inc