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