Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / analysisdata / arraydata.cpp
index ca64aa8c21904f5b5f620edf2a2ece2f0a6f9bed..60d4c89cfd906f28eed43237355de96d97c0ca90 100644 (file)
@@ -1,54 +1,63 @@
 /*
+ * 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 classes in arraydata.h.
  *
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
  * \ingroup module_analysisdata
  */
-#include "gromacs/analysisdata/arraydata.h"
+#include "gmxpre.h"
+
+#include "arraydata.h"
 
 #include <algorithm>
 
 #include "gromacs/analysisdata/dataframe.h"
-#include "gromacs/fatalerror/exceptions.h"
-#include "gromacs/fatalerror/gmxassert.h"
+#include "gromacs/analysisdata/datamodulemanager.h"
+#include "gromacs/utility/exceptions.h"
+#include "gromacs/utility/gmxassert.h"
 
 namespace gmx
 {
 
 AbstractAnalysisArrayData::AbstractAnalysisArrayData()
-    : _nrows(0), _xstart(0.0), _xstep(1.0), _bReady(false)
+    : rowCount_(0), pointSetInfo_(0, 0, 0, 0), xstep_(1.0),
+      bUniformX_(true), bReady_(false)
 {
+    xvalue_.push_back(0);
 }
 
 AbstractAnalysisArrayData::~AbstractAnalysisArrayData()
@@ -56,51 +65,24 @@ AbstractAnalysisArrayData::~AbstractAnalysisArrayData()
 }
 
 
-bool
-AbstractAnalysisArrayData::getDataWErr(int index, real *x, real *dx,
-                                       const real **y, const real **dy,
-                                       const bool **present) const
+AnalysisDataFrameRef
+AbstractAnalysisArrayData::tryGetDataFrameInternal(int index) const
 {
-    if (index < 0)
-    {
-        index += frameCount() - 1;
-        if (index < 0)
-        {
-            return false;
-        }
-    }
-    if (index >= frameCount())
-    {
-        return false;
-    }
-    if (x != NULL)
-    {
-        *x = xvalue(index);
-    }
-    if (dx != NULL)
-    {
-        *dx = 0.0;
-    }
-    if (y != NULL)
-    {
-        *y = &_value[index * columnCount()];
-    }
-    if (dy != NULL)
-    {
-        // TODO: Implement
-        *dy = NULL;
-    }
-    if (present != NULL)
+    if (!isAllocated())
     {
-        // TODO: Implement
-        *present = NULL;
+        return AnalysisDataFrameRef();
     }
-    return true;
+    std::vector<AnalysisDataValue>::const_iterator begin
+        = value_.begin() + index * columnCount();
+    return AnalysisDataFrameRef(
+            AnalysisDataFrameHeader(index, xvalue(index), 0.0),
+            constArrayRefFromVector<AnalysisDataValue>(begin, begin + columnCount()),
+            constArrayRefFromArray(&pointSetInfo_, 1));
 }
 
 
 bool
-AbstractAnalysisArrayData::requestStorage(int /*nframes*/)
+AbstractAnalysisArrayData::requestStorageInternal(int /*nframes*/)
 {
     return true;
 }
@@ -111,17 +93,29 @@ AbstractAnalysisArrayData::setColumnCount(int ncols)
 {
     GMX_RELEASE_ASSERT(!isAllocated(),
                        "Cannot change column count after data has been allocated");
-    AbstractAnalysisData::setColumnCount(ncols);
+    AbstractAnalysisData::setColumnCount(0, ncols);
+    pointSetInfo_ = AnalysisDataPointSetInfo(0, ncols, 0, 0);
 }
 
 
 void
-AbstractAnalysisArrayData::setRowCount(int nrows)
+AbstractAnalysisArrayData::setRowCount(int rowCount)
 {
-    GMX_RELEASE_ASSERT(nrows > 0, "Invalid number of rows");
+    GMX_RELEASE_ASSERT(rowCount > 0, "Invalid number of rows");
     GMX_RELEASE_ASSERT(!isAllocated(),
                        "Cannot change row count after data has been allocated");
-    _nrows = nrows;
+    GMX_RELEASE_ASSERT(bUniformX_ || rowCount_ == 0
+                       || rowCount == static_cast<int>(xvalue_.size()),
+                       "X axis set with setXAxisValue() does not match the row count");
+    xvalue_.resize(rowCount);
+    if (bUniformX_ && rowCount > rowCount_)
+    {
+        for (int i = rowCount_; i < rowCount; ++i)
+        {
+            xvalue_[i] = xvalue_[0] + i * xstep_;
+        }
+    }
+    rowCount_ = rowCount;
 }
 
 
