source: src/LinkedCell/linkedcell.cpp@ 46ce1c

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 46ce1c was 052c10, checked in by Frederik Heber <heber@…>, 10 years ago

MEMFIX: Tesselation::operator() did not free linkedlist.

  • Property mode set to 100644
File size: 12.3 KB
RevLine 
[bcf653]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[94d5ac6]5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
[bcf653]21 */
22
[edb93c]23/** \file linkedcell.cpp
24 *
[6bd7e0]25 * Function implementations for the class LinkedCell_deprecated.
[edb93c]26 *
27 */
28
[bf3817]29// include config.h
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33
[ad011c]34#include "CodePatterns/MemDebug.hpp"
[edb93c]35
[e1bc68]36#include "linkedcell.hpp"
[53c7fc]37
[6f0841]38#include "Atom/atom.hpp"
[ad011c]39#include "CodePatterns/Verbose.hpp"
[3738f0]40#include "CodePatterns/Range.hpp"
[ad011c]41#include "CodePatterns/Log.hpp"
[53c7fc]42#include "LinearAlgebra/Vector.hpp"
43#include "LinkedCell/IPointCloud.hpp"
[cee0b57]44#include "molecule.hpp"
[d127c8]45#include "Tesselation/tesselation.hpp"
[357fba]46
[6bd7e0]47// ========================================================= class LinkedCell_deprecated ===========================================
[357fba]48
[6bd7e0]49/** Constructor for class LinkedCell_deprecated.
[e1bc68]50 */
[6bd7e0]51LinkedCell_deprecated::LinkedCell_deprecated() :
[97b825]52 LC(NULL),
[ff58f1]53 RADIUS(0.),
54 index(-1)
[e1bc68]55{
[042f82]56 for(int i=0;i<NDIM;i++)
57 N[i] = 0;
58 max.Zero();
59 min.Zero();
[e1bc68]60};
61
62/** Puts all atoms in \a *mol into a linked cell list with cell's lengths of \a RADIUS
[357fba]63 * \param *set LCNodeSet class with all LCNode's
[e1bc68]64 * \param RADIUS edge length of cells
65 */
[6bd7e0]66LinkedCell_deprecated::LinkedCell_deprecated(IPointCloud & set, const double radius) :
[97b825]67 LC(NULL),
[ff58f1]68 RADIUS(radius),
[97b825]69 index(-1)
[e1bc68]70{
[357fba]71 TesselPoint *Walker = NULL;
[e1bc68]72
[042f82]73 for(int i=0;i<NDIM;i++)
74 N[i] = 0;
75 max.Zero();
76 min.Zero();
[47d041]77 LOG(1, "Begin of LinkedCell");
[af2c424]78 if (set.IsEmpty()) {
[47d041]79 ELOG(1, "set is NULL or contains no linked cell nodes!");
[042f82]80 return;
81 }
82 // 1. find max and min per axis of atoms
[af2c424]83 set.GoToFirst();
84 Walker = set.GetPoint();
[042f82]85 for (int i=0;i<NDIM;i++) {
[d74077]86 max[i] = Walker->at(i);
87 min[i] = Walker->at(i);
[042f82]88 }
[af2c424]89 set.GoToFirst();
90 while (!set.IsEnd()) {
91 Walker = set.GetPoint();
[042f82]92 for (int i=0;i<NDIM;i++) {
[d74077]93 if (max[i] < Walker->at(i))
94 max[i] = Walker->at(i);
95 if (min[i] > Walker->at(i))
96 min[i] = Walker->at(i);
[042f82]97 }
[af2c424]98 set.GoToNext();
[042f82]99 }
[47d041]100 LOG(2, "Bounding box is " << min << " and " << max << ".");
[6ac7ee]101
[357fba]102 // 2. find then number of cells per axis
[042f82]103 for (int i=0;i<NDIM;i++) {
[0a4f7f]104 N[i] = static_cast<int>(floor((max[i] - min[i])/RADIUS)+1);
[042f82]105 }
[47d041]106 LOG(2, "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << ".");
[6ac7ee]107
[042f82]108 // 3. allocate the lists
[47d041]109 LOG(2, "INFO: Allocating cells ... ");
[042f82]110 if (LC != NULL) {
[47d041]111 ELOG(1, "Linked Cell list is already allocated, I do nothing.");
[042f82]112 return;
113 }
[c66537]114 ASSERT(N[0]*N[1]*N[2] < MAX_LINKEDCELLNODES, "Number linked of linked cell nodes exceded hard-coded limit, use greater edge length!");
[34c43a]115 LC = new TesselPointSTLList[N[0]*N[1]*N[2]];
[042f82]116 for (index=0;index<N[0]*N[1]*N[2];index++) {
117 LC [index].clear();
118 }
[47d041]119 LOG(0, "done.");
[6ac7ee]120
[042f82]121 // 4. put each atom into its respective cell
[47d041]122 LOG(2, "INFO: Filling cells ... ");
[af2c424]123 set.GoToFirst();
124 while (!set.IsEnd()) {
125 Walker = set.GetPoint();
[042f82]126 for (int i=0;i<NDIM;i++) {
[d74077]127 n[i] = static_cast<int>(floor((Walker->at(i) - min[i])/RADIUS));
[042f82]128 }
129 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
130 LC[index].push_back(Walker);
[47d041]131 //LOG(2, *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << ".");
[af2c424]132 set.GoToNext();
[042f82]133 }
[47d041]134 LOG(0, "done.");
135 LOG(1, "End of LinkedCell");
[e1bc68]136};
137
[8cd903]138
[6bd7e0]139/** Destructor for class LinkedCell_deprecated.
[e1bc68]140 */
[6bd7e0]141LinkedCell_deprecated::~LinkedCell_deprecated()
[e1bc68]142{
[052c10]143 if (LC != NULL) {
144 for (index=0;index<N[0]*N[1]*N[2];index++) {
145 // don't delete pointers are just "borrowed"
146 LC[index].clear();
147 }
148 delete[] LC;
149 }
[042f82]150 for(int i=0;i<NDIM;i++)
151 N[i] = 0;
152 index = -1;
[e1bc68]153};
154
[6bd7e0]155/** Checks whether LinkedCell_deprecated::n[] is each within [0,N[]].
[e1bc68]156 * \return if all in intervals - true, else -false
157 */
[6bd7e0]158bool LinkedCell_deprecated::CheckBounds() const
[e1bc68]159{
[042f82]160 bool status = true;
161 for(int i=0;i<NDIM;i++)
162 status = status && ((n[i] >=0) && (n[i] < N[i]));
[bdc91e]163// if (!status)
[47d041]164// ELOG(1, "indices are out of bounds!");
[042f82]165 return status;
[e1bc68]166};
167
[6bd7e0]168/** Checks whether LinkedCell_deprecated::n[] plus relative offset is each within [0,N[]].
[266237]169 * Note that for this check we don't admonish if out of bounds.
[07051c]170 * \param relative[NDIM] relative offset to current cell
171 * \return if all in intervals - true, else -false
172 */
[6bd7e0]173bool LinkedCell_deprecated::CheckBounds(const int relative[NDIM]) const
[07051c]174{
175 bool status = true;
176 for(int i=0;i<NDIM;i++)
177 status = status && ((n[i]+relative[i] >=0) && (n[i]+relative[i] < N[i]));
178 return status;
179};
180
[e1bc68]181
182/** Returns a pointer to the current cell.
[6bd7e0]183 * \return LinkedAtoms pointer to current cell, NULL if LinkedCell_deprecated::n[] are out of bounds.
[e1bc68]184 */
[6bd7e0]185const TesselPointSTLList* LinkedCell_deprecated::GetCurrentCell() const
[e1bc68]186{
[042f82]187 if (CheckBounds()) {
188 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
189 return (&(LC[index]));
190 } else {
191 return NULL;
192 }
[e1bc68]193};
194
[07051c]195/** Returns a pointer to the current cell.
[6bd7e0]196 * \param relative[NDIM] offset for each axis with respect to the current cell LinkedCell_deprecated::n[NDIM]
197 * \return LinkedAtoms pointer to current cell, NULL if LinkedCell_deprecated::n[]+relative[] are out of bounds.
[07051c]198 */
[6bd7e0]199const TesselPointSTLList* LinkedCell_deprecated::GetRelativeToCurrentCell(const int relative[NDIM]) const
[07051c]200{
201 if (CheckBounds(relative)) {
202 index = (n[0]+relative[0]) * N[1] * N[2] + (n[1]+relative[1]) * N[2] + (n[2]+relative[2]);
203 return (&(LC[index]));
204 } else {
205 return NULL;
206 }
207};
208
[893bea]209/** Set the index to the cell containing a given Vector *x.
210 * \param *x Vector with coordinates
211 * \return Vector is inside bounding box - true, else - false
212 */
[6bd7e0]213bool LinkedCell_deprecated::SetIndexToVector(const Vector & x) const
[893bea]214{
215 for (int i=0;i<NDIM;i++)
[d74077]216 n[i] = (int)floor((x.at(i) - min[i])/RADIUS);
[893bea]217
218 return CheckBounds();
219};
220
[357fba]221/** Calculates the index for a given LCNode *Walker.
222 * \param *Walker LCNode to set index tos
[e1bc68]223 * \return if the atom is also found in this cell - true, else - false
224 */
[6bd7e0]225bool LinkedCell_deprecated::SetIndexToNode(const TesselPoint * const Walker) const
[e1bc68]226{
[042f82]227 bool status = false;
228 for (int i=0;i<NDIM;i++) {
[d74077]229 n[i] = static_cast<int>(floor((Walker->at(i) - min[i])/RADIUS));
[042f82]230 }
231 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
232 if (CheckBounds()) {
[34c43a]233 for (TesselPointSTLList::iterator Runner = LC[index].begin(); Runner != LC[index].end(); Runner++)
[042f82]234 status = status || ((*Runner) == Walker);
235 return status;
236 } else {
[47d041]237 ELOG(1, "Node at " << *Walker << " is out of bounds.");
[042f82]238 return false;
239 }
[e1bc68]240};
241
[0f4538]242/** Calculates the interval bounds of the linked cell grid.
[bdc91e]243 * \param lower lower bounds
244 * \param upper upper bounds
[061b06]245 * \param step how deep to check the neighbouring cells (i.e. number of layers to check)
[0f4538]246 */
[6bd7e0]247void LinkedCell_deprecated::GetNeighbourBounds(int lower[NDIM], int upper[NDIM], int step) const
[0f4538]248{
249 for (int i=0;i<NDIM;i++) {
[bdc91e]250 lower[i] = n[i]-step;
251 if (lower[i] < 0)
252 lower[i] = 0;
253 if (lower[i] >= N[i])
254 lower[i] = N[i]-1;
255 upper[i] = n[i]+step;
256 if (upper[i] >= N[i])
257 upper[i] = N[i]-1;
258 if (upper[i] < 0)
259 upper[i] = 0;
[47d041]260 //LOG(0, "axis " << i << " has bounds [" << lower[i] << "," << upper[i] << "]");
[0f4538]261 }
262};
263
[6bd7e0]264/** Returns a list with all neighbours from the current LinkedCell_deprecated::index.
[734816]265 * \param distance (if no distance, then adjacent cells are taken)
266 * \return list of tesselpoints
267 */
[6bd7e0]268TesselPointSTLList* LinkedCell_deprecated::GetallNeighbours(const double distance) const
[734816]269{
[893bea]270 int Nlower[NDIM], Nupper[NDIM];
[734816]271 TesselPoint *Walker = NULL;
[34c43a]272 TesselPointSTLList *TesselList = new TesselPointSTLList;
[734816]273
274 // then go through the current and all neighbouring cells and check the contained points for possible candidates
[893bea]275 const int step = (distance == 0) ? 1 : (int)floor(distance/RADIUS + 1.);
276 GetNeighbourBounds(Nlower, Nupper, step);
277
[734816]278 for (n[0] = Nlower[0]; n[0] <= Nupper[0]; n[0]++)
279 for (n[1] = Nlower[1]; n[1] <= Nupper[1]; n[1]++)
280 for (n[2] = Nlower[2]; n[2] <= Nupper[2]; n[2]++) {
[34c43a]281 const TesselPointSTLList *List = GetCurrentCell();
[47d041]282 //LOG(1, "Current cell is " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << ".");
[734816]283 if (List != NULL) {
[34c43a]284 for (TesselPointSTLList::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
[734816]285 Walker = *Runner;
286 TesselList->push_back(Walker);
287 }
288 }
289 }
290 return TesselList;
291};
292
[6bd7e0]293/** Set the index to the cell containing a given Vector *x, which is not inside the LinkedCell_deprecated's domain
[ffe885]294 * Note that as we have to check distance from every corner of the closest cell, this function is faw more
[6bd7e0]295 * expensive and if Vector is known to be inside LinkedCell_deprecated's domain, then SetIndexToVector() should be used.
[ffe885]296 * \param *x Vector with coordinates
297 * \return minimum squared distance of cell to given vector (if inside of domain, distance is 0)
298 */
[6bd7e0]299double LinkedCell_deprecated::SetClosestIndexToOutsideVector(const Vector * const x) const
[ffe885]300{
301 for (int i=0;i<NDIM;i++) {
[8cbb97]302 n[i] = (int)floor((x->at(i) - min[i])/RADIUS);
[ffe885]303 if (n[i] < 0)
304 n[i] = 0;
305 if (n[i] >= N[i])
306 n[i] = N[i]-1;
307 }
308
309 // calculate distance of cell to vector
310 double distanceSquared = 0.;
[6bd7e0]311 bool outside = true; // flag whether x is found in- or outside of LinkedCell_deprecated's domain/closest cell
[ffe885]312 Vector corner; // current corner of closest cell
313 Vector tester; // Vector pointing from corner to center of closest cell
314 Vector Distance; // Vector from corner of closest cell to x
315
316 Vector center; // center of the closest cell
317 for (int i=0;i<NDIM;i++)
[8cbb97]318 center[i] = min[i]+((double)n[i]+.5)*RADIUS;
[ffe885]319
320 int c[NDIM];
321 for (c[0]=0;c[0]<=1;c[0]++)
322 for (c[1]=0; c[1]<=1;c[1]++)
323 for (c[2]=0; c[2]<=1;c[2]++) {
324 // set up corner
325 for (int i=0;i<NDIM;i++)
[8cbb97]326 corner[i] = min[i]+RADIUS*((double)n[i]+c[i]);
[ffe885]327 // set up distance vector
[8cbb97]328 Distance = (*x) - corner;
[ffe885]329 const double dist = Distance.NormSquared();
330 // check whether distance is smaller
331 if (dist< distanceSquared)
332 distanceSquared = dist;
333 // check whether distance vector goes inside or outside
[8cbb97]334 tester = center -corner;
335 if (tester.ScalarProduct(Distance) < 0)
[ffe885]336 outside = false;
337 }
338 return (outside ? distanceSquared : 0.);
339};
[734816]340
341/** Returns a list of all TesselPoint with distance less than \a radius to \a *Center.
342 * \param radius radius of sphere
343 * \param *center center of sphere
344 * \return list of all points inside sphere
345 */
[6bd7e0]346TesselPointSTLList* LinkedCell_deprecated::GetPointsInsideSphere(const double radius, const Vector * const center) const
[734816]347{
348 const double radiusSquared = radius*radius;
349 TesselPoint *Walker = NULL;
[34c43a]350 TesselPointSTLList *TesselList = new TesselPointSTLList;
351 TesselPointSTLList *NeighbourList = NULL;
[734816]352
[893bea]353 // set index of LC to center of sphere
[ffe885]354 const double dist = SetClosestIndexToOutsideVector(center);
[061b06]355 if (dist > 2.*radius) {
[47d041]356 ELOG(1, "Vector " << *center << " is too far away from any atom in LinkedCell's bounding box.");
[734816]357 return TesselList;
[061b06]358 } else
[ce7bfd]359 LOG(3, "DEBUG: Distance of closest cell to center of sphere with radius " << radius << " is " << dist << ".");
[893bea]360
361 // gather all neighbours first, then look who fulfills distance criteria
[061b06]362 NeighbourList = GetallNeighbours(2.*radius-dist);
[47d041]363 //LOG(1, "I found " << NeighbourList->size() << " neighbours to check.");
[893bea]364 if (NeighbourList != NULL) {
[34c43a]365 for (TesselPointSTLList::const_iterator Runner = NeighbourList->begin(); Runner != NeighbourList->end(); Runner++) {
[893bea]366 Walker = *Runner;
[47d041]367 //LOG(1, "Current neighbour is at " << *Walker->node << ".");
[d74077]368 if ((Walker->DistanceSquared(*center) - radiusSquared) < MYEPSILON) {
[893bea]369 TesselList->push_back(Walker);
[734816]370 }
[893bea]371 }
[052c10]372 delete NeighbourList;
[893bea]373 } else
[47d041]374 ELOG(2, "Around vector " << *center << " there are no atoms.");
[734816]375 return TesselList;
376};
Note: See TracBrowser for help on using the repository browser.