| [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"
 | 
|---|
| [e138de] | 11 | #include "log.hpp"
 | 
|---|
| [cee0b57] | 12 | #include "molecule.hpp"
 | 
|---|
| [357fba] | 13 | #include "tesselation.hpp"
 | 
|---|
| [f66195] | 14 | #include "vector.hpp"
 | 
|---|
| [357fba] | 15 | 
 | 
|---|
 | 16 | // ========================================================= class LinkedCell ===========================================
 | 
|---|
 | 17 | 
 | 
|---|
| [e1bc68] | 18 | 
 | 
|---|
 | 19 | /** Constructor for class LinkedCell.
 | 
|---|
 | 20 |  */
 | 
|---|
 | 21 | LinkedCell::LinkedCell()
 | 
|---|
 | 22 | {
 | 
|---|
| [042f82] | 23 |   LC = NULL;
 | 
|---|
 | 24 |   for(int i=0;i<NDIM;i++)
 | 
|---|
 | 25 |     N[i] = 0;
 | 
|---|
 | 26 |   index = -1;
 | 
|---|
 | 27 |   RADIUS = 0.;
 | 
|---|
 | 28 |   max.Zero();
 | 
|---|
 | 29 |   min.Zero();
 | 
|---|
| [e1bc68] | 30 | };
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | /** Puts all atoms in \a *mol into a linked cell list with cell's lengths of \a RADIUS
 | 
|---|
| [357fba] | 33 |  * \param *set LCNodeSet class with all LCNode's
 | 
|---|
| [e1bc68] | 34 |  * \param RADIUS edge length of cells
 | 
|---|
 | 35 |  */
 | 
|---|
| [776b64] | 36 | LinkedCell::LinkedCell(const PointCloud * const set, const double radius)
 | 
|---|
| [e1bc68] | 37 | {
 | 
|---|
| [357fba] | 38 |   TesselPoint *Walker = NULL;
 | 
|---|
| [e1bc68] | 39 | 
 | 
|---|
| [042f82] | 40 |   RADIUS = radius;
 | 
|---|
 | 41 |   LC = NULL;
 | 
|---|
 | 42 |   for(int i=0;i<NDIM;i++)
 | 
|---|
 | 43 |     N[i] = 0;
 | 
|---|
 | 44 |   index = -1;
 | 
|---|
 | 45 |   max.Zero();
 | 
|---|
 | 46 |   min.Zero();
 | 
|---|
| [a67d19] | 47 |   DoLog(1) && (Log() << Verbose(1) << "Begin of LinkedCell" << endl);
 | 
|---|
| [caf4ba] | 48 |   if ((set == NULL) || (set->IsEmpty())) {
 | 
|---|
| [58ed4a] | 49 |     DoeLog(1) && (eLog()<< Verbose(1) << "set is NULL or contains no linked cell nodes!" << endl);
 | 
|---|
| [042f82] | 50 |     return;
 | 
|---|
 | 51 |   }
 | 
|---|
 | 52 |   // 1. find max and min per axis of atoms
 | 
|---|
| [357fba] | 53 |   set->GoToFirst();
 | 
|---|
 | 54 |   Walker = set->GetPoint();
 | 
|---|
| [042f82] | 55 |   for (int i=0;i<NDIM;i++) {
 | 
|---|
| [0a4f7f] | 56 |     max[i] = Walker->node->at(i);
 | 
|---|
 | 57 |     min[i] = Walker->node->at(i);
 | 
|---|
| [042f82] | 58 |   }
 | 
|---|
| [357fba] | 59 |   set->GoToFirst();
 | 
|---|
| [1999d8] | 60 |   while (!set->IsEnd()) {
 | 
|---|
| [357fba] | 61 |     Walker = set->GetPoint();
 | 
|---|
| [042f82] | 62 |     for (int i=0;i<NDIM;i++) {
 | 
|---|
| [0a4f7f] | 63 |       if (max[i] < Walker->node->at(i))
 | 
|---|
 | 64 |         max[i] = Walker->node->at(i);
 | 
|---|
 | 65 |       if (min[i] > Walker->node->at(i))
 | 
|---|
 | 66 |         min[i] = Walker->node->at(i);
 | 
|---|
| [042f82] | 67 |     }
 | 
|---|
| [357fba] | 68 |     set->GoToNext();
 | 
|---|
| [042f82] | 69 |   }
 | 
|---|
| [a67d19] | 70 |   DoLog(2) && (Log() << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl);
 | 
|---|
| [6ac7ee] | 71 | 
 | 
|---|
| [357fba] | 72 |   // 2. find then number of cells per axis
 | 
|---|
| [042f82] | 73 |   for (int i=0;i<NDIM;i++) {
 | 
|---|
| [0a4f7f] | 74 |     N[i] = static_cast<int>(floor((max[i] - min[i])/RADIUS)+1);
 | 
|---|
| [042f82] | 75 |   }
 | 
|---|
| [a67d19] | 76 |   DoLog(2) && (Log() << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl);
 | 
|---|
| [6ac7ee] | 77 | 
 | 
|---|
| [042f82] | 78 |   // 3. allocate the lists
 | 
|---|
| [a67d19] | 79 |   DoLog(2) && (Log() << Verbose(2) << "Allocating cells ... ");
 | 
|---|
| [042f82] | 80 |   if (LC != NULL) {
 | 
|---|
| [58ed4a] | 81 |     DoeLog(1) && (eLog()<< Verbose(1) << "Linked Cell list is already allocated, I do nothing." << endl);
 | 
|---|
| [042f82] | 82 |     return;
 | 
|---|
 | 83 |   }
 | 
|---|
| [357fba] | 84 |   LC = new LinkedNodes[N[0]*N[1]*N[2]];
 | 
|---|
| [042f82] | 85 |   for (index=0;index<N[0]*N[1]*N[2];index++) {
 | 
|---|
 | 86 |     LC [index].clear();
 | 
|---|
 | 87 |   }
 | 
|---|
| [a67d19] | 88 |   DoLog(0) && (Log() << Verbose(0) << "done."  << endl);
 | 
|---|
| [6ac7ee] | 89 | 
 | 
|---|
| [042f82] | 90 |   // 4. put each atom into its respective cell
 | 
|---|
| [a67d19] | 91 |   DoLog(2) && (Log() << Verbose(2) << "Filling cells ... ");
 | 
|---|
| [357fba] | 92 |   set->GoToFirst();
 | 
|---|
| [1999d8] | 93 |   while (!set->IsEnd()) {
 | 
|---|
| [357fba] | 94 |     Walker = set->GetPoint();
 | 
|---|
| [042f82] | 95 |     for (int i=0;i<NDIM;i++) {
 | 
|---|
| [0a4f7f] | 96 |       n[i] = static_cast<int>(floor((Walker->node->at(i) - min[i])/RADIUS));
 | 
|---|
| [042f82] | 97 |     }
 | 
|---|
 | 98 |     index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
 | 
|---|
 | 99 |     LC[index].push_back(Walker);
 | 
|---|
| [e138de] | 100 |     //Log() << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
 | 
|---|
| [357fba] | 101 |     set->GoToNext();
 | 
|---|
| [042f82] | 102 |   }
 | 
|---|
| [a67d19] | 103 |   DoLog(0) && (Log() << Verbose(0) << "done."  << endl);
 | 
|---|
 | 104 |   DoLog(1) && (Log() << Verbose(1) << "End of LinkedCell" << endl);
 | 
|---|
| [e1bc68] | 105 | };
 | 
|---|
 | 106 | 
 | 
|---|
| [8cd903] | 107 | 
 | 
|---|
 | 108 | /** Puts all atoms in \a *mol into a linked cell list with cell's lengths of \a RADIUS
 | 
|---|
 | 109 |  * \param *set LCNodeSet class with all LCNode's
 | 
|---|
 | 110 |  * \param RADIUS edge length of cells
 | 
|---|
 | 111 |  */
 | 
|---|
| [776b64] | 112 | LinkedCell::LinkedCell(LinkedNodes *set, const double radius)
 | 
|---|
| [8cd903] | 113 | {
 | 
|---|
 | 114 |   class TesselPoint *Walker = NULL;
 | 
|---|
 | 115 |   RADIUS = radius;
 | 
|---|
 | 116 |   LC = NULL;
 | 
|---|
 | 117 |   for(int i=0;i<NDIM;i++)
 | 
|---|
 | 118 |     N[i] = 0;
 | 
|---|
 | 119 |   index = -1;
 | 
|---|
 | 120 |   max.Zero();
 | 
|---|
 | 121 |   min.Zero();
 | 
|---|
| [a67d19] | 122 |   DoLog(1) && (Log() << Verbose(1) << "Begin of LinkedCell" << endl);
 | 
|---|
| [8cd903] | 123 |   if (set->empty()) {
 | 
|---|
| [58ed4a] | 124 |     DoeLog(1) && (eLog()<< Verbose(1) << "set contains no linked cell nodes!" << endl);
 | 
|---|
| [8cd903] | 125 |     return;
 | 
|---|
 | 126 |   }
 | 
|---|
 | 127 |   // 1. find max and min per axis of atoms
 | 
|---|
 | 128 |   LinkedNodes::iterator Runner = set->begin();
 | 
|---|
 | 129 |   for (int i=0;i<NDIM;i++) {
 | 
|---|
| [0a4f7f] | 130 |     max[i] = (*Runner)->node->at(i);
 | 
|---|
 | 131 |     min[i] = (*Runner)->node->at(i);
 | 
|---|
| [8cd903] | 132 |   }
 | 
|---|
 | 133 |   for (LinkedNodes::iterator Runner = set->begin(); Runner != set->end(); Runner++) {
 | 
|---|
 | 134 |     Walker = *Runner;
 | 
|---|
 | 135 |     for (int i=0;i<NDIM;i++) {
 | 
|---|
| [0a4f7f] | 136 |       if (max[i] < Walker->node->at(i))
 | 
|---|
 | 137 |         max[i] = Walker->node->at(i);
 | 
|---|
 | 138 |       if (min[i] > Walker->node->at(i))
 | 
|---|
 | 139 |         min[i] = Walker->node->at(i);
 | 
|---|
| [8cd903] | 140 |     }
 | 
|---|
 | 141 |   }
 | 
|---|
| [a67d19] | 142 |   DoLog(2) && (Log() << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl);
 | 
|---|
| [8cd903] | 143 | 
 | 
|---|
 | 144 |   // 2. find then number of cells per axis
 | 
|---|
 | 145 |   for (int i=0;i<NDIM;i++) {
 | 
|---|
| [0a4f7f] | 146 |     N[i] = static_cast<int>(floor((max[i] - min[i])/RADIUS)+1);
 | 
|---|
| [8cd903] | 147 |   }
 | 
|---|
| [a67d19] | 148 |   DoLog(2) && (Log() << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl);
 | 
|---|
| [8cd903] | 149 | 
 | 
|---|
 | 150 |   // 3. allocate the lists
 | 
|---|
| [a67d19] | 151 |   DoLog(2) && (Log() << Verbose(2) << "Allocating cells ... ");
 | 
|---|
| [8cd903] | 152 |   if (LC != NULL) {
 | 
|---|
| [58ed4a] | 153 |     DoeLog(1) && (eLog()<< Verbose(1) << "Linked Cell list is already allocated, I do nothing." << endl);
 | 
|---|
| [8cd903] | 154 |     return;
 | 
|---|
 | 155 |   }
 | 
|---|
 | 156 |   LC = new LinkedNodes[N[0]*N[1]*N[2]];
 | 
|---|
 | 157 |   for (index=0;index<N[0]*N[1]*N[2];index++) {
 | 
|---|
 | 158 |     LC [index].clear();
 | 
|---|
 | 159 |   }
 | 
|---|
| [a67d19] | 160 |   DoLog(0) && (Log() << Verbose(0) << "done."  << endl);
 | 
|---|
| [8cd903] | 161 | 
 | 
|---|
 | 162 |   // 4. put each atom into its respective cell
 | 
|---|
| [a67d19] | 163 |   DoLog(2) && (Log() << Verbose(2) << "Filling cells ... ");
 | 
|---|
| [8cd903] | 164 |   for (LinkedNodes::iterator Runner = set->begin(); Runner != set->end(); Runner++) {
 | 
|---|
 | 165 |     Walker = *Runner;
 | 
|---|
 | 166 |     for (int i=0;i<NDIM;i++) {
 | 
|---|
| [0a4f7f] | 167 |       n[i] = static_cast<int>(floor((Walker->node->at(i) - min[i])/RADIUS));
 | 
|---|
| [8cd903] | 168 |     }
 | 
|---|
 | 169 |     index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
 | 
|---|
 | 170 |     LC[index].push_back(Walker);
 | 
|---|
| [e138de] | 171 |     //Log() << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
 | 
|---|
| [8cd903] | 172 |   }
 | 
|---|
| [a67d19] | 173 |   DoLog(0) && (Log() << Verbose(0) << "done."  << endl);
 | 
|---|
 | 174 |   DoLog(1) && (Log() << Verbose(1) << "End of LinkedCell" << endl);
 | 
|---|
| [8cd903] | 175 | };
 | 
|---|
 | 176 | 
 | 
|---|
| [e1bc68] | 177 | /** Destructor for class LinkedCell.
 | 
|---|
 | 178 |  */
 | 
|---|
 | 179 | LinkedCell::~LinkedCell()
 | 
|---|
 | 180 | {
 | 
|---|
| [042f82] | 181 |   if (LC != NULL)
 | 
|---|
 | 182 |   for (index=0;index<N[0]*N[1]*N[2];index++)
 | 
|---|
 | 183 |     LC[index].clear();
 | 
|---|
 | 184 |   delete[](LC);
 | 
|---|
 | 185 |   for(int i=0;i<NDIM;i++)
 | 
|---|
 | 186 |     N[i] = 0;
 | 
|---|
 | 187 |   index = -1;
 | 
|---|
 | 188 |   max.Zero();
 | 
|---|
 | 189 |   min.Zero();
 | 
|---|
| [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]));
 | 
