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,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.
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.
38 * Implements gmx::TrajectoryAnalysisCommandLineRunner.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_trajectoryanalysis
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"
71 /********************************************************************
75 class RunnerModule : public ICommandLineOptionsModule
78 explicit RunnerModule(TrajectoryAnalysisModulePointer module) :
79 module_(std::move(module)), common_(&settings_)
83 void init(CommandLineModuleSettings* /*settings*/) override {}
84 void initOptions(IOptionsContainer* options, ICommandLineOptionsModuleSettings* settings) override;
85 void optionsFinished() override;
88 TrajectoryAnalysisModulePointer module_;
89 TrajectoryAnalysisSettings settings_;
90 TrajectoryAnalysisRunnerCommon common_;
91 SelectionCollection selections_;
94 void RunnerModule::initOptions(IOptionsContainer* options, ICommandLineOptionsModuleSettings* settings)
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();
104 settings_.setOptionsModuleSettings(settings);
105 module_->initOptions(&moduleOptions, &settings_);
106 settings_.setOptionsModuleSettings(nullptr);
107 common_.initOptions(&commonOptions, timeUnitBehavior.get());
108 selectionOptionBehavior->initOptions(&commonOptions);
111 void RunnerModule::optionsFinished()
113 common_.optionsFinished();
114 module_->optionsFinished(&settings_);
117 int RunnerModule::run()
119 common_.initTopology();
120 const TopologyInformation& topology = common_.topologyInformation();
121 module_->initAnalysis(settings_, topology);
124 common_.initFirstFrame();
125 common_.initFrameIndexGroup();
126 module_->initAfterFirstFrame(settings_, common_.frame());
129 t_pbc* ppbc = settings_.hasPBC() ? &pbc : nullptr;
132 AnalysisDataParallelOptions dataOptions;
133 TrajectoryAnalysisModuleDataPointer pdata(module_->startFrames(dataOptions, selections_));
137 t_trxframe& frame = common_.frame();
140 set_pbc(ppbc, topology.pbcType(), frame.box);
143 selections_.evaluate(&frame, ppbc);
144 module_->analyzeFrame(nframes, frame, ppbc, pdata.get());
145 module_->finishFrameSerial(nframes);
148 } while (common_.readNextFrame());
149 module_->finishFrames(pdata.get());
150 if (pdata.get() != nullptr)
156 if (common_.hasTrajectory())
158 fprintf(stderr, "Analyzed %d frames, last time %.3f\n", nframes, common_.frame().time);
162 fprintf(stderr, "Analyzed topology coordinates\n");
165 // Restore the maximal groups for dynamic selections.
166 selections_.evaluateFinal(nframes);
168 module_->finishAnalysis(nframes);
169 module_->writeOutput();
176 /********************************************************************
177 * TrajectoryAnalysisCommandLineRunner
181 int TrajectoryAnalysisCommandLineRunner::runAsMain(int argc, char* argv[], const ModuleFactoryMethod& factory)
183 auto runnerFactory = [factory] { return createModule(factory()); };
184 return ICommandLineOptionsModule::runAsMain(argc, argv, nullptr, nullptr, runnerFactory);
188 void TrajectoryAnalysisCommandLineRunner::registerModule(CommandLineModuleManager* manager,
190 const char* description,
191 const ModuleFactoryMethod& factory)
193 auto runnerFactory = [factory] { return createModule(factory()); };
194 ICommandLineOptionsModule::registerModuleFactory(manager, name, description, runnerFactory);
198 std::unique_ptr<ICommandLineOptionsModule>
199 TrajectoryAnalysisCommandLineRunner::createModule(TrajectoryAnalysisModulePointer module)
201 return ICommandLineOptionsModulePointer(new RunnerModule(std::move(module)));