Add check for unsorted index groups in selections.
[gromacs.git] / src / gromacs / selection / selectioncollection.h
bloba00ae0484b30b0ba2b222655b00a2af63a16332e
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013, 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.
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.
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.
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.
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.
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.
35 /*! \file
36 * \brief
37 * Declares gmx::SelectionCollection.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \inpublicapi
41 * \ingroup module_selection
43 #ifndef GMX_SELECTION_SELECTIONCOLLECTION_H
44 #define GMX_SELECTION_SELECTIONCOLLECTION_H
46 #include <string>
47 #include <vector>
49 #include "../legacyheaders/typedefs.h"
51 #include "../onlinehelp/helptopicinterface.h"
52 #include "../utility/common.h"
53 #include "selection.h" // For gmx::SelectionList
55 struct gmx_ana_indexgrps_t;
57 namespace gmx
60 class Options;
61 class SelectionCompiler;
62 class SelectionEvaluator;
64 /*! \brief
65 * Collection of selections.
67 * This class is the main interface to the core of the selection engine.
68 * It is used to initialize and manage a collection of selections that share
69 * the same topology. Selections within one collection can share variables and
70 * can be optimized together. Selections from two different collections do not
71 * interact.
73 * The constructor creates an empty selection collection object. To initialize
74 * the object, either call initOptions(), or both setReferencePosType() and
75 * setOutputPosType(). See these methods for more details on the
76 * initialization options.
78 * After setting the default values, one or more selections can be parsed with
79 * one or more calls to parseFromStdin(), parseFromFile(), and/or
80 * parseFromString(). After all selections are parsed, the topology must be
81 * set with setTopology() unless requiresTopology() returns false (the topology
82 * can also be set earlier).
83 * setIndexGroups() must also be called if external index group references are
84 * used in the selections; it can be called at any point before compile().
85 * Once all selections are parsed, they must be compiled all at once using
86 * compile().
88 * After compilation, dynamic selections have the maximum number of atoms they
89 * can evaluate to, but positions have undefined values (see \ref Selection and
90 * SelectionPosition). evaluate() can be used to update the selections for a
91 * new frame. evaluateFinal() can be called after all the frames have been
92 * processed to restore the selection values back to the ones they were after
93 * compile().
95 * At any point, requiresTopology() can be called to see whether the
96 * information provided so far requires loading the topology.
97 * printTree() can be used to print the internal representation of the
98 * selections (mostly useful for debugging).
100 * Note that for trajectory analysis using TrajectoryAnalysisModule, the
101 * SelectionCollection object is managed by Gromacs, and \ref Selection objects
102 * are obtained from SelectionOption.
104 * \inpublicapi
105 * \ingroup module_selection
107 class SelectionCollection
109 public:
110 /*! \brief
111 * Creates a help tree for selections.
113 * \throws std::bad_alloc if out of memory.
114 * \returns Root topic of the created selection tree.
116 static HelpTopicPointer createDefaultHelpTopic();
118 /*! \brief
119 * Creates an empty selection collection.
121 * \throws std::bad_alloc if out of memory.
123 SelectionCollection();
124 ~SelectionCollection();
126 /*! \brief
127 * Initializes options for setting global properties on the collection.
129 * \param[in,out] options Options object to initialize.
130 * \throws std::bad_alloc if out of memory.
132 * Adds options to \p options that can be used to set the default
133 * position types (see setReferencePosType() and setOutputPosType())
134 * and debugging flags.
136 void initOptions(Options *options);
138 /*! \brief
139 * Sets the default reference position handling for a selection
140 * collection.
142 * \param[in] type Default selection reference position type
143 * (one of the strings acceptable for
144 * PositionCalculationCollection::typeFromEnum()).
145 * \throws InternalError if \p type is invalid.
147 * Should be called before calling the parser functions, unless
148 * initOptions() has been called. In the latter case, can still be
149 * used to override the default value (before initOptions() is called)
150 * and/or the value provided through the Options object.
152 * Strong exception safety.
154 void setReferencePosType(const char *type);
155 /*! \brief
156 * Sets the default reference position handling for a selection
157 * collection.
159 * \param[in] type Default selection output position type
160 * (one of the strings acceptable for
161 * PositionCalculationCollection::typeFromEnum()).
162 * \throws InternalError if \p type is invalid.
164 * Should be called before calling the parser functions, unless
165 * initOptions() has been called. In the latter case, can still be
166 * used to override the default value (before initOptions() is called)
167 * and/or the value provided through the Options object.
169 * Strong exception safety.
171 void setOutputPosType(const char *type);
172 /*! \brief
173 * Sets the debugging level for the selection collection.
175 * \param[in] debugLevel Debug level to set (0 = no debug
176 * information).
178 * initOptions() creates debugging options that can also be used to set
179 * the debug level. These are normally hidden, but if this method is
180 * called before initOptions() with a non-zero \p debugLevel, they are
181 * made visible.
183 * Mostly useful for debugging tools.
185 * Does not throw.
187 void setDebugLevel(int debugLevel);
189 /*! \brief
190 * Returns true if the collection requires topology information for
191 * evaluation.
193 * \returns true if any selection in the collection requires topology
194 * information.
196 * Before the parser functions have been called, the return value is
197 * based just on the position types set.
198 * After parser functions have been called, the return value also takes
199 * into account the selection keywords used.
201 * Does not throw.
203 bool requiresTopology() const;
204 /*! \brief
205 * Sets the topology for the collection.
207 * \param[in] top Topology data.
208 * \param[in] natoms Number of atoms. If <=0, the number of
209 * atoms in the topology is used.
211 * Either the topology must be provided, or \p natoms must be > 0.
213 * \p natoms determines the largest atom index that can be selected by
214 * the selection: even if the topology contains more atoms, they will
215 * not be selected.
217 * Does not throw currently, but this is subject to change when more
218 * underlying code is converted to C++.
220 void setTopology(t_topology *top, int natoms);
221 /*! \brief
222 * Sets the external index groups to use for the selections.
224 * \param[in] grps Index groups to use for the selections.
225 * \throws std::bad_alloc if out of memory.
226 * \throws InconsistentInputError if a group reference cannot be resolved.
228 * Only the first call to this method can have a non-NULL \p grps.
229 * At this point, any selections that have already been provided are
230 * searched for references to external groups, and the references are
231 * replaced by the contents of the groups. If any referenced group
232 * cannot be found in \p grps (or if \p grps is NULL and there are any
233 * references), InconsistentInputError is thrown.
235 * The selection collection keeps a reference to \p grps until this
236 * method is called with a NULL \p grps.
237 * If this method is not called before compile(), it is automatically
238 * called as setIndexGroups(NULL).
240 void setIndexGroups(gmx_ana_indexgrps_t *grps);
241 /*! \brief
242 * Parses selection(s) from standard input.
244 * \param[in] count Number of selections to parse
245 * (if -1, parse as many as provided by the user).
246 * \param[in] bInteractive Whether the parser should behave
247 * interactively.
248 * \returns Vector of parsed selections.
249 * \throws std::bad_alloc if out of memory.
250 * \throws InvalidInputError if there is a parsing error
251 * (an interactive parser only throws this if too few selections
252 * are provided and the user forced the end of input).
254 * The returned objects remain valid for the lifetime of
255 * the selection collection.
256 * Some information about the selections only becomes available once
257 * compile() has been called; see \ref Selection.
259 SelectionList parseFromStdin(int count, bool bInteractive);
260 /*! \brief
261 * Parses selection(s) from a file.
263 * \param[in] filename Name of the file to parse selections from.
264 * \returns Vector of parsed selections.
265 * \throws std::bad_alloc if out of memory.
266 * \throws InvalidInputError if there is a parsing error.
268 * The returned objects remain valid for the lifetime of
269 * the selection collection.
270 * Some information about the selections only becomes available once
271 * compile() has been called; see \ref Selection.
273 SelectionList parseFromFile(const std::string &filename);
274 /*! \brief
275 * Parses selection(s) from a string.
277 * \param[in] str String to parse selections from.
278 * \returns Vector of parsed selections.
279 * \throws std::bad_alloc if out of memory.
280 * \throws InvalidInputError if there is a parsing error.
282 * The returned objects remain valid for the lifetime of
283 * the selection collection.
284 * Some information about the selections only becomes available once
285 * compile() has been called; see \ref Selection.
287 SelectionList parseFromString(const std::string &str);
288 /*! \brief
289 * Prepares the selections for evaluation and performs optimizations.
291 * \throws InconsistentInputError if topology is required but not set.
292 * \throws InvalidInputError if setIndexGroups() has not been called
293 * and there are index group references.
294 * \throws unspecified if compilation fails (TODO: list/reduce these).
296 * Before compilation, selections should have been added to the
297 * collection using the parseFrom*() functions.
298 * The compiled selection collection can be passed to evaluate() to
299 * evaluate the selection for a frame.
300 * Before the compiled selection is evaluated, the selections indicate
301 * the maximal set of atoms/positions to which they can be evaluated;
302 * see \ref Selection.
304 * If an error occurs, the collection is cleared.
306 * The covered fraction information is initialized to ::CFRAC_NONE for
307 * all selections.
309 void compile();
310 /*! \brief
311 * Evaluates selections in the collection.
313 * \param[in] fr Frame for which the evaluation should be carried out.
314 * \param[in] pbc PBC data, or NULL if no PBC should be used.
315 * \throws unspeficied Multiple possible exceptions to indicate
316 * evaluation failure (TODO: enumerate).
318 void evaluate(t_trxframe *fr, t_pbc *pbc);
319 /*! \brief
320 * Evaluates the largest possible index groups from dynamic selections.
322 * \param[in] nframes Total number of frames.
324 * This method restores the selections to the state they were after
325 * compile().
327 * \p nframes should equal the number of times evaluate() has been
328 * called.
330 * Does not throw.
332 void evaluateFinal(int nframes);
334 /*! \brief
335 * Prints a human-readable version of the internal selection element
336 * tree.
338 * \param[in] fp File handle to receive the output.
339 * \param[in] bValues If true, the evaluated values of selection
340 * elements are printed as well.
342 * The output is very techical, and intended for debugging purposes.
344 * Does not throw.
346 void printTree(FILE *fp, bool bValues) const;
347 /*! \brief
348 * Prints the selection strings into an XVGR file as comments.
350 * \param[in] fp Output file.
351 * \param[in] oenv Output options structure.
353 * Does not throw.
355 void printXvgrInfo(FILE *fp, output_env_t oenv) const;
357 private:
358 class Impl;
360 PrivateImplPointer<Impl> impl_;
362 /*! \brief
363 * Needed for the compiler to freely modify the collection.
365 friend class SelectionCompiler;
366 /*! \brief
367 * Needed for the evaluator to freely modify the collection.
369 friend class SelectionEvaluator;
372 } // namespace gmx
374 #endif