|---|
 | 200 |   if (!status)
 | 
|---|
| [58ed4a] | 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.
 | 
|---|
 | 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++) {
 | 
|---|
| [061b06] | 287 |     lower[i] = n[i];
 | 
|---|
| [893bea] | 288 |     for (int s=step; s>0;--s)
 | 
|---|
 | 289 |       if ((n[i]-s) >= 0) {
 | 
|---|
 | 290 |         lower[i] = n[i]-s;
 | 
|---|
 | 291 |         break;
 | 
|---|
 | 292 |       }
 | 
|---|
| [061b06] | 293 |     upper[i] = n[i];
 | 
|---|
| [893bea] | 294 |     for (int s=step; s>0;--s)
 | 
|---|
 | 295 |       if ((n[i]+s) < N[i]) {
 | 
|---|
 | 296 |         upper[i] = n[i]+s;
 | 
|---|
 | 297 |         break;
 | 
|---|
 | 298 |       }
 | 
|---|
| [e138de] | 299 |     //Log() << Verbose(0) << "axis " << i << " has bounds [" << lower[i] << "," << upper[i] << "]" << endl;
 | 
|---|
| [0f4538] | 300 |   }
 | 
|---|
 | 301 | };
 | 
|---|
 | 302 | 
 | 
|---|
| [734816] | 303 | /** Returns a list with all neighbours from the current LinkedCell::index.
 | 
|---|
 | 304 |  * \param distance (if no distance, then adjacent cells are taken)
 | 
|---|
 | 305 |  * \return list of tesselpoints
 | 
|---|
 | 306 |  */
 | 
