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-2026 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
23#pragma once
24
25/** \file
26 * Random number generation using Philox.
27 */
28
29#include <utils/Vector.hpp>
31#include <utils/u32_to_u64.hpp>
32#include <utils/uniform.hpp>
33
34#include <Random123/philox.h>
35
36#include <cstddef>
37#include <numbers>
38#include <random>
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 */
74template <RNGSalt salt>
76 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 requires((N >= 1) and (N <= 4))
114 int key2 = 0) {
115 auto const integers = philox_4_uint64s<salt>(counter, seed, key1, key2);
117 for (std::size_t i = 0; i < N; ++i) {
118 noise[i] = Utils::uniform(integers[i]) - 0.5;
119 }
120 return noise;
121}
122
123/** @brief Generator for Gaussian noise.
124 *
125 * Mean = 0, standard deviation = 1.0.
126 * Based on the Philox RNG using 4x64 bits.
127 * The Box-Muller transform is used to convert from uniform to normal
128 * distribution. The transform is only valid, if the uniformly distributed
129 * random numbers are not zero (approx one in 2^64). To avoid this case,
130 * such numbers are replaced by std::numeric_limits<double>::min()
131 * This breaks statistics in rare cases but allows for consistent RNG
132 * counters across MPI ranks.
133 *
134 * @tparam salt decorrelates different thermostat types
135 * @param counter counter for random number generation
136 * @param seed seed for random number generation
137 * @param key1 key for random number generation
138 * @param key2 key for random number generation
139 *
140 * @return Vector of Gaussian random numbers.
141 */
142template <RNGSalt salt, std::size_t N = 3>
143 requires((N >= 1) and (N <= 4))
145 int key2 = 0) {
146
147 auto const integers = philox_4_uint64s<salt>(counter, seed, key1, key2);
148
149 constexpr std::size_t M = (N <= 2) ? 2 : 4;
150 constexpr auto epsilon = std::numeric_limits<double>::min();
152 for (std::size_t i = 0; i < M; ++i) {
153 auto res = Utils::uniform(integers[i]);
154 u[i] = (res < epsilon) ? epsilon : res;
155 }
156
157 // Box-Muller transform code adapted from
158 // https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
159 // optimizations: the modulo is cached (logarithms are expensive), the
160 // sin/cos are evaluated simultaneously by gcc or separately by Clang
162 {
163 auto const modulo = std::sqrt(-2. * std::log(u[0]));
164 auto const angle = 2. * std::numbers::pi * u[1];
165 noise[0] = modulo * std::cos(angle);
166 if (N > 1) {
167 noise[1] = modulo * std::sin(angle);
168 }
169 }
170 if (N > 2) {
171 auto const modulo = std::sqrt(-2. * log(u[2]));
172 auto const angle = 2. * std::numbers::pi * u[3];
173 noise[2] = modulo * std::cos(angle);
174 if (N > 3) {
175 noise[3] = modulo * std::sin(angle);
176 }
177 }
178 return noise;
179}
180
181/** Mersenne Twister with warmup.
182 * The first 100'000 values of Mersenne Twister generators are often heavily
183 * correlated @cite panneton06a. This utility function discards the first
184 * 1'000'000 values.
185 *
186 * @param seed RNG seed
187 */
188template <typename T> std::mt19937 mt19937(T &&seed) {
189 std::mt19937 generator(seed);
190 generator.discard(1'000'000);
191 return generator;
192}
193
194} // namespace Random
Vector implementation and trait types for boost qvm interoperability.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
#define DEVICE_QUALIFIER
DEVICE_QUALIFIER auto noise_uniform(uint64_t counter, uint32_t seed, int key1, int key2=0)
Generator for random uniform noise.
Definition random.hpp:113
DEVICE_QUALIFIER auto noise_gaussian(uint64_t counter, uint32_t seed, int key1, int key2=0)
Generator for Gaussian noise.
Definition random.hpp:144
DEVICE_QUALIFIER 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:75
std::mt19937 mt19937(T &&seed)
Mersenne Twister with warmup.
Definition random.hpp:188
DEVICE_QUALIFIER constexpr uint64_t u32_to_u64(uint32_t high, uint32_t low)
constexpr DEVICE_QUALIFIER double uniform(uint64_t in)
Uniformly map unsigned integer to double.
Definition uniform.hpp:37
RNGSalt
Definition random.hpp:48
@ BROWNIAN_INC
@ LANGEVIN_ROT
@ BROWNIAN_ROT_WALK
@ THERMAL_STONER_WOHLFARTH
@ BROWNIAN_WALK
@ BROWNIAN_ROT_INC
@ NPTISO_VOLUME
@ THERMALIZED_BOND
@ NPTISO_PARTICLE