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