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