Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / analysisdata / abstractdata.cpp
index d1260cd23774afb7c5236f497d9782bc4d8da2c4..9d21ccad6c9c8d890e3d43fedea8db140b02017a 100644 (file)
@@ -1,47 +1,57 @@
 /*
+ * 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.
  */
 /*! \internal \file
  * \brief
  * Implements gmx::AbstractAnalysisData.
  *
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
  * \ingroup module_analysisdata
  */
-#include "gromacs/analysisdata/abstractdata.h"
+#include "gmxpre.h"
+
+#include "abstractdata.h"
+
+#include <vector>
 
+#include "gromacs/analysisdata/dataframe.h"
+#include "gromacs/analysisdata/datamodule.h"
+#include "gromacs/analysisdata/datamodulemanager.h"
 #include "gromacs/utility/exceptions.h"
 #include "gromacs/utility/gmxassert.h"
+#include "gromacs/utility/uniqueptr.h"
 
-#include "abstractdata-impl.h"
-#include "dataframe.h"
 #include "dataproxy.h"
 
 namespace gmx
@@ -51,42 +61,28 @@ namespace gmx
  * AbstractAnalysisData::Impl
  */
 
-AbstractAnalysisData::Impl::Impl()
-    : _bAllowMissing(true), _bDataStart(false), _bInData(false), _bInFrame(false),
-      _currIndex(-1), _nframes(0)
-{
-}
-
-AbstractAnalysisData::Impl::~Impl()
+/*! \internal \brief
+ * Private implementation class for AbstractAnalysisData.
+ *
+ * \ingroup module_analysisdata
+ */
+class AbstractAnalysisData::Impl
 {
-}
+    public:
+        Impl();
 
+        //! Column counts for each data set in the data.
+        std::vector<int>           columnCounts_;
+        //! Whether the data is multipoint.
+        bool                       bMultipoint_;
+        //! Manager for the added modules.
+        AnalysisDataModuleManager  modules_;
+};
 
-void
-AbstractAnalysisData::Impl::presentData(AbstractAnalysisData *data,
-                                        AnalysisDataModuleInterface *module)
+AbstractAnalysisData::Impl::Impl()
+    : bMultipoint_(false)
 {
-    module->dataStarted(data);
-    bool bCheckMissing = _bAllowMissing
-        && !(module->flags() & AnalysisDataModuleInterface::efAllowMissing);
-    for (int i = 0; i < data->frameCount(); ++i)
-    {
-        AnalysisDataFrameRef frame = data->getDataFrame(i);
-        GMX_RELEASE_ASSERT(frame.isValid(), "Invalid data frame returned");
-        // TODO: Check all frames before doing anything for slightly better
-        // exception behavior.
-        if (bCheckMissing && !frame.allPresent())
-        {
-            GMX_THROW(APIError("Missing data not supported by a module"));
-        }
-        module->frameStarted(frame.header());
-        module->pointsAdded(frame.points());
-        module->frameFinished(frame.header());
-    }
-    if (!_bInData)
-    {
-        module->dataFinished();
-    }
+    columnCounts_.push_back(0);
 }
 
 
