OpenMPCD
Histogram.cpp
1 #include <OpenMPCD/Histogram.hpp>
2 
5 
6 #include <fstream>
7 #include <limits>
8 
9 OpenMPCD::Histogram::Histogram(const std::string& name, const Configuration& conf)
10  : underflows(0), overflows(0)
11 {
12  const unsigned int binCount = conf.read<unsigned int>("instrumentation."+name+".binCount");
13  bins.resize(binCount, 0);
14 
15  conf.read("instrumentation."+name+".low", &lowEnd);
16  conf.read("instrumentation."+name+".high", &highEnd);
17 
18  binSize = (highEnd-lowEnd)/binCount;
19 }
20 
22 {
23  if(val<lowEnd)
24  {
25  ++underflows;
26  return;
27  }
28 
29  if(val>=highEnd)
30  {
31  ++overflows;
32  return;
33  }
34 
35  const unsigned int binID=static_cast<unsigned int>((val-lowEnd)/binSize);
36 
37  #ifdef OPENMPCD_DEBUG
38  if(binID>=bins.size())
39  OPENMPCD_THROW(OutOfBoundsException, "Unexpected");
40  #endif
41 
42  ++bins[binID];
43 }
44 
46 {
47  FP sum=0;
48 
49  for(Container::size_type i=0; i<bins.size(); ++i)
50  sum+=bins[i];
51 
52  return sum*binSize;
53 }
54 
56 {
57  if(binPoint<0 || binPoint>1)
59 
60  Graph ret;
61 
62  const FP integral=getIntegral();
63 
64  for(Container::size_type i=0; i<bins.size(); ++i)
65  {
66  const FP binPosition=lowEnd+i*binSize+binPoint*binSize;
67  const FP binValue=bins[i]/integral;
68 
69  ret.addPoint(binPosition, binValue);
70  }
71 
72  return ret;
73 }
74 
75 void OpenMPCD::Histogram::save(const std::string& filename, const FP binPoint) const
76 {
78 
79  std::ofstream file(filename.c_str(), std::ios::trunc);
80  file.precision(std::numeric_limits<FP>::digits10 + 2);
81 
82  file<<"#underflows = "<<underflows<<"\n";
83  file<<"#overflows =" <<overflows <<"\n";
84 
85  for(Container::size_type i=0; i<bins.size(); ++i)
86  {
87  const FP binPosition=lowEnd+i*binSize+binPoint*binSize;
88  const FP binValue=bins[i];
89 
90  file<<binPosition<<"\t"<<binValue<<"\n";
91  }
92 }
OpenMPCD::Histogram::getIntegral
FP getIntegral() const
Returns the integral of the histogram.
Definition: Histogram.cpp:45
OpenMPCD::Configuration
Represents the configuration of the simulation.
Definition: Configuration.hpp:28
Exceptions.hpp
OPENMPCD_THROW
#define OPENMPCD_THROW(ExceptionType, message)
Throws the given ExceptionType, passing the given message along with file and line number information...
Definition: Exceptions.hpp:22
Histogram.hpp
FilesystemUtilities.hpp
OpenMPCD::Configuration::read
void read(const std::pair< const char *, ValueType * >(&settingsAndValues)[settingsCount]) const
Reads the specified settings from the given configuration file and stores them in the given location.
Definition: Configuration.hpp:523
OpenMPCD::Histogram::getNormalizedGraph
const Graph getNormalizedGraph(const FP binPoint=0.5) const
Returns the graph corresponding to this histogram, with the area normalized to 1.
Definition: Histogram.cpp:55
OpenMPCD::FilesystemUtilities::ensureParentDirectory
static void ensureParentDirectory(const std::string &path)
Ensures that the parent directory of the path given exists.
Definition: FilesystemUtilities.cpp:18
OpenMPCD::Histogram::save
void save(const std::string &filename, const FP binPoint=0.5) const
Saves the histogram at the given path.
Definition: Histogram.cpp:75
OpenMPCD::FP
double FP
Default floating point type.
Definition: Types.hpp:13
OpenMPCD::Histogram::fill
void fill(const FP val)
Adds an entry to the histogram.
Definition: Histogram.cpp:21
OpenMPCD::Histogram::Histogram
Histogram(const std::string &name, const Configuration &conf)
The constructor.
Definition: Histogram.cpp:9
OpenMPCD::OutOfBoundsException
Exception for out-of-bounds access.
Definition: Exceptions.hpp:112
OpenMPCD::Graph::addPoint
void addPoint(const FP x, const FP y)
Adds a data point.
Definition: Graph.hpp:30
OpenMPCD::Graph
Represents a 2D graph.
Definition: Graph.hpp:19
OpenMPCD::InvalidArgumentException
Invalid argument exception.
Definition: Exceptions.hpp:128