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 functions in selmethod.h.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_selection
41 #include "gromacs/legacyheaders/macros.h"
42 #include "gromacs/legacyheaders/string2.h"
44 #include "gromacs/selection/selmethod.h"
45 #include "gromacs/utility/exceptions.h"
50 * These global variables cannot be const because gmx_ana_selmethod_register()
51 * modifies them to set some defaults. This is a small price to pay for the
52 * convenience of not having to remember exactly how the selection compiler
53 * expects the structures to be filled, and even more so if the expectations
54 * change. Also, even if the gmx_ana_selmethod_t structures were made const,
55 * the parameters could not be without typecasts somewhere, because the param
56 * field in gmx_ana_selmethod_t cannot be declared const.
58 * Even though the variables may be modified, this should be thread-safe as
59 * modifications are done only in gmx_ana_selmethod_register(), and it should
60 * work even if called more than once for the same structure, and even if
61 * called concurrently from multiple threads (as long as the selection
62 * collection is not the same).
64 * All of these problems should go away if/when the selection methods are
65 * implemented as C++ classes.
69 extern gmx_ana_selmethod_t sm_cog;
70 extern gmx_ana_selmethod_t sm_com;
71 /* From sm_simple.c */
72 extern gmx_ana_selmethod_t sm_all;
73 extern gmx_ana_selmethod_t sm_none;
74 extern gmx_ana_selmethod_t sm_atomnr;
75 extern gmx_ana_selmethod_t sm_resnr;
76 extern gmx_ana_selmethod_t sm_resindex;
77 extern gmx_ana_selmethod_t sm_molindex;
78 extern gmx_ana_selmethod_t sm_atomname;
79 extern gmx_ana_selmethod_t sm_pdbatomname;
80 extern gmx_ana_selmethod_t sm_atomtype;
81 extern gmx_ana_selmethod_t sm_resname;
82 extern gmx_ana_selmethod_t sm_insertcode;
83 extern gmx_ana_selmethod_t sm_chain;
84 extern gmx_ana_selmethod_t sm_mass;
85 extern gmx_ana_selmethod_t sm_charge;
86 extern gmx_ana_selmethod_t sm_altloc;
87 extern gmx_ana_selmethod_t sm_occupancy;
88 extern gmx_ana_selmethod_t sm_betafactor;
89 extern gmx_ana_selmethod_t sm_x;
90 extern gmx_ana_selmethod_t sm_y;
91 extern gmx_ana_selmethod_t sm_z;
92 /* From sm_distance.c */
93 extern gmx_ana_selmethod_t sm_distance;
94 extern gmx_ana_selmethod_t sm_mindistance;
95 extern gmx_ana_selmethod_t sm_within;
96 /* From sm_insolidangle.c */
97 extern gmx_ana_selmethod_t sm_insolidangle;
99 extern gmx_ana_selmethod_t sm_same;
101 /* From sm_merge.c */
102 extern gmx_ana_selmethod_t sm_merge;
103 extern gmx_ana_selmethod_t sm_plus;
104 /* From sm_permute.c */
105 extern gmx_ana_selmethod_t sm_permute;
108 * Helper structure for defining selection methods.
112 * Name to register the method under.
114 * If NULL, use the actual name of the method.
115 * This field is used for defining synonyms.
118 /** Method data structure to register. */
119 gmx_ana_selmethod_t *method;
122 /** Array of selection methods defined in the library. */
123 static const t_register_method smtable_def[] = {
131 {"resid", &sm_resnr},
132 {NULL, &sm_resindex},
133 {"residue", &sm_resindex},
134 {NULL, &sm_molindex},
135 {"mol", &sm_molindex},
136 {"molecule", &sm_molindex},
137 {NULL, &sm_atomname},
138 {"name", &sm_atomname},
139 {NULL, &sm_pdbatomname},
140 {"pdbname", &sm_pdbatomname},
141 {NULL, &sm_atomtype},
142 {"type", &sm_atomtype},
144 {NULL, &sm_insertcode},
149 {NULL, &sm_occupancy},
150 {NULL, &sm_betafactor},
155 {NULL, &sm_distance},
156 {NULL, &sm_mindistance},
158 {NULL, &sm_insolidangle},
167 * Convenience function for reporting errors found in selection methods.
170 report_error(FILE *fp, const char *name, const char *fmt, ...)
176 fprintf(fp, "selection method '%s': ", name);
177 vfprintf(fp, fmt, ap);
184 * Convenience function for reporting errors found in selection method parameters.
187 report_param_error(FILE *fp, const char *mname, const char *pname,
188 const char *fmt, ...)
194 fprintf(fp, "selection method '%s': parameter '%s': ", mname, pname);
195 vfprintf(fp, fmt, ap);
202 * Checks the validity of parameters.
204 * \param[in] fp File handle to use for diagnostic messages
206 * \param[in] name Name of the method (used for error messages).
207 * \param[in] nparams Number of parameters in \p param.
208 * \param[in,out] param Parameter array
209 * (only the \c flags field of boolean parameters may be modified).
210 * \param[in] symtab Symbol table (used for checking overlaps).
211 * \returns true if there are no problems with the parameters,
214 * This function performs some checks common to both check_method() and
216 * The purpose of these checks is to ensure that the selection parser does not
217 * need to check for the validity of the parameters at each turn, and to
218 * report programming errors as early as possible.
219 * If you remove a check, make sure that the parameter parser can handle the
220 * resulting parameters.
223 check_params(FILE *fp, const char *name, int nparams, gmx_ana_selparam_t param[],
224 const gmx::SelectionParserSymbolTable &symtab)
229 if (nparams > 0 && !param)
231 report_error(fp, name, "error: missing parameter data");
234 if (nparams == 0 && param)
236 report_error(fp, name, "warning: parameter data unused because nparams=0");
238 /* Check each parameter */
239 for (i = 0; i < nparams; ++i)
241 /* Check that there is at most one NULL name, in the beginning */
242 if (param[i].name == NULL && i > 0)
244 report_error(fp, name, "error: NULL parameter should be the first one");
248 /* Check for duplicates */
249 for (j = 0; j < i; ++j)
251 if (param[j].name == NULL)
255 if (!gmx_strcasecmp(param[i].name, param[j].name))
257 report_error(fp, name, "error: duplicate parameter name '%s'", param[i].name);
263 if (param[i].flags & SPAR_SET)
265 report_param_error(fp, name, param[i].name, "warning: flag SPAR_SET is set");
266 param[i].flags &= ~SPAR_SET;
268 if (param[i].flags & SPAR_RANGES)
270 if (param[i].val.type != INT_VALUE && param[i].val.type != REAL_VALUE)
272 report_param_error(fp, name, param[i].name, "error: SPAR_RANGES cannot be set for a non-numeric parameter");
275 if (param[i].flags & SPAR_DYNAMIC)
277 report_param_error(fp, name, param[i].name, "warning: SPAR_DYNAMIC does not have effect with SPAR_RANGES");
278 param[i].flags &= ~SPAR_DYNAMIC;
280 if (!(param[i].flags & SPAR_VARNUM) && param[i].val.nr != 1)
282 report_param_error(fp, name, param[i].name, "error: range should take either one or an arbitrary number of values");
285 if (param[i].flags & SPAR_ATOMVAL)
287 report_param_error(fp, name, param[i].name, "error: SPAR_RANGES and SPAR_ATOMVAL both set");
291 if ((param[i].flags & SPAR_VARNUM) && (param[i].flags & SPAR_ATOMVAL))
293 report_param_error(fp, name, param[i].name, "error: SPAR_VARNUM and SPAR_ATOMVAL both set");
296 if (param[i].flags & SPAR_ENUMVAL)
298 if (param[i].val.type != STR_VALUE)
300 report_param_error(fp, name, param[i].name, "error: SPAR_ENUMVAL can only be set for string parameters");
303 if (param[i].val.nr != 1)
305 report_param_error(fp, name, param[i].name, "error: SPAR_ENUMVAL parameters should take exactly one value");
308 if (param[i].flags & (SPAR_DYNAMIC | SPAR_VARNUM | SPAR_ATOMVAL))
310 report_param_error(fp, name, param[i].name, "error: only SPAR_OPTIONAL supported with SPAR_ENUMVAL");
314 /* Check boolean parameters */
315 if (param[i].val.type == NO_VALUE)
317 if (param[i].val.nr != 0)
319 report_param_error(fp, name, param[i].name, "error: number of values should be zero for boolean parameters");
322 /* The boolean parameters should always be optional, so set the
323 * flag for convenience. */
324 param[i].flags |= SPAR_OPTIONAL;
325 /* Any other flags should not be specified */
326 if (param[i].flags & ~SPAR_OPTIONAL)
328 report_param_error(fp, name, param[i].name, "error: boolean parameter should not have any flags set");
333 if (param[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL))
335 if (param[i].val.nr != -1)
337 report_param_error(fp, name, param[i].name, "warning: val.nr is not -1 although SPAR_VARNUM/SPAR_ATOMVAL is set");
339 param[i].val.nr = -1;
341 else if (param[i].val.type != NO_VALUE)
343 if (param[i].val.nr <= 0)
345 report_param_error(fp, name, param[i].name, "error: val.nr <= 0");
349 /* Check that the value pointer is NULL */
350 if (param[i].nvalptr != NULL)
352 report_param_error(fp, name, param[i].name, "warning: nvalptr is set");
354 if (param[i].val.u.ptr != NULL && !(param[i].flags & SPAR_ENUMVAL))
356 report_param_error(fp, name, param[i].name, "warning: value pointer is set");
358 /* Check that the name contains only valid characters */
359 if (param[i].name == NULL)
363 if (!isalpha(param[i].name[0]))
365 report_param_error(fp, name, param[i].name, "error: name does not begin with a letter");
369 for (j = 1; param[i].name[j] != 0; ++j)
371 if (param[i].name[j] != '_' && !isalnum(param[i].name[j]))
373 report_param_error(fp, name, param[i].name, "error: name contains non-alphanumeric characters");
378 if (param[i].name[j] != 0)
382 /* Check that the name does not conflict with a method */
383 if (symtab.findSymbol(param[i].name, true))
385 report_param_error(fp, name, param[i].name, "error: name conflicts with another method or a keyword");
388 } /* End of parameter loop */
389 /* Check parameters of existing methods */
390 gmx::SelectionParserSymbolIterator symbol
391 = symtab.beginIterator(gmx::SelectionParserSymbol::MethodSymbol);
392 while (symbol != symtab.endIterator())
394 gmx_ana_selmethod_t *method = symbol->methodValue();
395 gmx_ana_selparam_t *param =
396 gmx_ana_selmethod_find_param(name, method);
399 report_param_error(fp, method->name, param->name, "error: name conflicts with another method or a keyword");
408 * Checks the validity of selection method callback functions.
410 * \param[in] fp File handle to use for diagnostic messages
412 * \param[in] method The method to check.
413 * \returns true if there are no problems, false otherwise.
415 * This function performs some checks common to both check_method() and
417 * This function checks that all the required callbacks are defined, i.e.,
418 * not NULL, to find programming errors.
421 check_callbacks(FILE *fp, gmx_ana_selmethod_t *method)
427 /* Make some checks on init_data and free */
428 if (method->nparams > 0 && !method->init_data)
430 report_error(fp, method->name, "error: init_data should be provided because the method has parameters");
433 if (method->free && !method->init_data)
435 report_error(fp, method->name, "warning: free is not used because of missing init_data");
437 /* Check presence of outinit for position-valued methods */
438 if (method->type == POS_VALUE && !method->outinit)
440 report_error(fp, method->name, "error: outinit should be provided because the method has POS_VALUE");
443 /* Check presence of outinit for variable output count methods */
444 if ((method->flags & SMETH_VARNUMVAL) && !method->outinit)
446 report_error(fp, method->name, "error: outinit should be provided because the method has SMETH_VARNUMVAL");
449 /* Warn of dynamic callbacks in static methods */
450 if (!(method->flags & SMETH_MODIFIER))
452 if (method->pupdate && !(method->flags & SMETH_DYNAMIC))
454 report_error(fp, method->name, "warning: pupdate not used because the method is static");
455 method->pupdate = NULL;
458 /* Check that there is an evaluation function */
459 if (method->type != NO_VALUE && !method->update && !method->pupdate)
461 report_error(fp, method->name, "error: evaluation function missing");
464 /* Loop through the parameters to determine if initialization callbacks
467 for (i = 0; i < method->nparams; ++i)
469 if (method->param[i].val.type != POS_VALUE
470 && (method->param[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
475 /* Check that the callbacks required by the parameters are present */
476 if (bNeedInit && !method->init)
478 report_error(fp, method->name, "error: init should be provided");
485 * Checks the validity of a selection method.
487 * \param[in] fp File handle to use for diagnostic messages
489 * \param[in,out] method Method to check.
490 * \param[in] symtab Symbol table (used for checking overlaps).
492 * Checks the validity of the given selection method data structure
493 * that does not have \ref SMETH_MODIFIER set.
494 * If you remove a check, please make sure that the selection parser,
495 * compiler, and evaluation functions can deal with the method.
498 check_method(FILE *fp, gmx_ana_selmethod_t *method,
499 const gmx::SelectionParserSymbolTable &symtab)
504 if (method->type == NO_VALUE)
506 report_error(fp, method->name, "error: no value type specified");
509 if (method->type == STR_VALUE && method->nparams > 0)
511 report_error(fp, method->name, "error: evaluates to a string but is not a keyword");
515 if (method->type == GROUP_VALUE)
517 /* Group methods should always have SMETH_SINGLEVAL,
518 * so set it for convenience. */
519 method->flags |= SMETH_SINGLEVAL;
520 /* Check that conflicting flags are not present. */
521 if (method->flags & SMETH_VARNUMVAL)
523 report_error(fp, method->name, "error: SMETH_VARNUMVAL cannot be set for group-valued methods");
529 if ((method->flags & SMETH_SINGLEVAL)
530 && (method->flags & SMETH_VARNUMVAL))
532 report_error(fp, method->name, "error: SMETH_SINGLEVAL and SMETH_VARNUMVAL both set");
536 if ((method->flags & SMETH_CHARVAL) && method->type != STR_VALUE)
538 report_error(fp, method->name, "error: SMETH_CHARVAL can only be specified for STR_VALUE methods");
541 /* Check the parameters */
542 if (!check_params(fp, method->name, method->nparams, method->param, symtab))
546 /* Check the callback pointers */
547 if (!check_callbacks(fp, method))
556 * Checks the validity of a selection modifier method.
558 * \param[in] fp File handle to use for diagnostic messages
560 * \param[in,out] method Method to check.
561 * \param[in] symtab Symbol table (used for checking overlaps).
563 * Checks the validity of the given selection method data structure
564 * that has \ref SMETH_MODIFIER set.
565 * If you remove a check, please make sure that the selection parser,
566 * compiler, and evaluation functions can deal with the method.
569 check_modifier(FILE *fp, gmx_ana_selmethod_t *method,
570 const gmx::SelectionParserSymbolTable &symtab)
575 if (method->type != NO_VALUE && method->type != POS_VALUE)
577 report_error(fp, method->name, "error: modifier should have type POS_VALUE or NO_VALUE");
581 if (method->flags & (SMETH_SINGLEVAL | SMETH_VARNUMVAL))
583 report_error(fp, method->name, "error: modifier should not have SMETH_SINGLEVAL or SMETH_VARNUMVAL set");
586 /* Check the parameters */
587 /* The first parameter is skipped */
588 if (!check_params(fp, method->name, method->nparams-1, method->param+1, symtab))
592 /* Check the callback pointers */
593 if (!check_callbacks(fp, method))
599 report_error(fp, method->name, "error: modifier should not have update");
602 if (method->type == POS_VALUE && !method->pupdate)
604 report_error(fp, method->name, "error: evaluation function missing");
612 * \param[in,out] symtab Symbol table to register the method to.
613 * \param[in] name Name under which the method should be registered.
614 * \param[in] method Method to register.
615 * \returns 0 on success, EINVAL if there was something wrong with the
618 * \p name does not need to match the name of the method, and the same
619 * method can be registered multiple times under different names.
620 * If \p name equals some previously registered name,
621 * an error message is printed and the method is not registered.
623 * The function also performs some sanity checking on the input method,
624 * and refuses to register it if there are problems.
625 * Some problems only generate warnings.
626 * All problems are described to \p stderr.
629 gmx_ana_selmethod_register(gmx::SelectionParserSymbolTable *symtab,
630 const char *name, gmx_ana_selmethod_t *method)
634 /* Check the method */
635 if (method->flags & SMETH_MODIFIER)
637 bOk = check_modifier(stderr, method, *symtab);
641 bOk = check_method(stderr, method, *symtab);
643 /* Try to register the method if everything is ok */
648 symtab->addMethod(name, method);
650 catch (const gmx::APIError &ex)
652 report_error(stderr, name, ex.what());
658 report_error(stderr, name, "warning: not registered");
665 * \param[in,out] symtab Symbol table to register the methods to.
666 * \returns 0 on success, -1 if any of the default methods could not be
670 gmx_ana_selmethod_register_defaults(gmx::SelectionParserSymbolTable *symtab)
677 for (i = 0; i < asize(smtable_def); ++i)
679 gmx_ana_selmethod_t *method = smtable_def[i].method;
681 if (smtable_def[i].name == NULL)
683 rc = gmx_ana_selmethod_register(symtab, method->name, method);
687 rc = gmx_ana_selmethod_register(symtab, smtable_def[i].name, method);
698 * \param[in] name Name of the parameter to search.
699 * \param[in] method Method to search for the parameter.
700 * \returns Pointer to the parameter in the
701 * \ref gmx_ana_selmethod_t::param "method->param" array,
702 * or NULL if no parameter with name \p name was found.
704 * This is a simple wrapper for gmx_ana_selparam_find().
707 gmx_ana_selmethod_find_param(const char *name, gmx_ana_selmethod_t *method)
709 return gmx_ana_selparam_find(name, method->nparams, method->param);