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

openmc-dev / openmc / 12776996362

14 Jan 2025 09:49PM UTC coverage: 84.938% (+0.2%) from 84.729%
12776996362

Pull #3133

github

web-flow
Merge 0495246d9 into 549cc0973
Pull Request #3133: Kinetics parameters using Iterated Fission Probability

318 of 330 new or added lines in 10 files covered. (96.36%)

1658 existing lines in 66 files now uncovered.

50402 of 59340 relevant lines covered (84.94%)

33987813.96 hits per line

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

95.0
/include/openmc/source.h
1
//! \file source.h
2
//! \brief External source distributions
3

4
#ifndef OPENMC_SOURCE_H
5
#define OPENMC_SOURCE_H
6

7
#include <limits>
8
#include <unordered_set>
9

10
#include "pugixml.hpp"
11

12
#include "openmc/distribution_multi.h"
13
#include "openmc/distribution_spatial.h"
14
#include "openmc/memory.h"
15
#include "openmc/particle.h"
16
#include "openmc/vector.h"
17

18
namespace openmc {
19

20
//==============================================================================
21
// Constants
22
//==============================================================================
23

24
// Maximum number of external source spatial resamples to encounter before an
25
// error is thrown.
26
constexpr int EXTSRC_REJECT_THRESHOLD {10000};
27
constexpr double EXTSRC_REJECT_FRACTION {0.05};
28

29
//==============================================================================
30
// Global variables
31
//==============================================================================
32

33
class Source;
34

35
namespace model {
36

37
extern vector<unique_ptr<Source>> external_sources;
38

39
// Probability distribution for selecting external sources
40
extern DiscreteIndex external_sources_probability;
41

42
} // namespace model
43

44
//==============================================================================
45
//! Abstract source interface
46
//
47
//! The Source class provides the interface that must be implemented by derived
48
//! classes, namely the sample() method that returns a sampled source site. From
49
//! this base class, source rejection is handled within the
50
//! sample_with_constraints() method. However, note that some classes directly
51
//! check for constraints for efficiency reasons (like IndependentSource), in
52
//! which case the constraints_applied() method indicates that constraints
53
//! should not be checked a second time from the base class.
54
//==============================================================================
55

56
class Source {
57
public:
58
  // Domain types
59
  enum class DomainType { UNIVERSE, MATERIAL, CELL };
60
  // Constructors, destructors
61
  Source() = default;
5,355✔
62
  explicit Source(pugi::xml_node node);
63
  virtual ~Source() = default;
46,262✔
UNCOV
64

×
65
  // Methods that can be overridden
46,262✔
66
  virtual double strength() const { return strength_; }
67

68
  //! Sample a source site and apply constraints
73,104✔
69
  //
70
  //! \param[inout] seed Pseudorandom seed pointer
71
  //! \return Sampled site
72
  SourceSite sample_with_constraints(uint64_t* seed) const;
73

74
  //! Sample a source site (without applying constraints)
75
  //
76
  //! Sample from the external source distribution
77
  //! \param[inout] seed Pseudorandom seed pointer
78
  //! \return Sampled site
79
  virtual SourceSite sample(uint64_t* seed) const = 0;
80

81
  static unique_ptr<Source> create(pugi::xml_node node);
82

83
protected:
84
  // Strategy used for rejecting sites when constraints are applied. KILL means
85
  // that sites are always accepted but if they don't satisfy constraints, they
86
  // are given weight 0. RESAMPLE means that a new source site will be sampled
87
  // until constraints are met.
88
  enum class RejectionStrategy { KILL, RESAMPLE };
89

90
  // Indicates whether derived class already handles constraints
91
  virtual bool constraints_applied() const { return false; }
92

93
  // Methods for constraints
432,050✔
94
  void read_constraints(pugi::xml_node node);
95
  bool satisfies_spatial_constraints(Position r) const;
96
  bool satisfies_energy_constraints(double E) const;
97
  bool satisfies_time_constraints(double time) const;
98

99
  // Data members
100
  double strength_ {1.0};                  //!< Source strength
101
  std::unordered_set<int32_t> domain_ids_; //!< Domains to reject from
102
  DomainType domain_type_;                 //!< Domain type for rejection
103
  std::pair<double, double> time_bounds_ {-std::numeric_limits<double>::max(),
104
    std::numeric_limits<double>::max()}; //!< time limits
105
  std::pair<double, double> energy_bounds_ {
46,273✔
106
    0, std::numeric_limits<double>::max()}; //!< energy limits
107
  bool only_fissionable_ {
108
    false}; //!< Whether site must be in fissionable material
46,273✔
109
  RejectionStrategy rejection_strategy_ {
110
    RejectionStrategy::RESAMPLE}; //!< Procedure for rejecting
111
};
112

113
//==============================================================================
114
//! Source composed of independent spatial, angle, energy, and time
115
//! distributions
116
//==============================================================================
117

118
class IndependentSource : public Source {
119
public:
120
  // Constructors
121
  IndependentSource(
122
    UPtrSpace space, UPtrAngle angle, UPtrDist energy, UPtrDist time);
123
  explicit IndependentSource(pugi::xml_node node);
124

125
  //! Sample from the external source distribution
126
  //! \param[inout] seed Pseudorandom seed pointer
127
  //! \return Sampled site
128
  SourceSite sample(uint64_t* seed) const override;
129

130
  // Properties
131
  ParticleType particle_type() const { return particle_; }
132

133
  // Make observing pointers available
134
  SpatialDistribution* space() const { return space_.get(); }
643✔
135
  UnitSphereDistribution* angle() const { return angle_.get(); }
444✔
136
  Distribution* energy() const { return energy_.get(); }
464✔
137
  Distribution* time() const { return time_.get(); }
138

139
  // Make domain type and ids available
140
  DomainType domain_type() const { return domain_type_; }
141
  const std::unordered_set<int32_t>& domain_ids() const { return domain_ids_; }
464✔
142

143
protected:
144
  // Indicates whether derived class already handles constraints
145
  bool constraints_applied() const override { return true; }
146

147
private:
16,063,193✔
148
  // Data members
149
  ParticleType particle_ {ParticleType::neutron}; //!< Type of particle emitted
150
  UPtrSpace space_;                               //!< Spatial distribution
151
  UPtrAngle angle_;                               //!< Angular distribution
152
  UPtrDist energy_;                               //!< Energy distribution
153
  UPtrDist time_;                                 //!< Time distribution
154
};
155

156
//==============================================================================
157
//! Source composed of particles read from a file
158
//==============================================================================
159

160
class FileSource : public Source {
161
public:
162
  // Constructors
163
  explicit FileSource(pugi::xml_node node);
164
  explicit FileSource(const std::string& path);
165

166
  // Methods
167
  void load_sites_from_file(
168
    const std::string& path); //!< Load source sites from file
169

170
protected:
171
  SourceSite sample(uint64_t* seed) const override;
172

173
private:
174
  vector<SourceSite> sites_; //!< Source sites from a file
175
};
176

177
//==============================================================================
178
//! Wrapper for custom sources that manages opening/closing shared library
179
//==============================================================================
180

181
class CompiledSourceWrapper : public Source {
182
public:
183
  // Constructors, destructors
184
  CompiledSourceWrapper(pugi::xml_node node);
185
  ~CompiledSourceWrapper();
186

187
  double strength() const override { return compiled_source_->strength(); }
188

189
  void setup(const std::string& path, const std::string& parameters);
274✔
190

191
protected:
192
  // Defer implementation to custom source library
193
  SourceSite sample(uint64_t* seed) const override
194
  {
195
    return compiled_source_->sample(seed);
240,000✔
196
  }
197

240,000✔
198
private:
199
  void* shared_library_; //!< library from dlopen
200
  unique_ptr<Source> compiled_source_;
201
};
202

203
typedef unique_ptr<Source> create_compiled_source_t(std::string parameters);
204

205
//==============================================================================
206
//! Mesh-based source with different distributions for each element
207
//==============================================================================
208

209
class MeshSource : public Source {
210
public:
211
  // Constructors
212
  explicit MeshSource(pugi::xml_node node);
213

214
  //! Sample from the external source distribution
215
  //! \param[inout] seed Pseudorandom seed pointer
216
  //! \return Sampled site
217
  SourceSite sample(uint64_t* seed) const override;
218

219
  // Properties
220
  double strength() const override { return space_->total_strength(); }
221

222
  // Accessors
2,139✔
223
  const std::unique_ptr<Source>& source(int32_t i) const
224
  {
225
    return sources_.size() == 1 ? sources_[0] : sources_[i];
204,150✔
226
  }
227

204,150✔
228
protected:
229
  bool constraints_applied() const override { return true; }
230

231
private:
204,150✔
232
  // Data members
233
  unique_ptr<MeshSpatial> space_;           //!< Mesh spatial
234
  vector<std::unique_ptr<Source>> sources_; //!< Source distributions
235
};
236

237
//==============================================================================
238
// Functions
239
//==============================================================================
240

241
//! Initialize source bank from file/distribution
242
extern "C" void initialize_source();
243

244
//! Sample a site from all external source distributions in proportion to their
245
//! source strength
246
//! \param[inout] seed Pseudorandom seed pointer
247
//! \return Sampled source site
248
SourceSite sample_external_source(uint64_t* seed);
249

250
void free_memory_source();
251

252
} // namespace openmc
253

254
#endif // OPENMC_SOURCE_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