|---|
| [893bea] | 307 | LinkedCell::LinkedNodes* LinkedCell::GetallNeighbours(const double distance) const
 | 
|---|
| [734816] | 308 | {
 | 
|---|
| [893bea] | 309 |   int Nlower[NDIM], Nupper[NDIM];
 | 
|---|
| [734816] | 310 |   TesselPoint *Walker = NULL;
 | 
|---|
 | 311 |   LinkedNodes *TesselList = new LinkedNodes;
 | 
|---|
 | 312 | 
 | 
|---|
 | 313 |   // then go through the current and all neighbouring cells and check the contained points for possible candidates
 | 
|---|
| [893bea] | 314 |   const int step = (distance == 0) ? 1 : (int)floor(distance/RADIUS + 1.);
 | 
|---|
 | 315 |   GetNeighbourBounds(Nlower, Nupper, step);
 | 
|---|
 | 316 | 
 | 
|---|
| [734816] | 317 |   //Log() << Verbose(0) << endl;
 | 
|---|
 | 318 |   for (n[0] = Nlower[0]; n[0] <= Nupper[0]; n[0]++)
 | 
|---|
 | 319 |     for (n[1] = Nlower[1]; n[1] <= Nupper[1]; n[1]++)
 | 
|---|
 | 320 |       for (n[2] = Nlower[2]; n[2] <= Nupper[2]; n[2]++) {
 | 
|---|
 | 321 |         const LinkedNodes *List = GetCurrentCell();
 | 
|---|
 | 322 |         //Log() << Verbose(1) << "Current cell is " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
 | 
|---|
 | 323 |         if (List != NULL) {
 | 
|---|
 | 324 |           for (LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
 | 
|---|
 | 325 |             Walker = *Runner;
 | 
|---|
 | 326 |             TesselList->push_back(Walker);
 | 
|---|
 | 327 |           }
 | 
|---|
 | 328 |         }
 | 
|---|
 | 329 |       }
 | 
|---|
 | 330 |   return TesselList;
 | 
|---|
 | 331 | };
 | 
