queso-0.51.1
RngGsl.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008,2009,2010,2011,2012,2013 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include <queso/RngGsl.h>
26 #include <gsl/gsl_randist.h>
27 #include <mpi.h>
28 
29 namespace QUESO {
30 
31 // Default constructor ------------------------------
33  :
34  RngBase()
35 {
38  "RngGsl::constructor(), default",
39  "should not be used by user");
40 }
41 
43 RngGsl::RngGsl(int seed, int worldRank)
44  :
45  RngBase(seed,worldRank),
46  m_rng (NULL)
47 {
48  gsl_rng_default_seed = (unsigned long int) m_seed;
49  m_rng = gsl_rng_alloc(gsl_rng_ranlxd2);
50  UQ_FATAL_TEST_MACRO((m_rng == NULL),
52  "RngGsl::constructor()",
53  "null m_rng");
54 
55 //gsl_rng_set(m_rng, gsl_rng_default_seed);
56 #if 0
57  if (m_worldRank == 0) {
58  std::cout << "In RngGsl::constructor():"
59  << "\n m_seed = " << m_seed
60  << "\n internal seed = " << gsl_rng_default_seed
61  //<< "\n first generated sample from uniform distribution = " << gsl_rng_uniform(m_rng)
62  //<< "\n first generated sample from std normal distribution = " << gsl_ran_gaussian(m_rng,1.)
63  << std::endl;
64  }
65 #endif
66 }
67 
68 // Destructor ---------------------------------------
70 {
71  if (m_rng) gsl_rng_free(m_rng);
72 }
73 
74 // Sampling methods ---------------------------------
75 void
76 RngGsl::resetSeed(int newSeed)
77 {
78  RngBase::resetSeed(newSeed);
79  gsl_rng_free(m_rng);
80 
81  gsl_rng_default_seed = (unsigned long int) m_seed;
82  m_rng = gsl_rng_alloc(gsl_rng_ranlxd2);
83  UQ_FATAL_TEST_MACRO((m_rng == NULL),
85  "RngGsl::resetSeed()",
86  "null m_rng");
87  return;
88 }
89 
90 // --------------------------------------------------
91 double
93 {
94  return gsl_rng_uniform(m_rng);
95 }
96 
97 // --------------------------------------------------
98 double
99 RngGsl::gaussianSample(double stdDev) const
100 {
101  return gsl_ran_gaussian(m_rng,stdDev);
102 }
103 
104 // --------------------------------------------------
105 double
106 RngGsl::betaSample(double alpha, double beta) const
107 {
108  return gsl_ran_beta(m_rng,alpha,beta);
109 }
110 
111 // --------------------------------------------------
112 double
113 RngGsl::gammaSample(double a, double b) const
114 {
115  return gsl_ran_gamma(m_rng,a,b);
116 }
117 
118 const gsl_rng*
119 RngGsl::rng() const
120 {
121  return m_rng;
122 }
123 
124 } // End namespace QUESO
const gsl_rng * rng() const
GSL random number generator.
Definition: RngGsl.C:119
void resetSeed(int newSeed)
Resets the seed with value newSeed.
Definition: RngGsl.C:76
double uniformSample() const
Samples a value from a uniform distribution. Support: [0,1] or [a,b].
Definition: RngGsl.C:92
virtual void resetSeed(int newSeed)
Resets the seed with value newSeed.
Definition: RngBase.C:60
double gaussianSample(double stdDev) const
Definition: RngGsl.C:99
double gammaSample(double a, double b) const
Samples a value from a Gamma distribution. Support: [0,infinity).
Definition: RngGsl.C:113
~RngGsl()
Destructor.
Definition: RngGsl.C:69
double betaSample(double alpha, double beta) const
Samples a value from a Beta distribution. Support: [0,1].
Definition: RngGsl.C:106
RngGsl()
Default Constructor: it should not be used.
Definition: RngGsl.C:32
int m_worldRank
Rank of processor.
Definition: RngBase.h:88
unsigned long int gsl_rng_default_seed
gsl_rng * m_rng
GSL random number generator.
Definition: RngGsl.h:111
Class for random number generation (base class for either GSL or Boost RNG).
Definition: RngBase.h:45
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
int m_seed
Seed.
Definition: RngBase.h:85

Generated on Thu Apr 23 2015 19:26:15 for queso-0.51.1 by  doxygen 1.8.5