BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
AttLimits.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Fit/Tools/AttLimits.cpp
6 //! @brief Implements and implements class AttLimits.
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 
15 #include "Fit/Tools/AttLimits.h"
16 #include <iomanip>
17 #include <sstream>
18 
19 AttLimits::AttLimits() : m_limits(RealLimits::limitless()), m_att_fixed(Attributes::free()) {}
20 
21 AttLimits::AttLimits(const RealLimits& limits, const Attributes& fixedAttr)
22  : m_limits(limits), m_att_fixed(fixedAttr)
23 {
24 }
25 
26 AttLimits AttLimits::limitless()
27 {
28  return AttLimits();
29 }
30 
31 AttLimits AttLimits::lowerLimited(double bound_value)
32 {
33  return AttLimits(RealLimits::lowerLimited(bound_value), Attributes::free());
34 }
35 
36 AttLimits AttLimits::positive()
37 {
38  return AttLimits(RealLimits::positive(), Attributes::free());
39 }
40 
41 AttLimits AttLimits::nonnegative()
42 {
43  return AttLimits(RealLimits::nonnegative(), Attributes::free());
44 }
45 
46 AttLimits AttLimits::upperLimited(double bound_value)
47 {
48  return AttLimits(RealLimits::upperLimited(bound_value), Attributes::free());
49 }
50 
51 AttLimits AttLimits::limited(double left_bound_value, double right_bound_value)
52 {
53  return AttLimits(RealLimits::limited(left_bound_value, right_bound_value), Attributes::free());
54 }
55 
56 AttLimits AttLimits::fixed()
57 {
59 }
60 
61 bool AttLimits::isFixed() const
62 {
63  return m_att_fixed.isFixed();
64 }
65 
66 bool AttLimits::isLimited() const
67 {
68  return m_att_fixed.isFree() && m_limits.hasLowerAndUpperLimits();
69 }
70 
71 bool AttLimits::isUpperLimited() const
72 {
73  return m_att_fixed.isFree() && !m_limits.hasLowerLimit() && m_limits.hasUpperLimit();
74 }
75 
76 bool AttLimits::isLowerLimited() const
77 {
78  return m_att_fixed.isFree() && m_limits.hasLowerLimit() && !m_limits.hasUpperLimit();
79 }
80 
81 bool AttLimits::isLimitless() const
82 {
83  return m_att_fixed.isFree() && !m_limits.hasLowerLimit() && !m_limits.hasUpperLimit();
84 }
85 
86 double AttLimits::lowerLimit() const
87 {
88  return m_limits.lowerLimit();
89 }
90 
91 double AttLimits::upperLimit() const
92 {
93  return m_limits.upperLimit();
94 }
95 
96 void AttLimits::setFixed(bool isFixed)
97 {
98  m_limits.removeLimits();
99  m_att_fixed.setFixed(isFixed);
100 }
101 
102 bool AttLimits::operator==(const AttLimits& other) const
103 {
104  return m_limits == other.m_limits && m_att_fixed == other.m_att_fixed;
105 }
106 
107 bool AttLimits::operator!=(const AttLimits& other) const
108 {
109  return !(*this == other);
110 }
111 
112 std::string AttLimits::toString() const
113 {
114  std::ostringstream result;
115 
116  if (isFixed())
117  result << "fixed";
118  else if (isLimitless())
119  result << "free";
120  else if (isLowerLimited())
121  result << "lowerLimited(" << std::scientific << std::setprecision(2) << lowerLimit() << ")";
122  else if (isUpperLimited())
123  result << "upperLimited(" << std::scientific << std::setprecision(2) << upperLimit() << ")";
124  else if (isLimited())
125  result << "limited(" << std::scientific << std::setprecision(2) << lowerLimit() << ","
126  << std::scientific << std::setprecision(2) << upperLimit() << ")";
127 
128  return result.str();
129 }
Defines and implements class AttLimits.
Attributes and limits of a fit parameter, and coupling between these properties.
Definition: AttLimits.h:26
Attributes for a fit parameter.
Definition: Attributes.h:24
static Attributes fixed()
Creates a fixed value object.
Definition: Attributes.h:28
Limits for a real fit parameter.
Definition: RealLimits.h:25
static RealLimits limitless()
Creates an object withoud bounds (default)
Definition: RealLimits.cpp:128
static RealLimits upperLimited(double bound_value)
Creates an object bounded from the right.
Definition: RealLimits.cpp:118
bool hasUpperLimit() const
if has upper limit
Definition: RealLimits.cpp:55
static RealLimits lowerLimited(double bound_value)
Creates an object bounded from the left.
Definition: RealLimits.cpp:103
bool hasLowerAndUpperLimits() const
if has lower and upper limit
Definition: RealLimits.cpp:77
static RealLimits positive()
Creates an object which can have only positive values (>0., zero is not included)
Definition: RealLimits.cpp:108
double upperLimit() const
Returns upper limit.
Definition: RealLimits.cpp:60
double lowerLimit() const
Returns lower limit.
Definition: RealLimits.cpp:38
static RealLimits nonnegative()
Creates an object which can have only positive values with 0. included.
Definition: RealLimits.cpp:113
void removeLimits()
remove limits
Definition: RealLimits.cpp:88
static RealLimits limited(double left_bound_value, double right_bound_value)
Creates an object bounded from the left and right.
Definition: RealLimits.cpp:123
bool hasLowerLimit() const
if has lower limit
Definition: RealLimits.cpp:33
std::string scientific(const T value, int n=10)
Returns scientific string representing given value of any numeric type.
Definition: StringUtils.h:54