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

openmc-dev / openmc / 20278909785

16 Dec 2025 06:41PM UTC coverage: 81.834% (-0.09%) from 81.92%
20278909785

Pull #3493

github

web-flow
Merge 9dc7c7a58 into bbfa18d72
Pull Request #3493: Implement vector fitting to replace external `vectfit` package

17020 of 23572 branches covered (72.2%)

Branch coverage included in aggregate %.

188 of 207 new or added lines in 3 files covered. (90.82%)

3101 existing lines in 56 files now uncovered.

54979 of 64410 relevant lines covered (85.36%)

41388074.54 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()
75,362✔
51
{
52
  const auto& gt = simulation::global_tallies;
75,362✔
53

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

59
  double keff_reduced;
60
#ifdef OPENMC_MPI
61
  if (settings::solver_type != SolverType::RANDOM_RAY) {
38,316✔
62
    // Combine values across all processors
63
    MPI_Allreduce(&simulation::keff_generation, &keff_reduced, 1, MPI_DOUBLE,
36,476✔
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,840✔
72
  }
73
#else
74
  keff_reduced = simulation::keff_generation;
37,046✔
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) {
75,362✔
80
    keff_reduced /= settings::n_particles;
72,142✔
81
  }
82

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

86
void synchronize_bank()
72,142✔
87
{
88
  simulation::time_bank.start();
72,142✔
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;
36,476✔
101
  int64_t n_bank = simulation::fission_bank.size();
36,476✔
102
  MPI_Exscan(&n_bank, &start, 1, MPI_INT64_T, MPI_SUM, mpi::intracomm);
36,476✔
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)
36,476✔
108
    start = 0;
23,769✔
109

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

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

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

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

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

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

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

165
  // Locally comb particles in fission_bank
166
  double tooth = tooth_start * teeth_distance + teeth_offset;
72,142✔
167
  for (int64_t i = tooth_start; i < tooth_end; i++) {
127,318,642✔
168
    int64_t idx = std::floor(tooth) - start;
127,246,500✔
169
    temp_sites[index_temp] = simulation::fission_bank[idx];
127,246,500✔
170
    if (settings::ifp_on) {
127,246,500✔
171
      copy_ifp_data_from_fission_banks(
1,200,000✔
172
        idx, temp_delayed_groups[index_temp], temp_lifetimes[index_temp]);
1,200,000✔
173
    }
174
    ++index_temp;
127,246,500✔
175

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

203
  simulation::time_bank_sample.stop();
72,142✔
204
  simulation::time_bank_sendrecv.start();
72,142✔
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) {
36,476✔
213
    broadcast_ifp_n_generation(
640✔
214
      ifp_n_generation, temp_delayed_groups, temp_lifetimes);
215
  }
216

217
  int64_t index_local = 0;
36,476✔
218
  vector<MPI_Request> requests;
36,476✔
219

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

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

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

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

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

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

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

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

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

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

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

298
  while (start < simulation::work_index[mpi::rank + 1]) {
85,461✔
299
    // Determine how many sites need to be received
300
    int64_t n;
301
    if (neighbor == mpi::n_procs - 1) {
48,985✔
302
      n = simulation::work_index[mpi::rank + 1] - start;
30,016✔
303
    } else {
304
      n = std::min(bank_position[neighbor + 1],
18,969✔
305
            simulation::work_index[mpi::rank + 1]) -
37,938✔
306
          start;
307
    }
308

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

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

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

331
      if (settings::ifp_on) {
36,476✔
332
        copy_partial_ifp_data_to_source_banks(
640✔
333
          index_temp, n, index_local, temp_delayed_groups, temp_lifetimes);
334
      }
335
    }
336

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

350
  if (settings::ifp_on) {
36,476✔
351
    deserialize_ifp_info(ifp_n_generation, deserialization_info,
640✔
352
      recv_delayed_groups, recv_lifetimes);
353
  }
354

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

363
  simulation::time_bank_sendrecv.stop();
72,142✔
364
  simulation::time_bank.stop();
72,142✔
365
}
108,618✔
366

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

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

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

