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
33 * Implements functions in position.h.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_selection
48 #include "gromacs/selection/indexutil.h"
49 #include "gromacs/selection/position.h"
50 #include "gromacs/utility/gmxassert.h"
53 * \param[out] pos Output structure.
55 * Any contents of \p pos are discarded without freeing.
58 gmx_ana_pos_clear(gmx_ana_pos_t *pos)
64 gmx_ana_indexmap_clear(&pos->m);
70 * \param[in,out] pos Position data structure.
71 * \param[in] n Maximum number of positions.
72 * \param[in] isize Maximum number of atoms.
74 * Ensures that enough memory is allocated in \p pos to calculate \p n
75 * positions from \p isize atoms.
78 gmx_ana_pos_reserve(gmx_ana_pos_t *pos, int n, int isize)
80 if (pos->nalloc_x < n)
95 gmx_ana_indexmap_reserve(&pos->m, n, isize);
100 * \param[in,out] pos Position data structure.
102 * Currently, this function can only be called after gmx_ana_pos_reserve()
103 * has been called at least once with a \p n > 0.
106 gmx_ana_pos_reserve_velocities(gmx_ana_pos_t *pos)
108 GMX_RELEASE_ASSERT(pos->nalloc_x > 0,
109 "No memory reserved yet for positions");
112 snew(pos->v, pos->nalloc_x);
117 * \param[in,out] pos Position data structure.
119 * Currently, this function can only be called after gmx_ana_pos_reserve()
120 * has been called at least once with a \p n > 0.
123 gmx_ana_pos_reserve_forces(gmx_ana_pos_t *pos)
125 GMX_RELEASE_ASSERT(pos->nalloc_x > 0,
126 "No memory reserved yet for positions");
129 snew(pos->f, pos->nalloc_x);
134 * \param[out] pos Position data structure to initialize.
135 * \param[in] x Position vector to use.
138 gmx_ana_pos_init_const(gmx_ana_pos_t *pos, rvec x)
140 gmx_ana_pos_clear(pos);
146 copy_rvec(x, pos->x[0]);
147 clear_rvec(pos->v[0]);
148 clear_rvec(pos->f[0]);
149 gmx_ana_indexmap_init(&pos->m, NULL, NULL, INDEX_UNKNOWN);
153 * \param[in,out] pos Position data structure.
155 * Frees any memory allocated within \p pos.
156 * The pointer \p pos itself is not freed.
158 * \see gmx_ana_pos_free()
161 gmx_ana_pos_deinit(gmx_ana_pos_t *pos)
164 sfree(pos->x); pos->x = NULL;
165 sfree(pos->v); pos->v = NULL;
166 sfree(pos->f); pos->f = NULL;
168 gmx_ana_indexmap_deinit(&pos->m);
172 * \param[in,out] pos Position data structure.
174 * Frees any memory allocated for \p pos.
175 * The pointer \p pos is also freed, and is invalid after the call.
177 * \see gmx_ana_pos_deinit()
180 gmx_ana_pos_free(gmx_ana_pos_t *pos)
182 gmx_ana_pos_deinit(pos);
187 * \param[in,out] dest Destination positions.
188 * \param[in] src Source positions.
189 * \param[in] bFirst If true, memory is allocated for \p dest and a full
190 * copy is made; otherwise, only variable parts are copied, and no memory
193 * \p dest should have been initialized somehow (calloc() is enough).
196 gmx_ana_pos_copy(gmx_ana_pos_t *dest, gmx_ana_pos_t *src, bool bFirst)
200 gmx_ana_pos_reserve(dest, src->nr, 0);
203 gmx_ana_pos_reserve_velocities(dest);
207 gmx_ana_pos_reserve_forces(dest);
211 memcpy(dest->x, src->x, dest->nr*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, dest->nr*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, dest->nr*sizeof(*dest->f));
222 gmx_ana_indexmap_copy(&dest->m, &src->m, bFirst);
227 * \param[in,out] pos Position data structure.
228 * \param[in] nr Number of positions.
231 gmx_ana_pos_set_nr(gmx_ana_pos_t *pos, int nr)
237 * \param[in,out] pos Position data structure.
238 * \param g Evaluation group.
240 * The old group, if any, is discarded.
241 * Note that only a pointer to \p g is stored; it is the responsibility of
242 * the caller to ensure that \p g is not freed while it can be accessed
246 gmx_ana_pos_set_evalgrp(gmx_ana_pos_t *pos, gmx_ana_index_t *g)
252 * \param[in,out] pos Position data structure.
254 * Sets the number of positions to 0.
257 gmx_ana_pos_empty_init(gmx_ana_pos_t *pos)
264 /* This should not really be necessary, but do it for safety... */
265 pos->m.mapb.index[0] = 0;
266 pos->m.b.index[0] = 0;
267 /* This function should only be used to construct all the possible
268 * positions, so the result should always be static. */
269 pos->m.bStatic = true;
270 pos->m.bMapStatic = true;
274 * \param[in,out] pos Position data structure.
276 * Sets the number of positions to 0.
279 gmx_ana_pos_empty(gmx_ana_pos_t *pos)
284 /* This should not really be necessary, but do it for safety... */
285 pos->m.mapb.index[0] = 0;
286 /* We set the flags to true, although really in the empty state they
287 * should be false. This makes it possible to update the flags in
288 * gmx_ana_pos_append(), and just make a simple check in
289 * gmx_ana_pos_append_finish(). */
290 pos->m.bStatic = true;
291 pos->m.bMapStatic = true;
295 * \param[in,out] dest Data structure to which the new position is appended.
296 * \param[in,out] g Data structure to which the new atoms are appended.
297 * \param[in] src Data structure from which the position is copied.
298 * \param[in] i Index in \p from to copy.
301 gmx_ana_pos_append_init(gmx_ana_pos_t *dest, gmx_ana_index_t *g,
302 gmx_ana_pos_t *src, int i)
307 copy_rvec(src->x[i], dest->x[j]);
312 copy_rvec(src->v[i], dest->v[j]);
316 clear_rvec(dest->v[j]);
323 copy_rvec(src->f[i], dest->f[j]);
327 clear_rvec(dest->f[j]);
330 dest->m.refid[j] = j;
331 dest->m.mapid[j] = src->m.mapid[i];
332 dest->m.orgid[j] = src->m.orgid[i];
333 for (k = src->m.mapb.index[i]; k < src->m.mapb.index[i+1]; ++k)
335 g->index[g->isize++] = src->g->index[k];
336 dest->m.b.a[dest->m.b.nra++] = src->m.b.a[k];
338 dest->m.mapb.index[j+1] = g->isize;
339 dest->m.b.index[j+1] = g->isize;
341 dest->m.nr = dest->nr;
342 dest->m.mapb.nr = dest->nr;
343 dest->m.b.nr = dest->nr;
347 * \param[in,out] dest Data structure to which the new position is appended
348 * (can be NULL, in which case only \p g is updated).
349 * \param[in,out] g Data structure to which the new atoms are appended.
350 * \param[in] src Data structure from which the position is copied.
351 * \param[in] i Index in \p src to copy.
352 * \param[in] refid Reference ID in \p out
353 * (all negative values are treated as -1).
355 * If \p dest is NULL, the value of \p refid is not used.
358 gmx_ana_pos_append(gmx_ana_pos_t *dest, gmx_ana_index_t *g,
359 gmx_ana_pos_t *src, int i, int refid)
363 for (k = src->m.mapb.index[i]; k < src->m.mapb.index[i+1]; ++k)
365 g->index[g->isize++] = src->g->index[k];
374 copy_rvec(src->v[i], dest->v[j]);
378 clear_rvec(dest->v[j]);
385 copy_rvec(src->f[i], dest->f[j]);
389 clear_rvec(dest->f[j]);
392 copy_rvec(src->x[i], dest->x[j]);
395 dest->m.refid[j] = -1;
396 dest->m.bStatic = false;
397 /* If we are using masks, there is no need to alter the
404 dest->m.bStatic = false;
405 dest->m.bMapStatic = false;
407 dest->m.refid[j] = refid;
408 /* Use the original IDs from the output structure to correctly
409 * handle user customization. */
410 dest->m.mapid[j] = dest->m.orgid[refid];
412 dest->m.mapb.index[j+1] = g->isize;
414 dest->m.nr = dest->nr;
415 dest->m.mapb.nr = dest->nr;
420 * \param[in,out] pos Position data structure.
422 * After gmx_ana_pos_empty(), internal state of the position data structure
423 * is not consistent before this function is called. This function should be
424 * called after any gmx_ana_pos_append() calls have been made.
427 gmx_ana_pos_append_finish(gmx_ana_pos_t *pos)
429 if (pos->m.nr != pos->m.b.nr)
431 pos->m.bStatic = false;
432 pos->m.bMapStatic = false;