| 1 | /*
 | 
|---|
| 2 |  * analysis.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Oct 13, 2009
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include <iostream>
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include "analysis_correlation.hpp"
 | 
|---|
| 11 | #include "element.hpp"
 | 
|---|
| 12 | #include "molecule.hpp"
 | 
|---|
| 13 | #include "tesselation.hpp"
 | 
|---|
| 14 | #include "tesselationhelpers.hpp"
 | 
|---|
| 15 | #include "vector.hpp"
 | 
|---|
| 16 | #include "verbose.hpp"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | 
 | 
|---|
| 19 | /** Calculates the pair correlation between given elements.
 | 
|---|
| 20 |  * Note given element order is unimportant (i.e. g(Si, O) === g(O, Si))
 | 
|---|
| 21 |  * \param *out output stream for debugging
 | 
|---|
| 22 |  * \param *molecules list of molecules structure
 | 
|---|
| 23 |  * \param *type1 first element or NULL (if any element)
 | 
|---|
| 24 |  * \param *type2 second element or NULL (if any element)
 | 
|---|
| 25 |  * \return Map of doubles with values the pair of the two atoms.
 | 
|---|
| 26 |  */
 | 
|---|
| 27 | PairCorrelationMap *PairCorrelation( ofstream * const out, MoleculeListClass * const &molecules, const element * const type1, const element * const type2 )
 | 
|---|
| 28 | {
 | 
|---|
| 29 |   PairCorrelationMap *outmap = NULL;
 | 
|---|
| 30 |   double distance = 0.;
 | 
|---|
| 31 | 
 | 
|---|
| 32 |   if (molecules->ListOfMolecules.empty()) {
 | 
|---|
| 33 |     cerr << Verbose(1) <<"No molecule given." << endl;
 | 
|---|
| 34 |     return outmap;
 | 
|---|
| 35 |   }
 | 
|---|
| 36 |   outmap = new PairCorrelationMap;
 | 
|---|
| 37 |   for (MoleculeList::const_iterator MolWalker = molecules->ListOfMolecules.begin(); MolWalker != molecules->ListOfMolecules.end(); MolWalker++)
 | 
|---|
| 38 |     if ((*MolWalker)->ActiveFlag) {
 | 
|---|
| 39 |       cerr << Verbose(2) << "Current molecule is " << *MolWalker << "." << endl;
 | 
|---|
| 40 |       atom *Walker = (*MolWalker)->start;
 | 
|---|
| 41 |       while (Walker->next != (*MolWalker)->end) {
 | 
|---|
| 42 |         Walker = Walker->next;
 | 
|---|
| 43 |         *out << Verbose(3) << "Current atom is " << *Walker << "." << endl;
 | 
|---|
| 44 |         if ((type1 == NULL) || (Walker->type == type1)) {
 | 
|---|
| 45 |           for (MoleculeList::const_iterator MolOtherWalker = MolWalker; MolOtherWalker != molecules->ListOfMolecules.end(); MolOtherWalker++)
 | 
|---|
| 46 |             if ((*MolOtherWalker)->ActiveFlag) {
 | 
|---|
| 47 |               *out << Verbose(2) << "Current other molecule is " << *MolOtherWalker << "." << endl;
 | 
|---|
| 48 |               atom *OtherWalker = (*MolOtherWalker)->start;
 | 
|---|
| 49 |               while (OtherWalker->next != (*MolOtherWalker)->end) { // only go up to Walker
 | 
|---|
| 50 |                 OtherWalker = OtherWalker->next;
 | 
|---|
| 51 |                 *out << Verbose(3) << "Current otheratom is " << *OtherWalker << "." << endl;
 | 
|---|
| 52 |                 if (Walker->nr < OtherWalker->nr)
 | 
|---|
| 53 |                   if ((type2 == NULL) || (OtherWalker->type == type2)) {
 | 
|---|
| 54 |                     distance = Walker->node->PeriodicDistance(OtherWalker->node, (*MolWalker)->cell_size);
 | 
|---|
| 55 |                     //*out << Verbose(1) <<"Inserting " << *Walker << " and " << *OtherWalker << endl;
 | 
|---|
| 56 |                     outmap->insert ( pair<double, pair <atom *, atom*> > (distance, pair<atom *, atom*> (Walker, OtherWalker) ) );
 | 
|---|
| 57 |                   }
 | 
|---|
| 58 |               }
 | 
|---|
| 59 |           }
 | 
|---|
| 60 |         }
 | 
|---|
| 61 |       }
 | 
|---|
| 62 |     }
 | 
|---|
| 63 | 
 | 
|---|
| 64 |   return outmap;
 | 
|---|
| 65 | };
 | 
