SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / selection / selmethod.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2009-2018, The GROMACS development team.
5  * Copyright (c) 2019,2020, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 /*! \internal \file
37  * \brief
38  * Implements functions in selmethod.h.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \ingroup module_selection
42  */
43 #include "gmxpre.h"
44
45 #include "selmethod.h"
46
47 #include <cctype>
48 #include <cstdarg>
49
50 #include "gromacs/utility/arraysize.h"
51 #include "gromacs/utility/cstringutil.h"
52 #include "gromacs/utility/exceptions.h"
53 #include "gromacs/utility/stringutil.h"
54
55 #include "selmethod_impl.h"
56 #include "symrec.h"
57
58 /*! \internal \brief
59  * Helper structure for defining selection methods.
60  */
61 typedef struct
62 {
63     /*! \brief
64      * Name to register the method under.
65      *
66      * If NULL, use the actual name of the method.
67      * This field is used for defining synonyms.
68      */
69     const char* name;
70     /** Method data structure to register. */
71     gmx_ana_selmethod_t* method;
72 } t_register_method;
73
74 /*! \brief
75  * Convenience function for reporting errors found in selection methods.
76  */
77 static void report_error(FILE* fp, const char* name, gmx_fmtstr const char* fmt, ...)
78         gmx_format(printf, 3, 4);
79
80 static void report_error(FILE* fp, const char* name, gmx_fmtstr const char* fmt, ...)
81 {
82     va_list ap;
83     va_start(ap, fmt);
84     if (fp)
85     {
86         fprintf(fp, "selection method '%s': ", name);
87         vfprintf(fp, fmt, ap);
88         fprintf(fp, "\n");
89     }
90     va_end(ap);
91 }
92
93 /*! \brief
94  * Convenience function for reporting errors found in selection method parameters.
95  */
96 static void report_param_error(FILE* fp, const char* mname, const char* pname, gmx_fmtstr const char* fmt, ...)
97         gmx_format(printf, 4, 5);
98 static void report_param_error(FILE* fp, const char* mname, const char* pname, gmx_fmtstr const char* fmt, ...)
99 {
100     va_list ap;
101     va_start(ap, fmt);
102     if (fp)
103     {
104         fprintf(fp, "selection method '%s': parameter '%s': ", mname, pname);
105         vfprintf(fp, fmt, ap);
106         fprintf(fp, "\n");
107     }
108     va_end(ap);
109 }
110
111 /*! \brief
112  * Checks the validity of parameters.
113  *
114  * \param[in]     fp      File handle to use for diagnostic messages
115  *   (can be NULL).
116  * \param[in]     name    Name of the method (used for error messages).
117  * \param[in]     nparams Number of parameters in \p param.
118  * \param[in,out] param   Parameter array
119  *   (only the \c flags field of boolean parameters may be modified).
120  * \param[in]     symtab  Symbol table (used for checking overlaps).
121  * \returns       true if there are no problems with the parameters,
122  *   false otherwise.
123  *
124  * This function performs some checks common to both check_method() and
125  * check_modifier().
126  * The purpose of these checks is to ensure that the selection parser does not
127  * need to check for the validity of the parameters at each turn, and to
128  * report programming errors as early as possible.
129  * If you remove a check, make sure that the parameter parser can handle the
130  * resulting parameters.
131  */
132 static bool check_params(FILE*                                  fp,
133                          const char*                            name,
134                          int                                    nparams,
135                          gmx_ana_selparam_t                     param[],
136                          const gmx::SelectionParserSymbolTable& symtab)
137 {
138     bool bOk = true;
139     int  i, j;
140
141     if (nparams > 0 && !param)
142     {
143         report_error(fp, name, "error: missing parameter data");
144         return false;
145     }
146     if (nparams == 0 && param)
147     {
148         report_error(fp, name, "warning: parameter data unused because nparams=0");
149     }
150     /* Check each parameter */
151     for (i = 0; i < nparams; ++i)
152     {
153         /* Check that there is at most one NULL name, in the beginning */
154         if (param[i].name == nullptr && i > 0)
155         {
156             report_error(fp, name, "error: NULL parameter should be the first one");
157             bOk = false;
158             continue;
159         }
160         /* Check for duplicates */
161         for (j = 0; j < i; ++j)
162         {
163             if (param[j].name == nullptr)
164             {
165                 continue;
166             }
167             if (!gmx_strcasecmp(param[i].name, param[j].name))
168             {
169                 report_error(fp, name, "error: duplicate parameter name '%s'", param[i].name);
170                 bOk = false;
171                 break;
172             }
173         }
174         /* Check flags */
175         if (param[i].flags & SPAR_SET)
176         {
177             report_param_error(fp, name, param[i].name, "warning: flag SPAR_SET is set");
178             param[i].flags &= ~SPAR_SET;
179         }
180         if (param[i].flags & SPAR_RANGES)
181         {
182             if (param[i].val.type != INT_VALUE && param[i].val.type != REAL_VALUE)
183             {
184                 report_param_error(fp,
185                                    name,
186                                    param[i].name,
187                                    "error: SPAR_RANGES cannot be set for a non-numeric parameter");
188                 bOk = false;
189             }
190             if (param[i].flags & SPAR_DYNAMIC)
191             {
192                 report_param_error(fp,
193                                    name,
194                                    param[i].name,
195                                    "warning: SPAR_DYNAMIC does not have effect with SPAR_RANGES");
196                 param[i].flags &= ~SPAR_DYNAMIC;
197             }
198             if (!(param[i].flags & SPAR_VARNUM) && param[i].val.nr != 1)
199             {
200                 report_param_error(
201                         fp,
202                         name,
203                         param[i].name,
204                         "error: range should take either one or an arbitrary number of values");
205                 bOk = false;
206             }
207             if (param[i].flags & SPAR_ATOMVAL)
208             {
209                 report_param_error(
210                         fp, name, param[i].name, "error: SPAR_RANGES and SPAR_ATOMVAL both set");
211                 bOk = false;
212             }
213         }
214         if ((param[i].flags & SPAR_VARNUM) && (param[i].flags & SPAR_ATOMVAL))
215         {
216             report_param_error(
217                     fp, name, param[i].name, "error: SPAR_VARNUM and SPAR_ATOMVAL both set");
218             bOk = false;
219         }
220         if (param[i].flags & SPAR_ENUMVAL)
221         {
222             if (param[i].val.type != STR_VALUE)
223             {
224                 report_param_error(fp,
225                                    name,
226                                    param[i].name,
227                                    "error: SPAR_ENUMVAL can only be set for string parameters");
228                 bOk = false;
229             }
230             if (param[i].val.nr != 1)
231             {
232                 report_param_error(fp,
233                                    name,
234                                    param[i].name,
235                                    "error: SPAR_ENUMVAL parameters should take exactly one value");
236                 bOk = false;
237             }
238             if (param[i].flags & (SPAR_DYNAMIC | SPAR_VARNUM | SPAR_ATOMVAL))
239             {
240                 report_param_error(fp,
241                                    name,
242                                    param[i].name,
243                                    "error: only SPAR_OPTIONAL supported with SPAR_ENUMVAL");
244                 bOk = false;
245             }
246         }
247         /* Check boolean parameters */
248         if (param[i].val.type == NO_VALUE)
249         {
250             if (param[i].val.nr != 0)
251             {
252                 report_param_error(fp,
253                                    name,
254                                    param[i].name,
255                                    "error: number of values should be zero for boolean parameters");
256                 bOk = false;
257             }
258             /* The boolean parameters should always be optional, so set the
259              * flag for convenience. */
260             param[i].flags |= SPAR_OPTIONAL;
261             /* Any other flags should not be specified */
262             if (param[i].flags & ~SPAR_OPTIONAL)
263             {
264                 report_param_error(fp,
265                                    name,
266                                    param[i].name,
267                                    "error: boolean parameter should not have any flags set");
268                 bOk = false;
269             }
270         }
271         /* Check val.nr */
272         if (param[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL))
273         {
274             if (param[i].val.nr != -1)
275             {
276                 report_param_error(
277                         fp,
278                         name,
279                         param[i].name,
280                         "warning: val.nr is not -1 although SPAR_VARNUM/SPAR_ATOMVAL is set");
281             }
282             param[i].val.nr = -1;
283         }
284         else if (param[i].val.type != NO_VALUE)
285         {
286             if (param[i].val.nr <= 0)
287             {
288                 report_param_error(fp, name, param[i].name, "error: val.nr <= 0");
289                 bOk = false;
290             }
291         }
292         /* Check that the value pointer is NULL */
293         if (param[i].nvalptr != nullptr)
294         {
295             report_param_error(fp, name, param[i].name, "warning: nvalptr is set");
296         }
297         if (param[i].val.u.ptr != nullptr && !(param[i].flags & SPAR_ENUMVAL))
298         {
299             report_param_error(fp, name, param[i].name, "warning: value pointer is set");
300         }
301         /* Check that the name contains only valid characters */
302         if (param[i].name == nullptr)
303         {
304             continue;
305         }
306         if (!isalpha(param[i].name[0]))
307         {
308             report_param_error(fp, name, param[i].name, "error: name does not begin with a letter");
309             bOk = false;
310             continue;
311         }
312         for (j = 1; param[i].name[j] != 0; ++j)
313         {
314             if (param[i].name[j] != '_' && !isalnum(param[i].name[j]))
315             {
316                 report_param_error(
317                         fp, name, param[i].name, "error: name contains non-alphanumeric characters");
318                 bOk = false;
319                 break;
320             }
321         }
322         if (param[i].name[j] != 0)
323         {
324             continue;
325         }
326         /* Check that the name does not conflict with a method */
327         if (symtab.findSymbol(param[i].name) != nullptr)
328         {
329             report_param_error(fp,
330                                name,
331                                param[i].name,
332                                "error: name conflicts with another method or a keyword");
333             bOk = false;
334         }
335     } /* End of parameter loop */
336       /* Check parameters of existing methods */
337     gmx::SelectionParserSymbolIterator symbol =
338             symtab.beginIterator(gmx::SelectionParserSymbol::MethodSymbol);
339     while (symbol != symtab.endIterator())
340     {
341         gmx_ana_selmethod_t* method = symbol->methodValue();
342         gmx_ana_selparam_t*  param  = gmx_ana_selmethod_find_param(name, method);
343         if (param)
344         {
345             report_param_error(fp,
346                                method->name,
347                                param->name,
348                                "error: name conflicts with another method or a keyword");
349             bOk = false;
350         }
351         ++symbol;
352     }
353     return bOk;
354 }
355
356 /*! \brief
357  * Checks the validity of selection method callback functions.
358  *
359  * \param[in] fp        File handle to use for diagnostic messages
360  *   (can be NULL).
361  * \param[in] method    The method to check.
362  * \returns   true if there are no problems, false otherwise.
363  *
364  * This function performs some checks common to both check_method() and
365  * check_modifier().
366  * This function checks that all the required callbacks are defined, i.e.,
367  * not NULL, to find programming errors.
368  */
369 static bool check_callbacks(FILE* fp, gmx_ana_selmethod_t* method)
370 {
371     bool bOk = true;
372     bool bNeedInit;
373     int  i;
374
375     /* Make some checks on init_data and free */
376     if (method->nparams > 0 && !method->init_data)
377     {
378         report_error(fp,
379                      method->name,
380                      "error: init_data should be provided because the method has parameters");
381         bOk = false;
382     }
383     if (method->free && !method->init_data)
384     {
385         report_error(fp, method->name, "warning: free is not used because of missing init_data");
386     }
387     /* Check presence of outinit for position-valued methods */
388     if (method->type == POS_VALUE && !method->outinit)
389     {
390         report_error(fp,
391                      method->name,
392                      "error: outinit should be provided because the method has POS_VALUE");
393         bOk = false;
394     }
395     /* Check presence of outinit for variable output count methods */
396     if ((method->flags & SMETH_VARNUMVAL) && !method->outinit)
397     {
398         report_error(fp,
399                      method->name,
400                      "error: outinit should be provided because the method has SMETH_VARNUMVAL");
401         bOk = false;
402     }
403     /* Warn of dynamic callbacks in static methods */
404     if (!(method->flags & SMETH_MODIFIER))
405     {
406         if (method->pupdate && !(method->flags & SMETH_DYNAMIC))
407         {
408             report_error(fp, method->name, "warning: pupdate not used because the method is static");
409             method->pupdate = nullptr;
410         }
411     }
412     /* Check that there is an evaluation function */
413     if (method->type != NO_VALUE && !method->update && !method->pupdate)
414     {
415         report_error(fp, method->name, "error: evaluation function missing");
416         bOk = false;
417     }
418     /* Loop through the parameters to determine if initialization callbacks
419      * are needed. */
420     bNeedInit = false;
421     for (i = 0; i < method->nparams; ++i)
422     {
423         if (method->param[i].val.type != POS_VALUE
424             && (method->param[i].flags & (SPAR_VARNUM | SPAR_ATOMVAL)))
425         {
426             bNeedInit = true;
427         }
428     }
429     /* Check that the callbacks required by the parameters are present */
430     if (bNeedInit && !method->init)
431     {
432         report_error(fp, method->name, "error: init should be provided");
433         bOk = false;
434     }
435     return bOk;
436 }
437
438 /*! \brief
439  * Checks the validity of a selection method.
440  *
441  * \param[in]     fp     File handle to use for diagnostic messages
442  *   (can be NULL).
443  * \param[in,out] method Method to check.
444  * \param[in]     symtab Symbol table (used for checking overlaps).
445  *
446  * Checks the validity of the given selection method data structure
447  * that does not have \ref SMETH_MODIFIER set.
448  * If you remove a check, please make sure that the selection parser,
449  * compiler, and evaluation functions can deal with the method.
450  */
451 static bool check_method(FILE* fp, gmx_ana_selmethod_t* method, const gmx::SelectionParserSymbolTable& symtab)
452 {
453     bool bOk = true;
454
455     /* Check the type */
456     if (method->type == NO_VALUE)
457     {
458         report_error(fp, method->name, "error: no value type specified");
459         bOk = false;
460     }
461     if (method->type == STR_VALUE && method->nparams > 0)
462     {
463         report_error(fp, method->name, "error: evaluates to a string but is not a keyword");
464         bOk = false;
465     }
466     /* Check flags */
467     if (method->type == GROUP_VALUE)
468     {
469         /* Group methods should always have SMETH_SINGLEVAL,
470          * so set it for convenience. */
471         method->flags |= SMETH_SINGLEVAL;
472         /* Check that conflicting flags are not present. */
473         if (method->flags & SMETH_VARNUMVAL)
474         {
475             report_error(fp,
476                          method->name,
477                          "error: SMETH_VARNUMVAL cannot be set for group-valued methods");
478             bOk = false;
479         }
480     }
481     else
482     {
483         if ((method->flags & SMETH_SINGLEVAL) && (method->flags & SMETH_VARNUMVAL))
484         {
485             report_error(fp, method->name, "error: SMETH_SINGLEVAL and SMETH_VARNUMVAL both set");
486             bOk = false;
487         }
488     }
489     if ((method->flags & SMETH_CHARVAL) && method->type != STR_VALUE)
490     {
491         report_error(fp,
492                      method->name,
493                      "error: SMETH_CHARVAL can only be specified for STR_VALUE methods");
494         bOk = false;
495     }
496     /* Check the parameters */
497     if (!check_params(fp, method->name, method->nparams, method->param, symtab))
498     {
499         bOk = false;
500     }
501     /* Check the callback pointers */
502     if (!check_callbacks(fp, method))
503     {
504         bOk = false;
505     }
506
507     return bOk;
508 }
509
510 /*! \brief
511  * Checks the validity of a selection modifier method.
512  *
513  * \param[in]     fp     File handle to use for diagnostic messages
514  *   (can be NULL).
515  * \param[in,out] method Method to check.
516  * \param[in]     symtab Symbol table (used for checking overlaps).
517  *
518  * Checks the validity of the given selection method data structure
519  * that has \ref SMETH_MODIFIER set.
520  * If you remove a check, please make sure that the selection parser,
521  * compiler, and evaluation functions can deal with the method.
522  */
523 static bool check_modifier(FILE* fp, gmx_ana_selmethod_t* method, const gmx::SelectionParserSymbolTable& symtab)
524 {
525     bool bOk = true;
526
527     /* Check the type */
528     if (method->type != NO_VALUE && method->type != POS_VALUE)
529     {
530         report_error(fp, method->name, "error: modifier should have type POS_VALUE or NO_VALUE");
531         bOk = false;
532     }
533     /* Check flags */
534     if (method->flags & (SMETH_SINGLEVAL | SMETH_VARNUMVAL))
535     {
536         report_error(fp,
537                      method->name,
538                      "error: modifier should not have SMETH_SINGLEVAL or SMETH_VARNUMVAL set");
539         bOk = false;
540     }
541     /* Check the parameters */
542     /* The first parameter is skipped */
543     if (!check_params(fp, method->name, method->nparams - 1, method->param + 1, symtab))
544     {
545         bOk = false;
546     }
547     /* Check the callback pointers */
548     if (!check_callbacks(fp, method))
549     {
550         bOk = false;
551     }
552     if (method->update)
553     {
554         report_error(fp, method->name, "error: modifier should not have update");
555         bOk = false;
556     }
557     if (method->type == POS_VALUE && !method->pupdate)
558     {
559         report_error(fp, method->name, "error: evaluation function missing");
560         bOk = false;
561     }
562
563     return bOk;
564 }
565
566 /*!
567  * \param[in,out] symtab Symbol table to register the method to.
568  * \param[in]     name   Name under which the method should be registered.
569  * \param[in]     method Method to register.
570  * \returns       0 on success, -1 if there was something wrong with the
571  *   method.
572  *
573  * \p name does not need to match the name of the method, and the same
574  * method can be registered multiple times under different names.
575  * If \p name equals some previously registered name,
576  * an error message is printed and the method is not registered.
577  *
578  * The function also performs some sanity checking on the input method,
579  * and refuses to register it if there are problems.
580  * Some problems only generate warnings.
581  * All problems are described to \p stderr.
582  */
583 int gmx_ana_selmethod_register(gmx::SelectionParserSymbolTable* symtab,
584                                const char*                      name,
585                                gmx_ana_selmethod_t*             method)
586 {
587     bool bOk;
588
589     /* Check the method */
590     if (method->flags & SMETH_MODIFIER)
591     {
592         bOk = check_modifier(stderr, method, *symtab);
593     }
594     else
595     {
596         bOk = check_method(stderr, method, *symtab);
597     }
598     /* Try to register the method if everything is ok */
599     if (bOk)
600     {
601         try
602         {
603             symtab->addMethod(name, method);
604         }
605         catch (const gmx::APIError& ex)
606         {
607             report_error(stderr, name, "%s", ex.what());
608             bOk = false;
609         }
610     }
611     if (!bOk)
612     {
613         report_error(stderr, name, "warning: not registered");
614         return -1;
615     }
616     return 0;
617 }
618
619 /*!
620  * \param[in,out] symtab Symbol table to register the methods to.
621  * \returns       0 on success, -1 if any of the default methods could not be
622  *   registered.
623  */
624 int gmx_ana_selmethod_register_defaults(gmx::SelectionParserSymbolTable* symtab)
625 {
626     /* Array of selection methods defined in the library. */
627     const t_register_method smtable_def[] = {
628         { nullptr, &sm_cog },         { nullptr, &sm_com },
629
630         { nullptr, &sm_all },         { nullptr, &sm_none },
631         { nullptr, &sm_atomnr },      { nullptr, &sm_resnr },
632         { "resid", &sm_resnr },       { nullptr, &sm_resindex },
633         { "residue", &sm_resindex },  { nullptr, &sm_molindex },
634         { "mol", &sm_molindex },      { "molecule", &sm_molindex },
635         { nullptr, &sm_atomname },    { "name", &sm_atomname },
636         { nullptr, &sm_pdbatomname }, { "pdbname", &sm_pdbatomname },
637         { nullptr, &sm_atomtype },    { "type", &sm_atomtype },
638         { nullptr, &sm_resname },     { nullptr, &sm_insertcode },
639         { nullptr, &sm_chain },       { nullptr, &sm_mass },
640         { nullptr, &sm_charge },      { nullptr, &sm_altloc },
641         { nullptr, &sm_occupancy },   { nullptr, &sm_betafactor },
642         { "beta", &sm_betafactor },   { nullptr, &sm_x },
643         { nullptr, &sm_y },           { nullptr, &sm_z },
644
645         { nullptr, &sm_distance },    { "dist", &sm_distance },
646         { nullptr, &sm_mindistance }, { "mindist", &sm_mindistance },
647         { nullptr, &sm_within },      { nullptr, &sm_insolidangle },
648         { nullptr, &sm_same },
649
650         { nullptr, &sm_merge },       { nullptr, &sm_plus },
651         { nullptr, &sm_permute },
652     };
653
654     int  rc;
655     bool bOk;
656
657     bOk = true;
658     for (int i = 0; i < asize(smtable_def); ++i)
659     {
660         gmx_ana_selmethod_t* method = smtable_def[i].method;
661
662         if (smtable_def[i].name == nullptr)
663         {
664             rc = gmx_ana_selmethod_register(symtab, method->name, method);
665         }
666         else
667         {
668             rc = gmx_ana_selmethod_register(symtab, smtable_def[i].name, method);
669         }
670         if (rc != 0)
671         {
672             bOk = false;
673         }
674     }
675     return bOk ? 0 : -1;
676 }
677
678 /*!
679  * \param[in] name   Name of the parameter to search.
680  * \param[in] method Method to search for the parameter.
681  * \returns   Pointer to the parameter in the
682  *   \ref gmx_ana_selmethod_t::param "method->param" array,
683  *   or NULL if no parameter with name \p name was found.
684  *
685  * This is a simple wrapper for gmx_ana_selparam_find().
686  */
687 gmx_ana_selparam_t* gmx_ana_selmethod_find_param(const char* name, gmx_ana_selmethod_t* method)
688 {
689     return gmx_ana_selparam_find(name, method->nparams, method->param);
690 }