]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
Extend operator __add__ of MEDCouplingFieldDouble
authorageay <ageay>
Tue, 9 Apr 2013 10:56:32 +0000 (10:56 +0000)
committerageay <ageay>
Tue, 9 Apr 2013 10:56:32 +0000 (10:56 +0000)
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py
src/MEDCoupling_Swig/MEDCouplingCommon.i
src/MEDCoupling_Swig/MEDCouplingMemArray.i

index af38ebbc99af70641d4bd03d7c685d9d6c8a0547..00914dc7aff8085a89e8d9ca8da57246fe102e2e 100644 (file)
@@ -11964,6 +11964,40 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         self.assertTrue(NodeField.getArray().isUniform(0.,1e-12))
         pass
 
+    def testSwigFieldOperationOpen1(self):
+        m=MEDCouplingDataForTest.build2DTargetMesh_1()
+        f=MEDCouplingFieldDouble(ON_CELLS)
+        f.setMesh(m)
+        arr=DataArrayDouble(5,2)
+        arr[:,0]=range(5) ; arr[:,1]=2*arr[:,0]
+        f2=f.clone(True)
+        self.assertRaises(InterpKernelException,f.__add__,2)
+        self.assertRaises(InterpKernelException,f.__add__,range(5))
+        self.assertRaises(InterpKernelException,f.__add__,arr)
+        self.assertRaises(InterpKernelException,f.__add__,f2)
+        f.setArray(DataArrayDouble())
+        self.assertRaises(InterpKernelException,f.__add__,2)
+        self.assertRaises(InterpKernelException,f.__add__,range(5))
+        self.assertRaises(InterpKernelException,f.__add__,arr)
+        self.assertRaises(InterpKernelException,f.__add__,f2)
+        self.assertRaises(InterpKernelException,f.__getitem__,(slice(None),0))
+        f.getArray().alloc(5,2)
+        f.getArray()[:,0]=range(5) ; f.getArray()[:,1]=f.getArray()[:,0]+7
+        ff=f+2
+        ff.checkCoherency()
+        self.assertTrue(ff.getArray().isEqual(DataArrayDouble([(2, 9), (3, 10), (4, 11), (5, 12), (6, 13)]),1e-12))
+        ff=f+arr
+        ff.checkCoherency()
+        self.assertTrue(ff.getArray().isEqual(DataArrayDouble([(0, 7), (2, 10), (4, 13), (6, 16), (8, 19)]),1e-12))
+        self.assertRaises(InterpKernelException,f.__add__,f2)
+        f2.setArray(arr)
+        ff=f+f2
+        ff.checkCoherency()
+        self.assertTrue(ff.getArray().isEqual(DataArrayDouble([(0, 7), (2, 10), (4, 13), (6, 16), (8, 19)]),1e-12))
+        ff=f+[5,8]
+        self.assertTrue(ff.getArray().isEqual(DataArrayDouble([(5, 15), (6, 16), (7, 17), (8, 18), (9, 19)]),1e-12))
+        pass
+
     def setUp(self):
         pass
     pass
index 5395c22c4195c0b6c0f048c96c10ad56b96ab520..602816a5f6d64f6a653480989bac306893fb39c9 100644 (file)
@@ -132,7 +132,7 @@ using namespace INTERP_KERNEL;
 %newobject ParaMEDMEM::MEDCouplingFieldDouble::getIdsInRange;
 %newobject ParaMEDMEM::MEDCouplingFieldDouble::buildSubPart;
 %newobject ParaMEDMEM::MEDCouplingFieldDouble::__getitem__;
-%newobject ParaMEDMEM::MEDCouplingFieldDouble::operator+;
+%newobject ParaMEDMEM::MEDCouplingFieldDouble::__add__;
 %newobject ParaMEDMEM::MEDCouplingFieldDouble::operator-;
 %newobject ParaMEDMEM::MEDCouplingFieldDouble::operator*;
 %newobject ParaMEDMEM::MEDCouplingFieldDouble::operator/;
@@ -2945,7 +2945,6 @@ namespace ParaMEDMEM
     static MEDCouplingFieldDouble *MultiplyFields(const MEDCouplingFieldDouble *f1, const MEDCouplingFieldDouble *f2) throw(INTERP_KERNEL::Exception);
     static MEDCouplingFieldDouble *DivideFields(const MEDCouplingFieldDouble *f1, const MEDCouplingFieldDouble *f2) throw(INTERP_KERNEL::Exception);
     MEDCouplingFieldDouble *min(const MEDCouplingFieldDouble& other) const throw(INTERP_KERNEL::Exception);
-    MEDCouplingFieldDouble *operator+(const MEDCouplingFieldDouble& other) const throw(INTERP_KERNEL::Exception);
     MEDCouplingFieldDouble *operator-(const MEDCouplingFieldDouble& other) const throw(INTERP_KERNEL::Exception);
     MEDCouplingFieldDouble *operator*(const MEDCouplingFieldDouble& other) const throw(INTERP_KERNEL::Exception);
     MEDCouplingFieldDouble *operator/(const MEDCouplingFieldDouble& other) const throw(INTERP_KERNEL::Exception);
@@ -3339,6 +3338,73 @@ namespace ParaMEDMEM
         return self->extractSlice3D(orig,vect,eps);
       }
 
