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