1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
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-2008, 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 * Groningen Machine for Chemical Simulation
40 #include "groupcoord.h"
45 #include "gmx_ga2la.h"
47 #define MIN(a,b) (((a)<(b))?(a):(b))
51 /* Select the indices of the group's atoms which are local and store them in
52 * anrs_loc[0..nr_loc]. The indices are saved in coll_ind[] for later reduction
53 * in communicate_group_positions()
55 extern void dd_make_local_group_indices(
57 const int nr, /* IN: Total number of atoms in the group */
58 int anrs[], /* IN: Global atom numbers of the groups atoms */
59 int *nr_loc, /* OUT: Number of group atoms found locally */
60 int *anrs_loc[], /* OUT: Local atom numbers of the group */
61 int *nalloc_loc, /* IN+OUT: Allocation size of anrs_loc */
62 int coll_ind[]) /* OUT (opt): Where is this position found in the collective array? */
68 /* Loop over all the atom indices of the group to check
69 * which ones are on the local node */
73 if (ga2la_get_home(ga2la,anrs[i],&ii))
75 /* The atom with this index is a home atom */
76 if (localnr >= *nalloc_loc) /* Check whether memory suffices */
78 *nalloc_loc = over_alloc_dd(localnr+1);
79 /* We never need more memory than the number of atoms in the group */
80 *nalloc_loc = MIN(*nalloc_loc, nr);
81 srenew(*anrs_loc,*nalloc_loc);
83 /* Save the atoms index in the local atom numbers array */
84 (*anrs_loc)[localnr] = ii;
88 /* Keep track of where this local atom belongs in the collective index array.
89 * This is needed when reducing the local arrays to a collective/global array
90 * in communicate_group_positions */
91 coll_ind[localnr] = i;
94 /* add one to the local atom count */
99 /* Return the number of local atoms that were found */
104 static void get_shifts_group(
107 rvec *xcoll, /* IN: Collective set of positions [0..nr] */
108 int nr, /* IN: Total number of atoms in the group */
109 rvec *xcoll_old, /* IN: Positions from the last time step [0...nr] */
110 ivec *shifts) /* OUT: Shifts for xcoll */
116 /* Get the shifts such that each atom is within closest
117 * distance to its position at the last NS time step after shifting.
118 * If we start with a whole group, and always keep track of
119 * shift changes, the group will stay whole this way */
120 for (i=0; i < nr; i++)
121 clear_ivec(shifts[i]);
125 /* The distance this atom moved since the last time step */
126 /* If this is more than just a bit, it has changed its home pbc box */
127 rvec_sub(xcoll[i],xcoll_old[i],dx);
129 for(m=npbcdim-1; m>=0; m--)
131 while (dx[m] < -0.5*box[m][m])
137 while (dx[m] >= 0.5*box[m][m])
148 static void shift_positions_group(
150 rvec x[], /* The positions [0..nr] */
151 ivec *is, /* The shifts [0..nr] */
152 int nr) /* The number of positions and shifts */
157 /* Loop over the group's atoms */
160 for (i=0; i < nr; i++)
166 x[i][XX]=x[i][XX]+tx*box[XX][XX]+ty*box[YY][XX]+tz*box[ZZ][XX];
167 x[i][YY]=x[i][YY]+ty*box[YY][YY]+tz*box[ZZ][YY];
168 x[i][ZZ]=x[i][ZZ]+tz*box[ZZ][ZZ];
172 for (i=0; i < nr; i++)
178 x[i][XX]=x[i][XX]+tx*box[XX][XX];
179 x[i][YY]=x[i][YY]+ty*box[YY][YY];
180 x[i][ZZ]=x[i][ZZ]+tz*box[ZZ][ZZ];
186 /* Assemble the positions of the group such that every node has all of them.
187 * The atom indices are retrieved from anrs_loc[0..nr_loc]
188 * Note that coll_ind[i] = i is needed in the serial case */
189 extern void communicate_group_positions(
191 rvec *xcoll, /* OUT: Collective array of positions */
192 ivec *shifts, /* IN+OUT: Collective array of shifts for xcoll */
193 ivec *extra_shifts, /* BUF: Extra shifts since last time step */
194 const gmx_bool bNS, /* IN: NS step, the shifts have changed */
195 rvec *x_loc, /* IN: Local positions on this node */
196 const int nr, /* IN: Total number of atoms in the group */
197 const int nr_loc, /* IN: Local number of atoms in the group */
198 int *anrs_loc, /* IN: Local atom numbers */
199 int *coll_ind, /* IN: Collective index */
200 rvec *xcoll_old, /* IN+OUT: Positions from the last time step, used to make group whole */
206 /* Zero out the groups' global position array */
207 clear_rvecs(nr, xcoll);
209 /* Put the local positions that this node has into the right place of
210 * the collective array. Note that in the serial case, coll_ind[i] = i */
211 for (i=0; i<nr_loc; i++)
212 copy_rvec(x_loc[anrs_loc[i]], xcoll[coll_ind[i]]);
216 /* Add the arrays from all nodes together */
217 gmx_sum(nr*3, xcoll[0], cr);
219 /* To make the group whole, start with a whole group and each
220 * step move the assembled positions at closest distance to the positions
221 * from the last step. First shift the positions with the saved shift
222 * vectors (these are 0 when this routine is called for the first time!) */
223 shift_positions_group(box, xcoll, shifts, nr);
225 /* Now check if some shifts changed since the last step.
226 * This only needs to be done when the shifts are expected to have changed,
227 * i.e. after neighboursearching */
230 get_shifts_group(3, box, xcoll, nr, xcoll_old, extra_shifts);
232 /* Shift with the additional shifts such that we get a whole group now */
233 shift_positions_group(box, xcoll, extra_shifts, nr);
235 /* Add the shift vectors together for the next time step */
238 shifts[i][XX] += extra_shifts[i][XX];
239 shifts[i][YY] += extra_shifts[i][YY];
240 shifts[i][ZZ] += extra_shifts[i][ZZ];
243 /* Store current correctly-shifted positions for comparison in the next NS time step */
245 copy_rvec(xcoll[i],xcoll_old[i]);
250 /* Determine the (weighted) sum vector from positions x */
251 extern double get_sum_of_positions(rvec x[], real weight[], const int nat, dvec dsumvec)
255 double weight_sum = 0.0;
258 /* Zero out the center */
261 /* Loop over all atoms and add their weighted position vectors */
264 for (i=0; i<nat; i++)
266 weight_sum += weight[i];
267 svmul(weight[i], x[i], x_weighted);
268 dsumvec[XX] += x_weighted[XX];
269 dsumvec[YY] += x_weighted[YY];
270 dsumvec[ZZ] += x_weighted[ZZ];
275 for (i=0; i<nat; i++)
277 dsumvec[XX] += x[i][XX];
278 dsumvec[YY] += x[i][YY];
279 dsumvec[ZZ] += x[i][ZZ];
286 /* Determine center of structure from collective positions x */
287 extern void get_center(rvec x[], real weight[], const int nr, rvec rcenter)
290 double weight_sum, denom;
293 weight_sum = get_sum_of_positions(x, weight, nr, dcenter);
296 denom = weight_sum; /* Divide by the sum of weight */
298 denom = nr; /* Divide by the number of atoms */
300 dsvmul(1.0/denom, dcenter, dcenter);
302 rcenter[XX] = dcenter[XX];
303 rcenter[YY] = dcenter[YY];
304 rcenter[ZZ] = dcenter[ZZ];
308 /* Get the center from local positions that already have the correct
309 * PBC representation */
310 extern void get_center_comm(
312 rvec x_loc[], /* Local positions */
313 real weight_loc[], /* Local masses or other weights */
314 int nr_loc, /* Local number of atoms */
315 int nr_group, /* Total number of atoms of the group */
316 rvec center) /* Weighted center */
318 double weight_sum, denom;
323 weight_sum = get_sum_of_positions(x_loc, weight_loc, nr_loc, dsumvec);
325 /* Add the local contributions from all nodes. Put the sum vector and the
326 * weight in a buffer array so that we get along with a single communication
330 buf[0] = dsumvec[XX];
331 buf[1] = dsumvec[YY];
332 buf[2] = dsumvec[ZZ];
335 /* Communicate buffer */
336 gmx_sumd(4, buf, cr);
338 dsumvec[XX] = buf[0];
339 dsumvec[YY] = buf[1];
340 dsumvec[ZZ] = buf[2];
344 if (weight_loc != NULL)
345 denom = 1.0/weight_sum; /* Divide by the sum of weight to get center of mass e.g. */
347 denom = 1.0/nr_group; /* Divide by the number of atoms to get the geometrical center */
349 center[XX] = dsumvec[XX]*denom;
350 center[YY] = dsumvec[YY]*denom;
351 center[ZZ] = dsumvec[ZZ]*denom;
355 /* Translate x with transvec */
356 extern void translate_x(rvec x[], const int nr, const rvec transvec)
362 rvec_inc(x[i], transvec);
366 extern void rotate_x(rvec x[], const int nr, matrix rmat)
372 /* Apply the rotation matrix */
381 x[i][j] += rmat[k][j]*x_old[k];