2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013, 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::AbstractAnalysisData.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_analysisdata
43 #ifndef GMX_ANALYSISDATA_ABSTRACTDATA_H
44 #define GMX_ANALYSISDATA_ABSTRACTDATA_H
46 #include <boost/shared_ptr.hpp>
48 #include "../legacyheaders/types/simple.h"
50 #include "../utility/common.h"
55 class AnalysisDataModuleInterface;
56 class AnalysisDataFrameHeader;
57 class AnalysisDataFrameRef;
58 class AnalysisDataPointSetRef;
59 class AnalysisDataStorage;
61 //! Smart pointer for managing a generic analysis data module.
62 typedef boost::shared_ptr<AnalysisDataModuleInterface> AnalysisDataModulePointer;
65 * Abstract base class for all objects that provide data.
67 * The public interface includes methods for querying the data (isMultipoint(),
68 * columnCount(), frameCount(), tryGetDataFrame(), getDataFrame(),
69 * requestStorage()) and methods for using modules for processing the data
70 * (addModule(), addColumnModule(), applyModule()).
72 * Notice that even for non-const objects, the interface does not provide any
73 * means of altering the data. It is only possible to add modules, making it
74 * relatively safe to return a non-const pointer of this type pointing to an
75 * internal data structure without worrying about possible modifications of the
79 * This class also provides protected methods for use in derived classes.
80 * The properties returned by isMultipoint() and columnCount() must be set using
81 * setMultipoint() and setColumnCount(), and notify*() methods must be used to
82 * report when data becomes available for modules to process it.
83 * There are also two protected pure virtual methods that need to be
84 * implemented to provide access to stored data: requestStorageInternal() and
85 * tryGetDataFrameInternal().
87 * It is up to subclasses to ensure that the protected methods are called in a
88 * correct sequence (the methods will assert in most incorrect use cases), and
89 * that the data provided through the public interface matches that passed to
90 * the modules with the notify methods.
91 * Helper class AnalysisDataStorage provides a default implementation for
92 * storing data (calls to the pure virtual methods can simply be forwarded to
93 * appropriate methods in the helper class), and takes care of correctly
94 * calling the notification methods when new data is added to the storage.
95 * In most cases, it should be used to implement the derived classes.
98 * Currently, it is not possible to continue using the data object if an
99 * attached module throws an exception during data processing; it is only safe
100 * to destroy such data object.
103 * Improve the exception-handling semantics. In most cases, it doesn't make
104 * much sense to continue data processing after one module fails, but having
105 * the alternative would not hurt.
108 * \ingroup module_analysisdata
110 class AbstractAnalysisData
113 virtual ~AbstractAnalysisData();
116 * Whether the data can have multiple points in the same column
119 * \returns \c true if multiple points in the same column are
120 * allowed within a single frame.
122 * This kind of data can appear in many histogramming applications
123 * (e.g., RDFs), where each trajectory frame has several data points
124 * (possibly a different number for each frame). The current interface
125 * doesn't support storing such data, but this should rarely be
128 * The returned value does not change after modules have been notified
131 * Derived classes can change the type by calling setMultipoint()
132 * subject to the above restriction.
133 * If this is not done, the function always returns false.
138 bool isMultipoint() const { return bMultiPoint_; }
140 * Returns the number of columns in the data.
142 * \returns The number of columns in the data.
144 * If the number of columns is yet known, returns 0.
145 * The returned value does not change after modules have been notified
146 * of data start, but may change multiple times before that, depending
147 * on the actual data class.
149 * Derived classes should set the number of columns with
150 * setColumnCount(), within the above limitations.
155 int columnCount() const { return columnCount_; }
157 * Returns the total number of frames in the data.
159 * \returns The total number of frames in the data.
161 * This function returns the number of frames that the object has
162 * produced. If requestStorage() has been successfully called,
163 * tryGetDataframe() or getDataFrame() can be used to access some or
164 * all of these frames.
168 int frameCount() const;
170 * Access stored data.
172 * \param[in] index Zero-based frame index to access.
173 * \returns Frame reference to frame \p index, or an invalid
174 * reference if no such frame is available.
176 * Does not throw. Failure to access a frame with the given index is
177 * indicated through the return value. Negative \p index is allowed,
178 * and will always result in an invalid reference being returned.
180 * \see requestStorage()
181 * \see getDataFrame()
183 AnalysisDataFrameRef tryGetDataFrame(int index) const;
185 * Access stored data.
187 * \param[in] index Zero-based frame index to access.
188 * \returns Frame reference to frame \p index.
189 * \throws APIError if the requested frame is not accessible.
191 * If the data is not certainly available, use tryGetDataFrame().
193 * \see requestStorage()
194 * \see tryGetDataFrame()
196 AnalysisDataFrameRef getDataFrame(int index) const;
198 * Request storage of frames.
200 * \param[in] nframes Request storing at least \c nframes previous
201 * frames (-1 = request storing all). Must be >= -1.
202 * \returns true if the request could be satisfied.
204 * If called multiple times, the largest request is honored.
206 * Does not throw. Failure to honor the request is indicated through
209 * \see getDataFrame()
210 * \see tryGetDataFrame()
212 bool requestStorage(int nframes);
215 * Adds a module to process the data.
217 * \param module Module to add.
218 * \throws APIError if
219 * - \p module is not compatible with the data object
220 * - data has already been added to the data object and everything
221 * is not available through getDataFrame().
223 * If data has already been added to the module, the new module
224 * immediately processes all existing data. APIError is thrown
225 * if all data is not available through getDataFrame().
227 * The caller can keep a copy of the module pointer if it requires
228 * later access to the module.
230 * If the method throws, the state of the data object is not changed.
231 * The state of the data module is indeterminate.
233 void addModule(AnalysisDataModulePointer module);
235 * Adds a module that processes only a subset of the columns.
237 * \param[in] col First column.
238 * \param[in] span Number of columns.
239 * \param module Module to add.
240 * \throws APIError in same situations as addModule().
243 * This method doesn't currently work in all cases with multipoint
244 * data. In particular, if the added module requests storage and uses
245 * getDataFrame(), it will behave unpredictably (most likely asserts).
249 void addColumnModule(int col, int span, AnalysisDataModulePointer module);
251 * Applies a module to process data that is ready.
253 * \param module Module to apply.
254 * \throws APIError in same situations as addModule().
256 * This function works as addModule(), except that it does not keep a
257 * reference to \p module within the data object after it returns.
258 * Also, it can only be called after the data is ready, and only if
259 * getDataFrame() gives access to all of the data.
260 * It is provided for additional flexibility in postprocessing
264 * Currently, this method may not work correctly if \p module requests
265 * storage (addModule() has the same problem if called after data is
268 void applyModule(AnalysisDataModuleInterface *module);
273 * Initializes a new analysis data object.
275 * \throws std::bad_alloc if out of memory.
277 AbstractAnalysisData();
280 * Sets the number of columns.
282 * \param[in] columnCount Number of columns in the data (must be > 0).
284 * Can be called only before notifyDataStart(), otherwise asserts.
285 * Multiple calls are only allowed if all of them occur before
286 * addModule() has been called, otherwise asserts (a single call
287 * can occur after addModule() if no calls have been made earlier).
289 * Does not throw, but this may change with the below todo item.
292 * Consider whether the semantics with respect to addModule() and
293 * notifyDataStart(), and the performed checks, are suitable for all
298 void setColumnCount(int columnCount);
300 * Sets whether the data has multiple points per column in a frame.
302 * \param[in] multipoint Whether multiple points per column are
305 * Can be called only before addModule() or notifyDataStart(),
308 * Does not throw, but this may change with the todo item in
311 * \see isMultipoint()
313 void setMultipoint(bool multipoint);
316 * Implements access to data frames.
318 * \param[in] index Zero-based frame index to access.
319 * \returns Frame reference to frame \p index, or an invalid
320 * reference if no such frame is available.
322 * Must not throw. Failure to access a frame with the given index is
323 * indicated through the return value.
325 * Code in derived classes can assume that \p index is non-negative and
326 * less than frameCount().
328 * Derived classes can choose to return an invalid reference if
329 * requestStorageInternal() has not been called at all, or if the frame
330 * is too old (compared to the value given to requestStorageInternal()).
332 * This method is called internally by tryGetDataFrame() and
335 * \see AnalysisDataStorage
337 virtual AnalysisDataFrameRef tryGetDataFrameInternal(int index) const = 0;
339 * Implements storage requests.
341 * \param[in] nframes Request storing at least \c nframes previous
342 * frames (-1 = request storing all). Will be either -1 or >0.
343 * \returns true if the request could be satisfied.
345 * Must not throw. Failure to access a frame with the given index is
346 * indicated through the return value.
348 * Derived classes should be prepared for any number of calls to this
349 * method before notifyDataStart() is called (and during that call).
351 * This method is called internally by requestStorage().
353 * \see AnalysisDataStorage
355 virtual bool requestStorageInternal(int nframes) = 0;
358 * Notifies attached modules of the start of data.
360 * \throws APIError if any attached data module is not compatible.
361 * \throws unspecified Any exception thrown by attached data modules
362 * in AnalysisDataModuleInterface::dataStarted().
364 * Should be called once, after data properties have been set with
365 * setColumnCount() and isMultipoint(), and before any of the
366 * notification functions. The derived class should prepare for
367 * requestStorage() calls from the attached modules.
369 void notifyDataStart();
371 * Notifies attached modules of the start of a frame.
373 * \param[in] header Header information for the frame that is starting.
374 * \throws unspecified Any exception thrown by attached data modules
375 * in AnalysisDataModuleInterface::frameStarted().
377 * Should be called once for each frame, before notifyPointsAdd() calls
380 void notifyFrameStart(const AnalysisDataFrameHeader &header) const;
382 * Notifies attached modules of the addition of points to the
385 * \param[in] points Set of points added (also provides access to
387 * \throws APIError if any attached data module is not compatible.
388 * \throws unspecified Any exception thrown by attached data modules
389 * in AnalysisDataModuleInterface::pointsAdded().
391 * Can be called zero or more times for each frame.
392 * The caller should ensure that any column occurs at most once in the
393 * calls, unless the data is multipoint.
394 * For efficiency reasons, calls to this method should be aggregated
395 * whenever possible, i.e., it's better to handle multiple columns or
396 * even the whole frame in a single call rather than calling the method
397 * for each column separately.
399 void notifyPointsAdd(const AnalysisDataPointSetRef &points) const;
401 * Notifies attached modules of the end of a frame.
403 * \param[in] header Header information for the frame that is ending.
404 * \throws unspecified Any exception thrown by attached data modules
405 * in AnalysisDataModuleInterface::frameFinished().
407 * Should be called once for each call of notifyFrameStart(), after any
408 * notifyPointsAdd() calls for the frame.
409 * \p header should be identical to that used in the corresponding
410 * notifyFrameStart() call.
412 void notifyFrameFinish(const AnalysisDataFrameHeader &header);
414 * Notifies attached modules of the end of data.
416 * \throws unspecified Any exception thrown by attached data modules
417 * in AnalysisDataModuleInterface::dataFinished().
419 * Should be called once, after all the other notification calls.
421 void notifyDataFinish() const;
427 PrivateImplPointer<Impl> impl_;
432 * Needed to provide access to notification methods.
434 friend class AnalysisDataStorage;