Simpatico  v1.10
ddMd/neighbor/CellList.h
1 #ifndef DDMD_CELL_LIST_H
2 #define DDMD_CELL_LIST_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 "Cell.h"
12 #include <ddMd/chemistry/Atom.h>
13 #include <simp/boundary/Boundary.h>
14 #include <util/space/Grid.h>
15 #include <util/containers/DArray.h>
16 #include <util/containers/GArray.h>
17 #include <util/global.h>
18 
19 namespace DdMd
20 {
21 
22  using namespace Util;
23  using namespace Simp;
24 
91  class CellList
92  {
93 
94  public:
95 
96  // Public methods
97 
101  CellList();
102 
108  virtual ~CellList();
109 
135  void allocate(int atomCapacity, const Vector& lower, const Vector& upper,
136  const Vector& cutoffs, int nCellCut = 1);
137 
152  void allocate(int atomCapacity, const Vector& lower, const Vector& upper,
153  double cutoff, int nCellCut = 1);
154 
177  void
178  makeGrid(const Vector& lower, const Vector& upper, const Vector& cutoffs,
179  int nCellCut = 1);
180 
194  void placeAtom(Atom &atom);
195 
204  void build();
205 
212  void update();
213 
217  void clear();
218 
222  const Grid& grid() const;
223 
229  double cellLength(int i) const;
230 
239  int cellIndexFromPosition(const Vector& position) const;
240 
244  const Cell* begin() const;
245 
251  const Cell& cell(int i) const;
252 
256  int nAtom() const;
257 
261  int nReject() const;
262 
263  #ifdef UTIL_DEBUG
264 
267  int maxNAtomCell() const;
268  #endif
269 
273  int atomCapacity() const;
274 
278  int cellCapacity() const;
279 
283  bool isAllocated() const;
284 
290  bool isBuilt() const;
291 
297  bool isValid() const;
298 
299  private:
300 
301  /*
302  * Temporary storage for atom pointers, before copying to cells.
303  */
304  struct Tag {
305  Atom* ptr;
306  int cellRank;
307  };
308 
310  Cell::OffsetArray offsets_;
311 
313  Grid grid_;
314 
316  DArray<Tag> tags_;
317 
319  DArray<CellAtom> atoms_;
320 
322  GArray<Cell> cells_;
323 
325  Vector lower_;
326 
328  Vector upper_;
329 
331  Vector cellLengths_;
332 
334  Vector lowerOuter_;
335 
337  Vector upperOuter_;
338 
340  Cell* begin_;
341 
343  int nAtom_;
344 
346  int nReject_;
347 
348  #ifdef UTIL_DEBUG
349  int maxNAtomCell_;
351  #endif
352 
354  bool isBuilt_;
355 
368  void setGridDimensions(const Vector& lower, const Vector& upper,
369  const Vector& cutoffs, int nCellCut);
370 
374  bool isValidAtomId(int atomId);
375 
376  };
377 
378  // Public inline method definitions:
379 
380  /*
381  * Identify the cell for an Atom, based on its position.
382  */
383  inline int CellList::cellIndexFromPosition(const Vector& position) const
384  {
385  IntVector r;
386  for (int i = 0; i < Dimension; ++i) {
387  if (position[i] <= lowerOuter_[i]) {
388  return -1;
389  }
390  if (position[i] >= upperOuter_[i]) {
391  return -1;
392  }
393  r[i] = int( (position[i] - lowerOuter_[i])/ cellLengths_[i] );
394  assert(r[i] < grid_.dimension(i));
395  }
396  return grid_.rank(r);
397  }
398 
399  /*
400  * Add an Atom to the appropriate cell, based on its position.
401  */
402  inline void CellList::placeAtom(Atom &atom)
403  {
404  // Preconditon
405  assert(nAtom_ < tags_.capacity());
406 
407  int rank = cellIndexFromPosition(atom.position());
408  if (rank >= 0) {
409  tags_[nAtom_].cellRank = rank;
410  tags_[nAtom_].ptr = &atom;
411  cells_[rank].incrementCapacity();
412  ++nAtom_;
413  } else {
414  ++nReject_;
415  }
416  }
417 
418  /*
419  * Return true iff atomId is valid, i.e., if 0 <= 0 < atomCapacity.
420  */
421  inline bool CellList::isValidAtomId(int atomId)
422  { return ( (0 <= atomId) && (atomId < tags_.capacity()) ); }
423 
424  /*
425  * Return associated Grid object.
426  */
427  inline const Grid& CellList::grid() const
428  { return grid_; }
429 
430  /*
431  * Return length of each cell in direction i.
432  */
433  inline double CellList::cellLength(int i) const
434  { return cellLengths_[i]; }
435 
436  /*
437  * Return reference to cell number i.
438  */
439  inline const Cell& CellList::cell(int i) const
440  {
441  assert(i < cells_.size());
442  return cells_[i];
443  }
444 
445  /*
446  * Return pointer to first Cell.
447  */
448  inline const Cell* CellList::begin() const
449  { return begin_; }
450 
451  /*
452  * Is this CellList allocated?
453  */
454  inline bool CellList::isAllocated() const
455  { return (cells_.capacity() > 0); }
456 
457 }
458 #endif
const Cell * begin() const
Return pointer to first local cell in linked list.
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
A cell list used only to identify nearby atom pairs.
An automatically growable array, analogous to a std::vector.
Definition: GArray.h:33
A Vector is a Cartesian vector.
Definition: Vector.h:75
void placeAtom(Atom &atom)
Determine the appropriate cell for an Atom, based on its position.
Vector & position()
Get position Vector by reference.
File containing preprocessor macros for error handling.
A point particle in an MD simulation.
Parallel domain decomposition (DD) MD simulation.
Classes used by all simpatico molecular simulations.
Utility classes for scientific computation.
Definition: accumulators.mod:1
bool isAllocated() const
Has memory been allocated for this CellList?
double cellLength(int i) const
Dimension of each cell in direction i.
An IntVector is an integer Cartesian vector.
Definition: IntVector.h:73
A grid of points indexed by integer coordinates.
Definition: Grid.h:33
const Grid & grid() const
Return Grid object by const reference.
int cellIndexFromPosition(const Vector &position) const
Return the index of the cell that contains a position Vector.
const Cell & cell(int i) const
Return a specified cell by const reference.
A single Cell in a CellList.