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