3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * GROwing Monsters And Cloning Shrimps
43 #include "gromacs/fileio/futil.h"
49 #include "gmx_fatal.h"
53 #include "gromacs/fileio/confio.h"
57 #include "gmx_ga2la.h"
59 static void pull_set_pbcatom(t_commrec *cr, t_pullgrp *pg,
60 t_mdatoms *md, rvec *x,
69 if (!ga2la_get_home(cr->dd->ga2la, pg->pbcatom, &a))
79 if (a >= md->start && a < md->start+md->homenr)
81 copy_rvec(x[a], x_pbc);
90 copy_rvec(x[pg->pbcatom], x_pbc);
94 static void pull_set_pbcatoms(t_commrec *cr, t_pull *pull,
95 t_mdatoms *md, rvec *x,
101 for (g = 0; g < 1+pull->ngrp; g++)
103 if ((g == 0 && PULL_CYL(pull)) || pull->grp[g].pbcatom == -1)
105 clear_rvec(x_pbc[g]);
109 pull_set_pbcatom(cr, &pull->grp[g], md, x, x_pbc[g]);
110 for (m = 0; m < DIM; m++)
112 if (pull->dim[m] == 0)
121 if (cr && PAR(cr) && n > 0)
123 /* Sum over the nodes to get x_pbc from the home node of pbcatom */
124 gmx_sum((1+pull->ngrp)*DIM, x_pbc[0], cr);
128 /* switch function, x between r and w */
129 static real get_weight(real x, real r1, real r0)
143 weight = (r0 - x)/(r0 - r1);
149 static void make_cyl_refgrps(t_commrec *cr, t_pull *pull, t_mdatoms *md,
150 t_pbc *pbc, double t, rvec *x, rvec *xp)
152 int g, i, ii, m, start, end;
154 double r0_2, sum_a, sum_ap, dr2, mass, weight, wmass, wwmass, inp;
155 t_pullgrp *pref, *pgrp, *pdyna;
156 gmx_ga2la_t ga2la = NULL;
158 if (pull->dbuf_cyl == NULL)
160 snew(pull->dbuf_cyl, pull->ngrp*4);
163 if (cr && DOMAINDECOMP(cr))
165 ga2la = cr->dd->ga2la;
169 end = md->homenr + start;
171 r0_2 = dsqr(pull->cyl_r0);
173 /* loop over all groups to make a reference group for each*/
174 pref = &pull->grp[0];
175 for (g = 1; g < 1+pull->ngrp; g++)
177 pgrp = &pull->grp[g];
178 pdyna = &pull->dyna[g];
179 copy_rvec(pgrp->vec, dir);
186 for (m = 0; m < DIM; m++)
188 g_x[m] = pgrp->x[m] - pgrp->vec[m]*(pgrp->init[0] + pgrp->rate*t);
191 /* loop over all atoms in the main ref group */
192 for (i = 0; i < pref->nat; i++)
194 ii = pull->grp[0].ind[i];
197 if (!ga2la_get_home(ga2la, pref->ind[i], &ii))
202 if (ii >= start && ii < end)
204 pbc_dx_aiuc(pbc, x[ii], g_x, dx);
205 inp = iprod(dir, dx);
207 for (m = 0; m < DIM; m++)
209 dr2 += dsqr(dx[m] - inp*dir[m]);
214 /* add to index, to sum of COM, to weight array */
215 if (pdyna->nat_loc >= pdyna->nalloc_loc)
217 pdyna->nalloc_loc = over_alloc_large(pdyna->nat_loc+1);
218 srenew(pdyna->ind_loc, pdyna->nalloc_loc);
219 srenew(pdyna->weight_loc, pdyna->nalloc_loc);
221 pdyna->ind_loc[pdyna->nat_loc] = ii;
222 mass = md->massT[ii];
223 weight = get_weight(sqrt(dr2), pull->cyl_r1, pull->cyl_r0);
224 pdyna->weight_loc[pdyna->nat_loc] = weight;
225 sum_a += mass*weight*inp;
228 pbc_dx_aiuc(pbc, xp[ii], g_x, dx);
229 inp = iprod(dir, dx);
230 sum_ap += mass*weight*inp;
232 wmass += mass*weight;
233 wwmass += mass*sqr(weight);
238 pull->dbuf_cyl[(g-1)*4+0] = wmass;
239 pull->dbuf_cyl[(g-1)*4+1] = wwmass;
240 pull->dbuf_cyl[(g-1)*4+2] = sum_a;
241 pull->dbuf_cyl[(g-1)*4+3] = sum_ap;
246 /* Sum the contributions over the nodes */
247 gmx_sumd(pull->ngrp*4, pull->dbuf_cyl, cr);
250 for (g = 1; g < 1+pull->ngrp; g++)
252 pgrp = &pull->grp[g];
253 pdyna = &pull->dyna[g];
255 wmass = pull->dbuf_cyl[(g-1)*4+0];
256 wwmass = pull->dbuf_cyl[(g-1)*4+1];
257 pdyna->wscale = wmass/wwmass;
258 pdyna->invtm = 1.0/(pdyna->wscale*wmass);
260 for (m = 0; m < DIM; m++)
262 g_x[m] = pgrp->x[m] - pgrp->vec[m]*(pgrp->init[0] + pgrp->rate*t);
263 pdyna->x[m] = g_x[m] + pgrp->vec[m]*pull->dbuf_cyl[(g-1)*4+2]/wmass;
266 pdyna->xp[m] = g_x[m] + pgrp->vec[m]*pull->dbuf_cyl[(g-1)*4+3]/wmass;
272 fprintf(debug, "Pull cylinder group %d:%8.3f%8.3f%8.3f m:%8.3f\n",
273 g, pdyna->x[0], pdyna->x[1],
274 pdyna->x[2], 1.0/pdyna->invtm);
279 static double atan2_0_2pi(double y, double x)
291 /* calculates center of mass of selection index from all coordinates x */
292 void pull_calc_coms(t_commrec *cr,
293 t_pull *pull, t_mdatoms *md, t_pbc *pbc, double t,
297 real mass, w, wm, twopi_box = 0;
298 double wmass, wwmass, invwmass;
300 double cm, sm, cmp, smp, ccm, csm, ssm, csw, snw;
301 rvec *xx[2], x_pbc = {0, 0, 0}, dx;
304 if (pull->rbuf == NULL)
306 snew(pull->rbuf, 1+pull->ngrp);
308 if (pull->dbuf == NULL)
310 snew(pull->dbuf, 3*(1+pull->ngrp));
315 pull_set_pbcatoms(cr, pull, md, x, pull->rbuf);
318 if (pull->cosdim >= 0)
320 for (m = pull->cosdim+1; m < pull->npbcdim; m++)
322 if (pbc->box[m][pull->cosdim] != 0)
324 gmx_fatal(FARGS, "Can not do cosine weighting for trilinic dimensions");
327 twopi_box = 2.0*M_PI/pbc->box[pull->cosdim][pull->cosdim];
330 for (g = 0; g < 1+pull->ngrp; g++)
332 pgrp = &pull->grp[g];
344 if (!(g == 0 && PULL_CYL(pull)))
346 if (pgrp->epgrppbc == epgrppbcREFAT)
348 /* Set the pbc atom */
349 copy_rvec(pull->rbuf[g], x_pbc);
352 for (i = 0; i < pgrp->nat_loc; i++)
354 ii = pgrp->ind_loc[i];
355 mass = md->massT[ii];
356 if (pgrp->epgrppbc != epgrppbcCOS)
358 if (pgrp->weight_loc)
360 w = pgrp->weight_loc[i];
365 if (pgrp->epgrppbc == epgrppbcNONE)
367 /* Plain COM: sum the coordinates */
368 for (m = 0; m < DIM; m++)
370 com[m] += wm*x[ii][m];
374 for (m = 0; m < DIM; m++)
376 comp[m] += wm*xp[ii][m];
382 /* Sum the difference with the reference atom */
383 pbc_dx(pbc, x[ii], x_pbc, dx);
384 for (m = 0; m < DIM; m++)
390 /* For xp add the difference between xp and x to dx,
391 * such that we use the same periodic image,
392 * also when xp has a large displacement.
394 for (m = 0; m < DIM; m++)
396 comp[m] += wm*(dx[m] + xp[ii][m] - x[ii][m]);
403 /* Determine cos and sin sums */
404 csw = cos(x[ii][pull->cosdim]*twopi_box);
405 snw = sin(x[ii][pull->cosdim]*twopi_box);
414 csw = cos(xp[ii][pull->cosdim]*twopi_box);
415 snw = sin(xp[ii][pull->cosdim]*twopi_box);
423 /* Copy local sums to a buffer for global summing */
424 switch (pgrp->epgrppbc)
428 copy_dvec(com, pull->dbuf[g*3]);
429 copy_dvec(comp, pull->dbuf[g*3+1]);
430 pull->dbuf[g*3+2][0] = wmass;
431 pull->dbuf[g*3+2][1] = wwmass;
432 pull->dbuf[g*3+2][2] = 0;
435 pull->dbuf[g*3 ][0] = cm;
436 pull->dbuf[g*3 ][1] = sm;
437 pull->dbuf[g*3 ][2] = 0;
438 pull->dbuf[g*3+1][0] = ccm;
439 pull->dbuf[g*3+1][1] = csm;
440 pull->dbuf[g*3+1][2] = ssm;
441 pull->dbuf[g*3+2][0] = cmp;
442 pull->dbuf[g*3+2][1] = smp;
443 pull->dbuf[g*3+2][2] = 0;
450 /* Sum the contributions over the nodes */
451 gmx_sumd((1+pull->ngrp)*3*DIM, pull->dbuf[0], cr);
454 for (g = 0; g < 1+pull->ngrp; g++)
456 pgrp = &pull->grp[g];
457 if (pgrp->nat > 0 && !(g == 0 && PULL_CYL(pull)))
459 if (pgrp->epgrppbc != epgrppbcCOS)
461 /* Determine the inverse mass */
462 wmass = pull->dbuf[g*3+2][0];
463 wwmass = pull->dbuf[g*3+2][1];
465 /* invtm==0 signals a frozen group, so then we should keep it zero */
468 pgrp->wscale = wmass/wwmass;
469 pgrp->invtm = 1.0/(pgrp->wscale*wmass);
471 /* Divide by the total mass */
472 for (m = 0; m < DIM; m++)
474 pgrp->x[m] = pull->dbuf[g*3 ][m]*invwmass;
477 pgrp->xp[m] = pull->dbuf[g*3+1][m]*invwmass;
479 if (pgrp->epgrppbc == epgrppbcREFAT)
481 pgrp->x[m] += pull->rbuf[g][m];
484 pgrp->xp[m] += pull->rbuf[g][m];
491 /* Determine the optimal location of the cosine weight */
492 csw = pull->dbuf[g*3][0];
493 snw = pull->dbuf[g*3][1];
494 pgrp->x[pull->cosdim] = atan2_0_2pi(snw, csw)/twopi_box;
495 /* Set the weights for the local atoms */
496 wmass = sqrt(csw*csw + snw*snw);
497 wwmass = (pull->dbuf[g*3+1][0]*csw*csw +
498 pull->dbuf[g*3+1][1]*csw*snw +
499 pull->dbuf[g*3+1][2]*snw*snw)/(wmass*wmass);
500 pgrp->wscale = wmass/wwmass;
501 pgrp->invtm = 1.0/(pgrp->wscale*wmass);
502 /* Set the weights for the local atoms */
505 for (i = 0; i < pgrp->nat_loc; i++)
507 ii = pgrp->ind_loc[i];
508 pgrp->weight_loc[i] = csw*cos(twopi_box*x[ii][pull->cosdim]) +
509 snw*sin(twopi_box*x[ii][pull->cosdim]);
513 csw = pull->dbuf[g*3+2][0];
514 snw = pull->dbuf[g*3+2][1];
515 pgrp->xp[pull->cosdim] = atan2_0_2pi(snw, csw)/twopi_box;
520 fprintf(debug, "Pull group %d wmass %f wwmass %f invtm %f\n",
521 g, wmass, wwmass, pgrp->invtm);
528 /* Calculate the COMs for the cyclinder reference groups */
529 make_cyl_refgrps(cr, pull, md, pbc, t, x, xp);