Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / analysisdata / modules / plot.cpp
index 17290ccac1c64eaf9e946d3a6a5ec96c601a8af8..e421e2d64a85aea39d64679b758993531f6265a9 100644 (file)
@@ -1,62 +1,68 @@
 /*
+ * 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 plot.h.
  *
  * \ingroup module_analysisdata
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
  */
-#include "gromacs/analysisdata/modules/plot.h"
+#include "gmxpre.h"
 
-#include <string>
-#include <vector>
+#include "plot.h"
 
 #include <cstdio>
 #include <cstring>
 
+#include <string>
+#include <vector>
+
 #include <boost/shared_ptr.hpp>
 
-#include "gromacs/legacyheaders/gmxfio.h"
+#include "gromacs/analysisdata/dataframe.h"
+#include "gromacs/fileio/gmxfio.h"
+#include "gromacs/fileio/xvgr.h"
 #include "gromacs/legacyheaders/oenv.h"
-#include "gromacs/legacyheaders/vec.h"
-#include "gromacs/legacyheaders/xvgr.h"
-
+#include "gromacs/math/vec.h"
 #include "gromacs/options/basicoptions.h"
-#include "gromacs/analysisdata/dataframe.h"
 #include "gromacs/options/options.h"
 #include "gromacs/options/timeunitmanager.h"
 #include "gromacs/selection/selectioncollection.h"
 #include "gromacs/utility/exceptions.h"
 #include "gromacs/utility/gmxassert.h"
+#include "gromacs/utility/programcontext.h"
 #include "gromacs/utility/stringutil.h"
 
 namespace
@@ -64,7 +70,7 @@ namespace
 
 //! Enum values for plot formats.
 const char *const g_plotFormats[] = {
-    "none", "xmgrace", "xmgr", NULL
+    "none", "xmgrace", "xmgr"
 };
 
 } // namespace
@@ -110,25 +116,26 @@ class AbstractPlotModule::Impl
 
         void closeFile();
 
-        AnalysisDataPlotSettings settings_;
-        std::string             filename_;
-        FILE                   *fp_;
-
-        bool                    bPlain_;
-        bool                    gOmitX_;
-        std::string             title_;
-        std::string             subtitle_;
-        std::string             xlabel_;
-        std::string             ylabel_;
+        AnalysisDataPlotSettings  settings_;
+        std::string               filename_;
+        FILE                     *fp_;
+
+        bool                      bPlain_;
+        bool                      bOmitX_;
+        bool                      bErrorsAsSeparateColumn_;
+        std::string               title_;
+        std::string               subtitle_;
+        std::string               xlabel_;
+        std::string               ylabel_;
         std::vector<std::string>  legend_;
-        char                    xformat_[15];
-        char                    yformat_[15];
-        real                    xscale_;
+        char                      xformat_[15];
+        char                      yformat_[15];
+        real                      xscale_;
 };
 
 AbstractPlotModule::Impl::Impl(const AnalysisDataPlotSettings &settings)
-    : settings_(settings), fp_(NULL), bPlain_(false), gOmitX_(false),
-      xscale_(1.0)
+    : settings_(settings), fp_(NULL), bPlain_(false), bOmitX_(false),
+      bErrorsAsSeparateColumn_(false), xscale_(1.0)
 {
     strcpy(xformat_, "%11.3f");
     strcpy(yformat_, " %8.3f");
@@ -199,10 +206,17 @@ AbstractPlotModule::setPlainOutput(bool bPlain)
 }
 
 
+void
+AbstractPlotModule::setErrorsAsSeparateColumn(bool bSeparate)
+{
+    impl_->bErrorsAsSeparateColumn_ = bSeparate;
+}
+
+
 void
 AbstractPlotModule::setOmitX(bool bOmitX)
 {
-    impl_->gOmitX_ = bOmitX;
+    impl_->bOmitX_ = bOmitX;
 }
 
 
@@ -261,6 +275,13 @@ AbstractPlotModule::appendLegend(const char *setname)
 }
 
 
