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