391
    if (n > 1) {
59,410✔
392
      double t_value;
393
      if (settings::confidence_intervals) {
55,784✔
394
        // Calculate t-value for confidence intervals
395
        double alpha = 1.0 - CONFIDENCE_LEVEL;
98✔
396
        t_value = t_percentile(1.0 - alpha / 2.0, n - 1);
98✔
397
      } else {
398
        t_value = 1.0;
55,686✔
399
      }
400

401
      // Standard deviation of the sample mean of k
402
      simulation::keff_std =
55,784✔
403
        t_value *
55,784✔
404
        std::sqrt(
55,784✔
405
          (simulation::k_sum[1] / n - std::pow(simulation::keff, 2)) / (n - 1));
55,784✔
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)) {
55,784!
UNCOV
414
        simulation::keff_std = 0.0;
×
415
      }
416
    }
417
  }
418
}
75,362✔
419

420
int openmc_get_keff(double* k_combined)
11,474✔
421
{
422
  k_combined[0] = 0.0;
11,474✔
423
  k_combined[1] = 0.0;
11,474✔
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 ||
11,474✔
428
      settings::solver_type == SolverType::RANDOM_RAY) {
8,874✔
429
    k_combined[0] = simulation::keff;
2,760✔
430
    k_combined[1] = simulation::keff_std;
2,760✔
431
    if (simulation::n_realizations <= 1) {
2,760✔
432
      k_combined[1] = std::numeric_limits<double>::infinity();
1,900✔
433
    }
434
    return 0;
2,760✔
435
  }
436

437
  // Initialize variables
438
  int64_t n = simulation::n_realizations;
8,714✔
439

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

443
  array<double, 3> kv {};
8,714✔
444
  xt::xtensor<double, 2> cov = xt::zeros<double>({3, 3});
8,714✔
445
  kv[0] = gt(GlobalTally::K_COLLISION, TallyResult::SUM) / n;
8,714✔
446
  kv[1] = gt(GlobalTally::K_ABSORPTION, TallyResult::SUM) / n;
8,714✔
447
  kv[2] = gt(GlobalTally::K_TRACKLENGTH, TallyResult::SUM) / n;
8,714✔
448
  cov(0, 0) =
17,428✔
449
    (gt(GlobalTally::K_COLLISION, TallyResult::SUM_SQ) - n * kv[0] * kv[0]) /
8,714✔
450
    (n - 1);
8,714✔
451
  cov(1, 1) =
17,428✔
452
    (gt(GlobalTally::K_ABSORPTION, TallyResult::SUM_SQ) - n * kv[1] * kv[1]) /
8,714✔
453
    (n - 1);
8,714✔
454
  cov(2, 2) =
17,428✔
455
    (gt(GlobalTally::K_TRACKLENGTH, TallyResult::SUM_SQ) - n * kv[2] * kv[2]) /
8,714✔
456
    (n - 1);
8,714✔
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);
8,714✔
460
  cov(0, 2) = (simulation::k_col_tra - n * kv[0] * kv[2]) / (n - 1);
8,714✔
461
  cov(1, 2) = (simulation::k_abs_tra - n * kv[1] * kv[2]) / (n - 1);
8,714✔
462
  cov(1, 0) = cov(0, 1);
8,714✔
463
  cov(2, 0) = cov(0, 2);
8,714✔
464
  cov(2, 1) = cov(1, 2);
8,714✔
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;
8,714✔
476
  if ((std::abs(kv[0] - kv[1]) / kv[0] < FP_REL_PRECISION) &&
8,734✔
477
      (std::abs(cov(0, 0) - cov(1, 1)) / cov(0, 0) < FP_REL_PRECISION)) {
20!
478
    // 0 and 1 match, so only use 0 and 2 in our comparisons
479
    i = 0;
20✔
480
    j = 2;
20✔
481

482
  } else if ((std::abs(kv[0] - kv[2]) / kv[0] < FP_REL_PRECISION) &&
8,694!
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) &&
8,694!
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;
8,694✔
497
  }
