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