| [357fba] | 1 | /*
 | 
|---|
 | 2 |  * linkedcell.hpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  If the linked cell should be usable, the class has to inherit LCNodeSet and the nodes (containing the Vectors) have to inherit LCNode. This works well
 | 
|---|
 | 5 |  *  for molecule and atom classes.
 | 
|---|
 | 6 |  *
 | 
|---|
 | 7 |  *  Created on: Aug 3, 2009
 | 
|---|
 | 8 |  *      Author: heber
 | 
|---|
 | 9 |  */
 | 
|---|
 | 10 | 
 | 
|---|
| [e1bc68] | 11 | #ifndef LINKEDCELL_HPP_
 | 
|---|
 | 12 | #define LINKEDCELL_HPP_
 | 
|---|
 | 13 | 
 | 
|---|
| [357fba] | 14 | using namespace std;
 | 
|---|
 | 15 | 
 | 
|---|
| [f66195] | 16 | /*********************************************** includes ***********************************/
 | 
|---|
 | 17 | 
 | 
|---|
| [e1bc68] | 18 | // include config.h
 | 
|---|
 | 19 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 20 | #include <config.h>
 | 
|---|
 | 21 | #endif
 | 
|---|
 | 22 | 
 | 
|---|
| [357fba] | 23 | #include <list>
 | 
|---|
| [af2c424] | 24 | #include <vector>
 | 
|---|
 | 25 | #include <map>
 | 
|---|
| [357fba] | 26 | 
 | 
|---|
| [af2c424] | 27 | #include "Helpers/Assert.hpp"
 | 
|---|
 | 28 | #include "Helpers/Log.hpp"
 | 
|---|
 | 29 | #include "Helpers/Verbose.hpp"
 | 
|---|
| [e4fe8d] | 30 | #include "Helpers/defs.hpp"
 | 
|---|
| [af2c424] | 31 | #include "World.hpp"
 | 
|---|
| [57f243] | 32 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [357fba] | 33 | 
 | 
|---|
| [f66195] | 34 | /****************************************** forward declarations *****************************/
 | 
|---|
 | 35 | 
 | 
|---|
| [357fba] | 36 | class PointCloud;
 | 
|---|
| [f66195] | 37 | class TesselPoint;
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | /********************************************** definitions *********************************/
 | 
|---|
| [e1bc68] | 40 | 
 | 
|---|
| [c66537] | 41 | //< Upper bound for number of cell nodes used in sensibility check on allocation.
 | 
|---|
 | 42 | enum { MAX_LINKEDCELLNODES = 1000000 };
 | 
|---|
| [6ac7ee] | 43 | 
 | 
|---|
| [f66195] | 44 | /********************************************** declarations *******************************/
 | 
|---|
 | 45 | 
 | 
|---|
| [357fba] | 46 | /** Linked Cell class for containing Vectors in real space efficiently.
 | 
|---|
 | 47 |  */
 | 
