7df6fa1d348d27247af9d5b526bff573fc509484
[alexxy/gromacs.git] / src / gromacs / random / exponentialdistribution.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35
36 /*! \file
37  * \brief The exponential distribution
38  *
39  * Portable version of the exponential distribution that generates the same
40  * sequence on all platforms. Since stdlibc++ and libc++ provide different
41  * sequences we prefer this one so unit tests produce the same values on all
42  * platforms.
43  *
44  * \author Erik Lindahl <erik.lindahl@gmail.com>
45  * \inpublicapi
46  * \ingroup module_random
47  */
48
49 #ifndef GMX_RANDOM_EXPONENTIALDISTRIBUTION_H
50 #define GMX_RANDOM_EXPONENTIALDISTRIBUTION_H
51
52 #include <cmath>
53
54 #include <limits>
55
56 #include "gromacs/random/uniformrealdistribution.h"
57 #include "gromacs/utility/classhelpers.h"
58
59 /*
60  * The portable version of the exponential distribution (to make sure we get the
61  * same values on all platforms) has been modified from the LLVM libcxx headers,
62  * distributed under the MIT license:
63  *
64  * Copyright (c) The LLVM compiler infrastructure
65  *
66  * Permission is hereby granted, free of charge, to any person obtaining a copy
67  * of this software and associated documentation files (the "Software"), to deal
68  * in the Software without restriction, including without limitation the rights
69  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
70  * copies of the Software, and to permit persons to whom the Software is
71  * furnished to do so, subject to the following conditions:
72  *
73  * The above copyright notice and this permission notice shall be included in
74  * all copies or substantial portions of the Software.
75  *
76  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
77  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
78  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
79  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
80  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
81  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
82  * THE SOFTWARE.
83  */
84
85 namespace gmx
86 {
87
88 /*! \brief Exponential distribution
89  *
90  *  The C++ standard library does provide an exponential distribution, but even
91  *  though they all sample from a correct distribution, different standard
92  *  library implementations appear to return different sequences of numbers
93  *  for the same random number generator. To make it easier to use GROMACS
94  *  unit tests that depend on random numbers we have our own implementation.
95  *
96  * \tparam RealType Floating-point type, real by default in GROMACS.
97  */
98 template<class RealType = real>
99 class ExponentialDistribution
100 {
101 public:
102     /*! \brief Type of values returned */
103     typedef RealType result_type;
104
105     /*! \brief Exponential distribution parameters */
106     class param_type
107     {
108         /*! \brief The lambda/decay parameter */
109         result_type lambda_;
110
111     public:
112         /*! \brief Reference back to the distribution class */
113         typedef ExponentialDistribution distribution_type;
114
115         /*! \brief Construct parameter block
116          *
117          * \param lambda   lambda/decay parameter
118          */
119         explicit param_type(result_type lambda = 1.0) : lambda_(lambda) {}
120
121         /*! \brief Return lambda parameter */
122         result_type lambda() const { return lambda_; }
123
124         /*! \brief True if two parameter sets will return the same exponential distribution.
125          *
126          * \param x  Instance to compare with.
127          */
128         bool operator==(const param_type& x) const { return lambda_ == x.lambda_; }
129
130         /*! \brief True if two parameter sets will return different exponential distributions
131          *
132          * \param x  Instance to compare with.
133          */
134         bool operator!=(const param_type& x) const { return !operator==(x); }
135     };
136
137 public:
138     /*! \brief Construct new distribution with given floating-point parameter.
139      *
140      * \param lambda   lambda/decay parameter
141
142      */
143     explicit ExponentialDistribution(result_type lambda = 1.0) : param_(param_type(lambda)) {}
144
145     /*! \brief Construct new distribution from parameter class
146      *
147      * \param param  Parameter class as defined inside gmx::ExponentialDistribution.
148      */
149     explicit ExponentialDistribution(const param_type& param) : param_(param) {}
150
151     /*! \brief Flush all internal saved values  */
152     void reset() {}
153
154     /*! \brief Return values from exponential distribution with internal parameters
155      *
156      *  \tparam Rng   Random engine class
157      *
158      *  \param  g     Random engine
159      */
160     template<class Rng>
161     result_type operator()(Rng& g)
162     {
163         return (*this)(g, param_);
164     }
165
166     /*! \brief Return value from exponential distribution with given parameters
167      *
168      *  \tparam Rng   Random engine class
169      *
170      *  \param  g     Random engine
171      *  \param  param Parameters to use
172      */
173     template<class Rng>
174     result_type operator()(Rng& g, const param_type& param)
175     {
176         return -std::log(result_type(1)
177                          - generateCanonical<result_type, std::numeric_limits<result_type>::digits>(g))
178                / param.lambda();
179     }
180
181     /*! \brief Return the lambda parameter of the exponential distribution */
182     result_type lambda() const { return param_.lambda(); }
183
184     /*! \brief Return the full parameter class of exponential distribution */
185     param_type param() const { return param_; }
186
187     /*! \brief Smallest value that can be returned from exponential distribution */
188     result_type min() const { return 0; }
189
190     /*! \brief Largest value that can be returned from exponential distribution */
191     result_type max() const { return std::numeric_limits<result_type>::infinity(); }
192
193     /*! \brief True if two exponential distributions will produce the same values.
194      *
195      * \param  x     Instance to compare with.
196      */
197     bool operator==(const ExponentialDistribution& x) const { return param_ == x.param_; }
198
199     /*! \brief True if two exponential distributions will produce different values.
200      *
201      * \param  x     Instance to compare with.
202      */
203     bool operator!=(const ExponentialDistribution& x) const { return !operator==(x); }
204
205 private:
206     /*! \brief Internal value for parameters, can be overridden at generation time. */
207     param_type param_;
208
209     GMX_DISALLOW_COPY_AND_ASSIGN(ExponentialDistribution);
210 };
211
212 } // namespace gmx
213
214 #endif // GMX_MATH_RANDOM_H