BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ConstKBinAxis.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Base/Axis/ConstKBinAxis.cpp
6 //! @brief Implement class ConstKBinAxis.
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 
20 ConstKBinAxis::ConstKBinAxis(const std::string& name, size_t nbins)
21  : VariableBinAxis(name, nbins), m_start(0), m_end(0)
22 {
23 }
24 
25 ConstKBinAxis::ConstKBinAxis(const std::string& name, size_t nbins, double start, double end)
26  : VariableBinAxis(name, nbins), m_start(start), m_end(end)
27 {
28  if (m_start >= m_end)
30  "ConstKBinAxis::ConstKBinAxis() -> Error. start >= end is not allowed.");
31 
32  double start_sin = std::sin(m_start);
33  double end_sin = std::sin(m_end);
34  double step = (end_sin - start_sin) / (m_nbins);
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 * i);
40  }
41  setBinBoundaries(bin_boundaries);
42 }
43 
45 {
46  return new ConstKBinAxis(getName(), m_nbins, m_start, m_end);
47 }
48 
49 ConstKBinAxis* ConstKBinAxis::createClippedAxis(double left, double right) const
50 {
51  if (left >= right)
53  "ConstKBinAxis::createClippedAxis() -> Error. 'left'' should be smaller than 'right'");
54 
55  if (left < getMin())
56  left = getBin(0).getMidPoint();
57  if (right >= getMax())
58  right = getBin(size() - 1).getMidPoint();
59 
60  size_t nbin1 = findClosestIndex(left);
61  size_t nbin2 = findClosestIndex(right);
62 
63  size_t new_nbins = nbin2 - nbin1 + 1;
64  std::vector<double> new_boundaries;
65  std::vector<double> old_boundaries = getBinBoundaries();
66  for (size_t i = 0; i < new_nbins + 1; ++i) {
67  new_boundaries.push_back(old_boundaries[nbin1 + i]);
68  }
69 
70  ConstKBinAxis* result = new ConstKBinAxis(getName(), new_nbins);
71  result->m_start = new_boundaries.front();
72  result->m_end = new_boundaries.back();
73  result->setBinBoundaries(new_boundaries);
74  return result;
75 }
76 
77 bool ConstKBinAxis::equals(const IAxis& other) const
78 {
79  if (!IAxis::equals(other))
80  return false;
81  if (const ConstKBinAxis* otherAxis = dynamic_cast<const ConstKBinAxis*>(&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 }
92 
93 void ConstKBinAxis::print(std::ostream& ostr) const
94 {
95  ostr << "ConstKBinAxis(\"" << getName() << "\", " << size() << ", "
96  << std::setprecision(std::numeric_limits<double>::digits10 + 2) << m_start << ", " << m_end
97  << ")";
98 }
Defines and implements namespace algo with some algorithms.
Defines class ConstKBinAxis.
Defines many exception classes in namespace Exceptionss.
Axis with fixed bin size in sin(angle) space.
Definition: ConstKBinAxis.h:24
ConstKBinAxis * createClippedAxis(double left, double right) const final
Creates a new clipped axis.
void print(std::ostream &ostr) const final
ConstKBinAxis(const std::string &name, size_t nbins, double start, double end)
ConstKBinAxis constructor.
bool equals(const IAxis &other) const final
ConstKBinAxis * clone() const final
clone function
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
Axis with variable bin size.
double getMax() const
Returns value of last point of axis.
size_t size() const
retrieve the number of bins
void setBinBoundaries(const std::vector< double > &bin_boundaries)
Bin1D getBin(size_t index) const
retrieve a 1d bin for the given index
double getMin() const
Returns value of first point of axis.
std::vector< double > getBinBoundaries() const
size_t findClosestIndex(double value) const
find bin index which is best match for given value
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Definition: Algorithms.h:30
double getMidPoint() const
Definition: Bin.h:25