| 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(const std::vector<atom *> &atoms); | 
|---|
| 64 | std::map<atomId_t, Vector> CalculateZeroAngularDipole(const std::vector<molecule *> &molecules); | 
|---|
| 65 |  | 
|---|
| 66 | DipoleAngularCorrelationMap *DipoleAngularCorrelation(const Formula &DipoleFormula, const size_t timestep, const std::map<atomId_t, Vector> &ZeroVector, const enum ResetWorldTime DoTimeReset = DontResetTime); | 
|---|
| 67 | DipoleCorrelationMap *DipoleCorrelation(std::vector<molecule *> &molecules); | 
|---|
| 68 | PairCorrelationMap *PairCorrelation( | 
|---|
| 69 | const World::AtomComposite &atoms_first, | 
|---|
| 70 | const World::AtomComposite &atoms_second, | 
|---|
| 71 | const double max_distance); | 
|---|
| 72 | CorrelationToPointMap *CorrelationToPoint(std::vector<molecule *> &molecules, const std::vector<const element *> &elements, const Vector *point ); | 
|---|
| 73 | CorrelationToSurfaceMap *CorrelationToSurface(std::vector<molecule *> &molecules, const std::vector<const element *> &elements, const Tesselation * const Surface, const LinkedCell_deprecated *LC ); | 
|---|
| 74 | CorrelationToPointMap *PeriodicCorrelationToPoint(std::vector<molecule *> &molecules, const std::vector<const element *> &elements, const Vector *point, const int ranges[NDIM] ); | 
|---|
| 75 | CorrelationToSurfaceMap *PeriodicCorrelationToSurface(std::vector<molecule *> &molecules, const std::vector<const element *> &elements, const Tesselation * const Surface, const LinkedCell_deprecated *LC, const int ranges[NDIM] ); | 
|---|
| 76 | int GetBin ( const double value, const double BinWidth, const double BinStart ); | 
|---|
| 77 | void OutputCorrelation_Header( ofstream * const file ); | 
|---|
| 78 | void OutputCorrelation_Value( ofstream * const file, BinPairMap::const_iterator &runner ); | 
|---|
| 79 | void OutputDipoleAngularCorrelation_Header( ofstream * const file ); | 
|---|
| 80 | void OutputDipoleAngularCorrelation_Value( ofstream * const file, DipoleAngularCorrelationMap::const_iterator &runner ); | 
|---|
| 81 | void OutputDipoleCorrelation_Header( ofstream * const file ); | 
|---|
| 82 | void OutputDipoleCorrelation_Value( ofstream * const file, DipoleCorrelationMap::const_iterator &runner ); | 
|---|
| 83 | void OutputPairCorrelation_Header( ofstream * const file ); | 
|---|
| 84 | void OutputPairCorrelation_Value( ofstream * const file, PairCorrelationMap::const_iterator &runner ); | 
|---|
| 85 | void OutputCorrelationToPoint_Header( ofstream * const file ); | 
|---|
| 86 | void OutputCorrelationToPoint_Value( ofstream * const file, CorrelationToPointMap::const_iterator &runner ); | 
|---|
| 87 | void OutputCorrelationToSurface_Header( ofstream * const file ); | 
|---|
| 88 | void OutputCorrelationToSurface_Value( ofstream * const file, CorrelationToSurfaceMap::const_iterator &runner ); | 
|---|
| 89 |  | 
|---|
| 90 | /** Searches for lowest and highest value in a given map of doubles. | 
|---|
| 91 | * \param *map map of doubles to scan | 
|---|
| 92 | * \param &min minimum on return | 
|---|
| 93 | * \param &max maximum on return | 
|---|
| 94 | */ | 
|---|
| 95 | template <typename T> void GetMinMax ( T *map, double &min, double &max) | 
|---|
| 96 | { | 
|---|
| 97 | min = 0.; | 
|---|
| 98 | max = 0.; | 
|---|
| 99 | bool FirstMinFound = false; | 
|---|
| 100 | bool FirstMaxFound = false; | 
|---|
| 101 |  | 
|---|
| 102 | if (map == NULL) { | 
|---|
| 103 | ELOG(0, "Nothing to min/max, map is NULL!"); | 
|---|
| 104 | performCriticalExit(); | 
|---|
| 105 | return; | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | for (typename T::iterator runner = map->begin(); runner != map->end(); ++runner) { | 
|---|
| 109 | if ((min > runner->first) || (!FirstMinFound)) { | 
|---|
| 110 | min = runner->first; | 
|---|
| 111 | FirstMinFound = true; | 
|---|
| 112 | } | 
|---|
| 113 | if ((max < runner->first) || (!FirstMaxFound)) { | 
|---|
| 114 | max = runner->first; | 
|---|
| 115 | FirstMaxFound = true; | 
|---|
| 116 | } | 
|---|
| 117 | } | 
|---|
| 118 | }; | 
|---|
| 119 |  | 
|---|
| 120 | /** Puts given correlation data into bins of given size (histogramming). | 
|---|
| 121 | * Note that BinStart and BinEnd are desired to fill the complete range, even where Bins are zero. If this is | 
|---|
| 122 | * not desired, give equal BinStart and BinEnd and the map will contain only Bins where the count is one or greater. If a | 
|---|
| 123 | * certain start value is desired, give BinStart and a BinEnd that is smaller than BinStart, then only BinEnd will be | 
|---|
| 124 | * calculated automatically, i.e. BinStart = 0. and BinEnd = -1. . | 
|---|
| 125 | * Also note that the range is given inclusive, i.e. [ BinStart, BinEnd ]. | 
|---|
| 126 | * \param *map map of doubles to count | 
|---|
| 127 | * \param BinWidth desired width of the binds | 
|---|
| 128 | * \param BinStart first bin | 
|---|
| 129 | * \param BinEnd last bin | 
|---|
| 130 | * \return Map of doubles (the bins) with counts as values | 
|---|
| 131 | */ | 
|---|
| 132 | template <typename T> BinPairMap *BinData ( T *map, const double BinWidth, const double BinStart, const double BinEnd ) | 
|---|
| 133 | { | 
|---|
| 134 | BinPairMap *outmap = new BinPairMap; | 
|---|
| 135 | int bin = 0; | 
|---|
| 136 | double start = 0.; | 
|---|
| 137 | double end = 0.; | 
|---|
| 138 | pair <BinPairMap::iterator, bool > BinPairMapInserter; | 
|---|
| 139 |  | 
|---|
| 140 | if (map == NULL) { | 
|---|
| 141 | ELOG(0, "Nothing to bin, is NULL!"); | 
|---|
| 142 | performCriticalExit(); | 
|---|
| 143 | return outmap; | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | if (BinStart == BinEnd) { // if same, find range ourselves | 
|---|
| 147 | GetMinMax( map, start, end); | 
|---|
| 148 | } else if (BinEnd < BinStart) { // if BinEnd smaller, just look for new max | 
|---|
| 149 | GetMinMax( map, start, end); | 
|---|
| 150 | start = BinStart; | 
|---|
| 151 | } else { // else take both values | 
|---|
| 152 | start = BinStart; | 
|---|
| 153 | end = BinEnd; | 
|---|
| 154 | } | 
|---|
| 155 | // limit precision of start and end bin to 6 digits | 
|---|
| 156 | const double precision = 1e-6/BinWidth; | 
|---|
| 157 | end = precision*floor(end/precision); | 
|---|
| 158 | start = precision*floor(start/precision); | 
|---|
| 159 | for (int runner = 0; runner <= ceil((end-start)/BinWidth); runner++) | 
|---|
| 160 | outmap->insert( pair<double, int> ((double)runner*BinWidth+start, 0) ); | 
|---|
| 161 |  | 
|---|
| 162 | for (typename T::iterator runner = map->begin(); runner != map->end(); ++runner) { | 
|---|
| 163 | bin = GetBin (runner->first, BinWidth, start); | 
|---|
| 164 | BinPairMapInserter = outmap->insert ( pair<double, int> ((double)bin*BinWidth+start, 1) ); | 
|---|
| 165 | if (!BinPairMapInserter.second) {  // bin already present, increase | 
|---|
| 166 | BinPairMapInserter.first->second += 1; | 
|---|
| 167 | } else { // bin not present, then remove again | 
|---|
| 168 | outmap->erase(BinPairMapInserter.first); | 
|---|
| 169 | } | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | return outmap; | 
|---|
| 173 | }; | 
|---|
| 174 |  | 
|---|
| 175 | /** Prints correlation map of template type to file. | 
|---|
| 176 | * \param *file file to write to | 
|---|
| 177 | * \param *map map to write | 
|---|
| 178 | * \param (*header) pointer to function that adds specific part to header | 
|---|
| 179 | * \param (*value) pointer to function that prints value from given iterator | 
|---|
| 180 | */ | 
|---|
| 181 | template <typename T> | 
|---|
| 182 | void OutputCorrelationMap( | 
|---|
| 183 | ofstream * const file, | 
|---|
| 184 | const T * const map, | 
|---|
| 185 | void (*header)( ofstream * const file), | 
|---|
| 186 | void (*value)( ofstream * const file, typename T::const_iterator &runner) | 
|---|
| 187 | ) | 
|---|
| 188 | { | 
|---|
| 189 | Info FunctionInfo(__func__); | 
|---|
| 190 | *file << "BinStart\tBinCenter\tBinEnd"; | 
|---|
| 191 | header(file); | 
|---|
| 192 | *file << endl; | 
|---|
| 193 | typename T::const_iterator advancer = map->begin(); | 
|---|
| 194 | typename T::const_iterator runner = map->begin(); | 
|---|
| 195 | if (advancer != map->end()) | 
|---|
| 196 | advancer++; | 
|---|
| 197 | for ( ;(advancer != map->end()) && (runner != map->end()); ++advancer, ++runner) { | 
|---|
| 198 | *file << setprecision(8) | 
|---|
| 199 | << runner->first << "\t" | 
|---|
| 200 | << (advancer->first + runner->first)/2. << "\t" | 
|---|
| 201 | << advancer->first << "\t"; | 
|---|
| 202 | value(file, runner); | 
|---|
| 203 | *file << endl; | 
|---|
| 204 | } | 
|---|
| 205 | if(runner != map->end()) { | 
|---|
| 206 | *file << setprecision(8) << runner->first << "\t0\t0\t"; | 
|---|
| 207 | value(file, runner); | 
|---|
| 208 | *file << endl; | 
|---|
| 209 | } | 
|---|
| 210 | }; | 
|---|
| 211 |  | 
|---|
| 212 |  | 
|---|
| 213 | #endif /* ANALYSIS_HPP_ */ | 
|---|