BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
IParametricComponent.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Param/Base/IParametricComponent.cpp
6 //! @brief Implements interface IParametricComponent.
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 
18 #include <iostream>
19 #include <sstream>
20 #include <stdexcept>
21 
23  : m_name{name}, m_pool{new ParameterPool}
24 {
25 }
26 
28  : IParametricComponent(other.getName())
29 {
30  if (!other.parameterPool()->empty())
31  throw std::runtime_error("BUG: not prepared to copy parameters of " + getName());
32 }
33 
35 
37 {
38  auto* result = new ParameterPool;
39  m_pool->copyToExternalPool("/" + getName() + "/", result);
40  return result;
41 }
42 
44 {
45  std::ostringstream result;
46  std::unique_ptr<ParameterPool> P_pool(createParameterTree());
47  result << *P_pool << "\n";
48  return result.str();
49 }
50 
52 {
53  return m_pool->addParameter(
54  new RealParameter(name, data, getName(), [&]() -> void { onChange(); }));
55 }
56 
58  const std::string& units)
59 {
60  registerParameter(XComponentName(base_name), &((*p_vec)[0])).setUnit(units);
61  registerParameter(YComponentName(base_name), &((*p_vec)[1])).setUnit(units);
62  registerParameter(ZComponentName(base_name), &((*p_vec)[2])).setUnit(units);
63 }
64 
65 void IParametricComponent::setParameterValue(const std::string& name, double value)
66 {
67  if (name.find('*') == std::string::npos && name.find('/') == std::string::npos) {
68  m_pool->setParameterValue(name, value);
69  } else {
70  std::unique_ptr<ParameterPool> P_pool{createParameterTree()};
71  if (name.find('*') != std::string::npos)
72  P_pool->setMatchedParametersValue(name, value);
73  else
74  P_pool->setParameterValue(name, value);
75  }
76 }
77 
79 {
83 }
84 
85 //! Returns parameter with given 'name'.
87 {
88  return m_pool->parameter(name);
89 }
90 
92 {
93  m_pool->removeParameter(name);
94 }
95 
97 {
101 }
102 
103 std::string IParametricComponent::XComponentName(const std::string& base_name)
104 {
105  return base_name + "X";
106 }
107 
108 std::string IParametricComponent::YComponentName(const std::string& base_name)
109 {
110  return base_name + "Y";
111 }
112 
113 std::string IParametricComponent::ZComponentName(const std::string& base_name)
114 {
115  return base_name + "Z";
116 }
Defines interface IParametricComponent.
Defines class ParameterPool.
Defines class RealParameter.
T z() const
Returns z-component in cartesian coordinate system.
Definition: BasicVector3D.h:67
T y() const
Returns y-component in cartesian coordinate system.
Definition: BasicVector3D.h:65
T x() const
Returns x-component in cartesian coordinate system.
Definition: BasicVector3D.h:63
Manages a local parameter pool, and a tree of child pools.
const std::string & getName() const
void setVectorValue(const std::string &base_name, kvector_t value)
virtual ParameterPool * createParameterTree() const
Creates new parameter pool, with all local parameters and those of its children.
void registerVector(const std::string &base_name, kvector_t *p_vec, const std::string &units="nm")
void setParameterValue(const std::string &name, double value)
std::string parametersToString() const
Returns multiline string representing available parameters.
ParameterPool * parameterPool() const
Returns pointer to the parameter pool.
static std::string XComponentName(const std::string &base_name)
void removeVector(const std::string &base_name)
static std::string ZComponentName(const std::string &base_name)
void removeParameter(const std::string &name)
RealParameter * parameter(const std::string &name) const
Returns parameter with given 'name'.
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)
IParametricComponent(const std::string &name="")
RealParameter & registerParameter(const std::string &name, double *parpointer)
virtual ~IParametricComponent()
static std::string YComponentName(const std::string &base_name)
Container with parameters for IParametricComponent object.
Definition: ParameterPool.h:29
bool empty() const
Definition: ParameterPool.h:42
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:31
RealParameter & setUnit(const std::string &name)
MVVM_MODEL_EXPORT std::string base_name(const std::string &path)
Provide the filename of a file path.
Definition: fileutils.cpp:78
QString const & name(EShape k)
Definition: particles.cpp:21