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