File: INode.h

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (134 lines) | stat: -rw-r--r-- 4,211 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Param/Node/INode.h
//! @brief     Defines interface INode.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#ifndef BORNAGAIN_PARAM_NODE_INODE_H
#define BORNAGAIN_PARAM_NODE_INODE_H

#include <limits>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

const double INF = std::numeric_limits<double>::infinity();

//! Metadata of one model parameter.
struct ParaMeta {
    std::string name;
    std::string unit;
};

//! Base class for tree-like structures containing parameterized objects.

class INode {
public:
    INode() = default;
    INode(const std::vector<double>& PValues);

    virtual ~INode() = default;

    //! Returns the class name, to be hard-coded in each leaf class that inherits from INode.
    virtual std::string className() const = 0;

#ifndef SWIG
    //! Returns all children.
    virtual std::vector<const INode*> nodeChildren() const;

    //! Returns all descendants. // TODO sep22: rm if it remains UNUSED
    std::vector<const INode*> nodeOffspring() const;
#endif // SWIG

    //! Returns the parameter definitions, to be hard-coded in each leaf class.
    virtual std::vector<ParaMeta> parDefs() const { return {}; }

    const std::vector<double>& pars() const { return m_P; }

    //! Returns number of actual parameters.
    size_t nPars() const { return m_P.size(); }

    //! Returns value of specified parameter.
    double parVal(size_t i) const { return m_P.at(i); }

    virtual std::string validate() const { return ""; }
    void validateOrThrow() const;

protected:
    std::vector<double> m_P;

    mutable bool m_validated{false};

    static void requestGt0(std::vector<std::string>& errs, const double& val,
                           const std::string& name);
    static void requestGe0(std::vector<std::string>& errs, const double& val,
                           const std::string& name);
    static void requestIn(std::vector<std::string>& errs, const double& val,
                          const std::string& name, double min, double max);

    std::string jointError(const std::vector<std::string> errs) const;
};

//  ************************************************************************************************
//  vector concatenation, for use in nodeChildren functions
//  ************************************************************************************************

#ifndef SWIG

template <typename T>
std::vector<const INode*>& operator<<(std::vector<const INode*>& v_node,
                                      const std::unique_ptr<T>& node)
{
    if (node)
        v_node.push_back(node.get());
    return v_node;
}

template <typename T>
std::vector<const INode*>& operator<<(std::vector<const INode*>&& v_node,
                                      const std::unique_ptr<T>& node)
{
    if (node)
        v_node.push_back(node.get());
    return v_node;
}

inline std::vector<const INode*>& operator<<(std::vector<const INode*>& v_node, const INode* node)
{
    v_node.push_back(node);
    return v_node;
}

inline std::vector<const INode*>& operator<<(std::vector<const INode*>&& v_node, const INode* node)
{
    v_node.push_back(node);
    return v_node;
}

inline std::vector<const INode*>& operator<<(std::vector<const INode*>& v_node,
                                             const std::vector<const INode*>& other)
{
    v_node.insert(v_node.end(), other.begin(), other.end());
    return v_node;
}

inline std::vector<const INode*>& operator<<(std::vector<const INode*>&& v_node,
                                             const std::vector<const INode*>& other)
{
    v_node.insert(v_node.end(), other.begin(), other.end());
    return v_node;
}

#endif // SWIG

#endif // BORNAGAIN_PARAM_NODE_INODE_H