X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FMEDLoader%2FMEDLoader.cxx;h=687d0bcbf35a6d6c4b3b25c6b29c96bf83af4a3b;hb=f2ab61e92f55ee52cf5196f7904f625d93c8c2d0;hp=c6ad9872ee2da1a14a4b48be2ef06bea57d3d16e;hpb=5da72d398d0eb1820c3ce6dd90b8579b6b14edf5;p=tools%2Fmedcoupling.git diff --git a/src/MEDLoader/MEDLoader.cxx b/src/MEDLoader/MEDLoader.cxx index c6ad9872e..687d0bcbf 100644 --- a/src/MEDLoader/MEDLoader.cxx +++ b/src/MEDLoader/MEDLoader.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2016 CEA/DEN, EDF R&D +// Copyright (C) 2007-2020 CEA/DEN, EDF R&D // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -21,6 +21,7 @@ #include "MEDLoader.hxx" #include "MEDLoaderBase.hxx" #include "MEDFileUtilities.hxx" +#include "MEDLoaderNS.hxx" #include "MEDFileSafeCaller.txx" #include "MEDFileMesh.hxx" #include "MEDFileField.hxx" @@ -28,7 +29,10 @@ #include "MEDCouplingUMesh.hxx" #include "MEDCouplingMemArray.hxx" #include "MEDCouplingFieldDouble.hxx" +#include "MEDCouplingFieldFloat.hxx" +#include "MEDCouplingFieldInt32.hxx" #include "MEDCouplingGaussLocalization.hxx" +#include "MEDCouplingTraits.hxx" #include "MCAuto.hxx" #include "InterpKernelAutoPtr.hxx" @@ -43,7 +47,7 @@ #include #include #include - +#include med_geometry_type typmai[MED_N_CELL_FIXED_GEO] = { MED_POINT1, MED_SEG2, @@ -63,6 +67,7 @@ med_geometry_type typmai[MED_N_CELL_FIXED_GEO] = { MED_POINT1, MED_TETRA10, MED_PYRA13, MED_PENTA15, + MED_PENTA18, MED_HEXA20, MED_HEXA27, MED_POLYGON, @@ -89,13 +94,14 @@ INTERP_KERNEL::NormalizedCellType typmai2[MED_N_CELL_FIXED_GEO] = { INTERP_KERNE INTERP_KERNEL::NORM_TETRA10, INTERP_KERNEL::NORM_PYRA13, INTERP_KERNEL::NORM_PENTA15, + INTERP_KERNEL::NORM_PENTA18, INTERP_KERNEL::NORM_HEXA20, INTERP_KERNEL::NORM_HEXA27, INTERP_KERNEL::NORM_POLYGON, INTERP_KERNEL::NORM_QPOLYG, INTERP_KERNEL::NORM_POLYHED }; -med_geometry_type typmai3[34] = { MED_POINT1,//0 +med_geometry_type typmai3[INTERP_KERNEL::NORM_MAXTYPE] = { MED_POINT1,//0 MED_SEG2,//1 MED_SEG3,//2 MED_TRIA3,//3 @@ -123,12 +129,11 @@ med_geometry_type typmai3[34] = { MED_POINT1,//0 MED_PENTA15,//25 MED_NONE,//26 MED_HEXA27,//27 - MED_NONE,//28 + MED_PENTA18,//28 MED_NONE,//29 MED_HEXA20,//30 MED_POLYHEDRON,//31 - MED_POLYGON2,//32 - MED_NONE//33 + MED_POLYGON2//32 }; double _EPS_FOR_NODE_COMP=1.e-12; @@ -141,20 +146,20 @@ using namespace MEDCoupling; /// @cond INTERNAL -namespace MEDLoaderNS +INTERP_KERNEL::NormalizedCellType ConvertGeometryType(med_geometry_type geotype) { - 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); - med_int getIdFromMeshName(med_idt fid, const std::string& meshName, std::string& trueMeshName); - std::vector getMeshNamesFid(med_idt fid); + INTERP_KERNEL::NormalizedCellType result=INTERP_KERNEL::NORM_ERROR; + for(int i=0; i& possibilities) { possibilities.clear(); - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); int ret; std::set poss; char nommaa[MED_NAME_SIZE+1]; @@ -171,12 +176,12 @@ int MEDLoaderNS::readUMeshDimFromFile(const std::string& fileName, const std::st med_mesh_type type_maillage; med_int Sdim,Mdim; std::string trueMeshName; - med_int meshId=getIdFromMeshName(fid,meshName,trueMeshName); + int meshId=FromMedInt(getIdFromMeshName(fid,meshName,trueMeshName)); INTERP_KERNEL::AutoPtr dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE); med_sorting_type sortingType; med_int nstep; med_axis_type axisType; - int naxis(MEDmeshnAxis(fid,meshId)); + med_int naxis(MEDmeshnAxis(fid,meshId)); INTERP_KERNEL::AutoPtr axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE); INTERP_KERNEL::AutoPtr axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE); MEDFILESAFECALLERRD0(MEDmeshInfo,(fid,meshId,nommaa,&Sdim,&Mdim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit)); @@ -193,9 +198,9 @@ int MEDLoaderNS::readUMeshDimFromFile(const std::string& fileName, const std::st { med_geometry_type curMedType=typmai[i]; med_bool changement,transformation; - int curNbOfElemM(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation)); - int curNbOfElemF(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));//limitation - int curNbOfElem; + med_int curNbOfElemM(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation)); + med_int curNbOfElemF(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));//limitation + med_int curNbOfElem; med_entity_type whichEntity; MEDLoaderNS::dispatchElems(curNbOfElemM,curNbOfElemF,curNbOfElem,whichEntity); if(curNbOfElem>0) @@ -216,8 +221,8 @@ int MEDLoaderNS::readUMeshDimFromFile(const std::string& fileName, const std::st return ret; } -med_int MEDLoaderNS::getIdFromMeshName(med_idt fid, const std::string& meshName, std::string& trueMeshName) - { +int MEDLoaderNS::getIdFromMeshName(med_idt fid, const std::string& meshName, std::string& trueMeshName) +{ if(meshName.empty()) { std::vector meshes=getMeshNamesFid(fid); @@ -239,8 +244,8 @@ med_int MEDLoaderNS::getIdFromMeshName(med_idt fid, const std::string& meshName, throw INTERP_KERNEL::Exception(os2.str().c_str()); } trueMeshName=meshName; - return iter-meshes.begin()+1; - } + return (int)( iter-meshes.begin()+1 ); +} std::vector MEDLoaderNS::getMeshNamesFid(med_idt fid) { @@ -256,10 +261,10 @@ std::vector MEDLoaderNS::getMeshNamesFid(med_idt fid) std::vector ret(n); for(int i=0;i axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE); INTERP_KERNEL::AutoPtr axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE); - int nstep; + med_int nstep; MEDFILESAFECALLERRD0(MEDmeshInfo,(fid,i+1,nommaa,&space_dim,&mesh_dim,&type_maillage,maillage_description,dtunit,&stype,&nstep,&axistype,axisname,axisunit)); std::string cur=MEDLoaderBase::buildStringFromFortran(nommaa,sizeof(nommaa)); ret[i]=cur; @@ -270,7 +275,7 @@ std::vector MEDLoaderNS::getMeshNamesFid(med_idt fid) /*! * This methods allows to merger all entities and to considerate only cell types. */ -void MEDLoaderNS::dispatchElems(int nbOfElemCell, int nbOfElemFace, int& nbOfElem, med_entity_type& whichEntity) +void MEDLoaderNS::dispatchElems(med_int nbOfElemCell, med_int nbOfElemFace, med_int& nbOfElem, med_entity_type& whichEntity) { if(nbOfElemCell>=nbOfElemFace) { @@ -302,18 +307,59 @@ bool MEDCoupling::HasXDR() std::string MEDCoupling::MEDFileVersionStr() { - return std::string(MED_VERSION_STR); + const int SZ=20; + char buf[SZ]; + std::fill(buf,buf+SZ,'\0'); + const char START_EXPECTED[]="MED-"; + med_err ret(MEDlibraryStrVersion(buf)); + if(ret!=0) + throw INTERP_KERNEL::Exception("MEDFileVersionStr : fail to find version of MED file ! It looks very bad !"); + std::string zeRet(buf); + std::size_t pos(zeRet.find(START_EXPECTED,0)); + if(pos!=0) + { + std::ostringstream oss; oss << "MEDFileVersionStr : internal error ! The MEDFile returned version (\"" << zeRet << "\") has not the right pattern !"; + throw INTERP_KERNEL::Exception(oss.str()); + } + return zeRet.substr(sizeof(START_EXPECTED)-1,std::string::npos); +} + +std::string MEDCoupling::MEDFileVersionOfFileStr(const std::string& fileName) +{ +#if MED_NUM_MAJEUR>3 || ( MED_NUM_MAJEUR==3 && ( (MED_NUM_MINEUR==2 && MED_NUM_RELEASE>=1) || MED_NUM_MINEUR>=3) ) + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); + const int SZ=20; + const char START_EXPECTED[]="MED-"; + char buf[SZ]; + std::fill(buf,buf+SZ,'\0'); + MEDFILESAFECALLERRD0(MEDfileStrVersionRd,(fid,buf)); + std::string ret(buf); + std::size_t pos(ret.find(START_EXPECTED,0)); + if(pos!=0) + { + std::ostringstream oss; oss << "MEDFileVersionOfFileStr : internal error ! The MEDFile returned version (\"" << ret << "\") has not the right pattern !"; + throw INTERP_KERNEL::Exception(oss.str()); + } + return ret.substr(sizeof(START_EXPECTED)-1,std::string::npos); +#else + std::ostringstream oss; oss << "MEDFileVersionOfFileStr : is implemented with MEDFile " << MEDFileVersionStr() << " ! If you need this feature please use version >= 3.2.1."; + throw INTERP_KERNEL::Exception(oss.str()); +#endif } void MEDCoupling::MEDFileVersion(int& major, int& minor, int& release) { - major=MED_NUM_MAJEUR; - minor=MED_NUM_MINEUR; - release=MED_NUM_RELEASE; + med_int majj,minn,rell; + med_err ret(MEDlibraryNumVersion(&majj,&minn,&rell)); + if(ret!=0) + throw INTERP_KERNEL::Exception("MEDFileVersion : fail to call MEDlibraryNumVersion ! It looks very bad !"); + major=FromMedInt(majj); + minor=FromMedInt(minn); + release=FromMedInt(rell); } /*! - * This method sets the epsilon value used for node comparison when trying to buid a profile for a field on node/cell on an already written mesh. + * This method sets the epsilon value used for node comparison when trying to build a profile for a field on node/cell on an already written mesh. */ void MEDCoupling::SetEpsilonForNodeComp(double val) { @@ -345,24 +391,25 @@ void MEDCoupling::SetTooLongStrPolicy(int val) * - the space dimension * - the number of nodes */ -std::vector< std::vector< std::pair > > MEDCoupling::GetUMeshGlobalInfo(const std::string& fileName, const std::string& meshName, int &meshDim, int& spaceDim, int& numberOfNodes) +std::vector< std::vector< std::pair > > MEDCoupling::GetUMeshGlobalInfo(const std::string& fileName, const std::string& meshName, int &meshDim, int& spaceDim, mcIdType& numberOfNodes) { - CheckFileForRead(fileName); - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); std::set poss; char nommaa[MED_NAME_SIZE+1]; char maillage_description[MED_COMMENT_SIZE+1]; med_mesh_type type_maillage; std::string trueMeshName; - med_int meshId=MEDLoaderNS::getIdFromMeshName(fid,meshName,trueMeshName); + int meshId=MEDLoaderNS::getIdFromMeshName(fid,meshName,trueMeshName); INTERP_KERNEL::AutoPtr dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE); med_sorting_type sortingType; - med_int nstep; + med_int nstep, mDim, sDim; med_axis_type axisType; - int naxis(MEDmeshnAxis(fid,meshId)); + med_int naxis(MEDmeshnAxis(fid,meshId)); INTERP_KERNEL::AutoPtr axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE); INTERP_KERNEL::AutoPtr axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE); - MEDFILESAFECALLERRD0(MEDmeshInfo,(fid,meshId,nommaa,&spaceDim,&meshDim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit)); + MEDFILESAFECALLERRD0(MEDmeshInfo,(fid,meshId,nommaa,&sDim,&mDim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit)); + meshDim=FromMedInt(mDim); + spaceDim=FromMedInt(sDim); if(type_maillage!=MED_UNSTRUCTURED_MESH) { std::ostringstream oss; oss << "GetUMeshGlobalInfo : Mesh \""<< meshName << "\" in file \"" << fileName; @@ -382,7 +429,7 @@ std::vector< std::vector< std::pair > > M for(int i=0;i0) { INTERP_KERNEL::NormalizedCellType typp=typmai2[i]; @@ -399,7 +446,7 @@ std::vector< std::vector< std::pair > > M { ret[maxLev-dims[i]].push_back(geoTypes[i]); } - numberOfNodes=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation); + numberOfNodes=ToIdType(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation)); return ret; } @@ -410,16 +457,13 @@ void MEDCoupling::CheckFileForRead(const std::string& fileName) std::vector MEDCoupling::GetMeshNames(const std::string& fileName) { - MEDCoupling::CheckFileForRead(fileName); - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY); - std::vector ret=MEDLoaderNS::getMeshNamesFid(fid); - return ret; + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); + return MEDLoaderNS::getMeshNamesFid(fid); } std::vector< std::pair > MEDCoupling::GetComponentsNamesOfField(const std::string& fileName, const std::string& fieldName) { - MEDCoupling::CheckFileForRead(fileName); - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields(MEDnField(fid)); std::vector fields(nbFields); med_field_type typcha; @@ -452,12 +496,42 @@ std::vector< std::pair > MEDCoupling::GetComponentsName throw INTERP_KERNEL::Exception(oss.str().c_str()); } +// see reference : https://en.cppreference.com/w/cpp/iterator/iterator +class MEDVectorStringIterator : public std::iterator< std::input_iterator_tag, long, long, const std::string*, std::string > +{ + long _num = 0; + char *_data = nullptr; +public: + explicit MEDVectorStringIterator(long num , char *data) : _num(num),_data(data) {} + MEDVectorStringIterator& operator++() { ++_num; return *this;} + bool operator==(const MEDVectorStringIterator& other) const {return _num == other._num;} + bool operator!=(const MEDVectorStringIterator& other) const {return !(*this == other);} + reference operator*() const {return MEDLoaderBase::buildStringFromFortran(_data+_num*MED_LNAME_SIZE,MED_LNAME_SIZE);} +}; + +void MEDCoupling::GetFamiliesGroupsInfo(const std::string& fileName, const std::string& meshName, std::map& families, std::map>& groupsOnFam) +{ + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); + med_int nbFams(MEDnFamily(fid,meshName.c_str())); + char nomfam[MED_NAME_SIZE+1]; + for(med_int i=0;i gro{new char[MED_LNAME_SIZE*nbGrps+1]}; + MEDFILESAFECALLERRD0(MEDfamilyInfo,(fid,meshName.c_str(),i+1,nomfam,&famId,gro.get())); + std::string fam(MEDLoaderBase::buildStringFromFortran(nomfam,MED_NAME_SIZE)); + families[fam] = FromMedInt(famId); + std::vector v(nbGrps); + std::copy(MEDVectorStringIterator(0,gro.get()),MEDVectorStringIterator(nbGrps,gro.get()),v.begin()); + groupsOnFam[fam] = v; + } +} + std::vector MEDCoupling::GetMeshNamesOnField(const std::string& fileName, const std::string& fieldName) { - MEDCoupling::CheckFileForRead(fileName); std::vector ret; // - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields=MEDnField(fid); // med_field_type typcha; @@ -483,15 +557,14 @@ std::vector MEDCoupling::GetMeshNamesOnField(const std::string& fil std::vector MEDCoupling::GetMeshFamiliesNames(const std::string& fileName, const std::string& meshName) { - MEDCoupling::CheckFileForRead(fileName); - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nfam=MEDnFamily(fid,meshName.c_str()); std::vector ret(nfam); char nomfam[MED_NAME_SIZE+1]; med_int numfam; for(int i=0;i attide=new med_int[natt]; INTERP_KERNEL::AutoPtr attval=new med_int[natt]; @@ -507,15 +580,14 @@ std::vector MEDCoupling::GetMeshFamiliesNames(const std::string& fi std::vector MEDCoupling::GetMeshFamiliesNamesOnGroup(const std::string& fileName, const std::string& meshName, const std::string& grpName) { - MEDCoupling::CheckFileForRead(fileName); - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nfam=MEDnFamily(fid,meshName.c_str()); std::vector ret; char nomfam[MED_NAME_SIZE+1]; med_int numfam; for(int i=0;i attide=new med_int[natt]; INTERP_KERNEL::AutoPtr attval=new med_int[natt]; @@ -535,16 +607,15 @@ std::vector MEDCoupling::GetMeshFamiliesNamesOnGroup(const std::str std::vector MEDCoupling::GetMeshGroupsNamesOnFamily(const std::string& fileName, const std::string& meshName, const std::string& famName) { - MEDCoupling::CheckFileForRead(fileName); - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY); - med_int nfam=MEDnFamily(fid,meshName.c_str()); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); + med_int nfam(MEDnFamily(fid,meshName.c_str())); std::vector ret; char nomfam[MED_NAME_SIZE+1]; med_int numfam; bool found=false; for(int i=0;i attide=new med_int[natt]; INTERP_KERNEL::AutoPtr attval=new med_int[natt]; @@ -572,15 +643,14 @@ std::vector MEDCoupling::GetMeshGroupsNamesOnFamily(const std::stri std::vector MEDCoupling::GetMeshGroupsNames(const std::string& fileName, const std::string& meshName) { - MEDCoupling::CheckFileForRead(fileName); - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nfam=MEDnFamily(fid,meshName.c_str()); std::vector ret; char nomfam[MED_NAME_SIZE+1]; med_int numfam; for(int i=0;i attide=new med_int[natt]; INTERP_KERNEL::AutoPtr attval=new med_int[natt]; @@ -632,9 +702,8 @@ std::vector MEDCoupling::GetTypesOfField(const std::st std::vector MEDCoupling::GetAllFieldNames(const std::string& fileName) { - MEDCoupling::CheckFileForRead(fileName); std::vector ret; - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields=MEDnField(fid); med_field_type typcha; for(int i=0;i MEDCoupling::GetAllFieldNames(const std::string& fileNa std::vector MEDCoupling::GetAllFieldNamesOnMesh(const std::string& fileName, const std::string& meshName) { - MEDCoupling::CheckFileForRead(fileName); std::vector ret; - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields=MEDnField(fid); // med_field_type typcha; @@ -700,9 +768,8 @@ std::vector MEDCoupling::GetFieldNamesOnMesh(MEDCoupling::TypeOfFie std::vector MEDCoupling::GetCellFieldNamesOnMesh(const std::string& fileName, const std::string& meshName) { - MEDCoupling::CheckFileForRead(fileName); std::vector ret; - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields=MEDnField(fid); // med_field_type typcha; @@ -725,7 +792,7 @@ std::vector MEDCoupling::GetCellFieldNamesOnMesh(const std::string& MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt)); std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1); std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1); - int profilesize,nbi; + med_int profilesize,nbi; if(curMeshName==meshName) { bool found=false; @@ -750,9 +817,8 @@ std::vector MEDCoupling::GetCellFieldNamesOnMesh(const std::string& std::vector MEDCoupling::GetNodeFieldNamesOnMesh(const std::string& fileName, const std::string& meshName) { - MEDCoupling::CheckFileForRead(fileName); std::vector ret; - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields=MEDnField(fid); char pflname[MED_NAME_SIZE+1]=""; char locname[MED_NAME_SIZE+1]=""; @@ -776,7 +842,7 @@ std::vector MEDCoupling::GetNodeFieldNamesOnMesh(const std::string& std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1); if(nbPdt>0) { - int profilesize,nbi; + med_int profilesize,nbi; MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,1,&numdt,&numo,&dt)); med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE, pflname,&profilesize,locname,&nbi)); @@ -791,9 +857,8 @@ std::vector MEDCoupling::GetNodeFieldNamesOnMesh(const std::string& std::vector< std::pair< std::pair, double> > MEDCoupling::GetAllFieldIterations(const std::string& fileName, const std::string& fieldName) { - MEDCoupling::CheckFileForRead(fileName); std::vector< std::pair< std::pair, double > > ret; - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields=MEDnField(fid); // med_field_type typcha; @@ -834,8 +899,7 @@ std::vector< std::pair< std::pair, double> > MEDCoupling::GetAllFieldIt double MEDCoupling::GetTimeAttachedOnFieldIteration(const std::string& fileName, const std::string& fieldName, int iteration, int order) { - MEDCoupling::CheckFileForRead(fileName); - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields=MEDnField(fid); // med_field_type typcha; @@ -896,10 +960,9 @@ std::vector< std::pair > MEDCoupling::GetFieldIterations(MEDCoupling::T std::vector< std::pair > MEDCoupling::GetCellFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName) { - MEDCoupling::CheckFileForRead(fileName); std::string meshNameCpp(meshName); std::vector< std::pair > ret; - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields=MEDnField(fid); // med_field_type typcha; @@ -929,7 +992,7 @@ std::vector< std::pair > MEDCoupling::GetCellFieldIterations(const std: { for(int k=0;k > MEDCoupling::GetCellFieldIterations(const std: std::vector< std::pair > MEDCoupling::GetNodeFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName) { - MEDCoupling::CheckFileForRead(fileName); std::string meshNameCpp(meshName); std::vector< std::pair > ret; - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields=MEDnField(fid); // med_field_type typcha; @@ -998,7 +1060,7 @@ std::vector< std::pair > MEDCoupling::GetNodeFieldIterations(const std: { for(int k=0;kgetGroups(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(MEDFileAnyTypeField1TS::New(fileName,fieldName,iteration,order)); + MCAuto mesh(MEDFileMesh::New(fileName,f->getMeshName())); + { + MCAuto f1(MEDCoupling::DynamicCast(f)); + if(f1.isNotNull()) + { + MCAuto ret(f1->field(mesh)); + return MEDCoupling::DynamicCast(ret); + } + } + { + MCAuto f1(MEDCoupling::DynamicCast(f)); + if(f1.isNotNull()) + { + MCAuto ret(f1->field(mesh)); + return MEDCoupling::DynamicCast(ret); + } + } + { + MCAuto f1(MEDCoupling::DynamicCast(f)); + if(f1.isNotNull()) + { + MCAuto ret(f1->field(mesh)); + return MEDCoupling::DynamicCast(ret); + } + } + throw INTERP_KERNEL::Exception("MEDCoupling::ReadField : only FLOAT32, FLOAT64 and INT32 supported for the moment !"); +} + +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) @@ -1177,7 +1316,7 @@ std::vector MEDCoupling::ReadFieldsOnSame if(!mmuPtr) throw INTERP_KERNEL::Exception("ReadFieldsOnSameMesh : only unstructured mesh is managed !"); MCAuto m=mmuPtr->getMeshAtLevel(meshDimRelToMax); - const DataArrayInt *o2n=mmuPtr->getNumberFieldAtLevel(meshDimRelToMax); + const DataArrayIdType *o2n=mmuPtr->getNumberFieldAtLevel(meshDimRelToMax); MCAuto m2(m->clone(true)); if(o2n) m2->renumberCells(o2n->begin(),true); @@ -1221,97 +1360,139 @@ std::vector MEDCoupling::ReadFieldsGaussN return ReadFieldsOnSameMesh(ON_GAUSS_NE,fileName,meshName,meshDimRelToMax,fieldName,its); } -MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldCell(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) +namespace MEDCoupling { - MCAuto ff(MEDFileField1TS::New(fileName,fieldName,iteration,order)); - MCAuto mm(MEDFileMesh::New(fileName,meshName)); - MCAuto m(mm->getMeshAtLevel(meshDimRelToMax,false)); - MEDFileMesh *mPtr(mm); - MEDFileUMesh *muPtr=dynamic_cast(mPtr); - MCAuto ret(ff->getFieldOnMeshAtLevel(ON_CELLS,m)); - if(muPtr) - { - const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax)); - if(num) - ret->renumberCells(num->begin()); - } - return ret.retn(); -} + template + MCAuto::FieldType> ReadFieldCellLikeT(typename MLFieldTraits::F1TSType *ff, MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) + { + MCAuto mm(MEDFileMesh::New(fileName,meshName)); + MCAuto muPtr(MEDCoupling::DynamicCast(mm)); + MCAuto m(mm->getMeshAtLevel(meshDimRelToMax,false)); + MCAuto::FieldType> ret(ff->getFieldOnMeshAtLevel(type,m)); + if(muPtr.isNotNull()) + { + const DataArrayIdType *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax)); + if(num) + ret->renumberCells(num->begin()); + } + return ret; + } -MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldNode(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) -{ - MCAuto ff(MEDFileField1TS::New(fileName,fieldName,iteration,order)); - MCAuto mm(MEDFileMesh::New(fileName,meshName)); - MCAuto m(mm->getMeshAtLevel(meshDimRelToMax,false)); - MEDFileMesh *mPtr(mm); - MCAuto ret(ff->getFieldOnMeshAtLevel(ON_NODES,m)); - MEDFileUMesh *muPtr=dynamic_cast(mPtr); - if(ff->getPflsReallyUsed().empty()) + MEDCoupling::MEDCouplingField *ReadFieldCellLike(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) + { + MCAuto f(MEDFileAnyTypeField1TS::New(fileName,fieldName,iteration,order)); { - if(muPtr) + MCAuto f1(MEDCoupling::DynamicCast(f)); + if(f1.isNotNull()) { - const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax)); - if(num) - ret->renumberCells(num->begin()); + MCAuto ret(ReadFieldCellLikeT(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order)); + return ret.retn(); + } + } + { + MCAuto f1(MEDCoupling::DynamicCast(f)); + if(f1.isNotNull()) + { + MCAuto ret(ReadFieldCellLikeT(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order)); + return ret.retn(); } } - else { - DataArrayInt *pfl=0,*arr2=0; - MCAuto arr(ff->getFieldWithProfile(ON_NODES,meshDimRelToMax,mm,pfl)); - MCAuto pflSafe(pfl); - MCAuto mp(m->getCellIdsFullyIncludedInNodeIds(pfl->begin(),pfl->end())); - MCAuto mzip(static_cast(m->buildPartAndReduceNodes(mp->begin(),mp->end(),arr2))); - MCAuto arr2Safe(arr2); - MCAuto arr3(arr2->invertArrayO2N2N2O(mzip->getNumberOfNodes())); - MCAuto pflSorted(pflSafe->deepCopy()); pflSorted->sort(true); - if(!arr3->isEqualWithoutConsideringStr(*pflSorted)) - throw INTERP_KERNEL::Exception("ReadFieldNode : not implemented yet !"); - if(!arr3->isEqualWithoutConsideringStr(*pflSafe)) + MCAuto f1(MEDCoupling::DynamicCast(f)); + if(f1.isNotNull()) { - MCAuto o2n2(pflSafe->checkAndPreparePermutation()); - MCAuto n2o2(o2n2->invertArrayO2N2N2O(o2n2->getNumberOfTuples())); - mzip->renumberNodes(n2o2->begin(),n2o2->getNumberOfTuples()); - arr->setName(""); - ret->setArray(arr); + MCAuto ret(ReadFieldCellLikeT(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order)); + return ret.retn(); } - ret->setMesh(mzip); } - return ret.retn(); + throw INTERP_KERNEL::Exception("MEDCoupling::ReadFieldCell : only FLOAT32, FLOAT64 and INT32 supported for the moment !"); + } + + template + MCAuto::FieldType> ReadFieldNodeT(typename MLFieldTraits::F1TSType *ff, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) + { + MCAuto mm(MEDFileMesh::New(fileName,meshName)); + MCAuto m(mm->getMeshAtLevel(meshDimRelToMax,false)); + MCAuto::FieldType> ret(ff->getFieldOnMeshAtLevel(ON_NODES,m)); + MCAuto muPtr(MEDCoupling::DynamicCast(mm)); + if(ff->getPflsReallyUsed().empty()) + { + if(muPtr.isNotNull()) + { + const DataArrayIdType *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax)); + if(num) + ret->renumberCells(num->begin()); + } + } + else + { + DataArrayIdType *pfl(nullptr),*arr2(nullptr); + MCAuto::ArrayType> arr(ff->getFieldWithProfile(ON_NODES,meshDimRelToMax,mm,pfl)); + MCAuto pflSafe(pfl); + MCAuto mp(m->getCellIdsFullyIncludedInNodeIds(pfl->begin(),pfl->end())); + MCAuto mzip(static_cast(m->buildPartAndReduceNodes(mp->begin(),mp->end(),arr2))); + MCAuto arr2Safe(arr2); + MCAuto arr3(arr2->invertArrayO2N2N2O(mzip->getNumberOfNodes())); + MCAuto pflSorted(pflSafe->deepCopy()); pflSorted->sort(true); + if(!arr3->isEqualWithoutConsideringStr(*pflSorted)) + throw INTERP_KERNEL::Exception("ReadFieldNode : not implemented yet !"); + if(!arr3->isEqualWithoutConsideringStr(*pflSafe)) + { + MCAuto o2n2(pflSafe->checkAndPreparePermutation()); + MCAuto n2o2(o2n2->invertArrayO2N2N2O(o2n2->getNumberOfTuples())); + mzip->renumberNodes(n2o2->begin(),n2o2->getNumberOfTuples()); + arr->setName(""); + ret->setArray(arr); + } + ret->setMesh(mzip); + } + return ret; + } } -MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldGauss(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) +MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldCell(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) { - MCAuto ff(MEDFileField1TS::New(fileName,fieldName,iteration,order)); - MCAuto mm(MEDFileMesh::New(fileName,meshName)); - MCAuto m(mm->getMeshAtLevel(meshDimRelToMax,false)); - MEDFileMesh *mPtr(mm); - MEDFileUMesh *muPtr=dynamic_cast(mPtr); - MCAuto ret(ff->getFieldOnMeshAtLevel(ON_GAUSS_PT,m)); - if(muPtr) - { - const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax)); - if(num) - ret->renumberCells(num->begin()); - } - return ret.retn(); + return ReadFieldCellLike(ON_CELLS,fileName,meshName,meshDimRelToMax,fieldName,iteration,order); } -MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldGaussNE(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) +MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldNode(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) { - MCAuto ff(MEDFileField1TS::New(fileName,fieldName,iteration,order)); - MCAuto mm(MEDFileMesh::New(fileName,meshName)); - MCAuto m(mm->getMeshAtLevel(meshDimRelToMax,false)); - MEDFileMesh *mPtr(mm); - MEDFileUMesh *muPtr=dynamic_cast(mPtr); - MCAuto ret(ff->getFieldOnMeshAtLevel(ON_GAUSS_NE,m)); - if(muPtr) - { - const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax)); - if(num) - ret->renumberCells(num->begin()); - } - return ret.retn(); + MCAuto f(MEDFileAnyTypeField1TS::New(fileName,fieldName,iteration,order)); + { + MCAuto f1(MEDCoupling::DynamicCast(f)); + if(f1.isNotNull()) + { + MCAuto ret(ReadFieldNodeT(f1,fileName,meshName,meshDimRelToMax,fieldName,iteration,order)); + return ret.retn(); + } + } + { + MCAuto f1(MEDCoupling::DynamicCast(f)); + if(f1.isNotNull()) + { + MCAuto ret(ReadFieldNodeT(f1,fileName,meshName,meshDimRelToMax,fieldName,iteration,order)); + return ret.retn(); + } + } + { + MCAuto f1(MEDCoupling::DynamicCast(f)); + if(f1.isNotNull()) + { + MCAuto ret(ReadFieldNodeT(f1,fileName,meshName,meshDimRelToMax,fieldName,iteration,order)); + return ret.retn(); + } + } + throw INTERP_KERNEL::Exception("MEDCoupling::ReadFieldNode : only FLOAT32, FLOAT64 and INT32 supported for the moment !"); +} + +MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldGauss(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) +{ + return ReadFieldCellLike(ON_GAUSS_PT,fileName,meshName,meshDimRelToMax,fieldName,iteration,order); +} + +MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldGaussNE(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) +{ + return ReadFieldCellLike(ON_GAUSS_NE,fileName,meshName,meshDimRelToMax,fieldName,iteration,order); } void MEDCoupling::WriteMesh(const std::string& fileName, const MEDCoupling::MEDCouplingMesh *mesh, bool writeFromScratch) @@ -1398,31 +1579,32 @@ void MEDCoupling::WriteUMeshesPartitionDep(const std::string& fileName, const st void MEDCoupling::WriteUMeshes(const std::string& fileName, const std::vector& meshes, bool writeFromScratch) { - int mod=writeFromScratch?2:0; + int mod(writeFromScratch?2:0); MCAuto m(MEDFileUMesh::New()); AssignStaticWritePropertiesTo(*m); m->setMeshes(meshes,true); m->write(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::FieldType> f2(f->deepCopy()); const MEDCouplingMesh *m(f2->getMesh()); const MEDCouplingUMesh *um(dynamic_cast(m)); const MEDCoupling1GTUMesh *um2(dynamic_cast(m)); const MEDCouplingCMesh *um3(dynamic_cast(m)); const MEDCouplingCurveLinearMesh *um4(dynamic_cast(m)); MCAuto mm; - int mod=writeFromScratch?2:0; + int mod(writeFromScratch?2:0); if(um) { MCAuto mmu(MEDFileUMesh::New()); AssignStaticWritePropertiesTo(*mmu); - MCAuto o2n(um->getRenumArrForMEDFileFrmt()); - MCAuto n2o(o2n->invertArrayO2N2N2O(o2n->getNumberOfTuples())); + MCAuto o2n(um->getRenumArrForMEDFileFrmt()); + MCAuto n2o(o2n->invertArrayO2N2N2O(o2n->getNumberOfTuples())); f2->renumberCells(o2n->begin(),false); mmu->setMeshAtLevel(0,const_cast(static_cast(f2->getMesh()))); mmu->setRenumFieldArr(0,n2o); @@ -1458,12 +1640,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 WriteFieldT(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 !"; @@ -1471,44 +1654,47 @@ 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 { - std::vector meshNames=GetMeshNames(fileName); + std::vector meshNames(GetMeshNames(fileName)); if(!f->getMesh()) 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())); AssignStaticWritePropertiesTo(*mm); const MEDFileMesh *mmPtr(mm); - const MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); + 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< typename MEDCoupling::Traits::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()); + MCAuto mread(mmuPtr->getMeshAtLevel(m->getMeshDimension()-mm->getMeshDimension())); if(f2->getTypeOfField()!=ON_NODES) { - m->tryToShareSameCoordsPermute(*mread,_EPS_FOR_NODE_COMP); - DataArrayInt *part=0; - bool b=mread->areCellsIncludedIn(m,_COMP_FOR_CELL,part); - MCAuto partSafe(part); + if(!m->getCoords()->isEqualWithoutConsideringStr(*mread->getCoords(),_EPS_FOR_NODE_COMP)) + m->tryToShareSameCoordsPermute(*mread,_EPS_FOR_NODE_COMP); + else + mread->setCoords(m->getCoords()); + DataArrayIdType *part(NULL); + 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); @@ -1522,15 +1708,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); - MCAuto partSafe(part); + DataArrayIdType *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); @@ -1545,29 +1731,61 @@ void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MED } } -void MEDCoupling::WriteFieldDep(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch) +void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MEDCouplingField *f, bool writeFromScratch) +{ + if(!f) + throw INTERP_KERNEL::Exception("WriteField : input field is null !"); + { + const MEDCoupling::MEDCouplingFieldDouble *f1(dynamic_cast(f)); + if(f1) + { + WriteFieldT(fileName,f1,writeFromScratch); + return ; + } + } + { + const MEDCoupling::MEDCouplingFieldInt32 *f1(dynamic_cast(f)); + if(f1) + { + WriteFieldT(fileName,f1,writeFromScratch); + return ; + } + } + { + const MEDCoupling::MEDCouplingFieldFloat *f1(dynamic_cast(f)); + if(f1) + { + WriteFieldT(fileName,f1,writeFromScratch); + return ; + } + } + throw INTERP_KERNEL::Exception("WriteField : input field is not in FLOAT32, FLOAT64, INT32 !"); +} + +void MEDCoupling::WriteFieldDep(const std::string& fileName, const MEDCoupling::MEDCouplingField *f, bool writeFromScratch) { WriteField(fileName,f,writeFromScratch); } -void MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f) +template +void WriteFieldUsingAlreadyWrittenMeshT(const std::string& fileName, const typename MEDCoupling::Traits::FieldType *f) { if(!f) - throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMesh : input field is null !"); + throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMeshT : input field is null !"); f->checkConsistencyLight(); - int status=MEDLoaderBase::getStatusOfFile(fileName); + int status(MEDLoaderBase::getStatusOfFile(fileName)); if(status!=MEDLoaderBase::EXIST_RW) { std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions or not exists !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } - MCAuto f1ts(MEDFileField1TS::New()); + MCAuto< typename MLFieldTraits::F1TSType > f1ts(MLFieldTraits::F1TSType::New()); AssignStaticWritePropertiesTo(*f1ts); MEDCouplingUMesh *m(dynamic_cast(const_cast(f->getMesh()))); if(m) { - MCAuto o2n(m->getRenumArrForMEDFileFrmt()); - MCAuto f2(f->deepCopy()); + MCAuto o2n(m->getRenumArrForMEDFileFrmt()); + MCAuto< typename MEDCoupling::Traits::FieldType > f2(f->deepCopy()); f2->renumberCells(o2n->begin(),false); f1ts->setFieldNoProfileSBT(f2); } @@ -1575,3 +1793,28 @@ void MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, f1ts->setFieldNoProfileSBT(f); f1ts->write(fileName,0); } + +void MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingField *f) +{ + if(!f) + throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMesh : input field is null !"); + { + const MEDCoupling::MEDCouplingFieldDouble *f1(dynamic_cast(f)); + if(f1) + WriteFieldUsingAlreadyWrittenMeshT(fileName,f1); + return ; + } + { + const MEDCoupling::MEDCouplingFieldInt32 *f1(dynamic_cast(f)); + if(f1) + WriteFieldUsingAlreadyWrittenMeshT(fileName,f1); + return ; + } + { + const MEDCoupling::MEDCouplingFieldFloat *f1(dynamic_cast(f)); + if(f1) + WriteFieldUsingAlreadyWrittenMeshT(fileName,f1); + return ; + } + throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMesh : input field is not in FLOAT32, FLOAT64, INT32 !"); +}