#include <semaphore.h>
#include <iostream>
+#include <fstream>
#include <sstream>
#include <clocale>
#include <cstdlib>
delete _internal;
}
-void AdaoExchangeLayer4Quintet::init(AdaoModel::MainModel *model)
+void AdaoExchangeLayer4Quintet::init()
{
initPythonIfNeeded();
- loadTemplate(model);
+}
+
+PyObject *AdaoExchangeLayer4Quintet::getPythonContext() const
+{
+ if(!_internal)
+ throw AdaoExchangeLayerException("getPythonContext : not initialized !");
+ return _internal->_context;
}
void AdaoExchangeLayer4Quintet::initPythonIfNeeded()
{
public:
Visitor1(PyObjectRAII func, PyObject *context):_func(func),_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()=="Matrix" || obj->getKey()=="DiagonalSparseMatrix")
{
std::ostringstream oss; oss << "__" << _cnt++;
obj->setVarName(varname);
return ;
}
- 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(obj->getKey()=="OneFunction")
{
std::ostringstream oss; oss << "__" << _cnt++;
obj->setVarName(varname);
return ;
}
- 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;
PyObjectRAII _func;
- PyObject *_context;
+ PyObject *_context = nullptr;
};
void AdaoExchangeLayer4Quintet::loadTemplate(AdaoModel::MainModel *model)
//
{
std::string sciptPyOfModelMaker(model->pyStr());
+ {
+ std::ofstream ofs("/tmp/H87074/jj");
+ ofs << sciptPyOfModelMaker;
+ }
PyObjectRAII res(PyObjectRAII::FromNew(PyRun_String(sciptPyOfModelMaker.c_str(),Py_file_input,this->_internal->_context,this->_internal->_context)));
+ PyErr_Print();
_internal->_adao_case = PyObjectRAII::FromNew(PyDict_GetItemString(this->_internal->_context,"case"));
}
if(_internal->_adao_case.isNull())
#include <vector>
#include <iterator>
-std::vector<double> func(const std::vector<double>& vec)
-{
- return {vec[0],2.*vec[1],3.*vec[2],vec[0]+2.*vec[1]+3.*vec[2]};
-}
+#include "TestAdaoHelper.cxx"
PyObject *multiFunc(PyObject *inp)
{
}
//
PyGILState_Release(gstate);
- std::vector<double> res(func(vect));
+ std::vector<double> res(funcBase(vect));
gstate=PyGILState_Ensure();
//
py2cpp::PyPtr resPy(py2cpp::toPyPtr(res));
{
MainModel mm;
AdaoExchangeLayer4Quintet adao;
- adao.init(&mm);
+ adao.init();
+ // For bounds, Background/Vector, Observation/Vector
+ Visitor2 visitorPythonObj(adao.getPythonContext());
+ mm.visitPythonLeaves(&visitorPythonObj);
+ //
+ adao.loadTemplate(&mm);
+ //
{
std::string sciptPyOfModelMaker(mm.pyStr());
std::cerr << sciptPyOfModelMaker << std::endl;
mm.visitAll(&vis);
//
AdaoExchangeLayer4Quintet adao;
- adao.init(&mm);
+ adao.init();
+ // For bounds, Background/Vector, Observation/Vector
+ Visitor2 visitorPythonObj(adao.getPythonContext());
+ mm.visitPythonLeaves(&visitorPythonObj);
+ //
+ adao.loadTemplate(&mm);
+ //
{
std::string sciptPyOfModelMaker(mm.pyStr());
std::cerr << sciptPyOfModelMaker << std::endl;
mm.visitAll(&vis);
//
AdaoExchangeLayer4Quintet adao;
- adao.init(&mm);
+ adao.init();
+ // For bounds, Background/Vector, Observation/Vector
+ Visitor2 visitorPythonObj(adao.getPythonContext());
+ mm.visitPythonLeaves(&visitorPythonObj);
+ //
+ adao.loadTemplate(&mm);
+ //
{
std::string sciptPyOfModelMaker(mm.pyStr());
std::cerr << sciptPyOfModelMaker << std::endl;
CPPUNIT_ASSERT_DOUBLES_EQUAL(4.,vect[2],1e-7);
}
+void AdaoExchangeTest::testCasCrue()
+{
+ MainModel mm;
+ AdaoExchangeLayer4Quintet adao;
+ adao.init();
+ // For bounds, Background/Vector, Observation/Vector
+ VisitorCruePython visitorPythonObj(adao.getPythonContext());
+ mm.visitPythonLeaves(&visitorPythonObj);
+ //
+ adao.loadTemplate(&mm);
+ //
+ {
+ std::string sciptPyOfModelMaker(mm.pyStr());
+ std::cerr << sciptPyOfModelMaker << std::endl;
+ }
+ adao.execute();
+ PyObject *listOfElts( nullptr );
+ while( adao.next(listOfElts) )
+ {
+ PyObject *resultOfChunk(multiFuncCrue(listOfElts));
+ adao.setResult(resultOfChunk);
+ }
+ PyObject *res(adao.getResult());
+ PyObjectRAII optimum(PyObjectRAII::FromNew(res));
+ PyObjectRAII optimum_4_py2cpp(NumpyToListWaitingForPy2CppManagement(optimum));
+ std::vector<double> vect;
+ {
+ py2cpp::PyPtr obj(optimum_4_py2cpp);
+ py2cpp::fromPyPtr(obj,vect);
+ }
+ CPPUNIT_ASSERT_EQUAL(1,(int)vect.size());
+ CPPUNIT_ASSERT_DOUBLES_EQUAL(25.,vect[0],1e-3);
+}
+
CPPUNIT_TEST_SUITE_REGISTRATION( AdaoExchangeTest );
#include <cppunit/CompilerOutputter.h>
--- /dev/null
+#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;
+}
+
+PyObject *multiFuncCrue(PyObject *inp)
+{
+ PyGILState_STATE gstate(PyGILState_Ensure());
+ PyObjectRAII iterator(PyObjectRAII::FromNew(PyObject_GetIter(inp)));
+ if(iterator.isNull())
+ throw AdaoExchangeLayerException("Input object is not iterable !");
+ PyObject *item(nullptr);
+ PyObjectRAII numpyModule(PyObjectRAII::FromNew(PyImport_ImportModule("numpy")));
+ if(numpyModule.isNull())
+ throw AdaoExchangeLayerException("Failed to load numpy");
+ PyObjectRAII ravelFunc(PyObjectRAII::FromNew(PyObject_GetAttrString(numpyModule,"ravel")));
+ std::vector< PyObjectRAII > pyrets;
+ while( item = PyIter_Next(iterator) )
+ {
+ PyObjectRAII item2(PyObjectRAII::FromNew(item));
+ {
+ PyObjectRAII args(PyObjectRAII::FromNew(PyTuple_New(1)));
+ PyTuple_SetItem(args,0,item2.retn());
+ PyObjectRAII npyArray(PyObjectRAII::FromNew(PyObject_CallObject(ravelFunc,args)));
+ // Waiting management of npy arrays into py2cpp
+ PyObjectRAII lolistFunc(PyObjectRAII::FromNew(PyObject_GetAttrString(npyArray,"tolist")));
+ PyObjectRAII listPy;
+ {
+ PyObjectRAII args2(PyObjectRAII::FromNew(PyTuple_New(0)));
+ listPy=PyObjectRAII::FromNew(PyObject_CallObject(lolistFunc,args2));
+ }
+ std::vector<double> vect;
+ {
+ py2cpp::PyPtr listPy2(listPy.retn());
+ py2cpp::fromPyPtr(listPy2,vect);
+ }
+ //
+ PyGILState_Release(gstate);
+ std::vector<double> res(funcCrue(vect));
+ gstate=PyGILState_Ensure();
+ //
+ py2cpp::PyPtr resPy(py2cpp::toPyPtr(res));
+ PyObjectRAII resPy2(PyObjectRAII::FromBorrowed(resPy.get()));
+ pyrets.push_back(resPy2);
+ }
+ }
+ std::size_t len(pyrets.size());
+ PyObjectRAII ret(PyObjectRAII::FromNew(PyList_New(len)));
+ for(std::size_t i=0;i<len;++i)
+ {
+ PyList_SetItem(ret,i,pyrets[i].retn());
+ }
+ //PyObject *tmp(PyObject_Repr(ret));
+ // std::cerr << PyUnicode_AsUTF8(tmp) << std::endl;
+ PyGILState_Release(gstate);
+ return ret.retn();
+}
+
+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;
+};
+
+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;
+};
+