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 * Implementation of functions in selection.h.
35 /*! \internal \dir src/gmxlib/selection
37 * Source code for selection-related routines.
47 #include <gmx_fatal.h>
50 #include <selection.h>
51 #include <selmethod.h>
54 #include "selcollection.h"
59 * \param[out] scp Pointer to a newly allocated empty selection collection.
60 * \param[in] pcc Position calculation data structure to use for selection
61 * position evaluation.
62 * \returns 0 on success.
65 gmx_ana_selcollection_create(gmx_ana_selcollection_t **scp,
66 gmx_ana_poscalc_coll_t *pcc)
68 gmx_ana_selcollection_t *sc;
73 sc->bMaskOnly = FALSE;
74 sc->bVelocities = FALSE;
76 sc->bDebugCompile = FALSE;
83 gmx_ana_index_clear(&sc->gall);
86 _gmx_sel_symtab_create(&sc->symtab);
92 * \param[in,out] sc Selection collection to free.
94 * The pointer \p sc is invalid after the call.
97 gmx_ana_selcollection_free(gmx_ana_selcollection_t *sc)
101 _gmx_selelem_free_chain(sc->root);
104 for (i = 0; i < sc->nr; ++i)
106 gmx_ana_selection_free(sc->sel[i]);
110 for (i = 0; i < sc->nvars; ++i)
112 sfree(sc->varstrs[i]);
115 gmx_ana_index_deinit(&sc->gall);
118 _gmx_sel_mempool_destroy(sc->mempool);
120 _gmx_selcollection_clear_symtab(sc);
125 * \param[in,out] sc Selection collection.
128 _gmx_selcollection_clear_symtab(gmx_ana_selcollection_t *sc)
132 _gmx_sel_symtab_free(sc->symtab);
138 * \param[in,out] sc Selection collection to modify.
139 * \param[in] type Default selection reference position type
140 * (one of the strings acceptable for gmx_ana_poscalc_type_from_enum()).
142 * Should be called before calling gmx_ana_selcollection_requires_top() or
143 * gmx_ana_selcollection_parse_*().
146 gmx_ana_selcollection_set_refpostype(gmx_ana_selcollection_t *sc,
153 * \param[in,out] sc Selection collection to modify.
154 * \param[in] type Default selection output position type
155 * (one of the strings acceptable for gmx_ana_poslcalc_type_from_enum()).
156 * \param[in] bMaskOnly If TRUE, the output positions are initialized
157 * using \ref POS_MASKONLY.
159 * If \p type is NULL, the default type is not modified.
160 * Should be called before calling gmx_ana_selcollection_requires_top() or
161 * gmx_ana_selcollection_parse_*().
164 gmx_ana_selcollection_set_outpostype(gmx_ana_selcollection_t *sc,
165 const char *type, gmx_bool bMaskOnly)
171 sc->bMaskOnly = bMaskOnly;
175 * \param[in,out] sc Selection collection to modify.
176 * \param[in] bVelOut If TRUE, selections will also evaluate
180 gmx_ana_selcollection_set_veloutput(gmx_ana_selcollection_t *sc,
183 sc->bVelocities = bVelOut;
187 * \param[in,out] sc Selection collection to modify.
188 * \param[in] bForceOut If TRUE, selections will also evaluate
192 gmx_ana_selcollection_set_forceoutput(gmx_ana_selcollection_t *sc,
195 sc->bForces = bForceOut;
199 * \param[in,out] sc Selection collection to set the topology for.
200 * \param[in] top Topology data.
201 * \param[in] natoms Number of atoms. If <=0, the number of atoms in the
203 * \returns 0 on success, EINVAL if \p top is NULL and \p natoms <= 0.
205 * The topology is also set for the position calculation collection
206 * associated with \p sc.
208 * \p natoms determines the largest atom index that can be selected by the
209 * selection: even if the topology contains more atoms, they will not be
213 gmx_ana_selcollection_set_topology(gmx_ana_selcollection_t *sc, t_topology *top,
216 gmx_ana_poscalc_coll_set_topology(sc->pcc, top);
219 /* Get the number of atoms from the topology if it is not given */
224 gmx_incons("selections need either the topology or the number of atoms");
227 natoms = sc->top->atoms.nr;
229 gmx_ana_index_init_simple(&sc->gall, natoms, NULL);
234 * \param[in] sc Selection collection to query.
235 * \returns Number of selections in \p sc.
237 * If gmx_ana_selcollection_parse_*() has not been called, returns 0.
239 * \see gmx_ana_selcollection_get_selection()
242 gmx_ana_selcollection_get_count(gmx_ana_selcollection_t *sc)
248 * \param[in] sc Selection collection to query.
249 * \param[in] i Number of the selection.
250 * \returns Pointer to the \p i'th selection in \p sc,
251 * or NULL if there is no such selection.
253 * \p i should be between 0 and the value returned by
254 * gmx_ana_selcollection_get_count().
255 * The returned pointer should not be freed.
256 * If gmx_ana_selcollection_compile() has not been called, the returned
257 * selection is not completely initialized (but the returned pointer will be
258 * valid even after compilation, and will point to the initialized selection).
260 * \see gmx_ana_selcollection_get_count()
262 gmx_ana_selection_t *
263 gmx_ana_selcollection_get_selection(gmx_ana_selcollection_t *sc, int i)
265 if (i < 0 || i >= sc->nr || !sc->sel)
271 * \param[in] sc Selection collection to query.
272 * \returns TRUE if any selection in \p sc requires topology information,
275 * Before gmx_ana_selcollection_parse_*(), the return value is based just on
276 * the position types set.
277 * After gmx_ana_selcollection_parse_*(), the return value also takes into account the
278 * selection keywords used.
281 gmx_ana_selcollection_requires_top(gmx_ana_selcollection_t *sc)
291 rc = gmx_ana_poscalc_type_from_enum(sc->rpost, &type, &flags);
292 if (rc == 0 && type != POS_ATOM)
300 rc = gmx_ana_poscalc_type_from_enum(sc->spost, &type, &flags);
301 if (rc == 0 && type != POS_ATOM)
310 if (_gmx_selelem_requires_top(sel))
320 * \param[in] fp File handle to receive the output.
321 * \param[in] sc Selection collection to print.
322 * \param[in] bValues If TRUE, the evaluated values of selection elements
323 * are printed as well.
326 gmx_ana_selcollection_print_tree(FILE *fp, gmx_ana_selcollection_t *sc, gmx_bool bValues)
333 _gmx_selelem_print_tree(fp, sel, bValues, 0);
339 * \param[in] sel Selection to free.
341 * After the call, the pointer \p sel is invalid.
344 gmx_ana_selection_free(gmx_ana_selection_t *sel)
348 gmx_ana_pos_deinit(&sel->p);
349 if (sel->m != sel->orgm)
353 if (sel->q != sel->orgq)
363 * \param[in] sel Selection whose name is needed.
364 * \returns Pointer to the name of the selection.
366 * The return value should not be freed by the caller.
369 gmx_ana_selection_name(gmx_ana_selection_t *sel)
375 * \param[in] sel Selection for which information should be printed.
378 gmx_ana_selection_print_info(gmx_ana_selection_t *sel)
380 fprintf(stderr, "\"%s\" (%d position%s, %d atom%s%s)", sel->name,
381 sel->p.nr, sel->p.nr == 1 ? "" : "s",
382 sel->g->isize, sel->g->isize == 1 ? "" : "s",
383 sel->bDynamic ? ", dynamic" : "");
384 fprintf(stderr, "\n");
388 * \param[in] sel Selection to initialize.
389 * \param[in] type Type of covered fraction required.
390 * \returns TRUE if the covered fraction can be calculated for the selection,
394 gmx_ana_selection_init_coverfrac(gmx_ana_selection_t *sel, e_coverfrac_t type)
396 sel->cfractype = type;
397 if (type == CFRAC_NONE || !sel->selelem)
399 sel->bCFracDyn = FALSE;
401 else if (!_gmx_selelem_can_estimate_cover(sel->selelem))
403 sel->cfractype = CFRAC_NONE;
404 sel->bCFracDyn = FALSE;
408 sel->bCFracDyn = TRUE;
410 sel->cfrac = sel->bCFracDyn ? 0.0 : 1.0;
411 sel->avecfrac = sel->cfrac;
412 return type == CFRAC_NONE || sel->cfractype != CFRAC_NONE;
416 * \param[in] out Output file.
417 * \param[in] sc Selection collection which should be written.
418 * \param[in] oenv Output options structure.
420 void xvgr_selcollection(FILE *out, gmx_ana_selcollection_t *sc,
421 const output_env_t oenv)
425 if (output_env_get_xvg_format(oenv) != exvgNONE && sc)
427 fprintf(out, "# Selections:\n");
428 for (i = 0; i < sc->nvars; ++i)
430 fprintf(out, "# %s\n", sc->varstrs[i]);
432 for (i = 0; i < sc->nr; ++i)
434 fprintf(out, "# %s\n", sc->sel[i]->selstr);