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

openmc-dev / openmc / 29109839871

10 Jul 2026 05:07PM UTC coverage: 81.355% (+0.06%) from 81.295%
29109839871

Pull #3971

github

web-flow
Merge b03e8c77b into 7256d5046
Pull Request #3971: Delta tracking

18567 of 26880 branches covered (69.07%)

Branch coverage included in aggregate %.

604 of 650 new or added lines in 20 files covered. (92.92%)

59962 of 69646 relevant lines covered (86.1%)

49564145.57 hits per line

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

85.9
/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:
248,306,312✔
75
  case PDG_POSITRON:
248,306,312✔
76
    return MASS_ELECTRON_EV;
248,306,312✔
77
  default:
127,270,334✔
78
    return this->type().mass() * AMU_EV;
127,270,334✔
79
  }
80
}
81

82
bool Particle::create_secondary(
519,039,494✔
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();
519,039,494✔
88
  if (idx == C_NONE) {
519,039,494!
89
    return false;
90
  }
91
  if (E < settings::energy_cutoff[idx]) {
519,039,494✔
92
    return false;
93
  }
94

95
  // Increment number of secondaries created (for ParticleProductionFilter)
96
  n_secondaries()++;
271,407,983✔
97

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

114
  local_secondary_bank().emplace_back(bank);
271,407,983✔
115
  return true;
116
}
117

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

128
  // Convert signed index to a signed surface ID
129
  if (surface() == SURFACE_NONE) {
10,796,816✔
130
    bank.surf_id = SURFACE_NONE;
10,792,944✔
131
  } else {
132
    int surf_id = model::surfaces[surface_index()]->id_;
3,872✔
133
    bank.surf_id = (surface() > 0) ? surf_id : -surf_id;
3,872✔
134
  }
135

136
  bank.wgt_born = wgt_born();
10,796,816✔
137
  bank.wgt_ww_born = wgt_ww_born();
10,796,816✔
138
  bank.n_split = n_split();
10,796,816✔
139
  bank.parent_id = current_work();
10,796,816✔
140
  if (settings::use_shared_secondary_bank) {
10,796,816✔
141
    bank.progeny_id = n_progeny()++;
5,153,237✔
142
  }
143

144
  local_secondary_bank().emplace_back(bank);
10,796,816✔
145
}
10,796,816✔
146

147
void Particle::from_source(const SourceSite* src)
471,383,148✔
148
{
149
  // Reset some attributes
150
  clear();
471,383,148✔
151
  surface() = SURFACE_NONE;
471,383,148✔
152
  cell_born() = C_NONE;
471,383,148✔
153
  material() = C_NONE;
471,383,148✔
154
  n_collision() = 0;
471,383,148✔
155
  fission() = false;
471,383,148✔
156
  majorant() = 0.0;
471,383,148✔
157
  zero_flux_derivs();
471,383,148✔
158
  lifetime() = 0.0;
471,383,148✔
159
#ifdef OPENMC_DAGMC_ENABLED
160
  history().reset();
42,995,860✔
161
#endif
162

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

187
  // Convert signed surface ID to signed index
188
  if (src->surf_id != SURFACE_NONE) {
471,383,148✔
189
    int index_plus_one = model::surface_map[std::abs(src->surf_id)] + 1;
113,872✔
190
    surface() = (src->surf_id > 0) ? index_plus_one : -index_plus_one;
113,872✔
191
  }
192

193
  wgt_born() = src->wgt_born;
471,383,148✔
194
  wgt_ww_born() = src->wgt_ww_born;
471,383,148✔
195
  n_split() = src->n_split;
471,383,148✔
196

197
  if (delta_tracking()) {
471,383,148✔
198
    update_majorant();
168,944,105✔
199
  }
200
}
471,383,148✔
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)) {
293,378,580!
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)
293,378,580!
231
      cell_born() = lowest_coord().cell();
293,378,580✔
232

233
    // Initialize last cells from current cell
234
    for (int j = 0; j < n_coord(); ++j) {
604,250,000✔
235
      cell_last(j) = coord(j).cell();
310,871,420✔
236
    }
237
    n_coord_last() = n_coord();
293,378,580✔
238
  }
239

240
  // Write particle track.
241
  if (write_track())
2,147,483,647✔
242
    write_particle_track(*this);
