Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / random / random.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2008, The GROMACS development team.
6  * Copyright (c) 2010,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37
38 #ifndef GMX_RANDOM_RANDOM_H
39 #define GMX_RANDOM_RANDOM_H
40
41 #include "gromacs/utility/basedefinitions.h"
42 #include "gromacs/utility/real.h"
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 /*! Fixed random number seeds for different types of RNG */
49 #define RND_SEED_UPDATE    1 /**< For coordinate update (sd, bd, ..) */
50 #define RND_SEED_REPLEX    2 /**< For replica exchange */
51 #define RND_SEED_VRESCALE  3 /**< For V-rescale thermostat */
52 #define RND_SEED_ANDERSEN  4 /**< For Andersen thermostat */
53 #define RND_SEED_TPI       5 /**< For test particle insertion */
54 #define RND_SEED_EXPANDED  6 /**< For expanded emseble methods */
55
56 /*! \brief Abstract datatype for a random number generator
57  *
58  * This is a handle to the full state of a random number generator.
59  * You can not access anything inside the gmx_rng structure outside this
60  * file.
61  */
62 typedef struct gmx_rng *
63     gmx_rng_t;
64
65
66 /*! \brief Returns the size of the RNG integer data structure
67  *
68  * Returns the size of the RNG integer data structure.
69  * \threadsafe Yes.
70  */
71 int
72 gmx_rng_n(void);
73
74
75 /*! \brief Create a new RNG, seeded from a single integer.
76  *
77  * If you dont want to pick a seed, just call it as
78  * rng=gmx_rng_init(gmx_rng_make_seed()) to seed it from
79  * the system time or a random device.
80  *
81  * \param seed Random seed, unsigned 32-bit integer.
82  *
83  * \return Reference to a random number generator, or NULL if there was an
84  *         error.
85  *
86  * \threadsafe Yes.
87  */
88 gmx_rng_t
89 gmx_rng_init(unsigned int seed);
90
91
92 /*! \brief Generate a 'random' RNG seed.
93  *
94  * This routine tries to get a seed from /dev/random if present,
95  * and if not it uses time-of-day and process id to generate one.
96  *
97  * \return 32-bit unsigned integer random seed.
98  *
99  * Tip: If you use this in your code, it is a good idea to write the
100  * returned random seed to a logfile, so you can recreate the exact sequence
101  * of random number if you need to reproduce your run later for one reason
102  * or another.
103  *
104  * \threadsafe Yes.
105  */
106 unsigned int
107 gmx_rng_make_seed(void);
108
109
110 /*! \brief Initialize a RNG with 624 integers (>32 bits of entropy).
111  *
112  *  The Mersenne twister RNG used in Gromacs has an extremely long period,
113  *  but when you only initialize it with a 32-bit integer there are only
114  *  2^32 different possible sequences of number - much less than the generator
115  *  is capable of.
116  *
117  *  If you really need the full entropy, this routine makes it possible to
118  *  initialize the RNG with up to 624 32-bit integers, which will give you
119  *  up to 2^19968 bits of entropy.
120  *
121  *  \param seed Array of unsigned integers to form a seed
122  *  \param seed_length Number of integers in the array, up to 624 are used.
123  *
124  * \return Reference to a random number generator, or NULL if there was an
125  *         error.
126  *
127  * \threadsafe Yes.
128  */
129 gmx_rng_t
130 gmx_rng_init_array(unsigned int    seed[],
131                    int             seed_length);
132
133
134 /*! \brief Release resources of a RNG
135  *
136  *  This routine destroys a random number generator and releases all
137  *  resources allocated by it.
138  *
139  *  \param rng Handle to random number generator previously returned by
140  *                     gmx_rng_init() or gmx_rng_init_array().
141  *
142  * \threadsafe Function itself is threadsafe, but you should only destroy a
143  *             certain RNG once (i.e. from one thread).
144  */
145 void
146 gmx_rng_destroy(gmx_rng_t rng);
147
148
149 /*! \brief Get the state of a RNG
150  *
151  * This routine stores the random state in \p mt and \p mti.
152  *
153  * \param[in]  rng Handle to random number generator previously returned by
154  *     gmx_rng_init() or gmx_rng_init_array().
155  * \param[out] mt  Array of at least 624 integers to receive state.
156  * \param[out] mti Pointer to an integer to receive state.
157  */
158 void
159 gmx_rng_get_state(gmx_rng_t rng, unsigned int *mt, int *mti);
160
161
162 /*! \brief Set the state of a RNG
163  *
164  * This routine sets the random state from \p mt and \p mti.
165  *
166  * \param rng Handle to random number generator previously returned by
167  *     gmx_rng_init() or gmx_rng_init_array().
168  * \param[in]  mt  Array of at least 624 integers.
169  * \param[in]  mti Additional integer.
170  */
171 void
172 gmx_rng_set_state(gmx_rng_t rng, unsigned int *mt, int mti);
173
174
175 /*! \brief Random 32-bit integer from a uniform distribution
176  *
177  *  This routine returns a random integer from the random number generator
178  *  provided, and updates the state of that RNG.
179  *
180  *  \param rng Handle to random number generator previously returned by
181  *                     gmx_rng_init() or gmx_rng_init_array().
182  *
183  *  \return 32-bit unsigned integer from a uniform distribution.
184  *
185  *  \threadsafe Function yes, input data no. You should not call this function
186  *              from two different threads using the same RNG handle at the
187  *              same time. For performance reasons we cannot lock the handle
188  *              with a mutex every time we need a random number - that would
189  *              slow the routine down a factor 2-5. There are two simple
190  *              solutions: either use a mutex and lock it before calling
191  *              the function, or use a separate RNG handle for each thread.
192  */
193 unsigned int
194 gmx_rng_uniform_uint32(gmx_rng_t rng);
195
196
197 /*! \brief Random gmx_real_t 0<=x<1 from a uniform distribution
198  *
199  *  This routine returns a random floating-point number from the
200  *  random number generator provided, and updates the state of that RNG.
201  *
202  *  \param rng Handle to random number generator previously returned by
203  *                     gmx_rng_init() or gmx_rng_init_array().
204  *
205  *  \return floating-point number 0<=x<1 from a uniform distribution.
206  *
207  *  \threadsafe Function yes, input data no. You should not call this function
208  *              from two different threads using the same RNG handle at the
209  *              same time. For performance reasons we cannot lock the handle
210  *              with a mutex every time we need a random number - that would
211  *              slow the routine down a factor 2-5. There are two simple
212  *              solutions: either use a mutex and lock it before calling
213  *              the function, or use a separate RNG handle for each thread.
214  */
215 real
216 gmx_rng_uniform_real(gmx_rng_t rng);
217
218
219 /*! \brief Random gmx_real_t from a gaussian distribution
220  *
221  *  This routine returns a random floating-point number from the
222  *  random number generator provided, and updates the state of that RNG.
223  *
224  *  The Box-Muller algorithm is used to provide gaussian random numbers. This
225  *  is not the fastest known algorithm for gaussian numbers, but in contrast
226  *  to the alternatives it is very well studied and you can trust the returned
227  *  random numbers to have good properties and no correlations.
228  *
229  *  \param rng Handle to random number generator previously returned by
230  *                        gmx_rng_init() or gmx_rng_init_array().
231  *
232  *  \return Gaussian random floating-point number with average 0.0 and
233  *          standard deviation 1.0. You can get any average/mean you want
234  *          by first multiplying with the desired average and then adding
235  *          the average you want.
236  *
237  *  \threadsafe Function yes, input data no. You should not call this function
238  *              from two different threads using the same RNG handle at the
239  *              same time. For performance reasons we cannot lock the handle
240  *              with a mutex every time we need a random number - that would
241  *              slow the routine down a factor 2-5. There are two simple
242  *              solutions: either use a mutex and lock it before calling
243  *              the function, or use a separate RNG handle for each thread.
244  *
245  *  It works perfectly to mix calls to get uniform and gaussian random numbers
246  *  from the same generator, but since it will affect the sequence of returned
247  *  numbers it is probably better to use separate random number generator
248  *  structures.
249  */
250 real
251 gmx_rng_gaussian_real(gmx_rng_t rng);
252
253
254
255 /* Return a new gaussian random number with expectation value
256  * 0.0 and standard deviation 1.0. This routine uses a table
257  * lookup for maximum speed.
258  *
259  * WARNING: The lookup table is 16k by default, which means
260  *          the granularity of the random numbers is coarser
261  *          than what you get from gmx_rng_gauss_real().
262  *          In most cases this is no problem whatsoever,
263  *          and it is particularly true for BD/SD integration.
264  *          Note that you will NEVER get any really extreme
265  *          numbers: the maximum absolute value returned is
266  *          4.0255485.
267  *
268  * threadsafe: yes
269  */
270 real
271 gmx_rng_gaussian_table(gmx_rng_t rng);
272
273
274 /* The stateless cycle based random number generators below,
275  * which all use threefry2x64, take the following arguments:
276  *
277  * ctr1: In mdrun the step counter, in tools the frame(-step)
278  *       counter, so we can ensure reproducible results, even
279  *       we starting at different steps/frames. Might need to be
280  *       multiplied by a constant if we need more random numbers.
281  * ctr2: A local counter, in mdrun often a global atom index.
282  *       If any algorithm needs a variable number of random numbers,
283  *       the second counter is usually a function of the local
284  *       counter.
285  * key1: A user provided random seed.
286  * key2: A fixed seed which is particular for the algorithm,
287  *       as defined at the top of this file, to ensure different
288  *       random sequences when the same user seed is used for
289  *       different algorithms.
290  */
291
292 /* Return two uniform random numbers with 2^53 equally
293  * probable values between 0 and 1 - 2^-53.
294  * It uses a stateless counter based random number generator
295  * (threefry2x64).
296  */
297 void
298 gmx_rng_cycle_2uniform(gmx_int64_t ctr1, gmx_int64_t ctr2,
299                        gmx_int64_t key1, gmx_int64_t key2,
300                        double* rnd);
301
302 /* Return three Gaussian random numbers with expectation value
303  * 0.0 and standard deviation 1.0. This routine uses a table
304  * lookup for maximum speed. It uses a stateless counter
305  * based random number generator (threefry2x64). See
306  * gmx_rng_gaussian_table for a warning about accuracy of the table.
307  *
308  * threadsafe: yes
309  */
310 void
311 gmx_rng_cycle_3gaussian_table(gmx_int64_t ctr1, gmx_int64_t ctr2,
312                               gmx_int64_t key1, gmx_int64_t key2,
313                               real* rnd);
314
315 /* As gmx_rng_3gaussian_table, but returns 6 Gaussian numbers. */
316 void
317 gmx_rng_cycle_6gaussian_table(gmx_int64_t ctr1, gmx_int64_t ctr2,
318                               gmx_int64_t key1, gmx_int64_t key2,
319                               real* rnd);
320
321 #ifdef __cplusplus
322 }
323 #endif
324
325 #endif