Add gmx convert-trj
[gromacs.git] / src / gromacs / selection / sm_keywords.cpp
blob73228d6a5280f9b288b49d15e7f6c0e2ba74c18b
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019, 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.
35 /*! \internal \file
36 * \brief
37 * Implements internal selection methods for numeric and string keyword
38 * evaluation.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_selection
43 #include "gmxpre.h"
45 #include <cctype>
46 #include <cstring>
48 #include <regex>
49 #include <string>
51 #include "gromacs/utility/arraysize.h"
52 #include "gromacs/utility/cstringutil.h"
53 #include "gromacs/utility/exceptions.h"
54 #include "gromacs/utility/gmxassert.h"
55 #include "gromacs/utility/smalloc.h"
56 #include "gromacs/utility/stringutil.h"
58 #include "keywords.h"
59 #include "parsetree.h"
60 #include "position.h"
61 #include "scanner.h"
62 #include "selelem.h"
63 #include "selmethod.h"
65 /*! \brief
66 * Allocates data for integer keyword evaluation.
68 * \param[in] npar Not used.
69 * \param param Not used.
70 * \returns Pointer to the allocated data (\ref t_methoddata_kwint).
72 * Allocates memory for a \ref t_methoddata_kwint structure.
74 static void *
75 init_data_kwint(int npar, gmx_ana_selparam_t * param);
76 /*! \brief
77 * Allocates data for real keyword evaluation.
79 * \param[in] npar Not used.
80 * \param param Not used.
81 * \returns Pointer to the allocated data (\ref t_methoddata_kwreal).
83 * Allocates memory for a \ref t_methoddata_kwreal structure.
85 static void *
86 init_data_kwreal(int npar, gmx_ana_selparam_t * param);
87 /*! \brief
88 * Allocates data for string keyword evaluation.
90 * \param[in] npar Not used.
91 * \param param Not used.
92 * \returns Pointer to the allocated data (t_methoddata_kwstr).
94 * Allocates memory for a t_methoddata_kwstr structure.
96 static void *
97 init_data_kwstr(int npar, gmx_ana_selparam_t * param);
98 /** /brief Initializes data for integer keyword evaluation.
100 * \param[in] top Not used.
101 * \param[in] npar Not used (should be 2).
102 * \param[in] param Method parameters (should point to \ref smparams_keyword_int).
103 * \param[in] data Should point to \ref t_methoddata_kwint.
105 static void
106 init_kwint(const gmx_mtop_t *top, int npar, gmx_ana_selparam_t *param, void *data);
107 /*! \brief
108 * Initializes data for real keyword evaluation.
110 * \param[in] top Not used.
111 * \param[in] npar Not used (should be 2).
112 * \param[in] param Method parameters (should point to \ref smparams_keyword_real).
113 * \param[in] data Should point to \ref t_methoddata_kwreal.
114 * \returns 0 (the initialization always succeeds).
116 static void
117 init_kwreal(const gmx_mtop_t *top, int npar, gmx_ana_selparam_t *param, void *data);
118 /*! \brief
119 * Initializes data for string keyword evaluation.
121 * \param[in] top Not used.
122 * \param[in] npar Not used (should be 2).
123 * \param[in] param Method parameters (should point to \ref smparams_keyword_str).
124 * \param[in] data Should point to t_methoddata_kwstr.
126 static void
127 init_kwstr(const gmx_mtop_t *top, int npar, gmx_ana_selparam_t *param, void *data);
128 /** Frees the memory allocated for string keyword evaluation. */
129 static void
130 free_data_kwstr(void *data);
131 /** Evaluates integer selection keywords. */
132 static void
133 evaluate_keyword_int(const gmx::SelMethodEvalContext &context,
134 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
135 /** Evaluates real selection keywords. */
136 static void
137 evaluate_keyword_real(const gmx::SelMethodEvalContext &context,
138 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
139 /** Evaluates string selection keywords. */
140 static void
141 evaluate_keyword_str(const gmx::SelMethodEvalContext &context,
142 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
144 /*! \internal \brief
145 * Data structure for integer keyword expression evaluation.
147 typedef struct t_methoddata_kwint
149 /** Array of values for the keyword. */
150 int *v;
151 /** Number of ranges in the \p r array. */
152 int n;
153 /*! \brief
154 * Array of sorted integer ranges to match against.
156 * Each range is made of two integers, giving the endpoints (inclusive).
157 * This field stores the pointer to the ranges allocated by the
158 * parameter parser; see \ref SPAR_RANGES for more information.
160 int *r;
161 } t_methoddata_kwint;
163 /*! \internal \brief
164 * Data structure for real keyword expression evaluation.
166 typedef struct t_methoddata_kwreal
168 /** Array of values for the keyword. */
169 real *v;
170 /** Number of ranges in the \p r array. */
171 int n;
172 /*! \brief
173 * Array of sorted ranges to match against.
175 * Each range is made of two values, giving the endpoints (inclusive).
176 * This field stores the pointer to the ranges allocated by the
177 * parameter parser; see \ref SPAR_RANGES for more information.
179 real *r;
180 } t_methoddata_kwreal;
182 namespace
185 /*! \internal \brief
186 * Single item in the list of strings/regular expressions to match.
188 * \ingroup module_selection
190 class StringKeywordMatchItem
192 public:
193 /*! \brief
194 * Constructs a matcher from a string.
196 * \param[in] matchType String matching type.
197 * \param[in] str String to use for matching.
199 StringKeywordMatchItem(gmx::SelectionStringMatchType matchType,
200 const char *str)
201 : str_(str)
203 useRegExp_ = (matchType == gmx::eStringMatchType_RegularExpression);
204 if (matchType == gmx::eStringMatchType_Auto)
206 for (size_t j = 0; j < std::strlen(str); ++j)
208 if (std::ispunct(str[j]) && str[j] != '?' && str[j] != '*')
210 useRegExp_ = true;
211 break;
215 if (useRegExp_)
219 regex_.assign(str, std::regex::nosubs | std::regex::extended);
221 catch (const std::regex_error & /*ex*/)
223 // TODO: Better error messages.
224 GMX_THROW(gmx::InvalidInputError
225 (gmx::formatString("Error in regular expression \"%s\"", str)));
230 /*! \brief
231 * Checks whether this item matches a string.
233 * \param[in] matchType String matching type.
234 * \param[in] value String to match.
235 * \returns true if this item matches \p value.
237 bool match(gmx::SelectionStringMatchType matchType,
238 const char *value) const
240 if (matchType == gmx::eStringMatchType_Exact)
242 return str_ == value;
244 else if (useRegExp_)
246 return std::regex_match(value, regex_);
248 else
250 return gmx_wcmatch(str_.c_str(), value) == 0;
254 private:
255 //! The raw string passed for the matcher.
256 std::string str_;
257 //! Whether a regular expression match is used.
258 bool useRegExp_;
259 //! Regular expression compiled from \p str_, if applicable.
260 std::regex regex_;
263 /*! \internal \brief
264 * Data structure for string keyword expression evaluation.
266 struct t_methoddata_kwstr
268 /** Matching type for the strings. */
269 gmx::SelectionStringMatchType matchType;
270 /** Array of values for the keyword. */
271 char **v;
272 /** Array of strings/regular expressions to match against.*/
273 std::vector<StringKeywordMatchItem> matches;
276 } // namespace
278 /** Parameters for integer keyword evaluation. */
279 static gmx_ana_selparam_t smparams_keyword_int[] = {
280 {nullptr, {INT_VALUE, -1, {nullptr}}, nullptr, SPAR_ATOMVAL},
281 {nullptr, {INT_VALUE, -1, {nullptr}}, nullptr, SPAR_RANGES | SPAR_VARNUM},
284 /** Parameters for real keyword evaluation. */
285 static gmx_ana_selparam_t smparams_keyword_real[] = {
286 {nullptr, {REAL_VALUE, -1, {nullptr}}, nullptr, SPAR_ATOMVAL | SPAR_DYNAMIC},
287 {nullptr, {REAL_VALUE, -1, {nullptr}}, nullptr, SPAR_RANGES | SPAR_VARNUM},
290 /** Parameters for string keyword evaluation. */
291 static gmx_ana_selparam_t smparams_keyword_str[] = {
292 {nullptr, {STR_VALUE, -1, {nullptr}}, nullptr, SPAR_ATOMVAL},
293 {nullptr, {STR_VALUE, -1, {nullptr}}, nullptr, SPAR_VARNUM},
296 /** Selection method data for integer keyword evaluation. */
297 gmx_ana_selmethod_t sm_keyword_int = {
298 "kw_int", GROUP_VALUE, SMETH_SINGLEVAL,
299 asize(smparams_keyword_int), smparams_keyword_int,
300 &init_data_kwint,
301 nullptr,
302 &init_kwint,
303 nullptr,
304 nullptr,
305 nullptr,
306 &evaluate_keyword_int,
307 nullptr,
308 {nullptr, nullptr, 0, nullptr},
311 /** Selection method data for real keyword evaluation. */
312 gmx_ana_selmethod_t sm_keyword_real = {
313 "kw_real", GROUP_VALUE, SMETH_SINGLEVAL,
314 asize(smparams_keyword_real), smparams_keyword_real,
315 &init_data_kwreal,
316 nullptr,
317 &init_kwreal,
318 nullptr,
319 nullptr,
320 nullptr,
321 &evaluate_keyword_real,
322 nullptr,
323 {nullptr, nullptr, 0, nullptr},
326 /** Selection method data for string keyword evaluation. */
327 gmx_ana_selmethod_t sm_keyword_str = {
328 "kw_str", GROUP_VALUE, SMETH_SINGLEVAL,
329 asize(smparams_keyword_str), smparams_keyword_str,
330 &init_data_kwstr,
331 nullptr,
332 &init_kwstr,
333 nullptr,
334 &free_data_kwstr,
335 nullptr,
336 &evaluate_keyword_str,
337 nullptr,
338 {nullptr, nullptr, 0, nullptr},
341 /*! \brief
342 * Initializes keyword evaluation for an arbitrary group.
344 * \param[in] top Not used.
345 * \param[in] npar Not used.
346 * \param[in] param Not used.
347 * \param[in] data Should point to \ref t_methoddata_kweval.
348 * \returns 0 on success, a non-zero error code on return.
350 * Calls the initialization method of the wrapped keyword.
352 static void
353 init_kweval(const gmx_mtop_t *top, int npar, gmx_ana_selparam_t * param, void *data);
354 /*! \brief
355 * Initializes output for keyword evaluation in an arbitrary group.
357 * \param[in] top Not used.
358 * \param[in,out] out Pointer to output data structure.
359 * \param[in,out] data Should point to \c t_methoddata_kweval.
360 * \returns 0 for success.
362 static void
363 init_output_kweval(const gmx_mtop_t *top, gmx_ana_selvalue_t *out, void *data);
364 /** Frees the data allocated for keyword evaluation in an arbitrary group. */
365 static void
366 free_data_kweval(void *data);
367 /** Initializes frame evaluation for keyword evaluation in an arbitrary group. */
368 static void
369 init_frame_kweval(const gmx::SelMethodEvalContext &context, void *data);
370 /*! \brief
371 * Evaluates keywords in an arbitrary group.
373 * See sel_updatefunc() for description of the parameters.
374 * \p data should point to a \c t_methoddata_kweval.
376 * Calls the evaluation function of the wrapped keyword with the given
377 * parameters, with the exception of using \c t_methoddata_kweval::g for the
378 * evaluation group.
380 static void
381 evaluate_kweval(const gmx::SelMethodEvalContext &context, gmx_ana_index_t *g,
382 gmx_ana_selvalue_t *out, void *data);
383 /*! \brief
384 * Evaluates keywords in an arbitrary set of positions.
386 * See sel_updatefunc() for description of the parameters.
387 * \p data should point to a \c t_methoddata_kweval.
389 * Calls the evaluation function of the wrapped keyword with the given
390 * parameters, with the exception of using \c t_methoddata_kweval::p for the
391 * evaluation positions.
393 static void
394 evaluate_kweval_pos(const gmx::SelMethodEvalContext &context, gmx_ana_index_t *g,
395 gmx_ana_selvalue_t *out, void *data);
397 /*! \internal \brief
398 * Data structure for keyword evaluation in arbitrary groups.
400 struct t_methoddata_kweval
402 //! Initialize new keyword evaluator for the given keyword.
403 t_methoddata_kweval(gmx_ana_selmethod_t *method, void *data)
404 : kwmethod(method), kwmdata(data)
406 gmx_ana_index_clear(&g);
408 ~t_methoddata_kweval()
410 _gmx_selelem_free_method(kwmethod, kwmdata);
413 //! Wrapped keyword method for evaluating the values.
414 gmx_ana_selmethod_t *kwmethod;
415 //! Method data for \p kwmethod.
416 void *kwmdata;
417 //! Group in which \p kwmethod should be evaluated.
418 gmx_ana_index_t g;
419 //! Positions for which \p kwmethod should be evaluated.
420 gmx_ana_pos_t p;
423 /** Parameters for keyword evaluation in an arbitrary group. */
424 static gmx_ana_selparam_t smparams_kweval_group[] = {
425 {nullptr, {GROUP_VALUE, 1, {nullptr}}, nullptr, SPAR_DYNAMIC},
427 /** Parameters for keyword evaluation from positions. */
428 static gmx_ana_selparam_t smparams_kweval_pos[] = {
429 {nullptr, {POS_VALUE, 1, {nullptr}}, nullptr, SPAR_DYNAMIC},
433 /********************************************************************
434 * INTEGER KEYWORD EVALUATION
435 ********************************************************************/
437 static void *
438 init_data_kwint(int /* npar */, gmx_ana_selparam_t * /* param */)
440 t_methoddata_kwint *data;
442 snew(data, 1);
443 return data;
446 static void
447 init_kwint(const gmx_mtop_t * /* top */, int /* npar */, gmx_ana_selparam_t *param, void *data)
449 t_methoddata_kwint *d = static_cast<t_methoddata_kwint *>(data);
451 d->v = param[0].val.u.i;
452 d->n = param[1].val.nr;
453 d->r = param[1].val.u.i;
457 * See sel_updatefunc() for description of the parameters.
458 * \p data should point to a \c t_methoddata_kwint.
460 * Does a binary search to find which atoms match the ranges in the
461 * \c t_methoddata_kwint structure for this selection.
462 * Matching atoms are stored in \p out->u.g.
464 static void
465 evaluate_keyword_int(const gmx::SelMethodEvalContext & /*context*/,
466 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
468 t_methoddata_kwint *d = static_cast<t_methoddata_kwint *>(data);
469 int n, i, j, jmin, jmax;
470 int val;
472 out->u.g->isize = 0;
473 n = d->n;
474 for (i = 0; i < g->isize; ++i)
476 val = d->v[i];
477 if (d->r[0] > val || d->r[2*n-1] < val)
479 continue;
481 jmin = 0;
482 jmax = n;
483 while (jmax - jmin > 1)
485 j = jmin + (jmax - jmin) / 2;
486 if (val < d->r[2*j])
488 jmax = j;
490 else
492 jmin = j;
493 if (val <= d->r[2*j+1])
495 break;
497 /* ++jmin;*/
500 if (val <= d->r[2*jmin+1])
502 out->u.g->index[out->u.g->isize++] = g->index[i];
508 /********************************************************************
509 * REAL KEYWORD EVALUATION
510 ********************************************************************/
512 static void *
513 init_data_kwreal(int /* npar */, gmx_ana_selparam_t * /* param */)
515 t_methoddata_kwreal *data;
517 snew(data, 1);
518 return data;
521 static void
522 init_kwreal(const gmx_mtop_t * /* top */, int /* npar */, gmx_ana_selparam_t *param, void *data)
524 t_methoddata_kwreal *d = static_cast<t_methoddata_kwreal *>(data);
526 d->v = param[0].val.u.r;
527 d->n = param[1].val.nr;
528 d->r = param[1].val.u.r;
532 * See sel_updatefunc() for description of the parameters.
533 * \p data should point to a \c t_methoddata_kwreal.
535 * Does a binary search to find which atoms match the ranges in the
536 * \c t_methoddata_kwreal structure for this selection.
537 * Matching atoms are stored in \p out->u.g.
539 static void
540 evaluate_keyword_real(const gmx::SelMethodEvalContext & /*context*/,
541 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
543 t_methoddata_kwreal *d = static_cast<t_methoddata_kwreal *>(data);
544 int n, i, j, jmin, jmax;
545 real val;
547 out->u.g->isize = 0;
548 n = d->n;
549 for (i = 0; i < g->isize; ++i)
551 val = d->v[i];
552 if (d->r[0] > val || d->r[2*n-1] < val)
554 continue;
556 jmin = 0;
557 jmax = n;
558 while (jmax - jmin > 1)
560 j = jmin + (jmax - jmin) / 2;
561 if (val < d->r[2*j])
563 jmax = j;
565 else
567 jmin = j;
568 if (val <= d->r[2*j+1])
570 break;
572 /* ++jmin;*/
575 if (val <= d->r[2*jmin+1])
577 out->u.g->index[out->u.g->isize++] = g->index[i];
583 /********************************************************************
584 * STRING KEYWORD EVALUATION
585 ********************************************************************/
587 static void *
588 init_data_kwstr(int /* npar */, gmx_ana_selparam_t * /* param */)
590 t_methoddata_kwstr *data = new t_methoddata_kwstr();
591 data->matchType = gmx::eStringMatchType_Auto;
592 return data;
596 * \param[in,out] sel Selection element to initialize.
597 * \param[in] matchType Method to use to match string values.
599 * Sets the string matching method for string keyword matching.
601 void
602 _gmx_selelem_set_kwstr_match_type(const gmx::SelectionTreeElementPointer &sel,
603 gmx::SelectionStringMatchType matchType)
605 t_methoddata_kwstr *d = static_cast<t_methoddata_kwstr *>(sel->u.expr.mdata);
607 if (sel->type != SEL_EXPRESSION || !sel->u.expr.method
608 || sel->u.expr.method->name != sm_keyword_str.name)
610 return;
612 d->matchType = matchType;
615 static void
616 init_kwstr(const gmx_mtop_t * /* top */, int /* npar */, gmx_ana_selparam_t *param, void *data)
618 t_methoddata_kwstr *d = static_cast<t_methoddata_kwstr *>(data);
620 d->v = param[0].val.u.s;
621 /* Return if this is not the first time */
622 if (!d->matches.empty())
624 return;
626 int n = param[1].val.nr;
627 d->matches.reserve(n);
628 for (int i = 0; i < n; ++i)
630 const char *s = param[1].val.u.s[i];
631 d->matches.emplace_back(d->matchType, s);
636 * \param data Data to free (should point to a t_methoddata_kwstr).
638 static void
639 free_data_kwstr(void *data)
641 t_methoddata_kwstr *d = static_cast<t_methoddata_kwstr *>(data);
642 delete d;
646 * See sel_updatefunc() for description of the parameters.
647 * \p data should point to a \c t_methoddata_kwstr.
649 * Does a linear search to find which atoms match the strings in the
650 * \c t_methoddata_kwstr structure for this selection.
651 * Wildcards are allowed in the strings.
652 * Matching atoms are stored in \p out->u.g.
654 static void
655 evaluate_keyword_str(const gmx::SelMethodEvalContext & /*context*/,
656 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
658 t_methoddata_kwstr *d = static_cast<t_methoddata_kwstr *>(data);
660 out->u.g->isize = 0;
661 for (int i = 0; i < g->isize; ++i)
663 for (size_t j = 0; j < d->matches.size(); ++j)
665 if (d->matches[j].match(d->matchType, d->v[i]))
667 out->u.g->index[out->u.g->isize++] = g->index[i];
668 break;
675 /********************************************************************
676 * KEYWORD EVALUATION FOR ARBITRARY GROUPS
677 ********************************************************************/
679 static void
680 init_kweval(const gmx_mtop_t *top, int /* npar */, gmx_ana_selparam_t * /* param */, void *data)
682 t_methoddata_kweval *d = static_cast<t_methoddata_kweval *>(data);
684 d->kwmethod->init(top, 0, nullptr, d->kwmdata);
687 static void
688 init_output_kweval(const gmx_mtop_t * /* top */, gmx_ana_selvalue_t *out, void *data)
690 t_methoddata_kweval *d = static_cast<t_methoddata_kweval *>(data);
692 out->nr = d->g.isize;
693 _gmx_selvalue_reserve(out, out->nr);
697 * \param data Data to free (should point to a \c t_methoddata_kweval).
699 * Frees the memory allocated for all the members of \c t_methoddata_kweval.
701 static void
702 free_data_kweval(void *data)
704 t_methoddata_kweval *d = static_cast<t_methoddata_kweval *>(data);
706 delete d;
710 * \param[in] context Evaluation context.
711 * \param data Should point to a \ref t_methoddata_kweval.
713 static void
714 init_frame_kweval(const gmx::SelMethodEvalContext &context, void *data)
716 t_methoddata_kweval *d = static_cast<t_methoddata_kweval *>(data);
718 d->kwmethod->init_frame(context, d->kwmdata);
721 static void
722 evaluate_kweval(const gmx::SelMethodEvalContext &context,
723 gmx_ana_index_t * /* g */, gmx_ana_selvalue_t *out, void *data)
725 t_methoddata_kweval *d = static_cast<t_methoddata_kweval *>(data);
727 d->kwmethod->update(context, &d->g, out, d->kwmdata);
730 static void
731 evaluate_kweval_pos(const gmx::SelMethodEvalContext &context,
732 gmx_ana_index_t * /* g */, gmx_ana_selvalue_t *out, void *data)
734 t_methoddata_kweval *d = static_cast<t_methoddata_kweval *>(data);
736 d->kwmethod->pupdate(context, &d->p, out, d->kwmdata);
739 /*! \brief
740 * Initializes a selection element for evaluating a keyword in a given group.
742 * \param[in] method Keyword selection method to evaluate.
743 * \param[in] params Parameters to pass to initialization (the child group).
744 * \param[in] scanner Scanner data structure.
745 * \returns Pointer to the created selection element.
747 * Implements _gmx_sel_init_keyword_evaluator() for \ref GROUP_VALUE input
748 * selections.
750 static gmx::SelectionTreeElementPointer
751 init_evaluator_group(gmx_ana_selmethod_t *method,
752 const gmx::SelectionParserParameterList &params,
753 void *scanner)
755 if ((method->flags & (SMETH_SINGLEVAL | SMETH_VARNUMVAL))
756 || method->outinit || method->pupdate)
758 std::string message
759 = gmx::formatString("Keyword '%s' cannot be evaluated in this context",
760 method->name);
761 GMX_THROW(gmx::InvalidInputError(message));
764 // TODO: For same ... as ..., some other location could be more intuitive.
765 gmx::SelectionTreeElementPointer sel(
766 new gmx::SelectionTreeElement(
767 SEL_EXPRESSION, _gmx_sel_lexer_get_current_location(scanner)));
768 _gmx_selelem_set_method(sel, method, scanner);
770 t_methoddata_kweval *data
771 = new t_methoddata_kweval(sel->u.expr.method, sel->u.expr.mdata);
773 snew(sel->u.expr.method, 1);
774 sel->u.expr.method->name = data->kwmethod->name;
775 sel->u.expr.method->type = data->kwmethod->type;
776 sel->u.expr.method->flags = data->kwmethod->flags | SMETH_VARNUMVAL;
777 sel->u.expr.method->init_data = nullptr;
778 sel->u.expr.method->set_poscoll = nullptr;
779 sel->u.expr.method->init = method->init ? &init_kweval : nullptr;
780 sel->u.expr.method->outinit = &init_output_kweval;
781 sel->u.expr.method->free = &free_data_kweval;
782 sel->u.expr.method->init_frame = method->init_frame ? &init_frame_kweval : nullptr;
783 sel->u.expr.method->update = &evaluate_kweval;
784 sel->u.expr.method->pupdate = nullptr;
785 sel->u.expr.method->nparams = asize(smparams_kweval_group);
786 sel->u.expr.method->param = smparams_kweval_group;
787 _gmx_selelem_init_method_params(sel, scanner);
788 sel->u.expr.mdata = data;
790 sel->u.expr.method->param[0].val.u.g = &data->g;
792 _gmx_sel_parse_params(params, sel->u.expr.method->nparams,
793 sel->u.expr.method->param, sel, scanner);
794 return sel;
797 /*! \brief
798 * Initializes a selection element for evaluating a keyword in given positions.
800 * \param[in] method Keyword selection method to evaluate.
801 * \param[in] params Parameters to pass to initialization (the child positions).
802 * \param[in] scanner Scanner data structure.
803 * \returns Pointer to the created selection element.
805 * Implements _gmx_sel_init_keyword_evaluator() for \ref POS_VALUE input
806 * selections.
808 static gmx::SelectionTreeElementPointer
809 init_evaluator_pos(gmx_ana_selmethod_t *method,
810 const gmx::SelectionParserParameterList &params,
811 void *scanner)
813 if ((method->flags & (SMETH_SINGLEVAL | SMETH_VARNUMVAL))
814 || method->outinit || method->pupdate == nullptr)
816 std::string message
817 = gmx::formatString("Keyword '%s' cannot be evaluated in this context",
818 method->name);
819 GMX_THROW(gmx::InvalidInputError(message));
822 gmx::SelectionTreeElementPointer sel(
823 new gmx::SelectionTreeElement(
824 SEL_EXPRESSION, _gmx_sel_lexer_get_current_location(scanner)));
825 _gmx_selelem_set_method(sel, method, scanner);
827 t_methoddata_kweval *data
828 = new t_methoddata_kweval(sel->u.expr.method, sel->u.expr.mdata);
830 snew(sel->u.expr.method, 1);
831 sel->u.expr.method->name = data->kwmethod->name;
832 sel->u.expr.method->type = data->kwmethod->type;
833 sel->u.expr.method->flags = data->kwmethod->flags | SMETH_SINGLEVAL;
834 sel->u.expr.method->init_data = nullptr;
835 sel->u.expr.method->set_poscoll = nullptr;
836 sel->u.expr.method->init = method->init ? &init_kweval : nullptr;
837 sel->u.expr.method->outinit = nullptr;
838 sel->u.expr.method->free = &free_data_kweval;
839 sel->u.expr.method->init_frame = method->init_frame ? &init_frame_kweval : nullptr;
840 sel->u.expr.method->update = &evaluate_kweval_pos;
841 sel->u.expr.method->pupdate = nullptr;
842 sel->u.expr.method->nparams = asize(smparams_kweval_pos);
843 sel->u.expr.method->param = smparams_kweval_pos;
844 _gmx_selelem_init_method_params(sel, scanner);
845 sel->u.expr.mdata = data;
847 sel->u.expr.method->param[0].val.u.p = &data->p;
849 _gmx_sel_parse_params(params, sel->u.expr.method->nparams,
850 sel->u.expr.method->param, sel, scanner);
851 return sel;
854 gmx::SelectionTreeElementPointer
855 _gmx_sel_init_keyword_evaluator(gmx_ana_selmethod_t *method,
856 const gmx::SelectionTreeElementPointer &child,
857 void *scanner)
859 gmx::SelectionParserParameterList params;
860 params.push_back(
861 gmx::SelectionParserParameter::createFromExpression(nullptr, child));
862 if (child->v.type == GROUP_VALUE)
864 return init_evaluator_group(method, params, scanner);
866 else if (child->v.type == POS_VALUE)
868 return init_evaluator_pos(method, params, scanner);
870 else
872 std::string text(_gmx_sel_lexer_get_text(scanner, child->location()));
873 std::string message
874 = gmx::formatString("Expression '%s' cannot be used to evaluate keywords",
875 text.c_str());
876 GMX_THROW(gmx::InvalidInputError(message));