clang-tidy: override
[alexxy/gromacs.git] / src / gromacs / trajectoryanalysis / tests / test_selection.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2013,2014,2015,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.
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 Testing/debugging tool for the selection engine.
37  *
38  * \author Teemu Murtola <teemu.murtola@gmail.com>
39  * \ingroup module_trajectoryanalysis
40  */
41 #include "gmxpre.h"
42
43 #include "gromacs/options/basicoptions.h"
44 #include "gromacs/options/ioptionscontainer.h"
45 #include "gromacs/selection/selection.h"
46 #include "gromacs/selection/selectionoption.h"
47 #include "gromacs/trajectoryanalysis/analysismodule.h"
48 #include "gromacs/trajectoryanalysis/analysissettings.h"
49 #include "gromacs/trajectoryanalysis/cmdlinerunner.h"
50 #include "gromacs/utility/arrayref.h"
51 #include "gromacs/utility/exceptions.h"
52
53 namespace gmx
54 {
55
56 class SelectionTester : public TrajectoryAnalysisModule
57 {
58     public:
59         SelectionTester();
60         ~SelectionTester() override;
61
62         void initOptions(IOptionsContainer          *options,
63                          TrajectoryAnalysisSettings *settings) override;
64         void initAnalysis(const TrajectoryAnalysisSettings &settings,
65                           const TopologyInformation        &top) override;
66
67         void analyzeFrame(int frnr, const t_trxframe &fr, t_pbc *pbc,
68                           TrajectoryAnalysisModuleData *pdata) override;
69
70         void finishAnalysis(int nframes) override;
71         void writeOutput() override;
72
73     private:
74         void printSelections();
75
76         SelectionList            selections_;
77         int                      nmaxind_;
78 };
79
80 SelectionTester::SelectionTester()
81     : nmaxind_(20)
82 {
83 }
84
85 SelectionTester::~SelectionTester()
86 {
87 }
88
89 void
90 SelectionTester::printSelections()
91 {
92     fprintf(stderr, "\nSelections:\n");
93     for (size_t g = 0; g < selections_.size(); ++g)
94     {
95         selections_[g].printDebugInfo(stderr, nmaxind_);
96     }
97     fprintf(stderr, "\n");
98 }
99
100 void
101 SelectionTester::initOptions(IOptionsContainer          *options,
102                              TrajectoryAnalysisSettings *settings)
103 {
104     static const char *const desc[] = {
105         "This is a test program for selections."
106     };
107
108     settings->setHelpText(desc);
109
110     options->addOption(SelectionOption("select").storeVector(&selections_)
111                            .required().multiValue()
112                            .description("Selections to test"));
113     options->addOption(IntegerOption("pmax").store(&nmaxind_)
114                            .description("Maximum number of indices to print in lists (-1 = print all)"));
115 }
116
117 void
118 SelectionTester::initAnalysis(const TrajectoryAnalysisSettings & /*settings*/,
119                               const TopologyInformation        & /*top*/)
120 {
121     printSelections();
122 }
123
124 void
125 SelectionTester::analyzeFrame(int /*frnr*/, const t_trxframe & /*fr*/, t_pbc * /*pbc*/,
126                               TrajectoryAnalysisModuleData * /*pdata*/)
127 {
128     fprintf(stderr, "\n");
129     for (size_t g = 0; g < selections_.size(); ++g)
130     {
131         const Selection &sel = selections_[g];
132         int              n;
133
134         fprintf(stderr, "  Atoms (%d pcs):", sel.atomCount());
135         n = sel.atomCount();
136         if (nmaxind_ >= 0 && n > nmaxind_)
137         {
138             n = nmaxind_;
139         }
140         ArrayRef<const int> atoms = sel.atomIndices();
141         for (int i = 0; i < n; ++i)
142         {
143             fprintf(stderr, " %d", atoms[i]+1);
144         }
145         if (n < sel.atomCount())
146         {
147             fprintf(stderr, " ...");
148         }
149         fprintf(stderr, "\n");
150
151         fprintf(stderr, "  Positions (%d pcs):\n", sel.posCount());
152         n = sel.posCount();
153         if (nmaxind_ >= 0 && n > nmaxind_)
154         {
155             n = nmaxind_;
156         }
157         for (int i = 0; i < n; ++i)
158         {
159             const SelectionPosition &p = sel.position(i);
160             fprintf(stderr, "    (%.2f,%.2f,%.2f) r=%d, m=%d, n=%d\n",
161                     p.x()[XX], p.x()[YY], p.x()[ZZ],
162                     p.refId(), p.mappedId(), p.atomCount());
163         }
164         if (n < sel.posCount())
165         {
166             fprintf(stderr, "    ...\n");
167         }
168     }
169     fprintf(stderr, "\n");
170 }
171
172 void
173 SelectionTester::finishAnalysis(int /*nframes*/)
174 {
175     printSelections();
176 }
177
178 void
179 SelectionTester::writeOutput()
180 {
181 }
182
183 }  // namespace gmx
184
185 /*! \internal \brief
186  * The main function for the selection testing tool.
187  */
188 int
189 main(int argc, char *argv[])
190 {
191     return gmx::TrajectoryAnalysisCommandLineRunner::runAsMain<gmx::SelectionTester>(argc, argv);
192 }