source: molecuilder/src/linkedcell.cpp@ 09d3b8

Last change on this file since 09d3b8 was 543ce4, checked in by Frederik Heber <heber@…>, 16 years ago

Huge change from ofstream * (const) out --> Log().

  • first shift was done via regular expressions
  • then via error messages from the code
  • note that class atom, class element and class molecule kept in parts their output stream, was they print to file.
  • make check runs fine
  • MISSING: Verbosity is not fixed for everything (i.e. if no endl; is present and next has Verbose(0) ...)

Signed-off-by: Frederik Heber <heber@…>

  • Property mode set to 100644
File size: 9.0 KB
RevLine 
[eb167d]1/** \file linkedcell.cpp
2 *
3 * Function implementations for the class LinkedCell.
4 *
5 */
6
7
[17b3a5c]8#include "atom.hpp"
9#include "helpers.hpp"
[95183e]10#include "linkedcell.hpp"
[543ce4]11#include "log.hpp"
[f92d00]12#include "molecule.hpp"
[834ff3]13#include "tesselation.hpp"
[17b3a5c]14#include "vector.hpp"
[834ff3]15
16// ========================================================= class LinkedCell ===========================================
17
[95183e]18
19/** Constructor for class LinkedCell.
20 */
21LinkedCell::LinkedCell()
22{
[a048fa]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();
[95183e]30};
31
32/** Puts all atoms in \a *mol into a linked cell list with cell's lengths of \a RADIUS
[834ff3]33 * \param *set LCNodeSet class with all LCNode's
[95183e]34 * \param RADIUS edge length of cells
35 */
[a9b2a0a]36LinkedCell::LinkedCell(const PointCloud * const set, const double radius)
[95183e]37{
[834ff3]38 TesselPoint *Walker = NULL;
[95183e]39
[a048fa]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();
[543ce4]47 Log() << Verbose(1) << "Begin of LinkedCell" << endl;
[834ff3]48 if (set->IsEmpty()) {
[543ce4]49 eLog() << Verbose(0) << "ERROR: set contains no linked cell nodes!" << endl;
[a048fa]50 return;
51 }
52 // 1. find max and min per axis of atoms
[834ff3]53 set->GoToFirst();
54 Walker = set->GetPoint();
[a048fa]55 for (int i=0;i<NDIM;i++) {
[834ff3]56 max.x[i] = Walker->node->x[i];
57 min.x[i] = Walker->node->x[i];
[a048fa]58 }
[834ff3]59 set->GoToFirst();
[ef5521]60 while (!set->IsEnd()) {
[834ff3]61 Walker = set->GetPoint();
[a048fa]62 for (int i=0;i<NDIM;i++) {
[834ff3]63 if (max.x[i] < Walker->node->x[i])
64 max.x[i] = Walker->node->x[i];
65 if (min.x[i] > Walker->node->x[i])
66 min.x[i] = Walker->node->x[i];
[a048fa]67 }
[834ff3]68 set->GoToNext();
[a048fa]69 }
[543ce4]70 Log() << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl;
[e08f45]71
[834ff3]72 // 2. find then number of cells per axis
[a048fa]73 for (int i=0;i<NDIM;i++) {
74 N[i] = (int)floor((max.x[i] - min.x[i])/RADIUS)+1;
75 }
[543ce4]76 Log() << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl;
[e08f45]77
[a048fa]78 // 3. allocate the lists
[543ce4]79 Log() << Verbose(2) << "Allocating cells ... ";
[a048fa]80 if (LC != NULL) {
[543ce4]81 Log() << Verbose(1) << "ERROR: Linked Cell list is already allocated, I do nothing." << endl;
[a048fa]82 return;
83 }
[834ff3]84 LC = new LinkedNodes[N[0]*N[1]*N[2]];
[a048fa]85 for (index=0;index<N[0]*N[1]*N[2];index++) {
86 LC [index].clear();
87 }
[543ce4]88 Log() << Verbose(0) << "done." << endl;
[e08f45]89
[a048fa]90 // 4. put each atom into its respective cell
[543ce4]91 Log() << Verbose(2) << "Filling cells ... ";
[834ff3]92 set->GoToFirst();
[ef5521]93 while (!set->IsEnd()) {
[834ff3]94 Walker = set->GetPoint();
[a048fa]95 for (int i=0;i<NDIM;i++) {
[834ff3]96 n[i] = (int)floor((Walker->node->x[i] - min.x[i])/RADIUS);
[a048fa]97 }
98 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
99 LC[index].push_back(Walker);
[543ce4]100 //Log() << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
[834ff3]101 set->GoToNext();
[a048fa]102 }
[543ce4]103 Log() << Verbose(0) << "done." << endl;
104 Log() << Verbose(1) << "End of LinkedCell" << endl;
[95183e]105};
106
[6f1551]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 */
[a9b2a0a]112LinkedCell::LinkedCell(LinkedNodes *set, const double radius)
[6f1551]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();
[543ce4]122 Log() << Verbose(1) << "Begin of LinkedCell" << endl;
[6f1551]123 if (set->empty()) {
[543ce4]124 eLog() << Verbose(0) << "ERROR: set contains no linked cell nodes!" << endl;
[6f1551]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++) {
130 max.x[i] = (*Runner)->node->x[i];
131 min.x[i] = (*Runner)->node->x[i];
132 }
133 for (LinkedNodes::iterator Runner = set->begin(); Runner != set->end(); Runner++) {
134 Walker = *Runner;
135 for (int i=0;i<NDIM;i++) {
136 if (max.x[i] < Walker->node->x[i])
137 max.x[i] = Walker->node->x[i];
138 if (min.x[i] > Walker->node->x[i])
139 min.x[i] = Walker->node->x[i];
140 }
141 }
[543ce4]142 Log() << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl;
[6f1551]143
144 // 2. find then number of cells per axis
145 for (int i=0;i<NDIM;i++) {
146 N[i] = (int)floor((max.x[i] - min.x[i])/RADIUS)+1;
147 }
[543ce4]148 Log() << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl;
[6f1551]149
150 // 3. allocate the lists
[543ce4]151 Log() << Verbose(2) << "Allocating cells ... ";
[6f1551]152 if (LC != NULL) {
[543ce4]153 Log() << Verbose(1) << "ERROR: Linked Cell list is already allocated, I do nothing." << endl;
[6f1551]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 }
[543ce4]160 Log() << Verbose(0) << "done." << endl;
[6f1551]161
162 // 4. put each atom into its respective cell
[543ce4]163 Log() << Verbose(2) << "Filling cells ... ";
[6f1551]164 for (LinkedNodes::iterator Runner = set->begin(); Runner != set->end(); Runner++) {
165 Walker = *Runner;
166 for (int i=0;i<NDIM;i++) {
167 n[i] = (int)floor((Walker->node->x[i] - min.x[i])/RADIUS);
168 }
169 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
170 LC[index].push_back(Walker);
[543ce4]171 //Log() << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
[6f1551]172 }
[543ce4]173 Log() << Verbose(0) << "done." << endl;
174 Log() << Verbose(1) << "End of LinkedCell" << endl;
[6f1551]175};
176
[95183e]177/** Destructor for class LinkedCell.
178 */
179LinkedCell::~LinkedCell()
180{
[a048fa]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();
[95183e]190};
191
192/** Checks whether LinkedCell::n[] is each within [0,N[]].
193 * \return if all in intervals - true, else -false
194 */
[a9b2a0a]195bool LinkedCell::CheckBounds() const
[95183e]196{
[a048fa]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)
[543ce4]201 eLog() << Verbose(0) << "ERROR: indices are out of bounds!" << endl;
[a048fa]202 return status;
[95183e]203};
204
[aec775]205/** Checks whether LinkedCell::n[] plus relative offset is each within [0,N[]].
[872b51]206 * Note that for this check we don't admonish if out of bounds.
[aec775]207 * \param relative[NDIM] relative offset to current cell
208 * \return if all in intervals - true, else -false
209 */
[a9b2a0a]210bool LinkedCell::CheckBounds(const int relative[NDIM]) const
[aec775]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
[95183e]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 */
[a9b2a0a]222const LinkedNodes* LinkedCell::GetCurrentCell() const
[95183e]223{
[a048fa]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 }
[95183e]230};
231
[aec775]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 */
[a9b2a0a]236const LinkedNodes* LinkedCell::GetRelativeToCurrentCell(const int relative[NDIM]) const
[aec775]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
[834ff3]246/** Calculates the index for a given LCNode *Walker.
247 * \param *Walker LCNode to set index tos
[95183e]248 * \return if the atom is also found in this cell - true, else - false
249 */
[a9b2a0a]250bool LinkedCell::SetIndexToNode(const TesselPoint * const Walker) const
[95183e]251{
[a048fa]252 bool status = false;
253 for (int i=0;i<NDIM;i++) {
[834ff3]254 n[i] = (int)floor((Walker->node->x[i] - min.x[i])/RADIUS);
[a048fa]255 }
256 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
257 if (CheckBounds()) {
[834ff3]258 for (LinkedNodes::iterator Runner = LC[index].begin(); Runner != LC[index].end(); Runner++)
[a048fa]259 status = status || ((*Runner) == Walker);
260 return status;
261 } else {
[543ce4]262 eLog() << Verbose(1) << "ERROR: Node at " << *Walker << " is out of bounds." << endl;
[a048fa]263 return false;
264 }
[95183e]265};
266
[a87d5e6]267/** Calculates the interval bounds of the linked cell grid.
268 * \param *lower lower bounds
269 * \param *upper upper bounds
270 */
[a9b2a0a]271void LinkedCell::GetNeighbourBounds(int lower[NDIM], int upper[NDIM]) const
[a87d5e6]272{
273 for (int i=0;i<NDIM;i++) {
274 lower[i] = ((n[i]-1) >= 0) ? n[i]-1 : 0;
275 upper[i] = ((n[i]+1) < N[i]) ? n[i]+1 : N[i]-1;
[543ce4]276 //Log() << Verbose(0) << " [" << Nlower[i] << "," << Nupper[i] << "] ";
[a87d5e6]277 // check for this axis whether the point is outside of our grid
278 if (n[i] < 0)
279 upper[i] = lower[i];
280 if (n[i] > N[i])
281 lower[i] = upper[i];
282
[543ce4]283 //Log() << Verbose(0) << "axis " << i << " has bounds [" << lower[i] << "," << upper[i] << "]" << endl;
[a87d5e6]284 }
285};
286
[95183e]287/** Calculates the index for a given Vector *x.
288 * \param *x Vector with coordinates
289 * \return Vector is inside bounding box - true, else - false
290 */
[a9b2a0a]291bool LinkedCell::SetIndexToVector(const Vector * const x) const
[95183e]292{
[a048fa]293 bool status = true;
294 for (int i=0;i<NDIM;i++) {
295 n[i] = (int)floor((x->x[i] - min.x[i])/RADIUS);
296 if (max.x[i] < x->x[i])
297 status = false;
298 if (min.x[i] > x->x[i])
299 status = false;
300 }
301 return status;
[95183e]302};
[e08f45]303
Note: See TracBrowser for help on using the repository browser.