All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
NeighborList.cpp
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 The plumed team
3  (see the PEOPLE file at the root of the distribution for a list of names)
4 
5  See http://www.plumed-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
8 
9  plumed is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  plumed 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
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 #include <vector>
23 #include <algorithm>
24 #include "Vector.h"
25 #include "Pbc.h"
26 #include "AtomNumber.h"
27 #include "Tools.h"
28 #include "NeighborList.h"
29 
30 namespace PLMD{
31 using namespace std;
32 
33 NeighborList::NeighborList(const vector<AtomNumber>& list0, const vector<AtomNumber>& list1,
34  const bool& do_pair, const bool& do_pbc, const Pbc& pbc,
35  const double& distance, const unsigned& stride): reduced(false),
36  do_pair_(do_pair), do_pbc_(do_pbc), pbc_(&pbc),
37  distance_(distance), stride_(stride)
38 {
39 // store full list of atoms needed
40  fullatomlist_=list0;
41  fullatomlist_.insert(fullatomlist_.end(),list1.begin(),list1.end());
42  nlist0_=list0.size();
43  nlist1_=list1.size();
44  twolists_=true;
45  if(!do_pair){
47  }else{
48  plumed_assert(nlist0_==nlist1_);
50  }
51  initialize();
52  lastupdate_=0;
53 }
54 
55 NeighborList::NeighborList(const vector<AtomNumber>& list0, const bool& do_pbc,
56  const Pbc& pbc, const double& distance,
57  const unsigned& stride): reduced(false),
58  do_pbc_(do_pbc), pbc_(&pbc),
59  distance_(distance), stride_(stride){
60  fullatomlist_=list0;
61  nlist0_=list0.size();
62  twolists_=false;
64  initialize();
65  lastupdate_=0;
66 }
67 
69  neighbors_.clear();
70  for(unsigned int i=0;i<nallpairs_;++i){
71  neighbors_.push_back(getIndexPair(i));
72  }
73 }
74 
75 vector<AtomNumber>& NeighborList::getFullAtomList() {
76  return fullatomlist_;
77 }
78 
79 pair<unsigned,unsigned> NeighborList::getIndexPair(unsigned ipair) {
80  pair<unsigned,unsigned> index;
81  if(twolists_ && do_pair_){
82  index=pair<unsigned,unsigned>(ipair,ipair+nlist0_);
83  }else if (twolists_ && !do_pair_){
84  index=pair<unsigned,unsigned>(ipair/nlist1_,ipair%nlist1_+nlist0_);
85  }else if (!twolists_){
86  unsigned ii = nallpairs_-1-ipair;
87  unsigned K = unsigned(floor((sqrt(double(8*ii+1))+1)/2));
88  unsigned jj = ii-K*(K-1)/2;
89  index=pair<unsigned,unsigned>(nlist0_-1-K,nlist0_-1-jj);
90  }
91  return index;
92 }
93 
94 void NeighborList::update(const vector<Vector>& positions) {
95  neighbors_.clear();
96  const double d2=distance_*distance_;
97 // check if positions array has the correct length
98  plumed_assert(positions.size()==fullatomlist_.size());
99  for(unsigned int i=0;i<nallpairs_;++i){
100  pair<unsigned,unsigned> index=getIndexPair(i);
101  unsigned index0=index.first;
102  unsigned index1=index.second;
103  Vector distance;
104  if(do_pbc_){
105  distance=pbc_->distance(positions[index0],positions[index1]);
106  } else {
107  distance=delta(positions[index0],positions[index1]);
108  }
109  double value=modulo2(distance);
110  if(value<=d2) {neighbors_.push_back(index);}
111  }
112  setRequestList();
113 }
114 
116  requestlist_.clear();
117  for(unsigned int i=0;i<size();++i){
118  requestlist_.push_back(fullatomlist_[neighbors_[i].first]);
119  requestlist_.push_back(fullatomlist_[neighbors_[i].second]);
120  }
122  reduced=false;
123 }
124 
125 vector<AtomNumber>& NeighborList::getReducedAtomList() {
126  if(!reduced)for(unsigned int i=0;i<size();++i){
127  unsigned newindex0=0,newindex1=0;
128  AtomNumber index0=fullatomlist_[neighbors_[i].first];
129  AtomNumber index1=fullatomlist_[neighbors_[i].second];
130 // I exploit the fact that requestlist_ is an ordered vector
131  vector<AtomNumber>::iterator p;
132  p = std::find(requestlist_.begin(), requestlist_.end(), index0); plumed_assert(p!=requestlist_.end()); newindex0=p-requestlist_.begin();
133  p = std::find(requestlist_.begin(), requestlist_.end(), index1); plumed_assert(p!=requestlist_.end()); newindex1=p-requestlist_.begin();
134  neighbors_[i]=pair<unsigned,unsigned>(newindex0,newindex1);
135  }
136  reduced=true;
137  return requestlist_;
138 }
139 
140 unsigned NeighborList::getStride() const {
141  return stride_;
142 }
143 
144 unsigned NeighborList::getLastUpdate() const {
145  return lastupdate_;
146 }
147 
148 void NeighborList::setLastUpdate(unsigned step) {
149  lastupdate_=step;
150 }
151 
152 unsigned NeighborList::size() const {
153  return neighbors_.size();
154 }
155 
156  pair<unsigned,unsigned> NeighborList::getClosePair(unsigned i) const {
157  return neighbors_[i];
158 }
159 
160 vector<unsigned> NeighborList::getNeighbors(unsigned index) {
161  vector<unsigned> neighbors;
162  for(unsigned int i=0;i<size();++i){
163  if(neighbors_[i].first==index) neighbors.push_back(neighbors_[i].second);
164  if(neighbors_[i].second==index) neighbors.push_back(neighbors_[i].first);
165  }
166  return neighbors;
167 }
168 
169 }
Simple class to store the index of an atom.
Definition: AtomNumber.h:39
std::vector< std::pair< unsigned, unsigned > > neighbors_
Definition: NeighborList.h:42
std::vector< PLMD::AtomNumber > & getFullAtomList()
Return the list of all atoms. These are needed to rebuild the neighbor list.
Class implementing fixed size vectors of doubles.
Definition: Vector.h:74
std::vector< unsigned > getNeighbors(unsigned i)
Get the list of neighbors of the i-th atom.
STL namespace.
std::vector< PLMD::AtomNumber > requestlist_
Definition: NeighborList.h:41
std::vector< PLMD::AtomNumber > fullatomlist_
Definition: NeighborList.h:41
void setRequestList()
Extract the list of atoms from the current list of close pairs.
void update(const std::vector< PLMD::Vector > &positions)
Update the neighbor list and prepare the new list of atoms that will be requested to the main code...
Definition: Pbc.h:38
void setLastUpdate(unsigned step)
Set the step of the last update.
Vector distance(const Vector &, const Vector &, int *nshifts) const
internal version of distance, also returns the number of attempted shifts (used in Pbc::test())...
Definition: Pbc.cpp:158
std::pair< unsigned, unsigned > getClosePair(unsigned i) const
Get the i-th pair of the neighbor list.
VectorGeneric< n > delta(const VectorGeneric< n > &v1, const VectorGeneric< n > &v2)
Definition: Vector.h:262
std::pair< unsigned, unsigned > getIndexPair(unsigned i)
Return the pair of indexes in the positions array of the two atoms forming the i-th pair among all po...
double modulo2(const VectorGeneric< n > &v)
Definition: Vector.h:330
void initialize()
Initialize the neighbor list with all possible pairs.
static void removeDuplicates(std::vector< T > &vec)
Remove duplicates from a vector of type T.
Definition: Tools.h:150
unsigned getStride() const
Get the update stride of the neighbor list.
std::vector< PLMD::AtomNumber > & getReducedAtomList()
Update the indexes in the neighbor list to match the ordering in the new positions array and return t...
const PLMD::Pbc * pbc_
Definition: NeighborList.h:40
unsigned lastupdate_
Definition: NeighborList.h:44
unsigned getLastUpdate() const
Get the last step in which the neighbor list was updated.
NeighborList(const std::vector< PLMD::AtomNumber > &list0, const std::vector< PLMD::AtomNumber > &list1, const bool &do_pair, const bool &do_pbc, const PLMD::Pbc &pbc, const double &distance=1.0e+30, const unsigned &stride=0)
unsigned size() const
Get the size of the neighbor list.