Reinstantiate unsorted index group check
[alexxy/gromacs.git] / src / gromacs / selection / selmethod.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2009,2010,2011,2012,2013,2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal
36  * \page page_module_selection_custom Custom selection methods
37  *
38  * Custom selection methods are defined by creating a new instance of
39  * \c gmx_ana_selmethod_t and filling it with the necessary data for handling
40  * the selection.
41  * The structure contains callback pointers that define the actual behavior
42  * of the method.
43  * The following sections discuss how the structure should be filled and how
44  * to implement the callbacks.
45  *
46  *
47  * \section selmethods_define \c gmx_ana_selmethod_t data structure
48  *
49  * An example \c gmx_ana_selmethod_t definition could look like this:
50  *
51  * \code
52    gmx_ana_selmethod_t sm_example = {
53        "example", GROUP_VALUE, 0,
54        asize(sm_params_example), sm_params_example,
55        &init_data_example,
56         NULL,
57        &init_example,
58         NULL,
59        &free_data_example,
60        &init_frame_example,
61        &evaluate_example,
62         NULL,
63        {"example from POS_EXPR [cutoff REAL]", 0, NULL},
64    };
65  * \endcode
66  *
67  * The first value defines the name of the method.
68  * It is used mostly for informational purposes; the actual name(s) recognized
69  * by the selection parser are defined by the call to
70  * gmx_ana_selmethod_register() (see \ref selmethods_register).
71  *
72  * The second value defines the type of the value the method returns.
73  * Possible values are
74  *  - \ref NO_VALUE : This is allowed only for methods that have the flag
75  *    \ref SMETH_MODIFIER set (see \ref selmethods_modifiers).
76  *  - \ref INT_VALUE : The method returns one or more integer values.
77  *  - \ref REAL_VALUE : The method returns one or more floating-point values.
78  *  - \ref STR_VALUE : The method returns one or more strings.
79  *  - \ref POS_VALUE : The method returns one or more 3D vectors.
80  *  - \ref GROUP_VALUE : The method returns a single index group.
81  *
82  * The third value gives additional information about the method using
83  * a combination of flags.
84  * Possible flags are:
85  *  - \ref SMETH_REQTOP : If set, the topology information is always loaded
86  *    and the \p top pointer passed to the callbacks is guaranteed to be
87  *    non-NULL. Should be set if the method requires topology information
88  *    for evaluation.
89  *  - \ref SMETH_DYNAMIC : If set, the method can only be evaluated dynamically,
90  *    i.e., it requires data from the trajectory frame.
91  *  - \ref SMETH_MODIFIER : If set, the method is a selection modifier and
92  *    not an actual selection method.
93  *    For more details, see \ref selmethods_modifiers.
94  *  - \ref SMETH_ALLOW_UNSORTED : If set, the method supports unsorted atoms
95  *    in its input parameters. \ref SMETH_MODIFIER methods are assumed to always
96  *    support unsorted atoms, as their purpose is to affect the ordering.
97  *
98  * There are two additional flags that specify the number of values the
99  * method returns. Only one of them can be set at a time.
100  * If neither is set, the default behavior is to evaluate a value for each
101  * input atom (except for \ref GROUP_VALUE methods, which always return a
102  * single group).
103  * Other behaviors can be specified with these flags:
104  *  - \ref SMETH_SINGLEVAL : If set, the method evaluates to a single value.
105  *    This is automatically set if the type is \ref GROUP_VALUE.
106  *  - \ref SMETH_VARNUMVAL : If set, the method evaluates to an arbitrary
107  *    number of values.
108  *    The number of values is determined based on the values given by the user
109  *    to the method parameters (see \ref selmethods_params).
110  *  .
111  * If either of these flags is specified (and the method type is not
112  * \ref GROUP_VALUE), the group passed to the evaluation callback should not
113  * be used as it can be NULL.
114  * Currently, the above flags only work (have been tested) for \ref POS_VALUE
115  * methods.
116  *
117  * There is one additional flag that can only be specified for \ref STR_VALUE
118  * methods: \ref SMETH_CHARVAL . It is meant for to ease implementation of
119  * methods that evaluate to strings consisting of single characters.
120  *
121  * The next two values determine the number of parameters and a pointer to
122  * the parameter array. The contents of the parameter array are described in
123  * \ref selmethods_params. If the method does not take parameters, the first
124  * value should be 0 and the second can be NULL.
125  * Currently, \ref STR_VALUE methods cannot take parameters, but this limitation
126  * should be easy to lift if required.
127  *
128  * These are followed by function callbacks that determine the
129  * actual behavior of the method. Any of these except the evaluation callback
130  * can be NULL (the evaluation callback can also be NULL if \ref NO_VALUE is
131  * specified for a selection modifier). However, the presence of parameters
132  * can require some of the callbacks to be implemented.
133  * The details are described in \ref selmethods_callbacks.
134  *
135  * Finally, there is a data structure that gives help texts for the method.
136  *
137  * The \c gmx_ana_selmethod_t variable should be declared as a global variable
138  * or it should be otherwise ensured that the structure is not freed: only a
139  * pointer to the structure is stored by the library.
140  *
141  *
142  * \section selmethods_params Defining parameters
143  *
144  * Parameters to selection methods are defined in a separate array of
145  * \c gmx_ana_selparam_t structures.
146  * The order of the parameters does not matter (except possibly for callback
147  * implementation), with one important exception:
148  * If the method evaluates to a \ref POS_VALUE, the first parameter should
149  * have \ref GROUP_VALUE and be the one that is used to calculate the
150  * positions.
151  *
152  * An example parameter definition:
153  * \code
154    static gmx_ana_selparam_t sm_params_example[] = {
155      {"cutoff", {REAL_VALUE, 1, {NULL}}, NULL, SPAR_OPTIONAL},
156      {"from",   {POS_VALUE, -1, {NULL}}, NULL, SPAR_DYNAMIC | SPAR_VARNUM},
157    };
158  * \endcode
159  *
160  * The first value gives the name of the parameter.
161  * The first parameter can have a NULL name, which means that the value should
162  * immediately follow the method name. This can be used to specify methods
163  * of the type 'within 5 of ...'.
164  *
165  * The second value specifies the type of the value that the parameter accepts.
166  * \ref NO_VALUE can be used to specify a boolean parameter, other possibilities
167  * are the same as for the selection method type.
168  *
169  * The third value gives the number of values that the parameter accepts.
170  * For boolean parameters (\ref NO_VALUE), it should be 0.
171  * For parameters with \ref SPAR_VARNUM of \ref SPAR_ATOMVAL, it should be set
172  * to -1 for consistency (it is not used).
173  * If \ref SPAR_RANGES is specified, it should be either 1 (to accept a single
174  * continuous range) or -1 (if combined with \ref SPAR_VARNUM).
175  * In all other cases, it should be a positive integer; in most cases, it
176  * should be 1.
177  *
178  * The nest two pointers should always be NULL (they should be initialized in
179  * the callbacks), except the first pointer in the case of \ref SPAR_ENUMVAL
180  * (see below).
181  *
182  * The final value gives additional information about the acceptable values
183  * for the parameter using a combination of flags.
184  * The possible flags are:
185  *  - \ref SPAR_OPTIONAL : If set, the user does not need to provide a value
186  *    for the parameter. If not set, an error is reported if the parameter
187  *    is not specified by the user.
188  *  - \ref SPAR_DYNAMIC : If set, the method can handle dynamic values for
189  *    the parameter, i.e., the value(s) can be given by an expression that
190  *    evaluates to different values for different frames.
191  *  - \ref SPAR_RANGES : Can be set only for \ref INT_VALUE and
192  *    \ref REAL_VALUE parameters,
193  *    and cannot be combined with \ref SPAR_DYNAMIC.
194  *    If set, the parameter accepts ranges of values.
195  *    The ranges are automatically sorted and compacted such that a minimum
196  *    amount of non-overlapping ranges are given for the method.
197  *  - \ref SPAR_VARNUM : If set, the parameter can have a variable number
198  *    of values. These can be provided by the user as a list of values, or
199  *    using a single \ref SMETH_VARNUMVAL (or a single \ref SMETH_SINGLEVAL)
200  *    method.
201  *  - \ref SPAR_ATOMVAL : If set, the parameter accepts either a single value
202  *    or an expression that evaluates to a value for each input atom.
203  *    The single input value is treated as if the same value was returned for
204  *    each atom.
205  *    Cannot be combined with \ref SPAR_RANGES or \ref SPAR_VARNUM.
206  *  - \ref SPAR_ENUMVAL : Can only be set for \ref STR_VALUE parameters that
207  *    take a single value, and cannot be combined with any other flag than
208  *    \ref SPAR_OPTIONAL. If set, the parameter only accepts one of predefined
209  *    string values. See \ref SPAR_ENUMVAL documentation for details on how
210  *    to specify the acceptable values.
211  *
212  *
213  * \section selmethods_callbacks Implementing callbacks
214  *
215  * There are eight differen callback functions that can be implemented for
216  * selection methods: sel_datafunc(), sel_posfunc(), sel_initfunc(),
217  * sel_outinitfunc(), sel_freefunc(), sel_framefunc(), and two update functions.
218  * They are in this order in the \c gmx_ana_selmethod_t data structure.
219  * In general, any of the callbacks can be NULL, but the presence of
220  * parameters or other callbacks imposes some restrictions:
221  *  - sel_datafunc() should be provided if the method takes parameters.
222  *  - sel_initfunc() should be provided if the method takes
223  *    any parameters with the \ref SPAR_VARNUM or \ref SPAR_ATOMVAL flags,
224  *    except if those parameters have a \ref POS_VALUE.
225  *  - sel_outinitfunc() should be provided for \ref POS_VALUE methods
226  *    and \ref SMETH_VARNUMVAL methods.
227  *  - sel_freefunc() should be provided if sel_datafunc() and/or
228  *    sel_initfunc() allocate any dynamic memory in addition to the data
229  *    structure itself (or allocates the data structure using some other means
230  *    than malloc()).
231  *  - sel_updatefunc_pos() only makes sense for methods with \ref SMETH_DYNAMIC
232  *    set.
233  *  - At least one update function should be provided unless the method type is
234  *    \ref NO_VALUE.
235  *
236  * The documentations for the function pointer types provide more information
237  * about how the callbacks should be implemented.
238  *
239  *
240  * \section selmethods_modifiers Selection modifiers
241  *
242  * Selection modifiers are a special kind of selection methods that can be
243  * appended to the end of a selection. They are specified by adding the
244  * \ref SMETH_MODIFIER flag to the \c gmx_ana_selmethod_t.
245  * They can have two different types:
246  *  - \ref POS_VALUE : These modifiers are given the final positions
247  *    as an input, and they can make modifications to the selection that are
248  *    not possible otherwise (e.g., permute the atoms).
249  *    The modifier should implement sel_updatefunc_pos() and also have
250  *    one NULL parameter in the beginning of the parameter list that takes
251  *    \ref POS_VALUE and is used to give the input positions.
252  *  - \ref NO_VALUE : These modifiers do not modify the final selection, but
253  *    can be used to implement per-selection options for analysis tools
254  *    or to control the default behavior of the selection engine
255  *    (currently, such a framework is not implemented, but should be easy to
256  *    implement if required).
257  *
258  * In addition to restricting the type of the method, selection modifiers
259  * do not allow the flags \ref SMETH_SINGLEVAL and \ref SMETH_VARNUMVAL
260  * (they would not make sense).
261  *
262  * Parameters and callbacks should be implemented as with normal selection
263  * method, but beware that very little of the functionality has been tested.
264  *
265  * \todo
266  * The modifier handling could be made more flexible and more generic;
267  * the current implementation does not allow many things which would be
268  * possible with slight changes in the internals of the library.
269  *
270  *
271  * \section selmethods_register Registering the method
272  *
273  * After defining the method with \c gmx_ana_selmethod_t, it should be
274  * registered with the selection engine.
275  * In analysis programs, this can be done by calling
276  * gmx_ana_selmethod_register().
277  * If adding the method to the library, you should add a pointer to the new
278  * method structure into the \c smtable_def array (in selmethod.cpp), and it is
279  * registered automatically.
280  * In both cases, gmx_ana_selmethod_register() does several checks on the
281  * structure and reports any errors or inconsistencies it finds.
282  */
283 /*! \internal \file
284  * \brief API for handling selection methods.
285  *
286  * There should be no need to use the data structures or call the
287  * functions in this file directly unless implementing a custom selection
288  * method.
289  *
290  * Instructions for implementing custom selection methods can be found
291  * on a separate page: \ref page_module_selection_custom
292  *
293  * \author Teemu Murtola <teemu.murtola@gmail.com>
294  * \ingroup module_selection
295  */
296 #ifndef GMX_SELECTION_SELMETHOD_H
297 #define GMX_SELECTION_SELMETHOD_H
298
299 #include "../legacyheaders/typedefs.h"
300
301 #include "indexutil.h"
302 #include "selparam.h"
303 #include "selvalue.h"
304
305 namespace gmx
306 {
307 class PositionCalculationCollection;
308 class SelectionParserSymbolTable;
309 } // namespace gmx
310
311 struct gmx_ana_pos_t;
312 struct gmx_ana_selcollection_t;
313
314 /*! \name Selection method flags
315  * \anchor selmethod_flags
316  */
317 /*@{*/
318 /*! \brief
319  * If set, the method requires topology information.
320  */
321 #define SMETH_REQTOP     1
322 /*! \brief
323  * If set, the method can only be evaluated dynamically.
324  */
325 #define SMETH_DYNAMIC    2
326 /*! \brief
327  * If set, the method evaluates to a single value.
328  *
329  * The default is that the method evaluates to a value for each input atom.
330  * Cannot be combined with \ref SMETH_VARNUMVAL.
331  */
332 #define SMETH_SINGLEVAL  4
333 /*! \brief
334  * If set, the method evaluates to an arbitrary number of values.
335  *
336  * The default is that the method evaluates to a value for each input atom.
337  * Cannot be combined with \ref SMETH_SINGLEVAL or with \ref GROUP_VALUE.
338  */
339 #define SMETH_VARNUMVAL  8
340 /*! \brief
341  * If set, the method evaluates to single-character strings.
342  *
343  * This flag can only be set for \ref STR_VALUE methods. If it is set, the
344  * selection engine automatically allocates and frees the required strings.
345  * The evaluation function should store the character values as the first
346  * character in the strings in the output data structure and should not change
347  * the string pointers.
348  */
349 #define SMETH_CHARVAL    64
350 /*! \brief
351  * If set, the method accepts unsorted atoms in its input parameters.
352  *
353  * Currently, the support for this functionality is fairly limited, and only
354  * static index group references can actually contain unsorted atoms.
355  * But to make this single case work, the position evaluation must support
356  * unsorted atoms as well.
357  */
358 #define SMETH_ALLOW_UNSORTED 128
359 /*! \brief
360  * If set, the method is a selection modifier.
361  *
362  * The method type should be \ref GROUP_VALUE or \ref NO_VALUE .
363  * Cannot be combined with \ref SMETH_SINGLEVAL or \ref SMETH_VARNUMVAL .
364  */
365 #define SMETH_MODIFIER   256
366 /*@}*/
367
368 /*! \brief
369  * Allocates and initializes internal data and parameter values.
370  *
371  * \param[in]     npar  Number of parameters in \p param.
372  * \param[in,out] param Pointer to (a copy of) the method's
373  *   \c gmx_ana_selmethod_t::param.
374  * \returns       Pointer to method-specific data structure.
375  *   This pointer will be passed as the last parameter of all other function
376  *   calls.
377  * \throws        unspecified Any errors should be indicated by throwing an
378  *      exception.
379  *
380  * Should allocate and initialize any internal data required by the method.
381  * Should also initialize the value pointers (\c gmx_ana_selparam_t::val) in
382  * \p param to point to variables within the internal data structure,
383  * with the exception of parameters that specify the \ref SPAR_VARNUM or
384  * the \ref SPAR_ATOMVAL flag (these should be handled in sel_initfunc()).
385  * However, parameters with a position value should be initialized.
386  * It is also possible to initialize \ref SPAR_ENUMVAL statically outside
387  * this function (see \ref SPAR_ENUMVAL).
388  * The \c gmx_ana_selparam_t::nvalptr should also be initialized for
389  * non-position-valued parameters that have both \ref SPAR_VARNUM and
390  * \ref SPAR_DYNAMIC set (it can also be initialized for other parameters if
391  * desired, but the same information will be available through other means).
392  * For optional parameters, the default values can (and should) be initialized
393  * here, as the parameter values are not changed if the parameter is not
394  * provided.
395  *
396  * For boolean parameters (type equals \ref NO_VALUE), the default value
397  * should be set here. The user can override the value by giving the parameter
398  * either as 'NAME'/'noNAME', or as 'NAME on/off/yes/no'.
399  *
400  * If the method takes any parameters, this function must be provided.
401  */
402 typedef void *(*sel_datafunc)(int npar, gmx_ana_selparam_t *param);
403 /*! \brief
404  * Sets the position calculation collection for the method.
405  *
406  * \param[in]  pcc   Position calculation collection that the method should use
407  *   for position calculations.
408  * \param      data  Internal data structure from sel_datafunc().
409  *
410  * This function should be provided if the method uses the routines from
411  * poscalc.h for calculating positions.
412  * The pointer \p pcc should then be stored and used for initialization for
413  * any position calculation structures.
414  */
415 typedef void  (*sel_posfunc)(gmx::PositionCalculationCollection *pcc, void *data);
416 /*! \brief
417  * Does initialization based on topology and/or parameter values.
418  *
419  * \param[in]  top   Topology structure
420  *   (can be NULL if \ref SMETH_REQTOP is not set).
421  * \param[in]  npar  Number of parameters in \p param.
422  * \param[in]  param Pointer to (an initialized copy of) the method's
423  *   \c gmx_ana_selmethod_t::param.
424  * \param      data  Internal data structure from sel_datafunc().
425  * \returns    0 on success, a non-zero error code on failure.
426  *
427  * This function is called after the parameters have been processed:
428  * the values of the parameters are stored at the locations set in
429  * sel_datafunc().
430  * The flags \ref SPAR_DYNAMIC and \ref SPAR_ATOMVAL are cleared before
431  * calling the function if the value is static or single-valued, respectively.
432  * If a parameter had the \ref SPAR_VARNUM or \ref SPAR_ATOMVAL flag (and
433  * is not \ref POS_VALUE), a pointer to the memory allocated for the values is
434  * found in \c gmx_ana_selparam_t::val.
435  * The pointer should be stored by this function, otherwise the values
436  * cannot be accessed.
437  * For \ref SPAR_VARNUM parameters, the number of values can be accessed
438  * through \c gmx_ana_selparam_t::val. For parameters with \ref SPAR_DYNAMIC,
439  * the number is the maximum number of values (the actual number can be
440  * accessed in sel_framefunc() and in the update callback through the value
441  * pointed by \c gmx_ana_selparam_t::nvalptr).
442  * For \ref SPAR_ATOMVAL parameters, \c gmx_ana_selparam_t::val::nr is set to
443  * 1 if a single value was provided, otherwise it is set to the maximum number
444  * of values possibly passed to the method.
445  * The value pointed by \c gmx_ana_selparam_t::nvalptr always contains the same
446  * value as \c gmx_ana_selparam_t::val::nr.
447  *
448  * For dynamic \ref GROUP_VALUE parameters (\ref SPAR_DYNAMIC set), the value
449  * will be the largest possible selection that may occur during the
450  * evaluation. For other types of dynamic parameters, the values are
451  * undefined.
452  *
453  * If the method takes any parameters with the \ref SPAR_VARNUM or
454  * \ref SPAR_ATOMVAL flags, this function must be provided, except if these
455  * parameters all have \ref POS_VALUE.
456  *
457  * This function may be called multiple times for the same method if the
458  * method takes parameters with \ref SPAR_ATOMVAL set.
459  */
460 typedef void  (*sel_initfunc)(t_topology *top, int npar,
461                               gmx_ana_selparam_t *param, void *data);
462 /*! \brief
463  * Initializes output data structure.
464  *
465  * \param[in]     top   Topology structure
466  *   (can be NULL if \ref SMETH_REQTOP is not set).
467  * \param[in,out] out   Output data structure.
468  * \param[in]     data  Internal data structure from sel_datafunc().
469  * \returns       0 on success, an error code on error.
470  *
471  * This function is called immediately after sel_initfunc().
472  *
473  * If the method evaluates to a position (\ref POS_VALUE), this function
474  * should be provided, and it should initialize the \c gmx_ana_pos_t data
475  * structure pointed by \p out.p (the pointer is guaranteed to be non-NULL).
476  * The \p out.p->g pointer should be initialized to the group that is used
477  * to evaluate positions in sel_updatefunc() or sel_updatefunc_pos().
478  *
479  * The function should also be provided for non-position-valued
480  * \ref SMETH_VARNUMVAL methods. For these methods, it suffices to set the
481  * \p out->nr field to reflect the maximum number of values returned by the
482  * method.
483  *
484  * Currently, this function is not needed for other types of methods.
485  *
486  * This function may be called multiple times for the same method if the
487  * method takes parameters with \ref SPAR_ATOMVAL set.
488  */
489 typedef void  (*sel_outinitfunc)(t_topology *top, gmx_ana_selvalue_t *out,
490                                  void *data);
491 /*! \brief
492  * Frees the internal data.
493  *
494  * \param[in] data Internal data structure from sel_datafunc().
495  *
496  * This function should be provided if the internal data structure contains
497  * dynamically allocated data, and should free any such data.
498  * The data structure itself should also be freed.
499  * For convenience, if there is no dynamically allocated data within the
500  * structure and the structure is allocated using malloc()/snew(), this
501  * function is not needed: the selection engine automatically frees the
502  * structure using sfree().
503  * Any memory pointers received as values of parameters are managed externally,
504  * and should not be freed.
505  * Pointers set as the value pointer of \ref SPAR_ENUMVAL parameters should not
506  * be freed.
507  */
508 typedef void  (*sel_freefunc)(void *data);
509
510 /*! \brief
511  * Initializes the evaluation for a new frame.
512  *
513  * \param[in]  top  Topology structure
514  *   (can be NULL if \ref SMETH_REQTOP is not set).
515  * \param[in]  fr   Current frame.
516  * \param[in]  pbc  Initialized periodic boundary condition structure,
517  *   or NULL if PBC should not be used.
518  * \param      data Internal data structure from sel_datafunc().
519  * \returns    0 on success, a non-zero error code on failure.
520  *
521  * This function should be implemented if the selection method needs to
522  * do some preprocessing for each frame, and the preprocessing does not
523  * depend on the evaluation group.
524  * Because \p sel_updatefunc_* can be called more than once for a frame,
525  * it is inefficient do the preprocessing there.
526  * It is ensured that this function will be called before
527  * \p sel_updatefunc_* for each frame, and that it will be called at most
528  * once for each frame.
529  * For static methods, it is called once, with \p fr and \p pbc set to
530  * NULL.
531  */
532 typedef void  (*sel_framefunc)(t_topology *top, t_trxframe *fr, t_pbc *pbc,
533                                void *data);
534 /*! \brief
535  * Evaluates a selection method.
536  *
537  * \param[in]  top  Topology structure
538  *   (can be NULL if \ref SMETH_REQTOP is not set).
539  * \param[in]  fr   Current frame.
540  * \param[in]  pbc  Initialized periodic boundary condition structure,
541  *   or NULL if PBC should not be used.
542  * \param[in]  g    Index group for which the method should be evaluated.
543  * \param[out] out  Output data structure.
544  * \param      data Internal data structure from sel_datafunc().
545  * \returns    0 on success, a non-zero error code on error.
546  *
547  * This function should evaluate the method for each atom included in \p g,
548  * and write the output to \p out. The pointer in the union \p out->u that
549  * corresponds to the type of the method should be used.
550  * Enough memory has been allocated to store the output values.
551  * The number of values in \p out should also be updated if necessary.
552  * However, \ref POS_VALUE or \ref GROUP_VALUE methods should not touch
553  * \p out->nr (it should be 1 anyways).
554  *
555  * For \ref STR_VALUE methods, the pointers stored in \p out->s are discarded
556  * without freeing; it is the responsibility of this function to provide
557  * pointers that can be discarded without memory leaks.
558  */
559 typedef void  (*sel_updatefunc)(t_topology *top, t_trxframe *fr, t_pbc *pbc,
560                                 gmx_ana_index_t *g, gmx_ana_selvalue_t *out,
561                                 void *data);
562 /*! \brief
563  * Evaluates a selection method using positions.
564  *
565  * \param[in]  top  Topology structure
566  *   (can be NULL if \ref SMETH_REQTOP is not set).
567  * \param[in]  fr   Current frame.
568  * \param[in]  pbc  Initialized periodic boundary condition structure,
569  *   or NULL if PBC should not be used.
570  * \param[in]  pos  Positions for which the method should be evaluated.
571  * \param[out] out  Output data structure.
572  * \param      data Internal data structure from sel_datafunc().
573  * \returns    0 on success, a non-zero error code on error.
574  *
575  * This function should evaluate the method for each position in \p g,
576  * and write the output values to \p out. The pointer in the union \p out->u
577  * that corresponds to the type of the method should be used.
578  * Enough memory has been allocated to store the output values.
579  * The number of values in \p out should also be updated if necessary.
580  * However, \ref POS_VALUE or \ref GROUP_VALUE methods should not touch
581  * \p out->nr (it should be 1 anyways).
582  *
583  * For \ref STR_VALUE methods, the pointers stored in \p out->s are discarded
584  * without freeing; it is the responsibility of this function to provide
585  * pointers that can be discarded without memory leaks.
586  */
587 typedef void  (*sel_updatefunc_pos)(t_topology *top, t_trxframe *fr, t_pbc *pbc,
588                                     struct gmx_ana_pos_t *pos,
589                                     gmx_ana_selvalue_t *out,
590                                     void *data);
591
592 /*! \internal
593  * \brief
594  * Help information for a selection method.
595  *
596  * If some information is not available, the corresponding field can be set to
597  * 0/NULL.
598  */
599 typedef struct gmx_ana_selmethod_help_t
600 {
601     /*! \brief
602      * One-line description of the syntax of the method.
603      *
604      * If NULL, the name of the method is used.
605      */
606     const char         *syntax;
607     /*! \brief
608      * Number of strings in \p help.
609      *
610      * Set to 0 if \p help is NULL.
611      */
612     int                 nlhelp;
613     /*! \brief
614      * Detailed help for the method.
615      *
616      * If there is no help available in addition to \p syntax, this can be set
617      * to NULL.
618      */
619     const char        **help;
620 } gmx_ana_selmethod_help_t;
621
622 /*! \internal
623  * \brief
624  * Describes a selection method.
625  *
626  * Any of the function pointers except the update call can be NULL if the
627  * operation is not required or not supported. In this case,
628  * corresponding function calls are skipped.
629  *
630  * See the function pointer type documentation for details of how the
631  * functions should be implemented.
632  * More details on implementing new selection methods can be found on a
633  * separate page: \ref page_module_selection_custom.
634  */
635 typedef struct gmx_ana_selmethod_t
636 {
637     /** Name of the method. */
638     const char         *name;
639     /** Type which the method returns. */
640     e_selvalue_t        type;
641     /*! \brief
642      * Flags to specify how the method should be handled.
643      *
644      * See \ref selmethod_flags for allowed values.
645      */
646     int                 flags;
647     /** Number of parameters the method takes. */
648     int                 nparams;
649     /** Pointer to the array of parameter descriptions. */
650     gmx_ana_selparam_t *param;
651
652     /** Function for allocating and initializing internal data and parameters. */
653     sel_datafunc        init_data;
654     /** Function to set the position calculation collection. */
655     sel_posfunc         set_poscoll;
656     /** Function to do initialization based on topology and/or parameter values. */
657     sel_initfunc        init;
658     /** Function to initialize output data structure. */
659     sel_outinitfunc     outinit;
660     /** Function to free the internal data. */
661     sel_freefunc        free;
662
663     /** Function to initialize the calculation for a new frame. */
664     sel_framefunc       init_frame;
665     /** Function to evaluate the value. */
666     sel_updatefunc      update;
667     /** Function to evaluate the value using positions. */
668     sel_updatefunc_pos  pupdate;
669
670     /** Help data for the method. */
671     gmx_ana_selmethod_help_t help;
672 } gmx_ana_selmethod_t;
673
674 /** Registers a selection method. */
675 int
676 gmx_ana_selmethod_register(gmx::SelectionParserSymbolTable *symtab,
677                            const char *name, gmx_ana_selmethod_t *method);
678 /** Registers all selection methods in the library. */
679 int
680 gmx_ana_selmethod_register_defaults(gmx::SelectionParserSymbolTable *symtab);
681
682 /** Finds a parameter from a selection method by name. */
683 gmx_ana_selparam_t *
684 gmx_ana_selmethod_find_param(const char *name, gmx_ana_selmethod_t *method);
685
686 #endif