source: src/linkedcell.cpp@ 701ad6

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 701ad6 was ff58f1, checked in by Frederik Heber <heber@…>, 15 years ago

BUGFIX: fixed initializer list sequence of LinkedCell.

  • 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
[bf3817]7// include config.h
8#ifdef HAVE_CONFIG_H
9#include <config.h>
10#endif
11
[112b09]12#include "Helpers/MemDebug.hpp"
[edb93c]13
[f66195]14#include "atom.hpp"
[952f38]15#include "Helpers/helpers.hpp"
[e1bc68]16#include "linkedcell.hpp"
[952f38]17#include "Helpers/Verbose.hpp"
18#include "Helpers/Log.hpp"
[cee0b57]19#include "molecule.hpp"
[357fba]20#include "tesselation.hpp"
[57f243]21#include "LinearAlgebra/Vector.hpp"
[357fba]22
23// ========================================================= class LinkedCell ===========================================
24
[e1bc68]25
26/** Constructor for class LinkedCell.
27 */
[97b825]28LinkedCell::LinkedCell() :
29 LC(NULL),
[ff58f1]30 RADIUS(0.),
31 index(-1)
[e1bc68]32{
[042f82]33 for(int i=0;i<NDIM;i++)
34 N[i] = 0;
35 max.Zero();
36 min.Zero();
[e1bc68]37};
38
39/** Puts all atoms in \a *mol into a linked cell list with cell's lengths of \a RADIUS
[357fba]40 * \param *set LCNodeSet class with all LCNode's
[e1bc68]41 * \param RADIUS edge length of cells
42 */
[97b825]43LinkedCell::LinkedCell(const PointCloud * const set, const double radius) :
44 LC(NULL),
[ff58f1]45 RADIUS(radius),
[97b825]46 index(-1)
[e1bc68]47{
[357fba]48 TesselPoint *Walker = NULL;
[e1bc68]49
[042f82]50 for(int i=0;i<NDIM;i++)
51 N[i] = 0;
52 max.Zero();
53 min.Zero();
[a67d19]54 DoLog(1) && (Log() << Verbose(1) << "Begin of LinkedCell" << endl);
[caf4ba]55 if ((set == NULL) || (set->IsEmpty())) {
[58ed4a]56 DoeLog(1) && (eLog()<< Verbose(1) << "set is NULL or contains no linked cell nodes!" << endl);
[042f82]57 return;
58 }
59 // 1. find max and min per axis of atoms
[357fba]60 set->GoToFirst();
61 Walker = set->GetPoint();
[042f82]62 for (int i=0;i<NDIM;i++) {
[d74077]63 max[i] = Walker->at(i);
64 min[i] = Walker->at(i);
[042f82]65 }
[357fba]66 set->GoToFirst();
[1999d8]67 while (!set->IsEnd()) {
[357fba]68 Walker = set->GetPoint();
[042f82]69 for (int i=0;i<NDIM;i++) {
[d74077]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);
[042f82]74 }
[357fba]75 set->GoToNext();
[042f82]76 }
[a67d19]77 DoLog(2) && (Log() << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl);
[6ac7ee]78
[357fba]79 // 2. find then number of cells per axis
[042f82]80 for (int i=0;i<NDIM;i++) {
[0a4f7f]81 N[i] = static_cast<int>(floor((max[i] - min[i])/RADIUS)+1);
[042f82]82 }
[a67d19]83 DoLog(2) && (Log() << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl);
[6ac7ee]84
[042f82]85 // 3. allocate the lists
[a67d19]86 DoLog(2) && (Log() << Verbose(2) << "Allocating cells ... ");
[042f82]87 if (LC != NULL) {
[58ed4a]88 DoeLog(1) && (eLog()<< Verbose(1) << "Linked Cell list is already allocated, I do nothing." << endl);
[042f82]89 return;
90 }
[357fba]91 LC = new LinkedNodes[N[0]*N[1]*N[2]];
[042f82]92 for (index=0;index<N[0]*N[1]*N[2];index++) {
93 LC [index].clear();
94 }
[a67d19]95 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
[6ac7ee]96
[042f82]97 // 4. put each atom into its respective cell
[a67d19]98 DoLog(2) && (Log() << Verbose(2) << "Filling cells ... ");
[357fba]99 set->GoToFirst();
[1999d8]100 while (!set->IsEnd()) {
[357fba]101 Walker = set->GetPoint();
[042f82]102 for (int i=0;i<NDIM;i++) {
[d74077]103 n[i] = static_cast<int>(floor((Walker->at(i) - min[i])/RADIUS));
[042f82]104 }
105 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
106 LC[index].push_back(Walker);
[e138de]107 //Log() << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
[357fba]108 set->GoToNext();
[042f82]109 }
[a67d19]110 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
111 DoLog(1) && (Log() << Verbose(1) << "End of LinkedCell" << endl);
[e1bc68]112};
113
[8cd903]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 */
[97b825]119LinkedCell::LinkedCell(LinkedNodes *set, const double radius) :
120 LC(NULL),
[ff58f1]121 RADIUS(radius),
[97b825]122 index(-1)
[8cd903]123{
124 class TesselPoint *Walker = NULL;
125 for(int i=0;i<NDIM;i++)
126 N[i] = 0;
127 max.Zero();
128 min.Zero();
[a67d19]129 DoLog(1) && (Log() << Verbose(1) << "Begin of LinkedCell" << endl);
[8cd903]130 if (set->empty()) {
[58ed4a]131 DoeLog(1) && (eLog()<< Verbose(1) << "set contains no linked cell nodes!" << endl);
[8cd903]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++) {
[d74077]137 max[i] = (*Runner)->at(i);
138 min[i] = (*Runner)->at(i);
[8cd903]139 }
140 for (LinkedNodes::iterator Runner = set->begin(); Runner != set->end(); Runner++) {
141 Walker = *Runner;
142 for (int i=0;i<NDIM;i++) {
[d74077]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);
[8cd903]147 }
148 }
[a67d19]149 DoLog(2) && (Log() << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl);
[8cd903]150
151 // 2. find then number of cells per axis
152 for (int i=0;i<NDIM;i++) {
[0a4f7f]153 N[i] = static_cast<int>(floor((max[i] - min[i])/RADIUS)+1);
[8cd903]154 }
[a67d19]155 DoLog(2) && (Log() << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl);
[8cd903]156
157 // 3. allocate the lists
[a67d19]158 DoLog(2) && (Log() << Verbose(2) << "Allocating cells ... ");
[8cd903]159 if (LC != NULL) {
[58ed4a]160 DoeLog(1) && (eLog()<< Verbose(1) << "Linked Cell list is already allocated, I do nothing." << endl);
[8cd903]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 }
[a67d19]167 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
[8cd903]168
169 // 4. put each atom into its respective cell
[a67d19]170 DoLog(2) && (Log() << Verbose(2) << "Filling cells ... ");
[8cd903]171 for (LinkedNodes::iterator Runner = set->begin(); Runner != set->end(); Runner++) {
172 Walker = *Runner;
173 for (int i=0;i<NDIM;i++) {
[d74077]174 n[i] = static_cast<int>(floor((Walker->at(i) - min[i])/RADIUS));
[8cd903]175 }
176 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
177 LC[index].push_back(Walker);
[e138de]178 //Log() << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
[8cd903]179 }
[a67d19]180 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
181 DoLog(1) && (Log() << Verbose(1) << "End of LinkedCell" << endl);
[8cd903]182};
183
[e1bc68]184/** Destructor for class LinkedCell.
185 */
186LinkedCell::~LinkedCell()
187{
[042f82]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;
[e1bc68]195};
196
197/** Checks whether LinkedCell::n[] is each within [0,N[]].
198 * \return if all in intervals - true, else -false
199 */
[776b64]200bool LinkedCell::CheckBounds() const
[e1bc68]201{
[042f82]202 bool status = true;
203 for(int i=0;i<NDIM;i++)
204 status = status && ((n[i] >=0) && (n[i] < N[i]));
[bdc91e]205// if (!status)
206// DoeLog(1) && (eLog()<< Verbose(1) << "indices are out of bounds!" << endl);
[042f82]207 return status;
[e1bc68]208};
209
[07051c]210/** Checks whether LinkedCell::n[] plus relative offset is each within [0,N[]].
[266237]211 * Note that for this check we don't admonish if out of bounds.
[07051c]212 * \param relative[NDIM] relative offset to current cell
213 * \return if all in intervals - true, else -false
214 */
[776b64]215bool LinkedCell::CheckBounds(const int relative[NDIM]) const
[07051c]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
[e1bc68]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 */
[734816]227const LinkedCell::LinkedNodes* LinkedCell::GetCurrentCell() const
[e1bc68]228{
[042f82]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 }
[e1bc68]235};
236
[07051c]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 */
[734816]241const LinkedCell::LinkedNodes* LinkedCell::GetRelativeToCurrentCell(const int relative[NDIM]) const
[07051c]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
[893bea]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 */
[d74077]255bool LinkedCell::SetIndexToVector(const Vector & x) const
[893bea]256{
257 for (int i=0;i<NDIM;i++)
[d74077]258 n[i] = (int)floor((x.at(i) - min[i])/RADIUS);
[893bea]259
260 return CheckBounds();
261};
262
[357fba]263/** Calculates the index for a given LCNode *Walker.
264 * \param *Walker LCNode to set index tos
[e1bc68]265 * \return if the atom is also found in this cell - true, else - false
266 */
[776b64]267bool LinkedCell::SetIndexToNode(const TesselPoint * const Walker) const
[e1bc68]268{
[042f82]269 bool status = false;
270 for (int i=0;i<NDIM;i++) {
[d74077]271 n[i] = static_cast<int>(floor((Walker->at(i) - min[i])/RADIUS));
[042f82]272 }
273 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
274 if (CheckBounds()) {
[357fba]275 for (LinkedNodes::iterator Runner = LC[index].begin(); Runner != LC[index].end(); Runner++)
[042f82]276 status = status || ((*Runner) == Walker);
277 return status;
278 } else {
[58ed4a]279 DoeLog(1) && (eLog()<< Verbose(1) << "Node at " << *Walker << " is out of bounds." << endl);
[042f82]280 return false;
281 }
[e1bc68]282};
283
[0f4538]284/** Calculates the interval bounds of the linked cell grid.
[bdc91e]285 * \param lower lower bounds
286 * \param upper upper bounds
[061b06]287 * \param step how deep to check the neighbouring cells (i.e. number of layers to check)
[0f4538]288 */
[893bea]289void LinkedCell::GetNeighbourBounds(int lower[NDIM], int upper[NDIM], int step) const
[0f4538]290{
291 for (int i=0;i<NDIM;i++) {
[bdc91e]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;
[e138de]302 //Log() << Verbose(0) << "axis " << i << " has bounds [" << lower[i] << "," << upper[i] << "]" << endl;
[0f4538]303 }
304};
305
[734816]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 */
[893bea]310LinkedCell::LinkedNodes* LinkedCell::GetallNeighbours(const double distance) const
[734816]311{
[893bea]312 int Nlower[NDIM], Nupper[NDIM];
[734816]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
[893bea]317 const int step = (distance == 0) ? 1 : (int)floor(distance/RADIUS + 1.);
318 GetNeighbourBounds(Nlower, Nupper, step);
319
[734816]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
[ffe885]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++) {
[8cbb97]345 n[i] = (int)floor((x->at(i) - min[i])/RADIUS);
[ffe885]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++)
[8cbb97]361 center[i] = min[i]+((double)n[i]+.5)*RADIUS;
[ffe885]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++)
[8cbb97]369 corner[i] = min[i]+RADIUS*((double)n[i]+c[i]);
[ffe885]370 // set up distance vector
[8cbb97]371 Distance = (*x) - corner;
[ffe885]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
[8cbb97]377 tester = center -corner;
378 if (tester.ScalarProduct(Distance) < 0)
[ffe885]379 outside = false;
380 }
381 return (outside ? distanceSquared : 0.);
382};
[734816]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;
[893bea]394 LinkedNodes *NeighbourList = NULL;
[734816]395
[893bea]396 // set index of LC to center of sphere
[ffe885]397 const double dist = SetClosestIndexToOutsideVector(center);
[061b06]398 if (dist > 2.*radius) {
[ffe885]399 DoeLog(1) && (eLog()<< Verbose(1) << "Vector " << *center << " is too far away from any atom in LinkedCell's bounding box." << endl);
[734816]400 return TesselList;
[061b06]401 } else
[a67d19]402 DoLog(1) && (Log() << Verbose(1) << "Distance of closest cell to center of sphere with radius " << radius << " is " << dist << "." << endl);
[893bea]403
404 // gather all neighbours first, then look who fulfills distance criteria
[061b06]405 NeighbourList = GetallNeighbours(2.*radius-dist);
406 //Log() << Verbose(1) << "I found " << NeighbourList->size() << " neighbours to check." << endl;
[893bea]407 if (NeighbourList != NULL) {
408 for (LinkedNodes::const_iterator Runner = NeighbourList->begin(); Runner != NeighbourList->end(); Runner++) {
409 Walker = *Runner;
[061b06]410 //Log() << Verbose(1) << "Current neighbour is at " << *Walker->node << "." << endl;
[d74077]411 if ((Walker->DistanceSquared(*center) - radiusSquared) < MYEPSILON) {
[893bea]412 TesselList->push_back(Walker);
[734816]413 }
[893bea]414 }
415 delete(NeighbourList);
416 } else
417 DoeLog(2) && (eLog()<< Verbose(2) << "Around vector " << *center << " there are no atoms." << endl);
[734816]418 return TesselList;
419};
Note: See TracBrowser for help on using the repository browser.