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