-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestAdaoHelper.cxx
More file actions
169 lines (159 loc) · 5.22 KB
/
Copy pathTestAdaoHelper.cxx
File metadata and controls
169 lines (159 loc) · 5.22 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <vector>
#include "PyObjectRAII.hxx"
#include <cmath>
/* func pour test3DVar testBlue et testNonLinearLeastSquares*/
std::vector<double> funcBase(const std::vector<double>& vec)
{
return {vec[0],2.*vec[1],3.*vec[2],vec[0]+2.*vec[1]+3.*vec[2]};
}
double funcCrueInternal(double Q, double K_s)
{
constexpr double L(5.0e3);
constexpr double B(300.);
constexpr double Z_v(49.);
constexpr double Z_m(51.);
constexpr double alpha( (Z_m - Z_v)/L );
double H(pow((Q/(K_s*B*sqrt(alpha))),(3.0/5.0)));
return H;
}
/* func pour testCasCrue*/
std::vector<double> funcCrue(const std::vector<double>& vec)
{
double K_s(vec[0]);
constexpr double Qs[]={10.,20.,30.,40.};
constexpr size_t LEN(sizeof(Qs)/sizeof(double));
std::vector<double> ret(LEN);
for(std::size_t i=0;i<LEN;++i)
{
ret[i] = funcCrueInternal(Qs[i],K_s);
}
return ret;
}
/* Visitor commun pour test3DVar testBlue et testNonLinearLeastSquares*/
class Visitor2 : public AdaoModel::PythonLeafVisitor
{
public:
Visitor2(PyObject *context):_context(context)
{
std::vector< std::vector<double> > bounds{ {0., 10.}, {3., 13.}, {1.5, 15.5} };
std::vector< double > Xb{5.,7.,9.};
py2cpp::PyPtr boundsPy(py2cpp::toPyPtr(bounds));
_bounds = boundsPy.get();
Py_XINCREF(_bounds);
py2cpp::PyPtr XbPy(py2cpp::toPyPtr(Xb));
_Xb = XbPy.get();
Py_XINCREF(_Xb);
std::vector<double> observation{2., 6., 12., 20.};
py2cpp::PyPtr observationPy(py2cpp::toPyPtr(observation));
_observation = observationPy.get();
Py_XINCREF(_observation);
}
void visit(AdaoModel::MainModel *godFather, AdaoModel::PyObjKeyVal *obj) override
{
if(obj->getKey()=="Bounds")
{
std::ostringstream oss; oss << "___" << _cnt++;
std::string varname(oss.str());
obj->setVal(_bounds);
PyDict_SetItemString(_context,varname.c_str(),_bounds);
obj->setVarName(varname);
return ;
}
if(godFather->findPathOf(obj)=="Background/Vector")
{
std::ostringstream oss; oss << "___" << _cnt++;
std::string varname(oss.str());
obj->setVal(_Xb);
PyDict_SetItemString(_context,varname.c_str(),_Xb);
obj->setVarName(varname);
}
if(godFather->findPathOf(obj)=="Observation/Vector")
{
std::ostringstream oss; oss << "____" << _cnt++;
std::string varname(oss.str());
obj->setVal(_observation);
PyDict_SetItemString(_context,varname.c_str(),_observation);
obj->setVarName(varname);
}
}
private:
unsigned int _cnt = 0;
PyObject *_bounds = nullptr;
PyObject *_Xb = nullptr;
PyObject *_observation = nullptr;
PyObject *_context = nullptr;
};
/* Visitor pour testCasCrue */
class VisitorCruePython : public AdaoModel::PythonLeafVisitor
{
public:
VisitorCruePython(PyObject *context):_context(context)
{
{//case.set( 'Background', Vector=thetaB)
std::vector< double > Xb{ 20. };//thetaB
py2cpp::PyPtr XbPy(py2cpp::toPyPtr(Xb));
_Xb = XbPy.get();
Py_XINCREF(_Xb);
}
{//case.set( 'BackgroundError', DiagonalSparseMatrix=sigmaTheta )
std::vector< double > sigmaTheta{ 5.e10 };
py2cpp::PyPtr sigmaThetaPy(py2cpp::toPyPtr(sigmaTheta));
_sigmaTheta = sigmaThetaPy.get();
Py_XINCREF(_sigmaTheta);
}
{//case.set( 'Observation', Vector=Hobs)
std::vector<double> observation{0.19694513, 0.298513, 0.38073079, 0.45246109};
py2cpp::PyPtr observationPy(py2cpp::toPyPtr(observation));
_observation = observationPy.get();
Py_XINCREF(_observation);
}
{//case.set( 'ObservationError', ScalarSparseMatrix=sigmaH )
double sigmaH( 0.5);
py2cpp::PyPtr sigmaHPy(py2cpp::toPyPtr(sigmaH));
_sigmaH = sigmaHPy.get();
Py_XINCREF(_sigmaH);
}
}
void visit(AdaoModel::MainModel *godFather, AdaoModel::PyObjKeyVal *obj) override
{
if(godFather->findPathOf(obj)=="Background/Vector")
{
std::ostringstream oss; oss << "___" << _cnt++;
std::string varname(oss.str());
obj->setVal(_Xb);
PyDict_SetItemString(_context,varname.c_str(),_Xb);
obj->setVarName(varname);
}
if(godFather->findPathOf(obj)=="BackgroundError/Matrix")
{
std::ostringstream oss; oss << "___" << _cnt++;
std::string varname(oss.str());
obj->setVal(_sigmaTheta);
PyDict_SetItemString(_context,varname.c_str(),_Xb);
obj->setVarName(varname);
}
if(godFather->findPathOf(obj)=="Observation/Vector")
{
std::ostringstream oss; oss << "____" << _cnt++;
std::string varname(oss.str());
obj->setVal(_observation);
PyDict_SetItemString(_context,varname.c_str(),_observation);
obj->setVarName(varname);
}
if(godFather->findPathOf(obj)=="ObservationError/Matrix")
{
std::ostringstream oss; oss << "____" << _cnt++;
std::string varname(oss.str());
obj->setVal(_sigmaH);
PyDict_SetItemString(_context,varname.c_str(),_sigmaH);
obj->setVarName(varname);
}
}
private:
unsigned int _cnt = 0;
PyObject *_Xb = nullptr;
PyObject *_sigmaH = nullptr;
PyObject *_sigmaTheta = nullptr;
PyObject *_observation = nullptr;
PyObject *_context = nullptr;
};