@@ -131,16 +125,44 @@ AbstractAnalysisArrayData::allocateValues()
     GMX_RELEASE_ASSERT(!isAllocated(), "Can only allocate values once");
     GMX_RELEASE_ASSERT(rowCount() > 0 && columnCount() > 0,
                        "Row and column counts must be set before allocating values");
-    _value.resize(rowCount() * columnCount());
+    value_.resize(rowCount() * columnCount());
+    std::vector<AnalysisDataValue>::iterator i;
+    for (i = value_.begin(); i != value_.end(); ++i)
+    {
+        i->setValue(0.0);
+    }
 }
 
 
 void
 AbstractAnalysisArrayData::setXAxis(real start, real step)
 {
-    GMX_RELEASE_ASSERT(!_bReady, "X axis cannot be set after data is finished");
-    _xstart = start;
-    _xstep = step;
+    GMX_RELEASE_ASSERT(!bReady_, "X axis cannot be set after data is finished");
+    xvalue_[0] = start;
+    xstep_     = step;
+    bUniformX_ = true;
+    for (int i = 0; i < rowCount_; ++i)
+    {
+        xvalue_[i] = start + i * xstep_;
+    }
+}
+
+
+void
+AbstractAnalysisArrayData::setXAxisValue(int row, real value)
+{
+    GMX_RELEASE_ASSERT(!bReady_, "X axis cannot be set after data is finished");
+    if (rowCount_ > 0)
+    {
+        GMX_RELEASE_ASSERT(row >= 0 && row < rowCount(), "Row index out of range");
+    }
+    else if (row >= static_cast<int>(xvalue_.size()))
+    {
+        xvalue_.resize(row + 1);
+    }
+    bUniformX_   = false;
+    xstep_       = 0.0;
+    xvalue_[row] = value;
 }
 
 
@@ -148,29 +170,33 @@ void
 AbstractAnalysisArrayData::valuesReady()
 {
     GMX_RELEASE_ASSERT(isAllocated(), "There must be some data");
-    if (_bReady)
+    if (bReady_)
     {
         return;
     }
-    _bReady = true;
+    bReady_ = true;
 
-    notifyDataStart();
-    for (int i = 0; i < rowCount(); ++i)
+    std::vector<AnalysisDataValue>::const_iterator valueIter = value_.begin();
+    AnalysisDataModuleManager                     &modules   = moduleManager();
+    modules.notifyDataStart(this);
+    for (int i = 0; i < rowCount(); ++i, valueIter += columnCount())
     {
         AnalysisDataFrameHeader header(i, xvalue(i), 0);
-        notifyFrameStart(header);
-        notifyPointsAdd(AnalysisDataPointSetRef(header, 0, columnCount(),
-                                                &_value[i * columnCount()],
-                                                NULL, NULL));
-        notifyFrameFinish(header);
+        modules.notifyFrameStart(header);
+        modules.notifyPointsAdd(
+                AnalysisDataPointSetRef(
+                        header, pointSetInfo_,
+                        constArrayRefFromVector<AnalysisDataValue>(valueIter,
+                                                                   valueIter + columnCount())));
+        modules.notifyFrameFinish(header);
     }
-    notifyDataFinish();
+    modules.notifyDataFinish();
 }
 
 
 void
 AbstractAnalysisArrayData::copyContents(const AbstractAnalysisArrayData *src,
-                                        AbstractAnalysisArrayData *dest)
+                                        AbstractAnalysisArrayData       *dest)
 {
     GMX_RELEASE_ASSERT(src->isAllocated(), "Source data must not be empty");
     GMX_RELEASE_ASSERT(!dest->isAllocated(),
@@ -178,8 +204,10 @@ AbstractAnalysisArrayData::copyContents(const AbstractAnalysisArrayData *src,
     dest->setColumnCount(src->columnCount());
     dest->setRowCount(src->rowCount());
     dest->allocateValues();
-    dest->setXAxis(src->xstart(), src->xstep());
-    std::copy(src->_value.begin(), src->_value.end(), dest->_value.begin());
+    dest->xstep_     = src->xstep_;
+    dest->bUniformX_ = src->bUniformX_;
+    std::copy(src->xvalue_.begin(), src->xvalue_.end(), dest->xvalue_.begin());
+    std::copy(src->value_.begin(), src->value_.end(), dest->value_.begin());
 }
 
 } // namespace gmx