source: src/linkedcell.cpp@ c66537

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 c66537 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
Line 
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
8/** \file linkedcell.cpp
9 *
10 * Function implementations for the class LinkedCell.
11 *
12 */
13
14// include config.h
15#ifdef HAVE_CONFIG_H
16#include <config.h>
17#endif
18
19#include "Helpers/MemDebug.hpp"
20
21#include "atom.hpp"
22#include "Helpers/helpers.hpp"
23#include "linkedcell.hpp"
24#include "Helpers/Verbose.hpp"
25#include "Helpers/Log.hpp"
26#include "molecule.hpp"
27#include "tesselation.hpp"
28#include "LinearAlgebra/Vector.hpp"
29
30// ========================================================= class LinkedCell ===========================================
31
32
33/** Constructor for class LinkedCell.
34 */
35LinkedCell::LinkedCell() :
36 LC(NULL),
37 RADIUS(0.),
38 index(-1)
39{
40 for(int i=0;i<NDIM;i++)
41 N[i] = 0;
42 max.Zero();
43 min.Zero();
44};
45
46/** Puts all atoms in \a *mol into a linked cell list with cell's lengths of \a RADIUS
47 * \param *set LCNodeSet class with all LCNode's
48 * \param RADIUS edge length of cells
49 */
50LinkedCell::LinkedCell(const PointCloud * const set, const double radius) :
51 LC(NULL),
52 RADIUS(radius),
53 index(-1)
54{
55 TesselPoint *Walker = NULL;
56
57 for(int i=0;i<NDIM;i++)
58 N[i] = 0;
59 max.Zero();
60 min.Zero();
61 DoLog(1) && (Log() << Verbose(1) << "Begin of LinkedCell" << endl);
62 if ((set == NULL) || (set->IsEmpty())) {
63 DoeLog(1) && (eLog()<< Verbose(1) << "set is NULL or contains no linked cell nodes!" << endl);
64 return;
65 }
66 // 1. find max and min per axis of atoms
67 set->GoToFirst();
68 Walker = set->GetPoint();
69 for (int i=0;i<NDIM;i++) {
70 max[i] = Walker->at(i);
71 min[i] = Walker->at(i);
72 }
73 set->GoToFirst();
74 while (!set->IsEnd()) {
75 Walker = set->GetPoint();
76 for (int i=0;i<NDIM;i++) {
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);
81 }
82 set->GoToNext();
83 }
84 DoLog(2) && (Log() << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl);
85
86 // 2. find then number of cells per axis
87 for (int i=0;i<NDIM;i++) {
88 N[i] = static_cast<int>(floor((max[i] - min[i])/RADIUS)+1);
89 }
90 DoLog(2) && (Log() << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl);
91
92 // 3. allocate the lists
93 DoLog(2) && (Log() << Verbose(2) << "Allocating cells ... ");
94 if (LC != NULL) {
95 DoeLog(1) && (eLog()<< Verbose(1) << "Linked Cell list is already allocated, I do nothing." << endl);
96 return;
97 }
98 ASSERT(N[0]*N[1]*N[2] < MAX_LINKEDCELLNODES, "Number linked of linked cell nodes exceded hard-coded limit, use greater edge length!");
99 LC = new LinkedNodes[N[0]*N[1]*N[2]];
100 for (index=0;index<N[0]*N[1]*N[2];index++) {
101 LC [index].clear();
102 }
103 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
104
105 // 4. put each atom into its respective cell
106 DoLog(2) && (Log() << Verbose(2) << "Filling cells ... ");
107 set->GoToFirst();
108 while (!set->IsEnd()) {
109 Walker = set->GetPoint();
110 for (int i=0;i<NDIM;i++) {
111 n[i] = static_cast<int>(floor((Walker->at(i) - min[i])/RADIUS));
112 }
113 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
114 LC[index].push_back(Walker);
115 //Log() << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
116 set->GoToNext();
117 }
118 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
119 DoLog(1) && (Log() << Verbose(1) << "End of LinkedCell" << endl);
120};
121
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 */
127LinkedCell::LinkedCell(LinkedNodes *set, const double radius) :
128 LC(NULL),
129 RADIUS(radius),
130 index(-1)
131{
132 class TesselPoint *Walker = NULL;
133 for(int i=0;i<NDIM;i++)
134 N[i] = 0;
135 max.Zero();
136 min.Zero();
137 DoLog(1) && (Log() << Verbose(1) << "Begin of LinkedCell" << endl);
138 if (set->empty()) {
139 DoeLog(1) && (eLog()<< Verbose(1) << "set contains no linked cell nodes!" << endl);
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++) {
145 max[i] = (*Runner)->at(i);
146 min[i] = (*Runner)->at(i);
147 }
148 for (LinkedNodes::iterator Runner = set->begin(); Runner != set->end(); Runner++) {
149 Walker = *Runner;
150 for (int i=0;i<NDIM;i++) {
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);
155 }
156 }
157 DoLog(2) && (Log() << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl);
158
159 // 2. find then number of cells per axis
160 for (int i=0;i<NDIM;i++) {
161 N[i] = static_cast<int>(floor((max[i] - min[i])/RADIUS)+1);
162 }
163 DoLog(2) && (Log() << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl);
164
165 // 3. allocate the lists
166 DoLog(2) && (Log() << Verbose(2) << "Allocating cells ... ");
167 if (LC != NULL) {
168 DoeLog(1) && (eLog()<< Verbose(1) << "Linked Cell list is already allocated, I do nothing." << endl);
169 return;
170 }
171 ASSERT(N[0]*N[1]*N[2] < MAX_LINKEDCELLNODES, "Number linked of linked cell nodes exceded hard-coded limit, use greater edge length!");
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 }
176 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
177
178 // 4. put each atom into its respective cell
179 DoLog(2) && (Log() << Verbose(2) << "Filling cells ... ");
180 for (LinkedNodes::iterator Runner = set->begin(); Runner != set->end(); Runner++) {
181 Walker = *Runner;
182 for (int i=0;i<NDIM;i++) {
183 n[i] = static_cast<int>(floor((Walker->at(i) - min[i])/RADIUS));
184 }
185 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
186 LC[index].push_back(Walker);
187 //Log() << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
188 }
189 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
190 DoLog(1) && (Log() << Verbose(1) << "End of LinkedCell" << endl);
191};
192
193/** Destructor for class LinkedCell.
194 */
195LinkedCell::~LinkedCell()
196{
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;
204};
205
206/** Checks whether LinkedCell::n[] is each within [0,N[]].
207 * \return if all in intervals - true, else -false
208 */
209bool LinkedCell::CheckBounds() const
210{
211 bool status = true;
212 for(int i=0;i<NDIM;i++)
213 status = status && ((n[i] >=0) && (n[i] < N[i]));
214// if (!status)
215// DoeLog(1) && (eLog()<< Verbose(1) << "indices are out of bounds!" << endl);
216 return status;
217};
218
219/** Checks whether LinkedCell::n[] plus relative offset is each within [0,N[]].
220 * Note that for this check we don't admonish if out of bounds.
221 * \param relative[NDIM] relative offset to current cell
222 * \return if all in intervals - true, else -false
223 */
224bool LinkedCell::CheckBounds(const int relative[NDIM]) const
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
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 */
236const LinkedCell::LinkedNodes* LinkedCell::GetCurrentCell() const
237{
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 }
244};
245
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 */
250const LinkedCell::LinkedNodes* LinkedCell::GetRelativeToCurrentCell(const int relative[NDIM]) const
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
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 */
264bool LinkedCell::SetIndexToVector(const Vector & x) const
265{
266 for (int i=0;i<NDIM;i++)
267 n[i] = (int)floor((x.at(i) - min[i])/RADIUS);
268
269 return CheckBounds();
270};
271
272/** Calculates the index for a given LCNode *Walker.
273 * \param *Walker LCNode to set index tos
274 * \return if the atom is also found in this cell - true, else - false
275 */
276bool LinkedCell::SetIndexToNode(const TesselPoint * const Walker) const
277{
278 bool status = false;
279 for (int i=0;i<NDIM;i++) {
280 n[i] = static_cast<int>(floor((Walker->at(i) - min[i])/RADIUS));
281 }
282 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
283 if (CheckBounds()) {
284 for (LinkedNodes::iterator Runner = LC[index].begin(); Runner != LC[index].end(); Runner++)
285 status = status || ((*Runner) == Walker);
286 return status;
287 } else {
288 DoeLog(1) && (eLog()<< Verbose(1) << "Node at " << *Walker << " is out of bounds." << endl);
289 return false;
290 }
291};
292
293/** Calculates the interval bounds of the linked cell grid.
294 * \param lower lower bounds
295 * \param upper upper bounds
296 * \param step how deep to check the neighbouring cells (i.e. number of layers to check)
297 */
298void LinkedCell::GetNeighbourBounds(int lower[NDIM], int upper[NDIM], int step) const
299{
300 for (int i=0;i<NDIM;i++) {
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;
311 //Log() << Verbose(0) << "axis " << i << " has bounds [" << lower[i] << "," << upper[i] << "]" << endl;
312 }
313};
314
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 */
319LinkedCell::LinkedNodes* LinkedCell::GetallNeighbours(const double distance) const
320{
321 int Nlower[NDIM], Nupper[NDIM];
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
326 const int step = (distance == 0) ? 1 : (int)floor(distance/RADIUS + 1.);
327 GetNeighbourBounds(Nlower, Nupper, step);
328
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
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++) {
354 n[i] = (int)floor((x->at(i) - min[i])/RADIUS);
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++)
370 center[i] = min[i]+((double)n[i]+.5)*RADIUS;
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++)
378 corner[i] = min[i]+RADIUS*((double)n[i]+c[i]);
379 // set up distance vector
380 Distance = (*x) - corner;
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
386 tester = center -corner;
387 if (tester.ScalarProduct(Distance) < 0)
388 outside = false;
389 }
390 return (outside ? distanceSquared : 0.);
391};
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;
403 LinkedNodes *NeighbourList = NULL;
404
405 // set index of LC to center of sphere
406 const double dist = SetClosestIndexToOutsideVector(center);
407 if (dist > 2.*radius) {
408 DoeLog(1) && (eLog()<< Verbose(1) << "Vector " << *center << " is too far away from any atom in LinkedCell's bounding box." << endl);
409 return TesselList;
410 } else
411 DoLog(1) && (Log() << Verbose(1) << "Distance of closest cell to center of sphere with radius " << radius << " is " << dist << "." << endl);
412
413 // gather all neighbours first, then look who fulfills distance criteria
414 NeighbourList = GetallNeighbours(2.*radius-dist);
415 //Log() << Verbose(1) << "I found " << NeighbourList->size() << " neighbours to check." << endl;
416 if (NeighbourList != NULL) {
417 for (LinkedNodes::const_iterator Runner = NeighbourList->begin(); Runner != NeighbourList->end(); Runner++) {
418 Walker = *Runner;
419 //Log() << Verbose(1) << "Current neighbour is at " << *Walker->node << "." << endl;
420 if ((Walker->DistanceSquared(*center) - radiusSquared) < MYEPSILON) {
421 TesselList->push_back(Walker);
422 }
423 }
424 delete(NeighbourList);
425 } else
426 DoeLog(2) && (eLog()<< Verbose(2) << "Around vector " << *center << " there are no atoms." << endl);
427 return TesselList;
428};
Note: See TracBrowser for help on using the repository browser.