BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
RealParameter Class Reference

Wraps a parameter of type double. More...

Inheritance diagram for RealParameter:
[legend]
Collaboration diagram for RealParameter:
[legend]

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
 
double & getData () const
 
const std::string & getName () const
 
bool hasSameData (const IParameter &other)
 Returns true if two parameters are pointing to the same raw data. More...
 
virtual bool isNull () const
 Returns true if wrapped parameter was not initialized with proper real value. More...
 
RealLimits limits () const
 
void setData (double &data)
 
RealParametersetLimited (double lower, double upper)
 
RealParametersetLimits (const RealLimits &limits)
 
RealParametersetNonnegative ()
 
RealParametersetPositive ()
 
RealParametersetUnit (const std::string &name)
 
void setValue (double value)
 Sets value of wrapped parameter and emit signal. More...
 
std::string unit () const
 
double value () const
 Returns value of wrapped parameter. More...
 

Protected Member Functions

std::string fullName () const
 For use in error messages. More...
 

Protected Attributes

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

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:96
RealLimits limits() const
Attributes m_attr
Definition: RealParameter.h:59
double value() const
Returns value of wrapped parameter.
RealLimits m_limits
Definition: RealParameter.h:58
QString const & name(EShape k)
Definition: particles.cpp:21

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 RealParameter(), IParameter< double >::getName(), IParameter< double >::m_data, m_limits, IParameter< double >::m_onChange, IParameter< double >::m_parent_name, and unit().

Referenced by ParameterPool::copyToExternalPool().

Here is the call graph for this function:

◆ 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; }
const std::string m_name
Definition: IParameter.h:52

◆ getData()

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

Definition at line 41 of file IParameter.h.

41 { return *m_data; }

◆ getName()

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

Definition at line 49 of file IParameter.h.

49 { return m_name; }

◆ 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

◆ 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; }

◆ 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().

◆ 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  }

◆ 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:125
RealParameter & setLimits(const RealLimits &limits)

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

Referenced by Beam::Beam().

Here is the call graph for this function:

◆ 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:

◆ setNonnegative()

◆ 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:110

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

Referenced by BasicLattice2D::BasicLattice2D(), HexagonalLattice2D::HexagonalLattice2D(), and SquareLattice2D::SquareLattice2D().

Here is the call graph for this function:

◆ setUnit()

◆ 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:34
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:

◆ 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:31

References Unit::getName(), and m_unit.

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

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(), ParticleDistribution::generateParticles(), setValue(), and pyfmt2::valueTimesUnit().

Member Data Documentation

◆ m_attr

Attributes RealParameter::m_attr
protected

Definition at line 59 of file RealParameter.h.

Referenced by setValue().

◆ m_data

double * IParameter< double >::m_data
protectedinherited

Definition at line 53 of file IParameter.h.

◆ m_limits

RealLimits RealParameter::m_limits
protected

Definition at line 58 of file RealParameter.h.

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

◆ m_name

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

Definition at line 52 of file IParameter.h.

◆ m_onChange

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

Definition at line 55 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_unit

Unit RealParameter::m_unit
protected

Definition at line 60 of file RealParameter.h.

Referenced by setUnit(), and unit().


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