|---|
| 66 | 
 | 
|---|
| 67 | /** Calculates the distance (pair) correlation between a given element and a point.
 | 
|---|
| 68 |  * \param *out output stream for debugging
 | 
|---|
| 69 |  * \param *molecules list of molecules structure
 | 
|---|
| 70 |  * \param *type element or NULL (if any element)
 | 
|---|
| 71 |  * \param *point vector to the correlation point
 | 
|---|
| 72 |  * \return Map of dobules with values as pairs of atom and the vector
 | 
|---|
| 73 |  */
 | 
|---|
| 74 | CorrelationToPointMap *CorrelationToPoint(  ofstream * const out, MoleculeListClass * const &molecules, const element * const type, const Vector *point )
 | 
|---|
| 75 | {
 | 
|---|
| 76 |   CorrelationToPointMap *outmap = NULL;
 | 
|---|
| 77 |   double distance = 0.;
 | 
|---|
| 78 | 
 | 
|---|
| 79 |   if (molecules->ListOfMolecules.empty()) {
 | 
|---|
| 80 |     *out << Verbose(1) <<"No molecule given." << endl;
 | 
|---|
| 81 |     return outmap;
 | 
|---|
| 82 |   }
 | 
|---|
| 83 |   outmap = new CorrelationToPointMap;
 | 
|---|
| 84 |   for (MoleculeList::const_iterator MolWalker = molecules->ListOfMolecules.begin(); MolWalker != molecules->ListOfMolecules.end(); MolWalker++)
 | 
|---|
| 85 |     if ((*MolWalker)->ActiveFlag) {
 | 
|---|
| 86 |       *out << Verbose(2) << "Current molecule is " << *MolWalker << "." << endl;
 | 
|---|
| 87 |       atom *Walker = (*MolWalker)->start;
 | 
|---|
| 88 |       while (Walker->next != (*MolWalker)->end) {
 | 
|---|
| 89 |         Walker = Walker->next;
 | 
|---|
| 90 |         *out << Verbose(3) << "Current atom is " << *Walker << "." << endl;
 | 
|---|
| 91 |         if ((type == NULL) || (Walker->type == type)) {
 | 
|---|
| 92 |           distance = Walker->node->PeriodicDistance(point, (*MolWalker)->cell_size);
 | 
|---|
| 93 |           *out << Verbose(4) << "Current distance is " << distance << "." << endl;
 | 
|---|
| 94 |           outmap->insert ( pair<double, pair<atom *, const Vector*> >(distance, pair<atom *, const Vector*> (Walker, point) ) );
 | 
|---|
| 95 |         }
 | 
|---|
| 96 |       }
 | 
|---|
| 97 |     }
 | 
|---|
| 98 | 
 | 
|---|
| 99 |   return outmap;
 | 
|---|
| 100 | };
 | 
|---|
| 101 | 
 | 
|---|
| 102 | /** Calculates the distance (pair) correlation between a given element and a surface.
 | 
|---|
| 103 |  * \param *out output stream for debugging
 | 
|---|
| 104 |  * \param *molecules list of molecules structure
 | 
|---|
| 105 |  * \param *type element or NULL (if any element)
 | 
|---|
| 106 |  * \param *Surface pointer to Tesselation class surface
 | 
|---|
| 107 |  * \param *LC LinkedCell structure to quickly find neighbouring atoms
 | 
|---|
| 108 |  * \return Map of doubles with values as pairs of atom and the BoundaryTriangleSet that's closest
 | 
|---|
| 109 |  */
 | 
|---|
| 110 | CorrelationToSurfaceMap *CorrelationToSurface( ofstream * const out, MoleculeListClass * const &molecules, const element * const type, const Tesselation * const Surface, const LinkedCell *LC )
 | 
