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

openmc-dev / openmc / 30947192182

04 Aug 2026 08:17PM UTC coverage: 81.47% (+0.05%) from 81.425%
30947192182

Pull #4044

github

web-flow
Merge 66466fdb9 into 8202ef6fb
Pull Request #4044: Hybrid delta tracking

18952 of 27411 branches covered (69.14%)

Branch coverage included in aggregate %.

721 of 782 new or added lines in 20 files covered. (92.2%)

3 existing lines in 1 file now uncovered.

60943 of 70656 relevant lines covered (86.25%)

51279303.12 hits per line

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

86.22
/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:
183,852,125✔
78
    return this->type().mass() * AMU_EV;
183,852,125✔
79
  }
80
}
81

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

95
  // Increment number of secondaries created (for ParticleProductionFilter)
96
  n_secondaries()++;
43,573,832✔
97

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

114
  local_secondary_bank().emplace_back(bank);
43,573,832✔
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)
253,517,270✔
149
{
150
  // Reset some attributes
151
  clear();
253,517,270✔
152
  surface() = SURFACE_NONE;
253,517,270✔
153
  cell_born() = C_NONE;
253,517,270✔
154
  material() = C_NONE;
253,517,270✔
155
  n_collision() = src->n_collision;
253,517,270✔
156
  fission() = false;
253,517,270✔
157
  majorant() = 0.0;
253,517,270✔
158
  zero_flux_derivs();
253,517,270✔
159
  lifetime() = 0.0;
253,517,270✔
160
#ifdef OPENMC_DAGMC_ENABLED
161
  history().reset();
23,209,500✔
162
#endif
163

164
  // Copy attributes from source bank site
165
  type() = src->particle;
253,517,270✔
166
  wgt() = src->wgt;
253,517,270✔
167
  wgt_last() = src->wgt;
253,517,270✔
168
  r() = src->r;
253,517,270✔
169
  u() = src->u;
253,517,270✔
170
  r_born() = src->r;
253,517,270✔
171
  r_last_current() = src->r;
253,517,270✔
172
  r_last() = src->r;
253,517,270✔
173
  u_last() = src->u;
253,517,270✔
174
  if (settings::run_CE) {
253,517,270✔
175
    E() = src->E;
135,771,406✔
176
    g() = 0;
135,771,406✔
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();
253,517,270✔
183
  time() = src->time;
253,517,270✔
184
  time_last() = src->time;
253,517,270✔
185
  parent_nuclide() = src->parent_nuclide;
253,517,270✔
186
  delayed_group() = src->delayed_group;
253,517,270✔
187

188
  // Convert signed surface ID to signed index
189
  if (src->surf_id != SURFACE_NONE) {
253,517,270✔
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;
253,517,270✔
198
  wgt_ww_born() = src->wgt_ww_born;
253,517,270✔
199
  n_split() = src->n_split;
253,517,270✔
200

201
  // Revive with delta tracking turned on in most scenarios. There are a few
202
  // exceptions:
203
  // i)  If hybrid-in-cross-section tracking is being used and the threshold is
204
  //     0. This ensures the particle tracks and RNG stream fully mimic surface
205
  //     tracking.
206
  // ii) The birth energy is below the hybrid-in-energy cuttoff for the
207
  //     particle.
208
  if (settings::delta_tracking) {
253,517,270✔
209
    // i)
210
    const bool disable_i =
75,482,550✔
211
      settings::hybrid_delta_type == HybridTrackingType::CrossSection &&
37,741,275✔
212
      settings::hybrid_xs_threshold == 0.0;
27,071,583!
213

214
    // ii)
215
    const bool disable_iii =
37,741,275✔
216
      settings::hybrid_delta_type == HybridTrackingType::Energy &&
37,741,275✔
217
      E() < settings::hybrid_energy_threshold[type().transport_index()];
10,669,692✔
218

219
    delta_tracking() = !(disable_i || disable_iii);
37,741,275✔
220

221
    // Need to keep majorant in synch.
222
    update_majorant();
37,741,275✔
223
  }
224
}
253,517,270✔
225

226
void Particle::event_calculate_xs()
2,147,483,647✔
227
{
228
  // Set the random number stream
229
  stream() = STREAM_TRACKING;
2,147,483,647✔
230

231
  // Store pre-collision particle properties
232
  wgt_last() = wgt();
2,147,483,647✔
233
  E_last() = E();
2,147,483,647✔
234
  u_last() = u();
2,147,483,647✔
235
  r_last() = r();
2,147,483,647✔
236
  time_last() = time();
2,147,483,647✔
237

238
  // Reset event variables
239
  event() = TallyEvent::KILL;
2,147,483,647✔
240
  event_nuclide() = NUCLIDE_NONE;
2,147,483,647✔
241
  event_mt() = REACTION_NONE;
2,147,483,647✔
242

243
  // If the cell hasn't been determined based on the particle's location,
244
  // initiate a search for the current cell. This generally happens at the
245
  // beginning of the history and again for any secondary particles
246
  if (lowest_coord().cell() == C_NONE) {
2,147,483,647✔
247
    if (!exhaustive_find_cell(*this)) {
215,173,553!
248
      mark_as_lost(
×
249
        "Could not find the cell containing particle " + std::to_string(id()));
×
250
      return;
×
251
    }
252

253
    // Set birth cell attribute
254
    if (cell_born() == C_NONE)
215,173,553!
255
      cell_born() = lowest_coord().cell();
215,173,553✔
256

257
    // Initialize last cells from current cell
258
    for (int j = 0; j < n_coord(); ++j) {
457,316,614✔
259
      cell_last(j) = coord(j).cell();
242,143,061✔
260
    }
261
    n_coord_last() = n_coord();
215,173,553✔
262
  }
263

264
  // Write particle track.
265
  if (write_track())
2,147,483,647✔
266
    write_particle_track(*this);
5,627✔
267

268
  if (settings::check_overlaps)
2,147,483,647!
269
    check_cell_overlap(*this);
×
270

271
  // Calculate microscopic and macroscopic cross sections
272
  if (material() != MATERIAL_VOID) {
2,147,483,647✔
273
    if (settings::run_CE) {
2,147,483,647✔
274
      if (material() != material_last() || sqrtkT() != sqrtkT_last() ||
2,147,483,647✔
275
          density_mult() != density_mult_last()) {
920,763,382✔
276
        // If the material is the same as the last material and the
277
        // temperature hasn't changed, we don't need to lookup cross
278
        // sections again.
279
        model::materials[material()]->calculate_xs(*this);
2,147,483,647✔
280
      }
281
    } else {
282
      // Get the MG data; unlike the CE case above, we have to re-calculate
283
      // cross sections for every collision since the cross sections may
284
      // be angle-dependent
285
      data::mg.macro_xs_[material()].calculate_xs(*this);
2,082,832,565✔
286

287
      // Update the particle's group while we know we are multi-group
288
      g_last() = g();
2,082,832,565✔
289
    }
290
  } else {
291
    macro_xs().total = 0.0;
113,932,808✔
292
    macro_xs().absorption = 0.0;
113,932,808✔
293
    macro_xs().fission = 0.0;
113,932,808✔
294
    macro_xs().nu_fission = 0.0;
113,932,808✔
295
  }
296
}
297

298
void Particle::event_advance()
2,147,483,647✔
299
{
300
  // Find the distance to the nearest boundary
301
  boundary() = distance_to_boundary(*this);
2,147,483,647✔
302

303
  // Sample a distance to collision
304
  if (type() == ParticleType::electron() ||
2,147,483,647!
305
      type() == ParticleType::positron()) {
2,147,483,647!
306
    collision_distance() = material() == MATERIAL_VOID ? INFINITY : 0.0;
220,000!
307
  } else if (macro_xs().total == 0.0) {
2,147,483,647✔
308
    collision_distance() = INFINITY;
113,932,808✔
309
  } else {
310
    collision_distance() = -std::log(prn(current_seed())) / macro_xs().total;
2,147,483,647✔
311
  }
312

313
  double speed = this->speed();
2,147,483,647✔
314
  double time_cutoff = settings::time_cutoff[type().transport_index()];
2,147,483,647✔
315
  double distance_cutoff =
2,147,483,647✔
316
    (time_cutoff < INFTY) ? (time_cutoff - time()) * speed : INFTY;
2,147,483,647✔
317

318
  // Select smaller of the three distances
319
  double distance =
2,147,483,647✔
320
    std::min({boundary().distance(), collision_distance(), distance_cutoff});
2,147,483,647✔
321

322
  // Advance particle in space and time
323
  this->move_distance(distance);
2,147,483,647✔
324
  double dt = distance / speed;
2,147,483,647✔
325
  this->time() += dt;
2,147,483,647✔
326
  this->lifetime() += dt;
2,147,483,647✔
327

328
  // Score timed track-length tallies
329
  if (!model::active_timed_tracklength_tallies.empty()) {
2,147,483,647✔
330
    score_timed_tracklength_tally(*this, distance);
3,628,317✔
331
  }
332

333
  // Score track-length tallies
334
  if (!model::active_tracklength_tallies.empty()) {
2,147,483,647✔
335
    score_tracklength_tally(*this, distance);
2,147,483,647✔
336
  }
337

338
  // Score track-length estimate of k-eff
339
  if (settings::run_mode == RunMode::EIGENVALUE && type().is_neutron() &&
2,147,483,647✔
340
      !delta_tracking()) {
2,147,483,647!
341
    keff_tally_tracklength() += wgt() * distance * macro_xs().nu_fission;
2,147,483,647✔
342
  }
343

344
  // Score flux derivative accumulators for differential tallies.
345
  if (!model::active_tallies.empty()) {
2,147,483,647✔
346
    score_track_derivative(*this, distance);
2,147,483,647✔
347
  }
348

349
  // Set particle weight to zero if it hit the time boundary
350
  if (distance == distance_cutoff) {
2,147,483,647✔
351
    wgt() = 0.0;
224,928✔
352
  }
353

354
  // Clear surface component if distance is long enough
355
  if (distance > TINY_BIT)
2,147,483,647✔
356
    surface() = SURFACE_NONE;
2,147,483,647✔
357
}
2,147,483,647✔
358

359
void Particle::event_update_majorant()
200,403,555✔
360
{
361
  if (E() != E_last()) {
200,403,555✔
362
    update_majorant();
45,529,858✔
363
  }
364
}
200,403,555✔
365

366
void Particle::event_delta_advance()
200,403,555✔
367
{
368
  // Compute the distance to the next collision with the hybrid approach.
369
  collision_distance() = hybrid_distance_to_coll();
200,403,555✔
370

371
  // Update distance to problem boundary if we've flagged that this particle
372
  // should run with delta tracking. Otherwise, we need the distance to the
373
  // nearest surface.
374
  if (delta_tracking()) {
200,403,555✔
375
    boundary() = distance_to_external_boundary(*this);
152,721,789✔
376
    boundary().distance() -= FP_REL_PRECISION;
152,721,789✔
377
  } else {
378
    boundary() = distance_to_boundary(*this);
47,681,766✔
379
  }
380

381
  double speed = this->speed();
200,403,555✔
382
  double time_cutoff = settings::time_cutoff[type().transport_index()];
200,403,555!
383
  double distance_cutoff =
200,403,555✔
384
    (time_cutoff < INFTY) ? (time_cutoff - time()) * speed : INFTY;
200,403,555!
385

386
  // Move to the external boundary, collision site (real or virtual), or time
387
  // cutoff distance.
388
  double distance =
200,403,555✔
389
    std::min({collision_distance(), boundary().distance(), distance_cutoff});
200,403,555✔
390
  move_distance(distance);
200,403,555✔
391

392
  // Advance particle in time.
393
  double dt = distance / speed;
200,403,555✔
394
  time() += dt;
200,403,555✔
395
  lifetime() += dt;
200,403,555✔
396

397
  // Need to locate the particle at the collision site or boundary if flagged
398
  // for delta tracking.
399
  if (delta_tracking()) {
200,403,555✔
400
    for (int j = 0; j < n_coord(); ++j) {
429,790,614✔
401
      coord(j).reset();
277,068,825✔
402
    }
403
    if (!exhaustive_find_cell(*this)) {
152,721,789!
404
      // We've lost this particle.
NEW
405
      mark_as_lost(fmt::format(
×
406
        "Particle {} could not be located while running delta tracking!",
NEW
407
        id()));
×
NEW
408
      return;
×
409
    }
410
  }
411

412
  // Force re-calculation of material properties at the collision site if
413
  // running delta tracking.
414
  if (delta_tracking()) {
200,403,555✔
415
    material_last() = C_NONE;
152,721,789✔
416
  }
417

418
  // Set particle weight to zero if it hit the time boundary
419
  if (distance == distance_cutoff) {
200,403,555!
UNCOV
420
    wgt() = 0.0;
×
421
  }
422
}
423

424
void Particle::event_cross_surface()
2,147,483,647✔
425
{
426
  // Saving previous cell data
427
  for (int j = 0; j < n_coord(); ++j) {
2,147,483,647✔
428
    cell_last(j) = coord(j).cell();
2,147,483,647✔
429
  }
430
  n_coord_last() = n_coord();
2,147,483,647✔
431

432
  // Set surface that particle is on and adjust coordinate levels
433
  surface() = boundary().surface();
2,147,483,647✔
434
  n_coord() = boundary().coord_level();
2,147,483,647✔
435

436
  if (boundary().lattice_translation()[0] != 0 ||
2,147,483,647✔
437
      boundary().lattice_translation()[1] != 0 ||
2,147,483,647✔
438
      boundary().lattice_translation()[2] != 0) {
2,147,483,647✔
439
    // Particle crosses lattice boundary
440

441
    int i_lattice = coord(boundary().coord_level() - 1).lattice();
821,367,258!
442
    bool verbose = settings::verbosity >= 10 || trace();
821,367,258!
443
    cross_lattice(*this, boundary(), verbose);
821,367,258✔
444
    event() = TallyEvent::LATTICE;
821,367,258✔
445

446
    // Score cell to cell partial currents
447
    if (!model::active_surface_tallies.empty()) {
821,367,258✔
448
      auto& lat {*model::lattices[i_lattice]};
55✔
449
      bool is_valid;
55✔
450
      Direction normal =
55✔
451
        lat.get_normal(boundary().lattice_translation(), is_valid);
55✔
452
      if (is_valid) {
55!
453
        normal /= normal.norm();
55✔
454
        score_surface_tally(*this, model::active_surface_tallies, normal);
55✔
455
      }
456
    }
457

458
  } else {
459

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

462
    // Particle crosses surface
463
    // If BC, add particle to surface source before crossing surface
464
    if (surf.surf_source_ && surf.bc_) {
2,147,483,647✔
465
      add_surf_source_to_bank(*this, surf);
1,047,814,572✔
466
    }
467
    this->cross_surface(surf);
2,147,483,647✔
468
    // If no BC, add particle to surface source after crossing surface
469
    if (surf.surf_source_ && !surf.bc_) {
2,147,483,647✔
470
      add_surf_source_to_bank(*this, surf);
1,868,675,023✔
471
    }
472
    if (settings::weight_window_checkpoint_surface) {
2,147,483,647✔
473
      apply_weight_windows(*this);
13,175,293✔
474
    }
475
    event() = TallyEvent::SURFACE;
2,147,483,647✔
476

477
    // Score cell to cell partial currents
478
    if (!model::active_surface_tallies.empty()) {
2,147,483,647✔
479
      Direction normal = surf.normal(r());
34,933,558✔
480
      normal /= normal.norm();
34,933,558✔
481
      score_surface_tally(*this, model::active_surface_tallies, normal);
34,933,558✔
482
    }
483
  }
484
}
2,147,483,647✔
485

486
void Particle::event_collide()
2,147,483,647✔
487
{
488
  // Score collision estimate of keff
489
  if (settings::run_mode == RunMode::EIGENVALUE && type().is_neutron()) {
2,147,483,647✔
490
    keff_tally_collision() += wgt() * macro_xs().nu_fission / macro_xs().total;
2,147,483,647✔
491
  }
492

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

497
  if (!model::active_meshsurf_tallies.empty())
2,147,483,647✔
498
    score_meshsurface_tally(*this, model::active_meshsurf_tallies);
63,095,989✔
499

500
  // Preserve whether the particle is still associated with a recently crossed
501
  // surface so that a direction change during a near-surface collision can be
502
  // reconciled afterward. The surface marker is no longer needed during the
503
  // collision itself.
504
  const bool near_surface = surface() != SURFACE_NONE;
2,147,483,647✔
505
  surface() = SURFACE_NONE;
2,147,483,647✔
506

507
  if (settings::run_CE) {
2,147,483,647✔
508
    collision(*this);
1,661,518,372✔
509
  } else {
510
    collision_mg(*this);
1,801,144,774✔
511
  }
512

513
  // Collision track feature to recording particle interaction
514
  if (settings::collision_track) {
2,147,483,647✔
515
    collision_track_record(*this);
712,734✔
516
  }
517

518
  // Score collision estimator tallies -- this is done after a collision
519
  // has occurred rather than before because we need information on the
520
  // outgoing energy for any tallies with an outgoing energy filter
521
  if (!model::active_collision_tallies.empty())
2,147,483,647✔
522
    score_collision_tally(*this);
110,024,406✔
523
  if (!model::active_analog_tallies.empty()) {
2,147,483,647✔
524
    if (settings::run_CE) {
510,591,848✔
525
      score_analog_tally_ce(*this);
509,383,586✔
526
    } else {
527
      score_analog_tally_mg(*this);
1,208,262✔
528
    }
529
  }
530

531
  if (!model::active_pulse_height_tallies.empty() && type().is_photon()) {
2,147,483,647✔
532
    pht_collision_energy();
102,509✔
533
  }
534

535
  // Reset banked weight during collision
536
  n_bank() = 0;
2,147,483,647✔
537
  bank_second_E() = 0.0;
2,147,483,647✔
538
  wgt_bank() = 0.0;
2,147,483,647✔
539

540
  // Clear number of secondaries in this collision. This is
541
  // distinct from the number of created neutrons n_bank() above!
542
  n_secondaries() = 0;
2,147,483,647✔
543

544
  zero_delayed_bank();
2,147,483,647✔
545

546
  // Reset fission logical
547
  fission() = false;
2,147,483,647✔
548

549
  // Save coordinates for tallying purposes
550
  r_last_current() = r();
2,147,483,647✔
551

552
  // Set last material to none since cross sections will need to be
553
  // re-evaluated
554
  material_last() = C_NONE;
2,147,483,647✔
555

556
  // Set all directions to base level -- right now, after a collision, only
557
  // the base level directions are changed
558
  for (int j = 0; j < n_coord() - 1; ++j) {
2,147,483,647✔
559
    if (coord(j + 1).rotated()) {
373,171,810✔
560
      // If next level is rotated, apply rotation matrix
561
      const auto& m {model::cells[coord(j).cell()]->rotation_};
11,724,229✔
562
      const auto& u {coord(j).u()};
11,724,229✔
563
      coord(j + 1).u() = u.rotate(m);
11,724,229✔
564
    } else {
565
      // Otherwise, copy this level's direction
566
      coord(j + 1).u() = coord(j).u();
361,447,581✔
567
    }
568
  }
569

570
  // Score flux derivative accumulators for differential tallies.
571
  if (!model::active_tallies.empty())
2,147,483,647✔
572
    score_collision_derivative(*this);
1,479,391,149✔
573

574
#ifdef OPENMC_DAGMC_ENABLED
575
  history().reset();
317,148,356✔
576
#endif
577

578
  if (near_surface && alive())
2,147,483,647✔
579
    reconcile_cell_after_collision(*this);
14,942,697✔
580
}
2,147,483,647✔
581

582
void Particle::event_revive_from_secondary(const SourceSite& site)
63,620,497✔
583
{
584
  // Write final position for the previous track (skip if this is a freshly
585
  // constructed particle with no prior track, e.g., Phase 2 of shared
586
  // secondary transport)
587
  if (write_track() && n_event() > 0) {
63,620,497!
588
    write_particle_track(*this);
515✔
589
  }
590

591
  from_source(&site);
63,620,497✔
592

593
  n_event() = 0;
63,620,497✔
594
  if (!settings::use_shared_secondary_bank) {
63,620,497✔
595
    n_tracks()++;
48,168,784✔
596
  }
597
  bank_second_E() = 0.0;
63,620,497✔
598

599
  // Subtract secondary particle energy from interim pulse-height results.
600
  // In shared secondary mode, this subtraction was already done on the parent
601
  // particle during create_secondary(), so skip it here.
602
  if (!settings::use_shared_secondary_bank &&
111,789,281✔
603
      !model::active_pulse_height_tallies.empty() && this->type().is_photon()) {
63,620,497!
604
    // Since the birth cell of the particle has not been set we
605
    // have to determine it before the energy of the secondary particle can be
606
    // removed from the pulse-height of this cell.
607
    if (lowest_coord().cell() == C_NONE) {
33,429!
608
      bool verbose = settings::verbosity >= 10 || trace();
33,429!
609
      if (!exhaustive_find_cell(*this, verbose)) {
33,429!
610
        mark_as_lost("Could not find the cell containing particle " +
×
611
                     std::to_string(id()));
×
612
        return;
×
613
      }
614
      // Set birth cell attribute
615
      if (cell_born() == C_NONE)
33,429!
616
        cell_born() = lowest_coord().cell();
33,429✔
617

618
      // Initialize last cells from current cell
619
      for (int j = 0; j < n_coord(); ++j) {
66,858✔
620
        cell_last(j) = coord(j).cell();
33,429✔
621
      }
622
      n_coord_last() = n_coord();
33,429✔
623
    }
624
    pht_secondary_particles();
33,429✔
625
  }
626

627
  // Enter new particle in particle track file
628
  if (write_track())
63,620,497✔
629
    add_particle_track(*this);
515✔
630
}
631

632
void Particle::event_check_limit_and_revive()
2,147,483,647✔
633
{
634
  // If particle has too many events, display warning and kill it
635
  n_event()++;
2,147,483,647✔
636
  if (n_event() == settings::max_particle_events) {
2,147,483,647!
UNCOV
637
    warning("Particle " + std::to_string(id()) +
×
638
            " underwent maximum number of events.");
UNCOV
639
    wgt() = 0.0;
×
640
  }
641

642
  // In non-shared-secondary mode, revive from local secondary bank
643
  if (!alive() && !settings::use_shared_secondary_bank &&
2,147,483,647✔
644
      !local_secondary_bank().empty()) {
227,230,172✔
645
    // Revive with delta tracking turned on.
646
    delta_tracking() = settings::delta_tracking;
48,168,784✔
647

648
    SourceSite& site = local_secondary_bank().back();
48,168,784✔
649
    event_revive_from_secondary(site);
48,168,784✔
650
    local_secondary_bank().pop_back();
48,168,784✔
651
  }
652
}
2,147,483,647✔
653

654
void Particle::event_death()
195,412,942✔
655
{
656
#ifdef OPENMC_DAGMC_ENABLED
657
  history().reset();
17,847,606✔
658
#endif
659

660
  // Finish particle track output.
661
  if (write_track()) {
195,412,942✔
662
    write_particle_track(*this);
1,010✔
663
    finalize_particle_track(*this);
1,010✔
664
  }
665

666
  // Contribute tally reduction variables to global accumulator
667
  const auto k_absorption = keff_tally_absorption();
195,412,942✔
668
  const auto k_collision = keff_tally_collision();
195,412,942✔
669
  const auto k_tracklength = keff_tally_tracklength();
195,412,942✔
670
  const auto leakage = keff_tally_leakage();
195,412,942✔
671

672
  if (settings::run_mode == RunMode::EIGENVALUE) {
195,412,942✔
673
    if (k_absorption != 0.0) {
151,917,000✔
674
#pragma omp atomic
74,423,473✔
675
      global_tally_absorption += k_absorption;
61,510,113✔
676
    }
677
    if (k_collision != 0.0) {
151,917,000✔
678
#pragma omp atomic
79,454,888✔
679
      global_tally_collision += k_collision;
65,892,681✔
680
    }
681
    if (k_tracklength != 0.0 && !settings::delta_tracking) {
151,917,000!
682
#pragma omp atomic
82,127,080✔
683
      global_tally_tracklength += k_tracklength;
68,109,415✔
684
    }
685
  }
686
  if (leakage != 0.0) {
195,412,942✔
687
#pragma omp atomic
21,001,754✔
688
    global_tally_leakage += leakage;
17,122,905✔
689
  }
690

691
  // Reset particle tallies once accumulated
692
  keff_tally_absorption() = 0.0;
195,412,942✔
693
  keff_tally_collision() = 0.0;
195,412,942✔
694
  keff_tally_tracklength() = 0.0;
195,412,942✔
695
  keff_tally_leakage() = 0.0;
195,412,942✔
696

697
  if (!model::active_pulse_height_tallies.empty()) {
195,412,942✔
698
    score_pulse_height_tally(*this, model::active_pulse_height_tallies);
143,000✔
699
  }
700

701
  // Accumulate track count for this particle history
702
  if (!settings::use_shared_secondary_bank) {
195,412,942✔
703
#pragma omp atomic
97,750,968✔
704
    simulation::simulation_tracks_completed += n_tracks();
179,062,388✔
705
  }
706

707
  // Record the number of progeny created by this particle.
708
  // This data will be used to efficiently sort the fission bank.
709
  if (settings::run_mode == RunMode::EIGENVALUE ||
195,412,942✔
710
      settings::use_shared_secondary_bank) {
711
    simulation::progeny_per_particle[current_work()] = n_progeny();
168,267,554✔
712
  }
713
}
195,412,942✔
714

715
double Particle::hybrid_distance_to_coll()
200,403,555✔
716
{
717
  if (delta_tracking()) {
200,403,555✔
718
    if (majorant() == 0.0) {
152,721,789!
719
      return INFINITY;
720
    } else {
721
      return -std::log(prn(current_seed())) / majorant();
152,721,789✔
722
    }
723
  } else {
724
    if (macro_xs().total == 0.0) {
47,681,766!
725
      return INFINITY;
726
    } else {
727
      return -std::log(prn(current_seed())) / macro_xs().total;
47,681,766✔
728
    }
729
  }
730
}
731

732
void Particle::pht_collision_energy()
102,509✔
733
{
734
  // Adds the energy particles lose in a collision to the pulse-height
735

736
  // determine index of cell in pulse_height_cells
737
  auto it = std::find(model::pulse_height_cells.begin(),
102,509✔
738
    model::pulse_height_cells.end(), lowest_coord().cell());
102,509!
739

740
  if (it != model::pulse_height_cells.end()) {
102,509!
741
    int index = std::distance(model::pulse_height_cells.begin(), it);
102,509✔
742
    pht_storage()[index] += E_last() - E();
102,509✔
743

744
    // If the energy of the particle is below the cutoff, it will not be sampled
745
    // so its energy is added to the pulse-height in the cell
746
    int photon = ParticleType::photon().transport_index();
102,509✔
747
    if (E() < settings::energy_cutoff[photon]) {
102,509✔
748
      pht_storage()[index] += E();
45,375✔
749
    }
750
  }
751
}
102,509✔
752

753
void Particle::pht_secondary_particles()
33,429✔
754
{
755
  // Removes the energy of secondary produced particles from the pulse-height
756

757
  // determine index of cell in pulse_height_cells
758
  auto it = std::find(model::pulse_height_cells.begin(),
33,429✔
759
    model::pulse_height_cells.end(), cell_born());
33,429!
760

761
  if (it != model::pulse_height_cells.end()) {
33,429!
762
    int index = std::distance(model::pulse_height_cells.begin(), it);
33,429✔
763
    pht_storage()[index] -= E();
33,429✔
764
  }
765
}
33,429✔
766

767
void Particle::cross_surface(const Surface& surf)
2,147,483,647✔
768
{
769

770
  if (settings::verbosity >= 10 || trace()) {
2,147,483,647✔
771
    write_message(1, "    Crossing surface {}", surf.id_);
88✔
772
  }
773

774
// if we're crossing a CSG surface, make sure the DAG history is reset
775
#ifdef OPENMC_DAGMC_ENABLED
776
  if (surf.geom_type() == GeometryType::CSG)
265,812,699✔
777
    history().reset();
265,755,063✔
778
#endif
779

780
  // Handle any applicable boundary conditions.
781
  if (surf.bc_ && settings::run_mode != RunMode::PLOTTING &&
2,147,483,647!
782
      settings::run_mode != RunMode::VOLUME) {
783
    surf.bc_->handle_particle(*this, surf);
1,048,161,569✔
784
    return;
1,048,161,569✔
785
  }
786

787
  // ==========================================================================
788
  // SEARCH NEIGHBOR LISTS FOR NEXT CELL
789

790
#ifdef OPENMC_DAGMC_ENABLED
791
  // in DAGMC, we know what the next cell should be
792
  if (surf.geom_type() == GeometryType::DAG) {
170,084,196✔
793
    int32_t i_cell = next_cell(surface_index(), cell_last(n_coord() - 1),
46,716✔
794
                       lowest_coord().universe()) -
46,716✔
795
                     1;
46,716✔
796
    // save material, temperature, and density multiplier
797
    material_last() = material();
46,716✔
798
    sqrtkT_last() = sqrtkT();
46,716✔
799
    density_mult_last() = density_mult();
46,716✔
800
    // set new cell value
801
    lowest_coord().cell() = i_cell;
46,716✔
802
    auto& cell = model::cells[i_cell];
46,716✔
803

804
    cell_instance() = 0;
46,716✔
805
    if (cell->distribcell_index_ >= 0)
46,716✔
806
      cell_instance() = cell_instance_at_level(*this, n_coord() - 1);
45,692✔
807

808
    material() = cell->material(cell_instance());
46,716!
809
    sqrtkT() = cell->sqrtkT(cell_instance());
46,716!
810
    density_mult() = cell->density_mult(cell_instance());
46,716✔
811
    return;
46,716✔
812
  }
813
#endif
814

815
  bool verbose = settings::verbosity >= 10 || trace();
1,871,248,473!
816
  if (neighbor_list_find_cell(*this, verbose)) {
1,871,248,473✔
817
    return;
818
  }
819

820
  // ==========================================================================
821
  // COULDN'T FIND PARTICLE IN NEIGHBORING CELLS, SEARCH ALL CELLS
822

823
  // Remove lower coordinate levels
824
  n_coord() = 1;
29,977✔
825
  bool found = exhaustive_find_cell(*this, verbose);
29,977✔
826

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

833
    surface() = SURFACE_NONE;
5,865✔
834
    n_coord() = 1;
5,865✔
835
    r() += TINY_BIT * u();
5,865✔
836

837
    // Couldn't find next cell anywhere! This probably means there is an actual
838
    // undefined region in the geometry.
839

840
    if (!exhaustive_find_cell(*this, verbose)) {
5,865!
841
      mark_as_lost("After particle " + std::to_string(id()) +
17,586✔
842
                   " crossed surface " + std::to_string(surf.id_) +
17,586✔
843
                   " it could not be located in any cell and it did not leak.");
844
      return;
5,856✔
845
    }
846
  }
847
}
848

849
void Particle::cross_vacuum_bc(const Surface& surf)
39,117,732✔
850
{
851
  // Score any surface current tallies -- note that the particle is moved
852
  // forward slightly so that if the mesh boundary is on the surface, it is
853
  // still processed
854

855
  if (!model::active_meshsurf_tallies.empty()) {
39,117,732✔
856
    // TODO: Find a better solution to score surface currents than
857
    // physically moving the particle forward slightly
858

859
    r() += TINY_BIT * u();
936,210✔
860
    score_meshsurface_tally(*this, model::active_meshsurf_tallies);
936,210✔
861
  }
862

863
  // Score to global leakage tally
864
  keff_tally_leakage() += wgt();
39,117,732✔
865

866
  // Kill the particle
867
  wgt() = 0.0;
39,117,732✔
868

869
  // Display message
870
  if (settings::verbosity >= 10 || trace()) {
39,117,732!
871
    write_message(1, "    Leaked out of surface {}", surf.id_);
22✔
872
  }
873
}
39,117,732✔
874

875
void Particle::cross_reflective_bc(const Surface& surf, Direction new_u)
1,006,882,490✔
876
{
877
  // Do not handle reflective boundary conditions on lower universes
878
  if (n_coord() != 1) {
1,006,882,490!
879
    mark_as_lost("Cannot reflect particle " + std::to_string(id()) +
×
880
                 " off surface in a lower universe.");
881
    return;
×
882
  }
883

884
  // Score surface currents since reflection causes the direction of the
885
  // particle to change. For surface filters, we need to score the tallies
886
  // twice, once before the particle's surface attribute has changed and
887
  // once after. For mesh surface filters, we need to artificially move
888
  // the particle slightly back in case the surface crossing is coincident
889
  // with a mesh boundary
890

891
  if (!model::active_surface_tallies.empty()) {
1,006,882,490✔
892
    Direction normal = surf.normal(r());
285,021✔
893
    normal /= normal.norm();
285,021✔
894
    score_surface_tally(*this, model::active_surface_tallies, normal);
285,021✔
895
  }
896

897
  if (!model::active_meshsurf_tallies.empty()) {
1,006,882,490✔
898
    Position r {this->r()};
46,882,979✔
899
    this->r() -= TINY_BIT * u();
46,882,979✔
900
    score_meshsurface_tally(*this, model::active_meshsurf_tallies);
46,882,979✔
901
    this->r() = r;
46,882,979✔
902
  }
903

904
  // Set the new particle direction
905
  u() = new_u;
1,006,882,490✔
906

907
  // Reassign particle's cell and surface
908
  coord(0).cell() = cell_last(0);
1,006,882,490✔
909
  surface() = -surface();
1,006,882,490✔
910

911
  // If a reflective surface is coincident with a lattice or universe
912
  // boundary, it is necessary to redetermine the particle's coordinates in
913
  // the lower universes.
914
  // (unless we're using a dagmc model, which has exactly one universe)
915
  n_coord() = 1;
1,006,882,490✔
916
  if (surf.geom_type() != GeometryType::DAG &&
2,013,762,222!
917
      !neighbor_list_find_cell(*this)) {
1,006,879,732✔
918
    mark_as_lost("Couldn't find particle after reflecting from surface " +
×
919
                 std::to_string(surf.id_) + ".");
×
920
    return;
×
921
  }
922

923
  // Set previous coordinate going slightly past surface crossing
924
  r_last_current() = r() + TINY_BIT * u();
1,006,882,490✔
925

926
  // Diagnostic message
927
  if (settings::verbosity >= 10 || trace()) {
1,006,882,490!
928
    write_message(1, "    Reflected from surface {}", surf.id_);
×
929
  }
930
}
931

932
void Particle::cross_periodic_bc(
3,166,813✔
933
  const Surface& surf, Position new_r, Direction new_u, int new_surface)
934
{
935
  // Do not handle periodic boundary conditions on lower universes
936
  if (n_coord() != 1) {
3,166,813!
937
    mark_as_lost(
×
938
      "Cannot transfer particle " + std::to_string(id()) +
×
939
      " across surface in a lower universe. Boundary conditions must be "
940
      "applied to root universe.");
941
    return;
×
942
  }
943

944
  // Score surface currents since reflection causes the direction of the
945
  // particle to change -- artificially move the particle slightly back in
946
  // case the surface crossing is coincident with a mesh boundary
947
  if (!model::active_meshsurf_tallies.empty()) {
3,166,813!
948
    Position r {this->r()};
×
949
    this->r() -= TINY_BIT * u();
×
950
    score_meshsurface_tally(*this, model::active_meshsurf_tallies);
×
951
    this->r() = r;
×
952
  }
953

954
  // Adjust the particle's location and direction.
955
  r() = new_r;
3,166,813✔
956
  u() = new_u;
3,166,813✔
957

958
  // Reassign particle's surface
959
  surface() = new_surface;
3,166,813✔
960

961
  // Figure out what cell particle is in now
962
  n_coord() = 1;
3,166,813✔
963

964
  if (!neighbor_list_find_cell(*this)) {
3,166,813!
965
    mark_as_lost("Couldn't find particle after hitting periodic "
×
966
                 "boundary on surface " +
×
967
                 std::to_string(surf.id_) + ".");
×
968
    return;
×
969
  }
970

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

974
  // Diagnostic message
975
  if (settings::verbosity >= 10 || trace()) {
3,166,813!
NEW
976
    write_message(1, "    Hit periodic boundary on surface {}", surf.id_);
×
977
  }
978
}
979

980
void Particle::update_majorant()
85,251,133✔
981
{
982
  if (type().is_neutron()) {
85,251,133✔
983
    majorant() = NeutronMajorant::safety_factor_ *
39,635,068✔
984
                 data::n_majorant->calculate_neutron_xs(E());
39,635,068✔
985
  } else if (type().is_photon()) {
45,616,065!
986
    majorant() = PhotonMajorant::safety_factor_ *
45,616,065✔
987
                 data::p_majorant->calculate_photon_xs(E());
45,616,065✔
988
  }
989
}
85,251,133✔
990

991
bool Particle::kill_invalid_maj()
129,017,416✔
992
{
993
  if (alive() && (macro_xs().total > majorant())) {
129,017,416!
NEW
994
    mark_as_lost(
×
NEW
995
      fmt::format("Ratio of the total cross section ({}) to the majorant "
×
996
                  "cross section ({}) for particle {} ({}) with energy {} is "
997
                  "greater than unity!",
NEW
998
        macro_xs().total, majorant(), id(), type().str(), E()));
×
NEW
999
    return true;
×
1000
  }
1001
  return false;
1002
}
1003

1004
void Particle::update_tracking_type()
148,239,322✔
1005
{
1006
  switch (settings::hybrid_delta_type) {
148,239,322!
1007
  case HybridTrackingType::CrossSection: {
123,514,380✔
1008
    // We need to decide if delta tracking or surface tracking should be used.
1009
    // This is done based on Eq. 9 in the Serpent paper:
1010
    // http://doi.org/10.1016/j.anucene.2010.01.011
1011
    const double th = 1.0 - settings::hybrid_xs_threshold;
123,514,380✔
1012
    if (alive() && (macro_xs().total / majorant()) > th) {
123,514,380✔
1013
      delta_tracking() = true;
93,666,122✔
1014
    } else if (alive()) {
29,848,258✔
1015
      delta_tracking() = false;
2,883,452✔
1016
    }
1017
    break;
1018
  }
1019
  case HybridTrackingType::Energy: {
24,724,942✔
1020
    // Switch between tracking types based on energy. See
1021
    // Section 3.3 of https://doi.org/10.1080/23324309.2026.2618791
1022
    if (alive() &&
24,724,942✔
1023
        E() >= settings::hybrid_energy_threshold[type().transport_index()]) {
14,055,250✔
1024
      delta_tracking() = true;
7,083,318✔
1025
    } else if (alive()) {
17,641,624✔
1026
      delta_tracking() = false;
6,971,932✔
1027
    }
1028
    break;
1029
  }
1030
  }
1031
}
148,239,322✔
1032

1033
void Particle::mark_as_lost(const char* message)
5,865✔
1034
{
1035
  // Print warning and write lost particle file
1036
  warning(message);
5,865✔
1037
  if (settings::max_write_lost_particles < 0 ||
5,865✔
1038
      simulation::n_lost_particles < settings::max_write_lost_particles) {
5,500✔
1039
    write_restart();
440✔
1040
  }
1041
  // Increment number of lost particles
1042
  wgt() = 0.0;
5,865✔
1043
#pragma omp atomic
3,190✔
1044
  simulation::n_lost_particles += 1;
2,675✔
1045

1046
  // Count the total number of simulated particles (on this processor)
1047
  auto n = simulation::current_batch * settings::gen_per_batch *
5,865✔
1048
           simulation::work_per_rank;
1049

1050
  // Abort the simulation if the maximum number of lost particles has been
1051
  // reached
1052
  if (simulation::n_lost_particles >= settings::max_lost_particles &&
5,865✔
1053
      simulation::n_lost_particles >= settings::rel_max_lost_particles * n) {
9!
1054
    fatal_error("Maximum number of lost particles has been reached.");
9✔
1055
  }
1056
}
5,856✔
1057

1058
void Particle::write_restart() const
440✔
1059
{
1060
  // Dont write another restart file if in particle restart mode
1061
  if (settings::run_mode == RunMode::PARTICLE)
440✔
1062
    return;
33✔
1063

1064
  // Set up file name
1065
  auto filename = fmt::format("{}particle_{}_{}.h5", settings::path_output,
407✔
1066
    simulation::current_batch, id());
407✔
1067

1068
#pragma omp critical(WriteParticleRestart)
217✔
1069
  {
407✔
1070
    // Create file
1071
    hid_t file_id = file_open(filename, 'w');
407✔
1072

1073
    // Write filetype and version info
1074
    write_attribute(file_id, "filetype", "particle restart");
407✔
1075
    write_attribute(file_id, "version", VERSION_PARTICLE_RESTART);
407✔
1076
    write_attribute(file_id, "openmc_version", VERSION);
407✔
1077
#ifdef GIT_SHA1
1078
    write_attr_string(file_id, "git_sha1", GIT_SHA1);
1079
#endif
1080

1081
    // Write data to file
1082
    write_dataset(file_id, "current_batch", simulation::current_batch);
407✔
1083
    write_dataset(file_id, "generations_per_batch", settings::gen_per_batch);
407✔
1084
    write_dataset(file_id, "current_generation", simulation::current_gen);
407✔
1085
    write_dataset(file_id, "n_particles", settings::n_particles);
407✔
1086
    switch (settings::run_mode) {
407!
1087
    case RunMode::FIXED_SOURCE:
275✔
1088
      write_dataset(file_id, "run_mode", "fixed source");
275✔
1089
      break;
145✔
1090
    case RunMode::EIGENVALUE:
132✔
1091
      write_dataset(file_id, "run_mode", "eigenvalue");
132✔
1092
      break;
72✔
1093
    case RunMode::PARTICLE:
×
1094
      write_dataset(file_id, "run_mode", "particle restart");
×
1095
      break;
1096
    default:
1097
      break;
1098
    }
1099
    write_dataset(file_id, "id", id());
407✔
1100
    write_dataset(file_id, "type", type().pdg_number());
407✔
1101

1102
    // Get source site data for the particle that got lost
1103
    int64_t i = current_work();
407✔
1104
    SourceSite site;
407✔
1105
    if (settings::run_mode == RunMode::EIGENVALUE) {
407✔
1106
      site = simulation::source_bank[i];
132✔
1107
    } else if (settings::run_mode == RunMode::FIXED_SOURCE &&
275✔
1108
               settings::use_shared_secondary_bank &&
275!
1109
               i < simulation::shared_secondary_bank_read.size()) {
55!
1110
      site = simulation::shared_secondary_bank_read[i];
×
1111
    } else if (settings::run_mode == RunMode::FIXED_SOURCE) {
275!
1112
      // Re-sample using the same seed used to generate the source particle.
1113
      // current_work() is 0-indexed, compute_particle_id expects 1-indexed.
1114
      int64_t id = compute_transport_seed(compute_particle_id(i + 1));
275✔
1115
      uint64_t seed = init_seed(id, STREAM_SOURCE);
275✔
1116
      site = sample_external_source(&seed);
275✔
1117
    }
1118
    write_dataset(file_id, "weight", site.wgt);
407✔
1119
    write_dataset(file_id, "energy", site.E);
407✔
1120
    write_dataset(file_id, "xyz", site.r);
407✔
1121
    write_dataset(file_id, "uvw", site.u);
407✔
1122
    write_dataset(file_id, "time", site.time);
407✔
1123

1124
    // Close file
1125
    file_close(file_id);
407✔
1126
  } // #pragma omp critical
1127
}
407✔
1128

1129
void Particle::update_neutron_xs(
2,147,483,647✔
1130
  int i_nuclide, int i_grid, int i_sab, double sab_frac, double ncrystal_xs)
1131
{
1132
  // Get microscopic cross section cache
1133
  auto& micro = this->neutron_xs(i_nuclide);
2,147,483,647✔
1134

1135
  // If the cache doesn't match, recalculate micro xs
1136
  if (this->E() != micro.last_E || this->sqrtkT() != micro.last_sqrtkT ||
2,147,483,647✔
1137
      i_sab != micro.index_sab || sab_frac != micro.sab_frac ||
2,147,483,647✔
1138
      ncrystal_xs != micro.ncrystal_xs) {
2,147,483,647!
1139
    data::nuclides[i_nuclide]->calculate_xs(i_sab, i_grid, sab_frac, *this);
2,147,483,647✔
1140

1141
    // If NCrystal is being used, update micro cross section cache
1142
    micro.ncrystal_xs = ncrystal_xs;
2,147,483,647✔
1143
    if (ncrystal_xs >= 0.0) {
2,147,483,647✔
1144
      data::nuclides[i_nuclide]->calculate_elastic_xs(*this);
11,018,953✔
1145
      ncrystal_update_micro(ncrystal_xs, micro);
11,018,953✔
1146
    }
1147
  }
1148
}
2,147,483,647✔
1149

1150
//==============================================================================
1151
// Non-method functions
1152
//==============================================================================
1153
void add_surf_source_to_bank(Particle& p, const Surface& surf)
2,147,483,647✔
1154
{
1155
  if (simulation::current_batch <= settings::n_inactive ||
2,147,483,647✔
1156
      simulation::surf_source_bank.full()) {
2,147,483,647✔
1157
    return;
2,147,483,647✔
1158
  }
1159

1160
  // If a cell/cellfrom/cellto parameter is defined
1161
  if (settings::ssw_cell_id != C_NONE) {
304,420✔
1162

1163
    // Retrieve cell index and storage type
1164
    int cell_idx = model::cell_map[settings::ssw_cell_id];
222,355✔
1165

1166
    if (surf.bc_) {
222,355✔
1167
      // Leave if cellto with vacuum boundary condition
1168
      if (surf.bc_->type() == "vacuum" &&
284,576✔
1169
          settings::ssw_cell_type == SSWCellType::To) {
32,878✔
1170
        return;
1171
      }
1172

1173
      // Leave if other boundary condition than vacuum
1174
      if (surf.bc_->type() != "vacuum") {
260,246✔
1175
        return;
1176
      }
1177
    }
1178

1179
    // Check if the cell of interest has been exited
1180
    bool exited = false;
1181
    for (int i = 0; i < p.n_coord_last(); ++i) {
268,743✔
1182
      if (p.cell_last(i) == cell_idx) {
167,963✔
1183
        exited = true;
59,528✔
1184
      }
1185
    }
1186

1187
    // Check if the cell of interest has been entered
1188
    bool entered = false;
1189
    for (int i = 0; i < p.n_coord(); ++i) {
233,627✔
1190
      if (p.coord(i).cell() == cell_idx) {
132,847✔
1191
        entered = true;
43,892✔
1192
      }
1193
    }
1194

1195
    // Vacuum boundary conditions: return if cell is not exited
1196
    if (surf.bc_) {
100,780✔
1197
      if (surf.bc_->type() == "vacuum" && !exited) {
41,426!
1198
        return;
1199
      }
1200
    } else {
1201

1202
      // If we both enter and exit the cell of interest
1203
      if (entered && exited) {
80,067✔
1204
        return;
1205
      }
1206

1207
      // If we did not enter nor exit the cell of interest
1208
      if (!entered && !exited) {
66,550✔
1209
        return;
1210
      }
1211

1212
      // If cellfrom and the cell before crossing is not the cell of
1213
      // interest
1214
      if (settings::ssw_cell_type == SSWCellType::From && !exited) {
63,786✔
1215
        return;
1216
      }
1217

1218
      // If cellto and the cell after crossing is not the cell of interest
1219
      if (settings::ssw_cell_type == SSWCellType::To && !entered) {
52,314✔
1220
        return;
1221
      }
1222
    }
1223
  }
1224

1225
  SourceSite site;
128,645✔
1226
  site.r = p.r();
128,645✔
1227
  site.u = p.u();
128,645✔
1228
  site.E = p.E();
128,645✔
1229
  site.time = p.time();
128,645✔
1230
  site.wgt = p.wgt();
128,645✔
1231
  site.delayed_group = p.delayed_group();
128,645✔
1232
  site.surf_id = surf.id_;
128,645✔
1233
  site.particle = p.type();
128,645✔
1234
  site.parent_id = p.id();
128,645✔
1235
  site.progeny_id = p.n_progeny();
128,645✔
1236
  int64_t idx = simulation::surf_source_bank.thread_safe_append(site);
128,645✔
1237
}
1238

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