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
48 #include "gromacs/selection/selmethod.h"
53 * These global variables cannot be const because gmx_ana_selmethod_register()
54 * modifies them to set some defaults. This is a small price to pay for the
55 * convenience of not having to remember exactly how the selection compiler
56 * expects the structures to be filled, and even more so if the expectations
57 * change. Also, even if the gmx_ana_selmethod_t structures were made const,
58 * the parameters could not be without typecasts somewhere, because the param
59 * field in gmx_ana_selmethod_t cannot be declared const.
61 * Even though the variables may be modified, this should be thread-safe as
62 * modifications are done only in gmx_ana_selmethod_register(), and it should
63 * work even if called more than once for the same structure, and even if
64 * called concurrently from multiple threads (as long as the selection
65 * collection is not the same).
67 * All of these problems should go away if/when the selection methods are
68 * implemented as C++ classes.
72 extern gmx_ana_selmethod_t sm_cog;
73 extern gmx_ana_selmethod_t sm_com;
74 /* From sm_simple.c */
75 extern gmx_ana_selmethod_t sm_all;
76 extern gmx_ana_selmethod_t sm_none;
77 extern gmx_ana_selmethod_t sm_atomnr;
78 extern gmx_ana_selmethod_t sm_resnr;
79 extern gmx_ana_selmethod_t sm_resindex;
80 extern gmx_ana_selmethod_t sm_molindex;
81 extern gmx_ana_selmethod_t sm_atomname;
82 extern gmx_ana_selmethod_t sm_atomtype;
83 extern gmx_ana_selmethod_t sm_resname;
84 extern gmx_ana_selmethod_t sm_insertcode;
85 extern gmx_ana_selmethod_t sm_chain;
86 extern gmx_ana_selmethod_t sm_mass;
87 extern gmx_ana_selmethod_t sm_charge;
88 extern gmx_ana_selmethod_t sm_altloc;
89 extern gmx_ana_selmethod_t sm_occupancy;
90 extern gmx_ana_selmethod_t sm_betafactor;
91 extern gmx_ana_selmethod_t sm_x;
92 extern gmx_ana_selmethod_t sm_y;
93 extern gmx_ana_selmethod_t sm_z;
94 /* From sm_distance.c */
95 extern gmx_ana_selmethod_t sm_distance;
96 extern gmx_ana_selmethod_t sm_mindistance;
97 extern gmx_ana_selmethod_t sm_within;
98 /* From sm_insolidangle.c */
99 extern gmx_ana_selmethod_t sm_insolidangle;
101 extern gmx_ana_selmethod_t sm_same;
103 /* From sm_merge.c */
104 extern gmx_ana_selmethod_t sm_merge;
105 extern gmx_ana_selmethod_t sm_plus;
106 /* From sm_permute.c */
107 extern gmx_ana_selmethod_t sm_permute;
110 * Helper structure for defining selection methods.
114 * Name to register the method under.
116 * If NULL, use the actual name of the method.
117 * This field is used for defining synonyms.
120 /** Method data structure to register. */
121 gmx_ana_selmethod_t *method;
124 /** Array of selection methods defined in the library. */
125 static const t_register_method smtable_def[] = {
133 {"resid", &sm_resnr},
134 {NULL, &sm_resindex},
135 {"residue", &sm_resindex},
136 {NULL, &sm_molindex},
137 {"mol", &sm_molindex},
138 {"molecule", &sm_molindex},
139 {NULL, &sm_atomname},
140 {NULL, &sm_atomtype},
142 {NULL, &sm_insertcode},
147 {NULL, &sm_occupancy},
148 {NULL, &sm_betafactor},
153 {NULL, &sm_distance},
154 {NULL, &sm_mindistance},
156 {NULL, &sm_insolidangle},
165 * Convenience function for reporting errors found in selection methods.
168 report_error(FILE *fp, const char *name, const char *fmt, ...)
174 fprintf(fp, "selection method '%s': ", name);
175 vfprintf(fp, fmt, ap);
182 * Convenience function for reporting errors found in selection method parameters.
185 report_param_error(FILE *fp, const char *mname, const char *pname,
186 const char *fmt, ...)
192 fprintf(fp, "selection method '%s': parameter '%s': ", mname, pname);
193 vfprintf(fp, fmt, ap);
200 * Checks the validity of parameters.
202 * \param[in] fp File handle to use for diagnostic messages
204 * \param[in] name Name of the method (used for error messages).
205 * \param[in] nparams Number of parameters in \p param.
206 * \param[in,out] param Parameter array
207 * (only the \c flags field of boolean parameters may be modified).
208 * \param[in] symtab Symbol table (used for checking overlaps).
209 * \returns true if there are no problems with the parameters,
212 * This function performs some checks common to both check_method() and
214 * The purpose of these checks is to ensure that the selection parser does not
215 * need to check for the validity of the parameters at each turn, and to
216 * report programming errors as early as possible.
217 * If you remove a check, make sure that the parameter parser can handle the
218 * resulting parameters.
221 check_params(FILE *fp, const char *name, int nparams, gmx_ana_selparam_t param[],
222 gmx_sel_symtab_t *symtab)
225 gmx_sel_symrec_t *sym;
228 if (nparams > 0 && !param)
230 report_error(fp, name, "error: missing parameter data");
233 if (nparams == 0 && param)
235 report_error(fp, name, "warning: parameter data unused because nparams=0");
237 /* Check each parameter */
238 for (i = 0; i < nparams; ++i)
240 /* Check that there is at most one NULL name, in the beginning */
241 if (param[i].name == NULL && i > 0)
243 report_error(fp, name, "error: NULL parameter should be the first one");
247 /* Check for duplicates */
248 for (j = 0; j < i; ++j)
250 if (param[j].name == NULL)
254 if (!gmx_strcasecmp(param[i].name, param[j].name))
256 report_error(fp, name, "error: duplicate parameter name '%s'", param[i].name);
262 if (param[i].flags & SPAR_SET)
264 report_param_error(fp, name, param[i].name, "warning: flag SPAR_SET is set");
265 param[i].flags &= ~SPAR_SET;
267 if (param[i].flags & SPAR_RANGES)
269 if (param[i].val.type != INT_VALUE && param[i].val.type != REAL_VALUE)
271 report_param_error(fp, name, param[i].name, "error: SPAR_RANGES cannot be set for a non-numeric parameter");
274 if (param[i].flags & SPAR_DYNAMIC)
276 report_param_error(fp, name, param[i].name, "warning: SPAR_DYNAMIC does not have effect with SPAR_RANGES");
277 param[i].flags &= ~SPAR_DYNAMIC;
279 if (!(param[i].flags & SPAR_VARNUM) && param[i].val.nr != 1)
281 report_param_error(fp, name, param[i].name, "error: range should take either one or an arbitrary number of values");
284 if (param[i].flags & SPAR_ATOMVAL)
286 report_param_error(fp, name, param[i].name, "error: SPAR_RANGES and SPAR_ATOMVAL both set");
290 if ((param[i].flags & SPAR_VARNUM) && (param[i].flags & SPAR_ATOMVAL))
292 report_param_error(fp, name, param[i].name, "error: SPAR_VARNUM and SPAR_ATOMVAL both set");
295 if (param[i].flags & SPAR_ENUMVAL)
297 if (param[i].val.type != STR_VALUE)
299 report_param_error(fp, name, param[i].name, "error: SPAR_ENUMVAL can only be set for string parameters");
302 if (param[i].val.nr != 1)
304 report_param_error(fp, name, param[i].name, "error: SPAR_ENUMVAL parameters should take exactly one value");
307 if (param[i].flags & (SPAR_DYNAMIC | SPAR_VARNUM | SPAR_ATOMVAL))
309 report_param_error(fp, name, param[i].name, "error: only SPAR_OPTIONAL supported with SPAR_ENUMVAL");
313 /* Check boolean parameters */
314 if (param[i].val.type == NO_VALUE)
316 if (param[i].val.nr != 0)
318 report_param_error(fp, name, param[i].name, "error: number of values should be zero for boolean parameters");
321 /* The boolean parameters should always be optional, so set the
322 * flag for convenience. */
323 param[i].flags |= SPAR_OPTIONAL;
324 /* Any other flags should not be specified */
325 if (param[i].flags & ~SPAR_OPTIONAL)
327 report_param_error(fp, name, param[i].name, "error: boolean parameter should not have any flags set");
332 if (param[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL))
334 if (param[i].val.nr != -1)
336 report_param_error(fp, name, param[i].name, "warning: val.nr is not -1 although SPAR_VARNUM/SPAR_ATOMVAL is set");
338 param[i].val.nr = -1;
340 else if (param[i].val.type != NO_VALUE)
342 if (param[i].val.nr <= 0)
344 report_param_error(fp, name, param[i].name, "error: val.nr <= 0");
348 /* Check that the value pointer is NULL */
349 if (param[i].nvalptr != NULL)
351 report_param_error(fp, name, param[i].name, "warning: nvalptr is set");
353 if (param[i].val.u.ptr != NULL && !(param[i].flags & SPAR_ENUMVAL))
355 report_param_error(fp, name, param[i].name, "warning: value pointer is set");
357 /* Check that the name contains only valid characters */
358 if (param[i].name == NULL)
362 if (!isalpha(param[i].name[0]))
364 report_param_error(fp, name, param[i].name, "error: name does not begin with a letter");
368 for (j = 1; param[i].name[j] != 0; ++j)
370 if (param[i].name[j] != '_' && !isalnum(param[i].name[j]))
372 report_param_error(fp, name, param[i].name, "error: name contains non-alphanumeric characters");
377 if (param[i].name[j] != 0)
381 /* Check that the name does not conflict with a method */
382 if (_gmx_sel_find_symbol(symtab, param[i].name, true))
384 report_param_error(fp, name, param[i].name, "error: name conflicts with another method or a keyword");
387 } /* End of parameter loop */
388 /* Check parameters of existing methods */
389 sym = _gmx_sel_first_symbol(symtab, SYMBOL_METHOD);
392 gmx_ana_selmethod_t *method = _gmx_sel_sym_value_method(sym);
393 gmx_ana_selparam_t *param =
394 gmx_ana_selmethod_find_param(name, method);
397 report_param_error(fp, method->name, param->name, "error: name conflicts with another method or a keyword");
400 sym = _gmx_sel_next_symbol(sym, SYMBOL_METHOD);
406 * Checks the validity of selection method callback functions.
408 * \param[in] fp File handle to use for diagnostic messages
410 * \param[in] method The method to check.
411 * \returns true if there are no problems, false otherwise.
413 * This function performs some checks common to both check_method() and
415 * This function checks that all the required callbacks are defined, i.e.,
416 * not NULL, to find programming errors.
419 check_callbacks(FILE *fp, gmx_ana_selmethod_t *method)
425 /* Make some checks on init_data and free */
426 if (method->nparams > 0 && !method->init_data)
428 report_error(fp, method->name, "error: init_data should be provided because the method has parameters");
431 if (method->free && !method->init_data)
433 report_error(fp, method->name, "warning: free is not used because of missing init_data");
435 /* Check presence of outinit for position-valued methods */
436 if (method->type == POS_VALUE && !method->outinit)
438 report_error(fp, method->name, "error: outinit should be provided because the method has POS_VALUE");
441 /* Warn of dynamic callbacks in static methods */
442 if (!(method->flags & SMETH_MODIFIER))
444 if (method->pupdate && !(method->flags & SMETH_DYNAMIC))
446 report_error(fp, method->name, "warning: pupdate not used because the method is static");
447 method->pupdate = NULL;
450 /* Check that there is an evaluation function */
451 if (method->type != NO_VALUE && !method->update && !method->pupdate)
453 report_error(fp, method->name, "error: evaluation function missing");
456 /* Loop through the parameters to determine if initialization callbacks
459 for (i = 0; i < method->nparams; ++i)
461 if (method->param[i].val.type != POS_VALUE
462 && (method->param[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
467 /* Check that the callbacks required by the parameters are present */
468 if (bNeedInit && !method->init)
470 report_error(fp, method->name, "error: init should be provided");
477 * Checks the validity of a selection method.
479 * \param[in] fp File handle to use for diagnostic messages
481 * \param[in,out] method Method to check.
482 * \param[in] symtab Symbol table (used for checking overlaps).
484 * Checks the validity of the given selection method data structure
485 * that does not have \ref SMETH_MODIFIER set.
486 * If you remove a check, please make sure that the selection parser,
487 * compiler, and evaluation functions can deal with the method.
490 check_method(FILE *fp, gmx_ana_selmethod_t *method, gmx_sel_symtab_t *symtab)
495 if (method->type == NO_VALUE)
497 report_error(fp, method->name, "error: no value type specified");
500 if (method->type == STR_VALUE && method->nparams > 0)
502 report_error(fp, method->name, "error: evaluates to a string but is not a keyword");
506 if (method->type == GROUP_VALUE)
508 /* Group methods should always have SMETH_SINGLEVAL,
509 * so set it for convenience. */
510 method->flags |= SMETH_SINGLEVAL;
511 /* Check that conflicting flags are not present. */
512 if (method->flags & SMETH_VARNUMVAL)
514 report_error(fp, method->name, "error: SMETH_VARNUMVAL cannot be set for group-valued methods");
520 if ((method->flags & SMETH_SINGLEVAL)
521 && (method->flags & SMETH_VARNUMVAL))
523 report_error(fp, method->name, "error: SMETH_SINGLEVAL and SMETH_VARNUMVAL both set");
527 if ((method->flags & SMETH_CHARVAL) && method->type != STR_VALUE)
529 report_error(fp, method->name, "error: SMETH_CHARVAL can only be specified for STR_VALUE methods");
532 /* Check the parameters */
533 if (!check_params(fp, method->name, method->nparams, method->param, symtab))
537 /* Check the callback pointers */
538 if (!check_callbacks(fp, method))
547 * Checks the validity of a selection modifier method.
549 * \param[in] fp File handle to use for diagnostic messages
551 * \param[in,out] method Method to check.
552 * \param[in] symtab Symbol table (used for checking overlaps).
554 * Checks the validity of the given selection method data structure
555 * that has \ref SMETH_MODIFIER set.
556 * If you remove a check, please make sure that the selection parser,
557 * compiler, and evaluation functions can deal with the method.
560 check_modifier(FILE *fp, gmx_ana_selmethod_t *method, gmx_sel_symtab_t *symtab)
565 if (method->type != NO_VALUE && method->type != POS_VALUE)
567 report_error(fp, method->name, "error: modifier should have type POS_VALUE or NO_VALUE");
571 if (method->flags & (SMETH_SINGLEVAL | SMETH_VARNUMVAL))
573 report_error(fp, method->name, "error: modifier should not have SMETH_SINGLEVAL or SMETH_VARNUMVAL set");
576 /* Check the parameters */
577 /* The first parameter is skipped */
578 if (!check_params(fp, method->name, method->nparams-1, method->param+1, symtab))
582 /* Check the callback pointers */
583 if (!check_callbacks(fp, method))
589 report_error(fp, method->name, "error: modifier should not have update");
592 if (method->type == POS_VALUE && !method->pupdate)
594 report_error(fp, method->name, "error: evaluation function missing");
602 * \param[in,out] symtab Symbol table to register the method to.
603 * \param[in] name Name under which the method should be registered.
604 * \param[in] method Method to register.
605 * \returns 0 on success, EINVAL if there was something wrong with the
608 * \p name does not need to match the name of the method, and the same
609 * method can be registered multiple times under different names.
610 * If \p name equals some previously registered name,
611 * an error message is printed and the method is not registered.
613 * The function also performs some sanity checking on the input method,
614 * and refuses to register it if there are problems.
615 * Some problems only generate warnings.
616 * All problems are described to \p stderr.
619 gmx_ana_selmethod_register(gmx_sel_symtab_t *symtab,
620 const char *name, gmx_ana_selmethod_t *method)
624 /* Check the method */
625 if (method->flags & SMETH_MODIFIER)
627 bOk = check_modifier(stderr, method, symtab);
631 bOk = check_method(stderr, method, symtab);
633 /* Try to register the method if everything is ok */
636 if (!_gmx_sel_add_method_symbol(symtab, name, method))
643 report_error(stderr, name, "warning: not registered");
650 * \param[in,out] symtab Symbol table to register the methods to.
651 * \returns 0 on success, -1 if any of the default methods could not be
655 gmx_ana_selmethod_register_defaults(gmx_sel_symtab_t *symtab)
662 for (i = 0; i < asize(smtable_def); ++i)
664 gmx_ana_selmethod_t *method = smtable_def[i].method;
666 if (smtable_def[i].name == NULL)
668 rc = gmx_ana_selmethod_register(symtab, method->name, method);
672 rc = gmx_ana_selmethod_register(symtab, smtable_def[i].name, method);
683 * \param[in] name Name of the parameter to search.
684 * \param[in] method Method to search for the parameter.
685 * \returns Pointer to the parameter in the
686 * \ref gmx_ana_selmethod_t::param "method->param" array,
687 * or NULL if no parameter with name \p name was found.
689 * This is a simple wrapper for gmx_ana_selparam_find().
692 gmx_ana_selmethod_find_param(const char *name, gmx_ana_selmethod_t *method)
694 return gmx_ana_selparam_find(name, method->nparams, method->param);