| [edb93c] | 1 | /** \file linkedcell.cpp | 
|---|
|  | 2 | * | 
|---|
|  | 3 | * Function implementations for the class LinkedCell. | 
|---|
|  | 4 | * | 
|---|
|  | 5 | */ | 
|---|
|  | 6 |  | 
|---|
|  | 7 |  | 
|---|
| [f66195] | 8 | #include "atom.hpp" | 
|---|
|  | 9 | #include "helpers.hpp" | 
|---|
| [e1bc68] | 10 | #include "linkedcell.hpp" | 
|---|
| [cee0b57] | 11 | #include "molecule.hpp" | 
|---|
| [357fba] | 12 | #include "tesselation.hpp" | 
|---|
| [f66195] | 13 | #include "vector.hpp" | 
|---|
| [357fba] | 14 |  | 
|---|
|  | 15 | // ========================================================= class LinkedCell =========================================== | 
|---|
|  | 16 |  | 
|---|
| [e1bc68] | 17 |  | 
|---|
|  | 18 | /** Constructor for class LinkedCell. | 
|---|
|  | 19 | */ | 
|---|
|  | 20 | LinkedCell::LinkedCell() | 
|---|
|  | 21 | { | 
|---|
| [042f82] | 22 | LC = NULL; | 
|---|
|  | 23 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 24 | N[i] = 0; | 
|---|
|  | 25 | index = -1; | 
|---|
|  | 26 | RADIUS = 0.; | 
|---|
|  | 27 | max.Zero(); | 
|---|
|  | 28 | min.Zero(); | 
|---|
| [e1bc68] | 29 | }; | 
|---|
|  | 30 |  | 
|---|
|  | 31 | /** Puts all atoms in \a *mol into a linked cell list with cell's lengths of \a RADIUS | 
|---|
| [357fba] | 32 | * \param *set LCNodeSet class with all LCNode's | 
|---|
| [e1bc68] | 33 | * \param RADIUS edge length of cells | 
|---|
|  | 34 | */ | 
|---|
| [357fba] | 35 | LinkedCell::LinkedCell(PointCloud *set, double radius) | 
|---|
| [e1bc68] | 36 | { | 
|---|
| [357fba] | 37 | TesselPoint *Walker = NULL; | 
|---|
| [e1bc68] | 38 |  | 
|---|
| [042f82] | 39 | RADIUS = radius; | 
|---|
|  | 40 | LC = NULL; | 
|---|
|  | 41 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 42 | N[i] = 0; | 
|---|
|  | 43 | index = -1; | 
|---|
|  | 44 | max.Zero(); | 
|---|
|  | 45 | min.Zero(); | 
|---|
|  | 46 | cout << Verbose(1) << "Begin of LinkedCell" << endl; | 
|---|
| [357fba] | 47 | if (set->IsEmpty()) { | 
|---|
|  | 48 | cerr << "ERROR: set contains no linked cell nodes!" << endl; | 
|---|
| [042f82] | 49 | return; | 
|---|
|  | 50 | } | 
|---|
|  | 51 | // 1. find max and min per axis of atoms | 
|---|
| [357fba] | 52 | set->GoToFirst(); | 
|---|
|  | 53 | Walker = set->GetPoint(); | 
|---|
| [042f82] | 54 | for (int i=0;i<NDIM;i++) { | 
|---|
| [357fba] | 55 | max.x[i] = Walker->node->x[i]; | 
|---|
|  | 56 | min.x[i] = Walker->node->x[i]; | 
|---|
| [042f82] | 57 | } | 
|---|
| [357fba] | 58 | set->GoToFirst(); | 
|---|
| [1999d8] | 59 | while (!set->IsEnd()) { | 
|---|
| [357fba] | 60 | Walker = set->GetPoint(); | 
|---|
| [042f82] | 61 | for (int i=0;i<NDIM;i++) { | 
|---|
| [357fba] | 62 | if (max.x[i] < Walker->node->x[i]) | 
|---|
|  | 63 | max.x[i] = Walker->node->x[i]; | 
|---|
|  | 64 | if (min.x[i] > Walker->node->x[i]) | 
|---|
|  | 65 | min.x[i] = Walker->node->x[i]; | 
|---|
| [042f82] | 66 | } | 
|---|
| [357fba] | 67 | set->GoToNext(); | 
|---|
| [042f82] | 68 | } | 
|---|
|  | 69 | cout << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl; | 
|---|
| [6ac7ee] | 70 |  | 
|---|
| [357fba] | 71 | // 2. find then number of cells per axis | 
|---|
| [042f82] | 72 | for (int i=0;i<NDIM;i++) { | 
|---|
|  | 73 | N[i] = (int)floor((max.x[i] - min.x[i])/RADIUS)+1; | 
|---|
|  | 74 | } | 
|---|
|  | 75 | cout << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl; | 
|---|
| [6ac7ee] | 76 |  | 
|---|
| [042f82] | 77 | // 3. allocate the lists | 
|---|
|  | 78 | cout << Verbose(2) << "Allocating cells ... "; | 
|---|
|  | 79 | if (LC != NULL) { | 
|---|
|  | 80 | cout << Verbose(1) << "ERROR: Linked Cell list is already allocated, I do nothing." << endl; | 
|---|
|  | 81 | return; | 
|---|
|  | 82 | } | 
|---|
| [357fba] | 83 | LC = new LinkedNodes[N[0]*N[1]*N[2]]; | 
|---|
| [042f82] | 84 | for (index=0;index<N[0]*N[1]*N[2];index++) { | 
|---|
|  | 85 | LC [index].clear(); | 
|---|
|  | 86 | } | 
|---|
|  | 87 | cout << "done."  << endl; | 
|---|
| [6ac7ee] | 88 |  | 
|---|
| [042f82] | 89 | // 4. put each atom into its respective cell | 
|---|
| [8cede7] | 90 | cout << Verbose(2) << "Filling cells ... "; | 
|---|
| [357fba] | 91 | set->GoToFirst(); | 
|---|
| [1999d8] | 92 | while (!set->IsEnd()) { | 
|---|
| [357fba] | 93 | Walker = set->GetPoint(); | 
|---|
| [042f82] | 94 | for (int i=0;i<NDIM;i++) { | 
|---|
| [357fba] | 95 | n[i] = (int)floor((Walker->node->x[i] - min.x[i])/RADIUS); | 
|---|
| [042f82] | 96 | } | 
|---|
|  | 97 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2]; | 
|---|
|  | 98 | LC[index].push_back(Walker); | 
|---|
|  | 99 | //cout << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl; | 
|---|
| [357fba] | 100 | set->GoToNext(); | 
|---|
| [042f82] | 101 | } | 
|---|
| [8cede7] | 102 | cout << "done."  << endl; | 
|---|
| [042f82] | 103 | cout << Verbose(1) << "End of LinkedCell" << endl; | 
|---|
| [e1bc68] | 104 | }; | 
|---|
|  | 105 |  | 
|---|
| [8cd903] | 106 |  | 
|---|
|  | 107 | /** Puts all atoms in \a *mol into a linked cell list with cell's lengths of \a RADIUS | 
|---|
|  | 108 | * \param *set LCNodeSet class with all LCNode's | 
|---|
|  | 109 | * \param RADIUS edge length of cells | 
|---|
|  | 110 | */ | 
|---|
|  | 111 | LinkedCell::LinkedCell(LinkedNodes *set, double radius) | 
|---|
|  | 112 | { | 
|---|
|  | 113 | class TesselPoint *Walker = NULL; | 
|---|
|  | 114 | RADIUS = radius; | 
|---|
|  | 115 | LC = NULL; | 
|---|
|  | 116 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 117 | N[i] = 0; | 
|---|
|  | 118 | index = -1; | 
|---|
|  | 119 | max.Zero(); | 
|---|
|  | 120 | min.Zero(); | 
|---|
|  | 121 | cout << Verbose(1) << "Begin of LinkedCell" << endl; | 
|---|
|  | 122 | if (set->empty()) { | 
|---|
|  | 123 | cerr << "ERROR: set contains no linked cell nodes!" << endl; | 
|---|
|  | 124 | return; | 
|---|
|  | 125 | } | 
|---|
|  | 126 | // 1. find max and min per axis of atoms | 
|---|
|  | 127 | LinkedNodes::iterator Runner = set->begin(); | 
|---|
|  | 128 | for (int i=0;i<NDIM;i++) { | 
|---|
|  | 129 | max.x[i] = (*Runner)->node->x[i]; | 
|---|
|  | 130 | min.x[i] = (*Runner)->node->x[i]; | 
|---|
|  | 131 | } | 
|---|
|  | 132 | for (LinkedNodes::iterator Runner = set->begin(); Runner != set->end(); Runner++) { | 
|---|
|  | 133 | Walker = *Runner; | 
|---|
|  | 134 | for (int i=0;i<NDIM;i++) { | 
|---|
|  | 135 | if (max.x[i] < Walker->node->x[i]) | 
|---|
|  | 136 | max.x[i] = Walker->node->x[i]; | 
|---|
|  | 137 | if (min.x[i] > Walker->node->x[i]) | 
|---|
|  | 138 | min.x[i] = Walker->node->x[i]; | 
|---|
|  | 139 | } | 
|---|
|  | 140 | } | 
|---|
|  | 141 | cout << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl; | 
|---|
|  | 142 |  | 
|---|
|  | 143 | // 2. find then number of cells per axis | 
|---|
|  | 144 | for (int i=0;i<NDIM;i++) { | 
|---|
|  | 145 | N[i] = (int)floor((max.x[i] - min.x[i])/RADIUS)+1; | 
|---|
|  | 146 | } | 
|---|
|  | 147 | cout << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl; | 
|---|
|  | 148 |  | 
|---|
|  | 149 | // 3. allocate the lists | 
|---|
|  | 150 | cout << Verbose(2) << "Allocating cells ... "; | 
|---|
|  | 151 | if (LC != NULL) { | 
|---|
|  | 152 | cout << Verbose(1) << "ERROR: Linked Cell list is already allocated, I do nothing." << endl; | 
|---|
|  | 153 | return; | 
|---|
|  | 154 | } | 
|---|
|  | 155 | LC = new LinkedNodes[N[0]*N[1]*N[2]]; | 
|---|
|  | 156 | for (index=0;index<N[0]*N[1]*N[2];index++) { | 
|---|
|  | 157 | LC [index].clear(); | 
|---|
|  | 158 | } | 
|---|
|  | 159 | cout << "done."  << endl; | 
|---|
|  | 160 |  | 
|---|
|  | 161 | // 4. put each atom into its respective cell | 
|---|
|  | 162 | cout << Verbose(2) << "Filling cells ... "; | 
|---|
|  | 163 | for (LinkedNodes::iterator Runner = set->begin(); Runner != set->end(); Runner++) { | 
|---|
|  | 164 | Walker = *Runner; | 
|---|
|  | 165 | for (int i=0;i<NDIM;i++) { | 
|---|
|  | 166 | n[i] = (int)floor((Walker->node->x[i] - min.x[i])/RADIUS); | 
|---|
|  | 167 | } | 
|---|
|  | 168 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2]; | 
|---|
|  | 169 | LC[index].push_back(Walker); | 
|---|
|  | 170 | //cout << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl; | 
|---|
|  | 171 | } | 
|---|
|  | 172 | cout << "done."  << endl; | 
|---|
|  | 173 | cout << Verbose(1) << "End of LinkedCell" << endl; | 
|---|
|  | 174 | }; | 
|---|
|  | 175 |  | 
|---|
| [e1bc68] | 176 | /** Destructor for class LinkedCell. | 
|---|
|  | 177 | */ | 
|---|
|  | 178 | LinkedCell::~LinkedCell() | 
|---|
|  | 179 | { | 
|---|
| [042f82] | 180 | if (LC != NULL) | 
|---|
|  | 181 | for (index=0;index<N[0]*N[1]*N[2];index++) | 
|---|
|  | 182 | LC[index].clear(); | 
|---|
|  | 183 | delete[](LC); | 
|---|
|  | 184 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 185 | N[i] = 0; | 
|---|
|  | 186 | index = -1; | 
|---|
|  | 187 | max.Zero(); | 
|---|
|  | 188 | min.Zero(); | 
|---|
| [e1bc68] | 189 | }; | 
|---|
|  | 190 |  | 
|---|
|  | 191 | /** Checks whether LinkedCell::n[] is each within [0,N[]]. | 
|---|
|  | 192 | * \return if all in intervals - true, else -false | 
|---|
|  | 193 | */ | 
|---|
|  | 194 | bool LinkedCell::CheckBounds() | 
|---|
|  | 195 | { | 
|---|
| [042f82] | 196 | bool status = true; | 
|---|
|  | 197 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 198 | status = status && ((n[i] >=0) && (n[i] < N[i])); | 
|---|
|  | 199 | if (!status) | 
|---|
|  | 200 | cerr << "ERROR: indices are out of bounds!" << endl; | 
|---|
|  | 201 | return status; | 
|---|
| [e1bc68] | 202 | }; | 
|---|
|  | 203 |  | 
|---|
| [07051c] | 204 | /** Checks whether LinkedCell::n[] plus relative offset is each within [0,N[]]. | 
|---|
| [266237] | 205 | * Note that for this check we don't admonish if out of bounds. | 
|---|
| [07051c] | 206 | * \param relative[NDIM] relative offset to current cell | 
|---|
|  | 207 | * \return if all in intervals - true, else -false | 
|---|
|  | 208 | */ | 
|---|
|  | 209 | bool LinkedCell::CheckBounds(int relative[NDIM]) | 
|---|
|  | 210 | { | 
|---|
|  | 211 | bool status = true; | 
|---|
|  | 212 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 213 | status = status && ((n[i]+relative[i] >=0) && (n[i]+relative[i] < N[i])); | 
|---|
|  | 214 | return status; | 
|---|
|  | 215 | }; | 
|---|
|  | 216 |  | 
|---|
| [e1bc68] | 217 |  | 
|---|
|  | 218 | /** Returns a pointer to the current cell. | 
|---|
|  | 219 | * \return LinkedAtoms pointer to current cell, NULL if LinkedCell::n[] are out of bounds. | 
|---|
|  | 220 | */ | 
|---|
| [357fba] | 221 | LinkedNodes* LinkedCell::GetCurrentCell() | 
|---|
| [e1bc68] | 222 | { | 
|---|
| [042f82] | 223 | if (CheckBounds()) { | 
|---|
|  | 224 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2]; | 
|---|
|  | 225 | return (&(LC[index])); | 
|---|
|  | 226 | } else { | 
|---|
|  | 227 | return NULL; | 
|---|
|  | 228 | } | 
|---|
| [e1bc68] | 229 | }; | 
|---|
|  | 230 |  | 
|---|
| [07051c] | 231 | /** Returns a pointer to the current cell. | 
|---|
|  | 232 | * \param relative[NDIM] offset for each axis with respect to the current cell LinkedCell::n[NDIM] | 
|---|
|  | 233 | * \return LinkedAtoms pointer to current cell, NULL if LinkedCell::n[]+relative[] are out of bounds. | 
|---|
|  | 234 | */ | 
|---|
|  | 235 | LinkedNodes* LinkedCell::GetRelativeToCurrentCell(int relative[NDIM]) | 
|---|
|  | 236 | { | 
|---|
|  | 237 | if (CheckBounds(relative)) { | 
|---|
|  | 238 | index = (n[0]+relative[0]) * N[1] * N[2] + (n[1]+relative[1]) * N[2] + (n[2]+relative[2]); | 
|---|
|  | 239 | return (&(LC[index])); | 
|---|
|  | 240 | } else { | 
|---|
|  | 241 | return NULL; | 
|---|
|  | 242 | } | 
|---|
|  | 243 | }; | 
|---|
|  | 244 |  | 
|---|
| [357fba] | 245 | /** Calculates the index for a given LCNode *Walker. | 
|---|
|  | 246 | * \param *Walker LCNode to set index tos | 
|---|
| [e1bc68] | 247 | * \return if the atom is also found in this cell - true, else - false | 
|---|
|  | 248 | */ | 
|---|
| [ab1932] | 249 | bool LinkedCell::SetIndexToNode(const TesselPoint *Walker) | 
|---|
| [e1bc68] | 250 | { | 
|---|
| [042f82] | 251 | bool status = false; | 
|---|
|  | 252 | for (int i=0;i<NDIM;i++) { | 
|---|
| [357fba] | 253 | n[i] = (int)floor((Walker->node->x[i] - min.x[i])/RADIUS); | 
|---|
| [042f82] | 254 | } | 
|---|
|  | 255 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2]; | 
|---|
|  | 256 | if (CheckBounds()) { | 
|---|
| [357fba] | 257 | for (LinkedNodes::iterator Runner = LC[index].begin(); Runner != LC[index].end(); Runner++) | 
|---|
| [042f82] | 258 | status = status || ((*Runner) == Walker); | 
|---|
|  | 259 | return status; | 
|---|
|  | 260 | } else { | 
|---|
| [357fba] | 261 | cerr << Verbose(1) << "ERROR: Node at " << *Walker << " is out of bounds." << endl; | 
|---|
| [042f82] | 262 | return false; | 
|---|
|  | 263 | } | 
|---|
| [e1bc68] | 264 | }; | 
|---|
|  | 265 |  | 
|---|
| [0f4538] | 266 | /** Calculates the interval bounds of the linked cell grid. | 
|---|
|  | 267 | * \param *lower lower bounds | 
|---|
|  | 268 | * \param *upper upper bounds | 
|---|
|  | 269 | */ | 
|---|
|  | 270 | void LinkedCell::GetNeighbourBounds(int lower[NDIM], int upper[NDIM]) | 
|---|
|  | 271 | { | 
|---|
|  | 272 | for (int i=0;i<NDIM;i++) { | 
|---|
|  | 273 | lower[i] = ((n[i]-1) >= 0) ? n[i]-1 : 0; | 
|---|
|  | 274 | upper[i] = ((n[i]+1) < N[i]) ? n[i]+1 : N[i]-1; | 
|---|
|  | 275 | //cout << " [" << Nlower[i] << "," << Nupper[i] << "] "; | 
|---|
|  | 276 | // check for this axis whether the point is outside of our grid | 
|---|
|  | 277 | if (n[i] < 0) | 
|---|
|  | 278 | upper[i] = lower[i]; | 
|---|
|  | 279 | if (n[i] > N[i]) | 
|---|
|  | 280 | lower[i] = upper[i]; | 
|---|
|  | 281 |  | 
|---|
|  | 282 | //cout << "axis " << i << " has bounds [" << lower[i] << "," << upper[i] << "]" << endl; | 
|---|
|  | 283 | } | 
|---|
|  | 284 | }; | 
|---|
|  | 285 |  | 
|---|
| [e1bc68] | 286 | /** Calculates the index for a given Vector *x. | 
|---|
|  | 287 | * \param *x Vector with coordinates | 
|---|
|  | 288 | * \return Vector is inside bounding box - true, else - false | 
|---|
|  | 289 | */ | 
|---|
| [0f4538] | 290 | bool LinkedCell::SetIndexToVector(const Vector *x) | 
|---|
| [e1bc68] | 291 | { | 
|---|
| [042f82] | 292 | bool status = true; | 
|---|
|  | 293 | for (int i=0;i<NDIM;i++) { | 
|---|
|  | 294 | n[i] = (int)floor((x->x[i] - min.x[i])/RADIUS); | 
|---|
|  | 295 | if (max.x[i] < x->x[i]) | 
|---|
|  | 296 | status = false; | 
|---|
|  | 297 | if (min.x[i] > x->x[i]) | 
|---|
|  | 298 | status = false; | 
|---|
|  | 299 | } | 
|---|
|  | 300 | return status; | 
|---|
| [e1bc68] | 301 | }; | 
|---|
| [6ac7ee] | 302 |  | 
|---|