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,2013, 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/pbc.h"
49 #include "gromacs/legacyheaders/rmpbc.h"
50 #include "gromacs/legacyheaders/statutil.h"
51
52 #include "gromacs/analysisdata/paralleloptions.h"
53 #include "gromacs/commandline/cmdlinehelpcontext.h"
54 #include "gromacs/commandline/cmdlinehelpwriter.h"
55 #include "gromacs/commandline/cmdlinemodule.h"
56 #include "gromacs/commandline/cmdlinemodulemanager.h"
57 #include "gromacs/commandline/cmdlineparser.h"
58 #include "gromacs/options/options.h"
59 #include "gromacs/selection/selectioncollection.h"
60 #include "gromacs/selection/selectionoptionmanager.h"
61 #include "gromacs/trajectoryanalysis/analysismodule.h"
62 #include "gromacs/trajectoryanalysis/analysissettings.h"
63 #include "gromacs/trajectoryanalysis/runnercommon.h"
64 #include "gromacs/utility/exceptions.h"
65 #include "gromacs/utility/file.h"
66 #include "gromacs/utility/gmxassert.h"
67 #include "gromacs/utility/programinfo.h"
68
69 namespace gmx
70 {
71
72 /********************************************************************
73  * TrajectoryAnalysisCommandLineRunner::Impl
74  */
75
76 class TrajectoryAnalysisCommandLineRunner::Impl
77 {
78     public:
79         class RunnerCommandLineModule;
80
81         Impl(TrajectoryAnalysisModule *module);
82         ~Impl();
83
84         bool parseOptions(TrajectoryAnalysisSettings *settings,
85                           TrajectoryAnalysisRunnerCommon *common,
86                           SelectionCollection *selections,
87                           int *argc, char *argv[]);
88
89         TrajectoryAnalysisModule *module_;
90         int                       debugLevel_;
91 };
92
93
94 TrajectoryAnalysisCommandLineRunner::Impl::Impl(
95         TrajectoryAnalysisModule *module)
96     : module_(module), debugLevel_(0)
97 {
98 }
99
100
101 TrajectoryAnalysisCommandLineRunner::Impl::~Impl()
102 {
103 }
104
105
106 bool
107 TrajectoryAnalysisCommandLineRunner::Impl::parseOptions(
108         TrajectoryAnalysisSettings *settings,
109         TrajectoryAnalysisRunnerCommon *common,
110         SelectionCollection *selections,
111         int *argc, char *argv[])
112 {
113     Options options(NULL, NULL);
114     Options moduleOptions(module_->name(), module_->description());
115     Options commonOptions("common", "Common analysis control");
116     Options selectionOptions("selection", "Common selection control");
117     module_->initOptions(&moduleOptions, settings);
118     common->initOptions(&commonOptions);
119     selections->initOptions(&selectionOptions);
120
121     options.addSubSection(&commonOptions);
122     options.addSubSection(&selectionOptions);
123     options.addSubSection(&moduleOptions);
124
125     SelectionOptionManager seloptManager(selections);
126     setManagerForSelectionOptions(&options, &seloptManager);
127
128     {
129         CommandLineParser  parser(&options);
130         // TODO: Print the help if user provides an invalid option?
131         // Or just add a message advising the user to invoke the help?
132         parser.parse(argc, argv);
133         common->scaleTimeOptions(&options);
134         options.finish();
135     }
136
137     common->optionsFinished(&commonOptions);
138     module_->optionsFinished(&moduleOptions, settings);
139
140     common->initIndexGroups(selections);
141
142     // TODO: Check whether the input is a pipe.
143     bool bInteractive = true;
144     seloptManager.parseRequestedFromStdin(bInteractive);
145     common->doneIndexGroups(selections);
146
147     return true;
148 }
149
150
151 /********************************************************************
152  * TrajectoryAnalysisCommandLineRunner
153  */
154
155 TrajectoryAnalysisCommandLineRunner::TrajectoryAnalysisCommandLineRunner(
156         TrajectoryAnalysisModule *module)
157     : impl_(new Impl(module))
158 {
159 }
160
161
162 TrajectoryAnalysisCommandLineRunner::~TrajectoryAnalysisCommandLineRunner()
163 {
164 }
165
166
167 void
168 TrajectoryAnalysisCommandLineRunner::setSelectionDebugLevel(int debuglevel)
169 {
170     impl_->debugLevel_ = 1;
171 }
172
173
174 int
175 TrajectoryAnalysisCommandLineRunner::run(int argc, char *argv[])
176 {
177     TrajectoryAnalysisModule *module = impl_->module_;
178
179     SelectionCollection       selections;
180     selections.setDebugLevel(impl_->debugLevel_);
181
182     TrajectoryAnalysisSettings      settings;
183     TrajectoryAnalysisRunnerCommon  common(&settings);
184
185     if (!impl_->parseOptions(&settings, &common, &selections, &argc, argv))
186     {
187         return 0;
188     }
189
190     common.initTopology(&selections);
191     selections.compile();
192
193     const TopologyInformation &topology = common.topologyInformation();
194     module->initAnalysis(settings, topology);
195
196     // Load first frame.
197     common.initFirstFrame();
198     module->initAfterFirstFrame(common.frame());
199
200     t_pbc  pbc;
201     t_pbc *ppbc = settings.hasPBC() ? &pbc : NULL;
202
203     int    nframes = 0;
204     AnalysisDataParallelOptions         dataOptions;
205     TrajectoryAnalysisModuleDataPointer pdata(
206             module->startFrames(dataOptions, selections));
207     do
208     {
209         common.initFrame();
210         t_trxframe &frame = common.frame();
211         if (ppbc != NULL)
212         {
213             set_pbc(ppbc, topology.ePBC(), frame.box);
214         }
215
216         selections.evaluate(&frame, ppbc);
217         module->analyzeFrame(nframes, frame, ppbc, pdata.get());
218
219         nframes++;
220     }
221     while (common.readNextFrame());
222     module->finishFrames(pdata.get());
223     if (pdata.get() != NULL)
224     {
225         pdata->finish();
226     }
227     pdata.reset();
228
229     if (common.hasTrajectory())
230     {
231         fprintf(stderr, "Analyzed %d frames, last time %.3f\n",
232                 nframes, common.frame().time);
233     }
234     else
235     {
236         fprintf(stderr, "Analyzed topology coordinates\n");
237     }
238
239     // Restore the maximal groups for dynamic selections.
240     selections.evaluateFinal(nframes);
241
242     module->finishAnalysis(nframes);
243     module->writeOutput();
244
245     return 0;
246 }
247
248
249 void
250 TrajectoryAnalysisCommandLineRunner::writeHelp(const CommandLineHelpContext &context)
251 {
252     // TODO: This method duplicates some code from run().
253     // See how to best refactor it to share the common code.
254     SelectionCollection             selections;
255     TrajectoryAnalysisSettings      settings;
256     TrajectoryAnalysisRunnerCommon  common(&settings);
257
258     Options options(NULL, NULL);
259     Options moduleOptions(impl_->module_->name(), impl_->module_->description());
260     Options commonOptions("common", "Common analysis control");
261     Options selectionOptions("selection", "Common selection control");
262
263     impl_->module_->initOptions(&moduleOptions, &settings);
264     common.initOptions(&commonOptions);
265     selections.initOptions(&selectionOptions);
266
267     options.addSubSection(&commonOptions);
268     options.addSubSection(&selectionOptions);
269     options.addSubSection(&moduleOptions);
270
271     SelectionOptionManager seloptManager(&selections);
272     setManagerForSelectionOptions(&options, &seloptManager);
273
274     CommandLineHelpWriter(options)
275         .setShowDescriptions(true)
276         .setTimeUnitString(settings.timeUnitManager().timeUnitAsString())
277         .writeHelp(context);
278 }
279
280
281 /*! \internal \brief
282  * Command line module for a trajectory analysis module.
283  *
284  * \ingroup module_trajectoryanalysis
285  */
286 class TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule
287     : public CommandLineModuleInterface
288 {
289     public:
290         /*! \brief
291          * Constructs a module.
292          *
293          * \param[in] name         Name for the module.
294          * \param[in] description  One-line description for the module.
295          * \param[in] factory      Factory method to create the analysis module.
296          *
297          * Does not throw.  This is important for correct implementation of
298          * runAsMain().
299          */
300         RunnerCommandLineModule(const char *name, const char *description,
301                                 ModuleFactoryMethod factory)
302             : name_(name), description_(description), factory_(factory)
303         {
304         }
305
306         virtual const char *name() const { return name_; }
307         virtual const char *shortDescription() const { return description_; };
308
309         virtual int run(int argc, char *argv[]);
310         virtual void writeHelp(const CommandLineHelpContext &context) const;
311
312     private:
313         const char             *name_;
314         const char             *description_;
315         ModuleFactoryMethod     factory_;
316
317         GMX_DISALLOW_COPY_AND_ASSIGN(RunnerCommandLineModule);
318 };
319
320 int TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule::run(
321         int argc, char *argv[])
322 {
323     TrajectoryAnalysisModulePointer     module(factory_());
324     TrajectoryAnalysisCommandLineRunner runner(module.get());
325     return runner.run(argc, argv);
326 }
327
328 void TrajectoryAnalysisCommandLineRunner::Impl::RunnerCommandLineModule::writeHelp(
329         const CommandLineHelpContext &context) const
330 {
331     // TODO: Implement #969.
332     if (context.writerContext().outputFormat() != eHelpOutputFormat_Console)
333     {
334         return;
335     }
336     TrajectoryAnalysisModulePointer     module(factory_());
337     TrajectoryAnalysisCommandLineRunner runner(module.get());
338     runner.writeHelp(context);
339 }
340
341 // static
342 int
343 TrajectoryAnalysisCommandLineRunner::runAsMain(
344         int argc, char *argv[], ModuleFactoryMethod factory)
345 {
346     Impl::RunnerCommandLineModule module(NULL, NULL, factory);
347     return CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
348 }
349
350 // static
351 void
352 TrajectoryAnalysisCommandLineRunner::registerModule(
353         CommandLineModuleManager *manager, const char *name,
354         const char *description, ModuleFactoryMethod factory)
355 {
356     CommandLineModulePointer module(
357             new Impl::RunnerCommandLineModule(name, description, factory));
358     manager->addModule(move(module));
359 }
360
361 } // namespace gmx