Apply re-formatting to C++ in src/ tree.
[alexxy/gromacs.git] / src / gromacs / trajectoryanalysis / modules / convert_trj.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019,2020, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements gmx::analysismodules::ConvertTrj.
38  *
39  * \author Paul Bauer <paul.bauer.q@gmail.com>
40  * \ingroup module_trajectoryanalysis
41  */
42
43 #include "gmxpre.h"
44
45 #include "convert_trj.h"
46
47 #include <algorithm>
48
49 #include "gromacs/coordinateio/coordinatefile.h"
50 #include "gromacs/coordinateio/requirements.h"
51 #include "gromacs/fileio/trxio.h"
52 #include "gromacs/options/filenameoption.h"
53 #include "gromacs/options/ioptionscontainer.h"
54 #include "gromacs/selection/selectionoption.h"
55 #include "gromacs/trajectoryanalysis/analysissettings.h"
56 #include "gromacs/trajectoryanalysis/topologyinformation.h"
57
58 namespace gmx
59 {
60
61 namespace analysismodules
62 {
63
64 namespace
65 {
66
67 /*
68  * ConvertTrj
69  */
70
71 class ConvertTrj : public TrajectoryAnalysisModule
72 {
73 public:
74     ConvertTrj();
75
76     void initOptions(IOptionsContainer* options, TrajectoryAnalysisSettings* settings) override;
77     void optionsFinished(TrajectoryAnalysisSettings* settings) override;
78     void initAnalysis(const TrajectoryAnalysisSettings& settings, const TopologyInformation& top) override;
79     void analyzeFrame(int frnr, const t_trxframe& fr, t_pbc* pbc, TrajectoryAnalysisModuleData* pdata) override;
80
81     void finishAnalysis(int nframes) override;
82     void writeOutput() override;
83
84 private:
85     TrajectoryFrameWriterPointer    output_;
86     Selection                       sel_;
87     std::string                     name_;
88     OutputRequirementOptionDirector requirementsBuilder_;
89 };
90
91 ConvertTrj::ConvertTrj() {}
92
93
94 void ConvertTrj::initOptions(IOptionsContainer* options, TrajectoryAnalysisSettings* settings)
95 {
96     static const char* const desc[] = {
97         "[THISMODULE] converts trajectory files between different formats.",
98         "The module supports writing all GROMACS supported file formats from",
99         "the supported input formats.",
100         "",
101         "Included is also a selection of possible options to modify individual",
102         "trajectory frames, including options to produce slimmer",
103         "output files. It is also possible to replace the particle information stored",
104         "in the input trajectory with those from a structure file",
105         "",
106         "The module can also generate subsets of trajectories based on user supplied",
107         "selections.",
108     };
109
110     options->addOption(SelectionOption("select").store(&sel_).onlyAtoms().description(
111             "Selection of particles to write to the file"));
112
113     options->addOption(FileNameOption("o")
114                                .filetype(eftTrajectory)
115                                .outputFile()
116                                .store(&name_)
117                                .defaultBasename("trajout")
118                                .required()
119                                .description("Output trajectory"));
120
121     requirementsBuilder_.initOptions(options);
122
123     settings->setHelpText(desc);
124 }
125
126 void ConvertTrj::optionsFinished(TrajectoryAnalysisSettings* settings)
127 {
128     int frameFlags = TRX_NEED_X;
129
130     frameFlags |= TRX_READ_V;
131     frameFlags |= TRX_READ_F;
132
133     settings->setFrameFlags(frameFlags);
134 }
135
136
137 void ConvertTrj::initAnalysis(const TrajectoryAnalysisSettings& /*settings*/, const TopologyInformation& top)
138 {
139     output_ = createTrajectoryFrameWriter(top.mtop(),
140                                           sel_,
141                                           name_,
142                                           top.hasTopology() ? top.copyAtoms() : nullptr,
143                                           requirementsBuilder_.process());
144 }
145
146 void ConvertTrj::analyzeFrame(int frnr, const t_trxframe& fr, t_pbc* /* pbc */, TrajectoryAnalysisModuleData* /*pdata*/)
147 {
148     output_->prepareAndWriteFrame(frnr, fr);
149 }
150
151 void ConvertTrj::finishAnalysis(int /*nframes*/) {}
152
153
154 void ConvertTrj::writeOutput() {}
155
156 } // namespace
157
158 const char ConvertTrjInfo::name[]             = "convert-trj";
159 const char ConvertTrjInfo::shortDescription[] = "Converts between different trajectory types";
160
161 TrajectoryAnalysisModulePointer ConvertTrjInfo::create()
162 {
163     return TrajectoryAnalysisModulePointer(new ConvertTrj);
164 }
165
166 } // namespace analysismodules
167
168 } // namespace gmx