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