2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2013,2014,2015, 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.
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.
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.
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.
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.
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.
37 * Implements classes in selection.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_selection
44 #include "selection.h"
48 #include "gromacs/selection/nbsearch.h"
49 #include "gromacs/selection/position.h"
50 #include "gromacs/topology/topology.h"
51 #include "gromacs/utility/exceptions.h"
52 #include "gromacs/utility/gmxassert.h"
53 #include "gromacs/utility/stringutil.h"
64 /********************************************************************
68 SelectionData::SelectionData(SelectionTreeElement *elem,
70 : name_(elem->name()), selectionText_(selstr),
71 rootElement_(*elem), coveredFractionType_(CFRAC_NONE),
72 coveredFraction_(1.0), averageCoveredFraction_(1.0),
73 bDynamic_(false), bDynamicCoveredFraction_(false)
75 if (elem->child->type == SEL_CONST)
77 // TODO: This is not exception-safe if any called function throws.
78 gmx_ana_pos_copy(&rawPositions_, elem->child->v.u.p, true);
82 SelectionTreeElementPointer child = elem->child;
83 child->flags &= ~SEL_ALLOCVAL;
84 _gmx_selvalue_setstore(&child->v, &rawPositions_);
85 /* We should also skip any modifiers to determine the dynamic
87 while (child->type == SEL_MODIFIER)
94 if (child->type == SEL_SUBEXPRREF)
97 /* Because most subexpression elements are created
98 * during compilation, we need to check for them
101 if (child->type == SEL_SUBEXPR)
103 child = child->child;
109 /* For variable references, we should skip the
110 * SEL_SUBEXPRREF and SEL_SUBEXPR elements. */
111 if (child->type == SEL_SUBEXPRREF)
113 child = child->child->child;
115 bDynamic_ = (child->child->flags & SEL_DYNAMIC);
118 initCoveredFraction(CFRAC_NONE);
122 SelectionData::~SelectionData()
128 SelectionData::initCoveredFraction(e_coverfrac_t type)
130 coveredFractionType_ = type;
131 if (type == CFRAC_NONE)
133 bDynamicCoveredFraction_ = false;
135 else if (!_gmx_selelem_can_estimate_cover(rootElement()))
137 coveredFractionType_ = CFRAC_NONE;
138 bDynamicCoveredFraction_ = false;
142 bDynamicCoveredFraction_ = true;
144 coveredFraction_ = bDynamicCoveredFraction_ ? 0.0 : 1.0;
145 averageCoveredFraction_ = coveredFraction_;
146 return type == CFRAC_NONE || coveredFractionType_ != CFRAC_NONE;
153 * Helper function to compute total masses and charges for positions.
155 * \param[in] top Topology to take atom masses from.
156 * \param[in] pos Positions to compute masses and charges for.
157 * \param[out] masses Output masses.
158 * \param[out] charges Output charges.
160 * Does not throw if enough space has been reserved for the output vectors.
162 void computeMassesAndCharges(const t_topology *top, const gmx_ana_pos_t &pos,
163 std::vector<real> *masses,
164 std::vector<real> *charges)
166 GMX_ASSERT(top != NULL, "Should not have been called with NULL topology");
169 for (int b = 0; b < pos.count(); ++b)
173 for (int i = pos.m.mapb.index[b]; i < pos.m.mapb.index[b+1]; ++i)
175 const int index = pos.m.mapb.a[i];
176 mass += top->atoms.atom[index].m;
177 charge += top->atoms.atom[index].q;
179 masses->push_back(mass);
180 charges->push_back(charge);
187 SelectionData::refreshName()
189 rootElement_.fillNameIfMissing(selectionText_.c_str());
190 name_ = rootElement_.name();
194 SelectionData::initializeMassesAndCharges(const t_topology *top)
196 GMX_ASSERT(posMass_.empty() && posCharge_.empty(),
197 "Should not be called more than once");
198 posMass_.reserve(posCount());
199 posCharge_.reserve(posCount());
202 posMass_.resize(posCount(), 1.0);
203 posCharge_.resize(posCount(), 0.0);
207 computeMassesAndCharges(top, rawPositions_, &posMass_, &posCharge_);
213 SelectionData::refreshMassesAndCharges(const t_topology *top)
215 if (top != NULL && isDynamic() && !hasFlag(efSelection_DynamicMask))
217 computeMassesAndCharges(top, rawPositions_, &posMass_, &posCharge_);
223 SelectionData::updateCoveredFractionForFrame()
225 if (isCoveredFractionDynamic())
227 real cfrac = _gmx_selelem_estimate_coverfrac(rootElement());
228 coveredFraction_ = cfrac;
229 averageCoveredFraction_ += cfrac;
235 SelectionData::computeAverageCoveredFraction(int nframes)
237 if (isCoveredFractionDynamic() && nframes > 0)
239 averageCoveredFraction_ /= nframes;
245 SelectionData::restoreOriginalPositions(const t_topology *top)
249 gmx_ana_pos_t &p = rawPositions_;
250 gmx_ana_indexmap_update(&p.m, rootElement().v.u.g,
251 hasFlag(gmx::efSelection_DynamicMask));
252 refreshMassesAndCharges(top);
256 } // namespace internal
258 /********************************************************************
262 Selection::operator AnalysisNeighborhoodPositions() const
264 AnalysisNeighborhoodPositions pos(data().rawPositions_.x,
265 data().rawPositions_.count());
268 pos.exclusionIds(atomIndices());
275 Selection::setOriginalId(int i, int id)
277 data().rawPositions_.m.mapid[i] = id;
278 data().rawPositions_.m.orgid[i] = id;
283 Selection::initOriginalIdsToGroup(t_topology *top, e_index_t type)
287 return gmx_ana_indexmap_init_orgid_group(&data().rawPositions_.m, top, type);
289 catch (const InconsistentInputError &)
291 GMX_ASSERT(type == INDEX_RES || type == INDEX_MOL,
292 "Expected that only grouping by residue/molecule would fail");
293 std::string message =
294 formatString("Cannot group selection '%s' into %s, because some "
295 "positions have atoms from more than one such group.",
296 name(), type == INDEX_MOL ? "molecules" : "residues");
297 GMX_THROW(InconsistentInputError(message));
303 Selection::printInfo(FILE *fp) const
305 fprintf(fp, "\"%s\" (%d position%s, %d atom%s%s)", name(),
306 posCount(), posCount() == 1 ? "" : "s",
307 atomCount(), atomCount() == 1 ? "" : "s",
308 isDynamic() ? ", dynamic" : "");
314 Selection::printDebugInfo(FILE *fp, int nmaxind) const
316 const gmx_ana_pos_t &p = data().rawPositions_;
320 fprintf(fp, " Group ");
322 gmx_ana_index_set(&g, p.m.mapb.nra, p.m.mapb.a, 0);
323 gmx_ana_index_dump(fp, &g, nmaxind);
325 fprintf(fp, " Block (size=%d):", p.m.mapb.nr);
328 fprintf(fp, " (null)");
333 if (nmaxind >= 0 && n > nmaxind)
337 for (int i = 0; i <= n; ++i)
339 fprintf(fp, " %d", p.m.mapb.index[i]);
349 if (nmaxind >= 0 && n > nmaxind)
353 fprintf(fp, " RefId:");
356 fprintf(fp, " (null)");
360 for (int i = 0; i < n; ++i)
362 fprintf(fp, " %d", p.m.refid[i]);
371 fprintf(fp, " MapId:");
374 fprintf(fp, " (null)");
378 for (int i = 0; i < n; ++i)
380 fprintf(fp, " %d", p.m.mapid[i]);
391 /********************************************************************
395 SelectionPosition::operator AnalysisNeighborhoodPositions() const
397 AnalysisNeighborhoodPositions pos(sel_->rawPositions_.x,
398 sel_->rawPositions_.count());
399 if (sel_->hasOnlyAtoms())
401 // TODO: Move atomIndices() such that it can be reused here as well.
402 pos.exclusionIds(constArrayRefFromArray<int>(sel_->rawPositions_.m.mapb.a,
403 sel_->rawPositions_.m.mapb.nra));
405 return pos.selectSingleFromArray(i_);