Simplify code structure for C++ analysis tools
authorTeemu Murtola <teemu.murtola@gmail.com>
Tue, 22 Oct 2013 04:34:36 +0000 (07:34 +0300)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Mon, 28 Oct 2013 16:38:58 +0000 (17:38 +0100)
Move all the code for declaring the actual analysis tool into the source
file, and leave only a stub info object in the headers.  Make the class
declared in the header provide a static factory method to allow this.

This has several benefits:
 - Easier to write and maintain the tools, as all the logic is in one
   file only.
 - Clearer about the intended level of encapsulation of the tools.
 - Makes the tools in the library more closely resemble those written
   using the template.  The only difference now is that the main()
   method is replaced by the info object, and that the info object
   provides the names to pass to TrajectoryAnalysisModule constructor.

Change-Id: Ib545cc1d8900fe88cb58811df2cd4a63a6a99b2f

15 files changed:
src/gromacs/trajectoryanalysis/cmdlinerunner.h
src/gromacs/trajectoryanalysis/modules.cpp
src/gromacs/trajectoryanalysis/modules/angle.cpp
src/gromacs/trajectoryanalysis/modules/angle.h
src/gromacs/trajectoryanalysis/modules/distance.cpp
src/gromacs/trajectoryanalysis/modules/distance.h
src/gromacs/trajectoryanalysis/modules/freevolume.cpp
src/gromacs/trajectoryanalysis/modules/freevolume.h
src/gromacs/trajectoryanalysis/modules/select.cpp
src/gromacs/trajectoryanalysis/modules/select.h
src/gromacs/trajectoryanalysis/tests/angle.cpp
src/gromacs/trajectoryanalysis/tests/distance.cpp
src/gromacs/trajectoryanalysis/tests/freevolume.cpp
src/gromacs/trajectoryanalysis/tests/moduletest.h
src/gromacs/trajectoryanalysis/tests/select.cpp

index fefb93ef9b3c63a3da8355269f2e3fb35e7b4534..2a25d0ce1be48b7c4b3b6e5d1cbc91a4573a51ba 100644 (file)
 #ifndef GMX_TRAJECTORYANALYSIS_CMDLINERUNNER_H
 #define GMX_TRAJECTORYANALYSIS_CMDLINERUNNER_H
 
+#include "analysismodule.h"
 #include "../utility/common.h"
