source: src/linkedcell.cpp@ 2be37b

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 2be37b was bcf653, checked in by Frederik Heber <heber@…>, 15 years ago

Added copyright note to each .cpp file and an extensive one to builder.cpp.

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