From 0970cbef649b064f045fba1ce8e238e67732a4cd Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Thu, 31 Aug 2017 15:19:31 +0200 Subject: [PATCH] Continuing testing --- src/MEDCoupling_Swig/MEDCouplingCommon.i | 3 + .../MEDCouplingDataArrayTypemaps.i | 69 ++++++++++- src/MEDCoupling_Swig/MEDCouplingMemArray.i | 56 +-------- src/MEDLoader/Swig/MEDLoaderTest3.py | 113 ++++++++++++++++++ 4 files changed, 188 insertions(+), 53 deletions(-) diff --git a/src/MEDCoupling_Swig/MEDCouplingCommon.i b/src/MEDCoupling_Swig/MEDCouplingCommon.i index ddf21af6d..71dc5889d 100644 --- a/src/MEDCoupling_Swig/MEDCouplingCommon.i +++ b/src/MEDCoupling_Swig/MEDCouplingCommon.i @@ -485,6 +485,9 @@ using namespace INTERP_KERNEL; SWIGTITraits::TI_TUPLE=SWIGTYPE_p_MEDCoupling__DataArrayDoubleTuple; SWIGTITraits::TI_TUPLE=SWIGTYPE_p_MEDCoupling__DataArrayFloatTuple; SWIGTITraits::TI_TUPLE=SWIGTYPE_p_MEDCoupling__DataArrayIntTuple; + SWIGTITraits::TI_FIELD=SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble; + SWIGTITraits::TI_FIELD=SWIGTYPE_p_MEDCoupling__MEDCouplingFieldFloat; + SWIGTITraits::TI_FIELD=SWIGTYPE_p_MEDCoupling__MEDCouplingFieldInt; } %} diff --git a/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i b/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i index 66cb1d33c..7962865b8 100644 --- a/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i +++ b/src/MEDCoupling_Swig/MEDCouplingDataArrayTypemaps.i @@ -27,6 +27,8 @@ #include "InterpKernelAutoPtr.hxx" #include "MEDCouplingDataArrayTraits.hxx" +#include "MEDCouplingFieldDouble.hxx" +#include "MEDCouplingFieldFloat.hxx" #include @@ -3320,15 +3322,15 @@ struct SWIGTITraits template<> struct SWIGTITraits -{ static swig_type_info *TI; static swig_type_info *TI_TUPLE; }; +{ static swig_type_info *TI; static swig_type_info *TI_TUPLE; static swig_type_info *TI_FIELD; }; template<> struct SWIGTITraits -{ static swig_type_info *TI; static swig_type_info *TI_TUPLE; }; +{ static swig_type_info *TI; static swig_type_info *TI_TUPLE; static swig_type_info *TI_FIELD; }; template<> struct SWIGTITraits -{ static swig_type_info *TI; static swig_type_info *TI_TUPLE; }; +{ static swig_type_info *TI; static swig_type_info *TI_TUPLE; static swig_type_info *TI_FIELD; }; swig_type_info *SWIGTITraits::TI=NULL;//unfortunately SWIGTYPE_p_MEDCoupling__DataArrayDouble is null when called here ! Postpone initialization at inlined initializeMe() swig_type_info *SWIGTITraits::TI=NULL;//unfortunately SWIGTYPE_p_MEDCoupling__DataArrayFloat is null when called here ! Postpone initialization at inlined initializeMe() @@ -3336,6 +3338,9 @@ swig_type_info *SWIGTITraits::TI=NULL;//unfortunately SWIGTYPE_p_MEDCouplin swig_type_info *SWIGTITraits::TI_TUPLE=NULL;//unfortunately SWIGTYPE_p_MEDCoupling__DataArrayDouble is null when called here ! Postpone initialization at inlined initializeMe() swig_type_info *SWIGTITraits::TI_TUPLE=NULL;//unfortunately SWIGTYPE_p_MEDCoupling__DataArrayFloat is null when called here ! Postpone initialization at inlined initializeMe() swig_type_info *SWIGTITraits::TI_TUPLE=NULL;//unfortunately SWIGTYPE_p_MEDCoupling__DataArrayFloat is null when called here ! Postpone initialization at inlined initializeMe() +swig_type_info *SWIGTITraits::TI_FIELD=NULL;//unfortunately SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble is null when called here ! Postpone initialization at inlined initializeMe() +swig_type_info *SWIGTITraits::TI_FIELD=NULL;//unfortunately SWIGTYPE_p_MEDCoupling__MEDCouplingFieldFload is null when called here ! Postpone initialization at inlined initializeMe() +swig_type_info *SWIGTITraits::TI_FIELD=NULL;//unfortunately SWIGTYPE_p_MEDCoupling__MEDCouplingFieldInt is null when called here ! Postpone initialization at inlined initializeMe() #ifdef WITH_NUMPY PyTypeObject *NPYTraits::NPYFunc=&PyCallBackDataArrayDouble_RefType; @@ -3412,4 +3417,62 @@ typename MEDCoupling::Traits::ArrayType *DataArrayFPT_rmul(typename MEDCoupli } } +template +PyObject *DataArrayFPT__add__(typename MEDCoupling::Traits::ArrayType *self, PyObject *obj, typename MEDCoupling::Traits::FieldType *(*field_radd)(typename MEDCoupling::Traits::FieldType *, PyObject *)) +{ + const char msg[]="Unexpected situation in DataArrayDouble.__add__ !"; + T val; + typename MEDCoupling::Traits::ArrayType *a; + typename MEDCoupling::Traits::ArrayTuple *aa; + std::vector bb; + int sw; + // +#ifndef WITHOUT_AUTOFIELD + void *argp; + if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,0|0))) + { + typename MEDCoupling::Traits::FieldType *other=reinterpret_cast< typename MEDCoupling::Traits::FieldType * >(argp); + if(other) + { + PyObject *tmp=SWIG_NewPointerObj(SWIG_as_voidptr(self),SWIGTYPE_p_MEDCoupling__DataArrayDouble, 0 | 0 ); + MEDCoupling::MCAuto::FieldType> ret; + if(field_radd) + ret=field_radd(other,tmp); + Py_XDECREF(tmp); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret.retn()),SWIGTITraits::TI_FIELD, SWIG_POINTER_OWN | 0 ); + } + else + throw INTERP_KERNEL::Exception(msg); + } +#endif + // + //convertDoubleStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb); + convertFPStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb,SWIGTITraits::TI,SWIGTITraits::TI_TUPLE); + switch(sw) + { + case 1: + { + MEDCoupling::MCAuto::ArrayType> ret=self->deepCopy(); + ret->applyLin(1.,val); + return SWIG_NewPointerObj(SWIG_as_voidptr(ret.retn()),SWIGTITraits::TI, SWIG_POINTER_OWN | 0 ); + } + case 2: + { + return SWIG_NewPointerObj(SWIG_as_voidptr(MEDCoupling::Traits::ArrayType::Add(self,a)),SWIGTITraits::TI, SWIG_POINTER_OWN | 0 ); + } + case 3: + { + MEDCoupling::MCAuto::ArrayType> aaa(aa->buildDA(1,self->getNumberOfComponents())); + return SWIG_NewPointerObj(SWIG_as_voidptr(MEDCoupling::Traits::ArrayType::Add(self,aaa)),SWIGTITraits::TI, SWIG_POINTER_OWN | 0 ); + } + case 4: + { + MEDCoupling::MCAuto::ArrayType> aaa(MEDCoupling::Traits::ArrayType::New()); aaa->useArray(&bb[0],false,MEDCoupling::CPP_DEALLOC,1,(int)bb.size()); + return SWIG_NewPointerObj(SWIG_as_voidptr(MEDCoupling::Traits::ArrayType::Add(self,aaa)),SWIGTITraits::TI, SWIG_POINTER_OWN | 0 ); + } + default: + throw INTERP_KERNEL::Exception(msg); + } +} + #endif diff --git a/src/MEDCoupling_Swig/MEDCouplingMemArray.i b/src/MEDCoupling_Swig/MEDCouplingMemArray.i index 17a682bdb..8c4c04593 100644 --- a/src/MEDCoupling_Swig/MEDCouplingMemArray.i +++ b/src/MEDCoupling_Swig/MEDCouplingMemArray.i @@ -758,6 +758,11 @@ namespace MEDCoupling PyTuple_SetItem(ret,1,PyString_FromString(ret1.c_str())); return ret; } + + PyObject *__add__(PyObject *obj) throw(INTERP_KERNEL::Exception) + { + return DataArrayFPT__add__(self,obj,NULL);//MEDCoupling_MEDCouplingFieldDouble___radd__Impl); + } PyObject *__getitem__(PyObject *obj) throw(INTERP_KERNEL::Exception) { @@ -1471,56 +1476,7 @@ namespace MEDCoupling PyObject *__add__(PyObject *obj) throw(INTERP_KERNEL::Exception) { - const char msg[]="Unexpected situation in DataArrayDouble.__add__ !"; - double val; - DataArrayDouble *a; - DataArrayDoubleTuple *aa; - std::vector bb; - int sw; - // -#ifndef WITHOUT_AUTOFIELD - void *argp; - if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,0|0))) - { - MEDCouplingFieldDouble *other=reinterpret_cast< MEDCoupling::MEDCouplingFieldDouble * >(argp); - if(other) - { - PyObject *tmp=SWIG_NewPointerObj(SWIG_as_voidptr(self),SWIGTYPE_p_MEDCoupling__DataArrayDouble, 0 | 0 ); - MCAuto ret=MEDCoupling_MEDCouplingFieldDouble___radd__Impl(other,tmp); - Py_XDECREF(tmp); - return SWIG_NewPointerObj(SWIG_as_voidptr(ret.retn()),SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble, SWIG_POINTER_OWN | 0 ); - } - else - throw INTERP_KERNEL::Exception(msg); - } -#endif - // - convertDoubleStarLikePyObjToCpp_2(obj,sw,val,a,aa,bb); - switch(sw) - { - case 1: - { - MCAuto ret=self->deepCopy(); - ret->applyLin(1.,val); - return SWIG_NewPointerObj(SWIG_as_voidptr(ret.retn()),SWIGTYPE_p_MEDCoupling__DataArrayDouble, SWIG_POINTER_OWN | 0 ); - } - case 2: - { - return SWIG_NewPointerObj(SWIG_as_voidptr(DataArrayDouble::Add(self,a)),SWIGTYPE_p_MEDCoupling__DataArrayDouble, SWIG_POINTER_OWN | 0 ); - } - case 3: - { - MCAuto aaa=aa->buildDADouble(1,self->getNumberOfComponents()); - return SWIG_NewPointerObj(SWIG_as_voidptr(DataArrayDouble::Add(self,aaa)),SWIGTYPE_p_MEDCoupling__DataArrayDouble, SWIG_POINTER_OWN | 0 ); - } - case 4: - { - MCAuto aaa=DataArrayDouble::New(); aaa->useArray(&bb[0],false,CPP_DEALLOC,1,(int)bb.size()); - return SWIG_NewPointerObj(SWIG_as_voidptr(DataArrayDouble::Add(self,aaa)),SWIGTYPE_p_MEDCoupling__DataArrayDouble, SWIG_POINTER_OWN | 0 ); - } - default: - throw INTERP_KERNEL::Exception(msg); - } + return DataArrayFPT__add__(self,obj,MEDCoupling_MEDCouplingFieldDouble___radd__Impl); } DataArrayDouble *__radd__(PyObject *obj) throw(INTERP_KERNEL::Exception) diff --git a/src/MEDLoader/Swig/MEDLoaderTest3.py b/src/MEDLoader/Swig/MEDLoaderTest3.py index 3c1945f44..b34c22d62 100644 --- a/src/MEDLoader/Swig/MEDLoaderTest3.py +++ b/src/MEDLoader/Swig/MEDLoaderTest3.py @@ -5926,6 +5926,12 @@ class MEDLoaderTest3(unittest.TestCase): self.assertEqual(len(fs4[0]),len(fmts5)) self.assertTrue(isinstance(fmts5,MEDFileFloatFieldMultiTS)) self.assertTrue(fmts5[0].field(ms4[0]).isEqual((fs4[0][0]).field(ms4[0]),1e-12,1e-7)) + # MEDFileFloatField1TS + self.assertTrue(isinstance(fs4[0][0],MEDFileFloatField1TS)) + st=pickle.dumps(fs4[0][0],pickle.HIGHEST_PROTOCOL) + f1ts6=pickle.loads(st) + self.assertTrue(isinstance(f1ts6,MEDFileFloatField1TS)) + self.assertTrue(f1ts6.field(ms4[0]).isEqual((fs4[0][0]).field(ms4[0]),1e-12,1e-7)) pass def testGlobalNumOnNodes1(self): @@ -6112,6 +6118,113 @@ class MEDLoaderTest3(unittest.TestCase): self.assertTrue(not fs.changeMeshNames([('3DSurfMesh_1','3DSurfMesh')])) pass + def testAppendFieldProfileOnFloatField(self): + fname="Pyfile115.med" + arrX=DataArrayDouble([0,1,2,3]) + arrY=DataArrayDouble([0,1,2]) + mesh=MEDCouplingCMesh() ; mesh.setCoords(arrX,arrY) ; mesh.setName("Mesh") + mm=MEDFileCMesh() + mm.setMesh(mesh) + # + fmts=MEDFileFloatFieldMultiTS() + pflName="PFL" + pfl=DataArrayInt([1,3,5]) ; pfl.setName(pflName) + f=MEDCouplingFieldFloat(ON_CELLS) ; f.setMesh(mesh) + fieldName="FieldOnCell" + f.setTime(1.2,1,1) ; f.setName(fieldName) + arr=DataArrayFloat([101,102,103]) ; f.setArray(arr) + fmts.appendFieldProfile(f,mm,0,pfl) + # + mm.write(fname,2) + fmts.write(fname,0) + # + mm=MEDFileMesh.New(fname) + fmts=MEDFileAnyTypeFieldMultiTS.New(fname) + self.assertTrue(isinstance(fmts,MEDFileFloatFieldMultiTS)) + self.assertEqual(fmts.getName(),fieldName) + self.assertEqual(len(fmts),1) + f1ts=fmts[0] + ftest,pfltest=f1ts.getFieldWithProfile(ON_CELLS,0,mm) + self.assertEqual(pfltest.getName(),pflName) + self.assertEqual(ftest.getName(),fieldName) + self.assertTrue(ftest.isEqualWithoutConsideringStr(arr,1e-7)) + ftest2=f1ts.getFieldOnMeshAtLevel(ON_CELLS,0,mm) + self.assertTrue(ftest2.getArray().isEqualWithoutConsideringStr(arr,1e-7)) + self.assertEqual(ftest2.getTime(),f.getTime()) + self.assertEqual(ftest2.getMesh().getNumberOfCells(),len(arr)) + pass + + def testMEDFileFieldEasyField6(self): + """Same thantestMEDFileFieldEasyField1 except that here intfields are considered. + Check for all spatial discretization of field (cells,nodes,elno,gauss) for int field that all is OK. Here no profile and only top level is considered.""" + ## Basic test on cells on top level + fname="Pyfile116.med" + fieldName="field1" + mm=MEDFileUMesh() + coo=DataArrayDouble([(3,2,1),(8,7,6),(5,9,10)]) + m=MEDCouplingUMesh("mesh",2) ; m.setCoords(coo) + m.allocateCells() + m.insertNextCell(NORM_TRI3,[0,1,2]) + m.insertNextCell(NORM_TRI3,[3,4,5]) + m.insertNextCell(NORM_TRI3,[6,7,8]) + m.insertNextCell(NORM_TRI3,[9,10,11]) + m.insertNextCell(NORM_QUAD4,[100,101,102,103]) + m.insertNextCell(NORM_QUAD4,[104,105,106,107]) + mm[0]=m + mm.write(fname,2) + arr0=DataArrayFloat([10,11,12,13,100,101]) + f=MEDCouplingFieldFloat(ON_CELLS) ; f.setArray(arr0) ; f.setMesh(m) + f.setName(fieldName) ; f.setTime(2.,6,7) + f0=f.deepCopy() + ff=MEDFileFloatFieldMultiTS() ; ff.appendFieldNoProfileSBT(f) + ff.write(fname,0) + arr2=arr0+1000 ; f.setArray(arr2) + f.setTime(3.,8,9) ; ff=MEDFileFloatField1TS() ; ff.setFieldNoProfileSBT(f) + ff.write(fname,0) + f1=f.deepCopy() + ## + mm=MEDFileMesh.New(fname) + f1ts=MEDFileFloatField1TS(fname,fieldName,6,7) + ftst0=f1ts.field(mm) + self.assertTrue(f0.isEqual(ftst0,1e-12,0)) + f1ts=MEDFileFloatField1TS(fname,fieldName,8,9) + ftst1=f1ts.field(mm) + self.assertTrue(f1.isEqual(ftst1,1e-12,0)) + fmts=MEDFileFloatFieldMultiTS(fname,fieldName) + tmp=fmts.field(8,9,mm) + self.assertTrue(f1.isEqual(fmts.field(8,9,mm),1e-12,0)) + ## Basic test on nodes on top level + f2=MEDCouplingFieldFloat(ON_NODES) ; arr2=DataArrayFloat([200,201,202]) ; arr2.setInfoOnComponent(0,"tutu") ; f2.setArray(arr2) ; f2.setMesh(m) ; f2.setTime(22.,23,24) + f2.setName(fieldName) + mm.write(fname,2) + ff=MEDFileFloatField1TS() ; ff.setFieldNoProfileSBT(f2) ; ff.write(fname,0) + # + mm=MEDFileMesh.New(fname) + f1ts=MEDFileFloatField1TS(fname,fieldName,23,24) + self.assertTrue(f2.isEqual(f1ts.field(mm),1e-12,0)) + fmts=MEDFileFloatFieldMultiTS(fname,fieldName) + self.assertTrue(f2.isEqual(fmts.field(23,24,mm),1e-12,0)) + ## Node on elements + f3=MEDCouplingFieldFloat(ON_GAUSS_NE) ; f3.setMesh(m) ; arr3=DataArrayFloat([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]) ; f3.setArray(arr3) ; f3.setTime(0.5,2,3) + f3.setName(fieldName) ; f3.checkConsistencyLight() + mm.write(fname,2) ; ff=MEDFileFloatField1TS() ; ff.setFieldNoProfileSBT(f3) ; ff.write(fname,0) + # + mm=MEDFileMesh.New(fname) + f1ts=MEDFileFloatField1TS(fname,fieldName,2,3) + self.assertTrue(f3.isEqual(f1ts.field(mm),1e-12,0)) + ## Gauss + f4=MEDCouplingFieldFloat(ON_GAUSS_PT) ; f4.setMesh(m) ; f4.setName(fieldName) + f4.setGaussLocalizationOnType(NORM_TRI3,[0.,0.,1.,0.,1.,1.],[0.1,0.1, 0.2,0.2, 0.3,0.3, 0.4,0.4, 0.5,0.5],[0.2,0.3,0.1,0.05,0.35]) + f4.setGaussLocalizationOnType(NORM_QUAD4,[0.,0.,1.,0.,1.,1.,0.,1.],[0.3,0.4, 0.6,0.7],[0.7,0.3]) ; f4.setTime(0.25,4,5) + arr4=DataArrayFloat([0,1,2,3,4 ,10,11,12,13,14, 20,21,22,23,24, 30,31,32,33,34, 45,46, 55,56]) ; arr4.setInfoOnComponent(0,"abc") ; f4.setArray(arr4) + f4.checkConsistencyLight() + mm.write(fname,2) ; ff=MEDFileFloatField1TS() ; ff.setFieldNoProfileSBT(f4) ; ff.write(fname,0) + # + mm=MEDFileMesh.New(fname) + f1ts=MEDFileFloatField1TS(fname,fieldName,4,5) + self.assertTrue(f4.isEqual(f1ts.field(mm),1e-12,0)) + pass + pass if __name__ == "__main__": -- 2.39.2