2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012, 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.
37 * Implements functions in selmethod.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
45 #include "gromacs/legacyheaders/macros.h"
46 #include "gromacs/legacyheaders/string2.h"
48 #include "gromacs/selection/selmethod.h"
49 #include "gromacs/utility/exceptions.h"
54 * These global variables cannot be const because gmx_ana_selmethod_register()
55 * modifies them to set some defaults. This is a small price to pay for the
56 * convenience of not having to remember exactly how the selection compiler
57 * expects the structures to be filled, and even more so if the expectations
58 * change. Also, even if the gmx_ana_selmethod_t structures were made const,
59 * the parameters could not be without typecasts somewhere, because the param
60 * field in gmx_ana_selmethod_t cannot be declared const.
62 * Even though the variables may be modified, this should be thread-safe as
63 * modifications are done only in gmx_ana_selmethod_register(), and it should
64 * work even if called more than once for the same structure, and even if
65 * called concurrently from multiple threads (as long as the selection
66 * collection is not the same).
68 * All of these problems should go away if/when the selection methods are
69 * implemented as C++ classes.
73 extern gmx_ana_selmethod_t sm_cog;
74 extern gmx_ana_selmethod_t sm_com;
75 /* From sm_simple.c */
76 extern gmx_ana_selmethod_t sm_all;
77 extern gmx_ana_selmethod_t sm_none;
78 extern gmx_ana_selmethod_t sm_atomnr;
79 extern gmx_ana_selmethod_t sm_resnr;
80 extern gmx_ana_selmethod_t sm_resindex;
81 extern gmx_ana_selmethod_t sm_molindex;
82 extern gmx_ana_selmethod_t sm_atomname;
83 extern gmx_ana_selmethod_t sm_pdbatomname;
84 extern gmx_ana_selmethod_t sm_atomtype;
85 extern gmx_ana_selmethod_t sm_resname;
86 extern gmx_ana_selmethod_t sm_insertcode;
87 extern gmx_ana_selmethod_t sm_chain;
88 extern gmx_ana_selmethod_t sm_mass;
89 extern gmx_ana_selmethod_t sm_charge;
90 extern gmx_ana_selmethod_t sm_altloc;
91 extern gmx_ana_selmethod_t sm_occupancy;
92 extern gmx_ana_selmethod_t sm_betafactor;
93 extern gmx_ana_selmethod_t sm_x;
94 extern gmx_ana_selmethod_t sm_y;
95 extern gmx_ana_selmethod_t sm_z;
96 /* From sm_distance.c */
97 extern gmx_ana_selmethod_t sm_distance;
98 extern gmx_ana_selmethod_t sm_mindistance;
99 extern gmx_ana_selmethod_t sm_within;
100 /* From sm_insolidangle.c */
101 extern gmx_ana_selmethod_t sm_insolidangle;
103 extern gmx_ana_selmethod_t sm_same;
105 /* From sm_merge.c */
106 extern gmx_ana_selmethod_t sm_merge;
107 extern gmx_ana_selmethod_t sm_plus;
108 /* From sm_permute.c */
109 extern gmx_ana_selmethod_t sm_permute;
112 * Helper structure for defining selection methods.
116 * Name to register the method under.
118 * If NULL, use the actual name of the method.
119 * This field is used for defining synonyms.
122 /** Method data structure to register. */
123 gmx_ana_selmethod_t *method;
126 /** Array of selection methods defined in the library. */
127 static const t_register_method smtable_def[] = {
135 {"resid", &sm_resnr},
136 {NULL, &sm_resindex},
137 {"residue", &sm_resindex},
138 {NULL, &sm_molindex},
139 {"mol", &sm_molindex},
140 {"molecule", &sm_molindex},
141 {NULL, &sm_atomname},
142 {"name", &sm_atomname},
143 {NULL, &sm_pdbatomname},
144 {"pdbname", &sm_pdbatomname},
145 {NULL, &sm_atomtype},
146 {"type", &sm_atomtype},
148 {NULL, &sm_insertcode},
153 {NULL, &sm_occupancy},
154 {NULL, &sm_betafactor},
159 {NULL, &sm_distance},
160 {NULL, &sm_mindistance},
162 {NULL, &sm_insolidangle},
171 * Convenience function for reporting errors found in selection methods.
174 report_error(FILE *fp, const char *name, const char *fmt, ...)
180 fprintf(fp, "selection method '%s': ", name);
181 vfprintf(fp, fmt, ap);
188 * Convenience function for reporting errors found in selection method parameters.
191 report_param_error(FILE *fp, const char *mname, const char *pname,
192 const char *fmt, ...)
198 fprintf(fp, "selection method '%s': parameter '%s': ", mname, pname);
199 vfprintf(fp, fmt, ap);
206 * Checks the validity of parameters.
208 * \param[in] fp File handle to use for diagnostic messages
210 * \param[in] name Name of the method (used for error messages).
211 * \param[in] nparams Number of parameters in \p param.
212 * \param[in,out] param Parameter array
213 * (only the \c flags field of boolean parameters may be modified).
214 * \param[in] symtab Symbol table (used for checking overlaps).
215 * \returns true if there are no problems with the parameters,
218 * This function performs some checks common to both check_method() and
220 * The purpose of these checks is to ensure that the selection parser does not
221 * need to check for the validity of the parameters at each turn, and to
222 * report programming errors as early as possible.
223 * If you remove a check, make sure that the parameter parser can handle the
224 * resulting parameters.
227 check_params(FILE *fp, const char *name, int nparams, gmx_ana_selparam_t param[],
228 const gmx::SelectionParserSymbolTable &symtab)
233 if (nparams > 0 && !param)
235 report_error(fp, name, "error: missing parameter data");
238 if (nparams == 0 && param)
240 report_error(fp, name, "warning: parameter data unused because nparams=0");
242 /* Check each parameter */
243 for (i = 0; i < nparams; ++i)
245 /* Check that there is at most one NULL name, in the beginning */
246 if (param[i].name == NULL && i > 0)
248 report_error(fp, name, "error: NULL parameter should be the first one");
252 /* Check for duplicates */
253 for (j = 0; j < i; ++j)
255 if (param[j].name == NULL)
259 if (!gmx_strcasecmp(param[i].name, param[j].name))
261 report_error(fp, name, "error: duplicate parameter name '%s'", param[i].name);
267 if (param[i].flags & SPAR_SET)
269 report_param_error(fp, name, param[i].name, "warning: flag SPAR_SET is set");
270 param[i].flags &= ~SPAR_SET;
272 if (param[i].flags & SPAR_RANGES)
274 if (param[i].val.type != INT_VALUE && param[i].val.type != REAL_VALUE)
276 report_param_error(fp, name, param[i].name, "error: SPAR_RANGES cannot be set for a non-numeric parameter");
279 if (param[i].flags & SPAR_DYNAMIC)
281 report_param_error(fp, name, param[i].name, "warning: SPAR_DYNAMIC does not have effect with SPAR_RANGES");
282 param[i].flags &= ~SPAR_DYNAMIC;
284 if (!(param[i].flags & SPAR_VARNUM) && param[i].val.nr != 1)
286 report_param_error(fp, name, param[i].name, "error: range should take either one or an arbitrary number of values");
289 if (param[i].flags & SPAR_ATOMVAL)
291 report_param_error(fp, name, param[i].name, "error: SPAR_RANGES and SPAR_ATOMVAL both set");
295 if ((param[i].flags & SPAR_VARNUM) && (param[i].flags & SPAR_ATOMVAL))
297 report_param_error(fp, name, param[i].name, "error: SPAR_VARNUM and SPAR_ATOMVAL both set");
300 if (param[i].flags & SPAR_ENUMVAL)
302 if (param[i].val.type != STR_VALUE)
304 report_param_error(fp, name, param[i].name, "error: SPAR_ENUMVAL can only be set for string parameters");
307 if (param[i].val.nr != 1)
309 report_param_error(fp, name, param[i].name, "error: SPAR_ENUMVAL parameters should take exactly one value");
312 if (param[i].flags & (SPAR_DYNAMIC | SPAR_VARNUM | SPAR_ATOMVAL))
314 report_param_error(fp, name, param[i].name, "error: only SPAR_OPTIONAL supported with SPAR_ENUMVAL");
318 /* Check boolean parameters */
319 if (param[i].val.type == NO_VALUE)
321 if (param[i].val.nr != 0)
323 report_param_error(fp, name, param[i].name, "error: number of values should be zero for boolean parameters");
326 /* The boolean parameters should always be optional, so set the
327 * flag for convenience. */
328 param[i].flags |= SPAR_OPTIONAL;
329 /* Any other flags should not be specified */
330 if (param[i].flags & ~SPAR_OPTIONAL)
332 report_param_error(fp, name, param[i].name, "error: boolean parameter should not have any flags set");
337 if (param[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL))
339 if (param[i].val.nr != -1)
341 report_param_error(fp, name, param[i].name, "warning: val.nr is not -1 although SPAR_VARNUM/SPAR_ATOMVAL is set");
343 param[i].val.nr = -1;
345 else if (param[i].val.type != NO_VALUE)
347 if (param[i].val.nr <= 0)
349 report_param_error(fp, name, param[i].name, "error: val.nr <= 0");
353 /* Check that the value pointer is NULL */
354 if (param[i].nvalptr != NULL)
356 report_param_error(fp, name, param[i].name, "warning: nvalptr is set");
358 if (param[i].val.u.ptr != NULL && !(param[i].flags & SPAR_ENUMVAL))
360 report_param_error(fp, name, param[i].name, "warning: value pointer is set");
362 /* Check that the name contains only valid characters */
363 if (param[i].name == NULL)
367 if (!isalpha(param[i].name[0]))
369 report_param_error(fp, name, param[i].name, "error: name does not begin with a letter");
373 for (j = 1; param[i].name[j] != 0; ++j)
375 if (param[i].name[j] != '_' && !isalnum(param[i].name[j]))
377 report_param_error(fp, name, param[i].name, "error: name contains non-alphanumeric characters");
382 if (param[i].name[j] != 0)
386 /* Check that the name does not conflict with a method */
387 if (symtab.findSymbol(param[i].name, true))
389 report_param_error(fp, name, param[i].name, "error: name conflicts with another method or a keyword");
392 } /* End of parameter loop */
393 /* Check parameters of existing methods */
394 gmx::SelectionParserSymbolIterator symbol
395 = symtab.beginIterator(gmx::SelectionParserSymbol::MethodSymbol);
396 while (symbol != symtab.endIterator())
398 gmx_ana_selmethod_t *method = symbol->methodValue();
399 gmx_ana_selparam_t *param =
400 gmx_ana_selmethod_find_param(name, method);
403 report_param_error(fp, method->name, param->name, "error: name conflicts with another method or a keyword");
412 * Checks the validity of selection method callback functions.
414 * \param[in] fp File handle to use for diagnostic messages
416 * \param[in] method The method to check.
417 * \returns true if there are no problems, false otherwise.
419 * This function performs some checks common to both check_method() and
421 * This function checks that all the required callbacks are defined, i.e.,
422 * not NULL, to find programming errors.
425 check_callbacks(FILE *fp, gmx_ana_selmethod_t *method)
431 /* Make some checks on init_data and free */
432 if (method->nparams > 0 && !method->init_data)
434 report_error(fp, method->name, "error: init_data should be provided because the method has parameters");
437 if (method->free && !method->init_data)
439 report_error(fp, method->name, "warning: free is not used because of missing init_data");
441 /* Check presence of outinit for position-valued methods */
442 if (method->type == POS_VALUE && !method->outinit)
444 report_error(fp, method->name, "error: outinit should be provided because the method has POS_VALUE");
447 /* Check presence of outinit for variable output count methods */
448 if ((method->flags & SMETH_VARNUMVAL) && !method->outinit)
450 report_error(fp, method->name, "error: outinit should be provided because the method has SMETH_VARNUMVAL");
453 /* Warn of dynamic callbacks in static methods */
454 if (!(method->flags & SMETH_MODIFIER))
456 if (method->pupdate && !(method->flags & SMETH_DYNAMIC))
458 report_error(fp, method->name, "warning: pupdate not used because the method is static");
459 method->pupdate = NULL;
462 /* Check that there is an evaluation function */
463 if (method->type != NO_VALUE && !method->update && !method->pupdate)
465 report_error(fp, method->name, "error: evaluation function missing");
468 /* Loop through the parameters to determine if initialization callbacks
471 for (i = 0; i < method->nparams; ++i)
473 if (method->param[i].val.type != POS_VALUE
474 && (method->param[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
479 /* Check that the callbacks required by the parameters are present */
480 if (bNeedInit && !method->init)
482 report_error(fp, method->name, "error: init should be provided");
489 * Checks the validity of a selection method.
491 * \param[in] fp File handle to use for diagnostic messages
493 * \param[in,out] method Method to check.
494 * \param[in] symtab Symbol table (used for checking overlaps).
496 * Checks the validity of the given selection method data structure
497 * that does not have \ref SMETH_MODIFIER set.
498 * If you remove a check, please make sure that the selection parser,
499 * compiler, and evaluation functions can deal with the method.
502 check_method(FILE *fp, gmx_ana_selmethod_t *method,
503 const gmx::SelectionParserSymbolTable &symtab)
508 if (method->type == NO_VALUE)
510 report_error(fp, method->name, "error: no value type specified");
513 if (method->type == STR_VALUE && method->nparams > 0)
515 report_error(fp, method->name, "error: evaluates to a string but is not a keyword");
519 if (method->type == GROUP_VALUE)
521 /* Group methods should always have SMETH_SINGLEVAL,
522 * so set it for convenience. */
523 method->flags |= SMETH_SINGLEVAL;
524 /* Check that conflicting flags are not present. */
525 if (method->flags & SMETH_VARNUMVAL)
527 report_error(fp, method->name, "error: SMETH_VARNUMVAL cannot be set for group-valued methods");
533 if ((method->flags & SMETH_SINGLEVAL)
534 && (method->flags & SMETH_VARNUMVAL))
536 report_error(fp, method->name, "error: SMETH_SINGLEVAL and SMETH_VARNUMVAL both set");
540 if ((method->flags & SMETH_CHARVAL) && method->type != STR_VALUE)
542 report_error(fp, method->name, "error: SMETH_CHARVAL can only be specified for STR_VALUE methods");
545 /* Check the parameters */
546 if (!check_params(fp, method->name, method->nparams, method->param, symtab))
550 /* Check the callback pointers */
551 if (!check_callbacks(fp, method))
560 * Checks the validity of a selection modifier method.
562 * \param[in] fp File handle to use for diagnostic messages
564 * \param[in,out] method Method to check.
565 * \param[in] symtab Symbol table (used for checking overlaps).
567 * Checks the validity of the given selection method data structure
568 * that has \ref SMETH_MODIFIER set.
569 * If you remove a check, please make sure that the selection parser,
570 * compiler, and evaluation functions can deal with the method.
573 check_modifier(FILE *fp, gmx_ana_selmethod_t *method,
574 const gmx::SelectionParserSymbolTable &symtab)
579 if (method->type != NO_VALUE && method->type != POS_VALUE)
581 report_error(fp, method->name, "error: modifier should have type POS_VALUE or NO_VALUE");
585 if (method->flags & (SMETH_SINGLEVAL | SMETH_VARNUMVAL))
587 report_error(fp, method->name, "error: modifier should not have SMETH_SINGLEVAL or SMETH_VARNUMVAL set");
590 /* Check the parameters */
591 /* The first parameter is skipped */
592 if (!check_params(fp, method->name, method->nparams-1, method->param+1, symtab))
596 /* Check the callback pointers */
597 if (!check_callbacks(fp, method))
603 report_error(fp, method->name, "error: modifier should not have update");
606 if (method->type == POS_VALUE && !method->pupdate)
608 report_error(fp, method->name, "error: evaluation function missing");
616 * \param[in,out] symtab Symbol table to register the method to.
617 * \param[in] name Name under which the method should be registered.
618 * \param[in] method Method to register.
619 * \returns 0 on success, EINVAL if there was something wrong with the
622 * \p name does not need to match the name of the method, and the same
623 * method can be registered multiple times under different names.
624 * If \p name equals some previously registered name,
625 * an error message is printed and the method is not registered.
627 * The function also performs some sanity checking on the input method,
628 * and refuses to register it if there are problems.
629 * Some problems only generate warnings.
630 * All problems are described to \p stderr.
633 gmx_ana_selmethod_register(gmx::SelectionParserSymbolTable *symtab,
634 const char *name, gmx_ana_selmethod_t *method)
638 /* Check the method */
639 if (method->flags & SMETH_MODIFIER)
641 bOk = check_modifier(stderr, method, *symtab);
645 bOk = check_method(stderr, method, *symtab);
647 /* Try to register the method if everything is ok */
652 symtab->addMethod(name, method);
654 catch (const gmx::APIError &ex)
656 report_error(stderr, name, ex.what());
662 report_error(stderr, name, "warning: not registered");
669 * \param[in,out] symtab Symbol table to register the methods to.
670 * \returns 0 on success, -1 if any of the default methods could not be
674 gmx_ana_selmethod_register_defaults(gmx::SelectionParserSymbolTable *symtab)
681 for (i = 0; i < asize(smtable_def); ++i)
683 gmx_ana_selmethod_t *method = smtable_def[i].method;
685 if (smtable_def[i].name == NULL)
687 rc = gmx_ana_selmethod_register(symtab, method->name, method);
691 rc = gmx_ana_selmethod_register(symtab, smtable_def[i].name, method);
702 * \param[in] name Name of the parameter to search.
703 * \param[in] method Method to search for the parameter.
704 * \returns Pointer to the parameter in the
705 * \ref gmx_ana_selmethod_t::param "method->param" array,
706 * or NULL if no parameter with name \p name was found.
708 * This is a simple wrapper for gmx_ana_selparam_find().
711 gmx_ana_selmethod_find_param(const char *name, gmx_ana_selmethod_t *method)
713 return gmx_ana_selparam_find(name, method->nparams, method->param);