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