10,311✔
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()) {
902,532,281✔
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);
2,147,483,647✔
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,082,832,565✔
262

263
      // Update the particle's group while we know we are multi-group
264
      g_last() = g();
2,082,832,565✔
265
    }
266
  } else {
267
    macro_xs().total = 0.0;
111,961,618✔
268
    macro_xs().absorption = 0.0;
111,961,618✔
269
    macro_xs().fission = 0.0;
111,961,618✔
270
    macro_xs().nu_fission = 0.0;
111,961,618✔
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;
191,626,272!
283
  } else if (macro_xs().total == 0.0) {
2,147,483,647✔
284
    collision_distance() = INFINITY;
111,961,618✔
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);
2,147,483,647✔
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
      !delta_tracking()) {
2,147,483,647!
317
    keff_tally_tracklength() += wgt() * distance * macro_xs().nu_fission;
2,147,483,647✔
318
  }
319

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

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

331
void Particle::event_delta_advance()
264,250,987✔
332
{
333
  if (E() != E_last()) {
264,250,987✔
334
    update_majorant();
23,478,048✔
335
  }
336

337
  // Sample distance to next position
338
  if (type() == ParticleType::electron() ||
264,250,987✔
339
      type() == ParticleType::positron()) {
111,895,432✔
340
    // Electrons/positrons don't move
341
    collision_distance() = 0.0;
152,493,176✔
342
  } else if (majorant() == 0.0) {
111,757,811!
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();
111,757,811✔
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);
264,250,987✔
355
  boundary().distance() -= FP_REL_PRECISION;
264,250,987✔
356

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

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

368
  // Advance particle in time.
369
  double dt = distance / speed;
264,250,987✔
370
  time() += dt;
264,250,987✔
371
  lifetime() += dt;
264,250,987✔
372

373
  // Need to locate the particle at the collision site or boundary.
374
  for (int j = 0; j < n_coord(); ++j) {
623,807,096✔
375
    coord(j).reset();
359,556,109✔
376
  }
377
  if (!exhaustive_find_cell(*this)) {
264,250,987!
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;
264,250,987✔
386

387
  // Set particle weight to zero if it hit the time boundary
388
  if (distance == distance_cutoff) {
264,250,987!
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();
801,517,784!
411
    bool verbose = settings::verbosity >= 10 || trace();
801,517,784!
412
    cross_lattice(*this, boundary(), verbose);
801,517,784✔
413
    event() = TallyEvent::LATTICE;
801,517,784✔
414

415
    // Score cell to cell partial currents
416
    if (!model::active_surface_tallies.empty()) {
801,517,784✔
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,023,156,120✔
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,851,429,240✔
440
    }
441
    if (settings::weight_window_checkpoint_surface) {
2,147,483,647✔
442
      apply_weight_windows(*this);
174,249✔
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,931,622✔
449
      normal /= normal.norm();
34,931,622✔
450
      score_surface_tally(*this, model::active_surface_tallies, normal);
34,931,622✔
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,098,926✔
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,674,118,335✔
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);
728,673✔
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);
201,974,803✔
488
  if (!model::active_analog_tallies.empty()) {
2,147,483,647✔
489
    if (settings::run_CE) {
406,229,898✔
490
      score_analog_tally_ce(*this);
405,021,636✔
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();
8,668✔
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()) {
477,842,159✔
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();
466,117,930✔
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,419,525,083✔
538

539
#ifdef OPENMC_DAGMC_ENABLED
540
  history().reset();
317,623,722✔
541
#endif
542
}
2,147,483,647✔
543

544
void Particle::event_revive_from_secondary(const SourceSite& site)
283,218,996✔
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) {
283,218,996!
550
    write_particle_track(*this);
5,230✔
551
  }
552

553
  from_source(&site);
283,218,996✔
554

555
  n_event() = 0;
283,218,996✔
556
  if (!settings::use_shared_secondary_bank) {
283,218,996✔
557
    n_tracks()++;
261,884,487✔
558
  }
559
  bank_second_E() = 0.0;
283,218,996✔
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 &&
545,103,483✔
565
      !model::active_pulse_height_tallies.empty() && this->type().is_photon()) {
283,218,996✔
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) {
3,168!
570
      bool verbose = settings::verbosity >= 10 || trace();
3,168!
571
      if (!exhaustive_find_cell(*this, verbose)) {
3,168!
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)
3,168!
578
        cell_born() = lowest_coord().cell();
3,168✔
579

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

589
  // Enter new particle in particle track file
590
  if (write_track())
283,218,996✔
591
    add_particle_track(*this);
5,230✔
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()) {
440,207,599✔
607
    SourceSite& site = local_secondary_bank().back();
261,884,487✔
608
    event_revive_from_secondary(site);
261,884,487✔
609
    local_secondary_bank().pop_back();
261,884,487✔
610
  }
611
}
2,147,483,647✔
612

