mirror of
https://github.com/openmc-dev/openmc.git
synced 2026-07-28 06:05:58 -04:00
Make regular source distributions obey the same interface as custom sources
This commit is contained in:
parent
8f71ed2a1d
commit
713469bf59
8 changed files with 38 additions and 36 deletions
|
|
@ -5,7 +5,7 @@
|
|||
#include "openmc/source.h"
|
||||
#include "openmc/particle.h"
|
||||
|
||||
class Source : public openmc::CustomSource
|
||||
class Source : public openmc::SourceDistribution
|
||||
{
|
||||
openmc::Particle::Bank sample(uint64_t* seed)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -6,8 +6,7 @@
|
|||
#include "openmc/source.h"
|
||||
#include "openmc/particle.h"
|
||||
|
||||
class Source : public openmc::CustomSource
|
||||
{
|
||||
class Source : public openmc::SourceDistribution {
|
||||
public:
|
||||
Source(double radius, double energy) : radius_(radius), energy_(energy) { }
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ class SourceDistribution;
|
|||
|
||||
namespace model {
|
||||
|
||||
extern std::vector<SourceDistribution> external_sources;
|
||||
extern std::vector<std::unique_ptr<SourceDistribution>> external_sources;
|
||||
|
||||
} // namespace model
|
||||
|
||||
|
|
@ -32,19 +32,30 @@ extern std::vector<SourceDistribution> external_sources;
|
|||
//==============================================================================
|
||||
|
||||
class SourceDistribution {
|
||||
public:
|
||||
virtual ~SourceDistribution() = default;
|
||||
|
||||
// Methods that must be implemented
|
||||
virtual Particle::Bank sample(uint64_t* seed) = 0;
|
||||
|
||||
// Methods that can be overridden
|
||||
virtual double strength() const { return 1.0; }
|
||||
};
|
||||
|
||||
class IndependentSourceDistribution : public SourceDistribution {
|
||||
public:
|
||||
// Constructors
|
||||
SourceDistribution(UPtrSpace space, UPtrAngle angle, UPtrDist energy);
|
||||
explicit SourceDistribution(pugi::xml_node node);
|
||||
IndependentSourceDistribution(UPtrSpace space, UPtrAngle angle, UPtrDist energy);
|
||||
explicit IndependentSourceDistribution(pugi::xml_node node);
|
||||
|
||||
//! Sample from the external source distribution
|
||||
//! \param[inout] seed Pseudorandom seed pointer
|
||||
//! \return Sampled site
|
||||
Particle::Bank sample(uint64_t* seed) const;
|
||||
Particle::Bank sample(uint64_t* seed) override;
|
||||
|
||||
// Properties
|
||||
Particle::Type particle_type() const { return particle_; }
|
||||
double strength() const { return strength_; }
|
||||
double strength() const override { return strength_; }
|
||||
|
||||
// Make observing pointers available
|
||||
SpatialDistribution* space() const { return space_.get(); }
|
||||
|
|
@ -60,14 +71,7 @@ private:
|
|||
std::vector<Particle::Bank> sites_; //!< Source sites from a file
|
||||
};
|
||||
|
||||
class CustomSource {
|
||||
public:
|
||||
virtual ~CustomSource() {}
|
||||
|
||||
virtual Particle::Bank sample(uint64_t* seed) = 0;
|
||||
};
|
||||
|
||||
typedef std::unique_ptr<CustomSource> create_custom_source_t(std::string parameters);
|
||||
typedef std::unique_ptr<SourceDistribution> create_custom_source_t(std::string parameters);
|
||||
|
||||
//==============================================================================
|
||||
// Functions
|
||||
|
|
|
|||
|
|
@ -420,17 +420,16 @@ void read_settings_xml()
|
|||
|
||||
// Get point to list of <source> elements and make sure there is at least one
|
||||
for (pugi::xml_node node : root.children("source")) {
|
||||
model::external_sources.emplace_back(node);
|
||||
model::external_sources.push_back(std::make_unique<IndependentSourceDistribution>(node));
|
||||
}
|
||||
|
||||
// If no source specified, default to isotropic point source at origin with Watt spectrum
|
||||
if (model::external_sources.empty()) {
|
||||
SourceDistribution source {
|
||||
model::external_sources.push_back(std::make_unique<IndependentSourceDistribution>(
|
||||
UPtrSpace{new SpatialPoint({0.0, 0.0, 0.0})},
|
||||
UPtrAngle{new Isotropic()},
|
||||
UPtrDist{new Watt(0.988e6, 2.249e-6)}
|
||||
};
|
||||
model::external_sources.push_back(std::move(source));
|
||||
));
|
||||
}
|
||||
|
||||
// Check if we want to write out source
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ namespace openmc {
|
|||
|
||||
namespace model {
|
||||
|
||||
std::vector<SourceDistribution> external_sources;
|
||||
std::vector<std::unique_ptr<SourceDistribution>> external_sources;
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -47,7 +47,7 @@ namespace {
|
|||
|
||||
void* custom_source_library;
|
||||
std::string custom_source_parameters;
|
||||
std::unique_ptr<CustomSource> custom_source;
|
||||
std::unique_ptr<SourceDistribution> custom_source;
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -56,10 +56,10 @@ std::unique_ptr<CustomSource> custom_source;
|
|||
// SourceDistribution implementation
|
||||
//==============================================================================
|
||||
|
||||
SourceDistribution::SourceDistribution(UPtrSpace space, UPtrAngle angle, UPtrDist energy)
|
||||
IndependentSourceDistribution::IndependentSourceDistribution(UPtrSpace space, UPtrAngle angle, UPtrDist energy)
|
||||
: space_{std::move(space)}, angle_{std::move(angle)}, energy_{std::move(energy)} { }
|
||||
|
||||
SourceDistribution::SourceDistribution(pugi::xml_node node)
|
||||
IndependentSourceDistribution::IndependentSourceDistribution(pugi::xml_node node)
|
||||
{
|
||||
// Check for particle type
|
||||
if (check_for_node(node, "particle")) {
|
||||
|
|
@ -190,7 +190,7 @@ SourceDistribution::SourceDistribution(pugi::xml_node node)
|
|||
}
|
||||
|
||||
|
||||
Particle::Bank SourceDistribution::sample(uint64_t* seed) const
|
||||
Particle::Bank IndependentSourceDistribution::sample(uint64_t* seed)
|
||||
{
|
||||
Particle::Bank site;
|
||||
|
||||
|
|
@ -331,7 +331,7 @@ Particle::Bank sample_external_source(uint64_t* seed)
|
|||
// Determine total source strength
|
||||
double total_strength = 0.0;
|
||||
for (auto& s : model::external_sources)
|
||||
total_strength += s.strength();
|
||||
total_strength += s->strength();
|
||||
|
||||
// Sample from among multiple source distributions
|
||||
int i = 0;
|
||||
|
|
@ -339,13 +339,13 @@ Particle::Bank sample_external_source(uint64_t* seed)
|
|||
double xi = prn(seed)*total_strength;
|
||||
double c = 0.0;
|
||||
for (; i < model::external_sources.size(); ++i) {
|
||||
c += model::external_sources[i].strength();
|
||||
c += model::external_sources[i]->strength();
|
||||
if (xi < c) break;
|
||||
}
|
||||
}
|
||||
|
||||
// Sample source site from i-th source distribution
|
||||
Particle::Bank site {model::external_sources[i].sample(seed)};
|
||||
Particle::Bank site {model::external_sources[i]->sample(seed)};
|
||||
|
||||
// If running in MG, convert site.E to group
|
||||
if (!settings::run_CE) {
|
||||
|
|
@ -375,7 +375,7 @@ void load_custom_source_library()
|
|||
// reset errors
|
||||
dlerror();
|
||||
|
||||
// get the function to create the CustomSource from the library
|
||||
// get the function to create the custom source from the library
|
||||
auto create_custom_source = reinterpret_cast<create_custom_source_t*>(
|
||||
dlsym(custom_source_library, "openmc_create_source"));
|
||||
|
||||
|
|
@ -387,7 +387,7 @@ void load_custom_source_library()
|
|||
fatal_error(error_msg);
|
||||
}
|
||||
|
||||
// create a pointer to an instance of the CustomSource
|
||||
// create a pointer to an instance of the custom source
|
||||
custom_source = create_custom_source(custom_source_parameters);
|
||||
|
||||
#else
|
||||
|
|
@ -413,7 +413,7 @@ void close_custom_source_library()
|
|||
|
||||
Particle::Bank sample_custom_source_library(uint64_t* seed)
|
||||
{
|
||||
// sample from the instance of the CustomSource
|
||||
// sample from the instance of the custom source
|
||||
return custom_source->sample(seed);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -632,7 +632,7 @@ void Tally::accumulate()
|
|||
double total_source = 0.0;
|
||||
if (settings::run_mode == RunMode::FIXED_SOURCE) {
|
||||
for (const auto& s : model::external_sources) {
|
||||
total_source += s.strength();
|
||||
total_source += s->strength();
|
||||
}
|
||||
} else {
|
||||
total_source = 1.0;
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
#include "openmc/source.h"
|
||||
#include "openmc/particle.h"
|
||||
|
||||
class Source : openmc::CustomSource
|
||||
class Source : public openmc::SourceDistribution
|
||||
{
|
||||
openmc::Particle::Bank sample(uint64_t *seed)
|
||||
{
|
||||
|
|
@ -14,7 +14,7 @@ class Source : openmc::CustomSource
|
|||
particle.particle = openmc::Particle::Type::neutron;
|
||||
particle.wgt = 1.0;
|
||||
// position
|
||||
|
||||
|
||||
particle.r.x = 0.;
|
||||
particle.r.y = 0.;
|
||||
particle.r.z = 0.;
|
||||
|
|
@ -22,7 +22,7 @@ class Source : openmc::CustomSource
|
|||
particle.u = {1.0, 0.0, 0.0};
|
||||
particle.E = 14.08e6;
|
||||
particle.delayed_group = 0;
|
||||
return particle;
|
||||
return particle;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#include "openmc/source.h"
|
||||
#include "openmc/particle.h"
|
||||
|
||||
class Source : public openmc::CustomSource {
|
||||
class Source : public openmc::SourceDistribution {
|
||||
public:
|
||||
Source(double energy) : energy_(energy) { }
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue