| 1 | #include "linkedcell.hpp"
|
|---|
| 2 | #include "molecules.hpp"
|
|---|
| 3 | #include "tesselation.hpp"
|
|---|
| 4 |
|
|---|
| 5 | // ========================================================= class LinkedCell ===========================================
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 | /** Constructor for class LinkedCell.
|
|---|
| 9 | */
|
|---|
| 10 | LinkedCell::LinkedCell()
|
|---|
| 11 | {
|
|---|
| 12 | LC = NULL;
|
|---|
| 13 | for(int i=0;i<NDIM;i++)
|
|---|
| 14 | N[i] = 0;
|
|---|
| 15 | index = -1;
|
|---|
| 16 | RADIUS = 0.;
|
|---|
| 17 | max.Zero();
|
|---|
| 18 | min.Zero();
|
|---|
| 19 | };
|
|---|
| 20 |
|
|---|
| 21 | /** Puts all atoms in \a *mol into a linked cell list with cell's lengths of \a RADIUS
|
|---|
| 22 | * \param *set LCNodeSet class with all LCNode's
|
|---|
| 23 | * \param RADIUS edge length of cells
|
|---|
| 24 | */
|
|---|
| 25 | LinkedCell::LinkedCell(PointCloud *set, double radius)
|
|---|
| 26 | {
|
|---|
| 27 | TesselPoint *Walker = NULL;
|
|---|
| 28 |
|
|---|
| 29 | RADIUS = radius;
|
|---|
| 30 | LC = NULL;
|
|---|
| 31 | for(int i=0;i<NDIM;i++)
|
|---|
| 32 | N[i] = 0;
|
|---|
| 33 | index = -1;
|
|---|
| 34 | max.Zero();
|
|---|
| 35 | min.Zero();
|
|---|
| 36 | cout << Verbose(1) << "Begin of LinkedCell" << endl;
|
|---|
| 37 | if (set->IsEmpty()) {
|
|---|
| 38 | cerr << "ERROR: set contains no linked cell nodes!" << endl;
|
|---|
| 39 | return;
|
|---|
| 40 | }
|
|---|
| 41 | // 1. find max and min per axis of atoms
|
|---|
| 42 | set->GoToFirst();
|
|---|
| 43 | Walker = set->GetPoint();
|
|---|
| 44 | for (int i=0;i<NDIM;i++) {
|
|---|
| 45 | max.x[i] = Walker->node->x[i];
|
|---|
| 46 | min.x[i] = Walker->node->x[i];
|
|---|
| 47 | }
|
|---|
| 48 | set->GoToFirst();
|
|---|
| 49 | while (!set->IsLast()) {
|
|---|
| 50 | Walker = set->GetPoint();
|
|---|
| 51 | for (int i=0;i<NDIM;i++) {
|
|---|
| 52 | if (max.x[i] < Walker->node->x[i])
|
|---|
| 53 | max.x[i] = Walker->node->x[i];
|
|---|
| 54 | if (min.x[i] > Walker->node->x[i])
|
|---|
| 55 | min.x[i] = Walker->node->x[i];
|
|---|
| 56 | }
|
|---|
| 57 | set->GoToNext();
|
|---|
| 58 | }
|
|---|
| 59 | cout << Verbose(2) << "Bounding box is " << min << " and " << max << "." << endl;
|
|---|
| 60 |
|
|---|
| 61 | // 2. find then number of cells per axis
|
|---|
| 62 | for (int i=0;i<NDIM;i++) {
|
|---|
| 63 | N[i] = (int)floor((max.x[i] - min.x[i])/RADIUS)+1;
|
|---|
| 64 | }
|
|---|
| 65 | cout << Verbose(2) << "Number of cells per axis are " << N[0] << ", " << N[1] << " and " << N[2] << "." << endl;
|
|---|
| 66 |
|
|---|
| 67 | // 3. allocate the lists
|
|---|
| 68 | cout << Verbose(2) << "Allocating cells ... ";
|
|---|
| 69 | if (LC != NULL) {
|
|---|
| 70 | cout << Verbose(1) << "ERROR: Linked Cell list is already allocated, I do nothing." << endl;
|
|---|
| 71 | return;
|
|---|
| 72 | }
|
|---|
| 73 | LC = new LinkedNodes[N[0]*N[1]*N[2]];
|
|---|
| 74 | for (index=0;index<N[0]*N[1]*N[2];index++) {
|
|---|
| 75 | LC [index].clear();
|
|---|
| 76 | }
|
|---|
| 77 | cout << "done." << endl;
|
|---|
| 78 |
|
|---|
| 79 | // 4. put each atom into its respective cell
|
|---|
| 80 | cout << Verbose(2) << "Filling cells ... ";
|
|---|
| 81 | set->GoToFirst();
|
|---|
| 82 | while (!set->IsLast()) {
|
|---|
| 83 | Walker = set->GetPoint();
|
|---|
| 84 | for (int i=0;i<NDIM;i++) {
|
|---|
| 85 | n[i] = (int)floor((Walker->node->x[i] - min.x[i])/RADIUS);
|
|---|
| 86 | }
|
|---|
| 87 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
|
|---|
| 88 | LC[index].push_back(Walker);
|
|---|
| 89 | //cout << Verbose(2) << *Walker << " goes into cell " << n[0] << ", " << n[1] << ", " << n[2] << " with No. " << index << "." << endl;
|
|---|
| 90 | set->GoToNext();
|
|---|
| 91 | }
|
|---|
| 92 | cout << "done." << endl;
|
|---|
| 93 | cout << Verbose(1) << "End of LinkedCell" << endl;
|
|---|
| 94 | };
|
|---|
| 95 |
|
|---|
| 96 | /** Destructor for class LinkedCell.
|
|---|
| 97 | */
|
|---|
| 98 | LinkedCell::~LinkedCell()
|
|---|
| 99 | {
|
|---|
| 100 | if (LC != NULL)
|
|---|
| 101 | for (index=0;index<N[0]*N[1]*N[2];index++)
|
|---|
| 102 | LC[index].clear();
|
|---|
| 103 | delete[](LC);
|
|---|
| 104 | for(int i=0;i<NDIM;i++)
|
|---|
| 105 | N[i] = 0;
|
|---|
| 106 | index = -1;
|
|---|
| 107 | max.Zero();
|
|---|
| 108 | min.Zero();
|
|---|
| 109 | };
|
|---|
| 110 |
|
|---|
| 111 | /** Checks whether LinkedCell::n[] is each within [0,N[]].
|
|---|
| 112 | * \return if all in intervals - true, else -false
|
|---|
| 113 | */
|
|---|
| 114 | bool LinkedCell::CheckBounds()
|
|---|
| 115 | {
|
|---|
| 116 | bool status = true;
|
|---|
| 117 | for(int i=0;i<NDIM;i++)
|
|---|
| 118 | status = status && ((n[i] >=0) && (n[i] < N[i]));
|
|---|
| 119 | if (!status)
|
|---|
| 120 | cerr << "ERROR: indices are out of bounds!" << endl;
|
|---|
| 121 | return status;
|
|---|
| 122 | };
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 | /** Returns a pointer to the current cell.
|
|---|
| 126 | * \return LinkedAtoms pointer to current cell, NULL if LinkedCell::n[] are out of bounds.
|
|---|
| 127 | */
|
|---|
| 128 | LinkedNodes* LinkedCell::GetCurrentCell()
|
|---|
| 129 | {
|
|---|
| 130 | if (CheckBounds()) {
|
|---|
| 131 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
|
|---|
| 132 | return (&(LC[index]));
|
|---|
| 133 | } else {
|
|---|
| 134 | return NULL;
|
|---|
| 135 | }
|
|---|
| 136 | };
|
|---|
| 137 |
|
|---|
| 138 | /** Calculates the index for a given LCNode *Walker.
|
|---|
| 139 | * \param *Walker LCNode to set index tos
|
|---|
| 140 | * \return if the atom is also found in this cell - true, else - false
|
|---|
| 141 | */
|
|---|
| 142 | bool LinkedCell::SetIndexToNode(TesselPoint *Walker)
|
|---|
| 143 | {
|
|---|
| 144 | bool status = false;
|
|---|
| 145 | for (int i=0;i<NDIM;i++) {
|
|---|
| 146 | n[i] = (int)floor((Walker->node->x[i] - min.x[i])/RADIUS);
|
|---|
| 147 | }
|
|---|
| 148 | index = n[0] * N[1] * N[2] + n[1] * N[2] + n[2];
|
|---|
| 149 | if (CheckBounds()) {
|
|---|
| 150 | for (LinkedNodes::iterator Runner = LC[index].begin(); Runner != LC[index].end(); Runner++)
|
|---|
| 151 | status = status || ((*Runner) == Walker);
|
|---|
| 152 | return status;
|
|---|
| 153 | } else {
|
|---|
| 154 | cerr << Verbose(1) << "ERROR: Node at " << *Walker << " is out of bounds." << endl;
|
|---|
| 155 | return false;
|
|---|
| 156 | }
|
|---|
| 157 | };
|
|---|
| 158 |
|
|---|
| 159 | /** Calculates the index for a given Vector *x.
|
|---|
| 160 | * \param *x Vector with coordinates
|
|---|
| 161 | * \return Vector is inside bounding box - true, else - false
|
|---|
| 162 | */
|
|---|
| 163 | bool LinkedCell::SetIndexToVector(Vector *x)
|
|---|
| 164 | {
|
|---|
| 165 | bool status = true;
|
|---|
| 166 | for (int i=0;i<NDIM;i++) {
|
|---|
| 167 | n[i] = (int)floor((x->x[i] - min.x[i])/RADIUS);
|
|---|
| 168 | if (max.x[i] < x->x[i])
|
|---|
| 169 | status = false;
|
|---|
| 170 | if (min.x[i] > x->x[i])
|
|---|
| 171 | status = false;
|
|---|
| 172 | }
|
|---|
| 173 | return status;
|
|---|
| 174 | };
|
|---|
| 175 |
|
|---|