Fix copy-paste bug in gmx distance
[alexxy/gromacs.git] / src / gromacs / trajectoryanalysis / modules / distance.cpp
index de2c2fae1304521f62b3aea089c38691158c327f..524e735354f0bb6b769b02e744eb8edb98da4abd 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,2015, 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
  */
 #include "distance.h"
 
+#include <string>
+
 #include "gromacs/legacyheaders/pbc.h"
 #include "gromacs/legacyheaders/vec.h"
 
 #include "gromacs/analysisdata/analysisdata.h"
+#include "gromacs/analysisdata/modules/average.h"
+#include "gromacs/analysisdata/modules/histogram.h"
 #include "gromacs/analysisdata/modules/plot.h"
 #include "gromacs/options/basicoptions.h"
 #include "gromacs/options/filenameoption.h"
@@ -52,6 +56,7 @@
 #include "gromacs/selection/selection.h"
 #include "gromacs/selection/selectionoption.h"
 #include "gromacs/trajectoryanalysis/analysissettings.h"
+#include "gromacs/utility/arrayref.h"
 #include "gromacs/utility/exceptions.h"
 #include "gromacs/utility/stringutil.h"
 
@@ -61,12 +66,48 @@ namespace gmx
 namespace analysismodules
 {
 
-const char Distance::name[]             = "distance";
-const char Distance::shortDescription[] =
-    "Calculate distances between pairs of positions";
+namespace
+{
+
+class Distance : public TrajectoryAnalysisModule
+{
+    public:
+        Distance();
+
+        virtual void initOptions(Options                    *options,
+                                 TrajectoryAnalysisSettings *settings);
+        virtual void initAnalysis(const TrajectoryAnalysisSettings &settings,
+                                  const TopologyInformation        &top);
+
+        virtual void analyzeFrame(int frnr, const t_trxframe &fr, t_pbc *pbc,
+                                  TrajectoryAnalysisModuleData *pdata);
+
+        virtual void finishAnalysis(int nframes);
+        virtual void writeOutput();
+
+    private:
+        SelectionList                            sel_;
+        std::string                              fnAverage_;
+        std::string                              fnAll_;
+        std::string                              fnXYZ_;
+        std::string                              fnHistogram_;
+        std::string                              fnAllStats_;
+        double                                   meanLength_;
+        double                                   lengthDev_;
+        double                                   binWidth_;
+
+        AnalysisData                             distances_;
+        AnalysisData                             xyz_;
+        AnalysisDataAverageModulePointer         summaryStatsModule_;
+        AnalysisDataAverageModulePointer         allStatsModule_;
+        AnalysisDataFrameAverageModulePointer    averageModule_;
+        AnalysisDataSimpleHistogramModulePointer histogramModule_;
+
+        // Copy and assign disallowed by base.
+};
 
 Distance::Distance()
-    : TrajectoryAnalysisModule(name, shortDescription),
+    : TrajectoryAnalysisModule(DistanceInfo::name, DistanceInfo::shortDescription),
       meanLength_(0.1), lengthDev_(1.0), binWidth_(0.001)
 {
     summaryStatsModule_.reset(new AnalysisDataAverageModule());
@@ -88,16 +129,11 @@ Distance::Distance()
 }
 
 
-Distance::~Distance()
-{
-}
-
-
 void
 Distance::initOptions(Options *options, TrajectoryAnalysisSettings * /*settings*/)
 {
     static const char *const desc[] = {
-        "[TT]gmx distance[tt] calculates distances between pairs of positions",
+        "[THISMODULE] calculates distances between pairs of positions",
         "as a function of time. Each selection specifies an independent set",
         "of distances to calculate. Each selection should consist of pairs",
         "of positions, and the distances are computed between positions 1-2,",
@@ -114,7 +150,7 @@ Distance::initOptions(Options *options, TrajectoryAnalysisSettings * /*settings*
         "each individual distance, calculated over the frames."
     };
 
-    options->setDescription(concatenateStrings(desc));
+    options->setDescription(desc);
 
     options->addOption(FileNameOption("oav").filetype(eftPlot).outputFile()
                            .store(&fnAverage_).defaultBasename("distave")
@@ -146,9 +182,6 @@ Distance::initOptions(Options *options, TrajectoryAnalysisSettings * /*settings*
 }
 
 
-namespace
-{
-
 /*! \brief
  * Checks that selections conform to the expectations of the tool.
  */
@@ -181,8 +214,6 @@ void checkSelections(const SelectionList &sel)
     }
 }
 
-}       // namespace
-
 
 void
 Distance::initAnalysis(const TrajectoryAnalysisSettings &settings,
@@ -211,7 +242,10 @@ Distance::initAnalysis(const TrajectoryAnalysisSettings &settings,
         plotm->setTitle("Average distance");
         plotm->setXAxisIsTime();
         plotm->setYLabel("Distance (nm)");
-        // TODO: Add legends
+        for (size_t g = 0; g < sel_.size(); ++g)
+        {
+            plotm->appendLegend(sel_[g].name());
+        }
         averageModule_->addModule(plotm);
     }
 
@@ -231,7 +265,7 @@ Distance::initAnalysis(const TrajectoryAnalysisSettings &settings,
     {
         AnalysisDataPlotModulePointer plotm(
                 new AnalysisDataPlotModule(settings.plotSettings()));
-        plotm->setFileName(fnAll_);
+        plotm->setFileName(fnXYZ_);
         plotm->setTitle("Distance");
         plotm->setXAxisIsTime();
         plotm->setYLabel("Distance (nm)");
@@ -247,7 +281,10 @@ Distance::initAnalysis(const TrajectoryAnalysisSettings &settings,
         plotm->setTitle("Distance histogram");
         plotm->setXLabel("Distance (nm)");
         plotm->setYLabel("Probability");
-        // TODO: Add legends
+        for (size_t g = 0; g < sel_.size(); ++g)
+        {
+            plotm->appendLegend(sel_[g].name());
+        }
         histogramModule_->averager().addModule(plotm);
     }
 
@@ -260,7 +297,11 @@ Distance::initAnalysis(const TrajectoryAnalysisSettings &settings,
         plotm->setTitle("Statistics for individual distances");
         plotm->setXLabel("Distance index");
         plotm->setYLabel("Average/standard deviation (nm)");
-        // TODO: Add legends
+        for (size_t g = 0; g < sel_.size(); ++g)
+        {
+            plotm->appendLegend(std::string(sel_[g].name()) + " avg");
+            plotm->appendLegend(std::string(sel_[g].name()) + " std.dev.");
+        }
         // TODO: Consider whether this output format is the best possible.
         allStatsModule_->addModule(plotm);
     }
@@ -333,6 +374,17 @@ Distance::writeOutput()
     }
 }
 
+}       // namespace
+
+const char DistanceInfo::name[]             = "distance";
+const char DistanceInfo::shortDescription[] =
+    "Calculate distances between pairs of positions";
+
+TrajectoryAnalysisModulePointer DistanceInfo::create()
+{
+    return TrajectoryAnalysisModulePointer(new Distance);
+}
+
 } // namespace analysismodules
 
 } // namespace gmx