Initialize default index groups for C++ tools
authorTeemu Murtola <teemu.murtola@gmail.com>
Wed, 25 Dec 2013 18:47:56 +0000 (20:47 +0200)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Mon, 30 Dec 2013 14:29:27 +0000 (15:29 +0100)
If an index file is not explicitly provided, initialize default
index groups using the old analyse() method for the new C++ tools.
This is better than nothing, and keeps the default groups in sync.

Also some clean-up for related code in
TrajectoryAnalysisCommandLineRunner.

Temporary fix for #921.

Change-Id: I4f9d2586a0446302ce0a2d9326d2fd4c85b02ef0

src/gromacs/trajectoryanalysis/cmdlinerunner.cpp
src/gromacs/trajectoryanalysis/cmdlinerunner.h
src/gromacs/trajectoryanalysis/runnercommon.cpp
src/gromacs/trajectoryanalysis/runnercommon.h
src/gromacs/trajectoryanalysis/tests/moduletest.cpp

index a4070f464226a3abf419d55c8c1abd01bc6804a5..7e0dfe854891c69893acc0c334003df9b7f09c93 100644 (file)
@@ -80,19 +80,20 @@ class TrajectoryAnalysisCommandLineRunner::Impl
         Impl(TrajectoryAnalysisModule *module);
         ~Impl();
 
-        bool parseOptions(TrajectoryAnalysisSettings *settings,
+        void parseOptions(TrajectoryAnalysisSettings *settings,
                           TrajectoryAnalysisRunnerCommon *common,
                           SelectionCollection *selections,
                           int *argc, char *argv[]);
 
         TrajectoryAnalysisModule *module_;
+        bool                      bUseDefaultGroups_;
         int                       debugLevel_;
 };
 
 
 TrajectoryAnalysisCommandLineRunner::Impl::Impl(
         TrajectoryAnalysisModule *module)
-    : module_(module), debugLevel_(0)
+    : module_(module), bUseDefaultGroups_(true), debugLevel_(0)
 {
 }
 
@@ -102,7 +103,7 @@ TrajectoryAnalysisCommandLineRunner::Impl::~Impl()
 }
 
 
-bool
+void
 TrajectoryAnalysisCommandLineRunner::Impl::parseOptions(
         TrajectoryAnalysisSettings *settings,
         TrajectoryAnalysisRunnerCommon *common,
@@ -136,14 +137,15 @@ TrajectoryAnalysisCommandLineRunner::Impl::parseOptions(
     common->optionsFinished(&commonOptions);
     module_->optionsFinished(&moduleOptions, settings);
 
-    common->initIndexGroups(selections);
+    common->initIndexGroups(selections, bUseDefaultGroups_);
 
     // TODO: Check whether the input is a pipe.
-    bool bInteractive = true;
+    const bool bInteractive = true;
     seloptManager.parseRequestedFromStdin(bInteractive);
     common->doneIndexGroups(selections);
 
-    return true;
+    common->initTopology(selections);
+    selections->compile();
 }
 
 
@@ -163,6 +165,13 @@ TrajectoryAnalysisCommandLineRunner::~TrajectoryAnalysisCommandLineRunner()
 }
 
 
