From: Anthony Geay Date: Mon, 6 Mar 2017 07:43:13 +0000 (+0100) Subject: Overload of MEDLoader.ReadField method to ease access of medcoupling for new comers X-Git-Tag: V8_3_0a2~4 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=eb62814b5284621fc35a2684885a8c5e2c2a3f67;p=tools%2Fmedcoupling.git Overload of MEDLoader.ReadField method to ease access of medcoupling for new comers --- diff --git a/src/MEDLoader/MEDLoader.cxx b/src/MEDLoader/MEDLoader.cxx index 61ee8bbc2..e90691716 100644 --- a/src/MEDLoader/MEDLoader.cxx +++ b/src/MEDLoader/MEDLoader.cxx @@ -1153,19 +1153,73 @@ MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromGroups(const std::strin return mmuPtr->getGroups(meshDimRelToMax,grps,true); } -MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadField(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) +MCAuto MEDCoupling::ReadField(const std::string& fileName) +{ + std::vector fieldNames(GetAllFieldNames(fileName)); + std::size_t sz(fieldNames.size()); + if(sz==0) + { + std::ostringstream oss; + oss << "The file \"" << fileName << "\" contains no field !"; + throw INTERP_KERNEL::Exception(oss.str()); + } + if(sz>1) + { + std::ostringstream oss; + oss << "In file \"" << fileName << "\" there are more than one field !" << std::endl; + oss << "You are invited to use ReadField(fileName, fieldName) instead to avoid misleading concerning field you want to read !" << std::endl; + oss << "For information, fields available are :" << std::endl; + for(std::vector::const_iterator it=fieldNames.begin();it!=fieldNames.end();it++) + oss << " - \"" << *it << "\"" << std::endl; + throw INTERP_KERNEL::Exception(oss.str()); + } + return ReadField(fileName,fieldNames[0]); +} + +MCAuto MEDCoupling::ReadField(const std::string& fileName, const std::string& fieldName) +{ + std::vector< std::pair< std::pair, double> > iterations(GetAllFieldIterations(fileName,fieldName)); + std::size_t sz(iterations.size()); + if(sz==0) + { + std::ostringstream oss; + oss << "In file \"" << fileName << "\" field \"" << fieldName << "\" exists but with no time steps !"; + throw INTERP_KERNEL::Exception(oss.str()); + } + if(sz>1) + { + std::ostringstream oss; + oss << "In file \"" << fileName << "\" field \"" << fieldName << "\" exists but with more than one time steps !" << std::endl; + oss << "You are invited to use ReadField(fileName, fieldName, iteration, order) instead to avoid misleading concerning time steps." << std::endl; + oss << "For information, time steps available for field \"" << fieldName << "\" are :" << std::endl; + for(std::vector< std::pair< std::pair, double> >::const_iterator it=iterations.begin();it!=iterations.end();it++) + oss << " - " << (*it).first.first << ", " << (*it).first.second << " (" << (*it).second << ")" << std::endl; + throw INTERP_KERNEL::Exception(oss.str()); + } + return ReadField(fileName,fieldName,iterations[0].first.first,iterations[0].first.second); +} + +MCAuto MEDCoupling::ReadField(const std::string& fileName, const std::string& fieldName, int iteration, int order) +{ + MCAuto f(MEDFileField1TS::New(fileName,fieldName,iteration,order)); + MCAuto mesh(MEDFileMesh::New(fileName,f->getMeshName())); + MCAuto ret(f->field(mesh)); + return ret; +} + +MCAuto MEDCoupling::ReadField(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) { MEDCoupling::CheckFileForRead(fileName); switch(type) { case ON_CELLS: - return ReadFieldCell(fileName,meshName,meshDimRelToMax,fieldName,iteration,order); + return MCAuto(ReadFieldCell(fileName,meshName,meshDimRelToMax,fieldName,iteration,order)); case ON_NODES: - return ReadFieldNode(fileName,meshName,meshDimRelToMax,fieldName,iteration,order); + return MCAuto(ReadFieldNode(fileName,meshName,meshDimRelToMax,fieldName,iteration,order)); case ON_GAUSS_PT: - return ReadFieldGauss(fileName,meshName,meshDimRelToMax,fieldName,iteration,order); + return MCAuto(ReadFieldGauss(fileName,meshName,meshDimRelToMax,fieldName,iteration,order)); case ON_GAUSS_NE: - return ReadFieldGaussNE(fileName,meshName,meshDimRelToMax,fieldName,iteration,order); + return MCAuto(ReadFieldGaussNE(fileName,meshName,meshDimRelToMax,fieldName,iteration,order)); default: throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES, ON_CELLS, ON_GAUSS_PT or ON_GAUSS_NE !"); } diff --git a/src/MEDLoader/MEDLoader.hxx b/src/MEDLoader/MEDLoader.hxx index a8c5eafed..ab1732fe4 100644 --- a/src/MEDLoader/MEDLoader.hxx +++ b/src/MEDLoader/MEDLoader.hxx @@ -25,6 +25,7 @@ #include "InterpKernelException.hxx" #include "MEDCouplingRefCountObject.hxx" #include "NormalizedUnstructuredMesh.hxx" +#include "MCAuto.hxx" #include #include @@ -71,7 +72,10 @@ namespace MEDCoupling MEDLOADER_EXPORT MEDCoupling::MEDCouplingUMesh *ReadUMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax=0); MEDLOADER_EXPORT MEDCoupling::MEDCouplingUMesh *ReadUMeshFromFile(const std::string& fileName, int meshDimRelToMax=0); MEDLOADER_EXPORT int ReadUMeshDimFromFile(const std::string& fileName, const std::string& meshName); - MEDLOADER_EXPORT MEDCoupling::MEDCouplingFieldDouble *ReadField(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order); + MEDLOADER_EXPORT MCAuto ReadField(const std::string& fileName); + MEDLOADER_EXPORT MCAuto ReadField(const std::string& fileName, const std::string& fieldName); + MEDLOADER_EXPORT MCAuto ReadField(const std::string& fileName, const std::string& fieldName, int iteration, int order); + MEDLOADER_EXPORT MCAuto ReadField(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order); MEDLOADER_EXPORT std::vector ReadFieldsOnSameMesh(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, const std::vector >& its); MEDLOADER_EXPORT std::vector ReadFieldsCellOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, diff --git a/src/MEDLoader/Swig/MEDLoaderCommon.i b/src/MEDLoader/Swig/MEDLoaderCommon.i index de7f087a4..3725a37ba 100644 --- a/src/MEDLoader/Swig/MEDLoaderCommon.i +++ b/src/MEDLoader/Swig/MEDLoaderCommon.i @@ -77,9 +77,9 @@ using namespace MEDCoupling; %newobject ReadUMeshFromFamiliesSwig; %newobject ReadUMeshFromGroupsSwig; +%newobject ReadFieldSwig; %newobject MEDCoupling::ReadUMeshFromFile; %newobject MEDCoupling::ReadMeshFromFile; -%newobject MEDCoupling::ReadField; %newobject MEDCoupling::ReadFieldCell; %newobject MEDCoupling::ReadFieldNode; %newobject MEDCoupling::ReadFieldGauss; @@ -332,7 +332,6 @@ namespace MEDCoupling MEDCoupling::MEDCouplingUMesh *ReadUMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax=0) throw(INTERP_KERNEL::Exception); MEDCoupling::MEDCouplingUMesh *ReadUMeshFromFile(const std::string& fileName, int meshDimRelToMax=0) throw(INTERP_KERNEL::Exception); int ReadUMeshDimFromFile(const std::string& fileName, const std::string& meshName) throw(INTERP_KERNEL::Exception); - MEDCoupling::MEDCouplingFieldDouble *ReadField(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception); MEDCoupling::MEDCouplingFieldDouble *ReadFieldCell(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception); MEDCoupling::MEDCouplingFieldDouble *ReadFieldNode(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception); MEDCoupling::MEDCouplingFieldDouble *ReadFieldGauss(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception); @@ -359,6 +358,7 @@ namespace MEDCoupling %rename (GetTypesOfField) GetTypesOfFieldSwig; %rename (ReadUMeshFromGroups) ReadUMeshFromGroupsSwig; %rename (ReadUMeshFromFamilies) ReadUMeshFromFamiliesSwig; +%rename (ReadField) ReadFieldSwig; %inline { @@ -373,6 +373,30 @@ namespace MEDCoupling return ret; } + MEDCoupling::MEDCouplingFieldDouble *ReadFieldSwig(const std::string& fileName) throw(INTERP_KERNEL::Exception) + { + MCAuto ret(MEDCoupling::ReadField(fileName)); + return ret.retn(); + } + + MEDCoupling::MEDCouplingFieldDouble *ReadFieldSwig(const std::string& fileName, const std::string& fieldName) throw(INTERP_KERNEL::Exception) + { + MCAuto ret(MEDCoupling::ReadField(fileName,fieldName)); + return ret.retn(); + } + + MEDCoupling::MEDCouplingFieldDouble *ReadFieldSwig(const std::string& fileName, const std::string& fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception) + { + MCAuto ret(MEDCoupling::ReadField(fileName,fieldName,iteration,order)); + return ret.retn(); + } + + MEDCoupling::MEDCouplingFieldDouble *ReadFieldSwig(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception) + { + MCAuto ret(MEDCoupling::ReadField(type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order)); + return ret.retn(); + } + PyObject *GetFieldIterationsSwig(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, const std::string& fieldName) throw(INTERP_KERNEL::Exception) { std::vector< std::pair > res=MEDCoupling::GetFieldIterations(type,fileName,meshName,fieldName); diff --git a/src/MEDLoader/Swig/MEDLoaderTest1.py b/src/MEDLoader/Swig/MEDLoaderTest1.py index be8dba437..94f21b14e 100644 --- a/src/MEDLoader/Swig/MEDLoaderTest1.py +++ b/src/MEDLoader/Swig/MEDLoaderTest1.py @@ -751,6 +751,127 @@ class MEDLoaderTest1(unittest.TestCase): self.assertTrue(f11.isEqual(f3r,1e-12,1e-12)) self.assertTrue(f3r.getArray().isEqual(MEDLoader.DataArrayDouble([0.,4.,8.,12.,16.,20.,24.,28.,32.,36.,40.,44.,48.,52.,56.,60.,64.,68.,72.,76.]),1e-12)) pass + + def testEasyFieldRead1(self): + fname="Pyfile111.med" + arr=MEDLoader.DataArrayDouble(4) ; arr.iota() + m=MEDLoader.MEDCouplingCMesh() ; m.setCoords(arr,arr) + m=m.buildUnstructured() + m.setName("mesh") + f=MEDLoader.MEDCouplingFieldDouble(MEDLoader.ON_CELLS) + f.setName("field") + f.setTime(3.,1,2) + da=MEDLoader.DataArrayDouble([2,3,4,5,6,7,8,9,10]) + f.setArray(da) ; f.setMesh(m) + MEDLoader.WriteField(fname,f,True) + # + self.assertTrue(f.isEqual(MEDLoader.ReadField(fname),1e-12,1e-12)) + self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field"),1e-12,1e-12)) + self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field",1,2),1e-12,1e-12)) + self.assertTrue(f.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",1,2),1e-12,1e-12)) + # + f3=f.deepCopy() + f3.setArray(f.getArray()+30) + f3.setName("field2") + f3.setTime(5.,4,5) + MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fname,f3) + # + self.assertRaises(Exception,MEDLoader.ReadField,fname) # because several fields in fname now + self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field"),1e-12,1e-12)) + self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field",1,2),1e-12,1e-12)) + self.assertTrue(f.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",1,2),1e-12,1e-12)) + self.assertTrue(f3.isEqual(MEDLoader.ReadField(fname,"field2"),1e-12,1e-12)) + self.assertTrue(f3.isEqual(MEDLoader.ReadField(fname,"field2",4,5),1e-12,1e-12)) + self.assertTrue(f3.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field2",4,5),1e-12,1e-12)) + # + f2=f.deepCopy() + f2.setTime(4.,3,4) + f2.setArray(f2.getArray()+10) + MEDLoader.WriteFieldUsingAlreadyWrittenMesh(fname,f2) + # + self.assertRaises(Exception,MEDLoader.ReadField,fname) # because unique field "field" has more than one time step + self.assertRaises(Exception,MEDLoader.ReadField,fname,"field") # because unique field "field" has more than one time step + self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field",1,2),1e-12,1e-12)) + self.assertTrue(f.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",1,2),1e-12,1e-12)) + self.assertTrue(f2.isEqual(MEDLoader.ReadField(fname,"field",3,4),1e-12,1e-12)) + self.assertTrue(f2.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",3,4),1e-12,1e-12)) + # + self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field",1,2),1e-12,1e-12)) + self.assertTrue(f.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",1,2),1e-12,1e-12)) + self.assertTrue(f2.isEqual(MEDLoader.ReadField(fname,"field",3,4),1e-12,1e-12)) + self.assertTrue(f2.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",3,4),1e-12,1e-12)) + # + self.assertTrue(f3.isEqual(MEDLoader.ReadField(fname,"field2"),1e-12,1e-12)) + self.assertTrue(f3.isEqual(MEDLoader.ReadField(fname,"field2",4,5),1e-12,1e-12)) + self.assertRaises(Exception,MEDLoader.ReadField,fname,"field2",5,5) # invalid time step + self.assertTrue(f3.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field2",4,5),1e-12,1e-12)) + self.assertRaises(Exception,MEDLoader.ReadField,MEDLoader.ON_CELLS,fname,"mesh",0,"field2",5,5) # invalid time step + # Test on profile - restart from scratch + mm=MEDLoader.MEDFileUMesh() + mm[0]=m + mm.write(fname,2) + # + pfl=MEDLoader.DataArrayInt(range(8)) + pfl.setName("PFL") + # + f=MEDLoader.MEDCouplingFieldDouble(MEDLoader.ON_CELLS) + f.setName("field") + f.setTime(3.,1,2) + da=MEDLoader.DataArrayDouble([2,3,4,5,6,7,8,9]) + f.setArray(da) ; f.setMesh(m[pfl]) + f.checkConsistencyLight() + # + f1ts=MEDLoader.MEDFileField1TS() + f1ts.setFieldProfile(f,mm,0,pfl) + f1ts.write(fname,0) + # + self.assertTrue(f.isEqual(MEDLoader.ReadField(fname),1e-12,1e-12)) + self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field"),1e-12,1e-12)) + self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field",1,2),1e-12,1e-12)) + self.assertTrue(f.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",1,2),1e-12,1e-12)) + # + f3=f.deepCopy() + f3.setArray(f.getArray()+30) + f3.setName("field2") + f3.setTime(5.,4,5) + f1ts=MEDLoader.MEDFileField1TS() + f1ts.setFieldProfile(f3,mm,0,pfl) + f1ts.write(fname,0) + # + self.assertRaises(Exception,MEDLoader.ReadField,fname) # because several fields in fname now + self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field"),1e-12,1e-12)) + self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field",1,2),1e-12,1e-12)) + self.assertTrue(f.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",1,2),1e-12,1e-12)) + self.assertTrue(f3.isEqual(MEDLoader.ReadField(fname,"field2"),1e-12,1e-12)) + self.assertTrue(f3.isEqual(MEDLoader.ReadField(fname,"field2",4,5),1e-12,1e-12)) + self.assertTrue(f3.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field2",4,5),1e-12,1e-12)) + # + f2=f.deepCopy() + f2.setTime(4.,3,4) + f2.setArray(f2.getArray()+10) + f1ts=MEDLoader.MEDFileField1TS() + f1ts.setFieldProfile(f2,mm,0,pfl) + f1ts.write(fname,0) + # + self.assertRaises(Exception,MEDLoader.ReadField,fname) # because unique field "field" has more than one time step + self.assertRaises(Exception,MEDLoader.ReadField,fname,"field") # because unique field "field" has more than one time step + self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field",1,2),1e-12,1e-12)) + self.assertTrue(f.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",1,2),1e-12,1e-12)) + self.assertTrue(f2.isEqual(MEDLoader.ReadField(fname,"field",3,4),1e-12,1e-12)) + self.assertTrue(f2.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",3,4),1e-12,1e-12)) + # + self.assertTrue(f.isEqual(MEDLoader.ReadField(fname,"field",1,2),1e-12,1e-12)) + self.assertTrue(f.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",1,2),1e-12,1e-12)) + self.assertTrue(f2.isEqual(MEDLoader.ReadField(fname,"field",3,4),1e-12,1e-12)) + self.assertTrue(f2.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field",3,4),1e-12,1e-12)) + # + self.assertTrue(f3.isEqual(MEDLoader.ReadField(fname,"field2"),1e-12,1e-12)) + self.assertTrue(f3.isEqual(MEDLoader.ReadField(fname,"field2",4,5),1e-12,1e-12)) + self.assertRaises(Exception,MEDLoader.ReadField,fname,"field2",5,5) # invalid time step + self.assertTrue(f3.isEqual(MEDLoader.ReadField(MEDLoader.ON_CELLS,fname,"mesh",0,"field2",4,5),1e-12,1e-12)) + self.assertRaises(Exception,MEDLoader.ReadField,MEDLoader.ON_CELLS,fname,"mesh",0,"field2",5,5) # invalid time step + pass + pass if __name__ == "__main__": diff --git a/src/MEDLoader/Test/MEDLoaderTest.cxx b/src/MEDLoader/Test/MEDLoaderTest.cxx index 6d5d041b8..26079d3a3 100644 --- a/src/MEDLoader/Test/MEDLoaderTest.cxx +++ b/src/MEDLoader/Test/MEDLoaderTest.cxx @@ -477,9 +477,8 @@ void MEDLoaderTest::testFieldGaussRW1() const char fileName[]="file13.med"; MEDCouplingFieldDouble *f1=buildVecFieldOnGauss_1(); WriteField(fileName,f1,true); - MEDCouplingFieldDouble *f2=ReadField(ON_GAUSS_PT,fileName,f1->getMesh()->getName().c_str(),0,f1->getName().c_str(),1,5); + MCAuto f2(ReadField(ON_GAUSS_PT,fileName,f1->getMesh()->getName().c_str(),0,f1->getName().c_str(),1,5)); CPPUNIT_ASSERT(f1->isEqual(f2,1e-12,1e-12)); - f2->decrRef(); f1->decrRef(); } @@ -491,9 +490,8 @@ void MEDLoaderTest::testFieldGaussNERW1() std::vector tof(GetTypesOfField(fileName,"2DMesh_2","MyFieldOnGaussNE")); CPPUNIT_ASSERT_EQUAL(1,(int)tof.size()); CPPUNIT_ASSERT(ON_GAUSS_NE==tof[0]); - MEDCouplingFieldDouble *f2=ReadField(ON_GAUSS_NE,fileName,f1->getMesh()->getName().c_str(),0,f1->getName().c_str(),1,5); + MCAuto f2(ReadField(ON_GAUSS_NE,fileName,f1->getMesh()->getName().c_str(),0,f1->getName().c_str(),1,5)); CPPUNIT_ASSERT(f1->isEqual(f2,1e-12,1e-12)); - f2->decrRef(); f1->decrRef(); }