BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
RealParameter Class Reference
Inheritance diagram for RealParameter:
Collaboration diagram for RealParameter:

Public Member Functions

 RealParameter (const std::string &name, double *par, const std::string &parent_name="", const std::function< void()> &onChange=std::function< void()>(), const RealLimits &limits=RealLimits::limitless(), const Attributes &attr=Attributes::free())
 
virtual ~RealParameter ()=default
 
RealParameterclone (const std::string &new_name="") const
 
void setValue (double value)
 
double value () const
 
RealParametersetLimits (const RealLimits &limits)
 
RealLimits limits () const
 
RealParametersetLimited (double lower, double upper)
 
RealParametersetPositive ()
 
RealParametersetNonnegative ()
 
RealParametersetUnit (const std::string &name)
 
std::string unit () const
 
virtual bool isNull () const
 
double & getData () const
 
void setData (double &data)
 
bool hasSameData (const IParameter &other)
 
const std::string & getName () const
 

Protected Member Functions

std::string fullName () const
 

Protected Attributes

RealLimits m_limits
 
Attributes m_attr
 
Unit m_unit
 
const std::string m_name
 
double * m_data
 
const std::string m_parent_name
 
const std::function< void()> m_onChange
 

Detailed Description

Wraps a parameter of type double.

In addition to name and on-change callback held by the parent class IParameter, this class holds Limits, Attributes (currently only fixed or not), and a Unit.

Definition at line 31 of file RealParameter.h.

Constructor & Destructor Documentation

◆ RealParameter()

RealParameter::RealParameter ( const std::string &  name,
double *  par,
const std::string &  parent_name = "",
const std::function< void()> &  onChange = std::function<void()>(),
const RealLimits limits = RealLimits::limitless(),
const Attributes attr = Attributes::free() 
)

Definition at line 18 of file RealParameter.cpp.

21  : IParameter<double>(name, par, parent_name, onChange), m_limits(limits), m_attr(attr)
22 {
23  if (!m_limits.isInRange(value())) {
24  std::ostringstream message;
25  message << "Cannot initialize parameter " << fullName() << " with value " << value()
26  << ": out of bounds [" << limits << "]\n";
27  throw std::runtime_error(message.str());
28  }
29 }
std::string fullName() const
For use in error messages.
Definition: IParameter.h:58
bool isInRange(double value) const
returns true if proposed value is in limits range
Definition: RealLimits.cpp:94
RealLimits limits() const
Attributes m_attr
Definition: RealParameter.h:60
double value() const
Returns value of wrapped parameter.
RealLimits m_limits
Definition: RealParameter.h:59

References IParameter< double >::fullName(), RealLimits::isInRange(), limits(), m_limits, and value().

Referenced by clone().

Here is the call graph for this function:

◆ ~RealParameter()

virtual RealParameter::~RealParameter ( )
virtualdefault

Member Function Documentation

◆ clone()

RealParameter * RealParameter::clone ( const std::string &  new_name = "") const
virtual

Implements IParameter< double >.

Definition at line 31 of file RealParameter.cpp.

