Merge branch 'release-4-6'
[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
67 namespace gmx
68 {
69
70 /********************************************************************
71  * TrajectoryAnalysisCommandLineRunner::Impl
72  */
73
74 class TrajectoryAnalysisCommandLineRunner::Impl
75 {
76     public:
77         Impl(TrajectoryAnalysisModule *module);
78         ~Impl();
79
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[]);
87
88         TrajectoryAnalysisModule *module_;
89         int                       debugLevel_;
90         bool                      bPrintCopyright_;
91 };
92
93
94 TrajectoryAnalysisCommandLineRunner::Impl::Impl(
95         TrajectoryAnalysisModule *module)
96     : module_(module), debugLevel_(0), bPrintCopyright_(true)
97 {
98 }
99
100
101 TrajectoryAnalysisCommandLineRunner::Impl::~Impl()
102 {
103 }
104
105
106 void
107 TrajectoryAnalysisCommandLineRunner::Impl::printHelp(
108         const Options                        &options,
109         const TrajectoryAnalysisSettings     &settings,
110         const TrajectoryAnalysisRunnerCommon &common)
111 {
112     TrajectoryAnalysisRunnerCommon::HelpFlags flags = common.helpFlags();
113     if (flags != 0)
114     {
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())
121             .writeHelp(context);
122     }
123 }
124
125
126 bool
127 TrajectoryAnalysisCommandLineRunner::Impl::parseOptions(
128         TrajectoryAnalysisSettings *settings,
129         TrajectoryAnalysisRunnerCommon *common,
130         SelectionCollection *selections,
131         int *argc, char *argv[])
132 {
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);
140
141     options.addSubSection(&commonOptions);
142     options.addSubSection(&selectionOptions);
143     options.addSubSection(&moduleOptions);
144
145     SelectionOptionManager seloptManager(selections);
146     setManagerForSelectionOptions(&options, &seloptManager);
147
148     {
149         CommandLineParser  parser(&options);
150         try
151         {
152             parser.parse(argc, argv);
153         }
154         catch (const UserInputError &ex)
155         {
156             printHelp(options, *settings, *common);
157             throw;
158         }
159         printHelp(options, *settings, *common);
160         common->scaleTimeOptions(&options);
161         options.finish();
162     }
163
164     if (!common->optionsFinished(&commonOptions))
165     {
166         return false;
167     }
168     module_->optionsFinished(&moduleOptions, settings);
169
170     common->initIndexGroups(selections);
171
172     // TODO: Check whether the input is a pipe.
173     bool bInteractive = true;
174     seloptManager.parseRequestedFromStdin(bInteractive);
175     common->doneIndexGroups(selections);
176
177     return true;
178 }
179
180
181 /********************************************************************
182  * TrajectoryAnalysisCommandLineRunner
183  */
184
185 TrajectoryAnalysisCommandLineRunner::TrajectoryAnalysisCommandLineRunner(
186         TrajectoryAnalysisModule *module)
187     : impl_(new Impl(module))
188 {
189 }
190
191
192 TrajectoryAnalysisCommandLineRunner::~TrajectoryAnalysisCommandLineRunner()
193 {
194 }
195
196
197 void
198 TrajectoryAnalysisCommandLineRunner::setPrintCopyright(bool bPrint)
199 {
200     impl_->bPrintCopyright_ = bPrint;
201 }
202
203
204 void
205 TrajectoryAnalysisCommandLineRunner::setSelectionDebugLevel(int debuglevel)
206 {
207     impl_->debugLevel_ = 1;
208 }
209
210
211 int
212 TrajectoryAnalysisCommandLineRunner::run(int argc, char *argv[])
213 {
214     TrajectoryAnalysisModule *module = impl_->module_;
215
216     if (impl_->bPrintCopyright_)
217     {
218         CopyRight(stderr, argv[0]);
219     }
220
221     SelectionCollection  selections;
222     selections.setDebugLevel(impl_->debugLevel_);
223
224     TrajectoryAnalysisSettings      settings;
225     TrajectoryAnalysisRunnerCommon  common(&settings);
226
227     if (!impl_->parseOptions(&settings, &common, &selections, &argc, argv))
228     {
229         return 0;
230     }
231
232     common.initTopology(&selections);
233     selections.compile();
234
235     const TopologyInformation &topology = common.topologyInformation();
236     module->initAnalysis(settings, topology);
237
238     // Load first frame.
239     common.initFirstFrame();
240     module->initAfterFirstFrame(common.frame());
241
242     t_pbc  pbc;
243     t_pbc *ppbc = settings.hasPBC() ? &pbc : NULL;
244
245     int    nframes = 0;
246     AnalysisDataParallelOptions         dataOptions;
247     TrajectoryAnalysisModuleDataPointer pdata(
248             module->startFrames(dataOptions, selections));
249     do
250     {
251         common.initFrame();
252         t_trxframe &frame = common.frame();
253         if (ppbc != NULL)
254         {
255             set_pbc(ppbc, topology.ePBC(), frame.box);
256         }
257
258         selections.evaluate(&frame, ppbc);
259         module->analyzeFrame(nframes, frame, ppbc, pdata.get());
260
261         nframes++;
262     }
263     while (common.readNextFrame());
264     module->finishFrames(pdata.get());
265     if (pdata.get() != NULL)
266     {
267         pdata->finish();
268     }
269     pdata.reset();
270
271     if (common.hasTrajectory())
272     {
273         fprintf(stderr, "Analyzed %d frames, last time %.3f\n",
274                 nframes, common.frame().time);
275     }
276     else
277     {
278         fprintf(stderr, "Analyzed topology coordinates\n");
279     }
280
281     // Restore the maximal groups for dynamic selections.
282     selections.evaluateFinal(nframes);
283
284     module->finishAnalysis(nframes);
285     module->writeOutput();
286
287     return 0;
288 }
289
290
291 void
292 TrajectoryAnalysisCommandLineRunner::writeHelp(const HelpWriterContext &context)
293 {
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);
299
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");
304
305     impl_->module_->initOptions(&moduleOptions, &settings);
306     common.initOptions(&commonOptions);
307     selections.initOptions(&selectionOptions);
308
309     options.addSubSection(&commonOptions);
310     options.addSubSection(&selectionOptions);
311     options.addSubSection(&moduleOptions);
312
313     SelectionOptionManager seloptManager(&selections);
314     setManagerForSelectionOptions(&options, &seloptManager);
315
316     CommandLineHelpWriter(options)
317         .setShowDescriptions(true)
318         .setTimeUnitString(settings.timeUnitManager().timeUnitAsString())
319         .writeHelp(context);
320 }
321
322 } // namespace gmx