Split g96 I/O routines from confio.cpp
[gromacs.git] / src / gromacs / selection / selectioncollection.h
blob826f20a04d1a85c3a8ddd23f6e2afda3995d8ec4
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;
66 class TextInputStream;
67 class TextOutputStream;
69 /*! \brief
70 * Collection of selections.
72 * This class is the main interface to the core of the selection engine.
73 * It is used to initialize and manage a collection of selections that share
74 * the same topology. Selections within one collection can share variables and
75 * can be optimized together. Selections from two different collections do not
76 * interact.
78 * The constructor creates an empty selection collection object. To initialize
79 * the object, either call initOptions(), or both setReferencePosType() and
80 * setOutputPosType(). See these methods for more details on the
81 * initialization options.
83 * After setting the default values, one or more selections can be parsed with
84 * one or more calls to parseInteractive(), parseFromStdin(), parseFromFile(), and/or
85 * parseFromString(). After all selections are parsed, the topology must be
86 * set with setTopology() unless requiresTopology() returns false (the topology
87 * can also be set earlier).
88 * setIndexGroups() must also be called if external index group references are
89 * used in the selections; it can be called at any point before compile().
90 * Once all selections are parsed, they must be compiled all at once using
91 * compile().
93 * After compilation, dynamic selections have the maximum number of atoms they
94 * can evaluate to, but positions have undefined values (see \ref Selection and
95 * SelectionPosition). evaluate() can be used to update the selections for a
96 * new frame. evaluateFinal() can be called after all the frames have been
97 * processed to restore the selection values back to the ones they were after
98 * compile().
100 * At any point, requiresTopology() can be called to see whether the
101 * information provided so far requires loading the topology.
102 * printTree() can be used to print the internal representation of the
103 * selections (mostly useful for debugging).
105 * Note that for trajectory analysis using TrajectoryAnalysisModule, the
106 * SelectionCollection object is managed by Gromacs, and \ref Selection objects
107 * are obtained from SelectionOption.
109 * \inpublicapi
110 * \ingroup module_selection
112 class SelectionCollection
114 public:
115 /*! \brief
116 * Creates an empty selection collection.
118 * \throws std::bad_alloc if out of memory.
120 SelectionCollection();
121 ~SelectionCollection();
123 /*! \brief
124 * Initializes options for setting global properties on the collection.
126 * \param[in,out] options Options object to initialize.
127 * \throws std::bad_alloc if out of memory.
129 * Adds options to \p options that can be used to set the default
130 * position types (see setReferencePosType() and setOutputPosType())
131 * and debugging flags.
133 void initOptions(Options *options);
135 /*! \brief
136 * Sets the default reference position handling for a selection
137 * collection.
139 * \param[in] type Default selection reference position type
140 * (one of the strings acceptable for
141 * PositionCalculationCollection::typeFromEnum()).
142 * \throws InternalError if \p type is invalid.
144 * Should be called before calling the parser functions, unless
145 * initOptions() has been called. In the latter case, can still be
146 * used to override the default value (before initOptions() is called)
147 * and/or the value provided through the Options object.
149 * Strong exception safety.
151 void setReferencePosType(const char *type);
152 /*! \brief
153 * Sets the default reference position handling for a selection
154 * collection.
156 * \param[in] type Default selection output position type
157 * (one of the strings acceptable for
158 * PositionCalculationCollection::typeFromEnum()).
159 * \throws InternalError if \p type is invalid.
161 * Should be called before calling the parser functions, unless
162 * initOptions() has been called. In the latter case, can still be
163 * used to override the default value (before initOptions() is called)
164 * and/or the value provided through the Options object.
166 * Strong exception safety.
168 void setOutputPosType(const char *type);
169 /*! \brief
170 * Sets the debugging level for the selection collection.
172 * \param[in] debugLevel Debug level to set (0 = no debug
173 * information).
175 * initOptions() creates debugging options that can also be used to set
176 * the debug level. These are normally hidden, but if this method is
177 * called before initOptions() with a non-zero \p debugLevel, they are
178 * made visible.
180 * Mostly useful for debugging tools.
182 * Does not throw.
184 void setDebugLevel(int debugLevel);
186 /*! \brief
187 * Returns true if the collection requires topology information for
188 * evaluation.
190 * \returns true if any selection in the collection requires topology
191 * information.
193 * Before the parser functions have been called, the return value is
194 * based just on the position types set.
195 * After parser functions have been called, the return value also takes
196 * into account the selection keywords used.
198 * Does not throw.
200 bool requiresTopology() const;
201 /*! \brief
202 * Sets the topology for the collection.
204 * \param[in] top Topology data.
205 * \param[in] natoms Number of atoms. If <=0, the number of
206 * atoms in the topology is used.
208 * Either the topology must be provided, or \p natoms must be > 0.
210 * \p natoms determines the largest atom index that can be selected by
211 * the selection: even if the topology contains more atoms, they will
212 * not be selected.
214 * Does not throw currently, but this is subject to change when more
215 * underlying code is converted to C++.
217 void setTopology(t_topology *top, int natoms);
218 /*! \brief
219 * Sets the external index groups to use for the selections.
221 * \param[in] grps Index groups to use for the selections.
222 * \throws std::bad_alloc if out of memory.
223 * \throws InconsistentInputError if a group reference cannot be resolved.
225 * Only the first call to this method can have a non-NULL \p grps.
226 * At this point, any selections that have already been provided are
227 * searched for references to external groups, and the references are
228 * replaced by the contents of the groups. If any referenced group
229 * cannot be found in \p grps (or if \p grps is NULL and there are any
230 * references), InconsistentInputError is thrown.
232 * The selection collection keeps a reference to \p grps until this
233 * method is called with a NULL \p grps.
234 * If this method is not called before compile(), it is automatically
235 * called as setIndexGroups(NULL).
237 void setIndexGroups(gmx_ana_indexgrps_t *grps);
238 /*! \brief
239 * Parses selection(s) from standard input.
241 * \param[in] count Number of selections to parse
242 * (if -1, parse as many as provided by the user).
243 * \param[in] bInteractive Whether the parser should behave
244 * interactively.
245 * \param[in] context Context to print for interactive input.
246 * \returns Vector of parsed selections.
247 * \throws std::bad_alloc if out of memory.
248 * \throws InvalidInputError if there is a parsing error
249 * (an interactive parser only throws this if too few selections
250 * are provided and the user forced the end of input).
252 * The returned objects remain valid for the lifetime of
253 * the selection collection.
254 * Some information about the selections only becomes available once
255 * compile() has been called; see \ref Selection.
257 * The string provided to \p context should be such that it can replace
258 * the three dots in "Specify selections ...:". It can be empty.
260 SelectionList parseFromStdin(int count, bool bInteractive,
261 const std::string &context);
262 /*! \brief
263 * Parses selection(s) interactively using provided streams.
265 * \param[in] count Number of selections to parse
266 * (if -1, parse as many as provided by the user).
267 * \param[in] inputStream Stream to use for input.
268 * \param[in] outputStream Stream to use for output
269 * (if NULL, the parser runs non-interactively and does not
270 * produce any status messages).
271 * \param[in] context Context to print for interactive input.
272 * \returns Vector of parsed selections.
273 * \throws std::bad_alloc if out of memory.
274 * \throws InvalidInputError if there is a parsing error
275 * (an interactive parser only throws this if too few selections
276 * are provided and the user forced the end of input).
278 * Works the same as parseFromStdin(), except that the caller can
279 * provide streams to use instead of `stdin` and `stderr`.
281 * Mainly usable for unit testing interactive input.
283 SelectionList parseInteractive(int count,
284 TextInputStream *inputStream,
285 TextOutputStream *outputStream,
286 const std::string &context);
287 /*! \brief
288 * Parses selection(s) from a file.
290 * \param[in] filename Name of the file to parse selections from.
291 * \returns Vector of parsed selections.
292 * \throws std::bad_alloc if out of memory.
293 * \throws InvalidInputError if there is a parsing error.
295 * The returned objects remain valid for the lifetime of
296 * the selection collection.
297 * Some information about the selections only becomes available once
298 * compile() has been called; see \ref Selection.
300 SelectionList parseFromFile(const std::string &filename);
301 /*! \brief
302 * Parses selection(s) from a string.
304 * \param[in] str String to parse selections from.
305 * \returns Vector of parsed selections.
306 * \throws std::bad_alloc if out of memory.
307 * \throws InvalidInputError if there is a parsing error.
309 * The returned objects remain valid for the lifetime of
310 * the selection collection.
311 * Some information about the selections only becomes available once
312 * compile() has been called; see \ref Selection.
314 SelectionList parseFromString(const std::string &str);
315 /*! \brief
316 * Prepares the selections for evaluation and performs optimizations.
318 * \throws InconsistentInputError if topology is required but not set.
319 * \throws InvalidInputError if setIndexGroups() has not been called
320 * and there are index group references.
321 * \throws unspecified if compilation fails (TODO: list/reduce these).
323 * Before compilation, selections should have been added to the
324 * collection using the parseFrom*() functions.
325 * The compiled selection collection can be passed to evaluate() to
326 * evaluate the selection for a frame.
327 * Before the compiled selection is evaluated, the selections indicate
328 * the maximal set of atoms/positions to which they can be evaluated;
329 * see \ref Selection.
331 * If an error occurs, the collection is cleared.
333 * The covered fraction information is initialized to ::CFRAC_NONE for
334 * all selections.
336 void compile();
337 /*! \brief
338 * Evaluates selections in the collection.
340 * \param[in] fr Frame for which the evaluation should be carried out.
341 * \param[in] pbc PBC data, or NULL if no PBC should be used.
342 * \throws unspeficied Multiple possible exceptions to indicate
343 * evaluation failure (TODO: enumerate).
345 void evaluate(t_trxframe *fr, t_pbc *pbc);
346 /*! \brief
347 * Evaluates the largest possible index groups from dynamic selections.
349 * \param[in] nframes Total number of frames.
351 * This method restores the selections to the state they were after
352 * compile().
354 * \p nframes should equal the number of times evaluate() has been
355 * called.
357 * Does not throw.
359 void evaluateFinal(int nframes);
361 /*! \brief
362 * Prints a human-readable version of the internal selection element
363 * tree.
365 * \param[in] fp File handle to receive the output.
366 * \param[in] bValues If true, the evaluated values of selection
367 * elements are printed as well.
369 * The output is very techical, and intended for debugging purposes.
371 * Does not throw.
373 void printTree(FILE *fp, bool bValues) const;
374 /*! \brief
375 * Prints the selection strings into an XVGR file as comments.
377 * \param[in] fp Output file.
378 * \param[in] oenv Output options structure.
380 * Does not throw.
382 void printXvgrInfo(FILE *fp, output_env_t oenv) const;
384 private:
385 class Impl;
387 PrivateImplPointer<Impl> impl_;
389 /*! \brief
390 * Needed for the compiler to freely modify the collection.
392 friend class SelectionCompiler;
393 /*! \brief
394 * Needed for the evaluator to freely modify the collection.
396 friend class SelectionEvaluator;
399 } // namespace gmx
401 #endif