BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
IParameterized.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Param/Base/IParameterized.cpp
6 //! @brief Implements class IParameterized.
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"
19 #include <iostream>
20 #include <sstream>
21 #include <stdexcept>
22 
23 IParameterized::IParameterized(const std::string& name) : m_name{name}, m_pool{new ParameterPool} {}
24 
26 {
27  if (!other.parameterPool()->empty())
28  throw std::runtime_error("BUG: not prepared to copy parameters of " + getName());
29 }
30 
32 
34 {
35  auto* result = new ParameterPool;
36  m_pool->copyToExternalPool("/" + getName() + "/", result);
37  return result;
38 }
39 
41 {
42  std::ostringstream result;
43  std::unique_ptr<ParameterPool> P_pool(createParameterTree());
44  result << *P_pool << "\n";
45  return result.str();
46 }
47 
48 RealParameter& IParameterized::registerParameter(const std::string& name, double* data)
49 {
50  return m_pool->addParameter(
51  new RealParameter(name, data, getName(), [&]() -> void { onChange(); }));
52 }
53 
54 void IParameterized::registerVector(const std::string& base_name, kvector_t* p_vec,
55  const std::string& units)
56 {
57  registerParameter(XComponentName(base_name), &((*p_vec)[0])).setUnit(units);
58  registerParameter(YComponentName(base_name), &((*p_vec)[1])).setUnit(units);
59  registerParameter(ZComponentName(base_name), &((*p_vec)[2])).setUnit(units);
60 }
61 
62 void IParameterized::setParameterValue(const std::string& name, double value)
63 {
64  if (name.find('*') == std::string::npos && name.find('/') == std::string::npos) {
65  m_pool->setParameterValue(name, value);
66  } else {
67  std::unique_ptr<ParameterPool> P_pool{createParameterTree()};
68  if (name.find('*') != std::string::npos)
69  P_pool->setMatchedParametersValue(name, value);
70  else
71  P_pool->setParameterValue(name, value);
72  }
73 }
74 
75 void IParameterized::setVectorValue(const std::string& base_name, kvector_t value)
76 {
77  setParameterValue(XComponentName(base_name), value.x());
78  setParameterValue(YComponentName(base_name), value.y());
79  setParameterValue(ZComponentName(base_name), value.z());
80 }
81 
82 //! Returns parameter with given 'name'.
83 RealParameter* IParameterized::parameter(const std::string& name) const
84 {
85  return m_pool->parameter(name);
86 }
87 
88 void IParameterized::removeParameter(const std::string& name)
89 {
90  m_pool->removeParameter(name);
91 }
92 
93 void IParameterized::removeVector(const std::string& base_name)
94 {
95  removeParameter(XComponentName(base_name));
96  removeParameter(YComponentName(base_name));
97  removeParameter(ZComponentName(base_name));
98 }
99 
100 std::string IParameterized::XComponentName(const std::string& base_name)
101 {
102  return base_name + "X";
103 }
104 
105 std::string IParameterized::YComponentName(const std::string& base_name)
106 {
107  return base_name + "Y";
108 }
109 
110 std::string IParameterized::ZComponentName(const std::string& base_name)
111 {
112  return base_name + "Z";
113 }
Defines many exception classes in namespace Exceptionss.
Defines class IParameterized.
Defines class ParameterPool.
Defines class RealParameter.
T z() const
Returns z-component in cartesian coordinate system.
Definition: BasicVector3D.h:68
T y() const
Returns y-component in cartesian coordinate system.
Definition: BasicVector3D.h:66
T x() const
Returns x-component in cartesian coordinate system.
Definition: BasicVector3D.h:64
Manages a local parameter pool, and a tree of child pools.
RealParameter & registerParameter(const std::string &name, double *parpointer)
IParameterized(const std::string &name="")
std::string parametersToString() const
Returns multiline string representing available parameters.
RealParameter * parameter(const std::string &name) const
Returns parameter with given 'name'.
static std::string XComponentName(const std::string &base_name)
void removeParameter(const std::string &name)
const std::string & getName() const
void removeVector(const std::string &base_name)
void setVectorValue(const std::string &base_name, kvector_t value)
virtual void onChange()
Action to be taken in inherited class when a parameter has changed.
std::unique_ptr< ParameterPool > m_pool
parameter pool (kind of pointer-to-implementation)
ParameterPool * parameterPool() const
Returns pointer to the parameter pool.
void registerVector(const std::string &base_name, kvector_t *p_vec, const std::string &units="nm")
virtual ParameterPool * createParameterTree() const
Creates new parameter pool, with all local parameters and those of its children.
static std::string YComponentName(const std::string &base_name)
virtual ~IParameterized()
void setParameterValue(const std::string &name, double value)
static std::string ZComponentName(const std::string &base_name)
Container with parameters for IParameterized object.
Definition: ParameterPool.h:30
bool empty() const
Definition: ParameterPool.h:43
int setMatchedParametersValue(const std::string &wildcards, double value)
Sets value of the nonzero parameters that match pattern ('*' allowed), or throws.
Wraps a parameter of type double.
Definition: RealParameter.h:32
RealParameter & setUnit(const std::string &name)