Check frame atom count before evaluating selections
[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 "selparam.h"
300 #include "selvalue.h"
301
302 namespace gmx
303 {
304 class PositionCalculationCollection;
305 class SelectionParserSymbolTable;
306 } // namespace gmx
307
308 struct gmx_ana_index_t;
309 struct gmx_ana_pos_t;
310 struct gmx_ana_selcollection_t;
311 struct t_pbc;
312 struct t_topology;
313 struct t_trxframe;
314
315 /*! \name Selection method flags
316  * \anchor selmethod_flags
317  */
318 /*@{*/
319 /*! \brief
320  * If set, the method requires topology information.
321  */
322 #define SMETH_REQTOP     1
323 /*! \brief
324  * If set, the method can only be evaluated dynamically.
325  */
326 #define SMETH_DYNAMIC    2
327 /*! \brief
328  * If set, the method evaluates to a single value.
329  *
330  * The default is that the method evaluates to a value for each input atom.
331  * Cannot be combined with \ref SMETH_VARNUMVAL.
332  */
333 #define SMETH_SINGLEVAL  4
334 /*! \brief
335  * If set, the method evaluates to an arbitrary number of values.
336  *
337  * The default is that the method evaluates to a value for each input atom.
338  * Cannot be combined with \ref SMETH_SINGLEVAL or with \ref GROUP_VALUE.
339  */
340 #define SMETH_VARNUMVAL  8
341 /*! \brief
342  * If set, the method evaluates to single-character strings.
343  *
344  * This flag can only be set for \ref STR_VALUE methods. If it is set, the
345  * selection engine automatically allocates and frees the required strings.
346  * The evaluation function should store the character values as the first
347  * character in the strings in the output data structure and should not change
348  * the string pointers.
349  */
350 #define SMETH_CHARVAL    64
351 /*! \brief
352  * If set, the method accepts unsorted atoms in its input parameters.
353  *
354  * Currently, the support for this functionality is fairly limited, and only
355  * static index group references can actually contain unsorted atoms.
356  * But to make this single case work, the position evaluation must support
357  * unsorted atoms as well.
358  */
359 #define SMETH_ALLOW_UNSORTED 128
360 /*! \brief
361  * If set, the method is a selection modifier.
362  *
363  * The method type should be \ref GROUP_VALUE or \ref NO_VALUE .
364  * Cannot be combined with \ref SMETH_SINGLEVAL or \ref SMETH_VARNUMVAL .
365  */
366 #define SMETH_MODIFIER   256
367 /*@}*/
368
369 /*! \brief
370  * Allocates and initializes internal data and parameter values.
371  *
372  * \param[in]     npar  Number of parameters in \p param.
373  * \param[in,out] param Pointer to (a copy of) the method's
374  *   \c gmx_ana_selmethod_t::param.
375  * \returns       Pointer to method-specific data structure.
376  *   This pointer will be passed as the last parameter of all other function
377  *   calls.
378  * \throws        unspecified Any errors should be indicated by throwing an
379  *      exception.
380  *
381  * Should allocate and initialize any internal data required by the method.
382  * Should also initialize the value pointers (\c gmx_ana_selparam_t::val) in
383  * \p param to point to variables within the internal data structure,
384  * with the exception of parameters that specify the \ref SPAR_VARNUM or
385  * the \ref SPAR_ATOMVAL flag (these should be handled in sel_initfunc()).
386  * However, parameters with a position value should be initialized.
387  * It is also possible to initialize \ref SPAR_ENUMVAL statically outside
388  * this function (see \ref SPAR_ENUMVAL).
389  * The \c gmx_ana_selparam_t::nvalptr should also be initialized for
390  * non-position-valued parameters that have both \ref SPAR_VARNUM and
391  * \ref SPAR_DYNAMIC set (it can also be initialized for other parameters if
392  * desired, but the same information will be available through other means).
393  * For optional parameters, the default values can (and should) be initialized
394  * here, as the parameter values are not changed if the parameter is not
395  * provided.
396  *
397  * For boolean parameters (type equals \ref NO_VALUE), the default value
398  * should be set here. The user can override the value by giving the parameter
399  * either as 'NAME'/'noNAME', or as 'NAME on/off/yes/no'.
400  *
401  * If the method takes any parameters, this function must be provided.
402  */
403 typedef void *(*sel_datafunc)(int npar, gmx_ana_selparam_t *param);
404 /*! \brief
405  * Sets the position calculation collection for the method.
406  *
407  * \param[in]  pcc   Position calculation collection that the method should use
408  *   for position calculations.
409  * \param      data  Internal data structure from sel_datafunc().
410  *
411  * This function should be provided if the method uses the routines from
412  * poscalc.h for calculating positions.
413  * The pointer \p pcc should then be stored and used for initialization for
414  * any position calculation structures.
415  */
416 typedef void  (*sel_posfunc)(gmx::PositionCalculationCollection *pcc, void *data);
417 /*! \brief
418  * Does initialization based on topology and/or parameter values.
419  *
420  * \param[in]  top   Topology structure
421  *   (can be NULL if \ref SMETH_REQTOP is not set).
422  * \param[in]  npar  Number of parameters in \p param.
423  * \param[in]  param Pointer to (an initialized copy of) the method's
424  *   \c gmx_ana_selmethod_t::param.
425  * \param      data  Internal data structure from sel_datafunc().
426  * \returns    0 on success, a non-zero error code on failure.
427  *
428  * This function is called after the parameters have been processed:
429  * the values of the parameters are stored at the locations set in
430  * sel_datafunc().
431  * The flags \ref SPAR_DYNAMIC and \ref SPAR_ATOMVAL are cleared before
432  * calling the function if the value is static or single-valued, respectively.
433  * If a parameter had the \ref SPAR_VARNUM or \ref SPAR_ATOMVAL flag (and
434  * is not \ref POS_VALUE), a pointer to the memory allocated for the values is
435  * found in \c gmx_ana_selparam_t::val.
436  * The pointer should be stored by this function, otherwise the values
437  * cannot be accessed.
438  * For \ref SPAR_VARNUM parameters, the number of values can be accessed
439  * through \c gmx_ana_selparam_t::val. For parameters with \ref SPAR_DYNAMIC,
440  * the number is the maximum number of values (the actual number can be
441  * accessed in sel_framefunc() and in the update callback through the value
442  * pointed by \c gmx_ana_selparam_t::nvalptr).
443  * For \ref SPAR_ATOMVAL parameters, \c gmx_ana_selparam_t::val::nr is set to
444  * 1 if a single value was provided, otherwise it is set to the maximum number
445  * of values possibly passed to the method.
446  * The value pointed by \c gmx_ana_selparam_t::nvalptr always contains the same
447  * value as \c gmx_ana_selparam_t::val::nr.
448  *
449  * For dynamic \ref GROUP_VALUE parameters (\ref SPAR_DYNAMIC set), the value
450  * will be the largest possible selection that may occur during the
451  * evaluation. For other types of dynamic parameters, the values are
452  * undefined.
453  *
454  * If the method takes any parameters with the \ref SPAR_VARNUM or
455  * \ref SPAR_ATOMVAL flags, this function must be provided, except if these
456  * parameters all have \ref POS_VALUE.
457  *
458  * This function may be called multiple times for the same method if the
459  * method takes parameters with \ref SPAR_ATOMVAL set.
460  */
461 typedef void  (*sel_initfunc)(t_topology *top, int npar,
462                               gmx_ana_selparam_t *param, void *data);
463 /*! \brief
464  * Initializes output data structure.
465  *
466  * \param[in]     top   Topology structure
467  *   (can be NULL if \ref SMETH_REQTOP is not set).
468  * \param[in,out] out   Output data structure.
469  * \param[in]     data  Internal data structure from sel_datafunc().
470  * \returns       0 on success, an error code on error.
471  *
472  * This function is called immediately after sel_initfunc().
473  *
474  * If the method evaluates to a position (\ref POS_VALUE), this function
475  * should be provided, and it should initialize the \c gmx_ana_pos_t data
476  * structure pointed by \p out.p (the pointer is guaranteed to be non-NULL).
477  * The \p out.p->g pointer should be initialized to the group that is used
478  * to evaluate positions in sel_updatefunc() or sel_updatefunc_pos().
479  *
480  * The function should also be provided for non-position-valued
481  * \ref SMETH_VARNUMVAL methods. For these methods, it suffices to set the
482  * \p out->nr field to reflect the maximum number of values returned by the
483  * method.
484  *
485  * Currently, this function is not needed for other types of methods.
486  *
487  * This function may be called multiple times for the same method if the
488  * method takes parameters with \ref SPAR_ATOMVAL set.
489  */
490 typedef void  (*sel_outinitfunc)(t_topology *top, gmx_ana_selvalue_t *out,
491                                  void *data);
492 /*! \brief
493  * Frees the internal data.
494  *
495  * \param[in] data Internal data structure from sel_datafunc().
496  *
497  * This function should be provided if the internal data structure contains
498  * dynamically allocated data, and should free any such data.
499  * The data structure itself should also be freed.
500  * For convenience, if there is no dynamically allocated data within the
501  * structure and the structure is allocated using malloc()/snew(), this
502  * function is not needed: the selection engine automatically frees the
503  * structure using sfree().
504  * Any memory pointers received as values of parameters are managed externally,
505  * and should not be freed.
506  * Pointers set as the value pointer of \ref SPAR_ENUMVAL parameters should not
507  * be freed.
508  */
509 typedef void  (*sel_freefunc)(void *data);
510
511 /*! \brief
512  * Initializes the evaluation for a new frame.
513  *
514  * \param[in]  top  Topology structure
515  *   (can be NULL if \ref SMETH_REQTOP is not set).
516  * \param[in]  fr   Current frame.
517  * \param[in]  pbc  Initialized periodic boundary condition structure,
518  *   or NULL if PBC should not be used.
519  * \param      data Internal data structure from sel_datafunc().
520  * \returns    0 on success, a non-zero error code on failure.
521  *
522  * This function should be implemented if the selection method needs to
523  * do some preprocessing for each frame, and the preprocessing does not
524  * depend on the evaluation group.
525  * Because \p sel_updatefunc_* can be called more than once for a frame,
526  * it is inefficient do the preprocessing there.
527  * It is ensured that this function will be called before
528  * \p sel_updatefunc_* for each frame, and that it will be called at most
529  * once for each frame.
530  * For static methods, it is called once, with \p fr and \p pbc set to
531  * NULL.
532  */
533 typedef void  (*sel_framefunc)(t_topology *top, t_trxframe *fr, t_pbc *pbc,
534                                void *data);
535 /*! \brief
536  * Evaluates a selection method.
537  *
538  * \param[in]  top  Topology structure
539  *   (can be NULL if \ref SMETH_REQTOP is not set).
540  * \param[in]  fr   Current frame.
541  * \param[in]  pbc  Initialized periodic boundary condition structure,
542  *   or NULL if PBC should not be used.
543  * \param[in]  g    Index group for which the method should be evaluated.
544  * \param[out] out  Output data structure.
545  * \param      data Internal data structure from sel_datafunc().
546  * \returns    0 on success, a non-zero error code on error.
547  *
548  * This function should evaluate the method for each atom included in \p g,
549  * and write the output to \p out. The pointer in the union \p out->u that
550  * corresponds to the type of the method should be used.
551  * Enough memory has been allocated to store the output values.
552  * The number of values in \p out should also be updated if necessary.
553  * However, \ref POS_VALUE or \ref GROUP_VALUE methods should not touch
554  * \p out->nr (it should be 1 anyways).
555  *
556  * For \ref STR_VALUE methods, the pointers stored in \p out->s are discarded
557  * without freeing; it is the responsibility of this function to provide
558  * pointers that can be discarded without memory leaks.
559  *
560  * If the method accesses \p fr outside the index group specified in \p g or
561  * what it receives from its parameters, it must check that \p fr actually
562  * contains such an atom in case the \p fr has been loaded from a trajectory
563  * that only contains a subset of the system.
564  */
565 typedef void  (*sel_updatefunc)(t_topology *top, t_trxframe *fr, t_pbc *pbc,
566                                 gmx_ana_index_t *g, gmx_ana_selvalue_t *out,
567                                 void *data);
568 /*! \brief
569  * Evaluates a selection method using positions.
570  *
571  * \param[in]  top  Topology structure
572  *   (can be NULL if \ref SMETH_REQTOP is not set).
573  * \param[in]  fr   Current frame.
574  * \param[in]  pbc  Initialized periodic boundary condition structure,
575  *   or NULL if PBC should not be used.
576  * \param[in]  pos  Positions for which the method should be evaluated.
577  * \param[out] out  Output data structure.
578  * \param      data Internal data structure from sel_datafunc().
579  * \returns    0 on success, a non-zero error code on error.
580  *
581  * This function should evaluate the method for each position in \p g,
582  * and write the output values to \p out. The pointer in the union \p out->u
583  * that corresponds to the type of the method should be used.
584  * Enough memory has been allocated to store the output values.
585  * The number of values in \p out should also be updated if necessary.
586  * However, \ref POS_VALUE or \ref GROUP_VALUE methods should not touch
587  * \p out->nr (it should be 1 anyways).
588  *
589  * For \ref STR_VALUE methods, the pointers stored in \p out->s are discarded
590  * without freeing; it is the responsibility of this function to provide
591  * pointers that can be discarded without memory leaks.
592  *
593  * If the method accesses \p fr outside the atoms referenced in \p pos or
594  * what it receives from its parameters, it must check that \p fr actually
595  * contains such an atom in case the \p fr has been loaded from a trajectory
596  * that only contains a subset of the system.
597  */
598 typedef void  (*sel_updatefunc_pos)(t_topology *top, t_trxframe *fr, t_pbc *pbc,
599                                     gmx_ana_pos_t *pos, gmx_ana_selvalue_t *out,
600                                     void *data);
601
602 /*! \internal
603  * \brief
604  * Help information for a selection method.
605  *
606  * If some information is not available, the corresponding field can be set to
607  * 0/NULL.
608  */
609 struct gmx_ana_selmethod_help_t
610 {
611     /*! \brief
612      * One-line description of the syntax of the method.
613      *
614      * If NULL, the name of the method is used.
615      */
616     const char         *syntax;
617     /*! \brief
618      * Number of strings in \p help.
619      *
620      * Set to 0 if \p help is NULL.
621      */
622     int                 nlhelp;
623     /*! \brief
624      * Detailed help for the method.
625      *
626      * If there is no help available in addition to \p syntax, this can be set
627      * to NULL.
628      */
629     const char        **help;
630 };
631
632 /*! \internal
633  * \brief
634  * Describes a selection method.
635  *
636  * Any of the function pointers except the update call can be NULL if the
637  * operation is not required or not supported. In this case,
638  * corresponding function calls are skipped.
639  *
640  * See the function pointer type documentation for details of how the
641  * functions should be implemented.
642  * More details on implementing new selection methods can be found on a
643  * separate page: \ref page_module_selection_custom.
644  */
645 struct gmx_ana_selmethod_t
646 {
647     /** Name of the method. */
648     const char         *name;
649     /** Type which the method returns. */
650     e_selvalue_t        type;
651     /*! \brief
652      * Flags to specify how the method should be handled.
653      *
654      * See \ref selmethod_flags for allowed values.
655      */
656     int                 flags;
657     /** Number of parameters the method takes. */
658     int                 nparams;
659     /** Pointer to the array of parameter descriptions. */
660     gmx_ana_selparam_t *param;
661
662     /** Function for allocating and initializing internal data and parameters. */
663     sel_datafunc        init_data;
664     /** Function to set the position calculation collection. */
665     sel_posfunc         set_poscoll;
666     /** Function to do initialization based on topology and/or parameter values. */
667     sel_initfunc        init;
668     /** Function to initialize output data structure. */
669     sel_outinitfunc     outinit;
670     /** Function to free the internal data. */
671     sel_freefunc        free;
672
673     /** Function to initialize the calculation for a new frame. */
674     sel_framefunc       init_frame;
675     /** Function to evaluate the value. */
676     sel_updatefunc      update;
677     /** Function to evaluate the value using positions. */
678     sel_updatefunc_pos  pupdate;
679
680     /** Help data for the method. */
681     gmx_ana_selmethod_help_t help;
682 };
683
684 /** Registers a selection method. */
685 int
686 gmx_ana_selmethod_register(gmx::SelectionParserSymbolTable *symtab,
687                            const char *name, gmx_ana_selmethod_t *method);
688 /** Registers all selection methods in the library. */
689 int
690 gmx_ana_selmethod_register_defaults(gmx::SelectionParserSymbolTable *symtab);
691
692 /** Finds a parameter from a selection method by name. */
693 gmx_ana_selparam_t *
694 gmx_ana_selmethod_find_param(const char *name, gmx_ana_selmethod_t *method);
695
696 #endif