Update instructions in containers.rst
[gromacs.git] / src / gromacs / random / normaldistribution.h
blob5242e97d1cad14ae813c0021fb5d9514f36aad3c
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,2019, 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.
36 /*! \file
37 * \brief The normal distribution
39 * Portable version of the normal distribution 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>
44 * \inpublicapi
45 * \ingroup module_random
48 #ifndef GMX_RANDOM_NORMALDISTRIBUTION_H
49 #define GMX_RANDOM_NORMALDISTRIBUTION_H
51 #include <cmath>
53 #include <limits>
55 #include "gromacs/random/uniformrealdistribution.h"
56 #include "gromacs/utility/classhelpers.h"
59 * The portable version of the normal distribution (to make sure we get the same
60 * values on all platforms) has been modified from the LLVM libcxx headers,
61 * distributed under the MIT license:
63 * Copyright (c) The LLVM compiler infrastructure
65 * Permission is hereby granted, free of charge, to any person obtaining a copy
66 * of this software and associated documentation files (the "Software"), to deal
67 * in the Software without restriction, including without limitation the rights
68 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
69 * copies of the Software, and to permit persons to whom the Software is
70 * furnished to do so, subject to the following conditions:
72 * The above copyright notice and this permission notice shall be included in
73 * all copies or substantial portions of the Software.
75 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
76 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
77 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
78 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
79 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
80 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
81 * THE SOFTWARE.
84 namespace gmx
87 /*! \brief Normal distribution
89 * The C++ standard library does provide a normal distribution, but even
90 * though they all sample from the normal distribution different standard
91 * library implementations appear to return different sequences of numbers
92 * for the same random number generator. To make it easier to use GROMACS
93 * unit tests that depend on random numbers we have our own implementation.
95 * Be warned that the normal distribution draws values from the random engine
96 * in a loop, so you want to make sure you use a random stream with a
97 * very large margin to make sure you do not run out of random numbers
98 * in an unlucky case (which will lead to an exception with the GROMACS
99 * default random engine).
101 * \tparam RealType Floating-point type, real by default in GROMACS.
103 template<class RealType = real>
104 class NormalDistribution
106 public:
107 /*! \brief Type of values returned */
108 typedef RealType result_type;
110 /*! \brief Normal distribution parameters */
111 class param_type
113 /*! \brief Mean of normal distribution */
114 result_type mean_;
115 /*! \brief Standard deviation of distribution */
116 result_type stddev_;
118 public:
119 /*! \brief Reference back to the distribution class */
120 typedef NormalDistribution distribution_type;
122 /*! \brief Construct parameter block
124 * \param mean Mean of normal distribution
125 * \param stddev Standard deviation of normal distribution
127 explicit param_type(result_type mean = 0.0, result_type stddev = 1.0) :
128 mean_(mean),
129 stddev_(stddev)
133 /*! \brief Return first parameter */
134 result_type mean() const { return mean_; }
135 /*! \brief Return second parameter */
136 result_type stddev() const { return stddev_; }
138 /*! \brief True if two parameter sets will return the same normal distribution.
140 * \param x Instance to compare with.
142 bool operator==(const param_type& x) const
144 return mean_ == x.mean_ && stddev_ == x.stddev_;
147 /*! \brief True if two parameter sets will return different normal distributions
149 * \param x Instance to compare with.
151 bool operator!=(const param_type& x) const { return !operator==(x); }
154 public:
155 /*! \brief Construct new distribution with given floating-point parameters.
157 * \param mean Mean of normal distribution
158 * \param stddev Standard deviation of normal distribution
160 explicit NormalDistribution(result_type mean = 0.0, result_type stddev = 1.0) :
161 param_(param_type(mean, stddev)),
162 hot_(false),
163 saved_(0)
167 /*! \brief Construct new distribution from parameter class
169 * \param param Parameter class as defined inside gmx::NormalDistribution.
171 explicit NormalDistribution(const param_type& param) : param_(param), hot_(false), saved_(0) {}
173 /*! \brief Flush all internal saved values */
174 void reset() { hot_ = false; }
176 /*! \brief Return values from normal distribution with internal parameters
178 * \tparam Rng Random engine class
180 * \param g Random engine
182 template<class Rng>
183 result_type operator()(Rng& g)
185 return (*this)(g, param_);
188 /*! \brief Return value from normal distribution with given parameters
190 * \tparam Rng Random engine class
192 * \param g Random engine
193 * \param param Parameters to use
195 template<class Rng>
196 result_type operator()(Rng& g, const param_type& param)
198 result_type result;
200 if (hot_)
202 hot_ = false;
203 result = saved_;
205 else
207 UniformRealDistribution<result_type> uniformDist(-1.0, 1.0);
208 result_type u;
209 result_type v;
210 result_type s;
214 u = uniformDist(g);
215 v = uniformDist(g);
216 s = u * u + v * v;
217 } while (s > 1.0 || s == 0.0);
219 s = std::sqrt(-2.0 * std::log(s) / s);
220 saved_ = v * s;
221 hot_ = true;
222 result = u * s;
224 return result * param.stddev() + param.mean();
227 /*! \brief Return the mean of the normal distribution */
228 result_type mean() const { return param_.mean(); }
230 /*! \brief Return the standard deviation of the normal distribution */
231 result_type stddev() const { return param_.stddev(); }
233 /*! \brief Return the full parameter class of the normal distribution */
234 param_type param() const { return param_; }
236 /*! \brief Smallest value that can be returned from normal distribution */
237 result_type min() const { return -std::numeric_limits<result_type>::infinity(); }
239 /*! \brief Largest value that can be returned from normal distribution */
240 result_type max() const { return std::numeric_limits<result_type>::infinity(); }
242 /*! \brief True if two normal distributions will produce the same values.
244 * \param x Instance to compare with.
246 bool operator==(const NormalDistribution& x) const
248 /* Equal if: Params are identical, and saved-state is identical,
249 * and if we have something saved, it must be identical.
251 return param_ == x.param_ && hot_ == x.hot_ && (!hot_ || saved_ == x.saved_);
254 /*! \brief True if two normal distributions will produce different values.
256 * \param x Instance to compare with.
258 bool operator!=(const NormalDistribution& x) const { return !operator==(x); }
260 private:
261 /*! \brief Internal value for parameters, can be overridden at generation time. */
262 param_type param_;
263 /*! \brief True if there is a saved result to return */
264 bool hot_;
265 /*! \brief The saved result to return - only valid if hot_ is true */
266 result_type saved_;
268 GMX_DISALLOW_COPY_AND_ASSIGN(NormalDistribution);
272 } // namespace gmx
274 #endif // GMX_RANDOM_NORMALDISTRIBUTION_H