OpenMC/include/openmc/source.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

191 lines
6.1 KiB
C
Raw Permalink Normal View History

2018-08-23 21:38:23 -05:00
//! \file source.h
//! \brief External source distributions
2018-07-17 06:43:35 -05:00
#ifndef OPENMC_SOURCE_H
#define OPENMC_SOURCE_H
#include <unordered_set>
2018-07-17 06:43:35 -05:00
#include "pugixml.hpp"
#include "openmc/distribution_multi.h"
#include "openmc/distribution_spatial.h"
#include "openmc/memory.h"
2018-07-17 06:43:35 -05:00
#include "openmc/particle.h"
#include "openmc/vector.h"
2018-07-17 06:43:35 -05:00
namespace openmc {
//==============================================================================
// Constants
//==============================================================================
// Maximum number of external source spatial resamples to encounter before an
// error is thrown.
constexpr int EXTSRC_REJECT_THRESHOLD {10000};
constexpr double EXTSRC_REJECT_FRACTION {0.05};
2018-11-08 15:29:46 -06:00
//==============================================================================
// Global variables
//==============================================================================
2020-10-23 13:45:28 -05:00
class Source;
2018-11-08 15:29:46 -06:00
namespace model {
extern vector<unique_ptr<Source>> external_sources;
2018-11-08 15:29:46 -06:00
} // namespace model
2018-07-17 06:43:35 -05:00
//==============================================================================
2020-10-23 13:45:28 -05:00
//! Abstract source interface
2018-07-17 06:43:35 -05:00
//==============================================================================
2020-10-23 13:45:28 -05:00
class Source {
public:
2020-10-23 13:45:28 -05:00
virtual ~Source() = default;
// Methods that must be implemented
2021-04-29 16:23:54 -04:00
virtual SourceSite sample(uint64_t* seed) const = 0;
// Methods that can be overridden
virtual double strength() const { return 1.0; }
static unique_ptr<Source> create(pugi::xml_node node);
};
2020-10-23 13:45:28 -05:00
//==============================================================================
//! Source composed of independent spatial, angle, energy, and time
//! distributions
2020-10-23 13:45:28 -05:00
//==============================================================================
class IndependentSource : public Source {
2018-07-17 06:43:35 -05:00
public:
2018-08-23 21:38:23 -05:00
// Constructors
IndependentSource(
UPtrSpace space, UPtrAngle angle, UPtrDist energy, UPtrDist time);
2020-10-23 13:45:28 -05:00
explicit IndependentSource(pugi::xml_node node);
2018-07-17 06:43:35 -05:00
2018-08-23 21:38:23 -05:00
//! Sample from the external source distribution
//! \param[inout] seed Pseudorandom seed pointer
2018-08-23 21:38:23 -05:00
//! \return Sampled site
2021-04-29 16:23:54 -04:00
SourceSite sample(uint64_t* seed) const override;
2018-08-23 21:38:23 -05:00
// Properties
ParticleType particle_type() const { return particle_; }
double strength() const override { return strength_; }
// Make observing pointers available
SpatialDistribution* space() const { return space_.get(); }
UnitSphereDistribution* angle() const { return angle_.get(); }
Distribution* energy() const { return energy_.get(); }
Distribution* time() const { return time_.get(); }
2018-07-17 06:43:35 -05:00
private:
// Domain types
enum class DomainType { UNIVERSE, MATERIAL, CELL };
// Data members
ParticleType particle_ {ParticleType::neutron}; //!< Type of particle emitted
2018-08-23 21:38:23 -05:00
double strength_ {1.0}; //!< Source strength
UPtrSpace space_; //!< Spatial distribution
UPtrAngle angle_; //!< Angular distribution
UPtrDist energy_; //!< Energy distribution
UPtrDist time_; //!< Time distribution
DomainType domain_type_; //!< Domain type for rejection
std::unordered_set<int32_t> domain_ids_; //!< Domains to reject from
};
2020-10-23 13:45:28 -05:00
//==============================================================================
//! Source composed of particles read from a file
//==============================================================================
2020-10-23 13:45:28 -05:00
class FileSource : public Source {
public:
// Constructors
explicit FileSource(pugi::xml_node node);
explicit FileSource(const std::string& path);
// Methods
2021-04-29 16:23:54 -04:00
SourceSite sample(uint64_t* seed) const override;
void load_sites_from_file(
const std::string& path); //!< Load source sites from file
private:
2021-04-29 16:23:54 -04:00
vector<SourceSite> sites_; //!< Source sites from a file
2018-07-17 06:43:35 -05:00
};
2020-10-22 15:50:30 -05:00
//==============================================================================
//! Wrapper for custom sources that manages opening/closing shared library
//==============================================================================
class CompiledSourceWrapper : public Source {
2020-10-22 15:50:30 -05:00
public:
// Constructors, destructors
CompiledSourceWrapper(pugi::xml_node node);
~CompiledSourceWrapper();
2020-10-22 15:50:30 -05:00
// Defer implementation to custom source library
2021-04-29 16:23:54 -04:00
SourceSite sample(uint64_t* seed) const override
2020-10-22 15:50:30 -05:00
{
return compiled_source_->sample(seed);
2020-10-22 15:50:30 -05:00
}
double strength() const override { return compiled_source_->strength(); }
2021-08-11 11:41:49 -05:00
void setup(const std::string& path, const std::string& parameters);
2020-10-22 15:50:30 -05:00
private:
void* shared_library_; //!< library from dlopen
unique_ptr<Source> compiled_source_;
2020-10-22 15:50:30 -05:00
};
typedef unique_ptr<Source> create_compiled_source_t(std::string parameters);
//==============================================================================
//! Mesh-based source with different distributions for each element
//==============================================================================
class MeshSource : public Source {
public:
// Constructors
explicit MeshSource(pugi::xml_node node);
//! Sample from the external source distribution
//! \param[inout] seed Pseudorandom seed pointer
//! \return Sampled site
SourceSite sample(uint64_t* seed) const override;
// Properties
double strength() const override { return space_->total_strength(); }
// Accessors
const std::unique_ptr<Source>& source(int32_t i) const
{
return sources_.size() == 1 ? sources_[0] : sources_[i];
}
private:
// Data members
unique_ptr<MeshSpatial> space_; //!< Mesh spatial
vector<std::unique_ptr<Source>> sources_; //!< Source distributions
};
2018-07-17 06:43:35 -05:00
//==============================================================================
// Functions
//==============================================================================
//! Initialize source bank from file/distribution
extern "C" void initialize_source();
//! Sample a site from all external source distributions in proportion to their
//! source strength
//! \param[inout] seed Pseudorandom seed pointer
2018-08-23 21:38:23 -05:00
//! \return Sampled source site
2021-04-29 16:23:54 -04:00
SourceSite sample_external_source(uint64_t* seed);
2018-07-17 06:43:35 -05:00
2019-02-21 08:32:32 -06:00
void free_memory_source();
2018-07-17 06:43:35 -05:00
} // namespace openmc
#endif // OPENMC_SOURCE_H