| 1 | /* | 
|---|
| 2 | * analysis.cpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Oct 13, 2009 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include "Helpers/MemDebug.hpp" | 
|---|
| 9 |  | 
|---|
| 10 | #include <iostream> | 
|---|
| 11 | #include <iomanip> | 
|---|
| 12 |  | 
|---|
| 13 | #include "analysis_correlation.hpp" | 
|---|
| 14 | #include "element.hpp" | 
|---|
| 15 | #include "info.hpp" | 
|---|
| 16 | #include "log.hpp" | 
|---|
| 17 | #include "molecule.hpp" | 
|---|
| 18 | #include "tesselation.hpp" | 
|---|
| 19 | #include "tesselationhelpers.hpp" | 
|---|
| 20 | #include "triangleintersectionlist.hpp" | 
|---|
| 21 | #include "vector.hpp" | 
|---|
| 22 | #include "Matrix.hpp" | 
|---|
| 23 | #include "verbose.hpp" | 
|---|
| 24 | #include "World.hpp" | 
|---|
| 25 | #include "Box.hpp" | 
|---|
| 26 |  | 
|---|
| 27 |  | 
|---|
| 28 | /** Calculates the pair correlation between given elements. | 
|---|
| 29 | * Note given element order is unimportant (i.e. g(Si, O) === g(O, Si)) | 
|---|
| 30 | * \param *molecules vector of molecules | 
|---|
| 31 | * \param &elements vector of elements to correlate | 
|---|
| 32 | * \return Map of doubles with values the pair of the two atoms. | 
|---|
| 33 | */ | 
|---|
| 34 | PairCorrelationMap *PairCorrelation(std::vector<molecule *> &molecules, const std::vector<element *> &elements) | 
|---|
| 35 | { | 
|---|
| 36 | Info FunctionInfo(__func__); | 
|---|
| 37 | PairCorrelationMap *outmap = NULL; | 
|---|
| 38 | double distance = 0.; | 
|---|
| 39 | Box &domain = World::getInstance().getDomain(); | 
|---|
| 40 |  | 
|---|
| 41 | if (molecules.empty()) { | 
|---|
| 42 | DoeLog(1) && (eLog()<< Verbose(1) <<"No molecule given." << endl); | 
|---|
| 43 | return outmap; | 
|---|
| 44 | } | 
|---|
| 45 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin(); MolWalker != molecules.end(); MolWalker++) | 
|---|
| 46 | (*MolWalker)->doCountAtoms(); | 
|---|
| 47 |  | 
|---|
| 48 | // create all possible pairs of elements | 
|---|
| 49 | set <pair<element *, element *> > PairsOfElements; | 
|---|
| 50 | if (elements.size() >= 2) { | 
|---|
| 51 | for (vector<element *>::const_iterator type1 = elements.begin(); type1 != elements.end(); ++type1) | 
|---|
| 52 | for (vector<element *>::const_iterator type2 = elements.begin(); type2 != elements.end(); ++type2) | 
|---|
| 53 | if (type1 != type2) { | 
|---|
| 54 | PairsOfElements.insert( pair<element *, element*>(*type1,*type2) ); | 
|---|
| 55 | DoLog(1) && (Log() << Verbose(1) << "Creating element pair " << (*type1)->symbol << " and " << (*type2)->symbol << "." << endl); | 
|---|
| 56 | } | 
|---|
| 57 | } else if (elements.size() == 1) { // one to all are valid | 
|---|
| 58 | element *elemental = *elements.begin(); | 
|---|
| 59 | PairsOfElements.insert( pair<element *, element*>(elemental,(element *)NULL) ); | 
|---|
| 60 | PairsOfElements.insert( pair<element *, element*>((element *)NULL,elemental) ); | 
|---|
| 61 | } else { // all elements valid | 
|---|
| 62 | PairsOfElements.insert( pair<element *, element*>((element *)NULL, (element *)NULL) ); | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | outmap = new PairCorrelationMap; | 
|---|
| 66 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin(); MolWalker != molecules.end(); MolWalker++){ | 
|---|
| 67 | DoLog(2) && (Log()<< Verbose(2) << "Current molecule is " << *MolWalker << "." << endl); | 
|---|
| 68 | for (molecule::const_iterator iter = (*MolWalker)->begin(); iter != (*MolWalker)->end(); ++iter) { | 
|---|
| 69 | DoLog(3) && (Log() << Verbose(3) << "Current atom is " << **iter << "." << endl); | 
|---|
| 70 | for (std::vector<molecule *>::const_iterator MolOtherWalker = MolWalker; MolOtherWalker != molecules.end(); MolOtherWalker++){ | 
|---|
| 71 | DoLog(2) && (Log() << Verbose(2) << "Current other molecule is " << *MolOtherWalker << "." << endl); | 
|---|
| 72 | for (molecule::const_iterator runner = (*MolOtherWalker)->begin(); runner != (*MolOtherWalker)->end(); ++runner) { | 
|---|
| 73 | DoLog(3) && (Log() << Verbose(3) << "Current otheratom is " << **runner << "." << endl); | 
|---|
| 74 | if ((*iter)->getId() < (*runner)->getId()){ | 
|---|
| 75 | for (set <pair<element *, element *> >::iterator PairRunner = PairsOfElements.begin(); PairRunner != PairsOfElements.end(); ++PairRunner) | 
|---|
| 76 | if ((PairRunner->first == (**iter).type) && (PairRunner->second == (**runner).type)) { | 
|---|
| 77 | distance = domain.periodicDistance(*(*iter)->node,*(*runner)->node); | 
|---|
| 78 | //Log() << Verbose(1) <<"Inserting " << *(*iter) << " and " << *(*runner) << endl; | 
|---|
| 79 | outmap->insert ( pair<double, pair <atom *, atom*> > (distance, pair<atom *, atom*> ((*iter), (*runner)) ) ); | 
|---|
| 80 | } | 
|---|
| 81 | } | 
|---|
| 82 | } | 
|---|
| 83 | } | 
|---|
| 84 | } | 
|---|
| 85 | } | 
|---|
| 86 | return outmap; | 
|---|
| 87 | }; | 
|---|
| 88 |  | 
|---|
| 89 | /** Calculates the pair correlation between given elements. | 
|---|
| 90 | * Note given element order is unimportant (i.e. g(Si, O) === g(O, Si)) | 
|---|
| 91 | * \param *molecules list of molecules structure | 
|---|
| 92 | * \param &elements vector of elements to correlate | 
|---|
| 93 | * \param ranges[NDIM] interval boundaries for the periodic images to scan also | 
|---|
| 94 | * \return Map of doubles with values the pair of the two atoms. | 
|---|
| 95 | */ | 
|---|
| 96 | PairCorrelationMap *PeriodicPairCorrelation(std::vector<molecule *> &molecules, const std::vector<element *> &elements, const int ranges[NDIM] ) | 
|---|
| 97 | { | 
|---|
| 98 | Info FunctionInfo(__func__); | 
|---|
| 99 | PairCorrelationMap *outmap = NULL; | 
|---|
| 100 | double distance = 0.; | 
|---|
| 101 | int n[NDIM]; | 
|---|
| 102 | Vector checkX; | 
|---|
| 103 | Vector periodicX; | 
|---|
| 104 | int Othern[NDIM]; | 
|---|
| 105 | Vector checkOtherX; | 
|---|
| 106 | Vector periodicOtherX; | 
|---|
| 107 |  | 
|---|
| 108 | if (molecules.empty()) { | 
|---|
| 109 | DoeLog(1) && (eLog()<< Verbose(1) <<"No molecule given." << endl); | 
|---|
| 110 | return outmap; | 
|---|
| 111 | } | 
|---|
| 112 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin(); MolWalker != molecules.end(); MolWalker++) | 
|---|
| 113 | (*MolWalker)->doCountAtoms(); | 
|---|
| 114 |  | 
|---|
| 115 | // create all possible pairs of elements | 
|---|
| 116 | set <pair<element *, element *> > PairsOfElements; | 
|---|
| 117 | if (elements.size() >= 2) { | 
|---|
| 118 | for (vector<element *>::const_iterator type1 = elements.begin(); type1 != elements.end(); ++type1) | 
|---|
| 119 | for (vector<element *>::const_iterator type2 = elements.begin(); type2 != elements.end(); ++type2) | 
|---|
| 120 | if (type1 != type2) { | 
|---|
| 121 | PairsOfElements.insert( pair<element *, element*>(*type1,*type2) ); | 
|---|
| 122 | DoLog(1) && (Log() << Verbose(1) << "Creating element pair " << (*type1)->symbol << " and " << (*type2)->symbol << "." << endl); | 
|---|
| 123 | } | 
|---|
| 124 | } else if (elements.size() == 1) { // one to all are valid | 
|---|
| 125 | element *elemental = *elements.begin(); | 
|---|
| 126 | PairsOfElements.insert( pair<element *, element*>(elemental,(element *)NULL) ); | 
|---|
| 127 | PairsOfElements.insert( pair<element *, element*>((element *)NULL,elemental) ); | 
|---|
| 128 | } else { // all elements valid | 
|---|
| 129 | PairsOfElements.insert( pair<element *, element*>((element *)NULL, (element *)NULL) ); | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | outmap = new PairCorrelationMap; | 
|---|
| 133 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin(); MolWalker != molecules.end(); MolWalker++){ | 
|---|
| 134 | Matrix FullMatrix = World::getInstance().getDomain().getM(); | 
|---|
| 135 | Matrix FullInverseMatrix = World::getInstance().getDomain().getMinv(); | 
|---|
| 136 | DoLog(2) && (Log()<< Verbose(2) << "Current molecule is " << *MolWalker << "." << endl); | 
|---|
| 137 | for (molecule::const_iterator iter = (*MolWalker)->begin(); iter != (*MolWalker)->end(); ++iter) { | 
|---|
| 138 | DoLog(3) && (Log() << Verbose(3) << "Current atom is " << **iter << "." << endl); | 
|---|
| 139 | periodicX = FullInverseMatrix * (*(*iter)->node); // x now in [0,1)^3 | 
|---|
| 140 | // go through every range in xyz and get distance | 
|---|
| 141 | for (n[0]=-ranges[0]; n[0] <= ranges[0]; n[0]++) | 
|---|
| 142 | for (n[1]=-ranges[1]; n[1] <= ranges[1]; n[1]++) | 
|---|
| 143 | for (n[2]=-ranges[2]; n[2] <= ranges[2]; n[2]++) { | 
|---|
| 144 | checkX = FullMatrix * (Vector(n[0], n[1], n[2]) + periodicX); | 
|---|
| 145 | for (std::vector<molecule *>::const_iterator MolOtherWalker = MolWalker; MolOtherWalker != molecules.end(); MolOtherWalker++){ | 
|---|
| 146 | DoLog(2) && (Log() << Verbose(2) << "Current other molecule is " << *MolOtherWalker << "." << endl); | 
|---|
| 147 | for (molecule::const_iterator runner = (*MolOtherWalker)->begin(); runner != (*MolOtherWalker)->end(); ++runner) { | 
|---|
| 148 | DoLog(3) && (Log() << Verbose(3) << "Current otheratom is " << **runner << "." << endl); | 
|---|
| 149 | if ((*iter)->getId() < (*runner)->getId()){ | 
|---|
| 150 | for (set <pair<element *, element *> >::iterator PairRunner = PairsOfElements.begin(); PairRunner != PairsOfElements.end(); ++PairRunner) | 
|---|
| 151 | if ((PairRunner->first == (**iter).type) && (PairRunner->second == (**runner).type)) { | 
|---|
| 152 | periodicOtherX = FullInverseMatrix * (*(*runner)->node); // x now in [0,1)^3 | 
|---|
| 153 | // go through every range in xyz and get distance | 
|---|
| 154 | for (Othern[0]=-ranges[0]; Othern[0] <= ranges[0]; Othern[0]++) | 
|---|
| 155 | for (Othern[1]=-ranges[1]; Othern[1] <= ranges[1]; Othern[1]++) | 
|---|
| 156 | for (Othern[2]=-ranges[2]; Othern[2] <= ranges[2]; Othern[2]++) { | 
|---|
| 157 | checkOtherX = FullMatrix * (Vector(Othern[0], Othern[1], Othern[2]) + periodicOtherX); | 
|---|
| 158 | distance = checkX.distance(checkOtherX); | 
|---|
| 159 | //Log() << Verbose(1) <<"Inserting " << *(*iter) << " and " << *(*runner) << endl; | 
|---|
| 160 | outmap->insert ( pair<double, pair <atom *, atom*> > (distance, pair<atom *, atom*> ((*iter), (*runner)) ) ); | 
|---|
| 161 | } | 
|---|
| 162 | } | 
|---|
| 163 | } | 
|---|
| 164 | } | 
|---|
| 165 | } | 
|---|
| 166 | } | 
|---|
| 167 | } | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | return outmap; | 
|---|
| 171 | }; | 
|---|
| 172 |  | 
|---|
| 173 | /** Calculates the distance (pair) correlation between a given element and a point. | 
|---|
| 174 | * \param *molecules list of molecules structure | 
|---|
| 175 | * \param &elements vector of elements to correlate with point | 
|---|
| 176 | * \param *point vector to the correlation point | 
|---|
| 177 | * \return Map of dobules with values as pairs of atom and the vector | 
|---|
| 178 | */ | 
|---|
| 179 | CorrelationToPointMap *CorrelationToPoint(std::vector<molecule *> &molecules, const std::vector<element *> &elements, const Vector *point ) | 
|---|
| 180 | { | 
|---|
| 181 | Info FunctionInfo(__func__); | 
|---|
| 182 | CorrelationToPointMap *outmap = NULL; | 
|---|
| 183 | double distance = 0.; | 
|---|
| 184 | Box &domain = World::getInstance().getDomain(); | 
|---|
| 185 |  | 
|---|
| 186 | if (molecules.empty()) { | 
|---|
| 187 | DoLog(1) && (Log() << Verbose(1) <<"No molecule given." << endl); | 
|---|
| 188 | return outmap; | 
|---|
| 189 | } | 
|---|
| 190 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin(); MolWalker != molecules.end(); MolWalker++) | 
|---|
| 191 | (*MolWalker)->doCountAtoms(); | 
|---|
| 192 | outmap = new CorrelationToPointMap; | 
|---|
| 193 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin(); MolWalker != molecules.end(); MolWalker++) { | 
|---|
| 194 | DoLog(2) && (Log() << Verbose(2) << "Current molecule is " << *MolWalker << "." << endl); | 
|---|
| 195 | for (molecule::const_iterator iter = (*MolWalker)->begin(); iter != (*MolWalker)->end(); ++iter) { | 
|---|
| 196 | DoLog(3) && (Log() << Verbose(3) << "Current atom is " << **iter << "." << endl); | 
|---|
| 197 | for (vector<element *>::const_iterator type = elements.begin(); type != elements.end(); ++type) | 
|---|
| 198 | if ((*type == NULL) || ((*iter)->type == *type)) { | 
|---|
| 199 | distance = domain.periodicDistance(*(*iter)->node,*point); | 
|---|
| 200 | DoLog(4) && (Log() << Verbose(4) << "Current distance is " << distance << "." << endl); | 
|---|
| 201 | outmap->insert ( pair<double, pair<atom *, const Vector*> >(distance, pair<atom *, const Vector*> ((*iter), point) ) ); | 
|---|
| 202 | } | 
|---|
| 203 | } | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | return outmap; | 
|---|
| 207 | }; | 
|---|
| 208 |  | 
|---|
| 209 | /** Calculates the distance (pair) correlation between a given element, all its periodic images and a point. | 
|---|
| 210 | * \param *molecules list of molecules structure | 
|---|
| 211 | * \param &elements vector of elements to correlate to point | 
|---|
| 212 | * \param *point vector to the correlation point | 
|---|
| 213 | * \param ranges[NDIM] interval boundaries for the periodic images to scan also | 
|---|
| 214 | * \return Map of dobules with values as pairs of atom and the vector | 
|---|
| 215 | */ | 
|---|
| 216 | CorrelationToPointMap *PeriodicCorrelationToPoint(std::vector<molecule *> &molecules, const std::vector<element *> &elements, const Vector *point, const int ranges[NDIM] ) | 
|---|
| 217 | { | 
|---|
| 218 | Info FunctionInfo(__func__); | 
|---|
| 219 | CorrelationToPointMap *outmap = NULL; | 
|---|
| 220 | double distance = 0.; | 
|---|
| 221 | int n[NDIM]; | 
|---|
| 222 | Vector periodicX; | 
|---|
| 223 | Vector checkX; | 
|---|
| 224 |  | 
|---|
| 225 | if (molecules.empty()) { | 
|---|
| 226 | DoLog(1) && (Log() << Verbose(1) <<"No molecule given." << endl); | 
|---|
| 227 | return outmap; | 
|---|
| 228 | } | 
|---|
| 229 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin(); MolWalker != molecules.end(); MolWalker++) | 
|---|
| 230 | (*MolWalker)->doCountAtoms(); | 
|---|
| 231 | outmap = new CorrelationToPointMap; | 
|---|
| 232 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin(); MolWalker != molecules.end(); MolWalker++) { | 
|---|
| 233 | Matrix FullMatrix = World::getInstance().getDomain().getM(); | 
|---|
| 234 | Matrix FullInverseMatrix = World::getInstance().getDomain().getMinv(); | 
|---|
| 235 | DoLog(2) && (Log() << Verbose(2) << "Current molecule is " << *MolWalker << "." << endl); | 
|---|
| 236 | for (molecule::const_iterator iter = (*MolWalker)->begin(); iter != (*MolWalker)->end(); ++iter) { | 
|---|
| 237 | DoLog(3) && (Log() << Verbose(3) << "Current atom is " << **iter << "." << endl); | 
|---|
| 238 | for (vector<element *>::const_iterator type = elements.begin(); type != elements.end(); ++type) | 
|---|
| 239 | if ((*type == NULL) || ((*iter)->type == *type)) { | 
|---|
| 240 | periodicX = FullInverseMatrix * (*(*iter)->node); // x now in [0,1)^3 | 
|---|
| 241 | // go through every range in xyz and get distance | 
|---|
| 242 | for (n[0]=-ranges[0]; n[0] <= ranges[0]; n[0]++) | 
|---|
| 243 | for (n[1]=-ranges[1]; n[1] <= ranges[1]; n[1]++) | 
|---|
| 244 | for (n[2]=-ranges[2]; n[2] <= ranges[2]; n[2]++) { | 
|---|
| 245 | checkX = FullMatrix * (Vector(n[0], n[1], n[2]) + periodicX); | 
|---|
| 246 | distance = checkX.distance(*point); | 
|---|
| 247 | DoLog(4) && (Log() << Verbose(4) << "Current distance is " << distance << "." << endl); | 
|---|
| 248 | outmap->insert ( pair<double, pair<atom *, const Vector*> >(distance, pair<atom *, const Vector*> (*iter, point) ) ); | 
|---|
| 249 | } | 
|---|
| 250 | } | 
|---|
| 251 | } | 
|---|
| 252 | } | 
|---|
| 253 |  | 
|---|
| 254 | return outmap; | 
|---|
| 255 | }; | 
|---|
| 256 |  | 
|---|
| 257 | /** Calculates the distance (pair) correlation between a given element and a surface. | 
|---|
| 258 | * \param *molecules list of molecules structure | 
|---|
| 259 | * \param &elements vector of elements to correlate to surface | 
|---|
| 260 | * \param *Surface pointer to Tesselation class surface | 
|---|
| 261 | * \param *LC LinkedCell structure to quickly find neighbouring atoms | 
|---|
| 262 | * \return Map of doubles with values as pairs of atom and the BoundaryTriangleSet that's closest | 
|---|
| 263 | */ | 
|---|
| 264 | CorrelationToSurfaceMap *CorrelationToSurface(std::vector<molecule *> &molecules, const std::vector<element *> &elements, const Tesselation * const Surface, const LinkedCell *LC ) | 
|---|
| 265 | { | 
|---|
| 266 | Info FunctionInfo(__func__); | 
|---|
| 267 | CorrelationToSurfaceMap *outmap = NULL; | 
|---|
| 268 | double distance = 0; | 
|---|
| 269 | class BoundaryTriangleSet *triangle = NULL; | 
|---|
| 270 | Vector centroid; | 
|---|
| 271 |  | 
|---|
| 272 | if ((Surface == NULL) || (LC == NULL) || (molecules.empty())) { | 
|---|
| 273 | DoeLog(1) && (eLog()<< Verbose(1) <<"No Tesselation, no LinkedCell or no molecule given." << endl); | 
|---|
| 274 | return outmap; | 
|---|
| 275 | } | 
|---|
| 276 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin(); MolWalker != molecules.end(); MolWalker++) | 
|---|
| 277 | (*MolWalker)->doCountAtoms(); | 
|---|
| 278 | outmap = new CorrelationToSurfaceMap; | 
|---|
| 279 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin(); MolWalker != molecules.end(); MolWalker++) { | 
|---|
| 280 | DoLog(2) && (Log() << Verbose(2) << "Current molecule is " << (*MolWalker)->name << "." << endl); | 
|---|
| 281 | if ((*MolWalker)->empty()) | 
|---|
| 282 | DoLog(2) && (2) && (Log() << Verbose(2) << "\t is empty." << endl); | 
|---|
| 283 | for (molecule::const_iterator iter = (*MolWalker)->begin(); iter != (*MolWalker)->end(); ++iter) { | 
|---|
| 284 | DoLog(3) && (Log() << Verbose(3) << "\tCurrent atom is " << *(*iter) << "." << endl); | 
|---|
| 285 | for (vector<element *>::const_iterator type = elements.begin(); type != elements.end(); ++type) | 
|---|
| 286 | if ((*type == NULL) || ((*iter)->type == *type)) { | 
|---|
| 287 | TriangleIntersectionList Intersections((*iter)->node,Surface,LC); | 
|---|
| 288 | distance = Intersections.GetSmallestDistance(); | 
|---|
| 289 | triangle = Intersections.GetClosestTriangle(); | 
|---|
| 290 | outmap->insert ( pair<double, pair<atom *, BoundaryTriangleSet*> >(distance, pair<atom *, BoundaryTriangleSet*> ((*iter), triangle) ) ); | 
|---|
| 291 | } | 
|---|
| 292 | } | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 | return outmap; | 
|---|
| 296 | }; | 
|---|
| 297 |  | 
|---|
| 298 | /** Calculates the distance (pair) correlation between a given element, all its periodic images and and a surface. | 
|---|
| 299 | * Note that we also put all periodic images found in the cells given by [ -ranges[i], ranges[i] ] and i=0,...,NDIM-1. | 
|---|
| 300 | * I.e. We multiply the atom::node with the inverse of the domain matrix, i.e. transform it to \f$[0,0^3\f$, then add per | 
|---|
| 301 | * axis an integer from [ -ranges[i], ranges[i] ] onto it and multiply with the domain matrix to bring it back into | 
|---|
| 302 | * the real space. Then, we Tesselation::FindClosestTriangleToPoint() and DistanceToTrianglePlane(). | 
|---|
| 303 | * \param *molecules list of molecules structure | 
|---|
| 304 | * \param &elements vector of elements to correlate to surface | 
|---|
| 305 | * \param *Surface pointer to Tesselation class surface | 
|---|
| 306 | * \param *LC LinkedCell structure to quickly find neighbouring atoms | 
|---|
| 307 | * \param ranges[NDIM] interval boundaries for the periodic images to scan also | 
|---|
| 308 | * \return Map of doubles with values as pairs of atom and the BoundaryTriangleSet that's closest | 
|---|
| 309 | */ | 
|---|
| 310 | CorrelationToSurfaceMap *PeriodicCorrelationToSurface(std::vector<molecule *> &molecules, const std::vector<element *> &elements, const Tesselation * const Surface, const LinkedCell *LC, const int ranges[NDIM] ) | 
|---|
| 311 | { | 
|---|
| 312 | Info FunctionInfo(__func__); | 
|---|
| 313 | CorrelationToSurfaceMap *outmap = NULL; | 
|---|
| 314 | double distance = 0; | 
|---|
| 315 | class BoundaryTriangleSet *triangle = NULL; | 
|---|
| 316 | Vector centroid; | 
|---|
| 317 | int n[NDIM]; | 
|---|
| 318 | Vector periodicX; | 
|---|
| 319 | Vector checkX; | 
|---|
| 320 |  | 
|---|
| 321 | if ((Surface == NULL) || (LC == NULL) || (molecules.empty())) { | 
|---|
| 322 | DoLog(1) && (Log() << Verbose(1) <<"No Tesselation, no LinkedCell or no molecule given." << endl); | 
|---|
| 323 | return outmap; | 
|---|
| 324 | } | 
|---|
| 325 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin(); MolWalker != molecules.end(); MolWalker++) | 
|---|
| 326 | (*MolWalker)->doCountAtoms(); | 
|---|
| 327 | outmap = new CorrelationToSurfaceMap; | 
|---|
| 328 | double ShortestDistance = 0.; | 
|---|
| 329 | BoundaryTriangleSet *ShortestTriangle = NULL; | 
|---|
| 330 | for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin(); MolWalker != molecules.end(); MolWalker++) { | 
|---|
| 331 | Matrix FullMatrix = World::getInstance().getDomain().getM(); | 
|---|
| 332 | Matrix FullInverseMatrix = World::getInstance().getDomain().getMinv(); | 
|---|
| 333 | DoLog(2) && (Log() << Verbose(2) << "Current molecule is " << *MolWalker << "." << endl); | 
|---|
| 334 | for (molecule::const_iterator iter = (*MolWalker)->begin(); iter != (*MolWalker)->end(); ++iter) { | 
|---|
| 335 | DoLog(3) && (Log() << Verbose(3) << "Current atom is " << **iter << "." << endl); | 
|---|
| 336 | for (vector<element *>::const_iterator type = elements.begin(); type != elements.end(); ++type) | 
|---|
| 337 | if ((*type == NULL) || ((*iter)->type == *type)) { | 
|---|
| 338 | periodicX = FullInverseMatrix * (*(*iter)->node); // x now in [0,1)^3 | 
|---|
| 339 | // go through every range in xyz and get distance | 
|---|
| 340 | ShortestDistance = -1.; | 
|---|
| 341 | for (n[0]=-ranges[0]; n[0] <= ranges[0]; n[0]++) | 
|---|
| 342 | for (n[1]=-ranges[1]; n[1] <= ranges[1]; n[1]++) | 
|---|
| 343 | for (n[2]=-ranges[2]; n[2] <= ranges[2]; n[2]++) { | 
|---|
| 344 | checkX = FullMatrix * (Vector(n[0], n[1], n[2]) + periodicX); | 
|---|
| 345 | TriangleIntersectionList Intersections(&checkX,Surface,LC); | 
|---|
| 346 | distance = Intersections.GetSmallestDistance(); | 
|---|
| 347 | triangle = Intersections.GetClosestTriangle(); | 
|---|
| 348 | if ((ShortestDistance == -1.) || (distance < ShortestDistance)) { | 
|---|
| 349 | ShortestDistance = distance; | 
|---|
| 350 | ShortestTriangle = triangle; | 
|---|
| 351 | } | 
|---|
| 352 | } | 
|---|
| 353 | // insert | 
|---|
| 354 | outmap->insert ( pair<double, pair<atom *, BoundaryTriangleSet*> >(ShortestDistance, pair<atom *, BoundaryTriangleSet*> (*iter, ShortestTriangle) ) ); | 
|---|
| 355 | //Log() << Verbose(1) << "INFO: Inserting " << Walker << " with distance " << ShortestDistance << " to " << *ShortestTriangle << "." << endl; | 
|---|
| 356 | } | 
|---|
| 357 | } | 
|---|
| 358 | } | 
|---|
| 359 |  | 
|---|
| 360 | return outmap; | 
|---|
| 361 | }; | 
|---|
| 362 |  | 
|---|
| 363 | /** Returns the index of the bin for a given value. | 
|---|
| 364 | * \param value value whose bin to look for | 
|---|
| 365 | * \param BinWidth width of bin | 
|---|
| 366 | * \param BinStart first bin | 
|---|
| 367 | */ | 
|---|
| 368 | int GetBin ( const double value, const double BinWidth, const double BinStart ) | 
|---|
| 369 | { | 
|---|
| 370 | Info FunctionInfo(__func__); | 
|---|
| 371 | int bin =(int) (floor((value - BinStart)/BinWidth)); | 
|---|
| 372 | return (bin); | 
|---|
| 373 | }; | 
|---|
| 374 |  | 
|---|
| 375 |  | 
|---|
| 376 | /** Prints correlation (double, int) pairs to file. | 
|---|
| 377 | * \param *file file to write to | 
|---|
| 378 | * \param *map map to write | 
|---|
| 379 | */ | 
|---|
| 380 | void OutputCorrelation( ofstream * const file, const BinPairMap * const map ) | 
|---|
| 381 | { | 
|---|
| 382 | Info FunctionInfo(__func__); | 
|---|
| 383 | *file << "BinStart\tCount" << endl; | 
|---|
| 384 | for (BinPairMap::const_iterator runner = map->begin(); runner != map->end(); ++runner) { | 
|---|
| 385 | *file << setprecision(8) << runner->first << "\t" << runner->second << endl; | 
|---|
| 386 | } | 
|---|
| 387 | }; | 
|---|
| 388 |  | 
|---|
| 389 | /** Prints correlation (double, (atom*,atom*) ) pairs to file. | 
|---|
| 390 | * \param *file file to write to | 
|---|
| 391 | * \param *map map to write | 
|---|
| 392 | */ | 
|---|
| 393 | void OutputPairCorrelation( ofstream * const file, const PairCorrelationMap * const map ) | 
|---|
| 394 | { | 
|---|
| 395 | Info FunctionInfo(__func__); | 
|---|
| 396 | *file << "BinStart\tAtom1\tAtom2" << endl; | 
|---|
| 397 | for (PairCorrelationMap::const_iterator runner = map->begin(); runner != map->end(); ++runner) { | 
|---|
| 398 | *file << setprecision(8) << runner->first << "\t" << *(runner->second.first) << "\t" << *(runner->second.second) << endl; | 
|---|
| 399 | } | 
|---|
| 400 | }; | 
|---|
| 401 |  | 
|---|
| 402 | /** Prints correlation (double, int) pairs to file. | 
|---|
| 403 | * \param *file file to write to | 
|---|
| 404 | * \param *map map to write | 
|---|
| 405 | */ | 
|---|
| 406 | void OutputCorrelationToPoint( ofstream * const file, const CorrelationToPointMap * const map ) | 
|---|
| 407 | { | 
|---|
| 408 | Info FunctionInfo(__func__); | 
|---|
| 409 | *file << "BinStart\tAtom::x[i]-point.x[i]" << endl; | 
|---|
| 410 | for (CorrelationToPointMap::const_iterator runner = map->begin(); runner != map->end(); ++runner) { | 
|---|
| 411 | *file << runner->first; | 
|---|
| 412 | for (int i=0;i<NDIM;i++) | 
|---|
| 413 | *file << "\t" << setprecision(8) << (runner->second.first->node->at(i) - runner->second.second->at(i)); | 
|---|
| 414 | *file << endl; | 
|---|
| 415 | } | 
|---|
| 416 | }; | 
|---|
| 417 |  | 
|---|
| 418 | /** Prints correlation (double, int) pairs to file. | 
|---|
| 419 | * \param *file file to write to | 
|---|
| 420 | * \param *map map to write | 
|---|
| 421 | */ | 
|---|
| 422 | void OutputCorrelationToSurface( ofstream * const file, const CorrelationToSurfaceMap * const map ) | 
|---|
| 423 | { | 
|---|
| 424 | Info FunctionInfo(__func__); | 
|---|
| 425 | *file << "BinStart\tTriangle" << endl; | 
|---|
| 426 | if (!map->empty()) | 
|---|
| 427 | for (CorrelationToSurfaceMap::const_iterator runner = map->begin(); runner != map->end(); ++runner) { | 
|---|
| 428 | *file << setprecision(8) << runner->first << "\t" << *(runner->second.first) << "\t" << *(runner->second.second) << endl; | 
|---|
| 429 | } | 
|---|
| 430 | }; | 
|---|
| 431 |  | 
|---|