Uniform and less verbose startup for all binaries.
[alexxy/gromacs.git] / src / gromacs / trajectoryanalysis / cmdlinerunner.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements gmx::TrajectoryAnalysisCommandLineRunner.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_trajectoryanalysis
41  */
42 #include "cmdlinerunner.h"
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include "gromacs/legacyheaders/copyrite.h"
49 #include "gromacs/legacyheaders/pbc.h"
50 #include "gromacs/legacyheaders/rmpbc.h"
51 #include "gromacs/legacyheaders/statutil.h"
52
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"
67
68 namespace gmx
69 {
70
71 /********************************************************************
72  * TrajectoryAnalysisCommandLineRunner::Impl
73  */
74
75 class TrajectoryAnalysisCommandLineRunner::Impl
76 {
77     public:
78         Impl(TrajectoryAnalysisModule *module);
79         ~Impl();
80
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[]);
88
89         TrajectoryAnalysisModule *module_;
90         int                       debugLevel_;
91         bool                      bStandalone_;
92 };
93
94
95 TrajectoryAnalysisCommandLineRunner::Impl::Impl(
96         TrajectoryAnalysisModule *module)
97     : module_(module), debugLevel_(0), bStandalone_(true)
98 {
99 }
100
101
102 TrajectoryAnalysisCommandLineRunner::Impl::~Impl()
103 {
104 }
105
106
107 void
108 TrajectoryAnalysisCommandLineRunner::Impl::printHelp(
109         const Options                        &options,
110         const TrajectoryAnalysisSettings     &settings,
111         const TrajectoryAnalysisRunnerCommon &common)
112 {
113     TrajectoryAnalysisRunnerCommon::HelpFlags flags = common.helpFlags();
114     if (flags != 0)
115     {
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())
122             .writeHelp(context);
123     }
124 }
125
126
127 bool
128 TrajectoryAnalysisCommandLineRunner::Impl::parseOptions(
129         TrajectoryAnalysisSettings *settings,
130         TrajectoryAnalysisRunnerCommon *common,
131         SelectionCollection *selections,
132         int *argc, char *argv[])
133 {
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);
141
142     options.addSubSection(&commonOptions);
143     options.addSubSection(&selectionOptions);
144     options.addSubSection(&moduleOptions);
145
146     SelectionOptionManager seloptManager(selections);
147     setManagerForSelectionOptions(&options, &seloptManager);
148
149     {
150         CommandLineParser  parser(&options);
151         try
152         {
153             parser.parse(argc, argv);
154         }
155         catch (const UserInputError &ex)
156         {
157             printHelp(options, *settings, *common);
158             throw;
159         }
160         printHelp(options, *settings, *common);
161         common->scaleTimeOptions(&options);
162         options.finish();
163     }
164
165     if (!common->optionsFinished(&commonOptions))
166     {
167         return false;
168     }
169     module_->optionsFinished(&moduleOptions, settings);
170
171     common->initIndexGroups(selections);
172
173     // TODO: Check whether the input is a pipe.
174     bool bInteractive = true;
175     seloptManager.parseRequestedFromStdin(bInteractive);
176     common->doneIndexGroups(selections);
177
178     return true;
179 }
180
181
182 /********************************************************************
183  * TrajectoryAnalysisCommandLineRunner
184  */
185
186 TrajectoryAnalysisCommandLineRunner::TrajectoryAnalysisCommandLineRunner(
187         TrajectoryAnalysisModule *module)
188     : impl_(new Impl(module))
189 {
190 }
191
192
193 TrajectoryAnalysisCommandLineRunner::~TrajectoryAnalysisCommandLineRunner()
194 {
195 }
196
197
198 void
199 TrajectoryAnalysisCommandLineRunner::setStandalone(bool bStandalone)
200 {
201     impl_->bStandalone_ = bStandalone;
202 }
203
204
205 void
206 TrajectoryAnalysisCommandLineRunner::setSelectionDebugLevel(int debuglevel)
207 {
208     impl_->debugLevel_ = 1;
209 }
210
211
212 int
213 TrajectoryAnalysisCommandLineRunner::run(int argc, char *argv[])
214 {
215     TrajectoryAnalysisModule *module = impl_->module_;
216
217     if (impl_->bStandalone_)
218     {
219         printBinaryInformation(stderr, ProgramInfo::getInstance());
220     }
221
222     SelectionCollection  selections;
223     selections.setDebugLevel(impl_->debugLevel_);
224
225     TrajectoryAnalysisSettings      settings;
226     TrajectoryAnalysisRunnerCommon  common(&settings);
227
228     if (!impl_->parseOptions(&settings, &common, &selections, &argc, argv))
229     {
230         return 0;
231     }
232
233     common.initTopology(&selections);
234     selections.compile();
235
236     const TopologyInformation &topology = common.topologyInformation();
237     module->initAnalysis(settings, topology);
238
239     // Load first frame.
240     common.initFirstFrame();
241     module->initAfterFirstFrame(common.frame());
242
243     t_pbc  pbc;
244     t_pbc *ppbc = settings.hasPBC() ? &pbc : NULL;
245
246     int    nframes = 0;
247     AnalysisDataParallelOptions         dataOptions;
248     TrajectoryAnalysisModuleDataPointer pdata(
249             module->startFrames(dataOptions, selections));
250     do
251     {
252         common.initFrame();
253         t_trxframe &frame = common.frame();
254         if (ppbc != NULL)
255         {
256             set_pbc(ppbc, topology.ePBC(), frame.box);
257         }
258
259         selections.evaluate(&frame, ppbc);
260         module->analyzeFrame(nframes, frame, ppbc, pdata.get());
261
262         nframes++;
263     }
264     while (common.readNextFrame());
265     module->finishFrames(pdata.get());
266     if (pdata.get() != NULL)
267     {
268         pdata->finish();
269     }
270     pdata.reset();
271
272     if (common.hasTrajectory())
273     {
274         fprintf(stderr, "Analyzed %d frames, last time %.3f\n",
275                 nframes, common.frame().time);
276     }
277     else
278     {
279         fprintf(stderr, "Analyzed topology coordinates\n");
280     }
281
282     // Restore the maximal groups for dynamic selections.
283     selections.evaluateFinal(nframes);
284
285     module->finishAnalysis(nframes);
286     module->writeOutput();
287
288     if (impl_->bStandalone_)
289     {
290         gmx_thanx(stderr);
291     }
292
293     return 0;
294 }
295
296
297 void
298 TrajectoryAnalysisCommandLineRunner::writeHelp(const HelpWriterContext &context)
299 {
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);
305
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");
310
311     impl_->module_->initOptions(&moduleOptions, &settings);
312     common.initOptions(&commonOptions);
313     selections.initOptions(&selectionOptions);
314
315     options.addSubSection(&commonOptions);
316     options.addSubSection(&selectionOptions);
317     options.addSubSection(&moduleOptions);
318
319     SelectionOptionManager seloptManager(&selections);
320     setManagerForSelectionOptions(&options, &seloptManager);
321
322     CommandLineHelpWriter(options)
323         .setShowDescriptions(true)
324         .setTimeUnitString(settings.timeUnitManager().timeUnitAsString())
325         .writeHelp(context);
326 }
327
328 } // namespace gmx