4
4
#ifndef OPENMC_SOURCE_H
5
5
#define OPENMC_SOURCE_H
6
6
7
+ #include < limits>
7
8
#include < unordered_set>
8
9
9
10
#include " pugixml.hpp"
@@ -39,19 +40,72 @@ extern vector<unique_ptr<Source>> external_sources;
39
40
40
41
// ==============================================================================
41
42
// ! Abstract source interface
43
+ //
44
+ // ! The Source class provides the interface that must be implemented by derived
45
+ // ! classes, namely the sample() method that returns a sampled source site. From
46
+ // ! this base class, source rejection is handled within the
47
+ // ! sample_with_constraints() method. However, note that some classes directly
48
+ // ! check for constraints for efficiency reasons (like IndependentSource), in
49
+ // ! which case the constraints_applied() method indicates that constraints
50
+ // ! should not be checked a second time from the base class.
42
51
// ==============================================================================
43
52
44
53
class Source {
45
54
public:
55
+ // Constructors, destructors
56
+ Source () = default ;
57
+ explicit Source (pugi::xml_node node);
46
58
virtual ~Source () = default ;
47
59
48
- // Methods that must be implemented
49
- virtual SourceSite sample (uint64_t * seed) const = 0;
50
-
51
60
// Methods that can be overridden
52
- virtual double strength () const { return 1.0 ; }
61
+ virtual double strength () const { return strength_; }
62
+
63
+ // ! Sample a source site and apply constraints
64
+ //
65
+ // ! \param[inout] seed Pseudorandom seed pointer
66
+ // ! \return Sampled site
67
+ SourceSite sample_with_constraints (uint64_t * seed) const ;
68
+
69
+ // ! Sample a source site (without applying constraints)
70
+ //
71
+ // ! Sample from the external source distribution
72
+ // ! \param[inout] seed Pseudorandom seed pointer
73
+ // ! \return Sampled site
74
+ virtual SourceSite sample (uint64_t * seed) const = 0;
53
75
54
76
static unique_ptr<Source> create (pugi::xml_node node);
77
+
78
+ protected:
79
+ // Domain types
80
+ enum class DomainType { UNIVERSE, MATERIAL, CELL };
81
+
82
+ // Strategy used for rejecting sites when constraints are applied. KILL means
83
+ // that sites are always accepted but if they don't satisfy constraints, they
84
+ // are given weight 0. RESAMPLE means that a new source site will be sampled
85
+ // until constraints are met.
86
+ enum class RejectionStrategy { KILL, RESAMPLE };
87
+
88
+ // Indicates whether derived class already handles constraints
89
+ virtual bool constraints_applied () const { return false ; }
90
+
91
+ // Methods for constraints
92
+ void read_constraints (pugi::xml_node node);
93
+ bool satisfies_spatial_constraints (Position r) const ;
94
+ bool satisfies_energy_constraints (double E) const ;
95
+ bool satisfies_time_constraints (double time) const ;
96
+
97
+ // Data members
98
+ double strength_ {1.0 }; // !< Source strength
99
+ std::unordered_set<int32_t > domain_ids_; // !< Domains to reject from
100
+ DomainType domain_type_; // !< Domain type for rejection
101
+ std::pair<double , double > time_bounds_ {-std::numeric_limits<double >::max (),
102
+ std::numeric_limits<double >::max ()}; // !< time limits
103
+ std::pair<double , double > energy_bounds_ {
104
+ 0 , std::numeric_limits<double >::max ()}; // !< energy limits
105
+ bool only_fissionable_ {
106
+ false }; // !< Whether site must be in fissionable material
107
+ RejectionStrategy rejection_strategy_ {
108
+ RejectionStrategy::RESAMPLE}; // !< Procedure for rejecting
55
109
};
56
110
57
111
// ==============================================================================
@@ -73,27 +127,24 @@ class IndependentSource : public Source {
73
127
74
128
// Properties
75
129
ParticleType particle_type () const { return particle_; }
76
- double strength () const override { return strength_; }
77
130
78
131
// Make observing pointers available
79
132
SpatialDistribution* space () const { return space_.get (); }
80
133
UnitSphereDistribution* angle () const { return angle_.get (); }
81
134
Distribution* energy () const { return energy_.get (); }
82
135
Distribution* time () const { return time_.get (); }
83
136
84
- private :
85
- // Domain types
86
- enum class DomainType { UNIVERSE, MATERIAL, CELL };
137
+ protected :
138
+ // Indicates whether derived class already handles constraints
139
+ bool constraints_applied () const override { return true ; }
87
140
141
+ private:
88
142
// Data members
89
143
ParticleType particle_ {ParticleType::neutron}; // !< Type of particle emitted
90
- double strength_ {1.0 }; // !< Source strength
91
144
UPtrSpace space_; // !< Spatial distribution
92
145
UPtrAngle angle_; // !< Angular distribution
93
146
UPtrDist energy_; // !< Energy distribution
94
147
UPtrDist time_; // !< Time distribution
95
- DomainType domain_type_; // !< Domain type for rejection
96
- std::unordered_set<int32_t > domain_ids_; // !< Domains to reject from
97
148
};
98
149
99
150
// ==============================================================================
@@ -107,9 +158,12 @@ class FileSource : public Source {
107
158
explicit FileSource (const std::string& path);
108
159
109
160
// Methods
110
- SourceSite sample (uint64_t * seed) const override ;
111
161
void load_sites_from_file (
112
162
const std::string& path); // !< Load source sites from file
163
+
164
+ protected:
165
+ SourceSite sample (uint64_t * seed) const override ;
166
+
113
167
private:
114
168
vector<SourceSite> sites_; // !< Source sites from a file
115
169
};
@@ -124,16 +178,17 @@ class CompiledSourceWrapper : public Source {
124
178
CompiledSourceWrapper (pugi::xml_node node);
125
179
~CompiledSourceWrapper ();
126
180
181
+ double strength () const override { return compiled_source_->strength (); }
182
+
183
+ void setup (const std::string& path, const std::string& parameters);
184
+
185
+ protected:
127
186
// Defer implementation to custom source library
128
187
SourceSite sample (uint64_t * seed) const override
129
188
{
130
189
return compiled_source_->sample (seed);
131
190
}
132
191
133
- double strength () const override { return compiled_source_->strength (); }
134
-
135
- void setup (const std::string& path, const std::string& parameters);
136
-
137
192
private:
138
193
void * shared_library_; // !< library from dlopen
139
194
unique_ptr<Source> compiled_source_;
@@ -164,6 +219,9 @@ class MeshSource : public Source {
164
219
return sources_.size () == 1 ? sources_[0 ] : sources_[i];
165
220
}
166
221
222
+ protected:
223
+ bool constraints_applied () const override { return true ; }
224
+
167
225
private:
168
226
// Data members
169
227
unique_ptr<MeshSpatial> space_; // !< Mesh spatial
0 commit comments