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

openmc-dev / openmc / 19918574733

04 Dec 2025 05:19AM UTC coverage: 82.123% (+0.07%) from 82.055%
19918574733

Pull #3667

github

web-flow
Merge 3127c6bd7 into ad5a876be
Pull Request #3667: Allow DistribcellFilter to work with apply_tally_results=True

16952 of 23523 branches covered (72.07%)

Branch coverage included in aggregate %.

29 of 30 new or added lines in 7 files covered. (96.67%)

139 existing lines in 6 files now uncovered.

54963 of 64047 relevant lines covered (85.82%)

43580650.96 hits per line

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

90.22
/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()
84,783✔
51
{
52
  const auto& gt = simulation::global_tallies;
84,783✔
53

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

59
  double keff_reduced;
60
#ifdef OPENMC_MPI
61
  if (settings::solver_type != SolverType::RANDOM_RAY) {
47,785✔
62
    // Combine values across all processors
63
    MPI_Allreduce(&simulation::keff_generation, &keff_reduced, 1, MPI_DOUBLE,
45,485✔
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;
2,300✔
72
  }
73
#else
74
  keff_reduced = simulation::keff_generation;
36,998✔
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) {
84,783✔
80
    keff_reduced /= settings::n_particles;
81,103✔
81
  }
82

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

86
void synchronize_bank()
81,103✔
87
{
88
  simulation::time_bank.start();
81,103✔
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;
45,485✔
101
  int64_t n_bank = simulation::fission_bank.size();
45,485✔
102
  MPI_Exscan(&n_bank, &start, 1, MPI_INT64_T, MPI_SUM, mpi::intracomm);
45,485✔
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)
45,485✔
108
    start = 0;
29,650✔
109

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

114
#else
115
  int64_t start = 0;
35,618✔
116
  int64_t finish = simulation::fission_bank.size();
35,618✔
117
  int64_t total = finish;
35,618✔
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) {
81,103!
126
    fatal_error(
×
127
      "No fission sites banked on MPI rank " + std::to_string(mpi::rank));
×
128
  }
129

130
  simulation::time_bank_sample.start();
81,103✔
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;
81,103✔
135

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

138
  // Temporary banks for IFP
139
  vector<vector<int>> temp_delayed_groups;
81,103✔
140
  vector<vector<double>> temp_lifetimes;
81,103✔
141
  if (settings::ifp_on) {
81,103✔
142
    resize_ifp_data(
1,520✔
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();
81,103✔
154
  uint64_t seed = init_seed(id, STREAM_TRACKING);
81,103✔
155

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

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

165
  // Locally comb particles in fission_bank
166
  double tooth = tooth_start * teeth_distance + teeth_offset;
81,103✔
167
  for (int64_t i = tooth_start; i < tooth_end; i++) {
139,961,803✔
168
    int64_t idx = std::floor(tooth) - start;
139,880,700✔
169
    temp_sites[index_temp] = simulation::fission_bank[idx];
139,880,700✔
170
    if (settings::ifp_on) {
139,880,700✔
171
      copy_ifp_data_from_fission_banks(
1,320,000✔
172
        idx, temp_delayed_groups[index_temp], temp_lifetimes[index_temp]);
1,320,000✔
173
    }
174
    ++index_temp;
139,880,700✔
175

176
    // Next tooth
177
    tooth += teeth_distance;
139,880,700✔
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;
45,485✔
189
  MPI_Exscan(&index_temp, &start, 1, MPI_INT64_T, MPI_SUM, mpi::intracomm);
45,485✔
190
  finish = start + index_temp;
45,485✔
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];
45,485✔
196
  MPI_Allgather(
45,485✔
197
    &start, 1, MPI_INT64_T, bank_position, 1, MPI_INT64_T, mpi::intracomm);
198
#else
199
  start = 0;
35,618✔
200
  finish = index_temp;
35,618✔
201
#endif
202

203
  simulation::time_bank_sample.stop();
81,103✔
204
  simulation::time_bank_sendrecv.start();
81,103✔
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) {
45,485✔
213
    broadcast_ifp_n_generation(
800✔
214
      ifp_n_generation, temp_delayed_groups, temp_lifetimes);
215
  }
216

217
  int64_t index_local = 0;
45,485✔
218
  vector<MPI_Request> requests;
45,485✔
219

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

224
  if (start < settings::n_particles) {
45,485!
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(
45,485✔
228
      simulation::work_index.begin(), simulation::work_index.end(), start);
45,485✔
229

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

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

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

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

257
      // Increment all indices
258
      start += n;
61,084✔
259
      index_local += n;
61,084✔
260
      ++neighbor;
61,084✔
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)
61,084✔
266
        break;
37,454✔
267
    }
