Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / trajectoryanalysis / analysismodule.h
index 32bc2c6452962be0caf610d9194daa9611437d7c..a3846214b7ea14b2282c5857d3e6bd6c0a12c02d 100644 (file)
@@ -1,39 +1,43 @@
 /*
+ * This file is part of the GROMACS molecular simulation package.
  *
- *                This source code is part of
+ * Copyright (c) 2010,2011,2012,2013,2014, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
  *
- *                 G   R   O   M   A   C   S
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
  *
- *          GROningen MAchine for Chemical Simulations
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
- * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2009, The GROMACS development team,
- * check out http://www.gromacs.org for more information.
-
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
  *
- * If you want to redistribute modifications, please consider that
- * scientific software is very special. Version control is crucial -
- * bugs must be traceable. We will be happy to consider code for
- * inclusion in the official distribution, but derived work must not
- * be called official GROMACS. Details are found in the README & COPYING
- * files - if they are missing, get the official version at www.gromacs.org.
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
  *
  * To help us fund GROMACS development, we humbly ask that you cite
- * the papers on the package - you can find them in the top README file.
- *
- * For more info, check our website at http://www.gromacs.org
+ * the research papers on the package. Check out http://www.gromacs.org.
  */
 /*! \file
  * \brief
  * Declares gmx::TrajectoryAnalysisModule and
  * gmx::TrajectoryAnalysisModuleData.
  *
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
  * \inpublicapi
  * \ingroup module_trajectoryanalysis
  */
 #include <string>
 #include <vector>
 
-#include "../legacyheaders/typedefs.h"
+#include <boost/shared_ptr.hpp>
+
+#include "gromacs/selection/selection.h" // For gmx::SelectionList
+#include "gromacs/utility/common.h"
 
-#include "../selection/selection.h" // For gmx::SelectionList
-#include "../utility/common.h"
-#include "../utility/uniqueptr.h"
+struct t_pbc;
+struct t_trxframe;
 
 namespace gmx
 {
@@ -67,7 +73,8 @@ class TrajectoryAnalysisSettings;
  *
  * Thread-local storage of data handles and selections is implemented in this
  * class; TrajectoryAnalysisModule instances can access the thread-local values
- * using dataHandle() and parallelSelection().
+ * in their TrajectoryAnalysisModule::analyzeFrame() method using dataHandle()
+ * and parallelSelection().
  *
  * \see TrajectoryAnalysisModule::startFrames()
  * \see TrajectoryAnalysisModule::analyzeFrame()
@@ -84,7 +91,13 @@ class TrajectoryAnalysisModuleData
         /*! \brief
          * Performs any finishing actions after all frames have been processed.
          *
-         * This function is called immediately before the destructor.
+         * \throws  unspecified Implementation may throw exceptions to indicate
+         *      errors.
+         *
+         * This function is called immediately before the destructor, after
+         * TrajectoryAnalysisModule::finishFrames().
+         * Derived classes should implement any final operations that need to
+         * be done after successful analysis.
          * All implementations should call finishDataHandles().
          */
         virtual void finish() = 0;
@@ -92,16 +105,39 @@ class TrajectoryAnalysisModuleData
         /*! \brief
          * Returns a data handle for a given dataset.
          *
-         * Allowed data sets are those that have been registered with
+         * \param[in] data  Analysis data object.
+         * \returns   Data handle for \p data stored in this thread-local data.
+         *
+         * \p data should have previously been registered with
          * TrajectoryAnalysisModule::registerAnalysisDataset().
+         * If \p data has zero columns in all data sets, the returned data
+         * handle is invalid.
+         *
+         * Does not throw.
          */
         AnalysisDataHandle dataHandle(const AnalysisData &data);
         /*! \brief
          * Returns a selection that corresponds to the given selection.
+         *
+         * \param[in] selection Global selection object.
+         * \returns   Selection object corresponding to this thread-local data.
+         *
+         * \p selection is the selection object that was obtained from
+         * SelectionOption.  The return value is the corresponding selection
+         * in the selection collection with which this data object was
+         * constructed with.
+         *
+         * Does not throw.
          */
         Selection parallelSelection(const Selection &selection);
         /*! \brief
          * Returns a set of selection that corresponds to the given selections.
+         *
+         * \throws std::bad_alloc if out of memory.
+         *
+         * Works as parallelSelection(), but for a list of selections at once.
+         *
+         * \see parallelSelection()
          */
         SelectionList parallelSelections(const SelectionList &selections);
 
@@ -112,18 +148,24 @@ class TrajectoryAnalysisModuleData
          * \param[in] module     Analysis module to use for data objects.
          * \param[in] opt        Data parallelization options.
          * \param[in] selections Thread-local selection collection.
+         * \throws  std::bad_alloc if out of memory.
+         * \throws  unspecified Can throw any exception thrown by
+         *      AnalysisData::startData().
          *
          * Calls AnalysisData::startData() on all data objects registered with
          * TrajectoryAnalysisModule::registerAnalysisDataset() in \p module.
          * The handles are accessible through dataHandle().
          */
-        TrajectoryAnalysisModuleData(TrajectoryAnalysisModule *module,
+        TrajectoryAnalysisModuleData(TrajectoryAnalysisModule          *module,
                                      const AnalysisDataParallelOptions &opt,
-                                     const SelectionCollection &selections);
+                                     const SelectionCollection         &selections);
 
         /*! \brief
          * Calls finishData() on all data handles.
          *
+         * \throws  unspecified Can throw any exception thrown by
+         *      AnalysisDataHandle::finishData().
+         *
          * This function should be called from the implementation of finish()
          * in all subclasses.
          */
@@ -132,19 +174,19 @@ class TrajectoryAnalysisModuleData
     private:
         class Impl;
 
-        PrivateImplPointer<Impl> _impl;
+        PrivateImplPointer<Impl> impl_;
 };
 
 //! Smart pointer to manage a TrajectoryAnalysisModuleData object.