-#include "../utility/uniqueptr.h"
 
 namespace gmx
 {
 
 class CommandLineModuleManager;
 class CommandLineHelpContext;
-class TrajectoryAnalysisModule;
 
 /*! \brief
  * Runner class for command-line analysis tools.
@@ -68,6 +67,16 @@ class TrajectoryAnalysisModule;
 class TrajectoryAnalysisCommandLineRunner
 {
     public:
+        /*! \brief
+         * Factory method type for creating a trajectory analysis module.
+         *
+         * This method allows the module creation to be postponed to be inside
+         * the try/catch block in runAsMain()/registerModule() implementation
+         * methods and still keep the implementation out of the header, making
+         * the ABI more stable.
+         */
+        typedef TrajectoryAnalysisModulePointer (*ModuleFactoryMethod)();
+
         /*! \brief
          * Implements a main() method that runs a given module.
          *
@@ -109,6 +118,24 @@ class TrajectoryAnalysisCommandLineRunner
         {
             registerModule(manager, name, description, &createModule<ModuleType>);
         }
+        /*! \brief
+         * Registers a command-line module that runs a given module.
+         *
+         * \tparam ModuleType  Trajectory analysis module.
+         * \param  manager     Manager to register the module to.
+         * \param  name        Name of the module to register.
+         * \param  description One-line description for the module to register.
+         * \param  factory     Function that creates the module on demand.
+         *
+         * \p name and \p descriptions must be string constants or otherwise
+         * stay valid for the duration of the program execution.
+         *
+         * Implements the template registerModule() method, but can also be
+         * used independently.
+         */
+        static void registerModule(CommandLineModuleManager *manager,
+                                   const char *name, const char *description,
+                                   ModuleFactoryMethod factory);
 
         /*! \brief
          * Create a new runner with the provided module.
@@ -149,19 +176,6 @@ class TrajectoryAnalysisCommandLineRunner
         void writeHelp(const CommandLineHelpContext &context);
 
     private:
-        //! Smart pointer type for managing a trajectory analysis module.
-        typedef gmx_unique_ptr<TrajectoryAnalysisModule>::type
-            TrajectoryAnalysisModulePointer;
-
-        /*! \brief
-         * Factory method type for creating a trajectory analysis module.
-         *
-         * This method allows the module creation to be postponed to be inside
-         * the try/catch block in runAsMain()/registerModule() implementation
-         * methods and still keep the implementation out of the header, making
-         * the ABI more stable.
-         */
-        typedef TrajectoryAnalysisModulePointer (*ModuleFactoryMethod)();
         /*! \brief
          * Creates a trajectory analysis module of a given type.
          *
@@ -176,10 +190,6 @@ class TrajectoryAnalysisCommandLineRunner
         //! Implements the template runAsMain() method.
         static int runAsMain(int argc, char *argv[],
                              ModuleFactoryMethod factory);
-        //! Implements the template registerModule() method.
-        static void registerModule(CommandLineModuleManager *manager,
-                                   const char *name, const char *description,
-                                   ModuleFactoryMethod factory);
 
         class Impl;
 
index 1d7c6ef4a5cf1f3df7e0be9f5c9a45ffd8aabff3..c45421b4d4aa3d2402e9177b68fd5a7067ba2289 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2010,2011,2012, by the GROMACS development team, led by
+ * 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.
@@ -58,19 +58,20 @@ namespace
  * Convenience method for registering a command-line module for trajectory
  * analysis.
  *
- * \tparam ModuleType  Trajectory analysis module to wrap.
+ * \tparam ModuleInfo  Info about trajectory analysis module to wrap.
  *
- * \p ModuleType should be default-constructible, derive from
- * TrajectoryAnalysisModule, and have static public members
- * \c "const char name[]" and \c "const char shortDescription[]".
+ * \p ModuleInfo should have static public members
+ * `const char name[]`, `const char shortDescription[]`, and
+ * `gmx::TrajectoryAnalysisModulePointer create()`.
  *
  * \ingroup module_trajectoryanalysis
  */
-template <class ModuleType>
+template <class ModuleInfo>
 void registerModule(CommandLineModuleManager *manager)
 {
-    TrajectoryAnalysisCommandLineRunner::registerModule<ModuleType>(
-            manager, ModuleType::name, ModuleType::shortDescription);
+    TrajectoryAnalysisCommandLineRunner::registerModule(
+            manager, ModuleInfo::name, ModuleInfo::shortDescription,
+            &ModuleInfo::create);
 }
 
 }   // namespace
@@ -78,10 +79,10 @@ void registerModule(CommandLineModuleManager *manager)
 void registerTrajectoryAnalysisModules(CommandLineModuleManager *manager)
 {
     using namespace gmx::analysismodules;
-    registerModule<Angle>(manager);
-    registerModule<Distance>(manager);
-    registerModule<FreeVolume>(manager);
-    registerModule<Select>(manager);
+    registerModule<AngleInfo>(manager);
+    registerModule<DistanceInfo>(manager);
+    registerModule<FreeVolumeInfo>(manager);
+    registerModule<SelectInfo>(manager);
 }
 
 } // namespace gmx
index ff53dfa5ac1c4a331334703dbc2d18d37749b962..44a68ccb825c60839baade73efc4a1632e19d50c 100644 (file)
  */
 #include "angle.h"
 
+#include <string>
+#include <vector>
+
 #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"
