]> SALOME platform Git repositories - tools/adao_interface.git/commitdiff
Salome HOME
Le cas crue est dans la boite
authorAnthony Geay <anthony.geay@edf.fr>
Fri, 12 Jul 2019 12:45:38 +0000 (14:45 +0200)
committerAnthony Geay <anthony.geay@edf.fr>
Fri, 12 Jul 2019 12:45:38 +0000 (14:45 +0200)
AdaoExchangeLayer4Quintet.cxx
AdaoExchangeLayer4Quintet.hxx
AdaoModelKeyVal.cxx
TestAdaoExchange.cxx
TestAdaoExchange.hxx
TestAdaoHelper.cxx [new file with mode: 0644]

index 0825d1b8a33c0173739bd864118892792b3992c7..ea267bc5c71a08f468d3873ec11484779ee0d6c5 100644 (file)
@@ -29,6 +29,7 @@
 #include <semaphore.h>
 
 #include <iostream>
+#include <fstream>
 #include <sstream>
 #include <clocale>
 #include <cstdlib>
@@ -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<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++;
@@ -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())
index bb11f3378f431db44871098fcb4d17f9b9bf8093..0ef1405d883d64933de7cb908a5b4bb066e1e486 100644 (file)
@@ -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;
 };
index b38c56f94872b678baf79b2071c150bf52827ba3..b7b3aac893ac041f11cd72f7de163c8f5a482130 100644 (file)
@@ -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<DictKeyVal> EnumAlgoKeyVal::templateForOthers() const
 
 std::string DictKeyVal::pyStr() const
 {
-  std::ostringstream oss;
-  oss << "{ ";
+  std::vector<std::string> vect;
   std::size_t len(_pairs.size());
   for(std::size_t i=0;i<len;++i)
     {
       const auto& elt(_pairs[i]);
-      oss << elt->pyStrKeyVal();
+      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<len;++i)
+    {
+      oss << vect[i];
       if(i!=len-1)
         oss << ", ";
     }
index f038e1d7a8a438744336052a75ceafcb61a57499..a690fe6a4dda8f638c8137f8e5850270836f0ffe 100644 (file)
 #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)
 {
@@ -68,7 +65,7 @@ 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));
@@ -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<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>
index fd00d4b5d4d7da725525aaaadeed7e5047e84e2a..b40230996b5fb0fb23af04b6fc91ea8fec202b6a 100644 (file)
@@ -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 (file)
index 0000000..09d8929
--- /dev/null
@@ -0,0 +1,220 @@
+#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;
+};
+