X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FMEDLoader%2FMEDLoader.cxx;h=687d0bcbf35a6d6c4b3b25c6b29c96bf83af4a3b;hb=f2ab61e92f55ee52cf5196f7904f625d93c8c2d0;hp=f14e17a152833858b9415a9582f24c854815dd3f;hpb=a63942887e60f45ea6fac6a4fa04954ced93c3c8;p=tools%2Fmedcoupling.git diff --git a/src/MEDLoader/MEDLoader.cxx b/src/MEDLoader/MEDLoader.cxx index f14e17a15..687d0bcbf 100644 --- a/src/MEDLoader/MEDLoader.cxx +++ b/src/MEDLoader/MEDLoader.cxx @@ -1,9 +1,9 @@ -// Copyright (C) 2007-2013 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 // License as published by the Free Software Foundation; either -// version 2.1 of the License. +// version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -21,14 +21,19 @@ #include "MEDLoader.hxx" #include "MEDLoaderBase.hxx" #include "MEDFileUtilities.hxx" +#include "MEDLoaderNS.hxx" +#include "MEDFileSafeCaller.txx" #include "MEDFileMesh.hxx" #include "MEDFileField.hxx" #include "CellModel.hxx" #include "MEDCouplingUMesh.hxx" #include "MEDCouplingMemArray.hxx" #include "MEDCouplingFieldDouble.hxx" +#include "MEDCouplingFieldFloat.hxx" +#include "MEDCouplingFieldInt32.hxx" #include "MEDCouplingGaussLocalization.hxx" -#include "MEDCouplingAutoRefCountObjectPtr.hxx" +#include "MEDCouplingTraits.hxx" +#include "MCAuto.hxx" #include "InterpKernelAutoPtr.hxx" @@ -42,126 +47,128 @@ #include #include #include +#include med_geometry_type typmai[MED_N_CELL_FIXED_GEO] = { MED_POINT1, - MED_SEG2, - MED_SEG3, - MED_SEG4, - MED_TRIA3, - MED_QUAD4, - MED_TRIA6, - MED_TRIA7, - MED_QUAD8, - MED_QUAD9, - MED_TETRA4, - MED_PYRA5, - MED_PENTA6, - MED_HEXA8, - MED_OCTA12, - MED_TETRA10, - MED_PYRA13, - MED_PENTA15, - MED_HEXA20, - MED_HEXA27, - MED_POLYGON, - MED_POLYGON2, - MED_POLYHEDRON }; + MED_SEG2, + MED_SEG3, + MED_SEG4, + MED_TRIA3, + MED_QUAD4, + MED_TRIA6, + MED_TRIA7, + MED_QUAD8, + MED_QUAD9, + MED_TETRA4, + MED_PYRA5, + MED_PENTA6, + MED_HEXA8, + MED_OCTA12, + MED_TETRA10, + MED_PYRA13, + MED_PENTA15, + MED_PENTA18, + MED_HEXA20, + MED_HEXA27, + MED_POLYGON, + MED_POLYGON2, + MED_POLYHEDRON }; med_geometry_type typmainoeud[1] = { MED_NONE }; INTERP_KERNEL::NormalizedCellType typmai2[MED_N_CELL_FIXED_GEO] = { INTERP_KERNEL::NORM_POINT1, - INTERP_KERNEL::NORM_SEG2, - INTERP_KERNEL::NORM_SEG3, - INTERP_KERNEL::NORM_SEG4, - INTERP_KERNEL::NORM_TRI3, - INTERP_KERNEL::NORM_QUAD4, - INTERP_KERNEL::NORM_TRI6, - INTERP_KERNEL::NORM_TRI7, - INTERP_KERNEL::NORM_QUAD8, - INTERP_KERNEL::NORM_QUAD9, - INTERP_KERNEL::NORM_TETRA4, - INTERP_KERNEL::NORM_PYRA5, - INTERP_KERNEL::NORM_PENTA6, - INTERP_KERNEL::NORM_HEXA8, - INTERP_KERNEL::NORM_HEXGP12, - INTERP_KERNEL::NORM_TETRA10, - INTERP_KERNEL::NORM_PYRA13, - INTERP_KERNEL::NORM_PENTA15, - 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_SEG2,//1 - MED_SEG3,//2 - MED_TRIA3,//3 - MED_QUAD4,//4 - MED_POLYGON,//5 - MED_TRIA6,//6 - MED_TRIA7,//7 - MED_QUAD8,//8 - MED_QUAD9,//9 - MED_SEG4,//10 - MED_NONE,//11 - MED_NONE,//12 - MED_NONE,//13 - MED_TETRA4,//14 - MED_PYRA5,//15 - MED_PENTA6,//16 - MED_NONE,//17 - MED_HEXA8,//18 - MED_NONE,//19 - MED_TETRA10,//20 - MED_NONE,//21 - MED_OCTA12,//22 - MED_PYRA13,//23 - MED_NONE,//24 - MED_PENTA15,//25 - MED_NONE,//26 - MED_HEXA27,//27 - MED_NONE,//28 - MED_NONE,//29 - MED_HEXA20,//30 - MED_POLYHEDRON,//31 - MED_POLYGON2,//32 - MED_NONE//33 + INTERP_KERNEL::NORM_SEG2, + INTERP_KERNEL::NORM_SEG3, + INTERP_KERNEL::NORM_SEG4, + INTERP_KERNEL::NORM_TRI3, + INTERP_KERNEL::NORM_QUAD4, + INTERP_KERNEL::NORM_TRI6, + INTERP_KERNEL::NORM_TRI7, + INTERP_KERNEL::NORM_QUAD8, + INTERP_KERNEL::NORM_QUAD9, + INTERP_KERNEL::NORM_TETRA4, + INTERP_KERNEL::NORM_PYRA5, + INTERP_KERNEL::NORM_PENTA6, + INTERP_KERNEL::NORM_HEXA8, + INTERP_KERNEL::NORM_HEXGP12, + 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[INTERP_KERNEL::NORM_MAXTYPE] = { MED_POINT1,//0 + MED_SEG2,//1 + MED_SEG3,//2 + MED_TRIA3,//3 + MED_QUAD4,//4 + MED_POLYGON,//5 + MED_TRIA6,//6 + MED_TRIA7,//7 + MED_QUAD8,//8 + MED_QUAD9,//9 + MED_SEG4,//10 + MED_NONE,//11 + MED_NONE,//12 + MED_NONE,//13 + MED_TETRA4,//14 + MED_PYRA5,//15 + MED_PENTA6,//16 + MED_NONE,//17 + MED_HEXA8,//18 + MED_NONE,//19 + MED_TETRA10,//20 + MED_NONE,//21 + MED_OCTA12,//22 + MED_PYRA13,//23 + MED_NONE,//24 + MED_PENTA15,//25 + MED_NONE,//26 + MED_HEXA27,//27 + MED_PENTA18,//28 + MED_NONE,//29 + MED_HEXA20,//30 + MED_POLYHEDRON,//31 + MED_POLYGON2//32 }; -double MEDLoader::_EPS_FOR_NODE_COMP=1.e-12; +double _EPS_FOR_NODE_COMP=1.e-12; -int MEDLoader::_COMP_FOR_CELL=0; +int _COMP_FOR_CELL=0; -int MEDLoader::_TOO_LONG_STR=0; +int _TOO_LONG_STR=0; -using namespace ParaMEDMEM; +using namespace MEDCoupling; /// @cond INTERNAL -namespace MEDLoaderNS +INTERP_KERNEL::NormalizedCellType ConvertGeometryType(med_geometry_type geotype) { - int readUMeshDimFromFile(const char *fileName, const char *meshName, std::vector& possibilities); - void dispatchElems(int nbOfElemCell, int nbOfElemFace, int& nbOfElem, med_entity_type& whichEntity); - void writeFieldWithoutReadingAndMappingOfMeshInFile(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, bool writeFromScratch); - med_int getIdFromMeshName(med_idt fid, const char *meshName, std::string& trueMeshName) throw(INTERP_KERNEL::Exception); - std::vector getMeshNamesFid(med_idt fid); + INTERP_KERNEL::NormalizedCellType result=INTERP_KERNEL::NORM_ERROR; + for(int i=0; i& possibilities) +int MEDLoaderNS::readUMeshDimFromFile(const std::string& fileName, const std::string& meshName, std::vector& possibilities) { possibilities.clear(); - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); int ret; std::set poss; char nommaa[MED_NAME_SIZE+1]; @@ -169,15 +176,15 @@ int MEDLoaderNS::readUMeshDimFromFile(const char *fileName, const char *meshName 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); - MEDmeshInfo(fid,meshId,nommaa,&Sdim,&Mdim,&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)); // limitation if(nstep!=1) { @@ -185,15 +192,15 @@ int MEDLoaderNS::readUMeshDimFromFile(const char *fileName, const char *meshName } med_int numdt,numit; med_float dt; - MEDmeshComputationStepInfo(fid,nommaa,1,&numdt,&numit,&dt); + MEDFILESAFECALLERRD0(MEDmeshComputationStepInfo,(fid,nommaa,1,&numdt,&numit,&dt)); // endlimitation for(int i=0;i0) @@ -214,9 +221,9 @@ int MEDLoaderNS::readUMeshDimFromFile(const char *fileName, const char *meshName return ret; } -med_int MEDLoaderNS::getIdFromMeshName(med_idt fid, const char *meshName, std::string& trueMeshName) throw(INTERP_KERNEL::Exception) +int MEDLoaderNS::getIdFromMeshName(med_idt fid, const std::string& meshName, std::string& trueMeshName) { - if(meshName==0) + if(meshName.empty()) { std::vector meshes=getMeshNamesFid(fid); if(meshes.empty()) @@ -237,7 +244,7 @@ med_int MEDLoaderNS::getIdFromMeshName(med_idt fid, const char *meshName, std::s 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) @@ -254,11 +261,11 @@ 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; - MEDmeshInfo(fid,i+1,nommaa,&space_dim,&mesh_dim,&type_maillage,maillage_description,dtunit,&stype,&nstep,&axistype,axisname,axisunit); + 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; } @@ -268,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) { @@ -284,7 +291,12 @@ void MEDLoaderNS::dispatchElems(int nbOfElemCell, int nbOfElemFace, int& nbOfEle /// @endcond -bool MEDLoader::HasXDR() +void MEDCoupling::AssignStaticWritePropertiesTo(MEDCoupling::MEDFileWritable& obj) +{ + obj.setTooLongStrPolicy(_TOO_LONG_STR); +} + +bool MEDCoupling::HasXDR() { #ifdef HAS_XDR return true; @@ -293,22 +305,63 @@ bool MEDLoader::HasXDR() #endif } -std::string MEDLoader::MEDFileVersionStr() +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 MEDLoader::MEDFileVersion(int& major, int& minor, int& release) +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 MEDLoader::SetEpsilonForNodeComp(double val) +void MEDCoupling::SetEpsilonForNodeComp(double val) { _EPS_FOR_NODE_COMP=val; } @@ -316,7 +369,7 @@ void MEDLoader::SetEpsilonForNodeComp(double val) /*! * This method sets the policy comparison when trying to fit the already written mesh on a field. The semantic of the policy is specified in MEDCouplingUMesh::zipConnectivityTraducer. */ -void MEDLoader::SetCompPolicyForCell(int val) +void MEDCoupling::SetCompPolicyForCell(int val) { _COMP_FOR_CELL=val; } @@ -325,7 +378,7 @@ void MEDLoader::SetCompPolicyForCell(int val) * This method set the behaviour of MEDLoader when a too long string is seen in datastructure before copy it in MED file. * By default (0) an exception is thrown. If equal to 1 a warning is emitted in std_err but no exception is thrown. */ -void MEDLoader::SetTooLongStrPolicy(int val) +void MEDCoupling::SetTooLongStrPolicy(int val) { _TOO_LONG_STR=val; } @@ -338,36 +391,37 @@ void MEDLoader::SetTooLongStrPolicy(int val) * - the space dimension * - the number of nodes */ -std::vector< std::vector< std::pair > > MEDLoader::GetUMeshGlobalInfo(const char *fileName, const char *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,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); - 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 << "MEDLoader::GetUMeshGlobalInfo : Mesh \""<< meshName << "\" in file \"" << fileName; + std::ostringstream oss; oss << "GetUMeshGlobalInfo : Mesh \""<< meshName << "\" in file \"" << fileName; oss << "\" exists but it is not an unstructured mesh ! This method is not relevant for mesh types that are not unstructured !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } // limitation if(nstep!=1) - throw INTERP_KERNEL::Exception("MEDLoader::GetUMeshGlobalInfo : multisteps on mesh not managed !"); + throw INTERP_KERNEL::Exception("GetUMeshGlobalInfo : multisteps on mesh not managed !"); med_int numdt,numit; med_float dt; - MEDmeshComputationStepInfo(fid,nommaa,1,&numdt,&numit,&dt); + MEDFILESAFECALLERRD0(MEDmeshComputationStepInfo,(fid,nommaa,1,&numdt,&numit,&dt)); // endlimitation std::vector dims; std::vector< std::pair > geoTypes; @@ -375,7 +429,7 @@ std::vector< std::vector< std::pair > > M for(int i=0;i0) { INTERP_KERNEL::NormalizedCellType typp=typmai2[i]; @@ -392,33 +446,30 @@ 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; } -void MEDLoader::CheckFileForRead(const char *fileName) +void MEDCoupling::CheckFileForRead(const std::string& fileName) { MEDFileUtilities::CheckFileForRead(fileName); } -std::vector MEDLoader::GetMeshNames(const char *fileName) +std::vector MEDCoupling::GetMeshNames(const std::string& fileName) { - CheckFileForRead(fileName); - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,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 > MEDLoader::GetComponentsNamesOfField(const char *fileName, const char *fieldName) +std::vector< std::pair > MEDCoupling::GetComponentsNamesOfField(const std::string& fileName, const std::string& fieldName) { - CheckFileForRead(fileName); - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); - med_int nbFields=MEDnField(fid); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); + med_int nbFields(MEDnField(fid)); std::vector fields(nbFields); med_field_type typcha; for(int i=0;i comp=new char[ncomp*MED_SNAME_SIZE+1]; INTERP_KERNEL::AutoPtr unit=new char[ncomp*MED_SNAME_SIZE+1]; INTERP_KERNEL::AutoPtr dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE); @@ -426,7 +477,7 @@ std::vector< std::pair > MEDLoader::GetComponentsNamesO med_bool localmesh; INTERP_KERNEL::AutoPtr maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE); INTERP_KERNEL::AutoPtr nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE); - MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt); + MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt)); std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE); std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1); if(curFieldName==fieldName) @@ -434,23 +485,53 @@ std::vector< std::pair > MEDLoader::GetComponentsNamesO std::vector< std::pair > ret(ncomp); for(int j=0;j(MEDLoaderBase::buildStringFromFortran(((char *)comp)+j*MED_SNAME_SIZE,MED_SNAME_SIZE), - MEDLoaderBase::buildStringFromFortran(((char *)unit)+j*MED_SNAME_SIZE,MED_SNAME_SIZE)); + MEDLoaderBase::buildStringFromFortran(((char *)unit)+j*MED_SNAME_SIZE,MED_SNAME_SIZE)); return ret; } fields[i]=curFieldName; } - std::ostringstream oss; oss << "MEDLoader::GetComponentsNamesOfField : no such field \"" << fieldName << "\" in file \"" << fileName << "\" !" << std::endl; + std::ostringstream oss; oss << "GetComponentsNamesOfField : no such field \"" << fieldName << "\" in file \"" << fileName << "\" !" << std::endl; oss << "Possible field names are : " << std::endl; std::copy(fields.begin(),fields.end(),std::ostream_iterator(oss," ")); throw INTERP_KERNEL::Exception(oss.str().c_str()); } -std::vector MEDLoader::GetMeshNamesOnField(const char *fileName, const char *fieldName) +// 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) { - CheckFileForRead(fileName); std::vector ret; // - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields=MEDnField(fid); // med_field_type typcha; @@ -460,12 +541,12 @@ std::vector MEDLoader::GetMeshNamesOnField(const char *fileName, co // for(int i=0;i comp=new char[ncomp*MED_SNAME_SIZE+1]; INTERP_KERNEL::AutoPtr unit=new char[ncomp*MED_SNAME_SIZE+1]; med_int nbPdt; INTERP_KERNEL::AutoPtr maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE); - MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt); + MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt)); std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE); std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1); if(curFieldName==fieldName) @@ -474,23 +555,22 @@ std::vector MEDLoader::GetMeshNamesOnField(const char *fileName, co return ret; } -std::vector MEDLoader::GetMeshFamiliesNames(const char *fileName, const char *meshName) +std::vector MEDCoupling::GetMeshFamiliesNames(const std::string& fileName, const std::string& meshName) { - CheckFileForRead(fileName); - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); - med_int nfam=MEDnFamily(fid,meshName); + 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]; INTERP_KERNEL::AutoPtr attdes=new char[MED_COMMENT_SIZE*natt+1]; INTERP_KERNEL::AutoPtr gro=new char[MED_LNAME_SIZE*ngro+1]; - MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro); + MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro); std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam)); ret[i]=cur; } @@ -498,23 +578,22 @@ std::vector MEDLoader::GetMeshFamiliesNames(const char *fileName, c } -std::vector MEDLoader::GetMeshFamiliesNamesOnGroup(const char *fileName, const char *meshName, const char *grpName) +std::vector MEDCoupling::GetMeshFamiliesNamesOnGroup(const std::string& fileName, const std::string& meshName, const std::string& grpName) { - CheckFileForRead(fileName); - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); - med_int nfam=MEDnFamily(fid,meshName); + 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]; INTERP_KERNEL::AutoPtr attdes=new char[MED_COMMENT_SIZE*natt+1]; INTERP_KERNEL::AutoPtr gro=new char[MED_LNAME_SIZE*ngro+1]; - MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro); + MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro); std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam)); for(int j=0;j MEDLoader::GetMeshFamiliesNamesOnGroup(const char *file return ret; } -std::vector MEDLoader::GetMeshGroupsNamesOnFamily(const char *fileName, const char *meshName, const char *famName) +std::vector MEDCoupling::GetMeshGroupsNamesOnFamily(const std::string& fileName, const std::string& meshName, const std::string& famName) { - CheckFileForRead(fileName); - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); - med_int nfam=MEDnFamily(fid,meshName); + 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]; INTERP_KERNEL::AutoPtr attdes=new char[MED_COMMENT_SIZE*natt+1]; INTERP_KERNEL::AutoPtr gro=new char[MED_LNAME_SIZE*ngro+1]; - MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro); + MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro); std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam)); found=(cur==famName); if(found) @@ -556,30 +634,29 @@ std::vector MEDLoader::GetMeshGroupsNamesOnFamily(const char *fileN if(!found) { std::ostringstream oss; - oss << "MEDLoader::GetMeshGroupsNamesOnFamily : no such family \"" << famName << "\" in file \"" << fileName << "\" in mesh \"" << meshName << "\" !"; + oss << "GetMeshGroupsNamesOnFamily : no such family \"" << famName << "\" in file \"" << fileName << "\" in mesh \"" << meshName << "\" !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } return ret; } - -std::vector MEDLoader::GetMeshGroupsNames(const char *fileName, const char *meshName) + +std::vector MEDCoupling::GetMeshGroupsNames(const std::string& fileName, const std::string& meshName) { - CheckFileForRead(fileName); - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); - med_int nfam=MEDnFamily(fid,meshName); + 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]; INTERP_KERNEL::AutoPtr attdes=new char[MED_COMMENT_SIZE*natt+1]; INTERP_KERNEL::AutoPtr gro=new char[MED_LNAME_SIZE*ngro+1]; - MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro); + MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro); for(int j=0;j MEDLoader::GetMeshGroupsNames(const char *fileName, con } return ret; } -std::vector MEDLoader::GetTypesOfField(const char *fileName, const char *meshName, const char *fieldName) + +std::vector MEDCoupling::GetTypesOfField(const std::string& fileName, const std::string& meshName, const std::string& fieldName) { - CheckFileForRead(fileName); - std::vector ret; - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); - med_int nbFields=MEDnField(fid); - // - med_field_type typcha; - //med_int nbpdtnor=0,pflsize,*pflval,lnsize; - med_int numdt=0,numo=0; - med_float dt=0.0; - char pflname[MED_NAME_SIZE+1]=""; - char locname[MED_NAME_SIZE+1]=""; - char *maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE); - char *nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE); - med_bool localmesh; - // - for(int i=0;i ret; + MCAuto fs(MEDFileAnyTypeFieldMultiTS::New(fileName,fieldName,false)); + if(fs->getMeshName()!=meshName) { - med_int ncomp=MEDfieldnComponent(fid,i+1); - INTERP_KERNEL::AutoPtr comp=new char[ncomp*MED_SNAME_SIZE+1]; - INTERP_KERNEL::AutoPtr unit=new char[ncomp*MED_SNAME_SIZE+1]; - INTERP_KERNEL::AutoPtr dt_unit=new char[MED_LNAME_SIZE+1]; - med_int nbPdt; - 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); - if(curMeshName==meshName) - { - if(curFieldName==fieldName) - { - int profilesize,nbi; - if(nbPdt>0) - { - bool found=false; - for(int ii=0;ii0) - { - ret.push_back(ON_NODES); - found=true; - } - } - } - bool found=false; - for(int j=0;j0) - { - MEDfieldComputingStepInfo(fid,nomcha,1,&numdt,&numo,&dt); - med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE, - pflname,&profilesize,locname,&nbi); - if(nbOfVal>0) - { - found=true; - ret.push_back(ON_CELLS); - } - } - } - } - } + std::ostringstream oss; oss << "GetTypesOfField : The field \"" << fieldName << "\" in file \"" << fileName << "\" is not lying on mesh \"" << meshName << "\""; + oss << " The name of the mesh in file is \"" << fs->getMeshName() << "\"!"; + throw INTERP_KERNEL::Exception(oss.str().c_str()); } - delete [] maa_ass; - delete [] nomcha; - return ret; + int nbTS(fs->getNumberOfTS()); + if(nbTS==0) + return ret; + for(int i=0;i 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()) + ret.push_back(*it); + } + // sort ret to put before ON_NODES then ON_CELLS then the remaining. + std::vector ret2; + if(std::find(ret.begin(),ret.end(),ON_NODES)!=ret.end()) + ret2.push_back(ON_NODES); + if(std::find(ret.begin(),ret.end(),ON_CELLS)!=ret.end()) + ret2.push_back(ON_CELLS); + for(std::vector::const_iterator it=ret.begin();it!=ret.end();it++) + if(*it!=ON_NODES && *it!=ON_CELLS) + ret2.push_back(*it); + return ret2; } -std::vector MEDLoader::GetAllFieldNames(const char *fileName) +std::vector MEDCoupling::GetAllFieldNames(const std::string& fileName) { - CheckFileForRead(fileName); std::vector ret; - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields=MEDnField(fid); med_field_type typcha; for(int i=0;i comp=new char[ncomp*MED_SNAME_SIZE+1]; INTERP_KERNEL::AutoPtr unit=new char[ncomp*MED_SNAME_SIZE+1]; INTERP_KERNEL::AutoPtr nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE); @@ -676,17 +716,16 @@ std::vector MEDLoader::GetAllFieldNames(const char *fileName) INTERP_KERNEL::AutoPtr dt_unit=new char[MED_LNAME_SIZE+1]; med_int nbPdt; med_bool localmesh; - MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt); + MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt)); ret.push_back(std::string(nomcha)); } return ret; } -std::vector MEDLoader::GetAllFieldNamesOnMesh(const char *fileName, const char *meshName) +std::vector MEDCoupling::GetAllFieldNamesOnMesh(const std::string& fileName, const std::string& meshName) { - CheckFileForRead(fileName); std::vector ret; - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields=MEDnField(fid); // med_field_type typcha; @@ -695,13 +734,13 @@ std::vector MEDLoader::GetAllFieldNamesOnMesh(const char *fileName, // for(int i=0;i comp=new char[ncomp*MED_SNAME_SIZE+1]; INTERP_KERNEL::AutoPtr unit=new char[ncomp*MED_SNAME_SIZE+1]; INTERP_KERNEL::AutoPtr dt_unit=new char[MED_LNAME_SIZE+1]; med_int nbPdt; med_bool localmesh; - MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt); + 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); // @@ -713,25 +752,24 @@ std::vector MEDLoader::GetAllFieldNamesOnMesh(const char *fileName, return ret; } -std::vector MEDLoader::GetFieldNamesOnMesh(ParaMEDMEM::TypeOfField type, const char *fileName, const char *meshName) +std::vector MEDCoupling::GetFieldNamesOnMesh(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName) { - CheckFileForRead(fileName); + MEDCoupling::CheckFileForRead(fileName); switch(type) - { + { case ON_CELLS: return GetCellFieldNamesOnMesh(fileName,meshName); case ON_NODES: return GetNodeFieldNamesOnMesh(fileName,meshName); default: throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !"); - } + } } -std::vector MEDLoader::GetCellFieldNamesOnMesh(const char *fileName, const char *meshName) +std::vector MEDCoupling::GetCellFieldNamesOnMesh(const std::string& fileName, const std::string& meshName) { - CheckFileForRead(fileName); std::vector ret; - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields=MEDnField(fid); // med_field_type typcha; @@ -748,13 +786,13 @@ std::vector MEDLoader::GetCellFieldNamesOnMesh(const char *fileName // for(int i=0;i comp=new char[ncomp*MED_SNAME_SIZE+1]; INTERP_KERNEL::AutoPtr unit=new char[ncomp*MED_SNAME_SIZE+1]; - MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt); + 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; @@ -762,9 +800,9 @@ std::vector MEDLoader::GetCellFieldNamesOnMesh(const char *fileName { if(nbPdt>0) { - MEDfieldComputingStepInfo(fid,nomcha,1,&numdt,&numo,&dt); - med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE, - pflname,&profilesize,locname,&nbi); + MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,1,&numdt,&numo,&dt)); + med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE, + pflname,&profilesize,locname,&nbi)); if(nbOfVal>0) { found=true; @@ -777,11 +815,10 @@ std::vector MEDLoader::GetCellFieldNamesOnMesh(const char *fileName return ret; } -std::vector MEDLoader::GetNodeFieldNamesOnMesh(const char *fileName, const char *meshName) +std::vector MEDCoupling::GetNodeFieldNamesOnMesh(const std::string& fileName, const std::string& meshName) { - CheckFileForRead(fileName); std::vector ret; - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,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]=""; @@ -796,19 +833,19 @@ std::vector MEDLoader::GetNodeFieldNamesOnMesh(const char *fileName // for(int i=0;i comp=new char[ncomp*MED_SNAME_SIZE+1]; INTERP_KERNEL::AutoPtr unit=new char[ncomp*MED_SNAME_SIZE+1]; med_int nbPdt; - MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt); + 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); if(nbPdt>0) { - int profilesize,nbi; - 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); + 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)); if(curMeshName==meshName && nbOfVal>0) { ret.push_back(curFieldName); @@ -818,11 +855,10 @@ std::vector MEDLoader::GetNodeFieldNamesOnMesh(const char *fileName return ret; } -std::vector< std::pair< std::pair, double> > MEDLoader::GetAllFieldIterations(const char *fileName, const char *fieldName) +std::vector< std::pair< std::pair, double> > MEDCoupling::GetAllFieldIterations(const std::string& fileName, const std::string& fieldName) { - CheckFileForRead(fileName); std::vector< std::pair< std::pair, double > > ret; - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields=MEDnField(fid); // med_field_type typcha; @@ -833,20 +869,20 @@ std::vector< std::pair< std::pair, double> > MEDLoader::GetAllFieldIter INTERP_KERNEL::AutoPtr nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE); med_bool localmesh; // - std::ostringstream oss; oss << "MEDLoader::GetAllFieldIterations : No field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : "; + std::ostringstream oss; oss << "GetAllFieldIterations : No field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : "; for(int i=0;i comp=new char[ncomp*MED_SNAME_SIZE+1]; INTERP_KERNEL::AutoPtr unit=new char[ncomp*MED_SNAME_SIZE+1]; med_int nbPdt; - MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt); + 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); if(curFieldName==fieldName) { for(int k=0;k, double> > MEDLoader::GetAllFieldIter throw INTERP_KERNEL::Exception(oss.str().c_str()); } -double MEDLoader::GetTimeAttachedOnFieldIteration(const char *fileName, const char *fieldName, int iteration, int order) +double MEDCoupling::GetTimeAttachedOnFieldIteration(const std::string& fileName, const std::string& fieldName, int iteration, int order) { - CheckFileForRead(fileName); - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields=MEDnField(fid); // med_field_type typcha; @@ -880,18 +915,18 @@ double MEDLoader::GetTimeAttachedOnFieldIteration(const char *fileName, const ch double ret=std::numeric_limits::max(); for(int i=0;i comp=new char[ncomp*MED_SNAME_SIZE+1]; INTERP_KERNEL::AutoPtr unit=new char[ncomp*MED_SNAME_SIZE+1]; med_int nbPdt; - MEDfieldInfo(fid,i+1,nomcha,maa_ass,&local,&typcha,comp,unit,dt_unit,&nbPdt); + MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&local,&typcha,comp,unit,dt_unit,&nbPdt)); std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1); if(curFieldName==fieldName) { found=true; for(int k=0;k > MEDLoader::GetFieldIterations(ParaMEDMEM::TypeOfField type, const char *fileName, const char *meshName, const char *fieldName) +std::vector< std::pair > MEDCoupling::GetFieldIterations(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, const std::string& fieldName) { - CheckFileForRead(fileName); + MEDCoupling::CheckFileForRead(fileName); switch(type) - { + { case ON_CELLS: return GetCellFieldIterations(fileName,meshName,fieldName); case ON_NODES: return GetNodeFieldIterations(fileName,meshName,fieldName); default: throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !"); - } + } } -std::vector< std::pair > MEDLoader::GetCellFieldIterations(const char *fileName, const char *meshName, const char *fieldName) +std::vector< std::pair > MEDCoupling::GetCellFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName) { - CheckFileForRead(fileName); std::string meshNameCpp(meshName); std::vector< std::pair > ret; - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields=MEDnField(fid); // med_field_type typcha; @@ -941,15 +975,15 @@ std::vector< std::pair > MEDLoader::GetCellFieldIterations(const char * INTERP_KERNEL::AutoPtr nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE); med_bool localmesh; // - std::ostringstream oss; oss << "MEDLoader::GetCellFieldIterations : No cell Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : "; + std::ostringstream oss; oss << "GetCellFieldIterations : No cell Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : "; std::set s2; for(int i=0;i comp=new char[ncomp*MED_SNAME_SIZE+1]; INTERP_KERNEL::AutoPtr unit=new char[ncomp*MED_SNAME_SIZE+1]; med_int nbPdt; - MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt); + 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); if(curFieldName==fieldName) { @@ -958,10 +992,10 @@ std::vector< std::pair > MEDLoader::GetCellFieldIterations(const char * { for(int k=0;k0) { @@ -995,12 +1029,11 @@ std::vector< std::pair > MEDLoader::GetCellFieldIterations(const char * return ret; } -std::vector< std::pair > MEDLoader::GetNodeFieldIterations(const char *fileName, const char *meshName, const char *fieldName) +std::vector< std::pair > MEDCoupling::GetNodeFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName) { - CheckFileForRead(fileName); std::string meshNameCpp(meshName); std::vector< std::pair > ret; - MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY); + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); med_int nbFields=MEDnField(fid); // med_field_type typcha; @@ -1013,32 +1046,32 @@ std::vector< std::pair > MEDLoader::GetNodeFieldIterations(const char * INTERP_KERNEL::AutoPtr nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE); med_bool localmesh; // - std::ostringstream oss; oss << "MEDLoader::GetNodeFieldIterations : No node Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : "; + std::ostringstream oss; oss << "GetNodeFieldIterations : No node Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : "; std::set s2; for(int i=0;i comp=new char[ncomp*MED_SNAME_SIZE+1]; INTERP_KERNEL::AutoPtr unit=new char[ncomp*MED_SNAME_SIZE+1]; med_int nbPdt; - MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt); + 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); if(curFieldName==fieldName) { for(int k=0;k0) - { - if(meshNameCpp==maa_ass_cpp) - { ret.push_back(std::make_pair(numdt,numo)); } - else - s2.insert(maa_ass_cpp); - } + med_int profilesize,nbi; + MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt)); + med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE, + pflname,&profilesize,locname,&nbi)); + std::string maa_ass_cpp(maa_ass); + if(nbOfVal>0) + { + if(meshNameCpp==maa_ass_cpp) + { ret.push_back(std::make_pair(numdt,numo)); } + else + s2.insert(maa_ass_cpp); + } } } else @@ -1060,10 +1093,10 @@ std::vector< std::pair > MEDLoader::GetNodeFieldIterations(const char * return ret; } -ParaMEDMEM::MEDCouplingMesh *MEDLoader::ReadMeshFromFile(const char *fileName, const char *meshName, int meshDimRelToMax) +MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax) { - CheckFileForRead(fileName); - MEDCouplingAutoRefCountObjectPtr mm=MEDFileMesh::New(fileName,meshName); + MEDCoupling::CheckFileForRead(fileName); + MCAuto mm(MEDFileMesh::New(fileName,meshName)); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(mmuPtr) @@ -1080,14 +1113,14 @@ ParaMEDMEM::MEDCouplingMesh *MEDLoader::ReadMeshFromFile(const char *fileName, c const MEDCouplingCurveLinearMesh *ret(mmc2Ptr->getMesh()); ret->incrRef(); return const_cast(ret); } - std::ostringstream oss; oss << "MEDLoader::ReadMeshFromFile : The mesh \"" << meshName << "\" in file \"" << fileName << "\" has not a recognized type !"; + std::ostringstream oss; oss << "ReadMeshFromFile : The mesh \"" << meshName << "\" in file \"" << fileName << "\" has not a recognized type !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } -ParaMEDMEM::MEDCouplingMesh *MEDLoader::ReadMeshFromFile(const char *fileName, int meshDimRelToMax) +MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& fileName, int meshDimRelToMax) { - CheckFileForRead(fileName); - MEDCouplingAutoRefCountObjectPtr mm=MEDFileMesh::New(fileName); + MEDCoupling::CheckFileForRead(fileName); + MCAuto mm(MEDFileMesh::New(fileName)); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(mmuPtr) @@ -1104,78 +1137,155 @@ ParaMEDMEM::MEDCouplingMesh *MEDLoader::ReadMeshFromFile(const char *fileName, i const MEDCouplingCurveLinearMesh *ret(mmc2Ptr->getMesh()); ret->incrRef(); return const_cast(ret); } - std::ostringstream oss; oss << "MEDLoader::ReadMeshFromFile (2) : The first mesh \"" << mm->getName() << "\" in file \"" << fileName << "\" has not a recognized type !"; + std::ostringstream oss; oss << "ReadMeshFromFile (2) : The first mesh \"" << mm->getName() << "\" in file \"" << fileName << "\" has not a recognized type !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } -ParaMEDMEM::MEDCouplingUMesh *MEDLoader::ReadUMeshFromFile(const char *fileName, const char *meshName, int meshDimRelToMax) +MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax) { - CheckFileForRead(fileName); - MEDCouplingAutoRefCountObjectPtr mm=MEDFileMesh::New(fileName,meshName); + MEDCoupling::CheckFileForRead(fileName); + MCAuto mm(MEDFileMesh::New(fileName,meshName)); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(!mmuPtr) { - std::ostringstream oss; oss << "MEDLoader::ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName=\""<< meshName << "\" exists but it is not an unstructured mesh !"; + std::ostringstream oss; oss << "ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName=\""<< meshName << "\" exists but it is not an unstructured mesh !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } - return mmuPtr->getMeshAtLevel(meshDimRelToMax,true); + return mmuPtr->getMeshAtLevel(meshDimRelToMax,true); } -ParaMEDMEM::MEDCouplingUMesh *MEDLoader::ReadUMeshFromFile(const char *fileName, int meshDimRelToMax) +MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFile(const std::string& fileName, int meshDimRelToMax) { - CheckFileForRead(fileName); - MEDCouplingAutoRefCountObjectPtr mm=MEDFileMesh::New(fileName); + MEDCoupling::CheckFileForRead(fileName); + MCAuto mm(MEDFileMesh::New(fileName)); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(!mmuPtr) { - std::ostringstream oss; oss << "MEDLoader::ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !"; + std::ostringstream oss; oss << "ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } - return mmuPtr->getMeshAtLevel(meshDimRelToMax,true); + return mmuPtr->getMeshAtLevel(meshDimRelToMax,true); } -int MEDLoader::ReadUMeshDimFromFile(const char *fileName, const char *meshName) +int MEDCoupling::ReadUMeshDimFromFile(const std::string& fileName, const std::string& meshName) { - CheckFileForRead(fileName); + MEDCoupling::CheckFileForRead(fileName); std::vector poss; return MEDLoaderNS::readUMeshDimFromFile(fileName,meshName,poss); } -ParaMEDMEM::MEDCouplingUMesh *MEDLoader::ReadUMeshFromFamilies(const char *fileName, const char *meshName, int meshDimRelToMax, const std::vector& fams) +MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFamilies(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::vector& fams) { - CheckFileForRead(fileName); - MEDCouplingAutoRefCountObjectPtr mm=MEDFileMesh::New(fileName,meshName); + MEDCoupling::CheckFileForRead(fileName); + MCAuto mm(MEDFileMesh::New(fileName,meshName)); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(!mmuPtr) { - std::ostringstream oss; oss << "MEDLoader::ReadUMeshFromFamilies : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !"; + std::ostringstream oss; oss << "ReadUMeshFromFamilies : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } - return mmuPtr->getFamilies(meshDimRelToMax,fams,true); + return mmuPtr->getFamilies(meshDimRelToMax,fams,true); } -ParaMEDMEM::MEDCouplingUMesh *MEDLoader::ReadUMeshFromGroups(const char *fileName, const char *meshName, int meshDimRelToMax, const std::vector& grps) +MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromGroups(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::vector& grps) { - CheckFileForRead(fileName); - MEDCouplingAutoRefCountObjectPtr mm=MEDFileMesh::New(fileName,meshName); + MEDCoupling::CheckFileForRead(fileName); + MCAuto mm=MEDFileMesh::New(fileName,meshName); MEDFileMesh *mmPtr(mm); MEDFileUMesh *mmuPtr=dynamic_cast(mmPtr); if(!mmuPtr) { - std::ostringstream oss; oss << "MEDLoader::ReadUMeshFromGroups : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !"; + std::ostringstream oss; oss << "ReadUMeshFromGroups : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } - return mmuPtr->getGroups(meshDimRelToMax,grps,true); + return mmuPtr->getGroups(meshDimRelToMax,grps,true); } -ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadField(ParaMEDMEM::TypeOfField type, const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order) +MCAuto MEDCoupling::ReadField(const std::string& fileName) { - CheckFileForRead(fileName); - switch(type) + 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) + { case ON_CELLS: return ReadFieldCell(fileName,meshName,meshDimRelToMax,fieldName,iteration,order); case ON_NODES: @@ -1186,35 +1296,35 @@ ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadField(ParaMEDMEM::TypeOfField return ReadFieldGaussNE(fileName,meshName,meshDimRelToMax,fieldName,iteration,order); default: throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES, ON_CELLS, ON_GAUSS_PT or ON_GAUSS_NE !"); - } + } } -std::vector MEDLoader::ReadFieldsOnSameMesh(ParaMEDMEM::TypeOfField type, const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, - const std::vector >& its) throw(INTERP_KERNEL::Exception) +std::vector MEDCoupling::ReadFieldsOnSameMesh(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, + const std::vector >& its) { if(its.empty()) - return std::vector(); - CheckFileForRead(fileName); - std::vector ret(its.size()); - std::vector< MEDCouplingAutoRefCountObjectPtr > retSafe(its.size()); + return std::vector(); + MEDCoupling::CheckFileForRead(fileName); + std::vector ret(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("MEDLoader::ReadFieldsOnSameMesh : only unstructured mesh is managed !"); - MEDCouplingAutoRefCountObjectPtr m=mmuPtr->getMeshAtLevel(meshDimRelToMax); - const DataArrayInt *o2n=mmuPtr->getNumberFieldAtLevel(meshDimRelToMax); - MEDCouplingAutoRefCountObjectPtr m2(m->clone(true)); + throw INTERP_KERNEL::Exception("ReadFieldsOnSameMesh : only unstructured mesh is managed !"); + MCAuto m=mmuPtr->getMeshAtLevel(meshDimRelToMax); + const DataArrayIdType *o2n=mmuPtr->getNumberFieldAtLevel(meshDimRelToMax); + 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); @@ -1226,127 +1336,169 @@ std::vector MEDLoader::ReadFieldsOnSameMes return ret; } -std::vector MEDLoader::ReadFieldsCellOnSameMesh(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, - const std::vector >& its) throw(INTERP_KERNEL::Exception) +std::vector MEDCoupling::ReadFieldsCellOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, + const std::vector >& its) { return ReadFieldsOnSameMesh(ON_CELLS,fileName,meshName,meshDimRelToMax,fieldName,its); } -std::vector MEDLoader::ReadFieldsNodeOnSameMesh(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, - const std::vector >& its) throw(INTERP_KERNEL::Exception) +std::vector MEDCoupling::ReadFieldsNodeOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, + const std::vector >& its) { return ReadFieldsOnSameMesh(ON_NODES,fileName,meshName,meshDimRelToMax,fieldName,its); } -std::vector MEDLoader::ReadFieldsGaussOnSameMesh(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, - const std::vector >& its) throw(INTERP_KERNEL::Exception) +std::vector MEDCoupling::ReadFieldsGaussOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, + const std::vector >& its) { return ReadFieldsOnSameMesh(ON_GAUSS_PT,fileName,meshName,meshDimRelToMax,fieldName,its); } -std::vector MEDLoader::ReadFieldsGaussNEOnSameMesh(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, - const std::vector >& its) throw(INTERP_KERNEL::Exception) +std::vector MEDCoupling::ReadFieldsGaussNEOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, + const std::vector >& its) { return ReadFieldsOnSameMesh(ON_GAUSS_NE,fileName,meshName,meshDimRelToMax,fieldName,its); } -ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadFieldCell(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order) +namespace MEDCoupling { - MEDCouplingAutoRefCountObjectPtr ff=MEDFileField1TS::New(fileName,fieldName,iteration,order); - MEDCouplingAutoRefCountObjectPtr mm=MEDFileMesh::New(fileName,meshName); - MEDCouplingAutoRefCountObjectPtr m=mm->getGenMeshAtLevel(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 DataArrayIdType *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(); -} - -ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadFieldNode(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order) -{ - MEDCouplingAutoRefCountObjectPtr ff=MEDFileField1TS::New(fileName,fieldName,iteration,order); - MEDCouplingAutoRefCountObjectPtr mm=MEDFileMesh::New(fileName,meshName); - MEDCouplingAutoRefCountObjectPtr m=mm->getGenMeshAtLevel(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("MEDLoader::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 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; + } } -ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadFieldGauss(const char *fileName, const char *meshName, int meshDimRelToMax, const char *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->getGenMeshAtLevel(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); } -ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadFieldGaussNE(const char *fileName, const char *meshName, int meshDimRelToMax, const char *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->getGenMeshAtLevel(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 !"); } -void MEDLoader::WriteMesh(const char *fileName, const ParaMEDMEM::MEDCouplingMesh *mesh, bool writeFromScratch) +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) { if(!mesh) - throw INTERP_KERNEL::Exception("MEDLoader::WriteMesh : input mesh is null !"); + throw INTERP_KERNEL::Exception("WriteMesh : input mesh is null !"); const MEDCouplingUMesh *um(dynamic_cast(mesh)); if(um) { @@ -1357,7 +1509,8 @@ void MEDLoader::WriteMesh(const char *fileName, const ParaMEDMEM::MEDCouplingMes 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); return ; @@ -1365,7 +1518,8 @@ void MEDLoader::WriteMesh(const char *fileName, const ParaMEDMEM::MEDCouplingMes 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); return ; @@ -1373,31 +1527,33 @@ void MEDLoader::WriteMesh(const char *fileName, const ParaMEDMEM::MEDCouplingMes 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); return ; } - throw INTERP_KERNEL::Exception("MEDLoader::WriteMesh : only MEDCouplingUMesh, MEDCoupling1GTUMesh, MEDCouplingCMesh, MEDCouplingCurveLinear are dealed in this API for the moment !"); + throw INTERP_KERNEL::Exception("WriteMesh : only MEDCouplingUMesh, MEDCoupling1GTUMesh, MEDCouplingCMesh, MEDCouplingCurveLinear are dealed in this API for the moment !"); } -void MEDLoader::WriteUMesh(const char *fileName, const ParaMEDMEM::MEDCouplingUMesh *mesh, bool writeFromScratch) +void MEDCoupling::WriteUMesh(const std::string& fileName, const MEDCoupling::MEDCouplingUMesh *mesh, bool writeFromScratch) { if(!mesh) - throw INTERP_KERNEL::Exception("MEDLoader::WriteUMesh : input mesh is null !"); + throw INTERP_KERNEL::Exception("WriteUMesh : input mesh is null !"); int mod=writeFromScratch?2:0; - MEDCouplingAutoRefCountObjectPtr m=MEDFileUMesh::New(); - MEDCouplingAutoRefCountObjectPtr mcpy(static_cast(mesh->deepCpy())); + MCAuto m(MEDFileUMesh::New()); + AssignStaticWritePropertiesTo(*m); + MCAuto mcpy(static_cast(mesh->deepCopy())); m->setMeshAtLevel(0,mcpy,true); m->write(fileName,mod); } -void MEDLoader::WriteUMeshDep(const char *fileName, const ParaMEDMEM::MEDCouplingUMesh *mesh, bool writeFromScratch) +void MEDCoupling::WriteUMeshDep(const std::string& fileName, const MEDCoupling::MEDCouplingUMesh *mesh, bool writeFromScratch) { - MEDLoader::WriteUMesh(fileName,mesh,writeFromScratch); + WriteUMesh(fileName,mesh,writeFromScratch); } -void MEDLoader::WriteUMeshesPartition(const char *fileName, const char *meshNameC, const std::vector& meshes, bool writeFromScratch) +void MEDCoupling::WriteUMeshesPartition(const std::string& fileName, const std::string& meshNameC, const std::vector& meshes, bool writeFromScratch) { std::string meshName(meshNameC); if(meshName.empty()) @@ -1408,42 +1564,47 @@ void MEDLoader::WriteUMeshesPartition(const char *fileName, const char *meshName std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } - MEDCouplingAutoRefCountObjectPtr m=MEDFileUMesh::New(); + MCAuto m(MEDFileUMesh::New()); + AssignStaticWritePropertiesTo(*m); m->setGroupsFromScratch(0,meshes,true); m->setName(meshNameC); int mod=writeFromScratch?2:0; m->write(fileName,mod); } -void MEDLoader::WriteUMeshesPartitionDep(const char *fileName, const char *meshNameC, const std::vector& meshes, bool writeFromScratch) +void MEDCoupling::WriteUMeshesPartitionDep(const std::string& fileName, const std::string& meshNameC, const std::vector& meshes, bool writeFromScratch) { WriteUMeshesPartition(fileName,meshNameC,meshes,writeFromScratch); } -void MEDLoader::WriteUMeshes(const char *fileName, const std::vector& meshes, bool writeFromScratch) +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 char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, bool writeFromScratch) +template +void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const std::string& fileName, const typename MEDCoupling::Traits::FieldType *f, bool writeFromScratch) { - MEDCouplingAutoRefCountObjectPtr ff=MEDFileField1TS::New(); - MEDCouplingAutoRefCountObjectPtr f2(f->deepCpy()); + MCAuto< typename MLFieldTraits::F1TSType > ff(MLFieldTraits::F1TSType::New()); + AssignStaticWritePropertiesTo(*ff); + 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(); - MEDCouplingAutoRefCountObjectPtr o2n=um->getRenumArrForMEDFileFrmt(); - MEDCouplingAutoRefCountObjectPtr n2o=o2n->invertArrayO2N2N2O(o2n->getNumberOfTuples()); + MCAuto mmu(MEDFileUMesh::New()); + AssignStaticWritePropertiesTo(*mmu); + 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); @@ -1452,21 +1613,24 @@ void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const char *fil } else if(um2) { - MEDCouplingAutoRefCountObjectPtr mmu=MEDFileUMesh::New(); + MCAuto mmu(MEDFileUMesh::New()); + AssignStaticWritePropertiesTo(*mmu); mmu->setMeshAtLevel(0,const_cast(um2)); ff->setFieldNoProfileSBT(f2); mmu->write(fileName,mod); } else if(um3) { - MEDCouplingAutoRefCountObjectPtr mmc=MEDFileCMesh::New(); + MCAuto mmc(MEDFileCMesh::New()); + AssignStaticWritePropertiesTo(*mmc); mmc->setMesh(const_cast(um3)); ff->setFieldNoProfileSBT(f2); mmc->write(fileName,mod); } else if(um4) { - MEDCouplingAutoRefCountObjectPtr mmc=MEDFileCurveLinearMesh::New(); + MCAuto mmc(MEDFileCurveLinearMesh::New()); + AssignStaticWritePropertiesTo(*mmc); mmc->setMesh(const_cast(um4)); ff->setFieldNoProfileSBT(f2); mmc->write(fileName,mod); @@ -1476,12 +1640,13 @@ void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const char *fil ff->write(fileName,0); } -void MEDLoader::WriteField(const char *fileName, const ParaMEDMEM::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("MEDLoader::WriteField : input field is NULL !"); - f->checkCoherency(); - int status=MEDLoaderBase::getStatusOfFile(fileName); + throw INTERP_KERNEL::Exception("WriteField : input field is NULL !"); + 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 !"; @@ -1489,44 +1654,49 @@ void MEDLoader::WriteField(const char *fileName, const ParaMEDMEM::MEDCouplingFi } 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("MEDLoader::WriteField : trying to write a field with no mesh !"); + 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("MEDLoader::WriteField : only umeshes are supported now !"); - MEDCouplingAutoRefCountObjectPtr f2(f->deepCpy()); - MEDCouplingUMesh *m=dynamic_cast(const_cast(f2->getMesh())); + throw INTERP_KERNEL::Exception("WriteField : only umeshes are supported now !"); + MCAuto< typename MEDCoupling::Traits::FieldType > f2(f->deepCopy()); + MEDCouplingUMesh *m(dynamic_cast(const_cast(f2->getMesh()))); if(!m) - throw INTERP_KERNEL::Exception("MEDLoader::WriteField : only umesh in input field supported !"); - MEDCouplingAutoRefCountObjectPtr o2n=m->getRenumArrForMEDFileFrmt(); + throw INTERP_KERNEL::Exception("WriteField : only umesh in input field supported !"); + 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()); + DataArrayIdType *part(NULL); + bool b(mread->areCellsIncludedIn(m,_COMP_FOR_CELL,part)); + MCAuto partSafe(part); if(!b) { - std::ostringstream oss; oss << "MEDLoader::WriteField : The file \""<< fileName << "\" already contains a mesh named \""<< f->getMesh()->getName() << "\" and this mesh in the file is not compatible (a subpart) with the mesh you intend to write ! This is maybe due to a too strict policy ! Try with to lease it by calling SetCompPolicyForCell !"; + 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(); - if(part->isIdentity() && part->getNumberOfTuples()==mread->getNumberOfCells()) + MCAuto< typename MLFieldTraits::F1TSType > f1ts(MLFieldTraits::F1TSType::New()); + AssignStaticWritePropertiesTo(*f1ts); + if(part->isIota(mread->getNumberOfCells())) f1ts->setFieldNoProfileSBT(f2); else { @@ -1538,16 +1708,17 @@ void MEDLoader::WriteField(const char *fileName, const ParaMEDMEM::MEDCouplingFi } else { - DataArrayInt *part=0; - bool b=mread->getCoords()->areIncludedInMe(m->getCoords(),_EPS_FOR_NODE_COMP,part); - MEDCouplingAutoRefCountObjectPtr 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 << "MEDLoader::WriteField : The file \""<< fileName << "\" already contains a mesh named \""<< f->getMesh()->getName() << "\" and this mesh in the file is not compatible (a subpart regarding nodes) with the mesh you intend to write ! This is maybe due to a too strict epsilon ! Try with to lease it by calling SetEpsilonForNodeComp !"; + 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(); - if(part->isIdentity() && part->getNumberOfTuples()==mread->getNumberOfNodes()) + MCAuto< typename MLFieldTraits::F1TSType > f1ts(MLFieldTraits::F1TSType::New()); + AssignStaticWritePropertiesTo(*f1ts); + if(part->isIota(mread->getNumberOfNodes())) f1ts->setFieldNoProfileSBT(f2); else { @@ -1560,29 +1731,90 @@ void MEDLoader::WriteField(const char *fileName, const ParaMEDMEM::MEDCouplingFi } } -void MEDLoader::WriteFieldDep(const char *fileName, const ParaMEDMEM::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 MEDLoader::WriteFieldUsingAlreadyWrittenMesh(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f) +template +void WriteFieldUsingAlreadyWrittenMeshT(const std::string& fileName, const typename MEDCoupling::Traits::FieldType *f) { if(!f) - throw INTERP_KERNEL::Exception("MEDLoader::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(); - MEDCouplingUMesh *m=dynamic_cast(const_cast(f->getMesh())); - if(!m) - throw INTERP_KERNEL::Exception("MEDLoader::WriteFieldUsingAlreadyWrittenMesh : only umesh in input field supported !"); - MEDCouplingAutoRefCountObjectPtr o2n=m->getRenumArrForMEDFileFrmt(); - MEDCouplingAutoRefCountObjectPtr f2(f->deepCpy()); - f2->renumberCells(o2n->begin(),false); - f1ts->setFieldNoProfileSBT(f2); + 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< typename MEDCoupling::Traits::FieldType > f2(f->deepCopy()); + f2->renumberCells(o2n->begin(),false); + f1ts->setFieldNoProfileSBT(f2); + } + else + 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 !"); +}