ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
random.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 *
4 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
5 * Max-Planck-Institute for Polymer Research, Theory Group
6 *
7 * This file is part of ESPResSo.
8 *
9 * ESPResSo is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * ESPResSo is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22#ifndef RANDOM_H
23#define RANDOM_H
24
25/** \file
26 * Random number generation using Philox.
27 */
28
29#include <utils/Vector.hpp>
30#include <utils/u32_to_u64.hpp>
31#include <utils/uniform.hpp>
32
33#include <Random123/philox.h>
34
35#include <cstddef>
36#include <numbers>
37#include <random>
38#include <ranges>
39#include <vector>
40
41/*
42 * @brief Salt for the RNGs
43 *
44 * This is to avoid correlations between the
45 * noise on the particle coupling and the fluid
46 * thermalization.
47 */
63
64namespace Random {
65/**
66 * @brief get 4 random uint 64 from the Philox RNG
67 *
68 * This uses the Philox PRNG, the state is controlled
69 * by the counter, the salt and two keys.
70 * If any of the keys and salt differ, the noise is
71 * not correlated between two calls along the same counter
72 * sequence.
73 *
74 */
75template <RNGSalt salt>
76auto philox_4_uint64s(uint64_t counter, uint32_t seed, int key1, int key2 = 0) {
77
78 using rng_type = r123::Philox4x64;
79 using ctr_type = rng_type::ctr_type;
80 using key_type = rng_type::key_type;
81
82 const ctr_type c{{counter, 0u, 0u, 0u}};
83
84 auto const id1 = static_cast<uint32_t>(key1);
85 auto const id2 = static_cast<uint32_t>(key2);
86 const key_type k{{Utils::u32_to_u64(id1, id2),
87 Utils::u32_to_u64(static_cast<uint32_t>(salt), seed)}};
88
89 return rng_type{}(c, k);
90}
91
92/**
93 * @brief Generator for random uniform noise.
94 *
95 * Mean = 0, variance = 1 / 12.
96 * This uses the Philox PRNG, the state is controlled
97 * by the counter, the salt and two keys.
98 * If any of the keys and salt differ, the noise is
99 * not correlated between two calls along the same counter
100 * sequence.
101 *
102 * @tparam salt RNG salt
103 * @tparam N Size of the noise vector
104 * @param counter counter for random number generation
105 * @param seed seed for random number generation
106 * @param key1 key for random number generation
107 * @param key2 key for random number generation
108 *
109 * @return Vector of uniform random numbers.
110 */
111template <RNGSalt salt, std::size_t N = 3,
112 std::enable_if_t<(N > 1) and (N <= 4), int> = 0>
113auto noise_uniform(uint64_t counter, uint32_t seed, int key1, int key2 = 0) {
114
115 auto const integers = philox_4_uint64s<salt>(counter, seed, key1, key2);
116 Utils::VectorXd<N> noise{};
117 std::ranges::transform(integers | std::ranges::views::take(N), noise.begin(),
118 [](std::size_t v) { return Utils::uniform(v) - 0.5; });
119 return noise;
120}
121
122template <RNGSalt salt, std::size_t N, std::enable_if_t<N == 1, int> = 0>
123auto noise_uniform(uint64_t counter, uint32_t seed, int key1, int key2 = 0) {
124
125 auto const integers = philox_4_uint64s<salt>(counter, seed, key1, key2);
126 return Utils::uniform(integers[0]) - 0.5;
127}
128
129/** @brief Generator for Gaussian noise.
130 *
131 * Mean = 0, standard deviation = 1.0.
132 * Based on the Philox RNG using 4x64 bits.
133 * The Box-Muller transform is used to convert from uniform to normal
134 * distribution. The transform is only valid, if the uniformly distributed
135 * random numbers are not zero (approx one in 2^64). To avoid this case,
136 * such numbers are replaced by std::numeric_limits<double>::min()
137 * This breaks statistics in rare cases but allows for consistent RNG
138 * counters across MPI ranks.
139 *
140 * @tparam salt decorrelates different thermostat types
141 * @param counter counter for random number generation
142 * @param seed seed for random number generation
143 * @param key1 key for random number generation
144 * @param key2 key for random number generation
145 *
146 * @return Vector of Gaussian random numbers.
147 *
148 */
149template <RNGSalt salt, std::size_t N = 3,
150 class = std::enable_if_t<(N >= 1) and (N <= 4)>>
151auto noise_gaussian(uint64_t counter, uint32_t seed, int key1, int key2 = 0) {
152
153 auto const integers = philox_4_uint64s<salt>(counter, seed, key1, key2);
154 static const double epsilon = std::numeric_limits<double>::min();
155
156 constexpr std::size_t M = (N <= 2) ? 2 : 4;
158 std::ranges::transform(integers | std::ranges::views::take(M), u.begin(),
159 [](std::size_t value) {
160 auto u = Utils::uniform(value);
161 return (u < epsilon) ? epsilon : u;
162 });
163
164 // Box-Muller transform code adapted from
165 // https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
166 // optimizations: the modulo is cached (logarithms are expensive), the
167 // sin/cos are evaluated simultaneously by gcc or separately by Clang
168 Utils::VectorXd<N> noise{};
169 {
170 auto const modulo = sqrt(-2. * log(u[0]));
171 auto const angle = 2. * std::numbers::pi * u[1];
172 noise[0] = modulo * cos(angle);
173 if (N > 1) {
174 noise[1] = modulo * sin(angle);
175 }
176 }
177 if (N > 2) {
178 auto const modulo = sqrt(-2. * log(u[2]));
179 auto const angle = 2. * std::numbers::pi * u[3];
180 noise[2] = modulo * cos(angle);
181 if (N > 3) {
182 noise[3] = modulo * sin(angle);
183 }
184 }
185 return noise;
186}
187
188/** Mersenne Twister with warmup.
189 * The first 100'000 values of Mersenne Twister generators are often heavily
190 * correlated @cite panneton06a. This utility function discards the first
191 * 1'000'000 values.
192 *
193 * @param seed RNG seed
194 */
195template <typename T> std::mt19937 mt19937(T &&seed) {
196 std::mt19937 generator(seed);
197 generator.discard(1'000'000);
198 return generator;
199}
200
201} // namespace Random
202
203#endif
Vector implementation and trait types for boost qvm interoperability.
auto philox_4_uint64s(uint64_t counter, uint32_t seed, int key1, int key2=0)
get 4 random uint 64 from the Philox RNG
Definition random.hpp:76
auto noise_uniform(uint64_t counter, uint32_t seed, int key1, int key2=0)
Generator for random uniform noise.
Definition random.hpp:113
auto noise_gaussian(uint64_t counter, uint32_t seed, int key1, int key2=0)
Generator for Gaussian noise.
Definition random.hpp:151
std::mt19937 mt19937(T &&seed)
Mersenne Twister with warmup.
Definition random.hpp:195
constexpr double uniform(uint64_t in)
Uniformly map unsigned integer to double.
Definition uniform.hpp:36
constexpr uint64_t u32_to_u64(uint32_t high, uint32_t low)
RNGSalt
Definition random.hpp:48
@ NPTISO0_HALF_STEP1
@ BROWNIAN_INC
@ LANGEVIN_ROT
@ NPTISO0_HALF_STEP2
@ BROWNIAN_ROT_WALK
@ BROWNIAN_WALK
@ BROWNIAN_ROT_INC
@ THERMALIZED_BOND