2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * 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
42 #include "cmdlinerunner.h"
48 #include "gromacs/legacyheaders/copyrite.h"
49 #include "gromacs/legacyheaders/pbc.h"
50 #include "gromacs/legacyheaders/rmpbc.h"
51 #include "gromacs/legacyheaders/statutil.h"
53 #include "gromacs/analysisdata/paralleloptions.h"
54 #include "gromacs/commandline/cmdlinehelpwriter.h"
55 #include "gromacs/commandline/cmdlineparser.h"
56 #include "gromacs/onlinehelp/helpwritercontext.h"
57 #include "gromacs/options/options.h"
58 #include "gromacs/selection/selectioncollection.h"
59 #include "gromacs/selection/selectionoptionmanager.h"
60 #include "gromacs/trajectoryanalysis/analysismodule.h"
61 #include "gromacs/trajectoryanalysis/analysissettings.h"
62 #include "gromacs/trajectoryanalysis/runnercommon.h"
63 #include "gromacs/utility/exceptions.h"
64 #include "gromacs/utility/file.h"
65 #include "gromacs/utility/gmxassert.h"
70 /********************************************************************
71 * TrajectoryAnalysisCommandLineRunner::Impl
74 class TrajectoryAnalysisCommandLineRunner::Impl
77 Impl(TrajectoryAnalysisModule *module);
80 void printHelp(const Options &options,
81 const TrajectoryAnalysisSettings &settings,
82 const TrajectoryAnalysisRunnerCommon &common);
83 bool parseOptions(TrajectoryAnalysisSettings *settings,
84 TrajectoryAnalysisRunnerCommon *common,
85 SelectionCollection *selections,
86 int *argc, char *argv[]);
88 TrajectoryAnalysisModule *module_;
90 bool bPrintCopyright_;
94 TrajectoryAnalysisCommandLineRunner::Impl::Impl(
95 TrajectoryAnalysisModule *module)
96 : module_(module), debugLevel_(0), bPrintCopyright_(true)
101 TrajectoryAnalysisCommandLineRunner::Impl::~Impl()
107 TrajectoryAnalysisCommandLineRunner::Impl::printHelp(
108 const Options &options,
109 const TrajectoryAnalysisSettings &settings,
110 const TrajectoryAnalysisRunnerCommon &common)
112 TrajectoryAnalysisRunnerCommon::HelpFlags flags = common.helpFlags();
115 HelpWriterContext context(&File::standardError(),
116 eHelpOutputFormat_Console);
117 CommandLineHelpWriter(options)
118 .setShowDescriptions(flags & TrajectoryAnalysisRunnerCommon::efHelpShowDescriptions)
119 .setShowHidden(flags & TrajectoryAnalysisRunnerCommon::efHelpShowHidden)
120 .setTimeUnitString(settings.timeUnitManager().timeUnitAsString())
127 TrajectoryAnalysisCommandLineRunner::Impl::parseOptions(
128 TrajectoryAnalysisSettings *settings,
129 TrajectoryAnalysisRunnerCommon *common,
130 SelectionCollection *selections,
131 int *argc, char *argv[])
133 Options options(NULL, NULL);
134 Options moduleOptions(module_->name(), module_->description());
135 Options commonOptions("common", "Common analysis control");
136 Options selectionOptions("selection", "Common selection control");
137 module_->initOptions(&moduleOptions, settings);
138 common->initOptions(&commonOptions);
139 selections->initOptions(&selectionOptions);
141 options.addSubSection(&commonOptions);
142 options.addSubSection(&selectionOptions);
143 options.addSubSection(&moduleOptions);
145 SelectionOptionManager seloptManager(selections);
146 setManagerForSelectionOptions(&options, &seloptManager);
149 CommandLineParser parser(&options);
152 parser.parse(argc, argv);
154 catch (const UserInputError &ex)
156 printHelp(options, *settings, *common);
159 printHelp(options, *settings, *common);
160 common->scaleTimeOptions(&options);
164 if (!common->optionsFinished(&commonOptions))
168 module_->optionsFinished(&moduleOptions, settings);
170 common->initIndexGroups(selections);
172 // TODO: Check whether the input is a pipe.
173 bool bInteractive = true;
174 seloptManager.parseRequestedFromStdin(bInteractive);
175 common->doneIndexGroups(selections);
181 /********************************************************************
182 * TrajectoryAnalysisCommandLineRunner
185 TrajectoryAnalysisCommandLineRunner::TrajectoryAnalysisCommandLineRunner(
186 TrajectoryAnalysisModule *module)
187 : impl_(new Impl(module))
192 TrajectoryAnalysisCommandLineRunner::~TrajectoryAnalysisCommandLineRunner()
198 TrajectoryAnalysisCommandLineRunner::setPrintCopyright(bool bPrint)
200 impl_->bPrintCopyright_ = bPrint;
205 TrajectoryAnalysisCommandLineRunner::setSelectionDebugLevel(int debuglevel)
207 impl_->debugLevel_ = 1;
212 TrajectoryAnalysisCommandLineRunner::run(int argc, char *argv[])
214 TrajectoryAnalysisModule *module = impl_->module_;
216 if (impl_->bPrintCopyright_)
218 CopyRight(stderr, argv[0]);
221 SelectionCollection selections;
222 selections.setDebugLevel(impl_->debugLevel_);
224 TrajectoryAnalysisSettings settings;
225 TrajectoryAnalysisRunnerCommon common(&settings);
227 if (!impl_->parseOptions(&settings, &common, &selections, &argc, argv))
232 common.initTopology(&selections);
233 selections.compile();
235 const TopologyInformation &topology = common.topologyInformation();
236 module->initAnalysis(settings, topology);
239 common.initFirstFrame();
240 module->initAfterFirstFrame(common.frame());
243 t_pbc *ppbc = settings.hasPBC() ? &pbc : NULL;
246 AnalysisDataParallelOptions dataOptions;
247 TrajectoryAnalysisModuleDataPointer pdata(
248 module->startFrames(dataOptions, selections));
252 t_trxframe &frame = common.frame();
255 set_pbc(ppbc, topology.ePBC(), frame.box);
258 selections.evaluate(&frame, ppbc);
259 module->analyzeFrame(nframes, frame, ppbc, pdata.get());
263 while (common.readNextFrame());
264 module->finishFrames(pdata.get());
265 if (pdata.get() != NULL)
271 if (common.hasTrajectory())
273 fprintf(stderr, "Analyzed %d frames, last time %.3f\n",
274 nframes, common.frame().time);
278 fprintf(stderr, "Analyzed topology coordinates\n");
281 // Restore the maximal groups for dynamic selections.
282 selections.evaluateFinal(nframes);
284 module->finishAnalysis(nframes);
285 module->writeOutput();
292 TrajectoryAnalysisCommandLineRunner::writeHelp(const HelpWriterContext &context)
294 // TODO: This method duplicates some code from run() and Impl::printHelp().
295 // See how to best refactor it to share the common code.
296 SelectionCollection selections;
297 TrajectoryAnalysisSettings settings;
298 TrajectoryAnalysisRunnerCommon common(&settings);
300 Options options(NULL, NULL);
301 Options moduleOptions(impl_->module_->name(), impl_->module_->description());
302 Options commonOptions("common", "Common analysis control");
303 Options selectionOptions("selection", "Common selection control");
305 impl_->module_->initOptions(&moduleOptions, &settings);
306 common.initOptions(&commonOptions);
307 selections.initOptions(&selectionOptions);
309 options.addSubSection(&commonOptions);
310 options.addSubSection(&selectionOptions);
311 options.addSubSection(&moduleOptions);
313 SelectionOptionManager seloptManager(&selections);
314 setManagerForSelectionOptions(&options, &seloptManager);
316 CommandLineHelpWriter(options)
317 .setShowDescriptions(true)
318 .setTimeUnitString(settings.timeUnitManager().timeUnitAsString())