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 position.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
42 #include "gromacs/selection/position.h"
46 #include "gromacs/math/vec.h"
47 #include "gromacs/selection/indexutil.h"
48 #include "gromacs/utility/gmxassert.h"
49 #include "gromacs/utility/smalloc.h"
51 gmx_ana_pos_t::gmx_ana_pos_t()
56 gmx_ana_indexmap_clear(&m);
60 gmx_ana_pos_t::~gmx_ana_pos_t()
65 gmx_ana_indexmap_deinit(&m);
69 * \param[in,out] pos Position data structure.
70 * \param[in] n Maximum number of positions.
71 * \param[in] isize Maximum number of atoms.
73 * Ensures that enough memory is allocated in \p pos to calculate \p n
74 * positions from \p isize atoms.
77 gmx_ana_pos_reserve(gmx_ana_pos_t *pos, int n, int isize)
79 GMX_RELEASE_ASSERT(n >= 0, "Invalid position allocation count");
80 // Always reserve at least one entry to make NULL checks against pos->x
81 // and gmx_ana_pos_reserve_velocities/forces() work as expected in the case
82 // that there are actually no positions.
87 if (pos->nalloc_x < n)
102 gmx_ana_indexmap_reserve(&pos->m, n, isize);
107 * \param[in,out] pos Position data structure.
109 * Currently, this function can only be called after gmx_ana_pos_reserve()
110 * has been called at least once with a \p n >= 0.
113 gmx_ana_pos_reserve_velocities(gmx_ana_pos_t *pos)
115 GMX_RELEASE_ASSERT(pos->nalloc_x > 0,
116 "No memory reserved yet for positions");
119 snew(pos->v, pos->nalloc_x);
124 * \param[in,out] pos Position data structure.
126 * Currently, this function can only be called after gmx_ana_pos_reserve()
127 * has been called at least once with a \p n >= 0.
130 gmx_ana_pos_reserve_forces(gmx_ana_pos_t *pos)
132 GMX_RELEASE_ASSERT(pos->nalloc_x > 0,
133 "No memory reserved yet for positions");
136 snew(pos->f, pos->nalloc_x);
141 * \param[in,out] pos Position data structure.
142 * \param[in] n Maximum number of positions.
143 * \param[in] isize Maximum number of atoms.
144 * \param[in] bVelocities Whether to reserve space for velocities.
145 * \param[in] bForces Whether to reserve space for forces.
147 * Ensures that enough memory is allocated in \p pos to calculate \p n
148 * positions from \p isize atoms.
150 * This method needs to be called instead of gmx_ana_pos_reserve() if the
151 * intent is to use gmx_ana_pos_append_init()/gmx_ana_pos_append().
154 gmx_ana_pos_reserve_for_append(gmx_ana_pos_t *pos, int n, int isize,
155 bool bVelocities, bool bForces)
157 gmx_ana_pos_reserve(pos, n, isize);
158 snew(pos->m.mapb.a, isize);
159 pos->m.mapb.nalloc_a = isize;
162 gmx_ana_pos_reserve_velocities(pos);
166 gmx_ana_pos_reserve_forces(pos);
171 * \param[out] pos Position data structure to initialize.
172 * \param[in] x Position vector to use.
175 gmx_ana_pos_init_const(gmx_ana_pos_t *pos, const rvec x)
181 copy_rvec(x, pos->x[0]);
182 clear_rvec(pos->v[0]);
183 clear_rvec(pos->f[0]);
184 gmx_ana_indexmap_init(&pos->m, NULL, NULL, INDEX_UNKNOWN);
188 * \param[in,out] dest Destination positions.
189 * \param[in] src Source positions.
190 * \param[in] bFirst If true, memory is allocated for \p dest and a full
191 * copy is made; otherwise, only variable parts are copied, and no memory
194 * \p dest should have been initialized somehow (calloc() is enough).
197 gmx_ana_pos_copy(gmx_ana_pos_t *dest, gmx_ana_pos_t *src, bool bFirst)
201 gmx_ana_pos_reserve(dest, src->count(), 0);
204 gmx_ana_pos_reserve_velocities(dest);
208 gmx_ana_pos_reserve_forces(dest);
211 memcpy(dest->x, src->x, src->count()*sizeof(*dest->x));
214 GMX_ASSERT(src->v, "src velocities should be non-null if dest velocities are allocated");
215 memcpy(dest->v, src->v, src->count()*sizeof(*dest->v));
219 GMX_ASSERT(src->f, "src forces should be non-null if dest forces are allocated");
220 memcpy(dest->f, src->f, src->count()*sizeof(*dest->f));
222 gmx_ana_indexmap_copy(&dest->m, &src->m, bFirst);
226 * \param[in,out] pos Position data structure.
227 * \param[in] nr Number of positions.
230 gmx_ana_pos_set_nr(gmx_ana_pos_t *pos, int nr)
232 // TODO: This puts the mapping in a somewhat inconsistent state.
237 * \param[in,out] pos Position data structure.
239 * Sets the number of positions to 0.
242 gmx_ana_pos_empty_init(gmx_ana_pos_t *pos)
248 /* This should not really be necessary, but do it for safety... */
249 pos->m.mapb.index[0] = 0;
250 pos->m.b.index[0] = 0;
251 /* This function should only be used to construct all the possible
252 * positions, so the result should always be static. */
253 pos->m.bStatic = true;
257 * \param[in,out] pos Position data structure.
259 * Sets the number of positions to 0.
262 gmx_ana_pos_empty(gmx_ana_pos_t *pos)
266 /* This should not really be necessary, but do it for safety... */
267 pos->m.mapb.index[0] = 0;
268 /* We set the flag to true, although really in the empty state it
269 * should be false. This makes it possible to update the flag in
270 * gmx_ana_pos_append(), and just make a simple check in
271 * gmx_ana_pos_append_finish(). */
272 pos->m.bStatic = true;
276 * \param[in,out] dest Data structure to which the new position is appended.
277 * \param[in] src Data structure from which the position is copied.
278 * \param[in] i Index in \p from to copy.
281 gmx_ana_pos_append_init(gmx_ana_pos_t *dest, gmx_ana_pos_t *src, int i)
286 copy_rvec(src->x[i], dest->x[j]);
291 copy_rvec(src->v[i], dest->v[j]);
295 clear_rvec(dest->v[j]);
302 copy_rvec(src->f[i], dest->f[j]);
306 clear_rvec(dest->f[j]);
309 dest->m.refid[j] = j;
310 dest->m.mapid[j] = src->m.mapid[i];
311 dest->m.orgid[j] = src->m.orgid[i];
312 for (k = src->m.mapb.index[i]; k < src->m.mapb.index[i+1]; ++k)
314 dest->m.mapb.a[dest->m.mapb.nra++] = src->m.mapb.a[k];
315 dest->m.b.a[dest->m.b.nra++] = src->m.b.a[k];
317 dest->m.mapb.index[j+1] = dest->m.mapb.nra;
318 dest->m.b.index[j+1] = dest->m.mapb.nra;
324 * \param[in,out] dest Data structure to which the new position is appended.
325 * \param[in] src Data structure from which the position is copied.
326 * \param[in] i Index in \p src to copy.
327 * \param[in] refid Reference ID in \p out
328 * (all negative values are treated as -1).
331 gmx_ana_pos_append(gmx_ana_pos_t *dest, gmx_ana_pos_t *src, int i, int refid)
333 for (int k = src->m.mapb.index[i]; k < src->m.mapb.index[i+1]; ++k)
335 dest->m.mapb.a[dest->m.mapb.nra++] = src->m.mapb.a[k];
337 const int j = dest->count();
342 copy_rvec(src->v[i], dest->v[j]);
346 clear_rvec(dest->v[j]);
353 copy_rvec(src->f[i], dest->f[j]);
357 clear_rvec(dest->f[j]);
360 copy_rvec(src->x[i], dest->x[j]);
363 dest->m.refid[j] = -1;
364 dest->m.bStatic = false;
365 /* If we are using masks, there is no need to alter the
372 dest->m.bStatic = false;
374 dest->m.refid[j] = refid;
375 /* Use the original IDs from the output structure to correctly
376 * handle user customization. */
377 dest->m.mapid[j] = dest->m.orgid[refid];
379 dest->m.mapb.index[j+1] = dest->m.mapb.nra;
384 * \param[in,out] pos Position data structure.
386 * After gmx_ana_pos_empty(), internal state of the position data structure
387 * is not consistent before this function is called. This function should be
388 * called after any gmx_ana_pos_append() calls have been made.
391 gmx_ana_pos_append_finish(gmx_ana_pos_t *pos)
393 if (pos->m.mapb.nr != pos->m.b.nr)
395 pos->m.bStatic = false;
400 * \param[in,out] g Data structure to which the new atoms are appended.
401 * \param[in] src Data structure from which the position is copied.
402 * \param[in] i Index in \p src to copy.
405 gmx_ana_pos_add_to_group(gmx_ana_index_t *g, gmx_ana_pos_t *src, int i)
407 for (int k = src->m.mapb.index[i]; k < src->m.mapb.index[i+1]; ++k)
409 g->index[g->isize++] = src->m.mapb.a[k];