| [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 | 
 | 
|---|
 | 20 | #include <fstream>
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | // STL headers
 | 
|---|
 | 23 | #include <map>
 | 
|---|
 | 24 | 
 | 
|---|
 | 25 | #include "defs.hpp"
 | 
|---|
 | 26 | 
 | 
|---|
 | 27 | #include "atom.hpp"
 | 
|---|
 | 28 | 
 | 
|---|
 | 29 | /****************************************** forward declarations *****************************/
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | class BoundaryTriangleSet;
 | 
|---|
 | 32 | class element;
 | 
|---|
 | 33 | class LinkedCell;
 | 
|---|
| [a5551b] | 34 | class MoleculeListClass;
 | 
|---|
| [c4d4df] | 35 | class Tesselation;
 | 
|---|
 | 36 | class Vector;
 | 
|---|
 | 37 | 
 | 
|---|
 | 38 | /********************************************** definitions *********************************/
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | typedef multimap<double, pair<atom *, atom *> > PairCorrelationMap;
 | 
|---|
| [776b64] | 41 | typedef multimap<double, pair<atom *, const Vector *> > CorrelationToPointMap;
 | 
|---|
| [c4d4df] | 42 | typedef multimap<double, pair<atom *, BoundaryTriangleSet *> > CorrelationToSurfaceMap;
 | 
|---|
 | 43 | typedef map<double, int> BinPairMap;
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | /********************************************** declarations *******************************/
 | 
|---|
 | 46 | 
 | 
|---|
| [e138de] | 47 | PairCorrelationMap *PairCorrelation(MoleculeListClass * const &molecules, const element * const type1, const element * const  type2 );
 | 
|---|
 | 48 | CorrelationToPointMap *CorrelationToPoint(MoleculeListClass * const &molecules, const element * const type, const Vector *point );
 | 
|---|
 | 49 | CorrelationToSurfaceMap *CorrelationToSurface(MoleculeListClass * const &molecules, const element * const type, const Tesselation * const Surface, const LinkedCell *LC );
 | 
|---|
 | 50 | PairCorrelationMap *PeriodicPairCorrelation(MoleculeListClass * const &molecules, const element * const type1, const element * const  type2, const int ranges[NDIM] );
 | 
|---|
 | 51 | CorrelationToPointMap *PeriodicCorrelationToPoint(MoleculeListClass * const &molecules, const element * const type, const Vector *point, const int ranges[NDIM] );
 | 
|---|
 | 52 | CorrelationToSurfaceMap *PeriodicCorrelationToSurface(MoleculeListClass * const &molecules, const element * const type, const Tesselation * const Surface, const LinkedCell *LC, const int ranges[NDIM] );
 | 
|---|
| [bd61b41] | 53 | int GetBin ( const double value, const double BinWidth, const double BinStart );
 | 
|---|
| [a5551b] | 54 | void OutputCorrelation( ofstream * const file, const BinPairMap * const map );
 | 
|---|
| [244a84] | 55 | void OutputPairCorrelation( ofstream * const file, const PairCorrelationMap * const map );
 | 
|---|
 | 56 | void OutputCorrelationToPoint( ofstream * const file, const CorrelationToPointMap * const map );
 | 
|---|
 | 57 | void OutputCorrelationToSurface( ofstream * const file, const CorrelationToSurfaceMap * const map );
 | 
|---|
| [c4d4df] | 58 | 
 | 
|---|
 | 59 | 
 | 
|---|
 | 60 | /** Searches for lowest and highest value in a given map of doubles.
 | 
|---|
 | 61 |  * \param *map map of doubles to scan
 | 
|---|
 | 62 |  * \param &min minimum on return
 | 
|---|
 | 63 |  * \param &max maximum on return
 | 
|---|
 | 64 |  */
 | 
|---|
 | 65 | template <typename T> void GetMinMax ( T *map, double &min, double &max)
 | 
|---|
 | 66 | {
 | 
|---|
 | 67 |   min = 0.;
 | 
|---|
 | 68 |   max = 0.;
 | 
|---|
 | 69 |   bool FirstMinFound = false;
 | 
|---|
 | 70 |   bool FirstMaxFound = false;
 | 
|---|
 | 71 | 
 | 
|---|
| [f74d08] | 72 |   if (map == NULL) {
 | 
|---|
| [58ed4a] | 73 |     DoeLog(0) && (eLog()<< Verbose(0) << "Nothing to min/max, map is NULL!" << endl);
 | 
|---|
| [e359a8] | 74 |     performCriticalExit();
 | 
|---|
| [f74d08] | 75 |     return;
 | 
|---|
 | 76 |   }
 | 
|---|
 | 77 | 
 | 
|---|
| [c4d4df] | 78 |   for (typename T::iterator runner = map->begin(); runner != map->end(); ++runner) {
 | 
|---|
 | 79 |     if ((min > runner->first) || (!FirstMinFound)) {
 | 
|---|
 | 80 |       min = runner->first;
 | 
|---|
 | 81 |       FirstMinFound = true;
 | 
|---|
 | 82 |     }
 | 
|---|
 | 83 |     if ((max < runner->first) || (!FirstMaxFound)) {
 | 
|---|
 | 84 |       max = runner->first;
 | 
|---|
 | 85 |       FirstMaxFound = true;
 | 
|---|
 | 86 |     }
 | 
|---|
 | 87 |   }
 | 
|---|
 | 88 | };
 | 
|---|
 | 89 | 
 | 
|---|
 | 90 | /** Puts given correlation data into bins of given size (histogramming).
 | 
|---|
 | 91 |  * Note that BinStart and BinEnd are desired to fill the complete range, even where Bins are zero. If this is
 | 
|---|
| [790807] | 92 |  * not desired, give equal BinStart and BinEnd and the map will contain only Bins where the count is one or greater. If a
 | 
|---|
 | 93 |  * certain start value is desired, give BinStart and a BinEnd that is smaller than BinStart, then only BinEnd will be
 | 
|---|
 | 94 |  * calculated automatically, i.e. BinStart = 0. and BinEnd = -1. .
 | 
|---|
| [c4d4df] | 95 |  * Also note that the range is given inclusive, i.e. [ BinStart, BinEnd ].
 | 
|---|
 | 96 |  * \param *map map of doubles to count
 | 
|---|
 | 97 |  * \param BinWidth desired width of the binds
 | 
|---|
 | 98 |  * \param BinStart first bin
 | 
|---|
 | 99 |  * \param BinEnd last bin
 | 
|---|
 | 100 |  * \return Map of doubles (the bins) with counts as values
 | 
|---|
 | 101 |  */
 | 
|---|
| [e138de] | 102 | template <typename T> BinPairMap *BinData ( T *map, const double BinWidth, const double BinStart, const double BinEnd )
 | 
|---|
| [c4d4df] | 103 | {
 | 
|---|
 | 104 |   BinPairMap *outmap = new BinPairMap;
 | 
|---|
| [bd61b41] | 105 |   int bin = 0;
 | 
|---|
| [c4d4df] | 106 |   double start = 0.;
 | 
|---|
 | 107 |   double end = 0.;
 | 
|---|
 | 108 |   pair <BinPairMap::iterator, bool > BinPairMapInserter;
 | 
|---|
 | 109 | 
 | 
|---|
| [f74d08] | 110 |   if (map == NULL) {
 | 
|---|
| [58ed4a] | 111 |     DoeLog(0) && (eLog()<< Verbose(0) << "Nothing to bin, is NULL!" << endl);
 | 
|---|
| [e359a8] | 112 |     performCriticalExit();
 | 
|---|
| [f74d08] | 113 |     return outmap;
 | 
|---|
 | 114 |   }
 | 
|---|
 | 115 | 
 | 
|---|
| [c4d4df] | 116 |   if (BinStart == BinEnd) { // if same, find range ourselves
 | 
|---|
 | 117 |     GetMinMax( map, start, end);
 | 
|---|
| [790807] | 118 |   } else if (BinEnd < BinStart) { // if BinEnd smaller, just look for new max
 | 
|---|
 | 119 |     GetMinMax( map, start, end);
 | 
|---|
 | 120 |     start = BinStart;
 | 
|---|
 | 121 |   } else { // else take both values
 | 
|---|
| [c4d4df] | 122 |     start = BinStart;
 | 
|---|
 | 123 |     end = BinEnd;
 | 
|---|
 | 124 |   }
 | 
|---|
| [85da4e] | 125 |   for (int runner = 0; runner <= ceil((end-start)/BinWidth); runner++)
 | 
|---|
 | 126 |     outmap->insert( pair<double, int> ((double)runner*BinWidth+start, 0) );
 | 
|---|
| [c4d4df] | 127 | 
 | 
|---|
 | 128 |   for (typename T::iterator runner = map->begin(); runner != map->end(); ++runner) {
 | 
|---|
 | 129 |     bin = GetBin (runner->first, BinWidth, start);
 | 
|---|
| [bd61b41] | 130 |     BinPairMapInserter = outmap->insert ( pair<double, int> ((double)bin*BinWidth+start, 1) );
 | 
|---|
| [c4d4df] | 131 |     if (!BinPairMapInserter.second) {  // bin already present, increase
 | 
|---|
 | 132 |       BinPairMapInserter.first->second += 1;
 | 
|---|
 | 133 |     }
 | 
|---|
 | 134 |   }
 | 
|---|
 | 135 | 
 | 
|---|
 | 136 |   return outmap;
 | 
|---|
 | 137 | };
 | 
|---|
 | 138 | 
 | 
|---|
 | 139 | #endif /* ANALYSIS_HPP_ */
 | 
|---|