From 9598215e7409e9ebfa24615fe85890ba5b9ba67c Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Fri, 12 Jul 2019 14:45:38 +0200 Subject: [PATCH] Le cas crue est dans la boite --- AdaoExchangeLayer4Quintet.cxx | 68 ++++------- AdaoExchangeLayer4Quintet.hxx | 5 +- AdaoModelKeyVal.cxx | 26 +++- TestAdaoExchange.cxx | 65 ++++++++-- TestAdaoExchange.hxx | 2 + TestAdaoHelper.cxx | 220 ++++++++++++++++++++++++++++++++++ 6 files changed, 322 insertions(+), 64 deletions(-) create mode 100644 TestAdaoHelper.cxx diff --git a/AdaoExchangeLayer4Quintet.cxx b/AdaoExchangeLayer4Quintet.cxx index 0825d1b..ea267bc 100644 --- a/AdaoExchangeLayer4Quintet.cxx +++ b/AdaoExchangeLayer4Quintet.cxx @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -215,10 +216,16 @@ AdaoExchangeLayer4Quintet::~AdaoExchangeLayer4Quintet() 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() @@ -242,23 +249,11 @@ class Visitor1 : public AdaoModel::PythonLeafVisitor { public: Visitor1(PyObjectRAII func, PyObject *context):_func(func),_context(context) -{ - std::vector< std::vector > 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 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++; @@ -268,23 +263,6 @@ public: 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++; @@ -294,22 +272,11 @@ public: 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) @@ -341,7 +308,12 @@ 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()) diff --git a/AdaoExchangeLayer4Quintet.hxx b/AdaoExchangeLayer4Quintet.hxx index bb11f33..0ef1405 100644 --- a/AdaoExchangeLayer4Quintet.hxx +++ b/AdaoExchangeLayer4Quintet.hxx @@ -35,14 +35,15 @@ class AdaoExchangeLayer4Quintet public: AdaoExchangeLayer4Quintet(); ~AdaoExchangeLayer4Quintet(); - void init(AdaoModel::MainModel *model); + PyObject *getPythonContext() const; + void init(); + void loadTemplate(AdaoModel::MainModel *model); void execute(); bool next(PyObject *& inputRequested); void setResult(PyObject *outputAssociated); PyObject *getResult(); private: void initPythonIfNeeded(); - void loadTemplate(AdaoModel::MainModel *model); private: Internal *_internal = nullptr; }; diff --git a/AdaoModelKeyVal.cxx b/AdaoModelKeyVal.cxx index b38c56f..b7b3aac 100644 --- a/AdaoModelKeyVal.cxx +++ b/AdaoModelKeyVal.cxx @@ -94,9 +94,15 @@ std::string TopEntry::getParamForSet(const GenericKeyVal& entry) const std::string GenericKeyVal::pyStrKeyVal() const { - std::ostringstream oss; - oss << "\"" << this->getKey() << "\" : " << this->pyStr(); - return oss.str(); + std::string val(this->pyStr()); + if( !val.empty() ) + { + std::ostringstream oss; + oss << "\"" << this->getKey() << "\" : " << val; + return oss.str(); + } + else + return std::string(); } void GenericKeyVal::visitAll(MainModel *godFather, RecursiveVisitor *visitor) @@ -212,13 +218,21 @@ std::shared_ptr EnumAlgoKeyVal::templateForOthers() const std::string DictKeyVal::pyStr() const { - std::ostringstream oss; - oss << "{ "; + std::vector vect; std::size_t len(_pairs.size()); for(std::size_t i=0;ipyStrKeyVal(); + std::string cont(elt->pyStrKeyVal()); + if( ! cont.empty() ) + vect.push_back(cont); + } + len = vect.size(); + std::ostringstream oss; + oss << "{ "; + for(std::size_t i=0;i #include -std::vector func(const std::vector& 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) { @@ -68,7 +65,7 @@ PyObject *multiFunc(PyObject *inp) } // PyGILState_Release(gstate); - std::vector res(func(vect)); + std::vector res(funcBase(vect)); gstate=PyGILState_Ensure(); // py2cpp::PyPtr resPy(py2cpp::toPyPtr(res)); @@ -116,7 +113,13 @@ void AdaoExchangeTest::test3DVar() { 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; @@ -163,7 +166,13 @@ void AdaoExchangeTest::testBlue() 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; @@ -210,7 +219,13 @@ void AdaoExchangeTest::testNonLinearLeastSquares() 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; @@ -236,6 +251,40 @@ void AdaoExchangeTest::testNonLinearLeastSquares() 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 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 diff --git a/TestAdaoExchange.hxx b/TestAdaoExchange.hxx index fd00d4b..b402309 100644 --- a/TestAdaoExchange.hxx +++ b/TestAdaoExchange.hxx @@ -28,6 +28,7 @@ class AdaoExchangeTest : public CppUnit::TestFixture CPPUNIT_TEST(test3DVar); CPPUNIT_TEST(testBlue); CPPUNIT_TEST(testNonLinearLeastSquares); + CPPUNIT_TEST(testCasCrue); CPPUNIT_TEST_SUITE_END(); public: void setUp(); @@ -37,4 +38,5 @@ public: void test3DVar(); void testBlue(); void testNonLinearLeastSquares(); + void testCasCrue(); }; diff --git a/TestAdaoHelper.cxx b/TestAdaoHelper.cxx new file mode 100644 index 0000000..09d8929 --- /dev/null +++ b/TestAdaoHelper.cxx @@ -0,0 +1,220 @@ +#include +#include "PyObjectRAII.hxx" + +#include + +/* func pour test3DVar testBlue et testNonLinearLeastSquares*/ +std::vector funcBase(const std::vector& 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 funcCrue(const std::vector& vec) +{ + double K_s(vec[0]); + constexpr double Qs[]={10.,20.,30.,40.}; + constexpr size_t LEN(sizeof(Qs)/sizeof(double)); + std::vector ret(LEN); + for(std::size_t i=0;i 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 vect; + { + py2cpp::PyPtr listPy2(listPy.retn()); + py2cpp::fromPyPtr(listPy2,vect); + } + // + PyGILState_Release(gstate); + std::vector 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 > 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 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 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; +}; + -- 2.30.2