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

openmc-dev / openmc / 23919098363

02 Apr 2026 07:53PM UTC coverage: 81.336% (+0.01%) from 81.324%
23919098363

Pull #3734

github

web-flow
Merge 5ddfca290 into d9b30bbbd
Pull Request #3734: Specify temperature from a field (structured mesh only)

17720 of 25601 branches covered (69.22%)

Branch coverage included in aggregate %.

183 of 204 new or added lines in 15 files covered. (89.71%)

69 existing lines in 3 files now uncovered.

58284 of 67843 relevant lines covered (85.91%)

44754406.4 hits per line

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

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

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

6
#include <fmt/core.h>
7

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

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

41
namespace openmc {
42

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

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

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

68
double Particle::mass() const
2,147,483,647✔
69
{
70
  switch (type().pdg_number()) {
2,147,483,647✔
71
  case PDG_NEUTRON:
72
    return MASS_NEUTRON_EV;
73
  case PDG_ELECTRON:
54,166,806✔
74
  case PDG_POSITRON:
54,166,806✔
75
    return MASS_ELECTRON_EV;
54,166,806✔
76
  default:
22,304,433✔
77
    return this->type().mass() * AMU_EV;
22,304,433✔
78
  }
79
}
80

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

94
  // Increment number of secondaries created (for ParticleProductionFilter)
95
  n_secondaries()++;
58,921,349✔
96

97
  auto& bank = secondary_bank().emplace_back();
58,921,349✔
98
  bank.particle = type;
58,921,349✔
99
  bank.wgt = wgt;
58,921,349✔
100
  bank.r = r();
58,921,349!
101
  bank.u = u;
58,921,349✔
102
  bank.E = settings::run_CE ? E : g();
58,921,349!
103
  bank.time = time();
58,921,349✔
104
  bank_second_E() += bank.E;
58,921,349✔
105
  return true;
58,921,349✔
106
}
107

108
void Particle::split(double wgt)
4,250,847✔
109
{
110
  auto& bank = secondary_bank().emplace_back();
4,250,847✔
111
  bank.particle = type();
4,250,847✔
112
  bank.wgt = wgt;
4,250,847✔
113
  bank.r = r();
4,250,847✔
114
  bank.u = u();
4,250,847✔
115
  bank.E = settings::run_CE ? E() : g();
4,250,847✔
116
  bank.time = time();
4,250,847✔
117

118
  // Convert signed index to a signed surface ID
119
  if (surface() == SURFACE_NONE) {
4,250,847✔
120
    bank.surf_id = SURFACE_NONE;
4,250,302✔
121
  } else {
122
    int surf_id = model::surfaces[surface_index()]->id_;
545✔
123
    bank.surf_id = (surface() > 0) ? surf_id : -surf_id;
545✔
124
  }
125
}
4,250,847✔
126

127
void Particle::from_source(const SourceSite* src)
241,003,329✔
128
{
129
  // Reset some attributes
130
  clear();
241,003,329✔
131
  surface() = SURFACE_NONE;
241,003,329✔
132
  cell_born() = C_NONE;
241,003,329✔
133
  material() = C_NONE;
241,003,329✔
134
  n_collision() = 0;
241,003,329✔
135
  fission() = false;
241,003,329✔
136
  zero_flux_derivs();
241,003,329✔
137
  lifetime() = 0.0;
241,003,329✔
138
#ifdef OPENMC_DAGMC_ENABLED
139
  history().reset();
22,070,596✔
140
#endif
141

142
  // Copy attributes from source bank site
143
  type() = src->particle;
241,003,329✔
144
  wgt() = src->wgt;
241,003,329✔
145
  wgt_last() = src->wgt;
241,003,329✔
146
  r() = src->r;
241,003,329✔
147
  u() = src->u;
241,003,329✔
148
  r_born() = src->r;
241,003,329✔
149
  r_last_current() = src->r;
241,003,329✔
150
  r_last() = src->r;
241,003,329✔
151
  u_last() = src->u;
241,003,329✔
152
  if (settings::run_CE) {
241,003,329✔
153
    E() = src->E;
125,077,382✔
154
    g() = 0;
125,077,382✔
155
  } else {
156
    g() = static_cast<int>(src->E);
115,925,947✔
157
    g_last() = static_cast<int>(src->E);
115,925,947✔
158
    E() = data::mg.energy_bin_avg_[g()];
115,925,947✔
159
  }
160
  E_last() = E();
241,003,329✔
161
  time() = src->time;
241,003,329✔
162
  time_last() = src->time;
241,003,329✔
163
  parent_nuclide() = src->parent_nuclide;
241,003,329✔
164
  delayed_group() = src->delayed_group;
241,003,329✔
165

166
  // Convert signed surface ID to signed index
167
  if (src->surf_id != SURFACE_NONE) {
241,003,329✔
168
    int index_plus_one = model::surface_map[std::abs(src->surf_id)] + 1;
110,545✔
169
    surface() = (src->surf_id > 0) ? index_plus_one : -index_plus_one;
110,545✔
170
  }
171
}
241,003,329✔
172

