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 internal selection methods for numeric and string keyword
36 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
37 * \ingroup module_selection
44 #ifdef HAVE_SYS_TYPES_H
45 #include <sys/types.h> /*old Mac needs types before regex.h*/
56 #include "gromacs/selection/selmethod.h"
57 #include "gromacs/utility/errorcodes.h"
58 #include "gromacs/utility/messagestringcollector.h"
61 #include "parsetree.h"
65 /** Allocates data for integer keyword evaluation. */
67 init_data_kwint(int npar, gmx_ana_selparam_t *param);
68 /** Allocates data for real keyword evaluation. */
70 init_data_kwreal(int npar, gmx_ana_selparam_t *param);
71 /** Allocates data for string keyword evaluation. */
73 init_data_kwstr(int npar, gmx_ana_selparam_t *param);
74 /** Initializes data for integer keyword evaluation. */
76 init_kwint(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
77 /** Initializes data for real keyword evaluation. */
79 init_kwreal(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
80 /** Initializes data for string keyword evaluation. */
82 init_kwstr(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
83 /** Frees the memory allocated for string keyword evaluation. */
85 free_data_kwstr(void *data);
86 /** Evaluates integer selection keywords. */
88 evaluate_keyword_int(t_topology *top, t_trxframe *fr, t_pbc *pbc,
89 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
90 /** Evaluates real selection keywords. */
92 evaluate_keyword_real(t_topology *top, t_trxframe *fr, t_pbc *pbc,
93 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
94 /** Evaluates string selection keywords. */
96 evaluate_keyword_str(t_topology *top, t_trxframe *fr, t_pbc *pbc,
97 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
100 * Data structure for integer keyword expression evaluation.
102 typedef struct t_methoddata_kwint
104 /** Array of values for the keyword. */
106 /** Number of ranges in the \p r array. */
109 * Array of sorted integer ranges to match against.
111 * Each range is made of two integers, giving the endpoints (inclusive).
112 * This field stores the pointer to the ranges allocated by the
113 * parameter parser; see \ref SPAR_RANGES for more information.
116 } t_methoddata_kwint;
119 * Data structure for real keyword expression evaluation.
121 typedef struct t_methoddata_kwreal
123 /** Array of values for the keyword. */
125 /** Number of ranges in the \p r array. */
128 * Array of sorted ranges to match against.
130 * Each range is made of two values, giving the endpoints (inclusive).
131 * This field stores the pointer to the ranges allocated by the
132 * parameter parser; see \ref SPAR_RANGES for more information.
135 } t_methoddata_kwreal;
138 * Data structure for string keyword expression evaluation.
140 typedef struct t_methoddata_kwstr
142 /** Array of values for the keyword. */
144 /** Number of elements in the \p val array. */
147 * Array of strings/regular expressions to match against.
149 struct t_methoddata_kwstr_match {
150 /** true if the expression is a regular expression, false otherwise. */
152 /** The value to match against. */
155 /** Compiled regular expression if \p bRegExp is true. */
158 /** The string if \p bRegExp is false; */
162 /**< Array of strings/regular expressions to match against.*/
163 } t_methoddata_kwstr;
165 /** Parameters for integer keyword evaluation. */
166 static gmx_ana_selparam_t smparams_keyword_int[] = {
167 {NULL, {INT_VALUE, -1, {NULL}}, NULL, SPAR_ATOMVAL},
168 {NULL, {INT_VALUE, -1, {NULL}}, NULL, SPAR_RANGES | SPAR_VARNUM},
171 /** Parameters for real keyword evaluation. */
172 static gmx_ana_selparam_t smparams_keyword_real[] = {
173 {NULL, {REAL_VALUE, -1, {NULL}}, NULL, SPAR_ATOMVAL | SPAR_DYNAMIC},
174 {NULL, {REAL_VALUE, -1, {NULL}}, NULL, SPAR_RANGES | SPAR_VARNUM},
177 /** Parameters for string keyword evaluation. */
178 static gmx_ana_selparam_t smparams_keyword_str[] = {
179 {NULL, {STR_VALUE, -1, {NULL}}, NULL, SPAR_ATOMVAL},
180 {NULL, {STR_VALUE, -1, {NULL}}, NULL, SPAR_VARNUM},
183 /** \internal Selection method data for integer keyword evaluation. */
184 gmx_ana_selmethod_t sm_keyword_int = {
185 "kw_int", GROUP_VALUE, SMETH_SINGLEVAL,
186 asize(smparams_keyword_int), smparams_keyword_int,
193 &evaluate_keyword_int,
198 /** \internal Selection method data for real keyword evaluation. */
199 gmx_ana_selmethod_t sm_keyword_real = {
200 "kw_real", GROUP_VALUE, SMETH_SINGLEVAL,
201 asize(smparams_keyword_real), smparams_keyword_real,
208 &evaluate_keyword_real,
213 /** \internal Selection method data for string keyword evaluation. */
214 gmx_ana_selmethod_t sm_keyword_str = {
215 "kw_str", GROUP_VALUE, SMETH_SINGLEVAL,
216 asize(smparams_keyword_str), smparams_keyword_str,
223 &evaluate_keyword_str,
228 /** Initializes keyword evaluation for an arbitrary group. */
230 init_kweval(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
231 /** Initializes output for keyword evaluation in an arbitrary group. */
233 init_output_kweval(t_topology *top, gmx_ana_selvalue_t *out, void *data);
234 /** Frees the data allocated for keyword evaluation in an arbitrary group. */
236 free_data_kweval(void *data);
237 /** Initializes frame evaluation for keyword evaluation in an arbitrary group. */
239 init_frame_kweval(t_topology *top, t_trxframe *fr, t_pbc *pbc, void *data);
240 /** Evaluates keywords in an arbitrary group. */
242 evaluate_kweval(t_topology *top, t_trxframe *fr, t_pbc *pbc,
243 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
246 * Data structure for keyword evaluation in arbitrary groups.
250 /** Wrapped keyword method for evaluating the values. */
251 gmx_ana_selmethod_t *kwmethod;
252 /** Method data for \p kwmethod. */
254 /** Group in which \p kwmethod should be evaluated. */
256 } t_methoddata_kweval;
258 /** Parameters for keyword evaluation in an arbitrary group. */
259 static gmx_ana_selparam_t smparams_kweval[] = {
260 {NULL, {GROUP_VALUE, 1, {NULL}}, NULL, SPAR_DYNAMIC},
264 /********************************************************************
265 * INTEGER KEYWORD EVALUATION
266 ********************************************************************/
269 * \param[in] npar Not used.
270 * \param param Not used.
271 * \returns Pointer to the allocated data (\ref t_methoddata_kwint).
273 * Allocates memory for a \ref t_methoddata_kwint structure.
276 init_data_kwint(int npar, gmx_ana_selparam_t *param)
278 t_methoddata_kwint *data;
285 * \param[in] top Not used.
286 * \param[in] npar Not used (should be 2).
287 * \param[in] param Method parameters (should point to \ref smparams_keyword_int).
288 * \param[in] data Should point to \ref t_methoddata_kwint.
291 init_kwint(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
293 t_methoddata_kwint *d = (t_methoddata_kwint *)data;
295 d->v = param[0].val.u.i;
296 d->n = param[1].val.nr;
297 d->r = param[1].val.u.i;
301 * See sel_updatefunc() for description of the parameters.
302 * \p data should point to a \c t_methoddata_kwint.
304 * Does a binary search to find which atoms match the ranges in the
305 * \c t_methoddata_kwint structure for this selection.
306 * Matching atoms are stored in \p out->u.g.
309 evaluate_keyword_int(t_topology *top, t_trxframe *fr, t_pbc *pbc,
310 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
312 t_methoddata_kwint *d = (t_methoddata_kwint *)data;
313 int n, i, j, jmin, jmax;
318 for (i = 0; i < g->isize; ++i)
321 if (d->r[0] > val || d->r[2*n-1] < val)
327 while (jmax - jmin > 1)
329 j = jmin + (jmax - jmin) / 2;
337 if (val <= d->r[2*j+1])
344 if (val <= d->r[2*jmin+1])
346 out->u.g->index[out->u.g->isize++] = g->index[i];
352 /********************************************************************
353 * REAL KEYWORD EVALUATION
354 ********************************************************************/
357 * \param[in] npar Not used.
358 * \param param Not used.
359 * \returns Pointer to the allocated data (\ref t_methoddata_kwreal).
361 * Allocates memory for a \ref t_methoddata_kwreal structure.
364 init_data_kwreal(int npar, gmx_ana_selparam_t *param)
366 t_methoddata_kwreal *data;
373 * \param[in] top Not used.
374 * \param[in] npar Not used (should be 2).
375 * \param[in] param Method parameters (should point to \ref smparams_keyword_real).
376 * \param[in] data Should point to \ref t_methoddata_kwreal.
377 * \returns 0 (the initialization always succeeds).
380 init_kwreal(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
382 t_methoddata_kwreal *d = (t_methoddata_kwreal *)data;
384 d->v = param[0].val.u.r;
385 d->n = param[1].val.nr;
386 d->r = param[1].val.u.r;
390 * See sel_updatefunc() for description of the parameters.
391 * \p data should point to a \c t_methoddata_kwreal.
393 * Does a binary search to find which atoms match the ranges in the
394 * \c t_methoddata_kwreal structure for this selection.
395 * Matching atoms are stored in \p out->u.g.
398 evaluate_keyword_real(t_topology *top, t_trxframe *fr, t_pbc *pbc,
399 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
401 t_methoddata_kwreal *d = (t_methoddata_kwreal *)data;
402 int n, i, j, jmin, jmax;
407 for (i = 0; i < g->isize; ++i)
410 if (d->r[0] > val || d->r[2*n-1] < val)
416 while (jmax - jmin > 1)
418 j = jmin + (jmax - jmin) / 2;
426 if (val <= d->r[2*j+1])
433 if (val <= d->r[2*jmin+1])
435 out->u.g->index[out->u.g->isize++] = g->index[i];
441 /********************************************************************
442 * STRING KEYWORD EVALUATION
443 ********************************************************************/
446 * \param[in] npar Not used.
447 * \param param Not used.
448 * \returns Pointer to the allocated data (\ref t_methoddata_kwstr).
450 * Allocates memory for a \ref t_methoddata_kwstr structure.
453 init_data_kwstr(int npar, gmx_ana_selparam_t *param)
455 t_methoddata_kwstr *data;
462 * \param[in] top Not used.
463 * \param[in] npar Not used (should be 2).
464 * \param[in] param Method parameters (should point to \ref smparams_keyword_str).
465 * \param[in] data Should point to \ref t_methoddata_kwstr.
468 init_kwstr(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
470 t_methoddata_kwstr *d = (t_methoddata_kwstr *)data;
476 d->v = param[0].val.u.s;
477 d->n = param[1].val.nr;
478 /* Return if this is not the first time */
484 for (i = 0; i < d->n; ++i)
486 s = param[1].val.u.s[i];
488 for (j = 0; j < strlen(s); ++j)
490 if (ispunct(s[j]) && s[j] != '?' && s[j] != '*')
498 // TODO: Get rid of these prints to stderr
501 snew(buf, strlen(s) + 3);
502 sprintf(buf, "^%s$", s);
503 if (regcomp(&d->m[i].u.r, buf, REG_EXTENDED | REG_NOSUB))
506 fprintf(stderr, "WARNING: error in regular expression,\n"
507 " will match '%s' as a simple string\n", s);
512 fprintf(stderr, "WARNING: no regular expressions support,\n"
513 " will match '%s' as a simple string\n", s);
520 d->m[i].bRegExp = bRegExp;
525 * \param data Data to free (should point to a \ref t_methoddata_kwstr).
527 * Frees the memory allocated for t_methoddata_kwstr::val.
530 free_data_kwstr(void *data)
532 t_methoddata_kwstr *d = (t_methoddata_kwstr *)data;
535 for (i = 0; i < d->n; ++i)
540 /* This branch should only be taken if regular expressions
541 * are available, but the ifdef is still needed. */
542 regfree(&d->m[i].u.r);
550 * See sel_updatefunc() for description of the parameters.
551 * \p data should point to a \c t_methoddata_kwstr.
553 * Does a linear search to find which atoms match the strings in the
554 * \c t_methoddata_kwstr structure for this selection.
555 * Wildcards are allowed in the strings.
556 * Matching atoms are stored in \p out->u.g.
559 evaluate_keyword_str(t_topology *top, t_trxframe *fr, t_pbc *pbc,
560 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
562 t_methoddata_kwstr *d = (t_methoddata_kwstr *)data;
567 for (i = 0; i < g->isize; ++i)
570 for (j = 0; j < d->n && !bFound; ++j)
575 /* This branch should only be taken if regular expressions
576 * are available, but the ifdef is still needed. */
577 if (!regexec(&d->m[j].u.r, d->v[i], 0, NULL, 0))
585 if (gmx_wcmatch(d->m[j].u.s, d->v[i]) == 0)
593 out->u.g->index[out->u.g->isize++] = g->index[i];
599 /********************************************************************
600 * KEYWORD EVALUATION FOR ARBITRARY GROUPS
601 ********************************************************************/
604 * \param[in] top Not used.
605 * \param[in] npar Not used.
606 * \param[in] param Not used.
607 * \param[in] data Should point to \ref t_methoddata_kweval.
608 * \returns 0 on success, a non-zero error code on return.
610 * Calls the initialization method of the wrapped keyword.
613 init_kweval(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
615 t_methoddata_kweval *d = (t_methoddata_kweval *)data;
617 d->kwmethod->init(top, 0, NULL, d->kwmdata);
621 * \param[in] top Not used.
622 * \param[in,out] out Pointer to output data structure.
623 * \param[in,out] data Should point to \c t_methoddata_kweval.
624 * \returns 0 for success.
627 init_output_kweval(t_topology *top, gmx_ana_selvalue_t *out, void *data)
629 t_methoddata_kweval *d = (t_methoddata_kweval *)data;
631 out->nr = d->g.isize;
635 * \param data Data to free (should point to a \c t_methoddata_kweval).
637 * Frees the memory allocated for all the members of \c t_methoddata_kweval.
640 free_data_kweval(void *data)
642 t_methoddata_kweval *d = (t_methoddata_kweval *)data;
644 _gmx_selelem_free_method(d->kwmethod, d->kwmdata);
648 * \param[in] top Topology.
649 * \param[in] fr Current frame.
650 * \param[in] pbc PBC structure.
651 * \param data Should point to a \ref t_methoddata_kweval.
652 * \returns 0 on success, a non-zero error code on error.
654 * Creates a lookup structure that enables fast queries of whether a point
655 * is within the solid angle or not.
658 init_frame_kweval(t_topology *top, t_trxframe *fr, t_pbc *pbc, void *data)
660 t_methoddata_kweval *d = (t_methoddata_kweval *)data;
662 d->kwmethod->init_frame(top, fr, pbc, d->kwmdata);
666 * See sel_updatefunc() for description of the parameters.
667 * \p data should point to a \c t_methoddata_kweval.
669 * Calls the evaluation function of the wrapped keyword with the given
670 * parameters, with the exception of using \c t_methoddata_kweval::g for the
674 evaluate_kweval(t_topology *top, t_trxframe *fr, t_pbc *pbc,
675 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
677 t_methoddata_kweval *d = (t_methoddata_kweval *)data;
679 d->kwmethod->update(top, fr, pbc, &d->g, out, d->kwmdata);
683 * \param[out] selp Pointer to receive a pointer to the created selection
684 * element (set to NULL on error).
685 * \param[in] method Keyword selection method to evaluate.
686 * \param[in] param Parameter that gives the group to evaluate \p method in.
687 * \param[in] scanner Scanner data structure.
688 * \returns 0 on success, non-zero error code on error.
690 * Creates a \ref SEL_EXPRESSION selection element (pointer put in \c *selp)
691 * that evaluates the keyword method given by \p method in the group given by
695 _gmx_sel_init_keyword_evaluator(t_selelem **selp, gmx_ana_selmethod_t *method,
696 t_selexpr_param *param, void *scanner)
699 t_methoddata_kweval *data;
701 gmx::MessageStringCollector *errors = _gmx_sel_lexer_error_reporter(scanner);
703 sprintf(buf, "In evaluation of '%s'", method->name);
704 gmx::MessageStringContext context(errors, buf);
706 if ((method->flags & (SMETH_SINGLEVAL | SMETH_VARNUMVAL))
707 || method->outinit || method->pupdate)
709 _gmx_selexpr_free_params(param);
710 GMX_ERROR(gmx::eeInternalError,
711 "Unsupported keyword method for arbitrary group evaluation");
715 sel = _gmx_selelem_create(SEL_EXPRESSION);
716 _gmx_selelem_set_method(sel, method, scanner);
719 data->kwmethod = sel->u.expr.method;
720 data->kwmdata = sel->u.expr.mdata;
721 gmx_ana_index_clear(&data->g);
723 snew(sel->u.expr.method, 1);
724 memcpy(sel->u.expr.method, data->kwmethod, sizeof(gmx_ana_selmethod_t));
725 sel->u.expr.method->flags |= SMETH_VARNUMVAL;
726 sel->u.expr.method->init_data = NULL;
727 sel->u.expr.method->set_poscoll = NULL;
728 sel->u.expr.method->init = method->init ? &init_kweval : NULL;
729 sel->u.expr.method->outinit = &init_output_kweval;
730 sel->u.expr.method->free = &free_data_kweval;
731 sel->u.expr.method->init_frame = method->init_frame ? &init_frame_kweval : NULL;
732 sel->u.expr.method->update = &evaluate_kweval;
733 sel->u.expr.method->pupdate = NULL;
734 sel->u.expr.method->nparams = asize(smparams_kweval);
735 sel->u.expr.method->param = smparams_kweval;
736 _gmx_selelem_init_method_params(sel, scanner);
737 sel->u.expr.mdata = data;
739 sel->u.expr.method->param[0].val.u.g = &data->g;
743 if (!_gmx_sel_parse_params(param, sel->u.expr.method->nparams,
744 sel->u.expr.method->param, sel, scanner))
746 _gmx_selelem_free(sel);