From: Anthony Geay Date: Wed, 2 Nov 2016 16:24:28 +0000 (+0100) Subject: MEDFileMeshes, Fields FieldMultiTS Field1TS IntField1TS IntFieldMultiTS are now picke... X-Git-Tag: V8_2_0a1~4 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=55662ac42973d476653297249e379bc10d8eac53;p=tools%2Fmedcoupling.git MEDFileMeshes, Fields FieldMultiTS Field1TS IntField1TS IntFieldMultiTS are now pickelized --- diff --git a/src/MEDLoader/MEDFileUtilities.cxx b/src/MEDLoader/MEDFileUtilities.cxx index 673f0a69e..46354964f 100644 --- a/src/MEDLoader/MEDFileUtilities.cxx +++ b/src/MEDLoader/MEDFileUtilities.cxx @@ -206,14 +206,13 @@ MEDCoupling::MCAuto MEDCoupling::MEDFileWritableStan memfile.app_image_size=0; // std::string dftFileName(GenerateUniqueDftFileNameInMem()); - MEDFileUtilities::AutoFid fid(MEDmemFileOpen(dftFileName.c_str(),&memfile,MED_FALSE,MED_ACC_CREAT)); - writeLL(fid); + {// very important to let this braces ! The AutoFid destructor must be called, to have a "clean" memfile.app_image_ptr pointer embedded in the returned object. + MEDFileUtilities::AutoFid fid(MEDmemFileOpen(dftFileName.c_str(),&memfile,MED_FALSE,MED_ACC_CREAT)); + writeLL(fid); + } // MEDCoupling::MCAuto ret(MEDCoupling::DataArrayByte::New()); - //ret->useArray(reinterpret_cast(memfile.app_image_ptr),true,C_DEALLOC,memfile.app_image_size,1); - ret->alloc(memfile.app_image_size,1); - const char *srcData(reinterpret_cast(memfile.app_image_ptr)); - std::copy(srcData,srcData+memfile.app_image_size,ret->getPointer()); + ret->useArray(reinterpret_cast(memfile.app_image_ptr),true,C_DEALLOC,memfile.app_image_size,1); return ret; } diff --git a/src/MEDLoader/MEDFileUtilities.txx b/src/MEDLoader/MEDFileUtilities.txx index a8417d3ac..279afb774 100644 --- a/src/MEDLoader/MEDFileUtilities.txx +++ b/src/MEDLoader/MEDFileUtilities.txx @@ -33,7 +33,7 @@ T *MEDCoupling::MEDFileWritableStandAlone::BuildFromMemoryChunk(MEDCoupling::Dat memfile.app_image_ptr=db->getPointer(); memfile.app_image_size=db->getNbOfElems(); std::string dftFileName(MEDCoupling::MEDFileWritableStandAlone::GenerateUniqueDftFileNameInMem()); - MEDFileUtilities::AutoFid fid(MEDmemFileOpen(dftFileName.c_str(),&memfile,MED_FALSE,MED_ACC_RDONLY)); + MEDFileUtilities::AutoFid fid(MEDmemFileOpen(dftFileName.c_str(),&memfile,MED_FALSE,MED_ACC_RDWR)); return T::New(fid); } diff --git a/src/MEDLoader/Swig/MEDLoader.i b/src/MEDLoader/Swig/MEDLoader.i index c5dce740f..55482c4a6 100644 --- a/src/MEDLoader/Swig/MEDLoader.i +++ b/src/MEDLoader/Swig/MEDLoader.i @@ -141,9 +141,33 @@ def MEDCouplingExtrudedMeshnew(cls,*args): def MEDCouplingMEDFileUMeshnew(cls,*args): import _MEDLoader return _MEDLoader.MEDFileUMesh____new___(cls,args) +def MEDCouplingMEDFileCMeshnew(cls,*args): + import _MEDLoader + return _MEDLoader.MEDFileCMesh____new___(cls,args) +def MEDCouplingMEDFileCurveLinearMeshnew(cls,*args): + import _MEDLoader + return _MEDLoader.MEDFileCurveLinearMesh____new___(cls,args) +def MEDCouplingMEDFileMeshesnew(cls,*args): + import _MEDLoader + return _MEDLoader.MEDFileMeshes____new___(cls,args) def MEDCouplingMEDFileDatanew(cls,*args): import _MEDLoader return _MEDLoader.MEDFileData____new___(cls,args) +def MEDCouplingMEDFileFieldsnew(cls,*args): + import _MEDLoader + return _MEDLoader.MEDFileFields____new___(cls,args) +def MEDCouplingMEDFileField1TSnew(cls,*args): + import _MEDLoader + return _MEDLoader.MEDFileField1TS____new___(cls,args) +def MEDCouplingMEDFileFieldMultiTSnew(cls,*args): + import _MEDLoader + return _MEDLoader.MEDFileFieldMultiTS____new___(cls,args) +def MEDCouplingMEDFileIntField1TSnew(cls,*args): + import _MEDLoader + return _MEDLoader.MEDFileIntField1TS____new___(cls,args) +def MEDCouplingMEDFileIntFieldMultiTSnew(cls,*args): + import _MEDLoader + return _MEDLoader.MEDFileIntFieldMultiTS____new___(cls,args) %} %include "MEDCouplingFinalize.i" @@ -151,6 +175,22 @@ def MEDCouplingMEDFileDatanew(cls,*args): %pythoncode %{ MEDFileUMesh.__new__=classmethod(MEDCouplingMEDFileUMeshnew) del MEDCouplingMEDFileUMeshnew +MEDFileCMesh.__new__=classmethod(MEDCouplingMEDFileCMeshnew) +del MEDCouplingMEDFileCMeshnew +MEDFileCurveLinearMesh.__new__=classmethod(MEDCouplingMEDFileCurveLinearMeshnew) +del MEDCouplingMEDFileCurveLinearMeshnew MEDFileData.__new__=classmethod(MEDCouplingMEDFileDatanew) del MEDCouplingMEDFileDatanew +MEDFileMeshes.__new__=classmethod(MEDCouplingMEDFileMeshesnew) +del MEDCouplingMEDFileMeshesnew +MEDFileFields.__new__=classmethod(MEDCouplingMEDFileFieldsnew) +del MEDCouplingMEDFileFieldsnew +MEDFileField1TS.__new__=classmethod(MEDCouplingMEDFileField1TSnew) +del MEDCouplingMEDFileField1TSnew +MEDFileFieldMultiTS.__new__=classmethod(MEDCouplingMEDFileFieldMultiTSnew) +del MEDCouplingMEDFileFieldMultiTSnew +MEDFileIntField1TS.__new__=classmethod(MEDCouplingMEDFileIntField1TSnew) +del MEDCouplingMEDFileIntField1TSnew +MEDFileIntFieldMultiTS.__new__=classmethod(MEDCouplingMEDFileIntFieldMultiTSnew) +del MEDCouplingMEDFileIntFieldMultiTSnew %} diff --git a/src/MEDLoader/Swig/MEDLoaderCommon.i b/src/MEDLoader/Swig/MEDLoaderCommon.i index 19c1f2b5a..c7542ef96 100644 --- a/src/MEDLoader/Swig/MEDLoaderCommon.i +++ b/src/MEDLoader/Swig/MEDLoaderCommon.i @@ -547,6 +547,16 @@ namespace MEDCoupling return ret.retn(); } + PyObject *__getstate__() throw(INTERP_KERNEL::Exception) + { + PyObject *ret(PyList_New(0)); + return ret; + } + + void __setstate__(PyObject *inp) throw(INTERP_KERNEL::Exception) + { + } + PyObject *__getnewargs__() throw(INTERP_KERNEL::Exception) { #ifdef WITH_NUMPY @@ -1605,6 +1615,12 @@ namespace MEDCoupling { return MEDFileCMesh::New(db); } + + // serialization + static PyObject *___new___(PyObject *cls, PyObject *args) throw(INTERP_KERNEL::Exception) + { + return NewMethWrapCallInitOnlyIfDictWithSingleEltInInput(cls,args,"MEDFileCMesh"); + } PyObject *getMesh() const throw(INTERP_KERNEL::Exception) { @@ -1645,6 +1661,12 @@ namespace MEDCoupling { return MEDFileCurveLinearMesh::New(db); } + + // serialization + static PyObject *___new___(PyObject *cls, PyObject *args) throw(INTERP_KERNEL::Exception) + { + return NewMethWrapCallInitOnlyIfDictWithSingleEltInInput(cls,args,"MEDFileCurveLinearMesh"); + } PyObject *getMesh() const throw(INTERP_KERNEL::Exception) { @@ -1746,6 +1768,12 @@ namespace MEDCoupling return MEDFileMeshes::New(db); } + // serialization + static PyObject *___new___(PyObject *cls, PyObject *args) throw(INTERP_KERNEL::Exception) + { + return NewMethWrapCallInitOnlyIfDictWithSingleEltInInput(cls,args,"MEDFileMeshes"); + } + std::string __str__() const throw(INTERP_KERNEL::Exception) { return self->simpleRepr(); @@ -2152,6 +2180,12 @@ namespace MEDCoupling return MEDFileField1TS::New(); } + // serialization + static PyObject *___new___(PyObject *cls, PyObject *args) throw(INTERP_KERNEL::Exception) + { + return NewMethWrapCallInitOnlyIfDictWithSingleEltInInput(cls,args,"MEDFileField1TS"); + } + void copyTinyInfoFrom(const MEDCouplingFieldDouble *field) throw(INTERP_KERNEL::Exception) { const DataArrayDouble *arr=0; @@ -2291,6 +2325,12 @@ namespace MEDCoupling return MEDFileIntField1TS::New(db); } + // serialization + static PyObject *___new___(PyObject *cls, PyObject *args) throw(INTERP_KERNEL::Exception) + { + return NewMethWrapCallInitOnlyIfDictWithSingleEltInInput(cls,args,"MEDFileIntField1TS"); + } + std::string __str__() const throw(INTERP_KERNEL::Exception) { return self->simpleRepr(); @@ -2712,6 +2752,7 @@ namespace MEDCoupling static MEDFileFieldMultiTS *New() throw(INTERP_KERNEL::Exception); static MEDFileFieldMultiTS *New(const std::string& fileName, bool loadAll=true) throw(INTERP_KERNEL::Exception); static MEDFileFieldMultiTS *New(const std::string& fileName, const std::string& fieldName, bool loadAll=true) throw(INTERP_KERNEL::Exception); + static MEDFileFieldMultiTS *New(DataArrayByte *db) throw(INTERP_KERNEL::Exception); // MEDCouplingFieldDouble *field(int iteration, int order, const MEDFileMesh *mesh) const throw(INTERP_KERNEL::Exception); MEDCouplingFieldDouble *getFieldAtLevel(TypeOfField type, int iteration, int order, int meshDimRelToMax, int renumPol=0) const throw(INTERP_KERNEL::Exception); @@ -2739,6 +2780,17 @@ namespace MEDCoupling { return MEDFileFieldMultiTS::New(fileName,fieldName,loadAll); } + + MEDFileFieldMultiTS(DataArrayByte *db) throw(INTERP_KERNEL::Exception) + { + return MEDFileFieldMultiTS::New(db); + } + + // serialization + static PyObject *___new___(PyObject *cls, PyObject *args) throw(INTERP_KERNEL::Exception) + { + return NewMethWrapCallInitOnlyIfDictWithSingleEltInInput(cls,args,"MEDFileFieldMultiTS"); + } static MEDFileFieldMultiTS *LoadSpecificEntities(const std::string& fileName, const std::string& fieldName, PyObject *entities, bool loadAll=true) { @@ -2895,7 +2947,13 @@ namespace MEDCoupling { return MEDFileIntFieldMultiTS::New(db); } - + + // serialization + static PyObject *___new___(PyObject *cls, PyObject *args) throw(INTERP_KERNEL::Exception) + { + return NewMethWrapCallInitOnlyIfDictWithSingleEltInInput(cls,args,"MEDFileIntFieldMultiTS"); + } + static MEDFileIntFieldMultiTS *LoadSpecificEntities(const std::string& fileName, const std::string& fieldName, PyObject *entities, bool loadAll=true) { std::vector > tmp(convertTimePairIdsFromPy(entities)); @@ -2977,6 +3035,12 @@ namespace MEDCoupling return MEDFileFields::New(db); } + // serialization + static PyObject *___new___(PyObject *cls, PyObject *args) throw(INTERP_KERNEL::Exception) + { + return NewMethWrapCallInitOnlyIfDictWithSingleEltInInput(cls,args,"MEDFileFields"); + } + std::string __str__() const throw(INTERP_KERNEL::Exception) { return self->simpleRepr(); @@ -3591,16 +3655,6 @@ namespace MEDCoupling { return NewMethWrapCallInitOnlyIfDictWithSingleEltInInput(cls,args,"MEDFileData"); } - - PyObject *__getstate__() throw(INTERP_KERNEL::Exception) - { - PyObject *ret(PyList_New(0)); - return ret; - } - - void __setstate__(PyObject *inp) throw(INTERP_KERNEL::Exception) - { - } } }; diff --git a/src/MEDLoader/Swig/MEDLoaderTest3.py b/src/MEDLoader/Swig/MEDLoaderTest3.py index bd37a82e2..a6f702257 100644 --- a/src/MEDLoader/Swig/MEDLoaderTest3.py +++ b/src/MEDLoader/Swig/MEDLoaderTest3.py @@ -5775,6 +5775,134 @@ class MEDLoaderTest3(unittest.TestCase): mm.write(fname2,2) assert(LooseVersion(MEDFileVersionOfFileStr(fname2)).version[:2]==list(MEDFileVersion()[:2])) # checks that MED file version of written mesh is thoose of the current MED file lib pass + + @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy") + def testPickelizationOfMEDFileObjects1(self): + fname="Pyfile110.med" + coo=DataArrayDouble([0.,0.,0.5,0.,1.,0.,0.,0.5,0.5,0.5,1.,0.5,0.,1.,0.5,1.,1.,1.],9,2) + m0=MEDCouplingUMesh("Mesh",2) + m0.allocateCells(5) + m0.insertNextCell(NORM_TRI3,[1,4,2]) + m0.insertNextCell(NORM_TRI3,[4,5,2]) + m0.insertNextCell(NORM_QUAD4,[0,3,4,1]) + m0.insertNextCell(NORM_QUAD4,[3,6,7,4]) + m0.insertNextCell(NORM_QUAD4,[4,7,8,5]) + m0.finishInsertingCells() + m0.setCoords(coo) + m1=MEDCouplingUMesh(m0.getName(),1) + m1.allocateCells(9) + conn1=[0,1,0,3,3,4,4,1,5,4,2,4,1,2,3,6,5,8] + for i in xrange(9): + m1.insertNextCell(NORM_SEG2,conn1[2*i:2*i+2]) + pass + m1.finishInsertingCells() + m1.setCoords(coo) + # + m=MEDFileUMesh() + m.setMeshAtLevel(0,m0) + m.setMeshAtLevel(-1,m1) + # + dt=3 ; it=2 ; tim=4.5 + fieldNode0=MEDCouplingFieldDouble(ON_NODES,ONE_TIME) + fieldNode0.setName("fieldNode0") + fieldNode0.setTime(tim,dt,it) + pfl0=DataArrayInt([0,1,2,3,4]) ; pfl0.setName("PflIdentity0") # important to keep like that + arr=DataArrayDouble([10,11,12,13,14]) + fieldNode0.setArray(arr) + f0=MEDFileField1TS() + f0.setFieldProfile(fieldNode0,m,0,pfl0) + fieldNode1=MEDCouplingFieldDouble(ON_NODES,ONE_TIME) + fieldNode1.setName("fieldNode1") + fieldNode1.setTime(tim,dt,it) + pfl1=DataArrayInt([0,1,2,3,4,5,6]) ; pfl1.setName("PflIdentity1") + arr1=DataArrayDouble([20,21,22,23,24,25,26]) + fieldNode1.setArray(arr1) + f1=MEDFileField1TS() + f1.setFieldProfile(fieldNode1,m,-1,pfl1) + mfd=MEDFileData() + mfd.setMeshes(MEDFileMeshes()) ; mfd.setFields(MEDFileFields()) + mfd.getMeshes().pushMesh(m) + fmts=MEDFileFieldMultiTS() ; fmts.pushBackTimeStep(f0) + mfd.getFields().pushField(fmts) + # first start gently + d=mfd.serialize() + mfd2=MEDFileData(d) + self.assertEqual(len(mfd2.getMeshes()),1) + self.assertEqual(len(mfd2.getFields()),1) + self.assertEqual(len(mfd2.getFields()[0]),1) + self.assertTrue(mfd2.getMeshes()[0].isEqual(mfd.getMeshes()[0],1e-12)) + ff2=mfd2.getFields()[0][0].field(mfd2.getMeshes()[0]) + ff =mfd.getFields()[0][0].field(mfd.getMeshes()[0]) + self.assertTrue(ff2.isEqual(ff,1e-12,1e-12)) + # OK now end of joke -> serialization of MEDFileData + import cPickle + st=cPickle.dumps(mfd,cPickle.HIGHEST_PROTOCOL) + mfd3=cPickle.loads(st) + # check of object + self.assertEqual(len(mfd3.getMeshes()),1) + self.assertEqual(len(mfd3.getFields()),1) + self.assertEqual(len(mfd3.getFields()[0]),1) + self.assertTrue(mfd3.getMeshes()[0].isEqual(mfd.getMeshes()[0],1e-12)) + ff3=mfd3.getFields()[0][0].field(mfd3.getMeshes()[0]) + self.assertTrue(ff3.isEqual(ff,1e-12,1e-12)) + # serialization of MEDFileFields + st=cPickle.dumps(mfd.getFields(),cPickle.HIGHEST_PROTOCOL) + fs4=cPickle.loads(st) + ff4=fs4[0][0].field(mfd3.getMeshes()[0]) + self.assertTrue(ff4.isEqual(ff,1e-12,1e-12)) + # serialization of MEDFileFieldMulitTS + st=cPickle.dumps(mfd.getFields()[0],cPickle.HIGHEST_PROTOCOL) + fmts5=cPickle.loads(st) + ff5=fmts5[0].field(mfd3.getMeshes()[0]) + self.assertTrue(ff5.isEqual(ff,1e-12,1e-12)) + # serialization of MEDFileField1TS + st=cPickle.dumps(mfd.getFields()[0][0],cPickle.HIGHEST_PROTOCOL) + f1ts6=cPickle.loads(st) + ff6=f1ts6.field(mfd3.getMeshes()[0]) + self.assertTrue(ff6.isEqual(ff,1e-12,1e-12)) + # serialization of MEDFileMeshes + st=cPickle.dumps(mfd.getMeshes(),cPickle.HIGHEST_PROTOCOL) + ms7=cPickle.loads(st) + self.assertEqual(len(ms7),1) + self.assertTrue(ms7[0].isEqual(mfd.getMeshes()[0],1e-12)) + pass + + @unittest.skipUnless(MEDCouplingHasNumPyBindings(),"requires numpy") + def testPickelizationOfMEDFileObjects2(self): + # CMesh + self.testMEDMesh6() # generates MEDFileMesh5.med file + mm=MEDFileMesh.New("MEDFileMesh5.med") + self.assertTrue(isinstance(mm,MEDFileCMesh)) + import cPickle + st=cPickle.dumps(mm,cPickle.HIGHEST_PROTOCOL) + mm2=cPickle.loads(st) + self.assertTrue(isinstance(mm2,MEDFileCMesh)) + self.assertTrue(mm.getMesh().isEqual(mm2.getMesh(),1e-12)) + # CurveLinear + self.testCurveLinearMesh1() # generates Pyfile55.med + mm=MEDFileMesh.New("Pyfile55.med") + self.assertTrue(isinstance(mm,MEDFileCurveLinearMesh)) + st=cPickle.dumps(mm,cPickle.HIGHEST_PROTOCOL) + mm3=cPickle.loads(st) + self.assertTrue(isinstance(mm3,MEDFileCurveLinearMesh)) + self.assertTrue(mm.getMesh().isEqual(mm3.getMesh(),1e-12)) + self.testInt32InMEDFileFieldStar1()# generates Pyfile63.med + # MEDFileIntFieldMultiTS + fs4=MEDFileFields("Pyfile63.med") + ms4=MEDFileMeshes("Pyfile63.med") + self.assertTrue(isinstance(fs4[0],MEDFileIntFieldMultiTS)) + st=cPickle.dumps(fs4[0],cPickle.HIGHEST_PROTOCOL) + fmts5=cPickle.loads(st) + self.assertEqual(len(fs4[0]),len(fmts5)) + self.assertTrue(isinstance(fmts5,MEDFileIntFieldMultiTS)) + self.assertTrue(fmts5[0].field(ms4[0]).isEqual((fs4[0][0]).field(ms4[0]),1e-12,1e-12)) + # MEDFileIntField1TS + st=cPickle.dumps(fs4[0][0],cPickle.HIGHEST_PROTOCOL) + f1ts6=cPickle.loads(st) + self.assertTrue(isinstance(f1ts6,MEDFileIntField1TS)) + self.assertTrue(f1ts6.field(ms4[0]).isEqual((fs4[0][0]).field(ms4[0]),1e-12,1e-12)) + pass + pass if __name__ == "__main__":