| [e1bc68] | 1 | #include "linkedcell.hpp" | 
|---|
|  | 2 | #include "molecules.hpp" | 
|---|
| [357fba] | 3 | #include "tesselation.hpp" | 
|---|
|  | 4 |  | 
|---|
|  | 5 | // ========================================================= class LinkedCell =========================================== | 
|---|
|  | 6 |  | 
|---|
| [e1bc68] | 7 |  | 
|---|
|  | 8 | /** Constructor for class LinkedCell. | 
|---|
|  | 9 | */ | 
|---|
|  | 10 | LinkedCell::LinkedCell() | 
|---|
|  | 11 | { | 
|---|
| [042f82] | 12 | LC = NULL; | 
|---|
|  | 13 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 14 | N[i] = 0; | 
|---|
|  | 15 | index = -1; | 
|---|
|  | 16 | RADIUS = 0.; | 
|---|
|  | 17 | max.Zero(); | 
|---|
|  | 18 | min.Zero(); | 
|---|
| [e1bc68] | 19 | }; | 
|---|
|  | 20 |  | 
|---|
|  | 21 | /** Puts all atoms in \a *mol into a linked cell list with cell's lengths of \a RADIUS | 
|---|
| [357fba] | 22 | * \param *set LCNodeSet class with all LCNode's | 
|---|
| [e1bc68] | 23 | * \param RADIUS edge length of cells | 
|---|
|  | 24 | */ | 
|---|
| [357fba] | 25 | LinkedCell::LinkedCell(PointCloud *set, double radius) | 
|---|
| [e1bc68] | 26 | { | 
|---|
| [357fba] | 27 | TesselPoint *Walker = NULL; | 
|---|
| [e1bc68] | 28 |  | 
|---|
| [042f82] | 29 | RADIUS = radius; | 
|---|
|  | 30 | LC = NULL; | 
|---|
|  | 31 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 32 | N[i] = 0; | 
|---|
|  | 33 | index = -1; | 
|---|
|  | 34 | max.Zero(); | 
|---|
|  | 35 | min.Zero(); | 
|---|
|  | 36 | cout << Verbose(1) << "Begin of LinkedCell" << endl; | 
|---|
| [357fba] | 37 | if (set->IsEmpty()) { | 
|---|
|  | 38 | cerr << "ERROR: set contains no linked cell nodes!" << endl; | 
|---|
| [042f82] | 39 | return; | 
|---|
|  | 40 | } | 
|---|
|  | 41 | // 1. find max and min per axis of atoms | 
|---|
| [357fba] | 42 | set->GoToFirst(); | 
|---|
|  | 43 | Walker = set->GetPoint(); | 
|---|
| [042f82] | 44 | for (int i=0;i<NDIM;i++) { | 
|---|
| [357fba] | 45 | max.x[i] = Walker->node->x[i]; | 
|---|
|  | 46 | min.x[i] = Walker->node->x[i]; | 
|---|
| [042f82] | 47 | } | 
|---|
| [357fba] | 48 | set->GoToFirst(); | 
|---|
|  | 49 | while (!set->IsLast()) { | 
|---|
|  | 50 | Walker = set->GetPoint(); | 
|---|
| [042f82] | 51 | for (int i=0;i<NDIM;i++) { | 
|---|
| [357fba] | 52 | if (max.x[i] < Walker->node->x[i]) | 
|---|
|  | 53 | max.x[i] = Walker->node->x[i]; | 
|---|
|  | 54 | if (min.x[i] > Walker->node->x[i]) | 
|---|
|  | 55 | min.x[i] = Walker->node->x[i]; | 
|---|
| [042f82] | 56 | } | 
|---|
| [357fba] | 57 | set->GoToNext(); | 
|---|
| [042f82] | 58 | } | 
|---|
|  | 59 | cout << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl; | 
|---|
| [6ac7ee] | 60 |  | 
|---|
| [357fba] | 61 | // 2. find then number of cells per axis | 
|---|
| [042f82] | 62 | for (int i=0;i<NDIM;i++) { | 
|---|
|  | 63 | N[i] = (int)floor((max.x[i] - min.x[i])/RADIUS)+1; | 
|---|
|  | 64 | } | 
|---|
|  | 65 | cout << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl; | 
|---|
| [6ac7ee] | 66 |  | 
|---|
| [042f82] | 67 | // 3. allocate the lists | 
|---|
|  | 68 | cout << Verbose(2) << "Allocating cells ... "; | 
|---|
|  | 69 | if (LC != NULL) { | 
|---|
|  | 70 | cout << Verbose(1) << "ERROR: Linked Cell list is already allocated, I do nothing." << endl; | 
|---|
|  | 71 | return; | 
|---|
|  | 72 | } | 
|---|
| [357fba] | 73 | LC = new LinkedNodes[N[0]*N[1]*N[2]]; | 
|---|
| [042f82] | 74 | for (index=0;index<N[0]*N[1]*N[2];index++) { | 
|---|
|  | 75 | LC [index].clear(); | 
|---|
|  | 76 | } | 
|---|
|  | 77 | cout << "done."  << endl; | 
|---|
| [6ac7ee] | 78 |  | 
|---|
| [042f82] | 79 | // 4. put each atom into its respective cell | 
|---|
| [8cede7] | 80 | cout << Verbose(2) << "Filling cells ... "; | 
|---|
| [357fba] | 81 | set->GoToFirst(); | 
|---|
|  | 82 | while (!set->IsLast()) { | 
|---|
|  | 83 | Walker = set->GetPoint(); | 
|---|
| [042f82] | 84 | for (int i=0;i<NDIM;i++) { | 
|---|
| [357fba] | 85 | n[i] = (int)floor((Walker->node->x[i] - min.x[i])/RADIUS); | 
|---|
| [042f82] | 86 | } | 
|---|
|  | 87 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2]; | 
|---|
|  | 88 | LC[index].push_back(Walker); | 
|---|
|  | 89 | //cout << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl; | 
|---|
| [357fba] | 90 | set->GoToNext(); | 
|---|
| [042f82] | 91 | } | 
|---|
| [8cede7] | 92 | cout << "done."  << endl; | 
|---|
| [042f82] | 93 | cout << Verbose(1) << "End of LinkedCell" << endl; | 
|---|
| [e1bc68] | 94 | }; | 
|---|
|  | 95 |  | 
|---|
|  | 96 | /** Destructor for class LinkedCell. | 
|---|
|  | 97 | */ | 
|---|
|  | 98 | LinkedCell::~LinkedCell() | 
|---|
|  | 99 | { | 
|---|
| [042f82] | 100 | if (LC != NULL) | 
|---|
|  | 101 | for (index=0;index<N[0]*N[1]*N[2];index++) | 
|---|
|  | 102 | LC[index].clear(); | 
|---|
|  | 103 | delete[](LC); | 
|---|
|  | 104 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 105 | N[i] = 0; | 
|---|
|  | 106 | index = -1; | 
|---|
|  | 107 | max.Zero(); | 
|---|
|  | 108 | min.Zero(); | 
|---|
| [e1bc68] | 109 | }; | 
|---|
|  | 110 |  | 
|---|
|  | 111 | /** Checks whether LinkedCell::n[] is each within [0,N[]]. | 
|---|
|  | 112 | * \return if all in intervals - true, else -false | 
|---|
|  | 113 | */ | 
|---|
|  | 114 | bool LinkedCell::CheckBounds() | 
|---|
|  | 115 | { | 
|---|
| [042f82] | 116 | bool status = true; | 
|---|
|  | 117 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 118 | status = status && ((n[i] >=0) && (n[i] < N[i])); | 
|---|
|  | 119 | if (!status) | 
|---|
|  | 120 | cerr << "ERROR: indices are out of bounds!" << endl; | 
|---|
|  | 121 | return status; | 
|---|
| [e1bc68] | 122 | }; | 
|---|
|  | 123 |  | 
|---|
|  | 124 |  | 
|---|
|  | 125 | /** Returns a pointer to the current cell. | 
|---|
|  | 126 | * \return LinkedAtoms pointer to current cell, NULL if LinkedCell::n[] are out of bounds. | 
|---|
|  | 127 | */ | 
|---|
| [357fba] | 128 | LinkedNodes* LinkedCell::GetCurrentCell() | 
|---|
| [e1bc68] | 129 | { | 
|---|
| [042f82] | 130 | if (CheckBounds()) { | 
|---|
|  | 131 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2]; | 
|---|
|  | 132 | return (&(LC[index])); | 
|---|
|  | 133 | } else { | 
|---|
|  | 134 | return NULL; | 
|---|
|  | 135 | } | 
|---|
| [e1bc68] | 136 | }; | 
|---|
|  | 137 |  | 
|---|
| [357fba] | 138 | /** Calculates the index for a given LCNode *Walker. | 
|---|
|  | 139 | * \param *Walker LCNode to set index tos | 
|---|
| [e1bc68] | 140 | * \return if the atom is also found in this cell - true, else - false | 
|---|
|  | 141 | */ | 
|---|
| [357fba] | 142 | bool LinkedCell::SetIndexToNode(TesselPoint *Walker) | 
|---|
| [e1bc68] | 143 | { | 
|---|
| [042f82] | 144 | bool status = false; | 
|---|
|  | 145 | for (int i=0;i<NDIM;i++) { | 
|---|
| [357fba] | 146 | n[i] = (int)floor((Walker->node->x[i] - min.x[i])/RADIUS); | 
|---|
| [042f82] | 147 | } | 
|---|
|  | 148 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2]; | 
|---|
|  | 149 | if (CheckBounds()) { | 
|---|
| [357fba] | 150 | for (LinkedNodes::iterator Runner = LC[index].begin(); Runner != LC[index].end(); Runner++) | 
|---|
| [042f82] | 151 | status = status || ((*Runner) == Walker); | 
|---|
|  | 152 | return status; | 
|---|
|  | 153 | } else { | 
|---|
| [357fba] | 154 | cerr << Verbose(1) << "ERROR: Node at " << *Walker << " is out of bounds." << endl; | 
|---|
| [042f82] | 155 | return false; | 
|---|
|  | 156 | } | 
|---|
| [e1bc68] | 157 | }; | 
|---|
|  | 158 |  | 
|---|
|  | 159 | /** Calculates the index for a given Vector *x. | 
|---|
|  | 160 | * \param *x Vector with coordinates | 
|---|
|  | 161 | * \return Vector is inside bounding box - true, else - false | 
|---|
|  | 162 | */ | 
|---|
|  | 163 | bool LinkedCell::SetIndexToVector(Vector *x) | 
|---|
|  | 164 | { | 
|---|
| [042f82] | 165 | bool status = true; | 
|---|
|  | 166 | for (int i=0;i<NDIM;i++) { | 
|---|
|  | 167 | n[i] = (int)floor((x->x[i] - min.x[i])/RADIUS); | 
|---|
|  | 168 | if (max.x[i] < x->x[i]) | 
|---|
|  | 169 | status = false; | 
|---|
|  | 170 | if (min.x[i] > x->x[i]) | 
|---|
|  | 171 | status = false; | 
|---|
|  | 172 | } | 
|---|
|  | 173 | return status; | 
|---|
| [e1bc68] | 174 | }; | 
|---|
| [6ac7ee] | 175 |  | 
|---|