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

openmc-dev / openmc / 15593141567

11 Jun 2025 06:49PM UTC coverage: 85.168% (+0.01%) from 85.158%
15593141567

Pull #3423

github

web-flow
Merge 8d67d5464 into 2eeba8999
Pull Request #3423: Fix raytrace infinite loop.

5 of 5 new or added lines in 1 file covered. (100.0%)

362 existing lines in 7 files now uncovered.

52396 of 61521 relevant lines covered (85.17%)

36622826.42 hits per line

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

88.24
/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
// Minimum number of external source sites rejected before checking againts the
25
// source_rejection_fraction
26
constexpr int EXTSRC_REJECT_THRESHOLD {10000};
27

28
//==============================================================================
29
// Global variables
30
//==============================================================================
31

32
class Source;
33

34
namespace model {
35

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

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

41
} // namespace model
42

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

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

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

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

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

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

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

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

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

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

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

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

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

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

132
  // Make observing pointers available
UNCOV
133
  SpatialDistribution* space() const { return space_.get(); }
×
134
  UnitSphereDistribution* angle() const { return angle_.get(); }
135
  Distribution* energy() const { return energy_.get(); }
136
  Distribution* time() const { return time_.get(); }
137

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

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

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

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

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

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

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

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

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

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

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

188
  void setup(const std::string& path, const std::string& parameters);
32✔
189

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

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

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

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

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

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

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

221
  // Accessors
201✔
222
  const std::unique_ptr<Source>& source(int32_t i) const
223
  {
224
    return sources_.size() == 1 ? sources_[0] : sources_[i];
187,140✔
225
  }
226

187,140✔
227
protected:
228
  bool constraints_applied() const override { return true; }
229

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

236
//==============================================================================
237
// Functions
238
//==============================================================================
239

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

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

249
void free_memory_source();
250

251
} // namespace openmc
252

253
#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

© 2026 Coveralls, Inc