755782d8270a2aabdfe376922121b46e184b9336
[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-2018, The GROMACS development team.
5  * Copyright (c) 2019, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 /*! \internal \file
37  * \brief
38  * Implements gmx::TrajectoryAnalysisCommandLineRunner.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \ingroup module_trajectoryanalysis
42  */
43 #include "gmxpre.h"
44
45 #include "cmdlinerunner.h"
46
47 #include "gromacs/analysisdata/paralleloptions.h"
48 #include "gromacs/commandline/cmdlinemodulemanager.h"
49 #include "gromacs/commandline/cmdlineoptionsmodule.h"
50 #include "gromacs/options/ioptionscontainer.h"
51 #include "gromacs/options/timeunitmanager.h"
52 #include "gromacs/pbcutil/pbc.h"
53 #include "gromacs/selection/selectioncollection.h"
54 #include "gromacs/selection/selectionoptionbehavior.h"
55 #include "gromacs/trajectory/trajectoryframe.h"
56 #include "gromacs/trajectoryanalysis/analysismodule.h"
57 #include "gromacs/trajectoryanalysis/analysissettings.h"
58 #include "gromacs/trajectoryanalysis/topologyinformation.h"
59 #include "gromacs/utility/exceptions.h"
60 #include "gromacs/utility/filestream.h"
61 #include "gromacs/utility/gmxassert.h"
62
63 #include "runnercommon.h"
64
65 namespace gmx
66 {
67
68 namespace
69 {
70
71 /********************************************************************
72  * RunnerModule
73  */
74
75 class RunnerModule : public ICommandLineOptionsModule
76 {
77 public:
78     explicit RunnerModule(TrajectoryAnalysisModulePointer module) :
79         module_(std::move(module)),
80         common_(&settings_)
81     {
82     }
83
84     void init(CommandLineModuleSettings* /*settings*/) override {}
85     void initOptions(IOptionsContainer* options, ICommandLineOptionsModuleSettings* settings) override;
86     void optionsFinished() override;
87     int  run() override;
88
89     TrajectoryAnalysisModulePointer module_;
90     TrajectoryAnalysisSettings      settings_;
91     TrajectoryAnalysisRunnerCommon  common_;
92     SelectionCollection             selections_;
93 };
94
95 void RunnerModule::initOptions(IOptionsContainer* options, ICommandLineOptionsModuleSettings* settings)
96 {
97     std::shared_ptr<TimeUnitBehavior>        timeUnitBehavior(new TimeUnitBehavior());
98     std::shared_ptr<SelectionOptionBehavior> selectionOptionBehavior(
99             new SelectionOptionBehavior(&selections_, common_.topologyProvider()));
100     settings->addOptionsBehavior(timeUnitBehavior);
101     settings->addOptionsBehavior(selectionOptionBehavior);
102     IOptionsContainer& commonOptions = options->addGroup();
103     IOptionsContainer& moduleOptions = options->addGroup();
104
105     settings_.setOptionsModuleSettings(settings);
106     module_->initOptions(&moduleOptions, &settings_);
107     settings_.setOptionsModuleSettings(nullptr);
108     common_.initOptions(&commonOptions, timeUnitBehavior.get());
109     selectionOptionBehavior->initOptions(&commonOptions);
110 }
111
112 void RunnerModule::optionsFinished()
113 {
114     common_.optionsFinished();
115     module_->optionsFinished(&settings_);
116 }
117
118 int RunnerModule::run()
119 {
120     common_.initTopology();
121     const TopologyInformation& topology = common_.topologyInformation();
122     module_->initAnalysis(settings_, topology);
123
124     // Load first frame.
125     common_.initFirstFrame();
126     common_.initFrameIndexGroup();
127     module_->initAfterFirstFrame(settings_, common_.frame());
128
129     t_pbc  pbc;
130     t_pbc* ppbc = settings_.hasPBC() ? &pbc : nullptr;
131
132     int                                 nframes = 0;
133     AnalysisDataParallelOptions         dataOptions;
134     TrajectoryAnalysisModuleDataPointer pdata(module_->startFrames(dataOptions, selections_));
135     do
136     {
137         common_.initFrame();
138         t_trxframe& frame = common_.frame();
139         if (ppbc != nullptr)
140         {
141             set_pbc(ppbc, topology.ePBC(), frame.box);
142         }
143
144         selections_.evaluate(&frame, ppbc);
145         module_->analyzeFrame(nframes, frame, ppbc, pdata.get());
146         module_->finishFrameSerial(nframes);
147
148         ++nframes;
149     } while (common_.readNextFrame());
150     module_->finishFrames(pdata.get());
151     if (pdata.get() != nullptr)
152     {
153         pdata->finish();
154     }
155     pdata.reset();
156
157     if (common_.hasTrajectory())
158     {
159         fprintf(stderr, "Analyzed %d frames, last time %.3f\n", nframes, common_.frame().time);
160     }
161     else
162     {
163         fprintf(stderr, "Analyzed topology coordinates\n");
164     }
165
166     // Restore the maximal groups for dynamic selections.
167     selections_.evaluateFinal(nframes);
168
169     module_->finishAnalysis(nframes);
170     module_->writeOutput();
171
172     return 0;
173 }
174
175 } // namespace
176
177 /********************************************************************
178  * TrajectoryAnalysisCommandLineRunner
179  */
180
181 // static
182 int TrajectoryAnalysisCommandLineRunner::runAsMain(int argc, char* argv[], const ModuleFactoryMethod& factory)
183 {
184     auto runnerFactory = [factory] { return createModule(factory()); };
185     return ICommandLineOptionsModule::runAsMain(argc, argv, nullptr, nullptr, runnerFactory);
186 }
187
188 // static
189 void TrajectoryAnalysisCommandLineRunner::registerModule(CommandLineModuleManager*  manager,
190                                                          const char*                name,
191                                                          const char*                description,
192                                                          const ModuleFactoryMethod& factory)
193 {
194     auto runnerFactory = [factory] { return createModule(factory()); };
195     ICommandLineOptionsModule::registerModuleFactory(manager, name, description, runnerFactory);
196 }
197
198 // static
199 std::unique_ptr<ICommandLineOptionsModule>
200 TrajectoryAnalysisCommandLineRunner::createModule(TrajectoryAnalysisModulePointer module)
201 {
202     return ICommandLineOptionsModulePointer(new RunnerModule(std::move(module)));
203 }
204
205 } // namespace gmx