| 1 | /* | 
|---|
| 2 | * analysis.hpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Oct 13, 2009 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #ifndef ANALYSIS_HPP_ | 
|---|
| 9 | #define ANALYSIS_HPP_ | 
|---|
| 10 |  | 
|---|
| 11 | using namespace std; | 
|---|
| 12 |  | 
|---|
| 13 | /*********************************************** includes ***********************************/ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | #include <cmath> | 
|---|
| 21 | #include <iomanip> | 
|---|
| 22 | #include <fstream> | 
|---|
| 23 |  | 
|---|
| 24 | // STL headers | 
|---|
| 25 | #include <map> | 
|---|
| 26 | #include <vector> | 
|---|
| 27 |  | 
|---|
| 28 | #include "Helpers/defs.hpp" | 
|---|
| 29 |  | 
|---|
| 30 | #include "Atom/atom.hpp" | 
|---|
| 31 | #include "CodePatterns/Info.hpp" | 
|---|
| 32 | #include "CodePatterns/Log.hpp" | 
|---|
| 33 | #include "CodePatterns/Range.hpp" | 
|---|
| 34 | #include "CodePatterns/Verbose.hpp" | 
|---|
| 35 | #include "Helpers/helpers.hpp" | 
|---|
| 36 | #include "World.hpp" | 
|---|
| 37 |  | 
|---|
| 38 | /****************************************** forward declarations *****************************/ | 
|---|
| 39 |  | 
|---|
| 40 | class BoundaryTriangleSet; | 
|---|
| 41 | class Formula; | 
|---|
| 42 | class element; | 
|---|
| 43 | class LinkedCell_deprecated; | 
|---|
| 44 | class Tesselation; | 
|---|
| 45 | class Vector; | 
|---|
| 46 |  | 
|---|
| 47 | /********************************************** definitions *********************************/ | 
|---|
| 48 |  | 
|---|
| 49 | typedef multimap<double, pair<const TesselPoint *, const TesselPoint *> > PairCorrelationMap; | 
|---|
| 50 | typedef multimap<double, const atom * > DipoleAngularCorrelationMap; | 
|---|
| 51 | typedef multimap<double, pair<const molecule *, const molecule *> > DipoleCorrelationMap; | 
|---|
| 52 | typedef multimap<double, pair<const atom *, const Vector *> > CorrelationToPointMap; | 
|---|
| 53 | typedef multimap<double, pair<const atom *, BoundaryTriangleSet *> > CorrelationToSurfaceMap; | 
|---|
| 54 | typedef map<double, int> BinPairMap; | 
|---|
| 55 |  | 
|---|
| 56 | enum ResetWorldTime { | 
|---|
| 57 | DontResetTime, | 
|---|
| 58 | DoResetTime | 
|---|
| 59 | }; | 
|---|
| 60 |  | 
|---|
| 61 | /********************************************** declarations *******************************/ | 
|---|
| 62 |  | 
|---|
| 63 | range<size_t> getMaximumTrajectoryBounds( | 
|---|
| 64 | const std::vector<const atom *> &atoms); | 
|---|
| 65 | std::map<atomId_t, Vector> CalculateZeroAngularDipole( | 
|---|
| 66 | const std::vector<const molecule *> &molecules); | 
|---|
| 67 |  | 
|---|
| 68 | DipoleAngularCorrelationMap *DipoleAngularCorrelation( | 
|---|
| 69 | const Formula &DipoleFormula, | 
|---|
| 70 | const size_t timestep, | 
|---|
| 71 | const std::map<atomId_t, Vector> &ZeroVector, | 
|---|
| 72 | const enum ResetWorldTime DoTimeReset = DontResetTime); | 
|---|
| 73 | DipoleCorrelationMap *DipoleCorrelation( | 
|---|
| 74 | const std::vector<const molecule *> &molecules); | 
|---|
| 75 | PairCorrelationMap *PairCorrelation( | 
|---|
| 76 | const World::ConstAtomComposite &atoms_first, | 
|---|
| 77 | const World::ConstAtomComposite &atoms_second, | 
|---|
| 78 | const double max_distance); | 
|---|
| 79 | CorrelationToPointMap *CorrelationToPoint( | 
|---|
| 80 | const std::vector<const molecule *> &molecules, | 
|---|
| 81 | const std::vector<const element *> &elements, | 
|---|
| 82 | const Vector *point ); | 
|---|
| 83 | CorrelationToSurfaceMap *CorrelationToSurface( | 
|---|
| 84 | const std::vector<const molecule *> &molecules, | 
|---|
| 85 | const std::vector<const element *> &elements, | 
|---|
| 86 | const Tesselation * const Surface, | 
|---|
| 87 | const LinkedCell_deprecated *LC ); | 
|---|
| 88 | CorrelationToPointMap *PeriodicCorrelationToPoint( | 
|---|
| 89 | const std::vector<const molecule *> &molecules, | 
|---|
| 90 | const std::vector<const element *> &elements, | 
|---|
| 91 | const Vector *point, const int ranges[NDIM] ); | 
|---|
| 92 | CorrelationToSurfaceMap *PeriodicCorrelationToSurface( | 
|---|
| 93 | const std::vector<const molecule *> &molecules, | 
|---|
| 94 | const std::vector<const element *> &elements, | 
|---|
| 95 | const Tesselation * const Surface, | 
|---|
| 96 | const LinkedCell_deprecated *LC, | 
|---|
| 97 | const int ranges[NDIM] ); | 
|---|
| 98 | int GetBin ( | 
|---|
| 99 | const double value, | 
|---|
| 100 | const double BinWidth, | 
|---|
| 101 | const double BinStart ); | 
|---|
| 102 | void OutputCorrelation_Header( | 
|---|
| 103 | ofstream * const file ); | 
|---|
| 104 | void OutputCorrelation_Value( | 
|---|
| 105 | ofstream * const file, | 
|---|
| 106 | BinPairMap::const_iterator &runner ); | 
|---|
| 107 | void OutputDipoleAngularCorrelation_Header( | 
|---|
| 108 | ofstream * const file ); | 
|---|
| 109 | void OutputDipoleAngularCorrelation_Value( | 
|---|
| 110 | ofstream * const file, | 
|---|
| 111 | DipoleAngularCorrelationMap::const_iterator &runner ); | 
|---|
| 112 | void OutputDipoleCorrelation_Header( | 
|---|
| 113 | ofstream * const file ); | 
|---|
| 114 | void OutputDipoleCorrelation_Value( | 
|---|
| 115 | ofstream * const file, | 
|---|
| 116 | DipoleCorrelationMap::const_iterator &runner ); | 
|---|
| 117 | void OutputPairCorrelation_Header( | 
|---|
| 118 | ofstream * const file ); | 
|---|
| 119 | void OutputPairCorrelation_Value( | 
|---|
| 120 | ofstream * const file, | 
|---|
| 121 | PairCorrelationMap::const_iterator &runner ); | 
|---|
| 122 | void OutputCorrelationToPoint_Header( | 
|---|
| 123 | ofstream * const file ); | 
|---|
| 124 | void OutputCorrelationToPoint_Value( | 
|---|
| 125 | ofstream * const file, | 
|---|
| 126 | CorrelationToPointMap::const_iterator &runner ); | 
|---|
| 127 | void OutputCorrelationToSurface_Header( | 
|---|
| 128 | ofstream * const file ); | 
|---|
| 129 | void OutputCorrelationToSurface_Value( | 
|---|
| 130 | ofstream * const file, | 
|---|
| 131 | CorrelationToSurfaceMap::const_iterator &runner ); | 
|---|
| 132 |  | 
|---|
| 133 | /** Searches for lowest and highest value in a given map of doubles. | 
|---|
| 134 | * \param *map map of doubles to scan | 
|---|
| 135 | * \param &min minimum on return | 
|---|
| 136 | * \param &max maximum on return | 
|---|
| 137 | */ | 
|---|
| 138 | template <typename T> void GetMinMax ( T *map, double &min, double &max) | 
|---|
| 139 | { | 
|---|
| 140 | min = 0.; | 
|---|
| 141 | max = 0.; | 
|---|
| 142 | bool FirstMinFound = false; | 
|---|
| 143 | bool FirstMaxFound = false; | 
|---|
| 144 |  | 
|---|
| 145 | if (map == NULL) { | 
|---|
| 146 | ELOG(0, "Nothing to min/max, map is NULL!"); | 
|---|
| 147 | performCriticalExit(); | 
|---|
| 148 | return; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | for (typename T::iterator runner = map->begin(); runner != map->end(); ++runner) { | 
|---|
| 152 | if ((min > runner->first) || (!FirstMinFound)) { | 
|---|
| 153 | min = runner->first; | 
|---|
| 154 | FirstMinFound = true; | 
|---|
| 155 | } | 
|---|
| 156 | if ((max < runner->first) || (!FirstMaxFound)) { | 
|---|
| 157 | max = runner->first; | 
|---|
| 158 | FirstMaxFound = true; | 
|---|
| 159 | } | 
|---|
| 160 | } | 
|---|
| 161 | }; | 
|---|
| 162 |  | 
|---|
| 163 | /** Puts given correlation data into bins of given size (histogramming). | 
|---|
| 164 | * Note that BinStart and BinEnd are desired to fill the complete range, even where Bins are zero. If this is | 
|---|
| 165 | * not desired, give equal BinStart and BinEnd and the map will contain only Bins where the count is one or greater. If a | 
|---|
| 166 | * certain start value is desired, give BinStart and a BinEnd that is smaller than BinStart, then only BinEnd will be | 
|---|
| 167 | * calculated automatically, i.e. BinStart = 0. and BinEnd = -1. . | 
|---|
| 168 | * Also note that the range is given inclusive, i.e. [ BinStart, BinEnd ]. | 
|---|
| 169 | * \param *map map of doubles to count | 
|---|
| 170 | * \param BinWidth desired width of the binds | 
|---|
| 171 | * \param BinStart first bin | 
|---|
| 172 | * \param BinEnd last bin | 
|---|
| 173 | * \return Map of doubles (the bins) with counts as values | 
|---|
| 174 | */ | 
|---|
| 175 | template <typename T> BinPairMap *BinData ( T *map, const double BinWidth, const double BinStart, const double BinEnd ) | 
|---|
| 176 | { | 
|---|
| 177 | BinPairMap *outmap = new BinPairMap; | 
|---|
| 178 | int bin = 0; | 
|---|
| 179 | double start = 0.; | 
|---|
| 180 | double end = 0.; | 
|---|
| 181 | pair <BinPairMap::iterator, bool > BinPairMapInserter; | 
|---|
| 182 |  | 
|---|
| 183 | if (map == NULL) { | 
|---|
| 184 | ELOG(0, "Nothing to bin, is NULL!"); | 
|---|
| 185 | performCriticalExit(); | 
|---|
| 186 | return outmap; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | if (BinStart == BinEnd) { // if same, find range ourselves | 
|---|
| 190 | GetMinMax( map, start, end); | 
|---|
| 191 | } else if (BinEnd < BinStart) { // if BinEnd smaller, just look for new max | 
|---|
| 192 | GetMinMax( map, start, end); | 
|---|
| 193 | start = BinStart; | 
|---|
| 194 | } else { // else take both values | 
|---|
| 195 | start = BinStart; | 
|---|
| 196 | end = BinEnd; | 
|---|
| 197 | } | 
|---|
| 198 | // limit precision of start and end bin to 6 digits | 
|---|
| 199 | const double precision = 1e-6/BinWidth; | 
|---|
| 200 | end = precision*floor(end/precision); | 
|---|
| 201 | start = precision*floor(start/precision); | 
|---|
| 202 | for (int runner = 0; runner <= ceil((end-start)/BinWidth); runner++) | 
|---|
| 203 | outmap->insert( pair<double, int> ((double)runner*BinWidth+start, 0) ); | 
|---|
| 204 |  | 
|---|
| 205 | for (typename T::iterator runner = map->begin(); runner != map->end(); ++runner) { | 
|---|
| 206 | bin = GetBin (runner->first, BinWidth, start); | 
|---|
| 207 | BinPairMapInserter = outmap->insert ( pair<double, int> ((double)bin*BinWidth+start, 1) ); | 
|---|
| 208 | if (!BinPairMapInserter.second) {  // bin already present, increase | 
|---|
| 209 | BinPairMapInserter.first->second += 1; | 
|---|
| 210 | } else { // bin not present, then remove again | 
|---|
| 211 | outmap->erase(BinPairMapInserter.first); | 
|---|
| 212 | } | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 | return outmap; | 
|---|
| 216 | }; | 
|---|
| 217 |  | 
|---|
| 218 | /** Prints correlation map of template type to file. | 
|---|
| 219 | * \param *file file to write to | 
|---|
| 220 | * \param *map map to write | 
|---|
| 221 | * \param (*header) pointer to function that adds specific part to header | 
|---|
| 222 | * \param (*value) pointer to function that prints value from given iterator | 
|---|
| 223 | */ | 
|---|
| 224 | template <typename T> | 
|---|
| 225 | void OutputCorrelationMap( | 
|---|
| 226 | ofstream * const file, | 
|---|
| 227 | const T * const map, | 
|---|
| 228 | void (*header)( ofstream * const file), | 
|---|
| 229 | void (*value)( ofstream * const file, typename T::const_iterator &runner) | 
|---|
| 230 | ) | 
|---|
| 231 | { | 
|---|
| 232 | Info FunctionInfo(__func__); | 
|---|
| 233 | *file << "BinStart\tBinCenter\tBinEnd"; | 
|---|
| 234 | header(file); | 
|---|
| 235 | *file << endl; | 
|---|
| 236 | typename T::const_iterator advancer = map->begin(); | 
|---|
| 237 | typename T::const_iterator runner = map->begin(); | 
|---|
| 238 | if (advancer != map->end()) | 
|---|
| 239 | advancer++; | 
|---|
| 240 | for ( ;(advancer != map->end()) && (runner != map->end()); ++advancer, ++runner) { | 
|---|
| 241 | *file << setprecision(8) | 
|---|
| 242 | << runner->first << "\t" | 
|---|
| 243 | << (advancer->first + runner->first)/2. << "\t" | 
|---|
| 244 | << advancer->first << "\t"; | 
|---|
| 245 | value(file, runner); | 
|---|
| 246 | *file << endl; | 
|---|
| 247 | } | 
|---|
| 248 | if(runner != map->end()) { | 
|---|
| 249 | *file << setprecision(8) << runner->first << "\t0\t0\t"; | 
|---|
| 250 | value(file, runner); | 
|---|
| 251 | *file << endl; | 
|---|
| 252 | } | 
|---|
| 253 | }; | 
|---|
| 254 |  | 
|---|
| 255 |  | 
|---|
| 256 | #endif /* ANALYSIS_HPP_ */ | 
|---|