@@ -95,7 +91,7 @@ AbstractAnalysisData::Impl::presentData(AbstractAnalysisData *data,
  */
 /*! \cond libapi */
 AbstractAnalysisData::AbstractAnalysisData()
-    : _impl(new Impl()), _ncol(0), _bMultiPoint(false)
+    : impl_(new Impl())
 {
 }
 //! \endcond
@@ -104,11 +100,32 @@ AbstractAnalysisData::~AbstractAnalysisData()
 {
 }
 
+bool
+AbstractAnalysisData::isMultipoint() const
+{
+    return impl_->bMultipoint_;
+}
+
+int
+AbstractAnalysisData::dataSetCount() const
+{
+    return impl_->columnCounts_.size();
+}
+
+int
+AbstractAnalysisData::columnCount(int dataSet) const
+{
+    GMX_ASSERT(dataSet >= 0 && dataSet < dataSetCount(),
+               "Out of range data set index");
+    return impl_->columnCounts_[dataSet];
+}
 
 int
-AbstractAnalysisData::frameCount() const
+AbstractAnalysisData::columnCount() const
 {
-    return _impl->_nframes;
+    GMX_ASSERT(dataSetCount() == 1,
+               "Convenience method not available for multiple data sets");
+    return columnCount(0);
 }
 
 
@@ -150,24 +167,7 @@ AbstractAnalysisData::requestStorage(int nframes)
 void
 AbstractAnalysisData::addModule(AnalysisDataModulePointer module)
 {
-    if ((columnCount() > 1 && !(module->flags() & AnalysisDataModuleInterface::efAllowMulticolumn))
-        || (isMultipoint() && !(module->flags() & AnalysisDataModuleInterface::efAllowMultipoint))
-        || (!isMultipoint() && (module->flags() & AnalysisDataModuleInterface::efOnlyMultipoint)))
-    {
-        GMX_THROW(APIError("Data module not compatible with data object properties"));
-    }
-
-    if (_impl->_bDataStart)
-    {
-        GMX_RELEASE_ASSERT(!_impl->_bInFrame,
-                           "Cannot add data modules in mid-frame");
-        _impl->presentData(this, module.get());
-    }
-    if (!(module->flags() & AnalysisDataModuleInterface::efAllowMissing))
-    {
-        _impl->_bAllowMissing = false;
-    }
-    _impl->_modules.push_back(move(module));
+    impl_->modules_.addModule(this, module);
 }
 
 
@@ -175,13 +175,8 @@ void
 AbstractAnalysisData::addColumnModule(int col, int span,
                                       AnalysisDataModulePointer module)
 {
-    GMX_RELEASE_ASSERT(col >= 0 && span >= 1 && col + span <= _ncol,
+    GMX_RELEASE_ASSERT(col >= 0 && span >= 1,
                        "Invalid columns specified for a column module");
-    if (_impl->_bDataStart)
-    {
-        GMX_THROW(NotImplementedError("Cannot add column modules after data"));
-    }
-
     boost::shared_ptr<AnalysisDataProxy> proxy(
             new AnalysisDataProxy(col, span, this));
     proxy->addModule(module);
@@ -192,141 +187,55 @@ AbstractAnalysisData::addColumnModule(int col, int span,
 void
 AbstractAnalysisData::applyModule(AnalysisDataModuleInterface *module)
 {
-    if ((columnCount() > 1 && !(module->flags() & AnalysisDataModuleInterface::efAllowMulticolumn))
-        || (isMultipoint() && !(module->flags() & AnalysisDataModuleInterface::efAllowMultipoint))
-        || (!isMultipoint() && (module->flags() & AnalysisDataModuleInterface::efOnlyMultipoint)))
-    {
-        GMX_THROW(APIError("Data module not compatible with data object properties"));
-    }
-    GMX_RELEASE_ASSERT(_impl->_bDataStart && !_impl->_bInData,
-                       "Data module can only be applied to ready data");
-
-    _impl->presentData(this, module);
+    impl_->modules_.applyModule(this, module);
 }
 
 /*! \cond libapi */
 void
-AbstractAnalysisData::setColumnCount(int ncol)
+AbstractAnalysisData::setDataSetCount(int dataSetCount)
 {
-    GMX_RELEASE_ASSERT(ncol > 0, "Invalid data column count");
-    GMX_RELEASE_ASSERT(_ncol == 0 || _impl->_modules.empty(),
-                       "Data column count cannot be changed after modules are added");
-    GMX_RELEASE_ASSERT(!_impl->_bDataStart,
-                       "Data column count cannot be changed after data has been added");
-    _ncol = ncol;
+    GMX_RELEASE_ASSERT(dataSetCount > 0, "Invalid data column count");
+    impl_->modules_.dataPropertyAboutToChange(
+            AnalysisDataModuleManager::eMultipleDataSets, dataSetCount > 1);
+    impl_->columnCounts_.resize(dataSetCount);
 }
 
-
 void
-AbstractAnalysisData::setMultipoint(bool multipoint)
+AbstractAnalysisData::setColumnCount(int dataSet, int columnCount)
 {
-    GMX_RELEASE_ASSERT(_impl->_modules.empty(),
-                       "Data type cannot be changed after modules are added");
-    GMX_RELEASE_ASSERT(!_impl->_bDataStart,
-                       "Data type cannot be changed after data has been added");
-    _bMultiPoint = multipoint;
-}
+    GMX_RELEASE_ASSERT(dataSet >= 0 && dataSet < dataSetCount(),
+                       "Out of range data set index");
+    GMX_RELEASE_ASSERT(columnCount > 0, "Invalid data column count");
 
-
-/*! \internal
- * This method is not const because the dataStarted() methods of the attached
- * modules can request storage of the data.
- */
-void
-AbstractAnalysisData::notifyDataStart()
-{
-    GMX_RELEASE_ASSERT(!_impl->_bDataStart,
-                       "notifyDataStart() called more than once");
-    GMX_RELEASE_ASSERT(_ncol > 0, "Data column count is not set");
-    _impl->_bDataStart = _impl->_bInData = true;
-
-    Impl::ModuleList::const_iterator i;
-    for (i = _impl->_modules.begin(); i != _impl->_modules.end(); ++i)
+    bool bMultipleColumns = columnCount > 1;
+    for (int i = 0; i < dataSetCount() && !bMultipleColumns; ++i)
     {
-        if (_ncol > 1 && !((*i)->flags() & AnalysisDataModuleInterface::efAllowMulticolumn))
+        if (i != dataSet && this->columnCount(i) > 1)
         {
-            GMX_THROW(APIError("Data module not compatible with data object properties"));
+            bMultipleColumns = true;
         }
-        (*i)->dataStarted(this);
     }
+    impl_->modules_.dataPropertyAboutToChange(
+            AnalysisDataModuleManager::eMultipleColumns, bMultipleColumns);
+    impl_->columnCounts_[dataSet] = columnCount;
 }
 
-
 void
-AbstractAnalysisData::notifyFrameStart(const AnalysisDataFrameHeader &header) const
+AbstractAnalysisData::setMultipoint(bool bMultipoint)
 {
-    GMX_ASSERT(_impl->_bInData, "notifyDataStart() not called");
-    GMX_ASSERT(!_impl->_bInFrame,
-               "notifyFrameStart() called while inside a frame");
-    GMX_ASSERT(header.index() == _impl->_nframes,
-               "Out of order frames");
-    _impl->_bInFrame = true;
-    _impl->_currIndex = header.index();
-
-    Impl::ModuleList::const_iterator i;
-    for (i = _impl->_modules.begin(); i != _impl->_modules.end(); ++i)
-    {
-        (*i)->frameStarted(header);
-    }
+    impl_->modules_.dataPropertyAboutToChange(
+            AnalysisDataModuleManager::eMultipoint, bMultipoint);
+    impl_->bMultipoint_ = bMultipoint;
 }
 
-
-void
-AbstractAnalysisData::notifyPointsAdd(const AnalysisDataPointSetRef &points) const
-{
-    GMX_ASSERT(_impl->_bInData, "notifyDataStart() not called");
-    GMX_ASSERT(_impl->_bInFrame, "notifyFrameStart() not called");
-    GMX_ASSERT(points.lastColumn() < columnCount(), "Invalid columns");
-    GMX_ASSERT(points.frameIndex() == _impl->_currIndex,
-               "Points do not correspond to current frame");
-    if (!_impl->_bAllowMissing && !points.allPresent())
-    {
-        GMX_THROW(APIError("Missing data not supported by a module"));
-    }
-
-    Impl::ModuleList::const_iterator i;
-    for (i = _impl->_modules.begin(); i != _impl->_modules.end(); ++i)
-    {
-        (*i)->pointsAdded(points);
-    }
-}
-
-
-void
-AbstractAnalysisData::notifyFrameFinish(const AnalysisDataFrameHeader &header)
+AnalysisDataModuleManager &AbstractAnalysisData::moduleManager()
 {
-    GMX_ASSERT(_impl->_bInData, "notifyDataStart() not called");
-    GMX_ASSERT(_impl->_bInFrame, "notifyFrameStart() not called");
-    GMX_ASSERT(header.index() == _impl->_currIndex,
-               "Header does not correspond to current frame");
-    _impl->_bInFrame = false;
-    _impl->_currIndex = -1;
-
-    // Increment the counter before notifications to allow frame access from
-    // modules.
-    ++_impl->_nframes;
-
-    Impl::ModuleList::const_iterator i;
-    for (i = _impl->_modules.begin(); i != _impl->_modules.end(); ++i)
-    {
-        (*i)->frameFinished(header);
-    }
+    return impl_->modules_;
 }
 
-
-void
-AbstractAnalysisData::notifyDataFinish() const
+const AnalysisDataModuleManager &AbstractAnalysisData::moduleManager() const
 {
-    GMX_RELEASE_ASSERT(_impl->_bInData, "notifyDataStart() not called");
-    GMX_RELEASE_ASSERT(!_impl->_bInFrame,
-                       "notifyDataFinish() called while inside a frame");
-    _impl->_bInData = false;
-
-    Impl::ModuleList::const_iterator i;
-    for (i = _impl->_modules.begin(); i != _impl->_modules.end(); ++i)
-    {
-        (*i)->dataFinished();
-    }
+    return impl_->modules_;
 }
 //! \endcond