| [c4d4df] | 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 | 
 | 
|---|
| [146cff2] | 20 | #include <cmath>
 | 
|---|
| [92e5cb] | 21 | #include <iomanip>
 | 
|---|
| [c4d4df] | 22 | #include <fstream>
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 | // STL headers
 | 
|---|
 | 25 | #include <map>
 | 
|---|
| [e65de8] | 26 | #include <vector>
 | 
|---|
| [c4d4df] | 27 | 
 | 
|---|
| [e4fe8d] | 28 | #include "Helpers/defs.hpp"
 | 
|---|
| [c4d4df] | 29 | 
 | 
|---|
| [6f0841] | 30 | #include "Atom/atom.hpp"
 | 
|---|
| [92e5cb] | 31 | #include "CodePatterns/Info.hpp"
 | 
|---|
 | 32 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [1cc661] | 33 | #include "CodePatterns/Range.hpp"
 | 
|---|
| [ad011c] | 34 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| [255829] | 35 | #include "Helpers/helpers.hpp"
 | 
|---|
| [c4d4df] | 36 | 
 | 
|---|
 | 37 | /****************************************** forward declarations *****************************/
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | class BoundaryTriangleSet;
 | 
|---|
| [ea430a] | 40 | class Formula;
 | 
|---|
| [c4d4df] | 41 | class element;
 | 
|---|
 | 42 | class LinkedCell;
 | 
|---|
 | 43 | class Tesselation;
 | 
|---|
 | 44 | class Vector;
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 | /********************************************** definitions *********************************/
 | 
|---|
 | 47 | 
 | 
|---|
| [59fff1] | 48 | typedef multimap<double, pair<const atom *, const atom *> > PairCorrelationMap;
 | 
|---|
 | 49 | typedef multimap<double, const atom * > DipoleAngularCorrelationMap;
 | 
|---|
 | 50 | typedef multimap<double, pair<const molecule *, const molecule *> > DipoleCorrelationMap;
 | 
|---|
 | 51 | typedef multimap<double, pair<const atom *, const Vector *> > CorrelationToPointMap;
 | 
|---|
 | 52 | typedef multimap<double, pair<const atom *, BoundaryTriangleSet *> > CorrelationToSurfaceMap;
 | 
|---|
| [c4d4df] | 53 | typedef map<double, int> BinPairMap;
 | 
|---|
 | 54 | 
 | 
|---|
| [99b87a] | 55 | enum ResetWorldTime {
 | 
|---|
 | 56 |   DontResetTime,
 | 
|---|
 | 57 |   DoResetTime
 | 
|---|
 | 58 | };
 | 
|---|
 | 59 | 
 | 
|---|
| [c4d4df] | 60 | /********************************************** declarations *******************************/
 | 
|---|
 | 61 | 
 | 
|---|
| [e65878] | 62 | range<size_t> getMaximumTrajectoryBounds(const std::vector<atom *> &atoms);
 | 
|---|
 | 63 | std::map<atomId_t, Vector> CalculateZeroAngularDipole(const std::vector<molecule *> &molecules);
 | 
|---|
| [1cc661] | 64 | 
 | 
|---|
| [e65878] | 65 | DipoleAngularCorrelationMap *DipoleAngularCorrelation(const Formula &DipoleFormula, const size_t timestep, const std::map<atomId_t, Vector> &ZeroVector, const enum ResetWorldTime DoTimeReset = DontResetTime);
 | 
|---|
| [208237b] | 66 | DipoleCorrelationMap *DipoleCorrelation(std::vector<molecule *> &molecules);
 | 
|---|
| [e5c0a1] | 67 | PairCorrelationMap *PairCorrelation(std::vector<molecule *> &molecules, const std::vector<const element *> &elements);
 | 
|---|
 | 68 | CorrelationToPointMap *CorrelationToPoint(std::vector<molecule *> &molecules, const std::vector<const element *> &elements, const Vector *point );
 | 
|---|
 | 69 | CorrelationToSurfaceMap *CorrelationToSurface(std::vector<molecule *> &molecules, const std::vector<const element *> &elements, const Tesselation * const Surface, const LinkedCell *LC );
 | 
|---|
 | 70 | PairCorrelationMap *PeriodicPairCorrelation(std::vector<molecule *> &molecules, const std::vector<const element *> &elements, const int ranges[NDIM] );
 | 
|---|
 | 71 | CorrelationToPointMap *PeriodicCorrelationToPoint(std::vector<molecule *> &molecules, const std::vector<const element *> &elements, const Vector *point, const int ranges[NDIM] );
 | 
|---|
 | 72 | CorrelationToSurfaceMap *PeriodicCorrelationToSurface(std::vector<molecule *> &molecules, const std::vector<const element *> &elements, const Tesselation * const Surface, const LinkedCell *LC, const int ranges[NDIM] );
 | 
|---|
| [bd61b41] | 73 | int GetBin ( const double value, const double BinWidth, const double BinStart );
 | 
|---|
| [92e5cb] | 74 | void OutputCorrelation_Header( ofstream * const file );
 | 
