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