+void
+AbstractPlotModule::appendLegend(const std::string &setname)
+{
+    impl_->legend_.push_back(setname);
+}
+
+
 void
 AbstractPlotModule::setXFormat(int width, int precision, char format)
 {
@@ -288,12 +309,13 @@ AbstractPlotModule::setYFormat(int width, int precision, char format)
 int
 AbstractPlotModule::flags() const
 {
-    return efAllowMulticolumn | efAllowMultipoint;
+    return efAllowMissing | efAllowMulticolumn | efAllowMultipoint
+           | efAllowMultipleDataSets;
 }
 
 
 void
-AbstractPlotModule::dataStarted(AbstractAnalysisData *data)
+AbstractPlotModule::dataStarted(AbstractAnalysisData * /* data */)
 {
     if (!impl_->filename_.empty())
     {
@@ -303,14 +325,14 @@ AbstractPlotModule::dataStarted(AbstractAnalysisData *data)
         }
         else
         {
-            time_unit_t time_unit
+            time_unit_t  time_unit
                 = static_cast<time_unit_t>(impl_->settings_.timeUnit() + 1);
             xvg_format_t xvg_format
                 = (impl_->settings_.plotFormat() > 0
-                    ? static_cast<xvg_format_t>(impl_->settings_.plotFormat())
-                    : exvgNONE);
-            output_env_t oenv;
-            output_env_init(&oenv, 0, NULL, time_unit, FALSE, xvg_format, 0, 0);
+                   ? static_cast<xvg_format_t>(impl_->settings_.plotFormat())
+                   : exvgNONE);
+            output_env_t                  oenv;
+            output_env_init(&oenv, getProgramContext(), time_unit, FALSE, xvg_format, 0);
             boost::shared_ptr<output_env> oenvGuard(oenv, &output_env_done);
             impl_->fp_ = xvgropen(impl_->filename_.c_str(), impl_->title_.c_str(),
                                   impl_->xlabel_.c_str(), impl_->ylabel_.c_str(),
@@ -348,7 +370,7 @@ AbstractPlotModule::frameStarted(const AnalysisDataFrameHeader &frame)
     {
         return;
     }
-    if (!impl_->gOmitX_)
+    if (!impl_->bOmitX_)
     {
         std::fprintf(impl_->fp_, impl_->xformat_, frame.x() * impl_->xscale_);
     }
@@ -381,10 +403,16 @@ AbstractPlotModule::isFileOpen() const
 
 
 void
-AbstractPlotModule::writeValue(real value) const
+AbstractPlotModule::writeValue(const AnalysisDataValue &value) const
 {
     GMX_ASSERT(isFileOpen(), "File not opened, but write attempted");
-    std::fprintf(impl_->fp_, impl_->yformat_, value);
+    const real y = value.isSet() ? value.value() : 0.0;
+    std::fprintf(impl_->fp_, impl_->yformat_, y);
+    if (impl_->bErrorsAsSeparateColumn_)
+    {
+        const real dy = value.isSet() ? value.error() : 0.0;
+        std::fprintf(impl_->fp_, impl_->yformat_, dy);
+    }
 }
 //! \endcond
 
@@ -412,7 +440,7 @@ AnalysisDataPlotModule::pointsAdded(const AnalysisDataPointSetRef &points)
     }
     for (int i = 0; i < points.columnCount(); ++i)
     {
-        writeValue(points.y(i));
+        writeValue(points.values()[i]);
     }
 }
 
@@ -484,7 +512,7 @@ AnalysisDataVectorPlotModule::setWriteMask(bool bWrite[DIM + 1])
 void
 AnalysisDataVectorPlotModule::pointsAdded(const AnalysisDataPointSetRef &points)
 {
-    if (points.firstColumn() % DIM != 0)
+    if (points.firstColumn() % DIM != 0 || points.columnCount() % DIM != 0)
     {
         GMX_THROW(APIError("Partial data points"));
     }
@@ -498,13 +526,14 @@ AnalysisDataVectorPlotModule::pointsAdded(const AnalysisDataPointSetRef &points)
         {
             if (bWrite_[i])
             {
-                writeValue(points.y(i + d));
+                writeValue(points.values()[i + d]);
             }
         }
         if (bWrite_[DIM])
         {
-            rvec y = { points.y(i), points.y(i + 1), points.y(i + 2) };
-            writeValue(norm(y));
+            const rvec        y = { points.y(i), points.y(i + 1), points.y(i + 2) };
+            AnalysisDataValue value(norm(y));
+            writeValue(value);
         }
     }
 }