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
;
82 gmx_ana_index_clear(&sc
->gall
);
85 _gmx_sel_symtab_create(&sc
->symtab
);
91 * \param[in,out] sc Selection collection to free.
93 * The pointer \p sc is invalid after the call.
96 gmx_ana_selcollection_free(gmx_ana_selcollection_t
*sc
)
100 _gmx_selelem_free_chain(sc
->root
);
103 for (i
= 0; i
< sc
->nr
; ++i
)
105 gmx_ana_selection_free(sc
->sel
[i
]);
109 for (i
= 0; i
< sc
->nvars
; ++i
)
111 sfree(sc
->varstrs
[i
]);
114 gmx_ana_index_deinit(&sc
->gall
);
117 _gmx_sel_mempool_destroy(sc
->mempool
);
119 _gmx_selcollection_clear_symtab(sc
);
124 * \param[in,out] sc Selection collection.
127 _gmx_selcollection_clear_symtab(gmx_ana_selcollection_t
*sc
)
131 _gmx_sel_symtab_free(sc
->symtab
);
137 * \param[in,out] sc Selection collection to modify.
138 * \param[in] type Default selection reference position type
139 * (one of the strings acceptable for gmx_ana_poscalc_type_from_enum()).
141 * Should be called before calling gmx_ana_selcollection_requires_top() or
142 * gmx_ana_selcollection_parse_*().
145 gmx_ana_selcollection_set_refpostype(gmx_ana_selcollection_t
*sc
,
152 * \param[in,out] sc Selection collection to modify.
153 * \param[in] type Default selection output position type
154 * (one of the strings acceptable for gmx_ana_poslcalc_type_from_enum()).
155 * \param[in] bMaskOnly If TRUE, the output positions are initialized
156 * using \ref POS_MASKONLY.
158 * If \p type is NULL, the default type is not modified.
159 * Should be called before calling gmx_ana_selcollection_requires_top() or
160 * gmx_ana_selcollection_parse_*().
163 gmx_ana_selcollection_set_outpostype(gmx_ana_selcollection_t
*sc
,
164 const char *type
, bool bMaskOnly
)
170 sc
->bMaskOnly
= bMaskOnly
;
174 * \param[in,out] sc Selection collection to modify.
175 * \param[in] bVelOut If TRUE, selections will also evaluate
179 gmx_ana_selcollection_set_veloutput(gmx_ana_selcollection_t
*sc
,
182 sc
->bVelocities
= bVelOut
;
186 * \param[in,out] sc Selection collection to modify.
187 * \param[in] bForceOut If TRUE, selections will also evaluate
191 gmx_ana_selcollection_set_forceoutput(gmx_ana_selcollection_t
*sc
,
194 sc
->bForces
= bForceOut
;
198 * \param[in,out] sc Selection collection to set the topology for.
199 * \param[in] top Topology data.
200 * \param[in] natoms Number of atoms. If <=0, the number of atoms in the
202 * \returns 0 on success, EINVAL if \p top is NULL and \p natoms <= 0.
204 * The topology is also set for the position calculation collection
205 * associated with \p sc.
207 * \p natoms determines the largest atom index that can be selected by the
208 * selection: even if the topology contains more atoms, they will not be
212 gmx_ana_selcollection_set_topology(gmx_ana_selcollection_t
*sc
, t_topology
*top
,
215 gmx_ana_poscalc_coll_set_topology(sc
->pcc
, top
);
218 /* Get the number of atoms from the topology if it is not given */
223 gmx_incons("selections need either the topology or the number of atoms");
226 natoms
= sc
->top
->atoms
.nr
;
228 gmx_ana_index_init_simple(&sc
->gall
, natoms
, NULL
);
233 * \param[in] sc Selection collection to query.
234 * \returns Number of selections in \p sc.
236 * If gmx_ana_selcollection_parse_*() has not been called, returns 0.
238 * \see gmx_ana_selcollection_get_selection()
241 gmx_ana_selcollection_get_count(gmx_ana_selcollection_t
*sc
)
247 * \param[in] sc Selection collection to query.
248 * \param[in] i Number of the selection.
249 * \returns Pointer to the \p i'th selection in \p sc,
250 * or NULL if there is no such selection.
252 * \p i should be between 0 and the value returned by
253 * gmx_ana_selcollection_get_count().
254 * The returned pointer should not be freed.
255 * If gmx_ana_selcollection_compile() has not been called, the returned
256 * selection is not completely initialized (but the returned pointer will be
257 * valid even after compilation, and will point to the initialized selection).
259 * \see gmx_ana_selcollection_get_count()
261 gmx_ana_selection_t
*
262 gmx_ana_selcollection_get_selection(gmx_ana_selcollection_t
*sc
, int i
)
264 if (i
< 0 || i
>= sc
->nr
|| !sc
->sel
)
270 * \param[in] sc Selection collection to query.
271 * \returns TRUE if any selection in \p sc requires topology information,
274 * Before gmx_ana_selcollection_parse_*(), the return value is based just on
275 * the position types set.
276 * After gmx_ana_selcollection_parse_*(), the return value also takes into account the
277 * selection keywords used.
280 gmx_ana_selcollection_requires_top(gmx_ana_selcollection_t
*sc
)
290 rc
= gmx_ana_poscalc_type_from_enum(sc
->rpost
, &type
, &flags
);
291 if (rc
== 0 && type
!= POS_ATOM
)
299 rc
= gmx_ana_poscalc_type_from_enum(sc
->spost
, &type
, &flags
);
300 if (rc
== 0 && type
!= POS_ATOM
)
309 if (_gmx_selelem_requires_top(sel
))
319 * \param[in] fp File handle to receive the output.
320 * \param[in] sc Selection collection to print.
321 * \param[in] bValues If TRUE, the evaluated values of selection elements
322 * are printed as well.
325 gmx_ana_selcollection_print_tree(FILE *fp
, gmx_ana_selcollection_t
*sc
, bool bValues
)
332 _gmx_selelem_print_tree(fp
, sel
, bValues
, 0);
338 * \param[in] sel Selection to free.
340 * After the call, the pointer \p sel is invalid.
343 gmx_ana_selection_free(gmx_ana_selection_t
*sel
)
347 gmx_ana_pos_deinit(&sel
->p
);
348 if (sel
->m
!= sel
->orgm
)
352 if (sel
->q
!= sel
->orgq
)
362 * \param[in] sel Selection whose name is needed.
363 * \returns Pointer to the name of the selection.
365 * The return value should not be freed by the caller.
368 gmx_ana_selection_name(gmx_ana_selection_t
*sel
)
374 * \param[in] sel Selection for which information should be printed.
377 gmx_ana_selection_print_info(gmx_ana_selection_t
*sel
)
379 fprintf(stderr
, "\"%s\" (%d position%s, %d atom%s%s)", sel
->name
,
380 sel
->p
.nr
, sel
->p
.nr
== 1 ? "" : "s",
381 sel
->g
->isize
, sel
->g
->isize
== 1 ? "" : "s",
382 sel
->bDynamic
? ", dynamic" : "");
383 fprintf(stderr
, "\n");
387 * \param[in] sel Selection to initialize.
388 * \param[in] type Type of covered fraction required.
389 * \returns TRUE if the covered fraction can be calculated for the selection,
393 gmx_ana_selection_init_coverfrac(gmx_ana_selection_t
*sel
, e_coverfrac_t type
)
395 sel
->cfractype
= type
;
396 if (type
== CFRAC_NONE
|| !sel
->selelem
)
398 sel
->bCFracDyn
= FALSE
;
400 else if (!_gmx_selelem_can_estimate_cover(sel
->selelem
))
402 sel
->cfractype
= CFRAC_NONE
;
403 sel
->bCFracDyn
= FALSE
;
407 sel
->bCFracDyn
= TRUE
;
409 sel
->cfrac
= sel
->bCFracDyn
? 0.0 : 1.0;
410 sel
->avecfrac
= sel
->cfrac
;
411 return type
== CFRAC_NONE
|| sel
->cfractype
!= CFRAC_NONE
;
415 * \param[in] out Output file.
416 * \param[in] sc Selection collection which should be written.
417 * \param[in] oenv Output options structure.
419 void xvgr_selcollection(FILE *out
, gmx_ana_selcollection_t
*sc
,
420 const output_env_t oenv
)
424 if (output_env_get_xvg_format(oenv
) != exvgNONE
&& sc
)
426 fprintf(out
, "# Selections:\n");
427 for (i
= 0; i
< sc
->nvars
; ++i
)
429 fprintf(out
, "# %s\n", sc
->varstrs
[i
]);
431 for (i
= 0; i
< sc
->nr
; ++i
)
433 fprintf(out
, "# %s\n", sc
->sel
[i
]->selstr
);