173
void Particle::event_calculate_xs()
2,147,483,647✔
174
{
175
  // Set the random number stream
176
  stream() = STREAM_TRACKING;
2,147,483,647✔
177

178
  // Store pre-collision particle properties
179
  wgt_last() = wgt();
2,147,483,647✔
180
  E_last() = E();
2,147,483,647✔
181
  u_last() = u();
2,147,483,647✔
182
  r_last() = r();
2,147,483,647✔
183
  time_last() = time();
2,147,483,647✔
184

185
  // Reset event variables
186
  event() = TallyEvent::KILL;
2,147,483,647✔
187
  event_nuclide() = NUCLIDE_NONE;
2,147,483,647✔
188
  event_mt() = REACTION_NONE;
2,147,483,647✔
189

190
  // If the cell hasn't been determined based on the particle's location,
191
  // initiate a search for the current cell. This generally happens at the
192
  // beginning of the history and again for any secondary particles
193
  if (lowest_coord().cell() == C_NONE) {
2,147,483,647✔
194
    if (!exhaustive_find_cell(*this)) {
232,332,420!
UNCOV
195
      mark_as_lost(
×
UNCOV
196
        "Could not find the cell containing particle " + std::to_string(id()));
×
UNCOV
197
      return;
×
198
    }
199

200
    // Set birth cell attribute
201
    if (cell_born() == C_NONE)
232,332,420!
202
      cell_born() = lowest_coord().cell();
232,332,420✔
203

204
    // Initialize last cells from current cell
205
    for (int j = 0; j < n_coord(); ++j) {
482,136,752✔
206
      cell_last(j) = coord(j).cell();
249,804,332✔
207
    }
208
    n_coord_last() = n_coord();
232,332,420✔
209

210
    // Update temperature of the particle if temperature field
211
    if (settings::temperature_field_on) {
232,332,420✔
212
      simulation::temperature_field.update_particle_temperature(*this);
44,033✔
213
    }
214
  }
215

216
  // Write particle track.
217
  if (write_track())
2,147,483,647✔
218
    write_particle_track(*this);
10,309✔
219

220
  if (settings::check_overlaps)
2,147,483,647!
UNCOV
221
    check_cell_overlap(*this);
×
222

223
  // Calculate microscopic and macroscopic cross sections
224
  if (material() != MATERIAL_VOID) {
2,147,483,647✔
225
    if (settings::run_CE) {
2,147,483,647✔
226
      if (material() != material_last() || sqrtkT() != sqrtkT_last() ||
2,147,483,647✔
227
          density_mult() != density_mult_last()) {
385,345,556✔
228
        // If the material is the same as the last material and the
229
        // temperature hasn't changed, we don't need to lookup cross
230
        // sections again.
231
        model::materials[material()]->calculate_xs(*this);
1,782,653,381✔
232
      }
233
    } else {
234
      // Get the MG data; unlike the CE case above, we have to re-calculate
235
      // cross sections for every collision since the cross sections may
236
      // be angle-dependent
237
      data::mg.macro_xs_[material()].calculate_xs(*this);
2,063,937,744✔
238

239
      // Update the particle's group while we know we are multi-group
240
      g_last() = g();
2,063,937,744✔
241
    }
242
  } else {
243
    macro_xs().total = 0.0;
111,838,607✔
244
    macro_xs().absorption = 0.0;
111,838,607✔
245
    macro_xs().fission = 0.0;
111,838,607✔
246
    macro_xs().nu_fission = 0.0;
111,838,607✔
247
  }
248
}
249

