Remove unnecessary config.h includes
[alexxy/gromacs.git] / src / gromacs / mdlib / wall.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-2008, 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
38 #include "gmxpre.h"
39
40 #include <math.h>
41 #include <string.h>
42
43 #include "gromacs/legacyheaders/typedefs.h"
44 #include "gromacs/legacyheaders/macros.h"
45 #include "gromacs/legacyheaders/force.h"
46 #include "gromacs/legacyheaders/nrnb.h"
47 #include "gromacs/math/vec.h"
48
49 #include "gromacs/fileio/filenm.h"
50 #include "gromacs/math/utilities.h"
51 #include "gromacs/utility/cstringutil.h"
52 #include "gromacs/utility/smalloc.h"
53
54 void make_wall_tables(FILE *fplog, const output_env_t oenv,
55                       const t_inputrec *ir, const char *tabfn,
56                       const gmx_groups_t *groups,
57                       t_forcerec *fr)
58 {
59     int           w, negp_pp, egp, i, j;
60     int          *nm_ind;
61     char          buf[STRLEN];
62     t_forcetable *tab;
63
64     negp_pp = ir->opts.ngener - ir->nwall;
65     nm_ind  = groups->grps[egcENER].nm_ind;
66
67     if (fplog)
68     {
69         fprintf(fplog, "Reading user tables for %d energy groups with %d walls\n",
70                 negp_pp, ir->nwall);
71     }
72
73     snew(fr->wall_tab, ir->nwall);
74     for (w = 0; w < ir->nwall; w++)
75     {
76         snew(fr->wall_tab[w], negp_pp);
77         for (egp = 0; egp < negp_pp; egp++)
78         {
79             /* If the energy group pair is excluded, we don't need a table */
80             if (!(fr->egp_flags[egp*ir->opts.ngener+negp_pp+w] & EGP_EXCL))
81             {
82                 tab = &fr->wall_tab[w][egp];
83                 sprintf(buf, "%s", tabfn);
84                 sprintf(buf + strlen(tabfn) - strlen(ftp2ext(efXVG)) - 1, "_%s_%s.%s",
85                         *groups->grpname[nm_ind[egp]],
86                         *groups->grpname[nm_ind[negp_pp+w]],
87                         ftp2ext(efXVG));
88                 *tab = make_tables(fplog, oenv, fr, FALSE, buf, 0, GMX_MAKETABLES_FORCEUSER);
89                 /* Since wall have no charge, we can compress the table */
90                 for (i = 0; i <= tab->n; i++)
91                 {
92                     for (j = 0; j < 8; j++)
93                     {
94                         tab->data[8*i+j] = tab->data[12*i+4+j];
95                     }
96                 }
97             }
98         }
99     }
100 }
101
102 static void wall_error(int a, rvec *x, real r)
103 {
104     gmx_fatal(FARGS,
105               "An atom is beyond the wall: coordinates %f %f %f, distance %f\n"
106               "You might want to use the mdp option wall_r_linpot",
107               x[a][XX], x[a][YY], x[a][ZZ], r);
108 }
109
110 real do_walls(t_inputrec *ir, t_forcerec *fr, matrix box, t_mdatoms *md,
111               rvec x[], rvec f[], real lambda, real Vlj[], t_nrnb *nrnb)
112 {
113     int             nwall, w, lam, i;
114     int             ntw[2], at, ntype, ngid, ggid, *egp_flags, *type;
115     real           *nbfp, lamfac, fac_d[2], fac_r[2], Cd, Cr, Vtot, Fwall[2];
116     real            wall_z[2], r, mr, r1, r2, r4, Vd, Vr, V = 0, Fd, Fr, F = 0, dvdlambda;
117     dvec            xf_z;
118     int             n0, nnn;
119     real            tabscale, *VFtab, rt, eps, eps2, Yt, Ft, Geps, Heps, Heps2, Fp, VV, FF;
120     unsigned short *gid = md->cENER;
121     t_forcetable   *tab;
122
123     nwall     = ir->nwall;
124     ngid      = ir->opts.ngener;
125     ntype     = fr->ntype;
126     nbfp      = fr->nbfp;
127     egp_flags = fr->egp_flags;
128
129     for (w = 0; w < nwall; w++)
130     {
131         ntw[w] = 2*ntype*ir->wall_atomtype[w];
132         switch (ir->wall_type)
133         {
134             case ewt93:
135                 fac_d[w] = ir->wall_density[w]*M_PI/6;
136                 fac_r[w] = ir->wall_density[w]*M_PI/45;
137                 break;
138             case ewt104:
139                 fac_d[w] = ir->wall_density[w]*M_PI/2;
140                 fac_r[w] = ir->wall_density[w]*M_PI/5;
141                 break;
142             default:
143                 break;
144         }
145         Fwall[w] = 0;
146     }
147     wall_z[0] = 0;
148     wall_z[1] = box[ZZ][ZZ];
149
150     Vtot      = 0;
151     dvdlambda = 0;
152     clear_dvec(xf_z);
153     for (lam = 0; lam < (md->nPerturbed ? 2 : 1); lam++)
154     {
155         if (md->nPerturbed)
156         {
157             if (lam == 0)
158             {
159                 lamfac = 1 - lambda;
160                 type   = md->typeA;
161             }
162             else
163             {
164                 lamfac = lambda;
165                 type   = md->typeB;
166             }
167         }
168         else
169         {
170             lamfac = 1;
171             type   = md->typeA;
172         }
173         for (i = 0; i < md->homenr; i++)
174         {
175             for (w = 0; w < nwall; w++)
176             {
177                 /* The wall energy groups are always at the end of the list */
178                 ggid = gid[i]*ngid + ngid - nwall + w;
179                 at   = type[i];
180                 /* nbfp now includes the 6.0/12.0 derivative prefactors */
181                 Cd = nbfp[ntw[w]+2*at]/6.0;
182                 Cr = nbfp[ntw[w]+2*at+1]/12.0;
183                 if (!((Cd == 0 && Cr == 0) || (egp_flags[ggid] & EGP_EXCL)))
184                 {
185                     if (w == 0)
186                     {
187                         r = x[i][ZZ];
188                     }
189                     else
190                     {
191                         r = wall_z[1] - x[i][ZZ];
192                     }
193                     if (r < ir->wall_r_linpot)
194                     {
195                         mr = ir->wall_r_linpot - r;
196                         r  = ir->wall_r_linpot;
197                     }
198                     else
199                     {
200                         mr = 0;
201                     }
202                     switch (ir->wall_type)
203                     {
204                         case ewtTABLE:
205                             if (r < 0)
206                             {
207                                 wall_error(i, x, r);
208                             }
209                             tab      = &(fr->wall_tab[w][gid[i]]);
210                             tabscale = tab->scale;
211                             VFtab    = tab->data;
212
213                             rt    = r*tabscale;
214                             n0    = rt;
215                             if (n0 >= tab->n)
216                             {
217                                 /* Beyond the table range, set V and F to zero */
218                                 V     = 0;
219                                 F     = 0;
220                             }
221                             else
222                             {
223                                 eps   = rt - n0;
224                                 eps2  = eps*eps;
225                                 /* Dispersion */
226                                 nnn   = 8*n0;
227                                 Yt    = VFtab[nnn];
228                                 Ft    = VFtab[nnn+1];
229                                 Geps  = VFtab[nnn+2]*eps;
230                                 Heps2 = VFtab[nnn+3]*eps2;
231                                 Fp    = Ft + Geps + Heps2;
232                                 VV    = Yt + Fp*eps;
233                                 FF    = Fp + Geps + 2.0*Heps2;
234                                 Vd    = Cd*VV;
235                                 Fd    = Cd*FF;
236                                 /* Repulsion */
237                                 nnn   = nnn + 4;
238                                 Yt    = VFtab[nnn];
239                                 Ft    = VFtab[nnn+1];
240                                 Geps  = VFtab[nnn+2]*eps;
241                                 Heps2 = VFtab[nnn+3]*eps2;
242                                 Fp    = Ft + Geps + Heps2;
243                                 VV    = Yt + Fp*eps;
244                                 FF    = Fp + Geps + 2.0*Heps2;
245                                 Vr    = Cr*VV;
246                                 Fr    = Cr*FF;
247                                 V     = Vd + Vr;
248                                 F     = -lamfac*(Fd + Fr)*tabscale;
249                             }
250                             break;
251                         case ewt93:
252                             if (r <= 0)
253                             {
254                                 wall_error(i, x, r);
255                             }
256                             r1 = 1/r;
257                             r2 = r1*r1;
258                             r4 = r2*r2;
259                             Vd = fac_d[w]*Cd*r2*r1;
260                             Vr = fac_r[w]*Cr*r4*r4*r1;
261                             V  = Vr - Vd;
262                             F  = lamfac*(9*Vr - 3*Vd)*r1;
263                             break;
264                         case ewt104:
265                             if (r <= 0)
266                             {
267                                 wall_error(i, x, r);
268                             }
269                             r1 = 1/r;
270                             r2 = r1*r1;
271                             r4 = r2*r2;
272                             Vd = fac_d[w]*Cd*r4;
273                             Vr = fac_r[w]*Cr*r4*r4*r2;
274                             V  = Vr - Vd;
275                             F  = lamfac*(10*Vr - 4*Vd)*r1;
276                             break;
277                         case ewt126:
278                             if (r <= 0)
279                             {
280                                 wall_error(i, x, r);
281                             }
282                             r1 = 1/r;
283                             r2 = r1*r1;
284                             r4 = r2*r2;
285                             Vd = Cd*r4*r2;
286                             Vr = Cr*r4*r4*r4;
287                             V  = Vr - Vd;
288                             F  = lamfac*(12*Vr - 6*Vd)*r1;
289                             break;
290                         default:
291                             break;
292                     }
293                     if (mr > 0)
294                     {
295                         V += mr*F;
296                     }
297                     if (w == 1)
298                     {
299                         F = -F;
300                     }
301                     Vlj[ggid] += lamfac*V;
302                     Vtot      += V;
303                     f[i][ZZ]  += F;
304                     /* Because of the single sum virial calculation we need
305                      * to add  the full virial contribution of the walls.
306                      * Since the force only has a z-component, there is only
307                      * a contribution to the z component of the virial tensor.
308                      * We could also determine the virial contribution directly,
309                      * which would be cheaper here, but that would require extra
310                      * communication for f_novirsum for with virtual sites
311                      * in parallel.
312                      */
313                     xf_z[XX]  -= x[i][XX]*F;
314                     xf_z[YY]  -= x[i][YY]*F;
315                     xf_z[ZZ]  -= wall_z[w]*F;
316                 }
317             }
318         }
319         if (md->nPerturbed)
320         {
321             dvdlambda += (lam == 0 ? -1 : 1)*Vtot;
322         }
323
324         inc_nrnb(nrnb, eNR_WALLS, md->homenr);
325     }
326
327     for (i = 0; i < DIM; i++)
328     {
329         fr->vir_wall_z[i] = -0.5*xf_z[i];
330     }
331
332     return dvdlambda;
333 }