Merge branch 'master' into pygromacs
[alexxy/gromacs.git] / src / gromacs / trajectoryanalysis / cmdlinerunner.cpp
index 46d6255a5d79e9ef90f1688190aa787c502a2959..912db6c9633ce7b530abc8cfa47fd645b1659da2 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2010,2011,2012,2013,2014, by the GROMACS development team, led by
+ * 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.
@@ -58,7 +58,7 @@
 #include "gromacs/trajectoryanalysis/analysismodule.h"
 #include "gromacs/trajectoryanalysis/analysissettings.h"
 #include "gromacs/utility/exceptions.h"
-#include "gromacs/utility/file.h"
+#include "gromacs/utility/filestream.h"
 #include "gromacs/utility/gmxassert.h"
 
 #include "runnercommon.h"
@@ -81,7 +81,7 @@ class TrajectoryAnalysisCommandLineRunner::Impl
         void parseOptions(TrajectoryAnalysisSettings *settings,
                           TrajectoryAnalysisRunnerCommon *common,
                           SelectionCollection *selections,
-                          int *argc, char *argv[]);
+                          int *argc, char *argv[], bool full=true);
 
         TrajectoryAnalysisModule *module_;
         bool                      bUseDefaultGroups_;
@@ -106,7 +106,7 @@ TrajectoryAnalysisCommandLineRunner::Impl::parseOptions(
         TrajectoryAnalysisSettings *settings,
         TrajectoryAnalysisRunnerCommon *common,
         SelectionCollection *selections,
-        int *argc, char *argv[])
+        int *argc, char *argv[], bool full)
 {
     FileNameOptionManager  fileoptManager;
     SelectionOptionManager seloptManager(selections);
@@ -117,12 +117,16 @@ TrajectoryAnalysisCommandLineRunner::Impl::parseOptions(
 
     options.addManager(&fileoptManager);
     options.addManager(&seloptManager);
-    options.addSubSection(&commonOptions);
+    if (full) {
+        options.addSubSection(&commonOptions);
+    }
     options.addSubSection(&selectionOptions);
     options.addSubSection(&moduleOptions);
 
     module_->initOptions(&moduleOptions, settings);
-    common->initOptions(&commonOptions);
+    if (full) {
+        common->initOptions(&commonOptions);
+    }
     selections->initOptions(&selectionOptions);
 
     {
@@ -134,16 +138,19 @@ TrajectoryAnalysisCommandLineRunner::Impl::parseOptions(
         options.finish();
     }
 
-    common->optionsFinished(&commonOptions);
+    if (full) {
+        common->optionsFinished(&commonOptions);
+    }
     module_->optionsFinished(&moduleOptions, settings);
 
     common->initIndexGroups(selections, bUseDefaultGroups_);
 
-    const bool bInteractive = File::standardInput().isInteractive();
+    const bool bInteractive = StandardInputStream::instance().isInteractive();
     seloptManager.parseRequestedFromStdin(bInteractive);
-    common->doneIndexGroups(selections);
 
+    common->doneIndexGroups(selections);
     common->initTopology(selections);
+
     selections->compile();
 }
 
@@ -194,6 +201,20 @@ TrajectoryAnalysisCommandLineRunner::run(int argc, char *argv[])
     const TopologyInformation &topology = common.topologyInformation();
     module->initAnalysis(settings, topology);
 
+    TrajectoryAnalysisModule::Batch batch = module->getBatch();
+    std::vector<SelectionCollection*> batchSelections;
+    std::vector<Impl*> impls;
+    for (size_t i = 0; i < batch.size(); i++) {
+        TrajectoryAnalysisModule *bmodule = batch[i];
+        batchSelections.push_back(new SelectionCollection());
+        impls.push_back(new Impl(bmodule));
+        std::vector<char*> modArgv = module->getArgv(i);
+        int modArgc = modArgv.size();
+        impls.back()->parseOptions(&settings, &common, batchSelections.back(), &modArgc, modArgv.data(), false);
+
+        batch[i]->initAnalysis(settings, topology);
+    }
+
     // Load first frame.
     common.initFirstFrame();
     module->initAfterFirstFrame(settings, common.frame());
@@ -205,6 +226,17 @@ TrajectoryAnalysisCommandLineRunner::run(int argc, char *argv[])
     AnalysisDataParallelOptions         dataOptions;
     TrajectoryAnalysisModuleDataPointer pdata(
             module->startFrames(dataOptions, selections));
+
+    std::vector<AnalysisDataParallelOptions> batchOptions;
+    std::vector<TrajectoryAnalysisModuleDataPointer> batchDataPointers;
+    for (size_t i = 0; i < batch.size(); i++) {
+        batch[i]->initAfterFirstFrame(settings, common.frame());
+
+        batchOptions.push_back(AnalysisDataParallelOptions());
+        batchDataPointers.push_back(batch[i]->startFrames(
+            batchOptions.back(), *batchSelections[i]));
+    }
+
     do
     {
         common.initFrame();
@@ -214,6 +246,11 @@ TrajectoryAnalysisCommandLineRunner::run(int argc, char *argv[])
             set_pbc(ppbc, topology.ePBC(), frame.box);
         }
 
+        for (size_t i = 0; i < batch.size(); i++) {
+            batchSelections[i]->evaluate(&frame, ppbc);
+            batch[i]->analyzeFrame(nframes, frame, ppbc, batchDataPointers[i].get());
+            batch[i]->finishFrameSerial(nframes);
+        }
         selections.evaluate(&frame, ppbc);
         module->analyzeFrame(nframes, frame, ppbc, pdata.get());
         module->finishFrameSerial(nframes);
@@ -221,6 +258,14 @@ TrajectoryAnalysisCommandLineRunner::run(int argc, char *argv[])
         ++nframes;
     }
     while (common.readNextFrame());
+    for (size_t i = 0; i < batch.size(); i++) {
+        batch[i]->finishFrames(batchDataPointers[i].get());
+        if (batchDataPointers[i].get() != NULL) {
+            batchDataPointers[i]->finish();
+        }
+        batchDataPointers[i].reset();
+    }
+
     module->finishFrames(pdata.get());
     if (pdata.get() != NULL)
     {
@@ -239,6 +284,15 @@ TrajectoryAnalysisCommandLineRunner::run(int argc, char *argv[])
     }
 
     // Restore the maximal groups for dynamic selections.
+    for (size_t i = 0; i < batch.size(); i++) {
+        batchSelections[i]->evaluateFinal(nframes);
+        batch[i]->finishAnalysis(nframes);
+        batch[i]->writeOutput();
+
+        delete batchSelections[i];
+        delete impls[i];
+    }
+
     selections.evaluateFinal(nframes);
 
     module->finishAnalysis(nframes);
@@ -273,7 +327,7 @@ TrajectoryAnalysisCommandLineRunner::writeHelp(const CommandLineHelpContext &con
     selections.initOptions(&selectionOptions);
 
     CommandLineHelpWriter(options)
-        .setShowDescriptions(true)
+        .setHelpText(settings.helpText())
         .setTimeUnitString(settings.timeUnitManager().timeUnitAsString())
         .writeHelp(context);
 }
@@ -285,7 +339,7 @@ TrajectoryAnalysisCommandLineRunner::writeHelp(const CommandLineHelpContext &con
  * \ingroup module_trajectoryanalysis
  */
 class TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule
-    : public CommandLineModuleInterface
+    : public ICommandLineModule
 {
     public:
         /*! \brief
@@ -346,7 +400,7 @@ int TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule::run(
 void TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule::writeHelp(
         const CommandLineHelpContext &context) const
 {
-    TrajectoryAnalysisModulePointer     module(factory_());
+    TrajectoryAnalysisModulePointer     module(hasFunction_? factory_() : (*functor_)());
     TrajectoryAnalysisCommandLineRunner runner(module.get());
     runner.writeHelp(context);
 }