|---|
 | 75 | void OutputCorrelation_Value( ofstream * const file, BinPairMap::const_iterator &runner );
 | 
|---|
 | 76 | void OutputDipoleAngularCorrelation_Header( ofstream * const file );
 | 
|---|
 | 77 | void OutputDipoleAngularCorrelation_Value( ofstream * const file, DipoleAngularCorrelationMap::const_iterator &runner );
 | 
|---|
| [208237b] | 78 | void OutputDipoleCorrelation_Header( ofstream * const file );
 | 
|---|
 | 79 | void OutputDipoleCorrelation_Value( ofstream * const file, DipoleCorrelationMap::const_iterator &runner );
 | 
|---|
| [92e5cb] | 80 | void OutputPairCorrelation_Header( ofstream * const file );
 | 
|---|
 | 81 | void OutputPairCorrelation_Value( ofstream * const file, PairCorrelationMap::const_iterator &runner );
 | 
|---|
 | 82 | void OutputCorrelationToPoint_Header( ofstream * const file );
 | 
|---|
 | 83 | void OutputCorrelationToPoint_Value( ofstream * const file, CorrelationToPointMap::const_iterator &runner );
 | 
|---|
 | 84 | void OutputCorrelationToSurface_Header( ofstream * const file );
 | 
|---|
 | 85 | void OutputCorrelationToSurface_Value( ofstream * const file, CorrelationToSurfaceMap::const_iterator &runner );
 | 
|---|
| [c4d4df] | 86 | 
 | 
|---|
 | 87 | /** Searches for lowest and highest value in a given map of doubles.
 | 
|---|
 | 88 |  * \param *map map of doubles to scan
 | 
|---|
 | 89 |  * \param &min minimum on return
 | 
|---|
 | 90 |  * \param &max maximum on return
 | 
|---|
 | 91 |  */
 | 
|---|
 | 92 | template <typename T> void GetMinMax ( T *map, double &min, double &max)
 | 
|---|
 | 93 | {
 | 
|---|
 | 94 |   min = 0.;
 | 
|---|
 | 95 |   max = 0.;
 | 
|---|
 | 96 |   bool FirstMinFound = false;
 | 
|---|
 | 97 |   bool FirstMaxFound = false;
 | 
|---|
 | 98 | 
 | 
|---|
| [f74d08] | 99 |   if (map == NULL) {
 | 
|---|
| [47d041] | 100 |     ELOG(0, "Nothing to min/max, map is NULL!");
 | 
|---|
| [e359a8] | 101 |     performCriticalExit();
 | 
|---|
| [f74d08] | 102 |     return;
 | 
|---|
 | 103 |   }
 | 
|---|
 | 104 | 
 | 
|---|
| [c4d4df] | 105 |   for (typename T::iterator runner = map->begin(); runner != map->end(); ++runner) {
 | 
|---|
 | 106 |     if ((min > runner->first) || (!FirstMinFound)) {
 | 
|---|
 | 107 |       min = runner->first;
 | 
|---|
 | 108 |       FirstMinFound = true;
 | 
|---|
 | 109 |     }
 | 
|---|
 | 110 |     if ((max < runner->first) || (!FirstMaxFound)) {
 | 
|---|
 | 111 |       max = runner->first;
 | 
|---|
 | 112 |       FirstMaxFound = true;
 | 
|---|
 | 113 |     }
 | 
|---|
 | 114 |   }
 | 
|---|
 | 115 | };
 | 
|---|
 | 116 | 
 | 
|---|
 | 117 | /** Puts given correlation data into bins of given size (histogramming).
 | 
|---|
 | 118 |  * Note that BinStart and BinEnd are desired to fill the complete range, even where Bins are zero. If this is
 | 
|---|
| [790807] | 119 |  * not desired, give equal BinStart and BinEnd and the map will contain only Bins where the count is one or greater. If a
 | 
|---|
 | 120 |  * certain start value is desired, give BinStart and a BinEnd that is smaller than BinStart, then only BinEnd will be
 | 
|---|
 | 121 |  * calculated automatically, i.e. BinStart = 0. and BinEnd = -1. .
 | 
|---|
| [c4d4df] | 122 |  * Also note that the range is given inclusive, i.e. [ BinStart, BinEnd ].
 | 
|---|
 | 123 |  * \param *map map of doubles to count
 | 
|---|
 | 124 |  * \param BinWidth desired width of the binds
 | 
|---|
 | 125 |  * \param BinStart first bin
 | 
|---|
 | 126 |  * \param BinEnd last bin
 | 
|---|
 | 127 |  * \return Map of doubles (the bins) with counts as values
 | 
|---|
 | 128 |  */
 | 
|---|
| [e138de] | 129 | template <typename T> BinPairMap *BinData ( T *map, const double BinWidth, const double BinStart, const double BinEnd )
 | 