250
void Particle::event_advance()
2,147,483,647✔
251
{
252
  // Find the distance to the nearest geometry boundary
253
  boundary() = distance_to_boundary(*this);
2,147,483,647✔
254

255
  // Sample a distance to collision
256
  if (type() == ParticleType::electron() ||
2,147,483,647✔
257
      type() == ParticleType::positron()) {
2,147,483,647✔
258
    collision_distance() = material() == MATERIAL_VOID ? INFINITY : 0.0;
108,333,612!
259
  } else if (macro_xs().total == 0.0) {
2,147,483,647✔
260
    collision_distance() = INFINITY;
111,838,607✔
261
  } else {
262
    collision_distance() = -std::log(prn(current_seed())) / macro_xs().total;
2,147,483,647✔
263
  }
264

265
  // Find the distance to the nearest temperature mesh cell surface
266
  double distance_tmesh = INFTY;
2,147,483,647✔
267
  if (settings::temperature_field_on) {
2,147,483,647✔
268
    distance_tmesh =
1,364,077✔
269
      simulation::temperature_field.distance_to_next_boundary(r(), u());
1,364,077✔
270
  }
271

272
  // Calculate the distance corresponding to the time cutoff
273
  double speed = this->speed();
2,147,483,647✔
274
  double time_cutoff = settings::time_cutoff[type().transport_index()];
2,147,483,647✔
275
  double distance_cutoff =
2,147,483,647✔
276
    (time_cutoff < INFTY) ? (time_cutoff - time()) * speed : INFTY;
2,147,483,647✔
277

278
  // Select smaller distance
279
  double distance = std::min({boundary().distance(), collision_distance(),
2,147,483,647✔
280
    distance_cutoff, distance_tmesh});
281

282
  // Prepare the stop condition
283
  if (distance == distance_cutoff) {
2,147,483,647✔
284
    next_event() = EVENT_TIME_CUTOFF;
224,928✔
285
  } else if (distance == boundary().distance()) {
2,147,483,647✔
286
    next_event() = EVENT_CROSS_SURFACE;
1,487,446,170✔
287
  } else if (distance == distance_tmesh) {
2,147,483,647✔
288
    next_event() = EVENT_CROSS_TEMPERATURE_MESH;
308,517✔
289
  } else if (distance == collision_distance()) {
2,147,483,647!
290
    next_event() = EVENT_COLLIDE;
2,147,483,647✔
291
  }
292

293
  // Advance particle in space and time
294
  this->move_distance(distance);
2,147,483,647✔
295
  double dt = distance / speed;
2,147,483,647✔
296
  this->time() += dt;
2,147,483,647✔
297
  this->lifetime() += dt;
2,147,483,647✔
298

299
  // Score timed track-length tallies
300
  if (!model::active_timed_tracklength_tallies.empty()) {
2,147,483,647✔
301
    score_timed_tracklength_tally(*this, distance);
3,628,317✔
302
  }
303

304
  // Score track-length tallies
305
  if (!model::active_tracklength_tallies.empty()) {
2,147,483,647✔
306
    score_tracklength_tally(*this, distance);
1,722,580,161✔
307
  }
308

309
  // Score track-length estimate of k-eff
310
  if (settings::run_mode == RunMode::EIGENVALUE && type().is_neutron()) {
2,147,483,647✔
311
    keff_tally_tracklength() += wgt() * distance * macro_xs().nu_fission;
2,147,483,647✔
312
  }
313

314
  // Score flux derivative accumulators for differential tallies.
315
  if (!model::active_tallies.empty()) {
2,147,483,647✔
316
    score_track_derivative(*this, distance);
1,892,846,002✔
317
  }
318
}
2,147,483,647✔
319

320
void Particle::event_cross_temperature_mesh()
308,517✔
321
{
322
  // Update temperature of the particle
323
  simulation::temperature_field.update_particle_temperature(*this);
308,517✔
324
}
308,517✔
325

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

334
  // Set surface that particle is on and adjust coordinate levels
335
  surface() = boundary().surface();
2,147,483,647✔
336
  n_coord() = boundary().coord_level();
2,147,483,647✔
337

338
  if (boundary().lattice_translation()[0] != 0 ||
2,147,483,647✔
339
      boundary().lattice_translation()[1] != 0 ||
2,147,483,647✔
340
      boundary().lattice_translation()[2] != 0) {
1,860,400,274✔
341
    // Particle crosses lattice boundary
342

343
    bool verbose = settings::verbosity >= 10 || trace();
750,250,913!
344
    cross_lattice(*this, boundary(), verbose);
750,250,913✔
345
    event() = TallyEvent::LATTICE;
750,250,913✔
346

347
    // Score cell to cell partial currents
348
    if (!model::active_surface_tallies.empty()) {
750,250,913!
UNCOV
349
      auto& lat {*model::lattices[lowest_coord().lattice()]};
×
UNCOV
350
      bool is_valid;
×
UNCOV
351
      Direction normal =
×
UNCOV
352
        lat.get_normal(boundary().lattice_translation(), is_valid);
×
UNCOV
353
      if (is_valid) {
×
UNCOV
354
        normal /= normal.norm();
×
UNCOV
355
        score_surface_tally(*this, model::active_surface_tallies, normal);
×
356
      }
357
    }
358

359
  } else {
360

361
    const auto& surf {*model::surfaces[surface_index()].get()};
1,671,858,389✔
362

363
    // Particle crosses surface
364
    // If BC, add particle to surface source before crossing surface
365
    if (surf.surf_source_ && surf.bc_) {
1,671,858,389✔
366
      add_surf_source_to_bank(*this, surf);
720,615,732✔
367
    }
368
    this->cross_surface(surf);
1,671,858,389✔
369
    // If no BC, add particle to surface source after crossing surface
370
    if (surf.surf_source_ && !surf.bc_) {
1,671,858,380✔
371
      add_surf_source_to_bank(*this, surf);
950,004,821✔
372
    }
373
    if (settings::weight_window_checkpoint_surface) {
1,671,858,380✔
374
      apply_weight_windows(*this);
74,912✔
375
    }
376
    event() = TallyEvent::SURFACE;
1,671,858,380✔
377

378
    // Score cell to cell partial currents
379
    if (!model::active_surface_tallies.empty()) {
1,671,858,380✔
380
      Direction normal = surf.normal(r());
34,931,567✔
381
      normal /= normal.norm();
34,931,567✔
382
      score_surface_tally(*this, model::active_surface_tallies, normal);
34,931,567✔
383
    }
384
  }
385

386
  // Update temperature of the particle if temperature field
387
  if (settings::temperature_field_on) {
2,147,483,647✔
388
    simulation::temperature_field.update_particle_temperature(*this);
310,442✔
389
  }
390
}
2,147,483,647✔
391

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

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

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

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

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

