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 * Declares gmx::SelectionTreeElement and related things.
35 * The selection element trees constructed by the parser and the compiler
36 * are described on the respective pages:
37 * \ref page_module_selection_parser and \ref page_module_selection_compiler.
39 * This is an implementation header: there should be no need to use it outside
42 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
43 * \ingroup module_selection
45 #ifndef GMX_SELECTION_SELELEM_H
46 #define GMX_SELECTION_SELELEM_H
50 #include <boost/shared_ptr.hpp>
52 #include "gromacs/legacyheaders/types/simple.h"
53 #include "gromacs/utility/common.h"
55 #include "indexutil.h"
58 struct gmx_ana_poscalc_t;
59 struct gmx_ana_selparam_t;
60 struct gmx_ana_selmethod_t;
62 struct gmx_sel_evaluate_t;
63 struct gmx_sel_mempool_t;
65 struct t_compiler_data;
69 class SelectionTreeElement;
71 //! Smart pointer type for selection tree element pointers.
72 typedef boost::shared_ptr<SelectionTreeElement> SelectionTreeElementPointer;
75 /********************************************************************/
76 /*! \name Enumerations for expression types
77 ********************************************************************/
80 /** Defines the type of a gmx::SelectionTreeElement object. */
83 /** Constant-valued expression. */
85 /** Method expression that requires evaluation. */
87 /** Boolean expression. */
89 /** Arithmetic expression. */
91 /** Root node of the evaluation tree. */
93 /** Subexpression that may be referenced several times. */
95 /** Reference to a subexpression. */
97 /** Unresolved reference to an external group. */
99 /** Post-processing of selection value. */
103 /** Defines the boolean operation of gmx::SelectionTreeElement objects with type \ref SEL_BOOLEAN. */
106 BOOL_NOT, /**< Not */
107 BOOL_AND, /**< And */
109 BOOL_XOR /**< Xor (not implemented). */
112 /** Defines the arithmetic operation of gmx::SelectionTreeElement objects with type \ref SEL_ARITHMETIC. */
115 ARITH_PLUS, /**< + */
116 ARITH_MINUS, /**< - */
117 ARITH_NEG, /**< Unary - */
118 ARITH_MULT, /**< * */
120 ARITH_EXP /**< ^ (to power) */
123 /** Returns a string representation of the type of a gmx::SelectionTreeElement. */
125 _gmx_selelem_type_str(const gmx::SelectionTreeElement &sel);
126 /** Returns a string representation of the boolean type of a \ref SEL_BOOLEAN gmx::SelectionTreeElement. */
128 _gmx_selelem_boolean_type_str(const gmx::SelectionTreeElement &sel);
129 /** Returns a string representation of the type of a \c gmx_ana_selvalue_t. */
131 _gmx_sel_value_type_str(const gmx_ana_selvalue_t *val);
136 /********************************************************************/
137 /*! \name Selection expression flags
138 * \anchor selelem_flags
139 ********************************************************************/
142 * Selection value flags are set.
144 * If this flag is set, the flags covered by \ref SEL_VALFLAGMASK
145 * have been set properly for the element.
147 #define SEL_FLAGSSET 1
149 * The element evaluates to a single value.
151 * This flag is always set for \ref GROUP_VALUE elements.
153 #define SEL_SINGLEVAL 2
155 * The element evaluates to one value for each input atom.
157 #define SEL_ATOMVAL 4
159 * The element evaluates to an arbitrary number of values.
161 #define SEL_VARNUMVAL 8
163 * The element (or one of its children) is dynamic.
165 #define SEL_DYNAMIC 16
167 * Mask that covers the flags that describe the number of values.
169 #define SEL_VALTYPEMASK (SEL_SINGLEVAL | SEL_ATOMVAL | SEL_VARNUMVAL)
171 * Mask that covers the flags that describe the value type.
173 #define SEL_VALFLAGMASK (SEL_FLAGSSET | SEL_VALTYPEMASK | SEL_DYNAMIC)
175 * Data has been allocated for the \p v.u union.
177 * If not set, the \p v.u.ptr points to data allocated externally.
178 * This is the case if the value of the element is used as a parameter
179 * for a selection method or if the element evaluates the final value of
182 * Even if the flag is set, \p v.u.ptr can be NULL during initialization.
185 * This flag overlaps with the function of \p v.nalloc field, and could
186 * probably be removed, making memory management simpler. Currently, the
187 * \p v.nalloc field is not kept up-to-date in all cases when this flag
188 * is changed and is used in places where this flag is not, so this would
189 * require a careful investigation of the selection code.
191 #define SEL_ALLOCVAL (1<<8)
193 * Data has been allocated for the group/position structure.
195 * If not set, the memory allocated for fields in \p v.u.g or \p v.u.p is
196 * managed externally.
198 * This field has no effect if the value type is not \ref GROUP_VALUE or
199 * \ref POS_VALUE, but should not be set.
201 #define SEL_ALLOCDATA (1<<9)
203 * \p method->init_frame should be called for the frame.
205 #define SEL_INITFRAME (1<<10)
207 * Parameter has been evaluated for the current frame.
209 * This flag is set for children of \ref SEL_EXPRESSION elements (which
210 * describe method parameters) after the element has been evaluated for the
212 * It is not set for \ref SEL_ATOMVAL elements, because they may need to
213 * be evaluated multiple times.
215 #define SEL_EVALFRAME (1<<11)
217 * \p method->init has been called.
219 #define SEL_METHODINIT (1<<12)
221 * \p method->outinit has been called.
223 * This flag is also used for \ref SEL_SUBEXPRREF elements.
225 #define SEL_OUTINIT (1<<13)
233 * Function pointer for evaluating a gmx::SelectionTreeElement.
235 typedef void (*sel_evalfunc)(struct gmx_sel_evaluate_t *data,
236 const SelectionTreeElementPointer &sel,
240 * Represents an element of a selection expression.
242 class SelectionTreeElement
246 * Allocates memory and performs common initialization.
248 * \param[in] type Type of selection element to create.
250 * \a type is set to \p type,
251 * \a v::type is set to \ref GROUP_VALUE for boolean and comparison
252 * expressions and \ref NO_VALUE for others, and
253 * \ref SEL_ALLOCVAL is set for non-root elements (\ref SEL_ALLOCDATA
254 * is also set for \ref SEL_BOOLEAN elements).
255 * All the pointers are set to NULL.
257 explicit SelectionTreeElement(e_selelem_t type);
258 ~SelectionTreeElement();
260 //! Frees the memory allocated for the \a v union.
262 //! Frees the memory allocated for the \a u union.
263 void freeExpressionData();
264 /* In compiler.cpp */
266 * Frees the memory allocated for the selection compiler.
268 * This function only frees the data for the given selection, not its
269 * children. It is safe to call the function when compiler data has
270 * not been allocated or has already been freed; in such a case,
273 void freeCompilerData();
276 * Reserves memory for value from a memory pool.
278 * \param[in] count Number of values to reserve memory for.
280 * Reserves memory for the values of this element from the \a mempool
282 * If no memory pool is set, nothing is done.
284 void mempoolReserve(int count);
286 * Releases memory pool used for value.
288 * Releases the memory allocated for the values of this element from the
289 * \a mempool memory pool.
290 * If no memory pool is set, nothing is done.
292 void mempoolRelease();
294 //! Returns the name of the element.
295 const std::string &name() const { return name_; }
297 * Sets the name of the element.
299 * \param[in] name Name to set (can be NULL).
300 * \throws std::bad_alloc if out of memory.
302 void setName(const char *name) { name_ = (name != NULL ? name : ""); }
303 //! \copydoc setName(const char *)
304 void setName(const std::string &name) { name_ = name; }
306 //! Type of the element.
309 * Value storage of the element.
311 * This field contains the evaluated value of the element, as well as
312 * the output value type.
314 gmx_ana_selvalue_t v;
316 * Evaluation function for the element.
318 * Can be either NULL (if the expression is a constant and does not
319 * require evaluation) or point to one of the functions defined in
322 sel_evalfunc evaluate;
324 * Information flags about the element.
326 * Allowed flags are listed here:
327 * \ref selelem_flags "flags for gmx::SelectionTreeElement".
330 //! Data required by the evaluation function.
332 /*! \brief Index group data for several element types.
334 * - \ref SEL_CONST : if the value type is \ref GROUP_VALUE,
335 * this field holds the unprocessed group value.
336 * - \ref SEL_ROOT : holds the group value for which the
337 * selection subtree should be evaluated.
338 * - \ref SEL_SUBEXPR : holds the group for which the subexpression
339 * has been evaluated.
341 gmx_ana_index_t cgrp;
342 //! Data for \ref SEL_EXPRESSION and \ref SEL_MODIFIER elements.
344 //! Pointer the the method used in this expression.
345 struct gmx_ana_selmethod_t *method;
346 //! Pointer to the data allocated by the method's \p init_data (see sel_datafunc()).
348 //! Pointer to the position data passed to the method.
349 struct gmx_ana_pos_t *pos;
350 //! Pointer to the evaluation data for \p pos.
351 struct gmx_ana_poscalc_t *pc;
353 //! Operation type for \ref SEL_BOOLEAN elements.
355 //! Operation type for \ref SEL_ARITHMETIC elements.
359 //! String representation.
362 //! Associated selection parameter for \ref SEL_SUBEXPRREF elements.
363 struct gmx_ana_selparam_t *param;
364 //! The string/number used to reference the group.
366 //! Name of the referenced external group.
368 //! If \a name is NULL, the index number of the referenced group.
372 //! Memory pool to use for values, or NULL if standard memory handling.
373 struct gmx_sel_mempool_t *mempool;
374 //! Internal data for the selection compiler.
375 t_compiler_data *cdata;
377 /*! \brief The first child element.
379 * Other children can be accessed through the \p next field of \p child.
381 SelectionTreeElementPointer child;
382 //! The next sibling element.
383 SelectionTreeElementPointer next;
387 * Name of the element.
389 * This field is only used for informative purposes.
393 GMX_DISALLOW_COPY_AND_ASSIGN(SelectionTreeElement);
398 /********************************************************************/
399 /*! \name Selection expression functions
404 /** Writes out a human-readable name for an evaluation function. */
406 _gmx_sel_print_evalfunc_name(FILE *fp, gmx::sel_evalfunc evalfunc);
408 /** Sets the value type of a gmx::SelectionTreeElement. */
410 _gmx_selelem_set_vtype(const gmx::SelectionTreeElementPointer &sel,
413 /** Frees the memory allocated for a selection method. */
415 _gmx_selelem_free_method(struct gmx_ana_selmethod_t *method, void *mdata);
417 /** Prints a human-readable version of a selection element subtree. */
419 _gmx_selelem_print_tree(FILE *fp, const gmx::SelectionTreeElement &sel,
420 bool bValues, int level);
422 /** Prints a human-readable version of the internal compiler data structure. */
424 _gmx_selelem_print_compiler_info(FILE *fp, const gmx::SelectionTreeElement &sel,
427 /** Returns true if the selection element subtree requires topology information for evaluation. */
429 _gmx_selelem_requires_top(const gmx::SelectionTreeElement &root);
431 /* In sm_insolidangle.c */
432 /** Returns true if the covered fraction of the selection can be calculated. */
434 _gmx_selelem_can_estimate_cover(const gmx::SelectionTreeElement &sel);
435 /** Returns the covered fraction of the selection for the current frame. */
437 _gmx_selelem_estimate_coverfrac(const gmx::SelectionTreeElement &sel);