| [edb93c] | 1 | /** \file linkedcell.cpp | 
|---|
|  | 2 | * | 
|---|
|  | 3 | * Function implementations for the class LinkedCell. | 
|---|
|  | 4 | * | 
|---|
|  | 5 | */ | 
|---|
|  | 6 |  | 
|---|
|  | 7 |  | 
|---|
| [e1bc68] | 8 | #include "linkedcell.hpp" | 
|---|
|  | 9 | #include "molecules.hpp" | 
|---|
| [357fba] | 10 | #include "tesselation.hpp" | 
|---|
|  | 11 |  | 
|---|
|  | 12 | // ========================================================= class LinkedCell =========================================== | 
|---|
|  | 13 |  | 
|---|
| [e1bc68] | 14 |  | 
|---|
|  | 15 | /** Constructor for class LinkedCell. | 
|---|
|  | 16 | */ | 
|---|
|  | 17 | LinkedCell::LinkedCell() | 
|---|
|  | 18 | { | 
|---|
| [042f82] | 19 | LC = NULL; | 
|---|
|  | 20 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 21 | N[i] = 0; | 
|---|
|  | 22 | index = -1; | 
|---|
|  | 23 | RADIUS = 0.; | 
|---|
|  | 24 | max.Zero(); | 
|---|
|  | 25 | min.Zero(); | 
|---|
| [e1bc68] | 26 | }; | 
|---|
|  | 27 |  | 
|---|
|  | 28 | /** Puts all atoms in \a *mol into a linked cell list with cell's lengths of \a RADIUS | 
|---|
| [357fba] | 29 | * \param *set LCNodeSet class with all LCNode's | 
|---|
| [e1bc68] | 30 | * \param RADIUS edge length of cells | 
|---|
|  | 31 | */ | 
|---|
| [357fba] | 32 | LinkedCell::LinkedCell(PointCloud *set, double radius) | 
|---|
| [e1bc68] | 33 | { | 
|---|
| [357fba] | 34 | TesselPoint *Walker = NULL; | 
|---|
| [e1bc68] | 35 |  | 
|---|
| [042f82] | 36 | RADIUS = radius; | 
|---|
|  | 37 | LC = NULL; | 
|---|
|  | 38 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 39 | N[i] = 0; | 
|---|
|  | 40 | index = -1; | 
|---|
|  | 41 | max.Zero(); | 
|---|
|  | 42 | min.Zero(); | 
|---|
|  | 43 | cout << Verbose(1) << "Begin of LinkedCell" << endl; | 
|---|
| [357fba] | 44 | if (set->IsEmpty()) { | 
|---|
|  | 45 | cerr << "ERROR: set contains no linked cell nodes!" << endl; | 
|---|
| [042f82] | 46 | return; | 
|---|
|  | 47 | } | 
|---|
|  | 48 | // 1. find max and min per axis of atoms | 
|---|
| [357fba] | 49 | set->GoToFirst(); | 
|---|
|  | 50 | Walker = set->GetPoint(); | 
|---|
| [042f82] | 51 | for (int i=0;i<NDIM;i++) { | 
|---|
| [357fba] | 52 | max.x[i] = Walker->node->x[i]; | 
|---|
|  | 53 | min.x[i] = Walker->node->x[i]; | 
|---|
| [042f82] | 54 | } | 
|---|
| [357fba] | 55 | set->GoToFirst(); | 
|---|
| [1999d8] | 56 | while (!set->IsEnd()) { | 
|---|
| [357fba] | 57 | Walker = set->GetPoint(); | 
|---|
| [042f82] | 58 | for (int i=0;i<NDIM;i++) { | 
|---|
| [357fba] | 59 | if (max.x[i] < Walker->node->x[i]) | 
|---|
|  | 60 | max.x[i] = Walker->node->x[i]; | 
|---|
|  | 61 | if (min.x[i] > Walker->node->x[i]) | 
|---|
|  | 62 | min.x[i] = Walker->node->x[i]; | 
|---|
| [042f82] | 63 | } | 
|---|
| [357fba] | 64 | set->GoToNext(); | 
|---|
| [042f82] | 65 | } | 
|---|
|  | 66 | cout << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl; | 
|---|
| [6ac7ee] | 67 |  | 
|---|
| [357fba] | 68 | // 2. find then number of cells per axis | 
|---|
| [042f82] | 69 | for (int i=0;i<NDIM;i++) { | 
|---|
|  | 70 | N[i] = (int)floor((max.x[i] - min.x[i])/RADIUS)+1; | 
|---|
|  | 71 | } | 
|---|
|  | 72 | cout << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl; | 
|---|
| [6ac7ee] | 73 |  | 
|---|
| [042f82] | 74 | // 3. allocate the lists | 
|---|
|  | 75 | cout << Verbose(2) << "Allocating cells ... "; | 
|---|
|  | 76 | if (LC != NULL) { | 
|---|
|  | 77 | cout << Verbose(1) << "ERROR: Linked Cell list is already allocated, I do nothing." << endl; | 
|---|
|  | 78 | return; | 
|---|
|  | 79 | } | 
|---|
| [357fba] | 80 | LC = new LinkedNodes[N[0]*N[1]*N[2]]; | 
|---|
| [042f82] | 81 | for (index=0;index<N[0]*N[1]*N[2];index++) { | 
|---|
|  | 82 | LC [index].clear(); | 
|---|
|  | 83 | } | 
|---|
|  | 84 | cout << "done."  << endl; | 
|---|
| [6ac7ee] | 85 |  | 
|---|
| [042f82] | 86 | // 4. put each atom into its respective cell | 
|---|
| [8cede7] | 87 | cout << Verbose(2) << "Filling cells ... "; | 
|---|
| [357fba] | 88 | set->GoToFirst(); | 
|---|
| [1999d8] | 89 | while (!set->IsEnd()) { | 
|---|
| [357fba] | 90 | Walker = set->GetPoint(); | 
|---|
| [042f82] | 91 | for (int i=0;i<NDIM;i++) { | 
|---|
| [357fba] | 92 | n[i] = (int)floor((Walker->node->x[i] - min.x[i])/RADIUS); | 
|---|
| [042f82] | 93 | } | 
|---|
|  | 94 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2]; | 
|---|
|  | 95 | LC[index].push_back(Walker); | 
|---|
|  | 96 | //cout << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl; | 
|---|
| [357fba] | 97 | set->GoToNext(); | 
|---|
| [042f82] | 98 | } | 
|---|
| [8cede7] | 99 | cout << "done."  << endl; | 
|---|
| [042f82] | 100 | cout << Verbose(1) << "End of LinkedCell" << endl; | 
|---|
| [e1bc68] | 101 | }; | 
|---|
|  | 102 |  | 
|---|
|  | 103 | /** Destructor for class LinkedCell. | 
|---|
|  | 104 | */ | 
|---|
|  | 105 | LinkedCell::~LinkedCell() | 
|---|
|  | 106 | { | 
|---|
| [042f82] | 107 | if (LC != NULL) | 
|---|
|  | 108 | for (index=0;index<N[0]*N[1]*N[2];index++) | 
|---|
|  | 109 | LC[index].clear(); | 
|---|
|  | 110 | delete[](LC); | 
|---|
|  | 111 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 112 | N[i] = 0; | 
|---|
|  | 113 | index = -1; | 
|---|
|  | 114 | max.Zero(); | 
|---|
|  | 115 | min.Zero(); | 
|---|
| [e1bc68] | 116 | }; | 
|---|
|  | 117 |  | 
|---|
|  | 118 | /** Checks whether LinkedCell::n[] is each within [0,N[]]. | 
|---|
|  | 119 | * \return if all in intervals - true, else -false | 
|---|
|  | 120 | */ | 
|---|
|  | 121 | bool LinkedCell::CheckBounds() | 
|---|
|  | 122 | { | 
|---|
| [042f82] | 123 | bool status = true; | 
|---|
|  | 124 | for(int i=0;i<NDIM;i++) | 
|---|
|  | 125 | status = status && ((n[i] >=0) && (n[i] < N[i])); | 
|---|
|  | 126 | if (!status) | 
|---|
|  | 127 | cerr << "ERROR: indices are out of bounds!" << endl; | 
|---|
|  | 128 | return status; | 
|---|
| [e1bc68] | 129 | }; | 
|---|
|  | 130 |  | 
|---|
|  | 131 |  | 
|---|
|  | 132 | /** Returns a pointer to the current cell. | 
|---|
|  | 133 | * \return LinkedAtoms pointer to current cell, NULL if LinkedCell::n[] are out of bounds. | 
|---|
|  | 134 | */ | 
|---|
| [357fba] | 135 | LinkedNodes* LinkedCell::GetCurrentCell() | 
|---|
| [e1bc68] | 136 | { | 
|---|
| [042f82] | 137 | if (CheckBounds()) { | 
|---|
|  | 138 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2]; | 
|---|
|  | 139 | return (&(LC[index])); | 
|---|
|  | 140 | } else { | 
|---|
|  | 141 | return NULL; | 
|---|
|  | 142 | } | 
|---|
| [e1bc68] | 143 | }; | 
|---|
|  | 144 |  | 
|---|
| [357fba] | 145 | /** Calculates the index for a given LCNode *Walker. | 
|---|
|  | 146 | * \param *Walker LCNode to set index tos | 
|---|
| [e1bc68] | 147 | * \return if the atom is also found in this cell - true, else - false | 
|---|
|  | 148 | */ | 
|---|
| [ab1932] | 149 | bool LinkedCell::SetIndexToNode(const TesselPoint *Walker) | 
|---|
| [e1bc68] | 150 | { | 
|---|
| [042f82] | 151 | bool status = false; | 
|---|
|  | 152 | for (int i=0;i<NDIM;i++) { | 
|---|
| [357fba] | 153 | n[i] = (int)floor((Walker->node->x[i] - min.x[i])/RADIUS); | 
|---|
| [042f82] | 154 | } | 
|---|
|  | 155 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2]; | 
|---|
|  | 156 | if (CheckBounds()) { | 
|---|
| [357fba] | 157 | for (LinkedNodes::iterator Runner = LC[index].begin(); Runner != LC[index].end(); Runner++) | 
|---|
| [042f82] | 158 | status = status || ((*Runner) == Walker); | 
|---|
|  | 159 | return status; | 
|---|
|  | 160 | } else { | 
|---|
| [357fba] | 161 | cerr << Verbose(1) << "ERROR: Node at " << *Walker << " is out of bounds." << endl; | 
|---|
| [042f82] | 162 | return false; | 
|---|
|  | 163 | } | 
|---|
| [e1bc68] | 164 | }; | 
|---|
|  | 165 |  | 
|---|
| [0f4538] | 166 | /** Calculates the interval bounds of the linked cell grid. | 
|---|
|  | 167 | * \param *lower lower bounds | 
|---|
|  | 168 | * \param *upper upper bounds | 
|---|
|  | 169 | */ | 
|---|
|  | 170 | void LinkedCell::GetNeighbourBounds(int lower[NDIM], int upper[NDIM]) | 
|---|
|  | 171 | { | 
|---|
|  | 172 | for (int i=0;i<NDIM;i++) { | 
|---|
|  | 173 | lower[i] = ((n[i]-1) >= 0) ? n[i]-1 : 0; | 
|---|
|  | 174 | upper[i] = ((n[i]+1) < N[i]) ? n[i]+1 : N[i]-1; | 
|---|
|  | 175 | //cout << " [" << Nlower[i] << "," << Nupper[i] << "] "; | 
|---|
|  | 176 | // check for this axis whether the point is outside of our grid | 
|---|
|  | 177 | if (n[i] < 0) | 
|---|
|  | 178 | upper[i] = lower[i]; | 
|---|
|  | 179 | if (n[i] > N[i]) | 
|---|
|  | 180 | lower[i] = upper[i]; | 
|---|
|  | 181 |  | 
|---|
|  | 182 | //cout << "axis " << i << " has bounds [" << lower[i] << "," << upper[i] << "]" << endl; | 
|---|
|  | 183 | } | 
|---|
|  | 184 | }; | 
|---|
|  | 185 |  | 
|---|
| [e1bc68] | 186 | /** Calculates the index for a given Vector *x. | 
|---|
|  | 187 | * \param *x Vector with coordinates | 
|---|
|  | 188 | * \return Vector is inside bounding box - true, else - false | 
|---|
|  | 189 | */ | 
|---|
| [0f4538] | 190 | bool LinkedCell::SetIndexToVector(const Vector *x) | 
|---|
| [e1bc68] | 191 | { | 
|---|
| [042f82] | 192 | bool status = true; | 
|---|
|  | 193 | for (int i=0;i<NDIM;i++) { | 
|---|
|  | 194 | n[i] = (int)floor((x->x[i] - min.x[i])/RADIUS); | 
|---|
|  | 195 | if (max.x[i] < x->x[i]) | 
|---|
|  | 196 | status = false; | 
|---|
|  | 197 | if (min.x[i] > x->x[i]) | 
|---|
|  | 198 | status = false; | 
|---|
|  | 199 | } | 
|---|
|  | 200 | return status; | 
|---|
| [e1bc68] | 201 | }; | 
|---|
| [6ac7ee] | 202 |  | 
|---|