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