source: src/linkedcell.cpp@ b6dbff

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

Added #include "Helpers/MemDebug.hpp" to all .cpp files

  • 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"
[e138de]12#include "log.hpp"
[cee0b57]13#include "molecule.hpp"
[357fba]14#include "tesselation.hpp"
[f66195]15#include "vector.hpp"
[357fba]16
17// ========================================================= class LinkedCell ===========================================
18
[e1bc68]19
20/** Constructor for class LinkedCell.
21 */
22LinkedCell::LinkedCell()
23{
[042f82]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();
[e1bc68]31};
32
33/** Puts all atoms in \a *mol into a linked cell list with cell's lengths of \a RADIUS
[357fba]34 * \param *set LCNodeSet class with all LCNode's
[e1bc68]35 * \param RADIUS edge length of cells
36 */
[776b64]37LinkedCell::LinkedCell(const PointCloud * const set, const double radius)
[e1bc68]38{
[357fba]39 TesselPoint *Walker = NULL;
[e1bc68]40
[042f82]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();
[a67d19]48 DoLog(1) && (Log() << Verbose(1) << "Begin of LinkedCell" << endl);
[caf4ba]49 if ((set == NULL) || (set->IsEmpty())) {
[58ed4a]50 DoeLog(1) && (eLog()<< Verbose(1) << "set is NULL or contains no linked cell nodes!" << endl);
[042f82]51 return;
52 }
53 // 1. find max and min per axis of atoms
[357fba]54 set->GoToFirst();
55 Walker = set->GetPoint();
[042f82]56 for (int i=0;i<NDIM;i++) {
[0a4f7f]57 max[i] = Walker->node->at(i);
58 min[i] = Walker->node->at(i);
[042f82]59 }
[357fba]60 set->GoToFirst();
[1999d8]61 while (!set->IsEnd()) {
[357fba]62 Walker = set->GetPoint();
[042f82]63 for (int i=0;i<NDIM;i++) {
[0a4f7f]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);
[042f82]68 }
[357fba]69 set->GoToNext();
[042f82]70 }
[a67d19]71 DoLog(2) && (Log() << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl);
[6ac7ee]72
[357fba]73 // 2. find then number of cells per axis
[042f82]74 for (int i=0;i<NDIM;i++) {
[0a4f7f]75 N[i] = static_cast<int>(floor((max[i] - min[i])/RADIUS)+1);
[042f82]76 }
[a67d19]77 DoLog(2) && (Log() << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl);
[6ac7ee]78
[042f82]79 // 3. allocate the lists
[a67d19]80 DoLog(2) && (Log() << Verbose(2) << "Allocating cells ... ");
[042f82]81 if (LC != NULL) {
[58ed4a]82 DoeLog(1) && (eLog()<< Verbose(1) << "Linked Cell list is already allocated, I do nothing." << endl);
[042f82]83 return;
84 }
[357fba]85 LC = new LinkedNodes[N[0]*N[1]*N[2]];
[042f82]86 for (index=0;index<N[0]*N[1]*N[2];index++) {
87 LC [index].clear();
88 }
[a67d19]89 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
[6ac7ee]90
[042f82]91 // 4. put each atom into its respective cell
[a67d19]92 DoLog(2) && (Log() << Verbose(2) << "Filling cells ... ");
[357fba]93 set->GoToFirst();
[1999d8]94 while (!set->IsEnd()) {
[357fba]95 Walker = set->GetPoint();
[042f82]96 for (int i=0;i<NDIM;i++) {
[0a4f7f]97 n[i] = static_cast<int>(floor((Walker->node->at(i) - min[i])/RADIUS));
[042f82]98 }
99 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
100 LC[index].push_back(Walker);
[e138de]101 //Log() << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
[357fba]102 set->GoToNext();
[042f82]103 }
[a67d19]104 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
105 DoLog(1) && (Log() << Verbose(1) << "End of LinkedCell" << endl);
[e1bc68]106};
107
[8cd903]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 */
[776b64]113LinkedCell::LinkedCell(LinkedNodes *set, const double radius)
[8cd903]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();
[a67d19]123 DoLog(1) && (Log() << Verbose(1) << "Begin of LinkedCell" << endl);
[8cd903]124 if (set->empty()) {
[58ed4a]125 DoeLog(1) && (eLog()<< Verbose(1) << "set contains no linked cell nodes!" << endl);
[8cd903]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++) {
[0a4f7f]131 max[i] = (*Runner)->node->at(i);
132 min[i] = (*Runner)->node->at(i);
[8cd903]133 }
134 for (LinkedNodes::iterator Runner = set->begin(); Runner != set->end(); Runner++) {
135 Walker = *Runner;
136 for (int i=0;i<NDIM;i++) {
[0a4f7f]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);
[8cd903]141 }
142 }
[a67d19]143 DoLog(2) && (Log() << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl);
[8cd903]144
145 // 2. find then number of cells per axis
146 for (int i=0;i<NDIM;i++) {
[0a4f7f]147 N[i] = static_cast<int>(floor((max[i] - min[i])/RADIUS)+1);
[8cd903]148 }
[a67d19]149 DoLog(2) && (Log() << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl);
[8cd903]150
151 // 3. allocate the lists
[a67d19]152 DoLog(2) && (Log() << Verbose(2) << "Allocating cells ... ");
[8cd903]153 if (LC != NULL) {
[58ed4a]154 DoeLog(1) && (eLog()<< Verbose(1) << "Linked Cell list is already allocated, I do nothing." << endl);
[8cd903]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 }
[a67d19]161 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
[8cd903]162
163 // 4. put each atom into its respective cell
[a67d19]164 DoLog(2) && (Log() << Verbose(2) << "Filling cells ... ");
[8cd903]165 for (LinkedNodes::iterator Runner = set->begin(); Runner != set->end(); Runner++) {
166 Walker = *Runner;
167 for (int i=0;i<NDIM;i++) {
[0a4f7f]168 n[i] = static_cast<int>(floor((Walker->node->at(i) - min[i])/RADIUS));
[8cd903]169 }
170 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
171 LC[index].push_back(Walker);
[e138de]172 //Log() << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
[8cd903]173 }
[a67d19]174 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
175 DoLog(1) && (Log() << Verbose(1) << "End of LinkedCell" << endl);
[8cd903]176};
177
[e1bc68]178/** Destructor for class LinkedCell.
179 */
180LinkedCell::~LinkedCell()
181{
[042f82]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();
[e1bc68]191};
192
193/** Checks whether LinkedCell::n[] is each within [0,N[]].
194 * \return if all in intervals - true, else -false
195 */
[776b64]196bool LinkedCell::CheckBounds() const
[e1bc68]197{
[042f82]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)
[58ed4a]202 DoeLog(1) && (eLog()<< Verbose(1) << "indices are out of bounds!" << endl);
[042f82]203 return status;
[e1bc68]204};
205
[07051c]206/** Checks whether LinkedCell::n[] plus relative offset is each within [0,N[]].
[266237]207 * Note that for this check we don't admonish if out of bounds.
[07051c]208 * \param relative[NDIM] relative offset to current cell
209 * \return if all in intervals - true, else -false
210 */
[776b64]211bool LinkedCell::CheckBounds(const int relative[NDIM]) const
[07051c]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
[e1bc68]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 */
[734816]223const LinkedCell::LinkedNodes* LinkedCell::GetCurrentCell() const
[e1bc68]224{
[042f82]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 }
[e1bc68]231};
232
[07051c]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 */
[734816]237const LinkedCell::LinkedNodes* LinkedCell::GetRelativeToCurrentCell(const int relative[NDIM]) const
[07051c]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
[893bea]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 */
251bool LinkedCell::SetIndexToVector(const Vector * const x) const
252{
253 for (int i=0;i<NDIM;i++)
[8cbb97]254 n[i] = (int)floor((x->at(i) - min[i])/RADIUS);
[893bea]255
256 return CheckBounds();
257};
258
[357fba]259/** Calculates the index for a given LCNode *Walker.
260 * \param *Walker LCNode to set index tos
[e1bc68]261 * \return if the atom is also found in this cell - true, else - false
262 */
[776b64]263bool LinkedCell::SetIndexToNode(const TesselPoint * const Walker) const
[e1bc68]264{
[042f82]265 bool status = false;
266 for (int i=0;i<NDIM;i++) {
[0a4f7f]267 n[i] = static_cast<int>(floor((Walker->node->at(i) - min[i])/RADIUS));
[042f82]268 }
269 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
270 if (CheckBounds()) {
[357fba]271 for (LinkedNodes::iterator Runner = LC[index].begin(); Runner != LC[index].end(); Runner++)
[042f82]272 status = status || ((*Runner) == Walker);
273 return status;
274 } else {
[58ed4a]275 DoeLog(1) && (eLog()<< Verbose(1) << "Node at " << *Walker << " is out of bounds." << endl);
[042f82]276 return false;
277 }
[e1bc68]278};
279
[0f4538]280/** Calculates the interval bounds of the linked cell grid.
281 * \param *lower lower bounds
282 * \param *upper upper bounds
[061b06]283 * \param step how deep to check the neighbouring cells (i.e. number of layers to check)
[0f4538]284 */
[893bea]285void LinkedCell::GetNeighbourBounds(int lower[NDIM], int upper[NDIM], int step) const
[0f4538]286{
287 for (int i=0;i<NDIM;i++) {
[061b06]288 lower[i] = n[i];
[893bea]289 for (int s=step; s>0;--s)
290 if ((n[i]-s) >= 0) {
291 lower[i] = n[i]-s;
292 break;
293 }
[061b06]294 upper[i] = n[i];
[893bea]295 for (int s=step; s>0;--s)
296 if ((n[i]+s) < N[i]) {
297 upper[i] = n[i]+s;
298 break;
299 }
[e138de]300 //Log() << Verbose(0) << "axis " << i << " has bounds [" << lower[i] << "," << upper[i] << "]" << endl;
[0f4538]301 }
302};
303
[734816]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 */
[893bea]308LinkedCell::LinkedNodes* LinkedCell::GetallNeighbours(const double distance) const
[734816]309{
[893bea]310 int Nlower[NDIM], Nupper[NDIM];
[734816]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
[893bea]315 const int step = (distance == 0) ? 1 : (int)floor(distance/RADIUS + 1.);
316 GetNeighbourBounds(Nlower, Nupper, step);
317
[734816]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
[ffe885]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 */
340double LinkedCell::SetClosestIndexToOutsideVector(const Vector * const x) const
341{
342 for (int i=0;i<NDIM;i++) {
[8cbb97]343 n[i] = (int)floor((x->at(i) - min[i])/RADIUS);
[ffe885]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++)
[8cbb97]359 center[i] = min[i]+((double)n[i]+.5)*RADIUS;
[ffe885]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++)
[8cbb97]367 corner[i] = min[i]+RADIUS*((double)n[i]+c[i]);
[ffe885]368 // set up distance vector
[8cbb97]369 Distance = (*x) - corner;
[ffe885]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
[8cbb97]375 tester = center -corner;
376 if (tester.ScalarProduct(Distance) < 0)
[ffe885]377 outside = false;
378 }
379 return (outside ? distanceSquared : 0.);
380};
[734816]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 */
387LinkedCell::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;
[893bea]392 LinkedNodes *NeighbourList = NULL;
[734816]393
[893bea]394 // set index of LC to center of sphere
[ffe885]395 const double dist = SetClosestIndexToOutsideVector(center);
[061b06]396 if (dist > 2.*radius) {
[ffe885]397 DoeLog(1) && (eLog()<< Verbose(1) << "Vector " << *center << " is too far away from any atom in LinkedCell's bounding box." << endl);
[734816]398 return TesselList;
[061b06]399 } else
[a67d19]400 DoLog(1) && (Log() << Verbose(1) << "Distance of closest cell to center of sphere with radius " << radius << " is " << dist << "." << endl);
[893bea]401
402 // gather all neighbours first, then look who fulfills distance criteria
[061b06]403 NeighbourList = GetallNeighbours(2.*radius-dist);
404 //Log() << Verbose(1) << "I found " << NeighbourList->size() << " neighbours to check." << endl;
[893bea]405 if (NeighbourList != NULL) {
406 for (LinkedNodes::const_iterator Runner = NeighbourList->begin(); Runner != NeighbourList->end(); Runner++) {
407 Walker = *Runner;
[061b06]408 //Log() << Verbose(1) << "Current neighbour is at " << *Walker->node << "." << endl;
[8cbb97]409 if ((center->DistanceSquared(*Walker->node) - radiusSquared) < MYEPSILON) {
[893bea]410 TesselList->push_back(Walker);
[734816]411 }
[893bea]412 }
413 delete(NeighbourList);
414 } else
415 DoeLog(2) && (eLog()<< Verbose(2) << "Around vector " << *center << " there are no atoms." << endl);
[734816]416 return TesselList;
417};
Note: See TracBrowser for help on using the repository browser.