IAP GITLAB

Skip to content
Snippets Groups Projects
Commit e9be64c2 authored by Antonio Augusto Alves Junior's avatar Antonio Augusto Alves Junior Committed by ralfulrich
Browse files

[refactory-2020] Refinements: documentation, typedefs, inline hints etc

parent f3e67a65
No related branches found
No related tags found
No related merge requests found
Showing
with 249 additions and 59 deletions
......@@ -12,8 +12,8 @@
namespace corsika {
inline void RNGManager::registerRandomStream(std::string const& pStreamName) {
RNG rng;
inline void RNGManager::registerRandomStream(string_type const& pStreamName) {
prng_type rng;
if (auto const& it = seeds_.find(pStreamName); it != seeds_.end()) {
rng.seed(it->second);
......@@ -22,7 +22,7 @@ namespace corsika {
rngs_[pStreamName] = std::move(rng);
}
inline RNG& RNGManager::getRandomStream(std::string const& pStreamName) {
inline RNGManager::prng_type& RNGManager::getRandomStream(string_type const& pStreamName) {
if (isRegistered(pStreamName)) {
return rngs_.at(pStreamName);
} else { // this stream name is not in the map
......@@ -31,7 +31,7 @@ namespace corsika {
}
inline bool RNGManager::isRegistered(std::string const& pStreamName) const {
inline bool RNGManager::isRegistered(string_type const& pStreamName) const {
return rngs_.count(pStreamName) > 0;
}
......@@ -45,7 +45,7 @@ namespace corsika {
return buffer;
}
inline void RNGManager::seedAll(uint64_t vSeed) {
inline void RNGManager::seedAll(seed_type vSeed) {
for (auto& entry : rngs_) { entry.second.seed(vSeed++); }
}
......
......@@ -29,7 +29,7 @@ datadir::datadir(const std::string& dir) {
}
double qgran_(int&) {
static corsika::RNG& rng =
static corsika::default_prng_type& rng =
corsika::RNGManager::getInstance().GetRandomStream("qgran");
std::uniform_real_distribution<double> dist;
......
......@@ -110,7 +110,7 @@ namespace corsika {
TTracking& fTracking;
TProcessList& fProcessSequence;
TStack& fStack;
corsika::RNG& fRNG = corsika::RNGManager::getInstance().getRandomStream("cascade");
corsika::default_prng_type& fRNG = corsika::RNGManager::getInstance().getRandomStream("cascade");
};
} // namespace corsika
......
......@@ -12,26 +12,74 @@ n/*
#include <random>
namespace corsika {
// FIXME: This whole facility needs to re-designed.
// It is not parallel friendly neither polymorphic
// and the streaming management is prone to produce
// huge correlation between the streams
template <class TQuantity>
class ExponentialDistribution {
using RealType = typename TQuantity::value_type;
std::exponential_distribution<RealType> dist_{1.};
TQuantity const beta_;
using RealType = typename TQuantity::value_type;
typedef std::exponential_distribution<RealType> distribution_type;
typedef TQuantity quantity_type;
public:
ExponentialDistribution(TQuantity beta)
: beta_(beta) {}
ExponentialDistribution()=delete;
ExponentialDistribution(quantity_type vBeta)
: beta_(vBeta) {}
ExponentialDistribution(ExponentialDistribution<quantity_type>const& other):
beta_(other.getBeta())
{}
ExponentialDistribution<quantity_type>&
operator=(ExponentialDistribution<quantity_type>const& other){
if( this == &other) return *this;
beta_ = other.getBeta();
return *this;
}
/**
* @fn quantity_type getBeta()const
* @brief Get parameter of exponential distribution \f[ \beta e^{-X}\f]
* @pre
* @post
* @return quantity_type
*/
quantity_type getBeta() const {
return beta_;
}
/**
* @fn void setBeta(quantity_type)
* @brief Set parameter of exponential distribution \f[ \beta e^{-X}\f]
*
* @pre
* @post
* @param vBeta
*/
void setBeta(quantity_type vBeta) {
beta_ = vBeta;
}
/**
* @fn quantity_type operator ()(Generator&)
* @brief Generate a random number distributed like \f[ \beta e^{-X}\f]
*
* @pre
* @post
* @tparam Generator
* @param g
* @return
*/
template <class Generator>
TQuantity operator()(Generator& g) {
quantity_type operator()(Generator& g) {
return beta_ * dist_(g);
}
private:
distribution_type dist_{1.};
quantity_type beta_;
};
} // namespace corsika
......@@ -8,70 +8,134 @@
#pragma once
#include <corsika/framework/utility/Singleton.hpp>
#include <corsika/framework/logging/Logging.h>
#include <map>
#include <cstdint>
#include <random>
#include <string>
#include <corsika/framework/utility/Singleton.hpp>
#include <corsika/framework/logging/Logging.h>
/*!
* With this class modules can register streams of random numbers.
*/
namespace corsika {
// FIXME: This while facility needs to re-designed.
// It is not parallel friendly neither polymorphic
// and the streaming management is prone to produce
// huge correlation between the streams
using RNG = std::mt19937; //!< the actual RNG type that will be used
class RNGManager : public corsika::Singleton<RNGManager> {
friend class corsika::Singleton<RNGManager>;
std::map<std::string, RNG> rngs_;
std::map<std::string, std::seed_seq> seeds_;
public:
protected:
RNGManager() {} // why ?
typedef std::mt19937_64 prng_type;
typedef std::uint64_t seed_type;
typedef std::string string_type;
typedef std::map<std::string, prng_type> streams_type;
typedef std::map<std::string, std::seed_seq> seeds_type;
RNGManager( RNGManager const& ) = default;
RNGManager& operator=(RNGManager const& ) = delete;
public:
/*!
* This function is to be called by a module requiring a random-number
* stream during its initialization.
*
* \throws sth. when stream \a pModuleName is already registered
*/
void registerRandomStream(std::string const& pStreamName);
inline void registerRandomStream(string_type const& vStreamName);
/*!
* returns the pre-stored stream of given name \a pStreamName if
* available
*/
RNG& getRandomStream(std::string const& pStreamName);
inline prng_type& getRandomStream(string_type const& vStreamName);
/*!
* Check whether a stream has been registered.
*/
bool isRegistered(std::string const& pStreamName) const;
inline bool isRegistered(string_type const& vStreamName) const;
/*!
* dumps the names and states of all registered random-number streams
* into a std::stringstream.
*/
std::stringstream dumpState() const;
inline std::stringstream dumpState() const;
/**
* Set explicit seeds for all currently registered streams. The actual seed values
* are incremented from \a vSeed.
*/
void seedAll(uint64_t vSeed);
inline void seedAll(seed_type vSeed);
/**
* Set seeds for all currently registered streams.
*/
inline void seedAll(void); //!< seed all currently registered streams with "real" randomness
void seedAll(void); //!< seed all currently registered streams with "real" randomness
/**
* @fn const streams_type getRngs&()const
* @brief Constant access to the streams.
*
* @pre
* @post
* @return RNGManager::streams_type
*/
inline const streams_type& getRngs() const {
return rngs_;
}
/**
* @fn const seeds_type getSeeds&()const
* @brief Constant access to the seeds.
*
* @pre
* @post
* @return RNGManager::seeds_type
*/
inline const seeds_type& getSeeds() const {
return seeds_;
}
/**
* @fn streams_type Rngs()
* @brief Non-constant access to the streams.
*
* @pre
* @post
* @return RNGManager::streams_type&
*/
inline streams_type Rngs() {
return rngs_;
}
/**
* @fn seeds_type Seeds&()
* @brief Non-constant access to seeds.
*
* @pre
* @post
* @return RNGManager::seeds_type&
*/
inline seeds_type& Seeds() {
return seeds_;
}
protected:
RNGManager()=default;
private:
streams_type rngs_;
seeds_type seeds_;
};
typedef typename RNGManager::prng_type default_prng_type;
} // namespace corsika
#include <corsika/detail/framework/random/RNGManager.inl>
......@@ -13,30 +13,108 @@
namespace corsika {
// FIXME: This while facility needs to re-designed.
// It is not parallel friendly neither polymorphic
// and the streaming management is prone to produce
// huge correlation between the streams
template <class TQuantity>
class UniformRealDistribution {
using RealType = typename TQuantity::value_type;
std::uniform_real_distribution<RealType> dist_{RealType(0.), RealType(1.)};
TQuantity const min_, max_;
typedef std::uniform_real_distribution<RealType> distribution_type;
public:
typedef TQuantity quantity_type;
UniformRealDistribution() = delete;
UniformRealDistribution(TQuantity b)
: min_{TQuantity(phys::units::detail::magnitude_tag, 0)}
: min_{quantity_type(phys::units::detail::magnitude_tag, 0)}
, max_(b) {}
UniformRealDistribution(TQuantity a, TQuantity b)
: min_(a)
, max_(b) {}
UniformRealDistribution(quantity_type vMin, quantity_type vMax)
: min_(vMin)
, max_(vMax) {}
UniformRealDistribution(UniformRealDistribution<quantity_type> const& other):
min_(other.getMin())
, max_(other.getMax())
{}
inline UniformRealDistribution<quantity_type>&
operator=(UniformRealDistribution<quantity_type> const& other){
if( this == &other) return *this;
min_ = other.getMin();
max_ = other.getMax();
return *this;
}
/**
* @fn quantity_type getMax()const
* @brief Get the upper limit.
*
* @pre
* @post
* @return quantity_type
*/
inline quantity_type getMax() const {
return max_;
}
/**
* @fn void setMax(quantity_type)
* @brief Set the upper limit.
*
* @pre
* @post
* @param vMax
*/
inline void setMax(quantity_type vMax) {
max_ = vMax;
}
/**
* @fn quantity_type getMin()const
* @brief Get the lower limit.
*
* @pre
* @post
* @return
*/
inline quantity_type getMin() const {
return min_;
}
/**
* @fn void setMin(quantity_type)
* @brief Set the lower limit.
*
* @pre
* @post
* @param vMin
*/
inline void setMin(quantity_type vMin) {
min_ = vMin;
}
/**
* @fn quantity_type operator ()(Generator&)
* @brief Generate a random numberin the range [min, max]
*
* @pre
* @post
* @tparam Generator
* @param g
* @return quantity_type
*/
template <class Generator>
TQuantity operator()(Generator& g) {
inline quantity_type operator()(Generator& g) {
return min_ + dist_(g) * (max_ - min_);
}
private:
distribution_type dist_{RealType(0.), RealType(1.)};
quantity_type min_;
quantity_type max_;
};
} // namespace corsika
......@@ -39,7 +39,7 @@ namespace corsika::hadronic_elastic_model {
using eV2 = decltype(square(electronvolt));
using inveV2 = decltype(1 / square(electronvolt));
corsika::RNG& fRNG =
corsika::default_prng_type& fRNG =
corsika::RNGManager::getInstance().getRandomStream("HadronicElasticModel");
inveV2 B(eV2 s) const;
......
......@@ -55,7 +55,7 @@ namespace corsika::pythia8 {
corsika::EProcessReturn DoInteraction(TProjectile&);
private:
corsika::RNG& fRNG = corsika::RNGManager::getInstance().getRandomStream("pythia");
corsika::default_prng_type& fRNG = corsika::RNGManager::getInstance().getRandomStream("pythia");
Pythia8::Pythia fPythia;
Pythia8::SigmaTotal fSigma;
const bool fInternalDecays = true;
......
......@@ -19,7 +19,7 @@ namespace corsika::pythia8 {
private:
std::uniform_real_distribution<double> fDist;
corsika::RNG& fRNG = corsika::RNGManager::getInstance().getRandomStream("pythia");
corsika::default_prng_type& fRNG = corsika::RNGManager::getInstance().getRandomStream("pythia");
};
} // namespace corsika::pythia8
......
......@@ -55,7 +55,7 @@ namespace corsika::qgsjetII {
corsika::EProcessReturn DoInteraction(TProjectile&);
private:
corsika::RNG& fRNG = corsika::RNGManager::getInstance().getRandomStream("qgran");
corsika::default_prng_type& fRNG = corsika::RNGManager::getInstance().getRandomStream("qgran");
const int maxMassNumber_ = 208;
};
......
......@@ -14,7 +14,7 @@
namespace qgsjetII {
double rndm_interface() {
static corsika::RNG& rng =
static corsika::default_prng_type& rng =
corsika::RNGManager::getInstance().getRandomStream("qgran");
std::uniform_real_distribution<double> dist;
return dist(rng);
......
......@@ -65,7 +65,7 @@ namespace corsika::sibyll {
corsika::EProcessReturn DoInteraction(TProjectile&);
private:
corsika::RNG& RNG_ = corsika::RNGManager::getInstance().getRandomStream("s_rndm");
corsika::default_prng_type& RNG_ = corsika::RNGManager::getInstance().getRandomStream("s_rndm");
// FOR NOW keep trackedParticles private, could be configurable
std::vector<corsika::Code> const trackedParticles_ = {
corsika::Code::PiPlus, corsika::Code::PiMinus, corsika::Code::Pi0,
......
......@@ -56,7 +56,7 @@ namespace corsika::sibyll {
TEnvironment const& environment_;
corsika::sibyll::Interaction& hadronicInteraction_;
std::map<corsika::Code, int> targetComponentsIndex_;
corsika::RNG& RNG_ = corsika::RNGManager::getInstance().getRandomStream("s_rndm");
corsika::default_prng_type& RNG_ = corsika::RNGManager::getInstance().getRandomStream("s_rndm");
static constexpr unsigned int gNSample_ =
500; // number of samples in MC estimation of cross section
static constexpr unsigned int gMaxNucleusAProjectile_ = 56;
......
......@@ -14,7 +14,7 @@
namespace sibyll {
double rndm_interface() {
static corsika::RNG& rng =
static corsika::default_prng_type& rng =
corsika::RNGManager::getInstance().getRandomStream("s_rndm");
std::uniform_real_distribution<double> dist;
return dist(rng);
......
......@@ -17,7 +17,7 @@ namespace urqmd {
* the random number generator function of UrQMD
*/
double rndm_interface() {
static corsika::RNG& rng =
static corsika::default_prng_type& rng =
corsika::RNGManager::getInstance().getRandomStream("UrQMD");
static std::uniform_real_distribution<double> dist;
return dist(rng);
......
......@@ -38,7 +38,7 @@ namespace corsika::urqmd {
private:
static CrossSectionType GetCrossSection(corsika::Code, corsika::Code, HEPEnergyType,
int);
corsika::RNG& fRNG = corsika::RNGManager::getInstance().getRandomStream("UrQMD");
corsika::default_prng_type& fRNG = corsika::RNGManager::getInstance().getRandomStream("UrQMD");
std::uniform_int_distribution<int> fBooleanDist{0, 1};
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment