From 9898f8695d7482a088a0ba2f0fbf3116261b3a2d Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Mon, 6 Mar 2017 08:43:13 +0100 Subject: [PATCH] Test remaining --- src/MEDLoader/MEDLoader.cxx | 64 +++++++++++++++++++++++++--- src/MEDLoader/MEDLoader.hxx | 6 ++- src/MEDLoader/Swig/MEDLoader.i | 3 ++ src/MEDLoader/Swig/MEDLoaderCommon.i | 54 ++++++++++++++++++++++- src/MEDLoader/Swig/MEDLoaderTest1.py | 4 ++ src/MEDLoader/Test/MEDLoaderTest.cxx | 6 +-- 6 files changed, 125 insertions(+), 12 deletions(-) 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/MEDLoader.i b/src/MEDLoader/Swig/MEDLoader.i index 7add2d533..80448bf9b 100644 --- a/src/MEDLoader/Swig/MEDLoader.i +++ b/src/MEDLoader/Swig/MEDLoader.i @@ -171,6 +171,9 @@ def MEDCouplingMEDFileIntFieldMultiTSnew(cls,*args): def MEDCouplingMEDFileParametersnew(cls,*args): import _MEDLoader return _MEDLoader.MEDFileParameters____new___(cls,args) +def ReadField(*args): + import _MEDLoader + return _MEDLoader.ReadFieldSwig(args) %} %include "MEDCouplingFinalize.i" diff --git a/src/MEDLoader/Swig/MEDLoaderCommon.i b/src/MEDLoader/Swig/MEDLoaderCommon.i index de7f087a4..edd599fea 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); @@ -372,6 +371,57 @@ namespace MEDCoupling PyTuple_SetItem(ret,2,SWIG_From_int(release)); return ret; } + + MEDCoupling::MEDCouplingFieldDouble *ReadFieldSwig(PyObject *elt0) throw(INTERP_KERNEL::Exception) + { + const char MSG[]="Invalid call ReadField !\n Supported are :\n - ReadField(fileName)\n - ReadField(fileName,fieldName)\n - ReadField(fileName,fieldName,iteration,order)\n - ReadField(type,fileName,meshName,meshDimRelToMax,fieldName,iteration,orde)"; + if(elt0 && PyString_Check(elt0)) + { + std::string st(PyString_AsString(elt0)); + MCAuto ret(MEDCoupling::ReadField(st)); + return ret.retn(); + } + if(!elt0 || !PyTuple_Check(elt0)) + throw INTERP_KERNEL::Exception(MSG); + int sz(PyTuple_Size(elt0)); + if(sz==2) + { + PyObject *o0(PyTuple_GetItem(elt0,0)),*o1(PyTuple_GetItem(elt0,1)); + if(PyString_Check(o0) && PyString_Check(o1)) + { + std::string st0(PyString_AsString(o0)),st1(PyString_AsString(o1)); + MCAuto ret(MEDCoupling::ReadField(st0,st1)); + return ret.retn(); + } + throw INTERP_KERNEL::Exception(MSG); + } + if(sz==4) + { + PyObject *o0(PyTuple_GetItem(elt0,0)),*o1(PyTuple_GetItem(elt0,1)),*o2(PyTuple_GetItem(elt0,2)),*o3(PyTuple_GetItem(elt0,3)); + if(PyString_Check(o0) && PyString_Check(o1) && PyInt_Check(o2) && PyInt_Check(o3)) + { + std::string st0(PyString_AsString(o0)),st1(PyString_AsString(o1)); + int a((int)PyInt_AS_LONG(o2)),b((int)PyInt_AS_LONG(o3)); + MCAuto ret(MEDCoupling::ReadField(st0,st1,a,b)); + return ret.retn(); + } + throw INTERP_KERNEL::Exception(MSG); + } + if(sz==7) + { + PyObject *o0(PyTuple_GetItem(elt0,0)),*o1(PyTuple_GetItem(elt0,1)),*o2(PyTuple_GetItem(elt0,2)),*o3(PyTuple_GetItem(elt0,3)),*o4(PyTuple_GetItem(elt0,4)),*o5(PyTuple_GetItem(elt0,5)),*o6(PyTuple_GetItem(elt0,6)); + if(PyInt_Check(o0) && PyString_Check(o1) && PyString_Check(o2) && PyInt_Check(o3) && PyString_Check(o4) && PyInt_Check(o5) && PyInt_Check(o6)) + { + int a((int)PyInt_AS_LONG(o0)),b((int)PyInt_AS_LONG(o3)),c((int)PyInt_AS_LONG(o5)),d((int)PyInt_AS_LONG(o6)); + MEDCoupling::TypeOfField aa((MEDCoupling::TypeOfField)a); + std::string st0(PyString_AsString(o1)),st1(PyString_AsString(o2)),st2(PyString_AsString(o4)); + MCAuto ret(MEDCoupling::ReadField(aa,st0,st1,b,st2,c,d)); + return ret.retn(); + } + throw INTERP_KERNEL::Exception(MSG); + } + throw INTERP_KERNEL::Exception(MSG); + } PyObject *GetFieldIterationsSwig(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, const std::string& fieldName) throw(INTERP_KERNEL::Exception) { diff --git a/src/MEDLoader/Swig/MEDLoaderTest1.py b/src/MEDLoader/Swig/MEDLoaderTest1.py index be8dba437..83b0fada2 100644 --- a/src/MEDLoader/Swig/MEDLoaderTest1.py +++ b/src/MEDLoader/Swig/MEDLoaderTest1.py @@ -751,6 +751,10 @@ 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): + 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(); } -- 2.39.2