source: src/linkedcell.cpp@ bf3817

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since bf3817 was bf3817, checked in by Frederik Heber <heber@…>, 15 years ago

Added ifdef HAVE_CONFIG and config.h include to each and every cpp file.

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