From e381430d6351b6827fe2a7afce203d27a6206001 Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Mon, 4 Sep 2017 15:30:01 +0200 Subject: [PATCH] WriteField for everybody --- src/MEDLoader/MEDLoader.cxx | 59 +++++++++++++++++++++------- src/MEDLoader/MEDLoader.hxx | 2 +- src/MEDLoader/MEDLoaderNS.hxx | 3 +- src/MEDLoader/Swig/MEDLoaderCommon.i | 2 +- 4 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/MEDLoader/MEDLoader.cxx b/src/MEDLoader/MEDLoader.cxx index cd159472a..eeda4bde9 100644 --- a/src/MEDLoader/MEDLoader.cxx +++ b/src/MEDLoader/MEDLoader.cxx @@ -1474,11 +1474,12 @@ void MEDCoupling::WriteUMeshes(const std::string& fileName, const std::vectorwrite(fileName,mod); } -void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch) +template +void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const std::string& fileName, const typename MEDCoupling::Traits::FieldType *f, bool writeFromScratch) { - MCAuto ff(MEDFileField1TS::New()); + MCAuto< typename MLFieldTraits::F1TSType > ff(MLFieldTraits::F1TSType::New()); AssignStaticWritePropertiesTo(*ff); - MCAuto f2(f->deepCopy()); + MCAuto< typename MEDCoupling::Traits::FieldType > f2(f->deepCopy()); const MEDCouplingMesh *m(f2->getMesh()); const MEDCouplingUMesh *um(dynamic_cast(m)); const MEDCoupling1GTUMesh *um2(dynamic_cast(m)); @@ -1527,12 +1528,13 @@ void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const std::stri ff->write(fileName,0); } -void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch) +template +void WriteFieldImpl(const std::string& fileName, const typename MEDCoupling::Traits::FieldType *f, bool writeFromScratch) { if(!f) throw INTERP_KERNEL::Exception("WriteField : input field is NULL !"); f->checkConsistencyLight(); - int status=MEDLoaderBase::getStatusOfFile(fileName); + int status(MEDLoaderBase::getStatusOfFile(fileName)); if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST) { std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !"; @@ -1540,7 +1542,7 @@ void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MED } if(writeFromScratch || (!writeFromScratch && status==MEDLoaderBase::NOT_EXIST)) { - MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(fileName,f,true); + MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(fileName,f,true); } else { @@ -1549,7 +1551,7 @@ void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MED throw INTERP_KERNEL::Exception("WriteField : trying to write a field with no mesh !"); std::string fileNameCpp(f->getMesh()->getName()); if(std::find(meshNames.begin(),meshNames.end(),fileNameCpp)==meshNames.end()) - MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(fileName,f,false); + MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(fileName,f,false); else { MCAuto mm(MEDFileMesh::New(fileName,f->getMesh()->getName().c_str())); @@ -1558,11 +1560,11 @@ void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MED const MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(!mmuPtr) throw INTERP_KERNEL::Exception("WriteField : only umeshes are supported now !"); - MCAuto f2(f->deepCopy()); - MEDCouplingUMesh *m=dynamic_cast(const_cast(f2->getMesh())); + MCAuto::FieldType> f2(f->deepCopy()); + MEDCouplingUMesh *m(dynamic_cast(const_cast(f2->getMesh()))); if(!m) throw INTERP_KERNEL::Exception("WriteField : only umesh in input field supported !"); - MCAuto o2n=m->getRenumArrForMEDFileFrmt(); + MCAuto o2n(m->getRenumArrForMEDFileFrmt()); f2->renumberCells(o2n->begin(),false); m=static_cast(const_cast(f2->getMesh())); MCAuto mread=mmuPtr->getMeshAtLevel(m->getMeshDimension()-mm->getMeshDimension()); @@ -1570,14 +1572,14 @@ void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MED { m->tryToShareSameCoordsPermute(*mread,_EPS_FOR_NODE_COMP); DataArrayInt *part=0; - bool b=mread->areCellsIncludedIn(m,_COMP_FOR_CELL,part); + bool b(mread->areCellsIncludedIn(m,_COMP_FOR_CELL,part)); MCAuto partSafe(part); if(!b) { std::ostringstream oss; oss << "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()); } - MCAuto f1ts(MEDFileField1TS::New()); + MCAuto< typename MLFieldTraits::F1TSType > f1ts(MLFieldTraits::F1TSType::New()); AssignStaticWritePropertiesTo(*f1ts); if(part->isIota(mread->getNumberOfCells())) f1ts->setFieldNoProfileSBT(f2); @@ -1591,15 +1593,15 @@ void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MED } else { - DataArrayInt *part=0; - bool b=mread->getCoords()->areIncludedInMe(m->getCoords(),_EPS_FOR_NODE_COMP,part); + DataArrayInt *part(NULL); + bool b(mread->getCoords()->areIncludedInMe(m->getCoords(),_EPS_FOR_NODE_COMP,part)); MCAuto partSafe(part); if(!b) { std::ostringstream oss; oss << "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()); } - MCAuto f1ts(MEDFileField1TS::New()); + MCAuto< typename MLFieldTraits::F1TSType > f1ts(MLFieldTraits::F1TSType::New()); AssignStaticWritePropertiesTo(*f1ts); if(part->isIota(mread->getNumberOfNodes())) f1ts->setFieldNoProfileSBT(f2); @@ -1614,6 +1616,31 @@ void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MED } } +void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MEDCouplingField *f, bool writeFromScratch) +{ + const MEDCoupling::MEDCouplingFieldDouble *f1(dynamic_cast(f)); + if(f1) + { + WriteFieldImpl(fileName,f1,writeFromScratch); + return ; + } + const MEDCoupling::MEDCouplingFieldInt *f2(dynamic_cast(f)); + if(f2) + { + WriteFieldImpl(fileName,f2,writeFromScratch); + return ; + } + const MEDCoupling::MEDCouplingFieldFloat *f3(dynamic_cast(f)); + if(f3) + { + WriteFieldImpl(fileName,f3,writeFromScratch); + return ; + } + if(f) + throw INTERP_KERNEL::Exception("WriteField : only fielddouble, fieldint and fieldfloat are managed !"); + WriteFieldImpl(fileName,NULL,writeFromScratch); +} + void MEDCoupling::WriteFieldDep(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch) { WriteField(fileName,f,writeFromScratch); @@ -1666,6 +1693,8 @@ void MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, WriteFieldUsingAlreadyWrittenMeshImpl(fileName,f3); return ; } + if(f) + throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMesh : only fielddouble, fieldint and fieldfloat are managed !"); WriteFieldUsingAlreadyWrittenMeshImpl(fileName,NULL); } diff --git a/src/MEDLoader/MEDLoader.hxx b/src/MEDLoader/MEDLoader.hxx index aa17e825f..ad72e784d 100644 --- a/src/MEDLoader/MEDLoader.hxx +++ b/src/MEDLoader/MEDLoader.hxx @@ -97,7 +97,7 @@ namespace MEDCoupling MEDLOADER_EXPORT void WriteUMeshesPartition(const std::string& fileName, const std::string& meshName, const std::vector& meshes, bool writeFromScratch); MEDLOADER_EXPORT void WriteUMeshesPartitionDep(const std::string& fileName, const std::string& meshName, const std::vector& meshes, bool writeFromScratch); MEDLOADER_EXPORT void WriteUMeshes(const std::string& fileName, const std::vector& meshes, bool writeFromScratch); - MEDLOADER_EXPORT void WriteField(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch); + MEDLOADER_EXPORT void WriteField(const std::string& fileName, const MEDCoupling::MEDCouplingField *f, bool writeFromScratch); MEDLOADER_EXPORT void WriteFieldDep(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch); MEDLOADER_EXPORT void WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingField *f); diff --git a/src/MEDLoader/MEDLoaderNS.hxx b/src/MEDLoader/MEDLoaderNS.hxx index 3db880f54..b9a756a11 100644 --- a/src/MEDLoader/MEDLoaderNS.hxx +++ b/src/MEDLoader/MEDLoaderNS.hxx @@ -35,7 +35,8 @@ namespace MEDLoaderNS { int readUMeshDimFromFile(const std::string& fileName, const std::string& meshName, std::vector& possibilities); void dispatchElems(int nbOfElemCell, int nbOfElemFace, int& nbOfElem, med_entity_type& whichEntity); - void writeFieldWithoutReadingAndMappingOfMeshInFile(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch); + template + void writeFieldWithoutReadingAndMappingOfMeshInFile(const std::string& fileName, const typename MEDCoupling::Traits::FieldType *f, bool writeFromScratch); med_int getIdFromMeshName(med_idt fid, const std::string& meshName, std::string& trueMeshName); std::vector getMeshNamesFid(med_idt fid); } diff --git a/src/MEDLoader/Swig/MEDLoaderCommon.i b/src/MEDLoader/Swig/MEDLoaderCommon.i index 69c5f68b5..c6cd3aeb0 100644 --- a/src/MEDLoader/Swig/MEDLoaderCommon.i +++ b/src/MEDLoader/Swig/MEDLoaderCommon.i @@ -372,7 +372,7 @@ namespace MEDCoupling void WriteMesh(const std::string& fileName, const MEDCoupling::MEDCouplingMesh *mesh, bool writeFromScratch) throw(INTERP_KERNEL::Exception); void WriteUMesh(const std::string& fileName, const MEDCoupling::MEDCouplingUMesh *mesh, bool writeFromScratch) throw(INTERP_KERNEL::Exception); void WriteUMeshDep(const std::string& fileName, const MEDCoupling::MEDCouplingUMesh *mesh, bool writeFromScratch) throw(INTERP_KERNEL::Exception); - void WriteField(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch) throw(INTERP_KERNEL::Exception); + void WriteField(const std::string& fileName, const MEDCoupling::MEDCouplingField *f, bool writeFromScratch) throw(INTERP_KERNEL::Exception); void WriteFieldDep(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch) throw(INTERP_KERNEL::Exception); void WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingField *f) throw(INTERP_KERNEL::Exception); } -- 2.39.2