Merge branch release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / selection / selectioncollection-impl.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2009,2010,2011,2012, by the GROMACS development team, led by
5  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6  * others, as listed in the AUTHORS file in the top-level source
7  * 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 \file
36  * \brief
37  * Declares private implementation class for gmx::SelectionCollection.
38  *
39  * This header also defines ::gmx_ana_selcollection_t, which is used in the old
40  * C code for handling selection collections.
41  *
42  * \author Teemu Murtola <teemu.murtola@gmail.com>
43  * \ingroup module_selection
44  */
45 #ifndef GMX_SELECTION_SELECTIONCOLLECTION_IMPL_H
46 #define GMX_SELECTION_SELECTIONCOLLECTION_IMPL_H
47
48 #include <string>
49 #include <vector>
50
51 #include <boost/scoped_ptr.hpp>
52
53 #include "../legacyheaders/typedefs.h"
54
55 #include "../onlinehelp/helptopicinterface.h"
56 #include "../utility/uniqueptr.h"
57 #include "indexutil.h"
58 #include "poscalc.h"
59 #include "selection.h" // For gmx::SelectionList
60 #include "selectioncollection.h"
61 #include "selelem.h"
62
63 namespace gmx
64 {
65
66 //! Smart pointer for managing an internal selection data object.
67 typedef gmx_unique_ptr<internal::SelectionData>::type SelectionDataPointer;
68 //! Container for storing a list of selections internally.
69 typedef std::vector<SelectionDataPointer> SelectionDataList;
70
71 class SelectionParserSymbolTable;
72
73 } // namespace gmx
74
75 /*! \internal \brief
76  * Information for a collection of selections.
77  *
78  * \ingroup module_selection
79  */
80 struct gmx_ana_selcollection_t
81 {
82     //! Position calculation collection used for selection position evaluation.
83     gmx::PositionCalculationCollection  pcc;
84     //! Root of the selection element tree.
85     gmx::SelectionTreeElementPointer    root;
86     /*! \brief
87      * Array of compiled selections.
88      *
89      * Has the responsibility of managing the memory for the contained objects,
90      * but note that gmx::Selection instances also hold pointers to the
91      * objects.
92      */
93     gmx::SelectionDataList         sel;
94     /** Number of variables defined. */
95     int                            nvars;
96     /** Selection strings for variables. */
97     char                         **varstrs;
98
99     /** Topology for the collection. */
100     t_topology                                        *top;
101     /** Index group that contains all the atoms. */
102     struct gmx_ana_index_t                             gall;
103     /** Memory pool used for selection evaluation. */
104     struct gmx_sel_mempool_t                          *mempool;
105     //! Parser symbol table.
106     boost::scoped_ptr<gmx::SelectionParserSymbolTable> symtab;
107     //! Root of help topic tree (NULL is no help yet requested).
108     gmx::HelpTopicPointer                              rootHelp;
109 };
110
111 namespace gmx
112 {
113
114 class MessageStringCollector;
115
116 /*! \internal \brief
117  * Private implemention class for SelectionCollection.
118  *
119  * \ingroup module_selection
120  */
121 class SelectionCollection::Impl
122 {
123     public:
124         /*! \brief
125          * Creates a new selection collection.
126          *
127          * \throws  std::bad_alloc if out of memory.
128          */
129         Impl();
130         ~Impl();
131
132         /*! \brief
133          * Clears the symbol table of the selection collection.
134          *
135          * Does not throw.
136          */
137         void clearSymbolTable();
138         /*! \brief
139          * Replace group references by group contents.
140          *
141          * \param[in]    root    Root of selection tree to process.
142          * \param        errors  Object for reporting any error messages.
143          *
144          * Recursively searches the selection tree for unresolved external
145          * references.  If one is found, finds the referenced group in
146          * \a grps_ and replaces the reference with a constant element that
147          * contains the atoms from the referenced group.  Any failures to
148          * resolve references are reported to \p errors.
149          *
150          * Does not throw currently, but this is subject to change when more
151          * underlying code is converted to C++.
152          */
153         void resolveExternalGroups(const gmx::SelectionTreeElementPointer &root,
154                                    MessageStringCollector                 *errors);
155
156         //! Internal data, used for interfacing with old C code.
157         gmx_ana_selcollection_t sc_;
158         //! Default reference position type for selections.
159         std::string             rpost_;
160         //! Default output position type for selections.
161         std::string             spost_;
162         /*! \brief
163          * Debugging level for the collection.
164          *
165          * Possible values:
166          *  - 0: no debugging
167          *  - 1: print selection trees after parsing and compilation
168          *  - 2: like 1, also print intermediate compilation trees
169          *  - 3: like 1, also print the tree after evaluation
170          *  - 4: combine 2 and 3
171          */
172         int                     debugLevel_;
173         //! Whether setIndexGroups() has been called.
174         bool                    bExternalGroupsSet_;
175         //! External index groups (can be NULL).
176         gmx_ana_indexgrps_t    *grps_;
177 };
178
179 /*! \internal \brief
180  * Implements selection evaluation.
181  *
182  * This class is used to implement SelectionCollection::evaluate() and
183  * SelectionCollection::evaluateFinal().
184  *
185  * \ingroup module_selection
186  */
187 class SelectionEvaluator
188 {
189     public:
190         SelectionEvaluator();
191
192         /*! \brief
193          * Evaluates selections in a collection.
194          */
195         void evaluate(SelectionCollection *sc, t_trxframe *fr, t_pbc *pbc);
196         /*! \brief
197          * Evaluates the final state for dynamic selections.
198          */
199         void evaluateFinal(SelectionCollection *sc, int nframes);
200 };
201
202 } // namespace gmx
203
204 #endif