source: molecuilder/src/analysis_correlation.hpp@ 57cfb7

Last change on this file since 57cfb7 was 57cfb7, checked in by Frederik Heber <heber@…>, 16 years ago

Pair correlation analysis added.

Unit tests are working fine.

  • Property mode set to 100644
File size: 3.7 KB
Line 
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
11using 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
31class BoundaryTriangleSet;
32class element;
33class LinkedCell;
34class molecule;
35class Tesselation;
36class Vector;
37
38/********************************************** definitions *********************************/
39
40typedef multimap<double, pair<atom *, atom *> > PairCorrelationMap;
41typedef multimap<double, pair<atom *, Vector *> > CorrelationToPointMap;
42typedef multimap<double, pair<atom *, BoundaryTriangleSet *> > CorrelationToSurfaceMap;
43typedef map<double, int> BinPairMap;
44
45/********************************************** declarations *******************************/
46
47PairCorrelationMap *PairCorrelation( ofstream *out, molecule *mol, element *type1, element *type2 );
48CorrelationToPointMap *CorrelationToPoint( ofstream *out, molecule *mol, element *type, Vector *point );
49CorrelationToSurfaceMap *CorrelationToSurface( ofstream *out, molecule *mol, element *type, Tesselation *Surface, LinkedCell *LC );
50double GetBin ( double value, double BinWidth, double BinStart );
51void OutputCorrelation( ofstream *file, BinPairMap *map );
52
53
54/** Searches for lowest and highest value in a given map of doubles.
55 * \param *map map of doubles to scan
56 * \param &min minimum on return
57 * \param &max maximum on return
58 */
59template <typename T> void GetMinMax ( T *map, double &min, double &max)
60{
61 min = 0.;
62 max = 0.;
63 bool FirstMinFound = false;
64 bool FirstMaxFound = false;
65
66 for (typename T::iterator runner = map->begin(); runner != map->end(); ++runner) {
67 if ((min > runner->first) || (!FirstMinFound)) {
68 min = runner->first;
69 FirstMinFound = true;
70 }
71 if ((max < runner->first) || (!FirstMaxFound)) {
72 max = runner->first;
73 FirstMaxFound = true;
74 }
75 }
76};
77
78/** Puts given correlation data into bins of given size (histogramming).
79 * Note that BinStart and BinEnd are desired to fill the complete range, even where Bins are zero. If this is
80 * not desired, give equal BinStart and BinEnd and the map will contain only Bins where the count is one or greater.
81 * Also note that the range is given inclusive, i.e. [ BinStart, BinEnd ].
82 * \param *out output stream for debugging
83 * \param *map map of doubles to count
84 * \param BinWidth desired width of the binds
85 * \param BinStart first bin
86 * \param BinEnd last bin
87 * \return Map of doubles (the bins) with counts as values
88 */
89template <typename T> BinPairMap *BinData ( ofstream *out, T *map, double BinWidth, double BinStart, double BinEnd )
90{
91 BinPairMap *outmap = new BinPairMap;
92 double bin = 0.;
93 double start = 0.;
94 double end = 0.;
95 pair <BinPairMap::iterator, bool > BinPairMapInserter;
96
97 if (BinStart == BinEnd) { // if same, find range ourselves
98 GetMinMax( map, start, end);
99 } else { // if not, initialise range to zero
100 start = BinStart;
101 end = BinEnd;
102 for (double runner = start; runner <= end; runner += BinWidth)
103 outmap->insert( pair<double, int> (runner, 0.) );
104 }
105
106 for (typename T::iterator runner = map->begin(); runner != map->end(); ++runner) {
107 bin = GetBin (runner->first, BinWidth, start);
108 BinPairMapInserter = outmap->insert ( pair<double, int> (bin, 1) );
109 if (!BinPairMapInserter.second) { // bin already present, increase
110 BinPairMapInserter.first->second += 1;
111 }
112 }
113
114 return outmap;
115};
116
117#endif /* ANALYSIS_HPP_ */
Note: See TracBrowser for help on using the repository browser.