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.
49 #include <selection.h>
50 #include <selmethod.h>
52 #include "selcollection.h"
57 * \param[out] scp Pointer to a newly allocated empty selection collection.
58 * \param[in] pcc Position calculation data structure to use for selection
59 * position evaluation.
60 * \returns 0 on success.
63 gmx_ana_selcollection_create(gmx_ana_selcollection_t
**scp
,
64 gmx_ana_poscalc_coll_t
*pcc
)
66 gmx_ana_selcollection_t
*sc
;
76 gmx_ana_index_clear(&sc
->gall
);
78 _gmx_sel_symtab_create(&sc
->symtab
);
84 * \param[in,out] sc Selection collection to free.
86 * The pointer \p sc is invalid after the call.
89 gmx_ana_selcollection_free(gmx_ana_selcollection_t
*sc
)
94 _gmx_selelem_free_chain(sc
->root
);
97 for (i
= 0; i
< sc
->nr
; ++i
)
99 gmx_ana_selection_free(sc
->sel
[i
]);
103 gmx_ana_index_deinit(&sc
->gall
);
104 _gmx_selcollection_clear_symtab(sc
);
109 * \param[in,out] sc Selection collection.
112 _gmx_selcollection_clear_symtab(gmx_ana_selcollection_t
*sc
)
116 _gmx_sel_symtab_free(sc
->symtab
);
122 * \param[in,out] sc Selection collection to modify.
123 * \param[in] type Default selection reference position type
124 * (one of the strings acceptable for gmx_ana_poscalc_type_from_enum()).
126 * Should be called before calling gmx_ana_selcollection_requires_top() or
127 * gmx_ana_selcollection_parse_*().
130 gmx_ana_selcollection_set_refpostype(gmx_ana_selcollection_t
*sc
,
137 * \param[in,out] sc Selection collection to modify.
138 * \param[in] type Default selection output position type
139 * (one of the strings acceptable for gmx_ana_poslcalc_type_from_enum()).
140 * \param[in] bMaskOnly If TRUE, the output positions are initialized
141 * using \ref POS_MASKONLY.
143 * If \p type is NULL, the default type is not modified.
144 * Should be called before calling gmx_ana_selcollection_requires_top() or
145 * gmx_ana_selcollection_parse_*().
148 gmx_ana_selcollection_set_outpostype(gmx_ana_selcollection_t
*sc
,
149 const char *type
, bool bMaskOnly
)
155 sc
->bMaskOnly
= bMaskOnly
;
159 * \param[in,out] sc Selection collection to set the topology for.
160 * \param[in] top Topology data.
161 * \param[in] natoms Number of atoms. If <=0, the number of atoms in the
163 * \returns 0 on success, EINVAL if \p top is NULL and \p natoms <= 0.
165 * The topology is also set for the position calculation collection
166 * associated with \p sc.
168 * \p natoms determines the largest atom index that can be selected by the
169 * selection: even if the topology contains more atoms, they will not be
173 gmx_ana_selcollection_set_topology(gmx_ana_selcollection_t
*sc
, t_topology
*top
,
176 gmx_ana_poscalc_coll_set_topology(sc
->pcc
, top
);
179 /* Get the number of atoms from the topology if it is not given */
184 gmx_incons("selections need either the topology or the number of atoms");
187 natoms
= sc
->top
->atoms
.nr
;
189 gmx_ana_index_init_simple(&sc
->gall
, natoms
, NULL
);
194 * \param[in] sc Selection collection to query.
195 * \returns Number of selections in \p sc.
197 * If gmx_ana_selcollection_parse_*() has not been called, returns 0.
199 * \see gmx_ana_selcollection_get_selection()
202 gmx_ana_selcollection_get_count(gmx_ana_selcollection_t
*sc
)
208 * \param[in] sc Selection collection to query.
209 * \param[in] i Number of the selection.
210 * \returns Pointer to the \p i'th selection in \p sc,
211 * or NULL if there is no such selection.
213 * \p i should be between 0 and the value returned by
214 * gmx_ana_selcollection_get_count().
215 * The returned pointer should not be freed.
216 * If gmx_ana_selcollection_compile() has not been called, the returned
217 * selection is not completely initialized (but the returned pointer will be
218 * valid even after compilation, and will point to the initialized selection).
220 * \see gmx_ana_selcollection_get_count()
222 gmx_ana_selection_t
*
223 gmx_ana_selcollection_get_selection(gmx_ana_selcollection_t
*sc
, int i
)
225 if (i
< 0 || i
>= sc
->nr
|| !sc
->sel
)
231 * \param[in] sc Selection collection to query.
232 * \returns TRUE if any selection in \p sc requires topology information,
235 * Before gmx_ana_selcollection_parse_*(), the return value is based just on
236 * the position types set.
237 * After gmx_ana_selcollection_parse_*(), the return value also takes into account the
238 * selection keywords used.
241 gmx_ana_selcollection_requires_top(gmx_ana_selcollection_t
*sc
)
251 rc
= gmx_ana_poscalc_type_from_enum(sc
->rpost
, &type
, &flags
);
252 if (rc
== 0 && type
!= POS_ATOM
)
260 rc
= gmx_ana_poscalc_type_from_enum(sc
->spost
, &type
, &flags
);
261 if (rc
== 0 && type
!= POS_ATOM
)
270 if (_gmx_selelem_requires_top(sel
))
280 * \param[in] fp File handle to receive the output.
281 * \param[in] sc Selection collection to print.
282 * \param[in] bValues If TRUE, the evaluated values of selection elements
283 * are printed as well.
286 gmx_ana_selcollection_print_tree(FILE *fp
, gmx_ana_selcollection_t
*sc
, bool bValues
)
293 _gmx_selelem_print_tree(fp
, sel
, bValues
, 0);
299 * \param[in] sel Selection to free.
301 * After the call, the pointer \p sel is invalid.
304 gmx_ana_selection_free(gmx_ana_selection_t
*sel
)
308 gmx_ana_pos_deinit(&sel
->p
);
309 if (sel
->m
!= sel
->orgm
)
313 if (sel
->q
!= sel
->orgq
)
323 * \param[in] sel Selection whose name is needed.
324 * \returns Pointer to the name of the selection.
326 * The return value should not be freed by the caller.
329 gmx_ana_selection_name(gmx_ana_selection_t
*sel
)
335 * \param[in] sel Selection for which information should be printed.
338 gmx_ana_selection_print_info(gmx_ana_selection_t
*sel
)
340 fprintf(stderr
, "\"%s\" (%d position%s, %d atom%s%s)", sel
->name
,
341 sel
->p
.nr
, sel
->p
.nr
== 1 ? "" : "s",
342 sel
->g
->isize
, sel
->g
->isize
== 1 ? "" : "s",
343 sel
->bDynamic
? ", dynamic" : "");
344 fprintf(stderr
, "\n");
348 * \param[in] sel Selection to initialize.
349 * \param[in] type Type of covered fraction required.
350 * \returns TRUE if the covered fraction can be calculated for the selection,
354 gmx_ana_selection_init_coverfrac(gmx_ana_selection_t
*sel
, e_coverfrac_t type
)
356 sel
->cfractype
= type
;
357 if (type
== CFRAC_NONE
|| !sel
->selelem
)
359 sel
->bCFracDyn
= FALSE
;
361 else if (!_gmx_selelem_can_estimate_cover(sel
->selelem
))
363 sel
->cfractype
= CFRAC_NONE
;
364 sel
->bCFracDyn
= FALSE
;
368 sel
->bCFracDyn
= TRUE
;
370 sel
->cfrac
= sel
->bCFracDyn
? 0.0 : 1.0;
371 sel
->avecfrac
= sel
->cfrac
;
372 return type
== CFRAC_NONE
|| sel
->cfractype
!= CFRAC_NONE
;
376 * \param[in] out Output file.
377 * \param[in] sc Selection collection which should be written.
378 * \param[in] oenv Output options structure.
380 void xvgr_selcollection(FILE *out
, gmx_ana_selcollection_t
*sc
,
381 const output_env_t oenv
)
386 if (get_print_xvgr_codes(oenv
) && sc
&& sc
->selstr
)
388 fprintf(out
, "# Selections:\n");
389 buf
= strdup(sc
->selstr
);
391 while (p
&& p
[0] != 0)
393 nl
= strchr(p
, '\n');
398 fprintf(out
, "# %s\n", p
);