268
  }
269

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

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

276
  // IFP receive buffers
277
  vector<int> recv_delayed_groups;
45,485✔
278
  vector<double> recv_lifetimes;
45,485✔
279
  vector<DeserializationInfo> deserialization_info;
45,485✔
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]) {
45,485✔
286
    neighbor = mpi::n_procs - 1;
21,846✔
287
  } else {
288
    neighbor =
23,639✔
289
      upper_bound_index(bank_position, bank_position + mpi::n_procs, start);
23,639✔
290
  }
291

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

298
  while (start < simulation::work_index[mpi::rank + 1]) {
106,569✔
299
    // Determine how many sites need to be received
300
    int64_t n;
301
    if (neighbor == mpi::n_procs - 1) {
61,084✔
302
      n = simulation::work_index[mpi::rank + 1] - start;
37,445✔
303
    } else {
304
      n = std::min(bank_position[neighbor + 1],
23,639✔
305
            simulation::work_index[mpi::rank + 1]) -
47,278✔
306
          start;
307
    }
308

309
    if (neighbor != mpi::rank) {
61,084✔
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,599✔
314
      MPI_Irecv(&simulation::source_bank[index_local], static_cast<int>(n),
15,599✔
315
        mpi::source_site, neighbor, neighbor, mpi::intracomm, &requests.back());
15,599✔
316

317
      if (settings::ifp_on) {
15,599✔
318
        // Receive IFP data
319
        receive_ifp_data(index_local, n, ifp_n_generation, neighbor, requests,
200✔
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];
45,485✔
328
      std::copy(&temp_sites[index_temp], &temp_sites[index_temp + n],
45,485✔
329
        &simulation::source_bank[index_local]);
45,485✔
330

331
      if (settings::ifp_on) {
45,485✔
332
        copy_partial_ifp_data_to_source_banks(
800✔
333
          index_temp, n, index_local, temp_delayed_groups, temp_lifetimes);
334
      }
335
    }
336

337
    // Increment all indices
338
    start += n;
61,084✔
339
    index_local += n;
61,084✔
340
    ++neighbor;
61,084✔
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();
45,485✔
348
  MPI_Waitall(n_request, requests.data(), MPI_STATUSES_IGNORE);
45,485✔
349

350
  if (settings::ifp_on) {
45,485✔
351
    deserialize_ifp_info(ifp_n_generation, deserialization_info,
800✔
352
      recv_delayed_groups, recv_lifetimes);
353
  }
354

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

363
  simulation::time_bank_sendrecv.stop();
81,103✔
364
  simulation::time_bank.stop();
81,103✔
365
}
126,588✔
366

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

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

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

391
    if (n > 1) {
66,853✔
392
      double t_value;
393
      if (settings::confidence_intervals) {
62,806✔
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;
62,694✔
399
      }
400

401
      // Standard deviation of the sample mean of k
402
      simulation::keff_std =
62,806✔
403
        t_value *
62,806✔
404
        std::sqrt(
62,806✔
405
          (simulation::k_sum[1] / n - std::pow(simulation::keff, 2)) / (n - 1));
62,806✔
406

407
      // In some cases (such as an infinite medium problem), random ray
408
      // may estimate k exactly and in an unvarying manner between iterations.
409
      // In this case, the floating point roundoff between the division and the
410
      // power operations may cause an extremely small negative value to occur
411
      // inside the sqrt operation, leading to NaN. If this occurs, we check for
412
      // it and set the std dev to zero.
413
      if (!std::isfinite(simulation::keff_std)) {
62,806!
UNCOV
414
        simulation::keff_std = 0.0;
×
415
      }
416
    }
417
  }
418
}
84,783✔
419