410
  if (settings::run_CE) {
2,147,483,647✔
411
    collision(*this);
1,072,725,604✔
412
  } else {
413
    collision_mg(*this);
1,783,060,477✔
414
  }
415

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

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

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

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

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

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

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

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

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

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

473
  // Score flux derivative accumulators for differential tallies.
474
  if (!model::active_tallies.empty())
2,147,483,647✔
475
    score_collision_derivative(*this);
937,367,924✔
476

477
#ifdef OPENMC_DAGMC_ENABLED
478
  history().reset();
261,653,074✔
479
#endif
480
}
2,147,483,647✔
481

482
void Particle::event_revive_from_secondary()
2,147,483,647✔
483
{
484
  // If particle has too many events, display warning and kill it
485
  ++n_event();
2,147,483,647✔
486
  if (n_event() == settings::max_particle_events) {
2,147,483,647!
UNCOV
487
    warning("Particle " + std::to_string(id()) +
×
488
            " underwent maximum number of events.");
UNCOV
489
    wgt() = 0.0;
×
490
  }
491

492
  // Check for secondary particles if this particle is dead
493
  if (!alive()) {
2,147,483,647✔
494
    // Write final position for this particle
495
    if (write_track()) {
232,332,016✔
496
      write_particle_track(*this);
6,244✔
497
    }
498

499
    // If no secondary particles, break out of event loop
500
    if (secondary_bank().empty())
232,332,016✔
501
      return;
502

503
    from_source(&secondary_bank().back());
63,375,715✔
504
    secondary_bank().pop_back();
63,375,715✔
505
    n_event() = 0;
63,375,715✔
506
    bank_second_E() = 0.0;
63,375,715✔
507

508
    // Subtract secondary particle energy from interim pulse-height results
509
    if (!model::active_pulse_height_tallies.empty() &&
63,375,715✔
510
        this->type().is_photon()) {
15,499✔
511
      // Since the birth cell of the particle has not been set we
512
      // have to determine it before the energy of the secondary particle can be
513
      // removed from the pulse-height of this cell.
514
      if (lowest_coord().cell() == C_NONE) {
605!
515
        bool verbose = settings::verbosity >= 10 || trace();
605!
516
        if (!exhaustive_find_cell(*this, verbose)) {
605!
UNCOV
517
          mark_as_lost("Could not find the cell containing particle " +
×
UNCOV
518
                       std::to_string(id()));
×
UNCOV
519
          return;
×
520
        }
521
        // Set birth cell attribute
522
        if (cell_born() == C_NONE)
605!
523
          cell_born() = lowest_coord().cell();
605✔
524

525
        // Initialize last cells from current cell
526
        for (int j = 0; j < n_coord(); ++j) {
1,210✔
527
          cell_last(j) = coord(j).cell();
605✔
528
        }
529
        n_coord_last() = n_coord();
605✔
530
      }
531
      pht_secondary_particles();
605✔
532
    }
533

534
    // Enter new particle in particle track file
535
    if (write_track())
63,375,715✔
536
      add_particle_track(*this);
5,234✔
537
  }
538
}
539

540
void Particle::event_death()
168,957,301✔
541
{
542
#ifdef OPENMC_DAGMC_ENABLED
543
  history().reset();
15,436,415✔
544
#endif
545

546
  // Finish particle track output.
547
  if (write_track()) {
168,957,301✔
548
    finalize_particle_track(*this);
1,010✔
549
  }
550

551
// Contribute tally reduction variables to global accumulator
552
#pragma omp atomic
93,067,396✔
553
  global_tally_absorption += keff_tally_absorption();
168,957,301✔
554
#pragma omp atomic
93,362,094✔
555
  global_tally_collision += keff_tally_collision();
168,957,301✔
556
#pragma omp atomic
93,037,854✔
557
  global_tally_tracklength += keff_tally_tracklength();
168,957,301✔
558
#pragma omp atomic
92,468,000✔
559
  global_tally_leakage += keff_tally_leakage();
168,957,301✔
560

561
  // Reset particle tallies once accumulated
562
  keff_tally_absorption() = 0.0;
168,957,301✔
563
  keff_tally_collision() = 0.0;
168,957,301✔
564
  keff_tally_tracklength() = 0.0;
168,957,301✔
565
  keff_tally_leakage() = 0.0;
168,957,301✔
566

567
  if (!model::active_pulse_height_tallies.empty()) {
168,957,301✔
568
    score_pulse_height_tally(*this, model::active_pulse_height_tallies);
5,500✔
569
  }
570

571
  // Record the number of progeny created by this particle.
572
  // This data will be used to efficiently sort the fission bank.
573
  if (settings::run_mode == RunMode::EIGENVALUE) {
168,957,301✔
574
    int64_t offset = id() - 1 - simulation::work_index[mpi::rank];
141,578,700✔
575
    simulation::progeny_per_particle[offset] = n_progeny();
141,578,700✔
576
  }
577
}
168,957,301✔
578