|---|
| [c4d4df] | 130 | {
 | 
|---|
 | 131 |   BinPairMap *outmap = new BinPairMap;
 | 
|---|
| [bd61b41] | 132 |   int bin = 0;
 | 
|---|
| [c4d4df] | 133 |   double start = 0.;
 | 
|---|
 | 134 |   double end = 0.;
 | 
|---|
 | 135 |   pair <BinPairMap::iterator, bool > BinPairMapInserter;
 | 
|---|
 | 136 | 
 | 
|---|
| [f74d08] | 137 |   if (map == NULL) {
 | 
|---|
| [47d041] | 138 |     ELOG(0, "Nothing to bin, is NULL!");
 | 
|---|
| [e359a8] | 139 |     performCriticalExit();
 | 
|---|
| [f74d08] | 140 |     return outmap;
 | 
|---|
 | 141 |   }
 | 
|---|
 | 142 | 
 | 
|---|
| [c4d4df] | 143 |   if (BinStart == BinEnd) { // if same, find range ourselves
 | 
|---|
 | 144 |     GetMinMax( map, start, end);
 | 
|---|
| [790807] | 145 |   } else if (BinEnd < BinStart) { // if BinEnd smaller, just look for new max
 | 
|---|
 | 146 |     GetMinMax( map, start, end);
 | 
|---|
 | 147 |     start = BinStart;
 | 
|---|
 | 148 |   } else { // else take both values
 | 
|---|
| [c4d4df] | 149 |     start = BinStart;
 | 
|---|
 | 150 |     end = BinEnd;
 | 
|---|
 | 151 |   }
 | 
|---|
| [85da4e] | 152 |   for (int runner = 0; runner <= ceil((end-start)/BinWidth); runner++)
 | 
|---|
 | 153 |     outmap->insert( pair<double, int> ((double)runner*BinWidth+start, 0) );
 | 
|---|
| [c4d4df] | 154 | 
 | 
|---|
 | 155 |   for (typename T::iterator runner = map->begin(); runner != map->end(); ++runner) {
 | 
|---|
 | 156 |     bin = GetBin (runner->first, BinWidth, start);
 | 
|---|
| [bd61b41] | 157 |     BinPairMapInserter = outmap->insert ( pair<double, int> ((double)bin*BinWidth+start, 1) );
 | 
|---|
| [c4d4df] | 158 |     if (!BinPairMapInserter.second) {  // bin already present, increase
 | 
|---|
 | 159 |       BinPairMapInserter.first->second += 1;
 | 
|---|
 | 160 |     }
 | 
|---|
 | 161 |   }
 | 
|---|
 | 162 | 
 | 
|---|
 | 163 |   return outmap;
 | 
|---|
 | 164 | };
 | 
|---|
 | 165 | 
 | 
|---|
| [92e5cb] | 166 | /** Prints correlation map of template type to file.
 | 
|---|
 | 167 |  * \param *file file to write to
 | 
|---|
 | 168 |  * \param *map map to write
 | 
|---|
 | 169 |  * \param (*header) pointer to function that adds specific part to header
 | 
|---|
 | 170 |  * \param (*value) pointer to function that prints value from given iterator
 | 
|---|
 | 171 |  */
 | 
|---|
 | 172 | template <typename T>
 | 
|---|
 | 173 | void OutputCorrelationMap(
 | 
|---|
 | 174 |     ofstream * const file,
 | 
|---|
 | 175 |     const T * const map,
 | 
|---|
 | 176 |     void (*header)( ofstream * const file),
 | 
|---|
 | 177 |     void (*value)( ofstream * const file, typename T::const_iterator &runner)
 | 
|---|
 | 178 |     )
 | 
|---|
 | 179 | {
 | 
|---|
 | 180 |   Info FunctionInfo(__func__);
 | 
|---|
 | 181 |   *file << "BinStart\tBinCenter\tBinEnd";
 | 
|---|
 | 182 |   header(file);
 | 
|---|
 | 183 |   *file << endl;
 | 
|---|
 | 184 |   typename T::const_iterator advancer = map->begin();
 | 
|---|
 | 185 |   typename T::const_iterator runner = map->begin();
 | 
|---|
 | 186 |   if (advancer != map->end())
 | 
|---|
 | 187 |     advancer++;
 | 
|---|
 | 188 |   for ( ;(advancer != map->end()) && (runner != map->end()); ++advancer, ++runner) {
 | 
|---|
 | 189 |     *file << setprecision(8)
 | 
|---|
 | 190 |         << runner->first << "\t"
 | 
|---|
 | 191 |         << (advancer->first + runner->first)/2. << "\t"
 | 
|---|
 | 192 |         << advancer->first << "\t";
 | 
|---|
 | 193 |     value(file, runner);
 | 
|---|
 | 194 |     *file << endl;
 | 
|---|
 | 195 |   }
 | 
|---|
 | 196 |   if(runner != map->end()) {
 | 
|---|
 | 197 |     *file << setprecision(8) << runner->first << "\t0\t0\t";
 | 
|---|
 | 198 |     value(file, runner);
 | 
|---|
 | 199 |     *file << endl;
 | 
|---|
 | 200 |   }
 | 
|---|
 | 201 | };
 | 
|---|
 | 202 | 
 | 
|---|
 | 203 | 
 | 
|---|
| [c4d4df] | 204 | #endif /* ANALYSIS_HPP_ */
 | 
|---|