3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements classes in selection.h.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_selection
38 #include "selection.h"
50 SelectionData::SelectionData(SelectionTreeElement *elem,
52 : name_(elem->name()), selectionText_(selstr),
53 rootElement_(*elem), coveredFractionType_(CFRAC_NONE),
54 coveredFraction_(1.0), averageCoveredFraction_(1.0),
55 bDynamic_(false), bDynamicCoveredFraction_(false)
57 gmx_ana_pos_clear(&rawPositions_);
59 if (elem->child->type == SEL_CONST)
61 // TODO: This is not exception-safe if any called function throws.
62 gmx_ana_pos_copy(&rawPositions_, elem->child->v.u.p, true);
66 SelectionTreeElementPointer child = elem->child;
67 child->flags &= ~SEL_ALLOCVAL;
68 _gmx_selvalue_setstore(&child->v, &rawPositions_);
69 /* We should also skip any modifiers to determine the dynamic
71 while (child->type == SEL_MODIFIER)
74 if (child->type == SEL_SUBEXPRREF)
77 /* Because most subexpression elements are created
78 * during compilation, we need to check for them
81 if (child->type == SEL_SUBEXPR)
87 /* For variable references, we should skip the
88 * SEL_SUBEXPRREF and SEL_SUBEXPR elements. */
89 if (child->type == SEL_SUBEXPRREF)
91 child = child->child->child;
93 bDynamic_ = (child->child->flags & SEL_DYNAMIC);
95 initCoveredFraction(CFRAC_NONE);
99 SelectionData::~SelectionData()
101 gmx_ana_pos_deinit(&rawPositions_);
106 SelectionData::initCoveredFraction(e_coverfrac_t type)
108 coveredFractionType_ = type;
109 if (type == CFRAC_NONE)
111 bDynamicCoveredFraction_ = false;
113 else if (!_gmx_selelem_can_estimate_cover(rootElement()))
115 coveredFractionType_ = CFRAC_NONE;
116 bDynamicCoveredFraction_ = false;
120 bDynamicCoveredFraction_ = true;
122 coveredFraction_ = bDynamicCoveredFraction_ ? 0.0 : 1.0;
123 averageCoveredFraction_ = coveredFraction_;
124 return type == CFRAC_NONE || coveredFractionType_ != CFRAC_NONE;
131 * Helper function to compute total masses and charges for positions.
133 * \param[in] top Topology to take atom masses from.
134 * \param[in] pos Positions to compute masses and charges for.
135 * \param[out] masses Output masses.
136 * \param[out] charges Output charges.
138 * Does not throw if enough space has been reserved for the output vectors.
140 void computeMassesAndCharges(const t_topology *top, const gmx_ana_pos_t &pos,
141 std::vector<real> *masses,
142 std::vector<real> *charges)
144 GMX_ASSERT(top != NULL, "Should not have been called with NULL topology");
147 for (int b = 0; b < pos.nr; ++b)
151 for (int i = pos.m.mapb.index[b]; i < pos.m.mapb.index[b+1]; ++i)
153 int index = pos.g->index[i];
154 mass += top->atoms.atom[index].m;
155 charge += top->atoms.atom[index].q;
157 masses->push_back(mass);
158 charges->push_back(charge);
165 SelectionData::initializeMassesAndCharges(const t_topology *top)
167 GMX_ASSERT(posMass_.empty() && posCharge_.empty(),
168 "Should not be called more than once");
169 posMass_.reserve(posCount());
170 posCharge_.reserve(posCount());
173 posMass_.resize(posCount(), 1.0);
174 posCharge_.resize(posCount(), 0.0);
178 computeMassesAndCharges(top, rawPositions_, &posMass_, &posCharge_);
184 SelectionData::refreshMassesAndCharges(const t_topology *top)
186 if (top != NULL && isDynamic() && !hasFlag(efSelection_DynamicMask))
188 computeMassesAndCharges(top, rawPositions_, &posMass_, &posCharge_);
194 SelectionData::updateCoveredFractionForFrame()
196 if (isCoveredFractionDynamic())
198 real cfrac = _gmx_selelem_estimate_coverfrac(rootElement());
199 coveredFraction_ = cfrac;
200 averageCoveredFraction_ += cfrac;
206 SelectionData::computeAverageCoveredFraction(int nframes)
208 if (isCoveredFractionDynamic() && nframes > 0)
210 averageCoveredFraction_ /= nframes;
216 SelectionData::restoreOriginalPositions(const t_topology *top)
220 gmx_ana_pos_t &p = rawPositions_;
221 gmx_ana_index_copy(p.g, rootElement().v.u.g, false);
223 gmx_ana_indexmap_update(&p.m, p.g, hasFlag(gmx::efSelection_DynamicMask));
225 refreshMassesAndCharges(top);
229 } // namespace internal
233 Selection::printInfo(FILE *fp) const
235 fprintf(fp, "\"%s\" (%d position%s, %d atom%s%s)", name(),
236 posCount(), posCount() == 1 ? "" : "s",
237 atomCount(), atomCount() == 1 ? "" : "s",
238 isDynamic() ? ", dynamic" : "");
244 Selection::printDebugInfo(FILE *fp, int nmaxind) const
246 const gmx_ana_pos_t &p = data().rawPositions_;
251 gmx_ana_index_dump(fp, p.g, -1, nmaxind);
253 fprintf(fp, " Block (size=%d):", p.m.mapb.nr);
256 fprintf(fp, " (null)");
261 if (nmaxind >= 0 && n > nmaxind)
265 for (int i = 0; i <= n; ++i)
267 fprintf(fp, " %d", p.m.mapb.index[i]);
277 if (nmaxind >= 0 && n > nmaxind)
281 fprintf(fp, " RefId:");
284 fprintf(fp, " (null)");
288 for (int i = 0; i < n; ++i)
290 fprintf(fp, " %d", p.m.refid[i]);
299 fprintf(fp, " MapId:");
302 fprintf(fp, " (null)");
306 for (int i = 0; i < n; ++i)
308 fprintf(fp, " %d", p.m.mapid[i]);