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/utility/exceptions.h"
61 #include "gromacs/utility/file.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[]);
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[])
111 FileNameOptionManager fileoptManager;
112 SelectionOptionManager seloptManager(selections);
113 Options options(NULL, NULL);
114 Options moduleOptions(module_->name(), module_->description());
115 Options commonOptions("common", "Common analysis control");
116 Options selectionOptions("selection", "Common selection control");
118 options.addManager(&fileoptManager);
119 options.addManager(&seloptManager);
120 options.addSubSection(&commonOptions);
121 options.addSubSection(&selectionOptions);
122 options.addSubSection(&moduleOptions);
124 module_->initOptions(&moduleOptions, settings);
125 common->initOptions(&commonOptions);
126 selections->initOptions(&selectionOptions);
129 CommandLineParser parser(&options);
130 // TODO: Print the help if user provides an invalid option?
131 // Or just add a message advising the user to invoke the help?
132 parser.parse(argc, argv);
133 common->scaleTimeOptions(&options);
137 common->optionsFinished(&commonOptions);
138 module_->optionsFinished(&moduleOptions, settings);
140 common->initIndexGroups(selections, bUseDefaultGroups_);
142 const bool bInteractive = File::standardInput().isInteractive();
143 seloptManager.parseRequestedFromStdin(bInteractive);
144 common->doneIndexGroups(selections);
146 common->initTopology(selections);
147 selections->compile();
151 /********************************************************************
152 * TrajectoryAnalysisCommandLineRunner
155 TrajectoryAnalysisCommandLineRunner::TrajectoryAnalysisCommandLineRunner(
156 TrajectoryAnalysisModule *module)
157 : impl_(new Impl(module))
162 TrajectoryAnalysisCommandLineRunner::~TrajectoryAnalysisCommandLineRunner()
168 TrajectoryAnalysisCommandLineRunner::setUseDefaultGroups(bool bUseDefaults)
170 impl_->bUseDefaultGroups_ = bUseDefaults;
175 TrajectoryAnalysisCommandLineRunner::setSelectionDebugLevel(int debuglevel)
177 impl_->debugLevel_ = debuglevel;
182 TrajectoryAnalysisCommandLineRunner::run(int argc, char *argv[])
184 TrajectoryAnalysisModule *module = impl_->module_;
186 SelectionCollection selections;
187 selections.setDebugLevel(impl_->debugLevel_);
189 TrajectoryAnalysisSettings settings;
190 TrajectoryAnalysisRunnerCommon common(&settings);
192 impl_->parseOptions(&settings, &common, &selections, &argc, argv);
194 const TopologyInformation &topology = common.topologyInformation();
195 module->initAnalysis(settings, topology);
198 common.initFirstFrame();
199 module->initAfterFirstFrame(settings, common.frame());
202 t_pbc *ppbc = settings.hasPBC() ? &pbc : NULL;
205 AnalysisDataParallelOptions dataOptions;
206 TrajectoryAnalysisModuleDataPointer pdata(
207 module->startFrames(dataOptions, selections));
211 t_trxframe &frame = common.frame();
214 set_pbc(ppbc, topology.ePBC(), frame.box);
217 selections.evaluate(&frame, ppbc);
218 module->analyzeFrame(nframes, frame, ppbc, pdata.get());
222 while (common.readNextFrame());
223 module->finishFrames(pdata.get());
224 if (pdata.get() != NULL)
230 if (common.hasTrajectory())
232 fprintf(stderr, "Analyzed %d frames, last time %.3f\n",
233 nframes, common.frame().time);
237 fprintf(stderr, "Analyzed topology coordinates\n");
240 // Restore the maximal groups for dynamic selections.
241 selections.evaluateFinal(nframes);
243 module->finishAnalysis(nframes);
244 module->writeOutput();
251 TrajectoryAnalysisCommandLineRunner::writeHelp(const CommandLineHelpContext &context)
253 // TODO: This method duplicates some code from run().
254 // See how to best refactor it to share the common code.
255 SelectionCollection selections;
256 TrajectoryAnalysisSettings settings;
257 TrajectoryAnalysisRunnerCommon common(&settings);
259 SelectionOptionManager seloptManager(&selections);
260 Options options(NULL, NULL);
261 Options moduleOptions(impl_->module_->name(), impl_->module_->description());
262 Options commonOptions("common", "Common analysis control");
263 Options selectionOptions("selection", "Common selection control");
265 options.addManager(&seloptManager);
266 options.addSubSection(&commonOptions);
267 options.addSubSection(&selectionOptions);
268 options.addSubSection(&moduleOptions);
270 impl_->module_->initOptions(&moduleOptions, &settings);
271 common.initOptions(&commonOptions);
272 selections.initOptions(&selectionOptions);
274 CommandLineHelpWriter(options)
275 .setShowDescriptions(true)
276 .setTimeUnitString(settings.timeUnitManager().timeUnitAsString())
282 * Command line module for a trajectory analysis module.
284 * \ingroup module_trajectoryanalysis
286 class TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule
287 : public CommandLineModuleInterface
291 * Constructs a module.
293 * \param[in] name Name for the module.
294 * \param[in] description One-line description for the module.
295 * \param[in] factory Factory method to create the analysis module.
297 * Does not throw. This is important for correct implementation of
300 RunnerCommandLineModule(const char *name, const char *description,
301 ModuleFactoryMethod factory)
302 : name_(name), description_(description), factory_(factory)
306 virtual const char *name() const { return name_; }
307 virtual const char *shortDescription() const { return description_; };
309 virtual void init(CommandLineModuleSettings *settings);
310 virtual int run(int argc, char *argv[]);
311 virtual void writeHelp(const CommandLineHelpContext &context) const;
315 const char *description_;
316 ModuleFactoryMethod factory_;
318 GMX_DISALLOW_COPY_AND_ASSIGN(RunnerCommandLineModule);
321 void TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule::init(
322 CommandLineModuleSettings * /*settings*/)
326 int TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule::run(
327 int argc, char *argv[])
329 TrajectoryAnalysisModulePointer module(factory_());
330 TrajectoryAnalysisCommandLineRunner runner(module.get());
331 return runner.run(argc, argv);
334 void TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule::writeHelp(
335 const CommandLineHelpContext &context) const
337 TrajectoryAnalysisModulePointer module(factory_());
338 TrajectoryAnalysisCommandLineRunner runner(module.get());
339 runner.writeHelp(context);
344 TrajectoryAnalysisCommandLineRunner::runAsMain(
345 int argc, char *argv[], ModuleFactoryMethod factory)
347 Impl::RunnerCommandLineModule module(NULL, NULL, factory);
348 return CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
353 TrajectoryAnalysisCommandLineRunner::registerModule(
354 CommandLineModuleManager *manager, const char *name,
355 const char *description, ModuleFactoryMethod factory)
357 CommandLineModulePointer module(
358 new Impl::RunnerCommandLineModule(name, description, factory));
359 manager->addModule(move(module));