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