Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / mdlib / ewald.c
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  * Copyright (c) 2013,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #include "gmxpre.h"
38
39 #include <math.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42
43 #include "gromacs/legacyheaders/coulomb.h"
44 #include "gromacs/legacyheaders/macros.h"
45 #include "gromacs/legacyheaders/typedefs.h"
46 #include "gromacs/legacyheaders/types/commrec.h"
47 #include "gromacs/math/gmxcomplex.h"
48 #include "gromacs/math/units.h"
49 #include "gromacs/math/vec.h"
50 #include "gromacs/utility/fatalerror.h"
51 #include "gromacs/utility/futil.h"
52 #include "gromacs/utility/smalloc.h"
53
54 #define TOL 2e-5
55
56 struct ewald_tab
57 {
58     int        nx, ny, nz, kmax;
59     cvec     **eir;
60     t_complex *tab_xy, *tab_qxyz;
61 };
62
63
64
65 /* TODO: fix thread-safety */
66
67 /* the other routines are in complex.h */
68 static t_complex conjmul(t_complex a, t_complex b)
69 {
70     t_complex c;
71
72     c.re = a.re*b.re + a.im*b.im;
73     c.im = a.im*b.re - a.re*b.im;
74
75     return c;
76 }
77
78
79
80
81 static void tabulate_eir(int natom, rvec x[], int kmax, cvec **eir, rvec lll)
82 {
83     int  i, j, m;
84
85     if (kmax < 1)
86     {
87         printf("Go away! kmax = %d\n", kmax);
88         exit(1);
89     }
90
91     for (i = 0; (i < natom); i++)
92     {
93         for (m = 0; (m < 3); m++)
94         {
95             eir[0][i][m].re = 1;
96             eir[0][i][m].im = 0;
97         }
98
99         for (m = 0; (m < 3); m++)
100         {
101             eir[1][i][m].re = cos(x[i][m]*lll[m]);
102             eir[1][i][m].im = sin(x[i][m]*lll[m]);
103         }
104         for (j = 2; (j < kmax); j++)
105         {
106             for (m = 0; (m < 3); m++)
107             {
108                 eir[j][i][m] = cmul(eir[j-1][i][m], eir[1][i][m]);
109             }
110         }
111     }
112 }
113
114 void init_ewald_tab(ewald_tab_t *et, const t_inputrec *ir, FILE *fp)
115 {
116     int n;
117
118     snew(*et, 1);
119     if (fp)
120     {
121         fprintf(fp, "Will do ordinary reciprocal space Ewald sum.\n");
122     }
123
124     (*et)->nx       = ir->nkx+1;
125     (*et)->ny       = ir->nky+1;
126     (*et)->nz       = ir->nkz+1;
127     (*et)->kmax     = max((*et)->nx, max((*et)->ny, (*et)->nz));
128     (*et)->eir      = NULL;
129     (*et)->tab_xy   = NULL;
130     (*et)->tab_qxyz = NULL;
131 }
132
133
134
135 real do_ewald(t_inputrec *ir,
136               rvec x[],        rvec f[],
137               real chargeA[],  real chargeB[],
138               rvec box,
139               t_commrec *cr,   int natoms,
140               matrix lrvir,    real ewaldcoeff,
141               real lambda,     real *dvdlambda,
142               ewald_tab_t et)
143 {
144     real     factor     = -1.0/(4*ewaldcoeff*ewaldcoeff);
145     real     scaleRecip = 4.0*M_PI/(box[XX]*box[YY]*box[ZZ])*ONE_4PI_EPS0/ir->epsilon_r; /* 1/(Vol*e0) */
146     real    *charge, energy_AB[2], energy;
147     rvec     lll;
148     int      lowiy, lowiz, ix, iy, iz, n, q;
149     real     tmp, cs, ss, ak, akv, mx, my, mz, m2, scale;
150     gmx_bool bFreeEnergy;
151
152     if (cr != NULL)
153     {
154         if (PAR(cr))
155         {
156             gmx_fatal(FARGS, "No parallel Ewald. Use PME instead.\n");
157         }
158     }
159
160
161     if (!et->eir) /* allocate if we need to */
162     {
163         snew(et->eir, et->kmax);
164         for (n = 0; n < et->kmax; n++)
165         {
166             snew(et->eir[n], natoms);
167         }
168         snew(et->tab_xy, natoms);
169         snew(et->tab_qxyz, natoms);
170     }
171
172     bFreeEnergy = (ir->efep != efepNO);
173
174     clear_mat(lrvir);
175
176     calc_lll(box, lll);
177     /* make tables for the structure factor parts */
178     tabulate_eir(natoms, x, et->kmax, et->eir, lll);
179
180     for (q = 0; q < (bFreeEnergy ? 2 : 1); q++)
181     {
182         if (!bFreeEnergy)
183         {
184             charge = chargeA;
185             scale  = 1.0;
186         }
187         else if (q == 0)
188         {
189             charge = chargeA;
190             scale  = 1.0 - lambda;
191         }
192         else
193         {
194             charge = chargeB;
195             scale  = lambda;
196         }
197         lowiy        = 0;
198         lowiz        = 1;
199         energy_AB[q] = 0;
200         for (ix = 0; ix < et->nx; ix++)
201         {
202             mx = ix*lll[XX];
203             for (iy = lowiy; iy < et->ny; iy++)
204             {
205                 my = iy*lll[YY];
206                 if (iy >= 0)
207                 {
208                     for (n = 0; n < natoms; n++)
209                     {
210                         et->tab_xy[n] = cmul(et->eir[ix][n][XX], et->eir[iy][n][YY]);
211                     }
212                 }
213                 else
214                 {
215                     for (n = 0; n < natoms; n++)
216                     {
217                         et->tab_xy[n] = conjmul(et->eir[ix][n][XX], et->eir[-iy][n][YY]);
218                     }
219                 }
220                 for (iz = lowiz; iz < et->nz; iz++)
221                 {
222                     mz  = iz*lll[ZZ];
223                     m2  = mx*mx+my*my+mz*mz;
224                     ak  = exp(m2*factor)/m2;
225                     akv = 2.0*ak*(1.0/m2-factor);
226                     if (iz >= 0)
227                     {
228                         for (n = 0; n < natoms; n++)
229                         {
230                             et->tab_qxyz[n] = rcmul(charge[n], cmul(et->tab_xy[n],
231                                                                     et->eir[iz][n][ZZ]));
232                         }
233                     }
234                     else
235                     {
236                         for (n = 0; n < natoms; n++)
237                         {
238                             et->tab_qxyz[n] = rcmul(charge[n], conjmul(et->tab_xy[n],
239                                                                        et->eir[-iz][n][ZZ]));
240                         }
241                     }
242
243                     cs = ss = 0;
244                     for (n = 0; n < natoms; n++)
245                     {
246                         cs += et->tab_qxyz[n].re;
247                         ss += et->tab_qxyz[n].im;
248                     }
249                     energy_AB[q]  += ak*(cs*cs+ss*ss);
250                     tmp            = scale*akv*(cs*cs+ss*ss);
251                     lrvir[XX][XX] -= tmp*mx*mx;
252                     lrvir[XX][YY] -= tmp*mx*my;
253                     lrvir[XX][ZZ] -= tmp*mx*mz;
254                     lrvir[YY][YY] -= tmp*my*my;
255                     lrvir[YY][ZZ] -= tmp*my*mz;
256                     lrvir[ZZ][ZZ] -= tmp*mz*mz;
257                     for (n = 0; n < natoms; n++)
258                     {
259                         /*tmp=scale*ak*(cs*tab_qxyz[n].im-ss*tab_qxyz[n].re);*/
260                         tmp       = scale*ak*(cs*et->tab_qxyz[n].im-ss*et->tab_qxyz[n].re);
261                         f[n][XX] += tmp*mx*2*scaleRecip;
262                         f[n][YY] += tmp*my*2*scaleRecip;
263                         f[n][ZZ] += tmp*mz*2*scaleRecip;
264 #if 0
265                         f[n][XX] += tmp*mx;
266                         f[n][YY] += tmp*my;
267                         f[n][ZZ] += tmp*mz;
268 #endif
269                     }
270                     lowiz = 1-et->nz;
271                 }
272                 lowiy = 1-et->ny;
273             }
274         }
275     }
276
277     if (!bFreeEnergy)
278     {
279         energy = energy_AB[0];
280     }
281     else
282     {
283         energy      = (1.0 - lambda)*energy_AB[0] + lambda*energy_AB[1];
284         *dvdlambda += scaleRecip*(energy_AB[1] - energy_AB[0]);
285     }
286
287     lrvir[XX][XX] = -0.5*scaleRecip*(lrvir[XX][XX]+energy);
288     lrvir[XX][YY] = -0.5*scaleRecip*(lrvir[XX][YY]);
289     lrvir[XX][ZZ] = -0.5*scaleRecip*(lrvir[XX][ZZ]);
290     lrvir[YY][YY] = -0.5*scaleRecip*(lrvir[YY][YY]+energy);
291     lrvir[YY][ZZ] = -0.5*scaleRecip*(lrvir[YY][ZZ]);
292     lrvir[ZZ][ZZ] = -0.5*scaleRecip*(lrvir[ZZ][ZZ]+energy);
293
294     lrvir[YY][XX] = lrvir[XX][YY];
295     lrvir[ZZ][XX] = lrvir[XX][ZZ];
296     lrvir[ZZ][YY] = lrvir[YY][ZZ];
297
298     energy *= scaleRecip;
299
300     return energy;
301 }