+void
+TrajectoryAnalysisCommandLineRunner::setUseDefaultGroups(bool bUseDefaults)
+{
+    impl_->bUseDefaultGroups_ = bUseDefaults;
+}
+
+
 void
 TrajectoryAnalysisCommandLineRunner::setSelectionDebugLevel(int debuglevel)
 {
@@ -181,13 +190,7 @@ TrajectoryAnalysisCommandLineRunner::run(int argc, char *argv[])
     TrajectoryAnalysisSettings      settings;
     TrajectoryAnalysisRunnerCommon  common(&settings);
 
-    if (!impl_->parseOptions(&settings, &common, &selections, &argc, argv))
-    {
-        return 0;
-    }
-
-    common.initTopology(&selections);
-    selections.compile();
+    impl_->parseOptions(&settings, &common, &selections, &argc, argv);
 
     const TopologyInformation &topology = common.topologyInformation();
     module->initAnalysis(settings, topology);
index 97e4e20192747de63951d7f378bc77210e20ac58..0b87455e851a6960341e3426276e3178f3d41392 100644 (file)
@@ -150,12 +150,22 @@ class TrajectoryAnalysisCommandLineRunner
         ~TrajectoryAnalysisCommandLineRunner();
 
         /*! \brief
-         * Sets the default debugging level for selections.
+         * Sets whether default index groups are initialized.
          *
-         * This is intended only for use by internal debugging tools.
+         * This is intended only for internal unit testing purposes to avoid
+         * repeated, unnecessary initialization of the default groups, which
+         * can be expensive under, e.g., valgrind.
+         *
+         * Does not throw.
+         */
+        void setUseDefaultGroups(bool bUseDefaults);
+        /*! \brief
+         * Sets the default debugging level for selections.
          *
          * \param[in] debuglevel  Level of debugging verbosity.
          *
+         * This is intended only for use by internal debugging tools.
+         *
          * Does not throw.
          *
          * \see SelectionCollection::setDebugLevel()
index 0aea2050de340da516669bbed1f07bce2965f7bc..2b0fc38fe684831b0b0ae1d2d20223cc37e3784c 100644 (file)
@@ -250,18 +250,22 @@ TrajectoryAnalysisRunnerCommon::optionsFinished(Options *options)
 
 
 void
-TrajectoryAnalysisRunnerCommon::initIndexGroups(SelectionCollection *selections)
+TrajectoryAnalysisRunnerCommon::initIndexGroups(SelectionCollection *selections,
+                                                bool                 bUseDefaults)
 {
     if (impl_->ndxfile_.empty())
     {
-        // TODO: Initialize default selections
-        selections->setIndexGroups(NULL);
-    }
-    else
-    {
-        gmx_ana_indexgrps_init(&impl_->grps_, NULL, impl_->ndxfile_.c_str());
-        selections->setIndexGroups(impl_->grps_);
+        if (!bUseDefaults)
+        {
+            selections->setIndexGroups(NULL);
+            return;
+        }
+        initTopology(selections);
     }
+    const char *const ndxfile
+        = (!impl_->ndxfile_.empty() ? impl_->ndxfile_.c_str() : NULL);
+    gmx_ana_indexgrps_init(&impl_->grps_, impl_->topInfo_.topology(), ndxfile);
+    selections->setIndexGroups(impl_->grps_);
 }
 
 
@@ -280,8 +284,14 @@ TrajectoryAnalysisRunnerCommon::doneIndexGroups(SelectionCollection *selections)
 void
 TrajectoryAnalysisRunnerCommon::initTopology(SelectionCollection *selections)
 {
+    // Return immediately if the topology has already been loaded.
+    if (impl_->topInfo_.hasTopology())
+    {
+        return;
+    }
+
     const TrajectoryAnalysisSettings &settings = impl_->settings_;
-    bool bRequireTop
+    const bool bRequireTop
         = settings.hasFlag(TrajectoryAnalysisSettings::efRequireTop)
             || selections->requiresTopology();
     if (bRequireTop && impl_->topfile_.empty())
index caa3d5a65bd65850e6439b875d71abded8efcc22..44b22eceae082da7dacedda549bdb86186564c40 100644 (file)
@@ -90,7 +90,8 @@ class TrajectoryAnalysisRunnerCommon
          */
         void optionsFinished(Options *options);
         //! Initialize index groups for selections.
-        void initIndexGroups(SelectionCollection *selections);
+        void initIndexGroups(SelectionCollection *selections,
+                             bool                 bUseDefaults);
         //! Free memory allocated for index groups.
         void doneIndexGroups(SelectionCollection *selections);
         //! Load topology information if provided and/or required.
index c99c9ef9032bcb3415e42499730183a04e66ee1f..5d2a241206e86ae56e1c209b129930a40daad830 100644 (file)
@@ -214,6 +214,7 @@ AbstractTrajectoryAnalysisModuleTestFixture::runTest(const CommandLine &args)
     }
 
     TrajectoryAnalysisCommandLineRunner runner(&module);
+    runner.setUseDefaultGroups(false);
     int rc = 0;
     EXPECT_NO_THROW_GMX(rc = runner.run(impl_->cmdline_.argc(), impl_->cmdline_.argv()));
     EXPECT_EQ(0, rc);