1 // Copyright (C) 2007-2019 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 "MEDCouplingFieldInt.hxx"
34 #include "MEDCouplingGaussLocalization.hxx"
35 #include "MEDCouplingTraits.hxx"
38 #include "InterpKernelAutoPtr.hxx"
52 med_geometry_type typmai[MED_N_CELL_FIXED_GEO] = { MED_POINT1,
77 med_geometry_type typmainoeud[1] = { MED_NONE };
79 INTERP_KERNEL::NormalizedCellType typmai2[MED_N_CELL_FIXED_GEO] = { INTERP_KERNEL::NORM_POINT1,
80 INTERP_KERNEL::NORM_SEG2,
81 INTERP_KERNEL::NORM_SEG3,
82 INTERP_KERNEL::NORM_SEG4,
83 INTERP_KERNEL::NORM_TRI3,
84 INTERP_KERNEL::NORM_QUAD4,
85 INTERP_KERNEL::NORM_TRI6,
86 INTERP_KERNEL::NORM_TRI7,
87 INTERP_KERNEL::NORM_QUAD8,
88 INTERP_KERNEL::NORM_QUAD9,
89 INTERP_KERNEL::NORM_TETRA4,
90 INTERP_KERNEL::NORM_PYRA5,
91 INTERP_KERNEL::NORM_PENTA6,
92 INTERP_KERNEL::NORM_HEXA8,
93 INTERP_KERNEL::NORM_HEXGP12,
94 INTERP_KERNEL::NORM_TETRA10,
95 INTERP_KERNEL::NORM_PYRA13,
96 INTERP_KERNEL::NORM_PENTA15,
97 INTERP_KERNEL::NORM_PENTA18,
98 INTERP_KERNEL::NORM_HEXA20,
99 INTERP_KERNEL::NORM_HEXA27,
100 INTERP_KERNEL::NORM_POLYGON,
101 INTERP_KERNEL::NORM_QPOLYG,
102 INTERP_KERNEL::NORM_POLYHED };
104 med_geometry_type typmai3[INTERP_KERNEL::NORM_MAXTYPE] = { MED_POINT1,//0
139 double _EPS_FOR_NODE_COMP=1.e-12;
141 int _COMP_FOR_CELL=0;
145 using namespace MEDCoupling;
150 * This method returns a first quick overview of mesh with name \a meshName into the file \a fileName.
151 * @param possibilities the relativeToMeshDim authorized to returned maxdim. This vector is systematically cleared at the begin of this method.
152 * @return the maximal mesh dimension of specified mesh. If nothing found -1 is returned.
154 int MEDLoaderNS::readUMeshDimFromFile(const std::string& fileName, const std::string& meshName, std::vector<int>& possibilities)
156 possibilities.clear();
157 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
160 char nommaa[MED_NAME_SIZE+1];
161 char maillage_description[MED_COMMENT_SIZE+1];
162 med_mesh_type type_maillage;
164 std::string trueMeshName;
165 int meshId=FromMedInt<int>(getIdFromMeshName(fid,meshName,trueMeshName));
166 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
167 med_sorting_type sortingType;
169 med_axis_type axisType;
170 med_int naxis(MEDmeshnAxis(fid,meshId));
171 INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
172 INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
173 MEDFILESAFECALLERRD0(MEDmeshInfo,(fid,meshId,nommaa,&Sdim,&Mdim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit));
177 throw INTERP_KERNEL::Exception("multisteps on mesh not managed yet !");
181 MEDFILESAFECALLERRD0(MEDmeshComputationStepInfo,(fid,nommaa,1,&numdt,&numit,&dt));
183 for(int i=0;i<MED_N_CELL_GEO_FIXED_CON;i++)
185 med_geometry_type curMedType=typmai[i];
186 med_bool changement,transformation;
187 med_int curNbOfElemM(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));
188 med_int curNbOfElemF(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));//limitation
190 med_entity_type whichEntity;
191 MEDLoaderNS::dispatchElems(curNbOfElemM,curNbOfElemF,curNbOfElem,whichEntity);
194 INTERP_KERNEL::NormalizedCellType type=typmai2[i];
195 int curDim=(int)INTERP_KERNEL::CellModel::GetCellModel(type).getDimension();
202 for(std::set<int>::const_reverse_iterator it=poss.rbegin();it!=poss.rend();it++)
203 possibilities.push_back(*it-ret);
210 int MEDLoaderNS::getIdFromMeshName(med_idt fid, const std::string& meshName, std::string& trueMeshName)
214 std::vector<std::string> meshes=getMeshNamesFid(fid);
216 throw INTERP_KERNEL::Exception("No mesh in file");
217 trueMeshName=meshes[0];
220 std::string meshNameStr(meshName);
221 std::vector<std::string> meshes=getMeshNamesFid(fid);
223 throw INTERP_KERNEL::Exception("No mesh in file");
224 std::vector<std::string>::iterator iter=std::find(meshes.begin(),meshes.end(),meshNameStr);
225 if(iter==meshes.end())
227 std::ostringstream os2;
228 os2 << "MeshName '" << meshName << "' not in file : meshes available : ";
229 std::copy(meshes.begin(),meshes.end(),std::ostream_iterator<std::string>(os2," "));
230 throw INTERP_KERNEL::Exception(os2.str().c_str());
232 trueMeshName=meshName;
233 return (int)( iter-meshes.begin()+1 );
236 std::vector<std::string> MEDLoaderNS::getMeshNamesFid(med_idt fid)
238 med_mesh_type type_maillage;
239 char maillage_description[MED_COMMENT_SIZE+1];
240 char dtunit[MED_COMMENT_SIZE+1];
243 char nommaa[MED_NAME_SIZE+1];
244 med_axis_type axistype;
245 med_sorting_type stype;
246 med_int n=MEDnMesh(fid);
247 std::vector<std::string> ret(n);
250 med_int naxis(MEDmeshnAxis(fid,i+1));
251 INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
252 INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
254 MEDFILESAFECALLERRD0(MEDmeshInfo,(fid,i+1,nommaa,&space_dim,&mesh_dim,&type_maillage,maillage_description,dtunit,&stype,&nstep,&axistype,axisname,axisunit));
255 std::string cur=MEDLoaderBase::buildStringFromFortran(nommaa,sizeof(nommaa));
262 * This methods allows to merger all entities and to considerate only cell types.
264 void MEDLoaderNS::dispatchElems(med_int nbOfElemCell, med_int nbOfElemFace, med_int& nbOfElem, med_entity_type& whichEntity)
266 if(nbOfElemCell>=nbOfElemFace)
268 whichEntity=MED_CELL;
269 nbOfElem=nbOfElemCell;
273 whichEntity=MED_CELL;
274 nbOfElem=nbOfElemFace;
280 void MEDCoupling::AssignStaticWritePropertiesTo(MEDCoupling::MEDFileWritable& obj)
282 obj.setTooLongStrPolicy(_TOO_LONG_STR);
285 bool MEDCoupling::HasXDR()
294 std::string MEDCoupling::MEDFileVersionStr()
298 std::fill(buf,buf+SZ,'\0');
299 const char START_EXPECTED[]="MED-";
300 med_err ret(MEDlibraryStrVersion(buf));
302 throw INTERP_KERNEL::Exception("MEDFileVersionStr : fail to find version of MED file ! It looks very bad !");
303 std::string zeRet(buf);
304 std::size_t pos(zeRet.find(START_EXPECTED,0));
307 std::ostringstream oss; oss << "MEDFileVersionStr : internal error ! The MEDFile returned version (\"" << zeRet << "\") has not the right pattern !";
308 throw INTERP_KERNEL::Exception(oss.str());
310 return zeRet.substr(sizeof(START_EXPECTED)-1,std::string::npos);
313 std::string MEDCoupling::MEDFileVersionOfFileStr(const std::string& fileName)
315 #if MED_NUM_MAJEUR>3 || ( MED_NUM_MAJEUR==3 && ( (MED_NUM_MINEUR==2 && MED_NUM_RELEASE>=1) || MED_NUM_MINEUR>=3) )
316 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
318 const char START_EXPECTED[]="MED-";
320 std::fill(buf,buf+SZ,'\0');
321 MEDFILESAFECALLERRD0(MEDfileStrVersionRd,(fid,buf));
322 std::string ret(buf);
323 std::size_t pos(ret.find(START_EXPECTED,0));
326 std::ostringstream oss; oss << "MEDFileVersionOfFileStr : internal error ! The MEDFile returned version (\"" << ret << "\") has not the right pattern !";
327 throw INTERP_KERNEL::Exception(oss.str());
329 return ret.substr(sizeof(START_EXPECTED)-1,std::string::npos);
331 std::ostringstream oss; oss << "MEDFileVersionOfFileStr : is implemented with MEDFile " << MEDFileVersionStr() << " ! If you need this feature please use version >= 3.2.1.";
332 throw INTERP_KERNEL::Exception(oss.str());
336 void MEDCoupling::MEDFileVersion(int& major, int& minor, int& release)
338 med_int majj,minn,rell;
339 med_err ret(MEDlibraryNumVersion(&majj,&minn,&rell));
341 throw INTERP_KERNEL::Exception("MEDFileVersion : fail to call MEDlibraryNumVersion ! It looks very bad !");
342 major=FromMedInt<int>(majj);
343 minor=FromMedInt<int>(minn);
344 release=FromMedInt<int>(rell);
348 * 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.
350 void MEDCoupling::SetEpsilonForNodeComp(double val)
352 _EPS_FOR_NODE_COMP=val;
356 * 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.
358 void MEDCoupling::SetCompPolicyForCell(int val)
364 * This method set the behaviour of MEDLoader when a too long string is seen in datastructure before copy it in MED file.
365 * By default (0) an exception is thrown. If equal to 1 a warning is emitted in std_err but no exception is thrown.
367 void MEDCoupling::SetTooLongStrPolicy(int val)
373 * Given a 'fileName' and a 'meshName' this method returns global information concerning this mesh.
374 * It returns, in this order :
375 * - 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...
376 * - the mesh dimension
377 * - the space dimension
378 * - the number of nodes
380 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)
382 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
384 char nommaa[MED_NAME_SIZE+1];
385 char maillage_description[MED_COMMENT_SIZE+1];
386 med_mesh_type type_maillage;
387 std::string trueMeshName;
388 int meshId=MEDLoaderNS::getIdFromMeshName(fid,meshName,trueMeshName);
389 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
390 med_sorting_type sortingType;
391 med_int nstep, mDim, sDim;
392 med_axis_type axisType;
393 med_int naxis(MEDmeshnAxis(fid,meshId));
394 INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
395 INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
396 MEDFILESAFECALLERRD0(MEDmeshInfo,(fid,meshId,nommaa,&sDim,&mDim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit));
397 meshDim=FromMedInt<int>(mDim);
398 spaceDim=FromMedInt<int>(sDim);
399 if(type_maillage!=MED_UNSTRUCTURED_MESH)
401 std::ostringstream oss; oss << "GetUMeshGlobalInfo : Mesh \""<< meshName << "\" in file \"" << fileName;
402 oss << "\" exists but it is not an unstructured mesh ! This method is not relevant for mesh types that are not unstructured !";
403 throw INTERP_KERNEL::Exception(oss.str().c_str());
407 throw INTERP_KERNEL::Exception("GetUMeshGlobalInfo : multisteps on mesh not managed !");
410 MEDFILESAFECALLERRD0(MEDmeshComputationStepInfo,(fid,nommaa,1,&numdt,&numit,&dt));
412 std::vector<int> dims;
413 std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > geoTypes;
414 med_bool changement,transformation;
415 for(int i=0;i<MED_N_CELL_FIXED_GEO;i++)
417 med_geometry_type curMedType=typmai[i];
418 med_int curNbOfElemM(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));
421 INTERP_KERNEL::NormalizedCellType typp=typmai2[i];
422 int mdimCell=INTERP_KERNEL::CellModel::GetCellModel(typp).getDimension();
423 dims.push_back(mdimCell);
424 geoTypes.push_back(std::pair<INTERP_KERNEL::NormalizedCellType,int>(typp,curNbOfElemM));
427 int maxLev=*std::max_element(dims.begin(),dims.end());
428 int lowLev=*std::min_element(dims.begin(),dims.end());
429 int nbOfLevels=maxLev-lowLev+1;
430 std::vector< std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > > ret(nbOfLevels);
431 for(std::size_t i=0;i<dims.size();i++)
433 ret[maxLev-dims[i]].push_back(geoTypes[i]);
435 numberOfNodes=ToIdType(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation));
439 void MEDCoupling::CheckFileForRead(const std::string& fileName)
441 MEDFileUtilities::CheckFileForRead(fileName);
444 std::vector<std::string> MEDCoupling::GetMeshNames(const std::string& fileName)
446 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
447 return MEDLoaderNS::getMeshNamesFid(fid);
450 std::vector< std::pair<std::string,std::string> > MEDCoupling::GetComponentsNamesOfField(const std::string& fileName, const std::string& fieldName)
452 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
453 med_int nbFields(MEDnField(fid));
454 std::vector<std::string> fields(nbFields);
455 med_field_type typcha;
456 for(int i=0;i<nbFields;i++)
458 med_int ncomp(MEDfieldnComponent(fid,i+1));
459 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
460 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
461 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
464 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
465 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
466 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
467 std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
468 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
469 if(curFieldName==fieldName)
471 std::vector< std::pair<std::string,std::string> > ret(ncomp);
472 for(int j=0;j<ncomp;j++)
473 ret[j]=std::pair<std::string,std::string>(MEDLoaderBase::buildStringFromFortran(((char *)comp)+j*MED_SNAME_SIZE,MED_SNAME_SIZE),
474 MEDLoaderBase::buildStringFromFortran(((char *)unit)+j*MED_SNAME_SIZE,MED_SNAME_SIZE));
477 fields[i]=curFieldName;
479 std::ostringstream oss; oss << "GetComponentsNamesOfField : no such field \"" << fieldName << "\" in file \"" << fileName << "\" !" << std::endl;
480 oss << "Possible field names are : " << std::endl;
481 std::copy(fields.begin(),fields.end(),std::ostream_iterator<std::string>(oss," "));
482 throw INTERP_KERNEL::Exception(oss.str().c_str());
485 std::vector<std::string> MEDCoupling::GetMeshNamesOnField(const std::string& fileName, const std::string& fieldName)
487 std::vector<std::string> ret;
489 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
490 med_int nbFields=MEDnField(fid);
492 med_field_type typcha;
493 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
494 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
497 for(int i=0;i<nbFields;i++)
499 med_int ncomp(MEDfieldnComponent(fid,i+1));
500 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
501 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
503 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
504 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
505 std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
506 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
507 if(curFieldName==fieldName)
508 ret.push_back(meshName);
513 std::vector<std::string> MEDCoupling::GetMeshFamiliesNames(const std::string& fileName, const std::string& meshName)
515 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
516 med_int nfam=MEDnFamily(fid,meshName.c_str());
517 std::vector<std::string> ret(nfam);
518 char nomfam[MED_NAME_SIZE+1];
520 for(int i=0;i<nfam;i++)
522 med_int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
523 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
524 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
525 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
526 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
527 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
528 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
529 std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
536 std::vector<std::string> MEDCoupling::GetMeshFamiliesNamesOnGroup(const std::string& fileName, const std::string& meshName, const std::string& grpName)
538 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
539 med_int nfam=MEDnFamily(fid,meshName.c_str());
540 std::vector<std::string> ret;
541 char nomfam[MED_NAME_SIZE+1];
543 for(int i=0;i<nfam;i++)
545 med_int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
546 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
547 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
548 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
549 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
550 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
551 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
552 std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
553 for(int j=0;j<ngro;j++)
555 std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
563 std::vector<std::string> MEDCoupling::GetMeshGroupsNamesOnFamily(const std::string& fileName, const std::string& meshName, const std::string& famName)
565 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
566 med_int nfam(MEDnFamily(fid,meshName.c_str()));
567 std::vector<std::string> ret;
568 char nomfam[MED_NAME_SIZE+1];
571 for(int i=0;i<nfam && !found;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));
581 found=(cur==famName);
583 for(int j=0;j<ngro;j++)
585 std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
591 std::ostringstream oss;
592 oss << "GetMeshGroupsNamesOnFamily : no such family \"" << famName << "\" in file \"" << fileName << "\" in mesh \"" << meshName << "\" !";
593 throw INTERP_KERNEL::Exception(oss.str().c_str());
599 std::vector<std::string> MEDCoupling::GetMeshGroupsNames(const std::string& fileName, const std::string& meshName)
601 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
602 med_int nfam=MEDnFamily(fid,meshName.c_str());
603 std::vector<std::string> ret;
604 char nomfam[MED_NAME_SIZE+1];
606 for(int i=0;i<nfam;i++)
608 med_int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
609 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
610 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
611 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
612 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
613 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
614 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
615 for(int j=0;j<ngro;j++)
617 std::string cur=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
618 if(std::find(ret.begin(),ret.end(),cur)==ret.end())
625 std::vector<MEDCoupling::TypeOfField> MEDCoupling::GetTypesOfField(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
627 std::vector<MEDCoupling::TypeOfField> ret;
628 MCAuto<MEDFileAnyTypeFieldMultiTS> fs(MEDFileAnyTypeFieldMultiTS::New(fileName,fieldName,false));
629 if(fs->getMeshName()!=meshName)
631 std::ostringstream oss; oss << "GetTypesOfField : The field \"" << fieldName << "\" in file \"" << fileName << "\" is not lying on mesh \"" << meshName << "\"";
632 oss << " The name of the mesh in file is \"" << fs->getMeshName() << "\"!";
633 throw INTERP_KERNEL::Exception(oss.str().c_str());
635 int nbTS(fs->getNumberOfTS());
638 for(int i=0;i<nbTS;i++)
640 MCAuto<MEDFileAnyTypeField1TS> f1ts(fs->getTimeStepAtPos(i));
641 std::vector<MEDCoupling::TypeOfField> tof(f1ts->getTypesOfFieldAvailable());
642 for(std::vector<MEDCoupling::TypeOfField>::const_iterator it=tof.begin();it!=tof.end();it++)
643 if(std::find(ret.begin(),ret.end(),*it)==ret.end())
646 // sort ret to put before ON_NODES then ON_CELLS then the remaining.
647 std::vector<MEDCoupling::TypeOfField> ret2;
648 if(std::find(ret.begin(),ret.end(),ON_NODES)!=ret.end())
649 ret2.push_back(ON_NODES);
650 if(std::find(ret.begin(),ret.end(),ON_CELLS)!=ret.end())
651 ret2.push_back(ON_CELLS);
652 for(std::vector<MEDCoupling::TypeOfField>::const_iterator it=ret.begin();it!=ret.end();it++)
653 if(*it!=ON_NODES && *it!=ON_CELLS)
658 std::vector<std::string> MEDCoupling::GetAllFieldNames(const std::string& fileName)
660 std::vector<std::string> ret;
661 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
662 med_int nbFields=MEDnField(fid);
663 med_field_type typcha;
664 for(int i=0;i<nbFields;i++)
666 med_int ncomp(MEDfieldnComponent(fid,i+1));
667 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
668 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
669 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
670 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
671 INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
674 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
675 ret.push_back(std::string(nomcha));
680 std::vector<std::string> MEDCoupling::GetAllFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
682 std::vector<std::string> ret;
683 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
684 med_int nbFields=MEDnField(fid);
686 med_field_type typcha;
687 char *maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
688 char *nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
690 for(int i=0;i<nbFields;i++)
692 med_int ncomp(MEDfieldnComponent(fid,i+1));
693 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
694 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
695 INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
698 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
699 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
700 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
702 if(curMeshName==meshName)
703 ret.push_back(curFieldName);
710 std::vector<std::string> MEDCoupling::GetFieldNamesOnMesh(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName)
712 MEDCoupling::CheckFileForRead(fileName);
716 return GetCellFieldNamesOnMesh(fileName,meshName);
718 return GetNodeFieldNamesOnMesh(fileName,meshName);
720 throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
724 std::vector<std::string> MEDCoupling::GetCellFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
726 std::vector<std::string> ret;
727 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
728 med_int nbFields=MEDnField(fid);
730 med_field_type typcha;
731 //med_int nbpdtnor=0,pflsize,*pflval,lnsize;
732 med_int numdt=0,numo=0;
734 char pflname[MED_NAME_SIZE+1]="";
735 char locname[MED_NAME_SIZE+1]="";
736 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
737 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
738 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
742 for(int i=0;i<nbFields;i++)
744 med_int ncomp(MEDfieldnComponent(fid,i+1));
745 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
746 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
747 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
748 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
749 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
750 med_int profilesize,nbi;
751 if(curMeshName==meshName)
754 for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
758 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,1,&numdt,&numo,&dt));
759 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
760 pflname,&profilesize,locname,&nbi));
764 ret.push_back(curFieldName);
773 std::vector<std::string> MEDCoupling::GetNodeFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
775 std::vector<std::string> ret;
776 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
777 med_int nbFields=MEDnField(fid);
778 char pflname[MED_NAME_SIZE+1]="";
779 char locname[MED_NAME_SIZE+1]="";
781 med_field_type typcha;
782 med_int numdt=0,numo=0;
784 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
785 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
786 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
789 for(int i=0;i<nbFields;i++)
791 med_int ncomp(MEDfieldnComponent(fid,i+1));
792 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
793 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
795 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
796 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
797 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
800 med_int profilesize,nbi;
801 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,1,&numdt,&numo,&dt));
802 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
803 pflname,&profilesize,locname,&nbi));
804 if(curMeshName==meshName && nbOfVal>0)
806 ret.push_back(curFieldName);
813 std::vector< std::pair< std::pair<int,int>, double> > MEDCoupling::GetAllFieldIterations(const std::string& fileName, const std::string& fieldName)
815 std::vector< std::pair< std::pair<int,int>, double > > ret;
816 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
817 med_int nbFields=MEDnField(fid);
819 med_field_type typcha;
820 med_int numdt=0,numo=0;
822 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
823 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
824 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
827 std::ostringstream oss; oss << "GetAllFieldIterations : No field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
828 for(int i=0;i<nbFields;i++)
830 med_int ncomp(MEDfieldnComponent(fid,i+1));
831 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
832 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
834 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
835 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
836 if(curFieldName==fieldName)
838 for(int k=0;k<nbPdt;k++)
840 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
841 ret.push_back(std::make_pair(std::make_pair(numdt,numo),dt));
847 oss << "\"" << curFieldName << "\"";
848 if(i!=nbFields-1) oss << ", ";
852 throw INTERP_KERNEL::Exception(oss.str().c_str());
855 double MEDCoupling::GetTimeAttachedOnFieldIteration(const std::string& fileName, const std::string& fieldName, int iteration, int order)
857 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
858 med_int nbFields=MEDnField(fid);
860 med_field_type typcha;
861 med_int numdt=0,numo=0;
864 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
865 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
866 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
870 double ret=std::numeric_limits<double>::max();
871 for(int i=0;i<nbFields && !found;i++)
873 med_int ncomp(MEDfieldnComponent(fid,i+1));
874 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
875 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
877 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&local,&typcha,comp,unit,dt_unit,&nbPdt));
878 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
879 if(curFieldName==fieldName)
882 for(int k=0;k<nbPdt;k++)
884 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
885 if(numdt==iteration && numo==order)
893 if(!found || !found2)
895 std::ostringstream oss;
896 oss << "No such field with name \"" << fieldName << "\" and iteration,order=(" << iteration << "," << order << ") exists in file \"" << fileName << "\" !";
897 throw INTERP_KERNEL::Exception(oss.str().c_str());
902 std::vector< std::pair<int,int> > MEDCoupling::GetFieldIterations(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, const std::string& fieldName)
904 MEDCoupling::CheckFileForRead(fileName);
908 return GetCellFieldIterations(fileName,meshName,fieldName);
910 return GetNodeFieldIterations(fileName,meshName,fieldName);
912 throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
916 std::vector< std::pair<int,int> > MEDCoupling::GetCellFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
918 std::string meshNameCpp(meshName);
919 std::vector< std::pair<int,int> > ret;
920 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
921 med_int nbFields=MEDnField(fid);
923 med_field_type typcha;
924 med_int numdt=0,numo=0;
926 char pflname[MED_NAME_SIZE+1]="";
927 char locname[MED_NAME_SIZE+1]="";
928 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
929 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
930 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
933 std::ostringstream oss; oss << "GetCellFieldIterations : No cell Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
934 std::set<std::string> s2;
935 for(int i=0;i<nbFields;i++)
937 med_int ncomp(MEDfieldnComponent(fid,i+1));
938 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
939 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
941 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
942 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
943 if(curFieldName==fieldName)
946 for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
948 for(int k=0;k<nbPdt;k++)
950 med_int profilesize,nbi;
951 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
952 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
953 pflname,&profilesize,locname,&nbi));
954 std::string maa_ass_cpp(maa_ass);
957 if(meshNameCpp==maa_ass_cpp)
960 ret.push_back(std::make_pair(numdt,numo));
963 s2.insert(maa_ass_cpp);
970 oss << "\"" << curFieldName << "\"";
971 if(i!=nbFields-1) oss << ", ";
978 oss << ". Cell Field \"" << fieldName << "\" exists but lies on meshes with names : \"";
979 std::copy(s2.begin(),s2.end(),std::ostream_iterator<std::string>(oss,"\", \""));
982 throw INTERP_KERNEL::Exception(oss.str().c_str());
987 std::vector< std::pair<int,int> > MEDCoupling::GetNodeFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
989 std::string meshNameCpp(meshName);
990 std::vector< std::pair<int,int> > ret;
991 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
992 med_int nbFields=MEDnField(fid);
994 med_field_type typcha;
995 med_int numdt=0,numo=0;
997 char pflname[MED_NAME_SIZE+1]="";
998 char locname[MED_NAME_SIZE+1]="";
999 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
1000 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
1001 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
1004 std::ostringstream oss; oss << "GetNodeFieldIterations : No node Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
1005 std::set<std::string> s2;
1006 for(int i=0;i<nbFields;i++)
1008 med_int ncomp(MEDfieldnComponent(fid,i+1));
1009 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
1010 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
1012 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
1013 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
1014 if(curFieldName==fieldName)
1016 for(int k=0;k<nbPdt;k++)
1018 med_int profilesize,nbi;
1019 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
1020 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
1021 pflname,&profilesize,locname,&nbi));
1022 std::string maa_ass_cpp(maa_ass);
1025 if(meshNameCpp==maa_ass_cpp)
1026 { ret.push_back(std::make_pair(numdt,numo)); }
1028 s2.insert(maa_ass_cpp);
1034 oss << "\"" << curFieldName << "\"";
1035 if(i!=nbFields-1) oss << ", ";
1042 oss << ". Node Field \"" << fieldName << "\" exists but lies on meshes with names : \"";
1043 std::copy(s2.begin(),s2.end(),std::ostream_iterator<std::string>(oss,"\", \""));
1046 throw INTERP_KERNEL::Exception(oss.str().c_str());
1051 MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax)
1053 MEDCoupling::CheckFileForRead(fileName);
1054 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1055 MEDFileMesh *mmPtr(mm);
1056 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1058 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1059 MEDFileCMesh *mmcPtr=dynamic_cast<MEDFileCMesh *>(mmPtr);
1062 const MEDCouplingCMesh *ret(mmcPtr->getMesh()); ret->incrRef();
1063 return const_cast<MEDCouplingCMesh *>(ret);
1065 MEDFileCurveLinearMesh *mmc2Ptr=dynamic_cast<MEDFileCurveLinearMesh *>(mmPtr);
1068 const MEDCouplingCurveLinearMesh *ret(mmc2Ptr->getMesh()); ret->incrRef();
1069 return const_cast<MEDCouplingCurveLinearMesh *>(ret);
1071 std::ostringstream oss; oss << "ReadMeshFromFile : The mesh \"" << meshName << "\" in file \"" << fileName << "\" has not a recognized type !";
1072 throw INTERP_KERNEL::Exception(oss.str().c_str());
1075 MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& fileName, int meshDimRelToMax)
1077 MEDCoupling::CheckFileForRead(fileName);
1078 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName));
1079 MEDFileMesh *mmPtr(mm);
1080 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1082 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1083 MEDFileCMesh *mmcPtr=dynamic_cast<MEDFileCMesh *>(mmPtr);
1086 const MEDCouplingCMesh *ret(mmcPtr->getMesh()); ret->incrRef();
1087 return const_cast<MEDCouplingCMesh *>(ret);
1089 MEDFileCurveLinearMesh *mmc2Ptr=dynamic_cast<MEDFileCurveLinearMesh *>(mmPtr);
1092 const MEDCouplingCurveLinearMesh *ret(mmc2Ptr->getMesh()); ret->incrRef();
1093 return const_cast<MEDCouplingCurveLinearMesh *>(ret);
1095 std::ostringstream oss; oss << "ReadMeshFromFile (2) : The first mesh \"" << mm->getName() << "\" in file \"" << fileName << "\" has not a recognized type !";
1096 throw INTERP_KERNEL::Exception(oss.str().c_str());
1099 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax)
1101 MEDCoupling::CheckFileForRead(fileName);
1102 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1103 MEDFileMesh *mmPtr(mm);
1104 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1107 std::ostringstream oss; oss << "ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName=\""<< meshName << "\" exists but it is not an unstructured mesh !";
1108 throw INTERP_KERNEL::Exception(oss.str().c_str());
1110 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1113 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFile(const std::string& fileName, int meshDimRelToMax)
1115 MEDCoupling::CheckFileForRead(fileName);
1116 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName));
1117 MEDFileMesh *mmPtr(mm);
1118 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1121 std::ostringstream oss; oss << "ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1122 throw INTERP_KERNEL::Exception(oss.str().c_str());
1124 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1127 int MEDCoupling::ReadUMeshDimFromFile(const std::string& fileName, const std::string& meshName)
1129 MEDCoupling::CheckFileForRead(fileName);
1130 std::vector<int> poss;
1131 return MEDLoaderNS::readUMeshDimFromFile(fileName,meshName,poss);
1134 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFamilies(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::vector<std::string>& fams)
1136 MEDCoupling::CheckFileForRead(fileName);
1137 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1138 MEDFileMesh *mmPtr(mm);
1139 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1142 std::ostringstream oss; oss << "ReadUMeshFromFamilies : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1143 throw INTERP_KERNEL::Exception(oss.str().c_str());
1145 return mmuPtr->getFamilies(meshDimRelToMax,fams,true);
1148 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromGroups(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::vector<std::string>& grps)
1150 MEDCoupling::CheckFileForRead(fileName);
1151 MCAuto<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1152 MEDFileMesh *mmPtr(mm);
1153 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1156 std::ostringstream oss; oss << "ReadUMeshFromGroups : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1157 throw INTERP_KERNEL::Exception(oss.str().c_str());
1159 return mmuPtr->getGroups(meshDimRelToMax,grps,true);
1162 MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(const std::string& fileName)
1164 std::vector<std::string> fieldNames(GetAllFieldNames(fileName));
1165 std::size_t sz(fieldNames.size());
1168 std::ostringstream oss;
1169 oss << "The file \"" << fileName << "\" contains no field !";
1170 throw INTERP_KERNEL::Exception(oss.str());
1174 std::ostringstream oss;
1175 oss << "In file \"" << fileName << "\" there are more than one field !" << std::endl;
1176 oss << "You are invited to use ReadField(fileName, fieldName) instead to avoid misleading concerning field you want to read !" << std::endl;
1177 oss << "For information, fields available are :" << std::endl;
1178 for(std::vector<std::string>::const_iterator it=fieldNames.begin();it!=fieldNames.end();it++)
1179 oss << " - \"" << *it << "\"" << std::endl;
1180 throw INTERP_KERNEL::Exception(oss.str());
1182 return ReadField(fileName,fieldNames[0]);
1185 MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(const std::string& fileName, const std::string& fieldName)
1187 std::vector< std::pair< std::pair<int,int>, double> > iterations(GetAllFieldIterations(fileName,fieldName));
1188 std::size_t sz(iterations.size());
1191 std::ostringstream oss;
1192 oss << "In file \"" << fileName << "\" field \"" << fieldName << "\" exists but with no time steps !";
1193 throw INTERP_KERNEL::Exception(oss.str());
1197 std::ostringstream oss;
1198 oss << "In file \"" << fileName << "\" field \"" << fieldName << "\" exists but with more than one time steps !" << std::endl;
1199 oss << "You are invited to use ReadField(fileName, fieldName, iteration, order) instead to avoid misleading concerning time steps." << std::endl;
1200 oss << "For information, time steps available for field \"" << fieldName << "\" are :" << std::endl;
1201 for(std::vector< std::pair< std::pair<int,int>, double> >::const_iterator it=iterations.begin();it!=iterations.end();it++)
1202 oss << " - " << (*it).first.first << ", " << (*it).first.second << " (" << (*it).second << ")" << std::endl;
1203 throw INTERP_KERNEL::Exception(oss.str());
1205 return ReadField(fileName,fieldName,iterations[0].first.first,iterations[0].first.second);
1208 MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(const std::string& fileName, const std::string& fieldName, int iteration, int order)
1210 MCAuto<MEDFileAnyTypeField1TS> f(MEDFileAnyTypeField1TS::New(fileName,fieldName,iteration,order));
1211 MCAuto<MEDFileMesh> mesh(MEDFileMesh::New(fileName,f->getMeshName()));
1213 MCAuto<MEDFileField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileField1TS>(f));
1216 MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(f1->field(mesh));
1217 return MEDCoupling::DynamicCast<MEDCouplingFieldDouble,MEDCouplingField>(ret);
1221 MCAuto<MEDFileIntField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileIntField1TS>(f));
1224 MCAuto<MEDCoupling::MEDCouplingFieldInt> ret(f1->field(mesh));
1225 return MEDCoupling::DynamicCast<MEDCouplingFieldInt,MEDCouplingField>(ret);
1229 MCAuto<MEDFileFloatField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileFloatField1TS>(f));
1232 MCAuto<MEDCoupling::MEDCouplingFieldFloat> ret(f1->field(mesh));
1233 return MEDCoupling::DynamicCast<MEDCouplingFieldFloat,MEDCouplingField>(ret);
1236 throw INTERP_KERNEL::Exception("MEDCoupling::ReadField : only FLOAT32, FLOAT64 and INT32 supported for the moment !");
1239 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)
1241 MEDCoupling::CheckFileForRead(fileName);
1245 return ReadFieldCell(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1247 return ReadFieldNode(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1249 return ReadFieldGauss(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1251 return ReadFieldGaussNE(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1253 throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES, ON_CELLS, ON_GAUSS_PT or ON_GAUSS_NE !");
1257 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsOnSameMesh(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1258 const std::vector<std::pair<int,int> >& its)
1261 return std::vector<MEDCoupling::MEDCouplingFieldDouble *>();
1262 MEDCoupling::CheckFileForRead(fileName);
1263 std::vector<MEDCoupling::MEDCouplingFieldDouble *> ret(its.size());
1264 std::vector< MCAuto<MEDCouplingFieldDouble> > retSafe(its.size());
1267 //Retrieving mesh of rank 0 and field on rank 0 too.
1268 MCAuto<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1269 MEDFileMesh *mmPtr(mm);
1270 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1272 throw INTERP_KERNEL::Exception("ReadFieldsOnSameMesh : only unstructured mesh is managed !");
1273 MCAuto<MEDCouplingUMesh> m=mmuPtr->getMeshAtLevel(meshDimRelToMax);
1274 const DataArrayIdType *o2n=mmuPtr->getNumberFieldAtLevel(meshDimRelToMax);
1275 MCAuto<MEDCouplingUMesh> m2(m->clone(true));
1277 m2->renumberCells(o2n->begin(),true);
1279 for(std::vector<std::pair<int,int> >::const_iterator it=its.begin();it!=its.end();it++,i++)
1281 MCAuto<MEDFileField1TS> ff=MEDFileField1TS::New(fileName,fieldName,(*it).first,(*it).second);
1282 MCAuto<MEDCouplingFieldDouble> retElt=ff->getFieldOnMeshAtLevel(type,m);
1284 retElt->renumberCells(o2n->begin(),true);
1285 retElt->setMesh(m2);
1289 for(std::vector<std::pair<int,int> >::const_iterator it=its.begin();it!=its.end();it++,i++)
1290 ret[i]=retSafe[i].retn();
1294 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsCellOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1295 const std::vector<std::pair<int,int> >& its)
1297 return ReadFieldsOnSameMesh(ON_CELLS,fileName,meshName,meshDimRelToMax,fieldName,its);
1300 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsNodeOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1301 const std::vector<std::pair<int,int> >& its)
1303 return ReadFieldsOnSameMesh(ON_NODES,fileName,meshName,meshDimRelToMax,fieldName,its);
1306 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsGaussOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1307 const std::vector<std::pair<int,int> >& its)
1309 return ReadFieldsOnSameMesh(ON_GAUSS_PT,fileName,meshName,meshDimRelToMax,fieldName,its);
1312 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsGaussNEOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1313 const std::vector<std::pair<int,int> >& its)
1315 return ReadFieldsOnSameMesh(ON_GAUSS_NE,fileName,meshName,meshDimRelToMax,fieldName,its);
1318 namespace MEDCoupling
1321 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)
1323 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1324 MCAuto<MEDFileUMesh> muPtr(MEDCoupling::DynamicCast<MEDFileMesh,MEDFileUMesh>(mm));
1325 MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1326 MCAuto<typename Traits<T>::FieldType> ret(ff->getFieldOnMeshAtLevel(type,m));
1327 if(muPtr.isNotNull())
1329 const DataArrayIdType *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1331 ret->renumberCells(num->begin());
1336 MEDCoupling::MEDCouplingField *ReadFieldCellLike(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1338 MCAuto<MEDFileAnyTypeField1TS> f(MEDFileAnyTypeField1TS::New(fileName,fieldName,iteration,order));
1340 MCAuto<MEDFileField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileField1TS>(f));
1343 MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(ReadFieldCellLikeT<double>(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1348 MCAuto<MEDFileIntField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileIntField1TS>(f));
1351 MCAuto<MEDCoupling::MEDCouplingFieldInt> ret(ReadFieldCellLikeT<int>(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1356 MCAuto<MEDFileFloatField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileFloatField1TS>(f));
1359 MCAuto<MEDCoupling::MEDCouplingFieldFloat> ret(ReadFieldCellLikeT<float>(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1363 throw INTERP_KERNEL::Exception("MEDCoupling::ReadFieldCell : only FLOAT32, FLOAT64 and INT32 supported for the moment !");
1367 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)
1369 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1370 MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1371 MCAuto<typename Traits<T>::FieldType> ret(ff->getFieldOnMeshAtLevel(ON_NODES,m));
1372 MCAuto<MEDFileUMesh> muPtr(MEDCoupling::DynamicCast<MEDFileMesh,MEDFileUMesh>(mm));
1373 if(ff->getPflsReallyUsed().empty())
1375 if(muPtr.isNotNull())
1377 const DataArrayIdType *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1379 ret->renumberCells(num->begin());
1384 DataArrayIdType *pfl(nullptr),*arr2(nullptr);
1385 MCAuto<typename Traits<T>::ArrayType> arr(ff->getFieldWithProfile(ON_NODES,meshDimRelToMax,mm,pfl));
1386 MCAuto<DataArrayIdType> pflSafe(pfl);
1387 MCAuto<DataArrayIdType> mp(m->getCellIdsFullyIncludedInNodeIds(pfl->begin(),pfl->end()));
1388 MCAuto<MEDCouplingUMesh> mzip(static_cast<MEDCouplingUMesh *>(m->buildPartAndReduceNodes(mp->begin(),mp->end(),arr2)));
1389 MCAuto<DataArrayIdType> arr2Safe(arr2);
1390 MCAuto<DataArrayIdType> arr3(arr2->invertArrayO2N2N2O(mzip->getNumberOfNodes()));
1391 MCAuto<DataArrayIdType> pflSorted(pflSafe->deepCopy()); pflSorted->sort(true);
1392 if(!arr3->isEqualWithoutConsideringStr(*pflSorted))
1393 throw INTERP_KERNEL::Exception("ReadFieldNode : not implemented yet !");
1394 if(!arr3->isEqualWithoutConsideringStr(*pflSafe))
1396 MCAuto<DataArrayIdType> o2n2(pflSafe->checkAndPreparePermutation());
1397 MCAuto<DataArrayIdType> n2o2(o2n2->invertArrayO2N2N2O(o2n2->getNumberOfTuples()));
1398 mzip->renumberNodes(n2o2->begin(),n2o2->getNumberOfTuples());
1408 MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldCell(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1410 return ReadFieldCellLike(ON_CELLS,fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1413 MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldNode(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1415 MCAuto<MEDFileAnyTypeField1TS> f(MEDFileAnyTypeField1TS::New(fileName,fieldName,iteration,order));
1417 MCAuto<MEDFileField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileField1TS>(f));
1420 MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(ReadFieldNodeT<double>(f1,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1425 MCAuto<MEDFileIntField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileIntField1TS>(f));
1428 MCAuto<MEDCoupling::MEDCouplingFieldInt> ret(ReadFieldNodeT<int>(f1,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1433 MCAuto<MEDFileFloatField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileFloatField1TS>(f));
1436 MCAuto<MEDCoupling::MEDCouplingFieldFloat> ret(ReadFieldNodeT<float>(f1,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1440 throw INTERP_KERNEL::Exception("MEDCoupling::ReadFieldNode : only FLOAT32, FLOAT64 and INT32 supported for the moment !");
1443 MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldGauss(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1445 return ReadFieldCellLike(ON_GAUSS_PT,fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1448 MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldGaussNE(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1450 return ReadFieldCellLike(ON_GAUSS_NE,fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1453 void MEDCoupling::WriteMesh(const std::string& fileName, const MEDCoupling::MEDCouplingMesh *mesh, bool writeFromScratch)
1456 throw INTERP_KERNEL::Exception("WriteMesh : input mesh is null !");
1457 const MEDCouplingUMesh *um(dynamic_cast<const MEDCouplingUMesh *>(mesh));
1460 WriteUMesh(fileName,um,writeFromScratch);
1463 int mod=writeFromScratch?2:0;
1464 const MEDCoupling1GTUMesh *um2(dynamic_cast<const MEDCoupling1GTUMesh *>(mesh));
1467 MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1468 AssignStaticWritePropertiesTo(*mmu);
1469 mmu->setMeshAtLevel(0,const_cast<MEDCoupling1GTUMesh *>(um2));
1470 mmu->write(fileName,mod);
1473 const MEDCouplingCMesh *um3(dynamic_cast<const MEDCouplingCMesh *>(mesh));
1476 MCAuto<MEDFileCMesh> mmc(MEDFileCMesh::New());
1477 AssignStaticWritePropertiesTo(*mmc);
1478 mmc->setMesh(const_cast<MEDCouplingCMesh *>(um3));
1479 mmc->write(fileName,mod);
1482 const MEDCouplingCurveLinearMesh *um4(dynamic_cast<const MEDCouplingCurveLinearMesh *>(mesh));
1485 MCAuto<MEDFileCurveLinearMesh> mmc(MEDFileCurveLinearMesh::New());
1486 AssignStaticWritePropertiesTo(*mmc);
1487 mmc->setMesh(const_cast<MEDCouplingCurveLinearMesh *>(um4));
1488 mmc->write(fileName,mod);
1491 throw INTERP_KERNEL::Exception("WriteMesh : only MEDCouplingUMesh, MEDCoupling1GTUMesh, MEDCouplingCMesh, MEDCouplingCurveLinear are dealed in this API for the moment !");
1494 void MEDCoupling::WriteUMesh(const std::string& fileName, const MEDCoupling::MEDCouplingUMesh *mesh, bool writeFromScratch)
1497 throw INTERP_KERNEL::Exception("WriteUMesh : input mesh is null !");
1498 int mod=writeFromScratch?2:0;
1499 MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1500 AssignStaticWritePropertiesTo(*m);
1501 MCAuto<MEDCouplingUMesh> mcpy(static_cast<MEDCouplingUMesh *>(mesh->deepCopy()));
1502 m->setMeshAtLevel(0,mcpy,true);
1503 m->write(fileName,mod);
1506 void MEDCoupling::WriteUMeshDep(const std::string& fileName, const MEDCoupling::MEDCouplingUMesh *mesh, bool writeFromScratch)
1508 WriteUMesh(fileName,mesh,writeFromScratch);
1511 void MEDCoupling::WriteUMeshesPartition(const std::string& fileName, const std::string& meshNameC, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1513 std::string meshName(meshNameC);
1514 if(meshName.empty())
1515 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 !");
1516 int status=MEDLoaderBase::getStatusOfFile(fileName);
1517 if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
1519 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
1520 throw INTERP_KERNEL::Exception(oss.str().c_str());
1522 MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1523 AssignStaticWritePropertiesTo(*m);
1524 m->setGroupsFromScratch(0,meshes,true);
1525 m->setName(meshNameC);
1526 int mod=writeFromScratch?2:0;
1527 m->write(fileName,mod);
1530 void MEDCoupling::WriteUMeshesPartitionDep(const std::string& fileName, const std::string& meshNameC, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1532 WriteUMeshesPartition(fileName,meshNameC,meshes,writeFromScratch);
1535 void MEDCoupling::WriteUMeshes(const std::string& fileName, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1537 int mod(writeFromScratch?2:0);
1538 MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1539 AssignStaticWritePropertiesTo(*m);
1540 m->setMeshes(meshes,true);
1541 m->write(fileName,mod);
1545 void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const std::string& fileName, const typename MEDCoupling::Traits<T>::FieldType *f, bool writeFromScratch)
1547 MCAuto< typename MLFieldTraits<T>::F1TSType > ff(MLFieldTraits<T>::F1TSType::New());
1548 AssignStaticWritePropertiesTo(*ff);
1549 MCAuto<typename MEDCoupling::Traits<T>::FieldType> f2(f->deepCopy());
1550 const MEDCouplingMesh *m(f2->getMesh());
1551 const MEDCouplingUMesh *um(dynamic_cast<const MEDCouplingUMesh *>(m));
1552 const MEDCoupling1GTUMesh *um2(dynamic_cast<const MEDCoupling1GTUMesh *>(m));
1553 const MEDCouplingCMesh *um3(dynamic_cast<const MEDCouplingCMesh *>(m));
1554 const MEDCouplingCurveLinearMesh *um4(dynamic_cast<const MEDCouplingCurveLinearMesh *>(m));
1555 MCAuto<MEDFileMesh> mm;
1556 int mod(writeFromScratch?2:0);
1559 MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1560 AssignStaticWritePropertiesTo(*mmu);
1561 MCAuto<DataArrayIdType> o2n(um->getRenumArrForMEDFileFrmt());
1562 MCAuto<DataArrayIdType> n2o(o2n->invertArrayO2N2N2O(o2n->getNumberOfTuples()));
1563 f2->renumberCells(o2n->begin(),false);
1564 mmu->setMeshAtLevel(0,const_cast<MEDCouplingUMesh *>(static_cast<const MEDCouplingUMesh *>(f2->getMesh())));
1565 mmu->setRenumFieldArr(0,n2o);
1566 ff->setFieldNoProfileSBT(f2);
1567 mmu->write(fileName,mod);
1571 MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1572 AssignStaticWritePropertiesTo(*mmu);
1573 mmu->setMeshAtLevel(0,const_cast<MEDCoupling1GTUMesh *>(um2));
1574 ff->setFieldNoProfileSBT(f2);
1575 mmu->write(fileName,mod);
1579 MCAuto<MEDFileCMesh> mmc(MEDFileCMesh::New());
1580 AssignStaticWritePropertiesTo(*mmc);
1581 mmc->setMesh(const_cast<MEDCouplingCMesh *>(um3));
1582 ff->setFieldNoProfileSBT(f2);
1583 mmc->write(fileName,mod);
1587 MCAuto<MEDFileCurveLinearMesh> mmc(MEDFileCurveLinearMesh::New());
1588 AssignStaticWritePropertiesTo(*mmc);
1589 mmc->setMesh(const_cast<MEDCouplingCurveLinearMesh *>(um4));
1590 ff->setFieldNoProfileSBT(f2);
1591 mmc->write(fileName,mod);
1594 throw INTERP_KERNEL::Exception("MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile : only MEDCouplingUMesh, MEDCoupling1GTUMesh, MEDCouplingCMesh, MEDCouplingCurveLinear are dealed in this API for the moment !");
1595 ff->write(fileName,0);
1599 void WriteFieldT(const std::string& fileName, const typename MEDCoupling::Traits<T>::FieldType *f, bool writeFromScratch)
1602 throw INTERP_KERNEL::Exception("WriteField : input field is NULL !");
1603 f->checkConsistencyLight();
1604 int status(MEDLoaderBase::getStatusOfFile(fileName));
1605 if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
1607 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
1608 throw INTERP_KERNEL::Exception(oss.str().c_str());
1610 if(writeFromScratch || (!writeFromScratch && status==MEDLoaderBase::NOT_EXIST))
1612 MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile<T>(fileName,f,true);
1616 std::vector<std::string> meshNames(GetMeshNames(fileName));
1618 throw INTERP_KERNEL::Exception("WriteField : trying to write a field with no mesh !");
1619 std::string fileNameCpp(f->getMesh()->getName());
1620 if(std::find(meshNames.begin(),meshNames.end(),fileNameCpp)==meshNames.end())
1621 MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile<T>(fileName,f,false);
1624 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,f->getMesh()->getName().c_str()));
1625 AssignStaticWritePropertiesTo(*mm);
1626 const MEDFileMesh *mmPtr(mm);
1627 const MEDFileUMesh *mmuPtr(dynamic_cast<const MEDFileUMesh *>(mmPtr));
1629 throw INTERP_KERNEL::Exception("WriteField : only umeshes are supported now !");
1630 MCAuto< typename MEDCoupling::Traits<T>::FieldType > f2(f->deepCopy());
1631 MEDCouplingUMesh *m(dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh())));
1633 throw INTERP_KERNEL::Exception("WriteField : only umesh in input field supported !");
1634 MCAuto<DataArrayIdType> o2n(m->getRenumArrForMEDFileFrmt());
1635 f2->renumberCells(o2n->begin(),false);
1636 m=static_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh()));
1637 MCAuto<MEDCouplingUMesh> mread(mmuPtr->getMeshAtLevel(m->getMeshDimension()-mm->getMeshDimension()));
1638 if(f2->getTypeOfField()!=ON_NODES)
1640 if(!m->getCoords()->isEqualWithoutConsideringStr(*mread->getCoords(),_EPS_FOR_NODE_COMP))
1641 m->tryToShareSameCoordsPermute(*mread,_EPS_FOR_NODE_COMP);
1643 mread->setCoords(m->getCoords());
1644 DataArrayIdType *part(NULL);
1645 bool b(mread->areCellsIncludedIn(m,_COMP_FOR_CELL,part));
1646 MCAuto<DataArrayIdType> partSafe(part);
1649 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 !";
1650 throw INTERP_KERNEL::Exception(oss.str().c_str());
1652 MCAuto< typename MLFieldTraits<T>::F1TSType > f1ts(MLFieldTraits<T>::F1TSType::New());
1653 AssignStaticWritePropertiesTo(*f1ts);
1654 if(part->isIota(mread->getNumberOfCells()))
1655 f1ts->setFieldNoProfileSBT(f2);
1658 part->setName(f1ts->createNewNameOfPfl().c_str());
1659 f1ts->setFieldProfile(f2,mm,m->getMeshDimension()-mm->getMeshDimension(),part);
1661 f1ts->write(fileName,0);
1666 DataArrayIdType *part(NULL);
1667 bool b(mread->getCoords()->areIncludedInMe(m->getCoords(),_EPS_FOR_NODE_COMP,part));
1668 MCAuto<DataArrayIdType> partSafe(part);
1671 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 !";
1672 throw INTERP_KERNEL::Exception(oss.str().c_str());
1674 MCAuto< typename MLFieldTraits<T>::F1TSType > f1ts(MLFieldTraits<T>::F1TSType::New());
1675 AssignStaticWritePropertiesTo(*f1ts);
1676 if(part->isIota(mread->getNumberOfNodes()))
1677 f1ts->setFieldNoProfileSBT(f2);
1680 part->setName(f1ts->createNewNameOfPfl().c_str());
1681 f1ts->setFieldProfile(f2,mm,m->getMeshDimension()-mm->getMeshDimension(),part);
1683 f1ts->write(fileName,0);
1689 void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MEDCouplingField *f, bool writeFromScratch)
1692 throw INTERP_KERNEL::Exception("WriteField : input field is null !");
1694 const MEDCoupling::MEDCouplingFieldDouble *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldDouble *>(f));
1697 WriteFieldT<double>(fileName,f1,writeFromScratch);
1702 const MEDCoupling::MEDCouplingFieldInt *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldInt *>(f));
1705 WriteFieldT<int>(fileName,f1,writeFromScratch);
1710 const MEDCoupling::MEDCouplingFieldFloat *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldFloat *>(f));
1713 WriteFieldT<float>(fileName,f1,writeFromScratch);
1717 throw INTERP_KERNEL::Exception("WriteField : input field is not in FLOAT32, FLOAT64, INT32 !");
1720 void MEDCoupling::WriteFieldDep(const std::string& fileName, const MEDCoupling::MEDCouplingField *f, bool writeFromScratch)
1722 WriteField(fileName,f,writeFromScratch);
1726 void WriteFieldUsingAlreadyWrittenMeshT(const std::string& fileName, const typename MEDCoupling::Traits<T>::FieldType *f)
1729 throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMeshT : input field is null !");
1730 f->checkConsistencyLight();
1731 int status(MEDLoaderBase::getStatusOfFile(fileName));
1732 if(status!=MEDLoaderBase::EXIST_RW)
1734 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions or not exists !";
1735 throw INTERP_KERNEL::Exception(oss.str().c_str());
1737 MCAuto< typename MLFieldTraits<T>::F1TSType > f1ts(MLFieldTraits<T>::F1TSType::New());
1738 AssignStaticWritePropertiesTo(*f1ts);
1739 MEDCouplingUMesh *m(dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f->getMesh())));
1742 MCAuto<DataArrayIdType> o2n(m->getRenumArrForMEDFileFrmt());
1743 MCAuto< typename MEDCoupling::Traits<T>::FieldType > f2(f->deepCopy());
1744 f2->renumberCells(o2n->begin(),false);
1745 f1ts->setFieldNoProfileSBT(f2);
1748 f1ts->setFieldNoProfileSBT(f);
1749 f1ts->write(fileName,0);
1752 void MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingField *f)
1755 throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMesh : input field is null !");
1757 const MEDCoupling::MEDCouplingFieldDouble *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldDouble *>(f));
1759 WriteFieldUsingAlreadyWrittenMeshT<double>(fileName,f1);
1763 const MEDCoupling::MEDCouplingFieldInt *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldInt *>(f));
1765 WriteFieldUsingAlreadyWrittenMeshT<int>(fileName,f1);
1769 const MEDCoupling::MEDCouplingFieldFloat *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldFloat *>(f));
1771 WriteFieldUsingAlreadyWrittenMeshT<float>(fileName,f1);
1774 throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMesh : input field is not in FLOAT32, FLOAT64, INT32 !");