@@ -62,12 +67,57 @@ namespace gmx
 namespace analysismodules
 {
 
-const char Angle::name[]             = "gangle";
-const char Angle::shortDescription[] =
-    "Calculate angles";
+namespace
+{
+
+class Angle : public TrajectoryAnalysisModule
+{
+    public:
+        Angle();
+        virtual ~Angle();
+
+        virtual void initOptions(Options                    *options,
+                                 TrajectoryAnalysisSettings *settings);
+        virtual void optionsFinished(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:
+        void checkSelections(const SelectionList &sel1,
+                             const SelectionList &sel2) const;
+
+        SelectionList                            sel1_;
+        SelectionList                            sel2_;
+        SelectionOptionInfo                     *sel1info_;
+        SelectionOptionInfo                     *sel2info_;
+        std::string                              fnAverage_;
+        std::string                              fnAll_;
+        std::string                              fnHistogram_;
+
+        std::string                              g1type_;
+        std::string                              g2type_;
+        double                                   binWidth_;
+
+        AnalysisData                             angles_;
+        AnalysisDataFrameAverageModulePointer    averageModule_;
+        AnalysisDataSimpleHistogramModulePointer histogramModule_;
+        int                                      natoms1_;
+        int                                      natoms2_;
+        // TODO: It is not possible to put rvec into a container.
+        std::vector<rvec *>                      vt0_;
+
+        // Copy and assign disallowed by base.
+};
 
 Angle::Angle()
-    : TrajectoryAnalysisModule(name, shortDescription),
+    : TrajectoryAnalysisModule(AngleInfo::name, AngleInfo::shortDescription),
       sel1info_(NULL), sel2info_(NULL), binWidth_(1.0), natoms1_(0), natoms2_(0)
 {
     averageModule_.reset(new AnalysisDataFrameAverageModule());
@@ -551,6 +601,17 @@ Angle::writeOutput()
 {
 }
 
-} // namespace modules
+}       // namespace
+
+const char AngleInfo::name[]             = "gangle";
+const char AngleInfo::shortDescription[] =
+    "Calculate angles";
+
+TrajectoryAnalysisModulePointer AngleInfo::create()
+{
+    return TrajectoryAnalysisModulePointer(new Angle);
+}
+
+} // namespace analysismodules
 
-} // namespace gmxana
+} // namespace gmx
index 296f62044109ca6e512887d9ad82511ca169bacb..6da5ef693eda773bcc345125596ddb65c09d8238 100644 (file)
 #ifndef GMX_TRAJECTORYANALYSIS_MODULES_ANGLE_H
 #define GMX_TRAJECTORYANALYSIS_MODULES_ANGLE_H
 
-#include <string>
-#include <vector>
-
 #include "../analysismodule.h"
