2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009-2018, The GROMACS development team.
5 * Copyright (c) 2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
38 * Implements functions in selmethod.h.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_selection
45 #include "selmethod.h"
50 #include "gromacs/utility/arraysize.h"
51 #include "gromacs/utility/cstringutil.h"
52 #include "gromacs/utility/exceptions.h"
53 #include "gromacs/utility/stringutil.h"
55 #include "selmethod_impl.h"
59 * Helper structure for defining selection methods.
64 * Name to register the method under.
66 * If NULL, use the actual name of the method.
67 * This field is used for defining synonyms.
70 /** Method data structure to register. */
71 gmx_ana_selmethod_t* method;
75 * Convenience function for reporting errors found in selection methods.
77 static void report_error(FILE* fp, const char* name, gmx_fmtstr const char* fmt, ...)
78 gmx_format(printf, 3, 4);
80 static void report_error(FILE* fp, const char* name, gmx_fmtstr const char* fmt, ...)
86 fprintf(fp, "selection method '%s': ", name);
87 vfprintf(fp, fmt, ap);
94 * Convenience function for reporting errors found in selection method parameters.
96 static void report_param_error(FILE* fp, const char* mname, const char* pname, gmx_fmtstr const char* fmt, ...)
97 gmx_format(printf, 4, 5);
98 static void report_param_error(FILE* fp, const char* mname, const char* pname, gmx_fmtstr const char* fmt, ...)
104 fprintf(fp, "selection method '%s': parameter '%s': ", mname, pname);
105 vfprintf(fp, fmt, ap);
112 * Checks the validity of parameters.
114 * \param[in] fp File handle to use for diagnostic messages
116 * \param[in] name Name of the method (used for error messages).
117 * \param[in] nparams Number of parameters in \p param.
118 * \param[in,out] param Parameter array
119 * (only the \c flags field of boolean parameters may be modified).
120 * \param[in] symtab Symbol table (used for checking overlaps).
121 * \returns true if there are no problems with the parameters,
124 * This function performs some checks common to both check_method() and
126 * The purpose of these checks is to ensure that the selection parser does not
127 * need to check for the validity of the parameters at each turn, and to
128 * report programming errors as early as possible.
129 * If you remove a check, make sure that the parameter parser can handle the
130 * resulting parameters.
132 static bool check_params(FILE* fp,
135 gmx_ana_selparam_t param[],
136 const gmx::SelectionParserSymbolTable& symtab)
141 if (nparams > 0 && !param)
143 report_error(fp, name, "error: missing parameter data");
146 if (nparams == 0 && param)
148 report_error(fp, name, "warning: parameter data unused because nparams=0");
150 /* Check each parameter */
151 for (i = 0; i < nparams; ++i)
153 /* Check that there is at most one NULL name, in the beginning */
154 if (param[i].name == nullptr && i > 0)
156 report_error(fp, name, "error: NULL parameter should be the first one");
160 /* Check for duplicates */
161 for (j = 0; j < i; ++j)
163 if (param[j].name == nullptr)
167 if (!gmx_strcasecmp(param[i].name, param[j].name))
169 report_error(fp, name, "error: duplicate parameter name '%s'", param[i].name);
175 if (param[i].flags & SPAR_SET)
177 report_param_error(fp, name, param[i].name, "warning: flag SPAR_SET is set");
178 param[i].flags &= ~SPAR_SET;
180 if (param[i].flags & SPAR_RANGES)
182 if (param[i].val.type != INT_VALUE && param[i].val.type != REAL_VALUE)
184 report_param_error(fp,
187 "error: SPAR_RANGES cannot be set for a non-numeric parameter");
190 if (param[i].flags & SPAR_DYNAMIC)
192 report_param_error(fp,
195 "warning: SPAR_DYNAMIC does not have effect with SPAR_RANGES");
196 param[i].flags &= ~SPAR_DYNAMIC;
198 if (!(param[i].flags & SPAR_VARNUM) && param[i].val.nr != 1)
204 "error: range should take either one or an arbitrary number of values");
207 if (param[i].flags & SPAR_ATOMVAL)
210 fp, name, param[i].name, "error: SPAR_RANGES and SPAR_ATOMVAL both set");
214 if ((param[i].flags & SPAR_VARNUM) && (param[i].flags & SPAR_ATOMVAL))
217 fp, name, param[i].name, "error: SPAR_VARNUM and SPAR_ATOMVAL both set");
220 if (param[i].flags & SPAR_ENUMVAL)
222 if (param[i].val.type != STR_VALUE)
224 report_param_error(fp,
227 "error: SPAR_ENUMVAL can only be set for string parameters");
230 if (param[i].val.nr != 1)
232 report_param_error(fp,
235 "error: SPAR_ENUMVAL parameters should take exactly one value");
238 if (param[i].flags & (SPAR_DYNAMIC | SPAR_VARNUM | SPAR_ATOMVAL))
240 report_param_error(fp,
243 "error: only SPAR_OPTIONAL supported with SPAR_ENUMVAL");
247 /* Check boolean parameters */
248 if (param[i].val.type == NO_VALUE)
250 if (param[i].val.nr != 0)
252 report_param_error(fp,
255 "error: number of values should be zero for boolean parameters");
258 /* The boolean parameters should always be optional, so set the
259 * flag for convenience. */
260 param[i].flags |= SPAR_OPTIONAL;
261 /* Any other flags should not be specified */
262 if (param[i].flags & ~SPAR_OPTIONAL)
264 report_param_error(fp,
267 "error: boolean parameter should not have any flags set");
272 if (param[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL))
274 if (param[i].val.nr != -1)
280 "warning: val.nr is not -1 although SPAR_VARNUM/SPAR_ATOMVAL is set");
282 param[i].val.nr = -1;
284 else if (param[i].val.type != NO_VALUE)
286 if (param[i].val.nr <= 0)
288 report_param_error(fp, name, param[i].name, "error: val.nr <= 0");
292 /* Check that the value pointer is NULL */
293 if (param[i].nvalptr != nullptr)
295 report_param_error(fp, name, param[i].name, "warning: nvalptr is set");
297 if (param[i].val.u.ptr != nullptr && !(param[i].flags & SPAR_ENUMVAL))
299 report_param_error(fp, name, param[i].name, "warning: value pointer is set");
301 /* Check that the name contains only valid characters */
302 if (param[i].name == nullptr)
306 if (!isalpha(param[i].name[0]))
308 report_param_error(fp, name, param[i].name, "error: name does not begin with a letter");
312 for (j = 1; param[i].name[j] != 0; ++j)
314 if (param[i].name[j] != '_' && !isalnum(param[i].name[j]))
317 fp, name, param[i].name, "error: name contains non-alphanumeric characters");
322 if (param[i].name[j] != 0)
326 /* Check that the name does not conflict with a method */
327 if (symtab.findSymbol(param[i].name) != nullptr)
329 report_param_error(fp,
332 "error: name conflicts with another method or a keyword");
335 } /* End of parameter loop */
336 /* Check parameters of existing methods */
337 gmx::SelectionParserSymbolIterator symbol =
338 symtab.beginIterator(gmx::SelectionParserSymbol::MethodSymbol);
339 while (symbol != symtab.endIterator())
341 gmx_ana_selmethod_t* method = symbol->methodValue();
342 gmx_ana_selparam_t* param = gmx_ana_selmethod_find_param(name, method);
345 report_param_error(fp,
348 "error: name conflicts with another method or a keyword");
357 * Checks the validity of selection method callback functions.
359 * \param[in] fp File handle to use for diagnostic messages
361 * \param[in] method The method to check.
362 * \returns true if there are no problems, false otherwise.
364 * This function performs some checks common to both check_method() and
366 * This function checks that all the required callbacks are defined, i.e.,
367 * not NULL, to find programming errors.
369 static bool check_callbacks(FILE* fp, gmx_ana_selmethod_t* method)
375 /* Make some checks on init_data and free */
376 if (method->nparams > 0 && !method->init_data)
380 "error: init_data should be provided because the method has parameters");
383 if (method->free && !method->init_data)
385 report_error(fp, method->name, "warning: free is not used because of missing init_data");
387 /* Check presence of outinit for position-valued methods */
388 if (method->type == POS_VALUE && !method->outinit)
392 "error: outinit should be provided because the method has POS_VALUE");
395 /* Check presence of outinit for variable output count methods */
396 if ((method->flags & SMETH_VARNUMVAL) && !method->outinit)
400 "error: outinit should be provided because the method has SMETH_VARNUMVAL");
403 /* Warn of dynamic callbacks in static methods */
404 if (!(method->flags & SMETH_MODIFIER))
406 if (method->pupdate && !(method->flags & SMETH_DYNAMIC))
408 report_error(fp, method->name, "warning: pupdate not used because the method is static");
409 method->pupdate = nullptr;
412 /* Check that there is an evaluation function */
413 if (method->type != NO_VALUE && !method->update && !method->pupdate)
415 report_error(fp, method->name, "error: evaluation function missing");
418 /* Loop through the parameters to determine if initialization callbacks
421 for (i = 0; i < method->nparams; ++i)
423 if (method->param[i].val.type != POS_VALUE
424 && (method->param[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
429 /* Check that the callbacks required by the parameters are present */
430 if (bNeedInit && !method->init)
432 report_error(fp, method->name, "error: init should be provided");
439 * Checks the validity of a selection method.
441 * \param[in] fp File handle to use for diagnostic messages
443 * \param[in,out] method Method to check.
444 * \param[in] symtab Symbol table (used for checking overlaps).
446 * Checks the validity of the given selection method data structure
447 * that does not have \ref SMETH_MODIFIER set.
448 * If you remove a check, please make sure that the selection parser,
449 * compiler, and evaluation functions can deal with the method.
451 static bool check_method(FILE* fp, gmx_ana_selmethod_t* method, const gmx::SelectionParserSymbolTable& symtab)
456 if (method->type == NO_VALUE)
458 report_error(fp, method->name, "error: no value type specified");
461 if (method->type == STR_VALUE && method->nparams > 0)
463 report_error(fp, method->name, "error: evaluates to a string but is not a keyword");
467 if (method->type == GROUP_VALUE)
469 /* Group methods should always have SMETH_SINGLEVAL,
470 * so set it for convenience. */
471 method->flags |= SMETH_SINGLEVAL;
472 /* Check that conflicting flags are not present. */
473 if (method->flags & SMETH_VARNUMVAL)
477 "error: SMETH_VARNUMVAL cannot be set for group-valued methods");
483 if ((method->flags & SMETH_SINGLEVAL) && (method->flags & SMETH_VARNUMVAL))
485 report_error(fp, method->name, "error: SMETH_SINGLEVAL and SMETH_VARNUMVAL both set");
489 if ((method->flags & SMETH_CHARVAL) && method->type != STR_VALUE)
493 "error: SMETH_CHARVAL can only be specified for STR_VALUE methods");
496 /* Check the parameters */
497 if (!check_params(fp, method->name, method->nparams, method->param, symtab))
501 /* Check the callback pointers */
502 if (!check_callbacks(fp, method))
511 * Checks the validity of a selection modifier method.
513 * \param[in] fp File handle to use for diagnostic messages
515 * \param[in,out] method Method to check.
516 * \param[in] symtab Symbol table (used for checking overlaps).
518 * Checks the validity of the given selection method data structure
519 * that has \ref SMETH_MODIFIER set.
520 * If you remove a check, please make sure that the selection parser,
521 * compiler, and evaluation functions can deal with the method.
523 static bool check_modifier(FILE* fp, gmx_ana_selmethod_t* method, const gmx::SelectionParserSymbolTable& symtab)
528 if (method->type != NO_VALUE && method->type != POS_VALUE)
530 report_error(fp, method->name, "error: modifier should have type POS_VALUE or NO_VALUE");
534 if (method->flags & (SMETH_SINGLEVAL | SMETH_VARNUMVAL))
538 "error: modifier should not have SMETH_SINGLEVAL or SMETH_VARNUMVAL set");
541 /* Check the parameters */
542 /* The first parameter is skipped */
543 if (!check_params(fp, method->name, method->nparams - 1, method->param + 1, symtab))
547 /* Check the callback pointers */
548 if (!check_callbacks(fp, method))
554 report_error(fp, method->name, "error: modifier should not have update");
557 if (method->type == POS_VALUE && !method->pupdate)
559 report_error(fp, method->name, "error: evaluation function missing");
567 * \param[in,out] symtab Symbol table to register the method to.
568 * \param[in] name Name under which the method should be registered.
569 * \param[in] method Method to register.
570 * \returns 0 on success, -1 if there was something wrong with the
573 * \p name does not need to match the name of the method, and the same
574 * method can be registered multiple times under different names.
575 * If \p name equals some previously registered name,
576 * an error message is printed and the method is not registered.
578 * The function also performs some sanity checking on the input method,
579 * and refuses to register it if there are problems.
580 * Some problems only generate warnings.
581 * All problems are described to \p stderr.
583 int gmx_ana_selmethod_register(gmx::SelectionParserSymbolTable* symtab,
585 gmx_ana_selmethod_t* method)
589 /* Check the method */
590 if (method->flags & SMETH_MODIFIER)
592 bOk = check_modifier(stderr, method, *symtab);
596 bOk = check_method(stderr, method, *symtab);
598 /* Try to register the method if everything is ok */
603 symtab->addMethod(name, method);
605 catch (const gmx::APIError& ex)
607 report_error(stderr, name, "%s", ex.what());
613 report_error(stderr, name, "warning: not registered");
620 * \param[in,out] symtab Symbol table to register the methods to.
621 * \returns 0 on success, -1 if any of the default methods could not be
624 int gmx_ana_selmethod_register_defaults(gmx::SelectionParserSymbolTable* symtab)
626 /* Array of selection methods defined in the library. */
627 const t_register_method smtable_def[] = {
628 { nullptr, &sm_cog }, { nullptr, &sm_com },
630 { nullptr, &sm_all }, { nullptr, &sm_none },
631 { nullptr, &sm_atomnr }, { nullptr, &sm_resnr },
632 { "resid", &sm_resnr }, { nullptr, &sm_resindex },
633 { "residue", &sm_resindex }, { nullptr, &sm_molindex },
634 { "mol", &sm_molindex }, { "molecule", &sm_molindex },
635 { nullptr, &sm_atomname }, { "name", &sm_atomname },
636 { nullptr, &sm_pdbatomname }, { "pdbname", &sm_pdbatomname },
637 { nullptr, &sm_atomtype }, { "type", &sm_atomtype },
638 { nullptr, &sm_resname }, { nullptr, &sm_insertcode },
639 { nullptr, &sm_chain }, { nullptr, &sm_mass },
640 { nullptr, &sm_charge }, { nullptr, &sm_altloc },
641 { nullptr, &sm_occupancy }, { nullptr, &sm_betafactor },
642 { "beta", &sm_betafactor }, { nullptr, &sm_x },
643 { nullptr, &sm_y }, { nullptr, &sm_z },
645 { nullptr, &sm_distance }, { "dist", &sm_distance },
646 { nullptr, &sm_mindistance }, { "mindist", &sm_mindistance },
647 { nullptr, &sm_within }, { nullptr, &sm_insolidangle },
648 { nullptr, &sm_same },
650 { nullptr, &sm_merge }, { nullptr, &sm_plus },
651 { nullptr, &sm_permute },
658 for (int i = 0; i < asize(smtable_def); ++i)
660 gmx_ana_selmethod_t* method = smtable_def[i].method;
662 if (smtable_def[i].name == nullptr)
664 rc = gmx_ana_selmethod_register(symtab, method->name, method);
668 rc = gmx_ana_selmethod_register(symtab, smtable_def[i].name, method);
679 * \param[in] name Name of the parameter to search.
680 * \param[in] method Method to search for the parameter.
681 * \returns Pointer to the parameter in the
682 * \ref gmx_ana_selmethod_t::param "method->param" array,
683 * or NULL if no parameter with name \p name was found.
685 * This is a simple wrapper for gmx_ana_selparam_find().
687 gmx_ana_selparam_t* gmx_ana_selmethod_find_param(const char* name, gmx_ana_selmethod_t* method)
689 return gmx_ana_selparam_find(name, method->nparams, method->param);