BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
FixedBinAxis.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Base/Axis/FixedBinAxis.cpp
6 //! @brief Implement class FixedBinAxis.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
15 #include "Base/Axis/FixedBinAxis.h"
16 #include "Base/Types/Exceptions.h"
17 #include "Base/Utils/Algorithms.h"
18 #include "Base/Utils/PyFmt.h"
19 #include <iomanip>
20 #include <limits>
21 
22 FixedBinAxis::FixedBinAxis(const std::string& name, size_t nbins, double start, double end)
23  : IAxis(name), m_nbins(nbins), m_start(start), m_end(end)
24 {
25 }
26 
28 {
30  return result;
31 }
32 
33 double FixedBinAxis::operator[](size_t index) const
34 {
35  if (index >= m_nbins)
36  throw Exceptions::OutOfBoundsException("FixedBinAxis::operator[] -> Error. Wrong index.");
37 
38  double step = (m_end - m_start) / m_nbins;
39  return m_start + (index + 0.5) * step;
40 }
41 
42 Bin1D FixedBinAxis::getBin(size_t index) const
43 {
44  if (index >= m_nbins)
45  throw Exceptions::OutOfBoundsException("FixedBinAxis::getBin() -> Error. Wrong index.");
46 
47  double step = (m_end - m_start) / m_nbins;
48  Bin1D result(m_start + step * index, m_start + step * (index + 1));
49  return result;
50 }
51 
52 size_t FixedBinAxis::findClosestIndex(double value) const
53 {
54  if (value < getMin()) {
55  return 0;
56  } else if (value >= getMax()) {
57  return m_nbins - 1;
58  }
59 
60  double step = (m_end - m_start) / m_nbins;
61  return int((value - m_start) / step);
62 }
63 
64 std::vector<double> FixedBinAxis::getBinCenters() const
65 {
66  std::vector<double> result;
67  result.resize(size(), 0.0);
68  for (size_t i = 0; i < size(); ++i) {
69  result[i] = getBin(i).getMidPoint();
70  }
71  return result;
72 }
73 
74 std::vector<double> FixedBinAxis::getBinBoundaries() const
75 {
76  std::vector<double> result;
77  result.resize(size() + 1, 0.0);
78  for (size_t i = 0; i < size(); ++i) {
79  result[i] = getBin(i).m_lower;
80  }
81  result[size()] = getBin(size() - 1).m_upper;
82  return result;
83 }
84 
85 FixedBinAxis* FixedBinAxis::createClippedAxis(double left, double right) const
86 {
87  if (left >= right)
88  throw Exceptions::LogicErrorException("FixedBinAxis::createClippedAxis() -> Error. "
89  "'left' should be smaller than 'right'");
90 
91  if (left < getMin())
92  left = getBin(0).getMidPoint();
93  if (right >= getMax())
94  right = getBin(size() - 1).getMidPoint();
95 
96  size_t nbin1 = findClosestIndex(left);
97  size_t nbin2 = findClosestIndex(right);
98 
99  return new FixedBinAxis(getName(), nbin2 - nbin1 + 1, getBin(nbin1).m_lower,
100  getBin(nbin2).m_upper);
101 }
102 
103 void FixedBinAxis::print(std::ostream& ostr) const
104 {
105  ostr << "FixedBinAxis(\"" << getName() << "\", " << size() << ", "
106  << std::setprecision(std::numeric_limits<double>::digits10 + 2) << getMin() << ", "
107  << getMax() << ")";
108 }
109 
110 bool FixedBinAxis::equals(const IAxis& other) const
111 {
112  if (!IAxis::equals(other))
113  return false;
114  if (const FixedBinAxis* otherAxis = dynamic_cast<const FixedBinAxis*>(&other)) {
115  if (size() != otherAxis->size())
116  return false;
117  if (!algo::almostEqual(m_start, otherAxis->m_start))
118  return false;
119  if (!algo::almostEqual(m_end, otherAxis->m_end))
120  return false;
121  return true;
122  }
123  return false;
124 }
125 
126 std::string FixedBinAxis::pyString(const std::string& units, size_t) const
127 {
128  std::ostringstream result;
129  result << "ba.FixedBinAxis(" << pyfmt::printString(getName()) << ", " << size() << ", "
130  << pyfmt::printValue(getMin(), units) << ", " << pyfmt::printValue(getMax(), units)
131  << ")";
132  return result.str();
133 }
Defines and implements namespace algo with some algorithms.
Defines many exception classes in namespace Exceptionss.
Defines class FixedBinAxis.
Defines functions in namespace pyfmt.
Axis with fixed bin size.
Definition: FixedBinAxis.h:24
double m_start
Definition: FixedBinAxis.h:63
FixedBinAxis * createClippedAxis(double left, double right) const
Creates a new clipped axis.
virtual bool equals(const IAxis &other) const
double operator[](size_t index) const
indexed accessor retrieves a sample
size_t findClosestIndex(double value) const
find bin index which is best match for given value
double getMax() const
Returns value of last point of axis.
Definition: FixedBinAxis.h:43
void print(std::ostream &ostr) const
FixedBinAxis * clone() const
clone function
std::vector< double > getBinBoundaries() const
double getMin() const
Returns value of first point of axis.
Definition: FixedBinAxis.h:42
std::vector< double > getBinCenters() const
double m_end
Definition: FixedBinAxis.h:64
size_t m_nbins
Definition: FixedBinAxis.h:62
std::string pyString(const std::string &units, size_t) const final
FixedBinAxis(const std::string &name, size_t nbins, double start, double end)
FixedBinAxis constructor.
size_t size() const
retrieve the number of bins
Definition: FixedBinAxis.h:36
Bin1D getBin(size_t index) const
retrieve a 1d bin for the given index
Interface for one-dimensional axes.
Definition: IAxis.h:25
virtual bool equals(const IAxis &other) const
Definition: IAxis.cpp:18
std::string getName() const
retrieve the label of the axis
Definition: IAxis.h:40
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Definition: Algorithms.h:30
std::string printString(const std::string &value)
Definition: PyFmt.cpp:116
std::string printValue(double value, const std::string &units)
Definition: PyFmt.cpp:104
Definition: Bin.h:20
double m_upper
upper bound of the bin
Definition: Bin.h:24
double m_lower
lower bound of the bin
Definition: Bin.h:23
double getMidPoint() const
Definition: Bin.h:25