From b4df4f9ebfed802eb0bf89078baacfedd7509039 Mon Sep 17 00:00:00 2001 From: Sajid Ali Date: Wed, 8 May 2024 09:21:24 -0500 Subject: [PATCH] add missing header in foundation/distribution.h --- src/synergia/foundation/distribution.cc | 69 ++++++++------- src/synergia/foundation/distribution.h | 107 ++++++++++++------------ 2 files changed, 87 insertions(+), 89 deletions(-) diff --git a/src/synergia/foundation/distribution.cc b/src/synergia/foundation/distribution.cc index 21fdf5704..c936c062e 100644 --- a/src/synergia/foundation/distribution.cc +++ b/src/synergia/foundation/distribution.cc @@ -9,71 +9,68 @@ unsigned long int Random_distribution::get_default_seed(const char* device) { - unsigned long int seed; - std::ifstream devrandom(device, std::ios::binary); - if (devrandom) { - devrandom.read((char*)&seed, sizeof(unsigned long int)); - } else { - seed = std::time(0); - } - return seed; + unsigned long int seed; + std::ifstream devrandom(device, std::ios::binary); + if (devrandom) { + devrandom.read((char*)&seed, sizeof(unsigned long int)); + } else { + seed = std::time(0); + } + return seed; } Random_distribution::Random_distribution(unsigned long int seed, int rank, - Generator generator) : - rng(nullptr), - rng_type(nullptr), - rank(rank), - original_seed(0) + Generator generator) + : rng(nullptr), rng_type(nullptr), rank(rank), original_seed(0) { - gsl_rng_env_setup(); - if (generator == ranlxd2) { - rng_type = gsl_rng_ranlxd2; - } else if (generator == mt19937) { - rng_type = gsl_rng_mt19937; - } - rng = gsl_rng_alloc(rng_type); - set_seed(seed); // NOTE: this resets original_seed + gsl_rng_env_setup(); + if (generator == ranlxd2) { + rng_type = gsl_rng_ranlxd2; + } else if (generator == mt19937) { + rng_type = gsl_rng_mt19937; + } + rng = gsl_rng_alloc(rng_type); + set_seed(seed); // NOTE: this resets original_seed } void Random_distribution::set_seed(unsigned long int seed) { - if (seed == 0) { - original_seed = get_default_seed(); - } else { - original_seed = seed; - } - unsigned long int distributed_seed; - distributed_seed = - (1000 + 5 * (rank + original_seed)) * ((rank + original_seed) + 7) - 1; + if (seed == 0) { + original_seed = get_default_seed(); + } else { + original_seed = seed; + } + unsigned long int distributed_seed; + distributed_seed = + (1000 + 5 * (rank + original_seed)) * ((rank + original_seed) + 7) - 1; - gsl_rng_set(rng, distributed_seed); + gsl_rng_set(rng, distributed_seed); } unsigned long int Random_distribution::get_original_seed() const { - return original_seed; + return original_seed; } double Random_distribution::get() { - return gsl_rng_uniform(rng); + return gsl_rng_uniform(rng); } double Random_distribution::get_uniform(double min, double max) { - return gsl_ran_flat(rng, min, max); + return gsl_ran_flat(rng, min, max); } double Random_distribution::get_unit_gaussian() { - return gsl_ran_ugaussian_ratio_method(rng); + return gsl_ran_ugaussian_ratio_method(rng); } #if 0 @@ -102,11 +99,11 @@ Random_distribution::fill_unit_disk(double* x_array, double* y_array) Random_distribution::~Random_distribution() { - gsl_rng_free(rng); + gsl_rng_free(rng); } void Random_distribution::advance(uint64_t) { - throw std::runtime_error("Random_distribution can not be advanced."); + throw std::runtime_error("Random_distribution can not be advanced."); } diff --git a/src/synergia/foundation/distribution.h b/src/synergia/foundation/distribution.h index 0a52f6dc9..c87649972 100644 --- a/src/synergia/foundation/distribution.h +++ b/src/synergia/foundation/distribution.h @@ -1,31 +1,32 @@ #ifndef DISTRIBUTION_H_ #define DISTRIBUTION_H_ +#include #include /// Distribution is a virtual base class for obtaining the next number or set /// of numbers from a sequence according to a limited set of shapes. class Distribution { -public: - virtual ~Distribution() = default; + public: + virtual ~Distribution() = default; - /// Get the next number in the sequence (between 0 and 1). - virtual double get() = 0; + /// Get the next number in the sequence (between 0 and 1). + virtual double get() = 0; - /// Fill a one-dimensional array uniformly between min and max. - virtual double get_uniform(double min, double max) = 0; + /// Fill a one-dimensional array uniformly between min and max. + virtual double get_uniform(double min, double max) = 0; - /// Fill a one-dimensional array with Gaussian distribution of - /// zero mean and unit standard deviation. - virtual double get_unit_gaussian() = 0; + /// Fill a one-dimensional array with Gaussian distribution of + /// zero mean and unit standard deviation. + virtual double get_unit_gaussian() = 0; - /// Fill two one-dimensional arrays such that (x,y) are distributed - /// uniformly in the unit disk. - /// virtual void fill_unit_disk(double* x_array, double* y_array) = 0; + /// Fill two one-dimensional arrays such that (x,y) are distributed + /// uniformly in the unit disk. + /// virtual void fill_unit_disk(double* x_array, double* y_array) = 0; - /// Skip ahead the random number generator by delta. - /// NOTE: subclasses may implement this as a no-op. - virtual void advance(uint64_t delta) = 0; + /// Skip ahead the random number generator by delta. + /// NOTE: subclasses may implement this as a no-op. + virtual void advance(uint64_t delta) = 0; }; /// Random_distribution provides a Distribution of random numbers. The random @@ -33,56 +34,56 @@ class Distribution { /// random numbers from the GNU Scientific Library. class Random_distribution : public Distribution { -private: - gsl_rng* rng; - const gsl_rng_type* rng_type; - int rank; - unsigned long int original_seed; + private: + gsl_rng* rng; + const gsl_rng_type* rng_type; + int rank; + unsigned long int original_seed; -public: - enum Generator { ranlxd2, mt19937 }; + public: + enum Generator { ranlxd2, mt19937 }; - /// Construct a Random_distribution. - /// @param seed The random number seed. If seed == 0, the seed is - /// obtained from Random_distribution::get_default_seed(). - /// @param rank Set the seed for this rank. - /// @param generator The underlying random number generator to be used. - Random_distribution(unsigned long int seed, - int rank, - Generator generator = ranlxd2); + /// Construct a Random_distribution. + /// @param seed The random number seed. If seed == 0, the seed is + /// obtained from Random_distribution::get_default_seed(). + /// @param rank Set the seed for this rank. + /// @param generator The underlying random number generator to be used. + Random_distribution(unsigned long int seed, + int rank, + Generator generator = ranlxd2); - virtual ~Random_distribution(); + virtual ~Random_distribution(); - /// Generate a random seed. Attempt to read from device if present. - /// Otherwise, use the system clock. - /// @param device Read from pathname device. - static unsigned long int get_default_seed( - const char* device = "/dev/urandom"); + /// Generate a random seed. Attempt to read from device if present. + /// Otherwise, use the system clock. + /// @param device Read from pathname device. + static unsigned long int get_default_seed( + const char* device = "/dev/urandom"); - /// Set the random number generator seed. - /// @param seed The seed. - void set_seed(unsigned long int seed); + /// Set the random number generator seed. + /// @param seed The seed. + void set_seed(unsigned long int seed); - /// Get the seed used to start the random number generator. - unsigned long int get_original_seed() const; + /// Get the seed used to start the random number generator. + unsigned long int get_original_seed() const; - /// Get the next random number between 0 and 1. - double get() override; + /// Get the next random number between 0 and 1. + double get() override; - /// Fill a one-dimensional array uniformly between min and max. - double get_uniform(double min, double max) override; + /// Fill a one-dimensional array uniformly between min and max. + double get_uniform(double min, double max) override; - /// Fill a one-dimensional array with Gaussian distribution of - /// zero mean and unit standard deviation. - double get_unit_gaussian() override; + /// Fill a one-dimensional array with Gaussian distribution of + /// zero mean and unit standard deviation. + double get_unit_gaussian() override; - /// Fill two one-dimensional arrays such that (x,y) are distributed - /// uniformly in the unit disk. - /// void fill_unit_disk(double* x_array, double* y_array) override; + /// Fill two one-dimensional arrays such that (x,y) are distributed + /// uniformly in the unit disk. + /// void fill_unit_disk(double* x_array, double* y_array) override; - void advance(uint64_t delta) override; + void advance(uint64_t delta) override; - void test(); + void test(); }; #endif /* DISTRIBUTION_H_ */