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 | };
|
---|
191 |
|
---|
192 | /** Checks whether LinkedCell::n[] is each within [0,N[]].
|
---|
193 | * \return if all in intervals - true, else -false
|
---|
194 | */
|
---|
195 | bool LinkedCell::CheckBounds() const
|
---|
196 | {
|
---|
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)
|
---|
201 | // DoeLog(1) && (eLog()<< Verbose(1) << "indices are out of bounds!" << endl);
|
---|
202 | return status;
|
---|
203 | };
|
---|
204 |
|
---|
205 | /** Checks whether LinkedCell::n[] plus relative offset is each within [0,N[]].
|
---|
206 | * Note that for this check we don't admonish if out of bounds.
|
---|
207 | * \param relative[NDIM] relative offset to current cell
|
---|
208 | * \return if all in intervals - true, else -false
|
---|
209 | */
|
---|
210 | bool LinkedCell::CheckBounds(const int relative[NDIM]) const
|
---|
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 |
|
---|
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 | */
|
---|
222 | const LinkedCell::LinkedNodes* LinkedCell::GetCurrentCell() const
|
---|
223 | {
|
---|
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 | }
|
---|
230 | };
|
---|
231 |
|
---|
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 | */
|
---|
236 | const LinkedCell::LinkedNodes* LinkedCell::GetRelativeToCurrentCell(const int relative[NDIM]) const
|
---|
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 |
|
---|
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++)
|
---|
253 | n[i] = (int)floor((x->at(i) - min[i])/RADIUS);
|
---|
254 |
|
---|
255 | return CheckBounds();
|
---|
256 | };
|
---|
257 |
|
---|
258 | /** Calculates the index for a given LCNode *Walker.
|
---|
259 | * \param *Walker LCNode to set index tos
|
---|
260 | * \return if the atom is also found in this cell - true, else - false
|
---|
261 | */
|
---|
262 | bool LinkedCell::SetIndexToNode(const TesselPoint * const Walker) const
|
---|
263 | {
|
---|
264 | bool status = false;
|
---|
265 | for (int i=0;i<NDIM;i++) {
|
---|
266 | n[i] = static_cast<int>(floor((Walker->node->at(i) - min[i])/RADIUS));
|
---|
267 | }
|
---|
268 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
|
---|
269 | if (CheckBounds()) {
|
---|
270 | for (LinkedNodes::iterator Runner = LC[index].begin(); Runner != LC[index].end(); Runner++)
|
---|
271 | status = status || ((*Runner) == Walker);
|
---|
272 | return status;
|
---|
273 | } else {
|
---|
274 | DoeLog(1) && (eLog()<< Verbose(1) << "Node at " << *Walker << " is out of bounds." << endl);
|
---|
275 | return false;
|
---|
276 | }
|
---|
277 | };
|
---|
278 |
|
---|
279 | /** Calculates the interval bounds of the linked cell grid.
|
---|
280 | * \param lower lower bounds
|
---|
281 | * \param upper upper bounds
|
---|
282 | * \param step how deep to check the neighbouring cells (i.e. number of layers to check)
|
---|
283 | */
|
---|
284 | void LinkedCell::GetNeighbourBounds(int lower[NDIM], int upper[NDIM], int step) const
|
---|
285 | {
|
---|
286 | for (int i=0;i<NDIM;i++) {
|
---|
287 | lower[i] = n[i]-step;
|
---|
288 | if (lower[i] < 0)
|
---|
289 | lower[i] = 0;
|
---|
290 | if (lower[i] >= N[i])
|
---|
291 | lower[i] = N[i]-1;
|
---|
292 | upper[i] = n[i]+step;
|
---|
293 | if (upper[i] >= N[i])
|
---|
294 | upper[i] = N[i]-1;
|
---|
295 | if (upper[i] < 0)
|
---|
296 | upper[i] = 0;
|
---|
297 | //Log() << Verbose(0) << "axis " << i << " has bounds [" << lower[i] << "," << upper[i] << "]" << endl;
|
---|
298 | }
|
---|
299 | };
|
---|
300 |
|
---|
301 | /** Returns a list with all neighbours from the current LinkedCell::index.
|
---|
302 | * \param distance (if no distance, then adjacent cells are taken)
|
---|
303 | * \return list of tesselpoints
|
---|
304 | */
|
---|
305 | LinkedCell::LinkedNodes* LinkedCell::GetallNeighbours(const double distance) const
|
---|
306 | {
|
---|
307 | int Nlower[NDIM], Nupper[NDIM];
|
---|
308 | TesselPoint *Walker = NULL;
|
---|
309 | LinkedNodes *TesselList = new LinkedNodes;
|
---|
310 |
|
---|
311 | // then go through the current and all neighbouring cells and check the contained points for possible candidates
|
---|
312 | const int step = (distance == 0) ? 1 : (int)floor(distance/RADIUS + 1.);
|
---|
313 | GetNeighbourBounds(Nlower, Nupper, step);
|
---|
314 |
|
---|
315 | //Log() << Verbose(0) << endl;
|
---|
316 | for (n[0] = Nlower[0]; n[0] <= Nupper[0]; n[0]++)
|
---|
317 | for (n[1] = Nlower[1]; n[1] <= Nupper[1]; n[1]++)
|
---|
318 | for (n[2] = Nlower[2]; n[2] <= Nupper[2]; n[2]++) {
|
---|
319 | const LinkedNodes *List = GetCurrentCell();
|
---|
320 | //Log() << Verbose(1) << "Current cell is " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
|
---|
321 | if (List != NULL) {
|
---|
322 | for (LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
|
---|
323 | Walker = *Runner;
|
---|
324 | TesselList->push_back(Walker);
|
---|
325 | }
|
---|
326 | }
|
---|
327 | }
|
---|
328 | return TesselList;
|
---|
329 | };
|
---|
330 |
|
---|
331 | /** Set the index to the cell containing a given Vector *x, which is not inside the LinkedCell's domain
|
---|
332 | * Note that as we have to check distance from every corner of the closest cell, this function is faw more
|
---|
333 | * expensive and if Vector is known to be inside LinkedCell's domain, then SetIndexToVector() should be used.
|
---|
334 | * \param *x Vector with coordinates
|
---|
335 | * \return minimum squared distance of cell to given vector (if inside of domain, distance is 0)
|
---|
336 | */
|
---|
337 | double LinkedCell::SetClosestIndexToOutsideVector(const Vector * const x) const
|
---|
338 | {
|
---|
339 | for (int i=0;i<NDIM;i++) {
|
---|
340 | n[i] = (int)floor((x->at(i) - min[i])/RADIUS);
|
---|
341 | if (n[i] < 0)
|
---|
342 | n[i] = 0;
|
---|
343 | if (n[i] >= N[i])
|
---|
344 | n[i] = N[i]-1;
|
---|
345 | }
|
---|
346 |
|
---|
347 | // calculate distance of cell to vector
|
---|
348 | double distanceSquared = 0.;
|
---|
349 | bool outside = true; // flag whether x is found in- or outside of LinkedCell's domain/closest cell
|
---|
350 | Vector corner; // current corner of closest cell
|
---|
351 | Vector tester; // Vector pointing from corner to center of closest cell
|
---|
352 | Vector Distance; // Vector from corner of closest cell to x
|
---|
353 |
|
---|
354 | Vector center; // center of the closest cell
|
---|
355 | for (int i=0;i<NDIM;i++)
|
---|
356 | center[i] = min[i]+((double)n[i]+.5)*RADIUS;
|
---|
357 |
|
---|
358 | int c[NDIM];
|
---|
359 | for (c[0]=0;c[0]<=1;c[0]++)
|
---|
360 | for (c[1]=0; c[1]<=1;c[1]++)
|
---|
361 | for (c[2]=0; c[2]<=1;c[2]++) {
|
---|
362 | // set up corner
|
---|
363 | for (int i=0;i<NDIM;i++)
|
---|
364 | corner[i] = min[i]+RADIUS*((double)n[i]+c[i]);
|
---|
365 | // set up distance vector
|
---|
366 | Distance = (*x) - corner;
|
---|
367 | const double dist = Distance.NormSquared();
|
---|
368 | // check whether distance is smaller
|
---|
369 | if (dist< distanceSquared)
|
---|
370 | distanceSquared = dist;
|
---|
371 | // check whether distance vector goes inside or outside
|
---|
372 | tester = center -corner;
|
---|
373 | if (tester.ScalarProduct(Distance) < 0)
|
---|
374 | outside = false;
|
---|
375 | }
|
---|
376 | return (outside ? distanceSquared : 0.);
|
---|
377 | };
|
---|
378 |
|
---|
379 | /** Returns a list of all TesselPoint with distance less than \a radius to \a *Center.
|
---|
380 | * \param radius radius of sphere
|
---|
381 | * \param *center center of sphere
|
---|
382 | * \return list of all points inside sphere
|
---|
383 | */
|
---|
384 | LinkedCell::LinkedNodes* LinkedCell::GetPointsInsideSphere(const double radius, const Vector * const center) const
|
---|
385 | {
|
---|
386 | const double radiusSquared = radius*radius;
|
---|
387 | TesselPoint *Walker = NULL;
|
---|
388 | LinkedNodes *TesselList = new LinkedNodes;
|
---|
389 | LinkedNodes *NeighbourList = NULL;
|
---|
390 |
|
---|
391 | // set index of LC to center of sphere
|
---|
392 | const double dist = SetClosestIndexToOutsideVector(center);
|
---|
393 | if (dist > 2.*radius) {
|
---|
394 | DoeLog(1) && (eLog()<< Verbose(1) << "Vector " << *center << " is too far away from any atom in LinkedCell's bounding box." << endl);
|
---|
395 | return TesselList;
|
---|
396 | } else
|
---|
397 | DoLog(1) && (Log() << Verbose(1) << "Distance of closest cell to center of sphere with radius " << radius << " is " << dist << "." << endl);
|
---|
398 |
|
---|
399 | // gather all neighbours first, then look who fulfills distance criteria
|
---|
400 | NeighbourList = GetallNeighbours(2.*radius-dist);
|
---|
401 | //Log() << Verbose(1) << "I found " << NeighbourList->size() << " neighbours to check." << endl;
|
---|
402 | if (NeighbourList != NULL) {
|
---|
403 | for (LinkedNodes::const_iterator Runner = NeighbourList->begin(); Runner != NeighbourList->end(); Runner++) {
|
---|
404 | Walker = *Runner;
|
---|
405 | //Log() << Verbose(1) << "Current neighbour is at " << *Walker->node << "." << endl;
|
---|
406 | if ((center->DistanceSquared(*Walker->node) - radiusSquared) < MYEPSILON) {
|
---|
407 | TesselList->push_back(Walker);
|
---|
408 | }
|
---|
409 | }
|
---|
410 | delete(NeighbourList);
|
---|
411 | } else
|
---|
412 | DoeLog(2) && (eLog()<< Verbose(2) << "Around vector " << *center << " there are no atoms." << endl);
|
---|
413 | return TesselList;
|
---|
414 | };
|
---|