ParM  parm
A molecular dynamics library
Go to the documentation of this file.
1 #include "box.hpp"
3 #ifndef TRACKERS_H
4 #define TRACKERS_H
6 #include <set>
7 #include <map>
8 #include "assert.h"
16 class StateTracker {
17  public:
19  virtual void update(Box &box) = 0;
20  virtual ~StateTracker(){};
21 };
29 class PairList {
30  protected:
31  map<const AtomID, set<AtomID> > pairs;
32  public:
33  //~ PairList(AtomGroup *group);
34  PairList(){};
37  inline void ensure(const AtomID a){
38  pairs.insert(std::pair<AtomID, set<AtomID> >(a, set<AtomID>()));
39  }
41  inline void ensure(vector<AtomID> ps){
42  vector<AtomID>::iterator it;
43  for(it=ps.begin(); it != ps.end(); ++it) ensure(*it);
44  }
46  inline void ensure(AtomGroup &group){
47  for(uint i=0; i<group.size(); i++) ensure(group.get_id(i));
48  }
53  inline bool has_pair(AtomID a1, AtomID a2){
54  if(a1 > a2) return pairs[a1].count(a2) > 0;
55  else return pairs[a2].count(a1) > 0;
56  }
61  inline void add_pair(AtomID a1, AtomID a2){
62  //~ cout << "PairList ignore " << a1.n() << '-' << a2.n() << "\n";
63  if(a1 > a2){pairs[a1].insert(a2);}
64  else{pairs[a2].insert(a1);};
65  }
71  inline void erase_pair(AtomID a1, AtomID a2){
72  if(a1 > a2){pairs[a1].erase(a2);}
73  else{pairs[a2].erase(a1);};
74  }
83  inline set<AtomID> get_pairs(const AtomID a){ensure(a); return pairs[a];};
86  inline set<AtomID>::iterator begin(const AtomID a){return pairs[a].begin();};
88  inline set<AtomID>::iterator end(const AtomID a){return pairs[a].end();};
91  inline uint size() const { uint N=0; for(
92  map<const AtomID, set<AtomID> >::const_iterator it=pairs.begin();
93  it != pairs.end(); ++it) N+= (uint) it->second.size();
94  return N;
95  };
98  void clear();
99 };
118 class NeighborList : public StateTracker{
119  protected:
120  sptr<Box> box;
123  vector<flt> diameters;
124  vector<IDPair> curpairs;
126  vector<Vec> lastlocs;
128  bool ignorechanged; /*< if true, forces a full check on next update */
129  public:
130  NeighborList(sptr<Box> box, sptr<AtomVec> atoms, const flt skin);
134  void update(Box &newbox){assert(&newbox == box.get()); update_list(false);};
140  bool update_list(bool force = true);
141  // if force = false, we check if updating necessary first
143  AtomVec& vec(){return atoms.vec();};
144  inline uint which(){return updatenum;};
145  inline uint numpairs(){return (uint) curpairs.size();};
146  inline void ignore(AtomID a, AtomID b){ignorepairs.add_pair(a,b); ignorechanged=true;};
147  void add(AtomID a, flt diameter){
148  atoms.add(a);
149  //assert(diameters.size() == atoms.size() - 1);
150  diameters.push_back(diameter);
151  //assert(lastlocs.size() == atoms.size() - 1);
152  lastlocs.push_back(a->x);
153  ignorechanged = true;
154  }
157  inline uint ignore_size() const{return ignorepairs.size();};
159  inline uint size() const{return atoms.size();};
160  inline vector<IDPair>::iterator begin(){return curpairs.begin();};
161  inline vector<IDPair>::iterator end(){return curpairs.end();};
162  inline IDPair get(uint i){
163  //assert(i<curpairs.size());
164  return curpairs[i];
165  };
167 };
169 class GridIterator;
170 class GridPairedIterator;
178 class Grid {
179  public:
180  sptr<OriginBox> box;
181  sptr<AtomGroup> atoms;
193  vector<set<AtomID> > gridlocs;
195  vector<uint> neighbors(uint i);
196  uint get_loc(Vec v, Vec bsize);
198  public:
205  Grid(sptr<OriginBox> box, sptr<AtomGroup> atoms, const uint width=1);
210  Grid(sptr<OriginBox> box, sptr<AtomGroup> atoms, vector<uint> width);
216  Grid(sptr<OriginBox> box, sptr<AtomGroup> atoms, const flt minwidth, const flt goalwidth);
222  void optimize_widths();
226  void make_grid();
228  friend class GridIterator;
229  friend class GridPairedIterator;
231  iterator begin();
232  iterator end();
235  pair_iter pairs(AtomID a);
237  flt time_to_edge(Atom& a);
238  flt time_to_edge(uint i){return time_to_edge((*atoms)[i]);}
241  vector<IDPair> all_pairs();
243  vector<AtomID> all_pairs(AtomID a);
245  uint numcells(uint i){assert(i<NDIM); return widths[i];}
246  #ifdef VEC2D
247  uint numcells(){ return widths[0] * widths[1];};
248  #else
249  uint numcells(){ return widths[0] * widths[1] * widths[2];};
250  #endif
251 };
255  protected:
259  vector<uint> neighbor_cells;
260  vector<uint>::iterator cellnum2;
261  set<AtomID> *cell2;
262  set<AtomID>::iterator atom2;
265  bool increment_cell2();
266  bool increment_atom2();
268  public:
269  typedef set<AtomID>::iterator end_type;
271  GridPairedIterator(Grid & grid, AtomID a);
274  AtomID operator*(){return *atom2;};
275  bool operator==(const GridPairedIterator &other);
277  bool operator!=(const GridPairedIterator &other){
278  return !(*this == other);
279  };
281  bool operator==(const end_type other){
282  return (atom2 == other);
283  }
284  bool operator!=(const end_type other){
285  return !(atom2 == other);
286  };
288  end_type end(){return grid.gridlocs[neighbor_cells.back()].end();};
289 };
293  protected:
295  vector<set<AtomID> >::iterator cell1;
296  set<AtomID>::iterator atom1;
298  vector<uint> neighbor_cells;
299  vector<uint>::iterator cellnum2;
300  set<AtomID> *cell2;
301  set<AtomID>::iterator atom2;
303  // returns "successful increment", e.g. cell1 points to an element
304  bool increment_cell1();
305  bool increment_atom1();
306  bool increment_cell2();
307  bool increment_atom2();
309  public:
310  GridIterator(Grid & grid);
311  GridIterator(Grid & grid, vector<set<AtomID> >::iterator cell1);
314  IDPair operator*(){return IDPair(*atom1, *atom2);};
315  bool operator==(const GridIterator &other);
317  bool operator!=(const GridIterator &other){
318  return !(*this == other);
319  };
320 };
322 #endif
set< AtomID > * cell2
Definition: trackers.hpp:300
pair_iter pairs(AtomID a)
Get all pairs of a specific Atom.
Definition: trackers.cpp:227
void update(Box &newbox)
Calls update_list(false); exists as a requirement of StateTracker.
Definition: trackers.hpp:134
The basic class for representing each particle.
Definition: box.hpp:229
Grid & grid
Definition: trackers.hpp:294
vector< uint > neighbor_cells
Definition: trackers.hpp:259
Grid(sptr< OriginBox > box, sptr< AtomGroup > atoms, const uint width=1)
/param width Number of divisions along all axes
Definition: trackers.cpp:82
IDPair operator*()
Definition: trackers.hpp:314
iterator end()
Definition: trackers.cpp:223
Grid & grid
Definition: trackers.hpp:256
AtomID operator*()
Definition: trackers.hpp:274
void erase_pair(AtomID a1, AtomID a2)
Remove a pair from the list.
Definition: trackers.hpp:71
void ensure(vector< AtomID > ps)
Ensure that every Atom in input is in the overall list.
Definition: trackers.hpp:41
bool operator!=(const end_type other)
Definition: trackers.hpp:284
vector< IDPair > all_pairs()
Return a list of all pairs. SLOW function, but useful for debugging.
Definition: trackers.cpp:250
For iterating over all pairs of a single Atom.
Definition: trackers.hpp:254
a group of atoms, such as all of them (AtomVec), or a smaller group such as a molecule, sidebranch, etc.
Definition: box.hpp:312
void ensure(const AtomID a)
Ensure that a given Atom is in the overall list.
Definition: trackers.hpp:37
bool increment_atom2()
Definition: trackers.cpp:300
bool increment_atom1()
Definition: trackers.cpp:396
Maintains a Verlet list of "neighbors": molecules within a 'skin radius' of each other.
Definition: trackers.hpp:118
bool increment_cell2()
returns "successful increment", e.g.
Definition: trackers.cpp:292
sptr< OriginBox > box
Definition: trackers.hpp:180
sptr< AtomGroup > atoms
Definition: trackers.hpp:181
The main class for representing particles.
Definition: box.hpp:412
unsigned int uint
Definition: vec.hpp:20
Definition: trackers.hpp:34
double flt
The basic floating point type used in the simulations.
Definition: vecrand.hpp:45
uint numcells()
Definition: trackers.hpp:247
set< AtomID > * cell2
Definition: trackers.hpp:261
void ignore(AtomID a, AtomID b)
Definition: trackers.hpp:146
set< AtomID >::iterator atom2
Definition: trackers.hpp:262
void add(AtomID a, flt diameter)
Definition: trackers.hpp:147
vector< uint > neighbors(uint i)
Definition: trackers.cpp:112
map< const AtomID, set< AtomID > > pairs
Definition: trackers.hpp:31
void add(AtomID a)
Definition: box.hpp:464
A mapping of Atom -> [list of Atom], used by NeighborList to keep track of what atoms are near what o...
Definition: trackers.hpp:29
flt time_to_edge(uint i)
Definition: trackers.hpp:238
set< AtomID >::iterator atom1
Definition: trackers.hpp:296
vector< uint >::iterator cellnum2
Definition: trackers.hpp:299
virtual uint size() const =0
Number of atoms in the group.
A pointer to an Atom, that also knows its own index in an AtomVec.
Definition: box.hpp:270
flt minwidth
minwidth is minimum size of a box, in real units.
Definition: trackers.hpp:190
virtual ~StateTracker()
Definition: trackers.hpp:20
virtual void update(Box &box)=0
This function is called once per timestep, when particles are in their set position.
flt skin
Definition: trackers.hpp:121
vector< flt > diameters
Definition: trackers.hpp:123
set< AtomID >::iterator atom2
Definition: trackers.hpp:301
For iterating over all possible pairs.
Definition: trackers.hpp:292
AtomVec & vec()
Definition: box.hpp:460
uint size() const
Number of atoms in list.
Definition: trackers.hpp:159
end_type end()
Definition: trackers.hpp:288
GridIterator & operator++()
Definition: trackers.cpp:431
NeighborList(sptr< Box > box, sptr< AtomVec > atoms, const flt skin)
Definition: trackers.cpp:10
set< AtomID >::iterator end_type
Definition: trackers.hpp:269
vector< set< AtomID > > gridlocs
Atoms in each grid location.
Definition: trackers.hpp:193
void make_grid()
Reshape the grid to optimize number of Atom pairs.
Definition: trackers.cpp:205
bool increment_cell2()
Definition: trackers.cpp:407
AtomID atom1
Definition: trackers.hpp:257
sptr< Box > box
Definition: trackers.hpp:120
bool operator==(const GridIterator &other)
Definition: trackers.cpp:438
set< AtomID > get_pairs(const AtomID a)
Get all atoms neighboring an Atom that are "lesser" than the Atom.
Definition: trackers.hpp:83
uint numpairs()
Definition: trackers.hpp:145
uint get_loc(Vec v, Vec bsize)
Definition: trackers.cpp:190
vector< IDPair >::iterator end()
Definition: trackers.hpp:161
void optimize_widths()
Reshape the grid to optimize number of Atom pairs.
Definition: trackers.cpp:168
void clear()
Clear the inner lists.
Definition: trackers.cpp:3
GridIterator(Grid &grid)
Definition: trackers.cpp:322
bool update_list(bool force=true)
Update the neighbor list based on current positions.
Definition: trackers.cpp:14
iterator begin()
Iterator over pairs.
Definition: trackers.cpp:219
virtual AtomID get_id(cuint n)=0
vector< IDPair > curpairs
Definition: trackers.hpp:124
vector< uint > neighbor_cells
Definition: trackers.hpp:298
SubGroup atoms
Definition: trackers.hpp:122
vector< IDPair >::iterator begin()
Definition: trackers.hpp:160
void ensure(AtomGroup &group)
Ensure that every Atom in input is in the overall list.
Definition: trackers.hpp:46
uint updatenum
Definition: trackers.hpp:127
bool ignorechanged
Definition: trackers.hpp:128
bool operator!=(const GridIterator &other)
Definition: trackers.hpp:317
void add_pair(AtomID a1, AtomID a2)
Add a pair to the list.
Definition: trackers.hpp:61
vector< set< AtomID > >::iterator cell1
Definition: trackers.hpp:295
AtomVec & vec()
Definition: trackers.hpp:143
The general interface for a "tracker", a class that needs to be called every timestep.
Definition: trackers.hpp:16
vector< uint >::iterator cellnum2
Definition: trackers.hpp:260
#define NDIM
Definition: vecrand.hpp:12
Definition: box.hpp:282
GridPairedIterator pair_iter
Definition: trackers.hpp:200
uint size() const
Number of atoms in the group.
Definition: box.hpp:471
uint widths[NDIM]
Number of box divisions per dimension.
Definition: trackers.hpp:192
GridPairedIterator(Grid &grid, AtomID a)
Definition: trackers.cpp:269
PairList ignorepairs
Definition: trackers.hpp:125
bool increment_cell1()
Definition: trackers.cpp:387
bool has_pair(AtomID a1, AtomID a2)
Check if a given pair is is the list.
Definition: trackers.hpp:53
The virtual interface for the shape of the space and its boundaries.
Definition: box.hpp:50
Definition: trackers.hpp:166
bool increment_atom2()
Definition: trackers.cpp:419
Eigen::Matrix< flt, NDIM, 1 > Vec
The basic physics vector.
Definition: vecrand.hpp:53
Definition: box.hpp:454
A fast algorithm for finding all pairs of neighboring atoms.
Definition: trackers.hpp:178
set< AtomID >::iterator begin(const AtomID a)
for iterating over neighbors
Definition: trackers.hpp:86
uint numcells(uint i)
Definition: trackers.hpp:245
flt time_to_edge(Atom &a)
Find the amount of time until an Atom leaves its current cell.
Definition: trackers.cpp:231
bool operator!=(const GridPairedIterator &other)
Definition: trackers.hpp:277
flt goalwidth
goalwidth is how many atoms per box (goal).
Definition: trackers.hpp:191
uint ignore_size() const
Number of pairs currently being ignored.
Definition: trackers.hpp:157
uint size() const
for iterating over neighbors
Definition: trackers.hpp:91
set< AtomID >::iterator end(const AtomID a)
for iterating over neighbors
Definition: trackers.hpp:88
bool operator==(const end_type other)
Definition: trackers.hpp:281
GridIterator iterator
Definition: trackers.hpp:199
bool operator==(const GridPairedIterator &other)
Definition: trackers.cpp:315
GridPairedIterator & operator++()
Definition: trackers.cpp:310
uint which()
Definition: trackers.hpp:144
vector< Vec > lastlocs
Definition: trackers.hpp:126