2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009-2018, The GROMACS development team.
5 * Copyright (c) 2019, 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, name, param[i].name,
185 "error: SPAR_RANGES cannot be set for a non-numeric parameter");
188 if (param[i].flags & SPAR_DYNAMIC)
190 report_param_error(fp, name, param[i].name,
191 "warning: SPAR_DYNAMIC does not have effect with SPAR_RANGES");
192 param[i].flags &= ~SPAR_DYNAMIC;
194 if (!(param[i].flags & SPAR_VARNUM) && param[i].val.nr != 1)
197 fp, name, param[i].name,
198 "error: range should take either one or an arbitrary number of values");
201 if (param[i].flags & SPAR_ATOMVAL)
203 report_param_error(fp, name, param[i].name,
204 "error: SPAR_RANGES and SPAR_ATOMVAL both set");
208 if ((param[i].flags & SPAR_VARNUM) && (param[i].flags & SPAR_ATOMVAL))
210 report_param_error(fp, name, param[i].name,
211 "error: SPAR_VARNUM and SPAR_ATOMVAL both set");
214 if (param[i].flags & SPAR_ENUMVAL)
216 if (param[i].val.type != STR_VALUE)
218 report_param_error(fp, name, param[i].name,
219 "error: SPAR_ENUMVAL can only be set for string parameters");
222 if (param[i].val.nr != 1)
224 report_param_error(fp, name, param[i].name,
225 "error: SPAR_ENUMVAL parameters should take exactly one value");
228 if (param[i].flags & (SPAR_DYNAMIC | SPAR_VARNUM | SPAR_ATOMVAL))
230 report_param_error(fp, name, param[i].name,
231 "error: only SPAR_OPTIONAL supported with SPAR_ENUMVAL");
235 /* Check boolean parameters */
236 if (param[i].val.type == NO_VALUE)
238 if (param[i].val.nr != 0)
240 report_param_error(fp, name, param[i].name,
241 "error: number of values should be zero for boolean parameters");
244 /* The boolean parameters should always be optional, so set the
245 * flag for convenience. */
246 param[i].flags |= SPAR_OPTIONAL;
247 /* Any other flags should not be specified */
248 if (param[i].flags & ~SPAR_OPTIONAL)
250 report_param_error(fp, name, param[i].name,
251 "error: boolean parameter should not have any flags set");
256 if (param[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL))
258 if (param[i].val.nr != -1)
261 fp, name, param[i].name,
262 "warning: val.nr is not -1 although SPAR_VARNUM/SPAR_ATOMVAL is set");
264 param[i].val.nr = -1;
266 else if (param[i].val.type != NO_VALUE)
268 if (param[i].val.nr <= 0)
270 report_param_error(fp, name, param[i].name, "error: val.nr <= 0");
274 /* Check that the value pointer is NULL */
275 if (param[i].nvalptr != nullptr)
277 report_param_error(fp, name, param[i].name, "warning: nvalptr is set");
279 if (param[i].val.u.ptr != nullptr && !(param[i].flags & SPAR_ENUMVAL))
281 report_param_error(fp, name, param[i].name, "warning: value pointer is set");
283 /* Check that the name contains only valid characters */
284 if (param[i].name == nullptr)
288 if (!isalpha(param[i].name[0]))
290 report_param_error(fp, name, param[i].name, "error: name does not begin with a letter");
294 for (j = 1; param[i].name[j] != 0; ++j)
296 if (param[i].name[j] != '_' && !isalnum(param[i].name[j]))
298 report_param_error(fp, name, param[i].name,
299 "error: name contains non-alphanumeric characters");
304 if (param[i].name[j] != 0)
308 /* Check that the name does not conflict with a method */
309 if (symtab.findSymbol(param[i].name) != nullptr)
311 report_param_error(fp, name, param[i].name,
312 "error: name conflicts with another method or a keyword");
315 } /* End of parameter loop */
316 /* Check parameters of existing methods */
317 gmx::SelectionParserSymbolIterator symbol =
318 symtab.beginIterator(gmx::SelectionParserSymbol::MethodSymbol);
319 while (symbol != symtab.endIterator())
321 gmx_ana_selmethod_t* method = symbol->methodValue();
322 gmx_ana_selparam_t* param = gmx_ana_selmethod_find_param(name, method);
325 report_param_error(fp, method->name, param->name,
326 "error: name conflicts with another method or a keyword");
335 * Checks the validity of selection method callback functions.
337 * \param[in] fp File handle to use for diagnostic messages
339 * \param[in] method The method to check.
340 * \returns true if there are no problems, false otherwise.
342 * This function performs some checks common to both check_method() and
344 * This function checks that all the required callbacks are defined, i.e.,
345 * not NULL, to find programming errors.
347 static bool check_callbacks(FILE* fp, gmx_ana_selmethod_t* method)
353 /* Make some checks on init_data and free */
354 if (method->nparams > 0 && !method->init_data)
356 report_error(fp, method->name,
357 "error: init_data should be provided because the method has parameters");
360 if (method->free && !method->init_data)
362 report_error(fp, method->name, "warning: free is not used because of missing init_data");
364 /* Check presence of outinit for position-valued methods */
365 if (method->type == POS_VALUE && !method->outinit)
367 report_error(fp, method->name,
368 "error: outinit should be provided because the method has POS_VALUE");
371 /* Check presence of outinit for variable output count methods */
372 if ((method->flags & SMETH_VARNUMVAL) && !method->outinit)
374 report_error(fp, method->name,
375 "error: outinit should be provided because the method has SMETH_VARNUMVAL");
378 /* Warn of dynamic callbacks in static methods */
379 if (!(method->flags & SMETH_MODIFIER))
381 if (method->pupdate && !(method->flags & SMETH_DYNAMIC))
383 report_error(fp, method->name, "warning: pupdate not used because the method is static");
384 method->pupdate = nullptr;
387 /* Check that there is an evaluation function */
388 if (method->type != NO_VALUE && !method->update && !method->pupdate)
390 report_error(fp, method->name, "error: evaluation function missing");
393 /* Loop through the parameters to determine if initialization callbacks
396 for (i = 0; i < method->nparams; ++i)
398 if (method->param[i].val.type != POS_VALUE
399 && (method->param[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
404 /* Check that the callbacks required by the parameters are present */
405 if (bNeedInit && !method->init)
407 report_error(fp, method->name, "error: init should be provided");
414 * Checks the validity of a selection method.
416 * \param[in] fp File handle to use for diagnostic messages
418 * \param[in,out] method Method to check.
419 * \param[in] symtab Symbol table (used for checking overlaps).
421 * Checks the validity of the given selection method data structure
422 * that does not have \ref SMETH_MODIFIER set.
423 * If you remove a check, please make sure that the selection parser,
424 * compiler, and evaluation functions can deal with the method.
426 static bool check_method(FILE* fp, gmx_ana_selmethod_t* method, const gmx::SelectionParserSymbolTable& symtab)
431 if (method->type == NO_VALUE)
433 report_error(fp, method->name, "error: no value type specified");
436 if (method->type == STR_VALUE && method->nparams > 0)
438 report_error(fp, method->name, "error: evaluates to a string but is not a keyword");
442 if (method->type == GROUP_VALUE)
444 /* Group methods should always have SMETH_SINGLEVAL,
445 * so set it for convenience. */
446 method->flags |= SMETH_SINGLEVAL;
447 /* Check that conflicting flags are not present. */
448 if (method->flags & SMETH_VARNUMVAL)
450 report_error(fp, method->name,
451 "error: SMETH_VARNUMVAL cannot be set for group-valued methods");
457 if ((method->flags & SMETH_SINGLEVAL) && (method->flags & SMETH_VARNUMVAL))
459 report_error(fp, method->name, "error: SMETH_SINGLEVAL and SMETH_VARNUMVAL both set");
463 if ((method->flags & SMETH_CHARVAL) && method->type != STR_VALUE)
465 report_error(fp, method->name,
466 "error: SMETH_CHARVAL can only be specified for STR_VALUE methods");
469 /* Check the parameters */
470 if (!check_params(fp, method->name, method->nparams, method->param, symtab))
474 /* Check the callback pointers */
475 if (!check_callbacks(fp, method))
484 * Checks the validity of a selection modifier method.
486 * \param[in] fp File handle to use for diagnostic messages
488 * \param[in,out] method Method to check.
489 * \param[in] symtab Symbol table (used for checking overlaps).
491 * Checks the validity of the given selection method data structure
492 * that has \ref SMETH_MODIFIER set.
493 * If you remove a check, please make sure that the selection parser,
494 * compiler, and evaluation functions can deal with the method.
496 static bool check_modifier(FILE* fp, gmx_ana_selmethod_t* method, const gmx::SelectionParserSymbolTable& symtab)
501 if (method->type != NO_VALUE && method->type != POS_VALUE)
503 report_error(fp, method->name, "error: modifier should have type POS_VALUE or NO_VALUE");
507 if (method->flags & (SMETH_SINGLEVAL | SMETH_VARNUMVAL))
509 report_error(fp, method->name,
510 "error: modifier should not have SMETH_SINGLEVAL or SMETH_VARNUMVAL set");
513 /* Check the parameters */
514 /* The first parameter is skipped */
515 if (!check_params(fp, method->name, method->nparams - 1, method->param + 1, symtab))
519 /* Check the callback pointers */
520 if (!check_callbacks(fp, method))
526 report_error(fp, method->name, "error: modifier should not have update");
529 if (method->type == POS_VALUE && !method->pupdate)
531 report_error(fp, method->name, "error: evaluation function missing");
539 * \param[in,out] symtab Symbol table to register the method to.
540 * \param[in] name Name under which the method should be registered.
541 * \param[in] method Method to register.
542 * \returns 0 on success, -1 if there was something wrong with the
545 * \p name does not need to match the name of the method, and the same
546 * method can be registered multiple times under different names.
547 * If \p name equals some previously registered name,
548 * an error message is printed and the method is not registered.
550 * The function also performs some sanity checking on the input method,
551 * and refuses to register it if there are problems.
552 * Some problems only generate warnings.
553 * All problems are described to \p stderr.
555 int gmx_ana_selmethod_register(gmx::SelectionParserSymbolTable* symtab,
557 gmx_ana_selmethod_t* method)
561 /* Check the method */
562 if (method->flags & SMETH_MODIFIER)
564 bOk = check_modifier(stderr, method, *symtab);
568 bOk = check_method(stderr, method, *symtab);
570 /* Try to register the method if everything is ok */
575 symtab->addMethod(name, method);
577 catch (const gmx::APIError& ex)
579 report_error(stderr, name, "%s", ex.what());
585 report_error(stderr, name, "warning: not registered");
592 * \param[in,out] symtab Symbol table to register the methods to.
593 * \returns 0 on success, -1 if any of the default methods could not be
596 int gmx_ana_selmethod_register_defaults(gmx::SelectionParserSymbolTable* symtab)
598 /* Array of selection methods defined in the library. */
599 const t_register_method smtable_def[] = {
600 { nullptr, &sm_cog }, { nullptr, &sm_com },
602 { nullptr, &sm_all }, { nullptr, &sm_none },
603 { nullptr, &sm_atomnr }, { nullptr, &sm_resnr },
604 { "resid", &sm_resnr }, { nullptr, &sm_resindex },
605 { "residue", &sm_resindex }, { nullptr, &sm_molindex },
606 { "mol", &sm_molindex }, { "molecule", &sm_molindex },
607 { nullptr, &sm_atomname }, { "name", &sm_atomname },
608 { nullptr, &sm_pdbatomname }, { "pdbname", &sm_pdbatomname },
609 { nullptr, &sm_atomtype }, { "type", &sm_atomtype },
610 { nullptr, &sm_resname }, { nullptr, &sm_insertcode },
611 { nullptr, &sm_chain }, { nullptr, &sm_mass },
612 { nullptr, &sm_charge }, { nullptr, &sm_altloc },
613 { nullptr, &sm_occupancy }, { nullptr, &sm_betafactor },
614 { "beta", &sm_betafactor }, { nullptr, &sm_x },
615 { nullptr, &sm_y }, { nullptr, &sm_z },
617 { nullptr, &sm_distance }, { "dist", &sm_distance },
618 { nullptr, &sm_mindistance }, { "mindist", &sm_mindistance },
619 { nullptr, &sm_within }, { nullptr, &sm_insolidangle },
620 { nullptr, &sm_same },
622 { nullptr, &sm_merge }, { nullptr, &sm_plus },
623 { nullptr, &sm_permute },
630 for (int i = 0; i < asize(smtable_def); ++i)
632 gmx_ana_selmethod_t* method = smtable_def[i].method;
634 if (smtable_def[i].name == nullptr)
636 rc = gmx_ana_selmethod_register(symtab, method->name, method);
640 rc = gmx_ana_selmethod_register(symtab, smtable_def[i].name, method);
651 * \param[in] name Name of the parameter to search.
652 * \param[in] method Method to search for the parameter.
653 * \returns Pointer to the parameter in the
654 * \ref gmx_ana_selmethod_t::param "method->param" array,
655 * or NULL if no parameter with name \p name was found.
657 * This is a simple wrapper for gmx_ana_selparam_find().
659 gmx_ana_selparam_t* gmx_ana_selmethod_find_param(const char* name, gmx_ana_selmethod_t* method)
661 return gmx_ana_selparam_find(name, method->nparams, method->param);