2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements functions in selelem.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
44 #include "gromacs/legacyheaders/smalloc.h"
46 #include "gromacs/selection/indexutil.h"
47 #include "gromacs/selection/poscalc.h"
48 #include "gromacs/selection/position.h"
49 #include "gromacs/selection/selmethod.h"
50 #include "gromacs/utility/exceptions.h"
51 #include "gromacs/utility/gmxassert.h"
52 #include "gromacs/utility/stringutil.h"
59 * \param[in] sel Selection for which the string is requested
60 * \returns Pointer to a string that corresponds to \p sel->type.
62 * The return value points to a string constant and should not be \p free'd.
64 * The function returns NULL if \p sel->type is not one of the valid values.
67 _gmx_selelem_type_str(const gmx::SelectionTreeElement &sel)
71 case SEL_CONST: return "CONST";
72 case SEL_EXPRESSION: return "EXPR";
73 case SEL_BOOLEAN: return "BOOL";
74 case SEL_ARITHMETIC: return "ARITH";
75 case SEL_ROOT: return "ROOT";
76 case SEL_SUBEXPR: return "SUBEXPR";
77 case SEL_SUBEXPRREF: return "REF";
78 case SEL_GROUPREF: return "GROUPREF";
79 case SEL_MODIFIER: return "MODIFIER";
85 * \param[in] val Value structore for which the string is requested.
86 * \returns Pointer to a string that corresponds to \p val->type,
87 * NULL if the type value is invalid.
89 * The return value points to a string constant and should not be \p free'd.
92 _gmx_sel_value_type_str(const gmx_ana_selvalue_t *val)
96 case NO_VALUE: return "NONE";
97 case INT_VALUE: return "INT";
98 case REAL_VALUE: return "REAL";
99 case STR_VALUE: return "STR";
100 case POS_VALUE: return "VEC";
101 case GROUP_VALUE: return "GROUP";
106 /*! \copydoc _gmx_selelem_type_str() */
108 _gmx_selelem_boolean_type_str(const gmx::SelectionTreeElement &sel)
112 case BOOL_NOT: return "NOT"; break;
113 case BOOL_AND: return "AND"; break;
114 case BOOL_OR: return "OR"; break;
115 case BOOL_XOR: return "XOR"; break;
124 SelectionTreeElement::SelectionTreeElement(e_selelem_t type)
127 this->flags = (type != SEL_ROOT) ? SEL_ALLOCVAL : 0;
128 if (type == SEL_BOOLEAN)
130 this->v.type = GROUP_VALUE;
131 this->flags |= SEL_ALLOCDATA;
135 this->v.type = NO_VALUE;
137 _gmx_selvalue_clear(&this->v);
138 std::memset(&this->u, 0, sizeof(this->u));
139 this->evaluate = NULL;
140 this->mempool = NULL;
144 SelectionTreeElement::~SelectionTreeElement()
146 /* Free the children.
147 * Must be done before freeing other data, because the children may hold
148 * references to data in this element. */
152 freeExpressionData();
156 void SelectionTreeElement::freeValues()
159 if ((flags & SEL_ALLOCDATA) && v.u.ptr)
161 /* The number of position/group structures is constant, so the
162 * backup of using sel->v.nr should work for them.
163 * For strings, we report an error if we don't know the allocation
165 int n = (v.nalloc > 0) ? v.nalloc : v.nr;
169 GMX_RELEASE_ASSERT(v.nalloc != 0,
170 "SEL_ALLOCDATA should only be set for allocated "
172 for (int i = 0; i < n; ++i)
178 for (int i = 0; i < n; ++i)
180 gmx_ana_index_deinit(&v.u.g[i]);
183 default: /* No special handling for other types */
189 if (v.type == POS_VALUE)
198 _gmx_selvalue_setstore(&v, NULL);
199 if (type == SEL_SUBEXPRREF && u.param)
201 u.param->val.u.ptr = NULL;
206 SelectionTreeElement::freeExpressionData()
208 if (type == SEL_EXPRESSION || type == SEL_MODIFIER)
210 _gmx_selelem_free_method(u.expr.method, u.expr.mdata);
212 u.expr.method = NULL;
213 /* Free position data */
216 /* Free position calculation data */
219 gmx_ana_poscalc_free(u.expr.pc);
223 if (type == SEL_ARITHMETIC)
225 sfree(u.arith.opstr);
226 u.arith.opstr = NULL;
228 if (type == SEL_SUBEXPR || type == SEL_ROOT
229 || (type == SEL_CONST && v.type == GROUP_VALUE))
231 gmx_ana_index_deinit(&u.cgrp);
233 if (type == SEL_GROUPREF)
239 void SelectionTreeElement::mempoolReserve(int count)
248 v.u.i = static_cast<int *>(
249 _gmx_sel_mempool_alloc(mempool, sizeof(*v.u.i)*count));
253 v.u.r = static_cast<real *>(
254 _gmx_sel_mempool_alloc(mempool, sizeof(*v.u.r)*count));
258 _gmx_sel_mempool_alloc_group(mempool, v.u.g, count);
262 GMX_THROW(gmx::InternalError("Memory pooling not implemented for requested type"));
266 void SelectionTreeElement::mempoolRelease()
276 _gmx_sel_mempool_free(mempool, v.u.ptr);
277 _gmx_selvalue_setstore(&v, NULL);
283 _gmx_sel_mempool_free_group(mempool, v.u.g);
288 GMX_THROW(gmx::InternalError("Memory pooling not implemented for requested type"));
292 void SelectionTreeElement::fillNameIfMissing(const char *selectionText)
294 GMX_RELEASE_ASSERT(type == SEL_ROOT,
295 "Should not be called for non-root elements");
298 // Check whether the actual selection given was from an external group,
299 // and if so, use the name of the external group.
300 SelectionTreeElementPointer child = this->child;
301 while (child->type == SEL_MODIFIER)
303 if (!child->child || child->child->type != SEL_SUBEXPRREF
304 || !child->child->child)
308 child = child->child->child;
310 if (child->type == SEL_EXPRESSION
311 && child->child && child->child->type == SEL_SUBEXPRREF
312 && child->child->child)
314 if (child->child->child->type == SEL_CONST
315 && child->child->child->v.type == GROUP_VALUE)
317 setName(child->child->child->name());
320 // If the group reference is still unresolved, leave the name empty
321 // and fill it later.
322 if (child->child->child->type == SEL_GROUPREF)
327 // If there still is no name, use the selection string.
328 setName(selectionText);
332 void SelectionTreeElement::resolveIndexGroupReference(gmx_ana_indexgrps_t *grps)
334 GMX_RELEASE_ASSERT(type == SEL_GROUPREF,
335 "Should only be called for index group reference elements");
338 std::string message = formatString(
339 "Cannot match '%s', because index groups are not available.",
341 GMX_THROW(InconsistentInputError(message));
344 gmx_ana_index_t foundGroup;
345 std::string foundName;
346 if (u.gref.name != NULL)
348 if (!gmx_ana_indexgrps_find(&foundGroup, &foundName, grps, u.gref.name))
350 std::string message = formatString(
351 "Cannot match '%s', because no such index group can be found.",
353 GMX_THROW(InconsistentInputError(message));
358 if (!gmx_ana_indexgrps_extract(&foundGroup, &foundName, grps, u.gref.id))
360 std::string message = formatString(
361 "Cannot match '%s', because no such index group can be found.",
363 GMX_THROW(InconsistentInputError(message));
367 if (!gmx_ana_index_check_sorted(&foundGroup))
369 gmx_ana_index_deinit(&foundGroup);
370 std::string message = formatString(
371 "Group '%s' ('%s') cannot be used in selections, "
372 "because atom indices in it are not sorted and/or "
373 "it contains duplicate atoms.",
374 foundName.c_str(), name().c_str());
375 GMX_THROW(InconsistentInputError(message));
380 gmx_ana_index_set(&u.cgrp, foundGroup.isize, foundGroup.index,
381 foundGroup.nalloc_index);
388 * \param[in,out] sel Selection element to set the type for.
389 * \param[in] vtype Value type for the selection element.
391 * If the new type is \ref GROUP_VALUE or \ref POS_VALUE, the
392 * \ref SEL_ALLOCDATA flag is also set.
394 * This function should only be called at most once for each element,
395 * preferably right after calling _gmx_selelem_create().
398 _gmx_selelem_set_vtype(const gmx::SelectionTreeElementPointer &sel,
401 GMX_RELEASE_ASSERT(sel->type != SEL_BOOLEAN || vtype == GROUP_VALUE,
402 "Boolean elements must have a group value");
403 GMX_RELEASE_ASSERT(sel->v.type == NO_VALUE || vtype == sel->v.type,
404 "_gmx_selelem_set_vtype() called more than once");
406 if (vtype == GROUP_VALUE || vtype == POS_VALUE)
408 sel->flags |= SEL_ALLOCDATA;
413 * \param[in] method Method to free.
414 * \param[in] mdata Method data to free.
417 _gmx_selelem_free_method(gmx_ana_selmethod_t *method, void *mdata)
419 sel_freefunc free_func = NULL;
421 /* Save the pointer to the free function. */
422 if (method && method->free)
424 free_func = method->free;
427 /* Free the method itself.
428 * Has to be done before freeing the method data, because parameter
429 * values are typically stored in the method data, and here we may
435 /* Free the memory allocated for the parameters that are not managed
436 * by the selection method itself. */
437 for (i = 0; i < method->nparams; ++i)
439 gmx_ana_selparam_t *param = &method->param[i];
441 if (param->val.u.ptr)
443 if (param->val.type == GROUP_VALUE)
445 for (j = 0; j < param->val.nr; ++j)
447 gmx_ana_index_deinit(¶m->val.u.g[j]);
450 else if (param->val.type == POS_VALUE)
452 if (param->val.nalloc > 0)
454 delete[] param->val.u.p;
455 _gmx_selvalue_setstore(¶m->val, NULL);
459 if (param->val.nalloc > 0)
461 sfree(param->val.u.ptr);
465 sfree(method->param);
468 /* Free method data. */
483 * \param[in] fp File handle to receive the output.
484 * \param[in] sel Root of the selection subtree to print.
485 * \param[in] bValues If true, the evaluated values of selection elements
486 * are printed as well.
487 * \param[in] level Indentation level, starting from zero.
490 _gmx_selelem_print_tree(FILE *fp, const gmx::SelectionTreeElement &sel,
491 bool bValues, int level)
495 fprintf(fp, "%*c %s %s", level*2+1, '*',
496 _gmx_selelem_type_str(sel), _gmx_sel_value_type_str(&sel.v));
497 if (!sel.name().empty())
499 fprintf(fp, " \"%s\"", sel.name().c_str());
501 fprintf(fp, " flg=");
502 if (sel.flags & SEL_FLAGSSET)
506 if (sel.flags & SEL_SINGLEVAL)
510 if (sel.flags & SEL_ATOMVAL)
514 if (sel.flags & SEL_VARNUMVAL)
518 if (sel.flags & SEL_DYNAMIC)
522 if (!(sel.flags & SEL_VALFLAGMASK))
526 if (sel.flags & SEL_ALLOCVAL)
530 if (sel.flags & SEL_ALLOCDATA)
538 if (sel.type == SEL_CONST)
540 if (sel.v.type == INT_VALUE)
542 fprintf(fp, " %d", sel.v.u.i[0]);
544 else if (sel.v.type == REAL_VALUE)
546 fprintf(fp, " %f", sel.v.u.r[0]);
548 else if (sel.v.type == GROUP_VALUE)
550 const gmx_ana_index_t *g = sel.v.u.g;
551 if (!g || g->isize == 0)
555 fprintf(fp, " (%d atoms)", g->isize);
558 else if (sel.type == SEL_BOOLEAN)
560 fprintf(fp, " %s", _gmx_selelem_boolean_type_str(sel));
562 else if (sel.type == SEL_EXPRESSION
563 && sel.u.expr.method->name == sm_compare.name)
565 _gmx_selelem_print_compare_info(fp, sel.u.expr.mdata);
569 fprintf(fp, " eval=");
570 _gmx_sel_print_evalfunc_name(fp, sel.evaluate);
572 if (!(sel.flags & SEL_ALLOCVAL))
574 fprintf(fp, " (ext)");
578 if ((sel.type == SEL_CONST && sel.v.type == GROUP_VALUE) || sel.type == SEL_ROOT)
580 const gmx_ana_index_t *g = sel.v.u.g;
581 if (!g || g->isize == 0 || sel.evaluate != NULL)
587 fprintf(fp, "%*c group: (null)\n", level*2+1, ' ');
589 else if (g->isize > 0)
591 fprintf(fp, "%*c group:", level*2+1, ' ');
594 for (i = 0; i < g->isize; ++i)
596 fprintf(fp, " %d", g->index[i] + 1);
601 fprintf(fp, " %d atoms", g->isize);
606 else if (sel.type == SEL_EXPRESSION)
610 fprintf(fp, "%*c COM", level*2+3, '*');
617 _gmx_selelem_print_compiler_info(fp, sel, level);
620 if (bValues && sel.type != SEL_CONST && sel.type != SEL_ROOT && sel.v.u.ptr)
622 fprintf(fp, "%*c value: ", level*2+1, ' ');
626 /* In normal use, the pointer should never be NULL, but it's
627 * useful to have the check for debugging to avoid accidental
628 * segfaults when printing the selection tree. */
631 fprintf(fp, "(%f, %f, %f)",
632 sel.v.u.p->x[0][XX], sel.v.u.p->x[0][YY],
633 sel.v.u.p->x[0][ZZ]);
637 fprintf(fp, "(null)");
641 fprintf(fp, "%d atoms", sel.v.u.g->isize);
642 if (sel.v.u.g->isize < 20)
644 if (sel.v.u.g->isize > 0)
648 for (i = 0; i < sel.v.u.g->isize; ++i)
650 fprintf(fp, " %d", sel.v.u.g->index[i] + 1);
661 /* Print the subexpressions with one more level of indentation */
662 gmx::SelectionTreeElementPointer child = sel.child;
665 if (!(sel.type == SEL_SUBEXPRREF && child->type == SEL_SUBEXPR))
667 _gmx_selelem_print_tree(fp, *child, bValues, level+1);
674 * \param[in] root Root of the subtree to query.
675 * \returns true if \p root or any any of its elements require topology
676 * information, false otherwise.
679 _gmx_selelem_requires_top(const gmx::SelectionTreeElement &root)
681 if (root.type == SEL_EXPRESSION || root.type == SEL_MODIFIER)
683 if (root.u.expr.method && (root.u.expr.method->flags & SMETH_REQTOP))
687 if (root.u.expr.pc && gmx_ana_poscalc_requires_top(root.u.expr.pc))
692 gmx::SelectionTreeElementPointer child = root.child;
695 if (_gmx_selelem_requires_top(*child))