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