source: src/linkedcell.cpp@ 435065

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 435065 was 3738f0, checked in by Frederik Heber <heber@…>, 14 years ago

Moved molecule::CreateAdjacencyList over to class BondGraph.

to make this possible we had to:

other changes:

TESTFIXES:

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