579
void Particle::pht_collision_energy()
2,024✔
580
{
581
  // Adds the energy particles lose in a collision to the pulse-height
582

583
  // determine index of cell in pulse_height_cells
584
  auto it = std::find(model::pulse_height_cells.begin(),
2,024✔
585
    model::pulse_height_cells.end(), lowest_coord().cell());
2,024!
586

587
  if (it != model::pulse_height_cells.end()) {
2,024!
588
    int index = std::distance(model::pulse_height_cells.begin(), it);
2,024✔
589
    pht_storage()[index] += E_last() - E();
2,024✔
590

591
    // If the energy of the particle is below the cutoff, it will not be sampled
592
    // so its energy is added to the pulse-height in the cell
593
    int photon = ParticleType::photon().transport_index();
2,024✔
594
    if (E() < settings::energy_cutoff[photon]) {
2,024✔
595
      pht_storage()[index] += E();
825✔
596
    }
597
  }
598
}
2,024✔
599

600
void Particle::pht_secondary_particles()
605✔
601
{
602
  // Removes the energy of secondary produced particles from the pulse-height
603

604
  // determine index of cell in pulse_height_cells
605
  auto it = std::find(model::pulse_height_cells.begin(),
605✔
606
    model::pulse_height_cells.end(), cell_born());
605!
607

608
  if (it != model::pulse_height_cells.end()) {
605!
609
    int index = std::distance(model::pulse_height_cells.begin(), it);
605✔
610
    pht_storage()[index] -= E();
605✔
611
  }
612
}
605✔
613

614
void Particle::cross_surface(const Surface& surf)
1,673,782,143✔
615
{
616

617
  if (settings::verbosity >= 10 || trace()) {
1,673,782,143✔
618
    write_message(1, "    Crossing surface {}", surf.id_);
66✔
619
  }
620

621
// if we're crossing a CSG surface, make sure the DAG history is reset
622
#ifdef OPENMC_DAGMC_ENABLED
623
  if (surf.geom_type() == GeometryType::CSG)
152,856,114✔
624
    history().reset();
152,798,955✔
625
#endif
626

627
  // Handle any applicable boundary conditions.
628
  if (surf.bc_ && settings::run_mode != RunMode::PLOTTING &&
1,673,782,143!
629
      settings::run_mode != RunMode::VOLUME) {
630
    surf.bc_->handle_particle(*this, surf);
720,967,840✔
631
    return;
720,967,840✔
632
  }
633

634
  // ==========================================================================
635
  // SEARCH NEIGHBOR LISTS FOR NEXT CELL
636

637
#ifdef OPENMC_DAGMC_ENABLED
638
  // in DAGMC, we know what the next cell should be
639
  if (surf.geom_type() == GeometryType::DAG) {
86,870,090✔
640
    int32_t i_cell = next_cell(surface_index(), cell_last(n_coord() - 1),
46,350✔
641
                       lowest_coord().universe()) -
46,350✔
642
                     1;
46,350✔
643
    // save material, temperature, and density multiplier
644
    material_last() = material();
46,350✔
645
    sqrtkT_last() = sqrtkT();
46,350✔
646
    density_mult_last() = density_mult();
46,350✔
647
    // set new cell value
648
    lowest_coord().cell() = i_cell;
46,350✔
649
    auto& cell = model::cells[i_cell];
46,350✔
650

651
    cell_instance() = 0;
46,350✔
652
    if (cell->distribcell_index_ >= 0)
46,350✔
653
      cell_instance() = cell_instance_at_level(*this, n_coord() - 1);
45,326✔
654

655
    material() = cell->material(cell_instance());
46,350!
656
    sqrtkT() = cell->sqrtkT(cell_instance());
46,350!
657
    density_mult() = cell->density_mult(cell_instance());
46,350✔
658
    return;
46,350✔
659
  }
660
#endif
661

662
  bool verbose = settings::verbosity >= 10 || trace();
952,767,953!
663
  if (neighbor_list_find_cell(*this, verbose)) {
952,767,953✔
664
    return;
665
  }
666

667
  // ==========================================================================
668
  // COULDN'T FIND PARTICLE IN NEIGHBORING CELLS, SEARCH ALL CELLS
669

670
  // Remove lower coordinate levels
671
  n_coord() = 1;
29,911✔
672
  bool found = exhaustive_find_cell(*this, verbose);
29,911✔
673

674
  if (settings::run_mode != RunMode::PLOTTING && (!found)) {
29,911!
675
    // If a cell is still not found, there are two possible causes: 1) there is
676
    // a void in the model, and 2) the particle hit a surface at a tangent. If
677
    // the particle is really traveling tangent to a surface, if we move it
678
    // forward a tiny bit it should fix the problem.
679

680
    surface() = SURFACE_NONE;
5,799✔
681
    n_coord() = 1;
5,799✔
682
    r() += TINY_BIT * u();
5,799✔
683

684
    // Couldn't find next cell anywhere! This probably means there is an actual
685
    // undefined region in the geometry.
686

687
    if (!exhaustive_find_cell(*this, verbose)) {
5,799!
688
      mark_as_lost("After particle " + std::to_string(id()) +
17,388✔
689
                   " crossed surface " + std::to_string(surf.id_) +
17,388✔
690
                   " it could not be located in any cell and it did not leak.");
691
      return;
5,790✔
692
    }
693
  }
694
}
695

