Update copyright statements and change license to LGPL
[alexxy/gromacs.git] / include / maths.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-2004, The GROMACS development team,
6  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38
39 #ifndef _maths_h
40 #define _maths_h
41
42 #include <math.h>
43 #include "visibility.h"
44 #include "types/simple.h"
45 #include "typedefs.h"
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 #ifndef M_PI
52 #define M_PI            3.14159265358979323846
53 #endif
54
55 #ifndef M_PI_2
56 #define M_PI_2          1.57079632679489661923
57 #endif
58
59 #ifndef M_2PI
60 #define M_2PI           6.28318530717958647692
61 #endif
62     
63 #ifndef M_SQRT2
64 #define M_SQRT2 sqrt(2.0)
65 #endif
66
67 #ifndef M_1_PI
68 #define M_1_PI      0.31830988618379067154
69 #endif
70
71 #ifndef M_FLOAT_1_SQRTPI /* used in CUDA kernels */
72 /* 1.0 / sqrt(M_PI) */
73 #define M_FLOAT_1_SQRTPI 0.564189583547756f
74 #endif
75
76 #ifndef M_1_SQRTPI
77 /* 1.0 / sqrt(M_PI) */
78 #define M_1_SQRTPI 0.564189583547756
79 #endif
80
81 #ifndef M_2_SQRTPI
82 /* 2.0 / sqrt(M_PI) */
83 #define M_2_SQRTPI  1.128379167095513
84 #endif
85
86 /* Suzuki-Yoshida Constants, for n=3 and n=5, for symplectic integration  */
87 /* for n=1, w0 = 1 */
88 /* for n=3, w0 = w2 = 1/(2-2^-(1/3)), w1 = 1-2*w0 */
89 /* for n=5, w0 = w1 = w3 = w4 = 1/(4-4^-(1/3)), w1 = 1-4*w0 */
90     
91 #define MAX_SUZUKI_YOSHIDA_NUM 5
92 #define SUZUKI_YOSHIDA_NUM  5
93
94 static const double sy_const_1[] = { 1. };
95 static const double sy_const_3[] = { 0.828981543588751,-0.657963087177502,0.828981543588751 };
96 static const double sy_const_5[] = { 0.2967324292201065,0.2967324292201065,-0.186929716880426,0.2967324292201065,0.2967324292201065 };
97
98 static const double* sy_const[] = {
99     NULL,
100     sy_const_1,
101     NULL,
102     sy_const_3,
103     NULL,
104     sy_const_5
105 };
106
107 /*
108 static const double sy_const[MAX_SUZUKI_YOSHIDA_NUM+1][MAX_SUZUKI_YOSHIDA_NUM+1] = {
109     {},
110     {1},
111     {},
112     {0.828981543588751,-0.657963087177502,0.828981543588751},
113     {},
114     {0.2967324292201065,0.2967324292201065,-0.186929716880426,0.2967324292201065,0.2967324292201065}
115 };*/
116
117 GMX_LIBGMX_EXPORT
118 int             gmx_nint(real a);
119 real    sign(real x,real y);
120
121 real    cuberoot (real a);
122 GMX_LIBGMX_EXPORT
123 double  gmx_erfd(double x);
124 double  gmx_erfcd(double x);
125 GMX_LIBGMX_EXPORT
126 float   gmx_erff(float x);
127 GMX_LIBGMX_EXPORT
128 float   gmx_erfcf(float x);
129 #ifdef GMX_DOUBLE
130 #define gmx_erf(x)   gmx_erfd(x)
131 #define gmx_erfc(x)  gmx_erfcd(x)
132 #else
133 #define gmx_erf(x)   gmx_erff(x)
134 #define gmx_erfc(x)  gmx_erfcf(x)
135 #endif
136
137 GMX_LIBGMX_EXPORT
138 gmx_bool gmx_isfinite(real x);
139
140 /*! \brief Check if two numbers are within a tolerance
141  *
142  *  This routine checks if the relative difference between two numbers is
143  *  approximately within the given tolerance, defined as
144  *  fabs(f1-f2)<=tolerance*fabs(f1+f2).
145  *
146  *  To check if two floating-point numbers are almost identical, use this routine 
147  *  with the tolerance GMX_REAL_EPS, or GMX_DOUBLE_EPS if the check should be
148  *  done in double regardless of Gromacs precision.
149  *  
150  *  To check if two algorithms produce similar results you will normally need
151  *  to relax the tolerance significantly since many operations (e.g. summation)
152  *  accumulate floating point errors.
153  *
154  *  \param f1  First number to compare
155  *  \param f2  Second number to compare
156  *  \param tol Tolerance to use
157  *
158  *  \return 1 if the relative difference is within tolerance, 0 if not.
159  */
160 static int
161 gmx_within_tol(double   f1,
162                double   f2,
163                double   tol)
164 {
165     /* The or-equal is important - otherwise we return false if f1==f2==0 */
166     if( fabs(f1-f2) <= tol*0.5*(fabs(f1)+fabs(f2)) )
167     {
168         return 1;
169     }
170     else
171     {
172         return 0;
173     }
174 }
175
176
177
178 /** 
179  * Check if a number is smaller than some preset safe minimum
180  * value, currently defined as GMX_REAL_MIN/GMX_REAL_EPS.
181  *
182  * If a number is smaller than this value we risk numerical overflow
183  * if any number larger than 1.0/GMX_REAL_EPS is divided by it.
184  *
185  * \return 1  if 'almost' numerically zero, 0 otherwise.
186  */
187 static int
188 gmx_numzero(double a)
189 {
190   return gmx_within_tol(a,0.0,GMX_REAL_MIN/GMX_REAL_EPS);
191 }
192
193
194 static real
195 gmx_log2(real x)
196 {
197   const real iclog2 = 1.0/log( 2.0 );
198
199     return log( x ) * iclog2;
200 }
201
202 /*! /brief Multiply two large ints
203  *
204  *  Returns true when overflow did not occur.
205  */
206 GMX_LIBGMX_EXPORT
207 gmx_bool
208 check_int_multiply_for_overflow(gmx_large_int_t a,
209                                 gmx_large_int_t b,
210                                 gmx_large_int_t *result);
211
212 #ifdef __cplusplus
213 }
214 #endif
215
216 #endif  /* _maths_h */