420
int openmc_get_keff(double* k_combined)
12,573✔
421
{
422
  k_combined[0] = 0.0;
12,573✔
423
  k_combined[1] = 0.0;
12,573✔
424

425
  // Special case for n <=3. Notice that at the end,
426
  // there is a N-3 term in a denominator.
427
  if (simulation::n_realizations <= 3 ||
12,573✔
428
      settings::solver_type == SolverType::RANDOM_RAY) {
9,713✔
429
    k_combined[0] = simulation::keff;
3,036✔
430
    k_combined[1] = simulation::keff_std;
3,036✔
431
    if (simulation::n_realizations <= 1) {
3,036✔
432
      k_combined[1] = std::numeric_limits<double>::infinity();
2,090✔
433
    }
434
    return 0;
3,036✔
435
  }
436

437
  // Initialize variables
438
  int64_t n = simulation::n_realizations;
9,537✔
439

440
  // Copy estimates of k-effective and its variance (not variance of the mean)
441
  const auto& gt = simulation::global_tallies;
9,537✔
442

443
  array<double, 3> kv {};
9,537✔
444
  xt::xtensor<double, 2> cov = xt::zeros<double>({3, 3});
9,537✔
445
  kv[0] = gt(GlobalTally::K_COLLISION, TallyResult::SUM) / n;
9,537✔
446
  kv[1] = gt(GlobalTally::K_ABSORPTION, TallyResult::SUM) / n;
9,537✔
447
  kv[2] = gt(GlobalTally::K_TRACKLENGTH, TallyResult::SUM) / n;
9,537✔
448
  cov(0, 0) =
19,074✔
449
    (gt(GlobalTally::K_COLLISION, TallyResult::SUM_SQ) - n * kv[0] * kv[0]) /
9,537✔
450
    (n - 1);
9,537✔
451
  cov(1, 1) =
19,074✔
452
    (gt(GlobalTally::K_ABSORPTION, TallyResult::SUM_SQ) - n * kv[1] * kv[1]) /
9,537✔
453
    (n - 1);
9,537✔
454
  cov(2, 2) =
19,074✔
455
    (gt(GlobalTally::K_TRACKLENGTH, TallyResult::SUM_SQ) - n * kv[2] * kv[2]) /
9,537✔
456
    (n - 1);
9,537✔
457

458
  // Calculate covariances based on sums with Bessel's correction
459
  cov(0, 1) = (simulation::k_col_abs - n * kv[0] * kv[1]) / (n - 1);
9,537✔
460
  cov(0, 2) = (simulation::k_col_tra - n * kv[0] * kv[2]) / (n - 1);
9,537✔
461
  cov(1, 2) = (simulation::k_abs_tra - n * kv[1] * kv[2]) / (n - 1);
9,537✔
462
  cov(1, 0) = cov(0, 1);
9,537✔
463
  cov(2, 0) = cov(0, 2);
9,537✔
464
  cov(2, 1) = cov(1, 2);
9,537✔
465

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

473
  // First we will identify if there are any matching estimators
474
  int i, j;
475
  bool use_three = false;
9,537✔
476
  if ((std::abs(kv[0] - kv[1]) / kv[0] < FP_REL_PRECISION) &&
9,559✔
477
      (std::abs(cov(0, 0) - cov(1, 1)) / cov(0, 0) < FP_REL_PRECISION)) {
22!
478
    // 0 and 1 match, so only use 0 and 2 in our comparisons
479
    i = 0;
22✔
480
    j = 2;
22✔
481

482
  } else if ((std::abs(kv[0] - kv[2]) / kv[0] < FP_REL_PRECISION) &&
9,515!
UNCOV
483
             (std::abs(cov(0, 0) - cov(2, 2)) / cov(0, 0) < FP_REL_PRECISION)) {
×
484
    // 0 and 2 match, so only use 0 and 1 in our comparisons
UNCOV
485
    i = 0;
×
UNCOV
486
    j = 1;
×
487

488
  } else if ((std::abs(kv[1] - kv[2]) / kv[1] < FP_REL_PRECISION) &&
9,515!
UNCOV
489
             (std::abs(cov(1, 1) - cov(2, 2)) / cov(1, 1) < FP_REL_PRECISION)) {
×
490
    // 1 and 2 match, so only use 0 and 1 in our comparisons
UNCOV
491
    i = 0;
×
UNCOV
492
    j = 1;
×
493

494
  } else {
495
    // No two estimators match, so set boolean to use all three estimators.
496
    use_three = true;
9,515✔
497
  }
498

499
  if (use_three) {
9,537✔
500
    // Use three estimators as derived in the paper by Urbatsch
501

502
    // Initialize variables
503
    double g = 0.0;
9,515✔
504
    array<double, 3> S {};
9,515✔
505

506
    for (int l = 0; l < 3; ++l) {
38,060✔
507
      // Permutations of estimates
508
      int k;
509
      switch (l) {
28,545!
510
      case 0:
9,515✔
511
        // i = collision, j = absorption, k = tracklength
512
        i = 0;
9,515✔
513
        j = 1;
9,515✔
514
        k = 2;
9,515✔
515
        break;
9,515✔
516
      case 1:
9,515✔
517
        // i = absortion, j = tracklength, k = collision
518
        i = 1;
9,515✔
519
        j = 2;
9,515✔
520
        k = 0;
9,515✔
521
        break;
9,515✔
522
      case 2:
9,515✔
523
        // i = tracklength, j = collision, k = absorption
524
        i = 2;
9,515✔
525
        j = 0;
9,515✔
526
        k = 1;
9,515✔
527
        break;
9,515✔
528
      }
529

530
      // Calculate weighting
531
      double f = cov(j, j) * (cov(k, k) - cov(i, k)) - cov(k, k) * cov(i, j) +
28,545✔
532
                 cov(j, k) * (cov(i, j) + cov(i, k) - cov(j, k));
28,545✔
533

534
      // Add to S sums for variance of combined estimate
535
      S[0] += f * cov(0, l);
28,545✔
536
      S[1] += (cov(j, j) + cov(k, k) - 2.0 * cov(j, k)) * kv[l] * kv[l];
28,545✔
537
      S[2] += (cov(k, k) + cov(i, j) - cov(j, k) - cov(i, k)) * kv[l] * kv[j];
28,545✔
538

539
      // Add to sum for combined k-effective
540
      k_combined[0] += f * kv[l];
28,545✔
541
      g += f;
28,545✔
542
    }
543

544
    // Complete calculations of S sums
545
    for (auto& S_i : S) {
38,060✔
546
      S_i *= (n - 1);
28,545✔
547
    }
548
    S[0] *= (n - 1) * (n - 1);
9,515✔
549

550
    // Calculate combined estimate of k-effective
551
    k_combined[0] /= g;
9,515✔
552

553
    // Calculate standard deviation of combined estimate
554
    g *= (n - 1) * (n - 1);
9,515✔
555
    k_combined[1] =
9,515✔
556
      std::sqrt(S[0] / (g * n * (n - 3)) * (1 + n * ((S[1] - 2 * S[2]) / g)));
9,515✔
557

558
  } else {
559
    // Use only two estimators
560
    // These equations are derived analogously to that done in the paper by
561
    // Urbatsch, but are simpler than for the three estimators case since the
562
    // block matrices of the three estimator equations reduces to scalars here
563

564
    // Store the commonly used term
565
    double f = kv[i] - kv[j];
22✔
566
    double g = cov(i, i) + cov(j, j) - 2.0 * cov(i, j);
22✔
567

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

571
    // Calculate standard deviation of combined estimate
572
    k_combined[1] = (cov(i, i) * cov(j, j) - cov(i, j) * cov(i, j)) *
22✔
573
                    (g + n * f * f) / (n * (n - 2) * g * g);
22✔
574
    k_combined[1] = std::sqrt(k_combined[1]);
22✔
575
  }
576
  return 0;
9,537✔
577
}
9,537✔
578

