source: src/linkedcell.cpp@ 36166d

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 36166d was 36166d, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Removed left over parts from old memory-tracker

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