696
void Particle::cross_vacuum_bc(const Surface& surf)
35,098,891✔
697
{
698
  // Score any surface current tallies -- note that the particle is moved
699
  // forward slightly so that if the mesh boundary is on the surface, it is
700
  // still processed
701

702
  if (!model::active_meshsurf_tallies.empty()) {
35,098,891✔
703
    // TODO: Find a better solution to score surface currents than
704
    // physically moving the particle forward slightly
705

706
    r() += TINY_BIT * u();
937,222✔
707
    score_meshsurface_tally(*this, model::active_meshsurf_tallies);
937,222✔
708
  }
709

710
  // Score to global leakage tally
711
  keff_tally_leakage() += wgt();
35,098,891✔
712

713
  // Kill the particle
714
  wgt() = 0.0;
35,098,891✔
715

716
  // Display message
717
  if (settings::verbosity >= 10 || trace()) {
35,098,891!
718
    write_message(1, "    Leaked out of surface {}", surf.id_);
22✔
719
  }
720
}
35,098,891✔
721

722
void Particle::cross_reflective_bc(const Surface& surf, Direction new_u)
684,629,345✔
723
{
724
  // Do not handle reflective boundary conditions on lower universes
725
  if (n_coord() != 1) {
684,629,345!
UNCOV
726
    mark_as_lost("Cannot reflect particle " + std::to_string(id()) +
×
727
                 " off surface in a lower universe.");
UNCOV
728
    return;
×
729
  }
730

731
  // Score surface currents since reflection causes the direction of the
732
  // particle to change. For surface filters, we need to score the tallies
733
  // twice, once before the particle's surface attribute has changed and
734
  // once after. For mesh surface filters, we need to artificially move
735
  // the particle slightly back in case the surface crossing is coincident
736
  // with a mesh boundary
737

738
  if (!model::active_surface_tallies.empty()) {
684,629,345✔
739
    Direction normal = surf.normal(r());
285,021✔
740
    normal /= normal.norm();
285,021✔
741
    score_surface_tally(*this, model::active_surface_tallies, normal);
285,021✔
742
  }
743

744
  if (!model::active_meshsurf_tallies.empty()) {
684,629,345✔
745
    Position r {this->r()};
46,885,487✔
746
    this->r() -= TINY_BIT * u();
46,885,487✔
747
    score_meshsurface_tally(*this, model::active_meshsurf_tallies);
46,885,487✔
748
    this->r() = r;
46,885,487✔
749
  }
750

751
  // Set the new particle direction
752
  u() = new_u;
684,629,345✔
753

754
  // Reassign particle's cell and surface
755
  coord(0).cell() = cell_last(0);
684,629,345✔
756
  surface() = -surface();
684,629,345✔
757

758
  // If a reflective surface is coincident with a lattice or universe
759
  // boundary, it is necessary to redetermine the particle's coordinates in
760
  // the lower universes.
761
  // (unless we're using a dagmc model, which has exactly one universe)
762
  n_coord() = 1;
684,629,345✔
763
  if (surf.geom_type() != GeometryType::DAG &&
1,369,255,932!
764
      !neighbor_list_find_cell(*this)) {
684,626,587✔
UNCOV
765
    mark_as_lost("Couldn't find particle after reflecting from surface " +
×
UNCOV
766
                 std::to_string(surf.id_) + ".");
×
UNCOV
767
    return;
×
768
  }
769

770
  // Set previous coordinate going slightly past surface crossing
771
  r_last_current() = r() + TINY_BIT * u();
684,629,345✔
772

773
  // Diagnostic message
774
  if (settings::verbosity >= 10 || trace()) {
684,629,345!
UNCOV
775
    write_message(1, "    Reflected from surface {}", surf.id_);
×
776
  }
777
}
778

779
void Particle::cross_periodic_bc(
2,245,070✔
780
  const Surface& surf, Position new_r, Direction new_u, int new_surface)
781
{
782
  // Do not handle periodic boundary conditions on lower universes
783
  if (n_coord() != 1) {
2,245,070!
UNCOV
784
    mark_as_lost(
×
UNCOV
785
      "Cannot transfer particle " + std::to_string(id()) +
×
786
      " across surface in a lower universe. Boundary conditions must be "
787
      "applied to root universe.");
788
    return;
×
789
  }
790

791
  // Score surface currents since reflection causes the direction of the
792
  // particle to change -- artificially move the particle slightly back in
793
  // case the surface crossing is coincident with a mesh boundary
794
  if (!model::active_meshsurf_tallies.empty()) {
2,245,070!
UNCOV
795
    Position r {this->r()};
×
UNCOV
796
    this->r() -= TINY_BIT * u();
×
UNCOV
797
    score_meshsurface_tally(*this, model::active_meshsurf_tallies);
×
UNCOV
798
    this->r() = r;
×
799
  }
800

801
  // Adjust the particle's location and direction.
802
  r() = new_r;
2,245,070✔
803
  u() = new_u;
2,245,070✔
804

805
  // Reassign particle's surface
806
  surface() = new_surface;
2,245,070✔
807

808
  // Figure out what cell particle is in now
809
  n_coord() = 1;
2,245,070✔
810

811
  if (!neighbor_list_find_cell(*this)) {
2,245,070!
UNCOV
812
    mark_as_lost("Couldn't find particle after hitting periodic "
×
UNCOV
813
                 "boundary on surface " +
×
UNCOV
814
                 std::to_string(surf.id_) + ".");
×
UNCOV
815
    return;
×
816
  }
817

818
  // Set previous coordinate going slightly past surface crossing
819
  r_last_current() = r() + TINY_BIT * u();
2,245,070✔
820

821
  // Diagnostic message
822
  if (settings::verbosity >= 10 || trace()) {
2,245,070!
UNCOV
823
    write_message(1, "    Hit periodic boundary on surface {}", surf.id_);
×
824
  }
825
}
826

