From: Anthony Geay Date: Thu, 17 Aug 2023 13:55:09 +0000 (+0200) Subject: [EDF28448] : MEDFileUMesh.declarePartsUpdated and MEDFileField1TS context manager... X-Git-Tag: V9_12_0a1 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=f4b6fa7c4d1663acb098efa58ce8c391346dd544;p=tools%2Fmedcoupling.git [EDF28448] : MEDFileUMesh.declarePartsUpdated and MEDFileField1TS context manager dealing with file and not file situation indifferently --- diff --git a/src/MEDLoader/MEDFileField1TS.cxx b/src/MEDLoader/MEDFileField1TS.cxx index 735ca4539..9287ec66f 100644 --- a/src/MEDLoader/MEDFileField1TS.cxx +++ b/src/MEDLoader/MEDFileField1TS.cxx @@ -2724,6 +2724,11 @@ try:MEDFileTemplateField1TS(other,shallowCopyOfContent) catch(INTERP_KERNEL::Exception& e) { throw e; } +MCAuto MEDFileField1TS::buildNewEmpty() const +{ + return MCAuto(MEDFileField1TS::New()); +} + MEDFileField1TS *MEDFileField1TS::shallowCpy() const { return new MEDFileField1TS(*this); @@ -2737,6 +2742,11 @@ std::vector< std::vector > MEDFileField1TS::getFieldSplitedBy //= MEDFileInt32Field1TS +MCAuto MEDFileInt32Field1TS::buildNewEmpty() const +{ + return MCAuto(MEDFileInt32Field1TS::New()); +} + MCAuto MEDFileInt32Field1TS::ConvertFieldIntToFieldDouble(const MEDCouplingFieldInt32 *f) { if(!f) @@ -2752,6 +2762,11 @@ MCAuto MEDFileInt32Field1TS::ConvertFieldIntToFieldDoubl //= MEDFileInt64Field1TS +MCAuto MEDFileInt64Field1TS::buildNewEmpty() const +{ + return MCAuto(MEDFileInt64Field1TS::New()); +} + MCAuto MEDFileInt64Field1TS::ConvertFieldIntToFieldDouble(const MEDCouplingFieldInt64 *f) { if(!f) @@ -2765,3 +2780,9 @@ MCAuto MEDFileInt64Field1TS::ConvertFieldIntToFieldDoubl return ret; } +//= MEDFileFloatField1TS + +MCAuto MEDFileFloatField1TS::buildNewEmpty() const +{ + return MCAuto(MEDFileFloatField1TS::New()); +} diff --git a/src/MEDLoader/MEDFileField1TS.hxx b/src/MEDLoader/MEDFileField1TS.hxx index 11117d97e..223c38f6f 100644 --- a/src/MEDLoader/MEDFileField1TS.hxx +++ b/src/MEDLoader/MEDFileField1TS.hxx @@ -295,6 +295,7 @@ namespace MEDCoupling MEDLOADER_EXPORT static MEDFileAnyTypeField1TS *NewAdv(med_idt fid, const std::string& fieldName, int iteration, int order, bool loadAll, const MEDFileEntities *entities); MEDLOADER_EXPORT static MEDFileAnyTypeField1TS *NewAdv(med_idt fid, const std::string& fieldName, int iteration, int order, const MEDFileEntities *entities, const std::vector& distrib); + MEDLOADER_EXPORT virtual MCAuto buildNewEmpty() const = 0; MEDLOADER_EXPORT int getDimension() const; MEDLOADER_EXPORT int getIteration() const; MEDLOADER_EXPORT int getOrder() const; @@ -424,6 +425,7 @@ namespace MEDCoupling MEDLOADER_EXPORT MEDFileInt32Field1TS *convertToInt(bool isDeepCpyGlobs=true) const; MEDLOADER_EXPORT MEDFileInt64Field1TS *convertToInt64(bool isDeepCpyGlobs=true) const; public: + MEDLOADER_EXPORT MCAuto buildNewEmpty() const; MEDLOADER_EXPORT MEDFileField1TS *shallowCpy() const; MEDLOADER_EXPORT std::vector< std::vector > getFieldSplitedByType2(const std::string& mname, std::vector& types, std::vector< std::vector >& typesF, std::vector< std::vector >& pfls, std::vector< std::vector >& locs) const; @@ -460,6 +462,7 @@ namespace MEDCoupling { friend class MEDFileTemplateField1TS; public: + MEDLOADER_EXPORT MCAuto buildNewEmpty() const; MEDLOADER_EXPORT MEDFileInt32Field1TS *shallowCpy() const { return new MEDFileInt32Field1TS(*this); } MEDLOADER_EXPORT std::string getClassName() const override { return std::string("MEDFileInt32Field1TS"); } public: @@ -485,6 +488,7 @@ namespace MEDCoupling { friend class MEDFileTemplateField1TS; public: + MEDLOADER_EXPORT MCAuto buildNewEmpty() const; MEDLOADER_EXPORT MEDFileInt64Field1TS *shallowCpy() const { return new MEDFileInt64Field1TS(*this); } MEDLOADER_EXPORT std::string getClassName() const override { return std::string("MEDFileInt64Field1TS"); } public: @@ -513,6 +517,7 @@ namespace MEDCoupling med_field_type getMEDFileFieldType() const { return MED_FLOAT32; } MEDLOADER_EXPORT MEDFileFloatField1TS *shallowCpy() const { return new MEDFileFloatField1TS(*this); } MEDLOADER_EXPORT std::string getClassName() const override { return std::string("MEDFileFloatField1TS"); } + MEDLOADER_EXPORT MCAuto buildNewEmpty() const; private: ~MEDFileFloatField1TS() { } MEDFileFloatField1TS() { } diff --git a/src/MEDLoader/MEDFileMesh.cxx b/src/MEDLoader/MEDFileMesh.cxx index 4de31cc17..67b4aa667 100644 --- a/src/MEDLoader/MEDFileMesh.cxx +++ b/src/MEDLoader/MEDFileMesh.cxx @@ -3904,11 +3904,27 @@ MEDCouplingUMesh *MEDFileUMesh::getLevelM3Mesh(bool renum) const return getMeshAtLevel(-3,renum); } +/*! + * This method inform datastructure that vector of MEDCoupling1GTUMesh instances have been touched. + * So the version of data to take is vector of MEDCoupling1GTUMesh not MEDCouplingUMesh + */ +void MEDFileUMesh::declarePartsUpdated() const +{ + for(std::vector< MCAuto >::const_iterator it=_ms.begin();it!=_ms.end();it++) + { + const MEDFileUMeshSplitL1 *elt(*it); + if(elt) + elt->declarePartsUpdated(); + } +} + /*! * This method is for advanced users. There is two storing strategy of mesh in \a this. * Either MEDCouplingUMesh, or vector of MEDCoupling1GTUMesh instances. * When assignment is done the first one is done, which is not optimal in write mode for MED file. * This method allows to switch from MEDCouplingUMesh mode to MEDCoupling1GTUMesh mode. + * + * \sa MEDFileUMesh::declarePartsUpdated */ void MEDFileUMesh::forceComputationOfParts() const { diff --git a/src/MEDLoader/MEDFileMesh.hxx b/src/MEDLoader/MEDFileMesh.hxx index 2cdbaa0f7..4e53ede52 100644 --- a/src/MEDLoader/MEDFileMesh.hxx +++ b/src/MEDLoader/MEDFileMesh.hxx @@ -336,6 +336,7 @@ MCAuto& coords, MCAuto& partCoords, MCAuto getDirectUndergroundSingleGeoTypeMeshes(int meshDimRelToMax) const; diff --git a/src/MEDLoader/MEDFileMeshLL.cxx b/src/MEDLoader/MEDFileMeshLL.cxx index 030debd0c..eec65e3a3 100644 --- a/src/MEDLoader/MEDFileMeshLL.cxx +++ b/src/MEDLoader/MEDFileMeshLL.cxx @@ -1387,6 +1387,11 @@ void MEDFileUMeshSplitL1::forceComputationOfParts() const _m_by_types.forceComputationOfPartsFromUMesh(); } +void MEDFileUMeshSplitL1::declarePartsUpdated() const +{ + _m_by_types.declarePartsUpdated(); +} + void MEDFileUMeshSplitL1::assignParts(const std::vector< const MEDCoupling1GTUMesh * >& mParts) { _m_by_types.assignParts(mParts); @@ -1968,6 +1973,12 @@ void MEDFileUMeshAggregateCompute::forceComputationOfPartsFromUMesh() const _mp_time=std::max(_mp_time,_m_time); } +void MEDFileUMeshAggregateCompute::declarePartsUpdated() const +{ + _mp_time=std::max(_mp_time,_m_time) + 1; + _m.nullify(); +} + const PartDefinition *MEDFileUMeshAggregateCompute::getPartDefOfWithoutComputation(INTERP_KERNEL::NormalizedCellType gt) const { if(_mp_time<_m_time) diff --git a/src/MEDLoader/MEDFileMeshLL.hxx b/src/MEDLoader/MEDFileMeshLL.hxx index 737ad23c0..e33e18735 100644 --- a/src/MEDLoader/MEDFileMeshLL.hxx +++ b/src/MEDLoader/MEDFileMeshLL.hxx @@ -249,6 +249,7 @@ MCAuto& _coords, MCAuto& _fam_coords, MCAuto& tinyInt, std::vector< MCAuto >& bigArraysI) const; void unserialize(const std::string& name, DataArrayDouble *coo, std::vector& tinyInt, std::vector< MCAuto >& bigArraysI); @@ -287,6 +288,7 @@ MCAuto& _coords, MCAuto& _fam_coords, MCAuto& mParts); void forceComputationOfParts() const; + void declarePartsUpdated() const; bool empty() const; bool presenceOfOneFams(const std::vector& ids) const; int getMeshDimension() const; diff --git a/src/MEDLoader/Swig/MEDLoaderCommon.i b/src/MEDLoader/Swig/MEDLoaderCommon.i index b7c4eb645..bebf5cc0d 100644 --- a/src/MEDLoader/Swig/MEDLoaderCommon.i +++ b/src/MEDLoader/Swig/MEDLoaderCommon.i @@ -223,6 +223,7 @@ using namespace MEDCoupling; %newobject MEDCoupling::MEDFileAnyTypeField1TS::shallowCpy; %newobject MEDCoupling::MEDFileAnyTypeField1TS::deepCopy; %newobject MEDCoupling::MEDFileAnyTypeField1TS::extractPart; +%newobject MEDCoupling::MEDFileAnyTypeField1TS::buildNewEmpty; %newobject MEDCoupling::MEDFileField1TS::New; %newobject MEDCoupling::MEDFileField1TS::field; %newobject MEDCoupling::MEDFileField1TS::getFieldAtLevel; @@ -1453,6 +1454,7 @@ namespace MEDCoupling MEDCouplingUMesh *getLevelM1Mesh(bool renum=false) const; MEDCouplingUMesh *getLevelM2Mesh(bool renum=false) const; MEDCouplingUMesh *getLevelM3Mesh(bool renum=false) const; + void declarePartsUpdated() const; void forceComputationOfParts() const; void computeRevNum() const; // @@ -2212,6 +2214,12 @@ namespace MEDCoupling return elt; } + MEDFileAnyTypeField1TS *buildNewEmpty() const + { + MCAuto ret = self->buildNewEmpty(); + return ret.retn(); + } + void setProfileNameOnLeaf(INTERP_KERNEL::NormalizedCellType typ, int locId, const std::string& newPflName, bool forceRenameOnGlob=false) { self->setProfileNameOnLeaf(0,typ,locId,newPflName,forceRenameOnGlob); @@ -4405,10 +4413,10 @@ namespace MEDCoupling %pythoncode %{ def enter1TS(self): - self.loadArrays() + self.loadArraysIfNecessary() pass def exit1TS(self, exctype, exc, tb): - self.unloadArrays() + self.unloadArraysWithoutDataLoss() pass MEDFileAnyTypeField1TS.__enter__=enter1TS MEDFileAnyTypeField1TS.__exit__=exit1TS diff --git a/src/MEDLoader/Swig/MEDLoaderTest4.py b/src/MEDLoader/Swig/MEDLoaderTest4.py index e75d18a87..2bb28a80b 100644 --- a/src/MEDLoader/Swig/MEDLoaderTest4.py +++ b/src/MEDLoader/Swig/MEDLoaderTest4.py @@ -5716,6 +5716,58 @@ class MEDLoaderTest4(unittest.TestCase): m.mergeNodes(1e-5) # coords into m has been modified so coords of mm and m mismatches self.assertRaises(InterpKernelException,mm.write,fname,2) # write fails due to mismatch of number of tuples of coords of mm and those of m + def test47(self): + """ + [EDF28448] : MEDFileUMesh.declarePartsUpdated + """ + m = MEDCouplingUMesh("mesh",2) + m.setCoords(DataArrayDouble([(0,0,0)])) + m.allocateCells() + m.insertNextCell(NORM_QUAD4,[2,3,5,6]) + mm = MEDFileUMesh() + mm[0] = m + # + mm.forceComputationOfParts() + mtest = mm.getDirectUndergroundSingleGeoTypeMeshes(0)[0] + self.assertTrue( mtest.getNodalConnectivity().isEqual(DataArrayInt([2,3,5,6])) ) + # change content of data in parts + mtest.getNodalConnectivity()[:] += 1 + self.assertTrue( mtest.getNodalConnectivity().isEqual(DataArrayInt([3,4,6,7])) ) + self.assertTrue( mm[0].getNodalConnectivity().isEqual(DataArrayInt([NORM_QUAD4,2,3,5,6])) ) # <- oops UMesh is now not updated ! + mm.declarePartsUpdated() # invoke declarePartsUpdated to tell mm that something has changed ASTGUMesh vector parts and this is now the correct version + self.assertTrue( mm[0].getNodalConnectivity().isEqual(DataArrayInt([NORM_QUAD4,3,4,6,7])) ) # parts and umesh are now synchronized + + @WriteInTmpDir + def test48(self): + """ + [EDF28448] : MEDFileField1TS context manager deal with file and not file situation indifferently + """ + fname = "test48.med" + m = MEDCouplingUMesh("mesh",2) + m.setCoords(DataArrayDouble([(0,0,0)])) + m.allocateCells() + m.insertNextCell(NORM_QUAD4,[2,3,5,6]) + mm = MEDFileUMesh() + mm[0] = m + # now test context manager + fmts = MEDFileFieldMultiTS() + f1ts = MEDFileField1TS() + f = MEDCouplingFieldDouble(ON_CELLS) ; f.setName("Field") ; f.setMesh(mm[0]) ; f.setArray(DataArrayDouble([0.123])) + f1ts.setFieldNoProfileSBT(f) + fmts.pushBackTimeStep(f1ts) + with f1ts: + self.assertEqual( f1ts.getTypesOfFieldAvailable() , [ON_CELLS] ) + self.assertTrue( f1ts.getUndergroundDataArray().isEqual( DataArrayDouble([0.123]), 1e-12) ) + self.assertTrue( f1ts.getUndergroundDataArray().isEqual( DataArrayDouble([0.123]), 1e-12) ) + f1ts.write(fname,2) + fs_read = MEDFileFields(fname,False) + f1ts_read = fs_read[0][0] + self.assertEqual( f1ts_read.getTypesOfFieldAvailable() , [ON_CELLS] ) + self.assertTrue(not f1ts_read.getUndergroundDataArray().isAllocated()) + with f1ts_read: + self.assertTrue(f1ts_read.getUndergroundDataArray().isAllocated()) + self.assertTrue(not f1ts_read.getUndergroundDataArray().isAllocated()) + pass if __name__ == "__main__":