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::TrajectoryAnalysisModule and
34 * gmx::TrajectoryAnalysisModuleData.
36 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
38 * \ingroup module_trajectoryanalysis
40 #ifndef GMX_TRAJECTORYANALYSIS_ANALYSISMODULE_H
41 #define GMX_TRAJECTORYANALYSIS_ANALYSISMODULE_H
46 #include "../legacyheaders/typedefs.h"
48 #include "../selection/selection.h" // For gmx::SelectionList
49 #include "../utility/common.h"
50 #include "../utility/uniqueptr.h"
55 class AbstractAnalysisData;
57 class AnalysisDataHandle;
58 class AnalysisDataParallelOptions;
60 class SelectionCollection;
61 class TopologyInformation;
62 class TrajectoryAnalysisModule;
63 class TrajectoryAnalysisSettings;
66 * Base class for thread-local data storage during trajectory analysis.
68 * Thread-local storage of data handles and selections is implemented in this
69 * class; TrajectoryAnalysisModule instances can access the thread-local values
70 * in their TrajectoryAnalysisModule::analyzeFrame() method using dataHandle()
71 * and parallelSelection().
73 * \see TrajectoryAnalysisModule::startFrames()
74 * \see TrajectoryAnalysisModule::analyzeFrame()
75 * \see TrajectoryAnalysisModule::finishFrames()
78 * \ingroup module_trajectoryanalysis
80 class TrajectoryAnalysisModuleData
83 virtual ~TrajectoryAnalysisModuleData();
86 * Performs any finishing actions after all frames have been processed.
88 * \throws unspecified Implementation may throw exceptions to indicate
91 * This function is called immediately before the destructor, after
92 * TrajectoryAnalysisModule::finishFrames().
93 * Derived classes should implement any final operations that need to
94 * be done after successful analysis.
95 * All implementations should call finishDataHandles().
97 virtual void finish() = 0;
100 * Returns a data handle for a given dataset.
102 * \param[in] data Analysis data object.
103 * \returns Data handle for \p data stored in this thread-local data.
105 * \p data should have previously been registered with
106 * TrajectoryAnalysisModule::registerAnalysisDataset().
110 AnalysisDataHandle dataHandle(const AnalysisData &data);
112 * Returns a selection that corresponds to the given selection.
114 * \param[in] selection Global selection object.
115 * \returns Selection object corresponding to this thread-local data.
117 * \p selection is the selection object that was obtained from
118 * SelectionOption. The return value is the corresponding selection
119 * in the selection collection with which this data object was
124 Selection parallelSelection(const Selection &selection);
126 * Returns a set of selection that corresponds to the given selections.
128 * \throws std::bad_alloc if out of memory.
130 * Works as parallelSelection(), but for a list of selections at once.
132 * \see parallelSelection()
134 SelectionList parallelSelections(const SelectionList &selections);
138 * Initializes thread-local storage for data handles and selections.
140 * \param[in] module Analysis module to use for data objects.
141 * \param[in] opt Data parallelization options.
142 * \param[in] selections Thread-local selection collection.
143 * \throws std::bad_alloc if out of memory.
144 * \throws unspecified Can throw any exception thrown by
145 * AnalysisData::startData().
147 * Calls AnalysisData::startData() on all data objects registered with
148 * TrajectoryAnalysisModule::registerAnalysisDataset() in \p module.
149 * The handles are accessible through dataHandle().
151 TrajectoryAnalysisModuleData(TrajectoryAnalysisModule *module,
152 const AnalysisDataParallelOptions &opt,
153 const SelectionCollection &selections);
156 * Calls finishData() on all data handles.
158 * \throws unspecified Can throw any exception thrown by
159 * AnalysisDataHandle::finishData().
161 * This function should be called from the implementation of finish()
164 void finishDataHandles();
169 PrivateImplPointer<Impl> impl_;
172 //! Smart pointer to manage a TrajectoryAnalysisModuleData object.
173 typedef gmx_unique_ptr<TrajectoryAnalysisModuleData>::type
174 TrajectoryAnalysisModuleDataPointer;
177 * Base class for trajectory analysis modules.
179 * Trajectory analysis methods should derive from this class and override the
180 * necessary virtual methods to implement initialization (initOptions(),
181 * initOptionsDone(), initAnalysis(), initAfterFirstFrame()), per-frame analysis
182 * (analyzeFrame()), and final processing (finishFrames(), finishAnalysis(),
185 * For parallel analysis using threads, only a single object is constructed,
186 * but the methods startFrames(), analyzeFrame() and finishFrames() are called
187 * in each thread. Frame-local data should be initialized in startFrames() and
188 * stored in a class derived from TrajectoryAnalysisModuleData that is passed
189 * to the other methods. The default implementation of startFrames() can be
190 * used if only data handles and selections need to be thread-local.
192 * To get the full benefit from this class,
193 * \ref module_analysisdata "analysis data objects" and
194 * \ref module_selection "selections" should be used in the implementation.
195 * See the corresponding modules' documentation for details of how they work.
197 * Typical way of using AnalysisData in derived classes is to have the
198 * AnalysisData object as a member variable and register it using
199 * registerAnalysisDataset(). Analysis modules are initialized in
200 * initAnalysis() and the processing chain is initialized. If any of the
201 * modules is required, e.g., for post-processing in finishAnalysis(), it can
202 * be stored in a member variable. To add data to the data object in
203 * analyzeFrame(), a data handle is obtained using
204 * TrajectoryAnalysisModuleData::dataHandle().
206 * Typical way of using selections in derived classes is to have the required
207 * \ref Selection objects (or ::SelectionList objects) as member variables, and
208 * add the required selection options in initOptions(). These member variables
209 * can be accessed in initAnalysis() to get general information about the
210 * selections. In analyzeFrame(), these selection objects should not be used
211 * directly, but instead TrajectoryAnalysisModuleData::parallelSelection()
212 * should be used to obtain a selection object that works correctly also for
215 * Derived classes should use exceptions to indicate errors in the virtual
219 * \ingroup module_trajectoryanalysis
221 class TrajectoryAnalysisModule
224 virtual ~TrajectoryAnalysisModule();
227 * Initializes options understood by the module.
229 * \param[in,out] settings Settings to pass to and from the module.
230 * \returns Reference to options that are accepted by the module.
232 * Typical implementation returns a reference to a member variable
233 * after first adding necessary options to that object. Output values
234 * from options (including selections) should be stored in member
237 * In addition to initializing the options, this method can also
238 * provide information about the module's requirements using the
239 * \p settings object; see TrajectoryAnalysisSettings for more details.
241 * If settings depend on the option values provided by the user, see
244 virtual Options &initOptions(TrajectoryAnalysisSettings *settings) = 0;
246 * Called after all option values have been set.
248 * \param[in,out] settings Settings to pass to and from the module.
250 * This method is called after option values have been assigned (but
251 * interactive selection input has not yet been performed).
253 * If the module needs to change settings that affect topology loading
254 * (can be done using the \p settings object) or selection
255 * initialization (can be done using SelectionOptionInfo) based on
256 * option values, this method has to be overridden.
258 * The default implementation does nothing.
260 virtual void initOptionsDone(TrajectoryAnalysisSettings *settings);
262 * Initializes the analysis.
264 * \param[in] settings Settings to pass to and from the module.
265 * \param[in] top Topology information.
267 * When this function is called, selections have been initialized based
268 * on user input, and a topology has been loaded if provided by the
269 * user. For dynamic selections, the selections have been evaluated to
270 * the largest possible selection, i.e., the selections passed to
271 * analyzeFrame() are always a subset of the selections provided here.
273 virtual void initAnalysis(const TrajectoryAnalysisSettings &settings,
274 const TopologyInformation &top) = 0;
276 * Performs additional initialization after reading the first frame.
278 * When this function is called, selections are the same as in
279 * initAnalysis(), i.e., they have not been evaluated for the first
282 * It is necessary to override this method only if the module needs to
283 * do initialization for which it requires data from the first frame.
285 * The default implementation does nothing.
287 virtual void initAfterFirstFrame(const t_trxframe &fr);
290 * Starts the analysis of frames.
293 * \param[in] selections Frame-local selection collection object.
294 * \returns Data structure for thread-local data.
296 * This function is necessary only for threaded parallelization.
297 * It is called once for each thread and should initialize a class that
298 * contains any required frame-local data in the returned value.
299 * The default implementation creates a basic data structure that holds
300 * thread-local data handles for all data objects registered with
301 * registerAnalysisDataset(), as well as the thread-local selection
302 * collection. These can be accessed in analyzeFrame() using the
303 * methods in TrajectoryAnalysisModuleData.
304 * If other thread-local data is needed, this function should be
305 * overridden and it should create an instance of a class derived from
306 * TrajectoryAnalysisModuleData.
308 * \see TrajectoryAnalysisModuleData
310 virtual TrajectoryAnalysisModuleDataPointer startFrames(
311 const AnalysisDataParallelOptions &opt,
312 const SelectionCollection &selections);
314 * Analyzes a single frame.
316 * \param[in] frnr Frame number, a zero-based index that
317 * uniquely identifies the frame.
318 * \param[in] fr Current frame.
319 * \param[in] pbc Periodic boundary conditions for \p fr.
320 * \param[in,out] pdata Data structure for frame-local data.
322 * This method is called once for each frame to be analyzed, and should
323 * analyze the positions provided in the selections. Data handles and
324 * selections should be obtained from the \p pdata structure.
326 * For threaded analysis, this method is called asynchronously in
327 * different threads to analyze different frames. The \p pdata
328 * structure is one of the structures created with startFrames(),
329 * but no assumptions should be made about which of these data
330 * structures is used. It is guaranteed that two instances of
331 * analyzeFrame() are not running concurrently with the same \p pdata
333 * Any access to data structures not stored in \p pdata should be
334 * designed to be thread-safe.
336 virtual void analyzeFrame(int frnr, const t_trxframe &fr, t_pbc *pbc,
337 TrajectoryAnalysisModuleData *pdata) = 0;
339 * Finishes the analysis of frames.
341 * \param[in] pdata Data structure for thread-local data.
343 * This method is called once for each call of startFrames(), with the
344 * data structure returned by the corresponding startFrames().
345 * The \p pdata object should be destroyed by the caller after this
346 * function has been called.
348 * You only need to override this method if you need custom
349 * operations to combine data from the frame-local data structures
350 * to get the final result. In such cases, the data should be
351 * aggregated in this function and stored in a member attribute.
353 * The default implementation does nothing.
357 virtual void finishFrames(TrajectoryAnalysisModuleData *pdata);
360 * Postprocesses data after frames have been read.
362 * \param[in] nframes Total number of frames processed.
364 * This function is called after all finishFrames() calls have been
366 * \p nframes will equal the number of calls to analyzeFrame() that
369 virtual void finishAnalysis(int nframes) = 0;
371 * Writes output into files and/or standard output/error.
373 * All output from the module, excluding data written out for each
374 * frame during analyzeFrame(), should be confined into this function.
375 * This function is guaranteed to be called only after
378 virtual void writeOutput() = 0;
381 * Returns the number of datasets provided by the module.
385 int datasetCount() const;
387 * Returns a vector with the names of datasets provided by the module.
391 const std::vector<std::string> &datasetNames() const;
393 * Returns a pointer to the data set \p index.
395 * \param[in] index Data set to query for.
396 * \returns Reference to the requested data set.
397 * \throws APIError if \p index is not valid.
399 * \p index should be >= 0 and < datasetCount().
401 * The return value is not const to allow callers to add modules to the
402 * data sets. However, the AbstractAnalysisData interface does not
403 * provide any means to alter the data, so the module does not need to
404 * care about external modifications.
406 AbstractAnalysisData &datasetFromIndex(int index) const;
408 * Returns a pointer to the data set with name \p name
410 * \param[in] name Data set to query for.
411 * \returns Reference to the requested data set.
412 * \throws APIError if \p name is not valid.
414 * \p name should be one of the names returned by datasetNames().
416 * The return value is not const to allow callers to add modules to the
417 * data sets. However, the AbstractAnalysisData interface does not
418 * provide any means to alter the data, so the module does not need to
419 * care about external modifications.
421 AbstractAnalysisData &datasetFromName(const char *name) const;
425 * Initializes the dataset registration mechanism.
427 * \throws std::bad_alloc if out of memory.
429 TrajectoryAnalysisModule();
432 * Registers a dataset that exports data.
434 * \param data Data object to register.
435 * \param[in] name Name to register the dataset with.
436 * \throws std::bad_alloc if out of memory.
438 * Registers \p data as a dataset that provides output from the
439 * analysis module. Callers for the module can access the dataset
440 * with datasetFromName() using \p name as an AbstractAnalysisData
441 * object. This allows them to add their own data modules to do extra
444 * \p name must be unique across all calls within the same
445 * TrajectoryAnalysisModule instance.
447 void registerBasicDataset(AbstractAnalysisData *data, const char *name);
449 * Registers a parallelized dataset that exports data.
451 * \param data AnalysisData object to register.
452 * \param[in] name Name to register the dataset with.
453 * \throws std::bad_alloc if out of memory.
455 * This method works as registerBasicDataset(), but additionally allows
456 * data handles for \p data to be accessed using
457 * TrajectoryAnalysisData.
459 * \see registerBasicDataset()
461 void registerAnalysisDataset(AnalysisData *data, const char *name);
466 PrivateImplPointer<Impl> impl_;
469 * Needed to access the registered analysis data sets.
471 friend class TrajectoryAnalysisModuleData;
474 //! Smart pointer to manage a TrajectoryAnalysisModule.
475 typedef gmx_unique_ptr<TrajectoryAnalysisModule>::type
476 TrajectoryAnalysisModulePointer;