| [edb93c] | 1 | /** \file linkedcell.cpp | 
|---|
|  | 2 | * | 
|---|
|  | 3 | * Function implementations for the class LinkedCell. | 
|---|
|  | 4 | * | 
|---|
|  | 5 | */ | 
|---|
|  | 6 |  | 
|---|
| [112b09] | 7 | #include "Helpers/MemDebug.hpp" | 
|---|
| [edb93c] | 8 |  | 
|---|
| [f66195] | 9 | #include "atom.hpp" | 
|---|
|  | 10 | #include "helpers.hpp" | 
|---|
| [e1bc68] | 11 | #include "linkedcell.hpp" | 
|---|
| [36166d] | 12 | #include "verbose.hpp" | 
|---|
| [e138de] | 13 | #include "log.hpp" | 
|---|
| [cee0b57] | 14 | #include "molecule.hpp" | 
|---|
| [357fba] | 15 | #include "tesselation.hpp" | 
|---|
| [f66195] | 16 | #include "vector.hpp" | 
|---|
| [357fba] | 17 |  | 
|---|
|  | 18 | // ========================================================= class LinkedCell =========================================== | 
|---|
|  | 19 |  | 
|---|
| [e1bc68] | 20 |  | 
|---|
|  | 21 | /** Constructor for class LinkedCell. | 
|---|
|  | 22 | */ | 
|---|
|  | 23 | LinkedCell::LinkedCell() | 
|---|
|  | 24 | { | 
|---|
| [042f82] | 25 | LC = NULL; | 
|---|
|  | 26 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 27 | N[i] = 0; | 
|---|
|  | 28 | index = -1; | 
|---|
|  | 29 | RADIUS = 0.; | 
|---|
|  | 30 | max.Zero(); | 
|---|
|  | 31 | min.Zero(); | 
|---|
| [e1bc68] | 32 | }; | 
|---|
|  | 33 |  | 
|---|
|  | 34 | /** Puts all atoms in \a *mol into a linked cell list with cell's lengths of \a RADIUS | 
|---|
| [357fba] | 35 | * \param *set LCNodeSet class with all LCNode's | 
|---|
| [e1bc68] | 36 | * \param RADIUS edge length of cells | 
|---|
|  | 37 | */ | 
|---|
| [776b64] | 38 | LinkedCell::LinkedCell(const PointCloud * const set, const double radius) | 
|---|
| [e1bc68] | 39 | { | 
|---|
| [357fba] | 40 | TesselPoint *Walker = NULL; | 
|---|
| [e1bc68] | 41 |  | 
|---|
| [042f82] | 42 | RADIUS = radius; | 
|---|
|  | 43 | LC = NULL; | 
|---|
|  | 44 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 45 | N[i] = 0; | 
|---|
|  | 46 | index = -1; | 
|---|
|  | 47 | max.Zero(); | 
|---|
|  | 48 | min.Zero(); | 
|---|
| [a67d19] | 49 | DoLog(1) && (Log() << Verbose(1) << "Begin of LinkedCell" << endl); | 
|---|
| [caf4ba] | 50 | if ((set == NULL) || (set->IsEmpty())) { | 
|---|
| [58ed4a] | 51 | DoeLog(1) && (eLog()<< Verbose(1) << "set is NULL or contains no linked cell nodes!" << endl); | 
|---|
| [042f82] | 52 | return; | 
|---|
|  | 53 | } | 
|---|
|  | 54 | // 1. find max and min per axis of atoms | 
|---|
| [357fba] | 55 | set->GoToFirst(); | 
|---|
|  | 56 | Walker = set->GetPoint(); | 
|---|
| [042f82] | 57 | for (int i=0;i<NDIM;i++) { | 
|---|
| [0a4f7f] | 58 | max[i] = Walker->node->at(i); | 
|---|
|  | 59 | min[i] = Walker->node->at(i); | 
|---|
| [042f82] | 60 | } | 
|---|
| [357fba] | 61 | set->GoToFirst(); | 
|---|
| [1999d8] | 62 | while (!set->IsEnd()) { | 
|---|
| [357fba] | 63 | Walker = set->GetPoint(); | 
|---|
| [042f82] | 64 | for (int i=0;i<NDIM;i++) { | 
|---|
| [0a4f7f] | 65 | if (max[i] < Walker->node->at(i)) | 
|---|
|  | 66 | max[i] = Walker->node->at(i); | 
|---|
|  | 67 | if (min[i] > Walker->node->at(i)) | 
|---|
|  | 68 | min[i] = Walker->node->at(i); | 
|---|
| [042f82] | 69 | } | 
|---|
| [357fba] | 70 | set->GoToNext(); | 
|---|
| [042f82] | 71 | } | 
|---|
| [a67d19] | 72 | DoLog(2) && (Log() << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl); | 
|---|
| [6ac7ee] | 73 |  | 
|---|
| [357fba] | 74 | // 2. find then number of cells per axis | 
|---|
| [042f82] | 75 | for (int i=0;i<NDIM;i++) { | 
|---|
| [0a4f7f] | 76 | N[i] = static_cast<int>(floor((max[i] - min[i])/RADIUS)+1); | 
|---|
| [042f82] | 77 | } | 
|---|
| [a67d19] | 78 | DoLog(2) && (Log() << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl); | 
|---|
| [6ac7ee] | 79 |  | 
|---|
| [042f82] | 80 | // 3. allocate the lists | 
|---|
| [a67d19] | 81 | DoLog(2) && (Log() << Verbose(2) << "Allocating cells ... "); | 
|---|
| [042f82] | 82 | if (LC != NULL) { | 
|---|
| [58ed4a] | 83 | DoeLog(1) && (eLog()<< Verbose(1) << "Linked Cell list is already allocated, I do nothing." << endl); | 
|---|
| [042f82] | 84 | return; | 
|---|
|  | 85 | } | 
|---|
| [357fba] | 86 | LC = new LinkedNodes[N[0]*N[1]*N[2]]; | 
|---|
| [042f82] | 87 | for (index=0;index<N[0]*N[1]*N[2];index++) { | 
|---|
|  | 88 | LC [index].clear(); | 
|---|
|  | 89 | } | 
|---|
| [a67d19] | 90 | DoLog(0) && (Log() << Verbose(0) << "done."  << endl); | 
|---|
| [6ac7ee] | 91 |  | 
|---|
| [042f82] | 92 | // 4. put each atom into its respective cell | 
|---|
| [a67d19] | 93 | DoLog(2) && (Log() << Verbose(2) << "Filling cells ... "); | 
|---|
| [357fba] | 94 | set->GoToFirst(); | 
|---|
| [1999d8] | 95 | while (!set->IsEnd()) { | 
|---|
| [357fba] | 96 | Walker = set->GetPoint(); | 
|---|
| [042f82] | 97 | for (int i=0;i<NDIM;i++) { | 
|---|
| [0a4f7f] | 98 | n[i] = static_cast<int>(floor((Walker->node->at(i) - min[i])/RADIUS)); | 
|---|
| [042f82] | 99 | } | 
|---|
|  | 100 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2]; | 
|---|
|  | 101 | LC[index].push_back(Walker); | 
|---|
| [e138de] | 102 | //Log() << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl; | 
|---|
| [357fba] | 103 | set->GoToNext(); | 
|---|
| [042f82] | 104 | } | 
|---|
| [a67d19] | 105 | DoLog(0) && (Log() << Verbose(0) << "done."  << endl); | 
|---|
|  | 106 | DoLog(1) && (Log() << Verbose(1) << "End of LinkedCell" << endl); | 
|---|
| [e1bc68] | 107 | }; | 
|---|
|  | 108 |  | 
|---|
| [8cd903] | 109 |  | 
|---|
|  | 110 | /** Puts all atoms in \a *mol into a linked cell list with cell's lengths of \a RADIUS | 
|---|
|  | 111 | * \param *set LCNodeSet class with all LCNode's | 
|---|
|  | 112 | * \param RADIUS edge length of cells | 
|---|
|  | 113 | */ | 
|---|
| [776b64] | 114 | LinkedCell::LinkedCell(LinkedNodes *set, const double radius) | 
|---|
| [8cd903] | 115 | { | 
|---|
|  | 116 | class TesselPoint *Walker = NULL; | 
|---|
|  | 117 | RADIUS = radius; | 
|---|
|  | 118 | LC = NULL; | 
|---|
|  | 119 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 120 | N[i] = 0; | 
|---|
|  | 121 | index = -1; | 
|---|
|  | 122 | max.Zero(); | 
|---|
|  | 123 | min.Zero(); | 
|---|
| [a67d19] | 124 | DoLog(1) && (Log() << Verbose(1) << "Begin of LinkedCell" << endl); | 
|---|
| [8cd903] | 125 | if (set->empty()) { | 
|---|
| [58ed4a] | 126 | DoeLog(1) && (eLog()<< Verbose(1) << "set contains no linked cell nodes!" << endl); | 
|---|
| [8cd903] | 127 | return; | 
|---|
|  | 128 | } | 
|---|
|  | 129 | // 1. find max and min per axis of atoms | 
|---|
|  | 130 | LinkedNodes::iterator Runner = set->begin(); | 
|---|
|  | 131 | for (int i=0;i<NDIM;i++) { | 
|---|
| [0a4f7f] | 132 | max[i] = (*Runner)->node->at(i); | 
|---|
|  | 133 | min[i] = (*Runner)->node->at(i); | 
|---|
| [8cd903] | 134 | } | 
|---|
|  | 135 | for (LinkedNodes::iterator Runner = set->begin(); Runner != set->end(); Runner++) { | 
|---|
|  | 136 | Walker = *Runner; | 
|---|
|  | 137 | for (int i=0;i<NDIM;i++) { | 
|---|
| [0a4f7f] | 138 | if (max[i] < Walker->node->at(i)) | 
|---|
|  | 139 | max[i] = Walker->node->at(i); | 
|---|
|  | 140 | if (min[i] > Walker->node->at(i)) | 
|---|
|  | 141 | min[i] = Walker->node->at(i); | 
|---|
| [8cd903] | 142 | } | 
|---|
|  | 143 | } | 
|---|
| [a67d19] | 144 | DoLog(2) && (Log() << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl); | 
|---|
| [8cd903] | 145 |  | 
|---|
|  | 146 | // 2. find then number of cells per axis | 
|---|
|  | 147 | for (int i=0;i<NDIM;i++) { | 
|---|
| [0a4f7f] | 148 | N[i] = static_cast<int>(floor((max[i] - min[i])/RADIUS)+1); | 
|---|
| [8cd903] | 149 | } | 
|---|
| [a67d19] | 150 | DoLog(2) && (Log() << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl); | 
|---|
| [8cd903] | 151 |  | 
|---|
|  | 152 | // 3. allocate the lists | 
|---|
| [a67d19] | 153 | DoLog(2) && (Log() << Verbose(2) << "Allocating cells ... "); | 
|---|
| [8cd903] | 154 | if (LC != NULL) { | 
|---|
| [58ed4a] | 155 | DoeLog(1) && (eLog()<< Verbose(1) << "Linked Cell list is already allocated, I do nothing." << endl); | 
|---|
| [8cd903] | 156 | return; | 
|---|
|  | 157 | } | 
|---|
|  | 158 | LC = new LinkedNodes[N[0]*N[1]*N[2]]; | 
|---|
|  | 159 | for (index=0;index<N[0]*N[1]*N[2];index++) { | 
|---|
|  | 160 | LC [index].clear(); | 
|---|
|  | 161 | } | 
|---|
| [a67d19] | 162 | DoLog(0) && (Log() << Verbose(0) << "done."  << endl); | 
|---|
| [8cd903] | 163 |  | 
|---|
|  | 164 | // 4. put each atom into its respective cell | 
|---|
| [a67d19] | 165 | DoLog(2) && (Log() << Verbose(2) << "Filling cells ... "); | 
|---|
| [8cd903] | 166 | for (LinkedNodes::iterator Runner = set->begin(); Runner != set->end(); Runner++) { | 
|---|
|  | 167 | Walker = *Runner; | 
|---|
|  | 168 | for (int i=0;i<NDIM;i++) { | 
|---|
| [0a4f7f] | 169 | n[i] = static_cast<int>(floor((Walker->node->at(i) - min[i])/RADIUS)); | 
|---|
| [8cd903] | 170 | } | 
|---|
|  | 171 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2]; | 
|---|
|  | 172 | LC[index].push_back(Walker); | 
|---|
| [e138de] | 173 | //Log() << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl; | 
|---|
| [8cd903] | 174 | } | 
|---|
| [a67d19] | 175 | DoLog(0) && (Log() << Verbose(0) << "done."  << endl); | 
|---|
|  | 176 | DoLog(1) && (Log() << Verbose(1) << "End of LinkedCell" << endl); | 
|---|
| [8cd903] | 177 | }; | 
|---|
|  | 178 |  | 
|---|
| [e1bc68] | 179 | /** Destructor for class LinkedCell. | 
|---|
|  | 180 | */ | 
|---|
|  | 181 | LinkedCell::~LinkedCell() | 
|---|
|  | 182 | { | 
|---|
| [042f82] | 183 | if (LC != NULL) | 
|---|
|  | 184 | for (index=0;index<N[0]*N[1]*N[2];index++) | 
|---|
|  | 185 | LC[index].clear(); | 
|---|
|  | 186 | delete[](LC); | 
|---|
|  | 187 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 188 | N[i] = 0; | 
|---|
|  | 189 | index = -1; | 
|---|
| [e1bc68] | 190 | }; | 
|---|
|  | 191 |  | 
|---|
|  | 192 | /** Checks whether LinkedCell::n[] is each within [0,N[]]. | 
|---|
|  | 193 | * \return if all in intervals - true, else -false | 
|---|
|  | 194 | */ | 
|---|
| [776b64] | 195 | bool LinkedCell::CheckBounds() const | 
|---|
| [e1bc68] | 196 | { | 
|---|
| [042f82] | 197 | bool status = true; | 
|---|
|  | 198 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 199 | status = status && ((n[i] >=0) && (n[i] < N[i])); | 
|---|
| [bdc91e] | 200 | //  if (!status) | 
|---|
|  | 201 | //    DoeLog(1) && (eLog()<< Verbose(1) << "indices are out of bounds!" << endl); | 
|---|
| [042f82] | 202 | return status; | 
|---|
| [e1bc68] | 203 | }; | 
|---|
|  | 204 |  | 
|---|
| [07051c] | 205 | /** Checks whether LinkedCell::n[] plus relative offset is each within [0,N[]]. | 
|---|
| [266237] | 206 | * Note that for this check we don't admonish if out of bounds. | 
|---|
| [07051c] | 207 | * \param relative[NDIM] relative offset to current cell | 
|---|
|  | 208 | * \return if all in intervals - true, else -false | 
|---|
|  | 209 | */ | 
|---|
| [776b64] | 210 | bool LinkedCell::CheckBounds(const int relative[NDIM]) const | 
|---|
| [07051c] | 211 | { | 
|---|
|  | 212 | bool status = true; | 
|---|
|  | 213 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 214 | status = status && ((n[i]+relative[i] >=0) && (n[i]+relative[i] < N[i])); | 
|---|
|  | 215 | return status; | 
|---|
|  | 216 | }; | 
|---|
|  | 217 |  | 
|---|
| [e1bc68] | 218 |  | 
|---|
|  | 219 | /** Returns a pointer to the current cell. | 
|---|
|  | 220 | * \return LinkedAtoms pointer to current cell, NULL if LinkedCell::n[] are out of bounds. | 
|---|
|  | 221 | */ | 
|---|
| [734816] | 222 | const LinkedCell::LinkedNodes* LinkedCell::GetCurrentCell() const | 
|---|
| [e1bc68] | 223 | { | 
|---|
| [042f82] | 224 | if (CheckBounds()) { | 
|---|
|  | 225 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2]; | 
|---|
|  | 226 | return (&(LC[index])); | 
|---|
|  | 227 | } else { | 
|---|
|  | 228 | return NULL; | 
|---|
|  | 229 | } | 
|---|
| [e1bc68] | 230 | }; | 
|---|
|  | 231 |  | 
|---|
| [07051c] | 232 | /** Returns a pointer to the current cell. | 
|---|
|  | 233 | * \param relative[NDIM] offset for each axis with respect to the current cell LinkedCell::n[NDIM] | 
|---|
|  | 234 | * \return LinkedAtoms pointer to current cell, NULL if LinkedCell::n[]+relative[] are out of bounds. | 
|---|
|  | 235 | */ | 
|---|
| [734816] | 236 | const LinkedCell::LinkedNodes* LinkedCell::GetRelativeToCurrentCell(const int relative[NDIM]) const | 
|---|
| [07051c] | 237 | { | 
|---|
|  | 238 | if (CheckBounds(relative)) { | 
|---|
|  | 239 | index = (n[0]+relative[0]) * N[1] * N[2] + (n[1]+relative[1]) * N[2] + (n[2]+relative[2]); | 
|---|
|  | 240 | return (&(LC[index])); | 
|---|
|  | 241 | } else { | 
|---|
|  | 242 | return NULL; | 
|---|
|  | 243 | } | 
|---|
|  | 244 | }; | 
|---|
|  | 245 |  | 
|---|
| [893bea] | 246 | /** Set the index to the cell containing a given Vector *x. | 
|---|
|  | 247 | * \param *x Vector with coordinates | 
|---|
|  | 248 | * \return Vector is inside bounding box - true, else - false | 
|---|
|  | 249 | */ | 
|---|
|  | 250 | bool LinkedCell::SetIndexToVector(const Vector * const x) const | 
|---|
|  | 251 | { | 
|---|
|  | 252 | for (int i=0;i<NDIM;i++) | 
|---|
| [8cbb97] | 253 | n[i] = (int)floor((x->at(i) - min[i])/RADIUS); | 
|---|
| [893bea] | 254 |  | 
|---|
|  | 255 | return CheckBounds(); | 
|---|
|  | 256 | }; | 
|---|
|  | 257 |  | 
|---|
| [357fba] | 258 | /** Calculates the index for a given LCNode *Walker. | 
|---|
|  | 259 | * \param *Walker LCNode to set index tos | 
|---|
| [e1bc68] | 260 | * \return if the atom is also found in this cell - true, else - false | 
|---|
|  | 261 | */ | 
|---|
| [776b64] | 262 | bool LinkedCell::SetIndexToNode(const TesselPoint * const Walker) const | 
|---|
| [e1bc68] | 263 | { | 
|---|
| [042f82] | 264 | bool status = false; | 
|---|
|  | 265 | for (int i=0;i<NDIM;i++) { | 
|---|
| [0a4f7f] | 266 | n[i] = static_cast<int>(floor((Walker->node->at(i) - min[i])/RADIUS)); | 
|---|
| [042f82] | 267 | } | 
|---|
|  | 268 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2]; | 
|---|
|  | 269 | if (CheckBounds()) { | 
|---|
| [357fba] | 270 | for (LinkedNodes::iterator Runner = LC[index].begin(); Runner != LC[index].end(); Runner++) | 
|---|
| [042f82] | 271 | status = status || ((*Runner) == Walker); | 
|---|
|  | 272 | return status; | 
|---|
|  | 273 | } else { | 
|---|
| [58ed4a] | 274 | DoeLog(1) && (eLog()<< Verbose(1) << "Node at " << *Walker << " is out of bounds." << endl); | 
|---|
| [042f82] | 275 | return false; | 
|---|
|  | 276 | } | 
|---|
| [e1bc68] | 277 | }; | 
|---|
|  | 278 |  | 
|---|
| [0f4538] | 279 | /** Calculates the interval bounds of the linked cell grid. | 
|---|
| [bdc91e] | 280 | * \param lower lower bounds | 
|---|
|  | 281 | * \param upper upper bounds | 
|---|
| [061b06] | 282 | * \param step how deep to check the neighbouring cells (i.e. number of layers to check) | 
|---|
| [0f4538] | 283 | */ | 
|---|
| [893bea] | 284 | void LinkedCell::GetNeighbourBounds(int lower[NDIM], int upper[NDIM], int step) const | 
|---|
| [0f4538] | 285 | { | 
|---|
|  | 286 | for (int i=0;i<NDIM;i++) { | 
|---|
| [bdc91e] | 287 | lower[i] = n[i]-step; | 
|---|
|  | 288 | if (lower[i] < 0) | 
|---|
|  | 289 | lower[i] = 0; | 
|---|
|  | 290 | if (lower[i] >= N[i]) | 
|---|
|  | 291 | lower[i] = N[i]-1; | 
|---|
|  | 292 | upper[i] = n[i]+step; | 
|---|
|  | 293 | if (upper[i] >= N[i]) | 
|---|
|  | 294 | upper[i] = N[i]-1; | 
|---|
|  | 295 | if (upper[i] < 0) | 
|---|
|  | 296 | upper[i] = 0; | 
|---|
| [e138de] | 297 | //Log() << Verbose(0) << "axis " << i << " has bounds [" << lower[i] << "," << upper[i] << "]" << endl; | 
|---|
| [0f4538] | 298 | } | 
|---|
|  | 299 | }; | 
|---|
|  | 300 |  | 
|---|
| [734816] | 301 | /** Returns a list with all neighbours from the current LinkedCell::index. | 
|---|
|  | 302 | * \param distance (if no distance, then adjacent cells are taken) | 
|---|
|  | 303 | * \return list of tesselpoints | 
|---|
|  | 304 | */ | 
|---|
| [893bea] | 305 | LinkedCell::LinkedNodes* LinkedCell::GetallNeighbours(const double distance) const | 
|---|
| [734816] | 306 | { | 
|---|
| [893bea] | 307 | int Nlower[NDIM], Nupper[NDIM]; | 
|---|
| [734816] | 308 | TesselPoint *Walker = NULL; | 
|---|
|  | 309 | LinkedNodes *TesselList = new LinkedNodes; | 
|---|
|  | 310 |  | 
|---|
|  | 311 | // then go through the current and all neighbouring cells and check the contained points for possible candidates | 
|---|
| [893bea] | 312 | const int step = (distance == 0) ? 1 : (int)floor(distance/RADIUS + 1.); | 
|---|
|  | 313 | GetNeighbourBounds(Nlower, Nupper, step); | 
|---|
|  | 314 |  | 
|---|
| [734816] | 315 | //Log() << Verbose(0) << endl; | 
|---|
|  | 316 | for (n[0] = Nlower[0]; n[0] <= Nupper[0]; n[0]++) | 
|---|
|  | 317 | for (n[1] = Nlower[1]; n[1] <= Nupper[1]; n[1]++) | 
|---|
|  | 318 | for (n[2] = Nlower[2]; n[2] <= Nupper[2]; n[2]++) { | 
|---|
|  | 319 | const LinkedNodes *List = GetCurrentCell(); | 
|---|
|  | 320 | //Log() << Verbose(1) << "Current cell is " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl; | 
|---|
|  | 321 | if (List != NULL) { | 
|---|
|  | 322 | for (LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) { | 
|---|
|  | 323 | Walker = *Runner; | 
|---|
|  | 324 | TesselList->push_back(Walker); | 
|---|
|  | 325 | } | 
|---|
|  | 326 | } | 
|---|
|  | 327 | } | 
|---|
|  | 328 | return TesselList; | 
|---|
|  | 329 | }; | 
|---|
|  | 330 |  | 
|---|
| [ffe885] | 331 | /** Set the index to the cell containing a given Vector *x, which is not inside the LinkedCell's domain | 
|---|
|  | 332 | * Note that as we have to check distance from every corner of the closest cell, this function is faw more | 
|---|
|  | 333 | * expensive and if Vector is known to be inside LinkedCell's domain, then SetIndexToVector() should be used. | 
|---|
|  | 334 | * \param *x Vector with coordinates | 
|---|
|  | 335 | * \return minimum squared distance of cell to given vector (if inside of domain, distance is 0) | 
|---|
|  | 336 | */ | 
|---|
|  | 337 | double LinkedCell::SetClosestIndexToOutsideVector(const Vector * const x) const | 
|---|
|  | 338 | { | 
|---|
|  | 339 | for (int i=0;i<NDIM;i++) { | 
|---|
| [8cbb97] | 340 | n[i] = (int)floor((x->at(i) - min[i])/RADIUS); | 
|---|
| [ffe885] | 341 | if (n[i] < 0) | 
|---|
|  | 342 | n[i] = 0; | 
|---|
|  | 343 | if (n[i] >= N[i]) | 
|---|
|  | 344 | n[i] = N[i]-1; | 
|---|
|  | 345 | } | 
|---|
|  | 346 |  | 
|---|
|  | 347 | // calculate distance of cell to vector | 
|---|
|  | 348 | double distanceSquared = 0.; | 
|---|
|  | 349 | bool outside = true;  // flag whether x is found in- or outside of LinkedCell's domain/closest cell | 
|---|
|  | 350 | Vector corner; // current corner of closest cell | 
|---|
|  | 351 | Vector tester; // Vector pointing from corner to center of closest cell | 
|---|
|  | 352 | Vector Distance;  // Vector from corner of closest cell to x | 
|---|
|  | 353 |  | 
|---|
|  | 354 | Vector center;  // center of the closest cell | 
|---|
|  | 355 | for (int i=0;i<NDIM;i++) | 
|---|
| [8cbb97] | 356 | center[i] = min[i]+((double)n[i]+.5)*RADIUS; | 
|---|
| [ffe885] | 357 |  | 
|---|
|  | 358 | int c[NDIM]; | 
|---|
|  | 359 | for (c[0]=0;c[0]<=1;c[0]++) | 
|---|
|  | 360 | for (c[1]=0; c[1]<=1;c[1]++) | 
|---|
|  | 361 | for (c[2]=0; c[2]<=1;c[2]++) { | 
|---|
|  | 362 | // set up corner | 
|---|
|  | 363 | for (int i=0;i<NDIM;i++) | 
|---|
| [8cbb97] | 364 | corner[i] = min[i]+RADIUS*((double)n[i]+c[i]); | 
|---|
| [ffe885] | 365 | // set up distance vector | 
|---|
| [8cbb97] | 366 | Distance = (*x) - corner; | 
|---|
| [ffe885] | 367 | const double dist = Distance.NormSquared(); | 
|---|
|  | 368 | // check whether distance is smaller | 
|---|
|  | 369 | if (dist< distanceSquared) | 
|---|
|  | 370 | distanceSquared = dist; | 
|---|
|  | 371 | // check whether distance vector goes inside or outside | 
|---|
| [8cbb97] | 372 | tester = center -corner; | 
|---|
|  | 373 | if (tester.ScalarProduct(Distance) < 0) | 
|---|
| [ffe885] | 374 | outside = false; | 
|---|
|  | 375 | } | 
|---|
|  | 376 | return (outside ? distanceSquared : 0.); | 
|---|
|  | 377 | }; | 
|---|
| [734816] | 378 |  | 
|---|
|  | 379 | /** Returns a list of all TesselPoint with distance less than \a radius to \a *Center. | 
|---|
|  | 380 | * \param radius radius of sphere | 
|---|
|  | 381 | * \param *center center of sphere | 
|---|
|  | 382 | * \return list of all points inside sphere | 
|---|
|  | 383 | */ | 
|---|
|  | 384 | LinkedCell::LinkedNodes* LinkedCell::GetPointsInsideSphere(const double radius, const Vector * const center) const | 
|---|
|  | 385 | { | 
|---|
|  | 386 | const double radiusSquared = radius*radius; | 
|---|
|  | 387 | TesselPoint *Walker = NULL; | 
|---|
|  | 388 | LinkedNodes *TesselList = new LinkedNodes; | 
|---|
| [893bea] | 389 | LinkedNodes *NeighbourList = NULL; | 
|---|
| [734816] | 390 |  | 
|---|
| [893bea] | 391 | // set index of LC to center of sphere | 
|---|
| [ffe885] | 392 | const double dist = SetClosestIndexToOutsideVector(center); | 
|---|
| [061b06] | 393 | if (dist > 2.*radius) { | 
|---|
| [ffe885] | 394 | DoeLog(1) && (eLog()<< Verbose(1) << "Vector " << *center << " is too far away from any atom in LinkedCell's bounding box." << endl); | 
|---|
| [734816] | 395 | return TesselList; | 
|---|
| [061b06] | 396 | } else | 
|---|
| [a67d19] | 397 | DoLog(1) && (Log() << Verbose(1) << "Distance of closest cell to center of sphere with radius " << radius << " is " << dist << "." << endl); | 
|---|
| [893bea] | 398 |  | 
|---|
|  | 399 | // gather all neighbours first, then look who fulfills distance criteria | 
|---|
| [061b06] | 400 | NeighbourList = GetallNeighbours(2.*radius-dist); | 
|---|
|  | 401 | //Log() << Verbose(1) << "I found " << NeighbourList->size() << " neighbours to check." << endl; | 
|---|
| [893bea] | 402 | if (NeighbourList != NULL) { | 
|---|
|  | 403 | for (LinkedNodes::const_iterator Runner = NeighbourList->begin(); Runner != NeighbourList->end(); Runner++) { | 
|---|
|  | 404 | Walker = *Runner; | 
|---|
| [061b06] | 405 | //Log() << Verbose(1) << "Current neighbour is at " << *Walker->node << "." << endl; | 
|---|
| [8cbb97] | 406 | if ((center->DistanceSquared(*Walker->node) - radiusSquared) < MYEPSILON) { | 
|---|
| [893bea] | 407 | TesselList->push_back(Walker); | 
|---|
| [734816] | 408 | } | 
|---|
| [893bea] | 409 | } | 
|---|
|  | 410 | delete(NeighbourList); | 
|---|
|  | 411 | } else | 
|---|
|  | 412 | DoeLog(2) && (eLog()<< Verbose(2) << "Around vector " << *center << " there are no atoms." << endl); | 
|---|
| [734816] | 413 | return TesselList; | 
|---|
|  | 414 | }; | 
|---|