571d8a127fbe50b4dd12b0aad1afcf212d53f8c0
[alexxy/gromacs.git] / src / gmxlib / selection / selelem.h
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
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.
13
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.
18  *
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.
25  *
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.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief Definition of \c t_selelem and related things.
33  *
34  * The selection element trees constructed by the parser and the compiler
35  * are described on the respective pages:
36  * \ref selparser and \ref selcompiler.
37  *
38  * This is an implementation header: there should be no need to use it outside
39  * this directory.
40  */
41 #ifndef SELECTION_ELEMENT_H
42 #define SELECTION_ELEMENT_H
43
44 #include <types/simple.h>
45
46 #include <indexutil.h>
47 #include <selvalue.h>
48
49 struct gmx_ana_poscalc_t;
50 struct gmx_ana_selparam_t;
51 struct gmx_ana_selmethod_t;
52
53 struct gmx_sel_evaluate_t;
54 struct gmx_sel_mempool_t;
55 struct t_selelem;
56
57 /********************************************************************/
58 /*! \name Enumerations for expression types
59  ********************************************************************/
60 /*@{*/
61
62 /** Defines the type of a \c t_selelem object. */
63 typedef enum
64 {
65     /** Constant-valued expression. */
66     SEL_CONST,
67     /** Method expression that requires evaluation. */
68     SEL_EXPRESSION,
69     /** Boolean expression. */
70     SEL_BOOLEAN,
71     /** Arithmetic expression. */
72     SEL_ARITHMETIC,
73     /** Root node of the evaluation tree. */
74     SEL_ROOT,
75     /** Subexpression that may be referenced several times. */
76     SEL_SUBEXPR,
77     /** Reference to a subexpression. */
78     SEL_SUBEXPRREF,
79     /** Post-processing of selection value. */
80     SEL_MODIFIER
81 } e_selelem_t;
82
83 /** Defines the gmx_boolean operation of \c t_selelem objects with type \ref SEL_BOOLEAN. */
84 typedef enum
85 {
86     BOOL_NOT,           /**< Not */
87     BOOL_AND,           /**< And */
88     BOOL_OR,            /**< Or */
89     BOOL_XOR            /**< Xor (not implemented). */
90 } e_boolean_t;
91
92 /** Defines the arithmetic operation of \c t_selelem objects with type \ref SEL_ARITHMETIC. */
93 typedef enum
94 {
95     ARITH_PLUS,         /**< + */
96     ARITH_MINUS,        /**< - */
97     ARITH_NEG,          /**< Unary - */
98     ARITH_MULT,         /**< * */
99     ARITH_DIV,          /**< / */
100     ARITH_EXP           /**< ^ (to power) */
101 } e_arithmetic_t;
102
103 /** Returns a string representation of the type of a \c t_selelem. */
104 extern const char *
105 _gmx_selelem_type_str(struct t_selelem *sel);
106 /** Returns a string representation of the gmx_boolean type of a \ref SEL_BOOLEAN \c t_selelem. */
107 extern const char *
108 _gmx_selelem_gmx_boolean_type_str(struct t_selelem *sel);
109 /** Returns a string representation of the type of a \c gmx_ana_selvalue_t. */
110 extern const char *
111 _gmx_sel_value_type_str(gmx_ana_selvalue_t *val);
112
113 /*@}*/
114
115
116 /********************************************************************/
117 /*! \name Selection expression flags
118  * \anchor selelem_flags
119  ********************************************************************/
120 /*@{*/
121 /*! \brief
122  * Selection value flags are set.
123  *
124  * If this flag is set, the flags covered by \ref SEL_VALFLAGMASK
125  * have been set properly for the element.
126  */
127 #define SEL_FLAGSSET    1
128 /*! \brief
129  * The element evaluates to a single value.
130  *
131  * This flag is always set for \ref GROUP_VALUE elements.
132  */
133 #define SEL_SINGLEVAL   2
134 /*! \brief
135  * The element evaluates to one value for each input atom.
136  */
137 #define SEL_ATOMVAL     4
138 /*! \brief
139  * The element evaluates to an arbitrary number of values.
140  */
141 #define SEL_VARNUMVAL   8
142 /*! \brief
143  * The element (or one of its children) is dynamic.
144  */
145 #define SEL_DYNAMIC     16
146 /*! \brief
147  * Mask that covers the flags that describe the number of values.
148  */
149 #define SEL_VALTYPEMASK (SEL_SINGLEVAL | SEL_ATOMVAL | SEL_VARNUMVAL)
150 /*! \brief
151  * Mask that covers the flags that describe the value type.
152  */
153 #define SEL_VALFLAGMASK (SEL_FLAGSSET | SEL_VALTYPEMASK | SEL_DYNAMIC)
154 /*! \brief
155  * Data has been allocated for the \p v.u union.
156  *
157  * If not set, the \p v.u.ptr points to data allocated externally.
158  * This is the case if the value of the element is used as a parameter
159  * for a selection method or if the element evaluates the final value of
160  * a selection.
161  *
162  * Even if the flag is set, \p v.u.ptr can be NULL during initialization.
163  *
164  * \todo
165  * This flag overlaps with the function of \p v.nalloc field, and could
166  * probably be removed, making memory management simpler. Currently, the
167  * \p v.nalloc field is not kept up-to-date in all cases when this flag
168  * is changed and is used in places where this flag is not, so this would
169  * require a careful investigation of the selection code.
170  */
171 #define SEL_ALLOCVAL    (1<<8)
172 /*! \brief
173  * Data has been allocated for the group/position structure.
174  *
175  * If not set, the memory allocated for fields in \p v.u.g or \p v.u.p is
176  * managed externally.
177  *
178  * This field has no effect if the value type is not \ref GROUP_VALUE or
179  * \ref POS_VALUE, but should not be set.
180  */
181 #define SEL_ALLOCDATA   (1<<9)
182 /*! \brief
183  * \p method->init_frame should be called for the frame.
184  */
185 #define SEL_INITFRAME   (1<<10)
186 /*! \brief
187  * Parameter has been evaluated for the current frame.
188  *
189  * This flag is set for children of \ref SEL_EXPRESSION elements (which
190  * describe method parameters) after the element has been evaluated for the
191  * current frame.
192  * It is not set for \ref SEL_ATOMVAL elements, because they may need to
193  * be evaluated multiple times.
194  */
195 #define SEL_EVALFRAME   (1<<11)
196 /*! \brief
197  * \p method->init has been called.
198  */
199 #define SEL_METHODINIT  (1<<12)
200 /*! \brief
201  * \p method->outinit has been called.
202  *
203  * This flag is also used for \ref SEL_SUBEXPRREF elements.
204  */
205 #define SEL_OUTINIT     (1<<13)
206 /*@}*/
207
208
209 /********************************************************************/
210 /*! \name Selection expression data structures and functions
211  ********************************************************************/
212 /*@{*/
213
214 struct t_selelem;
215
216 /*! \brief
217  * Function pointer for evaluating a \c t_selelem.
218  */
219 typedef int (*sel_evalfunc)(struct gmx_sel_evaluate_t *data,
220                             struct t_selelem *sel, gmx_ana_index_t *g);
221
222 /*! \internal \brief
223  * Represents an element of a selection expression.
224  */
225 typedef struct t_selelem
226 {
227     /*! \brief Name of the element.
228      *
229      * This field is only used for informative purposes.
230      * It is always either NULL or a pointer to a string.
231      * Memory is never allocated for it directly.
232      */
233     const char                         *name;
234     /** Type of the element. */
235     e_selelem_t                         type;
236     /*! \brief
237      * Value storage of the element.
238      *
239      * This field contains the evaluated value of the element, as well as
240      * the output value type.
241      */
242     gmx_ana_selvalue_t                  v;
243     /*! \brief
244      * Evaluation function for the element.
245      *
246      * Can be either NULL (if the expression is a constant and does not require
247      * evaluation) or point to one of the functions defined in evaluate.h.
248      */
249     sel_evalfunc                        evaluate;
250     /*! \brief
251      * Information flags about the element.
252      *
253      * Allowed flags are listed here:
254      * \ref selelem_flags "flags for \c t_selelem".
255      */
256     int                                 flags;
257     /** Data required by the evaluation function. */
258     union {
259         /*! \brief Index group data for several element types.
260          *
261          *  - \ref SEL_CONST : if the value type is \ref GROUP_VALUE,
262          *    this field holds the unprocessed group value.
263          *  - \ref SEL_ROOT : holds the group value for which the
264          *    selection subtree should be evaluated.
265          *  - \ref SEL_SUBEXPR : holds the group for which the subexpression
266          *    has been evaluated.
267          */
268         gmx_ana_index_t                 cgrp;
269         /** Data for \ref SEL_EXPRESSION and \ref SEL_MODIFIER elements. */
270         struct {
271             /** Pointer the the method used in this expression. */
272             struct gmx_ana_selmethod_t *method;
273             /** Pointer to the data allocated by the method's \p init_data (see sel_datafunc()). */
274             void                       *mdata;
275             /** Pointer to the position data passed to the method. */
276             struct gmx_ana_pos_t       *pos;
277             /** Pointer to the evaluation data for \p pos. */
278             struct gmx_ana_poscalc_t   *pc;
279         }                               expr;
280         /** Operation type for \ref SEL_BOOLEAN elements. */
281         e_boolean_t                     boolt;
282         /** Operation type for \ref SEL_ARITHMETIC elements. */
283         struct {
284             /** Operation type. */
285             e_arithmetic_t              type;
286             /** String representation. */
287             char                       *opstr;
288         }                               arith;
289         /** Associated selection parameter for \ref SEL_SUBEXPRREF elements. */
290         struct gmx_ana_selparam_t      *param;
291     }                                   u;
292     /** Memory pool to use for values, or NULL if standard memory handling. */
293     struct gmx_sel_mempool_t           *mempool;
294     /** Internal data for the selection compiler. */
295     struct t_compiler_data             *cdata;
296     
297     /*! \brief The first child element.
298      *
299      * Other children can be accessed through the \p next field of \p child.
300      */
301     struct t_selelem                    *child;
302     /** The next sibling element. */
303     struct t_selelem                    *next;
304     /*! \brief Number of references to this element.
305      *
306      * Should be larger than one only for \ref SEL_SUBEXPR elements.
307      */
308     int                                  refcount;
309 } t_selelem;
310
311 /* In evaluate.c */
312 /** Writes out a human-readable name for an evaluation function. */
313 extern void
314 _gmx_sel_print_evalfunc_name(FILE *fp, sel_evalfunc evalfunc);
315
316 /** Allocates memory and performs some common initialization for a \c t_selelem. */
317 extern t_selelem *
318 _gmx_selelem_create(e_selelem_t type);
319 /** Sets the value type of a \c t_selelem. */
320 extern int
321 _gmx_selelem_set_vtype(t_selelem *sel, e_selvalue_t vtype);
322 /** Reserves memory for value of a \c t_selelem from a memory pool. */
323 extern int
324 _gmx_selelem_mempool_reserve(t_selelem *sel, int count);
325 /** Releases memory pool used for value of a \c t_selelem. */
326 extern void
327 _gmx_selelem_mempool_release(t_selelem *sel);
328 /** Frees the memory allocated for a \c t_selelem structure and all its children. */
329 extern void
330 _gmx_selelem_free(t_selelem *sel);
331 /** Frees the memory allocated for a \c t_selelem structure, all its children, and also all structures referenced through t_selelem::next fields. */
332 extern void
333 _gmx_selelem_free_chain(t_selelem *first);
334
335 /** Frees the memory allocated for the \c t_selelem::d union. */
336 extern void
337 _gmx_selelem_free_values(t_selelem *sel);
338 /** Frees the memory allocated for a selection method. */
339 extern void
340 _gmx_selelem_free_method(struct gmx_ana_selmethod_t *method, void *mdata);
341 /** Frees the memory allocated for the \c t_selelem::u field. */
342 extern void
343 _gmx_selelem_free_exprdata(t_selelem *sel);
344 /* In compiler.c */
345 /** Frees the memory allocated for the selection compiler. */
346 extern void
347 _gmx_selelem_free_compiler_data(t_selelem *sel);
348
349 /** Prints a human-readable version of a selection element subtree. */
350 extern void
351 _gmx_selelem_print_tree(FILE *fp, t_selelem *root, gmx_bool bValues, int level);
352 /* In compile.c */
353 /** Prints a human-readable version of the internal compiler data structure. */
354 extern void
355 _gmx_selelem_print_compiler_info(FILE *fp, t_selelem *sel, int level);
356
357 /** Returns TRUE if the selection element subtree requires topology information for evaluation. */
358 extern gmx_bool
359 _gmx_selelem_requires_top(t_selelem *root);
360
361 /* In sm_insolidangle.c */
362 /** Returns TRUE if the covered fraction of the selection can be calculated. */
363 extern gmx_bool
364 _gmx_selelem_can_estimate_cover(t_selelem *sel);
365 /** Returns the covered fraction of the selection for the current frame. */
366 extern real
367 _gmx_selelem_estimate_coverfrac(t_selelem *sel);
368
369 /*@}*/
370
371 #endif