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

openmc-dev / openmc / 16271854850

14 Jul 2025 04:04PM UTC coverage: 85.175% (-0.08%) from 85.251%
16271854850

Pull #3481

github

web-flow
Merge 0c4ea15ee into d700d395d
Pull Request #3481: Provide a way to get ID maps from plot parameters on the Model class

50 of 50 new or added lines in 3 files covered. (100.0%)

82 existing lines in 15 files now uncovered.

52541 of 61686 relevant lines covered (85.17%)

36733523.93 hits per line

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

90.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
// 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;
5,229✔
61
  explicit Source(pugi::xml_node node);
62
  virtual ~Source() = default;
46,242✔
63

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

67
  //! Sample a source site and apply constraints
51,783✔
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
1,909,554✔
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,252✔
105
    0, std::numeric_limits<double>::max()}; //!< energy limits
106
  bool only_fissionable_ {
107
    false}; //!< Whether site must be in fissionable material
46,252✔
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
  // Setter for spatial distribution
143
  void set_space(UPtrSpace space) { space_ = std::move(space); }
144

145
protected:
37,452✔
146
  // Indicates whether derived class already handles constraints
147
  bool constraints_applied() const override { return true; }
148

149
private:
27,431,203✔
150
  // Data members
151
  ParticleType particle_ {ParticleType::neutron}; //!< Type of particle emitted
152
  UPtrSpace space_;                               //!< Spatial distribution
153
  UPtrAngle angle_;                               //!< Angular distribution
154
  UPtrDist energy_;                               //!< Energy distribution
155
  UPtrDist time_;                                 //!< Time distribution
156
};
157

158
//==============================================================================
159
//! Source composed of particles read from a file
160
//==============================================================================
161

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

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

172
protected:
173
  SourceSite sample(uint64_t* seed) const override;
174

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

179
//==============================================================================
180
//! Wrapper for custom sources that manages opening/closing shared library
181
//==============================================================================
182

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

189
  double strength() const override { return compiled_source_->strength(); }
190

191
  void setup(const std::string& path, const std::string& parameters);
32✔
192

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

220,000✔
200
private:
201
  void* shared_library_; //!< library from dlopen
202
  unique_ptr<Source> compiled_source_;
203
};
204

205
typedef unique_ptr<Source> create_compiled_source_t(std::string parameters);
206

207
//==============================================================================
208
//! Mesh-based source with different distributions for each element
209
//==============================================================================
210

211
// Helper class to sample spatial position on a single mesh element
212
class MeshElementSpatial : public SpatialDistribution {
213
public:
214
  MeshElementSpatial(int32_t mesh_index, int elem_index)
215
    : mesh_index_(mesh_index), elem_index_(elem_index)
216
  {}
37,452✔
217

37,452✔
218
  //! Sample a position from the distribution
37,452✔
219
  //! \param seed Pseudorandom number seed pointer
220
  //! \return Sampled position
221
  Position sample(uint64_t* seed) const override;
222

223
private:
224
  int32_t mesh_index_ {C_NONE}; //!< Index in global meshes array
225
  int elem_index_;              //! Index of mesh element
226
};
227

228
class MeshSource : public Source {
229
public:
230
  // Constructors
231
  explicit MeshSource(pugi::xml_node node);
232

233
  //! Sample from the external source distribution
234
  //! \param[inout] seed Pseudorandom seed pointer
235
  //! \return Sampled site
236
  SourceSite sample(uint64_t* seed) const override;
237

238
  // Properties
239
  double strength() const override { return space_->total_strength(); }
240

241
  // Accessors
201✔
242
  const unique_ptr<IndependentSource>& source(int32_t i) const
243
  {
244
    return sources_.size() == 1 ? sources_[0] : sources_[i];
1,513,630✔
245
  }
246

1,513,630✔
247
private:
248
  // Data members
249
  unique_ptr<MeshSpatial> space_;                 //!< Mesh spatial
250
  vector<unique_ptr<IndependentSource>> sources_; //!< Source distributions
251
};
252

253
//==============================================================================
254
// Functions
255
//==============================================================================
256

257
//! Initialize source bank from file/distribution
258
extern "C" void initialize_source();
259

260
//! Sample a site from all external source distributions in proportion to their
261
//! source strength
262
//! \param[inout] seed Pseudorandom seed pointer
263
//! \return Sampled source site
264
SourceSite sample_external_source(uint64_t* seed);
265

266
void free_memory_source();
267

268
} // namespace openmc
269

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