BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
CustomBinAxis.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Base/Axis/CustomBinAxis.cpp
6 //! @brief Implement class CustomBinAxis.
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 
16 #include "Base/Axis/Bin.h"
17 #include "Base/Util/Algorithms.h"
18 #include <cmath>
19 #include <iomanip>
20 #include <limits>
21 
22 CustomBinAxis::CustomBinAxis(const std::string& name, size_t nbins, double start, double end)
23  : VariableBinAxis(name, nbins)
24  , m_start(start)
25  , m_end(end)
26 {
27  if (m_start >= m_end)
28  throw std::runtime_error("CustomBinAxis::CustomBinAxis() -> Error."
29  " start >= end is not allowed.");
30 
31  double start_sin = std::sin(start);
32  double end_sin = std::sin(end);
33  double step = (end_sin - start_sin) / (m_nbins - 1); // m_nbins-1 is intentionally
34 
35  m_bin_centers.resize(m_nbins, 0.0);
36  for (size_t i = 0; i < m_bin_centers.size(); ++i)
37  m_bin_centers[i] = std::asin(start_sin + i * step);
38 
39  std::vector<double> bin_boundaries;
40  bin_boundaries.resize(m_nbins + 1, 0.0);
41  for (size_t i = 0; i < bin_boundaries.size(); ++i)
42  bin_boundaries[i] = std::asin(start_sin - step / 2. + step * i);
43  setBinBoundaries(bin_boundaries);
44 }
45 
47 {
48  return new CustomBinAxis(axisName(), m_nbins, m_start, m_end);
49 }
50 
51 Bin1D CustomBinAxis::bin(size_t index) const
52 {
53  if (index >= m_nbins)
54  throw std::runtime_error("CustomBinAxis::bin() -> Error. Wrong index.");
55 
56  Bin1D result(m_bin_centers[index], m_bin_centers[index]);
57  return result;
58 }
59 
60 std::vector<double> CustomBinAxis::binCenters() const
61 {
62  return m_bin_centers;
63 }
64 
65 void CustomBinAxis::clip(double /* lower */, double /* upper */)
66 {
67  throw std::runtime_error("CustomBinAxis::clip() -> Error."
68  " Not implemented.");
69 }
70 
71 void CustomBinAxis::print(std::ostream& ostr) const
72 {
73  ostr << "CustomBinAxis(\"" << axisName() << "\", " << size() << ", "
74  << std::setprecision(std::numeric_limits<double>::digits10 + 2) << m_start << ", " << m_end
75  << ")";
76 }
77 
78 bool CustomBinAxis::equals(const IAxis& other) const
79 {
80  if (!IAxis::equals(other))
81  return false;
82  if (const auto* otherAxis = dynamic_cast<const CustomBinAxis*>(&other)) {
83  if (size() != otherAxis->size())
84  return false;
85  if (!BaseUtils::algo::almostEqual(m_start, otherAxis->m_start))
86  return false;
87  if (!BaseUtils::algo::almostEqual(m_end, otherAxis->m_end))
88  return false;
89  return true;
90  }
91  return false;
92 }
Defines and implements namespace BaseUtils::algo with some algorithms.
Defines structs Bin1D, Bin1DCVector.
Defines class CustomBinAxis.
Definition: Bin.h:20
Axis with fixed bin size in sin(angle) space used for numerical comparison with IsGisaxs....
Definition: CustomBinAxis.h:24
void clip(double lower, double upper) override
Clips this axis to the given values.
std::vector< double > binCenters() const override
void print(std::ostream &ostr) const override
CustomBinAxis * clone() const override
CustomBinAxis(const std::string &name, size_t nbins, double start, double end)
CustomBinAxis constructor.
std::vector< double > m_bin_centers
Definition: CustomBinAxis.h:48
bool equals(const IAxis &other) const override
Bin1D bin(size_t index) const override
retrieve a 1d bin for the given index
Abstract base class for one-dimensional axes.
Definition: IAxis.h:27
virtual bool equals(const IAxis &other) const
Definition: IAxis.cpp:20
std::string axisName() const
Returns the label of the axis.
Definition: IAxis.h:61
Axis with variable bin size.
void setBinBoundaries(const std::vector< double > &bin_boundaries)
size_t size() const override
Returns the number of bins.
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Definition: Algorithms.h:33