Convert t_forcetable to C++
[alexxy/gromacs.git] / src / gromacs / mdlib / wall.cpp
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,2015,2016,2017 by the GROMACS development team.
7  * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source 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 #include "gmxpre.h"
40
41 #include "wall.h"
42
43 #include <cstring>
44
45 #include <algorithm>
46
47 #include "gromacs/fileio/filetypes.h"
48 #include "gromacs/gmxlib/nrnb.h"
49 #include "gromacs/math/utilities.h"
50 #include "gromacs/math/vec.h"
51 #include "gromacs/mdtypes/forceoutput.h"
52 #include "gromacs/mdtypes/forcerec.h"
53 #include "gromacs/mdtypes/inputrec.h"
54 #include "gromacs/mdtypes/md_enums.h"
55 #include "gromacs/mdtypes/mdatom.h"
56 #include "gromacs/mdtypes/nblist.h"
57 #include "gromacs/tables/forcetable.h"
58 #include "gromacs/topology/topology.h"
59 #include "gromacs/utility/cstringutil.h"
60 #include "gromacs/utility/fatalerror.h"
61 #include "gromacs/utility/smalloc.h"
62
63 void make_wall_tables(FILE*                   fplog,
64                       const t_inputrec*       ir,
65                       const char*             tabfn,
66                       const SimulationGroups* groups,
67                       t_forcerec*             fr)
68 {
69     int  negp_pp;
70     char buf[STRLEN];
71
72     negp_pp                         = ir->opts.ngener - ir->nwall;
73     gmx::ArrayRef<const int> nm_ind = groups->groups[SimulationAtomGroupType::EnergyOutput];
74
75     if (fplog)
76     {
77         fprintf(fplog, "Reading user tables for %d energy groups with %d walls\n", negp_pp, ir->nwall);
78     }
79
80     snew(fr->wall_tab, ir->nwall);
81     for (int w = 0; w < ir->nwall; w++)
82     {
83         snew(fr->wall_tab[w], negp_pp);
84         for (int egp = 0; egp < negp_pp; egp++)
85         {
86             /* If the energy group pair is excluded, we don't need a table */
87             if (!(fr->egp_flags[egp * ir->opts.ngener + negp_pp + w] & EGP_EXCL))
88             {
89                 sprintf(buf, "%s", tabfn);
90                 sprintf(buf + strlen(tabfn) - strlen(ftp2ext(efXVG)) - 1, "_%s_%s.%s",
91                         *groups->groupNames[nm_ind[egp]], *groups->groupNames[nm_ind[negp_pp + w]],
92                         ftp2ext(efXVG));
93                 fr->wall_tab[w][egp] = make_tables(fplog, fr->ic, buf, 0, GMX_MAKETABLES_FORCEUSER);
94
95                 /* Since wall have no charge, we can compress the table */
96                 for (int i = 0; i <= fr->wall_tab[w][egp]->n; i++)
97                 {
98                     for (int j = 0; j < 8; j++)
99                     {
100                         fr->wall_tab[w][egp]->data[8 * i + j] =
101                                 fr->wall_tab[w][egp]->data[12 * i + 4 + j];
102                     }
103                 }
104             }
105         }
106     }
107 }
108
109 [[noreturn]] static void wall_error(int a, const rvec* x, real r)
110 {
111     gmx_fatal(FARGS,
112               "An atom is beyond the wall: coordinates %f %f %f, distance %f\n"
113               "You might want to use the mdp option wall_r_linpot",
114               x[a][XX], x[a][YY], x[a][ZZ], r);
115 }
116
117 static void tableForce(real r, const t_forcetable& tab, real Cd, real Cr, real* V, real* F)
118 {
119     const real  tabscale = tab.scale;
120     const real* VFtab    = tab.data.data();
121
122     real rt = r * tabscale;
123     int  n0 = static_cast<int>(rt);
124     if (n0 >= tab.n)
125     {
126         /* Beyond the table range, set V and F to zero */
127         *V = 0;
128         *F = 0;
129     }
130     else
131     {
132         real eps  = rt - n0;
133         real eps2 = eps * eps;
134         /* Dispersion */
135         int  nnn   = 8 * n0;
136         real Yt    = VFtab[nnn];
137         real Ft    = VFtab[nnn + 1];
138         real Geps  = VFtab[nnn + 2] * eps;
139         real Heps2 = VFtab[nnn + 3] * eps2;
140         real Fp    = Ft + Geps + Heps2;
141         real VV    = Yt + Fp * eps;
142         real FF    = Fp + Geps + 2.0 * Heps2;
143         real Vd    = 6 * Cd * VV;
144         real Fd    = 6 * Cd * FF;
145         /* Repulsion */
146         nnn     = nnn + 4;
147         Yt      = VFtab[nnn];
148         Ft      = VFtab[nnn + 1];
149         Geps    = VFtab[nnn + 2] * eps;
150         Heps2   = VFtab[nnn + 3] * eps2;
151         Fp      = Ft + Geps + Heps2;
152         VV      = Yt + Fp * eps;
153         FF      = Fp + Geps + 2.0 * Heps2;
154         real Vr = 12 * Cr * VV;
155         real Fr = 12 * Cr * FF;
156         *V      = Vd + Vr;
157         *F      = -(Fd + Fr) * tabscale;
158     }
159 }
160
161 real do_walls(const t_inputrec&     ir,
162               const t_forcerec&     fr,
163               const matrix          box,
164               const t_mdatoms&      md,
165               const rvec*           x,
166               gmx::ForceWithVirial* forceWithVirial,
167               real                  lambda,
168               real                  Vlj[],
169               t_nrnb*               nrnb)
170 {
171     constexpr real sixth   = 1.0 / 6.0;
172     constexpr real twelfth = 1.0 / 12.0;
173
174     int                   ntw[2];
175     real                  fac_d[2], fac_r[2];
176     const unsigned short* gid = md.cENER;
177
178     const int   nwall     = ir.nwall;
179     const int   ngid      = ir.opts.ngener;
180     const int   ntype     = fr.ntype;
181     const real* nbfp      = fr.nbfp.data();
182     const int*  egp_flags = fr.egp_flags;
183
184     for (int w = 0; w < nwall; w++)
185     {
186         ntw[w] = 2 * ntype * ir.wall_atomtype[w];
187         switch (ir.wall_type)
188         {
189             case ewt93:
190                 fac_d[w] = ir.wall_density[w] * M_PI / 6;
191                 fac_r[w] = ir.wall_density[w] * M_PI / 45;
192                 break;
193             case ewt104:
194                 fac_d[w] = ir.wall_density[w] * M_PI / 2;
195                 fac_r[w] = ir.wall_density[w] * M_PI / 5;
196                 break;
197             default: break;
198         }
199     }
200     const real wall_z[2] = { 0, box[ZZ][ZZ] };
201
202     rvec* gmx_restrict f = as_rvec_array(forceWithVirial->force_.data());
203
204     real   dvdlambda = 0;
205     double sumRF     = 0;
206     for (int lam = 0; lam < (md.nPerturbed ? 2 : 1); lam++)
207     {
208         real       lamfac;
209         const int* type;
210         if (md.nPerturbed)
211         {
212             if (lam == 0)
213             {
214                 lamfac = 1 - lambda;
215                 type   = md.typeA;
216             }
217             else
218             {
219                 lamfac = lambda;
220                 type   = md.typeB;
221             }
222         }
223         else
224         {
225             lamfac = 1;
226             type   = md.typeA;
227         }
228
229         real Vlambda = 0;
230         for (int i = 0; i < md.homenr; i++)
231         {
232             for (int w = 0; w < std::min(nwall, 2); w++)
233             {
234                 /* The wall energy groups are always at the end of the list */
235                 const int ggid = gid[i] * ngid + ngid - nwall + w;
236                 const int at   = type[i];
237                 /* nbfp now includes the 6/12 derivative prefactors */
238                 const real Cd = nbfp[ntw[w] + 2 * at] * sixth;
239                 const real Cr = nbfp[ntw[w] + 2 * at + 1] * twelfth;
240                 if (!((Cd == 0 && Cr == 0) || (egp_flags[ggid] & EGP_EXCL)))
241                 {
242                     real r, mr;
243                     if (w == 0)
244                     {
245                         r = x[i][ZZ];
246                     }
247                     else
248                     {
249                         r = wall_z[1] - x[i][ZZ];
250                     }
251                     if (r < ir.wall_r_linpot)
252                     {
253                         mr = ir.wall_r_linpot - r;
254                         r  = ir.wall_r_linpot;
255                     }
256                     else
257                     {
258                         mr = 0;
259                     }
260                     if (r <= 0)
261                     {
262                         wall_error(i, x, r);
263                     }
264
265                     real V, F;
266                     real r1, r2, r4, Vd, Vr;
267                     switch (ir.wall_type)
268                     {
269                         case ewtTABLE:
270                             tableForce(r, *fr.wall_tab[w][gid[i]], Cd, Cr, &V, &F);
271                             F *= lamfac;
272                             break;
273                         case ewt93:
274                             r1 = 1 / r;
275                             r2 = r1 * r1;
276                             r4 = r2 * r2;
277                             Vd = fac_d[w] * Cd * r2 * r1;
278                             Vr = fac_r[w] * Cr * r4 * r4 * r1;
279                             V  = Vr - Vd;
280                             F  = lamfac * (9 * Vr - 3 * Vd) * r1;
281                             break;
282                         case ewt104:
283                             r1 = 1 / r;
284                             r2 = r1 * r1;
285                             r4 = r2 * r2;
286                             Vd = fac_d[w] * Cd * r4;
287                             Vr = fac_r[w] * Cr * r4 * r4 * r2;
288                             V  = Vr - Vd;
289                             F  = lamfac * (10 * Vr - 4 * Vd) * r1;
290                             break;
291                         case ewt126:
292                             r1 = 1 / r;
293                             r2 = r1 * r1;
294                             r4 = r2 * r2;
295                             Vd = Cd * r4 * r2;
296                             Vr = Cr * r4 * r4 * r4;
297                             V  = Vr - Vd;
298                             F  = lamfac * (12 * Vr - 6 * Vd) * r1;
299                             break;
300                         default:
301                             V = 0;
302                             F = 0;
303                             break;
304                     }
305                     if (mr > 0)
306                     {
307                         V += mr * F;
308                     }
309                     sumRF += r * F;
310                     if (w == 1)
311                     {
312                         F = -F;
313                     }
314                     Vlj[ggid] += lamfac * V;
315                     Vlambda += V;
316                     f[i][ZZ] += F;
317                 }
318             }
319         }
320         if (md.nPerturbed)
321         {
322             dvdlambda += (lam == 0 ? -1 : 1) * Vlambda;
323         }
324
325         inc_nrnb(nrnb, eNR_WALLS, md.homenr);
326     }
327
328     if (forceWithVirial->computeVirial_)
329     {
330         rvec virial = { 0, 0, static_cast<real>(-0.5 * sumRF) };
331         forceWithVirial->addVirialContribution(virial);
332     }
333
334     return dvdlambda;
335 }