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
32 * \brief Implementation of functions in selmethod.h.
44 #include <selmethod.h>
46 #include "selcollection.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_atomtype;
80 extern gmx_ana_selmethod_t sm_resname;
81 extern gmx_ana_selmethod_t sm_insertcode;
82 extern gmx_ana_selmethod_t sm_chain;
83 extern gmx_ana_selmethod_t sm_mass;
84 extern gmx_ana_selmethod_t sm_charge;
85 extern gmx_ana_selmethod_t sm_altloc;
86 extern gmx_ana_selmethod_t sm_occupancy;
87 extern gmx_ana_selmethod_t sm_betafactor;
88 extern gmx_ana_selmethod_t sm_x;
89 extern gmx_ana_selmethod_t sm_y;
90 extern gmx_ana_selmethod_t sm_z;
91 /* From sm_distance.c */
92 extern gmx_ana_selmethod_t sm_distance;
93 extern gmx_ana_selmethod_t sm_mindistance;
94 extern gmx_ana_selmethod_t sm_within;
95 /* From sm_insolidangle.c */
96 extern gmx_ana_selmethod_t sm_insolidangle;
98 extern gmx_ana_selmethod_t sm_same;
100 /* From sm_merge.c */
101 extern gmx_ana_selmethod_t sm_merge;
102 extern gmx_ana_selmethod_t sm_plus;
103 /* From sm_permute.c */
104 extern gmx_ana_selmethod_t sm_permute;
107 * Helper structure for defining selection methods.
111 * Name to register the method under.
113 * If NULL, use the actual name of the method.
114 * This field is used for defining synonyms.
117 /** Method data structure to register. */
118 gmx_ana_selmethod_t *method;
121 /** Array of selection methods defined in the library. */
122 static const t_register_method smtable_def[] = {
130 {"resid", &sm_resnr},
131 {NULL, &sm_resindex},
132 {"residue", &sm_resindex},
133 {NULL, &sm_molindex},
134 {"mol", &sm_molindex},
135 {"molecule", &sm_molindex},
136 {NULL, &sm_atomname},
137 {NULL, &sm_atomtype},
139 {NULL, &sm_insertcode},
144 {NULL, &sm_occupancy},
145 {NULL, &sm_betafactor},
150 {NULL, &sm_distance},
151 {NULL, &sm_mindistance},
153 {NULL, &sm_insolidangle},
162 * Convenience function for reporting errors found in selection methods.
165 report_error(FILE *fp, const char *name, const char *fmt, ...)
171 fprintf(fp, "selection method '%s': ", name);
172 vfprintf(fp, fmt, ap);
179 * Convenience function for reporting errors found in selection method parameters.
182 report_param_error(FILE *fp, const char *mname, const char *pname,
183 const char *fmt, ...)
189 fprintf(fp, "selection method '%s': parameter '%s': ", mname, pname);
190 vfprintf(fp, fmt, ap);
197 * Checks the validity of parameters.
199 * \param[in] fp File handle to use for diagnostic messages
201 * \param[in] name Name of the method (used for error messages).
202 * \param[in] nparams Number of parameters in \p param.
203 * \param[in,out] param Parameter array
204 * (only the \c flags field of gmx_boolean parameters may be modified).
205 * \param[in] symtab Symbol table (used for checking overlaps).
206 * \returns TRUE if there are no problems with the parameters,
209 * This function performs some checks common to both check_method() and
211 * The purpose of these checks is to ensure that the selection parser does not
212 * need to check for the validity of the parameters at each turn, and to
213 * report programming errors as early as possible.
214 * If you remove a check, make sure that the parameter parser can handle the
215 * resulting parameters.
218 check_params(FILE *fp, const char *name, int nparams, gmx_ana_selparam_t param[],
219 gmx_sel_symtab_t *symtab)
222 gmx_sel_symrec_t *sym;
225 if (nparams > 0 && !param)
227 report_error(fp, name, "error: missing parameter data");
231 if (nparams == 0 && param)
233 report_error(fp, name, "warning: parameter data unused because nparams=0");
235 /* Check each parameter */
236 for (i = 0; i < nparams; ++i)
238 /* Check that there is at most one NULL name, in the beginning */
239 if (param[i].name == NULL && i > 0)
241 report_error(fp, name, "error: NULL parameter should be the first one");
245 /* Check for duplicates */
246 for (j = 0; j < i; ++j)
248 if (param[j].name == NULL)
252 if (!gmx_strcasecmp(param[i].name, param[j].name))
254 report_error(fp, name, "error: duplicate parameter name '%s'", param[i].name);
260 if (param[i].flags & SPAR_SET)
262 report_param_error(fp, name, param[i].name, "warning: flag SPAR_SET is set");
263 param[i].flags &= ~SPAR_SET;
265 if (param[i].flags & SPAR_RANGES)
267 if (param[i].val.type != INT_VALUE && param[i].val.type != REAL_VALUE)
269 report_param_error(fp, name, param[i].name, "error: SPAR_RANGES cannot be set for a non-numeric parameter");
272 if (param[i].flags & SPAR_DYNAMIC)
274 report_param_error(fp, name, param[i].name, "warning: SPAR_DYNAMIC does not have effect with SPAR_RANGES");
275 param[i].flags &= ~SPAR_DYNAMIC;
277 if (!(param[i].flags & SPAR_VARNUM) && param[i].val.nr != 1)
279 report_param_error(fp, name, param[i].name, "error: range should take either one or an arbitrary number of values");
282 if (param[i].flags & SPAR_ATOMVAL)
284 report_param_error(fp, name, param[i].name, "error: SPAR_RANGES and SPAR_ATOMVAL both set");
288 if ((param[i].flags & SPAR_VARNUM) && (param[i].flags & SPAR_ATOMVAL))
290 report_param_error(fp, name, param[i].name, "error: SPAR_VARNUM and SPAR_ATOMVAL both set");
293 if (param[i].flags & SPAR_ENUMVAL)
295 if (param[i].val.type != STR_VALUE)
297 report_param_error(fp, name, param[i].name, "error: SPAR_ENUMVAL can only be set for string parameters");
300 if (param[i].val.nr != 1)
302 report_param_error(fp, name, param[i].name, "error: SPAR_ENUMVAL parameters should take exactly one value");
305 if (param[i].flags & (SPAR_DYNAMIC | SPAR_VARNUM | SPAR_ATOMVAL))
307 report_param_error(fp, name, param[i].name, "error: only SPAR_OPTIONAL supported with SPAR_ENUMVAL");
311 /* Check gmx_boolean parameters */
312 if (param[i].val.type == NO_VALUE)
314 if (param[i].val.nr != 0)
316 report_param_error(fp, name, param[i].name, "error: number of values should be zero for gmx_boolean parameters");
319 /* The gmx_boolean parameters should always be optional, so set the
320 * flag for convenience. */
321 param[i].flags |= SPAR_OPTIONAL;
322 /* Any other flags should not be specified */
323 if (param[i].flags & ~SPAR_OPTIONAL)
325 report_param_error(fp, name, param[i].name, "error: gmx_boolean parameter should not have any flags set");
330 if (param[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL))
332 if (param[i].val.nr != -1)
334 report_param_error(fp, name, param[i].name, "warning: val.nr is not -1 although SPAR_VARNUM/SPAR_ATOMVAL is set");
336 param[i].val.nr = -1;
338 else if (param[i].val.type != NO_VALUE)
340 if (param[i].val.nr <= 0)
342 report_param_error(fp, name, param[i].name, "error: val.nr <= 0");
346 /* Check that the value pointer is NULL */
347 if (param[i].nvalptr != NULL)
349 report_param_error(fp, name, param[i].name, "warning: nvalptr is set");
351 if (param[i].val.u.ptr != NULL && !(param[i].flags & SPAR_ENUMVAL))
353 report_param_error(fp, name, param[i].name, "warning: value pointer is set");
355 /* Check that the name contains only valid characters */
356 if (param[i].name == NULL)
360 if (!isalpha(param[i].name[0]))
362 report_param_error(fp, name, param[i].name, "error: name does not begin with a letter");
366 for (j = 1; param[i].name[j] != 0; ++j)
368 if (param[i].name[j] != '_' && !isalnum(param[i].name[j]))
370 report_param_error(fp, name, param[i].name, "error: name contains non-alphanumeric characters");
375 if (param[i].name[j] != 0)
379 /* Check that the name does not conflict with a method */
380 if (_gmx_sel_find_symbol(symtab, param[i].name, TRUE))
382 report_param_error(fp, name, param[i].name, "error: name conflicts with another method or a keyword");
385 } /* End of parameter loop */
386 /* Check parameters of existing methods */
387 sym = _gmx_sel_first_symbol(symtab, SYMBOL_METHOD);
390 gmx_ana_selmethod_t *method = _gmx_sel_sym_value_method(sym);
391 gmx_ana_selparam_t *param =
392 gmx_ana_selmethod_find_param(name, method);
395 report_param_error(fp, method->name, param->name, "error: name conflicts with another method or a keyword");
398 sym = _gmx_sel_next_symbol(sym, SYMBOL_METHOD);
404 * Checks the validity of selection method callback functions.
406 * \param[in] fp File handle to use for diagnostic messages
408 * \param[in] method The method to check.
409 * \returns TRUE if there are no problems, FALSE otherwise.
411 * This function performs some checks common to both check_method() and
413 * This function checks that all the required callbacks are defined, i.e.,
414 * not NULL, to find programming errors.
417 check_callbacks(FILE *fp, gmx_ana_selmethod_t *method)
423 /* Make some checks on init_data and free */
424 if (method->nparams > 0 && !method->init_data)
426 report_error(fp, method->name, "error: init_data should be provided because the method has parameters");
429 if (method->free && !method->init_data)
431 report_error(fp, method->name, "warning: free is not used because of missing init_data");
433 /* Check presence of outinit for position-valued methods */
434 if (method->type == POS_VALUE && !method->outinit)
436 report_error(fp, method->name, "error: outinit should be provided because the method has POS_VALUE");
439 /* Warn of dynamic callbacks in static methods */
440 if (!(method->flags & SMETH_MODIFIER))
442 if (method->pupdate && !(method->flags & SMETH_DYNAMIC))
444 report_error(fp, method->name, "warning: pupdate not used because the method is static");
445 method->pupdate = NULL;
448 /* Check that there is an evaluation function */
449 if (method->type != NO_VALUE && !method->update && !method->pupdate)
451 report_error(fp, method->name, "error: evaluation function missing");
454 /* Loop through the parameters to determine if initialization callbacks
457 for (i = 0; i < method->nparams; ++i)
459 if (method->param[i].val.type != POS_VALUE
460 && (method->param[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
465 /* Check that the callbacks required by the parameters are present */
466 if (bNeedInit && !method->init)
468 report_error(fp, method->name, "error: init should be provided");
475 * Checks the validity of a selection method.
477 * \param[in] fp File handle to use for diagnostic messages
479 * \param[in,out] method Method to check.
480 * \param[in] symtab Symbol table (used for checking overlaps).
482 * Checks the validity of the given selection method data structure
483 * that does not have \ref SMETH_MODIFIER set.
484 * If you remove a check, please make sure that the selection parser,
485 * compiler, and evaluation functions can deal with the method.
488 check_method(FILE *fp, gmx_ana_selmethod_t *method, gmx_sel_symtab_t *symtab)
493 if (method->type == NO_VALUE)
495 report_error(fp, method->name, "error: no value type specified");
498 if (method->type == STR_VALUE && method->nparams > 0)
500 report_error(fp, method->name, "error: evaluates to a string but is not a keyword");
504 if (method->type == GROUP_VALUE)
506 /* Group methods should always have SMETH_SINGLEVAL,
507 * so set it for convenience. */
508 method->flags |= SMETH_SINGLEVAL;
509 /* Check that conflicting flags are not present. */
510 if (method->flags & SMETH_VARNUMVAL)
512 report_error(fp, method->name, "error: SMETH_VARNUMVAL cannot be set for group-valued methods");
518 if ((method->flags & SMETH_SINGLEVAL)
519 && (method->flags & SMETH_VARNUMVAL))
521 report_error(fp, method->name, "error: SMETH_SINGLEVAL and SMETH_VARNUMVAL both set");
525 if ((method->flags & SMETH_CHARVAL) && method->type != STR_VALUE)
527 report_error(fp, method->name, "error: SMETH_CHARVAL can only be specified for STR_VALUE methods");
530 /* Check the parameters */
531 if (!check_params(fp, method->name, method->nparams, method->param, symtab))
535 /* Check the callback pointers */
536 if (!check_callbacks(fp, method))
545 * Checks the validity of a selection modifier method.
547 * \param[in] fp File handle to use for diagnostic messages
549 * \param[in,out] method Method to check.
550 * \param[in] symtab Symbol table (used for checking overlaps).
552 * Checks the validity of the given selection method data structure
553 * that has \ref SMETH_MODIFIER set.
554 * If you remove a check, please make sure that the selection parser,
555 * compiler, and evaluation functions can deal with the method.
558 check_modifier(FILE *fp, gmx_ana_selmethod_t *method, gmx_sel_symtab_t *symtab)
563 if (method->type != NO_VALUE && method->type != POS_VALUE)
565 report_error(fp, method->name, "error: modifier should have type POS_VALUE or NO_VALUE");
569 if (method->flags & (SMETH_SINGLEVAL | SMETH_VARNUMVAL))
571 report_error(fp, method->name, "error: modifier should not have SMETH_SINGLEVAL or SMETH_VARNUMVAL set");
574 /* Check the parameters */
575 /* The first parameter is skipped */
576 if (!check_params(fp, method->name, method->nparams-1, method->param+1, symtab))
580 /* Check the callback pointers */
581 if (!check_callbacks(fp, method))
587 report_error(fp, method->name, "error: modifier should not have update");
590 if (method->type == POS_VALUE && !method->pupdate)
592 report_error(fp, method->name, "error: evaluation function missing");
600 * \param[in,out] sc Selection collection to registered the method to.
601 * \param[in] name Name under which the method should be registered.
602 * \param[in] method Method to register.
603 * \returns 0 on success, EINVAL if there was something wrong with the
606 * \p name does not need to match the name of the method, and the same
607 * method can be registered multiple times under different names.
608 * If \p name equals some previously registered name,
609 * an error message is printed and the method is not registered.
611 * The function also performs some sanity checking on the input method,
612 * and refuses to register it if there are problems.
613 * Some problems only generate warnings.
614 * All problems are described to \p stderr.
617 gmx_ana_selmethod_register(struct gmx_ana_selcollection_t *sc,
618 const char *name, gmx_ana_selmethod_t *method)
622 /* Check the method */
623 if (method->flags & SMETH_MODIFIER)
625 bOk = check_modifier(stderr, method, sc->symtab);
629 bOk = check_method(stderr, method, sc->symtab);
631 /* Try to register the method if everything is ok */
634 if (!_gmx_sel_add_method_symbol(sc->symtab, name, method))
641 report_error(stderr, name, "warning: not registered");
648 * \param[in,out] sc Selection collection to registered the methods to.
649 * \returns 0 on success, -1 if any of the default methods could not be
653 gmx_ana_selmethod_register_defaults(struct gmx_ana_selcollection_t *sc)
660 for (i = 0; i < asize(smtable_def); ++i)
662 gmx_ana_selmethod_t *method = smtable_def[i].method;
664 if (smtable_def[i].name == NULL)
666 rc = gmx_ana_selmethod_register(sc, method->name, method);
670 rc = gmx_ana_selmethod_register(sc, smtable_def[i].name, method);
681 * \param[in] name Name of the parameter to search.
682 * \param[in] method Method to search for the parameter.
683 * \returns Pointer to the parameter in the
684 * \ref gmx_ana_selmethod_t::param "method->param" array,
685 * or NULL if no parameter with name \p name was found.
687 * This is a simple wrapper for gmx_ana_selparam_find().
690 gmx_ana_selmethod_find_param(const char *name, gmx_ana_selmethod_t *method)
692 return gmx_ana_selparam_find(name, method->nparams, method->param);