2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,2014,2015, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Implements gmx::TrajectoryAnalysisCommandLineRunner.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_trajectoryanalysis
44 #include "cmdlinerunner.h"
46 #include "gromacs/analysisdata/paralleloptions.h"
47 #include "gromacs/commandline/cmdlinehelpcontext.h"
48 #include "gromacs/commandline/cmdlinehelpwriter.h"
49 #include "gromacs/commandline/cmdlinemodule.h"
50 #include "gromacs/commandline/cmdlinemodulemanager.h"
51 #include "gromacs/commandline/cmdlineparser.h"
52 #include "gromacs/fileio/trx.h"
53 #include "gromacs/options/filenameoptionmanager.h"
54 #include "gromacs/options/options.h"
55 #include "gromacs/pbcutil/pbc.h"
56 #include "gromacs/selection/selectioncollection.h"
57 #include "gromacs/selection/selectionoptionmanager.h"
58 #include "gromacs/trajectoryanalysis/analysismodule.h"
59 #include "gromacs/trajectoryanalysis/analysissettings.h"
60 #include "gromacs/utility/exceptions.h"
61 #include "gromacs/utility/filestream.h"
62 #include "gromacs/utility/gmxassert.h"
64 #include "runnercommon.h"
69 /********************************************************************
70 * TrajectoryAnalysisCommandLineRunner::Impl
73 class TrajectoryAnalysisCommandLineRunner::Impl
76 class RunnerCommandLineModule;
78 Impl(TrajectoryAnalysisModule *module);
81 void parseOptions(TrajectoryAnalysisSettings *settings,
82 TrajectoryAnalysisRunnerCommon *common,
83 SelectionCollection *selections,
84 int *argc, char *argv[], bool full = true);
86 TrajectoryAnalysisModule *module_;
87 bool bUseDefaultGroups_;
92 TrajectoryAnalysisCommandLineRunner::Impl::Impl(
93 TrajectoryAnalysisModule *module)
94 : module_(module), bUseDefaultGroups_(true), debugLevel_(0)
99 TrajectoryAnalysisCommandLineRunner::Impl::~Impl()
105 TrajectoryAnalysisCommandLineRunner::Impl::parseOptions(
106 TrajectoryAnalysisSettings *settings,
107 TrajectoryAnalysisRunnerCommon *common,
108 SelectionCollection *selections,
109 int *argc, char *argv[], bool full)
111 FileNameOptionManager fileoptManager;
112 SelectionOptionManager seloptManager(selections);
113 Options options(NULL, NULL);
114 IOptionsContainer &commonOptions = options.addGroup();
115 IOptionsContainer &moduleOptions = options.addGroup();
117 options.addManager(&fileoptManager);
118 options.addManager(&seloptManager);
120 module_->initOptions(&moduleOptions, settings);
123 common->initOptions(&commonOptions);
125 selections->initOptions(&commonOptions);
128 CommandLineParser parser(&options);
129 // TODO: Print the help if user provides an invalid option?
130 // Or just add a message advising the user to invoke the help?
131 parser.parse(argc, argv);
132 common->scaleTimeOptions(&options);
138 common->optionsFinished();
140 module_->optionsFinished(settings);
142 common->initIndexGroups(selections, bUseDefaultGroups_);
144 const bool bInteractive = StandardInputStream::instance().isInteractive();
145 seloptManager.parseRequestedFromStdin(bInteractive);
147 common->doneIndexGroups(selections);
148 common->initTopology(selections);
150 selections->compile();
154 /********************************************************************
155 * TrajectoryAnalysisCommandLineRunner
158 TrajectoryAnalysisCommandLineRunner::TrajectoryAnalysisCommandLineRunner(
159 TrajectoryAnalysisModule *module)
160 : impl_(new Impl(module))
165 TrajectoryAnalysisCommandLineRunner::~TrajectoryAnalysisCommandLineRunner()
171 TrajectoryAnalysisCommandLineRunner::setUseDefaultGroups(bool bUseDefaults)
173 impl_->bUseDefaultGroups_ = bUseDefaults;
178 TrajectoryAnalysisCommandLineRunner::setSelectionDebugLevel(int debuglevel)
180 impl_->debugLevel_ = debuglevel;
185 TrajectoryAnalysisCommandLineRunner::run(int argc, char *argv[])
187 TrajectoryAnalysisModule *module = impl_->module_;
189 SelectionCollection selections;
190 selections.setDebugLevel(impl_->debugLevel_);
192 TrajectoryAnalysisSettings settings;
193 TrajectoryAnalysisRunnerCommon common(&settings);
195 impl_->parseOptions(&settings, &common, &selections, &argc, argv);
197 const TopologyInformation &topology = common.topologyInformation();
198 module->initAnalysis(settings, topology);
200 TrajectoryAnalysisModule::Batch batch = module->getBatch();
201 std::vector<SelectionCollection*> batchSelections;
202 std::vector<Impl*> impls;
203 for (size_t i = 0; i < batch.size(); i++)
205 TrajectoryAnalysisModule *bmodule = batch[i];
206 batchSelections.push_back(new SelectionCollection());
207 impls.push_back(new Impl(bmodule));
208 std::vector<char*> modArgv = module->getArgv(i);
209 int modArgc = modArgv.size();
210 impls.back()->parseOptions(&settings, &common, batchSelections.back(), &modArgc, modArgv.data(), false);
212 batch[i]->initAnalysis(settings, topology);
216 common.initFirstFrame();
217 module->initAfterFirstFrame(settings, common.frame());
220 t_pbc *ppbc = settings.hasPBC() ? &pbc : NULL;
223 AnalysisDataParallelOptions dataOptions;
224 TrajectoryAnalysisModuleDataPointer pdata(
225 module->startFrames(dataOptions, selections));
227 std::vector<AnalysisDataParallelOptions> batchOptions;
228 std::vector<TrajectoryAnalysisModuleDataPointer> batchDataPointers;
229 for (size_t i = 0; i < batch.size(); i++)
231 batch[i]->initAfterFirstFrame(settings, common.frame());
233 batchOptions.push_back(AnalysisDataParallelOptions());
234 batchDataPointers.push_back(batch[i]->startFrames(
235 batchOptions.back(), *batchSelections[i]));
241 t_trxframe &frame = common.frame();
244 set_pbc(ppbc, topology.ePBC(), frame.box);
247 for (size_t i = 0; i < batch.size(); i++)
249 batchSelections[i]->evaluate(&frame, ppbc);
250 batch[i]->analyzeFrame(nframes, frame, ppbc, batchDataPointers[i].get());
251 batch[i]->finishFrameSerial(nframes);
253 selections.evaluate(&frame, ppbc);
254 module->analyzeFrame(nframes, frame, ppbc, pdata.get());
255 module->finishFrameSerial(nframes);
259 while (common.readNextFrame());
260 for (size_t i = 0; i < batch.size(); i++)
262 batch[i]->finishFrames(batchDataPointers[i].get());
263 if (batchDataPointers[i].get() != NULL)
265 batchDataPointers[i]->finish();
267 batchDataPointers[i].reset();
270 module->finishFrames(pdata.get());
271 if (pdata.get() != NULL)
277 if (common.hasTrajectory())
279 fprintf(stderr, "Analyzed %d frames, last time %.3f\n",
280 nframes, common.frame().time);
284 fprintf(stderr, "Analyzed topology coordinates\n");
287 // Restore the maximal groups for dynamic selections.
288 for (size_t i = 0; i < batch.size(); i++)
290 batchSelections[i]->evaluateFinal(nframes);
291 batch[i]->finishAnalysis(nframes);
292 batch[i]->writeOutput();
294 delete batchSelections[i];
298 selections.evaluateFinal(nframes);
300 module->finishAnalysis(nframes);
301 module->writeOutput();
308 TrajectoryAnalysisCommandLineRunner::writeHelp(const CommandLineHelpContext &context)
310 // TODO: This method duplicates some code from run().
311 // See how to best refactor it to share the common code.
312 SelectionCollection selections;
313 TrajectoryAnalysisSettings settings;
314 TrajectoryAnalysisRunnerCommon common(&settings);
316 SelectionOptionManager seloptManager(&selections);
317 Options options(NULL, NULL);
319 options.addManager(&seloptManager);
320 IOptionsContainer &commonOptions = options.addGroup();
321 IOptionsContainer &moduleOptions = options.addGroup();
323 impl_->module_->initOptions(&moduleOptions, &settings);
324 common.initOptions(&commonOptions);
325 selections.initOptions(&commonOptions);
327 CommandLineHelpWriter(options)
328 .setHelpText(settings.helpText())
329 .setTimeUnitString(settings.timeUnitManager().timeUnitAsString())
335 * Command line module for a trajectory analysis module.
337 * \ingroup module_trajectoryanalysis
339 class TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule
340 : public ICommandLineModule
344 * Constructs a module.
346 * \param[in] name Name for the module.
347 * \param[in] description One-line description for the module.
348 * \param[in] factory Factory method to create the analysis module.
350 * Does not throw. This is important for correct implementation of
353 RunnerCommandLineModule(const char *name, const char *description,
354 ModuleFactoryMethod factory)
355 : name_(name), description_(description), hasFunction_(true), factory_(factory), functor_(NULL)
360 * Overloaded constructor accepting a functor instead of function pointer.
362 RunnerCommandLineModule(const char *name, const char *description,
363 ModuleFactoryFunctor *factory)
364 : name_(name), description_(description), hasFunction_(false), factory_(NULL), functor_(factory)
368 virtual const char *name() const { return name_; }
369 virtual const char *shortDescription() const { return description_; };
371 virtual void init(CommandLineModuleSettings *settings);
372 virtual int run(int argc, char *argv[]);
373 virtual void writeHelp(const CommandLineHelpContext &context) const;
377 const char *description_;
379 ModuleFactoryMethod factory_;
380 ModuleFactoryFunctor *functor_;
382 GMX_DISALLOW_COPY_AND_ASSIGN(RunnerCommandLineModule);
385 void TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule::init(
386 CommandLineModuleSettings * /*settings*/)
390 int TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule::run(
391 int argc, char *argv[])
393 TrajectoryAnalysisModulePointer module(hasFunction_ ? factory_() : (*functor_)());
394 TrajectoryAnalysisCommandLineRunner runner(module.get());
395 return runner.run(argc, argv);
398 void TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule::writeHelp(
399 const CommandLineHelpContext &context) const
401 TrajectoryAnalysisModulePointer module(hasFunction_ ? factory_() : (*functor_)());
402 TrajectoryAnalysisCommandLineRunner runner(module.get());
403 runner.writeHelp(context);
408 TrajectoryAnalysisCommandLineRunner::runAsMain(
409 int argc, char *argv[], ModuleFactoryMethod factory)
411 Impl::RunnerCommandLineModule module(NULL, NULL, factory);
412 return CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
417 TrajectoryAnalysisCommandLineRunner::runAsMain(
418 int argc, char *argv[], ModuleFactoryFunctor *factory)
420 Impl::RunnerCommandLineModule module(NULL, NULL, factory);
421 return CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
426 TrajectoryAnalysisCommandLineRunner::registerModule(
427 CommandLineModuleManager *manager, const char *name,
428 const char *description, ModuleFactoryMethod factory)
430 CommandLineModulePointer module(
431 new Impl::RunnerCommandLineModule(name, description, factory));
432 manager->addModule(move(module));