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