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