2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 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.
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.
37 * Declares gmx::TrajectoryAnalysisModule and
38 * gmx::TrajectoryAnalysisModuleData.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
42 * \ingroup module_trajectoryanalysis
44 #ifndef GMX_TRAJECTORYANALYSIS_ANALYSISMODULE_H
45 #define GMX_TRAJECTORYANALYSIS_ANALYSISMODULE_H
50 #include "../legacyheaders/typedefs.h"
52 #include "../selection/selection.h" // For gmx::SelectionList
53 #include "../utility/common.h"
54 #include "../utility/uniqueptr.h"
59 class AbstractAnalysisData;
61 class AnalysisDataHandle;
62 class AnalysisDataParallelOptions;
64 class SelectionCollection;
65 class TopologyInformation;
66 class TrajectoryAnalysisModule;
67 class TrajectoryAnalysisSettings;
70 * Base class for thread-local data storage during trajectory analysis.
72 * Thread-local storage of data handles and selections is implemented in this
73 * class; TrajectoryAnalysisModule instances can access the thread-local values
74 * in their TrajectoryAnalysisModule::analyzeFrame() method using dataHandle()
75 * and parallelSelection().
77 * \see TrajectoryAnalysisModule::startFrames()
78 * \see TrajectoryAnalysisModule::analyzeFrame()
79 * \see TrajectoryAnalysisModule::finishFrames()
82 * \ingroup module_trajectoryanalysis
84 class TrajectoryAnalysisModuleData
87 virtual ~TrajectoryAnalysisModuleData();
90 * Performs any finishing actions after all frames have been processed.
92 * \throws unspecified Implementation may throw exceptions to indicate
95 * This function is called immediately before the destructor, after
96 * TrajectoryAnalysisModule::finishFrames().
97 * Derived classes should implement any final operations that need to
98 * be done after successful analysis.
99 * All implementations should call finishDataHandles().
101 virtual void finish() = 0;
104 * Returns a data handle for a given dataset.
106 * \param[in] data Analysis data object.
107 * \returns Data handle for \p data stored in this thread-local data.
109 * \p data should have previously been registered with
110 * TrajectoryAnalysisModule::registerAnalysisDataset().
114 AnalysisDataHandle dataHandle(const AnalysisData &data);
116 * Returns a selection that corresponds to the given selection.
118 * \param[in] selection Global selection object.
119 * \returns Selection object corresponding to this thread-local data.
121 * \p selection is the selection object that was obtained from
122 * SelectionOption. The return value is the corresponding selection
123 * in the selection collection with which this data object was
128 Selection parallelSelection(const Selection &selection);
130 * Returns a set of selection that corresponds to the given selections.
132 * \throws std::bad_alloc if out of memory.
134 * Works as parallelSelection(), but for a list of selections at once.
136 * \see parallelSelection()
138 SelectionList parallelSelections(const SelectionList &selections);
142 * Initializes thread-local storage for data handles and selections.
144 * \param[in] module Analysis module to use for data objects.
145 * \param[in] opt Data parallelization options.
146 * \param[in] selections Thread-local selection collection.
147 * \throws std::bad_alloc if out of memory.
148 * \throws unspecified Can throw any exception thrown by
149 * AnalysisData::startData().
151 * Calls AnalysisData::startData() on all data objects registered with
152 * TrajectoryAnalysisModule::registerAnalysisDataset() in \p module.
153 * The handles are accessible through dataHandle().
155 TrajectoryAnalysisModuleData(TrajectoryAnalysisModule *module,
156 const AnalysisDataParallelOptions &opt,
157 const SelectionCollection &selections);
160 * Calls finishData() on all data handles.
162 * \throws unspecified Can throw any exception thrown by
163 * AnalysisDataHandle::finishData().
165 * This function should be called from the implementation of finish()
168 void finishDataHandles();
173 PrivateImplPointer<Impl> impl_;
176 //! Smart pointer to manage a TrajectoryAnalysisModuleData object.
177 typedef gmx_unique_ptr<TrajectoryAnalysisModuleData>::type
178 TrajectoryAnalysisModuleDataPointer;
181 * Base class for trajectory analysis modules.
183 * Trajectory analysis methods should derive from this class and override the
184 * necessary virtual methods to implement initialization (initOptions(),
185 * optionsFinished(), initAnalysis(), initAfterFirstFrame()), per-frame analysis
186 * (analyzeFrame()), and final processing (finishFrames(), finishAnalysis(),
189 * For parallel analysis using threads, only a single object is constructed,
190 * but the methods startFrames(), analyzeFrame() and finishFrames() are called
191 * in each thread. Frame-local data should be initialized in startFrames() and
192 * stored in a class derived from TrajectoryAnalysisModuleData that is passed
193 * to the other methods. The default implementation of startFrames() can be
194 * used if only data handles and selections need to be thread-local.
196 * To get the full benefit from this class,
197 * \ref module_analysisdata "analysis data objects" and
198 * \ref module_selection "selections" should be used in the implementation.
199 * See the corresponding modules' documentation for details of how they work.
201 * Typical way of using AnalysisData in derived classes is to have the
202 * AnalysisData object as a member variable and register it using
203 * registerAnalysisDataset(). Analysis modules are initialized in
204 * initAnalysis() and the processing chain is initialized. If any of the
205 * modules is required, e.g., for post-processing in finishAnalysis(), it can
206 * be stored in a member variable. To add data to the data object in
207 * analyzeFrame(), a data handle is obtained using
208 * TrajectoryAnalysisModuleData::dataHandle().
210 * Typical way of using selections in derived classes is to have the required
211 * \ref Selection objects (or ::SelectionList objects) as member variables, and
212 * add the required selection options in initOptions(). These member variables
213 * can be accessed in initAnalysis() to get general information about the
214 * selections. In analyzeFrame(), these selection objects should not be used
215 * directly, but instead TrajectoryAnalysisModuleData::parallelSelection()
216 * should be used to obtain a selection object that works correctly also for
219 * Derived classes should use exceptions to indicate errors in the virtual
223 * \ingroup module_trajectoryanalysis
225 class TrajectoryAnalysisModule
228 virtual ~TrajectoryAnalysisModule();
231 * Initializes options understood by the module.
233 * \param[in,out] options Options object to add the options to.
234 * \param[in,out] settings Settings to pass to and from the module.
236 * This method is called first after the constructor, and it should
237 * add options understood by the module to \p options. Output values
238 * from options (including selections) should be stored in member
241 * In addition to initializing the options, this method can also
242 * provide information about the module's requirements using the
243 * \p settings object; see TrajectoryAnalysisSettings for more details.
245 * If settings depend on the option values provided by the user, see
248 virtual void initOptions(Options *options,
249 TrajectoryAnalysisSettings *settings) = 0;
251 * Called after all option values have been set.
253 * \param[in,out] options Options object in which options are stored.
254 * \param[in,out] settings Settings to pass to and from the module.
256 * This method is called after option values have been assigned (but
257 * interactive selection input has not yet been performed).
259 * If the module needs to change settings that affect topology loading
260 * (can be done using the \p settings object) or selection
261 * initialization (can be done using SelectionOptionInfo) based on
262 * option values, this method has to be overridden.
264 * The default implementation does nothing.
266 virtual void optionsFinished(Options *options,
267 TrajectoryAnalysisSettings *settings);
269 * Initializes the analysis.
271 * \param[in] settings Settings to pass to and from the module.
272 * \param[in] top Topology information.
274 * When this function is called, selections have been initialized based
275 * on user input, and a topology has been loaded if provided by the
276 * user. For dynamic selections, the selections have been evaluated to
277 * the largest possible selection, i.e., the selections passed to
278 * analyzeFrame() are always a subset of the selections provided here.
280 virtual void initAnalysis(const TrajectoryAnalysisSettings &settings,
281 const TopologyInformation &top) = 0;
283 * Performs additional initialization after reading the first frame.
285 * When this function is called, selections are the same as in
286 * initAnalysis(), i.e., they have not been evaluated for the first
289 * It is necessary to override this method only if the module needs to
290 * do initialization for which it requires data from the first frame.
292 * The default implementation does nothing.
294 virtual void initAfterFirstFrame(const t_trxframe &fr);
297 * Starts the analysis of frames.
300 * \param[in] selections Frame-local selection collection object.
301 * \returns Data structure for thread-local data.
303 * This function is necessary only for threaded parallelization.
304 * It is called once for each thread and should initialize a class that
305 * contains any required frame-local data in the returned value.
306 * The default implementation creates a basic data structure that holds
307 * thread-local data handles for all data objects registered with
308 * registerAnalysisDataset(), as well as the thread-local selection
309 * collection. These can be accessed in analyzeFrame() using the
310 * methods in TrajectoryAnalysisModuleData.
311 * If other thread-local data is needed, this function should be
312 * overridden and it should create an instance of a class derived from
313 * TrajectoryAnalysisModuleData.
315 * \see TrajectoryAnalysisModuleData
317 virtual TrajectoryAnalysisModuleDataPointer startFrames(
318 const AnalysisDataParallelOptions &opt,
319 const SelectionCollection &selections);
321 * Analyzes a single frame.
323 * \param[in] frnr Frame number, a zero-based index that
324 * uniquely identifies the frame.
325 * \param[in] fr Current frame.
326 * \param[in] pbc Periodic boundary conditions for \p fr.
327 * \param[in,out] pdata Data structure for frame-local data.
329 * This method is called once for each frame to be analyzed, and should
330 * analyze the positions provided in the selections. Data handles and
331 * selections should be obtained from the \p pdata structure.
333 * For threaded analysis, this method is called asynchronously in
334 * different threads to analyze different frames. The \p pdata
335 * structure is one of the structures created with startFrames(),
336 * but no assumptions should be made about which of these data
337 * structures is used. It is guaranteed that two instances of
338 * analyzeFrame() are not running concurrently with the same \p pdata
340 * Any access to data structures not stored in \p pdata should be
341 * designed to be thread-safe.
343 virtual void analyzeFrame(int frnr, const t_trxframe &fr, t_pbc *pbc,
344 TrajectoryAnalysisModuleData *pdata) = 0;
346 * Finishes the analysis of frames.
348 * \param[in] pdata Data structure for thread-local data.
350 * This method is called once for each call of startFrames(), with the
351 * data structure returned by the corresponding startFrames().
352 * The \p pdata object should be destroyed by the caller after this
353 * function has been called.
355 * You only need to override this method if you need custom
356 * operations to combine data from the frame-local data structures
357 * to get the final result. In such cases, the data should be
358 * aggregated in this function and stored in a member attribute.
360 * The default implementation does nothing.
364 virtual void finishFrames(TrajectoryAnalysisModuleData *pdata);
367 * Postprocesses data after frames have been read.
369 * \param[in] nframes Total number of frames processed.
371 * This function is called after all finishFrames() calls have been
373 * \p nframes will equal the number of calls to analyzeFrame() that
376 virtual void finishAnalysis(int nframes) = 0;
378 * Writes output into files and/or standard output/error.
380 * All output from the module, excluding data written out for each
381 * frame during analyzeFrame(), should be confined into this function.
382 * This function is guaranteed to be called only after
385 virtual void writeOutput() = 0;
388 * Returns the name of the analysis module.
392 const char *name() const;
394 * Returns short description for the analysis module.
398 const char *description() const;
400 * Returns the number of datasets provided by the module.
404 int datasetCount() const;
406 * Returns a vector with the names of datasets provided by the module.
410 const std::vector<std::string> &datasetNames() const;
412 * Returns a pointer to the data set \p index.
414 * \param[in] index Data set to query for.
415 * \returns Reference to the requested data set.
416 * \throws APIError if \p index is not valid.
418 * \p index should be >= 0 and < datasetCount().
420 * The return value is not const to allow callers to add modules to the
421 * data sets. However, the AbstractAnalysisData interface does not
422 * provide any means to alter the data, so the module does not need to
423 * care about external modifications.
425 AbstractAnalysisData &datasetFromIndex(int index) const;
427 * Returns a pointer to the data set with name \p name
429 * \param[in] name Data set to query for.
430 * \returns Reference to the requested data set.
431 * \throws APIError if \p name is not valid.
433 * \p name should be one of the names returned by datasetNames().
435 * The return value is not const to allow callers to add modules to the
436 * data sets. However, the AbstractAnalysisData interface does not
437 * provide any means to alter the data, so the module does not need to
438 * care about external modifications.
440 AbstractAnalysisData &datasetFromName(const char *name) const;
444 * Initializes the dataset registration mechanism.
446 * \param[in] name Name for the module.
447 * \param[in] description One-line description for the module.
448 * \throws std::bad_alloc if out of memory.
450 TrajectoryAnalysisModule(const char *name, const char *description);
453 * Registers a dataset that exports data.
455 * \param data Data object to register.
456 * \param[in] name Name to register the dataset with.
457 * \throws std::bad_alloc if out of memory.
459 * Registers \p data as a dataset that provides output from the
460 * analysis module. Callers for the module can access the dataset
461 * with datasetFromName() using \p name as an AbstractAnalysisData
462 * object. This allows them to add their own data modules to do extra
465 * \p name must be unique across all calls within the same
466 * TrajectoryAnalysisModule instance.
468 void registerBasicDataset(AbstractAnalysisData *data, const char *name);
470 * Registers a parallelized dataset that exports data.
472 * \param data AnalysisData object to register.
473 * \param[in] name Name to register the dataset with.
474 * \throws std::bad_alloc if out of memory.
476 * This method works as registerBasicDataset(), but additionally allows
477 * data handles for \p data to be accessed using
478 * TrajectoryAnalysisData.
480 * \see registerBasicDataset()
482 void registerAnalysisDataset(AnalysisData *data, const char *name);
487 PrivateImplPointer<Impl> impl_;
490 * Needed to access the registered analysis data sets.
492 friend class TrajectoryAnalysisModuleData;
495 //! Smart pointer to manage a TrajectoryAnalysisModule.
496 typedef gmx_unique_ptr<TrajectoryAnalysisModule>::type
497 TrajectoryAnalysisModulePointer;