|---|
 | 332 | 
 | 
|---|
| [ffe885] | 333 | /** Set the index to the cell containing a given Vector *x, which is not inside the LinkedCell's domain
 | 
|---|
 | 334 |  * Note that as we have to check distance from every corner of the closest cell, this function is faw more
 | 
|---|
 | 335 |  * expensive and if Vector is known to be inside LinkedCell's domain, then SetIndexToVector() should be used.
 | 
|---|
 | 336 |  * \param *x Vector with coordinates
 | 
|---|
 | 337 |  * \return minimum squared distance of cell to given vector (if inside of domain, distance is 0)
 | 
|---|
 | 338 |  */
 | 
|---|
 | 339 | double LinkedCell::SetClosestIndexToOutsideVector(const Vector * const x) const
 | 
|---|
 | 340 | {
 | 
|---|
 | 341 |   for (int i=0;i<NDIM;i++) {
 | 
|---|
| [8cbb97] | 342 |     n[i] = (int)floor((x->at(i) - min[i])/RADIUS);
 | 
|---|
| [ffe885] | 343 |     if (n[i] < 0)
 | 
|---|
 | 344 |       n[i] = 0;
 | 
|---|
 | 345 |     if (n[i] >= N[i])
 | 
|---|
 | 346 |       n[i] = N[i]-1;
 | 
|---|
 | 347 |   }
 | 
|---|
 | 348 | 
 | 
|---|
 | 349 |   // calculate distance of cell to vector
 | 
|---|
 | 350 |   double distanceSquared = 0.;
 | 
|---|
 | 351 |   bool outside = true;  // flag whether x is found in- or outside of LinkedCell's domain/closest cell
 | 
|---|
 | 352 |   Vector corner; // current corner of closest cell
 | 
|---|
 | 353 |   Vector tester; // Vector pointing from corner to center of closest cell
 | 
|---|
 | 354 |   Vector Distance;  // Vector from corner of closest cell to x
 | 
|---|
 | 355 | 
 | 
|---|
 | 356 |   Vector center;  // center of the closest cell
 | 
|---|
 | 357 |   for (int i=0;i<NDIM;i++)
 | 
|---|
| [8cbb97] | 358 |     center[i] = min[i]+((double)n[i]+.5)*RADIUS;
 | 
|---|
| [ffe885] | 359 | 
 | 
|---|
 | 360 |   int c[NDIM];
 | 
|---|
 | 361 |   for (c[0]=0;c[0]<=1;c[0]++)
 | 
|---|
 | 362 |     for (c[1]=0; c[1]<=1;c[1]++)
 | 
|---|
 | 363 |       for (c[2]=0; c[2]<=1;c[2]++) {
 | 
|---|
 | 364 |         // set up corner
 | 
|---|
 | 365 |         for (int i=0;i<NDIM;i++)
 | 
|---|
| [8cbb97] | 366 |           corner[i] = min[i]+RADIUS*((double)n[i]+c[i]);
 | 
|---|
| [ffe885] | 367 |         // set up distance vector
 | 
|---|
| [8cbb97] | 368 |         Distance = (*x) - corner;
 | 
|---|
| [ffe885] | 369 |         const double dist = Distance.NormSquared();
 | 
|---|
 | 370 |         // check whether distance is smaller
 | 
|---|
 | 371 |         if (dist< distanceSquared)
 | 
|---|
 | 372 |           distanceSquared = dist;
 | 
|---|
 | 373 |         // check whether distance vector goes inside or outside
 | 
|---|
| [8cbb97] | 374 |         tester = center -corner;
 | 
|---|
 | 375 |         if (tester.ScalarProduct(Distance) < 0)
 | 
|---|
| [ffe885] | 376 |           outside = false;
 | 
|---|
 | 377 |       }
 | 
|---|
 | 378 |   return (outside ? distanceSquared : 0.);
 | 
|---|
 | 379 | };
 | 
