1 // Copyright (C) 2007-2016 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"
37 #include "InterpKernelAutoPtr.hxx"
51 med_geometry_type typmai[MED_N_CELL_FIXED_GEO] = { MED_POINT1,
76 med_geometry_type typmainoeud[1] = { MED_NONE };
78 INTERP_KERNEL::NormalizedCellType typmai2[MED_N_CELL_FIXED_GEO] = { INTERP_KERNEL::NORM_POINT1,
79 INTERP_KERNEL::NORM_SEG2,
80 INTERP_KERNEL::NORM_SEG3,
81 INTERP_KERNEL::NORM_SEG4,
82 INTERP_KERNEL::NORM_TRI3,
83 INTERP_KERNEL::NORM_QUAD4,
84 INTERP_KERNEL::NORM_TRI6,
85 INTERP_KERNEL::NORM_TRI7,
86 INTERP_KERNEL::NORM_QUAD8,
87 INTERP_KERNEL::NORM_QUAD9,
88 INTERP_KERNEL::NORM_TETRA4,
89 INTERP_KERNEL::NORM_PYRA5,
90 INTERP_KERNEL::NORM_PENTA6,
91 INTERP_KERNEL::NORM_HEXA8,
92 INTERP_KERNEL::NORM_HEXGP12,
93 INTERP_KERNEL::NORM_TETRA10,
94 INTERP_KERNEL::NORM_PYRA13,
95 INTERP_KERNEL::NORM_PENTA15,
96 INTERP_KERNEL::NORM_PENTA18,
97 INTERP_KERNEL::NORM_HEXA20,
98 INTERP_KERNEL::NORM_HEXA27,
99 INTERP_KERNEL::NORM_POLYGON,
100 INTERP_KERNEL::NORM_QPOLYG,
101 INTERP_KERNEL::NORM_POLYHED };
103 med_geometry_type typmai3[34] = { 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 med_int meshId=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 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 int curNbOfElemM(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));
188 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 med_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 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 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(int nbOfElemCell, int nbOfElemFace, 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 !");
348 * This method sets the epsilon value used for node comparison when trying to buid a profile for a field on node/cell on an already written mesh.
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, int& 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 med_int meshId=MEDLoaderNS::getIdFromMeshName(fid,meshName,trueMeshName);
389 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
390 med_sorting_type sortingType;
392 med_axis_type axisType;
393 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,&spaceDim,&meshDim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit));
397 if(type_maillage!=MED_UNSTRUCTURED_MESH)
399 std::ostringstream oss; oss << "GetUMeshGlobalInfo : Mesh \""<< meshName << "\" in file \"" << fileName;
400 oss << "\" exists but it is not an unstructured mesh ! This method is not relevant for mesh types that are not unstructured !";
401 throw INTERP_KERNEL::Exception(oss.str().c_str());
405 throw INTERP_KERNEL::Exception("GetUMeshGlobalInfo : multisteps on mesh not managed !");
408 MEDFILESAFECALLERRD0(MEDmeshComputationStepInfo,(fid,nommaa,1,&numdt,&numit,&dt));
410 std::vector<int> dims;
411 std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > geoTypes;
412 med_bool changement,transformation;
413 for(int i=0;i<MED_N_CELL_FIXED_GEO;i++)
415 med_geometry_type curMedType=typmai[i];
416 int curNbOfElemM(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));
419 INTERP_KERNEL::NormalizedCellType typp=typmai2[i];
420 int mdimCell=INTERP_KERNEL::CellModel::GetCellModel(typp).getDimension();
421 dims.push_back(mdimCell);
422 geoTypes.push_back(std::pair<INTERP_KERNEL::NormalizedCellType,int>(typp,curNbOfElemM));
425 int maxLev=*std::max_element(dims.begin(),dims.end());
426 int lowLev=*std::min_element(dims.begin(),dims.end());
427 int nbOfLevels=maxLev-lowLev+1;
428 std::vector< std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > > ret(nbOfLevels);
429 for(std::size_t i=0;i<dims.size();i++)
431 ret[maxLev-dims[i]].push_back(geoTypes[i]);
433 numberOfNodes=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation);
437 void MEDCoupling::CheckFileForRead(const std::string& fileName)
439 MEDFileUtilities::CheckFileForRead(fileName);
442 std::vector<std::string> MEDCoupling::GetMeshNames(const std::string& fileName)
444 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
445 return MEDLoaderNS::getMeshNamesFid(fid);
448 std::vector< std::pair<std::string,std::string> > MEDCoupling::GetComponentsNamesOfField(const std::string& fileName, const std::string& fieldName)
450 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
451 med_int nbFields(MEDnField(fid));
452 std::vector<std::string> fields(nbFields);
453 med_field_type typcha;
454 for(int i=0;i<nbFields;i++)
456 med_int ncomp(MEDfieldnComponent(fid,i+1));
457 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
458 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
459 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
462 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
463 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
464 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
465 std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
466 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
467 if(curFieldName==fieldName)
469 std::vector< std::pair<std::string,std::string> > ret(ncomp);
470 for(int j=0;j<ncomp;j++)
471 ret[j]=std::pair<std::string,std::string>(MEDLoaderBase::buildStringFromFortran(((char *)comp)+j*MED_SNAME_SIZE,MED_SNAME_SIZE),
472 MEDLoaderBase::buildStringFromFortran(((char *)unit)+j*MED_SNAME_SIZE,MED_SNAME_SIZE));
475 fields[i]=curFieldName;
477 std::ostringstream oss; oss << "GetComponentsNamesOfField : no such field \"" << fieldName << "\" in file \"" << fileName << "\" !" << std::endl;
478 oss << "Possible field names are : " << std::endl;
479 std::copy(fields.begin(),fields.end(),std::ostream_iterator<std::string>(oss," "));
480 throw INTERP_KERNEL::Exception(oss.str().c_str());
483 std::vector<std::string> MEDCoupling::GetMeshNamesOnField(const std::string& fileName, const std::string& fieldName)
485 std::vector<std::string> ret;
487 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
488 med_int nbFields=MEDnField(fid);
490 med_field_type typcha;
491 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
492 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
495 for(int i=0;i<nbFields;i++)
497 med_int ncomp(MEDfieldnComponent(fid,i+1));
498 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
499 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
501 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
502 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
503 std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
504 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
505 if(curFieldName==fieldName)
506 ret.push_back(meshName);
511 std::vector<std::string> MEDCoupling::GetMeshFamiliesNames(const std::string& fileName, const std::string& meshName)
513 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
514 med_int nfam=MEDnFamily(fid,meshName.c_str());
515 std::vector<std::string> ret(nfam);
516 char nomfam[MED_NAME_SIZE+1];
518 for(int i=0;i<nfam;i++)
520 int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
521 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
522 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
523 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
524 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
525 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
526 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
527 std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
534 std::vector<std::string> MEDCoupling::GetMeshFamiliesNamesOnGroup(const std::string& fileName, const std::string& meshName, const std::string& grpName)
536 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
537 med_int nfam=MEDnFamily(fid,meshName.c_str());
538 std::vector<std::string> ret;
539 char nomfam[MED_NAME_SIZE+1];
541 for(int i=0;i<nfam;i++)
543 int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
544 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
545 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
546 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
547 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
548 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
549 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
550 std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
551 for(int j=0;j<ngro;j++)
553 std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
561 std::vector<std::string> MEDCoupling::GetMeshGroupsNamesOnFamily(const std::string& fileName, const std::string& meshName, const std::string& famName)
563 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
564 med_int nfam(MEDnFamily(fid,meshName.c_str()));
565 std::vector<std::string> ret;
566 char nomfam[MED_NAME_SIZE+1];
569 for(int i=0;i<nfam && !found;i++)
571 int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
572 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
573 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
574 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
575 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
576 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
577 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
578 std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
579 found=(cur==famName);
581 for(int j=0;j<ngro;j++)
583 std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
589 std::ostringstream oss;
590 oss << "GetMeshGroupsNamesOnFamily : no such family \"" << famName << "\" in file \"" << fileName << "\" in mesh \"" << meshName << "\" !";
591 throw INTERP_KERNEL::Exception(oss.str().c_str());
597 std::vector<std::string> MEDCoupling::GetMeshGroupsNames(const std::string& fileName, const std::string& meshName)
599 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
600 med_int nfam=MEDnFamily(fid,meshName.c_str());
601 std::vector<std::string> ret;
602 char nomfam[MED_NAME_SIZE+1];
604 for(int i=0;i<nfam;i++)
606 int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
607 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
608 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
609 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
610 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
611 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
612 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
613 for(int j=0;j<ngro;j++)
615 std::string cur=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
616 if(std::find(ret.begin(),ret.end(),cur)==ret.end())
623 std::vector<MEDCoupling::TypeOfField> MEDCoupling::GetTypesOfField(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
625 std::vector<MEDCoupling::TypeOfField> ret;
626 MCAuto<MEDFileAnyTypeFieldMultiTS> fs(MEDFileAnyTypeFieldMultiTS::New(fileName,fieldName,false));
627 if(fs->getMeshName()!=meshName)
629 std::ostringstream oss; oss << "GetTypesOfField : The field \"" << fieldName << "\" in file \"" << fileName << "\" is not lying on mesh \"" << meshName << "\"";
630 oss << " The name of the mesh in file is \"" << fs->getMeshName() << "\"!";
631 throw INTERP_KERNEL::Exception(oss.str().c_str());
633 int nbTS(fs->getNumberOfTS());
636 for(int i=0;i<nbTS;i++)
638 MCAuto<MEDFileAnyTypeField1TS> f1ts(fs->getTimeStepAtPos(i));
639 std::vector<MEDCoupling::TypeOfField> tof(f1ts->getTypesOfFieldAvailable());
640 for(std::vector<MEDCoupling::TypeOfField>::const_iterator it=tof.begin();it!=tof.end();it++)
641 if(std::find(ret.begin(),ret.end(),*it)==ret.end())
644 // sort ret to put before ON_NODES then ON_CELLS then the remaining.
645 std::vector<MEDCoupling::TypeOfField> ret2;
646 if(std::find(ret.begin(),ret.end(),ON_NODES)!=ret.end())
647 ret2.push_back(ON_NODES);
648 if(std::find(ret.begin(),ret.end(),ON_CELLS)!=ret.end())
649 ret2.push_back(ON_CELLS);
650 for(std::vector<MEDCoupling::TypeOfField>::const_iterator it=ret.begin();it!=ret.end();it++)
651 if(*it!=ON_NODES && *it!=ON_CELLS)
656 std::vector<std::string> MEDCoupling::GetAllFieldNames(const std::string& fileName)
658 std::vector<std::string> ret;
659 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
660 med_int nbFields=MEDnField(fid);
661 med_field_type typcha;
662 for(int i=0;i<nbFields;i++)
664 med_int ncomp(MEDfieldnComponent(fid,i+1));
665 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
666 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
667 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
668 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
669 INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
672 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
673 ret.push_back(std::string(nomcha));
678 std::vector<std::string> MEDCoupling::GetAllFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
680 std::vector<std::string> ret;
681 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
682 med_int nbFields=MEDnField(fid);
684 med_field_type typcha;
685 char *maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
686 char *nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
688 for(int i=0;i<nbFields;i++)
690 med_int ncomp(MEDfieldnComponent(fid,i+1));
691 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
692 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
693 INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
696 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
697 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
698 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
700 if(curMeshName==meshName)
701 ret.push_back(curFieldName);
708 std::vector<std::string> MEDCoupling::GetFieldNamesOnMesh(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName)
710 MEDCoupling::CheckFileForRead(fileName);
714 return GetCellFieldNamesOnMesh(fileName,meshName);
716 return GetNodeFieldNamesOnMesh(fileName,meshName);
718 throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
722 std::vector<std::string> MEDCoupling::GetCellFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
724 std::vector<std::string> ret;
725 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
726 med_int nbFields=MEDnField(fid);
728 med_field_type typcha;
729 //med_int nbpdtnor=0,pflsize,*pflval,lnsize;
730 med_int numdt=0,numo=0;
732 char pflname[MED_NAME_SIZE+1]="";
733 char locname[MED_NAME_SIZE+1]="";
734 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
735 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
736 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
740 for(int i=0;i<nbFields;i++)
742 med_int ncomp(MEDfieldnComponent(fid,i+1));
743 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
744 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
745 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
746 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
747 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
749 if(curMeshName==meshName)
752 for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
756 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,1,&numdt,&numo,&dt));
757 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
758 pflname,&profilesize,locname,&nbi));
762 ret.push_back(curFieldName);
771 std::vector<std::string> MEDCoupling::GetNodeFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
773 std::vector<std::string> ret;
774 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
775 med_int nbFields=MEDnField(fid);
776 char pflname[MED_NAME_SIZE+1]="";
777 char locname[MED_NAME_SIZE+1]="";
779 med_field_type typcha;
780 med_int numdt=0,numo=0;
782 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
783 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
784 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
787 for(int i=0;i<nbFields;i++)
789 med_int ncomp(MEDfieldnComponent(fid,i+1));
790 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
791 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
793 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
794 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
795 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
799 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,1,&numdt,&numo,&dt));
800 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
801 pflname,&profilesize,locname,&nbi));
802 if(curMeshName==meshName && nbOfVal>0)
804 ret.push_back(curFieldName);
811 std::vector< std::pair< std::pair<int,int>, double> > MEDCoupling::GetAllFieldIterations(const std::string& fileName, const std::string& fieldName)
813 std::vector< std::pair< std::pair<int,int>, double > > ret;
814 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
815 med_int nbFields=MEDnField(fid);
817 med_field_type typcha;
818 med_int numdt=0,numo=0;
820 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
821 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
822 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
825 std::ostringstream oss; oss << "GetAllFieldIterations : No field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
826 for(int i=0;i<nbFields;i++)
828 med_int ncomp(MEDfieldnComponent(fid,i+1));
829 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
830 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
832 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
833 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
834 if(curFieldName==fieldName)
836 for(int k=0;k<nbPdt;k++)
838 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
839 ret.push_back(std::make_pair(std::make_pair(numdt,numo),dt));
845 oss << "\"" << curFieldName << "\"";
846 if(i!=nbFields-1) oss << ", ";
850 throw INTERP_KERNEL::Exception(oss.str().c_str());
853 double MEDCoupling::GetTimeAttachedOnFieldIteration(const std::string& fileName, const std::string& fieldName, int iteration, int order)
855 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
856 med_int nbFields=MEDnField(fid);
858 med_field_type typcha;
859 med_int numdt=0,numo=0;
862 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
863 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
864 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
868 double ret=std::numeric_limits<double>::max();
869 for(int i=0;i<nbFields && !found;i++)
871 med_int ncomp(MEDfieldnComponent(fid,i+1));
872 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
873 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
875 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&local,&typcha,comp,unit,dt_unit,&nbPdt));
876 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
877 if(curFieldName==fieldName)
880 for(int k=0;k<nbPdt;k++)
882 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
883 if(numdt==iteration && numo==order)
891 if(!found || !found2)
893 std::ostringstream oss;
894 oss << "No such field with name \"" << fieldName << "\" and iteration,order=(" << iteration << "," << order << ") exists in file \"" << fileName << "\" !";
895 throw INTERP_KERNEL::Exception(oss.str().c_str());
900 std::vector< std::pair<int,int> > MEDCoupling::GetFieldIterations(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, const std::string& fieldName)
902 MEDCoupling::CheckFileForRead(fileName);
906 return GetCellFieldIterations(fileName,meshName,fieldName);
908 return GetNodeFieldIterations(fileName,meshName,fieldName);
910 throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
914 std::vector< std::pair<int,int> > MEDCoupling::GetCellFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
916 std::string meshNameCpp(meshName);
917 std::vector< std::pair<int,int> > ret;
918 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
919 med_int nbFields=MEDnField(fid);
921 med_field_type typcha;
922 med_int numdt=0,numo=0;
924 char pflname[MED_NAME_SIZE+1]="";
925 char locname[MED_NAME_SIZE+1]="";
926 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
927 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
928 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
931 std::ostringstream oss; oss << "GetCellFieldIterations : No cell Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
932 std::set<std::string> s2;
933 for(int i=0;i<nbFields;i++)
935 med_int ncomp(MEDfieldnComponent(fid,i+1));
936 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
937 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
939 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
940 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
941 if(curFieldName==fieldName)
944 for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
946 for(int k=0;k<nbPdt;k++)
949 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
950 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
951 pflname,&profilesize,locname,&nbi));
952 std::string maa_ass_cpp(maa_ass);
955 if(meshNameCpp==maa_ass_cpp)
958 ret.push_back(std::make_pair(numdt,numo));
961 s2.insert(maa_ass_cpp);
968 oss << "\"" << curFieldName << "\"";
969 if(i!=nbFields-1) oss << ", ";
976 oss << ". Cell Field \"" << fieldName << "\" exists but lies on meshes with names : \"";
977 std::copy(s2.begin(),s2.end(),std::ostream_iterator<std::string>(oss,"\", \""));
980 throw INTERP_KERNEL::Exception(oss.str().c_str());
985 std::vector< std::pair<int,int> > MEDCoupling::GetNodeFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
987 std::string meshNameCpp(meshName);
988 std::vector< std::pair<int,int> > ret;
989 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
990 med_int nbFields=MEDnField(fid);
992 med_field_type typcha;
993 med_int numdt=0,numo=0;
995 char pflname[MED_NAME_SIZE+1]="";
996 char locname[MED_NAME_SIZE+1]="";
997 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
998 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
999 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
1002 std::ostringstream oss; oss << "GetNodeFieldIterations : No node Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
1003 std::set<std::string> s2;
1004 for(int i=0;i<nbFields;i++)
1006 med_int ncomp(MEDfieldnComponent(fid,i+1));
1007 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
1008 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
1010 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
1011 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
1012 if(curFieldName==fieldName)
1014 for(int k=0;k<nbPdt;k++)
1016 int profilesize,nbi;
1017 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
1018 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
1019 pflname,&profilesize,locname,&nbi));
1020 std::string maa_ass_cpp(maa_ass);
1023 if(meshNameCpp==maa_ass_cpp)
1024 { ret.push_back(std::make_pair(numdt,numo)); }
1026 s2.insert(maa_ass_cpp);
1032 oss << "\"" << curFieldName << "\"";
1033 if(i!=nbFields-1) oss << ", ";
1040 oss << ". Node Field \"" << fieldName << "\" exists but lies on meshes with names : \"";
1041 std::copy(s2.begin(),s2.end(),std::ostream_iterator<std::string>(oss,"\", \""));
1044 throw INTERP_KERNEL::Exception(oss.str().c_str());
1049 MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax)
1051 MEDCoupling::CheckFileForRead(fileName);
1052 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1053 MEDFileMesh *mmPtr(mm);
1054 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1056 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1057 MEDFileCMesh *mmcPtr=dynamic_cast<MEDFileCMesh *>(mmPtr);
1060 const MEDCouplingCMesh *ret(mmcPtr->getMesh()); ret->incrRef();
1061 return const_cast<MEDCouplingCMesh *>(ret);
1063 MEDFileCurveLinearMesh *mmc2Ptr=dynamic_cast<MEDFileCurveLinearMesh *>(mmPtr);
1066 const MEDCouplingCurveLinearMesh *ret(mmc2Ptr->getMesh()); ret->incrRef();
1067 return const_cast<MEDCouplingCurveLinearMesh *>(ret);
1069 std::ostringstream oss; oss << "ReadMeshFromFile : The mesh \"" << meshName << "\" in file \"" << fileName << "\" has not a recognized type !";
1070 throw INTERP_KERNEL::Exception(oss.str().c_str());
1073 MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& fileName, int meshDimRelToMax)
1075 MEDCoupling::CheckFileForRead(fileName);
1076 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName));
1077 MEDFileMesh *mmPtr(mm);
1078 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1080 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1081 MEDFileCMesh *mmcPtr=dynamic_cast<MEDFileCMesh *>(mmPtr);
1084 const MEDCouplingCMesh *ret(mmcPtr->getMesh()); ret->incrRef();
1085 return const_cast<MEDCouplingCMesh *>(ret);
1087 MEDFileCurveLinearMesh *mmc2Ptr=dynamic_cast<MEDFileCurveLinearMesh *>(mmPtr);
1090 const MEDCouplingCurveLinearMesh *ret(mmc2Ptr->getMesh()); ret->incrRef();
1091 return const_cast<MEDCouplingCurveLinearMesh *>(ret);
1093 std::ostringstream oss; oss << "ReadMeshFromFile (2) : The first mesh \"" << mm->getName() << "\" in file \"" << fileName << "\" has not a recognized type !";
1094 throw INTERP_KERNEL::Exception(oss.str().c_str());
1097 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax)
1099 MEDCoupling::CheckFileForRead(fileName);
1100 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1101 MEDFileMesh *mmPtr(mm);
1102 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1105 std::ostringstream oss; oss << "ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName=\""<< meshName << "\" exists but it is not an unstructured mesh !";
1106 throw INTERP_KERNEL::Exception(oss.str().c_str());
1108 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1111 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFile(const std::string& fileName, int meshDimRelToMax)
1113 MEDCoupling::CheckFileForRead(fileName);
1114 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName));
1115 MEDFileMesh *mmPtr(mm);
1116 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1119 std::ostringstream oss; oss << "ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1120 throw INTERP_KERNEL::Exception(oss.str().c_str());
1122 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1125 int MEDCoupling::ReadUMeshDimFromFile(const std::string& fileName, const std::string& meshName)
1127 MEDCoupling::CheckFileForRead(fileName);
1128 std::vector<int> poss;
1129 return MEDLoaderNS::readUMeshDimFromFile(fileName,meshName,poss);
1132 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFamilies(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::vector<std::string>& fams)
1134 MEDCoupling::CheckFileForRead(fileName);
1135 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1136 MEDFileMesh *mmPtr(mm);
1137 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1140 std::ostringstream oss; oss << "ReadUMeshFromFamilies : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1141 throw INTERP_KERNEL::Exception(oss.str().c_str());
1143 return mmuPtr->getFamilies(meshDimRelToMax,fams,true);
1146 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromGroups(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::vector<std::string>& grps)
1148 MEDCoupling::CheckFileForRead(fileName);
1149 MCAuto<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1150 MEDFileMesh *mmPtr(mm);
1151 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1154 std::ostringstream oss; oss << "ReadUMeshFromGroups : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1155 throw INTERP_KERNEL::Exception(oss.str().c_str());
1157 return mmuPtr->getGroups(meshDimRelToMax,grps,true);
1160 MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(const std::string& fileName)
1162 std::vector<std::string> fieldNames(GetAllFieldNames(fileName));
1163 std::size_t sz(fieldNames.size());
1166 std::ostringstream oss;
1167 oss << "The file \"" << fileName << "\" contains no field !";
1168 throw INTERP_KERNEL::Exception(oss.str());
1172 std::ostringstream oss;
1173 oss << "In file \"" << fileName << "\" there are more than one field !" << std::endl;
1174 oss << "You are invited to use ReadField(fileName, fieldName) instead to avoid misleading concerning field you want to read !" << std::endl;
1175 oss << "For information, fields available are :" << std::endl;
1176 for(std::vector<std::string>::const_iterator it=fieldNames.begin();it!=fieldNames.end();it++)
1177 oss << " - \"" << *it << "\"" << std::endl;
1178 throw INTERP_KERNEL::Exception(oss.str());
1180 return ReadField(fileName,fieldNames[0]);
1183 MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(const std::string& fileName, const std::string& fieldName)
1185 std::vector< std::pair< std::pair<int,int>, double> > iterations(GetAllFieldIterations(fileName,fieldName));
1186 std::size_t sz(iterations.size());
1189 std::ostringstream oss;
1190 oss << "In file \"" << fileName << "\" field \"" << fieldName << "\" exists but with no time steps !";
1191 throw INTERP_KERNEL::Exception(oss.str());
1195 std::ostringstream oss;
1196 oss << "In file \"" << fileName << "\" field \"" << fieldName << "\" exists but with more than one time steps !" << std::endl;
1197 oss << "You are invited to use ReadField(fileName, fieldName, iteration, order) instead to avoid misleading concerning time steps." << std::endl;
1198 oss << "For information, time steps available for field \"" << fieldName << "\" are :" << std::endl;
1199 for(std::vector< std::pair< std::pair<int,int>, double> >::const_iterator it=iterations.begin();it!=iterations.end();it++)
1200 oss << " - " << (*it).first.first << ", " << (*it).first.second << " (" << (*it).second << ")" << std::endl;
1201 throw INTERP_KERNEL::Exception(oss.str());
1203 return ReadField(fileName,fieldName,iterations[0].first.first,iterations[0].first.second);
1206 MCAuto<MEDCoupling::MEDCouplingField> MEDCoupling::ReadField(const std::string& fileName, const std::string& fieldName, int iteration, int order)
1208 MCAuto<MEDFileAnyTypeField1TS> f(MEDFileAnyTypeField1TS::New(fileName,fieldName,iteration,order));
1209 MCAuto<MEDFileMesh> mesh(MEDFileMesh::New(fileName,f->getMeshName()));
1211 MCAuto<MEDFileField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileField1TS>(f));
1214 MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(f1->field(mesh));
1215 return MEDCoupling::DynamicCast<MEDCouplingFieldDouble,MEDCouplingField>(ret);
1219 MCAuto<MEDFileIntField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileIntField1TS>(f));
1222 MCAuto<MEDCoupling::MEDCouplingFieldInt> ret(f1->field(mesh));
1223 return MEDCoupling::DynamicCast<MEDCouplingFieldInt,MEDCouplingField>(ret);
1227 MCAuto<MEDFileFloatField1TS> f1(MEDCoupling::DynamicCast<MEDFileAnyTypeField1TS,MEDFileFloatField1TS>(f));
1230 MCAuto<MEDCoupling::MEDCouplingFieldFloat> ret(f1->field(mesh));
1231 return MEDCoupling::DynamicCast<MEDCouplingFieldFloat,MEDCouplingField>(ret);
1234 throw INTERP_KERNEL::Exception("MEDCoupling::ReadField : only FLOAT32, FLOAT64 and INT32 supported for the moment !");
1235 //MCAuto<MEDFileMesh> mesh(MEDFileMesh::New(fileName,f->getMeshName()));
1236 //MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(f->field(mesh));
1240 MCAuto<MEDCoupling::MEDCouplingFieldDouble> 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 MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldCell(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1248 return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldNode(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1250 return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldGauss(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1252 return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(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 DataArrayInt *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 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldCell(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1321 MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New(fileName,fieldName,iteration,order));
1322 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1323 MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1324 MEDFileMesh *mPtr(mm);
1325 MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1326 MCAuto<MEDCouplingFieldDouble> ret(ff->getFieldOnMeshAtLevel(ON_CELLS,m));
1329 const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1331 ret->renumberCells(num->begin());
1336 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldNode(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1338 MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New(fileName,fieldName,iteration,order));
1339 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1340 MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1341 MEDFileMesh *mPtr(mm);
1342 MCAuto<MEDCouplingFieldDouble> ret(ff->getFieldOnMeshAtLevel(ON_NODES,m));
1343 MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1344 if(ff->getPflsReallyUsed().empty())
1348 const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1350 ret->renumberCells(num->begin());
1355 DataArrayInt *pfl=0,*arr2=0;
1356 MCAuto<DataArrayDouble> arr(ff->getFieldWithProfile(ON_NODES,meshDimRelToMax,mm,pfl));
1357 MCAuto<DataArrayInt> pflSafe(pfl);
1358 MCAuto<DataArrayInt> mp(m->getCellIdsFullyIncludedInNodeIds(pfl->begin(),pfl->end()));
1359 MCAuto<MEDCouplingUMesh> mzip(static_cast<MEDCouplingUMesh *>(m->buildPartAndReduceNodes(mp->begin(),mp->end(),arr2)));
1360 MCAuto<DataArrayInt> arr2Safe(arr2);
1361 MCAuto<DataArrayInt> arr3(arr2->invertArrayO2N2N2O(mzip->getNumberOfNodes()));
1362 MCAuto<DataArrayInt> pflSorted(pflSafe->deepCopy()); pflSorted->sort(true);
1363 if(!arr3->isEqualWithoutConsideringStr(*pflSorted))
1364 throw INTERP_KERNEL::Exception("ReadFieldNode : not implemented yet !");
1365 if(!arr3->isEqualWithoutConsideringStr(*pflSafe))
1367 MCAuto<DataArrayInt> o2n2(pflSafe->checkAndPreparePermutation());
1368 MCAuto<DataArrayInt> n2o2(o2n2->invertArrayO2N2N2O(o2n2->getNumberOfTuples()));
1369 mzip->renumberNodes(n2o2->begin(),n2o2->getNumberOfTuples());
1378 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldGauss(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1380 MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New(fileName,fieldName,iteration,order));
1381 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1382 MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1383 MEDFileMesh *mPtr(mm);
1384 MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1385 MCAuto<MEDCouplingFieldDouble> ret(ff->getFieldOnMeshAtLevel(ON_GAUSS_PT,m));
1388 const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1390 ret->renumberCells(num->begin());
1395 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldGaussNE(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1397 MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New(fileName,fieldName,iteration,order));
1398 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1399 MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1400 MEDFileMesh *mPtr(mm);
1401 MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1402 MCAuto<MEDCouplingFieldDouble> ret(ff->getFieldOnMeshAtLevel(ON_GAUSS_NE,m));
1405 const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1407 ret->renumberCells(num->begin());
1412 void MEDCoupling::WriteMesh(const std::string& fileName, const MEDCoupling::MEDCouplingMesh *mesh, bool writeFromScratch)
1415 throw INTERP_KERNEL::Exception("WriteMesh : input mesh is null !");
1416 const MEDCouplingUMesh *um(dynamic_cast<const MEDCouplingUMesh *>(mesh));
1419 WriteUMesh(fileName,um,writeFromScratch);
1422 int mod=writeFromScratch?2:0;
1423 const MEDCoupling1GTUMesh *um2(dynamic_cast<const MEDCoupling1GTUMesh *>(mesh));
1426 MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1427 AssignStaticWritePropertiesTo(*mmu);
1428 mmu->setMeshAtLevel(0,const_cast<MEDCoupling1GTUMesh *>(um2));
1429 mmu->write(fileName,mod);
1432 const MEDCouplingCMesh *um3(dynamic_cast<const MEDCouplingCMesh *>(mesh));
1435 MCAuto<MEDFileCMesh> mmc(MEDFileCMesh::New());
1436 AssignStaticWritePropertiesTo(*mmc);
1437 mmc->setMesh(const_cast<MEDCouplingCMesh *>(um3));
1438 mmc->write(fileName,mod);
1441 const MEDCouplingCurveLinearMesh *um4(dynamic_cast<const MEDCouplingCurveLinearMesh *>(mesh));
1444 MCAuto<MEDFileCurveLinearMesh> mmc(MEDFileCurveLinearMesh::New());
1445 AssignStaticWritePropertiesTo(*mmc);
1446 mmc->setMesh(const_cast<MEDCouplingCurveLinearMesh *>(um4));
1447 mmc->write(fileName,mod);
1450 throw INTERP_KERNEL::Exception("WriteMesh : only MEDCouplingUMesh, MEDCoupling1GTUMesh, MEDCouplingCMesh, MEDCouplingCurveLinear are dealed in this API for the moment !");
1453 void MEDCoupling::WriteUMesh(const std::string& fileName, const MEDCoupling::MEDCouplingUMesh *mesh, bool writeFromScratch)
1456 throw INTERP_KERNEL::Exception("WriteUMesh : input mesh is null !");
1457 int mod=writeFromScratch?2:0;
1458 MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1459 AssignStaticWritePropertiesTo(*m);
1460 MCAuto<MEDCouplingUMesh> mcpy(static_cast<MEDCouplingUMesh *>(mesh->deepCopy()));
1461 m->setMeshAtLevel(0,mcpy,true);
1462 m->write(fileName,mod);
1465 void MEDCoupling::WriteUMeshDep(const std::string& fileName, const MEDCoupling::MEDCouplingUMesh *mesh, bool writeFromScratch)
1467 WriteUMesh(fileName,mesh,writeFromScratch);
1470 void MEDCoupling::WriteUMeshesPartition(const std::string& fileName, const std::string& meshNameC, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1472 std::string meshName(meshNameC);
1473 if(meshName.empty())
1474 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 !");
1475 int status=MEDLoaderBase::getStatusOfFile(fileName);
1476 if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
1478 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
1479 throw INTERP_KERNEL::Exception(oss.str().c_str());
1481 MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1482 AssignStaticWritePropertiesTo(*m);
1483 m->setGroupsFromScratch(0,meshes,true);
1484 m->setName(meshNameC);
1485 int mod=writeFromScratch?2:0;
1486 m->write(fileName,mod);
1489 void MEDCoupling::WriteUMeshesPartitionDep(const std::string& fileName, const std::string& meshNameC, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1491 WriteUMeshesPartition(fileName,meshNameC,meshes,writeFromScratch);
1494 void MEDCoupling::WriteUMeshes(const std::string& fileName, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1496 int mod=writeFromScratch?2:0;
1497 MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1498 AssignStaticWritePropertiesTo(*m);
1499 m->setMeshes(meshes,true);
1500 m->write(fileName,mod);
1503 void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch)
1505 MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New());
1506 AssignStaticWritePropertiesTo(*ff);
1507 MCAuto<MEDCouplingFieldDouble> f2(f->deepCopy());
1508 const MEDCouplingMesh *m(f2->getMesh());
1509 const MEDCouplingUMesh *um(dynamic_cast<const MEDCouplingUMesh *>(m));
1510 const MEDCoupling1GTUMesh *um2(dynamic_cast<const MEDCoupling1GTUMesh *>(m));
1511 const MEDCouplingCMesh *um3(dynamic_cast<const MEDCouplingCMesh *>(m));
1512 const MEDCouplingCurveLinearMesh *um4(dynamic_cast<const MEDCouplingCurveLinearMesh *>(m));
1513 MCAuto<MEDFileMesh> mm;
1514 int mod=writeFromScratch?2:0;
1517 MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1518 AssignStaticWritePropertiesTo(*mmu);
1519 MCAuto<DataArrayInt> o2n(um->getRenumArrForMEDFileFrmt());
1520 MCAuto<DataArrayInt> n2o(o2n->invertArrayO2N2N2O(o2n->getNumberOfTuples()));
1521 f2->renumberCells(o2n->begin(),false);
1522 mmu->setMeshAtLevel(0,const_cast<MEDCouplingUMesh *>(static_cast<const MEDCouplingUMesh *>(f2->getMesh())));
1523 mmu->setRenumFieldArr(0,n2o);
1524 ff->setFieldNoProfileSBT(f2);
1525 mmu->write(fileName,mod);
1529 MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1530 AssignStaticWritePropertiesTo(*mmu);
1531 mmu->setMeshAtLevel(0,const_cast<MEDCoupling1GTUMesh *>(um2));
1532 ff->setFieldNoProfileSBT(f2);
1533 mmu->write(fileName,mod);
1537 MCAuto<MEDFileCMesh> mmc(MEDFileCMesh::New());
1538 AssignStaticWritePropertiesTo(*mmc);
1539 mmc->setMesh(const_cast<MEDCouplingCMesh *>(um3));
1540 ff->setFieldNoProfileSBT(f2);
1541 mmc->write(fileName,mod);
1545 MCAuto<MEDFileCurveLinearMesh> mmc(MEDFileCurveLinearMesh::New());
1546 AssignStaticWritePropertiesTo(*mmc);
1547 mmc->setMesh(const_cast<MEDCouplingCurveLinearMesh *>(um4));
1548 ff->setFieldNoProfileSBT(f2);
1549 mmc->write(fileName,mod);
1552 throw INTERP_KERNEL::Exception("MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile : only MEDCouplingUMesh, MEDCoupling1GTUMesh, MEDCouplingCMesh, MEDCouplingCurveLinear are dealed in this API for the moment !");
1553 ff->write(fileName,0);
1556 void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch)
1559 throw INTERP_KERNEL::Exception("WriteField : input field is NULL !");
1560 f->checkConsistencyLight();
1561 int status=MEDLoaderBase::getStatusOfFile(fileName);
1562 if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
1564 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
1565 throw INTERP_KERNEL::Exception(oss.str().c_str());
1567 if(writeFromScratch || (!writeFromScratch && status==MEDLoaderBase::NOT_EXIST))
1569 MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(fileName,f,true);
1573 std::vector<std::string> meshNames=GetMeshNames(fileName);
1575 throw INTERP_KERNEL::Exception("WriteField : trying to write a field with no mesh !");
1576 std::string fileNameCpp(f->getMesh()->getName());
1577 if(std::find(meshNames.begin(),meshNames.end(),fileNameCpp)==meshNames.end())
1578 MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(fileName,f,false);
1581 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,f->getMesh()->getName().c_str()));
1582 AssignStaticWritePropertiesTo(*mm);
1583 const MEDFileMesh *mmPtr(mm);
1584 const MEDFileUMesh *mmuPtr=dynamic_cast<const MEDFileUMesh *>(mmPtr);
1586 throw INTERP_KERNEL::Exception("WriteField : only umeshes are supported now !");
1587 MCAuto<MEDCouplingFieldDouble> f2(f->deepCopy());
1588 MEDCouplingUMesh *m=dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh()));
1590 throw INTERP_KERNEL::Exception("WriteField : only umesh in input field supported !");
1591 MCAuto<DataArrayInt> o2n=m->getRenumArrForMEDFileFrmt();
1592 f2->renumberCells(o2n->begin(),false);
1593 m=static_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh()));
1594 MCAuto<MEDCouplingUMesh> mread=mmuPtr->getMeshAtLevel(m->getMeshDimension()-mm->getMeshDimension());
1595 if(f2->getTypeOfField()!=ON_NODES)
1597 m->tryToShareSameCoordsPermute(*mread,_EPS_FOR_NODE_COMP);
1598 DataArrayInt *part=0;
1599 bool b=mread->areCellsIncludedIn(m,_COMP_FOR_CELL,part);
1600 MCAuto<DataArrayInt> partSafe(part);
1603 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 !";
1604 throw INTERP_KERNEL::Exception(oss.str().c_str());
1606 MCAuto<MEDFileField1TS> f1ts(MEDFileField1TS::New());
1607 AssignStaticWritePropertiesTo(*f1ts);
1608 if(part->isIota(mread->getNumberOfCells()))
1609 f1ts->setFieldNoProfileSBT(f2);
1612 part->setName(f1ts->createNewNameOfPfl().c_str());
1613 f1ts->setFieldProfile(f2,mm,m->getMeshDimension()-mm->getMeshDimension(),part);
1615 f1ts->write(fileName,0);
1620 DataArrayInt *part=0;
1621 bool b=mread->getCoords()->areIncludedInMe(m->getCoords(),_EPS_FOR_NODE_COMP,part);
1622 MCAuto<DataArrayInt> partSafe(part);
1625 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 !";
1626 throw INTERP_KERNEL::Exception(oss.str().c_str());
1628 MCAuto<MEDFileField1TS> f1ts(MEDFileField1TS::New());
1629 AssignStaticWritePropertiesTo(*f1ts);
1630 if(part->isIota(mread->getNumberOfNodes()))
1631 f1ts->setFieldNoProfileSBT(f2);
1634 part->setName(f1ts->createNewNameOfPfl().c_str());
1635 f1ts->setFieldProfile(f2,mm,m->getMeshDimension()-mm->getMeshDimension(),part);
1637 f1ts->write(fileName,0);
1643 void MEDCoupling::WriteFieldDep(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch)
1645 WriteField(fileName,f,writeFromScratch);
1648 void MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f)
1651 throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMesh : input field is null !");
1652 f->checkConsistencyLight();
1653 int status=MEDLoaderBase::getStatusOfFile(fileName);
1654 if(status!=MEDLoaderBase::EXIST_RW)
1656 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions or not exists !";
1657 throw INTERP_KERNEL::Exception(oss.str().c_str());
1659 MCAuto<MEDFileField1TS> f1ts(MEDFileField1TS::New());
1660 AssignStaticWritePropertiesTo(*f1ts);
1661 MEDCouplingUMesh *m(dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f->getMesh())));
1664 MCAuto<DataArrayInt> o2n(m->getRenumArrForMEDFileFrmt());
1665 MCAuto<MEDCouplingFieldDouble> f2(f->deepCopy());
1666 f2->renumberCells(o2n->begin(),false);
1667 f1ts->setFieldNoProfileSBT(f2);
1670 f1ts->setFieldNoProfileSBT(f);
1671 f1ts->write(fileName,0);