32 {
33  auto* ret = new RealParameter(new_name != "" ? new_name : getName(), m_data, m_parent_name,
35  ret->setUnit(unit());
36  return ret;
37 }
const std::string & getName() const
Definition: IParameter.h:49
const std::function< void()> m_onChange
Definition: IParameter.h:55
const std::string m_parent_name
Definition: IParameter.h:54
std::string unit() const
RealParameter(const std::string &name, double *par, const std::string &parent_name="", const std::function< void()> &onChange=std::function< void()>(), const RealLimits &limits=RealLimits::limitless(), const Attributes &attr=Attributes::free())

References IParameter< double >::getName(), IParameter< double >::m_data, m_limits, IParameter< double >::m_onChange, IParameter< double >::m_parent_name, RealParameter(), and unit().

Referenced by ParameterPool::copyToExternalPool().

Here is the call graph for this function:

◆ setValue()

void RealParameter::setValue ( double  value)

Sets value of wrapped parameter and emit signal.

Definition at line 39 of file RealParameter.cpp.

40 {
41  if (value == *m_data)
42  return; // nothing to do
43 
44  if (isNull())
45  throw std::runtime_error("RealParameter::setValue() -> Error. Non-initialized parameter '"
46  + fullName() + "'");
47 
48  if (!m_limits.isInRange(value)) {
49  std::ostringstream message;
50  message << "Cannot set parameter " << fullName() << " to value " << value
51  << ": out of bounds [" << m_limits << "]\n";
52  throw std::runtime_error(message.str());
53  }
54 
55  if (m_attr.isFixed())
56  throw std::runtime_error("Parameter " + fullName() + " is fixed");
57 
58  *m_data = value;
59  if (m_onChange)
60  m_onChange();
61 }
bool isFixed() const
Definition: Attributes.h:32
virtual bool isNull() const
Returns true if wrapped parameter was not initialized with proper real value.
Definition: IParameter.h:39

References IParameter< double >::fullName(), Attributes::isFixed(), RealLimits::isInRange(), IParameter< double >::isNull(), m_attr, IParameter< double >::m_data, m_limits, IParameter< double >::m_onChange, and value().

Here is the call graph for this function:

◆ value()

double RealParameter::value ( ) const

Returns value of wrapped parameter.

Definition at line 63 of file RealParameter.cpp.

64 {
65  return *m_data;
66 }

References IParameter< double >::m_data.

Referenced by RealParameter(), setValue(), and pyfmt2::valueTimesUnit().

◆ setLimits()

RealParameter & RealParameter::setLimits ( const RealLimits limits)

Definition at line 68 of file RealParameter.cpp.

69 {
70  m_limits = limits;
71  return *this;
72 }

References limits(), and m_limits.

Referenced by DepthProbeSimulation::initialize(), SpecularSimulation::initialize(), setLimited(), setNonnegative(), and setPositive().

Here is the call graph for this function:

◆ limits()

RealLimits RealParameter::limits ( ) const

Definition at line 74 of file RealParameter.cpp.

75 {
76  return m_limits;
77 }

References m_limits.

Referenced by RealParameter(), ParameterPool::report_set_value_error(), and setLimits().

◆ setLimited()

RealParameter & RealParameter::setLimited ( double  lower,
double  upper 
)

Definition at line 79 of file RealParameter.cpp.

80 {
81  setLimits(RealLimits::limited(lower, upper));
82  return *this;
83 }
static RealLimits limited(double left_bound_value, double right_bound_value)
Creates an object bounded from the left and right.
Definition: RealLimits.cpp:123
RealParameter & setLimits(const RealLimits &limits)

References RealLimits::limited(), and setLimits().

Referenced by Beam::Beam().

Here is the call graph for this function:

◆ setPositive()

RealParameter & RealParameter::setPositive ( )

Definition at line 85 of file RealParameter.cpp.

86 {
88  return *this;
89 }
static RealLimits positive()
Creates an object which can have only positive values (>0., zero is not included)
Definition: RealLimits.cpp:108

References RealLimits::positive(), and setLimits().

Referenced by BasicLattice::BasicLattice(), HexagonalLattice::HexagonalLattice(), and SquareLattice::SquareLattice().

Here is the call graph for this function:

◆ setNonnegative()

◆ setUnit()

RealParameter & RealParameter::setUnit ( const std::string &  name)

Definition at line 97 of file RealParameter.cpp.

98 {
99  if (!(name == "" || name == "nm" || name == "rad" || name == "nm^2"))
100  throw std::runtime_error("RealParameter::setUnit() -> Error. Unexpected unit name " + name);
101 
102  m_unit.setUnit(name);
103  return *this;
104 }
void setUnit(const std::string &name)
Definition: Unit.h:26

References m_unit, and Unit::setUnit().

Referenced by BasicLattice::BasicLattice(), Beam::Beam(), HexagonalLattice::HexagonalLattice(), IInterferenceFunction::IInterferenceFunction(), InterferenceFunction1DLattice::InterferenceFunction1DLattice(), InterferenceFunction2DParaCrystal::InterferenceFunction2DParaCrystal(), InterferenceFunctionHardDisk::InterferenceFunctionHardDisk(), InterferenceFunctionRadialParaCrystal::InterferenceFunctionRadialParaCrystal(), InterferenceFunctionTwin::InterferenceFunctionTwin(), Lattice2D::Lattice2D(), LayerRoughness::LayerRoughness(), MultiLayer::MultiLayer(), Layer::registerThickness(), IParameterized::registerVector(), Lattice2D::setRotationEnabled(), DistributionLogNormal::setUnits(), and SquareLattice::SquareLattice().

Here is the call graph for this function:

◆ unit()

std::string RealParameter::unit ( ) const

Definition at line 106 of file RealParameter.cpp.

107 {
108  return m_unit.getName();
109 }
const std::string & getName() const
Definition: Unit.h:27

References Unit::getName(), and m_unit.

Referenced by clone(), ParameterUtils::poolParameterUnits(), and pyfmt2::valueTimesUnit().

Here is the call graph for this function:

◆ isNull()

virtual bool IParameter< double >::isNull ( ) const
inlinevirtualinherited

Returns true if wrapped parameter was not initialized with proper real value.

Definition at line 39 of file IParameter.h.

39 { return m_data ? false : true; }

◆ getData()

double & IParameter< double >::getData ( ) const
inlineinherited

Definition at line 41 of file IParameter.h.

41 { return *m_data; }

◆ setData()

void IParameter< double >::setData ( double &  data)
inlineinherited

Definition at line 42 of file IParameter.h.

43  {
44  m_data = &data;
45  m_onChange();
46  }

◆ hasSameData()

bool IParameter< double >::hasSameData ( const IParameter< double > &  other)
inherited

Returns true if two parameters are pointing to the same raw data.

Definition at line 48 of file IParameter.h.

73 {
74  return &getData() == &other.getData();
75 }
double & getData() const
Definition: IParameter.h:41

◆ getName()

const std::string& IParameter< double >::getName ( ) const
inlineinherited

Definition at line 49 of file IParameter.h.

49 { return m_name; }
const std::string m_name
Definition: IParameter.h:52

◆ fullName()

std::string IParameter< double >::fullName ( ) const
inlineprotectedinherited

For use in error messages.

Definition at line 58 of file IParameter.h.

58 { return m_parent_name + "/" + m_name; }

Member Data Documentation

◆ m_limits

RealLimits RealParameter::m_limits
protected

Definition at line 59 of file RealParameter.h.

Referenced by clone(), limits(), RealParameter(), setLimits(), and setValue().

◆ m_attr

Attributes RealParameter::m_attr
protected

Definition at line 60 of file RealParameter.h.

Referenced by setValue().

◆ m_unit

Unit RealParameter::m_unit
protected

Definition at line 61 of file RealParameter.h.

Referenced by setUnit(), and unit().

◆ m_name

const std::string IParameter< double >::m_name
protectedinherited

Definition at line 52 of file IParameter.h.

◆ m_data

double * IParameter< double >::m_data
protectedinherited

Definition at line 53 of file IParameter.h.

◆ m_parent_name

const std::string IParameter< double >::m_parent_name
protectedinherited

Definition at line 54 of file IParameter.h.

◆ m_onChange

const std::function<void()> IParameter< double >::m_onChange
protectedinherited

Definition at line 55 of file IParameter.h.


The documentation for this class was generated from the following files: