revision: v0.14
RNG.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2020, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #ifndef RNG_H
27 #define RNG_H
28 
29 //Used by the randomise function
30 #include <ctime>
31 #include <cmath>
32 #include <vector>
33 #include "ExtendedMath.h"
34 
38 enum class RNGType : unsigned char
39 {
40  LAGGED_FIBONACCI_GENERATOR = 0,
42 };
43 
52 class RNG
53 {
54 public:
58  RNG();
59 
64  void setRandomSeed(unsigned long int new_seed);
65 
66  void read(std::istream& is);
67 
68  void write(std::ostream& os) const;
69 
74 
76 
81  {
82  return getRandomNumber(min, max);
83  }
84 
85  /*
86  * \brief Shorthand for getRandomNumber()
87  */
89  {
90  return getRandomNumber(0.0, 1.0);
91  }
92 
98 
103 
107  unsigned int getPoissonVariate(Mdouble lambda);
108 
112  Mdouble test();
113 
117  void setLinearCongruentialGeneratorParmeters(const unsigned int a, const unsigned int c, unsigned int m);
118 
122  void randomise();
123 
127  void setLaggedFibonacciGeneratorParameters(const unsigned int p, const unsigned int q);
128 
133 
134 private:
135 
140 
145 
150 
155 
160 
164  unsigned long int a_, c_, m_;
165 
169  unsigned long int p_, q_;
170 
175 
176  /*
177  * \todo JMFT: These Box--Muller private variables are currently not
178  * read/write from .restart files. This leads to undefined (unrepeatable)
179  * behaviour when restarting if you use normal variates.
180  */
181 
186 
191 
192 };
193 
194 #endif
DPMBase::setMax
void setMax(const Vec3D &max)
Sets the maximum coordinates of the problem domain.
Definition: DPMBase.cc:1073
RNG::getNormalVariate
Mdouble getNormalVariate()
Produces a random number according to a normal distribution with mean 0 and standard deviation 1.
Definition: RNG.cc:163
RNG::c_
unsigned long int c_
Definition: RNG.h:164
RNG::savedBoxMuller_
Mdouble savedBoxMuller_
A storage space for the so-far-unused variate from the pair generated by Box–Muller.
Definition: RNG.h:190
mathsFunc::square
T square(const T val)
squares a number
Definition: ExtendedMath.h:106
DPMBase::setName
void setName(const std::string &name)
Allows to set the name of all the files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:420
StressStrainControl::StressStrainControl
StressStrainControl(const Matrix3D &stressGoal, const Matrix3D &strainRate, const Matrix3D &gainFactor, bool isStrainRateControlled)
[REV_ISO:construct]
Definition: REVIsotropicCompressionDemo.cpp:44
RNG::RNG
RNG()
default constructor
Definition: RNG.cc:35
File::setFileType
void setFileType(FileType fileType)
Sets the type of file needed to write into or read from. File::fileType_.
Definition: File.cc:215
DPMBase::setTimeStep
void setTimeStep(Mdouble newDt)
Sets a new value for the simulation time step.
Definition: DPMBase.cc:1225
DPMBase::setMin
void setMin(const Vec3D &min)
Sets the minimum coordinates of the problem domain.
Definition: DPMBase.cc:1109
constants::pi
const Mdouble pi
Definition: ExtendedMath.h:45
Flusher::FLUSH
@ FLUSH
Matrix3D::XX
Mdouble XX
all nine matrix elements
Definition: Matrix.h:43
DPMBase::setParticlesWriteVTK
void setParticlesWriteVTK(bool writeParticlesVTK)
Sets whether particles are written in a VTK file.
Definition: DPMBase.cc:934
StressStrainControl::isStrainRateControlled_
bool isStrainRateControlled_
Definition: REVIsotropicCompressionDemo.cpp:107
RNG::operator()
Mdouble operator()()
Definition: RNG.h:88
RNG::randomSeedLinearCongruentialGenerator_
unsigned long int randomSeedLinearCongruentialGenerator_
This is the initial seed of the RNG.
Definition: RNG.h:154
RNG::getPoissonVariate
unsigned int getPoissonVariate(Mdouble lambda)
Produces a random number according to a Poisson distribution.
Definition: RNG.cc:210
logger
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
mathsFunc::exp
Mdouble exp(Mdouble Exponent)
Definition: ExtendedMath.cc:84
StressStrainControl::setupInitialConditions
void setupInitialConditions() override
[REV_ISO:construct]
Definition: REVIsotropicCompressionDemo.cpp:65
RNG::read
void read(std::istream &is)
Definition: RNG.cc:58
RNG::randomSeedLaggedFibonacciGenerator_
std::vector< Mdouble > randomSeedLaggedFibonacciGenerator_
This is the seeds required for the LFG.
Definition: RNG.h:159
RNG::seedLaggedFibonacciGenerator
void seedLaggedFibonacciGenerator()
This seed the LFG.
Definition: RNG.cc:253
RNG::a_
unsigned long int a_
This are the two parameters that control the LCG random generated.
Definition: RNG.h:164
BaseHandler::getSize
unsigned int getSize() const
Gets the size of the particleHandler (including mpi and periodic particles)
Definition: BaseHandler.h:655
Mercury3D
This adds on the hierarchical grid code for 3D problems.
Definition: Mercury3D.h:37
RNG::q_
unsigned long int q_
Definition: RNG.h:169
DPMBase::restartFile
File restartFile
An instance of class File to handle in- and output into a .restart file.
Definition: DPMBase.h:1451
StressStrainControl
[REV_ISO:headers]
Definition: REVIsotropicCompressionDemo.cpp:40
INFO
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
main
int main(int argc UNUSED, char *argv[] UNUSED)
[REV_ISO:class]
Definition: REVIsotropicCompressionDemo.cpp:114
CubeInsertionBoundary
It's an insertion boundary which has cuboidal shape (yes, 'CuboidalInsertionBoundary' would have been...
Definition: CubeInsertionBoundary.h:42
mathsFunc::log
Mdouble log(Mdouble Power)
Definition: ExtendedMath.cc:104
RNG::p_
unsigned long int p_
This are the parameters that control the LFG random generator.
Definition: RNG.h:169
Vec3D
Definition: Vector.h:50
DPMBase::solve
void solve()
The work horse of the code.
Definition: DPMBase.cc:4003
StressStrainControl::stressGoal_
Matrix3D stressGoal_
[REV_ISO:setIni]
Definition: REVIsotropicCompressionDemo.cpp:104
DPMBase::fStatFile
File fStatFile
An instance of class File to handle in- and output into a .fstat file.
Definition: DPMBase.h:1441
RNG::setRandomNumberGenerator
void setRandomNumberGenerator(RNGType type)
Allows the user to set which random number generator is used.
Definition: RNG.cc:137
Helpers.h
Mdouble
double Mdouble
Definition: GeneralDefine.h:34
Matrix3D::YY
Mdouble YY
Definition: Matrix.h:43
DPMBase::boundaryHandler
BoundaryHandler boundaryHandler
An object of the class BoundaryHandler which concerns insertion and deletion of particles into or fro...
Definition: DPMBase.h:1410
char
RNG::operator()
Mdouble operator()(Mdouble min, Mdouble max)
Shorthand for getRandomNumber(min, max)
Definition: RNG.h:80
Matrix3D
Implementation of a 3D matrix.
Definition: Matrix.h:38
RNG.h
StressStrainControlBoundary
A cuboid box consists of periodic boundaries that can be strain/stress controlled and achieve differe...
Definition: StressStrainControlBoundary.h:53
RNG::randomise
void randomise()
sets the random variables such that they differ for each run
Definition: RNG.cc:97
RNG
This is a class that generates random numbers i.e. named the Random Number Generator (RNG).
Definition: RNG.h:53
double
DPMBase::getMax
Vec3D getMax() const
Definition: DPMBase.h:643
ERROR
LL< Log::ERROR > ERROR
Error log level.
Definition: Logger.cc:53
mathsFunc::sin
Mdouble sin(Mdouble x)
Definition: ExtendedMath.cc:44
StressStrainControlBoundary.h
StressStrainControlBoundary::set
void set(const Matrix3D &stressGoal, const Matrix3D &strainRate, const Matrix3D &gainFactor, bool isStrainRateControlled)
Sets all boundary inputs at once and determines which deformation mode it is, then combine the right ...
Definition: StressStrainControlBoundary.cc:316
LinearViscoelasticSpecies.h
Species< LinearViscoelasticNormalSpecies >
WARN
LL< Log::WARN > WARN
Warning log level.
Definition: Logger.cc:54
DPMBase::speciesHandler
SpeciesHandler speciesHandler
A handler to that stores the species type i.e. LinearViscoelasticSpecies, etc.
Definition: DPMBase.h:1385
RNG::setLaggedFibonacciGeneratorParameters
void setLaggedFibonacciGeneratorParameters(const unsigned int p, const unsigned int q)
This function sets the parameters for the LFG random number generator.
Definition: RNG.cc:338
InsertionBoundary::checkBoundaryBeforeTimeStep
void checkBoundaryBeforeTimeStep(DPMBase *md) override
Fills the boundary with particles.
Definition: InsertionBoundary.cc:184
SphericalParticle
A spherical particle is the most simple particle used in MercuryDPM.
Definition: SphericalParticle.h:37
StressStrainControl::gainFactor_
Matrix3D gainFactor_
Definition: REVIsotropicCompressionDemo.cpp:106
Matrix3D::ZZ
Mdouble ZZ
Definition: Matrix.h:43
RNG::getRandomNumberFromLinearCongruentialGenerator
Mdouble getRandomNumberFromLinearCongruentialGenerator(Mdouble min, Mdouble max)
This is a basic Linear Congruential Generator Random.
Definition: RNG.cc:234
DPMBase::setTimeMax
void setTimeMax(Mdouble newTMax)
Sets a new value for the maximum simulation duration.
Definition: DPMBase.cc:870
DPMBase::setGravity
void setGravity(Vec3D newGravity)
Sets a new value for the gravitational acceleration.
Definition: DPMBase.cc:1374
BaseBoundary::setHandler
void setHandler(BoundaryHandler *handler)
Sets the boundary's BoundaryHandler.
Definition: BaseBoundary.cc:134
constants::i
const std::complex< Mdouble > i
Definition: ExtendedMath.h:51
BaseHandler::getObject
T * getObject(const unsigned int id)
Gets a pointer to the Object at the specified index in the BaseHandler.
Definition: BaseHandler.h:613
RNG::write
void write(std::ostream &os) const
Definition: RNG.cc:77
DPMBase::eneFile
File eneFile
An instance of class File to handle in- and output into a .ene file.
Definition: DPMBase.h:1446
UNUSED
#define UNUSED
Definition: GeneralDefine.h:39
RNGType
RNGType
Definition: RNG.h:39
Matrix3D::XY
Mdouble XY
Definition: Matrix.h:43
DPMBase::getMin
Vec3D getMin() const
Definition: DPMBase.h:637
BaseHandler::copyAndAddObject
std::enable_if<!std::is_pointer< U >::value, U * >::type copyAndAddObject(const U &object)
Creates a copy of a Object and adds it to the BaseHandler.
Definition: BaseHandler.h:379
main
int main(int argc UNUSED, char *argv[] UNUSED)
[REV_SIM:class]
Definition: REVSimpleShearDemo.cpp:115
RNG::setRandomSeed
void setRandomSeed(unsigned long int new_seed)
This is the seed for the random number generator (note the call to seed_LFG is only required really i...
Definition: RNG.cc:52
StressStrainControl::strainRate_
Matrix3D strainRate_
Definition: REVIsotropicCompressionDemo.cpp:105
RNG::type_
RNGType type_
This is the type of random number generator.
Definition: RNG.h:174
RNG::getRandomNumber
Mdouble getRandomNumber()
This is a random generating routine can be used for initial positions.
Definition: RNG.cc:142
RNG::haveSavedBoxMuller_
bool haveSavedBoxMuller_
A flag that keeps track of whether or not to generate a new pair of normal variates (using Box–Muller...
Definition: RNG.h:185
BaseHandler::clear
virtual void clear()
Empties the whole BaseHandler by removing all Objects and setting all other variables to 0.
Definition: BaseHandler.h:528
RNG::getRandomNumberFromLaggedFibonacciGenerator
Mdouble getRandomNumberFromLaggedFibonacciGenerator(Mdouble min, Mdouble max)
This is a Lagged Fibonacci Generator.
Definition: RNG.cc:265
MPIContainer::broadcast
std::enable_if< std::is_scalar< T >::value, void >::type broadcast(T &t, int fromProcessor=0)
Broadcasts a scalar from the root to all other processors.
Definition: MpiContainer.h:441
unsigned
RNG::test
Mdouble test()
This function tests the quality of random numbers, based on the chi-squared test.
Definition: RNG.cc:289
PROCESSOR_ID
#define PROCESSOR_ID
Definition: GeneralDefine.h:63
main
int main(int argc UNUSED, char *argv[] UNUSED)
[REV_PUR:class]
Definition: REVPureShearDemo.cpp:114
DPMBase::particleHandler
ParticleHandler particleHandler
An object of the class ParticleHandler, contains the pointers to all the particles created.
Definition: DPMBase.h:1395
RNG::m_
unsigned long int m_
Definition: RNG.h:164
DPMBase::setSaveCount
void setSaveCount(unsigned int saveCount)
Sets File::saveCount_ for all files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:406
CubeInsertionBoundary.h
ExtendedMath.h
InsertionBoundary::setInitialVolume
void setInitialVolume(Mdouble initialVolume)
Gets the Volume which should be inserted by the insertion routine.
Definition: InsertionBoundary.cc:643
mathsFunc::cubic
T cubic(const T val)
calculates the cube of a number
Definition: ExtendedMath.h:115
Mercury3D.h
DPMBase::dataFile
File dataFile
An instance of class File to handle in- and output into a .data file.
Definition: DPMBase.h:1436
RNGType::LAGGED_FIBONACCI_GENERATOR
@ LAGGED_FIBONACCI_GENERATOR
CubeInsertionBoundary::set
void set(BaseParticle *particleToCopy, unsigned int maxFailed, Vec3D posMin, Vec3D posMax, Vec3D velMin, Vec3D velMax)
Sets the properties of the InsertionBoundary for mutliple different particle types.
Definition: CubeInsertionBoundary.cc:107
mathsFunc::chi_squared_prob
Mdouble chi_squared_prob(Mdouble x, unsigned int k)
This is the function which actually gives the probability back using a chi squared test.
Definition: ExtendedMath.cc:188
RNG::setLinearCongruentialGeneratorParmeters
void setLinearCongruentialGeneratorParmeters(const unsigned int a, const unsigned int c, unsigned int m)
This functions set the parameters for the LCG random number generator. It goes multiplier,...
Definition: RNG.cc:90
mathsFunc::cos
Mdouble cos(Mdouble x)
Definition: ExtendedMath.cc:64
MPIContainer::Instance
static MPIContainer & Instance()
fetch the instance to be used for communication
Definition: MpiContainer.h:134
DPMBase::setSystemDimensions
void setSystemDimensions(unsigned int newDim)
Sets the system dimensionality.
Definition: DPMBase.cc:1408