498

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

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

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

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

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

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

544
    // Complete calculations of S sums
545
    for (auto& S_i : S) {
34,776✔
546
      S_i *= (n - 1);
26,082✔
547
    }
548
    S[0] *= (n - 1) * (n - 1);
8,694✔
549

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

553
    // Calculate standard deviation of combined estimate
554
    g *= (n - 1) * (n - 1);
8,694✔
555
    k_combined[1] =
8,694✔
556
      std::sqrt(S[0] / (g * n * (n - 3)) * (1 + n * ((S[1] - 2 * S[2]) / g)));
8,694✔
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];
20✔
566
    double g = cov(i, i) + cov(j, j) - 2.0 * cov(i, j);
20✔
567

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

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

579
void shannon_entropy()
6,990✔
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(),
6,990✔
585
      simulation::fission_bank.size(), &sites_outside);
13,980✔
586

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

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

597
    // Sum values to obtain Shannon entropy
598
    double H = 0.0;
6,950✔
599
    for (auto p_i : p) {
533,050✔
600
      if (p_i > 0.0) {
526,100✔
601
        H -= p_i * std::log2(p_i);
412,450✔
602
      }
603
    }
604

605
    // Add value to vector
606
    simulation::entropy.push_back(H);
6,950✔
607
  }
608
}
6,990✔
609

610
void ufs_count_sites()
140✔
611
{
612
  if (simulation::current_batch == 1 && simulation::current_gen == 1) {
140!
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();
14✔
618
    double vol_frac = simulation::ufs_mesh->volume_frac_;
14✔
619
    simulation::source_frac = xt::xtensor<double, 1>({n}, vol_frac);
14✔
620

621
  } else {
14✔
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(),
252✔
626
        simulation::source_bank.size(), &sites_outside);
252✔
627

628
    // Check for sites outside of the mesh
629
    if (mpi::master && sites_outside) {
126!
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();
72✔
636
    MPI_Bcast(
72✔
637
      simulation::source_frac.data(), n_bins, MPI_DOUBLE, 0, mpi::intracomm);
72✔
638
#endif
639

640
    // Normalize to total weight to get fraction of source in each cell
641
    double total = xt::sum(simulation::source_frac)();
126✔
642
    simulation::source_frac /= total;
126✔
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) {
90,126✔
647
      simulation::source_bank[i].wgt *= settings::n_particles / total;
90,000✔
648
    }
649
  }
650
}
140✔
651

652
double ufs_get_weight(const Particle& p)
85,620✔
653
{
654
  // Determine indices on ufs mesh for current location
655
  int mesh_bin = simulation::ufs_mesh->get_bin(p.r());
85,620✔
656
  if (mesh_bin < 0) {
85,620!
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) {
85,620✔
662
    return simulation::ufs_mesh->volume_frac_ /
46,450✔
663
           simulation::source_frac(mesh_bin);
46,450✔
664
  } else {
665
    return 1.0;
39,170✔
666
  }
667
}
668

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

685
void read_eigenvalue_hdf5(hid_t group)
58✔
686
{
687
  read_dataset(group, "generations_per_batch", settings::gen_per_batch);
58✔
688
  int n = simulation::restart_batch * settings::gen_per_batch;
58✔
689
  simulation::k_generation.resize(n);
58✔
690
  read_dataset(group, "k_generation", simulation::k_generation);
58✔
691
  if (settings::entropy_on) {
58✔
692
    read_dataset(group, "entropy", simulation::entropy);
10✔
693
  }
694
  read_dataset(group, "k_col_abs", simulation::k_col_abs);
58✔
695
  read_dataset(group, "k_col_tra", simulation::k_col_tra);
58✔
696
  read_dataset(group, "k_abs_tra", simulation::k_abs_tra);
58✔
697
}
58✔
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