From 713469bf59143e9b8fae7dfb3e4bf4f1e86b3769 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Thu, 22 Oct 2020 15:05:43 -0500 Subject: [PATCH] Make regular source distributions obey the same interface as custom sources --- examples/custom_source/source_ring.cpp | 2 +- .../parameterized_source_ring.cpp | 3 +- include/openmc/source.h | 30 +++++++++++-------- src/settings.cpp | 7 ++--- src/source.cpp | 22 +++++++------- src/tallies/tally.cpp | 2 +- .../source_dlopen/source_sampling.cpp | 6 ++-- .../parameterized_source_sampling.cpp | 2 +- 8 files changed, 38 insertions(+), 36 deletions(-) diff --git a/examples/custom_source/source_ring.cpp b/examples/custom_source/source_ring.cpp index bc8a8a7f8..a23250db1 100644 --- a/examples/custom_source/source_ring.cpp +++ b/examples/custom_source/source_ring.cpp @@ -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) { diff --git a/examples/parameterized_custom_source/parameterized_source_ring.cpp b/examples/parameterized_custom_source/parameterized_source_ring.cpp index c88333e15..8668fd426 100644 --- a/examples/parameterized_custom_source/parameterized_source_ring.cpp +++ b/examples/parameterized_custom_source/parameterized_source_ring.cpp @@ -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) { } diff --git a/include/openmc/source.h b/include/openmc/source.h index e47c91851..f1fe40a39 100644 --- a/include/openmc/source.h +++ b/include/openmc/source.h @@ -23,7 +23,7 @@ class SourceDistribution; namespace model { -extern std::vector external_sources; +extern std::vector> external_sources; } // namespace model @@ -32,19 +32,30 @@ extern std::vector 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 sites_; //!< Source sites from a file }; -class CustomSource { - public: - virtual ~CustomSource() {} - - virtual Particle::Bank sample(uint64_t* seed) = 0; -}; - -typedef std::unique_ptr create_custom_source_t(std::string parameters); +typedef std::unique_ptr create_custom_source_t(std::string parameters); //============================================================================== // Functions diff --git a/src/settings.cpp b/src/settings.cpp index cab7c6c11..8b0cd5300 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -420,17 +420,16 @@ void read_settings_xml() // Get point to list of 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(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( 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 diff --git a/src/source.cpp b/src/source.cpp index 91a5f78cc..dc75a0c81 100644 --- a/src/source.cpp +++ b/src/source.cpp @@ -39,7 +39,7 @@ namespace openmc { namespace model { -std::vector external_sources; +std::vector> external_sources; } @@ -47,7 +47,7 @@ namespace { void* custom_source_library; std::string custom_source_parameters; -std::unique_ptr custom_source; +std::unique_ptr custom_source; } @@ -56,10 +56,10 @@ std::unique_ptr 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( 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); } diff --git a/src/tallies/tally.cpp b/src/tallies/tally.cpp index d5cc7dfb6..038bcec64 100644 --- a/src/tallies/tally.cpp +++ b/src/tallies/tally.cpp @@ -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; diff --git a/tests/regression_tests/source_dlopen/source_sampling.cpp b/tests/regression_tests/source_dlopen/source_sampling.cpp index ea74afdd9..4425af5a9 100644 --- a/tests/regression_tests/source_dlopen/source_sampling.cpp +++ b/tests/regression_tests/source_dlopen/source_sampling.cpp @@ -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; } }; diff --git a/tests/regression_tests/source_parameterized_dlopen/parameterized_source_sampling.cpp b/tests/regression_tests/source_parameterized_dlopen/parameterized_source_sampling.cpp index 3b0875bb0..3f9e87639 100644 --- a/tests/regression_tests/source_parameterized_dlopen/parameterized_source_sampling.cpp +++ b/tests/regression_tests/source_parameterized_dlopen/parameterized_source_sampling.cpp @@ -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) { }