BornAgain  1.19.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 reflection and scattering
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/Utils/Algorithms.h"
17 #include <iomanip>
18 #include <limits>
19 
20 FixedBinAxis::FixedBinAxis(const std::string& name, size_t nbins, double start, double end)
21  : IAxis(name), m_nbins(nbins), m_start(start), m_end(end)
22 {
23 }
24 
26 {
28  return result;
29 }
30 
31 double FixedBinAxis::operator[](size_t index) const
32 {
33  if (index >= m_nbins)
34  throw std::runtime_error("FixedBinAxis::operator[] -> Error. Wrong index.");
35 
36  double step = (m_end - m_start) / m_nbins;
37  return m_start + (index + 0.5) * step;
38 }
39 
40 Bin1D FixedBinAxis::bin(size_t index) const
41 {
42  if (index >= m_nbins)
43  throw std::runtime_error("FixedBinAxis::bin() -> Error. Wrong index.");
44 
45  double step = (m_end - m_start) / m_nbins;
46  Bin1D result(m_start + step * index, m_start + step * (index + 1));
47  return result;
48 }
49 
50 size_t FixedBinAxis::findClosestIndex(double value) const
51 {
52  if (value < lowerBound()) {
53  return 0;
54  } else if (value >= upperBound()) {
55  return m_nbins - 1;
56  }
57 
58  double step = (m_end - m_start) / m_nbins;
59  return int((value - m_start) / step);
60 }
61 
62 std::vector<double> FixedBinAxis::binCenters() const
63 {
64  std::vector<double> result;
65  result.resize(size(), 0.0);
66  for (size_t i = 0; i < size(); ++i) {
67  result[i] = bin(i).center();
68  }
69  return result;
70 }
71 
72 std::vector<double> FixedBinAxis::binBoundaries() const
73 {
74  std::vector<double> result;
75  result.resize(size() + 1, 0.0);
76  for (size_t i = 0; i < size(); ++i) {
77  result[i] = bin(i).m_lower;
78  }
79  result[size()] = bin(size() - 1).m_upper;
80  return result;
81 }
82 
83 FixedBinAxis* FixedBinAxis::createClippedAxis(double left, double right) const
84 {
85  if (left >= right)
86  throw std::runtime_error("FixedBinAxis::createClippedAxis() -> Error. "
87  "'left' should be smaller than 'right'");
88 
89  if (left < lowerBound())
90  left = bin(0).center();
91  if (right >= upperBound())
92  right = bin(size() - 1).center();
93 
94  size_t nbin1 = findClosestIndex(left);
95  size_t nbin2 = findClosestIndex(right);
96 
97  return new FixedBinAxis(getName(), nbin2 - nbin1 + 1, bin(nbin1).m_lower, bin(nbin2).m_upper);
98 }
99 
100 void FixedBinAxis::print(std::ostream& ostr) const
101 {
102  ostr << "FixedBinAxis(\"" << getName() << "\", " << size() << ", "
103  << std::setprecision(std::numeric_limits<double>::digits10 + 2) << lowerBound() << ", "
104  << upperBound() << ")";
105 }
106 
107 bool FixedBinAxis::equals(const IAxis& other) const
108 {
109  if (!IAxis::equals(other))
110  return false;
111  if (const FixedBinAxis* otherAxis = dynamic_cast<const FixedBinAxis*>(&other)) {
112  if (size() != otherAxis->size())
113  return false;
114  if (!algo::almostEqual(m_start, otherAxis->m_start))
115  return false;
116  if (!algo::almostEqual(m_end, otherAxis->m_end))
117  return false;
118  return true;
119  }
120  return false;
121 }
Defines and implements namespace algo with some algorithms.
Defines class FixedBinAxis.
Axis with fixed bin size.
Definition: FixedBinAxis.h:23
double m_start
Definition: FixedBinAxis.h:60
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
void print(std::ostream &ostr) const
std::vector< double > binBoundaries() const
Bin1D bin(size_t index) const
retrieve a 1d bin for the given index
FixedBinAxis * clone() const
clone function
double lowerBound() const
Returns value of first point of axis.
Definition: FixedBinAxis.h:41
double m_end
Definition: FixedBinAxis.h:61
double upperBound() const
Returns value of last point of axis.
Definition: FixedBinAxis.h:42
std::vector< double > binCenters() const
size_t m_nbins
Definition: FixedBinAxis.h:59
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:35
Interface for one-dimensional axes.
Definition: IAxis.h:25
virtual bool equals(const IAxis &other) const
Definition: IAxis.cpp:17
std::string getName() const
retrieve the label of the axis
Definition: IAxis.h:40
QString const & name(EShape k)
Definition: particles.cpp:21
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Definition: Algorithms.h:34
Definition: Bin.h:20
double center() const
Definition: Bin.h:25
double m_upper
upper bound of the bin
Definition: Bin.h:24
double m_lower
lower bound of the bin
Definition: Bin.h:23