BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
ConstKBinAxis.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
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/Axis/Bin.h"
17 #include "Base/Util/Algorithms.h"
18 #include <cmath>
19 #include <iomanip>
20 
21 ConstKBinAxis::ConstKBinAxis(const std::string& name, size_t nbins)
22  : VariableBinAxis(name, nbins)
23  , m_start(0)
24  , m_end(0)
25 {
26 }
27 
28 ConstKBinAxis::ConstKBinAxis(const std::string& name, size_t nbins, double start, double end)
29  : VariableBinAxis(name, nbins)
30  , m_start(start)
31  , m_end(end)
32 {
33  if (m_start >= m_end)
34  throw std::runtime_error(
35  "ConstKBinAxis::ConstKBinAxis() -> Error. start >= end is not allowed.");
36 
37  double start_sin = std::sin(m_start);
38  double end_sin = std::sin(m_end);
39  double step = (end_sin - start_sin) / (m_nbins);
40 
41  std::vector<double> bin_boundaries;
42  bin_boundaries.resize(m_nbins + 1, 0.0);
43  for (size_t i = 0; i < bin_boundaries.size(); ++i)
44  bin_boundaries[i] = std::asin(start_sin + step * i);
45  setBinBoundaries(bin_boundaries);
46 }
47 
49 {
50  return new ConstKBinAxis(axisName(), m_nbins, m_start, m_end);
51 }
52 
53 void ConstKBinAxis::clip(double lower, double upper)
54 {
55  if (lower >= upper)
56  throw std::runtime_error(
57  "ConstKBinAxis::clip() -> Error. 'lower' should be smaller than 'upper'");
58 
59  if (lower < min())
60  lower = bin(0).center();
61  if (upper >= max())
62  upper = bin(size() - 1).center();
63 
64  size_t nbin1 = findClosestIndex(lower);
65  size_t nbin2 = findClosestIndex(upper);
66 
67  size_t new_nbins = nbin2 - nbin1 + 1;
68  std::vector<double> new_boundaries;
69  std::vector<double> old_boundaries = binBoundaries();
70  for (size_t i = 0; i < new_nbins + 1; ++i)
71  new_boundaries.push_back(old_boundaries[nbin1 + i]);
72 
73  m_nbins = new_nbins;
74  m_start = new_boundaries.front();
75  m_end = new_boundaries.back();
76  setBinBoundaries(new_boundaries);
77 }
78 
79 bool ConstKBinAxis::equals(const IAxis& other) const
80 {
81  if (!IAxis::equals(other))
82  return false;
83  if (const auto* otherAxis = dynamic_cast<const ConstKBinAxis*>(&other)) {
84  if (size() != otherAxis->size())
85  return false;
86  if (!BaseUtils::algo::almostEqual(m_start, otherAxis->m_start))
87  return false;
88  if (!BaseUtils::algo::almostEqual(m_end, otherAxis->m_end))
89  return false;
90  return true;
91  }
92  return false;
93 }
94 
95 void ConstKBinAxis::print(std::ostream& ostr) const
96 {
97  ostr << "ConstKBinAxis(\"" << axisName() << "\", " << size() << ", "
98  << std::setprecision(std::numeric_limits<double>::digits10 + 2) << m_start << ", " << m_end
99  << ")";
100 }
Defines and implements namespace BaseUtils::algo with some algorithms.
Defines structs Bin1D, Bin1DCVector.
Defines class ConstKBinAxis.
double center() const
Definition: Bin.h:30
Axis with fixed bin size in sin(angle) space.
Definition: ConstKBinAxis.h:23
void print(std::ostream &ostr) const override
bool equals(const IAxis &other) const override
ConstKBinAxis(const std::string &name, size_t nbins, double start, double end)
ConstKBinAxis constructor.
void clip(double lower, double upper) override
Clips this axis to the given values.
ConstKBinAxis * clone() const override
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.
double max() const override
Returns value of last point of axis.
void setBinBoundaries(const std::vector< double > &bin_boundaries)
Bin1D bin(size_t index) const override
retrieve a 1d bin for the given index
size_t size() const override
Returns the number of bins.
std::vector< double > binBoundaries() const override
size_t findClosestIndex(double value) const override
find bin index which is best match for given value
double min() const override
Returns value of first point of axis.
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Definition: Algorithms.h:33