-#include "gromacs/analysisdata/analysisdata.h"
-#include "gromacs/analysisdata/modules/average.h"
-#include "gromacs/analysisdata/modules/histogram.h"
-#include "gromacs/selection/selection.h"
 
 namespace gmx
 {
 
-class SelectionOptionInfo;
-
 namespace analysismodules
 {
 
-class Angle : public TrajectoryAnalysisModule
+class AngleInfo
 {
     public:
         static const char name[];
         static const char shortDescription[];
-
-        Angle();
-        virtual ~Angle();
-
-        virtual void initOptions(Options                    *options,
-                                 TrajectoryAnalysisSettings *settings);
-        virtual void optionsFinished(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:
-        void checkSelections(const SelectionList &sel1,
-                             const SelectionList &sel2) const;
-
-        SelectionList                            sel1_;
-        SelectionList                            sel2_;
-        SelectionOptionInfo                     *sel1info_;
-        SelectionOptionInfo                     *sel2info_;
-        std::string                              fnAverage_;
-        std::string                              fnAll_;
-        std::string                              fnHistogram_;
-
-        std::string                              g1type_;
-        std::string                              g2type_;
-        double                                   binWidth_;
-
-        AnalysisData                             angles_;
-        AnalysisDataFrameAverageModulePointer    averageModule_;
-        AnalysisDataSimpleHistogramModulePointer histogramModule_;
-        int                                      natoms1_;
-        int                                      natoms2_;
-        // TODO: It is not possible to put rvec into a container.
-        std::vector<rvec *>                      vt0_;
-
-        // Copy and assign disallowed by base.
+        static TrajectoryAnalysisModulePointer create();
 };
 
 } // namespace analysismodules
index adf7bb6e8141aef14cf47fad8c593be0e15471ec..7a777f697ece98aeb4dd480a58cd9d14a12cfdb9 100644 (file)
  */
 #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"
@@ -61,12 +65,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,11 +128,6 @@ Distance::Distance()
 }
 
 
-Distance::~Distance()
-{
-}
-
-
 void
 Distance::initOptions(Options *options, TrajectoryAnalysisSettings * /*settings*/)
 {
@@ -146,9 +181,6 @@ Distance::initOptions(Options *options, TrajectoryAnalysisSettings * /*settings*
 }
 
 
-namespace
-{
-
 /*! \brief
  * Checks that selections conform to the expectations of the tool.
  */
@@ -181,8 +213,6 @@ void checkSelections(const SelectionList &sel)
     }
 }
 
-}       // namespace
-
 
 void
 Distance::initAnalysis(const TrajectoryAnalysisSettings &settings,
@@ -343,6 +373,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
index 6fc6d7c423b2ff2d5d208f21368663be277980cc..101548bde1c84d578273b445e69f20762b4f028a 100644 (file)
 #ifndef GMX_TRAJECTORYANALYSIS_MODULES_DISTANCE_H
 #define GMX_TRAJECTORYANALYSIS_MODULES_DISTANCE_H
 
-#include <string>
-
 #include "../analysismodule.h"
-#include "gromacs/analysisdata/analysisdata.h"
-#include "gromacs/analysisdata/modules/average.h"
-#include "gromacs/analysisdata/modules/histogram.h"
-#include "gromacs/selection/selection.h"
 
 namespace gmx
 {
@@ -56,45 +50,12 @@ namespace gmx
 namespace analysismodules
 {
 
-class Distance : public TrajectoryAnalysisModule
+class DistanceInfo
 {
     public:
         static const char name[];
         static const char shortDescription[];
-
-        Distance();
-        virtual ~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.
+        static TrajectoryAnalysisModulePointer create();
 };
 
 } // namespace analysismodules
index 10338826d05c78a759b51a4f18384a1279f053e8..7882e14ceb35910d934984a55f619b195929159b 100644 (file)
  */
 #include "freevolume.h"
 
-#include "gromacs/legacyheaders/pbc.h"
-#include "gromacs/legacyheaders/vec.h"
+#include <string>
+
 #include "gromacs/legacyheaders/atomprop.h"
 #include "gromacs/legacyheaders/copyrite.h"
+#include "gromacs/legacyheaders/gmx_random.h"
+#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/plot.h"
 #include "gromacs/options/basicoptions.h"
 #include "gromacs/options/filenameoption.h"
 #include "gromacs/options/options.h"
+#include "gromacs/selection/nbsearch.h"
 #include "gromacs/selection/selection.h"
 #include "gromacs/selection/selectionoption.h"
 #include "gromacs/trajectoryanalysis/analysissettings.h"
@@ -63,16 +68,69 @@ namespace gmx
 namespace analysismodules
 {
 
-const char FreeVolume::name[]             = "freevolume";
-const char FreeVolume::shortDescription[] =
-    "Calculate free volume";
+namespace
+{
+
+/*! \brief
+ * Class used to compute free volume in a simulations box.
+ *
+ * Inherits TrajectoryAnalysisModule and all functions from there.
+ * Does not implement any new functionality.
+ *
+ * \ingroup module_trajectoryanalysis
+ */
+class FreeVolume : public TrajectoryAnalysisModule
+{
+    public:
+        //! Constructor
+        FreeVolume();
+
+        //! Destructor
+        virtual ~FreeVolume();
+
+        //! Set the options and setting
+        virtual void initOptions(Options                    *options,
+                                 TrajectoryAnalysisSettings *settings);
+
+        //! First routine called by the analysis framework
+        virtual void initAnalysis(const TrajectoryAnalysisSettings &settings,
+                                  const TopologyInformation        &top);
+
+        //! Call for each frame of the trajectory
+        virtual void analyzeFrame(int frnr, const t_trxframe &fr, t_pbc *pbc,
+                                  TrajectoryAnalysisModuleData *pdata);
+
+        //! Last routine called by the analysis framework
+        virtual void finishAnalysis(int nframes);
+
+        //! Routine to write output, that is additional over the built-in
+        virtual void writeOutput();
+
+    private:
+        std::string                       fnFreevol_;
+        Selection                         sel_;
+        AnalysisData                      data_;
+        AnalysisDataAverageModulePointer  adata_;
+
+        int                               nmol_;
+        double                            mtot_;
+        double                            cutoff_;
+        double                            probeRadius_;
+        gmx_rng_t                         rng_;
+        int                               seed_, ninsert_;
+        AnalysisNeighborhood              nb_;
+        //! The van der Waals radius per atom
+        std::vector<double>               vdw_radius_;
+
+        // Copy and assign disallowed by base.
+};
 
 // Constructor. Here it is important to initialize the pointer to
 // subclasses that are elements of the main class. Here we have only
 // one. The type of this depends on what kind of tool you need.
 // Here we only have simple value/time kind of data.
 FreeVolume::FreeVolume()
-    : TrajectoryAnalysisModule(name, shortDescription),
+    : TrajectoryAnalysisModule(FreeVolumeInfo::name, FreeVolumeInfo::shortDescription),
       adata_(new AnalysisDataAverageModule())
 {
     // We only compute two numbers per frame
@@ -376,6 +434,17 @@ FreeVolume::writeOutput()
     printf("Fractional free volume %.3f +/- %.3f\n", FFVaver, FFVerror);
 }
 
+}       // namespace
+
+const char FreeVolumeInfo::name[]             = "freevolume";
+const char FreeVolumeInfo::shortDescription[] =
+    "Calculate free volume";
+
+TrajectoryAnalysisModulePointer FreeVolumeInfo::create()
+{
+    return TrajectoryAnalysisModulePointer(new FreeVolume);
+}
+
 } // namespace analysismodules
 
 } // namespace gmx
index ec8d2ca0d6adfa1a3b42605012cb3cb0e8b617c2..ecb93b1db42a9aea6fbd5ce8ec77370b9893876e 100644 (file)
@@ -34,7 +34,7 @@
  */
 /*! \internal \file
  * \brief
- * Declares trajectory analysis module for distance calculations.
+ * Declares trajectory analysis module for free volume calculations.
  *
  * \author David van der Spoel <david.vanderspoel@icm.uu.se>
  * \ingroup module_trajectoryanalysis
 #ifndef GMX_TRAJECTORYANALYSIS_MODULES_FREEVOLUME_H
 #define GMX_TRAJECTORYANALYSIS_MODULES_FREEVOLUME_H
 
-#include <string>
-#include "gromacs/legacyheaders/gmx_random.h"
 #include "../analysismodule.h"
-#include "gromacs/selection/nbsearch.h"
-#include "gromacs/selection/indexutil.h"
-#include "gromacs/analysisdata/analysisdata.h"
-#include "gromacs/analysisdata/modules/average.h"
-#include "gromacs/selection/selection.h"
 
 namespace gmx
 {
@@ -57,65 +50,12 @@ namespace gmx
 namespace analysismodules
 {
 
-/*! \brief
- * Class used to compute free volume in a simulations box.
- *
- * Inherits TrajectoryAnalysisModule and all functions from there.
- * Does not implement any new functionality.
- *
- * \inpublicapi
- * \ingroup module_trajectoryanalysis
- */
-class FreeVolume : public TrajectoryAnalysisModule
+class FreeVolumeInfo
 {
     public:
-        //! Name of the tool
         static const char name[];
-
-        //! One line description
         static const char shortDescription[];
-
-        //! Constructor
-        FreeVolume();
-
-        //! Destructor
-        virtual ~FreeVolume();
-
-        //! Set the options and setting
-        virtual void initOptions(Options                    *options,
-                                 TrajectoryAnalysisSettings *settings);
-
-        //! First routine called by the analysis frame work
-        virtual void initAnalysis(const TrajectoryAnalysisSettings &settings,
-                                  const TopologyInformation        &top);
-
-        //! Call for each frame of the trajectory
-        virtual void analyzeFrame(int frnr, const t_trxframe &fr, t_pbc *pbc,
-                                  TrajectoryAnalysisModuleData *pdata);
-
-        //! Last routine called by the analysis frame work
-        virtual void finishAnalysis(int nframes);
-
-        //! Routine to write output, that is additional over the built-in
-        virtual void writeOutput();
-
-    private:
-        std::string                       fnFreevol_;
-        Selection                         sel_;
-        AnalysisData                      data_;
-        AnalysisDataAverageModulePointer  adata_;
-
-        int                               nmol_;
-        double                            mtot_;
-        double                            cutoff_;
-        double                            probeRadius_;
-        gmx_rng_t                         rng_;
-        int                               seed_, ninsert_;
-        AnalysisNeighborhood              nb_;
-        //! The van der Waals radius per atom
-        std::vector<double>               vdw_radius_;
-
-        // Copy and assign disallowed by base.
+        static TrajectoryAnalysisModulePointer create();
 };
 
 } // namespace analysismodules
