Added option to gmx nmeig to print ZPE.
[gromacs.git] / src / gromacs / trajectoryanalysis / cmdlinerunner.cpp
blob733ebdd2c35b6f74420a0ea9c2fd0a0fe3860c32
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,2014,2015,2016,2017,2018, 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.
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.
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.
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.
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.
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.
35 /*! \internal \file
36 * \brief
37 * Implements gmx::TrajectoryAnalysisCommandLineRunner.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_trajectoryanalysis
42 #include "gmxpre.h"
44 #include "cmdlinerunner.h"
46 #include "gromacs/analysisdata/paralleloptions.h"
47 #include "gromacs/commandline/cmdlinemodulemanager.h"
48 #include "gromacs/commandline/cmdlineoptionsmodule.h"
49 #include "gromacs/options/ioptionscontainer.h"
50 #include "gromacs/options/timeunitmanager.h"
51 #include "gromacs/pbcutil/pbc.h"
52 #include "gromacs/selection/selectioncollection.h"
53 #include "gromacs/selection/selectionoptionbehavior.h"
54 #include "gromacs/trajectory/trajectoryframe.h"
55 #include "gromacs/trajectoryanalysis/analysismodule.h"
56 #include "gromacs/trajectoryanalysis/analysissettings.h"
57 #include "gromacs/trajectoryanalysis/topologyinformation.h"
58 #include "gromacs/utility/exceptions.h"
59 #include "gromacs/utility/filestream.h"
60 #include "gromacs/utility/gmxassert.h"
62 #include "runnercommon.h"
64 namespace gmx
67 namespace
70 /********************************************************************
71 * RunnerModule
74 class RunnerModule : public ICommandLineOptionsModule
76 public:
77 explicit RunnerModule(TrajectoryAnalysisModulePointer module)
78 : module_(std::move(module)), common_(&settings_)
82 void init(CommandLineModuleSettings * /*settings*/) override {}
83 void initOptions(IOptionsContainer *options,
84 ICommandLineOptionsModuleSettings *settings) override;
85 void optionsFinished() override;
86 int run() override;
88 TrajectoryAnalysisModulePointer module_;
89 TrajectoryAnalysisSettings settings_;
90 TrajectoryAnalysisRunnerCommon common_;
91 SelectionCollection selections_;
94 void RunnerModule::initOptions(
95 IOptionsContainer *options, ICommandLineOptionsModuleSettings *settings)
97 std::shared_ptr<TimeUnitBehavior> timeUnitBehavior(
98 new TimeUnitBehavior());
99 std::shared_ptr<SelectionOptionBehavior> selectionOptionBehavior(
100 new SelectionOptionBehavior(&selections_,
101 common_.topologyProvider()));
102 settings->addOptionsBehavior(timeUnitBehavior);
103 settings->addOptionsBehavior(selectionOptionBehavior);
104 IOptionsContainer &commonOptions = options->addGroup();
105 IOptionsContainer &moduleOptions = options->addGroup();
107 settings_.setOptionsModuleSettings(settings);
108 module_->initOptions(&moduleOptions, &settings_);
109 settings_.setOptionsModuleSettings(nullptr);
110 common_.initOptions(&commonOptions, timeUnitBehavior.get());
111 selectionOptionBehavior->initOptions(&commonOptions);
114 void RunnerModule::optionsFinished()
116 common_.optionsFinished();
117 module_->optionsFinished(&settings_);
120 int RunnerModule::run()
122 common_.initTopology();
123 const TopologyInformation &topology = common_.topologyInformation();
124 module_->initAnalysis(settings_, topology);
126 // Load first frame.
127 common_.initFirstFrame();
128 common_.initFrameIndexGroup();
129 module_->initAfterFirstFrame(settings_, common_.frame());
131 t_pbc pbc;
132 t_pbc *ppbc = settings_.hasPBC() ? &pbc : nullptr;
134 int nframes = 0;
135 AnalysisDataParallelOptions dataOptions;
136 TrajectoryAnalysisModuleDataPointer pdata(
137 module_->startFrames(dataOptions, selections_));
140 common_.initFrame();
141 t_trxframe &frame = common_.frame();
142 if (ppbc != nullptr)
144 set_pbc(ppbc, topology.ePBC(), frame.box);
147 selections_.evaluate(&frame, ppbc);
148 module_->analyzeFrame(nframes, frame, ppbc, pdata.get());
149 module_->finishFrameSerial(nframes);
151 ++nframes;
153 while (common_.readNextFrame());
154 module_->finishFrames(pdata.get());
155 if (pdata.get() != nullptr)
157 pdata->finish();
159 pdata.reset();
161 if (common_.hasTrajectory())
163 fprintf(stderr, "Analyzed %d frames, last time %.3f\n",
164 nframes, common_.frame().time);
166 else
168 fprintf(stderr, "Analyzed topology coordinates\n");
171 // Restore the maximal groups for dynamic selections.
172 selections_.evaluateFinal(nframes);
174 module_->finishAnalysis(nframes);
175 module_->writeOutput();
177 return 0;
180 } // namespace
182 /********************************************************************
183 * TrajectoryAnalysisCommandLineRunner
186 // static
188 TrajectoryAnalysisCommandLineRunner::runAsMain(
189 int argc, char *argv[], const ModuleFactoryMethod &factory)
191 auto runnerFactory = [factory]
193 return createModule(factory());
195 return ICommandLineOptionsModule::runAsMain(argc, argv, nullptr, nullptr, runnerFactory);
198 // static
199 void
200 TrajectoryAnalysisCommandLineRunner::registerModule(
201 CommandLineModuleManager *manager, const char *name,
202 const char *description, const ModuleFactoryMethod &factory)
204 auto runnerFactory = [factory]
206 return createModule(factory());
208 ICommandLineOptionsModule::registerModuleFactory(
209 manager, name, description, runnerFactory);
212 // static
213 std::unique_ptr<ICommandLineOptionsModule>
214 TrajectoryAnalysisCommandLineRunner::createModule(
215 TrajectoryAnalysisModulePointer module)
217 return ICommandLineOptionsModulePointer(new RunnerModule(std::move(module)));
220 } // namespace gmx