+      MEDCouplingFieldDouble *__add__(PyObject *obj) throw(INTERP_KERNEL::Exception)
+      {
+        const char msg[]="Unexpected situation in MEDCouplingFieldDouble.__add__ ! Expecting a not null MEDCouplingFieldDouble or DataArrayDouble or DataArrayDoubleTuple instance, or a list of double, or a double.";
+        const char msg2[]="in MEDCouplingFieldDouble.__add__ : self field has no Array of values set !";
+        void *argp;
+        //
+        if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_ParaMEDMEM__MEDCouplingFieldDouble,0|0)))
+          {
+            MEDCouplingFieldDouble *other=reinterpret_cast< ParaMEDMEM::MEDCouplingFieldDouble * >(argp);
+            if(other)
+              return (*self)+(*other);
+            else
+              throw INTERP_KERNEL::Exception(msg);
+          }
+        //
+        double val;
+        DataArrayDouble *a;
+        DataArrayDoubleTuple *aa;
+        std::vector<double> bb;
+        int sw;
+        convertObjToPossibleCpp5(obj,sw,val,a,aa,bb);
+        switch(sw)
+          {
+          case 1:
+            {
+              if(!self->getArray())
+                throw INTERP_KERNEL::Exception(msg2);
+              MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=self->getArray()->deepCpy();
+              ret->applyLin(1.,val);
+              MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> ret2=self->clone(false);
+              ret2->setArray(ret);
+              return ret2.retn();
+            }
+          case 2:
+            {
+              if(!self->getArray())
+                throw INTERP_KERNEL::Exception(msg2);
+              MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::Add(self->getArray(),a);
+              MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> ret2=self->clone(false);
+              ret2->setArray(ret);
+              return ret2.retn();
+            }
+          case 3:
+            {
+              if(!self->getArray())
+                throw INTERP_KERNEL::Exception(msg2);
+              MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> aaa=aa->buildDADouble(1,self->getNumberOfComponents());
+              MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::Add(self->getArray(),aaa);
+              MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> ret2=self->clone(false);
+              ret2->setArray(ret);
+              return ret2.retn();
+            }
+          case 4:
+            {
+              if(!self->getArray())
+                throw INTERP_KERNEL::Exception(msg2);
+              MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> aaa=DataArrayDouble::New(); aaa->useArray(&bb[0],false,CPP_DEALLOC,1,(int)bb.size());
+              MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> ret=DataArrayDouble::Add(self->getArray(),aaa);
+              MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> ret2=self->clone(false);
+              ret2->setArray(ret);
+              return ret2.retn();
+            }
+          default:
+            { throw INTERP_KERNEL::Exception(msg); }
+          }
+      }
+
       PyObject *___iadd___(PyObject *trueSelf, const MEDCouplingFieldDouble& other) throw(INTERP_KERNEL::Exception)
       {
         *self+=other;
index ad08183b31a35c482d6328466757ba80c2845bab..6c6f1a0bf39fe5ec9aeb92425f9d885d26bc49c7 100644 (file)
@@ -209,7 +209,7 @@ namespace ParaMEDMEM
 
       static DataArrayDouble *New(PyObject *elt0, PyObject *nbOfTuples=0, PyObject *elt2=0) throw(INTERP_KERNEL::Exception)
       {
-        const char *msg="ParaMEDMEM::DataArrayDouble::New : Available API are : \n-DataArrayDouble.New()\n--DataArrayDouble.New([1.,3.,4.])\n-DataArrayDouble.New([1.,3.,4.],3)\n-DataArrayDouble.New([1.,3.,4.,5.],2,2)\n-DataArrayDouble.New(5)\n-DataArrayDouble.New(5,2) !";
+        const char *msg="ParaMEDMEM::DataArrayDouble::New : Available API are : \n-DataArrayDouble.New()\n-DataArrayDouble.New([1.,3.,4.])\n-DataArrayDouble.New([1.,3.,4.],3)\n-DataArrayDouble.New([1.,3.,4.,5.],2,2)\n-DataArrayDouble.New([1.,3.,4.,5.,7,8.],3,2)\n-DataArrayDouble.New(5)\n-DataArrayDouble.New(5,2) !";
         if(PyList_Check(elt0) || PyTuple_Check(elt0))
           {
             if(nbOfTuples)
@@ -2213,7 +2213,7 @@ namespace ParaMEDMEM
 
       static DataArrayInt *New(PyObject *elt0, PyObject *nbOfTuples=0, PyObject *nbOfComp=0) throw(INTERP_KERNEL::Exception)
       {
-        const char *msg="ParaMEDMEM::DataArrayInt::New : Available API are : \n-DataArrayInt.New()\n--DataArrayInt.New([1,3,4])\n-DataArrayInt.New([1,3,4],3)\n-DataArrayInt.New([1,3,4,5],2,2)\n-DataArrayInt.New(5)\n-DataArrayInt.New(5,2) !";
+        const char *msg="ParaMEDMEM::DataArrayInt::New : Available API are : \n-DataArrayInt.New()\n-DataArrayInt.New([1,3,4])\n-DataArrayInt.New([1,3,4],3)\n-DataArrayInt.New([1,3,4,5],2,2)\n-DataArrayInt.New([1,3,4,5,7,8],3,2)\n-DataArrayInt.New(5)\n-DataArrayInt.New(5,2) !";
         if(PyList_Check(elt0) || PyTuple_Check(elt0))
           {
             if(nbOfTuples)