diff --git a/cmake/ExternalTools.cmake b/cmake/ExternalTools.cmake index 8f11335ce..9dadc5485 100644 --- a/cmake/ExternalTools.cmake +++ b/cmake/ExternalTools.cmake @@ -1,6 +1,24 @@ include(ExternalProject) include(FetchContent) +############# +# PCG RANDOM +############# + +option(ENABLE_PCG "Enable PCG random number generator" off) +if (ENABLE_PCG) + FetchContent_Declare( + pcg-cpp + URL https://github.com/imneme/pcg-cpp/archive/ffd522e7188bef30a00c74dc7eb9de5faff90092.tar.gz + URL_HASH MD5=051b969bbaf924f35f2159813f93e341) + FetchContent_GetProperties(pcg-cpp) + if(NOT pcg-cpp_POPULATED) + FetchContent_Populate(pcg-cpp) + endif() + include_directories(SYSTEM ${pcg-cpp_SOURCE_DIR}/include) + add_definitions("-DENABLE_PCG") +endif() + ######### # EXPRTK ######### diff --git a/docs/_docs/install.md b/docs/_docs/install.md index 5e9bb1e8c..4ee1d1183 100644 --- a/docs/_docs/install.md +++ b/docs/_docs/install.md @@ -74,6 +74,7 @@ CMake Option | Description `-DENABLE_PYTHON=OFF` | Build python bindings (experimental) `-DENABLE_FREESASA=ON` | Enable SASA routines (external download) `-DENABLE_TBB=OFF` | Build with Intel Threading Building Blocks (experimental) +`-DENABLE_PCG=OFF` | Use PCG random number generator instead of C++'s Mersenne Twister `-DBUILD_STATIC=OFF` | Build statically linked binaries `-DCMAKE_BUILD_TYPE=Release` | Alternatives: `Debug` or `RelWithDebInfo` `-DCMAKE_CXX_FLAGS_RELEASE="..."` | Compiler options for Release mode diff --git a/examples/cluster-ideal/cluster-ideal.yml b/examples/cluster-ideal/cluster-ideal.yml index 355055719..cda2e4809 100755 --- a/examples/cluster-ideal/cluster-ideal.yml +++ b/examples/cluster-ideal/cluster-ideal.yml @@ -27,7 +27,7 @@ energy: moves: - moltransrot: {molecule: water, dp: 20, dprot: 1, repeat: 1} - moltransrot: {molecule: other, dp: 20, dprot: 1, repeat: 1} - - cluster: {molecules: [water, other], dp: 20, dprot: 3, threshold: 7, satellites: [other]} + - cluster: {molecules: [water, other], dp: 19.99999, dprot: 3, threshold: 7, satellites: [other]} analysis: - savestate: {file: confout.pqr} diff --git a/src/core.cpp b/src/core.cpp index 2540a7d31..f7a1c2778 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -212,7 +212,7 @@ Point ranunit(Random &rand, const Point &dir) { TEST_CASE("[Faunus] ranunit") { Random r; - int n = 2e5; + int n = 4e5; Point rtp(0, 0, 0); for (int i = 0; i < n; i++) rtp += xyz2rtp(ranunit(r)); diff --git a/src/random.cpp b/src/random.cpp index 490dd7fbb..e03be3d65 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -31,9 +31,14 @@ void to_json(nlohmann::json &j, const Random &random) { std::ostringstream stream; stream << random.engine; // dump engine state to stream j["seed"] = stream.str(); + if constexpr (std::is_same::value) { + j["engine"] = "Mersenne Twister (std::mt19937)"; + } else { + j["engine"] = "Permuted Congruential Generator (pcg32)"; + } } -void Random::seed() { engine = std::mt19937(std::random_device()()); } +void Random::seed() { engine = RandomNumberEngine(std::random_device()()); } Random::Random() : dist01(0, 1) {} diff --git a/src/random.h b/src/random.h index 20ab13147..6e76b997d 100644 --- a/src/random.h +++ b/src/random.h @@ -5,8 +5,18 @@ #include #include +#ifdef ENABLE_PCG +#include +#endif + namespace Faunus { +#ifdef ENABLE_PCG +using RandomNumberEngine = pcg32; +#else +using RandomNumberEngine = std::mt19937; +#endif + /** * @brief Random number generator * @@ -22,7 +32,7 @@ class Random { private: std::uniform_real_distribution dist01; //!< Uniform real distribution [0,1) public: - std::mt19937 engine; //!< Random number engine used for all operations + RandomNumberEngine engine; //!< Random number engine used for all operations Random(); //!< Constructor with deterministic seed void seed(); //!< Set a non-deterministic ("hardware") seed double operator()(); //!< Random double in uniform range [0,1)