2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,2014, 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/trajectoryanalysis/runnercommon.h"
61 #include "gromacs/utility/exceptions.h"
62 #include "gromacs/utility/file.h"
63 #include "gromacs/utility/gmxassert.h"
68 /********************************************************************
69 * TrajectoryAnalysisCommandLineRunner::Impl
72 class TrajectoryAnalysisCommandLineRunner::Impl
75 class RunnerCommandLineModule;
77 Impl(TrajectoryAnalysisModule *module);
80 void parseOptions(TrajectoryAnalysisSettings *settings,
81 TrajectoryAnalysisRunnerCommon *common,
82 SelectionCollection *selections,
83 int *argc, char *argv[]);
85 TrajectoryAnalysisModule *module_;
86 bool bUseDefaultGroups_;
91 TrajectoryAnalysisCommandLineRunner::Impl::Impl(
92 TrajectoryAnalysisModule *module)
93 : module_(module), bUseDefaultGroups_(true), debugLevel_(0)
98 TrajectoryAnalysisCommandLineRunner::Impl::~Impl()
104 TrajectoryAnalysisCommandLineRunner::Impl::parseOptions(
105 TrajectoryAnalysisSettings *settings,
106 TrajectoryAnalysisRunnerCommon *common,
107 SelectionCollection *selections,
108 int *argc, char *argv[])
110 FileNameOptionManager fileoptManager;
111 SelectionOptionManager seloptManager(selections);
112 Options options(NULL, NULL);
113 Options moduleOptions(module_->name(), module_->description());
114 Options commonOptions("common", "Common analysis control");
115 Options selectionOptions("selection", "Common selection control");
117 options.addManager(&fileoptManager);
118 options.addManager(&seloptManager);
119 options.addSubSection(&commonOptions);
120 options.addSubSection(&selectionOptions);
121 options.addSubSection(&moduleOptions);
123 module_->initOptions(&moduleOptions, settings);
124 common->initOptions(&commonOptions);
125 selections->initOptions(&selectionOptions);
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);
136 common->optionsFinished(&commonOptions);
137 module_->optionsFinished(&moduleOptions, settings);
139 common->initIndexGroups(selections, bUseDefaultGroups_);
141 const bool bInteractive = File::standardInput().isInteractive();
142 seloptManager.parseRequestedFromStdin(bInteractive);
143 common->doneIndexGroups(selections);
145 common->initTopology(selections);
146 selections->compile();
150 /********************************************************************
151 * TrajectoryAnalysisCommandLineRunner
154 TrajectoryAnalysisCommandLineRunner::TrajectoryAnalysisCommandLineRunner(
155 TrajectoryAnalysisModule *module)
156 : impl_(new Impl(module))
161 TrajectoryAnalysisCommandLineRunner::~TrajectoryAnalysisCommandLineRunner()
167 TrajectoryAnalysisCommandLineRunner::setUseDefaultGroups(bool bUseDefaults)
169 impl_->bUseDefaultGroups_ = bUseDefaults;
174 TrajectoryAnalysisCommandLineRunner::setSelectionDebugLevel(int debuglevel)
176 impl_->debugLevel_ = debuglevel;
181 TrajectoryAnalysisCommandLineRunner::run(int argc, char *argv[])
183 TrajectoryAnalysisModule *module = impl_->module_;
185 SelectionCollection selections;
186 selections.setDebugLevel(impl_->debugLevel_);
188 TrajectoryAnalysisSettings settings;
189 TrajectoryAnalysisRunnerCommon common(&settings);
191 impl_->parseOptions(&settings, &common, &selections, &argc, argv);
193 const TopologyInformation &topology = common.topologyInformation();
194 module->initAnalysis(settings, topology);
197 common.initFirstFrame();
198 module->initAfterFirstFrame(settings, common.frame());
201 t_pbc *ppbc = settings.hasPBC() ? &pbc : NULL;
204 AnalysisDataParallelOptions dataOptions;
205 TrajectoryAnalysisModuleDataPointer pdata(
206 module->startFrames(dataOptions, selections));
210 t_trxframe &frame = common.frame();
213 set_pbc(ppbc, topology.ePBC(), frame.box);
216 selections.evaluate(&frame, ppbc);
217 module->analyzeFrame(nframes, frame, ppbc, pdata.get());
221 while (common.readNextFrame());
222 module->finishFrames(pdata.get());
223 if (pdata.get() != NULL)
229 if (common.hasTrajectory())
231 fprintf(stderr, "Analyzed %d frames, last time %.3f\n",
232 nframes, common.frame().time);
236 fprintf(stderr, "Analyzed topology coordinates\n");
239 // Restore the maximal groups for dynamic selections.
240 selections.evaluateFinal(nframes);
242 module->finishAnalysis(nframes);
243 module->writeOutput();
250 TrajectoryAnalysisCommandLineRunner::writeHelp(const CommandLineHelpContext &context)
252 // TODO: This method duplicates some code from run().
253 // See how to best refactor it to share the common code.
254 SelectionCollection selections;
255 TrajectoryAnalysisSettings settings;
256 TrajectoryAnalysisRunnerCommon common(&settings);
258 SelectionOptionManager seloptManager(&selections);
259 Options options(NULL, NULL);
260 Options moduleOptions(impl_->module_->name(), impl_->module_->description());
261 Options commonOptions("common", "Common analysis control");
262 Options selectionOptions("selection", "Common selection control");
264 options.addManager(&seloptManager);
265 options.addSubSection(&commonOptions);
266 options.addSubSection(&selectionOptions);
267 options.addSubSection(&moduleOptions);
269 impl_->module_->initOptions(&moduleOptions, &settings);
270 common.initOptions(&commonOptions);
271 selections.initOptions(&selectionOptions);
273 CommandLineHelpWriter(options)
274 .setShowDescriptions(true)
275 .setTimeUnitString(settings.timeUnitManager().timeUnitAsString())
281 * Command line module for a trajectory analysis module.
283 * \ingroup module_trajectoryanalysis
285 class TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule
286 : public CommandLineModuleInterface
290 * Constructs a module.
292 * \param[in] name Name for the module.
293 * \param[in] description One-line description for the module.
294 * \param[in] factory Factory method to create the analysis module.
296 * Does not throw. This is important for correct implementation of
299 RunnerCommandLineModule(const char *name, const char *description,
300 ModuleFactoryMethod factory)
301 : name_(name), description_(description), factory_(factory)
305 virtual const char *name() const { return name_; }
306 virtual const char *shortDescription() const { return description_; };
308 virtual void init(CommandLineModuleSettings *settings);
309 virtual int run(int argc, char *argv[]);
310 virtual void writeHelp(const CommandLineHelpContext &context) const;
314 const char *description_;
315 ModuleFactoryMethod factory_;
317 GMX_DISALLOW_COPY_AND_ASSIGN(RunnerCommandLineModule);
320 void TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule::init(
321 CommandLineModuleSettings * /*settings*/)
325 int TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule::run(
326 int argc, char *argv[])
328 TrajectoryAnalysisModulePointer module(factory_());
329 TrajectoryAnalysisCommandLineRunner runner(module.get());
330 return runner.run(argc, argv);
333 void TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule::writeHelp(
334 const CommandLineHelpContext &context) const
336 TrajectoryAnalysisModulePointer module(factory_());
337 TrajectoryAnalysisCommandLineRunner runner(module.get());
338 runner.writeHelp(context);
343 TrajectoryAnalysisCommandLineRunner::runAsMain(
344 int argc, char *argv[], ModuleFactoryMethod factory)
346 Impl::RunnerCommandLineModule module(NULL, NULL, factory);
347 return CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
352 TrajectoryAnalysisCommandLineRunner::registerModule(
353 CommandLineModuleManager *manager, const char *name,
354 const char *description, ModuleFactoryMethod factory)
356 CommandLineModulePointer module(
357 new Impl::RunnerCommandLineModule(name, description, factory));
358 manager->addModule(move(module));