Simplified uniform GPU selection in CMake
[gromacs.git] / src / gromacs / trajectoryanalysis / cmdlinerunner.cpp
blobfb0d2e17bf538ae4b596e22257edc54338f674e6
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010-2018, The GROMACS development team.
5 * Copyright (c) 2019,2020, 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.
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.
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.
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.
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.
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.
36 /*! \internal \file
37 * \brief
38 * Implements gmx::TrajectoryAnalysisCommandLineRunner.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_trajectoryanalysis
43 #include "gmxpre.h"
45 #include "cmdlinerunner.h"
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"
63 #include "runnercommon.h"
65 namespace gmx
68 namespace
71 /********************************************************************
72 * RunnerModule
75 class RunnerModule : public ICommandLineOptionsModule
77 public:
78 explicit RunnerModule(TrajectoryAnalysisModulePointer module) :
79 module_(std::move(module)),
80 common_(&settings_)
84 void init(CommandLineModuleSettings* /*settings*/) override {}
85 void initOptions(IOptionsContainer* options, ICommandLineOptionsModuleSettings* settings) override;
86 void optionsFinished() override;
87 int run() override;
89 TrajectoryAnalysisModulePointer module_;
90 TrajectoryAnalysisSettings settings_;
91 TrajectoryAnalysisRunnerCommon common_;
92 SelectionCollection selections_;
95 void RunnerModule::initOptions(IOptionsContainer* options, ICommandLineOptionsModuleSettings* settings)
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();
105 settings_.setOptionsModuleSettings(settings);
106 module_->initOptions(&moduleOptions, &settings_);
107 settings_.setOptionsModuleSettings(nullptr);
108 common_.initOptions(&commonOptions, timeUnitBehavior.get());
109 selectionOptionBehavior->initOptions(&commonOptions);
112 void RunnerModule::optionsFinished()
114 common_.optionsFinished();
115 module_->optionsFinished(&settings_);
118 int RunnerModule::run()
120 common_.initTopology();
121 const TopologyInformation& topology = common_.topologyInformation();
122 module_->initAnalysis(settings_, topology);
124 // Load first frame.
125 common_.initFirstFrame();
126 common_.initFrameIndexGroup();
127 module_->initAfterFirstFrame(settings_, common_.frame());
129 t_pbc pbc;
130 t_pbc* ppbc = settings_.hasPBC() ? &pbc : nullptr;
132 int nframes = 0;
133 AnalysisDataParallelOptions dataOptions;
134 TrajectoryAnalysisModuleDataPointer pdata(module_->startFrames(dataOptions, selections_));
137 common_.initFrame();
138 t_trxframe& frame = common_.frame();
139 if (ppbc != nullptr)
141 set_pbc(ppbc, topology.pbcType(), frame.box);
144 selections_.evaluate(&frame, ppbc);
145 module_->analyzeFrame(nframes, frame, ppbc, pdata.get());
146 module_->finishFrameSerial(nframes);
148 ++nframes;
149 } while (common_.readNextFrame());
150 module_->finishFrames(pdata.get());
151 if (pdata.get() != nullptr)
153 pdata->finish();
155 pdata.reset();
157 if (common_.hasTrajectory())
159 fprintf(stderr, "Analyzed %d frames, last time %.3f\n", nframes, common_.frame().time);
161 else
163 fprintf(stderr, "Analyzed topology coordinates\n");
166 // Restore the maximal groups for dynamic selections.
167 selections_.evaluateFinal(nframes);
169 module_->finishAnalysis(nframes);
170 module_->writeOutput();
172 return 0;
175 } // namespace
177 /********************************************************************
178 * TrajectoryAnalysisCommandLineRunner
181 // static
182 int TrajectoryAnalysisCommandLineRunner::runAsMain(int argc, char* argv[], const ModuleFactoryMethod& factory)
184 auto runnerFactory = [factory] { return createModule(factory()); };
185 return ICommandLineOptionsModule::runAsMain(argc, argv, nullptr, nullptr, runnerFactory);
188 // static
189 void TrajectoryAnalysisCommandLineRunner::registerModule(CommandLineModuleManager* manager,
190 const char* name,
191 const char* description,
192 const ModuleFactoryMethod& factory)
194 auto runnerFactory = [factory] { return createModule(factory()); };
195 ICommandLineOptionsModule::registerModuleFactory(manager, name, description, runnerFactory);
198 // static
199 std::unique_ptr<ICommandLineOptionsModule>
200 TrajectoryAnalysisCommandLineRunner::createModule(TrajectoryAnalysisModulePointer module)
202 return ICommandLineOptionsModulePointer(new RunnerModule(std::move(module)));
205 } // namespace gmx