|---|
| 111 | {
 | 
|---|
| 112 |   CorrelationToSurfaceMap *outmap = NULL;
 | 
|---|
| 113 |   double distance = 0;
 | 
|---|
| 114 |   class BoundaryTriangleSet *triangle = NULL;
 | 
|---|
| 115 |   Vector centroid;
 | 
|---|
| 116 |   int ranges[NDIM] = {1, 1, 1};
 | 
|---|
| 117 |   int n[NDIM];
 | 
|---|
| 118 |   Vector periodicX;
 | 
|---|
| 119 |   Vector checkX;
 | 
|---|
| 120 | 
 | 
|---|
| 121 |   if ((Surface == NULL) || (LC == NULL) || (molecules->ListOfMolecules.empty())) {
 | 
|---|
| 122 |     *out << Verbose(1) <<"No Tesselation, no LinkedCell or no molecule given." << endl;
 | 
|---|
| 123 |     return outmap;
 | 
|---|
| 124 |   }
 | 
|---|
| 125 |   outmap = new CorrelationToSurfaceMap;
 | 
|---|
| 126 |   for (MoleculeList::const_iterator MolWalker = molecules->ListOfMolecules.begin(); MolWalker != molecules->ListOfMolecules.end(); MolWalker++)
 | 
|---|
| 127 |     if ((*MolWalker)->ActiveFlag) {
 | 
|---|
| 128 |       const double * const FullMatrix = ReturnFullMatrixforSymmetric((*MolWalker)->cell_size);
 | 
|---|
| 129 |       const double * const FullInverseMatrix = InverseMatrix(FullMatrix);
 | 
|---|
| 130 |       *out << Verbose(2) << "Current molecule is " << *MolWalker << "." << endl;
 | 
|---|
| 131 |       atom *Walker = (*MolWalker)->start;
 | 
|---|
| 132 |       while (Walker->next != (*MolWalker)->end) {
 | 
|---|
| 133 |         Walker = Walker->next;
 | 
|---|
| 134 |         *out << Verbose(3) << "Current atom is " << *Walker << "." << endl;
 | 
|---|
| 135 |         if ((type == NULL) || (Walker->type == type)) {
 | 
|---|
| 136 |           periodicX.CopyVector(Walker->node);
 | 
|---|
| 137 |           periodicX.MatrixMultiplication(FullInverseMatrix);  // x now in [0,1)^3
 | 
|---|
| 138 |           // go through every range in xyz and get distance
 | 
|---|
| 139 |           for (n[0]=-ranges[0]; n[0] <= ranges[0]; n[0]++)
 | 
|---|
| 140 |             for (n[1]=-ranges[1]; n[1] <= ranges[1]; n[1]++)
 | 
|---|
| 141 |               for (n[2]=-ranges[2]; n[2] <= ranges[2]; n[2]++) {
 | 
|---|
| 142 |                 checkX.Init(n[0], n[1], n[2]);
 | 
|---|
| 143 |                 checkX.AddVector(&periodicX);
 | 
|---|
| 144 |                 checkX.MatrixMultiplication(FullMatrix);
 | 
|---|
| 145 |                 triangle = Surface->FindClosestTriangleToPoint(out, &checkX, LC );
 | 
|---|
| 146 |                 if (triangle != NULL) {
 | 
|---|
| 147 |                   distance = DistanceToTrianglePlane(out, &checkX, triangle);
 | 
|---|
| 148 |                   outmap->insert ( pair<double, pair<atom *, BoundaryTriangleSet*> >(distance, pair<atom *, BoundaryTriangleSet*> (Walker, triangle) ) );
 | 
|---|
| 149 |                 }
 | 
|---|
| 150 |           }
 | 
|---|
| 151 |         }
 | 
|---|
| 152 |       }
 | 
|---|
| 153 |     }
 | 
|---|
| 154 | 
 | 
|---|
| 155 |   return outmap;
 | 
|---|
| 156 | };
 | 
|---|
| 157 | 
 | 
|---|
| 158 | /** Returns the start of the bin for a given value.
 | 
|---|
| 159 |  * \param value value whose bin to look for
 | 
|---|
| 160 |  * \param BinWidth width of bin
 | 
|---|
| 161 |  * \param BinStart first bin
 | 
|---|
| 162 |  */
 | 
