SYCL: Avoid using no_init read accessor in rocFFT
[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,2020,2021, 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)), common_(&settings_)
80     {
81     }
82
83     void init(CommandLineModuleSettings* /*settings*/) override {}
84     void initOptions(IOptionsContainer* options, ICommandLineOptionsModuleSettings* settings) override;
85     void optionsFinished() override;
86     int  run() override;
87
88     TrajectoryAnalysisModulePointer module_;
89     TrajectoryAnalysisSettings      settings_;
90     TrajectoryAnalysisRunnerCommon  common_;
91     SelectionCollection             selections_;
92 };
93
94 void RunnerModule::initOptions(IOptionsContainer* options, ICommandLineOptionsModuleSettings* settings)
95 {
96     std::shared_ptr<TimeUnitBehavior>        timeUnitBehavior(new TimeUnitBehavior());
97     std::shared_ptr<SelectionOptionBehavior> selectionOptionBehavior(
98             new SelectionOptionBehavior(&selections_, common_.topologyProvider()));
99     settings->addOptionsBehavior(timeUnitBehavior);
100     settings->addOptionsBehavior(selectionOptionBehavior);
101     IOptionsContainer& commonOptions = options->addGroup();
102     IOptionsContainer& moduleOptions = options->addGroup();
103
104     settings_.setOptionsModuleSettings(settings);
105     module_->initOptions(&moduleOptions, &settings_);
106     settings_.setOptionsModuleSettings(nullptr);
107     common_.initOptions(&commonOptions, timeUnitBehavior.get());
108     selectionOptionBehavior->initOptions(&commonOptions);
109 }
110
111 void RunnerModule::optionsFinished()
112 {
113     common_.optionsFinished();
114     module_->optionsFinished(&settings_);
115 }
116
117 int RunnerModule::run()
118 {
119     common_.initTopology();
120     const TopologyInformation& topology = common_.topologyInformation();
121     module_->initAnalysis(settings_, topology);
122
123     // Load first frame.
124     common_.initFirstFrame();
125     common_.initFrameIndexGroup();
126     module_->initAfterFirstFrame(settings_, common_.frame());
127
128     t_pbc  pbc;
129     t_pbc* ppbc = settings_.hasPBC() ? &pbc : nullptr;
130
131     int                                 nframes = 0;
132     AnalysisDataParallelOptions         dataOptions;
133     TrajectoryAnalysisModuleDataPointer pdata(module_->startFrames(dataOptions, selections_));
134     do
135     {
136         common_.initFrame();
137         t_trxframe& frame = common_.frame();
138         if (ppbc != nullptr)
139         {
140             set_pbc(ppbc, topology.pbcType(), frame.box);
141         }
142
143         selections_.evaluate(&frame, ppbc);
144         module_->analyzeFrame(nframes, frame, ppbc, pdata.get());
145         module_->finishFrameSerial(nframes);
146
147         ++nframes;
148     } while (common_.readNextFrame());
149     module_->finishFrames(pdata.get());
150     if (pdata.get() != nullptr)
151     {
152         pdata->finish();
153     }
154     pdata.reset();
155
156     if (common_.hasTrajectory())
157     {
158         fprintf(stderr, "Analyzed %d frames, last time %.3f\n", nframes, common_.frame().time);
159     }
160     else
161     {
162         fprintf(stderr, "Analyzed topology coordinates\n");
163     }
164
165     // Restore the maximal groups for dynamic selections.
166     selections_.evaluateFinal(nframes);
167
168     module_->finishAnalysis(nframes);
169     module_->writeOutput();
170
171     return 0;
172 }
173
174 } // namespace
175
176 /********************************************************************
177  * TrajectoryAnalysisCommandLineRunner
178  */
179
180 // static
181 int TrajectoryAnalysisCommandLineRunner::runAsMain(int argc, char* argv[], const ModuleFactoryMethod& factory)
182 {
183     auto runnerFactory = [factory] { return createModule(factory()); };
184     return ICommandLineOptionsModule::runAsMain(argc, argv, nullptr, nullptr, runnerFactory);
185 }
186
187 // static
188 void TrajectoryAnalysisCommandLineRunner::registerModule(CommandLineModuleManager*  manager,
189                                                          const char*                name,
190                                                          const char*                description,
191                                                          const ModuleFactoryMethod& factory)
192 {
193     auto runnerFactory = [factory] { return createModule(factory()); };
194     ICommandLineOptionsModule::registerModuleFactory(manager, name, description, runnerFactory);
195 }
196
197 // static
198 std::unique_ptr<ICommandLineOptionsModule>
199 TrajectoryAnalysisCommandLineRunner::createModule(TrajectoryAnalysisModulePointer module)
200 {
201     return ICommandLineOptionsModulePointer(new RunnerModule(std::move(module)));
202 }
203
204 } // namespace gmx