-typedef gmx_unique_ptr<TrajectoryAnalysisModuleData>::type
-        TrajectoryAnalysisModuleDataPointer;
+typedef boost::shared_ptr<TrajectoryAnalysisModuleData>
+    TrajectoryAnalysisModuleDataPointer;
 
 /*! \brief
- * Base class for trajectory analysis methods.
+ * Base class for trajectory analysis modules.
  *
  * Trajectory analysis methods should derive from this class and override the
- * necessary virtual functions to implement initialization (initOptions(),
- * initOptionsDone(), initAnalysis(), initAfterFirstFrame()), per-frame analysis
+ * necessary virtual methods to implement initialization (initOptions(),
+ * optionsFinished(), initAnalysis(), initAfterFirstFrame()), per-frame analysis
  * (analyzeFrame()), and final processing (finishFrames(), finishAnalysis(),
  * writeOutput()).
  *
@@ -155,6 +197,32 @@ typedef gmx_unique_ptr<TrajectoryAnalysisModuleData>::type
  * to the other methods.  The default implementation of startFrames() can be
  * used if only data handles and selections need to be thread-local.
  *
+ * To get the full benefit from this class,
+ * \ref module_analysisdata "analysis data objects" and
+ * \ref module_selection "selections" should be used in the implementation.
+ * See the corresponding modules' documentation for details of how they work.
+ *
+ * Typical way of using AnalysisData in derived classes is to have the
+ * AnalysisData object as a member variable and register it using
+ * registerAnalysisDataset().  Analysis modules are initialized in
+ * initAnalysis() and the processing chain is initialized.  If any of the
+ * modules is required, e.g., for post-processing in finishAnalysis(), it can
+ * be stored in a member variable.  To add data to the data object in
+ * analyzeFrame(), a data handle is obtained using
+ * TrajectoryAnalysisModuleData::dataHandle().
+ *
+ * Typical way of using selections in derived classes is to have the required
+ * \ref Selection objects (or ::SelectionList objects) as member variables, and
+ * add the required selection options in initOptions().  These member variables
+ * can be accessed in initAnalysis() to get general information about the
+ * selections.  In analyzeFrame(), these selection objects should not be used
+ * directly, but instead TrajectoryAnalysisModuleData::parallelSelection()
+ * should be used to obtain a selection object that works correctly also for
+ * parallel analysis.
+ *
+ * Derived classes should use exceptions to indicate errors in the virtual
+ * methods.
+ *
  * \inpublicapi
  * \ingroup module_trajectoryanalysis
  */
@@ -166,27 +234,47 @@ class TrajectoryAnalysisModule
         /*! \brief
          * Initializes options understood by the module.
          *
-         * In addition to initializing the options, this function can also
-         * provide information about its requirements using the \p settings
-         * object; see TrajectoryAnalysisSettings for more details.
+         * \param[in,out] options  Options object to add the options to.
+         * \param[in,out] settings Settings to pass to and from the module.
+         *
+         * This method is called first after the constructor, and it should
+         * add options understood by the module to \p options.  Output values
+         * from options (including selections) should be stored in member
+         * variables.
+         *
+         * In addition to initializing the options, this method can also
+         * provide information about the module's requirements using the
+         * \p settings object; see TrajectoryAnalysisSettings for more details.
          *
          * If settings depend on the option values provided by the user, see
-         * initOptionsDone().
+         * optionsFinished().
          */
