2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2009, The GROMACS development team,
6 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012,2013, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
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.
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.
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.
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.
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.
39 * \brief Implementation of functions in centerofmass.h.
49 #include <centerofmass.h>
52 * \param[in] top Topology structure (unused, can be NULL).
53 * \param[in] x Position vectors of all atoms.
54 * \param[in] nrefat Number of atoms in the index.
55 * \param[in] index Indices of atoms.
56 * \param[out] xout COG position for the indexed atoms.
57 * \returns 0 on success.
60 gmx_calc_cog(t_topology *top, rvec x[], int nrefat, atom_id index[], rvec xout)
65 for (m = 0; m < nrefat; ++m)
68 rvec_inc(xout, x[ai]);
70 svmul(1.0/nrefat, xout, xout);
75 * \param[in] top Topology structure with masses.
76 * \param[in] x Position vectors of all atoms.
77 * \param[in] nrefat Number of atoms in the index.
78 * \param[in] index Indices of atoms.
79 * \param[out] xout COM position for the indexed atoms.
80 * \returns 0 on success, EINVAL if \p top is NULL.
82 * Works exactly as gmx_calc_cog() with the exception that a center of
83 * mass are calculated, and hence a topology with masses is required.
86 gmx_calc_com(t_topology *top, rvec x[], int nrefat, atom_id index[], rvec xout)
93 gmx_incons("no masses available while mass weighting was requested");
98 for (m = 0; m < nrefat; ++m)
101 mass = top->atoms.atom[ai].m;
102 for (j = 0; j < DIM; ++j)
104 xout[j] += mass * x[ai][j];
108 svmul(1.0/mtot, xout, xout);
113 * \param[in] top Topology structure with masses.
114 * \param[in] f Forces on all atoms.
115 * \param[in] nrefat Number of atoms in the index.
116 * \param[in] index Indices of atoms.
117 * \param[out] fout Force on the COG position for the indexed atoms.
118 * \returns 0 on success, EINVAL if \p top is NULL.
120 * No special function is provided for calculating the force on the center of
121 * mass, because this can be achieved with gmx_calc_cog().
124 gmx_calc_cog_f(t_topology *top, rvec f[], int nrefat, atom_id index[], rvec fout)
131 gmx_incons("no masses available while mass weighting was needed");
136 for (m = 0; m < nrefat; ++m)
139 mass = top->atoms.atom[ai].m;
140 for (j = 0; j < DIM; ++j)
142 fout[j] += f[ai][j] / mass;
146 svmul(mtot, fout, fout);
151 * \param[in] top Topology structure with masses
152 * (can be NULL if \p bMASS==FALSE).
153 * \param[in] x Position vectors of all atoms.
154 * \param[in] nrefat Number of atoms in the index.
155 * \param[in] index Indices of atoms.
156 * \param[in] bMass If TRUE, mass weighting is used.
157 * \param[out] xout COM/COG position for the indexed atoms.
158 * \returns 0 on success, EINVAL if \p top is NULL and \p bMass is TRUE.
160 * Calls either gmx_calc_com() or gmx_calc_cog() depending on the value of
162 * Other parameters are passed unmodified to these functions.
165 gmx_calc_comg(t_topology *top, rvec x[], int nrefat, atom_id index[],
166 gmx_bool bMass, rvec xout)
170 return gmx_calc_com(top, x, nrefat, index, xout);
174 return gmx_calc_cog(top, x, nrefat, index, xout);
179 * \param[in] top Topology structure with masses
180 * (can be NULL if \p bMASS==TRUE).
181 * \param[in] x Forces on all atoms.
182 * \param[in] nrefat Number of atoms in the index.
183 * \param[in] index Indices of atoms.
184 * \param[in] bMass If TRUE, force on COM is calculated.
185 * \param[out] xout Force on the COM/COG position for the indexed atoms.
186 * \returns 0 on success, EINVAL if \p top is NULL and \p bMass is FALSE.
188 * Calls either gmx_calc_cog() or gmx_calc_cog_f() depending on the value of
190 * Other parameters are passed unmodified to these functions.
193 gmx_calc_comg_f(t_topology *top, rvec f[], int nrefat, atom_id index[],
194 gmx_bool bMass, rvec fout)
198 return gmx_calc_cog(top, f, nrefat, index, fout);
202 return gmx_calc_cog_f(top, f, nrefat, index, fout);
208 * \param[in] top Topology structure (unused, can be NULL).
209 * \param[in] x Position vectors of all atoms.
210 * \param[in] pbc Periodic boundary conditions structure.
211 * \param[in] nrefat Number of atoms in the index.
212 * \param[in] index Indices of atoms.
213 * \param[out] xout COG position for the indexed atoms.
214 * \returns 0 on success.
216 * Works exactly as gmx_calc_com_pbc(), but calculates the center of geometry.
219 gmx_calc_cog_pbc(t_topology *top, rvec x[], t_pbc *pbc,
220 int nrefat, atom_id index[], rvec xout)
222 const real tol = 1e-4;
227 /* First simple calculation */
228 gmx_calc_cog(top, x, nrefat, index, xout);
229 /* Now check if any atom is more than half the box from the COM */
236 for (m = 0; m < nrefat; ++m)
239 pbc_dx(pbc, x[ai], xout, dx);
240 rvec_add(xout, dx, xtest);
241 for (j = 0; j < DIM; ++j)
243 if (fabs(xtest[j] - x[ai][j]) > tol)
245 /* Here we have used the wrong image for contributing to the COM */
246 xout[j] += (xtest[j] - x[ai][j]) / nrefat;
260 * \param[in] top Topology structure with masses.
261 * \param[in] x Position vectors of all atoms.
262 * \param[in] pbc Periodic boundary conditions structure.
263 * \param[in] nrefat Number of atoms in the index.
264 * \param[in] index Indices of atoms.
265 * \param[out] xout COM position for the indexed atoms.
266 * \returns 0 on success, EINVAL if \p top is NULL.
268 * Works as gmx_calc_com(), but takes into account periodic boundary
269 * conditions: If any atom is more than half the box from the COM,
270 * it is wrapped around and a new COM is calculated. This is repeated
271 * until no atoms violate the condition.
273 * Modified from src/tools/gmx_sorient.c in Gromacs distribution.
276 gmx_calc_com_pbc(t_topology *top, rvec x[], t_pbc *pbc,
277 int nrefat, atom_id index[], rvec xout)
279 const real tol = 1e-4;
287 gmx_incons("no masses available while mass weighting was requested");
290 /* First simple calculation */
293 for (m = 0; m < nrefat; ++m)
296 mass = top->atoms.atom[ai].m;
297 for (j = 0; j < DIM; ++j)
299 xout[j] += mass * x[ai][j];
303 svmul(1.0/mtot, xout, xout);
304 /* Now check if any atom is more than half the box from the COM */
311 for (m = 0; m < nrefat; ++m)
314 mass = top->atoms.atom[ai].m / mtot;
315 pbc_dx(pbc, x[ai], xout, dx);
316 rvec_add(xout, dx, xtest);
317 for (j = 0; j < DIM; ++j)
319 if (fabs(xtest[j] - x[ai][j]) > tol)
321 /* Here we have used the wrong image for contributing to the COM */
322 xout[j] += mass * (xtest[j] - x[ai][j]);
336 * \param[in] top Topology structure with masses
337 * (can be NULL if \p bMASS==FALSE).
338 * \param[in] x Position vectors of all atoms.
339 * \param[in] pbc Periodic boundary conditions structure.
340 * \param[in] nrefat Number of atoms in the index.
341 * \param[in] index Indices of atoms.
342 * \param[in] bMass If TRUE, mass weighting is used.
343 * \param[out] xout COM/COG position for the indexed atoms.
344 * \returns 0 on success, EINVAL if \p top is NULL and \p bMass is TRUE.
346 * Calls either gmx_calc_com() or gmx_calc_cog() depending on the value of
348 * Other parameters are passed unmodified to these functions.
351 gmx_calc_comg_pbc(t_topology *top, rvec x[], t_pbc *pbc,
352 int nrefat, atom_id index[], gmx_bool bMass, rvec xout)
356 return gmx_calc_com_pbc(top, x, pbc, nrefat, index, xout);
360 return gmx_calc_cog_pbc(top, x, pbc, nrefat, index, xout);
366 * \param[in] top Topology structure (unused, can be NULL).
367 * \param[in] x Position vectors of all atoms.
368 * \param[in] block t_block structure that divides \p index into blocks.
369 * \param[in] index Indices of atoms.
370 * \param[out] xout \p block->nr COG positions.
371 * \returns 0 on success.
374 gmx_calc_cog_block(t_topology *top, rvec x[], t_block *block, atom_id index[],
380 for (b = 0; b < block->nr; ++b)
383 for (i = block->index[b]; i < block->index[b+1]; ++i)
388 svmul(1.0/(block->index[b+1] - block->index[b]), xb, xout[b]);
394 * \param[in] top Topology structure with masses.
395 * \param[in] x Position vectors of all atoms.
396 * \param[in] block t_block structure that divides \p index into blocks.
397 * \param[in] index Indices of atoms.
398 * \param[out] xout \p block->nr COM positions.
399 * \returns 0 on success, EINVAL if \p top is NULL.
401 * Works exactly as gmx_calc_cog_block() with the exception that centers of
402 * mass are calculated, and hence a topology with masses is required.
405 gmx_calc_com_block(t_topology *top, rvec x[], t_block *block, atom_id index[],
414 gmx_incons("no masses available while mass weighting was requested");
417 for (b = 0; b < block->nr; ++b)
421 for (i = block->index[b]; i < block->index[b+1]; ++i)
424 mass = top->atoms.atom[ai].m;
425 for (d = 0; d < DIM; ++d)
427 xb[d] += mass * x[ai][d];
431 svmul(1.0/mtot, xb, xout[b]);
437 * \param[in] top Topology structure with masses.
438 * \param[in] f Forces on all atoms.
439 * \param[in] block t_block structure that divides \p index into blocks.
440 * \param[in] index Indices of atoms.
441 * \param[out] xout \p block->nr Forces on COG positions.
442 * \returns 0 on success, EINVAL if \p top is NULL.
445 gmx_calc_cog_f_block(t_topology *top, rvec f[], t_block *block, atom_id index[],
454 gmx_incons("no masses available while mass weighting was needed");
457 for (b = 0; b < block->nr; ++b)
461 for (i = block->index[b]; i < block->index[b+1]; ++i)
464 mass = top->atoms.atom[ai].m;
465 for (d = 0; d < DIM; ++d)
467 fb[d] += f[ai][d] / mass;
471 svmul(mtot, fb, fout[b]);
477 * \param[in] top Topology structure with masses
478 * (can be NULL if \p bMASS==FALSE).
479 * \param[in] x Position vectors of all atoms.
480 * \param[in] block t_block structure that divides \p index into blocks.
481 * \param[in] index Indices of atoms.
482 * \param[in] bMass If TRUE, mass weighting is used.
483 * \param[out] xout \p block->nr COM/COG positions.
484 * \returns 0 on success, EINVAL if \p top is NULL and \p bMass is TRUE.
486 * Calls either gmx_calc_com_block() or gmx_calc_cog_block() depending on the
488 * Other parameters are passed unmodified to these functions.
491 gmx_calc_comg_block(t_topology *top, rvec x[], t_block *block, atom_id index[],
492 gmx_bool bMass, rvec xout[])
496 return gmx_calc_com_block(top, x, block, index, xout);
500 return gmx_calc_cog_block(top, x, block, index, xout);
505 * \param[in] top Topology structure with masses
506 * (can be NULL if \p bMASS==FALSE).
507 * \param[in] f Forces on all atoms.
508 * \param[in] block t_block structure that divides \p index into blocks.
509 * \param[in] index Indices of atoms.
510 * \param[in] bMass If TRUE, force on COM is calculated.
511 * \param[out] xout \p block->nr forces on the COM/COG positions.
512 * \returns 0 on success, EINVAL if \p top is NULL and \p bMass is TRUE.
514 * Calls either gmx_calc_com_block() or gmx_calc_cog_block() depending on the
516 * Other parameters are passed unmodified to these functions.
519 gmx_calc_comg_f_block(t_topology *top, rvec f[], t_block *block, atom_id index[],
520 gmx_bool bMass, rvec fout[])
524 return gmx_calc_cog_block(top, f, block, index, fout);
528 return gmx_calc_cog_f_block(top, f, block, index, fout);
533 * \param[in] top Topology structure with masses
534 * (can be NULL if \p bMASS==FALSE).
535 * \param[in] x Position vectors of all atoms.
536 * \param[in] block Blocks for calculation.
537 * \param[in] bMass If TRUE, mass weighting is used.
538 * \param[out] xout \p block->nr COM/COG positions.
539 * \returns 0 on success, EINVAL if \p top is NULL and \p bMass is TRUE.
541 * Calls gmx_calc_comg_block(), converting the \p t_blocka structure into
542 * a \p t_block and an index. Other parameters are passed unmodified.
545 * This function assumes that a pointer to \c t_blocka can be safely typecast
546 * into \c t_block such that the index fields can still be referenced.
547 * With the present Gromacs defitions of these types, this is the case,
548 * but if the layout of these structures is changed, this may lead to strange
552 gmx_calc_comg_blocka(t_topology *top, rvec x[], t_blocka *block,
553 gmx_bool bMass, rvec xout[])
555 /* TODO: It would probably be better to do this without the type cast */
556 return gmx_calc_comg_block(top, x, (t_block *)block, block->a, bMass, xout);
560 * \param[in] top Topology structure with masses
561 * (can be NULL if \p bMASS==TRUE).
562 * \param[in] f Forces on all atoms.
563 * \param[in] block Blocks for calculation.
564 * \param[in] bMass If TRUE, force on COM is calculated.
565 * \param[out] fout \p block->nr forces on the COM/COG positions.
566 * \returns 0 on success, EINVAL if \p top is NULL and \p bMass is FALSE.
568 * Calls gmx_calc_comg_f_block(), converting the \p t_blocka structure into
569 * a \p t_block and an index. Other parameters are passed unmodified.
572 * This function assumes that a pointer to \c t_blocka can be safely typecast
573 * into \c t_block such that the index fields can still be referenced.
574 * With the present Gromacs defitions of these types, this is the case,
575 * but if the layout of these structures is changed, this may lead to strange
579 gmx_calc_comg_f_blocka(t_topology *top, rvec f[], t_blocka *block,
580 gmx_bool bMass, rvec fout[])
582 /* TODO: It would probably be better to do this without the type cast */
583 return gmx_calc_comg_f_block(top, f, (t_block *)block, block->a, bMass, fout);