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[34] = { MED_POINT1,//0
140 double _EPS_FOR_NODE_COMP=1.e-12;
142 int _COMP_FOR_CELL=0;
146 using namespace MEDCoupling;
151 * This method returns a first quick overview of mesh with name \a meshName into the file \a fileName.
152 * @param possibilities the relativeToMeshDim authorized to returned maxdim. This vector is systematically cleared at the begin of this method.
153 * @return the maximal mesh dimension of specified mesh. If nothing found -1 is returned.
155 int MEDLoaderNS::readUMeshDimFromFile(const std::string& fileName, const std::string& meshName, std::vector<int>& possibilities)
157 possibilities.clear();
158 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
161 char nommaa[MED_NAME_SIZE+1];
162 char maillage_description[MED_COMMENT_SIZE+1];
163 med_mesh_type type_maillage;
165 std::string trueMeshName;
166 int meshId=FromMedInt<int>(getIdFromMeshName(fid,meshName,trueMeshName));
167 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
168 med_sorting_type sortingType;
170 med_axis_type axisType;
171 med_int naxis(MEDmeshnAxis(fid,meshId));
172 INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
173 INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
174 MEDFILESAFECALLERRD0(MEDmeshInfo,(fid,meshId,nommaa,&Sdim,&Mdim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit));
178 throw INTERP_KERNEL::Exception("multisteps on mesh not managed yet !");
182 MEDFILESAFECALLERRD0(MEDmeshComputationStepInfo,(fid,nommaa,1,&numdt,&numit,&dt));
184 for(int i=0;i<MED_N_CELL_GEO_FIXED_CON;i++)
186 med_geometry_type curMedType=typmai[i];
187 med_bool changement,transformation;
188 med_int curNbOfElemM(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));
189 med_int curNbOfElemF(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));//limitation
191 med_entity_type whichEntity;
192 MEDLoaderNS::dispatchElems(curNbOfElemM,curNbOfElemF,curNbOfElem,whichEntity);
195 INTERP_KERNEL::NormalizedCellType type=typmai2[i];
196 int curDim=(int)INTERP_KERNEL::CellModel::GetCellModel(type).getDimension();
203 for(std::set<int>::const_reverse_iterator it=poss.rbegin();it!=poss.rend();it++)
204 possibilities.push_back(*it-ret);
211 int MEDLoaderNS::getIdFromMeshName(med_idt fid, const std::string& meshName, std::string& trueMeshName)
215 std::vector<std::string> meshes=getMeshNamesFid(fid);
217 throw INTERP_KERNEL::Exception("No mesh in file");
218 trueMeshName=meshes[0];
221 std::string meshNameStr(meshName);
222 std::vector<std::string> meshes=getMeshNamesFid(fid);
224 throw INTERP_KERNEL::Exception("No mesh in file");
225 std::vector<std::string>::iterator iter=std::find(meshes.begin(),meshes.end(),meshNameStr);
226 if(iter==meshes.end())
228 std::ostringstream os2;
229 os2 << "MeshName '" << meshName << "' not in file : meshes available : ";
230 std::copy(meshes.begin(),meshes.end(),std::ostream_iterator<std::string>(os2," "));
231 throw INTERP_KERNEL::Exception(os2.str().c_str());
233 trueMeshName=meshName;
234 return (int)( iter-meshes.begin()+1 );
237 std::vector<std::string> MEDLoaderNS::getMeshNamesFid(med_idt fid)
239 med_mesh_type type_maillage;
240 char maillage_description[MED_COMMENT_SIZE+1];
241 char dtunit[MED_COMMENT_SIZE+1];
244 char nommaa[MED_NAME_SIZE+1];
245 med_axis_type axistype;
246 med_sorting_type stype;
247 med_int n=MEDnMesh(fid);
248 std::vector<std::string> ret(n);
251 med_int naxis(MEDmeshnAxis(fid,i+1));
252 INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
253 INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
255 MEDFILESAFECALLERRD0(MEDmeshInfo,(fid,i+1,nommaa,&space_dim,&mesh_dim,&type_maillage,maillage_description,dtunit,&stype,&nstep,&axistype,axisname,axisunit));
256 std::string cur=MEDLoaderBase::buildStringFromFortran(nommaa,sizeof(nommaa));
263 * This methods allows to merger all entities and to considerate only cell types.
265 void MEDLoaderNS::dispatchElems(med_int nbOfElemCell, med_int nbOfElemFace, med_int& nbOfElem, med_entity_type& whichEntity)
267 if(nbOfElemCell>=nbOfElemFace)
269 whichEntity=MED_CELL;
270 nbOfElem=nbOfElemCell;
274 whichEntity=MED_CELL;
275 nbOfElem=nbOfElemFace;
281 void MEDCoupling::AssignStaticWritePropertiesTo(MEDCoupling::MEDFileWritable& obj)
283 obj.setTooLongStrPolicy(_TOO_LONG_STR);
286 bool MEDCoupling::HasXDR()
295 std::string MEDCoupling::MEDFileVersionStr()
299 std::fill(buf,buf+SZ,'\0');
300 const char START_EXPECTED[]="MED-";
301 med_err ret(MEDlibraryStrVersion(buf));
303 throw INTERP_KERNEL::Exception("MEDFileVersionStr : fail to find version of MED file ! It looks very bad !");
304 std::string zeRet(buf);
305 std::size_t pos(zeRet.find(START_EXPECTED,0));
308 std::ostringstream oss; oss << "MEDFileVersionStr : internal error ! The MEDFile returned version (\"" << zeRet << "\") has not the right pattern !";
309 throw INTERP_KERNEL::Exception(oss.str());
311 return zeRet.substr(sizeof(START_EXPECTED)-1,std::string::npos);
314 std::string MEDCoupling::MEDFileVersionOfFileStr(const std::string& fileName)
316 #if MED_NUM_MAJEUR>3 || ( MED_NUM_MAJEUR==3 && ( (MED_NUM_MINEUR==2 && MED_NUM_RELEASE>=1) || MED_NUM_MINEUR>=3) )
317 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
319 const char START_EXPECTED[]="MED-";
321 std::fill(buf,buf+SZ,'\0');
322 MEDFILESAFECALLERRD0(MEDfileStrVersionRd,(fid,buf));
323 std::string ret(buf);
324 std::size_t pos(ret.find(START_EXPECTED,0));
327 std::ostringstream oss; oss << "MEDFileVersionOfFileStr : internal error ! The MEDFile returned version (\"" << ret << "\") has not the right pattern !";
328 throw INTERP_KERNEL::Exception(oss.str());
330 return ret.substr(sizeof(START_EXPECTED)-1,std::string::npos);
332 std::ostringstream oss; oss << "MEDFileVersionOfFileStr : is implemented with MEDFile " << MEDFileVersionStr() << " ! If you need this feature please use version >= 3.2.1.";
333 throw INTERP_KERNEL::Exception(oss.str());
337 void MEDCoupling::MEDFileVersion(int& major, int& minor, int& release)
339 med_int majj,minn,rell;
340 med_err ret(MEDlibraryNumVersion(&majj,&minn,&rell));
342 throw INTERP_KERNEL::Exception("MEDFileVersion : fail to call MEDlibraryNumVersion ! It looks very bad !");
343 major=FromMedInt<int>(majj);
344 minor=FromMedInt<int>(minn);
345 release=FromMedInt<int>(rell);
349 * 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.
351 void MEDCoupling::SetEpsilonForNodeComp(double val)
353 _EPS_FOR_NODE_COMP=val;
357 * 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.
359 void MEDCoupling::SetCompPolicyForCell(int val)
365 * This method set the behaviour of MEDLoader when a too long string is seen in datastructure before copy it in MED file.
366 * By default (0) an exception is thrown. If equal to 1 a warning is emitted in std_err but no exception is thrown.
368 void MEDCoupling::SetTooLongStrPolicy(int val)
374 * Given a 'fileName' and a 'meshName' this method returns global information concerning this mesh.
375 * It returns, in this order :
376 * - 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...
377 * - the mesh dimension
378 * - the space dimension
379 * - the number of nodes
381 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)
383 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
385 char nommaa[MED_NAME_SIZE+1];
386 char maillage_description[MED_COMMENT_SIZE+1];
387 med_mesh_type type_maillage;
388 std::string trueMeshName;
389 int meshId=MEDLoaderNS::getIdFromMeshName(fid,meshName,trueMeshName);
390 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
391 med_sorting_type sortingType;
392 med_int nstep, mDim, sDim;
393 med_axis_type axisType;
394 med_int naxis(MEDmeshnAxis(fid,meshId));
395 INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
396 INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
397 MEDFILESAFECALLERRD0(MEDmeshInfo,(fid,meshId,nommaa,&sDim,&mDim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit));
398 meshDim=FromMedInt<int>(mDim);
399 spaceDim=FromMedInt<int>(sDim);
400 if(type_maillage!=MED_UNSTRUCTURED_MESH)
402 std::ostringstream oss; oss << "GetUMeshGlobalInfo : Mesh \""<< meshName << "\" in file \"" << fileName;
403 oss << "\" exists but it is not an unstructured mesh ! This method is not relevant for mesh types that are not unstructured !";
404 throw INTERP_KERNEL::Exception(oss.str().c_str());
408 throw INTERP_KERNEL::Exception("GetUMeshGlobalInfo : multisteps on mesh not managed !");
411 MEDFILESAFECALLERRD0(MEDmeshComputationStepInfo,(fid,nommaa,1,&numdt,&numit,&dt));
413 std::vector<int> dims;
414 std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > geoTypes;
415 med_bool changement,transformation;
416 for(int i=0;i<MED_N_CELL_FIXED_GEO;i++)
418 med_geometry_type curMedType=typmai[i];
419 med_int curNbOfElemM(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));
422 INTERP_KERNEL::NormalizedCellType typp=typmai2[i];
423 int mdimCell=INTERP_KERNEL::CellModel::GetCellModel(typp).getDimension();
424 dims.push_back(mdimCell);
425 geoTypes.push_back(std::pair<INTERP_KERNEL::NormalizedCellType,int>(typp,curNbOfElemM));
428 int maxLev=*std::max_element(dims.begin(),dims.end());
429 int lowLev=*std::min_element(dims.begin(),dims.end());
430 int nbOfLevels=maxLev-lowLev+1;
431 std::vector< std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > > ret(nbOfLevels);
432 for(std::size_t i=0;i<dims.size();i++)
434 ret[maxLev-dims[i]].push_back(geoTypes[i]);
436 numberOfNodes=ToIdType(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation));
440 void MEDCoupling::CheckFileForRead(const std::string& fileName)
442 MEDFileUtilities::CheckFileForRead(fileName);
445 std::vector<std::string> MEDCoupling::GetMeshNames(const std::string& fileName)
447 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
448 return MEDLoaderNS::getMeshNamesFid(fid);
451 std::vector< std::pair<std::string,std::string> > MEDCoupling::GetComponentsNamesOfField(const std::string& fileName, const std::string& fieldName)
453 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
454 med_int nbFields(MEDnField(fid));
455 std::vector<std::string> fields(nbFields);
456 med_field_type typcha;
457 for(int i=0;i<nbFields;i++)
459 med_int ncomp(MEDfieldnComponent(fid,i+1));
460 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
461 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
462 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
465 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
466 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
467 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
468 std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
469 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
470 if(curFieldName==fieldName)
472 std::vector< std::pair<std::string,std::string> > ret(ncomp);
473 for(int j=0;j<ncomp;j++)
474 ret[j]=std::pair<std::string,std::string>(MEDLoaderBase::buildStringFromFortran(((char *)comp)+j*MED_SNAME_SIZE,MED_SNAME_SIZE),
475 MEDLoaderBase::buildStringFromFortran(((char *)unit)+j*MED_SNAME_SIZE,MED_SNAME_SIZE));
478 fields[i]=curFieldName;
480 std::ostringstream oss; oss << "GetComponentsNamesOfField : no such field \"" << fieldName << "\" in file \"" << fileName << "\" !" << std::endl;
481 oss << "Possible field names are : " << std::endl;
482 std::copy(fields.begin(),fields.end(),std::ostream_iterator<std::string>(oss," "));
483 throw INTERP_KERNEL::Exception(oss.str().c_str());
486 std::vector<std::string> MEDCoupling::GetMeshNamesOnField(const std::string& fileName, const std::string& fieldName)
488 std::vector<std::string> ret;
490 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
491 med_int nbFields=MEDnField(fid);
493 med_field_type typcha;
494 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
495 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
498 for(int i=0;i<nbFields;i++)
500 med_int ncomp(MEDfieldnComponent(fid,i+1));
501 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
502 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
504 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
505 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
506 std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
507 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
508 if(curFieldName==fieldName)
509 ret.push_back(meshName);
514 std::vector<std::string> MEDCoupling::GetMeshFamiliesNames(const std::string& fileName, const std::string& meshName)
516 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
517 med_int nfam=MEDnFamily(fid,meshName.c_str());
518 std::vector<std::string> ret(nfam);
519 char nomfam[MED_NAME_SIZE+1];
521 for(int i=0;i<nfam;i++)
523 med_int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
524 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
525 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
526 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
527 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
528 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
529 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
530 std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
537 std::vector<std::string> MEDCoupling::GetMeshFamiliesNamesOnGroup(const std::string& fileName, const std::string& meshName, const std::string& grpName)
539 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
540 med_int nfam=MEDnFamily(fid,meshName.c_str());
541 std::vector<std::string> ret;
542 char nomfam[MED_NAME_SIZE+1];
544 for(int i=0;i<nfam;i++)
546 med_int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
547 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
548 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
549 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
550 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
551 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
552 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
553 std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
554 for(int j=0;j<ngro;j++)
556 std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
564 std::vector<std::string> MEDCoupling::GetMeshGroupsNamesOnFamily(const std::string& fileName, const std::string& meshName, const std::string& famName)
566 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
567 med_int nfam(MEDnFamily(fid,meshName.c_str()));
568 std::vector<std::string> ret;
569 char nomfam[MED_NAME_SIZE+1];
572 for(int i=0;i<nfam && !found;i++)
574 med_int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
575 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
576 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
577 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
578 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
579 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
580 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
581 std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
582 found=(cur==famName);
584 for(int j=0;j<ngro;j++)
586 std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
592 std::ostringstream oss;
593 oss << "GetMeshGroupsNamesOnFamily : no such family \"" << famName << "\" in file \"" << fileName << "\" in mesh \"" << meshName << "\" !";
594 throw INTERP_KERNEL::Exception(oss.str().c_str());
600 std::vector<std::string> MEDCoupling::GetMeshGroupsNames(const std::string& fileName, const std::string& meshName)
602 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
603 med_int nfam=MEDnFamily(fid,meshName.c_str());
604 std::vector<std::string> ret;
605 char nomfam[MED_NAME_SIZE+1];
607 for(int i=0;i<nfam;i++)
609 med_int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
610 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
611 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
612 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
613 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
614 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
615 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
616 for(int j=0;j<ngro;j++)
618 std::string cur=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
619 if(std::find(ret.begin(),ret.end(),cur)==ret.end())
626 std::vector<MEDCoupling::TypeOfField> MEDCoupling::GetTypesOfField(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
628 std::vector<MEDCoupling::TypeOfField> ret;
629 MCAuto<MEDFileAnyTypeFieldMultiTS> fs(MEDFileAnyTypeFieldMultiTS::New(fileName,fieldName,false));
630 if(fs->getMeshName()!=meshName)
632 std::ostringstream oss; oss << "GetTypesOfField : The field \"" << fieldName << "\" in file \"" << fileName << "\" is not lying on mesh \"" << meshName << "\"";
633 oss << " The name of the mesh in file is \"" << fs->getMeshName() << "\"!";
634 throw INTERP_KERNEL::Exception(oss.str().c_str());
636 int nbTS(fs->getNumberOfTS());
639 for(int i=0;i<nbTS;i++)
641 MCAuto<MEDFileAnyTypeField1TS> f1ts(fs->getTimeStepAtPos(i));
642 std::vector<MEDCoupling::TypeOfField> tof(f1ts->getTypesOfFieldAvailable());
643 for(std::vector<MEDCoupling::TypeOfField>::const_iterator it=tof.begin();it!=tof.end();it++)
644 if(std::find(ret.begin(),ret.end(),*it)==ret.end())
647 // sort ret to put before ON_NODES then ON_CELLS then the remaining.
648 std::vector<MEDCoupling::TypeOfField> ret2;
649 if(std::find(ret.begin(),ret.end(),ON_NODES)!=ret.end())
650 ret2.push_back(ON_NODES);
651 if(std::find(ret.begin(),ret.end(),ON_CELLS)!=ret.end())
652 ret2.push_back(ON_CELLS);
653 for(std::vector<MEDCoupling::TypeOfField>::const_iterator it=ret.begin();it!=ret.end();it++)
654 if(*it!=ON_NODES && *it!=ON_CELLS)
659 std::vector<std::string> MEDCoupling::GetAllFieldNames(const std::string& fileName)
661 std::vector<std::string> ret;
662 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
663 med_int nbFields=MEDnField(fid);
664 med_field_type typcha;
665 for(int i=0;i<nbFields;i++)
667 med_int ncomp(MEDfieldnComponent(fid,i+1));
668 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
669 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
670 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
671 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
672 INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
675 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
676 ret.push_back(std::string(nomcha));
681 std::vector<std::string> MEDCoupling::GetAllFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
683 std::vector<std::string> ret;
684 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
685 med_int nbFields=MEDnField(fid);
687 med_field_type typcha;
688 char *maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
689 char *nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
691 for(int i=0;i<nbFields;i++)
693 med_int ncomp(MEDfieldnComponent(fid,i+1));
694 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
695 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
696 INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
699 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
700 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
701 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
703 if(curMeshName==meshName)
704 ret.push_back(curFieldName);
711 std::vector<std::string> MEDCoupling::GetFieldNamesOnMesh(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName)
713 MEDCoupling::CheckFileForRead(fileName);
717 return GetCellFieldNamesOnMesh(fileName,meshName);
719 return GetNodeFieldNamesOnMesh(fileName,meshName);
721 throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
725 std::vector<std::string> MEDCoupling::GetCellFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
727 std::vector<std::string> ret;
728 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
729 med_int nbFields=MEDnField(fid);
731 med_field_type typcha;
732 //med_int nbpdtnor=0,pflsize,*pflval,lnsize;
733 med_int numdt=0,numo=0;
735 char pflname[MED_NAME_SIZE+1]="";
736 char locname[MED_NAME_SIZE+1]="";
737 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
738 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
739 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
743 for(int i=0;i<nbFields;i++)
745 med_int ncomp(MEDfieldnComponent(fid,i+1));
746 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
747 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
748 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
749 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
750 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
751 med_int profilesize,nbi;
752 if(curMeshName==meshName)
755 for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
759 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,1,&numdt,&numo,&dt));
760 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
761 pflname,&profilesize,locname,&nbi));
765 ret.push_back(curFieldName);
774 std::vector<std::string> MEDCoupling::GetNodeFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
776 std::vector<std::string> ret;
777 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
778 med_int nbFields=MEDnField(fid);
779 char pflname[MED_NAME_SIZE+1]="";
780 char locname[MED_NAME_SIZE+1]="";
782 med_field_type typcha;
783 med_int numdt=0,numo=0;
785 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
786 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
787 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
790 for(int i=0;i<nbFields;i++)
792 med_int ncomp(MEDfieldnComponent(fid,i+1));
793 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
794 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
796 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
797 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
798 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
801 med_int profilesize,nbi;
802 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,1,&numdt,&numo,&dt));
803 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
804 pflname,&profilesize,locname,&nbi));
805 if(curMeshName==meshName && nbOfVal>0)
807 ret.push_back(curFieldName);
814 std::vector< std::pair< std::pair<int,int>, double> > MEDCoupling::GetAllFieldIterations(const std::string& fileName, const std::string& fieldName)
816 std::vector< std::pair< std::pair<int,int>, double > > ret;
817 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
818 med_int nbFields=MEDnField(fid);
820 med_field_type typcha;
821 med_int numdt=0,numo=0;
823 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
824 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
825 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
828 std::ostringstream oss; oss << "GetAllFieldIterations : No field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
829 for(int i=0;i<nbFields;i++)
831 med_int ncomp(MEDfieldnComponent(fid,i+1));
832 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
833 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
835 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
836 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
837 if(curFieldName==fieldName)
839 for(int k=0;k<nbPdt;k++)
841 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
842 ret.push_back(std::make_pair(std::make_pair(numdt,numo),dt));
848 oss << "\"" << curFieldName << "\"";
849 if(i!=nbFields-1) oss << ", ";
853 throw INTERP_KERNEL::Exception(oss.str().c_str());
856 double MEDCoupling::GetTimeAttachedOnFieldIteration(const std::string& fileName, const std::string& fieldName, int iteration, int order)
858 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
859 med_int nbFields=MEDnField(fid);
861 med_field_type typcha;
862 med_int numdt=0,numo=0;
865 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
866 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
867 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
871 double ret=std::numeric_limits<double>::max();
872 for(int i=0;i<nbFields && !found;i++)
874 med_int ncomp(MEDfieldnComponent(fid,i+1));
875 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
876 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
878 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&local,&typcha,comp,unit,dt_unit,&nbPdt));
879 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
880 if(curFieldName==fieldName)
883 for(int k=0;k<nbPdt;k++)
885 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
886 if(numdt==iteration && numo==order)
894 if(!found || !found2)
896 std::ostringstream oss;
897 oss << "No such field with name \"" << fieldName << "\" and iteration,order=(" << iteration << "," << order << ") exists in file \"" << fileName << "\" !";
898 throw INTERP_KERNEL::Exception(oss.str().c_str());
903 std::vector< std::pair<int,int> > MEDCoupling::GetFieldIterations(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, const std::string& fieldName)
905 MEDCoupling::CheckFileForRead(fileName);
909 return GetCellFieldIterations(fileName,meshName,fieldName);
911 return GetNodeFieldIterations(fileName,meshName,fieldName);
913 throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
917 std::vector< std::pair<int,int> > MEDCoupling::GetCellFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
919 std::string meshNameCpp(meshName);
920 std::vector< std::pair<int,int> > ret;
921 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
922 med_int nbFields=MEDnField(fid);
924 med_field_type typcha;
925 med_int numdt=0,numo=0;
927 char pflname[MED_NAME_SIZE+1]="";
928 char locname[MED_NAME_SIZE+1]="";
929 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
930 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
931 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
934 std::ostringstream oss; oss << "GetCellFieldIterations : No cell Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
935 std::set<std::string> s2;
936 for(int i=0;i<nbFields;i++)
938 med_int ncomp(MEDfieldnComponent(fid,i+1));
939 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
940 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
942 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
943 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
944 if(curFieldName==fieldName)
947 for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
949 for(int k=0;k<nbPdt;k++)
951 med_int profilesize,nbi;
952 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
953 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
954 pflname,&profilesize,locname,&nbi));
955 std::string maa_ass_cpp(maa_ass);
958 if(meshNameCpp==maa_ass_cpp)
961 ret.push_back(std::make_pair(numdt,numo));
964 s2.insert(maa_ass_cpp);
971 oss << "\"" << curFieldName << "\"";
972 if(i!=nbFields-1) oss << ", ";
979 oss << ". Cell Field \"" << fieldName << "\" exists but lies on meshes with names : \"";
980 std::copy(s2.begin(),s2.end(),std::ostream_iterator<std::string>(oss,"\", \""));
983 throw INTERP_KERNEL::Exception(oss.str().c_str());
988 std::vector< std::pair<int,int> > MEDCoupling::GetNodeFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
990 std::string meshNameCpp(meshName);
991 std::vector< std::pair<int,int> > ret;
992 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
993 med_int nbFields=MEDnField(fid);
995 med_field_type typcha;
996 med_int numdt=0,numo=0;
998 char pflname[MED_NAME_SIZE+1]="";
999 char locname[MED_NAME_SIZE+1]="";
1000 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
1001 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
1002 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
1005 std::ostringstream oss; oss << "GetNodeFieldIterations : No node Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
1006 std::set<std::string> s2;
1007 for(int i=0;i<nbFields;i++)
1009 med_int ncomp(MEDfieldnComponent(fid,i+1));
1010 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
1011 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
1013 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
1014 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
1015 if(curFieldName==fieldName)
1017 for(int k=0;k<nbPdt;k++)
1019 med_int profilesize,nbi;
1020 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
1021 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
1022 pflname,&profilesize,locname,&nbi));
1023 std::string maa_ass_cpp(maa_ass);
1026 if(meshNameCpp==maa_ass_cpp)
1027 { ret.push_back(std::make_pair(numdt,numo)); }
1029 s2.insert(maa_ass_cpp);
1035 oss << "\"" << curFieldName << "\"";
1036 if(i!=nbFields-1) oss << ", ";
1043 oss << ". Node Field \"" << fieldName << "\" exists but lies on meshes with names : \"";
1044 std::copy(s2.begin(),s2.end(),std::ostream_iterator<std::string>(oss,"\", \""));
1047 throw INTERP_KERNEL::Exception(oss.str().c_str());
1052 MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax)
1054 MEDCoupling::CheckFileForRead(fileName);
1055 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1056 MEDFileMesh *mmPtr(mm);
1057 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1059 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1060 MEDFileCMesh *mmcPtr=dynamic_cast<MEDFileCMesh *>(mmPtr);
1063 const MEDCouplingCMesh *ret(mmcPtr->getMesh()); ret->incrRef();
1064 return const_cast<MEDCouplingCMesh *>(ret);
1066 MEDFileCurveLinearMesh *mmc2Ptr=dynamic_cast<MEDFileCurveLinearMesh *>(mmPtr);
1069 const MEDCouplingCurveLinearMesh *ret(mmc2Ptr->getMesh()); ret->incrRef();
1070 return const_cast<MEDCouplingCurveLinearMesh *>(ret);
1072 std::ostringstream oss; oss << "ReadMeshFromFile : The mesh \"" << meshName << "\" in file \"" << fileName << "\" has not a recognized type !";
1073 throw INTERP_KERNEL::Exception(oss.str().c_str());
1076 MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& fileName, int meshDimRelToMax)
1078 MEDCoupling::CheckFileForRead(fileName);
1079 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName));
1080 MEDFileMesh *mmPtr(mm);
1081 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1083 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1084 MEDFileCMesh *mmcPtr=dynamic_cast<MEDFileCMesh *>(mmPtr);
1087 const MEDCouplingCMesh *ret(mmcPtr->getMesh()); ret->incrRef();
1088 return const_cast<MEDCouplingCMesh *>(ret);
1090 MEDFileCurveLinearMesh *mmc2Ptr=dynamic_cast<MEDFileCurveLinearMesh *>(mmPtr);
1093 const MEDCouplingCurveLinearMesh *ret(mmc2Ptr->getMesh()); ret->incrRef();
1094 return const_cast<MEDCouplingCurveLinearMesh *>(ret);
1096 std::ostringstream oss; oss << "ReadMeshFromFile (2) : The first mesh \"" << mm->getName() << "\" in file \"" << fileName << "\" has not a recognized type !";
1097 throw INTERP_KERNEL::Exception(oss.str().c_str());
1100 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax)
1102 MEDCoupling::CheckFileForRead(fileName);
1103 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1104 MEDFileMesh *mmPtr(mm);
1105 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1108 std::ostringstream oss; oss << "ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName=\""<< meshName << "\" exists but it is not an unstructured mesh !";
1109 throw INTERP_KERNEL::Exception(oss.str().c_str());
1111 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1114 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFile(const std::string& fileName, int meshDimRelToMax)
1116 MEDCoupling::CheckFileForRead(fileName);
1117 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName));
1118 MEDFileMesh *mmPtr(mm);
1119 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1122 std::ostringstream oss; oss << "ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1123 throw INTERP_KERNEL::Exception(oss.str().c_str());
1125 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1128 int MEDCoupling::ReadUMeshDimFromFile(const std::string& fileName, const std::string& meshName)
1130 MEDCoupling::CheckFileForRead(fileName);
1131 std::vector<int> poss;
1132 return MEDLoaderNS::readUMeshDimFromFile(fileName,meshName,poss);
1135 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFamilies(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::vector<std::string>& fams)
1137 MEDCoupling::CheckFileForRead(fileName);
1138 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1139 MEDFileMesh *mmPtr(mm);
1140 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1143 std::ostringstream oss; oss << "ReadUMeshFromFamilies : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1144 throw INTERP_KERNEL::Exception(oss.str().c_str());
1146 return mmuPtr->getFamilies(meshDimRelToMax,fams,true);
1149 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromGroups(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::vector<std::string>& grps)
1151 MEDCoupling::CheckFileForRead(fileName);
1152 MCAuto<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1153 MEDFileMesh *mmPtr(mm);
1154 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1157 std::ostringstream oss; oss << "ReadUMeshFromGroups : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1158 throw INTERP_KERNEL::Exception(oss.str().c_str());
1160 return mmuPtr->getGroups(meshDimRelToMax,grps,true);
1163 MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(const std::string& fileName)
1165 std::vector<std::string> fieldNames(GetAllFieldNames(fileName));
1166 std::size_t sz(fieldNames.size());
1169 std::ostringstream oss;
1170 oss << "The file \"" << fileName << "\" contains no field !";
1171 throw INTERP_KERNEL::Exception(oss.str());
1175 std::ostringstream oss;
1176 oss << "In file \"" << fileName << "\" there are more than one field !" << std::endl;
1177 oss << "You are invited to use ReadField(fileName, fieldName) instead to avoid misleading concerning field you want to read !" << std::endl;
1178 oss << "For information, fields available are :" << std::endl;
1179 for(std::vector<std::string>::const_iterator it=fieldNames.begin();it!=fieldNames.end();it++)
1180 oss << " - \"" << *it << "\"" << std::endl;
1181 throw INTERP_KERNEL::Exception(oss.str());
1183 return ReadField(fileName,fieldNames[0]);
1186 MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(const std::string& fileName, const std::string& fieldName)
1188 std::vector< std::pair< std::pair<int,int>, double> > iterations(GetAllFieldIterations(fileName,fieldName));
1189 std::size_t sz(iterations.size());
1192 std::ostringstream oss;
1193 oss << "In file \"" << fileName << "\" field \"" << fieldName << "\" exists but with no time steps !";
1194 throw INTERP_KERNEL::Exception(oss.str());
1198 std::ostringstream oss;
1199 oss << "In file \"" << fileName << "\" field \"" << fieldName << "\" exists but with more than one time steps !" << std::endl;
1200 oss << "You are invited to use ReadField(fileName, fieldName, iteration, order) instead to avoid misleading concerning time steps." << std::endl;
1201 oss << "For information, time steps available for field \"" << fieldName << "\" are :" << std::endl;
1202 for(std::vector< std::pair< std::pair<int,int>, double> >::const_iterator it=iterations.begin();it!=iterations.end();it++)
1203 oss << " - " << (*it).first.first << ", " << (*it).first.second << " (" << (*it).second << ")" << std::endl;
1204 throw INTERP_KERNEL::Exception(oss.str());
1206 return ReadField(fileName,fieldName,iterations[0].first.first,iterations[0].first.second);
1209 MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(const std::string& fileName, const std::string& fieldName, int iteration, int order)
1211 MCAuto<MEDFileAnyTypeField1TS> f(MEDFileAnyTypeField1TS::New(fileName,fieldName,iteration,order));
1212 MCAuto<MEDFileMesh> mesh(MEDFileMesh::New(fileName,f->getMeshName()));
1214 MCAuto<MEDFileField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileField1TS>(f));
1217 MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(f1->field(mesh));
1218 return MEDCoupling::DynamicCast<MEDCouplingFieldDouble,MEDCouplingField>(ret);
1222 MCAuto<MEDFileIntField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileIntField1TS>(f));
1225 MCAuto<MEDCoupling::MEDCouplingFieldInt> ret(f1->field(mesh));
1226 return MEDCoupling::DynamicCast<MEDCouplingFieldInt,MEDCouplingField>(ret);
1230 MCAuto<MEDFileFloatField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileFloatField1TS>(f));
1233 MCAuto<MEDCoupling::MEDCouplingFieldFloat> ret(f1->field(mesh));
1234 return MEDCoupling::DynamicCast<MEDCouplingFieldFloat,MEDCouplingField>(ret);
1237 throw INTERP_KERNEL::Exception("MEDCoupling::ReadField : only FLOAT32, FLOAT64 and INT32 supported for the moment !");
1240 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)
1242 MEDCoupling::CheckFileForRead(fileName);
1246 return ReadFieldCell(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1248 return ReadFieldNode(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1250 return ReadFieldGauss(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1252 return ReadFieldGaussNE(fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1254 throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES, ON_CELLS, ON_GAUSS_PT or ON_GAUSS_NE !");
1258 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsOnSameMesh(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1259 const std::vector<std::pair<int,int> >& its)
1262 return std::vector<MEDCoupling::MEDCouplingFieldDouble *>();
1263 MEDCoupling::CheckFileForRead(fileName);
1264 std::vector<MEDCoupling::MEDCouplingFieldDouble *> ret(its.size());
1265 std::vector< MCAuto<MEDCouplingFieldDouble> > retSafe(its.size());
1268 //Retrieving mesh of rank 0 and field on rank 0 too.
1269 MCAuto<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1270 MEDFileMesh *mmPtr(mm);
1271 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1273 throw INTERP_KERNEL::Exception("ReadFieldsOnSameMesh : only unstructured mesh is managed !");
1274 MCAuto<MEDCouplingUMesh> m=mmuPtr->getMeshAtLevel(meshDimRelToMax);
1275 const DataArrayIdType *o2n=mmuPtr->getNumberFieldAtLevel(meshDimRelToMax);
1276 MCAuto<MEDCouplingUMesh> m2(m->clone(true));
1278 m2->renumberCells(o2n->begin(),true);
1280 for(std::vector<std::pair<int,int> >::const_iterator it=its.begin();it!=its.end();it++,i++)
1282 MCAuto<MEDFileField1TS> ff=MEDFileField1TS::New(fileName,fieldName,(*it).first,(*it).second);
1283 MCAuto<MEDCouplingFieldDouble> retElt=ff->getFieldOnMeshAtLevel(type,m);
1285 retElt->renumberCells(o2n->begin(),true);
1286 retElt->setMesh(m2);
1290 for(std::vector<std::pair<int,int> >::const_iterator it=its.begin();it!=its.end();it++,i++)
1291 ret[i]=retSafe[i].retn();
1295 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsCellOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1296 const std::vector<std::pair<int,int> >& its)
1298 return ReadFieldsOnSameMesh(ON_CELLS,fileName,meshName,meshDimRelToMax,fieldName,its);
1301 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsNodeOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1302 const std::vector<std::pair<int,int> >& its)
1304 return ReadFieldsOnSameMesh(ON_NODES,fileName,meshName,meshDimRelToMax,fieldName,its);
1307 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsGaussOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1308 const std::vector<std::pair<int,int> >& its)
1310 return ReadFieldsOnSameMesh(ON_GAUSS_PT,fileName,meshName,meshDimRelToMax,fieldName,its);
1313 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsGaussNEOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1314 const std::vector<std::pair<int,int> >& its)
1316 return ReadFieldsOnSameMesh(ON_GAUSS_NE,fileName,meshName,meshDimRelToMax,fieldName,its);
1319 namespace MEDCoupling
1322 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)
1324 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1325 MCAuto<MEDFileUMesh> muPtr(MEDCoupling::DynamicCast<MEDFileMesh,MEDFileUMesh>(mm));
1326 MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1327 MCAuto<typename Traits<T>::FieldType> ret(ff->getFieldOnMeshAtLevel(type,m));
1328 if(muPtr.isNotNull())
1330 const DataArrayIdType *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1332 ret->renumberCells(num->begin());
1337 MEDCoupling::MEDCouplingField *ReadFieldCellLike(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1339 MCAuto<MEDFileAnyTypeField1TS> f(MEDFileAnyTypeField1TS::New(fileName,fieldName,iteration,order));
1341 MCAuto<MEDFileField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileField1TS>(f));
1344 MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(ReadFieldCellLikeT<double>(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1349 MCAuto<MEDFileIntField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileIntField1TS>(f));
1352 MCAuto<MEDCoupling::MEDCouplingFieldInt> ret(ReadFieldCellLikeT<int>(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1357 MCAuto<MEDFileFloatField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileFloatField1TS>(f));
1360 MCAuto<MEDCoupling::MEDCouplingFieldFloat> ret(ReadFieldCellLikeT<float>(f1,type,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1364 throw INTERP_KERNEL::Exception("MEDCoupling::ReadFieldCell : only FLOAT32, FLOAT64 and INT32 supported for the moment !");
1368 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)
1370 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1371 MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1372 MCAuto<typename Traits<T>::FieldType> ret(ff->getFieldOnMeshAtLevel(ON_NODES,m));
1373 MCAuto<MEDFileUMesh> muPtr(MEDCoupling::DynamicCast<MEDFileMesh,MEDFileUMesh>(mm));
1374 if(ff->getPflsReallyUsed().empty())
1376 if(muPtr.isNotNull())
1378 const DataArrayIdType *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1380 ret->renumberCells(num->begin());
1385 DataArrayIdType *pfl(nullptr),*arr2(nullptr);
1386 MCAuto<typename Traits<T>::ArrayType> arr(ff->getFieldWithProfile(ON_NODES,meshDimRelToMax,mm,pfl));
1387 MCAuto<DataArrayIdType> pflSafe(pfl);
1388 MCAuto<DataArrayIdType> mp(m->getCellIdsFullyIncludedInNodeIds(pfl->begin(),pfl->end()));
1389 MCAuto<MEDCouplingUMesh> mzip(static_cast<MEDCouplingUMesh *>(m->buildPartAndReduceNodes(mp->begin(),mp->end(),arr2)));
1390 MCAuto<DataArrayIdType> arr2Safe(arr2);
1391 MCAuto<DataArrayIdType> arr3(arr2->invertArrayO2N2N2O(mzip->getNumberOfNodes()));
1392 MCAuto<DataArrayIdType> pflSorted(pflSafe->deepCopy()); pflSorted->sort(true);
1393 if(!arr3->isEqualWithoutConsideringStr(*pflSorted))
1394 throw INTERP_KERNEL::Exception("ReadFieldNode : not implemented yet !");
1395 if(!arr3->isEqualWithoutConsideringStr(*pflSafe))
1397 MCAuto<DataArrayIdType> o2n2(pflSafe->checkAndPreparePermutation());
1398 MCAuto<DataArrayIdType> n2o2(o2n2->invertArrayO2N2N2O(o2n2->getNumberOfTuples()));
1399 mzip->renumberNodes(n2o2->begin(),n2o2->getNumberOfTuples());
1409 MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldCell(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1411 return ReadFieldCellLike(ON_CELLS,fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1414 MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldNode(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1416 MCAuto<MEDFileAnyTypeField1TS> f(MEDFileAnyTypeField1TS::New(fileName,fieldName,iteration,order));
1418 MCAuto<MEDFileField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileField1TS>(f));
1421 MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(ReadFieldNodeT<double>(f1,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1426 MCAuto<MEDFileIntField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileIntField1TS>(f));
1429 MCAuto<MEDCoupling::MEDCouplingFieldInt> ret(ReadFieldNodeT<int>(f1,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1434 MCAuto<MEDFileFloatField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileFloatField1TS>(f));
1437 MCAuto<MEDCoupling::MEDCouplingFieldFloat> ret(ReadFieldNodeT<float>(f1,fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1441 throw INTERP_KERNEL::Exception("MEDCoupling::ReadFieldNode : only FLOAT32, FLOAT64 and INT32 supported for the moment !");
1444 MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldGauss(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1446 return ReadFieldCellLike(ON_GAUSS_PT,fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1449 MEDCoupling::MEDCouplingField *MEDCoupling::ReadFieldGaussNE(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1451 return ReadFieldCellLike(ON_GAUSS_NE,fileName,meshName,meshDimRelToMax,fieldName,iteration,order);
1454 void MEDCoupling::WriteMesh(const std::string& fileName, const MEDCoupling::MEDCouplingMesh *mesh, bool writeFromScratch)
1457 throw INTERP_KERNEL::Exception("WriteMesh : input mesh is null !");
1458 const MEDCouplingUMesh *um(dynamic_cast<const MEDCouplingUMesh *>(mesh));
1461 WriteUMesh(fileName,um,writeFromScratch);
1464 int mod=writeFromScratch?2:0;
1465 const MEDCoupling1GTUMesh *um2(dynamic_cast<const MEDCoupling1GTUMesh *>(mesh));
1468 MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1469 AssignStaticWritePropertiesTo(*mmu);
1470 mmu->setMeshAtLevel(0,const_cast<MEDCoupling1GTUMesh *>(um2));
1471 mmu->write(fileName,mod);
1474 const MEDCouplingCMesh *um3(dynamic_cast<const MEDCouplingCMesh *>(mesh));
1477 MCAuto<MEDFileCMesh> mmc(MEDFileCMesh::New());
1478 AssignStaticWritePropertiesTo(*mmc);
1479 mmc->setMesh(const_cast<MEDCouplingCMesh *>(um3));
1480 mmc->write(fileName,mod);
1483 const MEDCouplingCurveLinearMesh *um4(dynamic_cast<const MEDCouplingCurveLinearMesh *>(mesh));
1486 MCAuto<MEDFileCurveLinearMesh> mmc(MEDFileCurveLinearMesh::New());
1487 AssignStaticWritePropertiesTo(*mmc);
1488 mmc->setMesh(const_cast<MEDCouplingCurveLinearMesh *>(um4));
1489 mmc->write(fileName,mod);
1492 throw INTERP_KERNEL::Exception("WriteMesh : only MEDCouplingUMesh, MEDCoupling1GTUMesh, MEDCouplingCMesh, MEDCouplingCurveLinear are dealed in this API for the moment !");
1495 void MEDCoupling::WriteUMesh(const std::string& fileName, const MEDCoupling::MEDCouplingUMesh *mesh, bool writeFromScratch)
1498 throw INTERP_KERNEL::Exception("WriteUMesh : input mesh is null !");
1499 int mod=writeFromScratch?2:0;
1500 MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1501 AssignStaticWritePropertiesTo(*m);
1502 MCAuto<MEDCouplingUMesh> mcpy(static_cast<MEDCouplingUMesh *>(mesh->deepCopy()));
1503 m->setMeshAtLevel(0,mcpy,true);
1504 m->write(fileName,mod);
1507 void MEDCoupling::WriteUMeshDep(const std::string& fileName, const MEDCoupling::MEDCouplingUMesh *mesh, bool writeFromScratch)
1509 WriteUMesh(fileName,mesh,writeFromScratch);
1512 void MEDCoupling::WriteUMeshesPartition(const std::string& fileName, const std::string& meshNameC, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1514 std::string meshName(meshNameC);
1515 if(meshName.empty())
1516 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 !");
1517 int status=MEDLoaderBase::getStatusOfFile(fileName);
1518 if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
1520 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
1521 throw INTERP_KERNEL::Exception(oss.str().c_str());
1523 MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1524 AssignStaticWritePropertiesTo(*m);
1525 m->setGroupsFromScratch(0,meshes,true);
1526 m->setName(meshNameC);
1527 int mod=writeFromScratch?2:0;
1528 m->write(fileName,mod);
1531 void MEDCoupling::WriteUMeshesPartitionDep(const std::string& fileName, const std::string& meshNameC, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1533 WriteUMeshesPartition(fileName,meshNameC,meshes,writeFromScratch);
1536 void MEDCoupling::WriteUMeshes(const std::string& fileName, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1538 int mod(writeFromScratch?2:0);
1539 MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1540 AssignStaticWritePropertiesTo(*m);
1541 m->setMeshes(meshes,true);
1542 m->write(fileName,mod);
1546 void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const std::string& fileName, const typename MEDCoupling::Traits<T>::FieldType *f, bool writeFromScratch)
1548 MCAuto< typename MLFieldTraits<T>::F1TSType > ff(MLFieldTraits<T>::F1TSType::New());
1549 AssignStaticWritePropertiesTo(*ff);
1550 MCAuto<typename MEDCoupling::Traits<T>::FieldType> f2(f->deepCopy());
1551 const MEDCouplingMesh *m(f2->getMesh());
1552 const MEDCouplingUMesh *um(dynamic_cast<const MEDCouplingUMesh *>(m));
1553 const MEDCoupling1GTUMesh *um2(dynamic_cast<const MEDCoupling1GTUMesh *>(m));
1554 const MEDCouplingCMesh *um3(dynamic_cast<const MEDCouplingCMesh *>(m));
1555 const MEDCouplingCurveLinearMesh *um4(dynamic_cast<const MEDCouplingCurveLinearMesh *>(m));
1556 MCAuto<MEDFileMesh> mm;
1557 int mod(writeFromScratch?2:0);
1560 MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1561 AssignStaticWritePropertiesTo(*mmu);
1562 MCAuto<DataArrayIdType> o2n(um->getRenumArrForMEDFileFrmt());
1563 MCAuto<DataArrayIdType> n2o(o2n->invertArrayO2N2N2O(o2n->getNumberOfTuples()));
1564 f2->renumberCells(o2n->begin(),false);
1565 mmu->setMeshAtLevel(0,const_cast<MEDCouplingUMesh *>(static_cast<const MEDCouplingUMesh *>(f2->getMesh())));
1566 mmu->setRenumFieldArr(0,n2o);
1567 ff->setFieldNoProfileSBT(f2);
1568 mmu->write(fileName,mod);
1572 MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1573 AssignStaticWritePropertiesTo(*mmu);
1574 mmu->setMeshAtLevel(0,const_cast<MEDCoupling1GTUMesh *>(um2));
1575 ff->setFieldNoProfileSBT(f2);
1576 mmu->write(fileName,mod);
1580 MCAuto<MEDFileCMesh> mmc(MEDFileCMesh::New());
1581 AssignStaticWritePropertiesTo(*mmc);
1582 mmc->setMesh(const_cast<MEDCouplingCMesh *>(um3));
1583 ff->setFieldNoProfileSBT(f2);
1584 mmc->write(fileName,mod);
1588 MCAuto<MEDFileCurveLinearMesh> mmc(MEDFileCurveLinearMesh::New());
1589 AssignStaticWritePropertiesTo(*mmc);
1590 mmc->setMesh(const_cast<MEDCouplingCurveLinearMesh *>(um4));
1591 ff->setFieldNoProfileSBT(f2);
1592 mmc->write(fileName,mod);
1595 throw INTERP_KERNEL::Exception("MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile : only MEDCouplingUMesh, MEDCoupling1GTUMesh, MEDCouplingCMesh, MEDCouplingCurveLinear are dealed in this API for the moment !");
1596 ff->write(fileName,0);
1600 void WriteFieldT(const std::string& fileName, const typename MEDCoupling::Traits<T>::FieldType *f, bool writeFromScratch)
1603 throw INTERP_KERNEL::Exception("WriteField : input field is NULL !");
1604 f->checkConsistencyLight();
1605 int status(MEDLoaderBase::getStatusOfFile(fileName));
1606 if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
1608 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
1609 throw INTERP_KERNEL::Exception(oss.str().c_str());
1611 if(writeFromScratch || (!writeFromScratch && status==MEDLoaderBase::NOT_EXIST))
1613 MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile<T>(fileName,f,true);
1617 std::vector<std::string> meshNames(GetMeshNames(fileName));
1619 throw INTERP_KERNEL::Exception("WriteField : trying to write a field with no mesh !");
1620 std::string fileNameCpp(f->getMesh()->getName());
1621 if(std::find(meshNames.begin(),meshNames.end(),fileNameCpp)==meshNames.end())
1622 MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile<T>(fileName,f,false);
1625 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,f->getMesh()->getName().c_str()));
1626 AssignStaticWritePropertiesTo(*mm);
1627 const MEDFileMesh *mmPtr(mm);
1628 const MEDFileUMesh *mmuPtr(dynamic_cast<const MEDFileUMesh *>(mmPtr));
1630 throw INTERP_KERNEL::Exception("WriteField : only umeshes are supported now !");
1631 MCAuto< typename MEDCoupling::Traits<T>::FieldType > f2(f->deepCopy());
1632 MEDCouplingUMesh *m(dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh())));
1634 throw INTERP_KERNEL::Exception("WriteField : only umesh in input field supported !");
1635 MCAuto<DataArrayIdType> o2n(m->getRenumArrForMEDFileFrmt());
1636 f2->renumberCells(o2n->begin(),false);
1637 m=static_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh()));
1638 MCAuto<MEDCouplingUMesh> mread(mmuPtr->getMeshAtLevel(m->getMeshDimension()-mm->getMeshDimension()));
1639 if(f2->getTypeOfField()!=ON_NODES)
1641 if(!m->getCoords()->isEqualWithoutConsideringStr(*mread->getCoords(),_EPS_FOR_NODE_COMP))
1642 m->tryToShareSameCoordsPermute(*mread,_EPS_FOR_NODE_COMP);
1644 mread->setCoords(m->getCoords());
1645 DataArrayIdType *part(NULL);
1646 bool b(mread->areCellsIncludedIn(m,_COMP_FOR_CELL,part));
1647 MCAuto<DataArrayIdType> partSafe(part);
1650 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 !";
1651 throw INTERP_KERNEL::Exception(oss.str().c_str());
1653 MCAuto< typename MLFieldTraits<T>::F1TSType > f1ts(MLFieldTraits<T>::F1TSType::New());
1654 AssignStaticWritePropertiesTo(*f1ts);
1655 if(part->isIota(mread->getNumberOfCells()))
1656 f1ts->setFieldNoProfileSBT(f2);
1659 part->setName(f1ts->createNewNameOfPfl().c_str());
1660 f1ts->setFieldProfile(f2,mm,m->getMeshDimension()-mm->getMeshDimension(),part);
1662 f1ts->write(fileName,0);
1667 DataArrayIdType *part(NULL);
1668 bool b(mread->getCoords()->areIncludedInMe(m->getCoords(),_EPS_FOR_NODE_COMP,part));
1669 MCAuto<DataArrayIdType> partSafe(part);
1672 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 !";
1673 throw INTERP_KERNEL::Exception(oss.str().c_str());
1675 MCAuto< typename MLFieldTraits<T>::F1TSType > f1ts(MLFieldTraits<T>::F1TSType::New());
1676 AssignStaticWritePropertiesTo(*f1ts);
1677 if(part->isIota(mread->getNumberOfNodes()))
1678 f1ts->setFieldNoProfileSBT(f2);
1681 part->setName(f1ts->createNewNameOfPfl().c_str());
1682 f1ts->setFieldProfile(f2,mm,m->getMeshDimension()-mm->getMeshDimension(),part);
1684 f1ts->write(fileName,0);
1690 void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MEDCouplingField *f, bool writeFromScratch)
1693 throw INTERP_KERNEL::Exception("WriteField : input field is null !");
1695 const MEDCoupling::MEDCouplingFieldDouble *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldDouble *>(f));
1698 WriteFieldT<double>(fileName,f1,writeFromScratch);
1703 const MEDCoupling::MEDCouplingFieldInt *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldInt *>(f));
1706 WriteFieldT<int>(fileName,f1,writeFromScratch);
1711 const MEDCoupling::MEDCouplingFieldFloat *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldFloat *>(f));
1714 WriteFieldT<float>(fileName,f1,writeFromScratch);
1718 throw INTERP_KERNEL::Exception("WriteField : input field is not in FLOAT32, FLOAT64, INT32 !");
1721 void MEDCoupling::WriteFieldDep(const std::string& fileName, const MEDCoupling::MEDCouplingField *f, bool writeFromScratch)
1723 WriteField(fileName,f,writeFromScratch);
1727 void WriteFieldUsingAlreadyWrittenMeshT(const std::string& fileName, const typename MEDCoupling::Traits<T>::FieldType *f)
1730 throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMeshT : input field is null !");
1731 f->checkConsistencyLight();
1732 int status(MEDLoaderBase::getStatusOfFile(fileName));
1733 if(status!=MEDLoaderBase::EXIST_RW)
1735 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions or not exists !";
1736 throw INTERP_KERNEL::Exception(oss.str().c_str());
1738 MCAuto< typename MLFieldTraits<T>::F1TSType > f1ts(MLFieldTraits<T>::F1TSType::New());
1739 AssignStaticWritePropertiesTo(*f1ts);
1740 MEDCouplingUMesh *m(dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f->getMesh())));
1743 MCAuto<DataArrayIdType> o2n(m->getRenumArrForMEDFileFrmt());
1744 MCAuto< typename MEDCoupling::Traits<T>::FieldType > f2(f->deepCopy());
1745 f2->renumberCells(o2n->begin(),false);
1746 f1ts->setFieldNoProfileSBT(f2);
1749 f1ts->setFieldNoProfileSBT(f);
1750 f1ts->write(fileName,0);
1753 void MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingField *f)
1756 throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMesh : input field is null !");
1758 const MEDCoupling::MEDCouplingFieldDouble *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldDouble *>(f));
1760 WriteFieldUsingAlreadyWrittenMeshT<double>(fileName,f1);
1764 const MEDCoupling::MEDCouplingFieldInt *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldInt *>(f));
1766 WriteFieldUsingAlreadyWrittenMeshT<int>(fileName,f1);
1770 const MEDCoupling::MEDCouplingFieldFloat *f1(dynamic_cast<const MEDCoupling::MEDCouplingFieldFloat *>(f));
1772 WriteFieldUsingAlreadyWrittenMeshT<float>(fileName,f1);
1775 throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMesh : input field is not in FLOAT32, FLOAT64, INT32 !");