1 // Copyright (C) 2007-2021 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, or (at your option) any later version.
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 "MEDLoaderNS.hxx"
25 #include "MEDFileSafeCaller.txx"
26 #include "MEDFileMesh.hxx"
27 #include "MEDFileField.hxx"
28 #include "CellModel.hxx"
29 #include "MEDCouplingUMesh.hxx"
30 #include "MEDCouplingMemArray.hxx"
31 #include "MEDCouplingFieldDouble.hxx"
32 #include "MEDCouplingFieldFloat.hxx"
33 #include "MEDCouplingFieldInt32.hxx"
34 #include "MEDCouplingGaussLocalization.hxx"
35 #include "MEDCouplingTraits.hxx"
38 #include "InterpKernelAutoPtr.hxx"
52 extern med_geometry_type typmai[MED_N_CELL_FIXED_GEO];
53 extern med_geometry_type typmainoeud[1];
54 extern INTERP_KERNEL::NormalizedCellType typmai2[MED_N_CELL_FIXED_GEO];
55 extern med_geometry_type typmai3[INTERP_KERNEL::NORM_MAXTYPE];
58 med_geometry_type typmai[MED_N_CELL_FIXED_GEO] = { MED_POINT1,
83 med_geometry_type typmainoeud[1] = { MED_NONE };
85 INTERP_KERNEL::NormalizedCellType typmai2[MED_N_CELL_FIXED_GEO] = { INTERP_KERNEL::NORM_POINT1,
86 INTERP_KERNEL::NORM_SEG2,
87 INTERP_KERNEL::NORM_SEG3,
88 INTERP_KERNEL::NORM_SEG4,
89 INTERP_KERNEL::NORM_TRI3,
90 INTERP_KERNEL::NORM_QUAD4,
91 INTERP_KERNEL::NORM_TRI6,
92 INTERP_KERNEL::NORM_TRI7,
93 INTERP_KERNEL::NORM_QUAD8,
94 INTERP_KERNEL::NORM_QUAD9,
95 INTERP_KERNEL::NORM_TETRA4,
96 INTERP_KERNEL::NORM_PYRA5,
97 INTERP_KERNEL::NORM_PENTA6,
98 INTERP_KERNEL::NORM_HEXA8,
99 INTERP_KERNEL::NORM_HEXGP12,
100 INTERP_KERNEL::NORM_TETRA10,
101 INTERP_KERNEL::NORM_PYRA13,
102 INTERP_KERNEL::NORM_PENTA15,
103 INTERP_KERNEL::NORM_PENTA18,
104 INTERP_KERNEL::NORM_HEXA20,
105 INTERP_KERNEL::NORM_HEXA27,
106 INTERP_KERNEL::NORM_POLYGON,
107 INTERP_KERNEL::NORM_QPOLYG,
108 INTERP_KERNEL::NORM_POLYHED };
110 med_geometry_type typmai3[INTERP_KERNEL::NORM_MAXTYPE] = { MED_POINT1,//0
145 // Just for this translation unit:
146 static double _EPS_FOR_NODE_COMP=1.e-12;
147 static int _COMP_FOR_CELL=0;
148 static int _TOO_LONG_STR=0;
151 using namespace MEDCoupling;
155 INTERP_KERNEL::NormalizedCellType ConvertGeometryType(med_geometry_type geotype)
157 INTERP_KERNEL::NormalizedCellType result=INTERP_KERNEL::NORM_ERROR;
158 for(int i=0; i<MED_N_CELL_FIXED_GEO; i++)
160 if (typmai[i]==geotype)
170 * This method returns a first quick overview of mesh with name \a meshName into the file \a fileName.
171 * @param possibilities the relativeToMeshDim authorized to returned maxdim. This vector is systematically cleared at the begin of this method.
172 * @return the maximal mesh dimension of specified mesh. If nothing found -1 is returned.
174 int MEDLoaderNS::readUMeshDimFromFile(const std::string& fileName, const std::string& meshName, std::vector<int>& possibilities)
176 possibilities.clear();
177 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
180 char nommaa[MED_NAME_SIZE+1];
181 char maillage_description[MED_COMMENT_SIZE+1];
182 med_mesh_type type_maillage;
184 std::string trueMeshName;
185 int meshId=FromMedInt<int>(getIdFromMeshName(fid,meshName,trueMeshName));
186 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
187 med_sorting_type sortingType;
189 med_axis_type axisType;
190 med_int naxis(MEDmeshnAxis(fid,meshId));
191 INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
192 INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
193 MEDFILESAFECALLERRD0(MEDmeshInfo,(fid,meshId,nommaa,&Sdim,&Mdim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit));
197 throw INTERP_KERNEL::Exception("multisteps on mesh not managed yet !");
201 MEDFILESAFECALLERRD0(MEDmeshComputationStepInfo,(fid,nommaa,1,&numdt,&numit,&dt));
203 for(int i=0;i<MED_N_CELL_GEO_FIXED_CON;i++)
205 med_geometry_type curMedType=typmai[i];
206 med_bool changement,transformation;
207 med_int curNbOfElemM(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));
208 med_int curNbOfElemF(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));//limitation
210 med_entity_type whichEntity;
211 MEDLoaderNS::dispatchElems(curNbOfElemM,curNbOfElemF,curNbOfElem,whichEntity);
214 INTERP_KERNEL::NormalizedCellType type=typmai2[i];
215 int curDim=(int)INTERP_KERNEL::CellModel::GetCellModel(type).getDimension();
222 for(std::set<int>::const_reverse_iterator it=poss.rbegin();it!=poss.rend();it++)
223 possibilities.push_back(*it-ret);
230 int MEDLoaderNS::getIdFromMeshName(med_idt fid, const std::string& meshName, std::string& trueMeshName)
234 std::vector<std::string> meshes=getMeshNamesFid(fid);
236 throw INTERP_KERNEL::Exception("No mesh in file");
237 trueMeshName=meshes[0];
240 std::string meshNameStr(meshName);
241 std::vector<std::string> meshes=getMeshNamesFid(fid);
243 throw INTERP_KERNEL::Exception("No mesh in file");
244 std::vector<std::string>::iterator iter=std::find(meshes.begin(),meshes.end(),meshNameStr);
245 if(iter==meshes.end())
247 std::ostringstream os2;
248 os2 << "MeshName '" << meshName << "' not in file : meshes available : ";
249 std::copy(meshes.begin(),meshes.end(),std::ostream_iterator<std::string>(os2," "));
250 throw INTERP_KERNEL::Exception(os2.str().c_str());
252 trueMeshName=meshName;
253 return (int)( iter-meshes.begin()+1 );
256 std::vector<std::string> MEDLoaderNS::getMeshNamesFid(med_idt fid)
258 med_mesh_type type_maillage;
259 char maillage_description[MED_COMMENT_SIZE+1];
260 char dtunit[MED_COMMENT_SIZE+1];
263 char nommaa[MED_NAME_SIZE+1];
264 med_axis_type axistype;
265 med_sorting_type stype;
266 med_int n=MEDnMesh(fid);
267 std::vector<std::string> ret(n);
270 med_int naxis(MEDmeshnAxis(fid,i+1));
271 INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
272 INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
274 MEDFILESAFECALLERRD0(MEDmeshInfo,(fid,i+1,nommaa,&space_dim,&mesh_dim,&type_maillage,maillage_description,dtunit,&stype,&nstep,&axistype,axisname,axisunit));
275 std::string cur=MEDLoaderBase::buildStringFromFortran(nommaa,sizeof(nommaa));
282 * This methods allows to merger all entities and to considerate only cell types.
284 void MEDLoaderNS::dispatchElems(med_int nbOfElemCell, med_int nbOfElemFace, med_int& nbOfElem, med_entity_type& whichEntity)
286 if(nbOfElemCell>=nbOfElemFace)
288 whichEntity=MED_CELL;
289 nbOfElem=nbOfElemCell;
293 whichEntity=MED_CELL;
294 nbOfElem=nbOfElemFace;
300 void MEDCoupling::AssignStaticWritePropertiesTo(MEDCoupling::MEDFileWritable& obj)
302 obj.setTooLongStrPolicy(_TOO_LONG_STR);
305 bool MEDCoupling::HasXDR()
314 std::string MEDCoupling::MEDFileVersionStr()
318 std::fill(buf,buf+SZ,'\0');
319 const char START_EXPECTED[]="MED-";
320 med_err ret(MEDlibraryStrVersion(buf));
322 throw INTERP_KERNEL::Exception("MEDFileVersionStr : fail to find version of MED file ! It looks very bad !");
323 std::string zeRet(buf);
324 std::size_t pos(zeRet.find(START_EXPECTED,0));
327 std::ostringstream oss; oss << "MEDFileVersionStr : internal error ! The MEDFile returned version (\"" << zeRet << "\") has not the right pattern !";
328 throw INTERP_KERNEL::Exception(oss.str());
330 return zeRet.substr(sizeof(START_EXPECTED)-1,std::string::npos);
333 std::string MEDCoupling::MEDFileVersionOfFileStr(const std::string& fileName)
335 #if MED_NUM_MAJEUR>3 || ( MED_NUM_MAJEUR==3 && ( (MED_NUM_MINEUR==2 && MED_NUM_RELEASE>=1) || MED_NUM_MINEUR>=3) )
336 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
338 const char START_EXPECTED[]="MED-";
340 std::fill(buf,buf+SZ,'\0');
341 MEDFILESAFECALLERRD0(MEDfileStrVersionRd,(fid,buf));
342 std::string ret(buf);
343 std::size_t pos(ret.find(START_EXPECTED,0));
346 std::ostringstream oss; oss << "MEDFileVersionOfFileStr : internal error ! The MEDFile returned version (\"" << ret << "\") has not the right pattern !";
347 throw INTERP_KERNEL::Exception(oss.str());
349 return ret.substr(sizeof(START_EXPECTED)-1,std::string::npos);
351 std::ostringstream oss; oss << "MEDFileVersionOfFileStr : is implemented with MEDFile " << MEDFileVersionStr() << " ! If you need this feature please use version >= 3.2.1.";
352 throw INTERP_KERNEL::Exception(oss.str());
356 void MEDCoupling::MEDFileVersion(int& major, int& minor, int& release)
358 med_int majj,minn,rell;
359 med_err ret(MEDlibraryNumVersion(&majj,&minn,&rell));
361 throw INTERP_KERNEL::Exception("MEDFileVersion : fail to call MEDlibraryNumVersion ! It looks very bad !");
362 major=FromMedInt<int>(majj);
363 minor=FromMedInt<int>(minn);
364 release=FromMedInt<int>(rell);
368 * 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.
370 void MEDCoupling::SetEpsilonForNodeComp(double val)
372 _EPS_FOR_NODE_COMP=val;
376 * 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.
378 void MEDCoupling::SetCompPolicyForCell(int val)
384 * This method set the behaviour of MEDLoader when a too long string is seen in datastructure before copy it in MED file.
385 * By default (0) an exception is thrown. If equal to 1 a warning is emitted in std_err but no exception is thrown.
387 void MEDCoupling::SetTooLongStrPolicy(int val)
393 * Given a 'fileName' and a 'meshName' this method returns global information concerning this mesh.
394 * It returns, in this order :
395 * - 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...
396 * - the mesh dimension
397 * - the space dimension
398 * - the number of nodes
400 std::vector< std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > > MEDCoupling::GetUMeshGlobalInfo(const std::string& fileName, const std::string& meshName, int &meshDim, int& spaceDim, mcIdType& numberOfNodes)
402 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
404 char nommaa[MED_NAME_SIZE+1];
405 char maillage_description[MED_COMMENT_SIZE+1];
406 med_mesh_type type_maillage;
407 std::string trueMeshName;
408 int meshId=MEDLoaderNS::getIdFromMeshName(fid,meshName,trueMeshName);
409 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
410 med_sorting_type sortingType;
411 med_int nstep, mDim, sDim;
412 med_axis_type axisType;
413 med_int naxis(MEDmeshnAxis(fid,meshId));
414 INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
415 INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
416 MEDFILESAFECALLERRD0(MEDmeshInfo,(fid,meshId,nommaa,&sDim,&mDim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit));
417 meshDim=FromMedInt<int>(mDim);
418 spaceDim=FromMedInt<int>(sDim);
419 if(type_maillage!=MED_UNSTRUCTURED_MESH)
421 std::ostringstream oss; oss << "GetUMeshGlobalInfo : Mesh \""<< meshName << "\" in file \"" << fileName;
422 oss << "\" exists but it is not an unstructured mesh ! This method is not relevant for mesh types that are not unstructured !";
423 throw INTERP_KERNEL::Exception(oss.str().c_str());
427 throw INTERP_KERNEL::Exception("GetUMeshGlobalInfo : multisteps on mesh not managed !");
430 MEDFILESAFECALLERRD0(MEDmeshComputationStepInfo,(fid,nommaa,1,&numdt,&numit,&dt));
432 std::vector<int> dims;
433 std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > geoTypes;
434 med_bool changement,transformation;
435 for(int i=0;i<MED_N_CELL_FIXED_GEO;i++)
437 med_geometry_type curMedType=typmai[i];
438 med_int curNbOfElemM(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));
441 INTERP_KERNEL::NormalizedCellType typp=typmai2[i];
442 int mdimCell=INTERP_KERNEL::CellModel::GetCellModel(typp).getDimension();
443 dims.push_back(mdimCell);
444 geoTypes.push_back(std::pair<INTERP_KERNEL::NormalizedCellType,int>(typp,curNbOfElemM));
447 int maxLev=*std::max_element(dims.begin(),dims.end());
448 int lowLev=*std::min_element(dims.begin(),dims.end());
449 int nbOfLevels=maxLev-lowLev+1;
450 std::vector< std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > > ret(nbOfLevels);
451 for(std::size_t i=0;i<dims.size();i++)
453 ret[maxLev-dims[i]].push_back(geoTypes[i]);
455 numberOfNodes=ToIdType(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation));
459 void MEDCoupling::CheckFileForRead(const std::string& fileName)
461 MEDFileUtilities::CheckFileForRead(fileName);
464 std::vector<std::string> MEDCoupling::GetMeshNames(const std::string& fileName)
466 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
467 return MEDLoaderNS::getMeshNamesFid(fid);
470 std::vector< std::pair<std::string,std::string> > MEDCoupling::GetComponentsNamesOfField(const std::string& fileName, const std::string& fieldName)
472 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
473 med_int nbFields(MEDnField(fid));
474 std::vector<std::string> fields(nbFields);
475 med_field_type typcha;
476 for(int i=0;i<nbFields;i++)
478 med_int ncomp(MEDfieldnComponent(fid,i+1));
479 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
480 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
481 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
484 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
485 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
486 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
487 std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
488 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
489 if(curFieldName==fieldName)
491 std::vector< std::pair<std::string,std::string> > ret(ncomp);
492 for(int j=0;j<ncomp;j++)
493 ret[j]=std::pair<std::string,std::string>(MEDLoaderBase::buildStringFromFortran(((char *)comp)+j*MED_SNAME_SIZE,MED_SNAME_SIZE),
494 MEDLoaderBase::buildStringFromFortran(((char *)unit)+j*MED_SNAME_SIZE,MED_SNAME_SIZE));
497 fields[i]=curFieldName;
499 std::ostringstream oss; oss << "GetComponentsNamesOfField : no such field \"" << fieldName << "\" in file \"" << fileName << "\" !" << std::endl;
500 oss << "Possible field names are : " << std::endl;
501 std::copy(fields.begin(),fields.end(),std::ostream_iterator<std::string>(oss," "));
502 throw INTERP_KERNEL::Exception(oss.str().c_str());
505 // see reference : https://en.cppreference.com/w/cpp/iterator/iterator
506 class MEDVectorStringIterator : public std::iterator< std::input_iterator_tag, long, long, const std::string*, std::string >
509 char *_data = nullptr;
511 explicit MEDVectorStringIterator(long num , char *data) : _num(num),_data(data) {}
512 MEDVectorStringIterator& operator++() { ++_num; return *this;}
513 bool operator==(const MEDVectorStringIterator& other) const {return _num == other._num;}
514 bool operator!=(const MEDVectorStringIterator& other) const {return !(*this == other);}
515 reference operator*() const {return MEDLoaderBase::buildStringFromFortran(_data+_num*MED_LNAME_SIZE,MED_LNAME_SIZE);}
518 void MEDCoupling::GetFamiliesGroupsInfo(const std::string& fileName, const std::string& meshName, std::map<std::string,mcIdType>& families, std::map<std::string,std::vector<std::string>>& groupsOnFam)
520 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
521 med_int nbFams(MEDnFamily(fid,meshName.c_str()));
522 char nomfam[MED_NAME_SIZE+1];
523 for(med_int i=0;i<nbFams;++i)
525 med_int nbGrps(MEDnFamilyGroup(fid,meshName.c_str(),i+1)),famId;
526 std::unique_ptr<char[]> gro{new char[MED_LNAME_SIZE*nbGrps+1]};
527 MEDFILESAFECALLERRD0(MEDfamilyInfo,(fid,meshName.c_str(),i+1,nomfam,&famId,gro.get()));
528 std::string fam(MEDLoaderBase::buildStringFromFortran(nomfam,MED_NAME_SIZE));
529 families[fam] = FromMedInt<mcIdType>(famId);
530 std::vector<std::string> v(nbGrps);
531 std::copy(MEDVectorStringIterator(0,gro.get()),MEDVectorStringIterator(nbGrps,gro.get()),v.begin());
532 groupsOnFam[fam] = v;
536 std::vector<std::string> MEDCoupling::GetMeshNamesOnField(const std::string& fileName, const std::string& fieldName)
538 std::vector<std::string> ret;
540 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
541 med_int nbFields=MEDnField(fid);
543 med_field_type typcha;
544 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
545 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
548 for(int i=0;i<nbFields;i++)
550 med_int ncomp(MEDfieldnComponent(fid,i+1));
551 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
552 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
554 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
555 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
556 std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
557 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
558 if(curFieldName==fieldName)
559 ret.push_back(meshName);
564 std::vector<std::string> MEDCoupling::GetMeshFamiliesNames(const std::string& fileName, const std::string& meshName)
566 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
567 med_int nfam=MEDnFamily(fid,meshName.c_str());
568 std::vector<std::string> ret(nfam);
569 char nomfam[MED_NAME_SIZE+1];
571 for(int i=0;i<nfam;i++)
573 med_int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
574 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
575 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
576 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
577 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
578 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
579 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
580 std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
587 std::vector<std::string> MEDCoupling::GetMeshFamiliesNamesOnGroup(const std::string& fileName, const std::string& meshName, const std::string& grpName)
589 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
590 med_int nfam=MEDnFamily(fid,meshName.c_str());
591 std::vector<std::string> ret;
592 char nomfam[MED_NAME_SIZE+1];
594 for(int i=0;i<nfam;i++)
596 med_int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
597 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
598 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
599 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
600 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
601 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
602 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
603 std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
604 for(int j=0;j<ngro;j++)
606 std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
614 std::vector<std::string> MEDCoupling::GetMeshGroupsNamesOnFamily(const std::string& fileName, const std::string& meshName, const std::string& famName)
616 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
617 med_int nfam(MEDnFamily(fid,meshName.c_str()));
618 std::vector<std::string> ret;
619 char nomfam[MED_NAME_SIZE+1];
622 for(int i=0;i<nfam && !found;i++)
624 med_int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
625 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
626 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
627 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
628 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
629 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
630 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
631 std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
632 found=(cur==famName);
634 for(int j=0;j<ngro;j++)
636 std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
642 std::ostringstream oss;
643 oss << "GetMeshGroupsNamesOnFamily : no such family \"" << famName << "\" in file \"" << fileName << "\" in mesh \"" << meshName << "\" !";
644 throw INTERP_KERNEL::Exception(oss.str().c_str());
650 std::vector<std::string> MEDCoupling::GetMeshGroupsNames(const std::string& fileName, const std::string& meshName)
652 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
653 med_int nfam=MEDnFamily(fid,meshName.c_str());
654 std::vector<std::string> ret;
655 char nomfam[MED_NAME_SIZE+1];
657 for(int i=0;i<nfam;i++)
659 med_int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
660 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
661 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
662 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
663 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
664 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
665 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
666 for(int j=0;j<ngro;j++)
668 std::string cur=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
669 if(std::find(ret.begin(),ret.end(),cur)==ret.end())
676 std::vector<MEDCoupling::TypeOfField> MEDCoupling::GetTypesOfField(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
678 std::vector<MEDCoupling::TypeOfField> ret;
679 MCAuto<MEDFileAnyTypeFieldMultiTS> fs(MEDFileAnyTypeFieldMultiTS::New(fileName,fieldName,false));
680 if(fs->getMeshName()!=meshName)
682 std::ostringstream oss; oss << "GetTypesOfField : The field \"" << fieldName << "\" in file \"" << fileName << "\" is not lying on mesh \"" << meshName << "\"";
683 oss << " The name of the mesh in file is \"" << fs->getMeshName() << "\"!";
684 throw INTERP_KERNEL::Exception(oss.str().c_str());
686 int nbTS(fs->getNumberOfTS());
689 for(int i=0;i<nbTS;i++)
691 MCAuto<MEDFileAnyTypeField1TS> f1ts(fs->getTimeStepAtPos(i));
692 std::vector<MEDCoupling::TypeOfField> tof(f1ts->getTypesOfFieldAvailable());
693 for(std::vector<MEDCoupling::TypeOfField>::const_iterator it=tof.begin();it!=tof.end();it++)
694 if(std::find(ret.begin(),ret.end(),*it)==ret.end())
697 // sort ret to put before ON_NODES then ON_CELLS then the remaining.
698 std::vector<MEDCoupling::TypeOfField> ret2;
699 if(std::find(ret.begin(),ret.end(),ON_NODES)!=ret.end())
700 ret2.push_back(ON_NODES);
701 if(std::find(ret.begin(),ret.end(),ON_CELLS)!=ret.end())
702 ret2.push_back(ON_CELLS);
703 for(std::vector<MEDCoupling::TypeOfField>::const_iterator it=ret.begin();it!=ret.end();it++)
704 if(*it!=ON_NODES && *it!=ON_CELLS)
709 std::vector<std::string> MEDCoupling::GetAllFieldNames(const std::string& fileName)
711 std::vector<std::string> ret;
712 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
713 med_int nbFields=MEDnField(fid);
714 med_field_type typcha;
715 for(int i=0;i<nbFields;i++)
717 med_int ncomp(MEDfieldnComponent(fid,i+1));
718 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
719 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
720 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
721 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
722 INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
725 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
726 ret.push_back(std::string(nomcha));
731 std::vector<std::string> MEDCoupling::GetAllFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
733 std::vector<std::string> ret;
734 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
735 med_int nbFields=MEDnField(fid);
737 med_field_type typcha;
738 char *maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
739 char *nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
741 for(int i=0;i<nbFields;i++)
743 med_int ncomp(MEDfieldnComponent(fid,i+1));
744 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
745 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
746 INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
749 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
750 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
751 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
753 if(curMeshName==meshName)
754 ret.push_back(curFieldName);
761 std::vector<std::string> MEDCoupling::GetFieldNamesOnMesh(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName)
763 MEDCoupling::CheckFileForRead(fileName);
767 return GetCellFieldNamesOnMesh(fileName,meshName);
769 return GetNodeFieldNamesOnMesh(fileName,meshName);
771 throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
775 std::vector<std::string> MEDCoupling::GetCellFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
777 std::vector<std::string> ret;
778 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
779 med_int nbFields=MEDnField(fid);
781 med_field_type typcha;
782 //med_int nbpdtnor=0,pflsize,*pflval,lnsize;
783 med_int numdt=0,numo=0;
785 char pflname[MED_NAME_SIZE+1]="";
786 char locname[MED_NAME_SIZE+1]="";
787 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
788 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
789 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
793 for(int i=0;i<nbFields;i++)
795 med_int ncomp(MEDfieldnComponent(fid,i+1));
796 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
797 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
798 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
799 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
800 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
801 med_int profilesize,nbi;
802 if(curMeshName==meshName)
805 for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
809 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,1,&numdt,&numo,&dt));
810 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
811 pflname,&profilesize,locname,&nbi));
815 ret.push_back(curFieldName);
824 std::vector<std::string> MEDCoupling::GetNodeFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
826 std::vector<std::string> ret;
827 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
828 med_int nbFields=MEDnField(fid);
829 char pflname[MED_NAME_SIZE+1]="";
830 char locname[MED_NAME_SIZE+1]="";
832 med_field_type typcha;
833 med_int numdt=0,numo=0;
835 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
836 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
837 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
840 for(int i=0;i<nbFields;i++)
842 med_int ncomp(MEDfieldnComponent(fid,i+1));
843 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
844 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
846 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
847 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
848 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
851 med_int profilesize,nbi;
852 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,1,&numdt,&numo,&dt));
853 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
854 pflname,&profilesize,locname,&nbi));
855 if(curMeshName==meshName && nbOfVal>0)
857 ret.push_back(curFieldName);
864 std::vector< std::pair< std::pair<int,int>, double> > MEDCoupling::GetAllFieldIterations(const std::string& fileName, const std::string& fieldName)
866 std::vector< std::pair< std::pair<int,int>, double > > ret;
867 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
868 med_int nbFields=MEDnField(fid);
870 med_field_type typcha;
871 med_int numdt=0,numo=0;
873 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
874 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
875 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
878 std::ostringstream oss; oss << "GetAllFieldIterations : No field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
879 for(int i=0;i<nbFields;i++)
881 med_int ncomp(MEDfieldnComponent(fid,i+1));
882 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
883 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
885 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
886 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
887 if(curFieldName==fieldName)
889 for(int k=0;k<nbPdt;k++)
891 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
892 ret.push_back(std::make_pair(std::make_pair(numdt,numo),dt));
898 oss << "\"" << curFieldName << "\"";
899 if(i!=nbFields-1) oss << ", ";
903 throw INTERP_KERNEL::Exception(oss.str().c_str());
906 double MEDCoupling::GetTimeAttachedOnFieldIteration(const std::string& fileName, const std::string& fieldName, int iteration, int order)
908 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
909 med_int nbFields=MEDnField(fid);
911 med_field_type typcha;
912 med_int numdt=0,numo=0;
915 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
916 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
917 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
921 double ret=std::numeric_limits<double>::max();
922 for(int i=0;i<nbFields && !found;i++)
924 med_int ncomp(MEDfieldnComponent(fid,i+1));
925 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
926 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
928 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&local,&typcha,comp,unit,dt_unit,&nbPdt));
929 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
930 if(curFieldName==fieldName)
933 for(int k=0;k<nbPdt;k++)
935 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
936 if(numdt==iteration && numo==order)
944 if(!found || !found2)
946 std::ostringstream oss;
947 oss << "No such field with name \"" << fieldName << "\" and iteration,order=(" << iteration << "," << order << ") exists in file \"" << fileName << "\" !";
948 throw INTERP_KERNEL::Exception(oss.str().c_str());
953 std::vector< std::pair<int,int> > MEDCoupling::GetFieldIterations(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, const std::string& fieldName)
955 MEDCoupling::CheckFileForRead(fileName);
959 return GetCellFieldIterations(fileName,meshName,fieldName);
961 return GetNodeFieldIterations(fileName,meshName,fieldName);
963 throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
967 std::vector< std::pair<int,int> > MEDCoupling::GetCellFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
969 std::string meshNameCpp(meshName);
970 std::vector< std::pair<int,int> > ret;
971 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
972 med_int nbFields=MEDnField(fid);
974 med_field_type typcha;
975 med_int numdt=0,numo=0;
977 char pflname[MED_NAME_SIZE+1]="";
978 char locname[MED_NAME_SIZE+1]="";
979 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
980 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
981 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
984 std::ostringstream oss; oss << "GetCellFieldIterations : No cell Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
985 std::set<std::string> s2;
986 for(int i=0;i<nbFields;i++)
988 med_int ncomp(MEDfieldnComponent(fid,i+1));
989 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
990 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
992 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
993 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
994 if(curFieldName==fieldName)
997 for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
999 for(int k=0;k<nbPdt;k++)
1001 med_int profilesize,nbi;
1002 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
1003 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
1004 pflname,&profilesize,locname,&nbi));
1005 std::string maa_ass_cpp(maa_ass);
1008 if(meshNameCpp==maa_ass_cpp)
1011 ret.push_back(std::make_pair(numdt,numo));
1014 s2.insert(maa_ass_cpp);
1021 oss << "\"" << curFieldName << "\"";
1022 if(i!=nbFields-1) oss << ", ";
1029 oss << ". Cell Field \"" << fieldName << "\" exists but lies on meshes with names : \"";
1030 std::copy(s2.begin(),s2.end(),std::ostream_iterator<std::string>(oss,"\", \""));
1033 throw INTERP_KERNEL::Exception(oss.str().c_str());
1038 std::vector< std::pair<int,int> > MEDCoupling::GetNodeFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
1040 std::string meshNameCpp(meshName);
1041 std::vector< std::pair<int,int> > ret;
1042 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
1043 med_int nbFields=MEDnField(fid);
1045 med_field_type typcha;
1046 med_int numdt=0,numo=0;
1048 char pflname[MED_NAME_SIZE+1]="";
1049 char locname[MED_NAME_SIZE+1]="";
1050 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
1051 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
1052 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
1055 std::ostringstream oss; oss << "GetNodeFieldIterations : No node Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
1056 std::set<std::string> s2;
1057 for(int i=0;i<nbFields;i++)
1059 med_int ncomp(MEDfieldnComponent(fid,i+1));
1060 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
1061 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
1063 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
1064 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
1065 if(curFieldName==fieldName)
1067 for(int k=0;k<nbPdt;k++)
1069 med_int profilesize,nbi;
1070 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
1071 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
1072 pflname,&profilesize,locname,&nbi));
1073 std::string maa_ass_cpp(maa_ass);
1076 if(meshNameCpp==maa_ass_cpp)
1077 { ret.push_back(std::make_pair(numdt,numo)); }
1079 s2.insert(maa_ass_cpp);
1085 oss << "\"" << curFieldName << "\"";
1086 if(i!=nbFields-1) oss << ", ";
1093 oss << ". Node Field \"" << fieldName << "\" exists but lies on meshes with names : \"";
1094 std::copy(s2.begin(),s2.end(),std::ostream_iterator<std::string>(oss,"\", \""));
1097 throw INTERP_KERNEL::Exception(oss.str().c_str());
1102 MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax)
1104 MEDCoupling::CheckFileForRead(fileName);
1105 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1106 MEDFileMesh *mmPtr(mm);
1107 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1109 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1110 MEDFileCMesh *mmcPtr=dynamic_cast<MEDFileCMesh *>(mmPtr);
1113 const MEDCouplingCMesh *ret(mmcPtr->getMesh()); ret->incrRef();
1114 return const_cast<MEDCouplingCMesh *>(ret);
1116 MEDFileCurveLinearMesh *mmc2Ptr=dynamic_cast<MEDFileCurveLinearMesh *>(mmPtr);
1119 const MEDCouplingCurveLinearMesh *ret(mmc2Ptr->getMesh()); ret->incrRef();
1120 return const_cast<MEDCouplingCurveLinearMesh *>(ret);
1122 std::ostringstream oss; oss << "ReadMeshFromFile : The mesh \"" << meshName << "\" in file \"" << fileName << "\" has not a recognized type !";
1123 throw INTERP_KERNEL::Exception(oss.str().c_str());
1126 MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& fileName, int meshDimRelToMax)
1128 MEDCoupling::CheckFileForRead(fileName);
1129 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName));
1130 MEDFileMesh *mmPtr(mm);
1131 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1133 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1134 MEDFileCMesh *mmcPtr=dynamic_cast<MEDFileCMesh *>(mmPtr);
1137 const MEDCouplingCMesh *ret(mmcPtr->getMesh()); ret->incrRef();
1138 return const_cast<MEDCouplingCMesh *>(ret);
1140 MEDFileCurveLinearMesh *mmc2Ptr=dynamic_cast<MEDFileCurveLinearMesh *>(mmPtr);
1143 const MEDCouplingCurveLinearMesh *ret(mmc2Ptr->getMesh()); ret->incrRef();
1144 return const_cast<MEDCouplingCurveLinearMesh *>(ret);
1146 std::ostringstream oss; oss << "ReadMeshFromFile (2) : The first mesh \"" << mm->getName() << "\" in file \"" << fileName << "\" has not a recognized type !";
1147 throw INTERP_KERNEL::Exception(oss.str().c_str());
1150 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax)
1152 MEDCoupling::CheckFileForRead(fileName);
1153 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1154 MEDFileMesh *mmPtr(mm);
1155 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1158 std::ostringstream oss; oss << "ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName=\""<< meshName << "\" exists but it is not an unstructured mesh !";
1159 throw INTERP_KERNEL::Exception(oss.str().c_str());
1161 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1164 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFile(const std::string& fileName, int meshDimRelToMax)
1166 MEDCoupling::CheckFileForRead(fileName);
1167 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName));
1168 MEDFileMesh *mmPtr(mm);
1169 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1172 std::ostringstream oss; oss << "ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1173 throw INTERP_KERNEL::Exception(oss.str().c_str());
1175 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1178 int MEDCoupling::ReadUMeshDimFromFile(const std::string& fileName, const std::string& meshName)
1180 MEDCoupling::CheckFileForRead(fileName);
1181 std::vector<int> poss;
1182 return MEDLoaderNS::readUMeshDimFromFile(fileName,meshName,poss);
1185 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFamilies(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::vector<std::string>& fams)
1187 MEDCoupling::CheckFileForRead(fileName);
1188 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1189 MEDFileMesh *mmPtr(mm);
1190 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1193 std::ostringstream oss; oss << "ReadUMeshFromFamilies : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1194 throw INTERP_KERNEL::Exception(oss.str().c_str());
1196 return mmuPtr->getFamilies(meshDimRelToMax,fams,true);
1199 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromGroups(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::vector<std::string>& grps)
1201 MEDCoupling::CheckFileForRead(fileName);
1202 MCAuto<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1203 MEDFileMesh *mmPtr(mm);
1204 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1207 std::ostringstream oss; oss << "ReadUMeshFromGroups : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1208 throw INTERP_KERNEL::Exception(oss.str().c_str());
1210 return mmuPtr->getGroups(meshDimRelToMax,grps,true);
1213 MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(const std::string& fileName)
1215 std::vector<std::string> fieldNames(GetAllFieldNames(fileName));
1216 std::size_t sz(fieldNames.size());
1219 std::ostringstream oss;
1220 oss << "The file \"" << fileName << "\" contains no field !";
1221 throw INTERP_KERNEL::Exception(oss.str());
1225 std::ostringstream oss;
1226 oss << "In file \"" << fileName << "\" there are more than one field !" << std::endl;
1227 oss << "You are invited to use ReadField(fileName, fieldName) instead to avoid misleading concerning field you want to read !" << std::endl;
1228 oss << "For information, fields available are :" << std::endl;
1229 for(std::vector<std::string>::const_iterator it=fieldNames.begin();it!=fieldNames.end();it++)
1230 oss << " - \"" << *it << "\"" << std::endl;
1231 throw INTERP_KERNEL::Exception(oss.str());
1233 return ReadField(fileName,fieldNames[0]);
1236 MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(const std::string& fileName, const std::string& fieldName)
1238 std::vector< std::pair< std::pair<int,int>, double> > iterations(GetAllFieldIterations(fileName,fieldName));
1239 std::size_t sz(iterations.size());
1242 std::ostringstream oss;
1243 oss << "In file \"" << fileName << "\" field \"" << fieldName << "\" exists but with no time steps !";
1244 throw INTERP_KERNEL::Exception(oss.str());
1248 std::ostringstream oss;
1249 oss << "In file \"" << fileName << "\" field \"" << fieldName << "\" exists but with more than one time steps !" << std::endl;
1250 oss << "You are invited to use ReadField(fileName, fieldName, iteration, order) instead to avoid misleading concerning time steps." << std::endl;
1251 oss << "For information, time steps available for field \"" << fieldName << "\" are :" << std::endl;
1252 for(std::vector< std::pair< std::pair<int,int>, double> >::const_iterator it=iterations.begin();it!=iterations.end();it++)
1253 oss << " - " << (*it).first.first << ", " << (*it).first.second << " (" << (*it).second << ")" << std::endl;
1254 throw INTERP_KERNEL::Exception(oss.str());
1256 return ReadField(fileName,fieldName,iterations[0].first.first,iterations[0].first.second);
1259 MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(const std::string& fileName, const std::string& fieldName, int iteration, int order)
1261 MCAuto<MEDFileAnyTypeField1TS> f(MEDFileAnyTypeField1TS::New(fileName,fieldName,iteration,order));
1262 MCAuto<MEDFileMesh> mesh(MEDFileMesh::New(fileName,f->getMeshName()));
1264 MCAuto<MEDFileField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileField1TS>(f));
1267 MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(f1->field(mesh));
1268 return MEDCoupling::DynamicCast<MEDCouplingFieldDouble,MEDCouplingField>(ret);
1272 MCAuto<MEDFileInt32Field1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileInt32Field1TS>(f));
1275 MCAuto<MEDCoupling::MEDCouplingFieldInt32> ret(f1->field(mesh));
1276 return MEDCoupling::DynamicCast<MEDCouplingFieldInt32,MEDCouplingField>(ret);
1280 MCAuto<MEDFileFloatField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileFloatField1TS>(f));
1283 MCAuto<MEDCoupling::MEDCouplingFieldFloat> ret(f1->field(mesh));
1284 return MEDCoupling::DynamicCast<MEDCouplingFieldFloat,MEDCouplingField>(ret);
1287 throw INTERP_KERNEL::Exception("MEDCoupling::ReadField : only FLOAT32, FLOAT64 and INT32 supported for the moment !");
1290 MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1292 MEDCoupling::CheckFileForRead(fileName);
1296 return ReadFieldCell(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1298 return ReadFieldNode(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1300 return ReadFieldGauss(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1302 return ReadFieldGaussNE(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1304 throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES, ON_CELLS, ON_GAUSS_PT or ON_GAUSS_NE !");
1308 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsOnSameMesh(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1309 const std::vector<std::pair<int,int> >& its)
1312 return std::vector<MEDCoupling::MEDCouplingFieldDouble *>();
1313 MEDCoupling::CheckFileForRead(fileName);
1314 std::vector<MEDCoupling::MEDCouplingFieldDouble *> ret(its.size());
1315 std::vector< MCAuto<MEDCouplingFieldDouble> > retSafe(its.size());
1318 //Retrieving mesh of rank 0 and field on rank 0 too.
1319 MCAuto<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1320 MEDFileMesh *mmPtr(mm);
1321 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1323 throw INTERP_KERNEL::Exception("ReadFieldsOnSameMesh : only unstructured mesh is managed !");
1324 MCAuto<MEDCouplingUMesh> m=mmuPtr->getMeshAtLevel(meshDimRelToMax);
1325 const DataArrayIdType *o2n=mmuPtr->getNumberFieldAtLevel(meshDimRelToMax);
1326 MCAuto<MEDCouplingUMesh> m2(m->clone(true));
1328 m2->renumberCells(o2n->begin(),true);
1330 for(std::vector<std::pair<int,int> >::const_iterator it=its.begin();it!=its.end();it++,i++)
1332 MCAuto<MEDFileField1TS> ff=MEDFileField1TS::New(fileName,fieldName,(*it).first,(*it).second);
1333 MCAuto<MEDCouplingFieldDouble> retElt=ff->getFieldOnMeshAtLevel(type,m);
1335 retElt->renumberCells(o2n->begin(),true);
1336 retElt->setMesh(m2);
1340 for(std::vector<std::pair<int,int> >::const_iterator it=its.begin();it!=its.end();it++,i++)
1341 ret[i]=retSafe[i].retn();
1345 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsCellOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1346 const std::vector<std::pair<int,int> >& its)
1348 return ReadFieldsOnSameMesh(ON_CELLS,fileName,meshName,meshDimRelToMax,fieldName,its);
1351 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsNodeOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1352 const std::vector<std::pair<int,int> >& its)
1354 return ReadFieldsOnSameMesh(ON_NODES,fileName,meshName,meshDimRelToMax,fieldName,its);
1357 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsGaussOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1358 const std::vector<std::pair<int,int> >& its)
1360 return ReadFieldsOnSameMesh(ON_GAUSS_PT,fileName,meshName,meshDimRelToMax,fieldName,its);
1363 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsGaussNEOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1364 const std::vector<std::pair<int,int> >& its)
1366 return ReadFieldsOnSameMesh(ON_GAUSS_NE,fileName,meshName,meshDimRelToMax,fieldName,its);
1369 namespace MEDCoupling
1372 MCAuto<typename Traits<T>::FieldType> ReadFieldCellLikeT(typename MLFieldTraits<T>::F1TSType *ff, MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1374 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1375 MCAuto<MEDFileUMesh> muPtr(MEDCoupling::DynamicCast<MEDFileMesh,MEDFileUMesh>(mm));
1376 MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1377 MCAuto<typename Traits<T>::FieldType> ret(ff->getFieldOnMeshAtLevel(type,m));
1378 if(muPtr.isNotNull())
1380 const DataArrayIdType *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1382 ret->renumberCells(num->begin());
1387 MEDCoupling::MEDCouplingField *ReadFieldCellLike(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1389 MCAuto<MEDFileAnyTypeField1TS> f(MEDFileAnyTypeField1TS::New(fileName,fieldName,iteration,order));
1391 MCAuto<MEDFileField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileField1TS>(f));
1394 MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(ReadFieldCellLikeT<double>(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1399 MCAuto<MEDFileInt32Field1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileInt32Field1TS>(f));
1402 MCAuto<MEDCoupling::MEDCouplingFieldInt32> ret(ReadFieldCellLikeT<int>(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1407 MCAuto<MEDFileFloatField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileFloatField1TS>(f));
1410 MCAuto<MEDCoupling::MEDCouplingFieldFloat> ret(ReadFieldCellLikeT<float>(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1414 throw INTERP_KERNEL::Exception("MEDCoupling::ReadFieldCell : only FLOAT32, FLOAT64 and INT32 supported for the moment !");
1418 MCAuto<typename Traits<T>::FieldType> ReadFieldNodeT(typename MLFieldTraits<T>::F1TSType *ff, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1420 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1421 MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1422 MCAuto<typename Traits<T>::FieldType> ret(ff->getFieldOnMeshAtLevel(ON_NODES,m));
1423 MCAuto<MEDFileUMesh> muPtr(MEDCoupling::DynamicCast<MEDFileMesh,MEDFileUMesh>(mm));
1424 if(ff->getPflsReallyUsed().empty())
1426 if(muPtr.isNotNull())
1428 const DataArrayIdType *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1430 ret->renumberCells(num->begin());
1435 DataArrayIdType *pfl(nullptr),*arr2(nullptr);
1436 MCAuto<typename Traits<T>::ArrayType> arr(ff->getFieldWithProfile(ON_NODES,meshDimRelToMax,mm,pfl));
1437 MCAuto<DataArrayIdType> pflSafe(pfl);
1438 MCAuto<DataArrayIdType> mp(m->getCellIdsFullyIncludedInNodeIds(pfl->begin(),pfl->end()));
1439 MCAuto<MEDCouplingUMesh> mzip(static_cast<MEDCouplingUMesh *>(m->buildPartAndReduceNodes(mp->begin(),mp->end(),arr2)));
1440 MCAuto<DataArrayIdType> arr2Safe(arr2);
1441 MCAuto<DataArrayIdType> arr3(arr2->invertArrayO2N2N2O(mzip->getNumberOfNodes()));
1442 MCAuto<DataArrayIdType> pflSorted(pflSafe->deepCopy()); pflSorted->sort(true);
1443 if(!arr3->isEqualWithoutConsideringStr(*pflSorted))
1444 throw INTERP_KERNEL::Exception("ReadFieldNode : not implemented yet !");
1445 if(!arr3->isEqualWithoutConsideringStr(*pflSafe))
1447 MCAuto<DataArrayIdType> o2n2(pflSafe->checkAndPreparePermutation());
1448 MCAuto<DataArrayIdType> n2o2(o2n2->invertArrayO2N2N2O(o2n2->getNumberOfTuples()));
1449 mzip->renumberNodes(n2o2->begin(),n2o2->getNumberOfTuples());
1459 MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldCell(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1461 return ReadFieldCellLike(ON_CELLS,fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1464 MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldNode(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1466 MCAuto<MEDFileAnyTypeField1TS> f(MEDFileAnyTypeField1TS::New(fileName,fieldName,iteration,order));
1468 MCAuto<MEDFileField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileField1TS>(f));
1471 MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(ReadFieldNodeT<double>(f1,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1476 MCAuto<MEDFileInt32Field1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileInt32Field1TS>(f));
1479 MCAuto<MEDCoupling::MEDCouplingFieldInt32> ret(ReadFieldNodeT<int>(f1,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1484 MCAuto<MEDFileFloatField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileFloatField1TS>(f));
1487 MCAuto<MEDCoupling::MEDCouplingFieldFloat> ret(ReadFieldNodeT<float>(f1,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1491 throw INTERP_KERNEL::Exception("MEDCoupling::ReadFieldNode : only FLOAT32, FLOAT64 and INT32 supported for the moment !");
1494 MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldGauss(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1496 return ReadFieldCellLike(ON_GAUSS_PT,fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1499 MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldGaussNE(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1501 return ReadFieldCellLike(ON_GAUSS_NE,fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1504 void MEDCoupling::WriteMesh(const std::string& fileName, const MEDCoupling::MEDCouplingMesh *mesh, bool writeFromScratch)
1507 throw INTERP_KERNEL::Exception("WriteMesh : input mesh is null !");
1508 const MEDCouplingUMesh *um(dynamic_cast<const MEDCouplingUMesh *>(mesh));
1511 WriteUMesh(fileName,um,writeFromScratch);
1514 int mod=writeFromScratch?2:0;
1515 const MEDCoupling1GTUMesh *um2(dynamic_cast<const MEDCoupling1GTUMesh *>(mesh));
1518 MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1519 AssignStaticWritePropertiesTo(*mmu);
1520 mmu->setMeshAtLevel(0,const_cast<MEDCoupling1GTUMesh *>(um2));
1521 mmu->write(fileName,mod);
1524 const MEDCouplingCMesh *um3(dynamic_cast<const MEDCouplingCMesh *>(mesh));
1527 MCAuto<MEDFileCMesh> mmc(MEDFileCMesh::New());
1528 AssignStaticWritePropertiesTo(*mmc);
1529 mmc->setMesh(const_cast<MEDCouplingCMesh *>(um3));
1530 mmc->write(fileName,mod);
1533 const MEDCouplingCurveLinearMesh *um4(dynamic_cast<const MEDCouplingCurveLinearMesh *>(mesh));
1536 MCAuto<MEDFileCurveLinearMesh> mmc(MEDFileCurveLinearMesh::New());
1537 AssignStaticWritePropertiesTo(*mmc);
1538 mmc->setMesh(const_cast<MEDCouplingCurveLinearMesh *>(um4));
1539 mmc->write(fileName,mod);
1542 throw INTERP_KERNEL::Exception("WriteMesh : only MEDCouplingUMesh, MEDCoupling1GTUMesh, MEDCouplingCMesh, MEDCouplingCurveLinear are dealed in this API for the moment !");
1545 void MEDCoupling::WriteUMesh(const std::string& fileName, const MEDCoupling::MEDCouplingUMesh *mesh, bool writeFromScratch)
1548 throw INTERP_KERNEL::Exception("WriteUMesh : input mesh is null !");
1549 int mod=writeFromScratch?2:0;
1550 MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1551 AssignStaticWritePropertiesTo(*m);
1552 MCAuto<MEDCouplingUMesh> mcpy(static_cast<MEDCouplingUMesh *>(mesh->deepCopy()));
1553 m->setMeshAtLevel(0,mcpy,true);
1554 m->write(fileName,mod);
1557 void MEDCoupling::WriteUMeshDep(const std::string& fileName, const MEDCoupling::MEDCouplingUMesh *mesh, bool writeFromScratch)
1559 WriteUMesh(fileName,mesh,writeFromScratch);
1562 void MEDCoupling::WriteUMeshesPartition(const std::string& fileName, const std::string& meshNameC, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1564 std::string meshName(meshNameC);
1565 if(meshName.empty())
1566 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 !");
1567 int status=MEDLoaderBase::getStatusOfFile(fileName);
1568 if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
1570 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
1571 throw INTERP_KERNEL::Exception(oss.str().c_str());
1573 MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1574 AssignStaticWritePropertiesTo(*m);
1575 m->setGroupsFromScratch(0,meshes,true);
1576 m->setName(meshNameC);
1577 int mod=writeFromScratch?2:0;
1578 m->write(fileName,mod);
1581 void MEDCoupling::WriteUMeshesPartitionDep(const std::string& fileName, const std::string& meshNameC, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1583 WriteUMeshesPartition(fileName,meshNameC,meshes,writeFromScratch);
1586 void MEDCoupling::WriteUMeshes(const std::string& fileName, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1588 int mod(writeFromScratch?2:0);
1589 MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1590 AssignStaticWritePropertiesTo(*m);
1591 m->setMeshes(meshes,true);
1592 m->write(fileName,mod);
1596 void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const std::string& fileName, const typename MEDCoupling::Traits<T>::FieldType *f, bool writeFromScratch)
1598 MCAuto< typename MLFieldTraits<T>::F1TSType > ff(MLFieldTraits<T>::F1TSType::New());
1599 AssignStaticWritePropertiesTo(*ff);
1600 MCAuto<typename MEDCoupling::Traits<T>::FieldType> f2(f->deepCopy());
1601 const MEDCouplingMesh *m(f2->getMesh());
1602 const MEDCouplingUMesh *um(dynamic_cast<const MEDCouplingUMesh *>(m));
1603 const MEDCoupling1GTUMesh *um2(dynamic_cast<const MEDCoupling1GTUMesh *>(m));
1604 const MEDCouplingCMesh *um3(dynamic_cast<const MEDCouplingCMesh *>(m));
1605 const MEDCouplingCurveLinearMesh *um4(dynamic_cast<const MEDCouplingCurveLinearMesh *>(m));
1606 MCAuto<MEDFileMesh> mm;
1607 int mod(writeFromScratch?2:0);
1610 MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1611 AssignStaticWritePropertiesTo(*mmu);
1612 MCAuto<DataArrayIdType> o2n(um->getRenumArrForMEDFileFrmt());
1613 MCAuto<DataArrayIdType> n2o(o2n->invertArrayO2N2N2O(o2n->getNumberOfTuples()));
1614 f2->renumberCells(o2n->begin(),false);
1615 mmu->setMeshAtLevel(0,const_cast<MEDCouplingUMesh *>(static_cast<const MEDCouplingUMesh *>(f2->getMesh())));
1616 mmu->setRenumFieldArr(0,n2o);
1617 ff->setFieldNoProfileSBT(f2);
1618 mmu->write(fileName,mod);
1622 MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1623 AssignStaticWritePropertiesTo(*mmu);
1624 mmu->setMeshAtLevel(0,const_cast<MEDCoupling1GTUMesh *>(um2));
1625 ff->setFieldNoProfileSBT(f2);
1626 mmu->write(fileName,mod);
1630 MCAuto<MEDFileCMesh> mmc(MEDFileCMesh::New());
1631 AssignStaticWritePropertiesTo(*mmc);
1632 mmc->setMesh(const_cast<MEDCouplingCMesh *>(um3));
1633 ff->setFieldNoProfileSBT(f2);
1634 mmc->write(fileName,mod);
1638 MCAuto<MEDFileCurveLinearMesh> mmc(MEDFileCurveLinearMesh::New());
1639 AssignStaticWritePropertiesTo(*mmc);
1640 mmc->setMesh(const_cast<MEDCouplingCurveLinearMesh *>(um4));
1641 ff->setFieldNoProfileSBT(f2);
1642 mmc->write(fileName,mod);
1645 throw INTERP_KERNEL::Exception("MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile : only MEDCouplingUMesh, MEDCoupling1GTUMesh, MEDCouplingCMesh, MEDCouplingCurveLinear are dealed in this API for the moment !");
1646 ff->write(fileName,0);
1650 void WriteFieldT(const std::string& fileName, const typename MEDCoupling::Traits<T>::FieldType *f, bool writeFromScratch)
1653 throw INTERP_KERNEL::Exception("WriteField : input field is NULL !");
1654 f->checkConsistencyLight();
1655 int status(MEDLoaderBase::getStatusOfFile(fileName));
1656 if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
1658 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
1659 throw INTERP_KERNEL::Exception(oss.str().c_str());
1661 if(writeFromScratch || (!writeFromScratch && status==MEDLoaderBase::NOT_EXIST))
1663 MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile<T>(fileName,f,true);
1667 std::vector<std::string> meshNames(GetMeshNames(fileName));
1669 throw INTERP_KERNEL::Exception("WriteField : trying to write a field with no mesh !");
1670 std::string fileNameCpp(f->getMesh()->getName());
1671 if(std::find(meshNames.begin(),meshNames.end(),fileNameCpp)==meshNames.end())
1672 MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile<T>(fileName,f,false);
1675 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,f->getMesh()->getName().c_str()));
1676 AssignStaticWritePropertiesTo(*mm);
1677 const MEDFileMesh *mmPtr(mm);
1678 const MEDFileUMesh *mmuPtr(dynamic_cast<const MEDFileUMesh *>(mmPtr));
1680 throw INTERP_KERNEL::Exception("WriteField : only umeshes are supported now !");
1681 MCAuto< typename MEDCoupling::Traits<T>::FieldType > f2(f->deepCopy());
1682 MEDCouplingUMesh *m(dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh())));
1684 throw INTERP_KERNEL::Exception("WriteField : only umesh in input field supported !");
1685 MCAuto<DataArrayIdType> o2n(m->getRenumArrForMEDFileFrmt());
1686 f2->renumberCells(o2n->begin(),false);
1687 m=static_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh()));
1688 MCAuto<MEDCouplingUMesh> mread(mmuPtr->getMeshAtLevel(m->getMeshDimension()-mm->getMeshDimension()));
1689 if(f2->getTypeOfField()!=ON_NODES)
1691 if(!m->getCoords()->isEqualWithoutConsideringStr(*mread->getCoords(),_EPS_FOR_NODE_COMP))
1692 m->tryToShareSameCoordsPermute(*mread,_EPS_FOR_NODE_COMP);
1694 mread->setCoords(m->getCoords());
1695 DataArrayIdType *part(NULL);
1696 bool b(mread->areCellsIncludedIn(m,_COMP_FOR_CELL,part));
1697 MCAuto<DataArrayIdType> partSafe(part);
1700 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 !";
1701 throw INTERP_KERNEL::Exception(oss.str().c_str());
1703 MCAuto< typename MLFieldTraits<T>::F1TSType > f1ts(MLFieldTraits<T>::F1TSType::New());
1704 AssignStaticWritePropertiesTo(*f1ts);
1705 if(part->isIota(mread->getNumberOfCells()))
1706 f1ts->setFieldNoProfileSBT(f2);
1709 part->setName(f1ts->createNewNameOfPfl().c_str());
1710 f1ts->setFieldProfile(f2,mm,m->getMeshDimension()-mm->getMeshDimension(),part);
1712 f1ts->write(fileName,0);
1717 DataArrayIdType *part(NULL);
1718 bool b(mread->getCoords()->areIncludedInMe(m->getCoords(),_EPS_FOR_NODE_COMP,part));
1719 MCAuto<DataArrayIdType> partSafe(part);
1722 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 !";
1723 throw INTERP_KERNEL::Exception(oss.str().c_str());
1725 MCAuto< typename MLFieldTraits<T>::F1TSType > f1ts(MLFieldTraits<T>::F1TSType::New());
1726 AssignStaticWritePropertiesTo(*f1ts);
1727 if(part->isIota(mread->getNumberOfNodes()))
1728 f1ts->setFieldNoProfileSBT(f2);
1731 part->setName(f1ts->createNewNameOfPfl().c_str());
1732 f1ts->setFieldProfile(f2,mm,m->getMeshDimension()-mm->getMeshDimension(),part);
1734 f1ts->write(fileName,0);
1740 void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MEDCouplingField *f, bool writeFromScratch)
1743 throw INTERP_KERNEL::Exception("WriteField : input field is null !");
1745 const MEDCoupling::MEDCouplingFieldDouble *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldDouble *>(f));
1748 WriteFieldT<double>(fileName,f1,writeFromScratch);
1753 const MEDCoupling::MEDCouplingFieldInt32 *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldInt32 *>(f));
1756 WriteFieldT<int>(fileName,f1,writeFromScratch);
1761 const MEDCoupling::MEDCouplingFieldFloat *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldFloat *>(f));
1764 WriteFieldT<float>(fileName,f1,writeFromScratch);
1768 throw INTERP_KERNEL::Exception("WriteField : input field is not in FLOAT32, FLOAT64, INT32 !");
1771 void MEDCoupling::WriteFieldDep(const std::string& fileName, const MEDCoupling::MEDCouplingField *f, bool writeFromScratch)
1773 WriteField(fileName,f,writeFromScratch);
1777 void WriteFieldUsingAlreadyWrittenMeshT(const std::string& fileName, const typename MEDCoupling::Traits<T>::FieldType *f)
1780 throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMeshT : input field is null !");
1781 f->checkConsistencyLight();
1782 int status(MEDLoaderBase::getStatusOfFile(fileName));
1783 if(status!=MEDLoaderBase::EXIST_RW)
1785 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions or not exists !";
1786 throw INTERP_KERNEL::Exception(oss.str().c_str());
1788 MCAuto< typename MLFieldTraits<T>::F1TSType > f1ts(MLFieldTraits<T>::F1TSType::New());
1789 AssignStaticWritePropertiesTo(*f1ts);
1790 MEDCouplingUMesh *m(dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f->getMesh())));
1793 MCAuto<DataArrayIdType> o2n(m->getRenumArrForMEDFileFrmt());
1794 MCAuto< typename MEDCoupling::Traits<T>::FieldType > f2(f->deepCopy());
1795 f2->renumberCells(o2n->begin(),false);
1796 f1ts->setFieldNoProfileSBT(f2);
1799 f1ts->setFieldNoProfileSBT(f);
1800 f1ts->write(fileName,0);
1803 void MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingField *f)
1806 throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMesh : input field is null !");
1808 const MEDCoupling::MEDCouplingFieldDouble *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldDouble *>(f));
1811 WriteFieldUsingAlreadyWrittenMeshT<double>(fileName,f1);
1816 const MEDCoupling::MEDCouplingFieldInt32 *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldInt32 *>(f));
1819 WriteFieldUsingAlreadyWrittenMeshT<int>(fileName,f1);
1824 const MEDCoupling::MEDCouplingFieldFloat *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldFloat *>(f));
1827 WriteFieldUsingAlreadyWrittenMeshT<float>(fileName,f1);
1831 throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMesh : input field is not in FLOAT32, FLOAT64, INT32 !");