579
void shannon_entropy()
7,695✔
580
{
581
  // Get source weight in each mesh bin
582
  bool sites_outside;
583
  xt::xtensor<double, 1> p =
584
    simulation::entropy_mesh->count_sites(simulation::fission_bank.data(),
7,695✔
585
      simulation::fission_bank.size(), &sites_outside);
15,390✔
586

587
  // display warning message if there were sites outside entropy box
588
  if (sites_outside) {
7,695!
UNCOV
589
    if (mpi::master)
×
UNCOV
590
      warning("Fission source site(s) outside of entropy box.");
×
591
  }
592

593
  if (mpi::master) {
7,695✔
594
    // Normalize to total weight of bank sites
595
    p /= xt::sum(p);
7,645✔
596

597
    // Sum values to obtain Shannon entropy
598
    double H = 0.0;
7,645✔
599
    for (auto p_i : p) {
586,355✔
600
      if (p_i > 0.0) {
578,710✔
601
        H -= p_i * std::log2(p_i);
453,695✔
602
      }
603
    }
604

605
    // Add value to vector
606
    simulation::entropy.push_back(H);
7,645✔
607
  }
608
}
7,695✔
609

610
void ufs_count_sites()
160✔
611
{
612
  if (simulation::current_batch == 1 && simulation::current_gen == 1) {
160!
613
    // On the first generation, just assume that the source is already evenly
614
    // distributed so that effectively the production of fission sites is not
615
    // biased
616

617
    std::size_t n = simulation::ufs_mesh->n_bins();
16✔
618
    double vol_frac = simulation::ufs_mesh->volume_frac_;
16✔
619
    simulation::source_frac = xt::xtensor<double, 1>({n}, vol_frac);
16✔
620

621
  } else {
16✔
622
    // count number of source sites in each ufs mesh cell
623
    bool sites_outside;
624
    simulation::source_frac =
625
      simulation::ufs_mesh->count_sites(simulation::source_bank.data(),
288✔
626
        simulation::source_bank.size(), &sites_outside);
288✔
627

628
    // Check for sites outside of the mesh
629
    if (mpi::master && sites_outside) {
144!
UNCOV
630
      fatal_error("Source sites outside of the UFS mesh!");
×
631
    }
632

633
#ifdef OPENMC_MPI
634
    // Send source fraction to all processors
635
    int n_bins = simulation::ufs_mesh->n_bins();
90✔
636
    MPI_Bcast(
90✔
637
      simulation::source_frac.data(), n_bins, MPI_DOUBLE, 0, mpi::intracomm);
90✔
638
#endif
639

640
    // Normalize to total weight to get fraction of source in each cell
641
    double total = xt::sum(simulation::source_frac)();
144✔
642
    simulation::source_frac /= total;
144✔
643

644
    // Since the total starting weight is not equal to n_particles, we need to
645
    // renormalize the weight of the source sites
646
    for (int i = 0; i < simulation::work_per_rank; ++i) {
99,144✔
647
      simulation::source_bank[i].wgt *= settings::n_particles / total;
99,000✔
648
    }
649
  }
650
}
160✔
651

