Fix -seltype option for C++ tools
[gromacs.git] / src / gromacs / selection / selectioncollection.h
blob1c0d518c3e0bb8b1858d18f1b78f4509e0407f6b
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,2014,2015, 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.
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 <cstdio>
48 #include <string>
49 #include <vector>
51 #include "gromacs/legacyheaders/types/oenv.h"
52 #include "gromacs/selection/selection.h" // For gmx::SelectionList
53 #include "gromacs/utility/classhelpers.h"
55 struct gmx_ana_indexgrps_t;
56 struct t_pbc;
57 struct t_topology;
58 struct t_trxframe;
60 namespace gmx
63 class Options;
64 class SelectionCompiler;
65 class SelectionEvaluator;
67 /*! \brief
68 * Collection of selections.
70 * This class is the main interface to the core of the selection engine.
71 * It is used to initialize and manage a collection of selections that share
72 * the same topology. Selections within one collection can share variables and
73 * can be optimized together. Selections from two different collections do not
74 * interact.
76 * The constructor creates an empty selection collection object. To initialize
77 * the object, either call initOptions(), or both setReferencePosType() and
78 * setOutputPosType(). See these methods for more details on the
79 * initialization options.
81 * After setting the default values, one or more selections can be parsed with
82 * one or more calls to parseFromStdin(), parseFromFile(), and/or
83 * parseFromString(). After all selections are parsed, the topology must be
84 * set with setTopology() unless requiresTopology() returns false (the topology
85 * can also be set earlier).
86 * setIndexGroups() must also be called if external index group references are
87 * used in the selections; it can be called at any point before compile().
88 * Once all selections are parsed, they must be compiled all at once using
89 * compile().
91 * After compilation, dynamic selections have the maximum number of atoms they
92 * can evaluate to, but positions have undefined values (see \ref Selection and
93 * SelectionPosition). evaluate() can be used to update the selections for a
94 * new frame. evaluateFinal() can be called after all the frames have been
95 * processed to restore the selection values back to the ones they were after
96 * compile().
98 * At any point, requiresTopology() can be called to see whether the
99 * information provided so far requires loading the topology.
100 * printTree() can be used to print the internal representation of the
101 * selections (mostly useful for debugging).
103 * Note that for trajectory analysis using TrajectoryAnalysisModule, the
104 * SelectionCollection object is managed by Gromacs, and \ref Selection objects
105 * are obtained from SelectionOption.
107 * \inpublicapi
108 * \ingroup module_selection
110 class SelectionCollection
112 public:
113 //! Flag for initOptions() to select how to behave with -seltype option.
114 enum SelectionTypeOption
116 /*! \brief
117 * Add the option for the user to select default value for
118 * setOutputPosType().
120 IncludeSelectionTypeOption,
121 /*! \brief
122 * Do not add the option, selections will always select atoms by
123 * default.
125 AlwaysAtomSelections
128 /*! \brief
129 * Creates an empty selection collection.
131 * \throws std::bad_alloc if out of memory.
133 SelectionCollection();
134 ~SelectionCollection();
136 /*! \brief
137 * Initializes options for setting global properties on the collection.
139 * \param[in,out] options Options object to initialize.
140 * \param[in] selectionTypeOption
141 * Whether to add option to influence setOutputPosType().
142 * \throws std::bad_alloc if out of memory.
144 * Adds options to \p options that can be used to set the default
145 * position types (see setReferencePosType() and setOutputPosType())
146 * and debugging flags.
148 void initOptions(Options *options, SelectionTypeOption selectionTypeOption);
150 /*! \brief
151 * Sets the default reference position handling for a selection
152 * collection.
154 * \param[in] type Default selection reference position type
155 * (one of the strings acceptable for
156 * PositionCalculationCollection::typeFromEnum()).
157 * \throws InternalError if \p type is invalid.
159 * Should be called before calling the parser functions, unless
160 * initOptions() has been called. In the latter case, can still be
161 * used to override the default value (before initOptions() is called)
162 * and/or the value provided through the Options object.
164 * Strong exception safety.
166 void setReferencePosType(const char *type);
167 /*! \brief
168 * Sets the default reference position handling for a selection
169 * collection.
171 * \param[in] type Default selection output position type
172 * (one of the strings acceptable for
173 * PositionCalculationCollection::typeFromEnum()).
174 * \throws InternalError if \p type is invalid.
176 * Should be called before calling the parser functions, unless
177 * initOptions() has been called. In the latter case, can still be
178 * used to override the default value (before initOptions() is called)
179 * and/or the value provided through the Options object.
181 * Strong exception safety.
183 void setOutputPosType(const char *type);
184 /*! \brief
185 * Sets the debugging level for the selection collection.
187 * \param[in] debugLevel Debug level to set (0 = no debug
188 * information).
190 * initOptions() creates debugging options that can also be used to set
191 * the debug level. These are normally hidden, but if this method is
192 * called before initOptions() with a non-zero \p debugLevel, they are
193 * made visible.
195 * Mostly useful for debugging tools.
197 * Does not throw.
199 void setDebugLevel(int debugLevel);
201 /*! \brief
202 * Returns true if the collection requires topology information for
203 * evaluation.
205 * \returns true if any selection in the collection requires topology
206 * information.
208 * Before the parser functions have been called, the return value is
209 * based just on the position types set.
210 * After parser functions have been called, the return value also takes
211 * into account the selection keywords used.
213 * Does not throw.
215 bool requiresTopology() const;
216 /*! \brief
217 * Sets the topology for the collection.
219 * \param[in] top Topology data.
220 * \param[in] natoms Number of atoms. If <=0, the number of
221 * atoms in the topology is used.
223 * Either the topology must be provided, or \p natoms must be > 0.
225 * \p natoms determines the largest atom index that can be selected by
226 * the selection: even if the topology contains more atoms, they will
227 * not be selected.
229 * Does not throw currently, but this is subject to change when more
230 * underlying code is converted to C++.
232 void setTopology(t_topology *top, int natoms);
233 /*! \brief
234 * Sets the external index groups to use for the selections.
236 * \param[in] grps Index groups to use for the selections.
237 * \throws std::bad_alloc if out of memory.
238 * \throws InconsistentInputError if a group reference cannot be resolved.
240 * Only the first call to this method can have a non-NULL \p grps.
241 * At this point, any selections that have already been provided are
242 * searched for references to external groups, and the references are
243 * replaced by the contents of the groups. If any referenced group
244 * cannot be found in \p grps (or if \p grps is NULL and there are any
245 * references), InconsistentInputError is thrown.
247 * The selection collection keeps a reference to \p grps until this
248 * method is called with a NULL \p grps.
249 * If this method is not called before compile(), it is automatically
250 * called as setIndexGroups(NULL).
252 void setIndexGroups(gmx_ana_indexgrps_t *grps);
253 /*! \brief
254 * Parses selection(s) from standard input.
256 * \param[in] count Number of selections to parse
257 * (if -1, parse as many as provided by the user).
258 * \param[in] bInteractive Whether the parser should behave
259 * interactively.
260 * \param[in] context Context to print for interactive input.
261 * \returns Vector of parsed selections.
262 * \throws std::bad_alloc if out of memory.
263 * \throws InvalidInputError if there is a parsing error
264 * (an interactive parser only throws this if too few selections
265 * are provided and the user forced the end of input).
267 * The returned objects remain valid for the lifetime of
268 * the selection collection.
269 * Some information about the selections only becomes available once
270 * compile() has been called; see \ref Selection.
272 * The string provided to \p context should be such that it can replace
273 * the three dots in "Specify selections ...:". It can be empty.
275 SelectionList parseFromStdin(int count, bool bInteractive,
276 const std::string &context);
277 /*! \brief
278 * Parses selection(s) from a file.
280 * \param[in] filename Name of the file to parse selections from.
281 * \returns Vector of parsed selections.
282 * \throws std::bad_alloc if out of memory.
283 * \throws InvalidInputError if there is a parsing error.
285 * The returned objects remain valid for the lifetime of
286 * the selection collection.
287 * Some information about the selections only becomes available once
288 * compile() has been called; see \ref Selection.
290 SelectionList parseFromFile(const std::string &filename);
291 /*! \brief
292 * Parses selection(s) from a string.
294 * \param[in] str String to parse selections from.
295 * \returns Vector of parsed selections.
296 * \throws std::bad_alloc if out of memory.
297 * \throws InvalidInputError if there is a parsing error.
299 * The returned objects remain valid for the lifetime of
300 * the selection collection.
301 * Some information about the selections only becomes available once
302 * compile() has been called; see \ref Selection.
304 SelectionList parseFromString(const std::string &str);
305 /*! \brief
306 * Prepares the selections for evaluation and performs optimizations.
308 * \throws InconsistentInputError if topology is required but not set.
309 * \throws InvalidInputError if setIndexGroups() has not been called
310 * and there are index group references.
311 * \throws unspecified if compilation fails (TODO: list/reduce these).
313 * Before compilation, selections should have been added to the
314 * collection using the parseFrom*() functions.
315 * The compiled selection collection can be passed to evaluate() to
316 * evaluate the selection for a frame.
317 * Before the compiled selection is evaluated, the selections indicate
318 * the maximal set of atoms/positions to which they can be evaluated;
319 * see \ref Selection.
321 * If an error occurs, the collection is cleared.
323 * The covered fraction information is initialized to ::CFRAC_NONE for
324 * all selections.
326 void compile();
327 /*! \brief
328 * Evaluates selections in the collection.
330 * \param[in] fr Frame for which the evaluation should be carried out.
331 * \param[in] pbc PBC data, or NULL if no PBC should be used.
332 * \throws unspeficied Multiple possible exceptions to indicate
333 * evaluation failure (TODO: enumerate).
335 void evaluate(t_trxframe *fr, t_pbc *pbc);
336 /*! \brief
337 * Evaluates the largest possible index groups from dynamic selections.
339 * \param[in] nframes Total number of frames.
341 * This method restores the selections to the state they were after
342 * compile().
344 * \p nframes should equal the number of times evaluate() has been
345 * called.
347 * Does not throw.
349 void evaluateFinal(int nframes);
351 /*! \brief
352 * Prints a human-readable version of the internal selection element
353 * tree.
355 * \param[in] fp File handle to receive the output.
356 * \param[in] bValues If true, the evaluated values of selection
357 * elements are printed as well.
359 * The output is very techical, and intended for debugging purposes.
361 * Does not throw.
363 void printTree(FILE *fp, bool bValues) const;
364 /*! \brief
365 * Prints the selection strings into an XVGR file as comments.
367 * \param[in] fp Output file.
368 * \param[in] oenv Output options structure.
370 * Does not throw.
372 void printXvgrInfo(FILE *fp, output_env_t oenv) const;
374 private:
375 class Impl;
377 PrivateImplPointer<Impl> impl_;
379 /*! \brief
380 * Needed for the compiler to freely modify the collection.
382 friend class SelectionCompiler;
383 /*! \brief
384 * Needed for the evaluator to freely modify the collection.
386 friend class SelectionEvaluator;
389 } // namespace gmx
391 #endif