source: src/linkedcell.cpp@ dcea0f

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 dcea0f was 734816, checked in by heber <heber@…>, 15 years ago

LinkedNodes is now declared inside LinkedCell and some new functions.

  • as prepraratory measure we placed LinkedNodes as typedef into LinkedCell class. This caused same namespace changes elsewhere where LinkedNodes is used.
  • new function GetallNeighbours() which performs going through linked cells.
  • new function GetPointsInsideaSphere() which performs going through linked cells and returns all neighbours up to a given distance to a given center.

Note: New functions are not yet used elsewhere. Unit test has to be written beforehand.

Signed-off-by: heber <heber@…>

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