652
double ufs_get_weight(const Particle& p)
94,182✔
653
{
654
  // Determine indices on ufs mesh for current location
655
  int mesh_bin = simulation::ufs_mesh->get_bin(p.r());
94,182✔
656
  if (mesh_bin < 0) {
94,182!
UNCOV
657
    p.write_restart();
×
UNCOV
658
    fatal_error("Source site outside UFS mesh!");
×
659
  }
660

661
  if (simulation::source_frac(mesh_bin) != 0.0) {
94,182✔
662
    return simulation::ufs_mesh->volume_frac_ /
51,095✔
663
           simulation::source_frac(mesh_bin);
51,095✔
664
  } else {
665
    return 1.0;
43,087✔
666
  }
667
}
668

669
void write_eigenvalue_hdf5(hid_t group)
4,004✔
670
{
671
  write_dataset(group, "n_inactive", settings::n_inactive);
4,004✔
672
  write_dataset(group, "generations_per_batch", settings::gen_per_batch);
4,004✔
673
  write_dataset(group, "k_generation", simulation::k_generation);
4,004✔
674
  if (settings::entropy_on) {
4,004✔
675
    write_dataset(group, "entropy", simulation::entropy);
451✔
676
  }
677
  write_dataset(group, "k_col_abs", simulation::k_col_abs);
4,004✔
678
  write_dataset(group, "k_col_tra", simulation::k_col_tra);
4,004✔
679
  write_dataset(group, "k_abs_tra", simulation::k_abs_tra);
4,004✔
680
  array<double, 2> k_combined;
681
  openmc_get_keff(k_combined.data());
4,004✔
682
  write_dataset(group, "k_combined", k_combined);
4,004✔
683
}
4,004✔
684

685
void read_eigenvalue_hdf5(hid_t group)
65✔
686
{
687
  read_dataset(group, "generations_per_batch", settings::gen_per_batch);
65✔
688
  int n = simulation::restart_batch * settings::gen_per_batch;
65✔
689
  simulation::k_generation.resize(n);
65✔
690
  read_dataset(group, "k_generation", simulation::k_generation);
65✔
691
  if (settings::entropy_on) {
65✔
692
    read_dataset(group, "entropy", simulation::entropy);
11✔
693
  }
694
  read_dataset(group, "k_col_abs", simulation::k_col_abs);
65✔
695
  read_dataset(group, "k_col_tra", simulation::k_col_tra);
65✔
696
  read_dataset(group, "k_abs_tra", simulation::k_abs_tra);
65✔
697
}
65✔
698

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