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;
129 SelectionData::initializeMassesAndCharges(const t_topology *top)
131 posInfo_.reserve(posCount());
132 for (int b = 0; b < posCount(); ++b)
139 for (int i = rawPositions_.m.mapb.index[b];
140 i < rawPositions_.m.mapb.index[b+1];
143 int index = rawPositions_.g->index[i];
144 mass += top->atoms.atom[index].m;
145 charge += top->atoms.atom[index].q;
148 posInfo_.push_back(PositionInfo(mass, charge));
150 if (isDynamic() && !hasFlag(efSelection_DynamicMask))
152 originalPosInfo_ = posInfo_;
158 SelectionData::refreshMassesAndCharges()
160 if (!originalPosInfo_.empty())
163 for (int i = 0; i < posCount(); ++i)
165 int refid = rawPositions_.m.refid[i];
166 posInfo_.push_back(originalPosInfo_[refid]);
173 SelectionData::updateCoveredFractionForFrame()
175 if (isCoveredFractionDynamic())
177 real cfrac = _gmx_selelem_estimate_coverfrac(rootElement());
178 coveredFraction_ = cfrac;
179 averageCoveredFraction_ += cfrac;
185 SelectionData::computeAverageCoveredFraction(int nframes)
187 if (isCoveredFractionDynamic() && nframes > 0)
189 averageCoveredFraction_ /= nframes;
195 SelectionData::restoreOriginalPositions()
199 gmx_ana_pos_t &p = rawPositions_;
200 gmx_ana_index_copy(p.g, rootElement().v.u.g, false);
202 gmx_ana_indexmap_update(&p.m, p.g, hasFlag(gmx::efSelection_DynamicMask));
204 refreshMassesAndCharges();
208 } // namespace internal
212 Selection::printInfo(FILE *fp) const
214 fprintf(fp, "\"%s\" (%d position%s, %d atom%s%s)", name(),
215 posCount(), posCount() == 1 ? "" : "s",
216 atomCount(), atomCount() == 1 ? "" : "s",
217 isDynamic() ? ", dynamic" : "");
223 Selection::printDebugInfo(FILE *fp, int nmaxind) const
225 const gmx_ana_pos_t &p = data().rawPositions_;
230 gmx_ana_index_dump(fp, p.g, -1, nmaxind);
232 fprintf(fp, " Block (size=%d):", p.m.mapb.nr);
235 fprintf(fp, " (null)");
240 if (nmaxind >= 0 && n > nmaxind)
244 for (int i = 0; i <= n; ++i)
246 fprintf(fp, " %d", p.m.mapb.index[i]);
256 if (nmaxind >= 0 && n > nmaxind)
260 fprintf(fp, " RefId:");
263 fprintf(fp, " (null)");
267 for (int i = 0; i < n; ++i)
269 fprintf(fp, " %d", p.m.refid[i]);
278 fprintf(fp, " MapId:");
281 fprintf(fp, " (null)");
285 for (int i = 0; i < n; ++i)
287 fprintf(fp, " %d", p.m.mapid[i]);