From 17bb0579eb0e338c852621d58de53570e958c0ec Mon Sep 17 00:00:00 2001 From: ageay Date: Thu, 12 Dec 2013 08:47:50 +0000 Subject: [PATCH] In MEDLoader basic API export the static policy in the MEDLoader advanced objects in basic write mode. --- src/MEDLoader/MEDLoader.cxx | 64 ++++++++++++++++++---------- src/MEDLoader/MEDLoader.hxx | 3 ++ src/MEDLoader/Swig/MEDLoaderCommon.i | 1 + src/MEDLoader/Swig/MEDLoaderTest.py | 43 +++++++++++++++++++ 4 files changed, 89 insertions(+), 22 deletions(-) diff --git a/src/MEDLoader/MEDLoader.cxx b/src/MEDLoader/MEDLoader.cxx index f14e17a15..75c9acc1a 100644 --- a/src/MEDLoader/MEDLoader.cxx +++ b/src/MEDLoader/MEDLoader.cxx @@ -284,6 +284,11 @@ void MEDLoaderNS::dispatchElems(int nbOfElemCell, int nbOfElemFace, int& nbOfEle /// @endcond +void MEDLoader::AssignStaticWritePropertiesTo(ParaMEDMEM::MEDFileWritable& obj) +{ + obj.setTooLongStrPolicy(_TOO_LONG_STR); +} + bool MEDLoader::HasXDR() { #ifdef HAS_XDR @@ -1063,7 +1068,7 @@ std::vector< std::pair > MEDLoader::GetNodeFieldIterations(const char * ParaMEDMEM::MEDCouplingMesh *MEDLoader::ReadMeshFromFile(const char *fileName, const char *meshName, int meshDimRelToMax) { CheckFileForRead(fileName); - MEDCouplingAutoRefCountObjectPtr mm=MEDFileMesh::New(fileName,meshName); + MEDCouplingAutoRefCountObjectPtr mm(MEDFileMesh::New(fileName,meshName)); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(mmuPtr) @@ -1087,7 +1092,7 @@ ParaMEDMEM::MEDCouplingMesh *MEDLoader::ReadMeshFromFile(const char *fileName, c ParaMEDMEM::MEDCouplingMesh *MEDLoader::ReadMeshFromFile(const char *fileName, int meshDimRelToMax) { CheckFileForRead(fileName); - MEDCouplingAutoRefCountObjectPtr mm=MEDFileMesh::New(fileName); + MEDCouplingAutoRefCountObjectPtr mm(MEDFileMesh::New(fileName)); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(mmuPtr) @@ -1111,7 +1116,7 @@ ParaMEDMEM::MEDCouplingMesh *MEDLoader::ReadMeshFromFile(const char *fileName, i ParaMEDMEM::MEDCouplingUMesh *MEDLoader::ReadUMeshFromFile(const char *fileName, const char *meshName, int meshDimRelToMax) { CheckFileForRead(fileName); - MEDCouplingAutoRefCountObjectPtr mm=MEDFileMesh::New(fileName,meshName); + MEDCouplingAutoRefCountObjectPtr mm(MEDFileMesh::New(fileName,meshName)); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(!mmuPtr) @@ -1125,7 +1130,7 @@ ParaMEDMEM::MEDCouplingUMesh *MEDLoader::ReadUMeshFromFile(const char *fileName, ParaMEDMEM::MEDCouplingUMesh *MEDLoader::ReadUMeshFromFile(const char *fileName, int meshDimRelToMax) { CheckFileForRead(fileName); - MEDCouplingAutoRefCountObjectPtr mm=MEDFileMesh::New(fileName); + MEDCouplingAutoRefCountObjectPtr mm(MEDFileMesh::New(fileName)); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(!mmuPtr) @@ -1146,7 +1151,7 @@ int MEDLoader::ReadUMeshDimFromFile(const char *fileName, const char *meshName) ParaMEDMEM::MEDCouplingUMesh *MEDLoader::ReadUMeshFromFamilies(const char *fileName, const char *meshName, int meshDimRelToMax, const std::vector& fams) { CheckFileForRead(fileName); - MEDCouplingAutoRefCountObjectPtr mm=MEDFileMesh::New(fileName,meshName); + MEDCouplingAutoRefCountObjectPtr mm(MEDFileMesh::New(fileName,meshName)); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(!mmuPtr) @@ -1357,7 +1362,8 @@ void MEDLoader::WriteMesh(const char *fileName, const ParaMEDMEM::MEDCouplingMes const MEDCoupling1GTUMesh *um2(dynamic_cast(mesh)); if(um2) { - MEDCouplingAutoRefCountObjectPtr mmu=MEDFileUMesh::New(); + MEDCouplingAutoRefCountObjectPtr mmu(MEDFileUMesh::New()); + AssignStaticWritePropertiesTo(*mmu); mmu->setMeshAtLevel(0,const_cast(um2)); mmu->write(fileName,mod); return ; @@ -1365,7 +1371,8 @@ void MEDLoader::WriteMesh(const char *fileName, const ParaMEDMEM::MEDCouplingMes const MEDCouplingCMesh *um3(dynamic_cast(mesh)); if(um3) { - MEDCouplingAutoRefCountObjectPtr mmc=MEDFileCMesh::New(); + MEDCouplingAutoRefCountObjectPtr mmc(MEDFileCMesh::New()); + AssignStaticWritePropertiesTo(*mmc); mmc->setMesh(const_cast(um3)); mmc->write(fileName,mod); return ; @@ -1373,7 +1380,8 @@ void MEDLoader::WriteMesh(const char *fileName, const ParaMEDMEM::MEDCouplingMes const MEDCouplingCurveLinearMesh *um4(dynamic_cast(mesh)); if(um4) { - MEDCouplingAutoRefCountObjectPtr mmc=MEDFileCurveLinearMesh::New(); + MEDCouplingAutoRefCountObjectPtr mmc(MEDFileCurveLinearMesh::New()); + AssignStaticWritePropertiesTo(*mmc); mmc->setMesh(const_cast(um4)); mmc->write(fileName,mod); return ; @@ -1386,7 +1394,8 @@ void MEDLoader::WriteUMesh(const char *fileName, const ParaMEDMEM::MEDCouplingUM if(!mesh) throw INTERP_KERNEL::Exception("MEDLoader::WriteUMesh : input mesh is null !"); int mod=writeFromScratch?2:0; - MEDCouplingAutoRefCountObjectPtr m=MEDFileUMesh::New(); + MEDCouplingAutoRefCountObjectPtr m(MEDFileUMesh::New()); + AssignStaticWritePropertiesTo(*m); MEDCouplingAutoRefCountObjectPtr mcpy(static_cast(mesh->deepCpy())); m->setMeshAtLevel(0,mcpy,true); m->write(fileName,mod); @@ -1408,7 +1417,8 @@ void MEDLoader::WriteUMeshesPartition(const char *fileName, const char *meshName std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } - MEDCouplingAutoRefCountObjectPtr m=MEDFileUMesh::New(); + MEDCouplingAutoRefCountObjectPtr m(MEDFileUMesh::New()); + AssignStaticWritePropertiesTo(*m); m->setGroupsFromScratch(0,meshes,true); m->setName(meshNameC); int mod=writeFromScratch?2:0; @@ -1423,14 +1433,16 @@ void MEDLoader::WriteUMeshesPartitionDep(const char *fileName, const char *meshN void MEDLoader::WriteUMeshes(const char *fileName, const std::vector& meshes, bool writeFromScratch) { int mod=writeFromScratch?2:0; - MEDCouplingAutoRefCountObjectPtr m=MEDFileUMesh::New(); + MEDCouplingAutoRefCountObjectPtr m(MEDFileUMesh::New()); + AssignStaticWritePropertiesTo(*m); m->setMeshes(meshes,true); m->write(fileName,mod); } void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, bool writeFromScratch) { - MEDCouplingAutoRefCountObjectPtr ff=MEDFileField1TS::New(); + MEDCouplingAutoRefCountObjectPtr ff(MEDFileField1TS::New()); + MEDLoader::AssignStaticWritePropertiesTo(*ff); MEDCouplingAutoRefCountObjectPtr f2(f->deepCpy()); const MEDCouplingMesh *m(f2->getMesh()); const MEDCouplingUMesh *um(dynamic_cast(m)); @@ -1441,9 +1453,10 @@ void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const char *fil int mod=writeFromScratch?2:0; if(um) { - MEDCouplingAutoRefCountObjectPtr mmu=MEDFileUMesh::New(); - MEDCouplingAutoRefCountObjectPtr o2n=um->getRenumArrForMEDFileFrmt(); - MEDCouplingAutoRefCountObjectPtr n2o=o2n->invertArrayO2N2N2O(o2n->getNumberOfTuples()); + MEDCouplingAutoRefCountObjectPtr mmu(MEDFileUMesh::New()); + MEDLoader::AssignStaticWritePropertiesTo(*mmu); + MEDCouplingAutoRefCountObjectPtr o2n(um->getRenumArrForMEDFileFrmt()); + MEDCouplingAutoRefCountObjectPtr n2o(o2n->invertArrayO2N2N2O(o2n->getNumberOfTuples())); f2->renumberCells(o2n->begin(),false); mmu->setMeshAtLevel(0,const_cast(static_cast(f2->getMesh()))); mmu->setRenumFieldArr(0,n2o); @@ -1452,21 +1465,24 @@ void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const char *fil } else if(um2) { - MEDCouplingAutoRefCountObjectPtr mmu=MEDFileUMesh::New(); + MEDCouplingAutoRefCountObjectPtr mmu(MEDFileUMesh::New()); + MEDLoader::AssignStaticWritePropertiesTo(*mmu); mmu->setMeshAtLevel(0,const_cast(um2)); ff->setFieldNoProfileSBT(f2); mmu->write(fileName,mod); } else if(um3) { - MEDCouplingAutoRefCountObjectPtr mmc=MEDFileCMesh::New(); + MEDCouplingAutoRefCountObjectPtr mmc(MEDFileCMesh::New()); + MEDLoader::AssignStaticWritePropertiesTo(*mmc); mmc->setMesh(const_cast(um3)); ff->setFieldNoProfileSBT(f2); mmc->write(fileName,mod); } else if(um4) { - MEDCouplingAutoRefCountObjectPtr mmc=MEDFileCurveLinearMesh::New(); + MEDCouplingAutoRefCountObjectPtr mmc(MEDFileCurveLinearMesh::New()); + MEDLoader::AssignStaticWritePropertiesTo(*mmc); mmc->setMesh(const_cast(um4)); ff->setFieldNoProfileSBT(f2); mmc->write(fileName,mod); @@ -1501,7 +1517,8 @@ void MEDLoader::WriteField(const char *fileName, const ParaMEDMEM::MEDCouplingFi MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(fileName,f,false); else { - MEDCouplingAutoRefCountObjectPtr mm=MEDFileMesh::New(fileName,f->getMesh()->getName().c_str()); + MEDCouplingAutoRefCountObjectPtr mm(MEDFileMesh::New(fileName,f->getMesh()->getName().c_str())); + AssignStaticWritePropertiesTo(*mm); const MEDFileMesh *mmPtr(mm); const MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(!mmuPtr) @@ -1525,7 +1542,8 @@ void MEDLoader::WriteField(const char *fileName, const ParaMEDMEM::MEDCouplingFi std::ostringstream oss; oss << "MEDLoader::WriteField : The file \""<< fileName << "\" already contains a mesh named \""<< f->getMesh()->getName() << "\" and this mesh in the file is not compatible (a subpart) with the mesh you intend to write ! This is maybe due to a too strict policy ! Try with to lease it by calling SetCompPolicyForCell !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } - MEDCouplingAutoRefCountObjectPtr f1ts=MEDFileField1TS::New(); + MEDCouplingAutoRefCountObjectPtr f1ts(MEDFileField1TS::New()); + AssignStaticWritePropertiesTo(*f1ts); if(part->isIdentity() && part->getNumberOfTuples()==mread->getNumberOfCells()) f1ts->setFieldNoProfileSBT(f2); else @@ -1546,7 +1564,8 @@ void MEDLoader::WriteField(const char *fileName, const ParaMEDMEM::MEDCouplingFi std::ostringstream oss; oss << "MEDLoader::WriteField : The file \""<< fileName << "\" already contains a mesh named \""<< f->getMesh()->getName() << "\" and this mesh in the file is not compatible (a subpart regarding nodes) with the mesh you intend to write ! This is maybe due to a too strict epsilon ! Try with to lease it by calling SetEpsilonForNodeComp !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } - MEDCouplingAutoRefCountObjectPtr f1ts=MEDFileField1TS::New(); + MEDCouplingAutoRefCountObjectPtr f1ts(MEDFileField1TS::New()); + AssignStaticWritePropertiesTo(*f1ts); if(part->isIdentity() && part->getNumberOfTuples()==mread->getNumberOfNodes()) f1ts->setFieldNoProfileSBT(f2); else @@ -1576,7 +1595,8 @@ void MEDLoader::WriteFieldUsingAlreadyWrittenMesh(const char *fileName, const Pa std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions or not exists !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } - MEDCouplingAutoRefCountObjectPtr f1ts=MEDFileField1TS::New(); + MEDCouplingAutoRefCountObjectPtr f1ts(MEDFileField1TS::New()); + AssignStaticWritePropertiesTo(*f1ts); MEDCouplingUMesh *m=dynamic_cast(const_cast(f->getMesh())); if(!m) throw INTERP_KERNEL::Exception("MEDLoader::WriteFieldUsingAlreadyWrittenMesh : only umesh in input field supported !"); diff --git a/src/MEDLoader/MEDLoader.hxx b/src/MEDLoader/MEDLoader.hxx index 5e7187a19..1f0a7b130 100644 --- a/src/MEDLoader/MEDLoader.hxx +++ b/src/MEDLoader/MEDLoader.hxx @@ -35,6 +35,7 @@ namespace ParaMEDMEM class MEDCouplingMesh; class MEDCouplingUMesh; class MEDCouplingFieldDouble; + class MEDFileWritable; } class MEDLOADER_EXPORT MEDLoader @@ -97,6 +98,8 @@ class MEDLOADER_EXPORT MEDLoader static void WriteField(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, bool writeFromScratch); static void WriteFieldDep(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, bool writeFromScratch); static void WriteFieldUsingAlreadyWrittenMesh(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f); + public: + static void AssignStaticWritePropertiesTo(ParaMEDMEM::MEDFileWritable& obj); private: MEDLoader(); public: diff --git a/src/MEDLoader/Swig/MEDLoaderCommon.i b/src/MEDLoader/Swig/MEDLoaderCommon.i index 991bb2f1f..369001259 100644 --- a/src/MEDLoader/Swig/MEDLoaderCommon.i +++ b/src/MEDLoader/Swig/MEDLoaderCommon.i @@ -239,6 +239,7 @@ public: static std::vector GetCellFieldNamesOnMesh(const char *fileName, const char *meshName) throw(INTERP_KERNEL::Exception); static std::vector GetNodeFieldNamesOnMesh(const char *fileName, const char *meshName) throw(INTERP_KERNEL::Exception); static double GetTimeAttachedOnFieldIteration(const char *fileName, const char *fieldName, int iteration, int order) throw(INTERP_KERNEL::Exception); + static void AssignStaticWritePropertiesTo(ParaMEDMEM::MEDFileWritable& obj) throw(INTERP_KERNEL::Exception); %extend { static PyObject *MEDFileVersion() diff --git a/src/MEDLoader/Swig/MEDLoaderTest.py b/src/MEDLoader/Swig/MEDLoaderTest.py index f04d53d95..441ee6610 100644 --- a/src/MEDLoader/Swig/MEDLoaderTest.py +++ b/src/MEDLoader/Swig/MEDLoaderTest.py @@ -667,6 +667,49 @@ class MEDLoaderTest(unittest.TestCase): self.assertTrue("xonall1" in mesh.getGroupsNames()) pass + def testFieldWithTooLongName(self): + """ This test is a non regression test, to check that in basic API the policies are taken into account. + """ + fname="Pyfile75.med" + # Coordinates + coords = [0.,0., 0.,1., 1.,1., 1.,0.] + # lvl 0 connectivity + conn2D = [1,2,3,4] + # lvl 0 mesh + m=MEDLoader.MEDCouplingUMesh.New("mesh",2) + m.allocateCells(1) + m.insertNextCell(MEDLoader.NORM_QUAD4,4,conn2D) + m.finishInsertingCells() + # assigning coordinates + meshCoords=MEDLoader.DataArrayDouble.New() + meshCoords.setValues(coords, 4, 2) + m.setCoords(meshCoords) + # + f=MEDLoader.MEDCouplingFieldDouble.New(MEDLoader.ON_CELLS,MEDLoader.ONE_TIME) + f.setMesh(m) + d=MEDLoader.DataArrayDouble.New() + d.alloc(1,1) + d.iota(1.) + # seting a long name + d.setInfoOnComponent(0,"CONCENTRATION of I129") + f.setArray(d) + f.setName("field") + # + mm=MEDLoader.MEDFileUMesh() + MEDLoader.MEDLoader.SetTooLongStrPolicy(2) + MEDLoader.MEDLoader.AssignStaticWritePropertiesTo(mm) + self.assertEqual(2,mm.getTooLongStrPolicy()) + MEDLoader.MEDLoader.SetTooLongStrPolicy(0) + MEDLoader.MEDLoader.AssignStaticWritePropertiesTo(mm) + self.assertEqual(0,mm.getTooLongStrPolicy()) + del mm + # + MEDLoader.MEDLoader.SetTooLongStrPolicy(2) + self.assertRaises(MEDLoader.InterpKernelException,MEDLoader.MEDLoader.WriteField,fname,f,True)# the component name is too long + policy 2 -> throw + f.getArray().setInfoOnComponent(0,'I129') + MEDLoader.MEDLoader.WriteField(fname,f,True) + pass + pass unittest.main() -- 2.39.2