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.
40 * Implementation of functions in selection.h.
42 /*! \internal \dir src/gmxlib/selection
44 * Source code for selection-related routines.
54 #include <gmx_fatal.h>
57 #include <selection.h>
58 #include <selmethod.h>
61 #include "selcollection.h"
66 * \param[out] scp Pointer to a newly allocated empty selection collection.
67 * \param[in] pcc Position calculation data structure to use for selection
68 * position evaluation.
69 * \returns 0 on success.
72 gmx_ana_selcollection_create(gmx_ana_selcollection_t **scp,
73 gmx_ana_poscalc_coll_t *pcc)
75 gmx_ana_selcollection_t *sc;
80 sc->bMaskOnly = FALSE;
81 sc->bVelocities = FALSE;
83 sc->bDebugCompile = FALSE;
90 gmx_ana_index_clear(&sc->gall);
93 _gmx_sel_symtab_create(&sc->symtab);
99 * \param[in,out] sc Selection collection to free.
101 * The pointer \p sc is invalid after the call.
104 gmx_ana_selcollection_free(gmx_ana_selcollection_t *sc)
108 _gmx_selelem_free_chain(sc->root);
111 for (i = 0; i < sc->nr; ++i)
113 gmx_ana_selection_free(sc->sel[i]);
117 for (i = 0; i < sc->nvars; ++i)
119 sfree(sc->varstrs[i]);
122 gmx_ana_index_deinit(&sc->gall);
125 _gmx_sel_mempool_destroy(sc->mempool);
127 _gmx_selcollection_clear_symtab(sc);
132 * \param[in,out] sc Selection collection.
135 _gmx_selcollection_clear_symtab(gmx_ana_selcollection_t *sc)
139 _gmx_sel_symtab_free(sc->symtab);
145 * \param[in,out] sc Selection collection to modify.
146 * \param[in] type Default selection reference position type
147 * (one of the strings acceptable for gmx_ana_poscalc_type_from_enum()).
149 * Should be called before calling gmx_ana_selcollection_requires_top() or
150 * gmx_ana_selcollection_parse_*().
153 gmx_ana_selcollection_set_refpostype(gmx_ana_selcollection_t *sc,
160 * \param[in,out] sc Selection collection to modify.
161 * \param[in] type Default selection output position type
162 * (one of the strings acceptable for gmx_ana_poslcalc_type_from_enum()).
163 * \param[in] bMaskOnly If TRUE, the output positions are initialized
164 * using \ref POS_MASKONLY.
166 * If \p type is NULL, the default type is not modified.
167 * Should be called before calling gmx_ana_selcollection_requires_top() or
168 * gmx_ana_selcollection_parse_*().
171 gmx_ana_selcollection_set_outpostype(gmx_ana_selcollection_t *sc,
172 const char *type, gmx_bool bMaskOnly)
178 sc->bMaskOnly = bMaskOnly;
182 * \param[in,out] sc Selection collection to modify.
183 * \param[in] bVelOut If TRUE, selections will also evaluate
187 gmx_ana_selcollection_set_veloutput(gmx_ana_selcollection_t *sc,
190 sc->bVelocities = bVelOut;
194 * \param[in,out] sc Selection collection to modify.
195 * \param[in] bForceOut If TRUE, selections will also evaluate
199 gmx_ana_selcollection_set_forceoutput(gmx_ana_selcollection_t *sc,
202 sc->bForces = bForceOut;
206 * \param[in,out] sc Selection collection to set the topology for.
207 * \param[in] top Topology data.
208 * \param[in] natoms Number of atoms. If <=0, the number of atoms in the
210 * \returns 0 on success, EINVAL if \p top is NULL and \p natoms <= 0.
212 * The topology is also set for the position calculation collection
213 * associated with \p sc.
215 * \p natoms determines the largest atom index that can be selected by the
216 * selection: even if the topology contains more atoms, they will not be
220 gmx_ana_selcollection_set_topology(gmx_ana_selcollection_t *sc, t_topology *top,
223 gmx_ana_poscalc_coll_set_topology(sc->pcc, top);
226 /* Get the number of atoms from the topology if it is not given */
231 gmx_incons("selections need either the topology or the number of atoms");
234 natoms = sc->top->atoms.nr;
236 gmx_ana_index_init_simple(&sc->gall, natoms, NULL);
241 * \param[in] sc Selection collection to query.
242 * \returns Number of selections in \p sc.
244 * If gmx_ana_selcollection_parse_*() has not been called, returns 0.
246 * \see gmx_ana_selcollection_get_selection()
249 gmx_ana_selcollection_get_count(gmx_ana_selcollection_t *sc)
255 * \param[in] sc Selection collection to query.
256 * \param[in] i Number of the selection.
257 * \returns Pointer to the \p i'th selection in \p sc,
258 * or NULL if there is no such selection.
260 * \p i should be between 0 and the value returned by
261 * gmx_ana_selcollection_get_count().
262 * The returned pointer should not be freed.
263 * If gmx_ana_selcollection_compile() has not been called, the returned
264 * selection is not completely initialized (but the returned pointer will be
265 * valid even after compilation, and will point to the initialized selection).
267 * \see gmx_ana_selcollection_get_count()
269 gmx_ana_selection_t *
270 gmx_ana_selcollection_get_selection(gmx_ana_selcollection_t *sc, int i)
272 if (i < 0 || i >= sc->nr || !sc->sel)
278 * \param[in] sc Selection collection to query.
279 * \returns TRUE if any selection in \p sc requires topology information,
282 * Before gmx_ana_selcollection_parse_*(), the return value is based just on
283 * the position types set.
284 * After gmx_ana_selcollection_parse_*(), the return value also takes into account the
285 * selection keywords used.
288 gmx_ana_selcollection_requires_top(gmx_ana_selcollection_t *sc)
298 rc = gmx_ana_poscalc_type_from_enum(sc->rpost, &type, &flags);
299 if (rc == 0 && type != POS_ATOM)
307 rc = gmx_ana_poscalc_type_from_enum(sc->spost, &type, &flags);
308 if (rc == 0 && type != POS_ATOM)
317 if (_gmx_selelem_requires_top(sel))
327 * \param[in] fp File handle to receive the output.
328 * \param[in] sc Selection collection to print.
329 * \param[in] bValues If TRUE, the evaluated values of selection elements
330 * are printed as well.
333 gmx_ana_selcollection_print_tree(FILE *fp, gmx_ana_selcollection_t *sc, gmx_bool bValues)
340 _gmx_selelem_print_tree(fp, sel, bValues, 0);
346 * \param[in] sel Selection to free.
348 * After the call, the pointer \p sel is invalid.
351 gmx_ana_selection_free(gmx_ana_selection_t *sel)
355 gmx_ana_pos_deinit(&sel->p);
356 if (sel->m != sel->orgm)
360 if (sel->q != sel->orgq)
370 * \param[in] sel Selection whose name is needed.
371 * \returns Pointer to the name of the selection.
373 * The return value should not be freed by the caller.
376 gmx_ana_selection_name(gmx_ana_selection_t *sel)
382 * \param[in] sel Selection for which information should be printed.
385 gmx_ana_selection_print_info(gmx_ana_selection_t *sel)
387 fprintf(stderr, "\"%s\" (%d position%s, %d atom%s%s)", sel->name,
388 sel->p.nr, sel->p.nr == 1 ? "" : "s",
389 sel->g->isize, sel->g->isize == 1 ? "" : "s",
390 sel->bDynamic ? ", dynamic" : "");
391 fprintf(stderr, "\n");
395 * \param[in] sel Selection to initialize.
396 * \param[in] type Type of covered fraction required.
397 * \returns TRUE if the covered fraction can be calculated for the selection,
401 gmx_ana_selection_init_coverfrac(gmx_ana_selection_t *sel, e_coverfrac_t type)
403 sel->cfractype = type;
404 if (type == CFRAC_NONE || !sel->selelem)
406 sel->bCFracDyn = FALSE;
408 else if (!_gmx_selelem_can_estimate_cover(sel->selelem))
410 sel->cfractype = CFRAC_NONE;
411 sel->bCFracDyn = FALSE;
415 sel->bCFracDyn = TRUE;
417 sel->cfrac = sel->bCFracDyn ? 0.0 : 1.0;
418 sel->avecfrac = sel->cfrac;
419 return type == CFRAC_NONE || sel->cfractype != CFRAC_NONE;
423 * \param[in] out Output file.
424 * \param[in] sc Selection collection which should be written.
425 * \param[in] oenv Output options structure.
427 void xvgr_selcollection(FILE *out, gmx_ana_selcollection_t *sc,
428 const output_env_t oenv)
432 if (output_env_get_xvg_format(oenv) != exvgNONE && sc)
434 fprintf(out, "# Selections:\n");
435 for (i = 0; i < sc->nvars; ++i)
437 fprintf(out, "# %s\n", sc->varstrs[i]);
439 for (i = 0; i < sc->nr; ++i)
441 fprintf(out, "# %s\n", sc->sel[i]->selstr);