BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
INode.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Param/Node/INode.cpp
6 //! @brief Implements interface INode.
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 "Param/Node/INode.h"
16 #include "Base/Utils/Algorithms.h"
19 #include "Param/Node/NodeUtils.h"
20 #include <algorithm>
21 #include <exception>
22 
23 NodeMeta nodeMetaUnion(const std::vector<ParaMeta>& base, const NodeMeta& other)
24 {
25  return {other.className, other.tooltip, algo::concat(base, other.paraMeta)};
26 }
27 
28 INode::INode(const NodeMeta& meta, const std::vector<double>& PValues)
29  : /*m_tooltip(meta.tooltip),*/
30  m_NP(meta.paraMeta.size())
31 {
32  m_P.resize(m_NP);
33  setName(meta.className);
34  parameterPool()->clear(); // non-trivially needed by a few children
35  for (size_t i = 0; i < m_NP; ++i) {
36  m_P[i] = PValues[i];
37  const ParaMeta& pm = meta.paraMeta[i];
38  auto& reg = registerParameter(pm.name, &m_P[i]);
39  reg.setUnit(pm.unit);
40  if (pm.vMin == -INF) {
41  ASSERT(pm.vMax == +INF);
42  // nothing to do
43  } else if (pm.vMax == +INF) {
44  ASSERT(pm.vMin == 0);
45  reg.setNonnegative();
46  } else {
47  reg.setLimited(pm.vMin, pm.vMax);
48  }
49  }
50 }
51 
52 std::string INode::treeToString() const
53 {
54  return NodeUtils::nodeToString(this);
55 }
56 
58 {
59  ASSERT(node);
60  node->setParent(this);
61 }
62 
63 std::vector<const INode*> INode::getChildren() const
64 {
65  return {};
66 }
67 
68 std::vector<const INode*> INode::progeny() const
69 {
70  std::vector<const INode*> result;
71  result.push_back(this);
72  for (const auto* child : getChildren()) {
73  for (const auto* p : child->progeny())
74  result.push_back(p);
75  }
76  return result;
77 }
78 
79 void INode::setParent(const INode* newParent)
80 {
81  m_parent = newParent;
82 }
83 
84 const INode* INode::parent() const
85 {
86  return m_parent;
87 }
88 
90 {
91  return const_cast<INode*>(m_parent);
92 }
93 
94 int INode::copyNumber(const INode* node) const
95 {
96  if (node->parent() != this)
97  return -1;
98 
99  int result(-1), count(0);
100  for (auto child : getChildren()) {
101 
102  if (child == nullptr)
103  throw std::runtime_error("INode::copyNumber() -> Error. Nullptr as child.");
104 
105  if (child == node)
106  result = count;
107 
108  if (child->getName() == node->getName())
109  ++count;
110  }
111 
112  return count > 1 ? result : -1;
113 }
114 
115 std::string INode::displayName() const
116 {
117  std::string result = getName();
118  if (m_parent) {
119  int index = m_parent->copyNumber(this);
120  if (index >= 0)
121  result = result + std::to_string(index);
122  }
123  return result;
124 }
125 
127 {
128  std::unique_ptr<ParameterPool> result(new ParameterPool);
129 
130  for (const INode* child : progeny()) {
131  const std::string path = NodeUtils::nodePath(child, parent()) + "/";
132  child->parameterPool()->copyToExternalPool(path, result.get());
133  }
134 
135  return result.release();
136 }
Defines and implements namespace algo with some algorithms.
#define ASSERT(condition)
Definition: Assert.h:31
NodeMeta nodeMetaUnion(const std::vector< ParaMeta > &base, const NodeMeta &other)
Definition: INode.cpp:23
Defines interface INode.
const double INF
Definition: INode.h:25
Defines collection of utility functions for INode.
Defines class ParameterPool.
Defines class RealParameter.
Base class for tree-like structures containing parameterized objects.
Definition: INode.h:49
const INode * m_parent
Definition: INode.h:83
virtual std::string treeToString() const
Returns multiline string representing tree structure below the node.
Definition: INode.cpp:52
std::string displayName() const
Returns display name, composed from the name of node and it's copy number.
Definition: INode.cpp:115
virtual void setParent(const INode *newParent)
Definition: INode.cpp:79
INode()
Definition: INode.h:51
std::vector< const INode * > progeny() const
Returns a vector of all descendants.
Definition: INode.cpp:68
const INode * parent() const
Definition: INode.cpp:84
virtual std::vector< const INode * > getChildren() const
Returns a vector of children.
Definition: INode.cpp:63
ParameterPool * createParameterTree() const
Creates new parameter pool, with all local parameters and those of its children.
Definition: INode.cpp:126
std::vector< double > m_P
Definition: INode.h:89
void registerChild(INode *node)
Definition: INode.cpp:57
const size_t m_NP
Definition: INode.h:88
int copyNumber(const INode *node) const
Returns copyNumber of child, which takes into account existence of children with same name.
Definition: INode.cpp:94
const std::string & getName() const
ParameterPool * parameterPool() const
Returns pointer to the parameter pool.
void setName(const std::string &name)
RealParameter & registerParameter(const std::string &name, double *parpointer)
Container with parameters for IParametricComponent object.
Definition: ParameterPool.h:29
void clear()
Clears the parameter map.
std::string nodeToString(const INode *node)
Returns multiline string representing tree structure starting from given node.
Definition: NodeUtils.cpp:81
std::string nodePath(const INode *node, const INode *root=nullptr)
Returns path composed of node's displayName, with respect to root node.
Definition: NodeUtils.cpp:89
std::vector< T > concat(const std::vector< T > &v1, const std::vector< T > &v2)
Returns the concatenation of two std::vectors.
Definition: Algorithms.h:79
Metadata of one model node.
Definition: INode.h:38
std::string className
Definition: INode.h:39
std::string tooltip
Definition: INode.h:40
std::vector< ParaMeta > paraMeta
Definition: INode.h:41
Metadata of one model parameter.
Definition: INode.h:28
double vMin
Definition: INode.h:32
std::string unit
Definition: INode.h:30
double vMax
Definition: INode.h:33
std::string name
Definition: INode.h:29