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