827
void Particle::mark_as_lost(const char* message)
5,799✔
828
{
829
  // Print warning and write lost particle file
830
  warning(message);
5,799✔
831
  if (settings::max_write_lost_particles < 0 ||
5,799✔
832
      simulation::n_lost_particles < settings::max_write_lost_particles) {
5,500✔
833
    write_restart();
374✔
834
  }
835
  // Increment number of lost particles
836
  wgt() = 0.0;
5,799✔
837
#pragma omp atomic
3,154✔
838
  simulation::n_lost_particles += 1;
2,645✔
839

840
  // Count the total number of simulated particles (on this processor)
841
  auto n = simulation::current_batch * settings::gen_per_batch *
5,799✔
842
           simulation::work_per_rank;
843

844
  // Abort the simulation if the maximum number of lost particles has been
845
  // reached
846
  if (simulation::n_lost_particles >= settings::max_lost_particles &&
5,799✔
847
      simulation::n_lost_particles >= settings::rel_max_lost_particles * n) {
9!
848
    fatal_error("Maximum number of lost particles has been reached.");
9✔
849
  }
850
}
5,790✔
851

852
void Particle::write_restart() const
374✔
853
{
854
  // Dont write another restart file if in particle restart mode
855
  if (settings::run_mode == RunMode::PARTICLE)
374✔
856
    return;
22✔
857

858
  // Set up file name
859
  auto filename = fmt::format("{}particle_{}_{}.h5", settings::path_output,
352✔
860
    simulation::current_batch, id());
352✔
861

862
#pragma omp critical(WriteParticleRestart)
187✔
863
  {
352✔
864
    // Create file
865
    hid_t file_id = file_open(filename, 'w');
352✔
866

867
    // Write filetype and version info
868
    write_attribute(file_id, "filetype", "particle restart");
352✔
869
    write_attribute(file_id, "version", VERSION_PARTICLE_RESTART);
352✔
870
    write_attribute(file_id, "openmc_version", VERSION);
352✔
871
#ifdef GIT_SHA1
872
    write_attr_string(file_id, "git_sha1", GIT_SHA1);
873
#endif
874

875
    // Write data to file
876
    write_dataset(file_id, "current_batch", simulation::current_batch);
352✔
877
    write_dataset(file_id, "generations_per_batch", settings::gen_per_batch);
352✔
878
    write_dataset(file_id, "current_generation", simulation::current_gen);
352✔
879
    write_dataset(file_id, "n_particles", settings::n_particles);
352✔
880
    switch (settings::run_mode) {
352!
881
    case RunMode::FIXED_SOURCE:
220✔
882
      write_dataset(file_id, "run_mode", "fixed source");
220✔
883
      break;
115✔
884
    case RunMode::EIGENVALUE:
132✔
885
      write_dataset(file_id, "run_mode", "eigenvalue");
132✔
886
      break;
72✔
UNCOV
887
    case RunMode::PARTICLE:
×
UNCOV
888
      write_dataset(file_id, "run_mode", "particle restart");
×
889
      break;
890
    default:
891
      break;
892
    }
893
    write_dataset(file_id, "id", id());
352✔
894
    write_dataset(file_id, "type", type().pdg_number());
352✔
895

896
    int64_t i = current_work();
352✔
897
    if (settings::run_mode == RunMode::EIGENVALUE) {
352✔
898
      // take source data from primary bank for eigenvalue simulation
899
      write_dataset(file_id, "weight", simulation::source_bank[i - 1].wgt);
132✔
900
      write_dataset(file_id, "energy", simulation::source_bank[i - 1].E);
132✔
901
      write_dataset(file_id, "xyz", simulation::source_bank[i - 1].r);
132✔
902
      write_dataset(file_id, "uvw", simulation::source_bank[i - 1].u);
132✔
903
      write_dataset(file_id, "time", simulation::source_bank[i - 1].time);
132✔
904
    } else if (settings::run_mode == RunMode::FIXED_SOURCE) {
220!
905
      // re-sample using rng random number seed used to generate source particle
906
      int64_t id = (simulation::total_gen + overall_generation() - 1) *
220✔
907
                     settings::n_particles +
220✔
908
                   simulation::work_index[mpi::rank] + i;
220✔
909
      uint64_t seed = init_seed(id, STREAM_SOURCE);
220✔
910
      // re-sample source site
911
      auto site = sample_external_source(&seed);
220✔
912
      write_dataset(file_id, "weight", site.wgt);
220✔
913
      write_dataset(file_id, "energy", site.E);
220✔
914
      write_dataset(file_id, "xyz", site.r);
220✔
915
      write_dataset(file_id, "uvw", site.u);
220✔
916
      write_dataset(file_id, "time", site.time);
220✔
917
    }
918

919
    // Close file
920
    file_close(file_id);
352✔
921
  } // #pragma omp critical
922
}
352✔
923

