Changeset 3604 in Sophya for trunk/SophyaLib/BaseTools


Ignore:
Timestamp:
Apr 29, 2009, 12:14:04 PM (16 years ago)
Author:
ansari
Message:

Modifs,petites extensions + numero de version/initialisation de l'instance globale de RandomGenerator, Reza 29/04/2009

Location:
trunk/SophyaLib/BaseTools
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/BaseTools/randinterf.cc

    r3602 r3604  
    1414//-------------------------------------------------------------------------------
    1515// ------ Definition d'interface des classes de generateurs de nombres aleatoires
     16/*!
     17   \class RandomGeneratorInterface
     18   \ingroup BaseTools
     19   \brief Base class for random number generators
     20
     21   This class defines the interface for random number generator classes and
     22   implements the generation of some specific distributions (Gaussian, Poisson ...)
     23   through generation of random number with a flat distribution in the range [0,1[.
     24
     25   The sub classes inheriting from this class should implement the Next() method.
     26
     27   This base class manages also a global instance of a default generator.
     28
     29   \sa frand01 drand01 frandpm1 drandpm1
     30   \sa GauRnd PoissRand
     31
     32*/
     33
     34
     35RandomGeneratorInterface* RandomGeneratorInterface::gl_rndgen_p = NULL;
     36
     37/*!
     38   \brief: static method to set or change the intance of the global Random Generator object
     39
     40   This method should be called during initialization, before any call to global
     41   functions for random number generation. The rgp object should be created using new.
     42*/
     43void RandomGeneratorInterface::SetGlobalRandGenP(RandomGeneratorInterface* rgp)
     44{
     45  if (rgp == NULL) return;
     46  if (gl_rndgen_p) delete gl_rndgen_p;
     47  gl_rndgen_p = rgp;
     48  return;
     49}
    1650
    1751RandomGeneratorInterface::RandomGeneratorInterface()
    18   : usegaussian_(0), usepoisson_(0), useexpo_(0)
    19 {
     52{
     53  SelectGaussianAlgo();
     54  SelectPoissonAlgo();
     55  SelectExponentialAlgo();
    2056}
    2157 
     
    2662}
    2763
    28 void RandomGeneratorInterface::UseGaussian(uint_2 type)
    29 {
    30   usegaussian_ = type;
    31 }
    32 
    33 void RandomGeneratorInterface::UseExpo(uint_2 type)
    34 {
    35   useexpo_ = type;
    36 }
    37 
    38 void RandomGeneratorInterface::UsePoisson(uint_2 type)
    39 {
    40   usepoisson_ = type;
    41 }
    4264
    4365/////////////////////////////////////////////////////////////////////////
     
    150172}
    151173
    152 
    153 /////////////////////////////////////////////////////////////////////////
    154 /////////////////////////////////////////////////////////////////////////
    155 /////////////////////////////////////////////////////////////////////////
     174/////////////////////////////////////////////////////////////////////////
     175/////////////////////////////////////////////////////////////////////////
     176/////////////////////////////////////////////////////////////////////////
     177
     178r_8 RandomGeneratorInterface::Gaussian()
     179{
     180  switch (usegaussian_) {
     181    case C_Gaussian_BoxMuller :
     182      return GaussianBoxMuller();
     183      break;
     184    case C_Gaussian_RandLibSNorm :
     185      return GaussianSNorm();
     186      break;
     187    case C_Gaussian_PolarBoxMuller :
     188      return GaussianPolarBoxMuller();
     189      break;
     190    case C_Gaussian_RatioUnif :
     191      return GaussianRatioUnif();
     192      break;
     193    case C_Gaussian_LevaRatioUnif :
     194      return GaussianLevaRatioUnif();
     195      break;
     196    default:
     197      return GaussianBoxMuller();
     198      break;
     199  }
     200}
    156201
    157202//--- Generation de nombre aleatoires suivant une distribution gaussienne
     
    374419/////////////////////////////////////////////////////////////////////////
    375420
     421uint_8 RandomGeneratorInterface::Poisson(double mu, double mumax)
     422{
     423  switch (usepoisson_) {
     424    case C_Poisson_Simple :
     425      return PoissonSimple(mu,mumax);
     426      break;
     427    case C_Poisson_Ahrens :
     428      return PoissonAhrens(mu);
     429      break;
     430    default:
     431      return PoissonSimple(mu,mumax);
     432      break;
     433  }
     434}
     435
     436
    376437//--- Generation de nombre aleatoires suivant une distribution de Poisson
    377438uint_8 RandomGeneratorInterface::PoissonSimple(double mu,double mumax)
     
    634695/////////////////////////////////////////////////////////////////////////
    635696/////////////////////////////////////////////////////////////////////////
     697
     698r_8 RandomGeneratorInterface::Exponential()
     699{
     700  switch (useexpo_) {
     701    case C_Exponential_Simple :
     702      return ExpoSimple();
     703      break;
     704    case C_Exponential_Ahrens :
     705      return ExpoAhrens();
     706      break;
     707    default:
     708      return ExpoSimple();
     709      break;
     710  }
     711}
    636712
    637713r_8 RandomGeneratorInterface::ExpoSimple(void)
  • trunk/SophyaLib/BaseTools/randinterf.h

    r3602 r3604  
    1515namespace SOPHYA {
    1616
    17 //! Definition of the interface for Random number generator classes
     17//! enum definition for the type of algorithm used for Gaussian distribution generation
     18enum GaussianGenAlgo {
     19  C_Gaussian_BoxMuller = 0,
     20  C_Gaussian_RandLibSNorm = 1,
     21  C_Gaussian_PolarBoxMuller = 2,
     22  C_Gaussian_RatioUnif = 3,
     23  C_Gaussian_LevaRatioUnif = 4
     24};
     25
     26//! enum definition for the type of algorithm used for Poisson distribution generation
     27enum PoissonGenAlgo {
     28  C_Poisson_Simple = 0,
     29  C_Poisson_Ahrens = 1
     30};
     31
     32//! enum definition for the type of algorithm used for exponential distribution generation
     33enum ExponentialGenAlgo {
     34  C_Exponential_Simple = 0,
     35  C_Exponential_Ahrens = 1
     36};
     37
     38// Definition de l'interface pour les classes generateur aleatoire
    1839class RandomGeneratorInterface : public AnyDataObj {
    1940public:
     
    2142  virtual ~RandomGeneratorInterface();
    2243
    23   void UseGaussian(uint_2 type=0);
    24   void UsePoisson(uint_2 type=0);
    25   void UseExpo(uint_2 type=0);
     44//! Select Gaussian generation algorithm
     45  inline void SelectGaussianAlgo(GaussianGenAlgo typ=C_Gaussian_BoxMuller)
     46    {  usegaussian_ = typ; }
     47//! Select Poisson generation algorithm
     48  inline void SelectPoissonAlgo(PoissonGenAlgo typ=C_Poisson_Simple)
     49    {  usepoisson_ = typ; }
     50//! Select Exponential generation algorithm
     51  inline void SelectExponentialAlgo(ExponentialGenAlgo typ=C_Exponential_Simple)
     52    {  useexpo_ = typ; }
    2653
    27   void GenerateSeedVector(int nseed,vector<uint_2>& seed,int lp=0);
     54//! Return the Gaussian generation algorithm type
     55  inline GaussianGenAlgo GetGaussianAlgo()
     56    {  return usegaussian_; }
     57//! Return the Poisson generation algorithm type
     58  inline PoissonGenAlgo GetPoissonAlgo()
     59    {  return usepoisson_; }
     60//! Return the Exponential generation algorithm type
     61//! Select Exponential generation algorithm
     62  inline ExponentialGenAlgo GetExponentialAlgo()
     63    {  return useexpo_; }
     64
     65  virtual void GenerateSeedVector(int nseed,vector<uint_2>& seed,int lp=0);
    2866
    2967  // --- Le tirage sur une distribution plate
     
    3573  // --- Le tirage sur une distribution gaussienne
    3674  /*! \brief Return a random number following a gaussian distribution (sigma=1, mean=0)*/
    37   inline r_8 Gaussian()
    38          {
    39            if(usegaussian_==0) return GaussianBoxMuller();
    40            if(usegaussian_==1) return GaussianSNorm();
    41            if(usegaussian_==2) return GaussianPolarBoxMuller();
    42            if(usegaussian_==3) return GaussianRatioUnif();
    43            if(usegaussian_==4) return GaussianLevaRatioUnif();
    44            return GaussianBoxMuller();
    45          }
     75  virtual r_8 Gaussian();
     76
    4677  virtual r_8 GaussianBoxMuller();
    4778  virtual r_8 GaussianSNorm();
     
    5990  // --- Le tirage sur une distribution de poisson
    6091  /*! \brief Return a random number following a poisson distribution with mean mu */
    61   inline uint_8 Poisson(double mu, double mumax)
    62          {
    63            if(usepoisson_==0) return PoissonSimple(mu,mumax);
    64            if(usepoisson_==1) return PoissonAhrens(mu);
    65            return PoissonSimple(mu,mumax);
    66          }
     92  virtual uint_8 Poisson(double mu, double mumax=-1);
    6793  virtual uint_8 PoissonSimple(double mu, double mumax=-1);
    6894  virtual uint_8 PoissonAhrens(double mu);
    6995
    70   // --- Le tirage sur une distribution exponentielle
    71   inline r_8 Expo()
    72          {
    73            if(useexpo_==0) return ExpoSimple();
    74            if(useexpo_==1) return ExpoAhrens();
    75            return ExpoSimple();
    76          }
     96  //! Return a random number with exponential distribution
     97  virtual r_8 Exponential();
     98  inline r_8 Expo() { return  Exponential(); }
     99
    77100  virtual r_8 ExpoSimple();
    78101  virtual r_8 ExpoAhrens();
     
    87110         {double r=-log(1.-Next()); return sig*sqrt(2.*r);}
    88111
     112//! Return the pointer to the default global RandomGenerator object
     113  static inline RandomGeneratorInterface* GetGlobalRandGenP()
     114    { return gl_rndgen_p; }
     115
     116// Permet de definir l'instance global du generateur aleatoire
     117  static void SetGlobalRandGenP(RandomGeneratorInterface* rgp);
     118
    89119protected:
    90120  //! Return a random number in [0,1]
    91121  virtual r_8 Next();
    92   uint_2 usegaussian_, usepoisson_, useexpo_;
    93  
     122
     123// Selection de type d'algo pour les aleatoires autres que plates
     124  GaussianGenAlgo usegaussian_;
     125  PoissonGenAlgo usepoisson_;
     126  ExponentialGenAlgo useexpo_;
     127
     128  // Instance global d'un generateur par defaut
     129  static RandomGeneratorInterface* gl_rndgen_p;
    94130};
    95131
  • trunk/SophyaLib/BaseTools/randr48.cc

    r3602 r3604  
    157157}
    158158
     159
    159160void ThSDR48RandGen::GenSeq(void)
    160161{
     
    163164  ths_rand->unlock();
    164165  idx_ = 0;
     166}
     167 
     168r_8 ThSDR48RandGen::Next()
     169{
     170  if (rseq_.Size() == 0)  return drand48();
     171  else {
     172    if(idx_==rseq_.Size()) GenSeq();
     173    return(rseq_(idx_++));
     174  }
    165175}
    166176
  • trunk/SophyaLib/BaseTools/randr48.h

    r3602 r3604  
    1616namespace SOPHYA {
    1717
    18 //! Pseudorandom number generator class using dSFMT code
     18//! Implementation of the RandomGeneratorInterface class using drand48() functions
    1919class DR48RandGen : public RandomGeneratorInterface {
    2020
     
    3939//--------------------------------------------------------------------------------
    4040
     41//! Version thread-safe de RandomGeneratorInterface avec drand48() functions
    4142class ThSDR48RandGen : public DR48RandGen {
    4243
     
    6263  // ---- protected methods
    6364  void GenSeq();
    64   inline r_8 Next()
    65     {
    66       if (rseq_.Size() == 0)  return drand48();
    67       else {
    68         if(idx_==rseq_.Size()) GenSeq();
    69         return(rseq_(idx_++));
    70       }
    71     }
     65  virtual r_8 Next();
     66
    7267  // Non thread-safe version of Init() and GetSeed()
    7368  void SetSeed_P(uint_2 seed[3]);
  • trunk/SophyaLib/BaseTools/sophyainit.cc

    r3602 r3604  
    2323
    2424#include "sversion.h"
     25
     26#include "randr48.h"
    2527
    2628#include <iostream>
     
    3436//  - Nettoyage machdefs_mkmf.h
    3537//  - Ajout classe ThSafeOp ---> NDataBlock<T> Sw/SegDataBlock<T> ThreadSafe
    36 // Module version number - 2.15 ,  Oct08     
    37 #define MOD_VERS   2.15
     38// Module version number - 2.15 ,  Oct08
     39//  - Extension / ajout de classes generateur aleatoires (Mersenne-Twister ...)     
     40// Module version number - 2.20 ,  Avr09     
     41#define MOD_VERS   2.20
    3842
    3943// Pour garder la liste des modules et leurs numeros de version
     
    6468
    6569  ModListP = new map<string, double>;
    66  
     70
     71  // --- Les generateurs aleatoires ...
     72  DR48RandGen* grgp = new  DR48RandGen;
     73  RandomGeneratorInterface::SetGlobalRandGenP(grgp);
    6774
    6875  // Initialisation des mecanismes PPF I/O
  • trunk/SophyaLib/BaseTools/sversion.h

    r3572 r3604  
    33
    44#define SOPHYA_VERSION   2.1
    5 #define SOPHYA_REVISION  30
    6 #define SOPHYA_TAG       "V_Fev2009"
     5#define SOPHYA_REVISION  35
     6#define SOPHYA_TAG       "V_Avr2009"
    77
    88#endif
Note: See TracChangeset for help on using the changeset viewer.