A bit more modular selection position handling.
[gromacs/qmmm-gamess-us.git] / src / gmxlib / selection / selmethod.c
blobb2bb219eb41b00b2115dd36ad6ac12aabeedc848
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>
40 #include <macros.h>
41 #include <string2.h>
43 #include <selmethod.h>
45 #include "selcollection.h"
46 #include "symrec.h"
49 * These global variables cannot be const because gmx_ana_selmethod_register()
50 * modifies them to set some defaults. This is a small price to pay for the
51 * convenience of not having to remember exactly how the selection compiler
52 * expects the structures to be filled, and even more so if the expectations
53 * change. Also, even if the gmx_ana_selmethod_t structures were made const,
54 * the parameters could not be without typecasts somewhere, because the param
55 * field in gmx_ana_selmethod_t cannot be declared const.
57 * Even though the variables may be modified, this should be thread-safe as
58 * modifications are done only in gmx_ana_selmethod_register(), and it should
59 * work even if called more than once for the same structure, and even if
60 * called concurrently from multiple threads (as long as the selection
61 * collection is not the same).
63 * All of these problems should go away if/when the selection methods are
64 * implemented as C++ classes.
67 /* From sm_com.c */
68 extern gmx_ana_selmethod_t sm_cog;
69 extern gmx_ana_selmethod_t sm_com;
70 /* From sm_simple.c */
71 extern gmx_ana_selmethod_t sm_all;
72 extern gmx_ana_selmethod_t sm_none;
73 extern gmx_ana_selmethod_t sm_atomnr;
74 extern gmx_ana_selmethod_t sm_resnr;
75 extern gmx_ana_selmethod_t sm_atomname;
76 extern gmx_ana_selmethod_t sm_atomtype;
77 extern gmx_ana_selmethod_t sm_resname;
78 extern gmx_ana_selmethod_t sm_insertcode;
79 extern gmx_ana_selmethod_t sm_chain;
80 extern gmx_ana_selmethod_t sm_mass;
81 extern gmx_ana_selmethod_t sm_charge;
82 extern gmx_ana_selmethod_t sm_altloc;
83 extern gmx_ana_selmethod_t sm_occupancy;
84 extern gmx_ana_selmethod_t sm_betafactor;
85 extern gmx_ana_selmethod_t sm_x;
86 extern gmx_ana_selmethod_t sm_y;
87 extern gmx_ana_selmethod_t sm_z;
88 /* From sm_distance.c */
89 extern gmx_ana_selmethod_t sm_distance;
90 extern gmx_ana_selmethod_t sm_mindistance;
91 extern gmx_ana_selmethod_t sm_within;
92 /* From sm_insolidangle.c */
93 extern gmx_ana_selmethod_t sm_insolidangle;
94 /* From sm_same.c */
95 extern gmx_ana_selmethod_t sm_same;
97 /* From sm_merge.c */
98 extern gmx_ana_selmethod_t sm_merge;
99 extern gmx_ana_selmethod_t sm_plus;
100 /* From sm_permute.c */
101 extern gmx_ana_selmethod_t sm_permute;
103 /** Array of selection methods defined in the library. */
104 static gmx_ana_selmethod_t *const smtable_def[] = {
105 &sm_cog,
106 &sm_com,
108 &sm_all,
109 &sm_none,
110 &sm_atomnr,
111 &sm_resnr,
112 &sm_atomname,
113 &sm_atomtype,
114 &sm_resname,
115 &sm_insertcode,
116 &sm_chain,
117 &sm_mass,
118 &sm_charge,
119 &sm_altloc,
120 &sm_occupancy,
121 &sm_betafactor,
122 &sm_x,
123 &sm_y,
124 &sm_z,
126 &sm_distance,
127 &sm_mindistance,
128 &sm_within,
129 &sm_insolidangle,
130 &sm_same,
132 &sm_merge,
133 &sm_plus,
134 &sm_permute,
137 /*! \brief
138 * Convenience function for reporting errors found in selection methods.
140 static void
141 report_error(FILE *fp, const char *name, const char *fmt, ...)
143 va_list ap;
144 va_start(ap, fmt);
145 if (fp)
147 fprintf(fp, "selection method '%s': ", name);
148 vfprintf(fp, fmt, ap);
149 fprintf(fp, "\n");
151 va_end(ap);
154 /*! \brief
155 * Convenience function for reporting errors found in selection method parameters.
157 static void
158 report_param_error(FILE *fp, const char *mname, const char *pname,
159 const char *fmt, ...)
161 va_list ap;
162 va_start(ap, fmt);
163 if (fp)
165 fprintf(fp, "selection method '%s': parameter '%s': ", mname, pname);
166 vfprintf(fp, fmt, ap);
167 fprintf(fp, "\n");
169 va_end(ap);
172 /*! \brief
173 * Checks the validity of parameters.
175 * \param[in] fp File handle to use for diagnostic messages
176 * (can be NULL).
177 * \param[in] name Name of the method (used for error messages).
178 * \param[in] nparams Number of parameters in \p param.
179 * \param[in,out] param Parameter array
180 * (only the \c flags field of boolean parameters may be modified).
181 * \param[in] symtab Symbol table (used for checking overlaps).
182 * \returns TRUE if there are no problems with the parameters,
183 * FALSE otherwise.
185 * This function performs some checks common to both check_method() and
186 * check_modifier().
187 * The purpose of these checks is to ensure that the selection parser does not
188 * need to check for the validity of the parameters at each turn, and to
189 * report programming errors as early as possible.
190 * If you remove a check, make sure that the parameter parser can handle the
191 * resulting parameters.
193 static bool
194 check_params(FILE *fp, const char *name, int nparams, gmx_ana_selparam_t param[],
195 gmx_sel_symtab_t *symtab)
197 bool bOk = TRUE;
198 gmx_sel_symrec_t *sym;
199 int i, j;
201 if (nparams > 0 && !param)
203 report_error(fp, name, "error: missing parameter data");
204 bOk = FALSE;
205 return FALSE;
207 if (nparams == 0 && param)
209 report_error(fp, name, "warning: parameter data unused because nparams=0");
211 /* Check each parameter */
212 for (i = 0; i < nparams; ++i)
214 /* Check that there is at most one NULL name, in the beginning */
215 if (param[i].name == NULL && i > 0)
217 report_error(fp, name, "error: NULL parameter should be the first one");
218 bOk = FALSE;
219 continue;
221 /* Check for duplicates */
222 for (j = 0; j < i; ++j)
224 if (param[j].name == NULL)
226 continue;
228 if (!strcasecmp(param[i].name, param[j].name))
230 report_error(fp, name, "error: duplicate parameter name '%s'", param[i].name);
231 bOk = FALSE;
232 break;
235 /* Check flags */
236 if (param[i].flags & SPAR_SET)
238 report_param_error(fp, name, param[i].name, "warning: flag SPAR_SET is set");
239 param[i].flags &= ~SPAR_SET;
241 if (param[i].flags & SPAR_RANGES)
243 if (param[i].val.type != INT_VALUE && param[i].val.type != REAL_VALUE)
245 report_param_error(fp, name, param[i].name, "error: SPAR_RANGES cannot be set for a non-numeric parameter");
246 bOk = FALSE;
248 if (param[i].flags & SPAR_DYNAMIC)
250 report_param_error(fp, name, param[i].name, "warning: SPAR_DYNAMIC does not have effect with SPAR_RANGES");
251 param[i].flags &= ~SPAR_DYNAMIC;
253 if (!(param[i].flags & SPAR_VARNUM) && param[i].val.nr != 1)
255 report_param_error(fp, name, param[i].name, "error: range should take either one or an arbitrary number of values");
256 bOk = FALSE;
258 if (param[i].flags & SPAR_ATOMVAL)
260 report_param_error(fp, name, param[i].name, "error: SPAR_RANGES and SPAR_ATOMVAL both set");
261 bOk = FALSE;
264 if ((param[i].flags & SPAR_VARNUM) && (param[i].flags & SPAR_ATOMVAL))
266 report_param_error(fp, name, param[i].name, "error: SPAR_VARNUM and SPAR_ATOMVAL both set");
267 bOk = FALSE;
269 if (param[i].flags & SPAR_ENUMVAL)
271 if (param[i].val.type != STR_VALUE)
273 report_param_error(fp, name, param[i].name, "error: SPAR_ENUMVAL can only be set for string parameters");
274 bOk = FALSE;
276 if (param[i].val.nr != 1)
278 report_param_error(fp, name, param[i].name, "error: SPAR_ENUMVAL parameters should take exactly one value");
279 bOk = FALSE;
281 if (param[i].flags & (SPAR_DYNAMIC | SPAR_VARNUM | SPAR_ATOMVAL))
283 report_param_error(fp, name, param[i].name, "error: only SPAR_OPTIONAL supported with SPAR_ENUMVAL");
284 bOk = FALSE;
287 /* Check boolean parameters */
288 if (param[i].val.type == NO_VALUE)
290 if (param[i].val.nr != 0)
292 report_param_error(fp, name, param[i].name, "error: number of values should be zero for boolean parameters");
293 bOk = FALSE;
295 /* The boolean parameters should always be optional, so set the
296 * flag for convenience. */
297 param[i].flags |= SPAR_OPTIONAL;
298 /* Any other flags should not be specified */
299 if (param[i].flags & ~SPAR_OPTIONAL)
301 report_param_error(fp, name, param[i].name, "error: boolean parameter should not have any flags set");
302 bOk = FALSE;
305 /* Check val.nr */
306 if (param[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL))
308 if (param[i].val.nr != -1)
310 report_param_error(fp, name, param[i].name, "warning: val.nr is not -1 although SPAR_VARNUM/SPAR_ATOMVAL is set");
312 param[i].val.nr = -1;
314 else if (param[i].val.type != NO_VALUE)
316 if (param[i].val.nr <= 0)
318 report_param_error(fp, name, param[i].name, "error: val.nr <= 0");
319 bOk = FALSE;
322 /* Check that the value pointer is NULL */
323 if (param[i].nvalptr != NULL)
325 report_param_error(fp, name, param[i].name, "warning: nvalptr is set");
327 if (param[i].val.u.ptr != NULL && !(param[i].flags & SPAR_ENUMVAL))
329 report_param_error(fp, name, param[i].name, "warning: value pointer is set");
331 /* Check that the name contains only valid characters */
332 if (param[i].name == NULL)
334 continue;
336 if (!isalpha(param[i].name[0]))
338 report_param_error(fp, name, param[i].name, "error: name does not begin with a letter");
339 bOk = FALSE;
340 continue;
342 for (j = 1; param[i].name[j] != 0; ++j)
344 if (param[i].name[j] != '_' && !isalnum(param[i].name[j]))
346 report_param_error(fp, name, param[i].name, "error: name contains non-alphanumeric characters");
347 bOk = FALSE;
348 break;
351 if (param[i].name[j] != 0)
353 continue;
355 /* Check that the name does not conflict with a method */
356 if (_gmx_sel_find_symbol(symtab, param[i].name, TRUE))
358 report_param_error(fp, name, param[i].name, "error: name conflicts with another method or a keyword");
359 bOk = FALSE;
361 } /* End of parameter loop */
362 /* Check parameters of existing methods */
363 sym = _gmx_sel_first_symbol(symtab, SYMBOL_METHOD);
364 while (sym)
366 gmx_ana_selmethod_t *method = _gmx_sel_sym_value_method(sym);
367 gmx_ana_selparam_t *param =
368 gmx_ana_selmethod_find_param(name, method);
369 if (param)
371 report_param_error(fp, method->name, param->name, "error: name conflicts with another method or a keyword");
372 bOk = FALSE;
374 sym = _gmx_sel_next_symbol(sym, SYMBOL_METHOD);
376 return bOk;
379 /*! \brief
380 * Checks the validity of selection method callback functions.
382 * \param[in] fp File handle to use for diagnostic messages
383 * (can be NULL).
384 * \param[in] method The method to check.
385 * \returns TRUE if there are no problems, FALSE otherwise.
387 * This function performs some checks common to both check_method() and
388 * check_modifier().
389 * This function checks that all the required callbacks are defined, i.e.,
390 * not NULL, to find programming errors.
392 static bool
393 check_callbacks(FILE *fp, gmx_ana_selmethod_t *method)
395 bool bOk = TRUE;
396 bool bNeedInit;
397 bool bNeedFree;
398 int i;
400 /* Make some checks on init_data and free */
401 if (method->nparams > 0 && !method->init_data)
403 report_error(fp, method->name, "error: init_data should be provided because the method has parameters");
404 bOk = FALSE;
406 if (method->free && !method->init_data)
408 report_error(fp, method->name, "warning: free is not used because of missing init_data");
410 /* Check presence of outinit for position-valued methods */
411 if (method->type == POS_VALUE && !method->outinit)
413 report_error(fp, method->name, "error: outinit should be provided because the method has POS_VALUE");
414 bOk = FALSE;
416 /* Warn of dynamic callbacks in static methods */
417 if (!(method->flags & SMETH_MODIFIER))
419 if (method->init_frame && !(method->flags & SMETH_DYNAMIC))
421 report_error(fp, method->name, "warning: init_frame not used because the method is static");
423 if (method->pupdate && !(method->flags & SMETH_DYNAMIC))
425 report_error(fp, method->name, "warning: pupdate not used because the method is static");
426 method->pupdate = NULL;
429 /* Check that there is an evaluation function */
430 if (method->type != NO_VALUE && !method->update && !method->pupdate)
432 report_error(fp, method->name, "error: evaluation function missing");
433 bOk = FALSE;
435 /* Loop through the parameters to determine if initialization callbacks
436 * are needed. */
437 bNeedInit = FALSE;
438 bNeedFree = FALSE;
439 for (i = 0; i < method->nparams; ++i)
441 if (method->param[i].val.type == POS_VALUE
442 || method->param[i].val.type == GROUP_VALUE)
444 bNeedFree = TRUE;
446 if (method->param[i].val.type != POS_VALUE
447 && (method->param[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
449 bNeedInit = TRUE;
450 bNeedFree = TRUE;
453 /* Check that the callbacks required by the parameters are present */
454 if (bNeedInit && !method->init)
456 report_error(fp, method->name, "error: init should be provided");
457 bOk = FALSE;
459 if (bNeedFree && !method->free)
461 report_error(fp, method->name, "error: free should be provided");
462 bOk = FALSE;
464 return bOk;
468 * Checks the validity of a selection method.
470 * \param[in] fp File handle to use for diagnostic messages
471 * (can be NULL).
472 * \param[in,out] method Method to check.
473 * \param[in] symtab Symbol table (used for checking overlaps).
475 * Checks the validity of the given selection method data structure
476 * that does not have \ref SMETH_MODIFIER set.
477 * If you remove a check, please make sure that the selection parser,
478 * compiler, and evaluation functions can deal with the method.
480 static bool
481 check_method(FILE *fp, gmx_ana_selmethod_t *method, gmx_sel_symtab_t *symtab)
483 bool bOk = TRUE;
485 /* Check the type */
486 if (method->type == NO_VALUE)
488 report_error(fp, method->name, "error: no value type specified");
489 bOk = FALSE;
491 if (method->type == STR_VALUE && method->nparams > 0)
493 report_error(fp, method->name, "error: evaluates to a string but is not a keyword");
494 bOk = FALSE;
496 /* Check flags */
497 if (method->type == GROUP_VALUE)
499 /* Group methods should always have SMETH_SINGLEVAL,
500 * so set it for convenience. */
501 method->flags |= SMETH_SINGLEVAL;
502 /* Check that conflicting flags are not present. */
503 if (method->flags & SMETH_VARNUMVAL)
505 report_error(fp, method->name, "error: SMETH_VARNUMVAL cannot be set for group-valued methods");
506 bOk = FALSE;
509 else
511 if ((method->flags & SMETH_SINGLEVAL)
512 && (method->flags & SMETH_VARNUMVAL))
514 report_error(fp, method->name, "error: SMETH_SINGLEVAL and SMETH_VARNUMVAL both set");
515 bOk = FALSE;
518 if ((method->flags & SMETH_CHARVAL) && method->type != STR_VALUE)
520 report_error(fp, method->name, "error: SMETH_CHARVAL can only be specified for STR_VALUE methods");
521 bOk = FALSE;
523 /* Check the parameters */
524 if (!check_params(fp, method->name, method->nparams, method->param, symtab))
526 bOk = FALSE;
528 /* Check the callback pointers */
529 if (!check_callbacks(fp, method))
531 bOk = FALSE;
534 return bOk;
538 * Checks the validity of a selection modifier method.
540 * \param[in] fp File handle to use for diagnostic messages
541 * (can be NULL).
542 * \param[in,out] method Method to check.
543 * \param[in] symtab Symbol table (used for checking overlaps).
545 * Checks the validity of the given selection method data structure
546 * that has \ref SMETH_MODIFIER set.
547 * If you remove a check, please make sure that the selection parser,
548 * compiler, and evaluation functions can deal with the method.
550 static bool
551 check_modifier(FILE *fp, gmx_ana_selmethod_t *method, gmx_sel_symtab_t *symtab)
553 bool bOk = TRUE;
555 /* Check the type */
556 if (method->type != NO_VALUE && method->type != POS_VALUE)
558 report_error(fp, method->name, "error: modifier should have type POS_VALUE or NO_VALUE");
559 bOk = FALSE;
561 /* Check flags */
562 if (method->flags & (SMETH_SINGLEVAL | SMETH_VARNUMVAL))
564 report_error(fp, method->name, "error: modifier should not have SMETH_SINGLEVAL or SMETH_VARNUMVAL set");
565 bOk = FALSE;
567 /* Check the parameters */
568 /* The first parameter is skipped */
569 if (!check_params(fp, method->name, method->nparams-1, method->param+1, symtab))
571 bOk = FALSE;
573 /* Check the callback pointers */
574 if (!check_callbacks(fp, method))
576 bOk = FALSE;
578 if (method->update)
580 report_error(fp, method->name, "error: modifier should not have update");
581 bOk = FALSE;
583 if (method->type == POS_VALUE && !method->pupdate)
585 report_error(fp, method->name, "error: evaluation function missing");
586 bOk = FALSE;
589 return bOk;
593 * \param[in,out] sc Selection collection to registered the method to.
594 * \param[in] name Name under which the method should be registered.
595 * \param[in] method Method to register.
596 * \returns 0 on success, EINVAL if there was something wrong with the
597 * method.
599 * \p name does not need to match the name of the method, and the same
600 * method can be registered multiple times under different names.
601 * If \p name equals some previously registered name,
602 * an error message is printed and the method is not registered.
604 * The function also performs some sanity checking on the input method,
605 * and refuses to register it if there are problems.
606 * Some problems only generate warnings.
607 * All problems are described to \p stderr.
610 gmx_ana_selmethod_register(struct gmx_ana_selcollection_t *sc,
611 const char *name, gmx_ana_selmethod_t *method)
613 bool bOk;
615 /* Check the method */
616 if (method->flags & SMETH_MODIFIER)
618 bOk = check_modifier(stderr, method, sc->symtab);
620 else
622 bOk = check_method(stderr, method, sc->symtab);
624 /* Try to register the method if everything is ok */
625 if (bOk)
627 if (!_gmx_sel_add_method_symbol(sc->symtab, name, method))
629 bOk = FALSE;
632 if (!bOk)
634 report_error(stderr, name, "warning: not registered");
635 return EINVAL;
637 return 0;
641 * \param[in,out] sc Selection collection to registered the methods to.
642 * \returns 0 on success, -1 if any of the default methods could not be
643 * registered.
646 gmx_ana_selmethod_register_defaults(struct gmx_ana_selcollection_t *sc)
648 size_t i;
649 int rc;
650 bool bOk;
652 bOk = TRUE;
653 for (i = 0; i < asize(smtable_def); ++i)
655 rc = gmx_ana_selmethod_register(sc, smtable_def[i]->name, smtable_def[i]);
656 if (rc != 0)
658 bOk = FALSE;
661 return bOk ? 0 : -1;
665 * \param[in] name Name of the parameter to search.
666 * \param[in] method Method to search for the parameter.
667 * \returns Pointer to the parameter in the
668 * \ref gmx_ana_selmethod_t::param "method->param" array,
669 * or NULL if no parameter with name \p name was found.
671 * This is a simple wrapper for gmx_ana_selparam_find().
673 gmx_ana_selparam_t *
674 gmx_ana_selmethod_find_param(const char *name, gmx_ana_selmethod_t *method)
676 return gmx_ana_selparam_find(name, method->nparams, method->param);