Merge branch 'release-4-6' into master
[alexxy/gromacs.git] / src / gromacs / selection / selection.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2009,2010,2011,2012,2013, by the GROMACS development team, led by
5  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6  * others, as listed in the AUTHORS file in the top-level source
7  * 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 classes in selection.h.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_selection
41  */
42 #include "selection.h"
43
44 #include "position.h"
45 #include "selelem.h"
46 #include "selvalue.h"
47
48 namespace gmx
49 {
50
51 namespace internal
52 {
53
54 SelectionData::SelectionData(SelectionTreeElement *elem,
55                              const char           *selstr)
56     : name_(elem->name()), selectionText_(selstr),
57       rootElement_(*elem), coveredFractionType_(CFRAC_NONE),
58       coveredFraction_(1.0), averageCoveredFraction_(1.0),
59       bDynamic_(false), bDynamicCoveredFraction_(false)
60 {
61     gmx_ana_pos_clear(&rawPositions_);
62
63     if (elem->child->type == SEL_CONST)
64     {
65         // TODO: This is not exception-safe if any called function throws.
66         gmx_ana_pos_copy(&rawPositions_, elem->child->v.u.p, true);
67     }
68     else
69     {
70         SelectionTreeElementPointer child = elem->child;
71         child->flags     &= ~SEL_ALLOCVAL;
72         _gmx_selvalue_setstore(&child->v, &rawPositions_);
73         /* We should also skip any modifiers to determine the dynamic
74          * status. */
75         while (child->type == SEL_MODIFIER)
76         {
77             child = child->child;
78             if (child->type == SEL_SUBEXPRREF)
79             {
80                 child = child->child;
81                 /* Because most subexpression elements are created
82                  * during compilation, we need to check for them
83                  * explicitly here.
84                  */
85                 if (child->type == SEL_SUBEXPR)
86                 {
87                     child = child->child;
88                 }
89             }
90         }
91         /* For variable references, we should skip the
92          * SEL_SUBEXPRREF and SEL_SUBEXPR elements. */
93         if (child->type == SEL_SUBEXPRREF)
94         {
95             child = child->child->child;
96         }
97         bDynamic_ = (child->child->flags & SEL_DYNAMIC);
98     }
99     initCoveredFraction(CFRAC_NONE);
100 }
101
102
103 SelectionData::~SelectionData()
104 {
105     gmx_ana_pos_deinit(&rawPositions_);
106 }
107
108
109 bool
110 SelectionData::initCoveredFraction(e_coverfrac_t type)
111 {
112     coveredFractionType_ = type;
113     if (type == CFRAC_NONE)
114     {
115         bDynamicCoveredFraction_ = false;
116     }
117     else if (!_gmx_selelem_can_estimate_cover(rootElement()))
118     {
119         coveredFractionType_     = CFRAC_NONE;
120         bDynamicCoveredFraction_ = false;
121     }
122     else
123     {
124         bDynamicCoveredFraction_ = true;
125     }
126     coveredFraction_        = bDynamicCoveredFraction_ ? 0.0 : 1.0;
127     averageCoveredFraction_ = coveredFraction_;
128     return type == CFRAC_NONE || coveredFractionType_ != CFRAC_NONE;
129 }
130
131 namespace
132 {
133
134 /*! \brief
135  * Helper function to compute total masses and charges for positions.
136  *
137  * \param[in]  top     Topology to take atom masses from.
138  * \param[in]  pos     Positions to compute masses and charges for.
139  * \param[out] masses  Output masses.
140  * \param[out] charges Output charges.
141  *
142  * Does not throw if enough space has been reserved for the output vectors.
143  */
144 void computeMassesAndCharges(const t_topology *top, const gmx_ana_pos_t &pos,
145                              std::vector<real> *masses,
146                              std::vector<real> *charges)
147 {
148     GMX_ASSERT(top != NULL, "Should not have been called with NULL topology");
149     masses->clear();
150     charges->clear();
151     for (int b = 0; b < pos.nr; ++b)
152     {
153         real mass   = 0.0;
154         real charge = 0.0;
155         for (int i = pos.m.mapb.index[b]; i < pos.m.mapb.index[b+1]; ++i)
156         {
157             int index = pos.g->index[i];
158             mass   += top->atoms.atom[index].m;
159             charge += top->atoms.atom[index].q;
160         }
161         masses->push_back(mass);
162         charges->push_back(charge);
163     }
164 }
165
166 }       // namespace
167
168 void
169 SelectionData::initializeMassesAndCharges(const t_topology *top)
170 {
171     GMX_ASSERT(posMass_.empty() && posCharge_.empty(),
172                "Should not be called more than once");
173     posMass_.reserve(posCount());
174     posCharge_.reserve(posCount());
175     if (top == NULL)
176     {
177         posMass_.resize(posCount(), 1.0);
178         posCharge_.resize(posCount(), 0.0);
179     }
180     else
181     {
182         computeMassesAndCharges(top, rawPositions_, &posMass_, &posCharge_);
183     }
184 }
185
186
187 void
188 SelectionData::refreshMassesAndCharges(const t_topology *top)
189 {
190     if (top != NULL && isDynamic() && !hasFlag(efSelection_DynamicMask))
191     {
192         computeMassesAndCharges(top, rawPositions_, &posMass_, &posCharge_);
193     }
194 }
195
196
197 void
198 SelectionData::updateCoveredFractionForFrame()
199 {
200     if (isCoveredFractionDynamic())
201     {
202         real cfrac = _gmx_selelem_estimate_coverfrac(rootElement());
203         coveredFraction_         = cfrac;
204         averageCoveredFraction_ += cfrac;
205     }
206 }
207
208
209 void
210 SelectionData::computeAverageCoveredFraction(int nframes)
211 {
212     if (isCoveredFractionDynamic() && nframes > 0)
213     {
214         averageCoveredFraction_ /= nframes;
215     }
216 }
217
218
219 void
220 SelectionData::restoreOriginalPositions(const t_topology *top)
221 {
222     if (isDynamic())
223     {
224         gmx_ana_pos_t &p = rawPositions_;
225         gmx_ana_index_copy(p.g, rootElement().v.u.g, false);
226         gmx_ana_indexmap_update(&p.m, p.g, hasFlag(gmx::efSelection_DynamicMask));
227         p.nr = p.m.nr;
228         refreshMassesAndCharges(top);
229     }
230 }
231
232 }   // namespace internal
233
234
235 void
236 Selection::printInfo(FILE *fp) const
237 {
238     fprintf(fp, "\"%s\" (%d position%s, %d atom%s%s)", name(),
239             posCount(),  posCount()  == 1 ? "" : "s",
240             atomCount(), atomCount() == 1 ? "" : "s",
241             isDynamic() ? ", dynamic" : "");
242     fprintf(fp, "\n");
243 }
244
245
246 void
247 Selection::printDebugInfo(FILE *fp, int nmaxind) const
248 {
249     const gmx_ana_pos_t &p = data().rawPositions_;
250
251     fprintf(fp, "  ");
252     printInfo(fp);
253     fprintf(fp, "    Group ");
254     gmx_ana_index_dump(fp, p.g, nmaxind);
255
256     fprintf(fp, "    Block (size=%d):", p.m.mapb.nr);
257     if (!p.m.mapb.index)
258     {
259         fprintf(fp, " (null)");
260     }
261     else
262     {
263         int n = p.m.mapb.nr;
264         if (nmaxind >= 0 && n > nmaxind)
265         {
266             n = nmaxind;
267         }
268         for (int i = 0; i <= n; ++i)
269         {
270             fprintf(fp, " %d", p.m.mapb.index[i]);
271         }
272         if (n < p.m.mapb.nr)
273         {
274             fprintf(fp, " ...");
275         }
276     }
277     fprintf(fp, "\n");
278
279     int n = posCount();
280     if (nmaxind >= 0 && n > nmaxind)
281     {
282         n = nmaxind;
283     }
284     fprintf(fp, "    RefId:");
285     if (!p.m.refid)
286     {
287         fprintf(fp, " (null)");
288     }
289     else
290     {
291         for (int i = 0; i < n; ++i)
292         {
293             fprintf(fp, " %d", p.m.refid[i]);
294         }
295         if (n < posCount())
296         {
297             fprintf(fp, " ...");
298         }
299     }
300     fprintf(fp, "\n");
301
302     fprintf(fp, "    MapId:");
303     if (!p.m.mapid)
304     {
305         fprintf(fp, " (null)");
306     }
307     else
308     {
309         for (int i = 0; i < n; ++i)
310         {
311             fprintf(fp, " %d", p.m.mapid[i]);
312         }
313         if (n < posCount())
314         {
315             fprintf(fp, " ...");
316         }
317     }
318     fprintf(fp, "\n");
319 }
320
321 } // namespace gmx