1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #include "MEDLoader.hxx"
22 #include "MEDLoaderBase.hxx"
23 #include "MEDFileUtilities.hxx"
24 #include "MEDFileMesh.hxx"
25 #include "MEDFileField.hxx"
26 #include "CellModel.hxx"
27 #include "MEDCouplingUMesh.hxx"
28 #include "MEDCouplingMemArray.hxx"
29 #include "MEDCouplingFieldDouble.hxx"
30 #include "MEDCouplingGaussLocalization.hxx"
31 #include "MEDCouplingAutoRefCountObjectPtr.hxx"
33 #include "InterpKernelAutoPtr.hxx"
46 med_geometry_type typmai[MED_N_CELL_FIXED_GEO] = { MED_POINT1,
69 med_geometry_type typmainoeud[1] = { MED_NONE };
71 INTERP_KERNEL::NormalizedCellType typmai2[MED_N_CELL_FIXED_GEO] = { INTERP_KERNEL::NORM_POINT1,
72 INTERP_KERNEL::NORM_SEG2,
73 INTERP_KERNEL::NORM_SEG3,
74 INTERP_KERNEL::NORM_SEG4,
75 INTERP_KERNEL::NORM_TRI3,
76 INTERP_KERNEL::NORM_QUAD4,
77 INTERP_KERNEL::NORM_TRI6,
78 INTERP_KERNEL::NORM_TRI7,
79 INTERP_KERNEL::NORM_QUAD8,
80 INTERP_KERNEL::NORM_QUAD9,
81 INTERP_KERNEL::NORM_TETRA4,
82 INTERP_KERNEL::NORM_PYRA5,
83 INTERP_KERNEL::NORM_PENTA6,
84 INTERP_KERNEL::NORM_HEXA8,
85 INTERP_KERNEL::NORM_HEXGP12,
86 INTERP_KERNEL::NORM_TETRA10,
87 INTERP_KERNEL::NORM_PYRA13,
88 INTERP_KERNEL::NORM_PENTA15,
89 INTERP_KERNEL::NORM_HEXA20,
90 INTERP_KERNEL::NORM_HEXA27,
91 INTERP_KERNEL::NORM_POLYGON,
92 INTERP_KERNEL::NORM_POLYHED };
94 med_geometry_type typmai3[34] = { MED_POINT1,//0
130 double MEDLoader::_EPS_FOR_NODE_COMP=1.e-12;
132 int MEDLoader::_COMP_FOR_CELL=0;
134 int MEDLoader::_TOO_LONG_STR=0;
136 using namespace ParaMEDMEM;
140 namespace MEDLoaderNS
142 int readUMeshDimFromFile(const char *fileName, const char *meshName, std::vector<int>& possibilities);
143 void dispatchElems(int nbOfElemCell, int nbOfElemFace, int& nbOfElem, med_entity_type& whichEntity);
144 void writeFieldWithoutReadingAndMappingOfMeshInFile(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, bool writeFromScratch);
145 med_int getIdFromMeshName(med_idt fid, const char *meshName, std::string& trueMeshName) throw(INTERP_KERNEL::Exception);
146 std::vector<std::string> getMeshNamesFid(med_idt fid);
155 * This method returns a first quick overview of mesh with name 'meshName' into the file 'fileName'.
156 * @param possibilities the relativeToMeshDim authorized to returned maxdim. This vector is systematically cleared at the begin of this method.
157 * @return the maximal mesh dimension of specified mesh. If nothing found -1 is returned.
159 int MEDLoaderNS::readUMeshDimFromFile(const char *fileName, const char *meshName, std::vector<int>& possibilities)
161 possibilities.clear();
162 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
165 char nommaa[MED_NAME_SIZE+1];
166 char maillage_description[MED_COMMENT_SIZE+1];
167 med_mesh_type type_maillage;
169 std::string trueMeshName;
170 med_int meshId=getIdFromMeshName(fid,meshName,trueMeshName);
171 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
172 med_sorting_type sortingType;
174 med_axis_type axisType;
175 int naxis=MEDmeshnAxis(fid,meshId);
176 INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
177 INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
178 MEDmeshInfo(fid,meshId,nommaa,&Sdim,&Mdim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit);
182 throw INTERP_KERNEL::Exception("multisteps on mesh not managed yet !");
186 MEDmeshComputationStepInfo(fid,nommaa,1,&numdt,&numit,&dt);
188 for(int i=0;i<MED_N_CELL_GEO_FIXED_CON;i++)
190 med_geometry_type curMedType=typmai[i];
191 med_bool changement,transformation;
192 int curNbOfElemM=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);
193 int curNbOfElemF=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);//limitation
195 med_entity_type whichEntity;
196 MEDLoaderNS::dispatchElems(curNbOfElemM,curNbOfElemF,curNbOfElem,whichEntity);
199 INTERP_KERNEL::NormalizedCellType type=typmai2[i];
200 int curDim=(int)INTERP_KERNEL::CellModel::GetCellModel(type).getDimension();
207 for(std::set<int>::const_reverse_iterator it=poss.rbegin();it!=poss.rend();it++)
208 possibilities.push_back(*it-ret);
215 med_int MEDLoaderNS::getIdFromMeshName(med_idt fid, const char *meshName, std::string& trueMeshName) throw(INTERP_KERNEL::Exception)
219 std::vector<std::string> meshes=getMeshNamesFid(fid);
221 throw INTERP_KERNEL::Exception("No mesh in file");
222 trueMeshName=meshes[0];
225 std::string meshNameStr(meshName);
226 std::vector<std::string> meshes=getMeshNamesFid(fid);
228 throw INTERP_KERNEL::Exception("No mesh in file");
229 std::vector<std::string>::iterator iter=std::find(meshes.begin(),meshes.end(),meshNameStr);
230 if(iter==meshes.end())
232 std::ostringstream os2;
233 os2 << "MeshName '" << meshName << "' not in file : meshes available : ";
234 std::copy(meshes.begin(),meshes.end(),std::ostream_iterator<std::string>(os2," "));
235 throw INTERP_KERNEL::Exception(os2.str().c_str());
237 trueMeshName=meshName;
238 return iter-meshes.begin()+1;
241 std::vector<std::string> MEDLoaderNS::getMeshNamesFid(med_idt fid)
243 med_mesh_type type_maillage;
244 char maillage_description[MED_COMMENT_SIZE+1];
245 char dtunit[MED_COMMENT_SIZE+1];
248 char nommaa[MED_NAME_SIZE+1];
249 med_axis_type axistype;
250 med_sorting_type stype;
251 med_int n=MEDnMesh(fid);
252 std::vector<std::string> ret(n);
255 int naxis=MEDmeshnAxis(fid,i+1);
256 INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
257 INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
259 MEDmeshInfo(fid,i+1,nommaa,&space_dim,&mesh_dim,&type_maillage,maillage_description,dtunit,&stype,&nstep,&axistype,axisname,axisunit);
260 std::string cur=MEDLoaderBase::buildStringFromFortran(nommaa,sizeof(nommaa));
267 * This methods allows to merger all entities and to considerate only cell types.
269 void MEDLoaderNS::dispatchElems(int nbOfElemCell, int nbOfElemFace, int& nbOfElem, med_entity_type& whichEntity)
271 if(nbOfElemCell>=nbOfElemFace)
273 whichEntity=MED_CELL;
274 nbOfElem=nbOfElemCell;
278 whichEntity=MED_CELL;
279 nbOfElem=nbOfElemFace;
285 bool MEDLoader::HasXDR()
295 * 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.
297 void MEDLoader::SetEpsilonForNodeComp(double val)
299 _EPS_FOR_NODE_COMP=val;
303 * 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.
305 void MEDLoader::SetCompPolicyForCell(int val)
311 * This method set the behaviour of MEDLoader when a too long string is seen in datastructure before copy it in MED file.
312 * By default (0) an exception is thrown. If equal to 1 a warning is emitted in std_err but no exception is thrown.
314 void MEDLoader::SetTooLongStrPolicy(int val)
320 * Given a 'fileName' and a 'meshName' this method returns global information concerning this mesh.
321 * It returns, in this order :
322 * - number of cells sorted by dimension and by geometry type. The first entry in the vector is the maximal dimension, the 2nd in the vector is the maximal dimension-1...
323 * - the mesh dimension
324 * - the space dimension
325 * - the number of nodes
327 std::vector< std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > > MEDLoader::GetUMeshGlobalInfo(const char *fileName, const char *meshName, int &meshDim, int& spaceDim, int& numberOfNodes)
329 CheckFileForRead(fileName);
330 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
332 char nommaa[MED_NAME_SIZE+1];
333 char maillage_description[MED_COMMENT_SIZE+1];
334 med_mesh_type type_maillage;
335 std::string trueMeshName;
336 med_int meshId=MEDLoaderNS::getIdFromMeshName(fid,meshName,trueMeshName);
337 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
338 med_sorting_type sortingType;
340 med_axis_type axisType;
341 int naxis=MEDmeshnAxis(fid,meshId);
342 INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
343 INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
344 MEDmeshInfo(fid,meshId,nommaa,&spaceDim,&meshDim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit);
345 if(type_maillage!=MED_UNSTRUCTURED_MESH)
347 std::ostringstream oss; oss << "MEDLoader::GetUMeshGlobalInfo : Mesh \""<< meshName << "\" in file \"" << fileName;
348 oss << "\" exists but it is not an unstructured mesh ! This method is not relevant for mesh types that are not unstructured !";
349 throw INTERP_KERNEL::Exception(oss.str().c_str());
353 throw INTERP_KERNEL::Exception("MEDLoader::GetUMeshGlobalInfo : multisteps on mesh not managed !");
356 MEDmeshComputationStepInfo(fid,nommaa,1,&numdt,&numit,&dt);
358 std::vector<int> dims;
359 std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > geoTypes;
360 med_bool changement,transformation;
361 for(int i=0;i<MED_N_CELL_FIXED_GEO;i++)
363 med_geometry_type curMedType=typmai[i];
364 int curNbOfElemM=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation);
367 INTERP_KERNEL::NormalizedCellType typp=typmai2[i];
368 int mdimCell=INTERP_KERNEL::CellModel::GetCellModel(typp).getDimension();
369 dims.push_back(mdimCell);
370 geoTypes.push_back(std::pair<INTERP_KERNEL::NormalizedCellType,int>(typp,curNbOfElemM));
373 int maxLev=*std::max_element(dims.begin(),dims.end());
374 int lowLev=*std::min_element(dims.begin(),dims.end());
375 int nbOfLevels=maxLev-lowLev+1;
376 std::vector< std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > > ret(nbOfLevels);
377 for(std::size_t i=0;i<dims.size();i++)
379 ret[maxLev-dims[i]].push_back(geoTypes[i]);
381 numberOfNodes=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation);
385 void MEDLoader::CheckFileForRead(const char *fileName)
387 MEDFileUtilities::CheckFileForRead(fileName);
390 std::vector<std::string> MEDLoader::GetMeshNames(const char *fileName)
392 CheckFileForRead(fileName);
393 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
394 std::vector<std::string> ret=MEDLoaderNS::getMeshNamesFid(fid);
398 std::vector< std::pair<std::string,std::string> > MEDLoader::GetComponentsNamesOfField(const char *fileName, const char *fieldName)
400 CheckFileForRead(fileName);
401 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
402 med_int nbFields=MEDnField(fid);
403 std::vector<std::string> fields(nbFields);
404 med_field_type typcha;
405 for(int i=0;i<nbFields;i++)
407 med_int ncomp=MEDfieldnComponent(fid,i+1);
408 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
409 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
410 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
413 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
414 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
415 MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
416 std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
417 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
418 if(curFieldName==fieldName)
420 std::vector< std::pair<std::string,std::string> > ret(ncomp);
421 for(int j=0;j<ncomp;j++)
422 ret[j]=std::pair<std::string,std::string>(MEDLoaderBase::buildStringFromFortran(((char *)comp)+j*MED_SNAME_SIZE,MED_SNAME_SIZE),
423 MEDLoaderBase::buildStringFromFortran(((char *)unit)+j*MED_SNAME_SIZE,MED_SNAME_SIZE));
426 fields[i]=curFieldName;
428 std::ostringstream oss; oss << "MEDLoader::GetComponentsNamesOfField : no such field \"" << fieldName << "\" in file \"" << fileName << "\" !" << std::endl;
429 oss << "Possible field names are : " << std::endl;
430 std::copy(fields.begin(),fields.end(),std::ostream_iterator<std::string>(oss," "));
431 throw INTERP_KERNEL::Exception(oss.str().c_str());
434 std::vector<std::string> MEDLoader::GetMeshNamesOnField(const char *fileName, const char *fieldName)
436 CheckFileForRead(fileName);
437 std::vector<std::string> ret;
439 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
440 med_int nbFields=MEDnField(fid);
442 med_field_type typcha;
443 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
444 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
447 for(int i=0;i<nbFields;i++)
449 med_int ncomp=MEDfieldnComponent(fid,i+1);
450 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
451 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
453 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
454 MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
455 std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
456 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
457 if(curFieldName==fieldName)
458 ret.push_back(meshName);
463 std::vector<std::string> MEDLoader::GetMeshFamiliesNames(const char *fileName, const char *meshName)
465 CheckFileForRead(fileName);
466 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
467 med_int nfam=MEDnFamily(fid,meshName);
468 std::vector<std::string> ret(nfam);
469 char nomfam[MED_NAME_SIZE+1];
471 for(int i=0;i<nfam;i++)
473 int ngro=MEDnFamilyGroup(fid,meshName,i+1);
474 med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
475 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
476 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
477 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
478 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
479 MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
480 std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
487 std::vector<std::string> MEDLoader::GetMeshFamiliesNamesOnGroup(const char *fileName, const char *meshName, const char *grpName)
489 CheckFileForRead(fileName);
490 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
491 med_int nfam=MEDnFamily(fid,meshName);
492 std::vector<std::string> ret;
493 char nomfam[MED_NAME_SIZE+1];
495 for(int i=0;i<nfam;i++)
497 int ngro=MEDnFamilyGroup(fid,meshName,i+1);
498 med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
499 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
500 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
501 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
502 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
503 MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
504 std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
505 for(int j=0;j<ngro;j++)
507 std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
515 std::vector<std::string> MEDLoader::GetMeshGroupsNamesOnFamily(const char *fileName, const char *meshName, const char *famName)
517 CheckFileForRead(fileName);
518 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
519 med_int nfam=MEDnFamily(fid,meshName);
520 std::vector<std::string> ret;
521 char nomfam[MED_NAME_SIZE+1];
524 for(int i=0;i<nfam && !found;i++)
526 int ngro=MEDnFamilyGroup(fid,meshName,i+1);
527 med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
528 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
529 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
530 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
531 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
532 MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
533 std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
534 found=(cur==famName);
536 for(int j=0;j<ngro;j++)
538 std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
544 std::ostringstream oss;
545 oss << "MEDLoader::GetMeshGroupsNamesOnFamily : no such family \"" << famName << "\" in file \"" << fileName << "\" in mesh \"" << meshName << "\" !";
546 throw INTERP_KERNEL::Exception(oss.str().c_str());
552 std::vector<std::string> MEDLoader::GetMeshGroupsNames(const char *fileName, const char *meshName)
554 CheckFileForRead(fileName);
555 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
556 med_int nfam=MEDnFamily(fid,meshName);
557 std::vector<std::string> ret;
558 char nomfam[MED_NAME_SIZE+1];
560 for(int i=0;i<nfam;i++)
562 int ngro=MEDnFamilyGroup(fid,meshName,i+1);
563 med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
564 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
565 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
566 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
567 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
568 MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
569 for(int j=0;j<ngro;j++)
571 std::string cur=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
572 if(std::find(ret.begin(),ret.end(),cur)==ret.end())
578 std::vector<ParaMEDMEM::TypeOfField> MEDLoader::GetTypesOfField(const char *fileName, const char *meshName, const char *fieldName)
580 CheckFileForRead(fileName);
581 std::vector<ParaMEDMEM::TypeOfField> ret;
582 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
583 med_int nbFields=MEDnField(fid);
585 med_field_type typcha;
586 //med_int nbpdtnor=0,pflsize,*pflval,lnsize;
587 med_int numdt=0,numo=0;
589 char pflname[MED_NAME_SIZE+1]="";
590 char locname[MED_NAME_SIZE+1]="";
591 char *maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
592 char *nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
595 for(int i=0;i<nbFields;i++)
597 med_int ncomp=MEDfieldnComponent(fid,i+1);
598 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
599 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
600 INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
602 MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
603 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
604 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
605 if(curMeshName==meshName)
607 if(curFieldName==fieldName)
613 for(int ii=0;ii<nbPdt && !found;ii++)
615 MEDfieldComputingStepInfo(fid,nomcha,1,&numdt,&numo,&dt);
616 med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
617 pflname,&profilesize,locname,&nbi);
620 ret.push_back(ON_NODES);
626 for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
630 MEDfieldComputingStepInfo(fid,nomcha,1,&numdt,&numo,&dt);
631 med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
632 pflname,&profilesize,locname,&nbi);
636 ret.push_back(ON_CELLS);
648 std::vector<std::string> MEDLoader::GetAllFieldNames(const char *fileName)
650 CheckFileForRead(fileName);
651 std::vector<std::string> ret;
652 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
653 med_int nbFields=MEDnField(fid);
654 med_field_type typcha;
655 for(int i=0;i<nbFields;i++)
657 med_int ncomp=MEDfieldnComponent(fid,i+1);
658 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
659 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
660 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
661 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
662 INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
665 MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
666 ret.push_back(std::string(nomcha));
671 std::vector<std::string> MEDLoader::GetAllFieldNamesOnMesh(const char *fileName, const char *meshName)
673 CheckFileForRead(fileName);
674 std::vector<std::string> ret;
675 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
676 med_int nbFields=MEDnField(fid);
678 med_field_type typcha;
679 char *maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
680 char *nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
682 for(int i=0;i<nbFields;i++)
684 med_int ncomp=MEDfieldnComponent(fid,i+1);
685 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
686 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
687 INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
690 MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
691 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
692 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
694 if(curMeshName==meshName)
695 ret.push_back(curFieldName);
702 std::vector<std::string> MEDLoader::GetFieldNamesOnMesh(ParaMEDMEM::TypeOfField type, const char *fileName, const char *meshName)
704 CheckFileForRead(fileName);
708 return GetCellFieldNamesOnMesh(fileName,meshName);
710 return GetNodeFieldNamesOnMesh(fileName,meshName);
712 throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
716 std::vector<std::string> MEDLoader::GetCellFieldNamesOnMesh(const char *fileName, const char *meshName)
718 CheckFileForRead(fileName);
719 std::vector<std::string> ret;
720 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
721 med_int nbFields=MEDnField(fid);
723 med_field_type typcha;
724 //med_int nbpdtnor=0,pflsize,*pflval,lnsize;
725 med_int numdt=0,numo=0;
727 char pflname[MED_NAME_SIZE+1]="";
728 char locname[MED_NAME_SIZE+1]="";
729 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
730 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
731 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
735 for(int i=0;i<nbFields;i++)
737 med_int ncomp=MEDfieldnComponent(fid,i+1);
738 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
739 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
740 MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
741 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
742 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
744 if(curMeshName==meshName)
747 for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
751 MEDfieldComputingStepInfo(fid,nomcha,1,&numdt,&numo,&dt);
752 med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
753 pflname,&profilesize,locname,&nbi);
757 ret.push_back(curFieldName);
766 std::vector<std::string> MEDLoader::GetNodeFieldNamesOnMesh(const char *fileName, const char *meshName)
768 CheckFileForRead(fileName);
769 std::vector<std::string> ret;
770 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
771 med_int nbFields=MEDnField(fid);
772 char pflname[MED_NAME_SIZE+1]="";
773 char locname[MED_NAME_SIZE+1]="";
775 med_field_type typcha;
776 med_int numdt=0,numo=0;
778 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
779 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
780 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
783 for(int i=0;i<nbFields;i++)
785 med_int ncomp=MEDfieldnComponent(fid,i+1);
786 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
787 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
789 MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
790 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
791 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
795 MEDfieldComputingStepInfo(fid,nomcha,1,&numdt,&numo,&dt);
796 med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
797 pflname,&profilesize,locname,&nbi);
798 if(curMeshName==meshName && nbOfVal>0)
800 ret.push_back(curFieldName);
807 std::vector< std::pair< std::pair<int,int>, double> > MEDLoader::GetAllFieldIterations(const char *fileName, const char *fieldName)
809 CheckFileForRead(fileName);
810 std::vector< std::pair< std::pair<int,int>, double > > ret;
811 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
812 med_int nbFields=MEDnField(fid);
814 med_field_type typcha;
815 med_int numdt=0,numo=0;
817 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
818 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
819 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
822 std::ostringstream oss; oss << "MEDLoader::GetAllFieldIterations : No field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
823 for(int i=0;i<nbFields;i++)
825 med_int ncomp=MEDfieldnComponent(fid,i+1);
826 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
827 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
829 MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
830 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
831 if(curFieldName==fieldName)
833 for(int k=0;k<nbPdt;k++)
835 MEDfieldComputingStepInfo(fid,nomcha,k+1,&numdt,&numo,&dt);
836 ret.push_back(std::make_pair(std::make_pair(numdt,numo),dt));
842 oss << "\"" << curFieldName << "\"";
843 if(i!=nbFields-1) oss << ", ";
847 throw INTERP_KERNEL::Exception(oss.str().c_str());
850 double MEDLoader::GetTimeAttachedOnFieldIteration(const char *fileName, const char *fieldName, int iteration, int order)
852 CheckFileForRead(fileName);
853 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
854 med_int nbFields=MEDnField(fid);
856 med_field_type typcha;
857 med_int numdt=0,numo=0;
860 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
861 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
862 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
866 double ret=std::numeric_limits<double>::max();
867 for(int i=0;i<nbFields && !found;i++)
869 med_int ncomp=MEDfieldnComponent(fid,i+1);
870 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
871 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
873 MEDfieldInfo(fid,i+1,nomcha,maa_ass,&local,&typcha,comp,unit,dt_unit,&nbPdt);
874 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
875 if(curFieldName==fieldName)
878 for(int k=0;k<nbPdt;k++)
880 MEDfieldComputingStepInfo(fid,nomcha,k+1,&numdt,&numo,&dt);
881 if(numdt==iteration && numo==order)
889 if(!found || !found2)
891 std::ostringstream oss;
892 oss << "No such field with name \"" << fieldName << "\" and iteration,order=(" << iteration << "," << order << ") exists in file \"" << fileName << "\" !";
893 throw INTERP_KERNEL::Exception(oss.str().c_str());
898 std::vector< std::pair<int,int> > MEDLoader::GetFieldIterations(ParaMEDMEM::TypeOfField type, const char *fileName, const char *meshName, const char *fieldName)
900 CheckFileForRead(fileName);
904 return GetCellFieldIterations(fileName,meshName,fieldName);
906 return GetNodeFieldIterations(fileName,meshName,fieldName);
908 throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
912 std::vector< std::pair<int,int> > MEDLoader::GetCellFieldIterations(const char *fileName, const char *meshName, const char *fieldName)
914 CheckFileForRead(fileName);
915 std::string meshNameCpp(meshName);
916 std::vector< std::pair<int,int> > ret;
917 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
918 med_int nbFields=MEDnField(fid);
920 med_field_type typcha;
921 med_int numdt=0,numo=0;
923 char pflname[MED_NAME_SIZE+1]="";
924 char locname[MED_NAME_SIZE+1]="";
925 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
926 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
927 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
930 std::ostringstream oss; oss << "MEDLoader::GetCellFieldIterations : No cell Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
931 std::set<std::string> s2;
932 for(int i=0;i<nbFields;i++)
934 med_int ncomp=MEDfieldnComponent(fid,i+1);
935 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
936 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
938 MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
939 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
940 if(curFieldName==fieldName)
943 for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
945 for(int k=0;k<nbPdt;k++)
948 MEDfieldComputingStepInfo(fid,nomcha,k+1,&numdt,&numo,&dt);
949 med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
950 pflname,&profilesize,locname,&nbi);
951 std::string maa_ass_cpp(maa_ass);
954 if(meshNameCpp==maa_ass_cpp)
957 ret.push_back(std::make_pair(numdt,numo));
960 s2.insert(maa_ass_cpp);
967 oss << "\"" << curFieldName << "\"";
968 if(i!=nbFields-1) oss << ", ";
975 oss << ". Cell Field \"" << fieldName << "\" exists but lies on meshes with names : \"";
976 std::copy(s2.begin(),s2.end(),std::ostream_iterator<std::string>(oss,"\", \""));
979 throw INTERP_KERNEL::Exception(oss.str().c_str());
984 std::vector< std::pair<int,int> > MEDLoader::GetNodeFieldIterations(const char *fileName, const char *meshName, const char *fieldName)
986 CheckFileForRead(fileName);
987 std::string meshNameCpp(meshName);
988 std::vector< std::pair<int,int> > ret;
989 MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName,MED_ACC_RDONLY);
990 med_int nbFields=MEDnField(fid);
992 med_field_type typcha;
993 med_int numdt=0,numo=0;
995 char pflname[MED_NAME_SIZE+1]="";
996 char locname[MED_NAME_SIZE+1]="";
997 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
998 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
999 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
1002 std::ostringstream oss; oss << "MEDLoader::GetNodeFieldIterations : No node Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
1003 std::set<std::string> s2;
1004 for(int i=0;i<nbFields;i++)
1006 med_int ncomp=MEDfieldnComponent(fid,i+1);
1007 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
1008 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
1010 MEDfieldInfo(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt);
1011 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
1012 if(curFieldName==fieldName)
1014 for(int k=0;k<nbPdt;k++)
1016 int profilesize,nbi;
1017 MEDfieldComputingStepInfo(fid,nomcha,k+1,&numdt,&numo,&dt);
1018 med_int nbOfVal=MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
1019 pflname,&profilesize,locname,&nbi);
1020 std::string maa_ass_cpp(maa_ass);
1023 if(meshNameCpp==maa_ass_cpp)
1024 { ret.push_back(std::make_pair(numdt,numo)); }
1026 s2.insert(maa_ass_cpp);
1032 oss << "\"" << curFieldName << "\"";
1033 if(i!=nbFields-1) oss << ", ";
1040 oss << ". Node Field \"" << fieldName << "\" exists but lies on meshes with names : \"";
1041 std::copy(s2.begin(),s2.end(),std::ostream_iterator<std::string>(oss,"\", \""));
1044 throw INTERP_KERNEL::Exception(oss.str().c_str());
1049 MEDCouplingUMesh *MEDLoader::ReadUMeshFromFile(const char *fileName, const char *meshName, int meshDimRelToMax)
1051 CheckFileForRead(fileName);
1052 MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1053 MEDFileMesh *mmPtr(mm);
1054 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1057 std::ostringstream oss; oss << "MEDLoader::ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName=\""<< meshName << "\" exists but it is not an unstructured mesh !";
1058 throw INTERP_KERNEL::Exception(oss.str().c_str());
1060 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1063 ParaMEDMEM::MEDCouplingUMesh *MEDLoader::ReadUMeshFromFile(const char *fileName, int meshDimRelToMax)
1065 CheckFileForRead(fileName);
1066 MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName);
1067 MEDFileMesh *mmPtr(mm);
1068 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1071 std::ostringstream oss; oss << "MEDLoader::ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1072 throw INTERP_KERNEL::Exception(oss.str().c_str());
1074 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1077 int MEDLoader::ReadUMeshDimFromFile(const char *fileName, const char *meshName)
1079 CheckFileForRead(fileName);
1080 std::vector<int> poss;
1081 return MEDLoaderNS::readUMeshDimFromFile(fileName,meshName,poss);
1084 ParaMEDMEM::MEDCouplingUMesh *MEDLoader::ReadUMeshFromFamilies(const char *fileName, const char *meshName, int meshDimRelToMax, const std::vector<std::string>& fams)
1086 CheckFileForRead(fileName);
1087 MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1088 MEDFileMesh *mmPtr(mm);
1089 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1092 std::ostringstream oss; oss << "MEDLoader::ReadUMeshFromFamilies : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1093 throw INTERP_KERNEL::Exception(oss.str().c_str());
1095 return mmuPtr->getFamilies(meshDimRelToMax,fams,true);
1098 ParaMEDMEM::MEDCouplingUMesh *MEDLoader::ReadUMeshFromGroups(const char *fileName, const char *meshName, int meshDimRelToMax, const std::vector<std::string>& grps)
1100 CheckFileForRead(fileName);
1101 MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1102 MEDFileMesh *mmPtr(mm);
1103 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1106 std::ostringstream oss; oss << "MEDLoader::ReadUMeshFromGroups : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1107 throw INTERP_KERNEL::Exception(oss.str().c_str());
1109 return mmuPtr->getGroups(meshDimRelToMax,grps,true);
1112 ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadField(ParaMEDMEM::TypeOfField type, const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order)
1114 CheckFileForRead(fileName);
1118 return ReadFieldCell(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1120 return ReadFieldNode(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1122 return ReadFieldGauss(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1124 return ReadFieldGaussNE(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1126 throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES, ON_CELLS, ON_GAUSS_PT or ON_GAUSS_NE !");
1130 std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> MEDLoader::ReadFieldsOnSameMesh(ParaMEDMEM::TypeOfField type, const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName,
1131 const std::vector<std::pair<int,int> >& its) throw(INTERP_KERNEL::Exception)
1134 return std::vector<ParaMEDMEM::MEDCouplingFieldDouble *>();
1135 CheckFileForRead(fileName);
1136 std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> ret(its.size());
1137 std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> > retSafe(its.size());
1140 //Retrieving mesh of rank 0 and field on rank 0 too.
1141 MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1142 MEDFileMesh *mmPtr(mm);
1143 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1145 throw INTERP_KERNEL::Exception("MEDLoader::ReadFieldsOnSameMesh : only unstructured mesh is managed !");
1146 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> m=mmuPtr->getMeshAtLevel(meshDimRelToMax);
1147 const DataArrayInt *o2n=mmuPtr->getNumberFieldAtLevel(meshDimRelToMax);
1148 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> m2(m->clone(true));
1150 m2->renumberCells(o2n->begin(),true);
1152 for(std::vector<std::pair<int,int> >::const_iterator it=its.begin();it!=its.end();it++,i++)
1154 MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> ff=MEDFileField1TS::New(fileName,fieldName,(*it).first,(*it).second);
1155 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> retElt=ff->getFieldOnMeshAtLevel(type,m);
1157 retElt->renumberCells(o2n->begin(),true);
1158 retElt->setMesh(m2);
1162 for(std::vector<std::pair<int,int> >::const_iterator it=its.begin();it!=its.end();it++,i++)
1163 ret[i]=retSafe[i].retn();
1167 std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> MEDLoader::ReadFieldsCellOnSameMesh(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName,
1168 const std::vector<std::pair<int,int> >& its) throw(INTERP_KERNEL::Exception)
1170 return ReadFieldsOnSameMesh(ON_CELLS,fileName,meshName,meshDimRelToMax,fieldName,its);
1173 std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> MEDLoader::ReadFieldsNodeOnSameMesh(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName,
1174 const std::vector<std::pair<int,int> >& its) throw(INTERP_KERNEL::Exception)
1176 return ReadFieldsOnSameMesh(ON_NODES,fileName,meshName,meshDimRelToMax,fieldName,its);
1179 std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> MEDLoader::ReadFieldsGaussOnSameMesh(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName,
1180 const std::vector<std::pair<int,int> >& its) throw(INTERP_KERNEL::Exception)
1182 return ReadFieldsOnSameMesh(ON_GAUSS_PT,fileName,meshName,meshDimRelToMax,fieldName,its);
1185 std::vector<ParaMEDMEM::MEDCouplingFieldDouble *> MEDLoader::ReadFieldsGaussNEOnSameMesh(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName,
1186 const std::vector<std::pair<int,int> >& its) throw(INTERP_KERNEL::Exception)
1188 return ReadFieldsOnSameMesh(ON_GAUSS_NE,fileName,meshName,meshDimRelToMax,fieldName,its);
1191 ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadFieldCell(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order)
1193 MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> ff=MEDFileField1TS::New(fileName,fieldName,iteration,order);
1194 MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1195 MEDCouplingAutoRefCountObjectPtr<MEDCouplingMesh> m=mm->getGenMeshAtLevel(meshDimRelToMax,false);
1196 MEDFileMesh *mPtr(mm);
1197 MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1198 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> ret=ff->getFieldOnMeshAtLevel(ON_CELLS,m);
1201 const DataArrayInt *num=muPtr->getNumberFieldAtLevel(meshDimRelToMax);
1203 ret->renumberCells(num->begin());
1208 ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadFieldNode(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order)
1210 MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> ff=MEDFileField1TS::New(fileName,fieldName,iteration,order);
1211 MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1212 MEDCouplingAutoRefCountObjectPtr<MEDCouplingMesh> m=mm->getGenMeshAtLevel(meshDimRelToMax,false);
1213 MEDFileMesh *mPtr(mm);
1214 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> ret=ff->getFieldOnMeshAtLevel(ON_NODES,m);
1215 MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1216 if(ff->getPflsReallyUsed().empty())
1220 const DataArrayInt *num=muPtr->getNumberFieldAtLevel(meshDimRelToMax);
1222 ret->renumberCells(num->begin());
1227 DataArrayInt *pfl=0,*arr2=0;
1228 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> arr=ff->getFieldWithProfile(ON_NODES,meshDimRelToMax,mm,pfl);
1229 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> pflSafe(pfl);
1230 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> mp=m->getCellIdsFullyIncludedInNodeIds(pfl->begin(),pfl->end());
1231 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> mzip=static_cast<MEDCouplingUMesh *>(m->buildPartAndReduceNodes(mp->begin(),mp->end(),arr2));
1232 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr2Safe(arr2);
1233 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr3=arr2->invertArrayO2N2N2O(mzip->getNumberOfNodes());
1234 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> pflSorted(pflSafe->deepCpy()); pflSorted->sort(true);
1235 if(!arr3->isEqualWithoutConsideringStr(*pflSorted))
1236 throw INTERP_KERNEL::Exception("MEDLoader::ReadFieldNode : not implemented yet !");
1237 if(!arr3->isEqualWithoutConsideringStr(*pflSafe))
1239 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> o2n2=pflSafe->checkAndPreparePermutation();
1240 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> n2o2=o2n2->invertArrayO2N2N2O(o2n2->getNumberOfTuples());
1241 mzip->renumberNodes(n2o2->begin(),n2o2->getNumberOfTuples());
1250 ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadFieldGauss(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order)
1252 MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> ff=MEDFileField1TS::New(fileName,fieldName,iteration,order);
1253 MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1254 MEDCouplingAutoRefCountObjectPtr<MEDCouplingMesh> m=mm->getGenMeshAtLevel(meshDimRelToMax,false);
1255 MEDFileMesh *mPtr(mm);
1256 MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1257 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> ret=ff->getFieldOnMeshAtLevel(ON_GAUSS_PT,m);
1260 const DataArrayInt *num=muPtr->getNumberFieldAtLevel(meshDimRelToMax);
1262 ret->renumberCells(num->begin());
1267 ParaMEDMEM::MEDCouplingFieldDouble *MEDLoader::ReadFieldGaussNE(const char *fileName, const char *meshName, int meshDimRelToMax, const char *fieldName, int iteration, int order)
1269 MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> ff=MEDFileField1TS::New(fileName,fieldName,iteration,order);
1270 MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1271 MEDCouplingAutoRefCountObjectPtr<MEDCouplingMesh> m=mm->getGenMeshAtLevel(meshDimRelToMax,false);
1272 MEDFileMesh *mPtr(mm);
1273 MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1274 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> ret=ff->getFieldOnMeshAtLevel(ON_GAUSS_NE,m);
1277 const DataArrayInt *num=muPtr->getNumberFieldAtLevel(meshDimRelToMax);
1279 ret->renumberCells(num->begin());
1284 void MEDLoader::WriteUMesh(const char *fileName, const ParaMEDMEM::MEDCouplingUMesh *mesh, bool writeFromScratch)
1287 throw INTERP_KERNEL::Exception("MEDLoader::WriteUMesh : input mesh is null !");
1288 int mod=writeFromScratch?2:0;
1289 MEDCouplingAutoRefCountObjectPtr<MEDFileUMesh> m=MEDFileUMesh::New();
1290 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> mcpy(static_cast<MEDCouplingUMesh *>(mesh->deepCpy()));
1291 m->setMeshAtLevel(0,mcpy,true);
1292 m->write(fileName,mod);
1295 void MEDLoader::WriteUMeshDep(const char *fileName, const ParaMEDMEM::MEDCouplingUMesh *mesh, bool writeFromScratch)
1297 MEDLoader::WriteUMesh(fileName,mesh,writeFromScratch);
1300 void MEDLoader::WriteUMeshesPartition(const char *fileName, const char *meshNameC, const std::vector<const ParaMEDMEM::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1302 std::string meshName(meshNameC);
1303 if(meshName.empty())
1304 throw INTERP_KERNEL::Exception("Trying to write a unstructured mesh with no name ! MED file format needs a not empty mesh name : change 2nd parameter !");
1305 int status=MEDLoaderBase::getStatusOfFile(fileName);
1306 if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
1308 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
1309 throw INTERP_KERNEL::Exception(oss.str().c_str());
1311 MEDCouplingAutoRefCountObjectPtr<MEDFileUMesh> m=MEDFileUMesh::New();
1312 m->setGroupsFromScratch(0,meshes,true);
1313 m->setName(meshNameC);
1314 int mod=writeFromScratch?2:0;
1315 m->write(fileName,mod);
1318 void MEDLoader::WriteUMeshesPartitionDep(const char *fileName, const char *meshNameC, const std::vector<const ParaMEDMEM::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1320 WriteUMeshesPartition(fileName,meshNameC,meshes,writeFromScratch);
1323 void MEDLoader::WriteUMeshes(const char *fileName, const std::vector<const ParaMEDMEM::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1325 int mod=writeFromScratch?2:0;
1326 MEDCouplingAutoRefCountObjectPtr<MEDFileUMesh> m=MEDFileUMesh::New();
1327 m->setMeshes(meshes,true);
1328 m->write(fileName,mod);
1331 void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, bool writeFromScratch)
1333 MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> ff=MEDFileField1TS::New();
1334 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> f2(f->deepCpy());
1335 const MEDCouplingMesh *m=f2->getMesh();
1336 const MEDCouplingUMesh *um=dynamic_cast<const MEDCouplingUMesh *>(m);
1337 MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm;
1338 int mod=writeFromScratch?2:0;
1341 MEDCouplingAutoRefCountObjectPtr<MEDFileUMesh> mmu=MEDFileUMesh::New();
1342 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> o2n=um->getRenumArrForMEDFileFrmt();
1343 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> n2o=o2n->invertArrayO2N2N2O(o2n->getNumberOfTuples());
1344 f2->renumberCells(o2n->begin(),false);
1345 mmu->setMeshAtLevel(0,const_cast<MEDCouplingUMesh *>(static_cast<const MEDCouplingUMesh *>(f2->getMesh())));
1346 mmu->setRenumFieldArr(0,n2o);
1347 ff->setFieldNoProfileSBT(f2);
1348 mmu->write(fileName,mod);
1351 throw INTERP_KERNEL::Exception("MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile : only umeshes are dealed in this API for the moment !");
1352 ff->write(fileName,0);
1355 void MEDLoader::WriteField(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, bool writeFromScratch)
1358 throw INTERP_KERNEL::Exception("MEDLoader::WriteField : input field is NULL !");
1359 f->checkCoherency();
1360 int status=MEDLoaderBase::getStatusOfFile(fileName);
1361 if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
1363 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
1364 throw INTERP_KERNEL::Exception(oss.str().c_str());
1366 if(writeFromScratch || (!writeFromScratch && status==MEDLoaderBase::NOT_EXIST))
1368 MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(fileName,f,true);
1372 std::vector<std::string> meshNames=GetMeshNames(fileName);
1374 throw INTERP_KERNEL::Exception("MEDLoader::WriteField : trying to write a field with no mesh !");
1375 std::string fileNameCpp(f->getMesh()->getName());
1376 if(std::find(meshNames.begin(),meshNames.end(),fileNameCpp)==meshNames.end())
1377 MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(fileName,f,false);
1380 MEDCouplingAutoRefCountObjectPtr<MEDFileMesh> mm=MEDFileMesh::New(fileName,f->getMesh()->getName().c_str());
1381 const MEDFileMesh *mmPtr(mm);
1382 const MEDFileUMesh *mmuPtr=dynamic_cast<const MEDFileUMesh *>(mmPtr);
1384 throw INTERP_KERNEL::Exception("MEDLoader::WriteField : only umeshes are supported now !");
1385 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> f2(f->deepCpy());
1386 MEDCouplingUMesh *m=dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh()));
1388 throw INTERP_KERNEL::Exception("MEDLoader::WriteField : only umesh in input field supported !");
1389 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> o2n=m->getRenumArrForMEDFileFrmt();
1390 f2->renumberCells(o2n->begin(),false);
1391 m=static_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh()));
1392 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> mread=mmuPtr->getMeshAtLevel(m->getMeshDimension()-mm->getMeshDimension());
1393 if(f2->getTypeOfField()!=ON_NODES)
1395 m->tryToShareSameCoordsPermute(*mread,_EPS_FOR_NODE_COMP);
1396 DataArrayInt *part=0;
1397 bool b=mread->areCellsIncludedIn(m,_COMP_FOR_CELL,part);
1398 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> partSafe(part);
1401 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 !";
1402 throw INTERP_KERNEL::Exception(oss.str().c_str());
1404 MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> f1ts=MEDFileField1TS::New();
1405 if(part->isIdentity() && part->getNumberOfTuples()==mread->getNumberOfCells())
1406 f1ts->setFieldNoProfileSBT(f2);
1409 part->setName(f1ts->createNewNameOfPfl().c_str());
1410 f1ts->setFieldProfile(f2,mm,m->getMeshDimension()-mm->getMeshDimension(),part);
1412 f1ts->write(fileName,0);
1417 DataArrayInt *part=0;
1418 bool b=mread->getCoords()->areIncludedInMe(m->getCoords(),_EPS_FOR_NODE_COMP,part);
1419 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> partSafe(part);
1422 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 !";
1423 throw INTERP_KERNEL::Exception(oss.str().c_str());
1425 MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> f1ts=MEDFileField1TS::New();
1426 if(part->isIdentity() && part->getNumberOfTuples()==mread->getNumberOfNodes())
1427 f1ts->setFieldNoProfileSBT(f2);
1430 part->setName(f1ts->createNewNameOfPfl().c_str());
1431 f1ts->setFieldProfile(f2,mm,m->getMeshDimension()-mm->getMeshDimension(),part);
1433 f1ts->write(fileName,0);
1439 void MEDLoader::WriteFieldDep(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f, bool writeFromScratch)
1441 WriteField(fileName,f,writeFromScratch);
1444 void MEDLoader::WriteFieldUsingAlreadyWrittenMesh(const char *fileName, const ParaMEDMEM::MEDCouplingFieldDouble *f)
1447 throw INTERP_KERNEL::Exception("MEDLoader::WriteFieldUsingAlreadyWrittenMesh : input field is null !");
1448 f->checkCoherency();
1449 int status=MEDLoaderBase::getStatusOfFile(fileName);
1450 if(status!=MEDLoaderBase::EXIST_RW)
1452 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions or not exists !";
1453 throw INTERP_KERNEL::Exception(oss.str().c_str());
1455 MEDCouplingAutoRefCountObjectPtr<MEDFileField1TS> f1ts=MEDFileField1TS::New();
1456 MEDCouplingUMesh *m=dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f->getMesh()));
1458 throw INTERP_KERNEL::Exception("MEDLoader::WriteFieldUsingAlreadyWrittenMesh : only umesh in input field supported !");
1459 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> o2n=m->getRenumArrForMEDFileFrmt();
1460 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> f2(f->deepCpy());
1461 f2->renumberCells(o2n->begin(),false);
1462 f1ts->setFieldNoProfileSBT(f2);
1463 f1ts->write(fileName,0);