613
void Particle::event_death()
200,443,117✔
614
{
615
#ifdef OPENMC_DAGMC_ENABLED
616
  history().reset();
18,283,733✔
617
#endif
618

619
  // Finish particle track output.
620
  if (write_track()) {
200,443,117✔
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();
200,443,117✔
627
  const auto k_collision = keff_tally_collision();
200,443,117✔
628
  const auto k_tracklength = keff_tally_tracklength();
200,443,117✔
629
  const auto leakage = keff_tally_leakage();
200,443,117✔
630

631
  if (settings::run_mode == RunMode::EIGENVALUE) {
200,443,117✔
632
    if (k_absorption != 0.0) {
151,004,000✔
633
#pragma omp atomic
73,880,884✔
634
      global_tally_absorption += k_absorption;
61,097,772✔
635
    }
636
    if (k_collision != 0.0) {
151,004,000✔
637
#pragma omp atomic
78,863,733✔
638
      global_tally_collision += k_collision;
65,477,983✔
639
    }
640
    if (k_tracklength != 0.0 && !settings::delta_tracking) {
151,004,000!
641
#pragma omp atomic
81,978,782✔
642
      global_tally_tracklength += k_tracklength;
68,094,415✔
643
    }
644
  }
645
  if (leakage != 0.0) {
200,443,117✔
646
#pragma omp atomic
20,344,075✔
647
    global_tally_leakage += leakage;
16,364,429✔
648
  }
649

650
  // Reset particle tallies once accumulated
651
  keff_tally_absorption() = 0.0;
200,443,117✔
652
  keff_tally_collision() = 0.0;
200,443,117✔
653
  keff_tally_tracklength() = 0.0;
200,443,117✔
654
  keff_tally_leakage() = 0.0;
200,443,117✔
655

656
  if (!model::active_pulse_height_tallies.empty()) {
200,443,117✔
657
    score_pulse_height_tally(*this, model::active_pulse_height_tallies);
33,000✔
658
  }
659

660
  // Accumulate track count for this particle history
661
  if (!settings::use_shared_secondary_bank) {
200,443,117✔
662
#pragma omp atomic
97,348,272✔
663
    simulation::simulation_tracks_completed += n_tracks();
178,324,112✔
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 ||
200,443,117✔
669
      settings::use_shared_secondary_bank) {
670
    simulation::progeny_per_particle[current_work()] = n_progeny();
173,123,005✔
671
  }
672
}
200,443,117✔
673

674
void Particle::pht_collision_energy()
8,668✔
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(),
8,668✔
680
    model::pulse_height_cells.end(), lowest_coord().cell());
8,668!
681

682
  if (it != model::pulse_height_cells.end()) {
8,668!
683
    int index = std::distance(model::pulse_height_cells.begin(), it);
8,668✔
684
    pht_storage()[index] += E_last() - E();
8,668✔
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();
8,668✔
689
    if (E() < settings::energy_cutoff[photon]) {
8,668✔
690
      pht_storage()[index] += E();
3,740✔
691
    }
692
  }
693
}
8,668✔
694

695
void Particle::pht_secondary_particles()
3,168✔
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(),
3,168✔
701
    model::pulse_height_cells.end(), cell_born());
3,168!
702

703
  if (it != model::pulse_height_cells.end()) {
3,168!
704
    int index = std::distance(model::pulse_height_cells.begin(), it);
3,168✔
705
    pht_storage()[index] -= E();
3,168✔
706
  }
707
}
3,168✔
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)
262,299,111✔
719
    history().reset();
262,241,429✔
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,023,508,228✔
726
    return;
