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

openmc-dev / openmc / 21139931957

19 Jan 2026 01:50PM UTC coverage: 81.835% (-0.2%) from 82.058%
21139931957

Pull #2693

github

web-flow
Merge 1258e263b into 5847b0de2
Pull Request #2693: Add reactivity control to coupled transport-depletion analyses

17180 of 23968 branches covered (71.68%)

Branch coverage included in aggregate %.

78 of 84 new or added lines in 4 files covered. (92.86%)

345 existing lines in 27 files now uncovered.

55616 of 64987 relevant lines covered (85.58%)

50201535.92 hits per line

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

78.43
/src/random_lcg.cpp
1
#include "openmc/random_lcg.h"
2

3
#include <cmath>
4

5
namespace openmc {
6

7
// Starting seed
8
int64_t master_seed {1};
9

10
// LCG parameters
11
constexpr uint64_t prn_mult {6364136223846793005ULL}; // multiplication
12
constexpr uint64_t prn_add {1442695040888963407ULL};  // additive factor, c
13
uint64_t prn_stride {DEFAULT_STRIDE}; // stride between particles
14

15
//==============================================================================
16
// PRN
17
//==============================================================================
18

19
// 64 bit implementation of the PCG-RXS-M-XS 64-bit state / 64-bit output
20
// geneator Adapted from: https://github.com/imneme/pcg-c, in particular
21
// https://github.com/imneme/pcg-c/blob/83252d9c23df9c82ecb42210afed61a7b42402d7/include/pcg_variants.h#L188-L192
22
// @techreport{oneill:pcg2014,
23
//    title = "PCG: A Family of Simple Fast Space-Efficient Statistically Good
24
//    Algorithms for Random Number Generation", author = "Melissa E. O'Neill",
25
//    institution = "Harvey Mudd College",
26
//    address = "Claremont, CA",
27
//    number = "HMC-CS-2014-0905",
28
//    year = "2014",
29
//    month = Sep,
30
//    xurl = "https://www.cs.hmc.edu/tr/hmc-cs-2014-0905.pdf",
31
//}
32
double prn(uint64_t* seed)
2,147,483,647✔
33
{
34
  // Advance the LCG
UNCOV
35
  *seed = (prn_mult * (*seed) + prn_add);
×
36

37
  // Permute the output
UNCOV
38
  uint64_t word =
×
UNCOV
39
    ((*seed >> ((*seed >> 59u) + 5u)) ^ *seed) * 12605985483714917081ull;
×
UNCOV
40
  uint64_t result = (word >> 43u) ^ word;
×
41

42
  // Convert output from unsigned integer to double
UNCOV
43
  return ldexp(result, -64);
×
44
}
45

46
//==============================================================================
47
// FUTURE_PRN
48
//==============================================================================
49

50
double future_prn(int64_t n, uint64_t seed)
1,322,523,785✔
51
{
52
  uint64_t fseed = future_seed(static_cast<uint64_t>(n), seed);
1,322,523,785✔
53
  return prn(&fseed);
2,147,483,647✔
54
}
55

56
//==============================================================================
57
// INIT_SEED
58
//==============================================================================
59

60
uint64_t init_seed(int64_t id, int offset)
38,430,839✔
61
{
62
  return future_seed(
76,861,678✔
63
    static_cast<uint64_t>(id) * prn_stride, master_seed + offset);
38,430,839✔
64
}
65

66
//==============================================================================
67
// INIT_PARTICLE_SEEDS
68
//==============================================================================
69

70
void init_particle_seeds(int64_t id, uint64_t* seeds)
237,801,674✔
71
{
72
  for (int i = 0; i < N_STREAMS; i++) {
1,189,008,370✔
73
    seeds[i] =
1,902,413,392✔
74
      future_seed(static_cast<uint64_t>(id) * prn_stride, master_seed + i);
951,206,696✔
75
  }
76
}
237,801,674✔
77

78
//==============================================================================
79
// ADVANCE_PRN_SEED
80
//==============================================================================
81

82
void advance_prn_seed(int64_t n, uint64_t* seed)
2,147,483,647✔
83
{
84
  *seed = future_seed(static_cast<uint64_t>(n), *seed);
2,147,483,647✔
85
}
2,147,483,647✔
86

87
//==============================================================================
88
// FUTURE_SEED
89
//==============================================================================
90

91
uint64_t future_seed(uint64_t n, uint64_t seed)
2,147,483,647✔
92
{
93
  // The algorithm here to determine the parameters used to skip ahead is
94
  // described in F. Brown, "Random Number Generation with Arbitrary Stride,"
95
  // Trans. Am. Nucl. Soc. (Nov. 1994). This algorithm is able to skip ahead in
96
  // O(log2(N)) operations instead of O(N). Basically, it computes parameters G
97
  // and C which can then be used to find x_N = G*x_0 + C mod 2^M.
98

99
  // Initialize constants
100
  uint64_t g {prn_mult};
2,147,483,647✔
101
  uint64_t c {prn_add};
2,147,483,647✔
102
  uint64_t g_new {1};
2,147,483,647✔
103
  uint64_t c_new {0};
2,147,483,647✔
104

UNCOV
105
  while (n > 0) {
×
106
    // Check if the least significant bit is 1.
UNCOV
107
    if (n & 1) {
✔
108
      g_new *= g;
2,147,483,647✔
109
      c_new = c_new * g + c;
2,147,483,647✔
110
    }
UNCOV
111
    c *= (g + 1);
×
UNCOV
112
    g *= g;
×
113

114
    // Move bits right, dropping least significant bit.
UNCOV
115
    n >>= 1;
×
116
  }
117

118
  // With G and C, we can now find the new seed.
119
  return g_new * seed + c_new;
2,147,483,647✔
120
}
121

122
//==============================================================================
123
//                               API FUNCTIONS
124
//==============================================================================
125

126
extern "C" int64_t openmc_get_seed()
6,476✔
127
{
128
  return master_seed;
6,476✔
129
}
130

131
extern "C" void openmc_set_seed(int64_t new_seed)
15,756✔
132
{
133
  master_seed = new_seed;
15,756✔
134
}
15,756✔
135

136
extern "C" uint64_t openmc_get_stride()
6,466✔
137
{
138
  return prn_stride;
6,466✔
139
}
140

141
extern "C" void openmc_set_stride(uint64_t new_stride)
15,201✔
142
{
143
  prn_stride = new_stride;
15,201✔
144
}
15,201✔
145

146
} // namespace openmc
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc