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

openmc-dev / openmc / 19102841639

05 Nov 2025 01:01PM UTC coverage: 82.019% (-3.1%) from 85.155%
19102841639

Pull #3252

github

web-flow
Merge 3c71decac into bd76fc056
Pull Request #3252: Adding vtkhdf option to write vtk data

16722 of 23233 branches covered (71.98%)

Branch coverage included in aggregate %.

61 of 66 new or added lines in 1 file covered. (92.42%)

3177 existing lines in 103 files now uncovered.

54247 of 63294 relevant lines covered (85.71%)

42990146.99 hits per line

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

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

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

59
  double keff_reduced;
60
#ifdef OPENMC_MPI
61
  if (settings::solver_type != SolverType::RANDOM_RAY) {
49,084✔
62
    // Combine values across all processors
63
    MPI_Allreduce(&simulation::keff_generation, &keff_reduced, 1, MPI_DOUBLE,
47,184✔
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;
37,303✔
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) {
86,387✔
80
    keff_reduced /= settings::n_particles;
83,347✔
81
  }
82

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

86
void synchronize_bank()
83,347✔
87
{
88
  simulation::time_bank.start();
83,347✔
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;
47,184✔
101
  int64_t n_bank = simulation::fission_bank.size();
47,184✔
102
  MPI_Exscan(&n_bank, &start, 1, MPI_INT64_T, MPI_SUM, mpi::intracomm);
47,184✔
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)
47,184✔
108
    start = 0;
31,524✔
109

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

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

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

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

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

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

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

165
  // Locally comb particles in fission_bank
166
  double tooth = tooth_start * teeth_distance + teeth_offset;
83,347✔
167
  for (int64_t i = tooth_start; i < tooth_end; i++) {
140,321,847✔
168
    int64_t idx = std::floor(tooth) - start;
140,238,500✔
169
    temp_sites[index_temp] = simulation::fission_bank[idx];
140,238,500✔
170
    if (settings::ifp_on) {
140,238,500✔
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;
140,238,500✔
175

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

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

217
  int64_t index_local = 0;
47,184✔
218
  vector<MPI_Request> requests;
47,184✔
219

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

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

230
    // Resize IFP send buffers
231
    if (settings::ifp_on && mpi::n_procs > 1) {
47,184✔
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) {
70,569✔
237
      // Determine the number of sites to send
238
      int64_t n =
239
        std::min(simulation::work_index[neighbor + 1], finish) - start;
62,608✔
240

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

249
        if (settings::ifp_on) {
15,424✔
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;
62,608✔
259
      index_local += n;
62,608✔
260
      ++neighbor;
62,608✔
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)
62,608✔
266
        break;
39,223✔
267
    }
268
  }
269

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

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

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

292
  // Resize IFP receive buffers
293
  if (settings::ifp_on && mpi::n_procs > 1) {
47,184✔
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]) {
109,792✔
299
    // Determine how many sites need to be received
300
    int64_t n;
301
    if (neighbor == mpi::n_procs - 1) {
62,608✔
302
      n = simulation::work_index[mpi::rank + 1] - start;
39,249✔
303
    } else {
304
      n = std::min(bank_position[neighbor + 1],
23,359✔
305
            simulation::work_index[mpi::rank + 1]) -
46,718✔
306
          start;
307
    }
308

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

317
      if (settings::ifp_on) {
15,424✔
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];
47,184✔
328
      std::copy(&temp_sites[index_temp], &temp_sites[index_temp + n],
47,184✔
329
        &simulation::source_bank[index_local]);
47,184✔
330

331
      if (settings::ifp_on) {
47,184✔
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;
62,608✔
339
    index_local += n;
62,608✔
340
    ++neighbor;
62,608✔
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();
47,184✔
348
  MPI_Waitall(n_request, requests.data(), MPI_STATUSES_IGNORE);
47,184✔
349

350
  if (settings::ifp_on) {
47,184✔
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,
36,163✔
357
    simulation::source_bank.begin());
358
  if (settings::ifp_on) {
36,163✔
359
    copy_complete_ifp_data_to_source_banks(temp_delayed_groups, temp_lifetimes);
720✔
360
  }
361
#endif
362

363
  simulation::time_bank_sendrecv.stop();
83,347✔
364
  simulation::time_bank.stop();
83,347✔
365
}
130,531✔
366

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

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

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

391
    if (n > 1) {
67,539✔
392
      double t_value;
393
      if (settings::confidence_intervals) {
63,648✔
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;
63,536✔
399
      }
400

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

410
int openmc_get_keff(double* k_combined)
13,086✔
411
{
412
  k_combined[0] = 0.0;
13,086✔
413
  k_combined[1] = 0.0;
13,086✔
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 ||
13,086✔
418
      settings::solver_type == SolverType::RANDOM_RAY) {
9,919✔
419
    k_combined[0] = simulation::keff;
3,299✔
420
    k_combined[1] = simulation::keff_std;
3,299✔
421
    if (simulation::n_realizations <= 1) {
3,299✔
422
      k_combined[1] = std::numeric_limits<double>::infinity();
2,337✔
423
    }
424
    return 0;
3,299✔
425
  }
426

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

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

433
  array<double, 3> kv {};
9,787✔
434
  xt::xtensor<double, 2> cov = xt::zeros<double>({3, 3});
9,787✔
435
  kv[0] = gt(GlobalTally::K_COLLISION, TallyResult::SUM) / n;
9,787✔
436
  kv[1] = gt(GlobalTally::K_ABSORPTION, TallyResult::SUM) / n;
9,787✔
437
  kv[2] = gt(GlobalTally::K_TRACKLENGTH, TallyResult::SUM) / n;
9,787✔
438
  cov(0, 0) =
19,574✔
439
    (gt(GlobalTally::K_COLLISION, TallyResult::SUM_SQ) - n * kv[0] * kv[0]) /
9,787✔
440
    (n - 1);
9,787✔
441
  cov(1, 1) =
19,574✔
442
    (gt(GlobalTally::K_ABSORPTION, TallyResult::SUM_SQ) - n * kv[1] * kv[1]) /
9,787✔
443
    (n - 1);
9,787✔
444
  cov(2, 2) =
19,574✔
445
    (gt(GlobalTally::K_TRACKLENGTH, TallyResult::SUM_SQ) - n * kv[2] * kv[2]) /
9,787✔
446
    (n - 1);
9,787✔
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,787✔
450
  cov(0, 2) = (simulation::k_col_tra - n * kv[0] * kv[2]) / (n - 1);
9,787✔
451
  cov(1, 2) = (simulation::k_abs_tra - n * kv[1] * kv[2]) / (n - 1);
9,787✔
452
  cov(1, 0) = cov(0, 1);
9,787✔
453
  cov(2, 0) = cov(0, 2);
9,787✔
454
  cov(2, 1) = cov(1, 2);
9,787✔
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,787✔
466
  if ((std::abs(kv[0] - kv[1]) / kv[0] < FP_REL_PRECISION) &&
9,809✔
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,765!
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
UNCOV
475
    i = 0;
×
UNCOV
476
    j = 1;
×
477

478
  } else if ((std::abs(kv[1] - kv[2]) / kv[1] < FP_REL_PRECISION) &&
9,765!
UNCOV
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
UNCOV
481
    i = 0;
×
UNCOV
482
    j = 1;
×
483

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

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

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

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

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

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

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

534
    // Complete calculations of S sums
535
    for (auto& S_i : S) {
39,060✔
536
      S_i *= (n - 1);
29,295✔
537
    }
538
    S[0] *= (n - 1) * (n - 1);
9,765✔
539

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

543
    // Calculate standard deviation of combined estimate
544
    g *= (n - 1) * (n - 1);
9,765✔
545
    k_combined[1] =
9,765✔
546
      std::sqrt(S[0] / (g * n * (n - 3)) * (1 + n * ((S[1] - 2 * S[2]) / g)));
9,765✔
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,787✔
567
}
9,787✔
568

569
void shannon_entropy()
8,280✔
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,280✔
575
      simulation::fission_bank.size(), &sites_outside);
16,560✔
576

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

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

587
    // Sum values to obtain Shannon entropy
588
    double H = 0.0;
8,230✔
589
    for (auto p_i : p) {
619,550✔
590
      if (p_i > 0.0) {
611,320✔
591
        H -= p_i * std::log2(p_i);
482,520✔
592
      }
593
    }
594

595
    // Add value to vector
596
    simulation::entropy.push_back(H);
8,230✔
597
  }
598
}
8,280✔
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!
UNCOV
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!
UNCOV
647
    p.write_restart();
×
UNCOV
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,953✔
660
{
661
  write_dataset(group, "n_inactive", settings::n_inactive);
3,953✔
662
  write_dataset(group, "generations_per_batch", settings::gen_per_batch);
3,953✔
663
  write_dataset(group, "k_generation", simulation::k_generation);
3,953✔
664
  if (settings::entropy_on) {
3,953✔
665
    write_dataset(group, "entropy", simulation::entropy);
430✔
666
  }
667
  write_dataset(group, "k_col_abs", simulation::k_col_abs);
3,953✔
668
  write_dataset(group, "k_col_tra", simulation::k_col_tra);
3,953✔
669
  write_dataset(group, "k_abs_tra", simulation::k_abs_tra);
3,953✔
670
  array<double, 2> k_combined;
671
  openmc_get_keff(k_combined.data());
3,953✔
672
  write_dataset(group, "k_combined", k_combined);
3,953✔
673
}
3,953✔
674

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