3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
32 * \brief Implementation of functions in position.h.
45 #include <indexutil.h>
49 * \param[out] pos Output structure.
51 * Any contents of \p pos are discarded without freeing.
54 gmx_ana_pos_clear(gmx_ana_pos_t *pos)
60 gmx_ana_indexmap_clear(&pos->m);
66 * \param[in,out] pos Position data structure.
67 * \param[in] n Maximum number of positions.
68 * \param[in] isize Maximum number of atoms.
70 * Ensures that enough memory is allocated in \p pos to calculate \p n
71 * positions from \p isize atoms.
74 gmx_ana_pos_reserve(gmx_ana_pos_t *pos, int n, int isize)
76 if (pos->nalloc_x < n)
91 gmx_ana_indexmap_reserve(&pos->m, n, isize);
96 * \param[in,out] pos Position data structure.
98 * Currently, this function can only be called after gmx_ana_pos_reserve()
99 * has been called at least once with a \p n > 0.
102 gmx_ana_pos_reserve_velocities(gmx_ana_pos_t *pos)
104 assert(pos->nalloc_x > 0);
107 snew(pos->v, pos->nalloc_x);
112 * \param[in,out] pos Position data structure.
114 * Currently, this function can only be called after gmx_ana_pos_reserve()
115 * has been called at least once with a \p n > 0.
118 gmx_ana_pos_reserve_forces(gmx_ana_pos_t *pos)
120 assert(pos->nalloc_x > 0);
123 snew(pos->f, pos->nalloc_x);
128 * \param[out] pos Position data structure to initialize.
129 * \param[in] x Position vector to use.
132 gmx_ana_pos_init_const(gmx_ana_pos_t *pos, rvec x)
134 gmx_ana_pos_clear(pos);
140 copy_rvec(x, pos->x[0]);
141 clear_rvec(pos->v[0]);
142 clear_rvec(pos->f[0]);
143 gmx_ana_indexmap_init(&pos->m, NULL, NULL, INDEX_UNKNOWN);
147 * \param[in,out] pos Position data structure.
149 * Frees any memory allocated within \p pos.
150 * The pointer \p pos itself is not freed.
152 * \see gmx_ana_pos_free()
155 gmx_ana_pos_deinit(gmx_ana_pos_t *pos)
158 sfree(pos->x); pos->x = NULL;
159 sfree(pos->v); pos->v = NULL;
160 sfree(pos->f); pos->f = NULL;
162 gmx_ana_indexmap_deinit(&pos->m);
166 * \param[in,out] pos Position data structure.
168 * Frees any memory allocated for \p pos.
169 * The pointer \p pos is also freed, and is invalid after the call.
171 * \see gmx_ana_pos_deinit()
174 gmx_ana_pos_free(gmx_ana_pos_t *pos)
176 gmx_ana_pos_deinit(pos);
181 * \param[in,out] dest Destination positions.
182 * \param[in] src Source positions.
183 * \param[in] bFirst If TRUE, memory is allocated for \p dest and a full
184 * copy is made; otherwise, only variable parts are copied, and no memory
187 * \p dest should have been initialized somehow (calloc() is enough).
190 gmx_ana_pos_copy(gmx_ana_pos_t *dest, gmx_ana_pos_t *src, gmx_bool bFirst)
194 gmx_ana_pos_reserve(dest, src->nr, 0);
197 gmx_ana_pos_reserve_velocities(dest);
201 gmx_ana_pos_reserve_forces(dest);
205 memcpy(dest->x, src->x, dest->nr*sizeof(*dest->x));
209 memcpy(dest->v, src->v, dest->nr*sizeof(*dest->v));
214 memcpy(dest->f, src->f, dest->nr*sizeof(*dest->f));
216 gmx_ana_indexmap_copy(&dest->m, &src->m, bFirst);
221 * \param[in,out] pos Position data structure.
222 * \param[in] nr Number of positions.
225 gmx_ana_pos_set_nr(gmx_ana_pos_t *pos, int nr)
231 * \param[in,out] pos Position data structure.
232 * \param g Evaluation group.
234 * The old group, if any, is discarded.
235 * Note that only a pointer to \p g is stored; it is the responsibility of
236 * the caller to ensure that \p g is not freed while it can be accessed
240 gmx_ana_pos_set_evalgrp(gmx_ana_pos_t *pos, gmx_ana_index_t *g)
246 * \param[in,out] pos Position data structure.
248 * Sets the number of positions to 0.
251 gmx_ana_pos_empty_init(gmx_ana_pos_t *pos)
258 /* This should not really be necessary, but do it for safety... */
259 pos->m.mapb.index[0] = 0;
260 pos->m.b.index[0] = 0;
261 /* This function should only be used to construct all the possible
262 * positions, so the result should always be static. */
263 pos->m.bStatic = TRUE;
264 pos->m.bMapStatic = TRUE;
268 * \param[in,out] pos Position data structure.
270 * Sets the number of positions to 0.
273 gmx_ana_pos_empty(gmx_ana_pos_t *pos)
278 /* This should not really be necessary, but do it for safety... */
279 pos->m.mapb.index[0] = 0;
280 /* We set the flags to TRUE, although really in the empty state they
281 * should be FALSE. This makes it possible to update the flags in
282 * gmx_ana_pos_append(), and just make a simple check in
283 * gmx_ana_pos_append_finish(). */
284 pos->m.bStatic = TRUE;
285 pos->m.bMapStatic = TRUE;
289 * \param[in,out] dest Data structure to which the new position is appended.
290 * \param[in,out] g Data structure to which the new atoms are appended.
291 * \param[in] src Data structure from which the position is copied.
292 * \param[in] i Index in \p from to copy.
295 gmx_ana_pos_append_init(gmx_ana_pos_t *dest, gmx_ana_index_t *g,
296 gmx_ana_pos_t *src, int i)
301 copy_rvec(src->x[i], dest->x[j]);
306 copy_rvec(src->v[i], dest->v[j]);
310 clear_rvec(dest->v[j]);
317 copy_rvec(src->f[i], dest->f[j]);
321 clear_rvec(dest->f[j]);
324 dest->m.refid[j] = j;
325 dest->m.mapid[j] = src->m.mapid[i];
326 dest->m.orgid[j] = src->m.orgid[i];
327 for (k = src->m.mapb.index[i]; k < src->m.mapb.index[i+1]; ++k)
329 g->index[g->isize++] = src->g->index[k];
330 dest->m.b.a[dest->m.b.nra++] = src->m.b.a[k];
332 dest->m.mapb.index[j+1] = g->isize;
333 dest->m.b.index[j+1] = g->isize;
335 dest->m.nr = dest->nr;
336 dest->m.mapb.nr = dest->nr;
337 dest->m.b.nr = dest->nr;
341 * \param[in,out] dest Data structure to which the new position is appended
342 * (can be NULL, in which case only \p g is updated).
343 * \param[in,out] g Data structure to which the new atoms are appended.
344 * \param[in] src Data structure from which the position is copied.
345 * \param[in] i Index in \p src to copy.
346 * \param[in] refid Reference ID in \p out
347 * (all negative values are treated as -1).
349 * If \p dest is NULL, the value of \p refid is not used.
352 gmx_ana_pos_append(gmx_ana_pos_t *dest, gmx_ana_index_t *g,
353 gmx_ana_pos_t *src, int i, int refid)
357 for (k = src->m.mapb.index[i]; k < src->m.mapb.index[i+1]; ++k)
359 g->index[g->isize++] = src->g->index[k];
368 copy_rvec(src->v[i], dest->v[j]);
372 clear_rvec(dest->v[j]);
379 copy_rvec(src->f[i], dest->f[j]);
383 clear_rvec(dest->f[j]);
386 copy_rvec(src->x[i], dest->x[j]);
389 dest->m.refid[j] = -1;
390 dest->m.bStatic = FALSE;
391 /* If we are using masks, there is no need to alter the
398 dest->m.bStatic = FALSE;
399 dest->m.bMapStatic = FALSE;
401 dest->m.refid[j] = refid;
402 /* Use the original IDs from the output structure to correctly
403 * handle user customization. */
404 dest->m.mapid[j] = dest->m.orgid[refid];
406 dest->m.mapb.index[j+1] = g->isize;
408 dest->m.nr = dest->nr;
409 dest->m.mapb.nr = dest->nr;
414 * \param[in,out] pos Position data structure.
416 * After gmx_ana_pos_empty(), internal state of the position data structure
417 * is not consistent before this function is called. This function should be
418 * called after any gmx_ana_pos_append() calls have been made.
421 gmx_ana_pos_append_finish(gmx_ana_pos_t *pos)
423 if (pos->m.nr != pos->m.b.nr)
425 pos->m.bStatic = FALSE;
426 pos->m.bMapStatic = FALSE;