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 "MEDCouplingGaussLocalization.hxx"
35 #include "InterpKernelAutoPtr.hxx"
49 med_geometry_type typmai[MED_N_CELL_FIXED_GEO] = { MED_POINT1,
73 med_geometry_type typmainoeud[1] = { MED_NONE };
75 INTERP_KERNEL::NormalizedCellType typmai2[MED_N_CELL_FIXED_GEO] = { INTERP_KERNEL::NORM_POINT1,
76 INTERP_KERNEL::NORM_SEG2,
77 INTERP_KERNEL::NORM_SEG3,
78 INTERP_KERNEL::NORM_SEG4,
79 INTERP_KERNEL::NORM_TRI3,
80 INTERP_KERNEL::NORM_QUAD4,
81 INTERP_KERNEL::NORM_TRI6,
82 INTERP_KERNEL::NORM_TRI7,
83 INTERP_KERNEL::NORM_QUAD8,
84 INTERP_KERNEL::NORM_QUAD9,
85 INTERP_KERNEL::NORM_TETRA4,
86 INTERP_KERNEL::NORM_PYRA5,
87 INTERP_KERNEL::NORM_PENTA6,
88 INTERP_KERNEL::NORM_HEXA8,
89 INTERP_KERNEL::NORM_HEXGP12,
90 INTERP_KERNEL::NORM_TETRA10,
91 INTERP_KERNEL::NORM_PYRA13,
92 INTERP_KERNEL::NORM_PENTA15,
93 INTERP_KERNEL::NORM_HEXA20,
94 INTERP_KERNEL::NORM_HEXA27,
95 INTERP_KERNEL::NORM_POLYGON,
96 INTERP_KERNEL::NORM_QPOLYG,
97 INTERP_KERNEL::NORM_POLYHED };
99 med_geometry_type typmai3[34] = { MED_POINT1,//0
135 double _EPS_FOR_NODE_COMP=1.e-12;
137 int _COMP_FOR_CELL=0;
141 using namespace MEDCoupling;
146 * This method returns a first quick overview of mesh with name \a meshName into the file \a fileName.
147 * @param possibilities the relativeToMeshDim authorized to returned maxdim. This vector is systematically cleared at the begin of this method.
148 * @return the maximal mesh dimension of specified mesh. If nothing found -1 is returned.
150 int MEDLoaderNS::readUMeshDimFromFile(const std::string& fileName, const std::string& meshName, std::vector<int>& possibilities)
152 possibilities.clear();
153 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
156 char nommaa[MED_NAME_SIZE+1];
157 char maillage_description[MED_COMMENT_SIZE+1];
158 med_mesh_type type_maillage;
160 std::string trueMeshName;
161 med_int meshId=getIdFromMeshName(fid,meshName,trueMeshName);
162 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
163 med_sorting_type sortingType;
165 med_axis_type axisType;
166 int naxis(MEDmeshnAxis(fid,meshId));
167 INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
168 INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
169 MEDFILESAFECALLERRD0(MEDmeshInfo,(fid,meshId,nommaa,&Sdim,&Mdim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit));
173 throw INTERP_KERNEL::Exception("multisteps on mesh not managed yet !");
177 MEDFILESAFECALLERRD0(MEDmeshComputationStepInfo,(fid,nommaa,1,&numdt,&numit,&dt));
179 for(int i=0;i<MED_N_CELL_GEO_FIXED_CON;i++)
181 med_geometry_type curMedType=typmai[i];
182 med_bool changement,transformation;
183 int curNbOfElemM(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));
184 int curNbOfElemF(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));//limitation
186 med_entity_type whichEntity;
187 MEDLoaderNS::dispatchElems(curNbOfElemM,curNbOfElemF,curNbOfElem,whichEntity);
190 INTERP_KERNEL::NormalizedCellType type=typmai2[i];
191 int curDim=(int)INTERP_KERNEL::CellModel::GetCellModel(type).getDimension();
198 for(std::set<int>::const_reverse_iterator it=poss.rbegin();it!=poss.rend();it++)
199 possibilities.push_back(*it-ret);
206 med_int MEDLoaderNS::getIdFromMeshName(med_idt fid, const std::string& meshName, std::string& trueMeshName)
210 std::vector<std::string> meshes=getMeshNamesFid(fid);
212 throw INTERP_KERNEL::Exception("No mesh in file");
213 trueMeshName=meshes[0];
216 std::string meshNameStr(meshName);
217 std::vector<std::string> meshes=getMeshNamesFid(fid);
219 throw INTERP_KERNEL::Exception("No mesh in file");
220 std::vector<std::string>::iterator iter=std::find(meshes.begin(),meshes.end(),meshNameStr);
221 if(iter==meshes.end())
223 std::ostringstream os2;
224 os2 << "MeshName '" << meshName << "' not in file : meshes available : ";
225 std::copy(meshes.begin(),meshes.end(),std::ostream_iterator<std::string>(os2," "));
226 throw INTERP_KERNEL::Exception(os2.str().c_str());
228 trueMeshName=meshName;
229 return iter-meshes.begin()+1;
232 std::vector<std::string> MEDLoaderNS::getMeshNamesFid(med_idt fid)
234 med_mesh_type type_maillage;
235 char maillage_description[MED_COMMENT_SIZE+1];
236 char dtunit[MED_COMMENT_SIZE+1];
239 char nommaa[MED_NAME_SIZE+1];
240 med_axis_type axistype;
241 med_sorting_type stype;
242 med_int n=MEDnMesh(fid);
243 std::vector<std::string> ret(n);
246 int naxis(MEDmeshnAxis(fid,i+1));
247 INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
248 INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
250 MEDFILESAFECALLERRD0(MEDmeshInfo,(fid,i+1,nommaa,&space_dim,&mesh_dim,&type_maillage,maillage_description,dtunit,&stype,&nstep,&axistype,axisname,axisunit));
251 std::string cur=MEDLoaderBase::buildStringFromFortran(nommaa,sizeof(nommaa));
258 * This methods allows to merger all entities and to considerate only cell types.
260 void MEDLoaderNS::dispatchElems(int nbOfElemCell, int nbOfElemFace, int& nbOfElem, med_entity_type& whichEntity)
262 if(nbOfElemCell>=nbOfElemFace)
264 whichEntity=MED_CELL;
265 nbOfElem=nbOfElemCell;
269 whichEntity=MED_CELL;
270 nbOfElem=nbOfElemFace;
276 void MEDCoupling::AssignStaticWritePropertiesTo(MEDCoupling::MEDFileWritable& obj)
278 obj.setTooLongStrPolicy(_TOO_LONG_STR);
281 bool MEDCoupling::HasXDR()
290 std::string MEDCoupling::MEDFileVersionStr()
294 std::fill(buf,buf+SZ,'\0');
295 const char START_EXPECTED[]="MED-";
296 med_err ret(MEDlibraryStrVersion(buf));
298 throw INTERP_KERNEL::Exception("MEDFileVersionStr : fail to find version of MED file ! It looks very bad !");
299 std::string zeRet(buf);
300 std::size_t pos(zeRet.find(START_EXPECTED,0));
303 std::ostringstream oss; oss << "MEDFileVersionStr : internal error ! The MEDFile returned version (\"" << zeRet << "\") has not the right pattern !";
304 throw INTERP_KERNEL::Exception(oss.str());
306 return zeRet.substr(sizeof(START_EXPECTED)-1,std::string::npos);
309 std::string MEDCoupling::MEDFileVersionOfFileStr(const std::string& fileName)
311 #if MED_NUM_MAJEUR>=3 && MED_NUM_MINEUR>=2 && MED_NUM_RELEASE>=1
312 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
314 const char START_EXPECTED[]="MED-";
316 std::fill(buf,buf+SZ,'\0');
317 MEDFILESAFECALLERRD0(MEDfileStrVersionRd,(fid,buf));
318 std::string ret(buf);
319 std::size_t pos(ret.find(START_EXPECTED,0));
322 std::ostringstream oss; oss << "MEDFileVersionOfFileStr : internal error ! The MEDFile returned version (\"" << ret << "\") has not the right pattern !";
323 throw INTERP_KERNEL::Exception(oss.str());
325 return ret.substr(sizeof(START_EXPECTED)-1,std::string::npos);
327 std::ostringstream oss; oss << "MEDFileVersionOfFileStr : is implemented with MEDFile " << MEDFileVersionStr() << " ! If you need this feature please use version >= 3.2.1.";
328 throw INTERP_KERNEL::Exception(oss.str());
332 void MEDCoupling::MEDFileVersion(int& major, int& minor, int& release)
334 med_int majj,minn,rell;
335 med_err ret(MEDlibraryNumVersion(&majj,&minn,&rell));
337 throw INTERP_KERNEL::Exception("MEDFileVersion : fail to call MEDlibraryNumVersion ! It looks very bad !");
344 * 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.
346 void MEDCoupling::SetEpsilonForNodeComp(double val)
348 _EPS_FOR_NODE_COMP=val;
352 * 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.
354 void MEDCoupling::SetCompPolicyForCell(int val)
360 * This method set the behaviour of MEDLoader when a too long string is seen in datastructure before copy it in MED file.
361 * By default (0) an exception is thrown. If equal to 1 a warning is emitted in std_err but no exception is thrown.
363 void MEDCoupling::SetTooLongStrPolicy(int val)
369 * Given a 'fileName' and a 'meshName' this method returns global information concerning this mesh.
370 * It returns, in this order :
371 * - 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...
372 * - the mesh dimension
373 * - the space dimension
374 * - the number of nodes
376 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)
378 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
380 char nommaa[MED_NAME_SIZE+1];
381 char maillage_description[MED_COMMENT_SIZE+1];
382 med_mesh_type type_maillage;
383 std::string trueMeshName;
384 med_int meshId=MEDLoaderNS::getIdFromMeshName(fid,meshName,trueMeshName);
385 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
386 med_sorting_type sortingType;
388 med_axis_type axisType;
389 int naxis(MEDmeshnAxis(fid,meshId));
390 INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
391 INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
392 MEDFILESAFECALLERRD0(MEDmeshInfo,(fid,meshId,nommaa,&spaceDim,&meshDim,&type_maillage,maillage_description,dt_unit,&sortingType,&nstep,&axisType,axisname,axisunit));
393 if(type_maillage!=MED_UNSTRUCTURED_MESH)
395 std::ostringstream oss; oss << "GetUMeshGlobalInfo : Mesh \""<< meshName << "\" in file \"" << fileName;
396 oss << "\" exists but it is not an unstructured mesh ! This method is not relevant for mesh types that are not unstructured !";
397 throw INTERP_KERNEL::Exception(oss.str().c_str());
401 throw INTERP_KERNEL::Exception("GetUMeshGlobalInfo : multisteps on mesh not managed !");
404 MEDFILESAFECALLERRD0(MEDmeshComputationStepInfo,(fid,nommaa,1,&numdt,&numit,&dt));
406 std::vector<int> dims;
407 std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > geoTypes;
408 med_bool changement,transformation;
409 for(int i=0;i<MED_N_CELL_FIXED_GEO;i++)
411 med_geometry_type curMedType=typmai[i];
412 int curNbOfElemM(MEDmeshnEntity(fid,nommaa,numdt,numit,MED_CELL,curMedType,MED_CONNECTIVITY,MED_NODAL,&changement,&transformation));
415 INTERP_KERNEL::NormalizedCellType typp=typmai2[i];
416 int mdimCell=INTERP_KERNEL::CellModel::GetCellModel(typp).getDimension();
417 dims.push_back(mdimCell);
418 geoTypes.push_back(std::pair<INTERP_KERNEL::NormalizedCellType,int>(typp,curNbOfElemM));
421 int maxLev=*std::max_element(dims.begin(),dims.end());
422 int lowLev=*std::min_element(dims.begin(),dims.end());
423 int nbOfLevels=maxLev-lowLev+1;
424 std::vector< std::vector< std::pair<INTERP_KERNEL::NormalizedCellType,int> > > ret(nbOfLevels);
425 for(std::size_t i=0;i<dims.size();i++)
427 ret[maxLev-dims[i]].push_back(geoTypes[i]);
429 numberOfNodes=MEDmeshnEntity(fid,nommaa,numdt,numit,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation);
433 void MEDCoupling::CheckFileForRead(const std::string& fileName)
435 MEDFileUtilities::CheckFileForRead(fileName);
438 std::vector<std::string> MEDCoupling::GetMeshNames(const std::string& fileName)
440 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
441 return MEDLoaderNS::getMeshNamesFid(fid);
444 std::vector< std::pair<std::string,std::string> > MEDCoupling::GetComponentsNamesOfField(const std::string& fileName, const std::string& fieldName)
446 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
447 med_int nbFields(MEDnField(fid));
448 std::vector<std::string> fields(nbFields);
449 med_field_type typcha;
450 for(int i=0;i<nbFields;i++)
452 med_int ncomp(MEDfieldnComponent(fid,i+1));
453 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
454 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
455 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
458 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
459 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
460 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
461 std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
462 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
463 if(curFieldName==fieldName)
465 std::vector< std::pair<std::string,std::string> > ret(ncomp);
466 for(int j=0;j<ncomp;j++)
467 ret[j]=std::pair<std::string,std::string>(MEDLoaderBase::buildStringFromFortran(((char *)comp)+j*MED_SNAME_SIZE,MED_SNAME_SIZE),
468 MEDLoaderBase::buildStringFromFortran(((char *)unit)+j*MED_SNAME_SIZE,MED_SNAME_SIZE));
471 fields[i]=curFieldName;
473 std::ostringstream oss; oss << "GetComponentsNamesOfField : no such field \"" << fieldName << "\" in file \"" << fileName << "\" !" << std::endl;
474 oss << "Possible field names are : " << std::endl;
475 std::copy(fields.begin(),fields.end(),std::ostream_iterator<std::string>(oss," "));
476 throw INTERP_KERNEL::Exception(oss.str().c_str());
479 std::vector<std::string> MEDCoupling::GetMeshNamesOnField(const std::string& fileName, const std::string& fieldName)
481 std::vector<std::string> ret;
483 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
484 med_int nbFields=MEDnField(fid);
486 med_field_type typcha;
487 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
488 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
491 for(int i=0;i<nbFields;i++)
493 med_int ncomp(MEDfieldnComponent(fid,i+1));
494 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
495 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
497 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
498 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
499 std::string meshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE);
500 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
501 if(curFieldName==fieldName)
502 ret.push_back(meshName);
507 std::vector<std::string> MEDCoupling::GetMeshFamiliesNames(const std::string& fileName, const std::string& meshName)
509 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
510 med_int nfam=MEDnFamily(fid,meshName.c_str());
511 std::vector<std::string> ret(nfam);
512 char nomfam[MED_NAME_SIZE+1];
514 for(int i=0;i<nfam;i++)
516 int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
517 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
518 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
519 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
520 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
521 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
522 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
523 std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
530 std::vector<std::string> MEDCoupling::GetMeshFamiliesNamesOnGroup(const std::string& fileName, const std::string& meshName, const std::string& grpName)
532 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
533 med_int nfam=MEDnFamily(fid,meshName.c_str());
534 std::vector<std::string> ret;
535 char nomfam[MED_NAME_SIZE+1];
537 for(int i=0;i<nfam;i++)
539 int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
540 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
541 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
542 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
543 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
544 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
545 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
546 std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
547 for(int j=0;j<ngro;j++)
549 std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
557 std::vector<std::string> MEDCoupling::GetMeshGroupsNamesOnFamily(const std::string& fileName, const std::string& meshName, const std::string& famName)
559 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
560 med_int nfam(MEDnFamily(fid,meshName.c_str()));
561 std::vector<std::string> ret;
562 char nomfam[MED_NAME_SIZE+1];
565 for(int i=0;i<nfam && !found;i++)
567 int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
568 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
569 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
570 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
571 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
572 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
573 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
574 std::string cur=MEDLoaderBase::buildStringFromFortran(nomfam,sizeof(nomfam));
575 found=(cur==famName);
577 for(int j=0;j<ngro;j++)
579 std::string cur2=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
585 std::ostringstream oss;
586 oss << "GetMeshGroupsNamesOnFamily : no such family \"" << famName << "\" in file \"" << fileName << "\" in mesh \"" << meshName << "\" !";
587 throw INTERP_KERNEL::Exception(oss.str().c_str());
593 std::vector<std::string> MEDCoupling::GetMeshGroupsNames(const std::string& fileName, const std::string& meshName)
595 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
596 med_int nfam=MEDnFamily(fid,meshName.c_str());
597 std::vector<std::string> ret;
598 char nomfam[MED_NAME_SIZE+1];
600 for(int i=0;i<nfam;i++)
602 int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
603 med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
604 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
605 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
606 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
607 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
608 MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
609 for(int j=0;j<ngro;j++)
611 std::string cur=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
612 if(std::find(ret.begin(),ret.end(),cur)==ret.end())
619 std::vector<MEDCoupling::TypeOfField> MEDCoupling::GetTypesOfField(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
621 std::vector<MEDCoupling::TypeOfField> ret;
622 MCAuto<MEDFileAnyTypeFieldMultiTS> fs(MEDFileAnyTypeFieldMultiTS::New(fileName,fieldName,false));
623 if(fs->getMeshName()!=meshName)
625 std::ostringstream oss; oss << "GetTypesOfField : The field \"" << fieldName << "\" in file \"" << fileName << "\" is not lying on mesh \"" << meshName << "\"";
626 oss << " The name of the mesh in file is \"" << fs->getMeshName() << "\"!";
627 throw INTERP_KERNEL::Exception(oss.str().c_str());
629 int nbTS(fs->getNumberOfTS());
632 for(int i=0;i<nbTS;i++)
634 MCAuto<MEDFileAnyTypeField1TS> f1ts(fs->getTimeStepAtPos(i));
635 std::vector<MEDCoupling::TypeOfField> tof(f1ts->getTypesOfFieldAvailable());
636 for(std::vector<MEDCoupling::TypeOfField>::const_iterator it=tof.begin();it!=tof.end();it++)
637 if(std::find(ret.begin(),ret.end(),*it)==ret.end())
640 // sort ret to put before ON_NODES then ON_CELLS then the remaining.
641 std::vector<MEDCoupling::TypeOfField> ret2;
642 if(std::find(ret.begin(),ret.end(),ON_NODES)!=ret.end())
643 ret2.push_back(ON_NODES);
644 if(std::find(ret.begin(),ret.end(),ON_CELLS)!=ret.end())
645 ret2.push_back(ON_CELLS);
646 for(std::vector<MEDCoupling::TypeOfField>::const_iterator it=ret.begin();it!=ret.end();it++)
647 if(*it!=ON_NODES && *it!=ON_CELLS)
652 std::vector<std::string> MEDCoupling::GetAllFieldNames(const std::string& fileName)
654 std::vector<std::string> ret;
655 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
656 med_int nbFields=MEDnField(fid);
657 med_field_type typcha;
658 for(int i=0;i<nbFields;i++)
660 med_int ncomp(MEDfieldnComponent(fid,i+1));
661 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
662 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
663 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
664 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
665 INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
668 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
669 ret.push_back(std::string(nomcha));
674 std::vector<std::string> MEDCoupling::GetAllFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
676 std::vector<std::string> ret;
677 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
678 med_int nbFields=MEDnField(fid);
680 med_field_type typcha;
681 char *maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
682 char *nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
684 for(int i=0;i<nbFields;i++)
686 med_int ncomp(MEDfieldnComponent(fid,i+1));
687 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
688 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
689 INTERP_KERNEL::AutoPtr<char> dt_unit=new char[MED_LNAME_SIZE+1];
692 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
693 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
694 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
696 if(curMeshName==meshName)
697 ret.push_back(curFieldName);
704 std::vector<std::string> MEDCoupling::GetFieldNamesOnMesh(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName)
706 MEDCoupling::CheckFileForRead(fileName);
710 return GetCellFieldNamesOnMesh(fileName,meshName);
712 return GetNodeFieldNamesOnMesh(fileName,meshName);
714 throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
718 std::vector<std::string> MEDCoupling::GetCellFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
720 std::vector<std::string> ret;
721 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
722 med_int nbFields=MEDnField(fid);
724 med_field_type typcha;
725 //med_int nbpdtnor=0,pflsize,*pflval,lnsize;
726 med_int numdt=0,numo=0;
728 char pflname[MED_NAME_SIZE+1]="";
729 char locname[MED_NAME_SIZE+1]="";
730 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
731 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
732 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
736 for(int i=0;i<nbFields;i++)
738 med_int ncomp(MEDfieldnComponent(fid,i+1));
739 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
740 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
741 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
742 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
743 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
745 if(curMeshName==meshName)
748 for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
752 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,1,&numdt,&numo,&dt));
753 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
754 pflname,&profilesize,locname,&nbi));
758 ret.push_back(curFieldName);
767 std::vector<std::string> MEDCoupling::GetNodeFieldNamesOnMesh(const std::string& fileName, const std::string& meshName)
769 std::vector<std::string> ret;
770 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
771 med_int nbFields=MEDnField(fid);
772 char pflname[MED_NAME_SIZE+1]="";
773 char locname[MED_NAME_SIZE+1]="";
775 med_field_type typcha;
776 med_int numdt=0,numo=0;
778 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
779 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
780 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
783 for(int i=0;i<nbFields;i++)
785 med_int ncomp(MEDfieldnComponent(fid,i+1));
786 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
787 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
789 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
790 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
791 std::string curMeshName=MEDLoaderBase::buildStringFromFortran(maa_ass,MED_NAME_SIZE+1);
795 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,1,&numdt,&numo,&dt));
796 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
797 pflname,&profilesize,locname,&nbi));
798 if(curMeshName==meshName && nbOfVal>0)
800 ret.push_back(curFieldName);
807 std::vector< std::pair< std::pair<int,int>, double> > MEDCoupling::GetAllFieldIterations(const std::string& fileName, const std::string& fieldName)
809 std::vector< std::pair< std::pair<int,int>, double > > ret;
810 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
811 med_int nbFields=MEDnField(fid);
813 med_field_type typcha;
814 med_int numdt=0,numo=0;
816 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
817 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
818 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
821 std::ostringstream oss; oss << "GetAllFieldIterations : No field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
822 for(int i=0;i<nbFields;i++)
824 med_int ncomp(MEDfieldnComponent(fid,i+1));
825 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
826 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
828 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
829 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
830 if(curFieldName==fieldName)
832 for(int k=0;k<nbPdt;k++)
834 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
835 ret.push_back(std::make_pair(std::make_pair(numdt,numo),dt));
841 oss << "\"" << curFieldName << "\"";
842 if(i!=nbFields-1) oss << ", ";
846 throw INTERP_KERNEL::Exception(oss.str().c_str());
849 double MEDCoupling::GetTimeAttachedOnFieldIteration(const std::string& fileName, const std::string& fieldName, int iteration, int order)
851 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
852 med_int nbFields=MEDnField(fid);
854 med_field_type typcha;
855 med_int numdt=0,numo=0;
858 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
859 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
860 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
864 double ret=std::numeric_limits<double>::max();
865 for(int i=0;i<nbFields && !found;i++)
867 med_int ncomp(MEDfieldnComponent(fid,i+1));
868 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
869 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
871 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&local,&typcha,comp,unit,dt_unit,&nbPdt));
872 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
873 if(curFieldName==fieldName)
876 for(int k=0;k<nbPdt;k++)
878 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
879 if(numdt==iteration && numo==order)
887 if(!found || !found2)
889 std::ostringstream oss;
890 oss << "No such field with name \"" << fieldName << "\" and iteration,order=(" << iteration << "," << order << ") exists in file \"" << fileName << "\" !";
891 throw INTERP_KERNEL::Exception(oss.str().c_str());
896 std::vector< std::pair<int,int> > MEDCoupling::GetFieldIterations(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, const std::string& fieldName)
898 MEDCoupling::CheckFileForRead(fileName);
902 return GetCellFieldIterations(fileName,meshName,fieldName);
904 return GetNodeFieldIterations(fileName,meshName,fieldName);
906 throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES or ON_CELLS !");
910 std::vector< std::pair<int,int> > MEDCoupling::GetCellFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
912 std::string meshNameCpp(meshName);
913 std::vector< std::pair<int,int> > ret;
914 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
915 med_int nbFields=MEDnField(fid);
917 med_field_type typcha;
918 med_int numdt=0,numo=0;
920 char pflname[MED_NAME_SIZE+1]="";
921 char locname[MED_NAME_SIZE+1]="";
922 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
923 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
924 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
927 std::ostringstream oss; oss << "GetCellFieldIterations : No cell Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
928 std::set<std::string> s2;
929 for(int i=0;i<nbFields;i++)
931 med_int ncomp(MEDfieldnComponent(fid,i+1));
932 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
933 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
935 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
936 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
937 if(curFieldName==fieldName)
940 for(int j=0;j<MED_N_CELL_FIXED_GEO && !found;j++)
942 for(int k=0;k<nbPdt;k++)
945 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
946 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_CELL,typmai[j],1,MED_COMPACT_PFLMODE,
947 pflname,&profilesize,locname,&nbi));
948 std::string maa_ass_cpp(maa_ass);
951 if(meshNameCpp==maa_ass_cpp)
954 ret.push_back(std::make_pair(numdt,numo));
957 s2.insert(maa_ass_cpp);
964 oss << "\"" << curFieldName << "\"";
965 if(i!=nbFields-1) oss << ", ";
972 oss << ". Cell Field \"" << fieldName << "\" exists but lies on meshes with names : \"";
973 std::copy(s2.begin(),s2.end(),std::ostream_iterator<std::string>(oss,"\", \""));
976 throw INTERP_KERNEL::Exception(oss.str().c_str());
981 std::vector< std::pair<int,int> > MEDCoupling::GetNodeFieldIterations(const std::string& fileName, const std::string& meshName, const std::string& fieldName)
983 std::string meshNameCpp(meshName);
984 std::vector< std::pair<int,int> > ret;
985 MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName));
986 med_int nbFields=MEDnField(fid);
988 med_field_type typcha;
989 med_int numdt=0,numo=0;
991 char pflname[MED_NAME_SIZE+1]="";
992 char locname[MED_NAME_SIZE+1]="";
993 INTERP_KERNEL::AutoPtr<char> maa_ass=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
994 INTERP_KERNEL::AutoPtr<char> dt_unit=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
995 INTERP_KERNEL::AutoPtr<char> nomcha=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
998 std::ostringstream oss; oss << "GetNodeFieldIterations : No node Field field with name \"" << fieldName<< "\" in file \"" << fileName << "\" ! Possible fields are : ";
999 std::set<std::string> s2;
1000 for(int i=0;i<nbFields;i++)
1002 med_int ncomp(MEDfieldnComponent(fid,i+1));
1003 INTERP_KERNEL::AutoPtr<char> comp=new char[ncomp*MED_SNAME_SIZE+1];
1004 INTERP_KERNEL::AutoPtr<char> unit=new char[ncomp*MED_SNAME_SIZE+1];
1006 MEDFILESAFECALLERRD0(MEDfieldInfo,(fid,i+1,nomcha,maa_ass,&localmesh,&typcha,comp,unit,dt_unit,&nbPdt));
1007 std::string curFieldName=MEDLoaderBase::buildStringFromFortran(nomcha,MED_NAME_SIZE+1);
1008 if(curFieldName==fieldName)
1010 for(int k=0;k<nbPdt;k++)
1012 int profilesize,nbi;
1013 MEDFILESAFECALLERRD0(MEDfieldComputingStepInfo,(fid,nomcha,k+1,&numdt,&numo,&dt));
1014 med_int nbOfVal(MEDfieldnValueWithProfile(fid,nomcha,numdt,numo,MED_NODE,MED_NONE,1,MED_COMPACT_PFLMODE,
1015 pflname,&profilesize,locname,&nbi));
1016 std::string maa_ass_cpp(maa_ass);
1019 if(meshNameCpp==maa_ass_cpp)
1020 { ret.push_back(std::make_pair(numdt,numo)); }
1022 s2.insert(maa_ass_cpp);
1028 oss << "\"" << curFieldName << "\"";
1029 if(i!=nbFields-1) oss << ", ";
1036 oss << ". Node Field \"" << fieldName << "\" exists but lies on meshes with names : \"";
1037 std::copy(s2.begin(),s2.end(),std::ostream_iterator<std::string>(oss,"\", \""));
1040 throw INTERP_KERNEL::Exception(oss.str().c_str());
1045 MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax)
1047 MEDCoupling::CheckFileForRead(fileName);
1048 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1049 MEDFileMesh *mmPtr(mm);
1050 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1052 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1053 MEDFileCMesh *mmcPtr=dynamic_cast<MEDFileCMesh *>(mmPtr);
1056 const MEDCouplingCMesh *ret(mmcPtr->getMesh()); ret->incrRef();
1057 return const_cast<MEDCouplingCMesh *>(ret);
1059 MEDFileCurveLinearMesh *mmc2Ptr=dynamic_cast<MEDFileCurveLinearMesh *>(mmPtr);
1062 const MEDCouplingCurveLinearMesh *ret(mmc2Ptr->getMesh()); ret->incrRef();
1063 return const_cast<MEDCouplingCurveLinearMesh *>(ret);
1065 std::ostringstream oss; oss << "ReadMeshFromFile : The mesh \"" << meshName << "\" in file \"" << fileName << "\" has not a recognized type !";
1066 throw INTERP_KERNEL::Exception(oss.str().c_str());
1069 MEDCoupling::MEDCouplingMesh *MEDCoupling::ReadMeshFromFile(const std::string& fileName, int meshDimRelToMax)
1071 MEDCoupling::CheckFileForRead(fileName);
1072 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName));
1073 MEDFileMesh *mmPtr(mm);
1074 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1076 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1077 MEDFileCMesh *mmcPtr=dynamic_cast<MEDFileCMesh *>(mmPtr);
1080 const MEDCouplingCMesh *ret(mmcPtr->getMesh()); ret->incrRef();
1081 return const_cast<MEDCouplingCMesh *>(ret);
1083 MEDFileCurveLinearMesh *mmc2Ptr=dynamic_cast<MEDFileCurveLinearMesh *>(mmPtr);
1086 const MEDCouplingCurveLinearMesh *ret(mmc2Ptr->getMesh()); ret->incrRef();
1087 return const_cast<MEDCouplingCurveLinearMesh *>(ret);
1089 std::ostringstream oss; oss << "ReadMeshFromFile (2) : The first mesh \"" << mm->getName() << "\" in file \"" << fileName << "\" has not a recognized type !";
1090 throw INTERP_KERNEL::Exception(oss.str().c_str());
1093 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFile(const std::string& fileName, const std::string& meshName, int meshDimRelToMax)
1095 MEDCoupling::CheckFileForRead(fileName);
1096 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1097 MEDFileMesh *mmPtr(mm);
1098 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1101 std::ostringstream oss; oss << "ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName=\""<< meshName << "\" exists but it is not an unstructured mesh !";
1102 throw INTERP_KERNEL::Exception(oss.str().c_str());
1104 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1107 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFile(const std::string& fileName, int meshDimRelToMax)
1109 MEDCoupling::CheckFileForRead(fileName);
1110 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName));
1111 MEDFileMesh *mmPtr(mm);
1112 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1115 std::ostringstream oss; oss << "ReadUMeshFromFile : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1116 throw INTERP_KERNEL::Exception(oss.str().c_str());
1118 return mmuPtr->getMeshAtLevel(meshDimRelToMax,true);
1121 int MEDCoupling::ReadUMeshDimFromFile(const std::string& fileName, const std::string& meshName)
1123 MEDCoupling::CheckFileForRead(fileName);
1124 std::vector<int> poss;
1125 return MEDLoaderNS::readUMeshDimFromFile(fileName,meshName,poss);
1128 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromFamilies(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::vector<std::string>& fams)
1130 MEDCoupling::CheckFileForRead(fileName);
1131 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1132 MEDFileMesh *mmPtr(mm);
1133 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1136 std::ostringstream oss; oss << "ReadUMeshFromFamilies : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1137 throw INTERP_KERNEL::Exception(oss.str().c_str());
1139 return mmuPtr->getFamilies(meshDimRelToMax,fams,true);
1142 MEDCoupling::MEDCouplingUMesh *MEDCoupling::ReadUMeshFromGroups(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::vector<std::string>& grps)
1144 MEDCoupling::CheckFileForRead(fileName);
1145 MCAuto<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1146 MEDFileMesh *mmPtr(mm);
1147 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1150 std::ostringstream oss; oss << "ReadUMeshFromGroups : With fileName=\""<< fileName << "\", meshName (the first) =\""<< mm->getName() << "\" exists but it is not an unstructured mesh !";
1151 throw INTERP_KERNEL::Exception(oss.str().c_str());
1153 return mmuPtr->getGroups(meshDimRelToMax,grps,true);
1156 MCAuto<MEDCoupling::MEDCouplingFieldDouble> MEDCoupling::ReadField(const std::string& fileName)
1158 std::vector<std::string> fieldNames(GetAllFieldNames(fileName));
1159 std::size_t sz(fieldNames.size());
1162 std::ostringstream oss;
1163 oss << "The file \"" << fileName << "\" contains no field !";
1164 throw INTERP_KERNEL::Exception(oss.str());
1168 std::ostringstream oss;
1169 oss << "In file \"" << fileName << "\" there are more than one field !" << std::endl;
1170 oss << "You are invited to use ReadField(fileName, fieldName) instead to avoid misleading concerning field you want to read !" << std::endl;
1171 oss << "For information, fields available are :" << std::endl;
1172 for(std::vector<std::string>::const_iterator it=fieldNames.begin();it!=fieldNames.end();it++)
1173 oss << " - \"" << *it << "\"" << std::endl;
1174 throw INTERP_KERNEL::Exception(oss.str());
1176 return ReadField(fileName,fieldNames[0]);
1179 MCAuto<MEDCoupling::MEDCouplingFieldDouble> MEDCoupling::ReadField(const std::string& fileName, const std::string& fieldName)
1181 std::vector< std::pair< std::pair<int,int>, double> > iterations(GetAllFieldIterations(fileName,fieldName));
1182 std::size_t sz(iterations.size());
1185 std::ostringstream oss;
1186 oss << "In file \"" << fileName << "\" field \"" << fieldName << "\" exists but with no time steps !";
1187 throw INTERP_KERNEL::Exception(oss.str());
1191 std::ostringstream oss;
1192 oss << "In file \"" << fileName << "\" field \"" << fieldName << "\" exists but with more than one time steps !" << std::endl;
1193 oss << "You are invited to use ReadField(fileName, fieldName, iteration, order) instead to avoid misleading concerning time steps." << std::endl;
1194 oss << "For information, time steps available for field \"" << fieldName << "\" are :" << std::endl;
1195 for(std::vector< std::pair< std::pair<int,int>, double> >::const_iterator it=iterations.begin();it!=iterations.end();it++)
1196 oss << " - " << (*it).first.first << ", " << (*it).first.second << " (" << (*it).second << ")" << std::endl;
1197 throw INTERP_KERNEL::Exception(oss.str());
1199 return ReadField(fileName,fieldName,iterations[0].first.first,iterations[0].first.second);
1202 MCAuto<MEDCoupling::MEDCouplingFieldDouble> MEDCoupling::ReadField(const std::string& fileName, const std::string& fieldName, int iteration, int order)
1204 MCAuto<MEDFileField1TS> f(MEDFileField1TS::New(fileName,fieldName,iteration,order));
1205 MCAuto<MEDFileMesh> mesh(MEDFileMesh::New(fileName,f->getMeshName()));
1206 MCAuto<MEDCoupling::MEDCouplingFieldDouble> ret(f->field(mesh));
1210 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)
1212 MEDCoupling::CheckFileForRead(fileName);
1216 return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldCell(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1218 return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldNode(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1220 return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldGauss(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1222 return MCAuto<MEDCoupling::MEDCouplingFieldDouble>(ReadFieldGaussNE(fileName,meshName,meshDimRelToMax,fieldName,iteration,order));
1224 throw INTERP_KERNEL::Exception("Type of field specified not managed ! manages are ON_NODES, ON_CELLS, ON_GAUSS_PT or ON_GAUSS_NE !");
1228 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsOnSameMesh(MEDCoupling::TypeOfField type, const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1229 const std::vector<std::pair<int,int> >& its)
1232 return std::vector<MEDCoupling::MEDCouplingFieldDouble *>();
1233 MEDCoupling::CheckFileForRead(fileName);
1234 std::vector<MEDCoupling::MEDCouplingFieldDouble *> ret(its.size());
1235 std::vector< MCAuto<MEDCouplingFieldDouble> > retSafe(its.size());
1238 //Retrieving mesh of rank 0 and field on rank 0 too.
1239 MCAuto<MEDFileMesh> mm=MEDFileMesh::New(fileName,meshName);
1240 MEDFileMesh *mmPtr(mm);
1241 MEDFileUMesh *mmuPtr=dynamic_cast<MEDFileUMesh *>(mmPtr);
1243 throw INTERP_KERNEL::Exception("ReadFieldsOnSameMesh : only unstructured mesh is managed !");
1244 MCAuto<MEDCouplingUMesh> m=mmuPtr->getMeshAtLevel(meshDimRelToMax);
1245 const DataArrayInt *o2n=mmuPtr->getNumberFieldAtLevel(meshDimRelToMax);
1246 MCAuto<MEDCouplingUMesh> m2(m->clone(true));
1248 m2->renumberCells(o2n->begin(),true);
1250 for(std::vector<std::pair<int,int> >::const_iterator it=its.begin();it!=its.end();it++,i++)
1252 MCAuto<MEDFileField1TS> ff=MEDFileField1TS::New(fileName,fieldName,(*it).first,(*it).second);
1253 MCAuto<MEDCouplingFieldDouble> retElt=ff->getFieldOnMeshAtLevel(type,m);
1255 retElt->renumberCells(o2n->begin(),true);
1256 retElt->setMesh(m2);
1260 for(std::vector<std::pair<int,int> >::const_iterator it=its.begin();it!=its.end();it++,i++)
1261 ret[i]=retSafe[i].retn();
1265 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsCellOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1266 const std::vector<std::pair<int,int> >& its)
1268 return ReadFieldsOnSameMesh(ON_CELLS,fileName,meshName,meshDimRelToMax,fieldName,its);
1271 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsNodeOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1272 const std::vector<std::pair<int,int> >& its)
1274 return ReadFieldsOnSameMesh(ON_NODES,fileName,meshName,meshDimRelToMax,fieldName,its);
1277 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsGaussOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1278 const std::vector<std::pair<int,int> >& its)
1280 return ReadFieldsOnSameMesh(ON_GAUSS_PT,fileName,meshName,meshDimRelToMax,fieldName,its);
1283 std::vector<MEDCoupling::MEDCouplingFieldDouble *> MEDCoupling::ReadFieldsGaussNEOnSameMesh(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName,
1284 const std::vector<std::pair<int,int> >& its)
1286 return ReadFieldsOnSameMesh(ON_GAUSS_NE,fileName,meshName,meshDimRelToMax,fieldName,its);
1289 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldCell(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1291 MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New(fileName,fieldName,iteration,order));
1292 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1293 MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1294 MEDFileMesh *mPtr(mm);
1295 MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1296 MCAuto<MEDCouplingFieldDouble> ret(ff->getFieldOnMeshAtLevel(ON_CELLS,m));
1299 const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1301 ret->renumberCells(num->begin());
1306 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldNode(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1308 MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New(fileName,fieldName,iteration,order));
1309 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1310 MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1311 MEDFileMesh *mPtr(mm);
1312 MCAuto<MEDCouplingFieldDouble> ret(ff->getFieldOnMeshAtLevel(ON_NODES,m));
1313 MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1314 if(ff->getPflsReallyUsed().empty())
1318 const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1320 ret->renumberCells(num->begin());
1325 DataArrayInt *pfl=0,*arr2=0;
1326 MCAuto<DataArrayDouble> arr(ff->getFieldWithProfile(ON_NODES,meshDimRelToMax,mm,pfl));
1327 MCAuto<DataArrayInt> pflSafe(pfl);
1328 MCAuto<DataArrayInt> mp(m->getCellIdsFullyIncludedInNodeIds(pfl->begin(),pfl->end()));
1329 MCAuto<MEDCouplingUMesh> mzip(static_cast<MEDCouplingUMesh *>(m->buildPartAndReduceNodes(mp->begin(),mp->end(),arr2)));
1330 MCAuto<DataArrayInt> arr2Safe(arr2);
1331 MCAuto<DataArrayInt> arr3(arr2->invertArrayO2N2N2O(mzip->getNumberOfNodes()));
1332 MCAuto<DataArrayInt> pflSorted(pflSafe->deepCopy()); pflSorted->sort(true);
1333 if(!arr3->isEqualWithoutConsideringStr(*pflSorted))
1334 throw INTERP_KERNEL::Exception("ReadFieldNode : not implemented yet !");
1335 if(!arr3->isEqualWithoutConsideringStr(*pflSafe))
1337 MCAuto<DataArrayInt> o2n2(pflSafe->checkAndPreparePermutation());
1338 MCAuto<DataArrayInt> n2o2(o2n2->invertArrayO2N2N2O(o2n2->getNumberOfTuples()));
1339 mzip->renumberNodes(n2o2->begin(),n2o2->getNumberOfTuples());
1348 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldGauss(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1350 MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New(fileName,fieldName,iteration,order));
1351 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1352 MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1353 MEDFileMesh *mPtr(mm);
1354 MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1355 MCAuto<MEDCouplingFieldDouble> ret(ff->getFieldOnMeshAtLevel(ON_GAUSS_PT,m));
1358 const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1360 ret->renumberCells(num->begin());
1365 MEDCoupling::MEDCouplingFieldDouble *MEDCoupling::ReadFieldGaussNE(const std::string& fileName, const std::string& meshName, int meshDimRelToMax, const std::string& fieldName, int iteration, int order)
1367 MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New(fileName,fieldName,iteration,order));
1368 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,meshName));
1369 MCAuto<MEDCouplingMesh> m(mm->getMeshAtLevel(meshDimRelToMax,false));
1370 MEDFileMesh *mPtr(mm);
1371 MEDFileUMesh *muPtr=dynamic_cast<MEDFileUMesh *>(mPtr);
1372 MCAuto<MEDCouplingFieldDouble> ret(ff->getFieldOnMeshAtLevel(ON_GAUSS_NE,m));
1375 const DataArrayInt *num(muPtr->getNumberFieldAtLevel(meshDimRelToMax));
1377 ret->renumberCells(num->begin());
1382 void MEDCoupling::WriteMesh(const std::string& fileName, const MEDCoupling::MEDCouplingMesh *mesh, bool writeFromScratch)
1385 throw INTERP_KERNEL::Exception("WriteMesh : input mesh is null !");
1386 const MEDCouplingUMesh *um(dynamic_cast<const MEDCouplingUMesh *>(mesh));
1389 WriteUMesh(fileName,um,writeFromScratch);
1392 int mod=writeFromScratch?2:0;
1393 const MEDCoupling1GTUMesh *um2(dynamic_cast<const MEDCoupling1GTUMesh *>(mesh));
1396 MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1397 AssignStaticWritePropertiesTo(*mmu);
1398 mmu->setMeshAtLevel(0,const_cast<MEDCoupling1GTUMesh *>(um2));
1399 mmu->write(fileName,mod);
1402 const MEDCouplingCMesh *um3(dynamic_cast<const MEDCouplingCMesh *>(mesh));
1405 MCAuto<MEDFileCMesh> mmc(MEDFileCMesh::New());
1406 AssignStaticWritePropertiesTo(*mmc);
1407 mmc->setMesh(const_cast<MEDCouplingCMesh *>(um3));
1408 mmc->write(fileName,mod);
1411 const MEDCouplingCurveLinearMesh *um4(dynamic_cast<const MEDCouplingCurveLinearMesh *>(mesh));
1414 MCAuto<MEDFileCurveLinearMesh> mmc(MEDFileCurveLinearMesh::New());
1415 AssignStaticWritePropertiesTo(*mmc);
1416 mmc->setMesh(const_cast<MEDCouplingCurveLinearMesh *>(um4));
1417 mmc->write(fileName,mod);
1420 throw INTERP_KERNEL::Exception("WriteMesh : only MEDCouplingUMesh, MEDCoupling1GTUMesh, MEDCouplingCMesh, MEDCouplingCurveLinear are dealed in this API for the moment !");
1423 void MEDCoupling::WriteUMesh(const std::string& fileName, const MEDCoupling::MEDCouplingUMesh *mesh, bool writeFromScratch)
1426 throw INTERP_KERNEL::Exception("WriteUMesh : input mesh is null !");
1427 int mod=writeFromScratch?2:0;
1428 MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1429 AssignStaticWritePropertiesTo(*m);
1430 MCAuto<MEDCouplingUMesh> mcpy(static_cast<MEDCouplingUMesh *>(mesh->deepCopy()));
1431 m->setMeshAtLevel(0,mcpy,true);
1432 m->write(fileName,mod);
1435 void MEDCoupling::WriteUMeshDep(const std::string& fileName, const MEDCoupling::MEDCouplingUMesh *mesh, bool writeFromScratch)
1437 WriteUMesh(fileName,mesh,writeFromScratch);
1440 void MEDCoupling::WriteUMeshesPartition(const std::string& fileName, const std::string& meshNameC, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1442 std::string meshName(meshNameC);
1443 if(meshName.empty())
1444 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 !");
1445 int status=MEDLoaderBase::getStatusOfFile(fileName);
1446 if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
1448 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
1449 throw INTERP_KERNEL::Exception(oss.str().c_str());
1451 MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1452 AssignStaticWritePropertiesTo(*m);
1453 m->setGroupsFromScratch(0,meshes,true);
1454 m->setName(meshNameC);
1455 int mod=writeFromScratch?2:0;
1456 m->write(fileName,mod);
1459 void MEDCoupling::WriteUMeshesPartitionDep(const std::string& fileName, const std::string& meshNameC, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1461 WriteUMeshesPartition(fileName,meshNameC,meshes,writeFromScratch);
1464 void MEDCoupling::WriteUMeshes(const std::string& fileName, const std::vector<const MEDCoupling::MEDCouplingUMesh *>& meshes, bool writeFromScratch)
1466 int mod=writeFromScratch?2:0;
1467 MCAuto<MEDFileUMesh> m(MEDFileUMesh::New());
1468 AssignStaticWritePropertiesTo(*m);
1469 m->setMeshes(meshes,true);
1470 m->write(fileName,mod);
1473 void MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch)
1475 MCAuto<MEDFileField1TS> ff(MEDFileField1TS::New());
1476 AssignStaticWritePropertiesTo(*ff);
1477 MCAuto<MEDCouplingFieldDouble> f2(f->deepCopy());
1478 const MEDCouplingMesh *m(f2->getMesh());
1479 const MEDCouplingUMesh *um(dynamic_cast<const MEDCouplingUMesh *>(m));
1480 const MEDCoupling1GTUMesh *um2(dynamic_cast<const MEDCoupling1GTUMesh *>(m));
1481 const MEDCouplingCMesh *um3(dynamic_cast<const MEDCouplingCMesh *>(m));
1482 const MEDCouplingCurveLinearMesh *um4(dynamic_cast<const MEDCouplingCurveLinearMesh *>(m));
1483 MCAuto<MEDFileMesh> mm;
1484 int mod=writeFromScratch?2:0;
1487 MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1488 AssignStaticWritePropertiesTo(*mmu);
1489 MCAuto<DataArrayInt> o2n(um->getRenumArrForMEDFileFrmt());
1490 MCAuto<DataArrayInt> n2o(o2n->invertArrayO2N2N2O(o2n->getNumberOfTuples()));
1491 f2->renumberCells(o2n->begin(),false);
1492 mmu->setMeshAtLevel(0,const_cast<MEDCouplingUMesh *>(static_cast<const MEDCouplingUMesh *>(f2->getMesh())));
1493 mmu->setRenumFieldArr(0,n2o);
1494 ff->setFieldNoProfileSBT(f2);
1495 mmu->write(fileName,mod);
1499 MCAuto<MEDFileUMesh> mmu(MEDFileUMesh::New());
1500 AssignStaticWritePropertiesTo(*mmu);
1501 mmu->setMeshAtLevel(0,const_cast<MEDCoupling1GTUMesh *>(um2));
1502 ff->setFieldNoProfileSBT(f2);
1503 mmu->write(fileName,mod);
1507 MCAuto<MEDFileCMesh> mmc(MEDFileCMesh::New());
1508 AssignStaticWritePropertiesTo(*mmc);
1509 mmc->setMesh(const_cast<MEDCouplingCMesh *>(um3));
1510 ff->setFieldNoProfileSBT(f2);
1511 mmc->write(fileName,mod);
1515 MCAuto<MEDFileCurveLinearMesh> mmc(MEDFileCurveLinearMesh::New());
1516 AssignStaticWritePropertiesTo(*mmc);
1517 mmc->setMesh(const_cast<MEDCouplingCurveLinearMesh *>(um4));
1518 ff->setFieldNoProfileSBT(f2);
1519 mmc->write(fileName,mod);
1522 throw INTERP_KERNEL::Exception("MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile : only MEDCouplingUMesh, MEDCoupling1GTUMesh, MEDCouplingCMesh, MEDCouplingCurveLinear are dealed in this API for the moment !");
1523 ff->write(fileName,0);
1526 void MEDCoupling::WriteField(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch)
1529 throw INTERP_KERNEL::Exception("WriteField : input field is NULL !");
1530 f->checkConsistencyLight();
1531 int status=MEDLoaderBase::getStatusOfFile(fileName);
1532 if(status!=MEDLoaderBase::EXIST_RW && status!=MEDLoaderBase::NOT_EXIST)
1534 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions !";
1535 throw INTERP_KERNEL::Exception(oss.str().c_str());
1537 if(writeFromScratch || (!writeFromScratch && status==MEDLoaderBase::NOT_EXIST))
1539 MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(fileName,f,true);
1543 std::vector<std::string> meshNames=GetMeshNames(fileName);
1545 throw INTERP_KERNEL::Exception("WriteField : trying to write a field with no mesh !");
1546 std::string fileNameCpp(f->getMesh()->getName());
1547 if(std::find(meshNames.begin(),meshNames.end(),fileNameCpp)==meshNames.end())
1548 MEDLoaderNS::writeFieldWithoutReadingAndMappingOfMeshInFile(fileName,f,false);
1551 MCAuto<MEDFileMesh> mm(MEDFileMesh::New(fileName,f->getMesh()->getName().c_str()));
1552 AssignStaticWritePropertiesTo(*mm);
1553 const MEDFileMesh *mmPtr(mm);
1554 const MEDFileUMesh *mmuPtr=dynamic_cast<const MEDFileUMesh *>(mmPtr);
1556 throw INTERP_KERNEL::Exception("WriteField : only umeshes are supported now !");
1557 MCAuto<MEDCouplingFieldDouble> f2(f->deepCopy());
1558 MEDCouplingUMesh *m=dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh()));
1560 throw INTERP_KERNEL::Exception("WriteField : only umesh in input field supported !");
1561 MCAuto<DataArrayInt> o2n=m->getRenumArrForMEDFileFrmt();
1562 f2->renumberCells(o2n->begin(),false);
1563 m=static_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f2->getMesh()));
1564 MCAuto<MEDCouplingUMesh> mread=mmuPtr->getMeshAtLevel(m->getMeshDimension()-mm->getMeshDimension());
1565 if(f2->getTypeOfField()!=ON_NODES)
1567 m->tryToShareSameCoordsPermute(*mread,_EPS_FOR_NODE_COMP);
1568 DataArrayInt *part=0;
1569 bool b=mread->areCellsIncludedIn(m,_COMP_FOR_CELL,part);
1570 MCAuto<DataArrayInt> partSafe(part);
1573 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 !";
1574 throw INTERP_KERNEL::Exception(oss.str().c_str());
1576 MCAuto<MEDFileField1TS> f1ts(MEDFileField1TS::New());
1577 AssignStaticWritePropertiesTo(*f1ts);
1578 if(part->isIota(mread->getNumberOfCells()))
1579 f1ts->setFieldNoProfileSBT(f2);
1582 part->setName(f1ts->createNewNameOfPfl().c_str());
1583 f1ts->setFieldProfile(f2,mm,m->getMeshDimension()-mm->getMeshDimension(),part);
1585 f1ts->write(fileName,0);
1590 DataArrayInt *part=0;
1591 bool b=mread->getCoords()->areIncludedInMe(m->getCoords(),_EPS_FOR_NODE_COMP,part);
1592 MCAuto<DataArrayInt> partSafe(part);
1595 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 !";
1596 throw INTERP_KERNEL::Exception(oss.str().c_str());
1598 MCAuto<MEDFileField1TS> f1ts(MEDFileField1TS::New());
1599 AssignStaticWritePropertiesTo(*f1ts);
1600 if(part->isIota(mread->getNumberOfNodes()))
1601 f1ts->setFieldNoProfileSBT(f2);
1604 part->setName(f1ts->createNewNameOfPfl().c_str());
1605 f1ts->setFieldProfile(f2,mm,m->getMeshDimension()-mm->getMeshDimension(),part);
1607 f1ts->write(fileName,0);
1613 void MEDCoupling::WriteFieldDep(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f, bool writeFromScratch)
1615 WriteField(fileName,f,writeFromScratch);
1618 void MEDCoupling::WriteFieldUsingAlreadyWrittenMesh(const std::string& fileName, const MEDCoupling::MEDCouplingFieldDouble *f)
1621 throw INTERP_KERNEL::Exception("WriteFieldUsingAlreadyWrittenMesh : input field is null !");
1622 f->checkConsistencyLight();
1623 int status=MEDLoaderBase::getStatusOfFile(fileName);
1624 if(status!=MEDLoaderBase::EXIST_RW)
1626 std::ostringstream oss; oss << "File with name \'" << fileName << "\' has not valid permissions or not exists !";
1627 throw INTERP_KERNEL::Exception(oss.str().c_str());
1629 MCAuto<MEDFileField1TS> f1ts(MEDFileField1TS::New());
1630 AssignStaticWritePropertiesTo(*f1ts);
1631 MEDCouplingUMesh *m(dynamic_cast<MEDCouplingUMesh *>(const_cast<MEDCouplingMesh *>(f->getMesh())));
1634 MCAuto<DataArrayInt> o2n(m->getRenumArrForMEDFileFrmt());
1635 MCAuto<MEDCouplingFieldDouble> f2(f->deepCopy());
1636 f2->renumberCells(o2n->begin(),false);
1637 f1ts->setFieldNoProfileSBT(f2);
1640 f1ts->setFieldNoProfileSBT(f);
1641 f1ts->write(fileName,0);