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(t_selelem *elem, const char *selstr)
51 : name_(elem->name), selectionText_(selstr),
52 rootElement_(elem), coveredFractionType_(CFRAC_NONE),
53 coveredFraction_(1.0), averageCoveredFraction_(1.0),
54 bDynamic_(false), bDynamicCoveredFraction_(false)
56 gmx_ana_pos_clear(&rawPositions_);
58 if (elem->child->type == SEL_CONST)
60 // TODO: This is not exception-safe if any called function throws.
61 gmx_ana_pos_copy(&rawPositions_, elem->child->v.u.p, true);
68 child->flags &= ~SEL_ALLOCVAL;
69 _gmx_selvalue_setstore(&child->v, &rawPositions_);
70 /* We should also skip any modifiers to determine the dynamic
72 while (child->type == SEL_MODIFIER)
75 if (child->type == SEL_SUBEXPRREF)
78 /* Because most subexpression elements are created
79 * during compilation, we need to check for them
82 if (child->type == SEL_SUBEXPR)
88 /* For variable references, we should skip the
89 * SEL_SUBEXPRREF and SEL_SUBEXPR elements. */
90 if (child->type == SEL_SUBEXPRREF)
92 child = child->child->child;
94 bDynamic_ = (child->child->flags & SEL_DYNAMIC);
96 initCoveredFraction(CFRAC_NONE);
100 SelectionData::~SelectionData()
102 gmx_ana_pos_deinit(&rawPositions_);
107 SelectionData::initCoveredFraction(e_coverfrac_t type)
109 coveredFractionType_ = type;
110 if (type == CFRAC_NONE || rootElement_ == NULL)
112 bDynamicCoveredFraction_ = false;
114 else if (!_gmx_selelem_can_estimate_cover(rootElement_))
116 coveredFractionType_ = CFRAC_NONE;
117 bDynamicCoveredFraction_ = false;
121 bDynamicCoveredFraction_ = true;
123 coveredFraction_ = bDynamicCoveredFraction_ ? 0.0 : 1.0;
124 averageCoveredFraction_ = coveredFraction_;
125 return type == CFRAC_NONE || coveredFractionType_ != CFRAC_NONE;
130 SelectionData::initializeMassesAndCharges(const t_topology *top)
132 posInfo_.reserve(posCount());
133 for (int b = 0; b < posCount(); ++b)
140 for (int i = rawPositions_.m.mapb.index[b];
141 i < rawPositions_.m.mapb.index[b+1];
144 int index = rawPositions_.g->index[i];
145 mass += top->atoms.atom[index].m;
146 charge += top->atoms.atom[index].q;
149 posInfo_.push_back(PositionInfo(mass, charge));
151 if (isDynamic() && !hasFlag(efSelection_DynamicMask))
153 originalPosInfo_ = posInfo_;
159 SelectionData::refreshMassesAndCharges()
161 if (!originalPosInfo_.empty())
164 for (int i = 0; i < posCount(); ++i)
166 int refid = rawPositions_.m.refid[i];
167 posInfo_.push_back(originalPosInfo_[refid]);
174 SelectionData::updateCoveredFractionForFrame()
176 if (isCoveredFractionDynamic())
178 real cfrac = _gmx_selelem_estimate_coverfrac(rootElement());
179 coveredFraction_ = cfrac;
180 averageCoveredFraction_ += cfrac;
186 SelectionData::computeAverageCoveredFraction(int nframes)
188 if (isCoveredFractionDynamic() && nframes > 0)
190 averageCoveredFraction_ /= nframes;
196 SelectionData::restoreOriginalPositions()
200 gmx_ana_pos_t &p = rawPositions_;
201 gmx_ana_index_copy(p.g, rootElement_->v.u.g, false);
203 gmx_ana_indexmap_update(&p.m, p.g, hasFlag(gmx::efSelection_DynamicMask));
205 refreshMassesAndCharges();
209 } // namespace internal
213 Selection::printInfo(FILE *fp) const
215 fprintf(fp, "\"%s\" (%d position%s, %d atom%s%s)", name(),
216 posCount(), posCount() == 1 ? "" : "s",
217 atomCount(), atomCount() == 1 ? "" : "s",
218 isDynamic() ? ", dynamic" : "");
224 Selection::printDebugInfo(FILE *fp, int nmaxind) const
226 const gmx_ana_pos_t &p = data().rawPositions_;
231 gmx_ana_index_dump(fp, p.g, -1, nmaxind);
233 fprintf(fp, " Block (size=%d):", p.m.mapb.nr);
236 fprintf(fp, " (null)");
241 if (nmaxind >= 0 && n > nmaxind)
243 for (int i = 0; i <= n; ++i)
244 fprintf(fp, " %d", p.m.mapb.index[i]);
251 if (nmaxind >= 0 && n > nmaxind)
253 fprintf(fp, " RefId:");
256 fprintf(fp, " (null)");
260 for (int i = 0; i < n; ++i)
261 fprintf(fp, " %d", p.m.refid[i]);
267 fprintf(fp, " MapId:");
270 fprintf(fp, " (null)");
274 for (int i = 0; i < n; ++i)
275 fprintf(fp, " %d", p.m.mapid[i]);