Add support for cumulative histograms
[alexxy/gromacs.git] / src / gromacs / analysisdata / modules / histogram.h
index 4530406be797eaa9bd0d9a0a45b6a1430e04015b..0717b4b822d1c68b76a5f0397f7700af3cfd90c8 100644 (file)
@@ -1,48 +1,53 @@
 /*
+ * 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 analysis data modules for calculating histograms.
  *
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
  * \inpublicapi
  * \ingroup module_analysisdata
  */
 #ifndef GMX_ANALYSISDATA_MODULES_HISTOGRAM_H
 #define GMX_ANALYSISDATA_MODULES_HISTOGRAM_H
 
-#include "../abstractdata.h"
-#include "../arraydata.h"
-#include "../datamodule.h"
-#include "../../utility/uniqueptr.h"
+#include <boost/shared_ptr.hpp>
+
+#include "gromacs/analysisdata/abstractdata.h"
+#include "gromacs/analysisdata/arraydata.h"
+#include "gromacs/analysisdata/datamodule.h"
 
 namespace gmx
 {
@@ -179,7 +184,7 @@ inline AnalysisHistogramSettingsInitializer
 histogramFromBins(real start, int nbins, real binwidth)
 {
     return AnalysisHistogramSettingsInitializer()
-        .start(start).binCount(nbins).binWidth(binwidth);
+               .start(start).binCount(nbins).binWidth(binwidth);
 }
 
 
@@ -202,9 +207,9 @@ class AnalysisHistogramSettings
          * This constructor is not explicit to allow initialization of
          * histograms directly from AnalysisHistogramSettingsInitializer:
          * \code
-         gmx::AnalysisDataSimpleHistogramModule *hist =
-            new gmx::AnalysisDataSimpleHistogramModule(
-                histogramFromRange(0.0, 5.0).binWidth(0.5));
+           gmx::AnalysisDataSimpleHistogramModule *hist =
+                   new gmx::AnalysisDataSimpleHistogramModule(
+                           histogramFromRange(0.0, 5.0).binWidth(0.5));
          * \endcode
          */
         AnalysisHistogramSettings(const AnalysisHistogramSettingsInitializer &settings);
@@ -232,18 +237,11 @@ class AnalysisHistogramSettings
 };
 
 
-namespace internal
-{
-
-class BasicHistogramImpl;
-
-} // namespace internal
-
 class AbstractAverageHistogram;
 
 //! Smart pointer to manage an AbstractAverageHistogram object.
-typedef gmx_unique_ptr<AbstractAverageHistogram>::type
-        AverageHistogramPointer;
+typedef boost::shared_ptr<AbstractAverageHistogram>
+    AverageHistogramPointer;
 
 /*! \brief
  * Base class for representing histograms averaged over frames.
@@ -255,6 +253,11 @@ typedef gmx_unique_ptr<AbstractAverageHistogram>::type
  * the main use of the object is to postprocess the histogram once the
  * calculation is finished.
  *
+ * This class can represent multiple histograms in one object: each column in
+ * the data is an independent histogram.
+ * The X values correspond to center of the bins, except for a cumulative
+ * histogram made with makeCumulative().
+ *
  * \inpublicapi
  * \ingroup module_analysisdata
  */
@@ -288,10 +291,20 @@ class AbstractAverageHistogram : public AbstractAnalysisArrayData
         AverageHistogramPointer clone() const;
         //! Normalizes the histogram such that the integral over it is one.
         void normalizeProbability();
-        //! Scales the value of each bin by an uniform scaling factor.
-        void scale(real norm);
+        /*! \brief
+         * Makes the histograms cumulative by summing up each bin to all bins
+         * after it.
+         *
+         * The X values in the data are adjusted such that they match the right
+         * edges of bins instead of bin centers.
+         */
+        void makeCumulative();
+        //! Scales a single histogram by a uniform scaling factor.
+        void scaleSingle(int index, real factor);
+        //! Scales all histograms by a uniform scaling factor.
+        void scaleAll(real factor);
         //! Scales the value of each bin by a different scaling factor.
