source: src/linkedcell.cpp@ 3ae731

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 Candidate_v1.7.0 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 3ae731 was c66537, checked in by Frederik Heber <heber@…>, 15 years ago

FIX: changed minimal bondddistance threshold to 0.1, as otherwise molecular hydrogen is not scanned.

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