Removed obsolete selection code.
[gromacs/qmmm-gamess-us.git] / src / gmxlib / selection / selmethod.c
blob14482957b594eb580c7d045fc54f3608a5f38896
1 /*
3 * This source code is part of
5 * G R O M A C S
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
31 /*! \internal \file
32 * \brief Implementation of functions in selmethod.h.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
38 #include <ctype.h>
39 #include <stdarg.h>
41 #include <macros.h>
42 #include <string2.h>
44 #include <selmethod.h>
46 #include "selcollection.h"
47 #include "symrec.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.
68 /* From sm_com.c */
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;
97 /* From sm_same.c */
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;
106 /*! \brief
107 * Helper structure for defining selection methods.
109 typedef struct {
110 /*! \brief
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.
116 const char *name;
117 /** Method data structure to register. */
118 gmx_ana_selmethod_t *method;
119 } t_register_method;
121 /** Array of selection methods defined in the library. */
122 static const t_register_method smtable_def[] = {
123 {NULL, &sm_cog},
124 {NULL, &sm_com},
126 {NULL, &sm_all},
127 {NULL, &sm_none},
128 {NULL, &sm_atomnr},
129 {NULL, &sm_resnr},
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},
138 {NULL, &sm_resname},
139 {NULL, &sm_insertcode},
140 {NULL, &sm_chain},
141 {NULL, &sm_mass},
142 {NULL, &sm_charge},
143 {NULL, &sm_altloc},
144 {NULL, &sm_occupancy},
145 {NULL, &sm_betafactor},
146 {NULL, &sm_x},
147 {NULL, &sm_y},
148 {NULL, &sm_z},
150 {NULL, &sm_distance},
151 {NULL, &sm_mindistance},
152 {NULL, &sm_within},
153 {NULL, &sm_insolidangle},
154 {NULL, &sm_same},
156 {NULL, &sm_merge},
157 {NULL, &sm_plus},
158 {NULL, &sm_permute},
161 /*! \brief
162 * Convenience function for reporting errors found in selection methods.
164 static void
165 report_error(FILE *fp, const char *name, const char *fmt, ...)
167 va_list ap;
168 va_start(ap, fmt);
169 if (fp)
171 fprintf(fp, "selection method '%s': ", name);
172 vfprintf(fp, fmt, ap);
173 fprintf(fp, "\n");
175 va_end(ap);
178 /*! \brief
179 * Convenience function for reporting errors found in selection method parameters.
181 static void
182 report_param_error(FILE *fp, const char *mname, const char *pname,
183 const char *fmt, ...)
185 va_list ap;
186 va_start(ap, fmt);
187 if (fp)
189 fprintf(fp, "selection method '%s': parameter '%s': ", mname, pname);
190 vfprintf(fp, fmt, ap);
191 fprintf(fp, "\n");
193 va_end(ap);
196 /*! \brief
197 * Checks the validity of parameters.
199 * \param[in] fp File handle to use for diagnostic messages
200 * (can be NULL).
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 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,
207 * FALSE otherwise.
209 * This function performs some checks common to both check_method() and
210 * check_modifier().
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.
217 static bool
218 check_params(FILE *fp, const char *name, int nparams, gmx_ana_selparam_t param[],
219 gmx_sel_symtab_t *symtab)
221 bool bOk = TRUE;
222 gmx_sel_symrec_t *sym;
223 int i, j;
225 if (nparams > 0 && !param)
227 report_error(fp, name, "error: missing parameter data");
228 bOk = FALSE;
229 return FALSE;
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");
242 bOk = FALSE;
243 continue;
245 /* Check for duplicates */
246 for (j = 0; j < i; ++j)
248 if (param[j].name == NULL)
250 continue;
252 if (!strcasecmp(param[i].name, param[j].name))
254 report_error(fp, name, "error: duplicate parameter name '%s'", param[i].name);
255 bOk = FALSE;
256 break;
259 /* Check flags */
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");
270 bOk = FALSE;
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");
280 bOk = FALSE;
282 if (param[i].flags & SPAR_ATOMVAL)
284 report_param_error(fp, name, param[i].name, "error: SPAR_RANGES and SPAR_ATOMVAL both set");
285 bOk = FALSE;
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");
291 bOk = FALSE;
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");
298 bOk = FALSE;
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");
303 bOk = FALSE;
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");
308 bOk = FALSE;
311 /* Check 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 boolean parameters");
317 bOk = FALSE;
319 /* The 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: boolean parameter should not have any flags set");
326 bOk = FALSE;
329 /* Check val.nr */
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");
343 bOk = FALSE;
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)
358 continue;
360 if (!isalpha(param[i].name[0]))
362 report_param_error(fp, name, param[i].name, "error: name does not begin with a letter");
363 bOk = FALSE;
364 continue;
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");
371 bOk = FALSE;
372 break;
375 if (param[i].name[j] != 0)
377 continue;
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");
383 bOk = FALSE;
385 } /* End of parameter loop */
386 /* Check parameters of existing methods */
387 sym = _gmx_sel_first_symbol(symtab, SYMBOL_METHOD);
388 while (sym)
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);
393 if (param)
395 report_param_error(fp, method->name, param->name, "error: name conflicts with another method or a keyword");
396 bOk = FALSE;
398 sym = _gmx_sel_next_symbol(sym, SYMBOL_METHOD);
400 return bOk;
403 /*! \brief
404 * Checks the validity of selection method callback functions.
406 * \param[in] fp File handle to use for diagnostic messages
407 * (can be NULL).
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
412 * check_modifier().
413 * This function checks that all the required callbacks are defined, i.e.,
414 * not NULL, to find programming errors.
416 static bool
417 check_callbacks(FILE *fp, gmx_ana_selmethod_t *method)
419 bool bOk = TRUE;
420 bool bNeedInit;
421 int i;
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");
427 bOk = FALSE;
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");
437 bOk = FALSE;
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");
452 bOk = FALSE;
454 /* Loop through the parameters to determine if initialization callbacks
455 * are needed. */
456 bNeedInit = FALSE;
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)))
462 bNeedInit = TRUE;
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");
469 bOk = FALSE;
471 return bOk;
475 * Checks the validity of a selection method.
477 * \param[in] fp File handle to use for diagnostic messages
478 * (can be NULL).
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.
487 static bool
488 check_method(FILE *fp, gmx_ana_selmethod_t *method, gmx_sel_symtab_t *symtab)
490 bool bOk = TRUE;
492 /* Check the type */
493 if (method->type == NO_VALUE)
495 report_error(fp, method->name, "error: no value type specified");
496 bOk = FALSE;
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");
501 bOk = FALSE;
503 /* Check flags */
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");
513 bOk = FALSE;
516 else
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");
522 bOk = FALSE;
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");
528 bOk = FALSE;
530 /* Check the parameters */
531 if (!check_params(fp, method->name, method->nparams, method->param, symtab))
533 bOk = FALSE;
535 /* Check the callback pointers */
536 if (!check_callbacks(fp, method))
538 bOk = FALSE;
541 return bOk;
545 * Checks the validity of a selection modifier method.
547 * \param[in] fp File handle to use for diagnostic messages
548 * (can be NULL).
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.
557 static bool
558 check_modifier(FILE *fp, gmx_ana_selmethod_t *method, gmx_sel_symtab_t *symtab)
560 bool bOk = TRUE;
562 /* Check the type */
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");
566 bOk = FALSE;
568 /* Check flags */
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");
572 bOk = FALSE;
574 /* Check the parameters */
575 /* The first parameter is skipped */
576 if (!check_params(fp, method->name, method->nparams-1, method->param+1, symtab))
578 bOk = FALSE;
580 /* Check the callback pointers */
581 if (!check_callbacks(fp, method))
583 bOk = FALSE;
585 if (method->update)
587 report_error(fp, method->name, "error: modifier should not have update");
588 bOk = FALSE;
590 if (method->type == POS_VALUE && !method->pupdate)
592 report_error(fp, method->name, "error: evaluation function missing");
593 bOk = FALSE;
596 return bOk;
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
604 * method.
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)
620 bool bOk;
622 /* Check the method */
623 if (method->flags & SMETH_MODIFIER)
625 bOk = check_modifier(stderr, method, sc->symtab);
627 else
629 bOk = check_method(stderr, method, sc->symtab);
631 /* Try to register the method if everything is ok */
632 if (bOk)
634 if (!_gmx_sel_add_method_symbol(sc->symtab, name, method))
636 bOk = FALSE;
639 if (!bOk)
641 report_error(stderr, name, "warning: not registered");
642 return EINVAL;
644 return 0;
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
650 * registered.
653 gmx_ana_selmethod_register_defaults(struct gmx_ana_selcollection_t *sc)
655 size_t i;
656 int rc;
657 bool bOk;
659 bOk = TRUE;
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);
668 else
670 rc = gmx_ana_selmethod_register(sc, smtable_def[i].name, method);
672 if (rc != 0)
674 bOk = FALSE;
677 return bOk ? 0 : -1;
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().
689 gmx_ana_selparam_t *
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);