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

openmc-dev / openmc / 14840340664

05 May 2025 03:38PM UTC coverage: 85.195% (-0.009%) from 85.204%
14840340664

Pull #3392

github

web-flow
Merge 5bc1ec35f into 1e7d8324e
Pull Request #3392: Map Compton subshell data to atomic relaxation data

14 of 14 new or added lines in 2 files covered. (100.0%)

330 existing lines in 19 files now uncovered.

52194 of 61264 relevant lines covered (85.2%)

37398320.1 hits per line

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

60.0
/include/openmc/distribution.h
1
//! \file distribution.h
2
//! Univariate probability distributions
3

4
#ifndef OPENMC_DISTRIBUTION_H
5
#define OPENMC_DISTRIBUTION_H
6

7
#include <cstddef> // for size_t
8

9
#include "pugixml.hpp"
10

11
#include "openmc/constants.h"
12
#include "openmc/memory.h" // for unique_ptr
13
#include "openmc/span.h"
14
#include "openmc/vector.h" // for vector
15

16
namespace openmc {
17

18
//==============================================================================
19
//! Abstract class representing a univariate probability distribution
20
//==============================================================================
21

22
class Distribution {
23
public:
24
  virtual ~Distribution() = default;
25
  virtual double sample(uint64_t* seed) const = 0;
26

27
  //! Return integral of distribution
28
  //! \return Integral of distribution
UNCOV
29
  virtual double integral() const { return 1.0; };
×
30
};
31

32
using UPtrDist = unique_ptr<Distribution>;
33

34
//! Return univariate probability distribution specified in XML file
35
//! \param[in] node XML node representing distribution
36
//! \return Unique pointer to distribution
37
UPtrDist distribution_from_xml(pugi::xml_node node);
38

39
//==============================================================================
40
//! A discrete distribution index (probability mass function)
41
//==============================================================================
42

43
class DiscreteIndex {
44
public:
45
  DiscreteIndex() {};
46
  DiscreteIndex(pugi::xml_node node);
47
  DiscreteIndex(span<const double> p);
48

49
  void assign(span<const double> p);
50

51
  //! Sample a value from the distribution
52
  //! \param seed Pseudorandom number seed pointer
53
  //! \return Sampled value
54
  size_t sample(uint64_t* seed) const;
55

56
  // Properties
57
  const vector<double>& prob() const { return prob_; }
2,731✔
58
  const vector<size_t>& alias() const { return alias_; }
59
  double integral() const { return integral_; }
60

61
private:
62
  vector<double> prob_; //!< Probability of accepting the uniformly sampled bin,
63
                        //!< mapped to alias method table
64
  vector<size_t> alias_; //!< Alias table
65
  double integral_;      //!< Integral of distribution
66

67
  //! Normalize distribution so that probabilities sum to unity
68
  void normalize();
69

70
  //! Initialize alias tables for distribution
71
  void init_alias();
72
};
73

74
//==============================================================================
75
//! A discrete distribution (probability mass function)
76
//==============================================================================
77

78
class Discrete : public Distribution {
79
public:
80
  explicit Discrete(pugi::xml_node node);
81
  Discrete(const double* x, const double* p, size_t n);
82

83
  //! Sample a value from the distribution
84
  //! \param seed Pseudorandom number seed pointer
85
  //! \return Sampled value
86
  double sample(uint64_t* seed) const override;
87

88
  double integral() const override { return di_.integral(); };
89

90
  // Properties
91
  const vector<double>& x() const { return x_; }
16,750,381✔
92
  const vector<double>& prob() const { return di_.prob(); }
2,731✔
93
  const vector<size_t>& alias() const { return di_.alias(); }
94

95
private:
96
  vector<double> x_; //!< Possible outcomes
97
  DiscreteIndex di_; //!< discrete probability distribution of
98
                     //!< outcome indices
99
};
100

101
//==============================================================================
102
//! Uniform distribution over the interval [a,b]
103
//==============================================================================
104

105
class Uniform : public Distribution {
106
public:
107
  explicit Uniform(pugi::xml_node node);
UNCOV
108
  Uniform(double a, double b) : a_ {a}, b_ {b} {};
×
109

110
  //! Sample a value from the distribution
111
  //! \param seed Pseudorandom number seed pointer
112
  //! \return Sampled value
113
  double sample(uint64_t* seed) const override;
114

115
  double a() const { return a_; }
116
  double b() const { return b_; }
117

118
private:
119
  double a_; //!< Lower bound of distribution
120
  double b_; //!< Upper bound of distribution
121
};
122

123
//==============================================================================
124
//! PowerLaw distribution over the interval [a,b] with exponent n : p(x)=c x^n
125
//==============================================================================
126

127
class PowerLaw : public Distribution {
128
public:
129
  explicit PowerLaw(pugi::xml_node node);
130
  PowerLaw(double a, double b, double n)
131
    : offset_ {std::pow(a, n + 1)}, span_ {std::pow(b, n + 1) - offset_},
132
      ninv_ {1 / (n + 1)} {};
133

134
  //! Sample a value from the distribution
135
  //! \param seed Pseudorandom number seed pointer
136
  //! \return Sampled value
137
  double sample(uint64_t* seed) const override;
138

139
  double a() const { return std::pow(offset_, ninv_); }
140
  double b() const { return std::pow(offset_ + span_, ninv_); }
141
  double n() const { return 1 / ninv_ - 1; }
142

143
private:
144
  //! Store processed values in object to allow for faster sampling
145
  double offset_; //!< a^(n+1)
146
  double span_;   //!< b^(n+1) - a^(n+1)
147
  double ninv_;   //!< 1/(n+1)
148
};
149

150
//==============================================================================
151
//! Maxwellian distribution of form c*sqrt(E)*exp(-E/theta)
152
//==============================================================================
153

154
class Maxwell : public Distribution {
155
public:
156
  explicit Maxwell(pugi::xml_node node);
157
  Maxwell(double theta) : theta_ {theta} {};
158

159
  //! Sample a value from the distribution
160
  //! \param seed Pseudorandom number seed pointer
161
  //! \return Sampled value
162
  double sample(uint64_t* seed) const override;
163

164
  double theta() const { return theta_; }
165

166
private:
167
  double theta_; //!< Factor in exponential [eV]
168
};
169

170
//==============================================================================
171
//! Watt fission spectrum with form c*exp(-E/a)*sinh(sqrt(b*E))
172
//==============================================================================
173

174
class Watt : public Distribution {
175
public:
176
  explicit Watt(pugi::xml_node node);
177
  Watt(double a, double b) : a_ {a}, b_ {b} {};
178

179
  //! Sample a value from the distribution
180
  //! \param seed Pseudorandom number seed pointer
181
  //! \return Sampled value
182
  double sample(uint64_t* seed) const override;
183

184
  double a() const { return a_; }
185
  double b() const { return b_; }
186

187
private:
188
  double a_; //!< Factor in exponential [eV]
189
  double b_; //!< Factor in square root [1/eV]
190
};
191

192
//==============================================================================
193
//! Normal distributions with form 1/2*std_dev*sqrt(pi) exp
194
//! (-(e-E0)/2*std_dev)^2
195
//==============================================================================
196

197
class Normal : public Distribution {
198
public:
199
  explicit Normal(pugi::xml_node node);
200
  Normal(double mean_value, double std_dev)
201
    : mean_value_ {mean_value}, std_dev_ {std_dev} {};
202

203
  //! Sample a value from the distribution
204
  //! \param seed Pseudorandom number seed pointer
205
  //! \return Sampled value
206
  double sample(uint64_t* seed) const override;
207

208
  double mean_value() const { return mean_value_; }
209
  double std_dev() const { return std_dev_; }
210

211
private:
212
  double mean_value_; //!< middle of distribution [eV]
213
  double std_dev_;    //!< standard deviation [eV]
214
};
215

216
//==============================================================================
217
//! Histogram or linear-linear interpolated tabular distribution
218
//==============================================================================
219

220
class Tabular : public Distribution {
221
public:
222
  explicit Tabular(pugi::xml_node node);
223
  Tabular(const double* x, const double* p, int n, Interpolation interp,
224
    const double* c = nullptr);
225

226
  //! Sample a value from the distribution
227
  //! \param seed Pseudorandom number seed pointer
228
  //! \return Sampled value
229
  double sample(uint64_t* seed) const override;
230

231
  // properties
232
  vector<double>& x() { return x_; }
233
  const vector<double>& x() const { return x_; }
234
  const vector<double>& p() const { return p_; }
235
  Interpolation interp() const { return interp_; }
236
  double integral() const override { return integral_; };
237

238
private:
239
  vector<double> x_;     //!< tabulated independent variable
240
  vector<double> p_;     //!< tabulated probability density
241
  vector<double> c_;     //!< cumulative distribution at tabulated values
242
  Interpolation interp_; //!< interpolation rule
243
  double integral_;      //!< Integral of distribution
244

245
  //! Initialize tabulated probability density function
246
  //! \param x Array of values for independent variable
247
  //! \param p Array of tabulated probabilities
248
  //! \param n Number of tabulated values
249
  void init(
250
    const double* x, const double* p, std::size_t n, const double* c = nullptr);
251
};
252

253
//==============================================================================
254
//! Equiprobable distribution
255
//==============================================================================
256

257
class Equiprobable : public Distribution {
258
public:
259
  explicit Equiprobable(pugi::xml_node node);
260
  Equiprobable(const double* x, int n) : x_ {x, x + n} {};
261

262
  //! Sample a value from the distribution
263
  //! \param seed Pseudorandom number seed pointer
264
  //! \return Sampled value
265
  double sample(uint64_t* seed) const override;
266

267
  const vector<double>& x() const { return x_; }
268

269
private:
270
  vector<double> x_; //! Possible outcomes
271
};
272

273
//==============================================================================
274
//! Mixture distribution
275
//==============================================================================
276

277
class Mixture : public Distribution {
278
public:
279
  explicit Mixture(pugi::xml_node node);
280

281
  //! Sample a value from the distribution
282
  //! \param seed Pseudorandom number seed pointer
283
  //! \return Sampled value
284
  double sample(uint64_t* seed) const override;
285

286
  double integral() const override { return integral_; }
287

288
private:
289
  // Storrage for probability + distribution
290
  using DistPair = std::pair<double, UPtrDist>;
291

292
  vector<DistPair>
293
    distribution_;  //!< sub-distributions + cummulative probabilities
294
  double integral_; //!< integral of distribution
295
};
296

297
} // namespace openmc
298

299
#endif // OPENMC_DISTRIBUTION_H
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