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"
66 #include "gromacs/utility/programinfo.h"
71 /********************************************************************
72 * TrajectoryAnalysisCommandLineRunner::Impl
75 class TrajectoryAnalysisCommandLineRunner::Impl
78 Impl(TrajectoryAnalysisModule *module);
81 void printHelp(const Options &options,
82 const TrajectoryAnalysisSettings &settings,
83 const TrajectoryAnalysisRunnerCommon &common);
84 bool parseOptions(TrajectoryAnalysisSettings *settings,
85 TrajectoryAnalysisRunnerCommon *common,
86 SelectionCollection *selections,
87 int *argc, char *argv[]);
89 TrajectoryAnalysisModule *module_;
95 TrajectoryAnalysisCommandLineRunner::Impl::Impl(
96 TrajectoryAnalysisModule *module)
97 : module_(module), debugLevel_(0), bStandalone_(true)
102 TrajectoryAnalysisCommandLineRunner::Impl::~Impl()
108 TrajectoryAnalysisCommandLineRunner::Impl::printHelp(
109 const Options &options,
110 const TrajectoryAnalysisSettings &settings,
111 const TrajectoryAnalysisRunnerCommon &common)
113 TrajectoryAnalysisRunnerCommon::HelpFlags flags = common.helpFlags();
116 HelpWriterContext context(&File::standardError(),
117 eHelpOutputFormat_Console);
118 CommandLineHelpWriter(options)
119 .setShowDescriptions(flags & TrajectoryAnalysisRunnerCommon::efHelpShowDescriptions)
120 .setShowHidden(flags & TrajectoryAnalysisRunnerCommon::efHelpShowHidden)
121 .setTimeUnitString(settings.timeUnitManager().timeUnitAsString())
128 TrajectoryAnalysisCommandLineRunner::Impl::parseOptions(
129 TrajectoryAnalysisSettings *settings,
130 TrajectoryAnalysisRunnerCommon *common,
131 SelectionCollection *selections,
132 int *argc, char *argv[])
134 Options options(NULL, NULL);
135 Options moduleOptions(module_->name(), module_->description());
136 Options commonOptions("common", "Common analysis control");
137 Options selectionOptions("selection", "Common selection control");
138 module_->initOptions(&moduleOptions, settings);
139 common->initOptions(&commonOptions);
140 selections->initOptions(&selectionOptions);
142 options.addSubSection(&commonOptions);
143 options.addSubSection(&selectionOptions);
144 options.addSubSection(&moduleOptions);
146 SelectionOptionManager seloptManager(selections);
147 setManagerForSelectionOptions(&options, &seloptManager);
150 CommandLineParser parser(&options);
153 parser.parse(argc, argv);
155 catch (const UserInputError &ex)
157 printHelp(options, *settings, *common);
160 printHelp(options, *settings, *common);
161 common->scaleTimeOptions(&options);
165 if (!common->optionsFinished(&commonOptions))
169 module_->optionsFinished(&moduleOptions, settings);
171 common->initIndexGroups(selections);
173 // TODO: Check whether the input is a pipe.
174 bool bInteractive = true;
175 seloptManager.parseRequestedFromStdin(bInteractive);
176 common->doneIndexGroups(selections);
182 /********************************************************************
183 * TrajectoryAnalysisCommandLineRunner
186 TrajectoryAnalysisCommandLineRunner::TrajectoryAnalysisCommandLineRunner(
187 TrajectoryAnalysisModule *module)
188 : impl_(new Impl(module))
193 TrajectoryAnalysisCommandLineRunner::~TrajectoryAnalysisCommandLineRunner()
199 TrajectoryAnalysisCommandLineRunner::setStandalone(bool bStandalone)
201 impl_->bStandalone_ = bStandalone;
206 TrajectoryAnalysisCommandLineRunner::setSelectionDebugLevel(int debuglevel)
208 impl_->debugLevel_ = 1;
213 TrajectoryAnalysisCommandLineRunner::run(int argc, char *argv[])
215 TrajectoryAnalysisModule *module = impl_->module_;
217 if (impl_->bStandalone_)
219 printBinaryInformation(stderr, ProgramInfo::getInstance());
222 SelectionCollection selections;
223 selections.setDebugLevel(impl_->debugLevel_);
225 TrajectoryAnalysisSettings settings;
226 TrajectoryAnalysisRunnerCommon common(&settings);
228 if (!impl_->parseOptions(&settings, &common, &selections, &argc, argv))
233 common.initTopology(&selections);
234 selections.compile();
236 const TopologyInformation &topology = common.topologyInformation();
237 module->initAnalysis(settings, topology);
240 common.initFirstFrame();
241 module->initAfterFirstFrame(common.frame());
244 t_pbc *ppbc = settings.hasPBC() ? &pbc : NULL;
247 AnalysisDataParallelOptions dataOptions;
248 TrajectoryAnalysisModuleDataPointer pdata(
249 module->startFrames(dataOptions, selections));
253 t_trxframe &frame = common.frame();
256 set_pbc(ppbc, topology.ePBC(), frame.box);
259 selections.evaluate(&frame, ppbc);
260 module->analyzeFrame(nframes, frame, ppbc, pdata.get());
264 while (common.readNextFrame());
265 module->finishFrames(pdata.get());
266 if (pdata.get() != NULL)
272 if (common.hasTrajectory())
274 fprintf(stderr, "Analyzed %d frames, last time %.3f\n",
275 nframes, common.frame().time);
279 fprintf(stderr, "Analyzed topology coordinates\n");
282 // Restore the maximal groups for dynamic selections.
283 selections.evaluateFinal(nframes);
285 module->finishAnalysis(nframes);
286 module->writeOutput();
288 if (impl_->bStandalone_)
298 TrajectoryAnalysisCommandLineRunner::writeHelp(const HelpWriterContext &context)
300 // TODO: This method duplicates some code from run() and Impl::printHelp().
301 // See how to best refactor it to share the common code.
302 SelectionCollection selections;
303 TrajectoryAnalysisSettings settings;
304 TrajectoryAnalysisRunnerCommon common(&settings);
306 Options options(NULL, NULL);
307 Options moduleOptions(impl_->module_->name(), impl_->module_->description());
308 Options commonOptions("common", "Common analysis control");
309 Options selectionOptions("selection", "Common selection control");
311 impl_->module_->initOptions(&moduleOptions, &settings);
312 common.initOptions(&commonOptions);
313 selections.initOptions(&selectionOptions);
315 options.addSubSection(&commonOptions);
316 options.addSubSection(&selectionOptions);
317 options.addSubSection(&moduleOptions);
319 SelectionOptionManager seloptManager(&selections);
320 setManagerForSelectionOptions(&options, &seloptManager);
322 CommandLineHelpWriter(options)
323 .setShowDescriptions(true)
324 .setTimeUnitString(settings.timeUnitManager().timeUnitAsString())