index ef211292b427a9fbc56787f0e893cb22c86e58ce..6fa093bea2c4ced023aed6a82b56e1e887ceb36b 100644 (file)
@@ -57,6 +57,7 @@
 #include "gromacs/analysisdata/dataframe.h"
 #include "gromacs/analysisdata/datamodule.h"
 #include "gromacs/analysisdata/modules/average.h"
+#include "gromacs/analysisdata/modules/lifetime.h"
 #include "gromacs/analysisdata/modules/plot.h"
 #include "gromacs/options/basicoptions.h"
 #include "gromacs/options/filenameoption.h"
@@ -246,19 +247,59 @@ void IndexFileWriterModule::dataFinished()
     closeFile();
 }
 
-}       // namespace
-
-
 /********************************************************************
  * Select
  */
 
-const char Select::name[]             = "select";
-const char Select::shortDescription[] =
-    "Print general information about selections";
+class Select : public TrajectoryAnalysisModule
+{
+    public:
+        Select();
+
+        virtual void initOptions(Options                    *options,
+                                 TrajectoryAnalysisSettings *settings);
+        virtual void optionsFinished(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_;
+        SelectionOptionInfo                *selOpt_;
+
+        std::string                         fnSize_;
+        std::string                         fnFrac_;
+        std::string                         fnIndex_;
+        std::string                         fnNdx_;
+        std::string                         fnMask_;
+        std::string                         fnOccupancy_;
+        std::string                         fnPDB_;
+        std::string                         fnLifetime_;
+        bool                                bTotNorm_;
+        bool                                bFracNorm_;
+        bool                                bResInd_;
+        bool                                bCumulativeLifetimes_;
+        std::string                         resNumberType_;
+        std::string                         pdbAtoms_;
+
+        const TopologyInformation          *top_;
+        std::vector<int>                    totsize_;
+        AnalysisData                        sdata_;
+        AnalysisData                        cdata_;
+        AnalysisData                        idata_;
+        AnalysisData                        mdata_;
+        AnalysisDataAverageModulePointer    occupancyModule_;
+        AnalysisDataLifetimeModulePointer   lifetimeModule_;
+};
 
 Select::Select()
-    : TrajectoryAnalysisModule(name, shortDescription),
+    : TrajectoryAnalysisModule(SelectInfo::name, SelectInfo::shortDescription),
       selOpt_(NULL),
       bTotNorm_(false), bFracNorm_(false), bResInd_(false),
       bCumulativeLifetimes_(true), top_(NULL),
@@ -280,11 +321,6 @@ Select::Select()
 }
 
 