|---|
| [734816] | 380 | 
 | 
|---|
 | 381 | /** Returns a list of all TesselPoint with distance less than \a radius to \a *Center.
 | 
|---|
 | 382 |  * \param radius radius of sphere
 | 
|---|
 | 383 |  * \param *center center of sphere
 | 
|---|
 | 384 |  * \return list of all points inside sphere
 | 
|---|
 | 385 |  */
 | 
|---|
 | 386 | LinkedCell::LinkedNodes* LinkedCell::GetPointsInsideSphere(const double radius, const Vector * const center) const
 | 
|---|
 | 387 | {
 | 
|---|
 | 388 |   const double radiusSquared = radius*radius;
 | 
|---|
 | 389 |   TesselPoint *Walker = NULL;
 | 
|---|
 | 390 |   LinkedNodes *TesselList = new LinkedNodes;
 | 
|---|
| [893bea] | 391 |   LinkedNodes *NeighbourList = NULL;
 | 
|---|
| [734816] | 392 | 
 | 
|---|
| [893bea] | 393 |   // set index of LC to center of sphere
 | 
|---|
| [ffe885] | 394 |   const double dist = SetClosestIndexToOutsideVector(center);
 | 
|---|
| [061b06] | 395 |   if (dist > 2.*radius) {
 | 
|---|
| [ffe885] | 396 |     DoeLog(1) && (eLog()<< Verbose(1) << "Vector " << *center << " is too far away from any atom in LinkedCell's bounding box." << endl);
 | 
|---|
| [734816] | 397 |     return TesselList;
 | 
|---|
| [061b06] | 398 |   } else
 | 
|---|
| [a67d19] | 399 |     DoLog(1) && (Log() << Verbose(1) << "Distance of closest cell to center of sphere with radius " << radius << " is " << dist << "." << endl);
 | 
|---|
| [893bea] | 400 | 
 | 
|---|
 | 401 |   // gather all neighbours first, then look who fulfills distance criteria
 | 
|---|
| [061b06] | 402 |   NeighbourList = GetallNeighbours(2.*radius-dist);
 | 
|---|
 | 403 |   //Log() << Verbose(1) << "I found " << NeighbourList->size() << " neighbours to check." << endl;
 | 
|---|
| [893bea] | 404 |   if (NeighbourList != NULL) {
 | 
|---|
 | 405 |     for (LinkedNodes::const_iterator Runner = NeighbourList->begin(); Runner != NeighbourList->end(); Runner++) {
 | 
|---|
 | 406 |       Walker = *Runner;
 | 
|---|
| [061b06] | 407 |       //Log() << Verbose(1) << "Current neighbour is at " << *Walker->node << "." << endl;
 | 
|---|
| [8cbb97] | 408 |       if ((center->DistanceSquared(*Walker->node) - radiusSquared) < MYEPSILON) {
 | 
|---|
| [893bea] | 409 |         TesselList->push_back(Walker);
 | 
|---|
| [734816] | 410 |       }
 | 
|---|
| [893bea] | 411 |     }
 | 
|---|
 | 412 |     delete(NeighbourList);
 | 
|---|
 | 413 |   } else
 | 
|---|
 | 414 |     DoeLog(2) && (eLog()<< Verbose(2) << "Around vector " << *center << " there are no atoms." << endl);
 | 
|---|
| [734816] | 415 |   return TesselList;
 | 
|---|
 | 416 | };
 | 
|---|