-        void scaleVector(real norm[]);
+        void scaleAllByVector(real factor[]);
         /*! \brief
          * Notifies attached modules of the histogram data.
          *
@@ -326,16 +339,23 @@ class AbstractAverageHistogram : public AbstractAnalysisArrayData
 /*! \brief
  * Data module for per-frame histograms.
  *
- * Output data contains the same number of frames as the input data.
- * Each frame contains the histogram for the points in that frame.
- * All input columns are averaged into the same histogram.
- * The number of columns equals the number of bins in the histogram.
+ * Output data contains the same number of frames and data sets as the input
+ * data.  Each frame contains the histogram(s) for the points in that frame.
+ * Each input data set is processed independently into the corresponding output
+ * data set.  Missing values are ignored.
+ * All input columns for a data set are averaged into the same histogram.
+ * The number of columns for all data sets equals the number of bins in the
+ * histogram.
+ *
+ * The histograms are accumulated as 64-bit integers within a frame and summed
+ * in double precision across frames, even if the output data is in single
+ * precision.
  *
  * \inpublicapi
  * \ingroup module_analysisdata
  */
 class AnalysisDataSimpleHistogramModule : public AbstractAnalysisData,
-                                          public AnalysisDataModuleInterface
+                                          public AnalysisDataModuleParallel
 {
     public:
         /*! \brief
@@ -367,9 +387,13 @@ class AnalysisDataSimpleHistogramModule : public AbstractAnalysisData,
         //! Returns bin properties for the histogram.
         const AnalysisHistogramSettings &settings() const;
 
+        virtual int frameCount() const;
+
         virtual int flags() const;
 
-        virtual void dataStarted(AbstractAnalysisData *data);
+        virtual bool parallelDataStarted(
+            AbstractAnalysisData              *data,
+            const AnalysisDataParallelOptions &options);
         virtual void frameStarted(const AnalysisDataFrameHeader &header);
         virtual void pointsAdded(const AnalysisDataPointSetRef &points);
         virtual void frameFinished(const AnalysisDataFrameHeader &header);
@@ -379,7 +403,9 @@ class AnalysisDataSimpleHistogramModule : public AbstractAnalysisData,
         virtual AnalysisDataFrameRef tryGetDataFrameInternal(int index) const;
         virtual bool requestStorageInternal(int nframes);
 
-        PrivateImplPointer<internal::BasicHistogramImpl> impl_;
+        class Impl;
+
+        PrivateImplPointer<Impl> impl_;
 
         // Copy and assign disallowed by base.
 };
@@ -388,19 +414,26 @@ class AnalysisDataSimpleHistogramModule : public AbstractAnalysisData,
 /*! \brief
  * Data module for per-frame weighted histograms.
  *
- * Output data contains the same number of frames as the input data.
- * Each frame contains the histogram for the points in that frame, interpreted
- * such that the first column passed to pointsAdded() determines the bin and
- * the rest give weights to be added to that bin (input data should have at
- * least two colums, and at least two columns should be added at the same time).
- * All input columns are averaged into the same histogram.
- * The number of columns equals the number of bins in the histogram.
+ * Output data contains the same number of frames and data sets as the input
+ * data.  Each frame contains the histogram(s) for the points in that frame,
+ * interpreted such that the first column passed to pointsAdded() determines
+ * the bin and the rest give weights to be added to that bin (input data should
+ * have at least two colums, and at least two columns should be added at the
+ * same time).
+ * Each input data set is processed independently into the corresponding output
+ * data set.
+ * All input columns for a data set are averaged into the same histogram.
+ * The number of columns for all data sets equals the number of bins in the
+ * histogram.
+ *
+ * The histograms are accumulated in double precision, even if the output data
+ * is in single precision.
  *
  * \inpublicapi
  * \ingroup module_analysisdata
  */
 class AnalysisDataWeightedHistogramModule : public AbstractAnalysisData,
-                                            public AnalysisDataModuleInterface
+                                            public AnalysisDataModuleParallel
 {
     public:
         //! \copydoc AnalysisDataSimpleHistogramModule::AnalysisDataSimpleHistogramModule()
@@ -418,9 +451,13 @@ class AnalysisDataWeightedHistogramModule : public AbstractAnalysisData,
         //! \copydoc AnalysisDataSimpleHistogramModule::settings()
         const AnalysisHistogramSettings &settings() const;
 
+        virtual int frameCount() const;
+
         virtual int flags() const;
 
-        virtual void dataStarted(AbstractAnalysisData *data);
+        virtual bool parallelDataStarted(
+            AbstractAnalysisData              *data,
+            const AnalysisDataParallelOptions &options);
         virtual void frameStarted(const AnalysisDataFrameHeader &header);
         virtual void pointsAdded(const AnalysisDataPointSetRef &points);
         virtual void frameFinished(const AnalysisDataFrameHeader &header);
@@ -430,7 +467,9 @@ class AnalysisDataWeightedHistogramModule : public AbstractAnalysisData,
         virtual AnalysisDataFrameRef tryGetDataFrameInternal(int index) const;
         virtual bool requestStorageInternal(int nframes);
 
-        PrivateImplPointer<internal::BasicHistogramImpl> impl_;
+        class Impl;
+
+        PrivateImplPointer<Impl> impl_;
 
         // Copy and assign disallowed by base.
 };
@@ -440,20 +479,20 @@ class AnalysisDataWeightedHistogramModule : public AbstractAnalysisData,
  * Data module for bin averages.
  *
  * Output data contains one row for each bin; see AbstractAverageHistogram.
- * Output data contains three columns: the first is the average over all frames
- * for that bin, the second is the standard deviation of the values, and the
- * third is the number of samples in that bin.
+ * Output data contains one column for each input data set.
+ * The value in a column is the average over all frames of that data set for
+ * that bin.
  * The input data is interpreted such that the first column passed to
  * pointsAdded() determines the bin and the rest give values to be added to
  * that bin (input data should have at least two colums, and at least two
  * columns should be added at the same time).
- * All input columns are averaged into the same histogram.
+ * All input columns for a data set are averaged into the same histogram.
  *
  * \inpublicapi
  * \ingroup module_analysisdata
  */
 class AnalysisDataBinAverageModule : public AbstractAnalysisArrayData,
-                                     public AnalysisDataModuleInterface
+                                     public AnalysisDataModuleSerial
 {
     public:
         //! \copydoc AnalysisDataSimpleHistogramModule::AnalysisDataSimpleHistogramModule()
@@ -466,7 +505,7 @@ class AnalysisDataBinAverageModule : public AbstractAnalysisArrayData,
         void init(const AnalysisHistogramSettings &settings);
 
         //! \copydoc AnalysisDataSimpleHistogramModule::settings()
-        const AnalysisHistogramSettings &settings() const { return settings_; }
+        const AnalysisHistogramSettings &settings() const;
 
         virtual int flags() const;
 
@@ -477,20 +516,22 @@ class AnalysisDataBinAverageModule : public AbstractAnalysisArrayData,
         virtual void dataFinished();
 
     private:
-        AnalysisHistogramSettings  settings_;
+        class Impl;
+
+        PrivateImplPointer<Impl>   impl_;
 
         // Copy and assign disallowed by base.
 };
 
 //! Smart pointer to manage an AnalysisDataSimpleHistogramModule object.
 typedef boost::shared_ptr<AnalysisDataSimpleHistogramModule>
-        AnalysisDataSimpleHistogramModulePointer;
+    AnalysisDataSimpleHistogramModulePointer;
 //! Smart pointer to manage an AnalysisDataWeightedHistogramModule object.
 typedef boost::shared_ptr<AnalysisDataWeightedHistogramModule>
-        AnalysisDataWeightedHistogramModulePointer;
+    AnalysisDataWeightedHistogramModulePointer;
 //! Smart pointer to manage an AnalysisDataBinAverageModule object.
 typedef boost::shared_ptr<AnalysisDataBinAverageModule>
-        AnalysisDataBinAverageModulePointer;
+    AnalysisDataBinAverageModulePointer;
 
 } // namespace gmx