3d90b1ae9c8aa6af93414d2510bef57a05a7a6f9
[alexxy/gromacs.git] / src / gromacs / pulling / pullutil.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 "config.h"
40
41 #include <stdlib.h>
42
43 #include "gromacs/utility/futil.h"
44 #include "gromacs/math/vec.h"
45 #include "gromacs/utility/smalloc.h"
46 #include "gromacs/legacyheaders/typedefs.h"
47 #include "gromacs/legacyheaders/types/commrec.h"
48 #include "gromacs/legacyheaders/names.h"
49 #include "gromacs/utility/fatalerror.h"
50 #include "gromacs/legacyheaders/macros.h"
51 #include "gromacs/fileio/confio.h"
52 #include "gromacs/legacyheaders/network.h"
53 #include "gromacs/pbcutil/pbc.h"
54 #include "pull.h"
55 #include "gromacs/legacyheaders/gmx_ga2la.h"
56
57 static void pull_set_pbcatom(t_commrec *cr, t_pull_group *pgrp,
58                              rvec *x,
59                              rvec x_pbc)
60 {
61     int a, m;
62
63     if (cr != NULL && DOMAINDECOMP(cr))
64     {
65         if (ga2la_get_home(cr->dd->ga2la, pgrp->pbcatom, &a))
66         {
67             copy_rvec(x[a], x_pbc);
68         }
69         else
70         {
71             clear_rvec(x_pbc);
72         }
73     }
74     else
75     {
76         copy_rvec(x[pgrp->pbcatom], x_pbc);
77     }
78 }
79
80 static void pull_set_pbcatoms(t_commrec *cr, t_pull *pull,
81                               rvec *x,
82                               rvec *x_pbc)
83 {
84     int g, n, m;
85
86     n = 0;
87     for (g = 0; g < pull->ngroup; g++)
88     {
89         if ((g == 0 && PULL_CYL(pull)) || pull->group[g].pbcatom == -1)
90         {
91             clear_rvec(x_pbc[g]);
92         }
93         else
94         {
95             pull_set_pbcatom(cr, &pull->group[g], x, x_pbc[g]);
96             for (m = 0; m < DIM; m++)
97             {
98                 if (pull->dim[m] == 0)
99                 {
100                     x_pbc[g][m] = 0.0;
101                 }
102             }
103             n++;
104         }
105     }
106
107     if (cr && PAR(cr) && n > 0)
108     {
109         /* Sum over the nodes to get x_pbc from the home node of pbcatom */
110         gmx_sum(pull->ngroup*DIM, x_pbc[0], cr);
111     }
112 }
113
114 /* switch function, x between r and w */
115 static real get_weight(real x, real r1, real r0)
116 {
117     real weight;
118
119     if (x >= r0)
120     {
121         weight = 0;
122     }
123     else if (x <= r1)
124     {
125         weight = 1;
126     }
127     else
128     {
129         weight = (r0 - x)/(r0 - r1);
130     }
131
132     return weight;
133 }
134
135 static void make_cyl_refgrps(t_commrec *cr, t_pull *pull, t_mdatoms *md,
136                              t_pbc *pbc, double t, rvec *x, rvec *xp)
137 {
138     int           c, i, ii, m, start, end;
139     rvec          g_x, dx, dir;
140     double        r0_2, sum_a, sum_ap, dr2, mass, weight, wmass, wwmass, inp;
141     t_pull_coord *pcrd;
142     t_pull_group *pref, *pgrp, *pdyna;
143     gmx_ga2la_t   ga2la = NULL;
144
145     if (pull->dbuf_cyl == NULL)
146     {
147         snew(pull->dbuf_cyl, pull->ncoord*4);
148     }
149
150     if (cr && DOMAINDECOMP(cr))
151     {
152         ga2la = cr->dd->ga2la;
153     }
154
155     start = 0;
156     end   = md->homenr;
157
158     r0_2 = dsqr(pull->cyl_r0);
159
160     /* loop over all groups to make a reference group for each*/
161     for (c = 0; c < pull->ncoord; c++)
162     {
163         pcrd  = &pull->coord[c];
164
165         /* pref will be the same group for all pull coordinates */
166         pref  = &pull->group[pcrd->group[0]];
167         pgrp  = &pull->group[pcrd->group[1]];
168         pdyna = &pull->dyna[c];
169         copy_rvec(pcrd->vec, dir);
170         sum_a          = 0;
171         sum_ap         = 0;
172         wmass          = 0;
173         wwmass         = 0;
174         pdyna->nat_loc = 0;
175
176         for (m = 0; m < DIM; m++)
177         {
178             g_x[m] = pgrp->x[m] - pcrd->vec[m]*(pcrd->init + pcrd->rate*t);
179         }
180
181         /* loop over all atoms in the main ref group */
182         for (i = 0; i < pref->nat; i++)
183         {
184             ii = pref->ind[i];
185             if (ga2la)
186             {
187                 if (!ga2la_get_home(ga2la, pref->ind[i], &ii))
188                 {
189                     ii = -1;
190                 }
191             }
192             if (ii >= start && ii < end)
193             {
194                 pbc_dx_aiuc(pbc, x[ii], g_x, dx);
195                 inp = iprod(dir, dx);
196                 dr2 = 0;
197                 for (m = 0; m < DIM; m++)
198                 {
199                     dr2 += dsqr(dx[m] - inp*dir[m]);
200                 }
201
202                 if (dr2 < r0_2)
203                 {
204                     /* add to index, to sum of COM, to weight array */
205                     if (pdyna->nat_loc >= pdyna->nalloc_loc)
206                     {
207                         pdyna->nalloc_loc = over_alloc_large(pdyna->nat_loc+1);
208                         srenew(pdyna->ind_loc, pdyna->nalloc_loc);
209                         srenew(pdyna->weight_loc, pdyna->nalloc_loc);
210                     }
211                     pdyna->ind_loc[pdyna->nat_loc] = ii;
212                     mass   = md->massT[ii];
213                     weight = get_weight(sqrt(dr2), pull->cyl_r1, pull->cyl_r0);
214                     pdyna->weight_loc[pdyna->nat_loc] = weight;
215                     sum_a += mass*weight*inp;
216                     if (xp)
217                     {
218                         pbc_dx_aiuc(pbc, xp[ii], g_x, dx);
219                         inp     = iprod(dir, dx);
220                         sum_ap += mass*weight*inp;
221                     }
222                     wmass  += mass*weight;
223                     wwmass += mass*sqr(weight);
224                     pdyna->nat_loc++;
225                 }
226             }
227         }
228         pull->dbuf_cyl[c*4+0] = wmass;
229         pull->dbuf_cyl[c*4+1] = wwmass;
230         pull->dbuf_cyl[c*4+2] = sum_a;
231         pull->dbuf_cyl[c*4+3] = sum_ap;
232     }
233
234     if (cr && PAR(cr))
235     {
236         /* Sum the contributions over the nodes */
237         gmx_sumd(pull->ncoord*4, pull->dbuf_cyl, cr);
238     }
239
240     for (c = 0; c < pull->ncoord; c++)
241     {
242         pcrd  = &pull->coord[c];
243
244         pdyna = &pull->dyna[c];
245         pgrp  = &pull->group[pcrd->group[1]];
246
247         wmass         = pull->dbuf_cyl[c*4+0];
248         wwmass        = pull->dbuf_cyl[c*4+1];
249         pdyna->wscale = wmass/wwmass;
250         pdyna->invtm  = 1.0/(pdyna->wscale*wmass);
251
252         for (m = 0; m < DIM; m++)
253         {
254             g_x[m]      = pgrp->x[m] - pcrd->vec[m]*(pcrd->init + pcrd->rate*t);
255             pdyna->x[m] = g_x[m] + pcrd->vec[m]*pull->dbuf_cyl[c*4+2]/wmass;
256             if (xp)
257             {
258                 pdyna->xp[m] = g_x[m] + pcrd->vec[m]*pull->dbuf_cyl[c*4+3]/wmass;
259             }
260         }
261
262         if (debug)
263         {
264             fprintf(debug, "Pull cylinder group %d:%8.3f%8.3f%8.3f m:%8.3f\n",
265                     c, pdyna->x[0], pdyna->x[1],
266                     pdyna->x[2], 1.0/pdyna->invtm);
267         }
268     }
269 }
270
271 static double atan2_0_2pi(double y, double x)
272 {
273     double a;
274
275     a = atan2(y, x);
276     if (a < 0)
277     {
278         a += 2.0*M_PI;
279     }
280     return a;
281 }
282
283 /* calculates center of mass of selection index from all coordinates x */
284 void pull_calc_coms(t_commrec *cr,
285                     t_pull *pull, t_mdatoms *md, t_pbc *pbc, double t,
286                     rvec x[], rvec *xp)
287 {
288     int           g, i, ii, m;
289     real          mass, w, wm, twopi_box = 0;
290     double        wmass, wwmass, invwmass;
291     dvec          com, comp;
292     double        cm, sm, cmp, smp, ccm, csm, ssm, csw, snw;
293     rvec         *xx[2], x_pbc = {0, 0, 0}, dx;
294     t_pull_group *pgrp;
295
296     if (pull->rbuf == NULL)
297     {
298         snew(pull->rbuf, pull->ngroup);
299     }
300     if (pull->dbuf == NULL)
301     {
302         snew(pull->dbuf, 3*pull->ngroup);
303     }
304
305     if (pull->bRefAt && pull->bSetPBCatoms)
306     {
307         pull_set_pbcatoms(cr, pull, x, pull->rbuf);
308
309         if (cr != NULL && DOMAINDECOMP(cr))
310         {
311             /* We can keep these PBC reference coordinates fixed for nstlist
312              * steps, since atoms won't jump over PBC.
313              * This avoids a global reduction at the next nstlist-1 steps.
314              * Note that the exact values of the pbc reference coordinates
315              * are irrelevant, as long all atoms in the group are within
316              * half a box distance of the reference coordinate.
317              */
318             pull->bSetPBCatoms = FALSE;
319         }
320     }
321
322     if (pull->cosdim >= 0)
323     {
324         for (m = pull->cosdim+1; m < pull->npbcdim; m++)
325         {
326             if (pbc->box[m][pull->cosdim] != 0)
327             {
328                 gmx_fatal(FARGS, "Can not do cosine weighting for trilinic dimensions");
329             }
330         }
331         twopi_box = 2.0*M_PI/pbc->box[pull->cosdim][pull->cosdim];
332     }
333
334     for (g = 0; g < pull->ngroup; g++)
335     {
336         pgrp = &pull->group[g];
337         clear_dvec(com);
338         clear_dvec(comp);
339         wmass  = 0;
340         wwmass = 0;
341         cm     = 0;
342         sm     = 0;
343         cmp    = 0;
344         smp    = 0;
345         ccm    = 0;
346         csm    = 0;
347         ssm    = 0;
348         if (!(g == 0 && PULL_CYL(pull)))
349         {
350             if (pgrp->epgrppbc == epgrppbcREFAT)
351             {
352                 /* Set the pbc atom */
353                 copy_rvec(pull->rbuf[g], x_pbc);
354             }
355             w = 1;
356             for (i = 0; i < pgrp->nat_loc; i++)
357             {
358                 ii   = pgrp->ind_loc[i];
359                 mass = md->massT[ii];
360                 if (pgrp->epgrppbc != epgrppbcCOS)
361                 {
362                     if (pgrp->weight_loc)
363                     {
364                         w = pgrp->weight_loc[i];
365                     }
366                     wm      = w*mass;
367                     wmass  += wm;
368                     wwmass += wm*w;
369                     if (pgrp->epgrppbc == epgrppbcNONE)
370                     {
371                         /* Plain COM: sum the coordinates */
372                         for (m = 0; m < DIM; m++)
373                         {
374                             com[m]    += wm*x[ii][m];
375                         }
376                         if (xp)
377                         {
378                             for (m = 0; m < DIM; m++)
379                             {
380                                 comp[m] += wm*xp[ii][m];
381                             }
382                         }
383                     }
384                     else
385                     {
386                         /* Sum the difference with the reference atom */
387                         pbc_dx(pbc, x[ii], x_pbc, dx);
388                         for (m = 0; m < DIM; m++)
389                         {
390                             com[m]    += wm*dx[m];
391                         }
392                         if (xp)
393                         {
394                             /* For xp add the difference between xp and x to dx,
395                              * such that we use the same periodic image,
396                              * also when xp has a large displacement.
397                              */
398                             for (m = 0; m < DIM; m++)
399                             {
400                                 comp[m] += wm*(dx[m] + xp[ii][m] - x[ii][m]);
401                             }
402                         }
403                     }
404                 }
405                 else
406                 {
407                     /* Determine cos and sin sums */
408                     csw  = cos(x[ii][pull->cosdim]*twopi_box);
409                     snw  = sin(x[ii][pull->cosdim]*twopi_box);
410                     cm  += csw*mass;
411                     sm  += snw*mass;
412                     ccm += csw*csw*mass;
413                     csm += csw*snw*mass;
414                     ssm += snw*snw*mass;
415
416                     if (xp)
417                     {
418                         csw  = cos(xp[ii][pull->cosdim]*twopi_box);
419                         snw  = sin(xp[ii][pull->cosdim]*twopi_box);
420                         cmp += csw*mass;
421                         smp += snw*mass;
422                     }
423                 }
424             }
425         }
426
427         /* Copy local sums to a buffer for global summing */
428         switch (pgrp->epgrppbc)
429         {
430             case epgrppbcNONE:
431             case epgrppbcREFAT:
432                 copy_dvec(com, pull->dbuf[g*3]);
433                 copy_dvec(comp, pull->dbuf[g*3+1]);
434                 pull->dbuf[g*3+2][0] = wmass;
435                 pull->dbuf[g*3+2][1] = wwmass;
436                 pull->dbuf[g*3+2][2] = 0;
437                 break;
438             case epgrppbcCOS:
439                 pull->dbuf[g*3  ][0] = cm;
440                 pull->dbuf[g*3  ][1] = sm;
441                 pull->dbuf[g*3  ][2] = 0;
442                 pull->dbuf[g*3+1][0] = ccm;
443                 pull->dbuf[g*3+1][1] = csm;
444                 pull->dbuf[g*3+1][2] = ssm;
445                 pull->dbuf[g*3+2][0] = cmp;
446                 pull->dbuf[g*3+2][1] = smp;
447                 pull->dbuf[g*3+2][2] = 0;
448                 break;
449         }
450     }
451
452     if (cr && PAR(cr))
453     {
454         /* Sum the contributions over the nodes */
455         gmx_sumd(pull->ngroup*3*DIM, pull->dbuf[0], cr);
456     }
457
458     for (g = 0; g < pull->ngroup; g++)
459     {
460         pgrp = &pull->group[g];
461         if (pgrp->nat > 0 && !(g == 0 && PULL_CYL(pull)))
462         {
463             if (pgrp->epgrppbc != epgrppbcCOS)
464             {
465                 /* Determine the inverse mass */
466                 wmass    = pull->dbuf[g*3+2][0];
467                 wwmass   = pull->dbuf[g*3+2][1];
468                 invwmass = 1/wmass;
469                 /* invtm==0 signals a frozen group, so then we should keep it zero */
470                 if (pgrp->invtm > 0)
471                 {
472                     pgrp->wscale = wmass/wwmass;
473                     pgrp->invtm  = 1.0/(pgrp->wscale*wmass);
474                 }
475                 /* Divide by the total mass */
476                 for (m = 0; m < DIM; m++)
477                 {
478                     pgrp->x[m]    = pull->dbuf[g*3  ][m]*invwmass;
479                     if (xp)
480                     {
481                         pgrp->xp[m] = pull->dbuf[g*3+1][m]*invwmass;
482                     }
483                     if (pgrp->epgrppbc == epgrppbcREFAT)
484                     {
485                         pgrp->x[m]    += pull->rbuf[g][m];
486                         if (xp)
487                         {
488                             pgrp->xp[m] += pull->rbuf[g][m];
489                         }
490                     }
491                 }
492             }
493             else
494             {
495                 /* Determine the optimal location of the cosine weight */
496                 csw                   = pull->dbuf[g*3][0];
497                 snw                   = pull->dbuf[g*3][1];
498                 pgrp->x[pull->cosdim] = atan2_0_2pi(snw, csw)/twopi_box;
499                 /* Set the weights for the local atoms */
500                 wmass  = sqrt(csw*csw + snw*snw);
501                 wwmass = (pull->dbuf[g*3+1][0]*csw*csw +
502                           pull->dbuf[g*3+1][1]*csw*snw +
503                           pull->dbuf[g*3+1][2]*snw*snw)/(wmass*wmass);
504                 pgrp->wscale = wmass/wwmass;
505                 pgrp->invtm  = 1.0/(pgrp->wscale*wmass);
506                 /* Set the weights for the local atoms */
507                 csw *= pgrp->invtm;
508                 snw *= pgrp->invtm;
509                 for (i = 0; i < pgrp->nat_loc; i++)
510                 {
511                     ii                  = pgrp->ind_loc[i];
512                     pgrp->weight_loc[i] = csw*cos(twopi_box*x[ii][pull->cosdim]) +
513                         snw*sin(twopi_box*x[ii][pull->cosdim]);
514                 }
515                 if (xp)
516                 {
517                     csw                    = pull->dbuf[g*3+2][0];
518                     snw                    = pull->dbuf[g*3+2][1];
519                     pgrp->xp[pull->cosdim] = atan2_0_2pi(snw, csw)/twopi_box;
520                 }
521             }
522             if (debug)
523             {
524                 fprintf(debug, "Pull group %d wmass %f wwmass %f invtm %f\n",
525                         g, wmass, wwmass, pgrp->invtm);
526             }
527         }
528     }
529
530     if (PULL_CYL(pull))
531     {
532         /* Calculate the COMs for the cyclinder reference groups */
533         make_cyl_refgrps(cr, pull, md, pbc, t, x, xp);
534     }
535 }