-        virtual Options &initOptions(TrajectoryAnalysisSettings *settings) = 0;
+        virtual void initOptions(Options                    *options,
+                                 TrajectoryAnalysisSettings *settings) = 0;
         /*! \brief
          * Called after all option values have been set.
          *
+         * \param[in,out] options  Options object in which options are stored.
+         * \param[in,out] settings Settings to pass to and from the module.
+         *
+         * This method is called after option values have been assigned (but
+         * interactive selection input has not yet been performed).
+         *
          * If the module needs to change settings that affect topology loading
-         * or selection initialization based on option values, this function
-         * has to be overridden.
+         * (can be done using the \p settings object) or selection
+         * initialization (can be done using SelectionOptionInfo) based on
+         * option values, this method has to be overridden.
          *
          * The default implementation does nothing.
          */
-        virtual void initOptionsDone(TrajectoryAnalysisSettings *settings);
+        virtual void optionsFinished(Options                    *options,
+                                     TrajectoryAnalysisSettings *settings);
         /*! \brief
          * Initializes the analysis.
          *
+         * \param[in]    settings Settings to pass to and from the module.
+         * \param[in]    top      Topology information.
+         *
          * When this function is called, selections have been initialized based
          * on user input, and a topology has been loaded if provided by the
          * user.  For dynamic selections, the selections have been evaluated to
@@ -194,7 +282,7 @@ class TrajectoryAnalysisModule
          * analyzeFrame() are always a subset of the selections provided here.
          */
         virtual void initAnalysis(const TrajectoryAnalysisSettings &settings,
-                                  const TopologyInformation &top) = 0;
+                                  const TopologyInformation        &top) = 0;
         /*! \brief
          * Performs additional initialization after reading the first frame.
          *
@@ -207,7 +295,8 @@ class TrajectoryAnalysisModule
          *
          * The default implementation does nothing.
          */
-        virtual void initAfterFirstFrame(const t_trxframe &fr);
+        virtual void initAfterFirstFrame(const TrajectoryAnalysisSettings &settings,
+                                         const t_trxframe                 &fr);
 
         /*! \brief
          * Starts the analysis of frames.
@@ -231,8 +320,8 @@ class TrajectoryAnalysisModule
          * \see TrajectoryAnalysisModuleData
          */
         virtual TrajectoryAnalysisModuleDataPointer startFrames(
-                const AnalysisDataParallelOptions &opt,
-                const SelectionCollection &selections);
+            const AnalysisDataParallelOptions &opt,
+            const SelectionCollection         &selections);
         /*! \brief
          * Analyzes a single frame.
          *
@@ -242,14 +331,15 @@ class TrajectoryAnalysisModule
          * \param[in]     pbc    Periodic boundary conditions for \p fr.
          * \param[in,out] pdata  Data structure for frame-local data.
          *
-         * This function is called once for each frame to be analyzed,
-         * and should analyze the positions provided in \p sel.
+         * This method is called once for each frame to be analyzed, and should
+         * analyze the positions provided in the selections.  Data handles and
+         * selections should be obtained from the \p pdata structure.
          *
-         * For threaded analysis, this function is called asynchronously in
-         * different threads to analyze different frames. The \p pdata
+         * For threaded analysis, this method is called asynchronously in
+         * different threads to analyze different frames.  The \p pdata
          * structure is one of the structures created with startFrames(),
          * but no assumptions should be made about which of these data
-         * structures is used. It is guaranteed that two instances of
+         * structures is used.  It is guaranteed that two instances of
          * analyzeFrame() are not running concurrently with the same \p pdata
          * data structure.
          * Any access to data structures not stored in \p pdata should be
@@ -262,14 +352,14 @@ class TrajectoryAnalysisModule
          *
          * \param[in]  pdata    Data structure for thread-local data.
          *
-         * This function is called once for each call of startFrames(),
-         * with the data structure returned by the corresponding startFrames().
+         * This method is called once for each call of startFrames(), with the
+         * data structure returned by the corresponding startFrames().
          * The \p pdata object should be destroyed by the caller after this
          * function has been called.
          *
          * You only need to override this method if you need custom
          * operations to combine data from the frame-local data structures
-         * to get the final result. In such cases, the data should be
+         * to get the final result.  In such cases, the data should be
          * aggregated in this function and stored in a member attribute.
          *
          * The default implementation does nothing.
@@ -281,8 +371,12 @@ class TrajectoryAnalysisModule
         /*! \brief
          * Postprocesses data after frames have been read.
          *
+         * \param[in]  nframes  Total number of frames processed.
+         *
          * This function is called after all finishFrames() calls have been
          * called.
+         * \p nframes will equal the number of calls to analyzeFrame() that
+         * have occurred.
          */
         virtual void finishAnalysis(int nframes) = 0;
         /*! \brief
@@ -295,20 +389,38 @@ class TrajectoryAnalysisModule
          */
         virtual void writeOutput() = 0;
 