-Select::~Select()
-{
-}
-
-
 void
 Select::initOptions(Options *options, TrajectoryAnalysisSettings * /*settings*/)
 {
@@ -690,6 +726,17 @@ Select::writeOutput()
     }
 }
 
+}       // namespace
+
+const char SelectInfo::name[]             = "select";
+const char SelectInfo::shortDescription[] =
+    "Print general information about selections";
+
+TrajectoryAnalysisModulePointer SelectInfo::create()
+{
+    return TrajectoryAnalysisModulePointer(new Select);
+}
+
 } // namespace analysismodules
 
 } // namespace gmx
index b25b476c2b1646dc261c81c2936005cc679e64c1..8f8fa245c054b83820485b9c56bb339d00585c70 100644 (file)
 #ifndef GMX_TRAJECTORYANALYSIS_MODULES_SELECT_H
 #define GMX_TRAJECTORYANALYSIS_MODULES_SELECT_H
 
-#include <string>
-#include <vector>
-
 #include "../analysismodule.h"
-#include "gromacs/analysisdata/analysisdata.h"
-#include "gromacs/analysisdata/modules/average.h"
-#include "gromacs/analysisdata/modules/lifetime.h"
-#include "gromacs/selection/selection.h"
 
 namespace gmx
 {
 
-class SelectionOptionInfo;
-
 namespace analysismodules
 {
 
-class Select : public TrajectoryAnalysisModule
+class SelectInfo
 {
     public:
         static const char name[];
         static const char shortDescription[];
-
-        Select();
-        virtual ~Select();
-
-        virtual void initOptions(Options                    *options,
-                                 TrajectoryAnalysisSettings *settings);
-        virtual void optionsFinished(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_;
-        SelectionOptionInfo                *selOpt_;
-
-        std::string                         fnSize_;
-        std::string                         fnFrac_;
-        std::string                         fnIndex_;
-        std::string                         fnNdx_;
-        std::string                         fnMask_;
-        std::string                         fnOccupancy_;
-        std::string                         fnPDB_;
-        std::string                         fnLifetime_;
-        bool                                bTotNorm_;
-        bool                                bFracNorm_;
-        bool                                bResInd_;
-        bool                                bCumulativeLifetimes_;
-        std::string                         resNumberType_;
-        std::string                         pdbAtoms_;
-
-        const TopologyInformation          *top_;
-        std::vector<int>                    totsize_;
-        AnalysisData                        sdata_;
-        AnalysisData                        cdata_;
-        AnalysisData                        idata_;
-        AnalysisData                        mdata_;
-        AnalysisDataAverageModulePointer    occupancyModule_;
-        AnalysisDataLifetimeModulePointer   lifetimeModule_;
+        static TrajectoryAnalysisModulePointer create();
 };
 
 } // namespace analysismodules
index 66f2641566d077cd5149b09f9a01c9f8c1dd6cc5..1d07fe3d3acf56b080848fe9968ff4a9469631f9 100644 (file)
@@ -57,7 +57,7 @@ using gmx::test::CommandLine;
  */
 
 //! Test fixture for the angle analysis module.
-typedef gmx::test::TrajectoryAnalysisModuleTestFixture<gmx::analysismodules::Angle>
+typedef gmx::test::TrajectoryAnalysisModuleTestFixture<gmx::analysismodules::AngleInfo>
     AngleModuleTest;
 
 TEST_F(AngleModuleTest, ComputesSimpleAngles)
index 2ddd62919121b02ed230fb4ac658212d616dec77..d5f87495794d44c4ace7b82b12f8fa50cbee8869 100644 (file)
@@ -57,7 +57,7 @@ using gmx::test::CommandLine;
  */
 
 //! Test fixture for the angle analysis module.
-typedef gmx::test::TrajectoryAnalysisModuleTestFixture<gmx::analysismodules::Distance>
+typedef gmx::test::TrajectoryAnalysisModuleTestFixture<gmx::analysismodules::DistanceInfo>
     DistanceModuleTest;
 
 TEST_F(DistanceModuleTest, ComputesDistances)
index e31bbac57ff400b44cebd21fb0f527252e7fcffa..3a01165207fd321751ed0119d13f92370cd38c3d 100644 (file)
@@ -57,7 +57,7 @@ using gmx::test::CommandLine;
  */
 
 //! Test fixture for the angle analysis module.
-typedef gmx::test::TrajectoryAnalysisModuleTestFixture<gmx::analysismodules::FreeVolume>
+typedef gmx::test::TrajectoryAnalysisModuleTestFixture<gmx::analysismodules::FreeVolumeInfo>
     FreeVolumeModuleTest;
 
 TEST_F(FreeVolumeModuleTest, ComputesFreeVolume)
index 190fbabfaac27c14bc4f6653e518f06dd741d615..c7484b0adf9c66ae33c0109e630a9f8945ce59c0 100644 (file)
@@ -188,21 +188,21 @@ class AbstractTrajectoryAnalysisModuleTestFixture : public ::testing::Test
 /*! \internal \brief
  * Test fixture for a trajectory analysis module.
  *
- * \tparam ModuleType  Type of the analysis module to test.
+ * \tparam ModuleInfo  Info object for the analysis module to test.
  *
- * \p ModuleType should derive from TrajectoryAnalysisModule and be
- * default-constructible.
+ * \p ModuleInfo should provide a static
+ * `TrajectoryAnalysisModulePointer create()` method.
  *
  * \ingroup module_trajectoryanalysis
  */
-template <class ModuleType>
+template <class ModuleInfo>
 class TrajectoryAnalysisModuleTestFixture
     : public AbstractTrajectoryAnalysisModuleTestFixture
 {
     protected:
         virtual TrajectoryAnalysisModulePointer createModule()
         {
-            return TrajectoryAnalysisModulePointer(new ModuleType);
+            return ModuleInfo::create();
         }
 };
 
index 8adc76abcedd2d4a7b122f35ddf968223f316275..d2060679edf249f532c7df505b3ab2cd6b4b1395 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2012, by the GROMACS development team, led by
+ * Copyright (c) 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.
@@ -65,7 +65,7 @@ using gmx::test::CommandLine;
  */
 
 //! Test fixture for the select analysis module.
-typedef gmx::test::TrajectoryAnalysisModuleTestFixture<gmx::analysismodules::Select>
+typedef gmx::test::TrajectoryAnalysisModuleTestFixture<gmx::analysismodules::SelectInfo>
     SelectModuleTest;
 
 TEST_F(SelectModuleTest, BasicTest)