source: src/linkedcell.cpp@ 73916f

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 73916f was af2c424, checked in by Frederik Heber <heber@…>, 14 years ago

LinkedCell constructor rewritten.

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