+        /*! \brief
+         * Returns the name of the analysis module.
+         *
+         * Does not throw.
+         */
+        const char *name() const;
+        /*! \brief
+         * Returns short description for the analysis module.
+         *
+         * Does not throw.
+         */
+        const char *description() const;
         /*! \brief
          * Returns the number of datasets provided by the module.
+         *
+         * Does not throw.
          */
         int datasetCount() const;
         /*! \brief
-         * Returns a vector with the names of the datasets.
+         * Returns a vector with the names of datasets provided by the module.
+         *
+         * Does not throw.
          */
         const std::vector<std::string> &datasetNames() const;
         /*! \brief
          * Returns a pointer to the data set \p index.
          *
          * \param[in] index  Data set to query for.
-         * \returns   A pointer to the data set, or NULL if \p index is not
-         *      valid.
+         * \returns   Reference to the requested data set.
+         * \throws    APIError if \p index is not valid.
+         *
+         * \p index should be >= 0 and < datasetCount().
          *
          * The return value is not const to allow callers to add modules to the
          * data sets. However, the AbstractAnalysisData interface does not
@@ -320,8 +432,10 @@ class TrajectoryAnalysisModule
          * Returns a pointer to the data set with name \p name
          *
          * \param[in] name  Data set to query for.
-         * \returns   A pointer to the data set, or NULL if \p name is not
-         *      recognized.
+         * \returns   Reference to the requested data set.
+         * \throws    APIError if \p name is not valid.
+         *
+         * \p name should be one of the names returned by datasetNames().
          *
          * The return value is not const to allow callers to add modules to the
          * data sets. However, the AbstractAnalysisData interface does not
@@ -331,22 +445,51 @@ class TrajectoryAnalysisModule
         AbstractAnalysisData &datasetFromName(const char *name) const;
 
     protected:
-        //! Initializes the dataset registration mechanism.
-        TrajectoryAnalysisModule();
+        /*! \brief
+         * Initializes the dataset registration mechanism.
+         *
+         * \param[in] name         Name for the module.
+         * \param[in] description  One-line description for the module.
+         * \throws    std::bad_alloc if out of memory.
+         */
+        TrajectoryAnalysisModule(const char *name, const char *description);
 
         /*! \brief
          * Registers a dataset that exports data.
+         *
+         * \param     data  Data object to register.
+         * \param[in] name  Name to register the dataset with.
+         * \throws    std::bad_alloc if out of memory.
+         *
+         * Registers \p data as a dataset that provides output from the
+         * analysis module.  Callers for the module can access the dataset
+         * with datasetFromName() using \p name as an AbstractAnalysisData
+         * object.  This allows them to add their own data modules to do extra
+         * processing.
+         *
+         * \p name must be unique across all calls within the same
+         * TrajectoryAnalysisModule instance.
          */
         void registerBasicDataset(AbstractAnalysisData *data, const char *name);
         /*! \brief
          * Registers a parallelized dataset that exports data.
+         *
+         * \param     data  AnalysisData object to register.
+         * \param[in] name  Name to register the dataset with.
+         * \throws    std::bad_alloc if out of memory.
+         *
+         * This method works as registerBasicDataset(), but additionally allows
+         * data handles for \p data to be accessed using
+         * TrajectoryAnalysisData.
+         *
+         * \see registerBasicDataset()
          */
         void registerAnalysisDataset(AnalysisData *data, const char *name);
 
     private:
         class Impl;
 
-        PrivateImplPointer<Impl> _impl;
+        PrivateImplPointer<Impl> impl_;
 
         /*! \brief
          * Needed to access the registered analysis data sets.
@@ -355,8 +498,8 @@ class TrajectoryAnalysisModule
 };
 
 //! Smart pointer to manage a TrajectoryAnalysisModule.
-typedef gmx_unique_ptr<TrajectoryAnalysisModule>::type
-        TrajectoryAnalysisModulePointer;
+typedef boost::shared_ptr<TrajectoryAnalysisModule>
+    TrajectoryAnalysisModulePointer;
 
 } // namespace gmx