|---|
| 163 | double GetBin ( const double value, const double BinWidth, const double BinStart )
 | 
|---|
| 164 | {
 | 
|---|
| 165 |   double bin =(double) (floor((value - BinStart)/BinWidth));
 | 
|---|
| 166 |   return (bin*BinWidth+BinStart);
 | 
|---|
| 167 | };
 | 
|---|
| 168 | 
 | 
|---|
| 169 | 
 | 
|---|
| 170 | /** Prints correlation (double, int) pairs to file.
 | 
|---|
| 171 |  * \param *file file to write to
 | 
|---|
| 172 |  * \param *map map to write
 | 
|---|
| 173 |  */
 | 
|---|
| 174 | void OutputCorrelation( ofstream * const file, const BinPairMap * const map )
 | 
|---|
| 175 | {
 | 
|---|
| 176 |   *file << "# BinStart\tCount" << endl;
 | 
|---|
| 177 |   for (BinPairMap::const_iterator runner = map->begin(); runner != map->end(); ++runner) {
 | 
|---|
| 178 |     *file << runner->first << "\t" << runner->second << endl;
 | 
|---|
| 179 |   }
 | 
|---|
| 180 | };
 | 
|---|
| 181 | 
 | 
|---|
| 182 | /** Prints correlation (double, (atom*,atom*) ) pairs to file.
 | 
|---|
| 183 |  * \param *file file to write to
 | 
|---|
| 184 |  * \param *map map to write
 | 
|---|
| 185 |  */
 | 
|---|
| 186 | void OutputPairCorrelation( ofstream * const file, const PairCorrelationMap * const map )
 | 
|---|
| 187 | {
 | 
|---|
| 188 |   *file << "# BinStart\tAtom1\tAtom2" << endl;
 | 
|---|
| 189 |   for (PairCorrelationMap::const_iterator runner = map->begin(); runner != map->end(); ++runner) {
 | 
|---|
| 190 |     *file << runner->first << "\t" << *(runner->second.first) << "\t" << *(runner->second.second) << endl;
 | 
|---|
| 191 |   }
 | 
|---|
| 192 | };
 | 
|---|
| 193 | 
 | 
|---|
| 194 | /** Prints correlation (double, int) pairs to file.
 | 
|---|
| 195 |  * \param *file file to write to
 | 
|---|
| 196 |  * \param *map map to write
 | 
|---|
| 197 |  */
 | 
|---|
| 198 | void OutputCorrelationToPoint( ofstream * const file, const CorrelationToPointMap * const map )
 | 
|---|
| 199 | {
 | 
|---|
| 200 |   *file << "# BinStart\tAtom::x[i]-point.x[i]" << endl;
 | 
|---|
| 201 |   for (CorrelationToPointMap::const_iterator runner = map->begin(); runner != map->end(); ++runner) {
 | 
|---|
| 202 |     *file << runner->first;
 | 
|---|
| 203 |     for (int i=0;i<NDIM;i++)
 | 
|---|
| 204 |       *file << "\t" << (runner->second.first->node->x[i] - runner->second.second->x[i]);
 | 
|---|
| 205 |     *file << endl;
 | 
|---|
| 206 |   }
 | 
|---|
| 207 | };
 | 
|---|
| 208 | 
 | 
|---|
| 209 | /** Prints correlation (double, int) pairs to file.
 | 
|---|
| 210 |  * \param *file file to write to
 | 
|---|
| 211 |  * \param *map map to write
 | 
|---|
| 212 |  */
 | 
|---|
| 213 | void OutputCorrelationToSurface( ofstream * const file, const CorrelationToSurfaceMap * const map )
 | 
|---|
| 214 | {
 | 
|---|
| 215 |   *file << "# BinStart\tTriangle" << endl;
 | 
|---|
| 216 |   for (CorrelationToSurfaceMap::const_iterator runner = map->begin(); runner != map->end(); ++runner) {
 | 
|---|
| 217 |     *file << runner->first << "\t" << *(runner->second.second) << endl;
 | 
|---|
| 218 |   }
 | 
|---|
| 219 | };
 | 
|---|
| 220 | 
 | 
|---|