2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,2018, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS 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 GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * \brief The uniform real distribution
39 * Portable version of the uniform real that generates the same sequence
40 * on all platforms. Since stdlibc++ and libc++ provide different sequences
41 * we prefer this one so unit tests produce the same values on all platforms.
43 * \author Erik Lindahl <erik.lindahl@gmail.com>
45 * \ingroup module_random
48 #ifndef GMX_RANDOM_UNIFORMREALDISTRIBUTION_H
49 #define GMX_RANDOM_UNIFORMREALDISTRIBUTION_H
56 #include "gromacs/math/functions.h"
57 #include "gromacs/utility/basedefinitions.h"
58 #include "gromacs/utility/classhelpers.h"
59 #include "gromacs/utility/gmxassert.h"
60 #include "gromacs/utility/real.h"
63 * The portable version of the uniform real distribution (to make sure we get
64 * the same values on all platforms) has been modified from the LLVM libcxx
65 * headers, distributed under the MIT license:
67 * Copyright (c) The LLVM compiler infrastructure
69 * Permission is hereby granted, free of charge, to any person obtaining a copy
70 * of this software and associated documentation files (the "Software"), to deal
71 * in the Software without restriction, including without limitation the rights
72 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
73 * copies of the Software, and to permit persons to whom the Software is
74 * furnished to do so, subject to the following conditions:
76 * The above copyright notice and this permission notice shall be included in
77 * all copies or substantial portions of the Software.
79 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
80 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
81 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
82 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
83 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
84 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
91 /*! \brief Generate a floating-point value with specified number of random bits
93 * \tparam RealType Floating-point type to generate
94 * \tparam Bits Number of random bits to generate
95 * \tparam Rng Random number generator class
97 * \param g Random number generator to use
99 * This implementation avoids the bug in libc++ and stdlibc++ (which is due
100 * to the C++ standard being unclear) where 1.0 can be returned occasionally.
103 template<class RealType
= real
, unsigned int Bits
, class Rng
>
105 generateCanonical(Rng
&g
)
107 // No point in using more bits than fit in RealType
108 const uint64_t digits
= std::numeric_limits
<RealType
>::digits
;
109 const uint64_t realBits
= std::min(digits
, static_cast<uint64_t>(Bits
));
110 const uint64_t range
= Rng::max() - Rng::min() + uint64_t(1);
111 uint64_t log2R
= (range
== 0) ? std::numeric_limits
<uint64_t>::digits
: log2I(range
);
112 uint64_t k
= realBits
/ log2R
+ (realBits
% log2R
!= 0) + (realBits
== 0);
113 RealType r
= Rng::max() - Rng::min() + RealType(1);
114 RealType s
= g() - Rng::min();
118 for (uint64_t i
= 1; i
< k
; ++i
)
120 s
+= RealType(g()-Rng::min()) * base
;
125 // This implementation is specified by the C++ standard, but unfortunately it
126 // has a bug where 1.0 can be generated occasionally due to the limited
127 // precision of floating point, while 0.0 is only generated half as often as
128 // it should. We "solve" both these issues by swapping 1.0 for 0.0 when it happens.
131 // https://llvm.org/bugs/show_bug.cgi?id=18767
132 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63176
134 // Note that we prefer not to use the gcc 'fix' of looping until the result
135 // is smaller than 1.0, since that breaks the strict specification of the
136 // number of times the rng will be called.
138 // This can only happen when we ask for the same number of bits that fit
139 // in RealType, so by checking for that we avoid the extra code in all other
140 // cases. If you are worried about it: Use RealType=double with 32 bits.
142 if (realBits
== digits
&& result
== 1.0)
150 /*! \brief Uniform real distribution
152 * The C++ standard library does provide this distribution, but even
153 * though they all sample from the correct distribution different standard
154 * library implementations appear to return different sequences of numbers
155 * for the same random number generator. To make it easier to use GROMACS
156 * unit tests that depend on random numbers we have our own implementation.
158 * \tparam RealType Floating-point type, real by default in GROMACS.
160 template<class RealType
= real
>
161 class UniformRealDistribution
164 /*! \brief Type of values returned */
165 typedef RealType result_type
;
167 /*! \brief Uniform real distribution parameters */
170 /*! \brief Lower end of range (inclusive) */
172 /*! \brief Upper end of range (exclusive) */
176 /*! \brief Reference back to the distribution class */
177 typedef UniformRealDistribution distribution_type
;
179 /*! \brief Construct parameter block
181 * \param a Lower end of range (inclusive)
182 * \param b Upper end of range (exclusive)
184 explicit param_type(result_type a
= 0.0, result_type b
= 1.0)
187 GMX_RELEASE_ASSERT(a
< b
, "The uniform real distribution requires a<b");
190 /*! \brief Return first parameter */
191 result_type
a() const { return a_
; }
192 /*! \brief Return second parameter */
193 result_type
b() const { return b_
; }
195 /*! \brief True if two parameter sets will return the same uniform real distribution.
197 * \param x Instance to compare with.
200 operator==(const param_type
&x
) const
202 return a_
== x
.a_
&& b_
== x
.b_
;
205 /*! \brief True if two parameter sets will return different uniform real distributions
207 * \param x Instance to compare with.
210 operator!=(const param_type
&x
) const { return !operator==(x
); }
215 /*! \brief Construct new distribution with given floating-point parameters.
217 * \param a Lower end of range (inclusive)
218 * \param b Upper end of range (exclusive)
220 explicit UniformRealDistribution(result_type a
= 0.0, result_type b
= 1.0)
221 : param_(param_type(a
, b
)) {}
223 /*! \brief Construct new distribution from parameter class
225 * \param param Parameter class as defined inside gmx::UniformRealDistribution.
227 explicit UniformRealDistribution(const param_type
¶m
) : param_(param
) {}
229 /*! \brief Flush all internal saved values */
233 /*! \brief Return values from uniform real distribution with internal parameters
235 * \tparam Rng Random engine class
237 * \param g Random engine
241 operator()(Rng
&g
) { return (*this)(g
, param_
); }
243 /*! \brief Return value from uniform real distribution with given parameters
245 * \tparam Rng Random engine class
247 * \param g Random engine
248 * \param param Parameters to use
252 operator()(Rng
&g
, const param_type
¶m
)
254 result_type r
= generateCanonical
<RealType
, std::numeric_limits
<RealType
>::digits
>(g
);
255 return ( param
.b() - param
.a() ) * r
+ param
.a();
258 /*! \brief Return the lower range uniform real distribution */
260 a() const { return param_
.a(); }
262 /*! \brief Return the upper range of the uniform real distribution */
264 b() const { return param_
.b(); }
266 /*! \brief Return the full parameter class of the uniform real distribution */
267 param_type
param() const { return param_
; }
269 /*! \brief Smallest value that can be returned from uniform real distribution */
271 min() const { return a(); }
273 /*! \brief Largest value that can be returned from uniform real distribution */
275 max() const { return b(); }
277 /*! \brief True if two uniform real distributions will produce the same values.
279 * \param x Instance to compare with.
282 operator==(const UniformRealDistribution
&x
) const
283 { return param_
== x
.param_
; }
285 /*! \brief True if two uniform real distributions will produce different values.
287 * \param x Instance to compare with.
290 operator!=(const UniformRealDistribution
&x
) const
291 { return !operator==(x
); }
294 /*! \brief Internal value for parameters, can be overridden at generation time. */
297 GMX_DISALLOW_COPY_AND_ASSIGN(UniformRealDistribution
);
302 #endif // GMX_RANDOM_UNIFORMREALDISTRIBUTION_H