1,023,508,228✔
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,813,704✔
735
    int32_t i_cell = next_cell(surface_index(), cell_last(n_coord() - 1),
46,742✔
736
                       lowest_coord().universe()) -
46,742✔
737
                     1;
46,742✔
738
    // save material, temperature, and density multiplier
739
    material_last() = material();
46,742✔
740
    sqrtkT_last() = sqrtkT();
46,742✔
741
    density_mult_last() = density_mult();
46,742✔
742
    // set new cell value
743
    lowest_coord().cell() = i_cell;
46,742✔
744
    auto& cell = model::cells[i_cell];
46,742✔
745

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

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

757
  bool verbose = settings::verbosity >= 10 || trace();
1,854,230,282!
758
  if (neighbor_list_find_cell(*this, verbose)) {
1,854,230,282✔
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)
37,452,986✔
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()) {
37,452,986✔
798
    // TODO: Find a better solution to score surface currents than
799
    // physically moving the particle forward slightly
800

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

805
  // Score to global leakage tally
806
  keff_tally_leakage() += wgt();
37,452,986✔
807

808
  // Kill the particle
809
  wgt() = 0.0;
37,452,986✔
810

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

817
void Particle::cross_reflective_bc(const Surface& surf, Direction new_u)
983,889,625✔
818
{
819
  // Do not handle reflective boundary conditions on lower universes
820
  if (n_coord() != 1) {
983,889,625!
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()) {
983,889,625✔
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()) {
983,889,625✔
840
    Position r {this->r()};
46,885,487✔
841
    this->r() -= TINY_BIT * u();
46,885,487✔
842
    score_meshsurface_tally(*this, model::active_meshsurf_tallies);
46,885,487✔
843
    this->r() = r;
46,885,487✔
844
  }
845

846
  // Set the new particle direction
847
  u() = new_u;
983,889,625✔
848

849
  // Reassign particle's cell and surface
850
  coord(0).cell() = cell_last(0);
983,889,625✔
851
  surface() = -surface();
983,889,625✔
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;
983,889,625✔
858
  if (surf.geom_type() != GeometryType::DAG &&
1,967,776,492!
859
      !neighbor_list_find_cell(*this)) {
983,886,867✔
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();
983,889,625✔
867

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

874
void Particle::cross_periodic_bc(
3,171,083✔
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,171,083!
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,171,083!
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,171,083✔
898
  u() = new_u;
3,171,083✔
899

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

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

906
  if (!neighbor_list_find_cell(*this)) {
3,171,083!
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,171,083✔
915

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

922
void Particle::update_majorant()
193,522,153✔
923
{
924
  if (type().is_neutron()) {
193,522,153✔
925
    majorant() = NeutronMajorant::safety_factor_ *
21,403,074✔
926
                 data::n_majorant->calculate_neutron_xs(E());
21,403,074✔
927
  } else if (type().is_photon()) {
172,119,079✔
928
    majorant() = PhotonMajorant::safety_factor_ *
19,625,903✔
929
                 data::p_majorant->calculate_photon_xs(E());
19,625,903✔
930
  }
931
}
193,522,153✔
932

933
bool Particle::kill_invalid_maj()
97,273,869✔
934
{
935
  if (alive() && (macro_xs().total > majorant())) {
97,273,869!
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) {
337,079✔
1075

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

1079
    if (surf.bc_) {
254,434✔
1080
      // Leave if cellto with vacuum boundary condition
1081
      if (surf.bc_->type() == "vacuum" &&
298,916✔
1082
          settings::ssw_cell_type == SSWCellType::To) {
33,098✔
1083
        return;
1084
      }
1085

1086
      // Leave if other boundary condition than vacuum
1087
      if (surf.bc_->type() != "vacuum") {
274,646✔
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) {
333,667✔
1095
      if (p.cell_last(i) == cell_idx) {
207,728✔
1096
        exited = true;
73,764✔
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) {
297,969✔
1103
      if (p.coord(i).cell() == cell_idx) {
172,030✔
1104
        entered = true;
57,517✔
1105
      }
1106
    }
1107

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

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

1120
      // If we did not enter nor exit the cell of interest
1121
      if (!entered && !exited) {
77,773✔
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) {
64,275✔
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,732✔
1133
        return;
1134
      }
1135
    }
1136
  }
1137

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

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