X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FMEDLoader%2FMEDLoader.cxx;h=8b1ffc7c63e135d9d86c4c8430190cd36070d1ce;hb=844b1a4785909ad42206965838a6d0f826dfb5c1;hp=f8211f58bc979d472f1b408f8c368f946e562868;hpb=c8d33262ed6d2041da8512b02f8c8b4ffadf733d;p=tools%2Fmedcoupling.git diff --git a/src/MEDLoader/MEDLoader.cxx b/src/MEDLoader/MEDLoader.cxx index f8211f58b..8b1ffc7c6 100644 --- a/src/MEDLoader/MEDLoader.cxx +++ b/src/MEDLoader/MEDLoader.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2015 CEA/DEN, EDF R&D +// Copyright (C) 2007-2019 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,8 +29,11 @@ #include "MEDCouplingUMesh.hxx" #include "MEDCouplingMemArray.hxx" #include "MEDCouplingFieldDouble.hxx" +#include "MEDCouplingFieldFloat.hxx" +#include "MEDCouplingFieldInt.hxx" #include "MEDCouplingGaussLocalization.hxx" -#include "MEDCouplingAutoRefCountObjectPtr.hxx" +#include "MEDCouplingTraits.hxx" +#include "MCAuto.hxx" #include "InterpKernelAutoPtr.hxx" @@ -44,14 +48,6 @@ #include #include -/*! \class MEDLoader - * - * \brief Static class offering the "basic" API to read and write MED files/ - * - * This class implements only static methods and offers the high level API to access MED files. - * Take a look at \ref medloader for more details. - * - */ med_geometry_type typmai[MED_N_CELL_FIXED_GEO] = { MED_POINT1, MED_SEG2, @@ -71,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, @@ -97,6 +94,7 @@ 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, @@ -131,7 +129,7 @@ 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 @@ -147,20 +145,6 @@ int _TOO_LONG_STR=0; using namespace MEDCoupling; -/// @cond INTERNAL - -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); - med_int getIdFromMeshName(med_idt fid, const std::string& meshName, std::string& trueMeshName); - std::vector getMeshNamesFid(med_idt fid); -} - -/// @endcond - - /// @cond INTERNAL /*! @@ -171,7 +155,7 @@ namespace MEDLoaderNS int MEDLoaderNS::readUMeshDimFromFile(const std::string& fileName, const std::string& meshName, std::vector& 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]; @@ -310,18 +294,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=majj; + minor=minn; + release=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) { @@ -355,8 +380,7 @@ void MEDCoupling::SetTooLongStrPolicy(int val) */ std::vector< std::vector< std::pair > > MEDCoupling::GetUMeshGlobalInfo(const std::string& fileName, const std::string& meshName, int &meshDim, int& spaceDim, int& 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]; @@ -418,16 +442,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; @@ -462,10 +483,9 @@ std::vector< std::pair > MEDCoupling::GetComponentsName 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; @@ -491,8 +511,7 @@ 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]; @@ -515,8 +534,7 @@ 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]; @@ -543,9 +561,8 @@ 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; @@ -580,8 +597,7 @@ 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]; @@ -608,7 +624,7 @@ std::vector MEDCoupling::GetMeshGroupsNames(const std::string& file std::vector MEDCoupling::GetTypesOfField(const std::string& fileName, const std::string& meshName, const std::string& fieldName) { std::vector ret; - MEDCouplingAutoRefCountObjectPtr fs(MEDFileAnyTypeFieldMultiTS::New(fileName,fieldName,false)); + MCAuto fs(MEDFileAnyTypeFieldMultiTS::New(fileName,fieldName,false)); if(fs->getMeshName()!=meshName) { std::ostringstream oss; oss << "GetTypesOfField : The field \"" << fieldName << "\" in file \"" << fileName << "\" is not lying on mesh \"" << meshName << "\""; @@ -620,7 +636,7 @@ std::vector MEDCoupling::GetTypesOfField(const std::st return ret; for(int i=0;i f1ts(fs->getTimeStepAtPos(i)); + MCAuto f1ts(fs->getTimeStepAtPos(i)); std::vector tof(f1ts->getTypesOfFieldAvailable()); for(std::vector::const_iterator it=tof.begin();it!=tof.end();it++) if(std::find(ret.begin(),ret.end(),*it)==ret.end()) @@ -640,9 +656,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; @@ -708,9 +722,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; @@ -758,9 +771,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]=""; @@ -799,9 +811,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; @@ -842,8 +853,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; @@ -904,10 +914,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; @@ -976,10 +985,9 @@ std::vector< std::pair > 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; @@ -1042,7 +1050,7 @@ std::vector< std::pair > MEDCoupling::GetNodeFieldIterations(const std: MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax) { MEDCoupling::CheckFileForRead(fileName); - MEDCouplingAutoRefCountObjectPtr mm(MEDFileMesh::New(fileName,meshName)); + MCAuto mm(MEDFileMesh::New(fileName,meshName)); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(mmuPtr) @@ -1066,7 +1074,7 @@ MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& f MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& fileName, int meshDimRelToMax) { MEDCoupling::CheckFileForRead(fileName); - MEDCouplingAutoRefCountObjectPtr mm(MEDFileMesh::New(fileName)); + MCAuto mm(MEDFileMesh::New(fileName)); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(mmuPtr) @@ -1090,7 +1098,7 @@ MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& f MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax) { MEDCoupling::CheckFileForRead(fileName); - MEDCouplingAutoRefCountObjectPtr mm(MEDFileMesh::New(fileName,meshName)); + MCAuto mm(MEDFileMesh::New(fileName,meshName)); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(!mmuPtr) @@ -1104,7 +1112,7 @@ MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFile(const std::string& MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFile(const std::string& fileName, int meshDimRelToMax) { MEDCoupling::CheckFileForRead(fileName); - MEDCouplingAutoRefCountObjectPtr mm(MEDFileMesh::New(fileName)); + MCAuto mm(MEDFileMesh::New(fileName)); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(!mmuPtr) @@ -1125,7 +1133,7 @@ int MEDCoupling::ReadUMeshDimFromFile(const std::string& fileName, const std::st MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFamilies(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::vector& fams) { MEDCoupling::CheckFileForRead(fileName); - MEDCouplingAutoRefCountObjectPtr mm(MEDFileMesh::New(fileName,meshName)); + MCAuto mm(MEDFileMesh::New(fileName,meshName)); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(!mmuPtr) @@ -1139,7 +1147,7 @@ MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFamilies(const std::str MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromGroups(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::vector& grps) { MEDCoupling::CheckFileForRead(fileName); - MEDCouplingAutoRefCountObjectPtr mm=MEDFileMesh::New(fileName,meshName); + MCAuto mm=MEDFileMesh::New(fileName,meshName); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(!mmuPtr) @@ -1150,7 +1158,84 @@ 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(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) @@ -1175,25 +1260,25 @@ std::vector MEDCoupling::ReadFieldsOnSame return std::vector(); MEDCoupling::CheckFileForRead(fileName); std::vector ret(its.size()); - std::vector< MEDCouplingAutoRefCountObjectPtr > retSafe(its.size()); + std::vector< MCAuto > retSafe(its.size()); if(its.empty()) return ret; //Retrieving mesh of rank 0 and field on rank 0 too. - MEDCouplingAutoRefCountObjectPtr mm=MEDFileMesh::New(fileName,meshName); + MCAuto mm=MEDFileMesh::New(fileName,meshName); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(!mmuPtr) throw INTERP_KERNEL::Exception("ReadFieldsOnSameMesh : only unstructured mesh is managed !"); - MEDCouplingAutoRefCountObjectPtr m=mmuPtr->getMeshAtLevel(meshDimRelToMax); + MCAuto m=mmuPtr->getMeshAtLevel(meshDimRelToMax); const DataArrayInt *o2n=mmuPtr->getNumberFieldAtLevel(meshDimRelToMax); - MEDCouplingAutoRefCountObjectPtr m2(m->clone(true)); + MCAuto m2(m->clone(true)); if(o2n) m2->renumberCells(o2n->begin(),true); int i=0; for(std::vector >::const_iterator it=its.begin();it!=its.end();it++,i++) { - MEDCouplingAutoRefCountObjectPtr ff=MEDFileField1TS::New(fileName,fieldName,(*it).first,(*it).second); - MEDCouplingAutoRefCountObjectPtr retElt=ff->getFieldOnMeshAtLevel(type,m); + MCAuto ff=MEDFileField1TS::New(fileName,fieldName,(*it).first,(*it).second); + MCAuto retElt=ff->getFieldOnMeshAtLevel(type,m); if(o2n) retElt->renumberCells(o2n->begin(),true); retElt->setMesh(m2); @@ -1229,97 +1314,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 { - MEDCouplingAutoRefCountObjectPtr ff(MEDFileField1TS::New(fileName,fieldName,iteration,order)); - MEDCouplingAutoRefCountObjectPtr mm(MEDFileMesh::New(fileName,meshName)); - MEDCouplingAutoRefCountObjectPtr m(mm->getMeshAtLevel(meshDimRelToMax,false)); - MEDFileMesh *mPtr(mm); - MEDFileUMesh *muPtr=dynamic_cast(mPtr); - MEDCouplingAutoRefCountObjectPtr ret(ff->getFieldOnMeshAtLevel(ON_CELLS,m)); - if(muPtr) + 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 DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax)); + if(num) + ret->renumberCells(num->begin()); + } + return ret; + } + + 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)); { - const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax)); - if(num) - ret->renumberCells(num->begin()); + MCAuto f1(MEDCoupling::DynamicCast(f)); + if(f1.isNotNull()) + { + MCAuto ret(ReadFieldCellLikeT(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order)); + return ret.retn(); + } } - return ret.retn(); -} - -MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldNode(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order) -{ - MEDCouplingAutoRefCountObjectPtr ff(MEDFileField1TS::New(fileName,fieldName,iteration,order)); - MEDCouplingAutoRefCountObjectPtr mm(MEDFileMesh::New(fileName,meshName)); - MEDCouplingAutoRefCountObjectPtr m(mm->getMeshAtLevel(meshDimRelToMax,false)); - MEDFileMesh *mPtr(mm); - MEDCouplingAutoRefCountObjectPtr ret(ff->getFieldOnMeshAtLevel(ON_NODES,m)); - MEDFileUMesh *muPtr=dynamic_cast(mPtr); - if(ff->getPflsReallyUsed().empty()) { - 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(); } } - else { - DataArrayInt *pfl=0,*arr2=0; - MEDCouplingAutoRefCountObjectPtr arr(ff->getFieldWithProfile(ON_NODES,meshDimRelToMax,mm,pfl)); - MEDCouplingAutoRefCountObjectPtr pflSafe(pfl); - MEDCouplingAutoRefCountObjectPtr mp(m->getCellIdsFullyIncludedInNodeIds(pfl->begin(),pfl->end())); - MEDCouplingAutoRefCountObjectPtr mzip(static_cast(m->buildPartAndReduceNodes(mp->begin(),mp->end(),arr2))); - MEDCouplingAutoRefCountObjectPtr arr2Safe(arr2); - MEDCouplingAutoRefCountObjectPtr arr3(arr2->invertArrayO2N2N2O(mzip->getNumberOfNodes())); - MEDCouplingAutoRefCountObjectPtr pflSorted(pflSafe->deepCpy()); 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()) { - MEDCouplingAutoRefCountObjectPtr o2n2(pflSafe->checkAndPreparePermutation()); - MEDCouplingAutoRefCountObjectPtr 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 DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax)); + if(num) + ret->renumberCells(num->begin()); + } + } + else + { + DataArrayInt *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) { - MEDCouplingAutoRefCountObjectPtr ff(MEDFileField1TS::New(fileName,fieldName,iteration,order)); - MEDCouplingAutoRefCountObjectPtr mm(MEDFileMesh::New(fileName,meshName)); - MEDCouplingAutoRefCountObjectPtr m(mm->getMeshAtLevel(meshDimRelToMax,false)); - MEDFileMesh *mPtr(mm); - MEDFileUMesh *muPtr=dynamic_cast(mPtr); - MEDCouplingAutoRefCountObjectPtr 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) { - MEDCouplingAutoRefCountObjectPtr ff(MEDFileField1TS::New(fileName,fieldName,iteration,order)); - MEDCouplingAutoRefCountObjectPtr mm(MEDFileMesh::New(fileName,meshName)); - MEDCouplingAutoRefCountObjectPtr m(mm->getMeshAtLevel(meshDimRelToMax,false)); - MEDFileMesh *mPtr(mm); - MEDFileUMesh *muPtr=dynamic_cast(mPtr); - MEDCouplingAutoRefCountObjectPtr 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) @@ -1336,7 +1463,7 @@ void MEDCoupling::WriteMesh(const std::string& fileName, const MEDCoupling::MEDC const MEDCoupling1GTUMesh *um2(dynamic_cast(mesh)); if(um2) { - MEDCouplingAutoRefCountObjectPtr mmu(MEDFileUMesh::New()); + MCAuto mmu(MEDFileUMesh::New()); AssignStaticWritePropertiesTo(*mmu); mmu->setMeshAtLevel(0,const_cast(um2)); mmu->write(fileName,mod); @@ -1345,7 +1472,7 @@ void MEDCoupling::WriteMesh(const std::string& fileName, const MEDCoupling::MEDC const MEDCouplingCMesh *um3(dynamic_cast(mesh)); if(um3) { - MEDCouplingAutoRefCountObjectPtr mmc(MEDFileCMesh::New()); + MCAuto mmc(MEDFileCMesh::New()); AssignStaticWritePropertiesTo(*mmc); mmc->setMesh(const_cast(um3)); mmc->write(fileName,mod); @@ -1354,7 +1481,7 @@ void MEDCoupling::WriteMesh(const std::string& fileName, const MEDCoupling::MEDC const MEDCouplingCurveLinearMesh *um4(dynamic_cast(mesh)); if(um4) { - MEDCouplingAutoRefCountObjectPtr mmc(MEDFileCurveLinearMesh::New()); + MCAuto mmc(MEDFileCurveLinearMesh::New()); AssignStaticWritePropertiesTo(*mmc); mmc->setMesh(const_cast(um4)); mmc->write(fileName,mod); @@ -1368,9 +1495,9 @@ void MEDCoupling::WriteUMesh(const std::string& fileName, const MEDCoupling::MED if(!mesh) throw INTERP_KERNEL::Exception("WriteUMesh : input mesh is null !"); int mod=writeFromScratch?2:0; - MEDCouplingAutoRefCountObjectPtr m(MEDFileUMesh::New()); + MCAuto m(MEDFileUMesh::New()); AssignStaticWritePropertiesTo(*m); - MEDCouplingAutoRefCountObjectPtr mcpy(static_cast(mesh->deepCpy())); + MCAuto mcpy(static_cast(mesh->deepCopy())); m->setMeshAtLevel(0,mcpy,true); m->write(fileName,mod); } @@ -1391,7 +1518,7 @@ void MEDCoupling::WriteUMeshesPartition(const std::string& fileName, const std:: std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } - MEDCouplingAutoRefCountObjectPtr m(MEDFileUMesh::New()); + MCAuto m(MEDFileUMesh::New()); AssignStaticWritePropertiesTo(*m); m->setGroupsFromScratch(0,meshes,true); m->setName(meshNameC); @@ -1406,31 +1533,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; - MEDCouplingAutoRefCountObjectPtr m(MEDFileUMesh::New()); + 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) { - MEDCouplingAutoRefCountObjectPtr ff(MEDFileField1TS::New()); + MCAuto< typename MLFieldTraits::F1TSType > ff(MLFieldTraits::F1TSType::New()); AssignStaticWritePropertiesTo(*ff); - MEDCouplingAutoRefCountObjectPtr f2(f->deepCpy()); + 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)); - MEDCouplingAutoRefCountObjectPtr mm; - int mod=writeFromScratch?2:0; + MCAuto mm; + int mod(writeFromScratch?2:0); if(um) { - MEDCouplingAutoRefCountObjectPtr mmu(MEDFileUMesh::New()); + MCAuto mmu(MEDFileUMesh::New()); AssignStaticWritePropertiesTo(*mmu); - MEDCouplingAutoRefCountObjectPtr o2n(um->getRenumArrForMEDFileFrmt()); - MEDCouplingAutoRefCountObjectPtr 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); @@ -1439,7 +1567,7 @@ void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const std::stri } else if(um2) { - MEDCouplingAutoRefCountObjectPtr mmu(MEDFileUMesh::New()); + MCAuto mmu(MEDFileUMesh::New()); AssignStaticWritePropertiesTo(*mmu); mmu->setMeshAtLevel(0,const_cast(um2)); ff->setFieldNoProfileSBT(f2); @@ -1447,7 +1575,7 @@ void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const std::stri } else if(um3) { - MEDCouplingAutoRefCountObjectPtr mmc(MEDFileCMesh::New()); + MCAuto mmc(MEDFileCMesh::New()); AssignStaticWritePropertiesTo(*mmc); mmc->setMesh(const_cast(um3)); ff->setFieldNoProfileSBT(f2); @@ -1455,7 +1583,7 @@ void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const std::stri } else if(um4) { - MEDCouplingAutoRefCountObjectPtr mmc(MEDFileCurveLinearMesh::New()); + MCAuto mmc(MEDFileCurveLinearMesh::New()); AssignStaticWritePropertiesTo(*mmc); mmc->setMesh(const_cast(um4)); ff->setFieldNoProfileSBT(f2); @@ -1466,12 +1594,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->checkCoherency(); - int status=MEDLoaderBase::getStatusOfFile(fileName); + f->checkConsistencyLight(); + 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 !"; @@ -1479,46 +1608,49 @@ 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 { - MEDCouplingAutoRefCountObjectPtr mm(MEDFileMesh::New(fileName,f->getMesh()->getName().c_str())); + 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 !"); - MEDCouplingAutoRefCountObjectPtr f2(f->deepCpy()); - 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 !"); - MEDCouplingAutoRefCountObjectPtr o2n=m->getRenumArrForMEDFileFrmt(); + MCAuto o2n(m->getRenumArrForMEDFileFrmt()); f2->renumberCells(o2n->begin(),false); m=static_cast(const_cast(f2->getMesh())); - MEDCouplingAutoRefCountObjectPtr 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); - MEDCouplingAutoRefCountObjectPtr partSafe(part); + if(!m->getCoords()->isEqualWithoutConsideringStr(*mread->getCoords(),_EPS_FOR_NODE_COMP)) + m->tryToShareSameCoordsPermute(*mread,_EPS_FOR_NODE_COMP); + else + mread->setCoords(m->getCoords()); + DataArrayInt *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()); } - MEDCouplingAutoRefCountObjectPtr f1ts(MEDFileField1TS::New()); + MCAuto< typename MLFieldTraits::F1TSType > f1ts(MLFieldTraits::F1TSType::New()); AssignStaticWritePropertiesTo(*f1ts); - if(part->isIdentity2(mread->getNumberOfCells())) + if(part->isIota(mread->getNumberOfCells())) f1ts->setFieldNoProfileSBT(f2); else { @@ -1530,17 +1662,17 @@ 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); - MEDCouplingAutoRefCountObjectPtr partSafe(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()); } - MEDCouplingAutoRefCountObjectPtr f1ts(MEDFileField1TS::New()); + MCAuto< typename MLFieldTraits::F1TSType > f1ts(MLFieldTraits::F1TSType::New()); AssignStaticWritePropertiesTo(*f1ts); - if(part->isIdentity2(mread->getNumberOfNodes())) + if(part->isIota(mread->getNumberOfNodes())) f1ts->setFieldNoProfileSBT(f2); else { @@ -1553,29 +1685,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::MEDCouplingFieldInt *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 !"); - f->checkCoherency(); - int status=MEDLoaderBase::getStatusOfFile(fileName); + throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMeshT : input field is null !"); + f->checkConsistencyLight(); + 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()); } - MEDCouplingAutoRefCountObjectPtr f1ts(MEDFileField1TS::New()); + MCAuto< typename MLFieldTraits::F1TSType > f1ts(MLFieldTraits::F1TSType::New()); AssignStaticWritePropertiesTo(*f1ts); MEDCouplingUMesh *m(dynamic_cast(const_cast(f->getMesh()))); if(m) { - MEDCouplingAutoRefCountObjectPtr o2n(m->getRenumArrForMEDFileFrmt()); - MEDCouplingAutoRefCountObjectPtr f2(f->deepCpy()); + MCAuto o2n(m->getRenumArrForMEDFileFrmt()); + MCAuto< typename MEDCoupling::Traits::FieldType > f2(f->deepCopy()); f2->renumberCells(o2n->begin(),false); f1ts->setFieldNoProfileSBT(f2); } @@ -1583,3 +1747,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::MEDCouplingFieldInt *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 !"); +}