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
;
77 gmx_ana_index_clear(&sc
->gall
);
79 _gmx_sel_symtab_create(&sc
->symtab
);
85 * \param[in,out] sc Selection collection to free.
87 * The pointer \p sc is invalid after the call.
90 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 for (i
= 0; i
< sc
->nvars
; ++i
)
105 sfree(sc
->varstrs
[i
]);
108 gmx_ana_index_deinit(&sc
->gall
);
109 _gmx_selcollection_clear_symtab(sc
);
114 * \param[in,out] sc Selection collection.
117 _gmx_selcollection_clear_symtab(gmx_ana_selcollection_t
*sc
)
121 _gmx_sel_symtab_free(sc
->symtab
);
127 * \param[in,out] sc Selection collection to modify.
128 * \param[in] type Default selection reference position type
129 * (one of the strings acceptable for gmx_ana_poscalc_type_from_enum()).
131 * Should be called before calling gmx_ana_selcollection_requires_top() or
132 * gmx_ana_selcollection_parse_*().
135 gmx_ana_selcollection_set_refpostype(gmx_ana_selcollection_t
*sc
,
142 * \param[in,out] sc Selection collection to modify.
143 * \param[in] type Default selection output position type
144 * (one of the strings acceptable for gmx_ana_poslcalc_type_from_enum()).
145 * \param[in] bMaskOnly If TRUE, the output positions are initialized
146 * using \ref POS_MASKONLY.
148 * If \p type is NULL, the default type is not modified.
149 * Should be called before calling gmx_ana_selcollection_requires_top() or
150 * gmx_ana_selcollection_parse_*().
153 gmx_ana_selcollection_set_outpostype(gmx_ana_selcollection_t
*sc
,
154 const char *type
, bool bMaskOnly
)
160 sc
->bMaskOnly
= bMaskOnly
;
164 * \param[in,out] sc Selection collection to set the topology for.
165 * \param[in] top Topology data.
166 * \param[in] natoms Number of atoms. If <=0, the number of atoms in the
168 * \returns 0 on success, EINVAL if \p top is NULL and \p natoms <= 0.
170 * The topology is also set for the position calculation collection
171 * associated with \p sc.
173 * \p natoms determines the largest atom index that can be selected by the
174 * selection: even if the topology contains more atoms, they will not be
178 gmx_ana_selcollection_set_topology(gmx_ana_selcollection_t
*sc
, t_topology
*top
,
181 gmx_ana_poscalc_coll_set_topology(sc
->pcc
, top
);
184 /* Get the number of atoms from the topology if it is not given */
189 gmx_incons("selections need either the topology or the number of atoms");
192 natoms
= sc
->top
->atoms
.nr
;
194 gmx_ana_index_init_simple(&sc
->gall
, natoms
, NULL
);
199 * \param[in] sc Selection collection to query.
200 * \returns Number of selections in \p sc.
202 * If gmx_ana_selcollection_parse_*() has not been called, returns 0.
204 * \see gmx_ana_selcollection_get_selection()
207 gmx_ana_selcollection_get_count(gmx_ana_selcollection_t
*sc
)
213 * \param[in] sc Selection collection to query.
214 * \param[in] i Number of the selection.
215 * \returns Pointer to the \p i'th selection in \p sc,
216 * or NULL if there is no such selection.
218 * \p i should be between 0 and the value returned by
219 * gmx_ana_selcollection_get_count().
220 * The returned pointer should not be freed.
221 * If gmx_ana_selcollection_compile() has not been called, the returned
222 * selection is not completely initialized (but the returned pointer will be
223 * valid even after compilation, and will point to the initialized selection).
225 * \see gmx_ana_selcollection_get_count()
227 gmx_ana_selection_t
*
228 gmx_ana_selcollection_get_selection(gmx_ana_selcollection_t
*sc
, int i
)
230 if (i
< 0 || i
>= sc
->nr
|| !sc
->sel
)
236 * \param[in] sc Selection collection to query.
237 * \returns TRUE if any selection in \p sc requires topology information,
240 * Before gmx_ana_selcollection_parse_*(), the return value is based just on
241 * the position types set.
242 * After gmx_ana_selcollection_parse_*(), the return value also takes into account the
243 * selection keywords used.
246 gmx_ana_selcollection_requires_top(gmx_ana_selcollection_t
*sc
)
256 rc
= gmx_ana_poscalc_type_from_enum(sc
->rpost
, &type
, &flags
);
257 if (rc
== 0 && type
!= POS_ATOM
)
265 rc
= gmx_ana_poscalc_type_from_enum(sc
->spost
, &type
, &flags
);
266 if (rc
== 0 && type
!= POS_ATOM
)
275 if (_gmx_selelem_requires_top(sel
))
285 * \param[in] fp File handle to receive the output.
286 * \param[in] sc Selection collection to print.
287 * \param[in] bValues If TRUE, the evaluated values of selection elements
288 * are printed as well.
291 gmx_ana_selcollection_print_tree(FILE *fp
, gmx_ana_selcollection_t
*sc
, bool bValues
)
298 _gmx_selelem_print_tree(fp
, sel
, bValues
, 0);
304 * \param[in] sel Selection to free.
306 * After the call, the pointer \p sel is invalid.
309 gmx_ana_selection_free(gmx_ana_selection_t
*sel
)
313 gmx_ana_pos_deinit(&sel
->p
);
314 if (sel
->m
!= sel
->orgm
)
318 if (sel
->q
!= sel
->orgq
)
328 * \param[in] sel Selection whose name is needed.
329 * \returns Pointer to the name of the selection.
331 * The return value should not be freed by the caller.
334 gmx_ana_selection_name(gmx_ana_selection_t
*sel
)
340 * \param[in] sel Selection for which information should be printed.
343 gmx_ana_selection_print_info(gmx_ana_selection_t
*sel
)
345 fprintf(stderr
, "\"%s\" (%d position%s, %d atom%s%s)", sel
->name
,
346 sel
->p
.nr
, sel
->p
.nr
== 1 ? "" : "s",
347 sel
->g
->isize
, sel
->g
->isize
== 1 ? "" : "s",
348 sel
->bDynamic
? ", dynamic" : "");
349 fprintf(stderr
, "\n");
353 * \param[in] sel Selection to initialize.
354 * \param[in] type Type of covered fraction required.
355 * \returns TRUE if the covered fraction can be calculated for the selection,
359 gmx_ana_selection_init_coverfrac(gmx_ana_selection_t
*sel
, e_coverfrac_t type
)
361 sel
->cfractype
= type
;
362 if (type
== CFRAC_NONE
|| !sel
->selelem
)
364 sel
->bCFracDyn
= FALSE
;
366 else if (!_gmx_selelem_can_estimate_cover(sel
->selelem
))
368 sel
->cfractype
= CFRAC_NONE
;
369 sel
->bCFracDyn
= FALSE
;
373 sel
->bCFracDyn
= TRUE
;
375 sel
->cfrac
= sel
->bCFracDyn
? 0.0 : 1.0;
376 sel
->avecfrac
= sel
->cfrac
;
377 return type
== CFRAC_NONE
|| sel
->cfractype
!= CFRAC_NONE
;
381 * \param[in] out Output file.
382 * \param[in] sc Selection collection which should be written.
383 * \param[in] oenv Output options structure.
385 void xvgr_selcollection(FILE *out
, gmx_ana_selcollection_t
*sc
,
386 const output_env_t oenv
)
390 if (get_print_xvgr_codes(oenv
) && sc
)
392 fprintf(out
, "# Selections:\n");
393 for (i
= 0; i
< sc
->nvars
; ++i
)
395 fprintf(out
, "# %s\n", sc
->varstrs
[i
]);
397 for (i
= 0; i
< sc
->nr
; ++i
)
399 fprintf(out
, "# %s\n", sc
->sel
[i
]->selstr
);