Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / analysisdata / modules / plot.cpp
index b3db5ea4faa2964fa26dc32808e93d159030e11e..e421e2d64a85aea39d64679b758993531f6265a9 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
- * David van der Spoel, Berk Hess, Erik Lindahl, and including many
- * others, as listed in the AUTHORS file in the top-level source
- * directory and at http://www.gromacs.org.
+ * 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.
  *
  * GROMACS is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
  * \ingroup module_analysisdata
  * \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
@@ -119,7 +121,8 @@ class AbstractPlotModule::Impl
         FILE                     *fp_;
 
         bool                      bPlain_;
-        bool                      gOmitX_;
+        bool                      bOmitX_;
+        bool                      bErrorsAsSeparateColumn_;
         std::string               title_;
         std::string               subtitle_;
         std::string               xlabel_;
@@ -131,8 +134,8 @@ class AbstractPlotModule::Impl
 };
 
 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");
@@ -203,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;
 }
 
 
@@ -265,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)
 {
@@ -292,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())
     {
@@ -314,7 +332,7 @@ AbstractPlotModule::dataStarted(AbstractAnalysisData *data)
                    ? 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);
+            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(),
@@ -352,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_);
     }
@@ -385,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
 
@@ -416,7 +440,7 @@ AnalysisDataPlotModule::pointsAdded(const AnalysisDataPointSetRef &points)
     }
     for (int i = 0; i < points.columnCount(); ++i)
     {
-        writeValue(points.y(i));
+        writeValue(points.values()[i]);
     }
 }
 
@@ -488,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"));
     }
@@ -502,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);
         }
     }
 }