source: src/linkedcell.cpp@ b8b75d

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 b8b75d was 07051c, checked in by Frederik Heber <heber@…>, 16 years ago

Extended class LinkedCell to handle a relative offset to current cell.

  • Property mode set to 100644
File size: 8.7 KB
Line 
1/** \file linkedcell.cpp
2 *
3 * Function implementations for the class LinkedCell.
4 *
5 */
6
7
8#include "atom.hpp"
9#include "helpers.hpp"
10#include "linkedcell.hpp"
11#include "molecule.hpp"
12#include "tesselation.hpp"
13#include "vector.hpp"
14
15// ========================================================= class LinkedCell ===========================================
16
17
18/** Constructor for class LinkedCell.
19 */
20LinkedCell::LinkedCell()
21{
22 LC = NULL;
23 for(int i=0;i<NDIM;i++)
24 N[i] = 0;
25 index = -1;
26 RADIUS = 0.;
27 max.Zero();
28 min.Zero();
29};
30
31/** Puts all atoms in \a *mol into a linked cell list with cell's lengths of \a RADIUS
32 * \param *set LCNodeSet class with all LCNode's
33 * \param RADIUS edge length of cells
34 */
35LinkedCell::LinkedCell(PointCloud *set, double radius)
36{
37 TesselPoint *Walker = NULL;
38
39 RADIUS = radius;
40 LC = NULL;
41 for(int i=0;i<NDIM;i++)
42 N[i] = 0;
43 index = -1;
44 max.Zero();
45 min.Zero();
46 cout << Verbose(1) << "Begin of LinkedCell" << endl;
47 if (set->IsEmpty()) {
48 cerr << "ERROR: set contains no linked cell nodes!" << endl;
49 return;
50 }
51 // 1. find max and min per axis of atoms
52 set->GoToFirst();
53 Walker = set->GetPoint();
54 for (int i=0;i<NDIM;i++) {
55 max.x[i] = Walker->node->x[i];
56 min.x[i] = Walker->node->x[i];
57 }
58 set->GoToFirst();
59 while (!set->IsEnd()) {
60 Walker = set->GetPoint();
61 for (int i=0;i<NDIM;i++) {
62 if (max.x[i] < Walker->node->x[i])
63 max.x[i] = Walker->node->x[i];
64 if (min.x[i] > Walker->node->x[i])
65 min.x[i] = Walker->node->x[i];
66 }
67 set->GoToNext();
68 }
69 cout << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl;
70
71 // 2. find then number of cells per axis
72 for (int i=0;i<NDIM;i++) {
73 N[i] = (int)floor((max.x[i] - min.x[i])/RADIUS)+1;
74 }
75 cout << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl;
76
77 // 3. allocate the lists
78 cout << Verbose(2) << "Allocating cells ... ";
79 if (LC != NULL) {
80 cout << Verbose(1) << "ERROR: Linked Cell list is already allocated, I do nothing." << endl;
81 return;
82 }
83 LC = new LinkedNodes[N[0]*N[1]*N[2]];
84 for (index=0;index<N[0]*N[1]*N[2];index++) {
85 LC [index].clear();
86 }
87 cout << "done." << endl;
88
89 // 4. put each atom into its respective cell
90 cout << Verbose(2) << "Filling cells ... ";
91 set->GoToFirst();
92 while (!set->IsEnd()) {
93 Walker = set->GetPoint();
94 for (int i=0;i<NDIM;i++) {
95 n[i] = (int)floor((Walker->node->x[i] - min.x[i])/RADIUS);
96 }
97 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
98 LC[index].push_back(Walker);
99 //cout << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
100 set->GoToNext();
101 }
102 cout << "done." << endl;
103 cout << Verbose(1) << "End of LinkedCell" << endl;
104};
105
106
107/** Puts all atoms in \a *mol into a linked cell list with cell's lengths of \a RADIUS
108 * \param *set LCNodeSet class with all LCNode's
109 * \param RADIUS edge length of cells
110 */
111LinkedCell::LinkedCell(LinkedNodes *set, double radius)
112{
113 class TesselPoint *Walker = NULL;
114 RADIUS = radius;
115 LC = NULL;
116 for(int i=0;i<NDIM;i++)
117 N[i] = 0;
118 index = -1;
119 max.Zero();
120 min.Zero();
121 cout << Verbose(1) << "Begin of LinkedCell" << endl;
122 if (set->empty()) {
123 cerr << "ERROR: set contains no linked cell nodes!" << endl;
124 return;
125 }
126 // 1. find max and min per axis of atoms
127 LinkedNodes::iterator Runner = set->begin();
128 for (int i=0;i<NDIM;i++) {
129 max.x[i] = (*Runner)->node->x[i];
130 min.x[i] = (*Runner)->node->x[i];
131 }
132 for (LinkedNodes::iterator Runner = set->begin(); Runner != set->end(); Runner++) {
133 Walker = *Runner;
134 for (int i=0;i<NDIM;i++) {
135 if (max.x[i] < Walker->node->x[i])
136 max.x[i] = Walker->node->x[i];
137 if (min.x[i] > Walker->node->x[i])
138 min.x[i] = Walker->node->x[i];
139 }
140 }
141 cout << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl;
142
143 // 2. find then number of cells per axis
144 for (int i=0;i<NDIM;i++) {
145 N[i] = (int)floor((max.x[i] - min.x[i])/RADIUS)+1;
146 }
147 cout << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl;
148
149 // 3. allocate the lists
150 cout << Verbose(2) << "Allocating cells ... ";
151 if (LC != NULL) {
152 cout << Verbose(1) << "ERROR: Linked Cell list is already allocated, I do nothing." << endl;
153 return;
154 }
155 LC = new LinkedNodes[N[0]*N[1]*N[2]];
156 for (index=0;index<N[0]*N[1]*N[2];index++) {
157 LC [index].clear();
158 }
159 cout << "done." << endl;
160
161 // 4. put each atom into its respective cell
162 cout << Verbose(2) << "Filling cells ... ";
163 for (LinkedNodes::iterator Runner = set->begin(); Runner != set->end(); Runner++) {
164 Walker = *Runner;
165 for (int i=0;i<NDIM;i++) {
166 n[i] = (int)floor((Walker->node->x[i] - min.x[i])/RADIUS);
167 }
168 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
169 LC[index].push_back(Walker);
170 //cout << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
171 }
172 cout << "done." << endl;
173 cout << Verbose(1) << "End of LinkedCell" << endl;
174};
175
176/** Destructor for class LinkedCell.
177 */
178LinkedCell::~LinkedCell()
179{
180 if (LC != NULL)
181 for (index=0;index<N[0]*N[1]*N[2];index++)
182 LC[index].clear();
183 delete[](LC);
184 for(int i=0;i<NDIM;i++)
185 N[i] = 0;
186 index = -1;
187 max.Zero();
188 min.Zero();
189};
190
191/** Checks whether LinkedCell::n[] is each within [0,N[]].
192 * \return if all in intervals - true, else -false
193 */
194bool LinkedCell::CheckBounds()
195{
196 bool status = true;
197 for(int i=0;i<NDIM;i++)
198 status = status && ((n[i] >=0) && (n[i] < N[i]));
199 if (!status)
200 cerr << "ERROR: indices are out of bounds!" << endl;
201 return status;
202};
203
204/** Checks whether LinkedCell::n[] plus relative offset is each within [0,N[]].
205 * \param relative[NDIM] relative offset to current cell
206 * \return if all in intervals - true, else -false
207 */
208bool LinkedCell::CheckBounds(int relative[NDIM])
209{
210 bool status = true;
211 for(int i=0;i<NDIM;i++)
212 status = status && ((n[i]+relative[i] >=0) && (n[i]+relative[i] < N[i]));
213 if (!status)
214 cerr << "ERROR: indices are out of bounds!" << endl;
215 return status;
216};
217
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 */
222LinkedNodes* LinkedCell::GetCurrentCell()
223{
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 }
230};
231
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 */
236LinkedNodes* LinkedCell::GetRelativeToCurrentCell(int relative[NDIM])
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
246/** Calculates the index for a given LCNode *Walker.
247 * \param *Walker LCNode to set index tos
248 * \return if the atom is also found in this cell - true, else - false
249 */
250bool LinkedCell::SetIndexToNode(const TesselPoint *Walker)
251{
252 bool status = false;
253 for (int i=0;i<NDIM;i++) {
254 n[i] = (int)floor((Walker->node->x[i] - min.x[i])/RADIUS);
255 }
256 index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
257 if (CheckBounds()) {
258 for (LinkedNodes::iterator Runner = LC[index].begin(); Runner != LC[index].end(); Runner++)
259 status = status || ((*Runner) == Walker);
260 return status;
261 } else {
262 cerr << Verbose(1) << "ERROR: Node at " << *Walker << " is out of bounds." << endl;
263 return false;
264 }
265};
266
267/** Calculates the interval bounds of the linked cell grid.
268 * \param *lower lower bounds
269 * \param *upper upper bounds
270 */
271void LinkedCell::GetNeighbourBounds(int lower[NDIM], int upper[NDIM])
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;
276 //cout << " [" << Nlower[i] << "," << Nupper[i] << "] ";
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
283 //cout << "axis " << i << " has bounds [" << lower[i] << "," << upper[i] << "]" << endl;
284 }
285};
286
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 */
291bool LinkedCell::SetIndexToVector(const Vector *x)
292{
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;
302};
303
Note: See TracBrowser for help on using the repository browser.