Simpatico  v1.10
AtomMap.h
1 #ifndef DDMD_ATOM_MAP_H
2 #define DDMD_ATOM_MAP_H
3 
4 /*
5 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include <ddMd/chemistry/Atom.h> // member template argument
12 #include <util/containers/DArray.h> // member template
13 #include <ddMd/chemistry/Group.h> // member function template
14 #include <util/global.h>
15 
16 #ifdef UTIL_CXX11
17 #include <unordered_map>
18 #else
19 #include <map>
20 #endif
21 
22 namespace Util {
23  template <typename T> class ArraySet;
24 }
25 
26 namespace DdMd
27 {
28 
29  using namespace Util;
30 
36  class AtomMap
37  {
38 
39  public:
40 
44  AtomMap();
45 
49  ~AtomMap();
50 
58  void allocate(int totalAtomCapacity);
59 
65  void addLocal(Atom* ptr);
66 
74  void removeLocal(Atom* ptr);
75 
81  void addGhost(Atom* ptr);
82 
91  void removeGhost(Atom* ptr);
92 
98  void clearGhosts(const ArraySet<Atom>& ghostSet);
99 
101 
103 
112  Atom* find(int atomId) const;
113 
117  int nLocal() const;
118 
122  int nGhostDistinct() const;
123 
127  int nGhost() const;
128 
145  template <int N>
146  int findGroupLocalAtoms(Group<N>& group) const;
147 
165  template <int N>
166  int findGroupGhostAtoms(Group<N>& group) const;
167 
173  bool isValid() const;
174 
176 
177  private:
178 
179  #ifdef UTIL_CXX11
180  typedef std::unordered_multimap<int, Atom*> GhostMap;
181  #else
182  typedef std::multimap<int, Atom*> GhostMap;
183  #endif
184 
185  // Array of pointers to atoms, indexed by Id.
186  // Elements corresponding to absent atoms hold null pointers.
187  DArray<Atom*> atomPtrs_;
188 
189  // Map for extra ghost images
190  GhostMap ghostMap_;
191 
193  int nLocal_;
194 
196  int nGhostDistinct_;
197 
198  // Maximum number of atoms on all processors, maximum id + 1
199  int totalAtomCapacity_;
200 
201  // Has this map been initialized (i.e., allocated)?
202  bool isInitialized_;
203 
204  /*
205  * Design / invariants:
206  *
207  * - If a local atom with id i is present, atomPtrs_[i]
208  * contains a pointer to that atom.
209  *
210  * - If one or more ghosts with an atom id i are present,
211  * but there is no local atom with that id, atomPtrs_[i]
212  * contains a pointer to one such ghost.
213  *
214  * - ghostMap_ contains pointers to all ghosts except those
215  * in atomPtrs_, stored using atom indices as keys.
216  *
217  * One image of each physical atom, identified by id, is thus
218  * stored * in atomPtrs_, while ghostMap_ holds any "extra"
219  * ghost images of atoms. If this processor does not contain
220  * multiple images of any particle, ghostMap_ will be empty.
221  */
222 
223  };
224 
225  // Inline function definitions
226 
227  /*
228  * Return pointer to an Atom with specified id.
229  */
230  inline Atom* AtomMap::find(int atomId) const
231  { return atomPtrs_[atomId]; }
232 
233  /*
234  * Return the number of local atoms.
235  */
236  inline int AtomMap::nLocal() const
237  { return nLocal_; }
238 
239  /*
240  * Return the number of ghosts with distinct ids.
241  */
242  inline int AtomMap::nGhostDistinct() const
243  { return nGhostDistinct_; }
244 
245  /*
246  * Return the total number of ghosts, including images.
247  */
248  inline int AtomMap::nGhost() const
249  { return nGhostDistinct_ + ghostMap_.size(); }
250 
251  // Function template definitions
252 
253  /*
254  * Set pointers to all atoms in a Group<N> object.
255  */
256  template <int N>
257  int AtomMap::findGroupLocalAtoms(Group<N>& group) const
258  {
259  Atom* ptr;
260  int nAtom = 0;
261  for (int i = 0; i < N; ++i) {
262  ptr = atomPtrs_[group.atomId(i)];
263  if (ptr) {
264  assert(!ptr->isGhost());
265  assert(ptr->id() == group.atomId(i));
266  group.setAtomPtr(i, ptr);
267  ++nAtom;
268  } else {
269  group.clearAtomPtr(i);
270  }
271  }
272  return nAtom;
273  }
274 
275  /*
276  * Set pointers to atoms in a Group<N> object.
277  */
278  template <int N>
279  int AtomMap::findGroupGhostAtoms(Group<N>& group) const
280  {
281  Atom* ptr;
282  int nAtom = 0;
283  for (int i = 0; i < N; ++i) {
284  if (group.atomPtr(i) != 0) {
285  // assert(!(ptr->isGhost()));
286  ++nAtom;
287  } else {
288  int atomId = group.atomId(i);
289  ptr = atomPtrs_[atomId];
290  if (ptr) {
291  assert(ptr->isGhost());
292  assert(ptr->id() == atomId);
293  group.setAtomPtr(i, ptr);
294  ++nAtom;
295  } else {
296  group.clearAtomPtr(i);
297  }
298  }
299  }
300  return nAtom;
301  }
302 
303 }
304 #endif
void setAtomPtr(int i, Atom *atomPtr)
Set the pointer to a specific atom.
Associative container for finding atoms identified by integer id.
Definition: AtomMap.h:36
File containing preprocessor macros for error handling.
Atom * atomPtr(int i) const
Get a pointer to a specific Atom.
A point particle in an MD simulation.
Parallel domain decomposition (DD) MD simulation.
int id() const
Get unique global index for this atom.
bool isGhost() const
Is this atom a ghost?
A container for pointers to a subset of elements of an associated array.
Definition: ArraySet.h:46
Utility classes for scientific computation.
Definition: accumulators.mod:1
Dynamically allocatable contiguous array template.
Definition: DArray.h:31
A group of covalently interacting atoms.
void clearAtomPtr(int i)
Clear the pointer to a specific atom.
int atomId(int i) const
Get the id for a specific atom in the Group.