Requested updates to source

Rename sample_source -> sample.
Use std::string as type of argument to openmc_create_source.
Use reinterpret_cast when accessing dlsym.
Also moves check for dlerror to be before the attempt to create the
source (avoids null reference if fails) and capture error message
before dlclose, so that the error message is still available.
Formatting updates and some refactoring for examples and tests.
This commit is contained in:
Dan Short 2020-08-28 09:49:36 +01:00
parent 9435020197
commit 21e8d91e90
6 changed files with 40 additions and 49 deletions

View file

@ -1,18 +1,12 @@
#include "openmc/source.h"
#include "openmc/particle.h"
class Source : public openmc::CustomSource
{
class Source : public openmc::CustomSource {
public:
double energy;
Source(double energy)
{
this->energy = energy;
}
Source(double energy) : energy_(energy) { }
// Samples from an instance of this class.
openmc::Particle::Bank sample_source(uint64_t* seed)
openmc::Particle::Bank sample(uint64_t* seed)
{
openmc::Particle::Bank particle;
// wgt
@ -24,17 +18,21 @@ class Source : public openmc::CustomSource
particle.r.z = 0.0;
// angle
particle.u = {1.0, 0.0, 0.0};
particle.E = this->energy;
particle.E = this->energy_;
particle.delayed_group = 0;
return particle;
}
private:
double energy_;
};
// 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" std::unique_ptr<Source> openmc_create_source(const char* parameter)
extern "C" std::unique_ptr<Source> openmc_create_source(std::string parameter)
{
return std::unique_ptr<Source> (new Source(atof(parameter)));
double energy = std::stod(parameter);
return std::make_unique<Source>(energy);
}