Merge "Improve gmx::test::CommandLine."
[alexxy/gromacs.git] / src / gromacs / selection / selectioncollection-impl.h
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief
33  * Declares private implementation class for gmx::SelectionCollection.
34  *
35  * This header also defines ::gmx_ana_selcollection_t, which is used in the old
36  * C code for handling selection collections.
37  *
38  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
39  * \ingroup module_selection
40  */
41 #ifndef GMX_SELECTION_SELECTIONCOLLECTION_IMPL_H
42 #define GMX_SELECTION_SELECTIONCOLLECTION_IMPL_H
43
44 #include <string>
45 #include <vector>
46
47 #include "../legacyheaders/typedefs.h"
48
49 #include "../options/options.h"
50 #include "../utility/uniqueptr.h"
51 #include "indexutil.h"
52 #include "poscalc.h"
53 #include "selection.h" // For gmx::SelectionList
54 #include "selectioncollection.h"
55
56 namespace gmx
57 {
58 //! Smart pointer for managing an internal selection data object.
59 typedef gmx_unique_ptr<internal::SelectionData>::type SelectionDataPointer;
60 //! Container for storing a list of selections internally.
61 typedef std::vector<SelectionDataPointer> SelectionDataList;
62 }
63
64 /*! \internal \brief
65  * Information for a collection of selections.
66  *
67  * \ingroup module_selection
68  */
69 struct gmx_ana_selcollection_t
70 {
71     /** Root of the selection element tree. */
72     struct t_selelem           *root;
73     /*! \brief
74      * Array of compiled selections.
75      *
76      * Has the responsibility of managing the memory for the contained objects,
77      * but note that gmx::Selection instances also hold pointers to the
78      * objects.
79      */
80     gmx::SelectionDataList         sel;
81     /** Number of variables defined. */
82     int                            nvars;
83     /** Selection strings for variables. */
84     char                         **varstrs;
85
86     /** Topology for the collection. */
87     t_topology                    *top;
88     /** Index group that contains all the atoms. */
89     struct gmx_ana_index_t         gall;
90     /** Position calculation collection used for selection position evaluation. */
91     gmx::PositionCalculationCollection  pcc;
92     /** Memory pool used for selection evaluation. */
93     struct gmx_sel_mempool_t      *mempool;
94     /** Parser symbol table. */
95     struct gmx_sel_symtab_t     *symtab;
96 };
97
98 namespace gmx
99 {
100
101 class MessageStringCollector;
102
103 /*! \internal \brief
104  * Private implemention class for SelectionCollection.
105  *
106  * \ingroup module_selection
107  */
108 class SelectionCollection::Impl
109 {
110     public:
111         /*! \brief
112          * Creates a new selection collection.
113          *
114          * \throws  std::bad_alloc if out of memory.
115          */
116         Impl();
117         ~Impl();
118
119         /*! \brief
120          * Clears the symbol table of the selection collection.
121          *
122          * Does not throw.
123          */
124         void clearSymbolTable();
125         /*! \brief
126          * Helper function that runs the parser once the tokenizer has been
127          * initialized.
128          *
129          * \param[in,out] scanner Scanner data structure.
130          * \param[in]     maxnr   Maximum number of selections to parse
131          *      (if -1, parse as many as provided by the user).
132          * \returns       Vector of parsed selections.
133          * \throws        std::bad_alloc if out of memory.
134          * \throws        InvalidInputError if there is a parsing error.
135          *
136          * Used internally to implement parseFromStdin(), parseFromFile() and
137          * parseFromString().
138          */
139         SelectionList runParser(void *scanner, int maxnr);
140         /*! \brief
141          * Replace group references by group contents.
142          *
143          * \param[in]    root    Root of selection tree to process.
144          * \param        errors  Object for reporting any error messages.
145          *
146          * Recursively searches the selection tree for unresolved external
147          * references.  If one is found, finds the referenced group in
148          * \a _grps and replaces the reference with a constant element that
149          * contains the atoms from the referenced group.  Any failures to
150          * resolve references are reported to \p errors.
151          *
152          * Does not throw currently, but this is subject to change when more
153          * underlying code is converted to C++.
154          */
155         void resolveExternalGroups(struct t_selelem *root,
156                                    MessageStringCollector *errors);
157
158         //! Internal data, used for interfacing with old C code.
159         gmx_ana_selcollection_t _sc;
160         //! Options object for setting global properties on the collection.
161         Options                 _options;
162         //! Default reference position type for selections.
163         std::string             _rpost;
164         //! Default output position type for selections.
165         std::string             _spost;
166         /*! \brief
167          * Debugging level for the collection.
168          *
169          * Possible values:
170          *  - 0: no debugging
171          *  - 1: print selection trees after parsing and compilation
172          *  - 2: like 1, also print intermediate compilation trees
173          *  - 3: like 1, also print the tree after evaluation
174          *  - 4: combine 2 and 3
175          */
176         int                     _debugLevel;
177         //! Whether setIndexGroups() has been called.
178         bool                    _bExternalGroupsSet;
179         //! External index groups (can be NULL).
180         gmx_ana_indexgrps_t    *_grps;
181 };
182
183 /*! \internal \brief
184  * Implements selection evaluation.
185  *
186  * This class is used to implement SelectionCollection::evaluate() and
187  * SelectionCollection::evaluateFinal().
188  *
189  * \ingroup module_selection
190  */
191 class SelectionEvaluator
192 {
193     public:
194         SelectionEvaluator();
195
196         /*! \brief
197          * Evaluates selections in a collection.
198          */
199         void evaluate(SelectionCollection *sc, t_trxframe *fr, t_pbc *pbc);
200         /*! \brief
201          * Evaluates the final state for dynamic selections.
202          */
203         void evaluateFinal(SelectionCollection *sc, int nframes);
204 };
205
206 } // namespace gmx
207
208 #endif