3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements gmx::TrajectoryAnalysisCommandLineRunner.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_trajectoryanalysis
38 #include "cmdlinerunner.h"
44 #include "gromacs/legacyheaders/copyrite.h"
45 #include "gromacs/legacyheaders/pbc.h"
46 #include "gromacs/legacyheaders/rmpbc.h"
47 #include "gromacs/legacyheaders/statutil.h"
49 #include "gromacs/analysisdata/paralleloptions.h"
50 #include "gromacs/commandline/cmdlinehelpwriter.h"
51 #include "gromacs/commandline/cmdlineparser.h"
52 #include "gromacs/onlinehelp/helpwritercontext.h"
53 #include "gromacs/options/options.h"
54 #include "gromacs/selection/selectioncollection.h"
55 #include "gromacs/selection/selectionoptionmanager.h"
56 #include "gromacs/trajectoryanalysis/analysismodule.h"
57 #include "gromacs/trajectoryanalysis/analysissettings.h"
58 #include "gromacs/trajectoryanalysis/runnercommon.h"
59 #include "gromacs/utility/exceptions.h"
60 #include "gromacs/utility/file.h"
61 #include "gromacs/utility/gmxassert.h"
66 /********************************************************************
67 * TrajectoryAnalysisCommandLineRunner::Impl
70 class TrajectoryAnalysisCommandLineRunner::Impl
73 Impl(TrajectoryAnalysisModule *module);
76 void printHelp(const Options &options,
77 const TrajectoryAnalysisSettings &settings,
78 const TrajectoryAnalysisRunnerCommon &common);
79 bool parseOptions(TrajectoryAnalysisSettings *settings,
80 TrajectoryAnalysisRunnerCommon *common,
81 SelectionCollection *selections,
82 int *argc, char *argv[]);
84 TrajectoryAnalysisModule *module_;
86 bool bPrintCopyright_;
90 TrajectoryAnalysisCommandLineRunner::Impl::Impl(
91 TrajectoryAnalysisModule *module)
92 : module_(module), debugLevel_(0), bPrintCopyright_(true)
97 TrajectoryAnalysisCommandLineRunner::Impl::~Impl()
103 TrajectoryAnalysisCommandLineRunner::Impl::printHelp(
104 const Options &options,
105 const TrajectoryAnalysisSettings &settings,
106 const TrajectoryAnalysisRunnerCommon &common)
108 TrajectoryAnalysisRunnerCommon::HelpFlags flags = common.helpFlags();
111 HelpWriterContext context(&File::standardError(),
112 eHelpOutputFormat_Console);
113 CommandLineHelpWriter(options)
114 .setShowDescriptions(flags & TrajectoryAnalysisRunnerCommon::efHelpShowDescriptions)
115 .setShowHidden(flags & TrajectoryAnalysisRunnerCommon::efHelpShowHidden)
116 .setTimeUnitString(settings.timeUnitManager().timeUnitAsString())
123 TrajectoryAnalysisCommandLineRunner::Impl::parseOptions(
124 TrajectoryAnalysisSettings *settings,
125 TrajectoryAnalysisRunnerCommon *common,
126 SelectionCollection *selections,
127 int *argc, char *argv[])
129 Options options(NULL, NULL);
130 Options moduleOptions(module_->name(), module_->description());
131 Options commonOptions("common", "Common analysis control");
132 Options selectionOptions("selection", "Common selection control");
133 module_->initOptions(&moduleOptions, settings);
134 common->initOptions(&commonOptions);
135 selections->initOptions(&selectionOptions);
137 options.addSubSection(&commonOptions);
138 options.addSubSection(&selectionOptions);
139 options.addSubSection(&moduleOptions);
141 SelectionOptionManager seloptManager(selections);
142 setManagerForSelectionOptions(&options, &seloptManager);
145 CommandLineParser parser(&options);
148 parser.parse(argc, argv);
150 catch (const UserInputError &ex)
152 printHelp(options, *settings, *common);
155 printHelp(options, *settings, *common);
156 common->scaleTimeOptions(&options);
160 if (!common->optionsFinished(&commonOptions))
164 module_->optionsFinished(&moduleOptions, settings);
166 common->initIndexGroups(selections);
168 // TODO: Check whether the input is a pipe.
169 bool bInteractive = true;
170 seloptManager.parseRequestedFromStdin(bInteractive);
171 common->doneIndexGroups(selections);
177 /********************************************************************
178 * TrajectoryAnalysisCommandLineRunner
181 TrajectoryAnalysisCommandLineRunner::TrajectoryAnalysisCommandLineRunner(
182 TrajectoryAnalysisModule *module)
183 : impl_(new Impl(module))
188 TrajectoryAnalysisCommandLineRunner::~TrajectoryAnalysisCommandLineRunner()
194 TrajectoryAnalysisCommandLineRunner::setPrintCopyright(bool bPrint)
196 impl_->bPrintCopyright_ = bPrint;
201 TrajectoryAnalysisCommandLineRunner::setSelectionDebugLevel(int debuglevel)
203 impl_->debugLevel_ = 1;
208 TrajectoryAnalysisCommandLineRunner::run(int argc, char *argv[])
210 TrajectoryAnalysisModule *module = impl_->module_;
212 if (impl_->bPrintCopyright_)
214 CopyRight(stderr, argv[0]);
217 SelectionCollection selections;
218 selections.setDebugLevel(impl_->debugLevel_);
220 TrajectoryAnalysisSettings settings;
221 TrajectoryAnalysisRunnerCommon common(&settings);
223 if (!impl_->parseOptions(&settings, &common, &selections, &argc, argv))
228 common.initTopology(&selections);
229 selections.compile();
231 const TopologyInformation &topology = common.topologyInformation();
232 module->initAnalysis(settings, topology);
235 common.initFirstFrame();
236 module->initAfterFirstFrame(common.frame());
239 t_pbc *ppbc = settings.hasPBC() ? &pbc : NULL;
242 AnalysisDataParallelOptions dataOptions;
243 TrajectoryAnalysisModuleDataPointer pdata(
244 module->startFrames(dataOptions, selections));
248 t_trxframe &frame = common.frame();
251 set_pbc(ppbc, topology.ePBC(), frame.box);
254 selections.evaluate(&frame, ppbc);
255 module->analyzeFrame(nframes, frame, ppbc, pdata.get());
259 while (common.readNextFrame());
260 module->finishFrames(pdata.get());
261 if (pdata.get() != NULL)
267 if (common.hasTrajectory())
269 fprintf(stderr, "Analyzed %d frames, last time %.3f\n",
270 nframes, common.frame().time);
274 fprintf(stderr, "Analyzed topology coordinates\n");
277 // Restore the maximal groups for dynamic selections.
278 selections.evaluateFinal(nframes);
280 module->finishAnalysis(nframes);
281 module->writeOutput();
288 TrajectoryAnalysisCommandLineRunner::writeHelp(const HelpWriterContext &context)
290 // TODO: This method duplicates some code from run() and Impl::printHelp().
291 // See how to best refactor it to share the common code.
292 SelectionCollection selections;
293 TrajectoryAnalysisSettings settings;
294 TrajectoryAnalysisRunnerCommon common(&settings);
296 Options options(NULL, NULL);
297 Options moduleOptions(impl_->module_->name(), impl_->module_->description());
298 Options commonOptions("common", "Common analysis control");
299 Options selectionOptions("selection", "Common selection control");
301 impl_->module_->initOptions(&moduleOptions, &settings);
302 common.initOptions(&commonOptions);
303 selections.initOptions(&selectionOptions);
305 options.addSubSection(&commonOptions);
306 options.addSubSection(&selectionOptions);
307 options.addSubSection(&moduleOptions);
309 SelectionOptionManager seloptManager(&selections);
310 setManagerForSelectionOptions(&options, &seloptManager);
312 CommandLineHelpWriter(options)
313 .setShowDescriptions(true)
314 .setTimeUnitString(settings.timeUnitManager().timeUnitAsString())