BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
CustomBinAxis.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
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/Types/Exceptions.h"
17 #include "Base/Utils/Algorithms.h"
18 #include <iomanip>
19 #include <limits>
20 
21 CustomBinAxis::CustomBinAxis(const std::string& name, size_t nbins, double start, double end)
22  : VariableBinAxis(name, nbins), m_start(start), m_end(end)
23 {
24  if (m_start >= m_end)
25  throw Exceptions::LogicErrorException("CustomBinAxis::CustomBinAxis() -> Error."
26  " start >= end is not allowed.");
27 
28  double start_sin = std::sin(start);
29  double end_sin = std::sin(end);
30  double step = (end_sin - start_sin) / (m_nbins - 1); // m_nbins-1 is intentionally
31 
32  m_bin_centers.resize(m_nbins, 0.0);
33  for (size_t i = 0; i < m_bin_centers.size(); ++i) {
34  m_bin_centers[i] = std::asin(start_sin + i * step);
35  }
36 
37  std::vector<double> bin_boundaries;
38  bin_boundaries.resize(m_nbins + 1, 0.0);
39  for (size_t i = 0; i < bin_boundaries.size(); ++i) {
40  bin_boundaries[i] = std::asin(start_sin - step / 2. + step * i);
41  }
42  setBinBoundaries(bin_boundaries);
43 }
44 
46 {
47  return new CustomBinAxis(getName(), m_nbins, m_start, m_end);
48 }
49 
50 Bin1D CustomBinAxis::getBin(size_t index) const
51 {
52  if (index >= m_nbins)
53  throw Exceptions::OutOfBoundsException("CustomBinAxis::getBin() -> Error. Wrong index.");
54 
55  Bin1D result(m_bin_centers[index], m_bin_centers[index]);
56  return result;
57 }
58 
59 std::vector<double> CustomBinAxis::getBinCenters() const
60 {
61  return m_bin_centers;
62 }
63 
64 CustomBinAxis* CustomBinAxis::createClippedAxis(double /* left */, double /* right */) const
65 {
66  throw Exceptions::NotImplementedException("VariableBinAxis::CustomBinAxis() -> Error."
67  " Not implemented.");
68 }
69 
70 void CustomBinAxis::print(std::ostream& ostr) const
71 {
72  ostr << "CustomBinAxis(\"" << getName() << "\", " << size() << ", "
73  << std::setprecision(std::numeric_limits<double>::digits10 + 2) << m_start << ", " << m_end
74  << ")";
75 }
76 
77 bool CustomBinAxis::equals(const IAxis& other) const
78 {
79  if (!IAxis::equals(other))
80  return false;
81  if (const CustomBinAxis* otherAxis = dynamic_cast<const CustomBinAxis*>(&other)) {
82  if (size() != otherAxis->size())
83  return false;
84  if (!algo::almostEqual(m_start, otherAxis->m_start))
85  return false;
86  if (!algo::almostEqual(m_end, otherAxis->m_end))
87  return false;
88  return true;
89  }
90  return false;
91 }
Defines and implements namespace algo with some algorithms.
Defines class CustomBinAxis.
Defines many exception classes in namespace Exceptionss.
Axis with fixed bin size in sin(angle) space used for numerical comparison with IsGisaxs.
Definition: CustomBinAxis.h:25
CustomBinAxis * createClippedAxis(double left, double right) const
Creates a new clipped axis.
CustomBinAxis * clone() const
clone function
CustomBinAxis(const std::string &name, size_t nbins, double start, double end)
CustomBinAxis constructor.
Bin1D getBin(size_t index) const
retrieve a 1d bin for the given index
Interface for one-dimensional axes.
Definition: IAxis.h:25
std::string getName() const
retrieve the label of the axis
Definition: IAxis.h:40
Axis with variable bin size.
size_t size() const
retrieve the number of bins
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Definition: Algorithms.h:30
Definition: Bin.h:20