Remove old source_sampling custom source method

Custom sources are now created only through the new class-based method,
which supports parameterization.
New method also slightly adjusted to create the source as managed by a
unique_ptr.
Examples and tests updated to align with this approach.
Documentation updated.
This commit is contained in:
Dan Short 2020-08-21 12:05:43 +01:00
parent 475656e6e0
commit 821e7d9825
10 changed files with 177 additions and 151 deletions

View file

@ -1,17 +1,20 @@
#include <cmath> // for M_PI
#include <memory> // for unique_ptr
#include <unordered_map>
#include "openmc/random_lcg.h"
#include "openmc/source.h"
#include "openmc/particle.h"
class ParameterizedSource : public openmc::CustomSource {
class Source : public openmc::CustomSource
{
protected:
double radius_;
double energy_;
// Protect the constructor so that the class can only be created by serialisation.
ParameterizedSource(double radius, double energy) {
// Protect the constructor as we only want the class to be created by the from_string method.
Source(double radius, double energy)
{
radius_ = radius;
energy_ = energy;
}
@ -21,9 +24,10 @@ class ParameterizedSource : public openmc::CustomSource {
double radius() { return radius_; }
double energy() { return energy_; }
// Defines a function that can create a pointer to a new instance of this class
// by deserializing from the provided string.
static ParameterizedSource* from_string(const char* parameters) {
// Defines a function that can create a unique pointer to a new instance of this class
// by extracting the parameters from the provided string.
static std::unique_ptr<Source> from_string(const char* parameters)
{
std::unordered_map<std::string, std::string> parameter_mapping;
std::stringstream ss(parameters);
@ -35,17 +39,20 @@ class ParameterizedSource : public openmc::CustomSource {
parameter_mapping[key] = value;
}
return new ParameterizedSource(std::stod(parameter_mapping["radius"]), std::stod(parameter_mapping["energy"]));
return std::unique_ptr<Source> (
new Source(std::stod(parameter_mapping["radius"]), std::stod(parameter_mapping["energy"]))
);
}
// Samples from an instance of this class.
openmc::Particle::Bank sample_source(uint64_t* seed) {
openmc::Particle::Bank sample_source(uint64_t* seed)
{
openmc::Particle::Bank particle;
// wgt
particle.particle = openmc::Particle::Type::neutron;
particle.wgt = 1.0;
// position
double angle = 2. * M_PI * openmc::prn(seed);
double angle = 2.0 * M_PI * openmc::prn(seed);
double radius = this->radius();
particle.r.x = radius * std::cos(angle);
particle.r.y = radius * std::sin(angle);
@ -59,9 +66,10 @@ class ParameterizedSource : public openmc::CustomSource {
}
};
// A function to create a pointer to an instance of this class when generated
// A function to create a unique pointer to an instance of this class when generated
// via a plugin call using dlopen/dlsym.
// You must have external C linkage here otherwise dlopen will not find the file
extern "C" ParameterizedSource* openmc_create_source(const char* parameters) {
return ParameterizedSource::from_string(parameters);
extern "C" std::unique_ptr<Source> openmc_create_source(const char* parameters)
{
return Source::from_string(parameters);
}