|---|
 | 48 | class LinkedCell {
 | 
|---|
| [734816] | 49 | private:
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 | public:
 | 
|---|
| [af2c424] | 52 |   class LinkedNodes : public std::list<TesselPoint *> {
 | 
|---|
 | 53 |   public:
 | 
|---|
 | 54 |     LinkedNodes();
 | 
|---|
 | 55 |     ~LinkedNodes();
 | 
|---|
 | 56 | 
 | 
|---|
 | 57 |     TesselPoint * getValue (const_iterator &rhs) const;
 | 
|---|
 | 58 |     TesselPoint * getValue (iterator &rhs) const;
 | 
|---|
 | 59 |   };
 | 
|---|
 | 60 |   //typedef list<TesselPoint *> LinkedNodes;
 | 
|---|
| [734816] | 61 | 
 | 
|---|
 | 62 | 
 | 
|---|
| [042f82] | 63 |     Vector max;       // upper boundary
 | 
|---|
 | 64 |     Vector min;       // lower boundary
 | 
|---|
| [357fba] | 65 |     LinkedNodes *LC;  // linked cell list
 | 
|---|
| [042f82] | 66 |     double RADIUS;    // cell edge length
 | 
|---|
 | 67 |     int N[NDIM];      // number of cells per axis
 | 
|---|
| [776b64] | 68 |     mutable int n[NDIM];      // temporary variable for current cell per axis
 | 
|---|
 | 69 |     mutable int index;        // temporary index variable , access by index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
 | 
|---|
| [6ac7ee] | 70 | 
 | 
|---|
| [042f82] | 71 |     LinkedCell();
 | 
|---|
| [af2c424] | 72 |     LinkedCell(const PointCloud &set, const double radius);
 | 
|---|
 | 73 |     template <class T> LinkedCell(const T &set, const double radius) :
 | 
|---|
 | 74 |       LC(NULL),
 | 
|---|
 | 75 |       RADIUS(radius),
 | 
|---|
 | 76 |       index(-1)
 | 
|---|
 | 77 |     {
 | 
|---|
 | 78 |       class TesselPoint *Walker = NULL;
 | 
|---|
 | 79 |       for(int i=0;i<NDIM;i++)
 | 
|---|
 | 80 |         N[i] = 0;
 | 
|---|
 | 81 |       max.Zero();
 | 
|---|
 | 82 |       min.Zero();
 | 
|---|
 | 83 |       DoLog(1) && (Log() << Verbose(1) << "Begin of LinkedCell" << endl);
 | 
|---|
 | 84 |       if (set.begin() == set.end()) {
 | 
|---|
 | 85 |         DoeLog(1) && (eLog()<< Verbose(1) << "set contains no linked cell nodes!" << endl);
 | 
|---|
 | 86 |         return;
 | 
|---|
 | 87 |       }
 | 
|---|
 | 88 |       // 1. find max and min per axis of atoms
 | 
|---|
 | 89 |       typename T::const_iterator Runner = set.begin();
 | 
|---|
 | 90 |       for (int i=0;i<NDIM;i++) {
 | 
|---|
 | 91 |         max[i] = set.getValue(Runner)->at(i);
 | 
|---|
 | 92 |         min[i] = set.getValue(Runner)->at(i);
 | 
|---|
 | 93 |       }
 | 
|---|
 | 94 |       for (typename T::const_iterator Runner = set.begin(); Runner != set.end(); Runner++) {
 | 
|---|
 | 95 |         Walker = set.getValue(Runner);
 | 
|---|
 | 96 |         for (int i=0;i<NDIM;i++) {
 | 
|---|
 | 97 |           if (max[i] < Walker->at(i))
 | 
|---|
 | 98 |             max[i] = Walker->at(i);
 | 
|---|
 | 99 |           if (min[i] > Walker->at(i))
 | 
|---|
 | 100 |             min[i] = Walker->at(i);
 | 
|---|
 | 101 |         }
 | 
|---|
 | 102 |       }
 | 
|---|
 | 103 |       DoLog(2) && (Log() << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl);
 | 
|---|
 | 104 | 
 | 
|---|
 | 105 |       // 2. find then number of cells per axis
 | 
|---|
 | 106 |       for (int i=0;i<NDIM;i++) {
 | 
|---|
 | 107 |         N[i] = static_cast<int>(floor((max[i] - min[i])/RADIUS)+1);
 | 
|---|
 | 108 |       }
 | 
|---|
 | 109 |       DoLog(2) && (Log() << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl);
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 |       // 3. allocate the lists
 | 
|---|
 | 112 |       DoLog(2) && (Log() << Verbose(2) << "Allocating cells ... ");
 | 
|---|
 | 113 |       if (LC != NULL) {
 | 
|---|
 | 114 |         DoeLog(1) && (eLog()<< Verbose(1) << "Linked Cell list is already allocated, I do nothing." << endl);
 | 
|---|
 | 115 |         return;
 | 
|---|
 | 116 |       }
 | 
|---|
 | 117 |       ASSERT(N[0]*N[1]*N[2] < MAX_LINKEDCELLNODES, "Number linked of linked cell nodes exceded hard-coded limit, use greater edge length!");
 | 
|---|
 | 118 |       LC = new LinkedNodes[N[0]*N[1]*N[2]];
 | 
|---|
 | 119 |       for (index=0;index<N[0]*N[1]*N[2];index++) {
 | 
|---|
 | 120 |         LC [index].clear();
 | 
|---|
 | 121 |       }
 | 
|---|
 | 122 |       DoLog(0) && (Log() << Verbose(0) << "done."  << endl);
 | 
|---|
 | 123 | 
 | 
|---|
 | 124 |       // 4. put each atom into its respective cell
 | 
|---|
 | 125 |       DoLog(2) && (Log() << Verbose(2) << "Filling cells ... ");
 | 
|---|
 | 126 |       for (typename T::const_iterator Runner = set.begin(); Runner != set.end(); Runner++) {
 | 
|---|
 | 127 |         Walker = set.getValue(Runner);
 | 
|---|
 | 128 |         for (int i=0;i<NDIM;i++) {
 | 
|---|
 | 129 |           n[i] = static_cast<int>(floor((Walker->at(i) - min[i])/RADIUS));
 | 
|---|
 | 130 |         }
 | 
|---|
 | 131 |         index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
 | 
|---|
 | 132 |         LC[index].push_back(Walker);
 | 
|---|
 | 133 |         //Log() << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
 | 
|---|
 | 134 |       }
 | 
|---|
 | 135 |       DoLog(0) && (Log() << Verbose(0) << "done."  << endl);
 | 
|---|
 | 136 |       DoLog(1) && (Log() << Verbose(1) << "End of LinkedCell" << endl);
 | 
|---|
 | 137 |     };
 | 
|---|
 | 138 | 
 | 
|---|
 | 139 | 
 | 
|---|
| [042f82] | 140 |     ~LinkedCell();
 | 
|---|
| [734816] | 141 |     const LinkedCell::LinkedNodes* GetCurrentCell()const ;
 | 
|---|
 | 142 |     const LinkedCell::LinkedNodes* GetRelativeToCurrentCell(const int relative[NDIM])const ;
 | 
|---|
| [776b64] | 143 |     bool SetIndexToNode(const TesselPoint * const Walker)const ;
 | 
|---|
| [d74077] | 144 |     bool SetIndexToVector(const Vector &x)const ;
 | 
|---|
| [ffe885] | 145 |     double SetClosestIndexToOutsideVector(const Vector * const x) const;
 | 
|---|
| [776b64] | 146 |     bool CheckBounds()const ;
 | 
|---|
 | 147 |     bool CheckBounds(const int relative[NDIM])const ;
 | 
|---|
| [893bea] | 148 |     void GetNeighbourBounds(int lower[NDIM], int upper[NDIM], int step = 1)const ;
 | 
|---|
| [6ac7ee] | 149 | 
 | 
|---|
| [893bea] | 150 |     LinkedCell::LinkedNodes* GetallNeighbours(const double distance = 0) const;
 | 
|---|
| [734816] | 151 |     LinkedCell::LinkedNodes* GetPointsInsideSphere(const double radius, const Vector * const center) const;
 | 
|---|
| [042f82] | 152 |     // not implemented yet
 | 
|---|
| [357fba] | 153 |     bool AddNode(Vector *Walker);
 | 
|---|
 | 154 |     bool DeleteNode(Vector *Walker);
 | 
|---|
 | 155 |     bool MoveNode(Vector *Walker);
 | 
|---|
| [e1bc68] | 156 | };
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 | #endif /*LINKEDCELL_HPP_*/
 | 
|---|