2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements functions in centerofmass.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
42 #include "gromacs/selection/centerofmass.h"
46 #include "gromacs/legacyheaders/typedefs.h"
47 #include "gromacs/legacyheaders/pbc.h"
48 #include "gromacs/legacyheaders/vec.h"
51 gmx_calc_cog(t_topology * /* top */, rvec x[], int nrefat, atom_id index[], rvec xout)
56 for (m = 0; m < nrefat; ++m)
59 rvec_inc(xout, x[ai]);
61 svmul(1.0/nrefat, xout, xout);
66 * \param[in] top Topology structure with masses.
67 * \param[in] x Position vectors of all atoms.
68 * \param[in] nrefat Number of atoms in the index.
69 * \param[in] index Indices of atoms.
70 * \param[out] xout COM position for the indexed atoms.
71 * \returns 0 on success, EINVAL if \p top is NULL.
73 * Works exactly as gmx_calc_cog() with the exception that a center of
74 * mass are calculated, and hence a topology with masses is required.
77 gmx_calc_com(t_topology *top, rvec x[], int nrefat, atom_id index[], rvec xout)
84 gmx_incons("no masses available while mass weighting was requested");
89 for (m = 0; m < nrefat; ++m)
92 mass = top->atoms.atom[ai].m;
93 for (j = 0; j < DIM; ++j)
95 xout[j] += mass * x[ai][j];
99 svmul(1.0/mtot, xout, xout);
104 * \param[in] top Topology structure with masses.
105 * \param[in] f Forces on all atoms.
106 * \param[in] nrefat Number of atoms in the index.
107 * \param[in] index Indices of atoms.
108 * \param[out] fout Force on the COG position for the indexed atoms.
109 * \returns 0 on success, EINVAL if \p top is NULL.
112 gmx_calc_cog_f(t_topology *top, rvec f[], int nrefat, atom_id index[], rvec fout)
119 gmx_incons("no masses available while mass weighting was needed");
124 for (m = 0; m < nrefat; ++m)
127 mass = top->atoms.atom[ai].m;
128 for (j = 0; j < DIM; ++j)
130 fout[j] += f[ai][j] / mass;
134 svmul(mtot / nrefat, fout, fout);
139 gmx_calc_com_f(t_topology * /* top */, rvec f[], int nrefat, atom_id index[], rvec fout)
142 for (int m = 0; m < nrefat; ++m)
144 const int ai = index[m];
145 rvec_inc(fout, f[ai]);
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 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] f 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] fout 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_f() or gmx_calc_com_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 bool bMass, rvec fout)
198 return gmx_calc_com_f(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[], 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 gmx_calc_cog_block(t_topology * /* top */, rvec x[], t_block *block, atom_id index[],
372 for (b = 0; b < block->nr; ++b)
375 for (i = block->index[b]; i < block->index[b+1]; ++i)
380 svmul(1.0/(block->index[b+1] - block->index[b]), xb, xout[b]);
386 * \param[in] top Topology structure with masses.
387 * \param[in] x Position vectors of all atoms.
388 * \param[in] block t_block structure that divides \p index into blocks.
389 * \param[in] index Indices of atoms.
390 * \param[out] xout \p block->nr COM positions.
391 * \returns 0 on success, EINVAL if \p top is NULL.
393 * Works exactly as gmx_calc_cog_block() with the exception that centers of
394 * mass are calculated, and hence a topology with masses is required.
397 gmx_calc_com_block(t_topology *top, rvec x[], t_block *block, atom_id index[],
406 gmx_incons("no masses available while mass weighting was requested");
409 for (b = 0; b < block->nr; ++b)
413 for (i = block->index[b]; i < block->index[b+1]; ++i)
416 mass = top->atoms.atom[ai].m;
417 for (d = 0; d < DIM; ++d)
419 xb[d] += mass * x[ai][d];
423 svmul(1.0/mtot, xb, xout[b]);
429 * \param[in] top Topology structure with masses.
430 * \param[in] f Forces on all atoms.
431 * \param[in] block t_block structure that divides \p index into blocks.
432 * \param[in] index Indices of atoms.
433 * \param[out] fout \p block->nr Forces on COG positions.
434 * \returns 0 on success, EINVAL if \p top is NULL.
437 gmx_calc_cog_f_block(t_topology *top, rvec f[], t_block *block, atom_id index[],
446 gmx_incons("no masses available while mass weighting was needed");
449 for (b = 0; b < block->nr; ++b)
453 for (i = block->index[b]; i < block->index[b+1]; ++i)
456 mass = top->atoms.atom[ai].m;
457 for (d = 0; d < DIM; ++d)
459 fb[d] += f[ai][d] / mass;
463 svmul(mtot / (block->index[b+1] - block->index[b]), fb, fout[b]);
469 gmx_calc_com_f_block(t_topology * /* top */, rvec f[], t_block *block, atom_id index[],
472 for (int b = 0; b < block->nr; ++b)
476 for (int i = block->index[b]; i < block->index[b+1]; ++i)
478 const int ai = index[i];
481 copy_rvec(fb, fout[b]);
487 * \param[in] top Topology structure with masses
488 * (can be NULL if \p bMASS==false).
489 * \param[in] x Position vectors of all atoms.
490 * \param[in] block t_block structure that divides \p index into blocks.
491 * \param[in] index Indices of atoms.
492 * \param[in] bMass If true, mass weighting is used.
493 * \param[out] xout \p block->nr COM/COG positions.
494 * \returns 0 on success, EINVAL if \p top is NULL and \p bMass is true.
496 * Calls either gmx_calc_com_block() or gmx_calc_cog_block() depending on the
498 * Other parameters are passed unmodified to these functions.
501 gmx_calc_comg_block(t_topology *top, rvec x[], t_block *block, atom_id index[],
502 bool bMass, rvec xout[])
506 return gmx_calc_com_block(top, x, block, index, xout);
510 return gmx_calc_cog_block(top, x, block, index, xout);
515 * \param[in] top Topology structure with masses
516 * (can be NULL if \p bMASS==false).
517 * \param[in] f Forces on all atoms.
518 * \param[in] block t_block structure that divides \p index into blocks.
519 * \param[in] index Indices of atoms.
520 * \param[in] bMass If true, force on COM is calculated.
521 * \param[out] fout \p block->nr forces on the COM/COG positions.
522 * \returns 0 on success, EINVAL if \p top is NULL and \p bMass is true.
524 * Calls either gmx_calc_com_f_block() or gmx_calc_cog_f_block() depending on
525 * the value of \p bMass.
526 * Other parameters are passed unmodified to these functions.
529 gmx_calc_comg_f_block(t_topology *top, rvec f[], t_block *block, atom_id index[],
530 bool bMass, rvec fout[])
534 return gmx_calc_com_f_block(top, f, block, index, fout);
538 return gmx_calc_cog_f_block(top, f, block, index, fout);
543 * \param[in] top Topology structure with masses
544 * (can be NULL if \p bMASS==false).
545 * \param[in] x Position vectors of all atoms.
546 * \param[in] block Blocks for calculation.
547 * \param[in] bMass If true, mass weighting is used.
548 * \param[out] xout \p block->nr COM/COG positions.
549 * \returns 0 on success, EINVAL if \p top is NULL and \p bMass is true.
551 * Calls gmx_calc_comg_block(), converting the \p t_blocka structure into
552 * a \p t_block and an index. Other parameters are passed unmodified.
555 * This function assumes that a pointer to \c t_blocka can be safely typecast
556 * into \c t_block such that the index fields can still be referenced.
557 * With the present Gromacs defitions of these types, this is the case,
558 * but if the layout of these structures is changed, this may lead to strange
562 gmx_calc_comg_blocka(t_topology *top, rvec x[], t_blocka *block,
563 bool bMass, rvec xout[])
565 /* TODO: It would probably be better to do this without the type cast */
566 return gmx_calc_comg_block(top, x, (t_block *)block, block->a, bMass, xout);
570 * \param[in] top Topology structure with masses
571 * (can be NULL if \p bMASS==true).
572 * \param[in] f Forces on all atoms.
573 * \param[in] block Blocks for calculation.
574 * \param[in] bMass If true, force on COM is calculated.
575 * \param[out] fout \p block->nr forces on the COM/COG positions.
576 * \returns 0 on success, EINVAL if \p top is NULL and \p bMass is false.
578 * Calls gmx_calc_comg_f_block(), converting the \p t_blocka structure into
579 * a \p t_block and an index. Other parameters are passed unmodified.
582 * This function assumes that a pointer to \c t_blocka can be safely typecast
583 * into \c t_block such that the index fields can still be referenced.
584 * With the present Gromacs defitions of these types, this is the case,
585 * but if the layout of these structures is changed, this may lead to strange
589 gmx_calc_comg_f_blocka(t_topology *top, rvec f[], t_blocka *block,
590 bool bMass, rvec fout[])
592 /* TODO: It would probably be better to do this without the type cast */
593 return gmx_calc_comg_f_block(top, f, (t_block *)block, block->a, bMass, fout);