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
40 #include "gromacs/legacyheaders/smalloc.h"
41 #include "gromacs/legacyheaders/typedefs.h"
42 #include "gromacs/legacyheaders/vec.h"
44 #include "gromacs/selection/indexutil.h"
45 #include "gromacs/selection/position.h"
46 #include "gromacs/utility/gmxassert.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 GMX_RELEASE_ASSERT(n >= 0, "Invalid position allocation count");
77 // Always reserve at least one entry to make NULL checks against pos->x
78 // and gmx_ana_pos_reserve_velocities/forces() work as expected in the case
79 // that there are actually no positions.
84 if (pos->nalloc_x < n)
99 gmx_ana_indexmap_reserve(&pos->m, n, isize);
104 * \param[in,out] pos Position data structure.
106 * Currently, this function can only be called after gmx_ana_pos_reserve()
107 * has been called at least once with a \p n >= 0.
110 gmx_ana_pos_reserve_velocities(gmx_ana_pos_t *pos)
112 GMX_RELEASE_ASSERT(pos->nalloc_x > 0,
113 "No memory reserved yet for positions");
116 snew(pos->v, pos->nalloc_x);
121 * \param[in,out] pos Position data structure.
123 * Currently, this function can only be called after gmx_ana_pos_reserve()
124 * has been called at least once with a \p n >= 0.
127 gmx_ana_pos_reserve_forces(gmx_ana_pos_t *pos)
129 GMX_RELEASE_ASSERT(pos->nalloc_x > 0,
130 "No memory reserved yet for positions");
133 snew(pos->f, pos->nalloc_x);
138 * \param[out] pos Position data structure to initialize.
139 * \param[in] x Position vector to use.
142 gmx_ana_pos_init_const(gmx_ana_pos_t *pos, const rvec x)
144 gmx_ana_pos_clear(pos);
150 copy_rvec(x, pos->x[0]);
151 clear_rvec(pos->v[0]);
152 clear_rvec(pos->f[0]);
153 gmx_ana_indexmap_init(&pos->m, NULL, NULL, INDEX_UNKNOWN);
157 * \param[in,out] pos Position data structure.
159 * Frees any memory allocated within \p pos.
160 * The pointer \p pos itself is not freed.
162 * \see gmx_ana_pos_free()
165 gmx_ana_pos_deinit(gmx_ana_pos_t *pos)
168 sfree(pos->x); pos->x = NULL;
169 sfree(pos->v); pos->v = NULL;
170 sfree(pos->f); pos->f = NULL;
172 gmx_ana_indexmap_deinit(&pos->m);
176 * \param[in,out] pos Position data structure.
178 * Frees any memory allocated for \p pos.
179 * The pointer \p pos is also freed, and is invalid after the call.
181 * \see gmx_ana_pos_deinit()
184 gmx_ana_pos_free(gmx_ana_pos_t *pos)
186 gmx_ana_pos_deinit(pos);
191 * \param[in,out] dest Destination positions.
192 * \param[in] src Source positions.
193 * \param[in] bFirst If true, memory is allocated for \p dest and a full
194 * copy is made; otherwise, only variable parts are copied, and no memory
197 * \p dest should have been initialized somehow (calloc() is enough).
200 gmx_ana_pos_copy(gmx_ana_pos_t *dest, gmx_ana_pos_t *src, bool bFirst)
204 gmx_ana_pos_reserve(dest, src->nr, 0);
207 gmx_ana_pos_reserve_velocities(dest);
211 gmx_ana_pos_reserve_forces(dest);
215 memcpy(dest->x, src->x, dest->nr*sizeof(*dest->x));
218 GMX_ASSERT(src->v, "src velocities should be non-null if dest velocities are allocated");
219 memcpy(dest->v, src->v, dest->nr*sizeof(*dest->v));
223 GMX_ASSERT(src->f, "src forces should be non-null if dest forces are allocated");
224 memcpy(dest->f, src->f, dest->nr*sizeof(*dest->f));
226 gmx_ana_indexmap_copy(&dest->m, &src->m, bFirst);
231 * \param[in,out] pos Position data structure.
232 * \param[in] nr Number of positions.
235 gmx_ana_pos_set_nr(gmx_ana_pos_t *pos, int nr)
241 * \param[in,out] pos Position data structure.
242 * \param g Evaluation group.
244 * The old group, if any, is discarded.
245 * Note that only a pointer to \p g is stored; it is the responsibility of
246 * the caller to ensure that \p g is not freed while it can be accessed
250 gmx_ana_pos_set_evalgrp(gmx_ana_pos_t *pos, gmx_ana_index_t *g)
256 * \param[in,out] pos Position data structure.
258 * Sets the number of positions to 0.
261 gmx_ana_pos_empty_init(gmx_ana_pos_t *pos)
268 /* This should not really be necessary, but do it for safety... */
269 pos->m.mapb.index[0] = 0;
270 pos->m.b.index[0] = 0;
271 /* This function should only be used to construct all the possible
272 * positions, so the result should always be static. */
273 pos->m.bStatic = true;
274 pos->m.bMapStatic = true;
278 * \param[in,out] pos Position data structure.
280 * Sets the number of positions to 0.
283 gmx_ana_pos_empty(gmx_ana_pos_t *pos)
288 /* This should not really be necessary, but do it for safety... */
289 pos->m.mapb.index[0] = 0;
290 /* We set the flags to true, although really in the empty state they
291 * should be false. This makes it possible to update the flags in
292 * gmx_ana_pos_append(), and just make a simple check in
293 * gmx_ana_pos_append_finish(). */
294 pos->m.bStatic = true;
295 pos->m.bMapStatic = true;
299 * \param[in,out] dest Data structure to which the new position is appended.
300 * \param[in,out] g Data structure to which the new atoms are appended.
301 * \param[in] src Data structure from which the position is copied.
302 * \param[in] i Index in \p from to copy.
305 gmx_ana_pos_append_init(gmx_ana_pos_t *dest, gmx_ana_index_t *g,
306 gmx_ana_pos_t *src, int i)
311 copy_rvec(src->x[i], dest->x[j]);
316 copy_rvec(src->v[i], dest->v[j]);
320 clear_rvec(dest->v[j]);
327 copy_rvec(src->f[i], dest->f[j]);
331 clear_rvec(dest->f[j]);
334 dest->m.refid[j] = j;
335 dest->m.mapid[j] = src->m.mapid[i];
336 dest->m.orgid[j] = src->m.orgid[i];
337 for (k = src->m.mapb.index[i]; k < src->m.mapb.index[i+1]; ++k)
339 g->index[g->isize++] = src->g->index[k];
340 dest->m.b.a[dest->m.b.nra++] = src->m.b.a[k];
342 dest->m.mapb.index[j+1] = g->isize;
343 dest->m.b.index[j+1] = g->isize;
345 dest->m.nr = dest->nr;
346 dest->m.mapb.nr = dest->nr;
347 dest->m.b.nr = dest->nr;
351 * \param[in,out] dest Data structure to which the new position is appended
352 * (can be NULL, in which case only \p g is updated).
353 * \param[in,out] g Data structure to which the new atoms are appended.
354 * \param[in] src Data structure from which the position is copied.
355 * \param[in] i Index in \p src to copy.
356 * \param[in] refid Reference ID in \p out
357 * (all negative values are treated as -1).
359 * If \p dest is NULL, the value of \p refid is not used.
362 gmx_ana_pos_append(gmx_ana_pos_t *dest, gmx_ana_index_t *g,
363 gmx_ana_pos_t *src, int i, int refid)
367 for (k = src->m.mapb.index[i]; k < src->m.mapb.index[i+1]; ++k)
369 g->index[g->isize++] = src->g->index[k];
378 copy_rvec(src->v[i], dest->v[j]);
382 clear_rvec(dest->v[j]);
389 copy_rvec(src->f[i], dest->f[j]);
393 clear_rvec(dest->f[j]);
396 copy_rvec(src->x[i], dest->x[j]);
399 dest->m.refid[j] = -1;
400 dest->m.bStatic = false;
401 /* If we are using masks, there is no need to alter the
408 dest->m.bStatic = false;
409 dest->m.bMapStatic = false;
411 dest->m.refid[j] = refid;
412 /* Use the original IDs from the output structure to correctly
413 * handle user customization. */
414 dest->m.mapid[j] = dest->m.orgid[refid];
416 dest->m.mapb.index[j+1] = g->isize;
418 dest->m.nr = dest->nr;
419 dest->m.mapb.nr = dest->nr;
424 * \param[in,out] pos Position data structure.
426 * After gmx_ana_pos_empty(), internal state of the position data structure
427 * is not consistent before this function is called. This function should be
428 * called after any gmx_ana_pos_append() calls have been made.
431 gmx_ana_pos_append_finish(gmx_ana_pos_t *pos)
433 if (pos->m.nr != pos->m.b.nr)
435 pos->m.bStatic = false;
436 pos->m.bMapStatic = false;