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 * Implements functions in selelem.h.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_selection
40 #include "gromacs/legacyheaders/smalloc.h"
42 #include "gromacs/selection/indexutil.h"
43 #include "gromacs/selection/poscalc.h"
44 #include "gromacs/selection/position.h"
45 #include "gromacs/selection/selmethod.h"
46 #include "gromacs/utility/exceptions.h"
47 #include "gromacs/utility/gmxassert.h"
54 * \param[in] sel Selection for which the string is requested
55 * \returns Pointer to a string that corresponds to \p sel->type.
57 * The return value points to a string constant and should not be \p free'd.
59 * The function returns NULL if \p sel->type is not one of the valid values.
62 _gmx_selelem_type_str(const gmx::SelectionTreeElement &sel)
66 case SEL_CONST: return "CONST";
67 case SEL_EXPRESSION: return "EXPR";
68 case SEL_BOOLEAN: return "BOOL";
69 case SEL_ARITHMETIC: return "ARITH";
70 case SEL_ROOT: return "ROOT";
71 case SEL_SUBEXPR: return "SUBEXPR";
72 case SEL_SUBEXPRREF: return "REF";
73 case SEL_GROUPREF: return "GROUPREF";
74 case SEL_MODIFIER: return "MODIFIER";
80 * \param[in] val Value structore for which the string is requested.
81 * \returns Pointer to a string that corresponds to \p val->type,
82 * NULL if the type value is invalid.
84 * The return value points to a string constant and should not be \p free'd.
87 _gmx_sel_value_type_str(const gmx_ana_selvalue_t *val)
91 case NO_VALUE: return "NONE";
92 case INT_VALUE: return "INT";
93 case REAL_VALUE: return "REAL";
94 case STR_VALUE: return "STR";
95 case POS_VALUE: return "VEC";
96 case GROUP_VALUE: return "GROUP";
101 /*! \copydoc _gmx_selelem_type_str() */
103 _gmx_selelem_boolean_type_str(const gmx::SelectionTreeElement &sel)
107 case BOOL_NOT: return "NOT"; break;
108 case BOOL_AND: return "AND"; break;
109 case BOOL_OR: return "OR"; break;
110 case BOOL_XOR: return "XOR"; break;
119 SelectionTreeElement::SelectionTreeElement(e_selelem_t type)
123 this->flags = (type != SEL_ROOT) ? SEL_ALLOCVAL : 0;
124 if (type == SEL_BOOLEAN)
126 this->v.type = GROUP_VALUE;
127 this->flags |= SEL_ALLOCDATA;
131 this->v.type = NO_VALUE;
133 _gmx_selvalue_clear(&this->v);
134 std::memset(&this->u, 0, sizeof(this->u));
135 this->evaluate = NULL;
136 this->mempool = NULL;
140 SelectionTreeElement::~SelectionTreeElement()
142 /* Free the children.
143 * Must be done before freeing other data, because the children may hold
144 * references to data in this element. */
148 freeExpressionData();
152 void SelectionTreeElement::freeValues()
155 if ((flags & SEL_ALLOCDATA) && v.u.ptr)
157 /* The number of position/group structures is constant, so the
158 * backup of using sel->v.nr should work for them.
159 * For strings, we report an error if we don't know the allocation
161 int n = (v.nalloc > 0) ? v.nalloc : v.nr;
165 GMX_RELEASE_ASSERT(v.nalloc != 0,
166 "SEL_ALLOCDATA should only be set for allocated "
168 for (int i = 0; i < n; ++i)
174 for (int i = 0; i < n; ++i)
176 gmx_ana_pos_deinit(&v.u.p[i]);
180 for (int i = 0; i < n; ++i)
182 gmx_ana_index_deinit(&v.u.g[i]);
185 default: /* No special handling for other types */
189 if (flags & SEL_ALLOCVAL)
193 _gmx_selvalue_setstore(&v, NULL);
194 if (type == SEL_SUBEXPRREF && u.param)
196 u.param->val.u.ptr = NULL;
201 SelectionTreeElement::freeExpressionData()
203 if (type == SEL_EXPRESSION || type == SEL_MODIFIER)
205 _gmx_selelem_free_method(u.expr.method, u.expr.mdata);
207 u.expr.method = NULL;
208 /* Free position data */
211 gmx_ana_pos_free(u.expr.pos);
214 /* Free position calculation data */
217 gmx_ana_poscalc_free(u.expr.pc);
221 if (type == SEL_ARITHMETIC)
223 sfree(u.arith.opstr);
224 u.arith.opstr = NULL;
226 if (type == SEL_SUBEXPR || type == SEL_ROOT
227 || (type == SEL_CONST && v.type == GROUP_VALUE))
229 gmx_ana_index_deinit(&u.cgrp);
231 if (type == SEL_GROUPREF)
237 void SelectionTreeElement::mempoolReserve(int count)
246 v.u.i = static_cast<int *>(
247 _gmx_sel_mempool_alloc(mempool, sizeof(*v.u.i)*count));
251 v.u.r = static_cast<real *>(
252 _gmx_sel_mempool_alloc(mempool, sizeof(*v.u.r)*count));
256 _gmx_sel_mempool_alloc_group(mempool, v.u.g, count);
260 GMX_THROW(gmx::InternalError("Memory pooling not implemented for requested type"));
264 void SelectionTreeElement::mempoolRelease()
274 _gmx_sel_mempool_free(mempool, v.u.ptr);
275 _gmx_selvalue_setstore(&v, NULL);
281 _gmx_sel_mempool_free_group(mempool, v.u.g);
286 GMX_THROW(gmx::InternalError("Memory pooling not implemented for requested type"));
293 * \param[in,out] sel Selection element to set the type for.
294 * \param[in] vtype Value type for the selection element.
296 * If the new type is \ref GROUP_VALUE or \ref POS_VALUE, the
297 * \ref SEL_ALLOCDATA flag is also set.
299 * This function should only be called at most once for each element,
300 * preferably right after calling _gmx_selelem_create().
303 _gmx_selelem_set_vtype(const gmx::SelectionTreeElementPointer &sel,
306 GMX_RELEASE_ASSERT(sel->type != SEL_BOOLEAN || vtype == GROUP_VALUE,
307 "Boolean elements must have a group value");
308 GMX_RELEASE_ASSERT(sel->v.type == NO_VALUE || vtype == sel->v.type,
309 "_gmx_selelem_set_vtype() called more than once");
311 if (vtype == GROUP_VALUE || vtype == POS_VALUE)
313 sel->flags |= SEL_ALLOCDATA;
318 * \param[in] method Method to free.
319 * \param[in] mdata Method data to free.
322 _gmx_selelem_free_method(gmx_ana_selmethod_t *method, void *mdata)
324 sel_freefunc free_func = NULL;
326 /* Save the pointer to the free function. */
327 if (method && method->free)
329 free_func = method->free;
332 /* Free the method itself.
333 * Has to be done before freeing the method data, because parameter
334 * values are typically stored in the method data, and here we may
340 /* Free the memory allocated for the parameters that are not managed
341 * by the selection method itself. */
342 for (i = 0; i < method->nparams; ++i)
344 gmx_ana_selparam_t *param = &method->param[i];
346 if (param->val.u.ptr)
348 if (param->val.type == GROUP_VALUE)
350 for (j = 0; j < param->val.nr; ++j)
352 gmx_ana_index_deinit(¶m->val.u.g[j]);
355 else if (param->val.type == POS_VALUE)
357 for (j = 0; j < param->val.nr; ++j)
359 gmx_ana_pos_deinit(¶m->val.u.p[j]);
363 if (param->val.nalloc > 0)
365 sfree(param->val.u.ptr);
369 sfree(method->param);
372 /* Free method data. */
384 * \param[in] fp File handle to receive the output.
385 * \param[in] sel Root of the selection subtree to print.
386 * \param[in] bValues If true, the evaluated values of selection elements
387 * are printed as well.
388 * \param[in] level Indentation level, starting from zero.
391 _gmx_selelem_print_tree(FILE *fp, const gmx::SelectionTreeElement &sel,
392 bool bValues, int level)
396 fprintf(fp, "%*c %s %s", level*2+1, '*',
397 _gmx_selelem_type_str(sel), _gmx_sel_value_type_str(&sel.v));
400 fprintf(fp, " \"%s\"", sel.name);
402 fprintf(fp, " flg=");
403 if (sel.flags & SEL_FLAGSSET)
407 if (sel.flags & SEL_SINGLEVAL)
411 if (sel.flags & SEL_ATOMVAL)
415 if (sel.flags & SEL_VARNUMVAL)
419 if (sel.flags & SEL_DYNAMIC)
423 if (!(sel.flags & SEL_VALFLAGMASK))
431 if (sel.type == SEL_CONST)
433 if (sel.v.type == INT_VALUE)
435 fprintf(fp, " %d", sel.v.u.i[0]);
437 else if (sel.v.type == REAL_VALUE)
439 fprintf(fp, " %f", sel.v.u.r[0]);
441 else if (sel.v.type == GROUP_VALUE)
443 const gmx_ana_index_t *g = sel.v.u.g;
444 if (!g || g->isize == 0)
446 fprintf(fp, " (%d atoms)", g->isize);
449 else if (sel.type == SEL_BOOLEAN)
451 fprintf(fp, " %s", _gmx_selelem_boolean_type_str(sel));
453 else if (sel.type == SEL_EXPRESSION
454 && sel.u.expr.method->name == sm_compare.name)
456 _gmx_selelem_print_compare_info(fp, sel.u.expr.mdata);
460 fprintf(fp, " eval=");
461 _gmx_sel_print_evalfunc_name(fp, sel.evaluate);
463 if (!(sel.flags & SEL_ALLOCVAL))
465 fprintf(fp, " (ext. output)");
469 if ((sel.type == SEL_CONST && sel.v.type == GROUP_VALUE) || sel.type == SEL_ROOT)
471 const gmx_ana_index_t *g = sel.v.u.g;
472 if (!g || g->isize == 0 || sel.evaluate != NULL)
478 fprintf(fp, "%*c group: (null)\n", level*2+1, ' ');
480 else if (g->isize > 0)
482 fprintf(fp, "%*c group:", level*2+1, ' ');
485 for (i = 0; i < g->isize; ++i)
487 fprintf(fp, " %d", g->index[i] + 1);
492 fprintf(fp, " %d atoms", g->isize);
497 else if (sel.type == SEL_EXPRESSION)
501 fprintf(fp, "%*c COM", level*2+3, '*');
508 _gmx_selelem_print_compiler_info(fp, sel, level);
511 if (bValues && sel.type != SEL_CONST && sel.type != SEL_ROOT && sel.v.u.ptr)
513 fprintf(fp, "%*c value: ", level*2+1, ' ');
517 /* In normal use, the pointer should never be NULL, but it's
518 * useful to have the check for debugging to avoid accidental
519 * segfaults when printing the selection tree. */
522 fprintf(fp, "(%f, %f, %f)",
523 sel.v.u.p->x[0][XX], sel.v.u.p->x[0][YY],
524 sel.v.u.p->x[0][ZZ]);
528 fprintf(fp, "(null)");
532 fprintf(fp, "%d atoms", sel.v.u.g->isize);
533 if (sel.v.u.g->isize < 20)
535 if (sel.v.u.g->isize > 0)
539 for (i = 0; i < sel.v.u.g->isize; ++i)
541 fprintf(fp, " %d", sel.v.u.g->index[i] + 1);
552 /* Print the subexpressions with one more level of indentation */
553 gmx::SelectionTreeElementPointer child = sel.child;
556 if (!(sel.type == SEL_SUBEXPRREF && child->type == SEL_SUBEXPR))
558 _gmx_selelem_print_tree(fp, *child, bValues, level+1);
565 * \param[in] root Root of the subtree to query.
566 * \returns true if \p root or any any of its elements require topology
567 * information, false otherwise.
570 _gmx_selelem_requires_top(const gmx::SelectionTreeElement &root)
572 if (root.type == SEL_EXPRESSION || root.type == SEL_MODIFIER)
574 if (root.u.expr.method && (root.u.expr.method->flags & SMETH_REQTOP))
578 if (root.u.expr.pc && gmx_ana_poscalc_requires_top(root.u.expr.pc))
583 gmx::SelectionTreeElementPointer child = root.child;
586 if (_gmx_selelem_requires_top(*child))