924
void Particle::update_neutron_xs(
2,147,483,647✔
925
  int i_nuclide, int i_grid, int i_sab, double sab_frac, double ncrystal_xs)
926
{
927
  // Get microscopic cross section cache
928
  auto& micro = this->neutron_xs(i_nuclide);
2,147,483,647✔
929

930
  // If the cache doesn't match, recalculate micro xs
931
  if (this->E() != micro.last_E || this->sqrtkT() != micro.last_sqrtkT ||
2,147,483,647✔
932
      i_sab != micro.index_sab || sab_frac != micro.sab_frac ||
2,147,483,647✔
933
      ncrystal_xs != micro.ncrystal_xs) {
2,147,483,647!
934
    data::nuclides[i_nuclide]->calculate_xs(i_sab, i_grid, sab_frac, *this);
2,147,483,647✔
935

936
    // If NCrystal is being used, update micro cross section cache
937
    micro.ncrystal_xs = ncrystal_xs;
2,147,483,647✔
938
    if (ncrystal_xs >= 0.0) {
2,147,483,647✔
939
      data::nuclides[i_nuclide]->calculate_elastic_xs(*this);
11,018,953✔
940
      ncrystal_update_micro(ncrystal_xs, micro);
11,018,953✔
941
    }
942
  }
943
}
2,147,483,647✔
944

945
//==============================================================================
946
// Non-method functions
947
//==============================================================================
948
void add_surf_source_to_bank(Particle& p, const Surface& surf)
1,670,620,553✔
949
{
950
  if (simulation::current_batch <= settings::n_inactive ||
1,670,620,553✔
951
      simulation::surf_source_bank.full()) {
1,285,152,194✔
952
    return;
1,670,490,900✔
953
  }
954

955
  // If a cell/cellfrom/cellto parameter is defined
956
  if (settings::ssw_cell_id != C_NONE) {
337,078✔
957

958
    // Retrieve cell index and storage type
959
    int cell_idx = model::cell_map[settings::ssw_cell_id];
254,433✔
960

961
    if (surf.bc_) {
254,433✔
962
      // Leave if cellto with vacuum boundary condition
963
      if (surf.bc_->type() == "vacuum" &&
298,918✔
964
          settings::ssw_cell_type == SSWCellType::To) {
33,099✔
965
        return;
966
      }
967

968
      // Leave if other boundary condition than vacuum
969
      if (surf.bc_->type() != "vacuum") {
274,648✔
970
        return;
971
      }
972
    }
973

974
    // Check if the cell of interest has been exited
975
    bool exited = false;
976
    for (int i = 0; i < p.n_coord_last(); ++i) {
333,665✔
977
      if (p.cell_last(i) == cell_idx) {
207,727✔
978
        exited = true;
73,764✔
979
      }
980
    }
981

982
    // Check if the cell of interest has been entered
983
    bool entered = false;
984
    for (int i = 0; i < p.n_coord(); ++i) {
297,967✔
985
      if (p.coord(i).cell() == cell_idx) {
172,029✔
986
        entered = true;
57,514✔
987
      }
988
    }
989

990
    // Vacuum boundary conditions: return if cell is not exited
991
    if (surf.bc_) {
125,938✔
992
      if (surf.bc_->type() == "vacuum" && !exited) {
41,928!
993
        return;
994
      }
995
    } else {
996

997
      // If we both enter and exit the cell of interest
998
      if (entered && exited) {
104,974✔
999
        return;
1000
      }
1001

1002
      // If we did not enter nor exit the cell of interest
1003
      if (!entered && !exited) {
77,771✔
1004
        return;
1005
      }
1006

1007
      // If cellfrom and the cell before crossing is not the cell of
1008
      // interest
1009
      if (settings::ssw_cell_type == SSWCellType::From && !exited) {
64,272✔
1010
        return;
1011
      }
1012

1013
      // If cellto and the cell after crossing is not the cell of interest
1014
      if (settings::ssw_cell_type == SSWCellType::To && !entered) {
52,732✔
1015
        return;
1016
      }
1017
    }
1018
  }
1019

1020
  SourceSite site;
129,653✔
1021
  site.r = p.r();
129,653✔
1022
  site.u = p.u();
129,653✔
1023
  site.E = p.E();
129,653✔
1024
  site.time = p.time();
129,653✔
1025
  site.wgt = p.wgt();
129,653✔
1026
  site.delayed_group = p.delayed_group();
129,653✔
1027
  site.surf_id = surf.id_;
129,653✔
1028
  site.particle = p.type();
129,653✔
1029
  site.parent_id = p.id();
129,653✔
1030
  site.progeny_id = p.n_progeny();
129,653✔
1031
  int64_t idx = simulation::surf_source_bank.thread_safe_append(site);
129,653✔
1032
}
1033

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