source: src/linkedcell.cpp@ 65487f

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

Removed Helpers.hpp, deleted Helpers.cpp and libMoleCuilderHelpers.la is history.

  • defs.cpp is now compiled into libmolecuilder.la.
  • ShapeUnitTest alone needs defs.cpp.
  • Most changes are removal of Helpers/helpers.hpp.
  • performCriticalExit() now inline function in Helpers/helpers.hpp.
  • also inclusion possible where performCriticalExit() is needed.
  • Helpers/helpers.hpp does not include defs.hpp anymore and this causes lots of missing Helpers/defs.hpp, CodePatterns/Log.hpp and alikes.
  • removed src/Helpers from configure.ac.
  • Property mode set to 100644
File size: 11.9 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
[ad011c]19#include "CodePatterns/MemDebug.hpp"
[edb93c]20
[f66195]21#include "atom.hpp"
[e1bc68]22#include "linkedcell.hpp"
[ad011c]23#include "CodePatterns/Verbose.hpp"
[3738f0]24#include "CodePatterns/Range.hpp"
[ad011c]25#include "CodePatterns/Log.hpp"
[cee0b57]26#include "molecule.hpp"
[34c43a]27#include "IPointCloud.hpp"
[357fba]28#include "tesselation.hpp"
[57f243]29#include "LinearAlgebra/Vector.hpp"
[357fba]30
31// ========================================================= class LinkedCell ===========================================
32
[e1bc68]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 */
[34c43a]50LinkedCell::LinkedCell(IPointCloud & set, const double radius) :
[97b825]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);
[af2c424]62 if (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
[af2c424]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 }
[af2c424]73 set.GoToFirst();
74 while (!set.IsEnd()) {
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 }
[af2c424]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 }
[c66537]98 ASSERT(N[0]*N[1]*N[2] < MAX_LINKEDCELLNODES, "Number linked of linked cell nodes exceded hard-coded limit, use greater edge length!");
[34c43a]99 LC = new TesselPointSTLList[N[0]*N[1]*N[2]];
[042f82]100 for (index=0;index<N[0]*N[1]*N[2];index++) {
101 LC [index].clear();
102 }
[a67d19]103 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
[6ac7ee]104
[042f82]105 // 4. put each atom into its respective cell
[a67d19]106 DoLog(2) && (Log() << Verbose(2) << "Filling cells ... ");
[af2c424]107 set.GoToFirst();
108 while (!set.IsEnd()) {
109 Walker = set.GetPoint();
[042f82]110 for (int i=0;i<NDIM;i++) {
[d74077]111 n[i] = static_cast<int>(floor((Walker->at(i) - min[i])/RADIUS));
[042f82]112 }
113 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
114 LC[index].push_back(Walker);
[e138de]115 //Log() << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
[af2c424]116 set.GoToNext();
[042f82]117 }
[a67d19]118 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
119 DoLog(1) && (Log() << Verbose(1) << "End of LinkedCell" << endl);
[e1bc68]120};
121
[8cd903]122
[e1bc68]123/** Destructor for class LinkedCell.
124 */
125LinkedCell::~LinkedCell()
126{
[042f82]127 if (LC != NULL)
128 for (index=0;index<N[0]*N[1]*N[2];index++)
129 LC[index].clear();
130 delete[](LC);
131 for(int i=0;i<NDIM;i++)
132 N[i] = 0;
133 index = -1;
[e1bc68]134};
135
136/** Checks whether LinkedCell::n[] is each within [0,N[]].
137 * \return if all in intervals - true, else -false
138 */
[776b64]139bool LinkedCell::CheckBounds() const
[e1bc68]140{
[042f82]141 bool status = true;
142 for(int i=0;i<NDIM;i++)
143 status = status && ((n[i] >=0) && (n[i] < N[i]));
[bdc91e]144// if (!status)
145// DoeLog(1) && (eLog()<< Verbose(1) << "indices are out of bounds!" << endl);
[042f82]146 return status;
[e1bc68]147};
148
[07051c]149/** Checks whether LinkedCell::n[] plus relative offset is each within [0,N[]].
[266237]150 * Note that for this check we don't admonish if out of bounds.
[07051c]151 * \param relative[NDIM] relative offset to current cell
152 * \return if all in intervals - true, else -false
153 */
[776b64]154bool LinkedCell::CheckBounds(const int relative[NDIM]) const
[07051c]155{
156 bool status = true;
157 for(int i=0;i<NDIM;i++)
158 status = status && ((n[i]+relative[i] >=0) && (n[i]+relative[i] < N[i]));
159 return status;
160};
161
[e1bc68]162
163/** Returns a pointer to the current cell.
164 * \return LinkedAtoms pointer to current cell, NULL if LinkedCell::n[] are out of bounds.
165 */
[34c43a]166const TesselPointSTLList* LinkedCell::GetCurrentCell() const
[e1bc68]167{
[042f82]168 if (CheckBounds()) {
169 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
170 return (&(LC[index]));
171 } else {
172 return NULL;
173 }
[e1bc68]174};
175
[07051c]176/** Returns a pointer to the current cell.
177 * \param relative[NDIM] offset for each axis with respect to the current cell LinkedCell::n[NDIM]
178 * \return LinkedAtoms pointer to current cell, NULL if LinkedCell::n[]+relative[] are out of bounds.
179 */
[34c43a]180const TesselPointSTLList* LinkedCell::GetRelativeToCurrentCell(const int relative[NDIM]) const
[07051c]181{
182 if (CheckBounds(relative)) {
183 index = (n[0]+relative[0]) * N[1] * N[2] + (n[1]+relative[1]) * N[2] + (n[2]+relative[2]);
184 return (&(LC[index]));
185 } else {
186 return NULL;
187 }
188};
189
[893bea]190/** Set the index to the cell containing a given Vector *x.
191 * \param *x Vector with coordinates
192 * \return Vector is inside bounding box - true, else - false
193 */
[d74077]194bool LinkedCell::SetIndexToVector(const Vector & x) const
[893bea]195{
196 for (int i=0;i<NDIM;i++)
[d74077]197 n[i] = (int)floor((x.at(i) - min[i])/RADIUS);
[893bea]198
199 return CheckBounds();
200};
201
[357fba]202/** Calculates the index for a given LCNode *Walker.
203 * \param *Walker LCNode to set index tos
[e1bc68]204 * \return if the atom is also found in this cell - true, else - false
205 */
[776b64]206bool LinkedCell::SetIndexToNode(const TesselPoint * const Walker) const
[e1bc68]207{
[042f82]208 bool status = false;
209 for (int i=0;i<NDIM;i++) {
[d74077]210 n[i] = static_cast<int>(floor((Walker->at(i) - min[i])/RADIUS));
[042f82]211 }
212 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
213 if (CheckBounds()) {
[34c43a]214 for (TesselPointSTLList::iterator Runner = LC[index].begin(); Runner != LC[index].end(); Runner++)
[042f82]215 status = status || ((*Runner) == Walker);
216 return status;
217 } else {
[58ed4a]218 DoeLog(1) && (eLog()<< Verbose(1) << "Node at " << *Walker << " is out of bounds." << endl);
[042f82]219 return false;
220 }
[e1bc68]221};
222
[0f4538]223/** Calculates the interval bounds of the linked cell grid.
[bdc91e]224 * \param lower lower bounds
225 * \param upper upper bounds
[061b06]226 * \param step how deep to check the neighbouring cells (i.e. number of layers to check)
[0f4538]227 */
[893bea]228void LinkedCell::GetNeighbourBounds(int lower[NDIM], int upper[NDIM], int step) const
[0f4538]229{
230 for (int i=0;i<NDIM;i++) {
[bdc91e]231 lower[i] = n[i]-step;
232 if (lower[i] < 0)
233 lower[i] = 0;
234 if (lower[i] >= N[i])
235 lower[i] = N[i]-1;
236 upper[i] = n[i]+step;
237 if (upper[i] >= N[i])
238 upper[i] = N[i]-1;
239 if (upper[i] < 0)
240 upper[i] = 0;
[e138de]241 //Log() << Verbose(0) << "axis " << i << " has bounds [" << lower[i] << "," << upper[i] << "]" << endl;
[0f4538]242 }
243};
244
[734816]245/** Returns a list with all neighbours from the current LinkedCell::index.
246 * \param distance (if no distance, then adjacent cells are taken)
247 * \return list of tesselpoints
248 */
[34c43a]249TesselPointSTLList* LinkedCell::GetallNeighbours(const double distance) const
[734816]250{
[893bea]251 int Nlower[NDIM], Nupper[NDIM];
[734816]252 TesselPoint *Walker = NULL;
[34c43a]253 TesselPointSTLList *TesselList = new TesselPointSTLList;
[734816]254
255 // then go through the current and all neighbouring cells and check the contained points for possible candidates
[893bea]256 const int step = (distance == 0) ? 1 : (int)floor(distance/RADIUS + 1.);
257 GetNeighbourBounds(Nlower, Nupper, step);
258
[734816]259 //Log() << Verbose(0) << endl;
260 for (n[0] = Nlower[0]; n[0] <= Nupper[0]; n[0]++)
261 for (n[1] = Nlower[1]; n[1] <= Nupper[1]; n[1]++)
262 for (n[2] = Nlower[2]; n[2] <= Nupper[2]; n[2]++) {
[34c43a]263 const TesselPointSTLList *List = GetCurrentCell();
[734816]264 //Log() << Verbose(1) << "Current cell is " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
265 if (List != NULL) {
[34c43a]266 for (TesselPointSTLList::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
[734816]267 Walker = *Runner;
268 TesselList->push_back(Walker);
269 }
270 }
271 }
272 return TesselList;
273};
274
[ffe885]275/** Set the index to the cell containing a given Vector *x, which is not inside the LinkedCell's domain
276 * Note that as we have to check distance from every corner of the closest cell, this function is faw more
277 * expensive and if Vector is known to be inside LinkedCell's domain, then SetIndexToVector() should be used.
278 * \param *x Vector with coordinates
279 * \return minimum squared distance of cell to given vector (if inside of domain, distance is 0)
280 */
281double LinkedCell::SetClosestIndexToOutsideVector(const Vector * const x) const
282{
283 for (int i=0;i<NDIM;i++) {
[8cbb97]284 n[i] = (int)floor((x->at(i) - min[i])/RADIUS);
[ffe885]285 if (n[i] < 0)
286 n[i] = 0;
287 if (n[i] >= N[i])
288 n[i] = N[i]-1;
289 }
290
291 // calculate distance of cell to vector
292 double distanceSquared = 0.;
293 bool outside = true; // flag whether x is found in- or outside of LinkedCell's domain/closest cell
294 Vector corner; // current corner of closest cell
295 Vector tester; // Vector pointing from corner to center of closest cell
296 Vector Distance; // Vector from corner of closest cell to x
297
298 Vector center; // center of the closest cell
299 for (int i=0;i<NDIM;i++)
[8cbb97]300 center[i] = min[i]+((double)n[i]+.5)*RADIUS;
[ffe885]301
302 int c[NDIM];
303 for (c[0]=0;c[0]<=1;c[0]++)
304 for (c[1]=0; c[1]<=1;c[1]++)
305 for (c[2]=0; c[2]<=1;c[2]++) {
306 // set up corner
307 for (int i=0;i<NDIM;i++)
[8cbb97]308 corner[i] = min[i]+RADIUS*((double)n[i]+c[i]);
[ffe885]309 // set up distance vector
[8cbb97]310 Distance = (*x) - corner;
[ffe885]311 const double dist = Distance.NormSquared();
312 // check whether distance is smaller
313 if (dist< distanceSquared)
314 distanceSquared = dist;
315 // check whether distance vector goes inside or outside
[8cbb97]316 tester = center -corner;
317 if (tester.ScalarProduct(Distance) < 0)
[ffe885]318 outside = false;
319 }
320 return (outside ? distanceSquared : 0.);
321};
[734816]322
323/** Returns a list of all TesselPoint with distance less than \a radius to \a *Center.
324 * \param radius radius of sphere
325 * \param *center center of sphere
326 * \return list of all points inside sphere
327 */
[34c43a]328TesselPointSTLList* LinkedCell::GetPointsInsideSphere(const double radius, const Vector * const center) const
[734816]329{
330 const double radiusSquared = radius*radius;
331 TesselPoint *Walker = NULL;
[34c43a]332 TesselPointSTLList *TesselList = new TesselPointSTLList;
333 TesselPointSTLList *NeighbourList = NULL;
[734816]334
[893bea]335 // set index of LC to center of sphere
[ffe885]336 const double dist = SetClosestIndexToOutsideVector(center);
[061b06]337 if (dist > 2.*radius) {
[ffe885]338 DoeLog(1) && (eLog()<< Verbose(1) << "Vector " << *center << " is too far away from any atom in LinkedCell's bounding box." << endl);
[734816]339 return TesselList;
[061b06]340 } else
[a67d19]341 DoLog(1) && (Log() << Verbose(1) << "Distance of closest cell to center of sphere with radius " << radius << " is " << dist << "." << endl);
[893bea]342
343 // gather all neighbours first, then look who fulfills distance criteria
[061b06]344 NeighbourList = GetallNeighbours(2.*radius-dist);
345 //Log() << Verbose(1) << "I found " << NeighbourList->size() << " neighbours to check." << endl;
[893bea]346 if (NeighbourList != NULL) {
[34c43a]347 for (TesselPointSTLList::const_iterator Runner = NeighbourList->begin(); Runner != NeighbourList->end(); Runner++) {
[893bea]348 Walker = *Runner;
[061b06]349 //Log() << Verbose(1) << "Current neighbour is at " << *Walker->node << "." << endl;
[d74077]350 if ((Walker->DistanceSquared(*center) - radiusSquared) < MYEPSILON) {
[893bea]351 TesselList->push_back(Walker);
[734816]352 }
[893bea]353 }
354 delete(NeighbourList);
355 } else
356 DoeLog(2) && (eLog()<< Verbose(2) << "Around vector " << *center << " there are no atoms." << endl);
[734816]357 return TesselList;
358};
Note: See TracBrowser for help on using the repository browser.