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 position.h.
52 #include <indexutil.h>
56 * \param[out] pos Output structure.
58 * Any contents of \p pos are discarded without freeing.
61 gmx_ana_pos_clear(gmx_ana_pos_t *pos)
67 gmx_ana_indexmap_clear(&pos->m);
73 * \param[in,out] pos Position data structure.
74 * \param[in] n Maximum number of positions.
75 * \param[in] isize Maximum number of atoms.
77 * Ensures that enough memory is allocated in \p pos to calculate \p n
78 * positions from \p isize atoms.
81 gmx_ana_pos_reserve(gmx_ana_pos_t *pos, int n, int isize)
83 if (pos->nalloc_x < n)
98 gmx_ana_indexmap_reserve(&pos->m, n, isize);
103 * \param[in,out] pos Position data structure.
105 * Currently, this function can only be called after gmx_ana_pos_reserve()
106 * has been called at least once with a \p n > 0.
109 gmx_ana_pos_reserve_velocities(gmx_ana_pos_t *pos)
111 assert(pos->nalloc_x > 0);
114 snew(pos->v, pos->nalloc_x);
119 * \param[in,out] pos Position data structure.
121 * Currently, this function can only be called after gmx_ana_pos_reserve()
122 * has been called at least once with a \p n > 0.
125 gmx_ana_pos_reserve_forces(gmx_ana_pos_t *pos)
127 assert(pos->nalloc_x > 0);
130 snew(pos->f, pos->nalloc_x);
135 * \param[out] pos Position data structure to initialize.
136 * \param[in] x Position vector to use.
139 gmx_ana_pos_init_const(gmx_ana_pos_t *pos, rvec x)
141 gmx_ana_pos_clear(pos);
147 copy_rvec(x, pos->x[0]);
148 clear_rvec(pos->v[0]);
149 clear_rvec(pos->f[0]);
150 gmx_ana_indexmap_init(&pos->m, NULL, NULL, INDEX_UNKNOWN);
154 * \param[in,out] pos Position data structure.
156 * Frees any memory allocated within \p pos.
157 * The pointer \p pos itself is not freed.
159 * \see gmx_ana_pos_free()
162 gmx_ana_pos_deinit(gmx_ana_pos_t *pos)
165 sfree(pos->x); pos->x = NULL;
166 sfree(pos->v); pos->v = NULL;
167 sfree(pos->f); pos->f = NULL;
169 gmx_ana_indexmap_deinit(&pos->m);
173 * \param[in,out] pos Position data structure.
175 * Frees any memory allocated for \p pos.
176 * The pointer \p pos is also freed, and is invalid after the call.
178 * \see gmx_ana_pos_deinit()
181 gmx_ana_pos_free(gmx_ana_pos_t *pos)
183 gmx_ana_pos_deinit(pos);
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, gmx_bool bFirst)
201 gmx_ana_pos_reserve(dest, src->nr, 0);
204 gmx_ana_pos_reserve_velocities(dest);
208 gmx_ana_pos_reserve_forces(dest);
212 memcpy(dest->x, src->x, dest->nr*sizeof(*dest->x));
216 memcpy(dest->v, src->v, dest->nr*sizeof(*dest->v));
221 memcpy(dest->f, src->f, dest->nr*sizeof(*dest->f));
223 gmx_ana_indexmap_copy(&dest->m, &src->m, bFirst);
228 * \param[in,out] pos Position data structure.
229 * \param[in] nr Number of positions.
232 gmx_ana_pos_set_nr(gmx_ana_pos_t *pos, int nr)
238 * \param[in,out] pos Position data structure.
239 * \param g Evaluation group.
241 * The old group, if any, is discarded.
242 * Note that only a pointer to \p g is stored; it is the responsibility of
243 * the caller to ensure that \p g is not freed while it can be accessed
247 gmx_ana_pos_set_evalgrp(gmx_ana_pos_t *pos, gmx_ana_index_t *g)
253 * \param[in,out] pos Position data structure.
255 * Sets the number of positions to 0.
258 gmx_ana_pos_empty_init(gmx_ana_pos_t *pos)
265 /* This should not really be necessary, but do it for safety... */
266 pos->m.mapb.index[0] = 0;
267 pos->m.b.index[0] = 0;
268 /* This function should only be used to construct all the possible
269 * positions, so the result should always be static. */
270 pos->m.bStatic = TRUE;
271 pos->m.bMapStatic = TRUE;
275 * \param[in,out] pos Position data structure.
277 * Sets the number of positions to 0.
280 gmx_ana_pos_empty(gmx_ana_pos_t *pos)
285 /* This should not really be necessary, but do it for safety... */
286 pos->m.mapb.index[0] = 0;
287 /* We set the flags to TRUE, although really in the empty state they
288 * should be FALSE. This makes it possible to update the flags in
289 * gmx_ana_pos_append(), and just make a simple check in
290 * gmx_ana_pos_append_finish(). */
291 pos->m.bStatic = TRUE;
292 pos->m.bMapStatic = TRUE;
296 * \param[in,out] dest Data structure to which the new position is appended.
297 * \param[in,out] g Data structure to which the new atoms are appended.
298 * \param[in] src Data structure from which the position is copied.
299 * \param[in] i Index in \p from to copy.
302 gmx_ana_pos_append_init(gmx_ana_pos_t *dest, gmx_ana_index_t *g,
303 gmx_ana_pos_t *src, int i)
308 copy_rvec(src->x[i], dest->x[j]);
313 copy_rvec(src->v[i], dest->v[j]);
317 clear_rvec(dest->v[j]);
324 copy_rvec(src->f[i], dest->f[j]);
328 clear_rvec(dest->f[j]);
331 dest->m.refid[j] = j;
332 dest->m.mapid[j] = src->m.mapid[i];
333 dest->m.orgid[j] = src->m.orgid[i];
334 for (k = src->m.mapb.index[i]; k < src->m.mapb.index[i+1]; ++k)
336 g->index[g->isize++] = src->g->index[k];
337 dest->m.b.a[dest->m.b.nra++] = src->m.b.a[k];
339 dest->m.mapb.index[j+1] = g->isize;
340 dest->m.b.index[j+1] = g->isize;
342 dest->m.nr = dest->nr;
343 dest->m.mapb.nr = dest->nr;
344 dest->m.b.nr = dest->nr;
348 * \param[in,out] dest Data structure to which the new position is appended
349 * (can be NULL, in which case only \p g is updated).
350 * \param[in,out] g Data structure to which the new atoms are appended.
351 * \param[in] src Data structure from which the position is copied.
352 * \param[in] i Index in \p src to copy.
353 * \param[in] refid Reference ID in \p out
354 * (all negative values are treated as -1).
356 * If \p dest is NULL, the value of \p refid is not used.
359 gmx_ana_pos_append(gmx_ana_pos_t *dest, gmx_ana_index_t *g,
360 gmx_ana_pos_t *src, int i, int refid)
364 for (k = src->m.mapb.index[i]; k < src->m.mapb.index[i+1]; ++k)
366 g->index[g->isize++] = src->g->index[k];
375 copy_rvec(src->v[i], dest->v[j]);
379 clear_rvec(dest->v[j]);
386 copy_rvec(src->f[i], dest->f[j]);
390 clear_rvec(dest->f[j]);
393 copy_rvec(src->x[i], dest->x[j]);
396 dest->m.refid[j] = -1;
397 dest->m.bStatic = FALSE;
398 /* If we are using masks, there is no need to alter the
405 dest->m.bStatic = FALSE;
406 dest->m.bMapStatic = FALSE;
408 dest->m.refid[j] = refid;
409 /* Use the original IDs from the output structure to correctly
410 * handle user customization. */
411 dest->m.mapid[j] = dest->m.orgid[refid];
413 dest->m.mapb.index[j+1] = g->isize;
415 dest->m.nr = dest->nr;
416 dest->m.mapb.nr = dest->nr;
421 * \param[in,out] pos Position data structure.
423 * After gmx_ana_pos_empty(), internal state of the position data structure
424 * is not consistent before this function is called. This function should be
425 * called after any gmx_ana_pos_append() calls have been made.
428 gmx_ana_pos_append_finish(gmx_ana_pos_t *pos)
430 if (pos->m.nr != pos->m.b.nr)
432 pos->m.bStatic = FALSE;
433 pos->m.bMapStatic = FALSE;