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