3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
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.
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.
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.
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.
29 * For more info, check our website at http://www.gromacs.org
33 * Declares gmx::SelectionCollection.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
37 * \ingroup module_selection
39 #ifndef GMX_SELECTION_SELECTIONCOLLECTION_H
40 #define GMX_SELECTION_SELECTIONCOLLECTION_H
45 #include "../legacyheaders/typedefs.h"
47 #include "../utility/common.h"
48 #include "selection.h" // For gmx::SelectionList
50 struct gmx_ana_indexgrps_t;
56 class SelectionCompiler;
57 class SelectionEvaluator;
58 class SelectionOptionStorage;
61 * Collection of selections.
63 * This class is the main interface to the core of the selection engine.
64 * It is used to initialize and manage a collection of selections that share
65 * the same topology. Selections within one collection can share variables and
66 * can be optimized together. Selections from two different collections do not
69 * The constructor creates an empty selection collection object. To initialize
70 * the object, either call initOptions(), or both setReferencePosType() and
71 * setOutputPosType(). See these methods for more details on the
72 * initialization options.
74 * After setting the default values, one or more selections can be parsed with
75 * one or more calls to parseFromStdin(), parseFromFile(), and/or
76 * parseFromString(). parseRequestedFromStdin() and parseRequestedFromString()
77 * are provided for integration with SelectionOption. After all selections are
78 * parsed, the topology must be set with setTopology() unless
79 * requiresTopology() returns false (the topology can also be set earlier).
80 * setIndexGroups() must also be called if external index group references are
81 * used in the selections; it can be called at any point before compile().
82 * Once all selections are parsed, they must be compiled all at once using
85 * After compilation, dynamic selections have the maximum number of atoms they
86 * can evaluate to, but positions have undefined values (see \ref Selection and
87 * SelectionPosition). evaluate() can be used to update the selections for a
88 * new frame. evaluateFinal() can be called after all the frames have been
89 * processed to restore the selection values back to the ones they were after
92 * At any point, requiresTopology() can be called to see whether the
93 * information provided so far requires loading the topology.
94 * printTree() can be used to print the internal representation of the
95 * selections (mostly useful for debugging).
97 * Note that for trajectory analysis using TrajectoryAnalysisModule, the
98 * SelectionCollection object is managed by Gromacs, and \ref Selection objects
99 * are obtained from SelectionOption.
102 * \ingroup module_selection
104 class SelectionCollection
108 * Creates an empty selection collection.
110 * \throws std::bad_alloc if out of memory.
112 SelectionCollection();
113 ~SelectionCollection();
116 * Initializes options for setting global properties on the collection.
118 * \returns Initialized options object.
119 * \throws std::bad_alloc if out of memory.
121 * The returned options can be used to set the default position types
122 * (see setReferencePosType() and setOutputPosType()) and debugging
125 Options &initOptions();
128 * Sets the default reference position handling for a selection
131 * \param[in] type Default selection reference position type
132 * (one of the strings acceptable for
133 * PositionCalculationCollection::typeFromEnum()).
134 * \throws InternalError if \p type is invalid.
136 * Should be called before calling the parser functions, unless
137 * initOptions() has been called. In the latter case, can still be
138 * used to override the default value (before initOptions() is called)
139 * and/or the value provided through the Options object.
141 * Strong exception safety.
143 void setReferencePosType(const char *type);
145 * Sets the default reference position handling for a selection
148 * \param[in] type Default selection output position type
149 * (one of the strings acceptable for
150 * PositionCalculationCollection::typeFromEnum()).
151 * \throws InternalError if \p type is invalid.
153 * Should be called before calling the parser functions, unless
154 * initOptions() has been called. In the latter case, can still be
155 * used to override the default value (before initOptions() is called)
156 * and/or the value provided through the Options object.
158 * Strong exception safety.
160 void setOutputPosType(const char *type);
162 * Sets the debugging level for the selection collection.
164 * \param[in] debugLevel Debug level to set (0 = no debug
167 * initOptions() creates debugging options that can also be used to set
168 * the debug level. These are normally hidden, but if this method is
169 * called before initOptions() with a non-zero \p debugLevel, they are
172 * Mostly useful for debugging tools.
176 void setDebugLevel(int debugLevel);
179 * Returns true if the collection requires topology information for
182 * \returns true if any selection in the collection requires topology
185 * Before the parser functions have been called, the return value is
186 * based just on the position types set.
187 * After parser functions have been called, the return value also takes
188 * into account the selection keywords used.
192 bool requiresTopology() const;
194 * Sets the topology for the collection.
196 * \param[in] top Topology data.
197 * \param[in] natoms Number of atoms. If <=0, the number of
198 * atoms in the topology is used.
200 * Either the topology must be provided, or \p natoms must be > 0.
202 * \p natoms determines the largest atom index that can be selected by
203 * the selection: even if the topology contains more atoms, they will
206 * Does not throw currently, but this is subject to change when more
207 * underlying code is converted to C++.
209 void setTopology(t_topology *top, int natoms);
211 * Sets the external index groups to use for the selections.
213 * \param[in] grps Index groups to use for the selections.
214 * \throws std::bad_alloc if out of memory.
215 * \throws InvalidInputError if a group reference cannot be resolved.
217 * Only the first call to this method can have a non-NULL \p grps.
218 * At this point, any selections that have already been provided are
219 * searched for references to external groups, and the references are
220 * replaced by the contents of the groups. If any referenced group
221 * cannot be found in \p grps (or if \p grps is NULL and there are any
222 * references), InvalidInputError is thrown.
224 * The selection collection keeps a reference to \p grps until this
225 * method is called with a NULL \p grps.
226 * If this method is not called before compile(), it is automatically
227 * called as setIndexGroups(NULL).
229 void setIndexGroups(gmx_ana_indexgrps_t *grps);
231 * Parses selection(s) from standard input for options not yet
234 * \param[in] bInteractive Whether the parser should behave
236 * \throws unspecified Can throw any exception thrown by
238 * \throws std::bad_alloc if out of memory.
240 * This method cooperates with SelectionOption to allow interactive
241 * input of missing selections after all options have been processed.
242 * It should be called after the Options::finish() method has been
243 * called on all options that add selections to this collection.
244 * For each required selection option that has not been given, as well
245 * as for optional selection options that have been specified without
246 * values, it will prompt the user to input the necessary selections.
248 void parseRequestedFromStdin(bool bInteractive);
250 * Parses selection(s) from a file for options not yet provided.
252 * \param[in] filename Name of the file to parse selections from.
253 * \throws unspecified Can throw any exception thrown by
255 * \throws std::bad_alloc if out of memory.
256 * \throws InvalidInputError if
257 * - the number of selections in \p filename doesn't match the
259 * - any selection uses a feature that is not allowed for the
260 * corresponding option.
261 * - if there is a request for any number of selections that is
262 * not the last (in which case it is not possible to determine
263 * which selections belong to which request).
265 * This method behaves as parseRequestedFromStdin(), but reads the
266 * selections from a file instead of standard input.
267 * This is used to implement SelectionFileOption.
269 * \see parseRequestedFromStdin()
271 void parseRequestedFromFile(const std::string &filename);
273 * Parses selection(s) from a string for options not yet provided.
275 * \param[in] str String to parse.
276 * \throws unspecified Can throw any exception thrown by
278 * \throws std::bad_alloc if out of memory.
279 * \throws InvalidInputError in same conditions as
280 * parseRequestedFromFile().
282 * This method behaves as parseRequestedFromFile(), but reads the
283 * selections from a string instead of a file.
284 * This method is mainly used for testing.
286 * \see parseRequestedFromFile()
288 void parseRequestedFromString(const std::string &str);
290 * Parses selection(s) from standard input.
292 * \param[in] count Number of selections to parse
293 * (if -1, parse as many as provided by the user).
294 * \param[in] bInteractive Whether the parser should behave
296 * \param[out] output Vector to which parsed selections are appended.
297 * \throws std::bad_alloc if out of memory.
298 * \throws InvalidInputError if there is a parsing error
299 * (an interactive parser only throws this if too few selections
300 * are provided and the user forced the end of input).
302 * Parsed selections are appended to \p output without clearing it
303 * first. If parsing fails, \p output is not modified.
305 * The objects returned in \p output remain valid for the lifetime of
306 * the selection collection.
307 * Some information about the selections only becomes available once
308 * compile() has been called; see \ref Selection.
310 void parseFromStdin(int count, bool bInteractive,
311 SelectionList *output);
313 * Parses selection(s) from a file.
315 * \param[in] filename Name of the file to parse selections from.
316 * \param[out] output Vector to which parsed selections are appended.
317 * \throws std::bad_alloc if out of memory.
318 * \throws InvalidInputError if there is a parsing error.
320 * Parsed selections are appended to \p output without clearing it
321 * first. If parsing fails, \p output is not modified.
323 * The objects returned in \p output remain valid for the lifetime of
324 * the selection collection.
325 * Some information about the selections only becomes available once
326 * compile() has been called; see \ref Selection.
328 void parseFromFile(const std::string &filename,
329 SelectionList *output);
331 * Parses selection(s) from a string.
333 * \param[in] str String to parse selections from.
334 * \param[out] output Vector to which parsed selections are appended.
335 * \throws std::bad_alloc if out of memory.
336 * \throws InvalidInputError if there is a parsing error.
338 * Parsed selections are appended to \p output without clearing it
339 * first. If parsing fails, \p output is not modified.
341 * The objects returned in \p output remain valid for the lifetime of
342 * the selection collection.
343 * Some information about the selections only becomes available once
344 * compile() has been called; see \ref Selection.
346 void parseFromString(const std::string &str,
347 SelectionList *output);
349 * Prepares the selections for evaluation and performs optimizations.
351 * \throws InconsistentInputError if topology is required but not set.
352 * \throws InvalidInputError if setIndexGroups() has not been called
353 * and there are index group references.
354 * \throws unspecified if compilation fails (TODO: list/reduce these).
356 * Before compilation, selections should have been added to the
357 * collection using the parseFrom*() functions.
358 * The compiled selection collection can be passed to evaluate() to
359 * evaluate the selection for a frame.
360 * Before the compiled selection is evaluated, the selections indicate
361 * the maximal set of atoms/positions to which they can be evaluated;
362 * see \ref Selection.
364 * If an error occurs, the collection is cleared.
366 * The covered fraction information is initialized to ::CFRAC_NONE for
371 * Evaluates selections in the collection.
373 * \param[in] fr Frame for which the evaluation should be carried out.
374 * \param[in] pbc PBC data, or NULL if no PBC should be used.
375 * \throws unspeficied Multiple possible exceptions to indicate
376 * evaluation failure (TODO: enumerate).
378 void evaluate(t_trxframe *fr, t_pbc *pbc);
380 * Evaluates the largest possible index groups from dynamic selections.
382 * \param[in] nframes Total number of frames.
384 * This method restores the selections to the state they were after
387 * \p nframes should equal the number of times evaluate() has been
392 void evaluateFinal(int nframes);
395 * Prints a human-readable version of the internal selection element
398 * \param[in] fp File handle to receive the output.
399 * \param[in] bValues If true, the evaluated values of selection
400 * elements are printed as well.
402 * The output is very techical, and intended for debugging purposes.
406 void printTree(FILE *fp, bool bValues) const;
408 * Prints the selection strings into an XVGR file as comments.
410 * \param[in] fp Output file.
411 * \param[in] oenv Output options structure.
415 void printXvgrInfo(FILE *fp, output_env_t oenv) const;
420 PrivateImplPointer<Impl> _impl;
423 * Needed for the compiler to freely modify the collection.
425 friend class SelectionCompiler;
427 * Needed for the evaluator to freely modify the collection.
429 friend class SelectionEvaluator;
431 * Needed for handling delayed selection parsing requests.
433 friend class SelectionOptionStorage;