- pflname,&profilesize,locname,&nbi);
- std::string maa_ass_cpp(maa_ass);
- if(meshNameCpp==maa_ass_cpp && nbOfVal>0)
- {
- ret.push_back(std::make_pair(numdt,numo));
- }
- }
- }
- }
- MEDfileClose(fid);
- return ret;
-}
-
-/*!
- * This method reads all the content of a field 'fieldName' at a time specified by (iteration,order) lying on a mesh 'meshName' with a specified type 'TypeOfOutField'
- * The returned values are strored in 'field' (sorted by type of cell), time corresponding to field, and 'infos' to load properly little strings.
- * The principle of this method is to put into 'field' only data that fulfills \b perfectly request.
- */
-void MEDLoaderNS::readFieldDoubleDataInMedFile(const char *fileName, const char *meshName, const char *fieldName,
- int iteration, int order, ParaMEDMEM::TypeOfField typeOfOutField,
- std::list<MEDLoader::MEDFieldDoublePerCellType>& field,
- double& time, std::vector<std::string>& infos)
-{
- time=0.;
- MEDFileUtilities::CheckFileForRead(fileName);
- MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
- med_int nbFields=MEDnField(fid);
- //
- med_field_type typcha;
- char nomcha[MED_NAME_SIZE+1]="";
- char pflname [MED_NAME_SIZE+1]="";
- char locname [MED_NAME_SIZE+1]="";
- std::map<ParaMEDMEM::TypeOfField, med_entity_type> tabEnt;
- std::map<ParaMEDMEM::TypeOfField, med_geometry_type *> tabType;
- std::map<ParaMEDMEM::TypeOfField, int> tabTypeLgth;
- med_bool localmesh;
- bool found=false;
- tabEnt[ON_CELLS]=MED_CELL;
- tabType[ON_CELLS]=typmai;
- tabTypeLgth[ON_CELLS]=MED_N_CELL_FIXED_GEO;
- tabEnt[ON_NODES]=MED_NODE;
- tabType[ON_NODES]=typmainoeud;
- tabTypeLgth[ON_NODES]=1;
- tabEnt[ON_GAUSS_PT]=MED_CELL;
- tabType[ON_GAUSS_PT]=typmai;
- tabTypeLgth[ON_GAUSS_PT]=MED_N_CELL_FIXED_GEO;
- tabEnt[ON_GAUSS_NE]=MED_NODE_ELEMENT;
- tabType[ON_GAUSS_NE]=typmai;
- tabTypeLgth[ON_GAUSS_NE]=MED_N_CELL_FIXED_GEO;
- //
- for(int i=0;i<nbFields && !found;i++)
- {
- med_int ncomp=MEDfieldnComponent(fid,i+1);
- INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
- INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
- INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
- INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
- med_int nbPdt;
- MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
- std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
- std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
- found=(curFieldName==fieldName) && (curMeshName==meshName);
- if(found)
- {
- infos.resize(ncomp);
- for(int ii=0;ii<ncomp;ii++)
- infos[ii]=MEDLoaderBase::buildUnionUnit(comp+ii*MED_SNAME_SIZE,MED_SNAME_SIZE,unit+ii*MED_SNAME_SIZE,MED_SNAME_SIZE);
- bool found2=false;
- med_int numdt=0,numo=0;
- med_float dt=0.0;
- for(int k=0;k<nbPdt && !found2;k++)
- {
- MEDfieldComputingStepInfo(fid,fieldName,k+1,&numdt,&numo,&dt);
- found2=(numdt==iteration && numo==order);
- if(found2)
- time=dt;
- }
- if(!found2)
- {
- std::ostringstream oss; oss << "FieldDouble in file \""<< fileName<< "\" with name \"" << fieldName << "\" on mesh \"" << meshName;
- oss << "\" does not have such time step : iteration=" << iteration << " order=" << order << std::endl;
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- for(int j=0;j<tabTypeLgth[typeOfOutField];j++)
- {
- if(nbPdt>0)
- {
- int profilesize,nbi;
- int nval=MEDfieldnValueWithProfile(fid,fieldName,numdt,numo,tabEnt[typeOfOutField],tabType[typeOfOutField][j],1,MED_COMPACT_PFLMODE,pflname,&profilesize,locname,&nbi);
- if(nval>0)
- {
- double *valr=new double[ncomp*nval*nbi];
- MEDfieldValueWithProfileRd(fid,fieldName,iteration,order,tabEnt[typeOfOutField],tabType[typeOfOutField][j],MED_COMPACT_PFLMODE,
- pflname,MED_FULL_INTERLACE,MED_ALL_CONSTITUENT,(unsigned char*)valr);
- std::string tmp(locname);
- if((locname[0]!='\0' && (typeOfOutField!=ON_GAUSS_PT))
- || (locname[0]=='\0' && typeOfOutField==ON_GAUSS_PT))
- {
- delete [] valr;
- continue;
- }
- INTERP_KERNEL::AutoPtr<int> pfl=0;
- if(pflname[0]!='\0')
- {
- pfl=new int[nval];
- MEDprofileRd(fid,pflname,pfl);
- }
- field.push_back(MEDLoader::MEDFieldDoublePerCellType(typmai2[j],valr,ncomp,nval*nbi,pfl,locname));
- }
- }
- }
- }
- }
- if(!found)
- {
- std::ostringstream oss; oss << "MEDLoaderNS::readFieldDoubleDataInMedFile : no such couple meshName=\"" << meshName << "\", fieldName=\"" << fieldName << "\" in file \"" << fileName << "\" !";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
-}
-
-std::vector<int> MEDLoaderNS::getIdsFromFamilies(const char *fileName, const char *meshName, const std::vector<std::string>& fams)
-{
- std::vector<int> ret;
- med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
- med_int nfam=MEDnFamily(fid,meshName);
- char nomfam[MED_NAME_SIZE+1];
- med_int numfam;
- for(int i=0;i<nfam;i++)
- {
- int ngro=MEDnFamilyGroup(fid,meshName,i+1);
- med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
- INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
- INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
- INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
- INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
- MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
- std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
- if(std::find(fams.begin(),fams.end(),cur)!=fams.end())
- ret.push_back(numfam);
- }
- MEDfileClose(fid);
- return ret;
-}
-
-std::vector<int> MEDLoaderNS::getIdsFromGroups(const char *fileName, const char *meshName, const std::vector<std::string>& grps)
-{
- std::vector<int> ret;
- med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
- med_int nfam=MEDnFamily(fid,meshName);
- char nomfam[MED_NAME_SIZE+1];
- med_int numfam;
- for(int i=0;i<nfam;i++)
- {
- int ngro=MEDnFamilyGroup(fid,meshName,i+1);
- med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
- INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
- INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
- INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
- INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
- MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
- std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
- for(int j=0;j<ngro;j++)
- {
- std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
- if(std::find(grps.begin(),grps.end(),cur2)!=grps.end())
- {
- ret.push_back(numfam);
- break;
- }
- }
- }
- MEDfileClose(fid);
- return ret;
-}
-
-med_int MEDLoaderNS::getIdFromMeshName(med_idt fid, const char *meshName, std::string& trueMeshName) throw(INTERP_KERNEL::Exception)
-{
- if(meshName==0)
- {
- std::vector<std::string> meshes=getMeshNamesFid(fid);
- if(meshes.empty())
- throw INTERP_KERNEL::Exception("No mesh in file");
- trueMeshName=meshes[0];
- return 1;
- }
- std::string meshNameStr(meshName);
- std::vector<std::string> meshes=getMeshNamesFid(fid);
- if(meshes.empty())
- throw INTERP_KERNEL::Exception("No mesh in file");
- std::vector<std::string>::iterator iter=std::find(meshes.begin(),meshes.end(),meshNameStr);
- if(iter==meshes.end())
- {
- std::ostringstream os2;
- os2 << "MeshName '" << meshName << "' not in file : meshes available : ";
- std::copy(meshes.begin(),meshes.end(),std::ostream_iterator<std::string>(os2," "));
- throw INTERP_KERNEL::Exception(os2.str().c_str());
- }
- trueMeshName=meshName;
- return iter-meshes.begin()+1;
-}
-
-/*!
- * 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)
-{
- if(nbOfElemCell>=nbOfElemFace)
- {
- whichEntity=MED_CELL;
- nbOfElem=nbOfElemCell;
- }
- else
- {
- whichEntity=MED_CELL;
- nbOfElem=nbOfElemFace;
- }
-}
-
-/*!
- * This method returns a first quick overview of mesh with name 'meshName' into the file 'fileName'.
- * @param possibilities the relativeToMeshDim authorized to returned maxdim. This vector is systematically cleared at the begin of this method.
- * @return the maximal mesh dimension of specified mesh. If nothing found -1 is returned.
- */
-int MEDLoaderNS::readUMeshDimFromFile(const char *fileName, const char *meshName, std::vector<int>& possibilities)
-{
- possibilities.clear();
- med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
- int ret;
- std::set<int> poss;
- char nommaa[MED_NAME_SIZE+1];
- char maillage_description[MED_COMMENT_SIZE+1];
- med_mesh_type type_maillage;
- med_int Sdim,Mdim;
- std::string trueMeshName;
- med_int meshId=getIdFromMeshName(fid,meshName,trueMeshName);
- INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
- med_sorting_type sortingType;
- med_int nstep;
- med_axis_type axisType;
- int naxis=MEDmeshnAxis(fid,meshId);
- INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
- INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
- MEDmeshInfo(fid,meshId,nommaa,&Sdim,&Mdim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit);
- // limitation
- if(nstep!=1)
- {
- throw INTERP_KERNEL::Exception("multisteps on mesh not managed yet !");
- }
- med_int numdt,numit;
- med_float dt;
- MEDmeshComputationStepInfo(fid,nommaa,1,&numdt,&numit,&dt);
- // endlimitation
- for(int i=0;i<MED_N_CELL_GEO_FIXED_CON;i++)
- {
- med_geometry_type curMedType=typmai[i];
- med_bool changement,transformation;
- int curNbOfElemM=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);
- int curNbOfElemF=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);//limitation
- int curNbOfElem;
- med_entity_type whichEntity;
- MEDLoaderNS::dispatchElems(curNbOfElemM,curNbOfElemF,curNbOfElem,whichEntity);
- if(curNbOfElem>0)
- {
- INTERP_KERNEL::NormalizedCellType type=typmai2[i];
- int curDim=(int)INTERP_KERNEL::CellModel::GetCellModel(type).getDimension();
- poss.insert(curDim);
- }
- }
- MEDfileClose(fid);
- if(!poss.empty())
- {
- ret=*poss.rbegin();
- for(std::set<int>::const_reverse_iterator it=poss.rbegin();it!=poss.rend();it++)
- possibilities.push_back(*it-ret);
- }
- else
- ret=-2;
- return ret;
-}
-
-void MEDLoaderNS::readUMeshDataInMedFile(med_idt fid, med_int meshId, DataArrayDouble *&coords, std::list<MEDLoader::MEDConnOfOneElemType>& conn, std::string& description)
-{
- char nommaa[MED_NAME_SIZE+1];
- char maillage_description[MED_COMMENT_SIZE+1];
- med_mesh_type type_maillage;
- med_int Mdim;
- med_int Sdim;
- INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
- med_sorting_type sortingType;
- med_int nstep;
- med_axis_type axisType;
- med_int numdt,numit;
- med_float dt;
- med_bool changement,transformation;
- // endlimitation
- Sdim=MEDmeshnAxis(fid,1);
- INTERP_KERNEL::AutoPtr<char> comp=MEDLoaderBase::buildEmptyString(Sdim*MED_SNAME_SIZE);
- INTERP_KERNEL::AutoPtr<char> unit=MEDLoaderBase::buildEmptyString(Sdim*MED_SNAME_SIZE);
- MEDmeshInfo(fid,meshId,nommaa,&Sdim,&Mdim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,comp,unit);
- description=MEDLoaderBase::buildStringFromFortran(maillage_description,sizeof(maillage_description));
- MEDmeshComputationStepInfo(fid,nommaa,1,&numdt,&numit,&dt);
- int spaceDim=std::max((int)Mdim,(int)Sdim);
- int nCoords=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation);
- // limitation
- if(nstep!=1)
- {
- throw INTERP_KERNEL::Exception("multisteps on mesh not managed yet !");
- }
- coords=DataArrayDouble::New();
- coords->alloc(nCoords,spaceDim);
- double *coordsPtr=coords->getPointer();
- MEDmeshNodeCoordinateRd(fid,nommaa,numdt,numit,MED_FULL_INTERLACE,coordsPtr);
- for(int i=0;i<spaceDim;i++)
- {
- std::string info=MEDLoaderBase::buildUnionUnit(comp+i*MED_SNAME_SIZE,MED_SNAME_SIZE,unit+i*MED_SNAME_SIZE,MED_SNAME_SIZE);
- coords->setInfoOnComponent(i,info.c_str());
- }
- for(int i=0;i<MED_N_CELL_GEO_FIXED_CON;i++)
- {
- med_geometry_type curMedType=typmai[i];
- med_entity_type whichEntity;
- int curNbOfElemM=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);
- int curNbOfElemF=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);//limitation
- int curNbOfElem;
- MEDLoaderNS::dispatchElems(curNbOfElemM,curNbOfElemF,curNbOfElem,whichEntity);
- if(curNbOfElem>0)
- {
- int *connTab=new int[(curMedType%100)*curNbOfElem];
- int *fam=new int[curNbOfElem];
- MEDLoader::MEDConnOfOneElemType elem(typmai2[i],connTab,0,fam,curNbOfElem,-1);
- char *noms=new char[MED_SNAME_SIZE*curNbOfElem+1];
- med_bool withname=MED_FALSE,withnumber=MED_FALSE,withfam=MED_FALSE;
- int *globArr=new int[curNbOfElem];
- MEDmeshElementRd(fid,nommaa,numdt,numit,whichEntity,curMedType,MED_NODAL,MED_FULL_INTERLACE,connTab,&withname,noms,&withnumber,globArr,&withfam,fam);
- if(!withfam)
- std::fill(fam,fam+curNbOfElem,0);
- delete [] noms;
- //trying to read global numbering
- if(withnumber)
- elem.setGlobal(globArr);
- else
- delete [] globArr;
- //limitation manage withfam==false
- conn.push_back(elem);
- }
- }
- int curNbOfPolyElem;
- int curNbOfPolyElemM=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYGON,MED_INDEX_NODE,MED_NODAL,&changement,&transformation)-1;
- int curNbOfPolyElemF=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYGON,MED_INDEX_NODE,MED_NODAL,&changement,&transformation)-1;//limitation
- med_entity_type whichPolyEntity;
- MEDLoaderNS::dispatchElems(curNbOfPolyElemM,curNbOfPolyElemF,curNbOfPolyElem,whichPolyEntity);
- if(curNbOfPolyElem>0)
- {
- med_int arraySize=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYGON,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);
- int *index=new int[curNbOfPolyElem+1];
- int *locConn=new int[arraySize];
- int *fam=new int[curNbOfPolyElem];
- int *globArr=new int[curNbOfPolyElem];
- MEDLoader::MEDConnOfOneElemType elem(INTERP_KERNEL::NORM_POLYGON,locConn,index,fam,curNbOfPolyElem,arraySize);
- MEDmeshPolygonRd(fid,nommaa,numdt,numit,MED_CELL,MED_NODAL,index,locConn);
- if(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYGON,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
- {
- if(MEDmeshEntityFamilyNumberRd(fid,nommaa,numdt,numit,MED_CELL,MED_POLYGON,fam)!=0)
- std::fill(fam,fam+curNbOfPolyElem,0);
- }
- else
- std::fill(fam,fam+curNbOfPolyElem,0);
- if(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYGON,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
- {
- if(MEDmeshEntityNumberRd(fid,nommaa,numdt,numit,whichPolyEntity,MED_POLYGON,globArr)==0)
- elem.setGlobal(globArr);
- else
- delete [] globArr;
- }
- else
- delete [] globArr;
- conn.push_back(elem);
- }
- curNbOfPolyElem=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYHEDRON,MED_INDEX_FACE,MED_NODAL,&changement,&transformation)-1;
- if(curNbOfPolyElem>0)
- {
- med_int indexFaceLgth,connFaceLgth;
- indexFaceLgth=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYHEDRON,MED_INDEX_NODE,MED_NODAL,&changement,&transformation);
- connFaceLgth=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYHEDRON,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);
- INTERP_KERNEL::AutoPtr<int> index=new int[curNbOfPolyElem+1];
- INTERP_KERNEL::AutoPtr<int> indexFace=new int[indexFaceLgth];
- INTERP_KERNEL::AutoPtr<int> locConn=new int[connFaceLgth];
- int *fam=new int[curNbOfPolyElem];
- int *globArr=new int[curNbOfPolyElem];
- MEDmeshPolyhedronRd(fid,nommaa,numdt,numit,MED_CELL,MED_NODAL,index,indexFace,locConn);
- if(MEDmeshnEntity(fid,nommaa,numdt,numit,whichPolyEntity,MED_POLYHEDRON,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
- {
- if(MEDmeshEntityFamilyNumberRd(fid,nommaa,numdt,numit,whichPolyEntity,MED_POLYHEDRON,fam)!=0)
- std::fill(fam,fam+curNbOfPolyElem,0);
- }
- else
- std::fill(fam,fam+curNbOfPolyElem,0);
- int arraySize=connFaceLgth;
- for(int i=0;i<curNbOfPolyElem;i++)
- arraySize+=index[i+1]-index[i]-1;
- int *finalConn=new int[arraySize];
- int *finalIndex=new int[curNbOfPolyElem+1];
- finalIndex[0]=1;
- int *wFinalConn=finalConn;
- for(int i=0;i<curNbOfPolyElem;i++)
- {
- finalIndex[i+1]=finalIndex[i]+index[i+1]-index[i]-1+indexFace[index[i+1]-1]-indexFace[index[i]-1];
- wFinalConn=std::copy(locConn+indexFace[index[i]-1]-1,locConn+indexFace[index[i]]-1,wFinalConn);
- for(int j=index[i];j<index[i+1]-1;j++)
- {
- *wFinalConn++=0;
- wFinalConn=std::copy(locConn+indexFace[j]-1,locConn+indexFace[j+1]-1,wFinalConn);
- }
- }
- MEDLoader::MEDConnOfOneElemType elem(INTERP_KERNEL::NORM_POLYHED,finalConn,finalIndex,fam,curNbOfPolyElem,arraySize);
- if(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,MED_POLYHEDRON,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
- {
- if(MEDmeshEntityNumberRd(fid,nommaa,numdt,numit,whichPolyEntity,MED_POLYHEDRON,globArr)==0)
- elem.setGlobal(globArr);
- else
- delete [] globArr;
- }
- else
- delete [] globArr;
- conn.push_back(elem);
- }
-}
-
-/// @cond INTERNAL
-
-namespace MEDLoaderNS
-{
- template<class T>
- unsigned calculateHighestMeshDim(const std::list<T>& conn)
- {
- unsigned ret=0;
- for(typename std::list<T>::const_iterator iter=conn.begin();iter!=conn.end();iter++)
- {
- unsigned curDim=INTERP_KERNEL::CellModel::GetCellModel((*iter).getType()).getDimension();
- if(ret<curDim)
- ret=curDim;
- }
- return ret;
- }
-
- template<class T>
- void keepSpecifiedMeshDim(typename std::list<T>& conn, unsigned meshDim)
- {
- for(typename std::list<T>::iterator iter=conn.begin();iter!=conn.end();)
- {
- unsigned curDim=INTERP_KERNEL::CellModel::GetCellModel((*iter).getType()).getDimension();
- if(curDim!=meshDim)
- {
- (*iter).releaseArray();
- iter=conn.erase(iter);
- }
- else
- iter++;
- }
- }
-
- template<class T>
- void keepTypes(typename std::list<T>& conn, const std::vector<INTERP_KERNEL::NormalizedCellType>& typesToKeep)
- {
- if(!typesToKeep.empty())
- {
- for(typename std::list<T>::iterator iter=conn.begin();iter!=conn.end();)
- {
- INTERP_KERNEL::NormalizedCellType curType=(*iter).getType();
- if(std::find(typesToKeep.begin(),typesToKeep.end(),curType)==typesToKeep.end())
- {
- (*iter).releaseArray();
- iter=conn.erase(iter);
- }
- else
- iter++;
- }
- }
- }
-}
-
-class FieldPerTypeAccumulator
-{
-public:
- int operator()(int res, const MEDLoader::MEDFieldDoublePerCellType& elt) { return res+elt.getNbOfTuple(); }
-};
-
-ParaMEDMEM::DataArrayDouble *MEDLoaderNS::buildArrayFromRawData(const std::list<MEDLoader::MEDFieldDoublePerCellType>& fieldPerType,
- const std::vector<std::string>& infos)
-{
- ParaMEDMEM::DataArrayDouble *ret=ParaMEDMEM::DataArrayDouble::New();
- int totalNbOfTuple=std::accumulate(fieldPerType.begin(),fieldPerType.end(),0,FieldPerTypeAccumulator());
- int nbOfComp=(*fieldPerType.begin()).getNbComp();
- double *ptr=new double[nbOfComp*totalNbOfTuple];
- ret->useArray(ptr,true,ParaMEDMEM::CPP_DEALLOC,totalNbOfTuple,nbOfComp);
- std::for_each(fieldPerType.begin(),fieldPerType.end(),FieldPerTypeCopier(ptr));
- for(int i=0;i<nbOfComp;i++)
- ret->setInfoOnComponent(i,infos[i].c_str());
- return ret;
-}
-
-class PolyCounterForFams
-{
-public:
- PolyCounterForFams(int id, const int *index):_id(id),_index(index),_count(0),_sigma(0) { }
- void operator()(int val) { if(val==_id) _sigma+=_index[_count+1]-_index[_count]; _count++; }
- int getSigma() const { return _sigma; }
-private:
- int _id;
- const int *_index;
- int _count;
- int _sigma;
-};
-
-/*!
- * This method fills unstructured connectivity using basic MED file format 'medConnFrmt'.
- * If in each elements of 'medConnFrmt' a renumbering cell array is found the aggregate array 'cellRenum' is returned.
- */
-void MEDLoaderNS::tradMEDFileCoreFrmt2MEDCouplingUMesh(const std::list<MEDLoader::MEDConnOfOneElemType>& medConnFrmt,
- const std::vector<int>& familiesToKeep,
- DataArrayInt* &conn,
- DataArrayInt* &connIndex,
- int *&cellRenum)
-{
- bool keepAll=familiesToKeep.empty();
- if(medConnFrmt.empty())
- {
- conn=0;
- connIndex=0;
- cellRenum=0;
- return ;
- }
- std::list<MEDLoader::MEDConnOfOneElemType>::const_iterator iter=medConnFrmt.begin();
- int totalNbOfCells=0;
- int totalNbOfMedConn=0;
- bool renumber=true;
- cellRenum=0;
- for(;iter!=medConnFrmt.end();iter++)
- {
- if((*iter).getGlobal()==0)
- renumber=false;
- const INTERP_KERNEL::CellModel& cellMod=INTERP_KERNEL::CellModel::GetCellModel((*iter).getType());
- if(keepAll)
- totalNbOfCells+=(*iter).getLength();
- else
- for(std::vector<int>::const_iterator iter2=familiesToKeep.begin();iter2!=familiesToKeep.end();iter2++)
- totalNbOfCells+=std::count((*iter).getFam(),(*iter).getFam()+(*iter).getLength(),*iter2);
- if(!cellMod.isDynamic())
- if(keepAll)
- totalNbOfMedConn+=(*iter).getLength()*cellMod.getNumberOfNodes();
- else
- for(std::vector<int>::const_iterator iter2=familiesToKeep.begin();iter2!=familiesToKeep.end();iter2++)
- totalNbOfMedConn+=std::count((*iter).getFam(),(*iter).getFam()+(*iter).getLength(),*iter2)*cellMod.getNumberOfNodes();
- else
- if(keepAll)
- totalNbOfMedConn+=(*iter).getConnLength();
- else
- for(std::vector<int>::const_iterator iter2=familiesToKeep.begin();iter2!=familiesToKeep.end();iter2++)
- {
- PolyCounterForFams res=std::for_each((*iter).getFam(),(*iter).getFam()+(*iter).getLength(),PolyCounterForFams(*iter2,(*iter).getIndex()));
- totalNbOfMedConn+=res.getSigma();
- }
- }
- connIndex=DataArrayInt::New();
- conn=DataArrayInt::New();
- connIndex->alloc(totalNbOfCells+1,1);
- int *connIdxPtr=connIndex->getPointer();
- int connFillId=0;
- conn->alloc(totalNbOfMedConn+totalNbOfCells,1);
- int *connPtr=conn->getPointer();
- if(renumber)
- cellRenum=new int[totalNbOfCells];
- int *renumW=cellRenum;
- for(iter=medConnFrmt.begin();iter!=medConnFrmt.end();iter++)
- {
- INTERP_KERNEL::NormalizedCellType type=(*iter).getType();
- const int *sourceConn=(*iter).getArray();
- const int *sourceIndex=(*iter).getIndex();
- const int *globalNum=(*iter).getGlobal();
- const INTERP_KERNEL::CellModel& cellMod=INTERP_KERNEL::CellModel::GetCellModel(type);
- int nbOfCellsInCurType;
- int nbOfNodesIn1Cell=cellMod.getNumberOfNodes();
- nbOfCellsInCurType=(*iter).getLength();
- bool isDyn=cellMod.isDynamic();
- int *tmpConnPtr;
- for(int i=0;i<nbOfCellsInCurType;i++)
- {
- if(keepAll)
- {//duplication of next 3 lines needed.
- *connIdxPtr=connFillId;
- *connPtr++=type;
- if(renumber)
- *renumW++=globalNum[i];
- if(!isDyn)
- tmpConnPtr=std::transform(sourceConn,sourceConn+nbOfNodesIn1Cell,connPtr,std::bind2nd(std::minus<int>(),1));
- else
- tmpConnPtr=std::transform(sourceConn,sourceConn+sourceIndex[i+1]-sourceIndex[i],connPtr,std::bind2nd(std::minus<int>(),1));
- connIdxPtr++;
- nbOfNodesIn1Cell=tmpConnPtr-connPtr;
- connFillId+=nbOfNodesIn1Cell+1;
- connPtr=tmpConnPtr;
- }
- else if(std::find(familiesToKeep.begin(),familiesToKeep.end(),(*iter).getFam()[i])!=familiesToKeep.end())
- {//duplication of next 3 lines needed.
- *connIdxPtr=connFillId;
- *connPtr++=type;
- if(renumber)
- *renumW++=globalNum[i];
- if(!isDyn)
- tmpConnPtr=std::transform(sourceConn,sourceConn+nbOfNodesIn1Cell,connPtr,std::bind2nd(std::minus<int>(),1));
- else//The duplication of code is motivated by the line underneath.
- tmpConnPtr=std::transform((*iter).getArray()+sourceIndex[i]-1,(*iter).getArray()+sourceIndex[i+1]-1,connPtr,std::bind2nd(std::minus<int>(),1));
- connIdxPtr++;
- nbOfNodesIn1Cell=tmpConnPtr-connPtr;
- connFillId+=nbOfNodesIn1Cell+1;
- connPtr=tmpConnPtr;
- }
- sourceConn+=nbOfNodesIn1Cell;
- }
- *connIdxPtr=connFillId;
- }
-}
-
-namespace MEDLoaderNS
-{
- template<class T>
- void releaseMEDFileCoreFrmt(typename std::list<T>& medConnFrmt)
- {
- for(typename std::list<T>::iterator iter=medConnFrmt.begin();iter!=medConnFrmt.end();iter++)
- (*iter).releaseArray();
- medConnFrmt.clear();
- }
-}
-
-/*!
- * This method builds a sub set of connectivity for a given type 'type'. \b WARNING connV,connVIndex and familiesV must have same size !
- * @param connV input containing connectivity with MEDCoupling format.
- * @param connVIndex input containing connectivity index in MEDCoupling format.
- * @param familiesV input that may be equal to 0. This specifies an array specifying cell family foreach cell.
- * @param type input specifying which cell types will be extracted in conn4MEDFile.
- * @param conn4MEDFile output containing the connectivity directly understandable by MEDFile; conn4MEDFile has to be empty before this method called.
- * @param connIndex4MEDFile output containing index connectivity understandable by MEDFile; only used by polygons and polyhedrons (it is face nodal connec).
- * @param connIndexRk24MEDFile output containing index of rank 2 understandable by MEDFile; only used by polyhedrons.
- * @param fam4MEDFile output containing family number of cells whose type is 'type'. This output is updated only if 'families' is different than 0.
- * @return nb of elements extracted.
- */
-int MEDLoaderNS::buildMEDSubConnectivityOfOneTypeStaticTypes(const std::vector<const DataArrayInt *>& connV, const std::vector<const DataArrayInt *>& connVIndex, const std::vector<const DataArrayInt *>& familiesV,
- INTERP_KERNEL::NormalizedCellType type, std::vector<int>& conn4MEDFile, std::vector<int>& fam4MEDFile, std::vector<int>& renumber)
-{
- int ret=0;
- int nbOfMeshes=connV.size();
- int renumOffset=0;
- for(int i=0;i<nbOfMeshes;i++)
- {
- const DataArrayInt *conn=connV[i];
- const DataArrayInt *connIndex=connVIndex[i];
- const DataArrayInt *families=familiesV[i];
- int nbOfElem=connIndex->getNbOfElems()-1;
- const int *connPtr=conn->getConstPointer();
- const int *connIdxPtr=connIndex->getConstPointer();
- const int *famPtr=0;
- if(families)
- famPtr=families->getConstPointer();
- for(int ii=0;ii<nbOfElem;ii++)
- {
- int delta=connIdxPtr[1]-connIdxPtr[0];
- if(*connPtr==type)
- {
- conn4MEDFile.insert(conn4MEDFile.end(),connPtr+1,connPtr+delta);
- if(families)
- fam4MEDFile.push_back(famPtr[ii]);
- renumber.push_back(ii+1+renumOffset);
- ret++;
- }
- connIdxPtr++;
- connPtr+=delta;
- }
- renumOffset+=nbOfElem;
- }
- std::transform(conn4MEDFile.begin(),conn4MEDFile.end(),conn4MEDFile.begin(),std::bind2nd(std::plus<int>(),1));
- return ret;
-}
-
-int MEDLoaderNS::buildMEDSubConnectivityOfOneTypesPolyg(const std::vector<const DataArrayInt *>&connV, const std::vector<const DataArrayInt *>& connVIndex, const std::vector<const DataArrayInt *>& familiesV,
- std::vector<int>& conn4MEDFile, std::vector<int>& connIndex4MEDFile, std::vector<int>& fam4MEDFile, std::vector<int>& renumber)
-{
- int ret=0;
- int nbOfMeshes=connV.size();
- connIndex4MEDFile.push_back(1);
- int renumOffset=0;
- for(int i=0;i<nbOfMeshes;i++)
- {
- const DataArrayInt *conn=connV[i];
- const DataArrayInt *connIndex=connVIndex[i];
- const DataArrayInt *families=familiesV[i];
- int nbOfElem=connIndex->getNbOfElems()-1;
- const int *connPtr=conn->getConstPointer();
- const int *connIdxPtr=connIndex->getConstPointer();
- const int *famPtr=0;
- if(families)
- famPtr=families->getConstPointer();
- for(int ii=0;ii<nbOfElem;ii++)
- {
- int delta=connIdxPtr[1]-connIdxPtr[0];
- if(*connPtr==INTERP_KERNEL::NORM_POLYGON)
- {
- conn4MEDFile.insert(conn4MEDFile.end(),connPtr+1,connPtr+delta);
- connIndex4MEDFile.push_back(connIndex4MEDFile.back()+delta-1);
- if(families)
- fam4MEDFile.push_back(famPtr[ii]);
- renumber.push_back(ii+1+renumOffset);
- ret++;
- }
- connIdxPtr++;
- connPtr+=delta;
- }
- renumOffset+=nbOfElem;
- }
- std::transform(conn4MEDFile.begin(),conn4MEDFile.end(),conn4MEDFile.begin(),std::bind2nd(std::plus<int>(),1));
- return ret;
-}
-
-int MEDLoaderNS::buildMEDSubConnectivityOfOneTypesPolyh(const std::vector<const DataArrayInt *>& connV, const std::vector<const DataArrayInt *>& connVIndex, const std::vector<const DataArrayInt *>& familiesV,
- std::vector<int>& conn4MEDFile, std::vector<int>& connIndex4MEDFile, std::vector<int>& connIndexRk24MEDFile,
- std::vector<int>& fam4MEDFile, std::vector<int>& renumber)
-{
- int ret=0;
- int nbOfMeshes=connV.size();
- connIndexRk24MEDFile.push_back(1);
- connIndex4MEDFile.push_back(1);
- int renumOffset=0;
- for(int i=0;i<nbOfMeshes;i++)
- {
- const DataArrayInt *conn=connV[i];
- const DataArrayInt *connIndex=connVIndex[i];
- const DataArrayInt *families=familiesV[i];
- int nbOfElem=connIndex->getNbOfElems()-1;
- const int *connPtr=conn->getConstPointer();
- const int *connIdxPtr=connIndex->getConstPointer();
- const int *famPtr=0;
- if(families)
- famPtr=families->getConstPointer();
- for(int ii=0;ii<nbOfElem;ii++)
- {
- int delta=connIdxPtr[1]-connIdxPtr[0];
- if(*connPtr==INTERP_KERNEL::NORM_POLYHED)
- {
- int nbOfFacesOfPolyh=std::count(connPtr+1,connPtr+delta,-1)+1;
- const int *work=connPtr+1;
- while(work!=connPtr+delta)
- {
- const int *end=std::find(work,connPtr+delta,-1);
- conn4MEDFile.insert(conn4MEDFile.end(),work,end);
- connIndex4MEDFile.push_back(connIndex4MEDFile.back()+std::distance(work,end));
- if(end==connPtr+delta)
- work=connPtr+delta;
- else
- work=end+1;
- }
- connIndexRk24MEDFile.push_back(connIndexRk24MEDFile.back()+nbOfFacesOfPolyh);
- if(families)
- fam4MEDFile.push_back(famPtr[ii]);
- renumber.push_back(ii+1+renumOffset);
- ret++;
- }
- connIdxPtr++;
- connPtr+=delta;
- }
- renumOffset+=nbOfElem;
- }
- std::transform(conn4MEDFile.begin(),conn4MEDFile.end(),conn4MEDFile.begin(),std::bind2nd(std::plus<int>(),1));
- return ret;
-}
-
-/*!
- * This method builds a sub set of connectivity for a given type 'type'.
- * @param conn input containing connectivity with MEDCoupling format.
- * @param connIndex input containing connectivity index in MEDCoupling format.
- * @param families input containing, if any, the family number of each cells
- * @param type input specifying which cell types will be extracted in conn4MEDFile.
- * @param conn4MEDFile output containing the connectivity directly understandable by MEDFile; conn4MEDFile has to be empty before this method called.
- * @param connIndex4MEDFile output containing index connectivity understandable by MEDFile; only used by polygons and polyhedrons (it is face nodal connec).
- * @param connIndexRk24MEDFile output containing index of rank 2 understandable by MEDFile; only used by polyhedrons.
- * @param fam4MEDFile output containing families id of cells whose type is 'type'.
- * @return nb of elements extracted.
- */
-int MEDLoaderNS::buildMEDSubConnectivityOfOneType(const std::vector<const DataArrayInt *>& conn, const std::vector<const DataArrayInt *>& connIndex, const std::vector<const DataArrayInt *>& families,
- INTERP_KERNEL::NormalizedCellType type, std::vector<int>& conn4MEDFile,
- std::vector<int>& connIndex4MEDFile, std::vector<int>& connIndexRk24MEDFile, std::vector<int>& fam4MEDFile, std::vector<int>& renumber)
-{
-
- const INTERP_KERNEL::CellModel& cellMod=INTERP_KERNEL::CellModel::GetCellModel(type);
- if(!cellMod.isDynamic())
- return buildMEDSubConnectivityOfOneTypeStaticTypes(conn,connIndex,families,type,conn4MEDFile,fam4MEDFile,renumber);
- else
- {
- if(type==INTERP_KERNEL::NORM_POLYGON)
- return buildMEDSubConnectivityOfOneTypesPolyg(conn,connIndex,families,conn4MEDFile,connIndex4MEDFile,fam4MEDFile,renumber);
- else
- return buildMEDSubConnectivityOfOneTypesPolyh(conn,connIndex,families,conn4MEDFile,connIndex4MEDFile,connIndexRk24MEDFile,fam4MEDFile,renumber);
- }
-}
-
-/*!
- * @param ids is a in vector containing families ids whose cells have to be kept. If empty all cells are kept.
- * @param typesToKeep is a in vector that indicates which types to keep after dimension filtering.
- * @param meshDimExtract out parameter that gives the mesh dimension.
- * @param cellRenum out parameter that specifies the renumbering (if !=0) of cells in file.
- */
-MEDCouplingUMesh *MEDLoaderNS::readUMeshFromFileLev1(const char *fileName, const char *meshName, int meshDimRelToMax, const std::vector<int>& ids,
- const std::vector<INTERP_KERNEL::NormalizedCellType>& typesToKeep, unsigned& meshDimExtract, int *&cellRenum) throw(INTERP_KERNEL::Exception)
-{
- if(meshDimRelToMax>0)
- throw INTERP_KERNEL::Exception("meshDimRelToMax must be <=0 !");
- //Extraction data from MED file.
- med_idt fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
- std::string trueMeshName;
- med_int mid=getIdFromMeshName(fid,meshName,trueMeshName);
- DataArrayDouble *coords=0;
- std::list<MEDLoader::MEDConnOfOneElemType> conn;
- std::string descr;
- readUMeshDataInMedFile(fid,mid,coords,conn,descr);
- meshDimExtract=MEDLoaderNS::calculateHighestMeshDim<MEDLoader::MEDConnOfOneElemType>(conn);
- meshDimExtract=meshDimExtract+meshDimRelToMax;
- MEDLoaderNS::keepSpecifiedMeshDim<MEDLoader::MEDConnOfOneElemType>(conn,meshDimExtract);
- MEDLoaderNS::keepTypes<MEDLoader::MEDConnOfOneElemType>(conn,typesToKeep);
- MEDfileClose(fid);
- //Put data in returned data structure.
- MEDCouplingUMesh *ret=MEDCouplingUMesh::New();
- ret->setName(trueMeshName.c_str());
- ret->setDescription(descr.c_str());
- ret->setMeshDimension(meshDimExtract);
- //
- ret->setCoords(coords);
- coords->decrRef();
- //
- DataArrayInt *connArr,*connIndexArr;
- tradMEDFileCoreFrmt2MEDCouplingUMesh(conn,ids,connArr,connIndexArr,cellRenum);
- ret->setConnectivity(connArr,connIndexArr);
- //clean-up
- if(connArr)
- connArr->decrRef();
- if(connIndexArr)
- connIndexArr->decrRef();
- releaseMEDFileCoreFrmt<MEDLoader::MEDConnOfOneElemType>(conn);
- return ret;
-}
-
-ParaMEDMEM::MEDCouplingFieldDouble *MEDLoaderNS::readFieldDoubleLev2(const char *fileName, ParaMEDMEM::TypeOfField typeOfOutField, unsigned meshDim, const int *cellRenum, const ParaMEDMEM::MEDCouplingUMesh *mesh,
- const std::vector<std::string>& infos, const char *fieldName, int iteration, int order, double time,
- std::list<MEDLoader::MEDFieldDoublePerCellType>& fieldPerCellType) throw(INTERP_KERNEL::Exception)
-{
- if(typeOfOutField==ON_CELLS || typeOfOutField==ON_GAUSS_PT || typeOfOutField==ON_GAUSS_NE)
- MEDLoaderNS::keepSpecifiedMeshDim<MEDLoader::MEDFieldDoublePerCellType>(fieldPerCellType,meshDim);
- if(fieldPerCellType.empty())
- {
- std::ostringstream oss; oss << "Error on reading file \"" << fileName << "\" meshName=\"" << mesh->getName();
- oss << std::endl << "FieldName=\"" << fieldName << "\" (iteration=" << iteration << ",order=" << order << ")" << std::endl;
- if(typeOfOutField==ON_CELLS || typeOfOutField==ON_GAUSS_PT || typeOfOutField==ON_GAUSS_NE)
- oss << "Request for cell field, maybe it is an ON_NODES field ?";
- else
- oss << "Request for a node field, maybe it is an ON_CELLS field ?";
- throw INTERP_KERNEL::Exception(oss.str().c_str());
- }
- //for profiles
- ParaMEDMEM::MEDCouplingUMesh *newMesh=0;
- std::string mName(mesh->getName());
- for(std::list<MEDLoader::MEDFieldDoublePerCellType>::const_iterator iter=fieldPerCellType.begin();iter!=fieldPerCellType.end();iter++)
- {
- const std::vector<int>& cellIds=(*iter).getCellIdPerType();
- if(!cellIds.empty())
- {
- std::vector<int> ci(cellIds.size());
- std::transform(cellIds.begin(),cellIds.end(),ci.begin(),std::bind2nd(std::plus<int>(),-1));
- ParaMEDMEM::MEDCouplingUMesh *mesh2=0;
- if(typeOfOutField==ON_CELLS)
- {
- if(newMesh)
- mesh2=newMesh->keepSpecifiedCells((*iter).getType(),&ci[0],&ci[0]+ci.size());
- else
- mesh2=mesh->keepSpecifiedCells((*iter).getType(),&ci[0],&ci[0]+ci.size());
- }
- else if(typeOfOutField==ON_NODES)
- {
- DataArrayInt *da=0,*da2=0;
- if(newMesh)
- {
- if((int)ci.size()!=newMesh->getNumberOfNodes())
- {
- da=newMesh->getCellIdsFullyIncludedInNodeIds(&ci[0],&ci[ci.size()]);
- mesh2=dynamic_cast<MEDCouplingUMesh *>(newMesh->buildPartAndReduceNodes(da->getConstPointer(),da->getConstPointer()+da->getNbOfElems(),da2));
- }
- }
- else
- {
- if((int)ci.size()!=mesh->getNumberOfNodes())
- {
- da=mesh->getCellIdsFullyIncludedInNodeIds(&ci[0],&ci[ci.size()]);
- mesh2=dynamic_cast<MEDCouplingUMesh *>(mesh->buildPartAndReduceNodes(da->getConstPointer(),da->getConstPointer()+da->getNbOfElems(),da2));
- //
- int nnodes=mesh2->getNumberOfNodes();
- MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da3=DataArrayInt::New();
- const int *da2Ptr=da2->getConstPointer();
- da3->alloc(nnodes,1);
- int *da3Ptr=da3->getPointer();
- for(int i=0;i<(int)ci.size();i++)
- {
- int val=da2Ptr[ci[i]];
- if(val!=-1)
- da3Ptr[val]=i;
- }
- mesh2->renumberNodes(da3->getConstPointer(),nnodes);
- }
- else
- {
- mesh2=mesh->clone(true);
- da=DataArrayInt::New();
- da->alloc((int)ci.size(),1);
- std::copy(ci.begin(),ci.end(),da->getPointer());
- da2=da->invertArrayO2N2N2O(ci.size());
- mesh2->renumberNodes(da2->getConstPointer(),(int)ci.size());
- }
- }
- if(da)
- da->decrRef();
- if(da2)
- da2->decrRef();