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

openmc-dev / openmc / 17852533892

19 Sep 2025 08:10AM UTC coverage: 85.197% (-0.004%) from 85.201%
17852533892

push

github

web-flow
Combing for fission site sampling, and delayed neutron emission time (#2992)

Co-authored-by: Gavin Ridley <gavin.keith.ridley@gmail.com>
Co-authored-by: Paul Romano <paul.k.romano@gmail.com>

54 of 58 new or added lines in 7 files covered. (93.1%)

4 existing lines in 3 files now uncovered.

53173 of 62412 relevant lines covered (85.2%)

38753013.29 hits per line

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

95.64
/src/eigenvalue.cpp
1
#include "openmc/eigenvalue.h"
2

3
#include "xtensor/xbuilder.hpp"
4
#include "xtensor/xmath.hpp"
5
#include "xtensor/xtensor.hpp"
6
#include "xtensor/xview.hpp"
7

8
#include "openmc/array.h"
9
#include "openmc/bank.h"
10
#include "openmc/capi.h"
11
#include "openmc/constants.h"
12
#include "openmc/error.h"
13
#include "openmc/hdf5_interface.h"
14
#include "openmc/ifp.h"
15
#include "openmc/math_functions.h"
16
#include "openmc/mesh.h"
17
#include "openmc/message_passing.h"
18
#include "openmc/random_lcg.h"
19
#include "openmc/search.h"
20
#include "openmc/settings.h"
21
#include "openmc/simulation.h"
22
#include "openmc/tallies/tally.h"
23
#include "openmc/timer.h"
24

25
#include <algorithm> // for min
26
#include <cmath>     // for sqrt, abs, pow
27
#include <iterator>  // for back_inserter
28
#include <limits>    //for infinity
29
#include <string>
30

31
namespace openmc {
32

33
//==============================================================================
34
// Global variables
35
//==============================================================================
36

37
namespace simulation {
38

39
double keff_generation;
40
array<double, 2> k_sum;
41
vector<double> entropy;
42
xt::xtensor<double, 1> source_frac;
43

44
} // namespace simulation
45

46
//==============================================================================
47
// Non-member functions
48
//==============================================================================
49

50
void calculate_generation_keff()
80,572✔
51
{
52
  const auto& gt = simulation::global_tallies;
80,572✔
53

54
  // Get keff for this generation by subtracting off the starting value
55
  simulation::keff_generation =
80,572✔
56
    gt(GlobalTally::K_TRACKLENGTH, TallyResult::VALUE) -
80,572✔
57
    simulation::keff_generation;
58

59
  double keff_reduced;
60
#ifdef OPENMC_MPI
61
  if (settings::solver_type != SolverType::RANDOM_RAY) {
46,593✔
62
    // Combine values across all processors
63
    MPI_Allreduce(&simulation::keff_generation, &keff_reduced, 1, MPI_DOUBLE,
44,693✔
64
      MPI_SUM, mpi::intracomm);
65
  } else {
66
    // If using random ray, MPI parallelism is provided by domain replication.
67
    // As such, all fluxes will be reduced at the end of each transport sweep,
68
    // such that all ranks have identical scalar flux vectors, and will all
69
    // independently compute the same value of k. Thus, there is no need to
70
    // perform any additional MPI reduction here.
71
    keff_reduced = simulation::keff_generation;
1,900✔
72
  }
73
#else
74
  keff_reduced = simulation::keff_generation;
33,979✔
75
#endif
76

77
  // Normalize single batch estimate of k
78
  // TODO: This should be normalized by total_weight, not by n_particles
79
  if (settings::solver_type != SolverType::RANDOM_RAY) {
80,572✔
80
    keff_reduced /= settings::n_particles;
77,532✔
81
  }
82

83
  simulation::k_generation.push_back(keff_reduced);
80,572✔
84
}
80,572✔
85

86
void synchronize_bank()
77,532✔
87
{
88
  simulation::time_bank.start();
77,532✔
89

90
  // In order to properly understand the fission bank algorithm, you need to
91
  // think of the fission and source bank as being one global array divided
92
  // over multiple processors. At the start, each processor has a random amount
93
  // of fission bank sites -- each processor needs to know the total number of
94
  // sites in order to figure out the probability for selecting
95
  // sites. Furthermore, each proc also needs to know where in the 'global'
96
  // fission bank its own sites starts in order to ensure reproducibility by
97
  // skipping ahead to the proper seed.
98

99
#ifdef OPENMC_MPI
100
  int64_t start = 0;
44,693✔
101
  int64_t n_bank = simulation::fission_bank.size();
44,693✔
102
  MPI_Exscan(&n_bank, &start, 1, MPI_INT64_T, MPI_SUM, mpi::intracomm);
44,693✔
103

104
  // While we would expect the value of start on rank 0 to be 0, the MPI
105
  // standard says that the receive buffer on rank 0 is undefined and not
106
  // significant
107
  if (mpi::rank == 0)
44,693✔
108
    start = 0;
29,133✔
109

110
  int64_t finish = start + simulation::fission_bank.size();
44,693✔
111
  int64_t total = finish;
44,693✔
112
  MPI_Bcast(&total, 1, MPI_INT64_T, mpi::n_procs - 1, mpi::intracomm);
44,693✔
113

114
#else
115
  int64_t start = 0;
32,839✔
116
  int64_t finish = simulation::fission_bank.size();
32,839✔
117
  int64_t total = finish;
32,839✔
118
#endif
119

120
  // If there are not that many particles per generation, it's possible that no
121
  // fission sites were created at all on a single processor. Rather than add
122
  // extra logic to treat this circumstance, we really want to ensure the user
123
  // runs enough particles to avoid this in the first place.
124

125
  if (simulation::fission_bank.size() == 0) {
77,532✔
126
    fatal_error(
×
127
      "No fission sites banked on MPI rank " + std::to_string(mpi::rank));
×
128
  }
129

130
  simulation::time_bank_sample.start();
77,532✔
131

132
  // Allocate temporary source bank -- we don't really know how many fission
133
  // sites were created, so overallocate by a factor of 3
134
  int64_t index_temp = 0;
77,532✔
135

136
  vector<SourceSite> temp_sites(3 * simulation::work_per_rank);
77,532✔
137

138
  // Temporary banks for IFP
139
  vector<vector<int>> temp_delayed_groups;
77,532✔
140
  vector<vector<double>> temp_lifetimes;
77,532✔
141
  if (settings::ifp_on) {
77,532✔
142
    resize_ifp_data(
320✔
143
      temp_delayed_groups, temp_lifetimes, 3 * simulation::work_per_rank);
144
  }
145

146
  // ==========================================================================
147
  // SAMPLE N_PARTICLES FROM FISSION BANK AND PLACE IN TEMP_SITES
148

149
  // We use Uniform Combing method to exactly get the targeted particle size
150
  // [https://doi.org/10.1080/00295639.2022.2091906]
151

152
  // Make sure all processors use the same random number seed.
153
  int64_t id = simulation::total_gen + overall_generation();
77,532✔
154
  uint64_t seed = init_seed(id, STREAM_TRACKING);
77,532✔
155

156
  // Comb specification
157
  double teeth_distance = static_cast<double>(total) / settings::n_particles;
77,532✔
158
  double teeth_offset = prn(&seed) * teeth_distance;
77,532✔
159

160
  // First and last hitting tooth
161
  int64_t end = start + simulation::fission_bank.size();
77,532✔
162
  int64_t tooth_start = std::ceil((start - teeth_offset) / teeth_distance);
77,532✔
163
  int64_t tooth_end = std::floor((end - teeth_offset) / teeth_distance) + 1;
77,532✔
164

165
  // Locally comb particles in fission_bank
166
  double tooth = tooth_start * teeth_distance + teeth_offset;
77,532✔
167
  for (int64_t i = tooth_start; i < tooth_end; i++) {
134,529,932✔
168
    int64_t idx = std::floor(tooth) - start;
134,452,400✔
169
    temp_sites[index_temp] = simulation::fission_bank[idx];
134,452,400✔
170
    if (settings::ifp_on) {
134,452,400✔
171
      copy_ifp_data_from_fission_banks(
220,000✔
172
        idx, temp_delayed_groups[index_temp], temp_lifetimes[index_temp]);
220,000✔
173
    }
174
    ++index_temp;
134,452,400✔
175

176
    // Next tooth
177
    tooth += teeth_distance;
134,452,400✔
178
  }
179

180
  // At this point, the sampling of source sites is done and now we need to
181
  // figure out where to send source sites. Since it is possible that one
182
  // processor's share of the source bank spans more than just the immediate
183
  // neighboring processors, we have to perform an ALLGATHER to determine the
184
  // indices for all processors
185

186
#ifdef OPENMC_MPI
187
  // First do an exclusive scan to get the starting indices for
188
  start = 0;
44,693✔
189
  MPI_Exscan(&index_temp, &start, 1, MPI_INT64_T, MPI_SUM, mpi::intracomm);
44,693✔
190
  finish = start + index_temp;
44,693✔
191

192
  // TODO: protect for MPI_Exscan at rank 0
193

194
  // Allocate space for bank_position if this hasn't been done yet
195
  int64_t bank_position[mpi::n_procs];
44,693✔
196
  MPI_Allgather(
44,693✔
197
    &start, 1, MPI_INT64_T, bank_position, 1, MPI_INT64_T, mpi::intracomm);
198
#else
199
  start = 0;
32,839✔
200
  finish = index_temp;
32,839✔
201
#endif
202

203
  simulation::time_bank_sample.stop();
77,532✔
204
  simulation::time_bank_sendrecv.start();
77,532✔
205

206
#ifdef OPENMC_MPI
207
  // ==========================================================================
208
  // SEND BANK SITES TO NEIGHBORS
209

210
  // IFP number of generation
211
  int ifp_n_generation;
212
  if (settings::ifp_on) {
44,693✔
213
    broadcast_ifp_n_generation(
200✔
214
      ifp_n_generation, temp_delayed_groups, temp_lifetimes);
215
  }
216

217
  int64_t index_local = 0;
44,693✔
218
  vector<MPI_Request> requests;
44,693✔
219

220
  // IFP send buffers
221
  vector<int> send_delayed_groups;
44,693✔
222
  vector<double> send_lifetimes;
44,693✔
223

224
  if (start < settings::n_particles) {
44,693✔
225
    // Determine the index of the processor which has the first part of the
226
    // source_bank for the local processor
227
    int neighbor = upper_bound_index(
44,693✔
228
      simulation::work_index.begin(), simulation::work_index.end(), start);
44,693✔
229

230
    // Resize IFP send buffers
231
    if (settings::ifp_on && mpi::n_procs > 1) {
44,693✔
232
      resize_ifp_data(send_delayed_groups, send_lifetimes,
200✔
233
        ifp_n_generation * 3 * simulation::work_per_rank);
200✔
234
    }
235

236
    while (start < finish) {
67,933✔
237
      // Determine the number of sites to send
238
      int64_t n =
239
        std::min(simulation::work_index[neighbor + 1], finish) - start;
60,017✔
240

241
      // Initiate an asynchronous send of source sites to the neighboring
242
      // process
243
      if (neighbor != mpi::rank) {
60,017✔
244
        requests.emplace_back();
15,324✔
245
        MPI_Isend(&temp_sites[index_local], static_cast<int>(n),
15,324✔
246
          mpi::source_site, neighbor, mpi::rank, mpi::intracomm,
247
          &requests.back());
15,324✔
248

249
        if (settings::ifp_on) {
15,324✔
250
          // Send IFP data
251
          send_ifp_info(index_local, n, ifp_n_generation, neighbor, requests,
100✔
252
            temp_delayed_groups, send_delayed_groups, temp_lifetimes,
253
            send_lifetimes);
254
        }
255
      }
256

257
      // Increment all indices
258
      start += n;
60,017✔
259
      index_local += n;
60,017✔
260
      ++neighbor;
60,017✔
261

262
      // Check for sites out of bounds -- this only happens in the rare
263
      // circumstance that a processor close to the end has so many sites that
264
      // it would exceed the bank on the last processor
265
      if (neighbor > mpi::n_procs - 1)
60,017✔
266
        break;
36,777✔
267
    }
268
  }
269

270
  // ==========================================================================
271
  // RECEIVE BANK SITES FROM NEIGHBORS OR TEMPORARY BANK
272

273
  start = simulation::work_index[mpi::rank];
44,693✔
274
  index_local = 0;
44,693✔
275

276
  // IFP receive buffers
277
  vector<int> recv_delayed_groups;
44,693✔
278
  vector<double> recv_lifetimes;
44,693✔
279
  vector<DeserializationInfo> deserialization_info;
44,693✔
280

281
  // Determine what process has the source sites that will need to be stored at
282
  // the beginning of this processor's source bank.
283

284
  int neighbor;
285
  if (start >= bank_position[mpi::n_procs - 1]) {
44,693✔
286
    neighbor = mpi::n_procs - 1;
21,489✔
287
  } else {
288
    neighbor =
23,204✔
289
      upper_bound_index(bank_position, bank_position + mpi::n_procs, start);
23,204✔
290
  }
291

292
  // Resize IFP receive buffers
293
  if (settings::ifp_on && mpi::n_procs > 1) {
44,693✔
294
    resize_ifp_data(recv_delayed_groups, recv_lifetimes,
200✔
295
      ifp_n_generation * simulation::work_per_rank);
200✔
296
  }
297

298
  while (start < simulation::work_index[mpi::rank + 1]) {
104,710✔
299
    // Determine how many sites need to be received
300
    int64_t n;
301
    if (neighbor == mpi::n_procs - 1) {
60,017✔
302
      n = simulation::work_index[mpi::rank + 1] - start;
36,813✔
303
    } else {
304
      n = std::min(bank_position[neighbor + 1],
23,204✔
305
            simulation::work_index[mpi::rank + 1]) -
46,408✔
306
          start;
307
    }
308

309
    if (neighbor != mpi::rank) {
60,017✔
310
      // If the source sites are not on this processor, initiate an
311
      // asynchronous receive for the source sites
312

313
      requests.emplace_back();
15,324✔
314
      MPI_Irecv(&simulation::source_bank[index_local], static_cast<int>(n),
15,324✔
315
        mpi::source_site, neighbor, neighbor, mpi::intracomm, &requests.back());
15,324✔
316

317
      if (settings::ifp_on) {
15,324✔
318
        // Receive IFP data
319
        receive_ifp_data(index_local, n, ifp_n_generation, neighbor, requests,
100✔
320
          recv_delayed_groups, recv_lifetimes, deserialization_info);
321
      }
322

323
    } else {
324
      // If the source sites are on this processor, we can simply copy them
325
      // from the temp_sites bank
326

327
      index_temp = start - bank_position[mpi::rank];
44,693✔
328
      std::copy(&temp_sites[index_temp], &temp_sites[index_temp + n],
44,693✔
329
        &simulation::source_bank[index_local]);
44,693✔
330

331
      if (settings::ifp_on) {
44,693✔
332
        copy_partial_ifp_data_to_source_banks(
200✔
333
          index_temp, n, index_local, temp_delayed_groups, temp_lifetimes);
334
      }
335
    }
336

337
    // Increment all indices
338
    start += n;
60,017✔
339
    index_local += n;
60,017✔
340
    ++neighbor;
60,017✔
341
  }
342

343
  // Since we initiated a series of asynchronous ISENDs and IRECVs, now we have
344
  // to ensure that the data has actually been communicated before moving on to
345
  // the next generation
346

347
  int n_request = requests.size();
44,693✔
348
  MPI_Waitall(n_request, requests.data(), MPI_STATUSES_IGNORE);
44,693✔
349

350
  if (settings::ifp_on) {
44,693✔
351
    deserialize_ifp_info(ifp_n_generation, deserialization_info,
200✔
352
      recv_delayed_groups, recv_lifetimes);
353
  }
354

355
#else
356
  std::copy(temp_sites.data(), temp_sites.data() + settings::n_particles,
32,839✔
357
    simulation::source_bank.begin());
358
  if (settings::ifp_on) {
32,839✔
359
    copy_complete_ifp_data_to_source_banks(temp_delayed_groups, temp_lifetimes);
120✔
360
  }
361
#endif
362

363
  simulation::time_bank_sendrecv.stop();
77,532✔
364
  simulation::time_bank.stop();
77,532✔
365
}
122,225✔
366

367
void calculate_average_keff()
80,572✔
368
{
369
  // Determine overall generation and number of active generations
370
  int i = overall_generation() - 1;
80,572✔
371
  int n;
372
  if (simulation::current_batch > settings::n_inactive) {
80,572✔
373
    n = settings::gen_per_batch * simulation::n_realizations +
62,620✔
374
        simulation::current_gen;
375
  } else {
376
    n = 0;
17,952✔
377
  }
378

379
  if (n <= 0) {
80,572✔
380
    // For inactive generations, use current generation k as estimate for next
381
    // generation
382
    simulation::keff = simulation::k_generation[i];
17,952✔
383
  } else {
384
    // Sample mean of keff
385
    simulation::k_sum[0] += simulation::k_generation[i];
62,620✔
386
    simulation::k_sum[1] += std::pow(simulation::k_generation[i], 2);
62,620✔
387

388
    // Determine mean
389
    simulation::keff = simulation::k_sum[0] / n;
62,620✔
390

391
    if (n > 1) {
62,620✔
392
      double t_value;
393
      if (settings::confidence_intervals) {
58,849✔
394
        // Calculate t-value for confidence intervals
395
        double alpha = 1.0 - CONFIDENCE_LEVEL;
112✔
396
        t_value = t_percentile(1.0 - alpha / 2.0, n - 1);
112✔
397
      } else {
398
        t_value = 1.0;
58,737✔
399
      }
400

401
      // Standard deviation of the sample mean of k
402
      simulation::keff_std =
58,849✔
403
        t_value *
58,849✔
404
        std::sqrt(
58,849✔
405
          (simulation::k_sum[1] / n - std::pow(simulation::keff, 2)) / (n - 1));
58,849✔
406
    }
407
  }
408
}
80,572✔
409

410
int openmc_get_keff(double* k_combined)
12,831✔
411
{
412
  k_combined[0] = 0.0;
12,831✔
413
  k_combined[1] = 0.0;
12,831✔
414

415
  // Special case for n <=3. Notice that at the end,
416
  // there is a N-3 term in a denominator.
417
  if (simulation::n_realizations <= 3 ||
12,831✔
418
      settings::solver_type == SolverType::RANDOM_RAY) {
9,675✔
419
    k_combined[0] = simulation::keff;
3,288✔
420
    k_combined[1] = simulation::keff_std;
3,288✔
421
    if (simulation::n_realizations <= 1) {
3,288✔
422
      k_combined[1] = std::numeric_limits<double>::infinity();
2,322✔
423
    }
424
    return 0;
3,288✔
425
  }
426

427
  // Initialize variables
428
  int64_t n = simulation::n_realizations;
9,543✔
429

430
  // Copy estimates of k-effective and its variance (not variance of the mean)
431
  const auto& gt = simulation::global_tallies;
9,543✔
432

433
  array<double, 3> kv {};
9,543✔
434
  xt::xtensor<double, 2> cov = xt::zeros<double>({3, 3});
9,543✔
435
  kv[0] = gt(GlobalTally::K_COLLISION, TallyResult::SUM) / n;
9,543✔
436
  kv[1] = gt(GlobalTally::K_ABSORPTION, TallyResult::SUM) / n;
9,543✔
437
  kv[2] = gt(GlobalTally::K_TRACKLENGTH, TallyResult::SUM) / n;
9,543✔
438
  cov(0, 0) =
19,086✔
439
    (gt(GlobalTally::K_COLLISION, TallyResult::SUM_SQ) - n * kv[0] * kv[0]) /
9,543✔
440
    (n - 1);
9,543✔
441
  cov(1, 1) =
19,086✔
442
    (gt(GlobalTally::K_ABSORPTION, TallyResult::SUM_SQ) - n * kv[1] * kv[1]) /
9,543✔
443
    (n - 1);
9,543✔
444
  cov(2, 2) =
19,086✔
445
    (gt(GlobalTally::K_TRACKLENGTH, TallyResult::SUM_SQ) - n * kv[2] * kv[2]) /
9,543✔
446
    (n - 1);
9,543✔
447

448
  // Calculate covariances based on sums with Bessel's correction
449
  cov(0, 1) = (simulation::k_col_abs - n * kv[0] * kv[1]) / (n - 1);
9,543✔
450
  cov(0, 2) = (simulation::k_col_tra - n * kv[0] * kv[2]) / (n - 1);
9,543✔
451
  cov(1, 2) = (simulation::k_abs_tra - n * kv[1] * kv[2]) / (n - 1);
9,543✔
452
  cov(1, 0) = cov(0, 1);
9,543✔
453
  cov(2, 0) = cov(0, 2);
9,543✔
454
  cov(2, 1) = cov(1, 2);
9,543✔
455

456
  // Check to see if two estimators are the same; this is guaranteed to happen
457
  // in MG-mode with survival biasing when the collision and absorption
458
  // estimators are the same, but can theoretically happen at anytime.
459
  // If it does, the standard estimators will produce floating-point
460
  // exceptions and an expression specifically derived for the combination of
461
  // two estimators (vice three) should be used instead.
462

463
  // First we will identify if there are any matching estimators
464
  int i, j;
465
  bool use_three = false;
9,543✔
466
  if ((std::abs(kv[0] - kv[1]) / kv[0] < FP_REL_PRECISION) &&
9,565✔
467
      (std::abs(cov(0, 0) - cov(1, 1)) / cov(0, 0) < FP_REL_PRECISION)) {
22✔
468
    // 0 and 1 match, so only use 0 and 2 in our comparisons
469
    i = 0;
22✔
470
    j = 2;
22✔
471

472
  } else if ((std::abs(kv[0] - kv[2]) / kv[0] < FP_REL_PRECISION) &&
9,521✔
UNCOV
473
             (std::abs(cov(0, 0) - cov(2, 2)) / cov(0, 0) < FP_REL_PRECISION)) {
×
474
    // 0 and 2 match, so only use 0 and 1 in our comparisons
475
    i = 0;
×
476
    j = 1;
×
477

478
  } else if ((std::abs(kv[1] - kv[2]) / kv[1] < FP_REL_PRECISION) &&
9,521✔
479
             (std::abs(cov(1, 1) - cov(2, 2)) / cov(1, 1) < FP_REL_PRECISION)) {
×
480
    // 1 and 2 match, so only use 0 and 1 in our comparisons
481
    i = 0;
×
482
    j = 1;
×
483

484
  } else {
485
    // No two estimators match, so set boolean to use all three estimators.
486
    use_three = true;
9,521✔
487
  }
488

489
  if (use_three) {
9,543✔
490
    // Use three estimators as derived in the paper by Urbatsch
491

492
    // Initialize variables
493
    double g = 0.0;
9,521✔
494
    array<double, 3> S {};
9,521✔
495

496
    for (int l = 0; l < 3; ++l) {
38,084✔
497
      // Permutations of estimates
498
      int k;
499
      switch (l) {
28,563✔
500
      case 0:
9,521✔
501
        // i = collision, j = absorption, k = tracklength
502
        i = 0;
9,521✔
503
        j = 1;
9,521✔
504
        k = 2;
9,521✔
505
        break;
9,521✔
506
      case 1:
9,521✔
507
        // i = absortion, j = tracklength, k = collision
508
        i = 1;
9,521✔
509
        j = 2;
9,521✔
510
        k = 0;
9,521✔
511
        break;
9,521✔
512
      case 2:
9,521✔
513
        // i = tracklength, j = collision, k = absorption
514
        i = 2;
9,521✔
515
        j = 0;
9,521✔
516
        k = 1;
9,521✔
517
        break;
9,521✔
518
      }
519

520
      // Calculate weighting
521
      double f = cov(j, j) * (cov(k, k) - cov(i, k)) - cov(k, k) * cov(i, j) +
28,563✔
522
                 cov(j, k) * (cov(i, j) + cov(i, k) - cov(j, k));
28,563✔
523

524
      // Add to S sums for variance of combined estimate
525
      S[0] += f * cov(0, l);
28,563✔
526
      S[1] += (cov(j, j) + cov(k, k) - 2.0 * cov(j, k)) * kv[l] * kv[l];
28,563✔
527
      S[2] += (cov(k, k) + cov(i, j) - cov(j, k) - cov(i, k)) * kv[l] * kv[j];
28,563✔
528

529
      // Add to sum for combined k-effective
530
      k_combined[0] += f * kv[l];
28,563✔
531
      g += f;
28,563✔
532
    }
533

534
    // Complete calculations of S sums
535
    for (auto& S_i : S) {
38,084✔
536
      S_i *= (n - 1);
28,563✔
537
    }
538
    S[0] *= (n - 1) * (n - 1);
9,521✔
539

540
    // Calculate combined estimate of k-effective
541
    k_combined[0] /= g;
9,521✔
542

543
    // Calculate standard deviation of combined estimate
544
    g *= (n - 1) * (n - 1);
9,521✔
545
    k_combined[1] =
9,521✔
546
      std::sqrt(S[0] / (g * n * (n - 3)) * (1 + n * ((S[1] - 2 * S[2]) / g)));
9,521✔
547

548
  } else {
549
    // Use only two estimators
550
    // These equations are derived analogously to that done in the paper by
551
    // Urbatsch, but are simpler than for the three estimators case since the
552
    // block matrices of the three estimator equations reduces to scalars here
553

554
    // Store the commonly used term
555
    double f = kv[i] - kv[j];
22✔
556
    double g = cov(i, i) + cov(j, j) - 2.0 * cov(i, j);
22✔
557

558
    // Calculate combined estimate of k-effective
559
    k_combined[0] = kv[i] - (cov(i, i) - cov(i, j)) / g * f;
22✔
560

561
    // Calculate standard deviation of combined estimate
562
    k_combined[1] = (cov(i, i) * cov(j, j) - cov(i, j) * cov(i, j)) *
22✔
563
                    (g + n * f * f) / (n * (n - 2) * g * g);
22✔
564
    k_combined[1] = std::sqrt(k_combined[1]);
22✔
565
  }
566
  return 0;
9,543✔
567
}
9,543✔
568

569
void shannon_entropy()
8,580✔
570
{
571
  // Get source weight in each mesh bin
572
  bool sites_outside;
573
  xt::xtensor<double, 1> p =
574
    simulation::entropy_mesh->count_sites(simulation::fission_bank.data(),
8,580✔
575
      simulation::fission_bank.size(), &sites_outside);
17,160✔
576

577
  // display warning message if there were sites outside entropy box
578
  if (sites_outside) {
8,580✔
579
    if (mpi::master)
×
580
      warning("Fission source site(s) outside of entropy box.");
×
581
  }
582

583
  if (mpi::master) {
8,580✔
584
    // Normalize to total weight of bank sites
585
    p /= xt::sum(p);
8,530✔
586

587
    // Sum values to obtain Shannon entropy
588
    double H = 0.0;
8,530✔
589
    for (auto p_i : p) {
649,850✔
590
      if (p_i > 0.0) {
641,320✔
591
        H -= p_i * std::log2(p_i);
508,735✔
592
      }
593
    }
594

595
    // Add value to vector
596
    simulation::entropy.push_back(H);
8,530✔
597
  }
598
}
8,580✔
599

600
void ufs_count_sites()
160✔
601
{
602
  if (simulation::current_batch == 1 && simulation::current_gen == 1) {
160✔
603
    // On the first generation, just assume that the source is already evenly
604
    // distributed so that effectively the production of fission sites is not
605
    // biased
606

607
    std::size_t n = simulation::ufs_mesh->n_bins();
16✔
608
    double vol_frac = simulation::ufs_mesh->volume_frac_;
16✔
609
    simulation::source_frac = xt::xtensor<double, 1>({n}, vol_frac);
16✔
610

611
  } else {
16✔
612
    // count number of source sites in each ufs mesh cell
613
    bool sites_outside;
614
    simulation::source_frac =
615
      simulation::ufs_mesh->count_sites(simulation::source_bank.data(),
288✔
616
        simulation::source_bank.size(), &sites_outside);
288✔
617

618
    // Check for sites outside of the mesh
619
    if (mpi::master && sites_outside) {
144✔
620
      fatal_error("Source sites outside of the UFS mesh!");
×
621
    }
622

623
#ifdef OPENMC_MPI
624
    // Send source fraction to all processors
625
    int n_bins = simulation::ufs_mesh->n_bins();
90✔
626
    MPI_Bcast(
90✔
627
      simulation::source_frac.data(), n_bins, MPI_DOUBLE, 0, mpi::intracomm);
90✔
628
#endif
629

630
    // Normalize to total weight to get fraction of source in each cell
631
    double total = xt::sum(simulation::source_frac)();
144✔
632
    simulation::source_frac /= total;
144✔
633

634
    // Since the total starting weight is not equal to n_particles, we need to
635
    // renormalize the weight of the source sites
636
    for (int i = 0; i < simulation::work_per_rank; ++i) {
99,144✔
637
      simulation::source_bank[i].wgt *= settings::n_particles / total;
99,000✔
638
    }
639
  }
640
}
160✔
641

642
double ufs_get_weight(const Particle& p)
94,182✔
643
{
644
  // Determine indices on ufs mesh for current location
645
  int mesh_bin = simulation::ufs_mesh->get_bin(p.r());
94,182✔
646
  if (mesh_bin < 0) {
94,182✔
647
    p.write_restart();
×
648
    fatal_error("Source site outside UFS mesh!");
×
649
  }
650

651
  if (simulation::source_frac(mesh_bin) != 0.0) {
94,182✔
652
    return simulation::ufs_mesh->volume_frac_ /
51,095✔
653
           simulation::source_frac(mesh_bin);
51,095✔
654
  } else {
655
    return 1.0;
43,087✔
656
  }
657
}
658

659
void write_eigenvalue_hdf5(hid_t group)
3,826✔
660
{
661
  write_dataset(group, "n_inactive", settings::n_inactive);
3,826✔
662
  write_dataset(group, "generations_per_batch", settings::gen_per_batch);
3,826✔
663
  write_dataset(group, "k_generation", simulation::k_generation);
3,826✔
664
  if (settings::entropy_on) {
3,826✔
665
    write_dataset(group, "entropy", simulation::entropy);
437✔
666
  }
667
  write_dataset(group, "k_col_abs", simulation::k_col_abs);
3,826✔
668
  write_dataset(group, "k_col_tra", simulation::k_col_tra);
3,826✔
669
  write_dataset(group, "k_abs_tra", simulation::k_abs_tra);
3,826✔
670
  array<double, 2> k_combined;
671
  openmc_get_keff(k_combined.data());
3,826✔
672
  write_dataset(group, "k_combined", k_combined);
3,826✔
673
}
3,826✔
674

675
void read_eigenvalue_hdf5(hid_t group)
68✔
676
{
677
  read_dataset(group, "generations_per_batch", settings::gen_per_batch);
68✔
678
  int n = simulation::restart_batch * settings::gen_per_batch;
68✔
679
  simulation::k_generation.resize(n);
68✔
680
  read_dataset(group, "k_generation", simulation::k_generation);
68✔
681
  if (settings::entropy_on) {
68✔
682
    read_dataset(group, "entropy", simulation::entropy);
12✔
683
  }
684
  read_dataset(group, "k_col_abs", simulation::k_col_abs);
68✔
685
  read_dataset(group, "k_col_tra", simulation::k_col_tra);
68✔
686
  read_dataset(group, "k_abs_tra", simulation::k_abs_tra);
68✔
687
}
68✔
688

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

© 2025 Coveralls, Inc