1 // Copyright (C) 2007-2013 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.
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 "MEDFileMeshLL.hxx"
22 #include "MEDFileMesh.hxx"
23 #include "MEDLoaderBase.hxx"
24 #include "MEDFileMeshReadSelector.hxx"
26 #include "MEDCouplingUMesh.hxx"
28 #include "InterpKernelAutoPtr.hxx"
29 #include "CellModel.hxx"
33 extern med_geometry_type typmai[MED_N_CELL_FIXED_GEO];
34 extern INTERP_KERNEL::NormalizedCellType typmai2[MED_N_CELL_FIXED_GEO];
35 extern med_geometry_type typmainoeud[1];
37 using namespace ParaMEDMEM;
39 MEDFileMeshL2::MEDFileMeshL2():_name(MED_NAME_SIZE),_description(MED_COMMENT_SIZE),_univ_name(MED_LNAME_SIZE),_dt_unit(MED_LNAME_SIZE)
43 int MEDFileMeshL2::GetMeshIdFromName(med_idt fid, const char *mname, ParaMEDMEM::MEDCouplingMeshType& meshType, int& dt, int& it, std::string& dtunit1) throw(INTERP_KERNEL::Exception)
45 med_mesh_type type_maillage;
46 char maillage_description[MED_COMMENT_SIZE+1];
47 char dtunit[MED_LNAME_SIZE+1];
49 char nommaa[MED_NAME_SIZE+1];
50 med_int n=MEDnMesh(fid);
53 med_sorting_type stype;
54 std::vector<std::string> ms;
56 med_axis_type axistype;
57 for(int i=0;i<n && !found;i++)
59 int naxis=MEDmeshnAxis(fid,i+1);
60 INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
61 INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
62 MEDmeshInfo(fid,i+1,nommaa,&spaceDim,&dim,&type_maillage,maillage_description,dtunit,&stype,&nstep,&axistype,axisname,axisunit);
63 dtunit1=MEDLoaderBase::buildStringFromFortran(dtunit,sizeof(dtunit));
64 std::string cur=MEDLoaderBase::buildStringFromFortran(nommaa,sizeof(nommaa));
74 std::ostringstream oss;
75 oss << "No such meshname (" << mname << ") in file ! Must be in : ";
76 std::copy(ms.begin(),ms.end(),std::ostream_iterator<std::string>(oss,", "));
77 throw INTERP_KERNEL::Exception(oss.str().c_str());
81 case MED_UNSTRUCTURED_MESH:
82 meshType=UNSTRUCTURED;
84 case MED_STRUCTURED_MESH:
87 MEDmeshGridTypeRd(fid,mname,>);
90 case MED_CARTESIAN_GRID:
93 case MED_CURVILINEAR_GRID:
94 meshType=CURVE_LINEAR;
97 throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getMeshIdFromName : unrecognized structured mesh type ! Supported are :\n - cartesian\n - curve linear\n");
102 throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getMeshIdFromName : unrecognized mesh type !");
106 MEDmeshComputationStepInfo(fid,mname,1,&numdt,&numit,&dtt);
111 double MEDFileMeshL2::CheckMeshTimeStep(med_idt fid, const char *mName, int nstep, int dt, int it) throw(INTERP_KERNEL::Exception)
116 std::vector< std::pair<int,int> > p(nstep);
117 for(int i=0;i<nstep;i++)
119 MEDmeshComputationStepInfo(fid,mName,i+1,&numdt,&numit,&dtt);
120 p[i]=std::make_pair<int,int>(numdt,numit);
121 found=(numdt==dt) && (numit==numit);
125 std::ostringstream oss; oss << "No such iteration=" << dt << ",order=" << it << " numbers found for mesh '" << mName << "' ! ";
126 oss << "Possibilities are : ";
127 for(int i=0;i<nstep;i++)
128 oss << "(" << p[i].first << "," << p[i].second << "), ";
129 throw INTERP_KERNEL::Exception(oss.str().c_str());
134 std::vector<std::string> MEDFileMeshL2::getAxisInfoOnMesh(med_idt fid, int mId, const char *mName, ParaMEDMEM::MEDCouplingMeshType& meshType, int& nstep, int& Mdim) throw(INTERP_KERNEL::Exception)
136 med_mesh_type type_maillage;
138 med_sorting_type stype;
139 med_axis_type axistype;
140 int naxis=MEDmeshnAxis(fid,mId);
141 INTERP_KERNEL::AutoPtr<char> nameTmp=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
142 INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
143 INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
144 INTERP_KERNEL::AutoPtr<char> univTmp=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
145 if(MEDmeshInfo(fid,mId,nameTmp,&spaceDim,&Mdim,&type_maillage,_description.getPointer(),_dt_unit.getPointer(),
146 &stype,&nstep,&axistype,axisname,axisunit)!=0)
147 throw INTERP_KERNEL::Exception("A problem has been detected when trying to get info on mesh !");
148 MEDmeshUniversalNameRd(fid,nameTmp,_univ_name.getPointer());
149 switch(type_maillage)
151 case MED_UNSTRUCTURED_MESH:
152 meshType=UNSTRUCTURED;
154 case MED_STRUCTURED_MESH:
157 MEDmeshGridTypeRd(fid,mName,>);
160 case MED_CARTESIAN_GRID:
163 case MED_CURVILINEAR_GRID:
164 meshType=CURVE_LINEAR;
167 throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getAxisInfoOnMesh : unrecognized structured mesh type ! Supported are :\n - cartesian\n - curve linear\n");
172 throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getMeshIdFromName : unrecognized mesh type !");
175 std::vector<std::string> infosOnComp(naxis);
176 for(int i=0;i<naxis;i++)
178 std::string info=MEDLoaderBase::buildUnionUnit(((char *)axisname)+i*MED_SNAME_SIZE,MED_SNAME_SIZE,((char *)axisunit)+i*MED_SNAME_SIZE,MED_SNAME_SIZE);
184 void MEDFileMeshL2::ReadFamiliesAndGrps(med_idt fid, const char *meshName, std::map<std::string,int>& fams, std::map<std::string, std::vector<std::string> >& grps, MEDFileMeshReadSelector *mrs)
186 if(mrs && !(mrs->isCellFamilyFieldReading() || mrs->isNodeFamilyFieldReading()))
188 char nomfam[MED_NAME_SIZE+1];
190 int nfam=MEDnFamily(fid,meshName);
191 for(int i=0;i<nfam;i++)
193 int ngro=MEDnFamilyGroup(fid,meshName,i+1);
194 med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
195 INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
196 INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
197 INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
198 INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
199 MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
200 std::string famName=MEDLoaderBase::buildStringFromFortran(nomfam,MED_NAME_SIZE);
201 fams[famName]=numfam;
202 for(int j=0;j<ngro;j++)
204 std::string groupname=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
205 grps[groupname].push_back(famName);
210 void MEDFileMeshL2::WriteFamiliesAndGrps(med_idt fid, const char *mname, const std::map<std::string,int>& fams, const std::map<std::string, std::vector<std::string> >& grps, int tooLongStrPol)
212 for(std::map<std::string,int>::const_iterator it=fams.begin();it!=fams.end();it++)
214 std::vector<std::string> grpsOfFam;
215 for(std::map<std::string, std::vector<std::string> >::const_iterator it1=grps.begin();it1!=grps.end();it1++)
217 if(std::find((*it1).second.begin(),(*it1).second.end(),(*it).first)!=(*it1).second.end())
218 grpsOfFam.push_back((*it1).first);
220 int ngro=grpsOfFam.size();
221 INTERP_KERNEL::AutoPtr<char> groName=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE*ngro);
223 for(std::vector<std::string>::const_iterator it2=grpsOfFam.begin();it2!=grpsOfFam.end();it2++,i++)
224 MEDLoaderBase::safeStrCpy2((*it2).c_str(),MED_LNAME_SIZE-1,groName+i*MED_LNAME_SIZE,tooLongStrPol);
225 INTERP_KERNEL::AutoPtr<char> famName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
226 MEDLoaderBase::safeStrCpy((*it).first.c_str(),MED_NAME_SIZE,famName,tooLongStrPol);
227 int ret=MEDfamilyCr(fid,mname,famName,(*it).second,ngro,groName);
232 MEDFileUMeshL2::MEDFileUMeshL2()
236 void MEDFileUMeshL2::loadAll(med_idt fid, int mId, const char *mName, int dt, int it, MEDFileMeshReadSelector *mrs)
241 ParaMEDMEM::MEDCouplingMeshType meshType;
242 std::vector<std::string> infosOnComp=getAxisInfoOnMesh(fid,mId,mName,meshType,nstep,Mdim);
243 if(meshType!=UNSTRUCTURED)
244 throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected an unstructured one whereas in file it is not an unstructured !");
245 _time=CheckMeshTimeStep(fid,mName,nstep,dt,it);
248 loadConnectivity(fid,Mdim,mName,dt,it,mrs);//to improve check (dt,it) coherency
249 loadCoords(fid,mId,infosOnComp,mName,dt,it);
252 void MEDFileUMeshL2::loadConnectivity(med_idt fid, int mdim, const char *mName, int dt, int it, MEDFileMeshReadSelector *mrs)
254 _per_type_mesh.resize(1);
255 _per_type_mesh[0].clear();
256 for(int j=0;j<MED_N_CELL_FIXED_GEO;j++)
258 MEDFileUMeshPerType *tmp=MEDFileUMeshPerType::New(fid,mName,dt,it,mdim,typmai[j],typmai2[j],mrs);
260 _per_type_mesh[0].push_back(tmp);
265 void MEDFileUMeshL2::loadCoords(med_idt fid, int mId, const std::vector<std::string>& infosOnComp, const char *mName, int dt, int it) throw(INTERP_KERNEL::Exception)
267 int spaceDim=infosOnComp.size();
268 med_bool changement,transformation;
269 int nCoords=MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation);
270 _coords=DataArrayDouble::New();
271 _coords->alloc(nCoords,spaceDim);
272 double *coordsPtr=_coords->getPointer();
273 MEDmeshNodeCoordinateRd(fid,mName,dt,it,MED_FULL_INTERLACE,coordsPtr);
274 if(MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
276 _fam_coords=DataArrayInt::New();
277 _fam_coords->alloc(nCoords,1);
278 MEDmeshEntityFamilyNumberRd(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,_fam_coords->getPointer());
282 if(MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
284 _num_coords=DataArrayInt::New();
285 _num_coords->alloc(nCoords,1);
286 MEDmeshEntityNumberRd(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,_num_coords->getPointer());
290 if(MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NAME,MED_NODAL,&changement,&transformation)>0)
292 _name_coords=DataArrayAsciiChar::New();
293 _name_coords->alloc(nCoords+1,MED_SNAME_SIZE);//not a bug to avoid the memory corruption due to last \0 at the end
294 MEDmeshEntityNameRd(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,_name_coords->getPointer());
295 _name_coords->reAlloc(nCoords);//not a bug to avoid the memory corruption due to last \0 at the end
299 for(int i=0;i<spaceDim;i++)
300 _coords->setInfoOnComponent(i,infosOnComp[i].c_str());
303 void MEDFileUMeshL2::sortTypes()
306 std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> > tmp(_per_type_mesh[0]);
307 _per_type_mesh.clear();
308 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=tmp.begin();it!=tmp.end();it++)
309 mdims.insert((*it)->getDim());
312 int mdim=*mdims.rbegin();
313 _per_type_mesh.resize(mdim+1);
314 for(int dim=mdim+1;dim!=0;dim--)
316 std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >& elt=_per_type_mesh[mdim+1-dim];
317 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=tmp.begin();it!=tmp.end();it++)
318 if((*it)->getDim()==dim-1)
321 // suppression of contiguous empty levels at the end of _per_type_mesh.
322 int nbOfUselessLev=0;
324 for(std::vector< std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> > >::reverse_iterator it2=_per_type_mesh.rbegin();it2!=_per_type_mesh.rend();it2++)
326 if((*it2).empty() && isFirst)
333 _per_type_mesh.resize(_per_type_mesh.size()-nbOfUselessLev);
336 void MEDFileUMeshL2::WriteCoords(med_idt fid, const char *mname, int dt, int it, double time, const DataArrayDouble *coords, const DataArrayInt *famCoords, const DataArrayInt *numCoords, const DataArrayAsciiChar *nameCoords)
340 MEDmeshNodeCoordinateWr(fid,mname,dt,it,time,MED_FULL_INTERLACE,coords->getNumberOfTuples(),coords->getConstPointer());
342 MEDmeshEntityFamilyNumberWr(fid,mname,dt,it,MED_NODE,MED_NO_GEOTYPE,famCoords->getNumberOfTuples(),famCoords->getConstPointer());
344 MEDmeshEntityNumberWr(fid,mname,dt,it,MED_NODE,MED_NO_GEOTYPE,numCoords->getNumberOfTuples(),numCoords->getConstPointer());
347 if(nameCoords->getNumberOfComponents()!=MED_SNAME_SIZE)
349 std::ostringstream oss; oss << " MEDFileUMeshL2::WriteCoords : expected a name field on nodes with number of components set to " << MED_SNAME_SIZE;
350 oss << " ! The array has " << nameCoords->getNumberOfComponents() << " components !";
351 throw INTERP_KERNEL::Exception(oss.str().c_str());
353 MEDmeshEntityNameWr(fid,mname,dt,it,MED_NODE,MED_NO_GEOTYPE,nameCoords->getNumberOfTuples(),nameCoords->getConstPointer());
357 bool MEDFileUMeshL2::isFamDefinedOnLev(int levId) const
359 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
360 if((*it)->getFam()==0)
365 bool MEDFileUMeshL2::isNumDefinedOnLev(int levId) const
367 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
368 if((*it)->getNum()==0)
373 bool MEDFileUMeshL2::isNamesDefinedOnLev(int levId) const
375 for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
376 if((*it)->getNames()==0)
381 MEDFileCMeshL2::MEDFileCMeshL2()
385 void MEDFileCMeshL2::loadAll(med_idt fid, int mId, const char *mName, int dt, int it) throw(INTERP_KERNEL::Exception)
390 ParaMEDMEM::MEDCouplingMeshType meshType;
391 std::vector<std::string> infosOnComp=getAxisInfoOnMesh(fid,mId,mName,meshType,nstep,Mdim);
392 if(meshType!=CARTESIAN)
393 throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected a structured one whereas in file it is not a structured !");
394 _time=CheckMeshTimeStep(fid,mName,nstep,dt,it);
398 med_grid_type gridtype;
399 MEDmeshGridTypeRd(fid,mName,&gridtype);
400 if(gridtype!=MED_CARTESIAN_GRID)
401 throw INTERP_KERNEL::Exception("Invalid structured mesh ! Expected cartesian mesh type !");
402 _cmesh=MEDCouplingCMesh::New();
403 for(int i=0;i<Mdim;i++)
405 med_data_type dataTypeReq=GetDataTypeCorrespondingToSpaceId(i);
406 med_bool chgt=MED_FALSE,trsf=MED_FALSE;
407 int nbOfElt=MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NONE,dataTypeReq,MED_NO_CMODE,&chgt,&trsf);
408 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> da=DataArrayDouble::New();
409 da->alloc(nbOfElt,1);
410 da->setInfoOnComponent(0,infosOnComp[i].c_str());
411 MEDmeshGridIndexCoordinateRd(fid,mName,dt,it,i+1,da->getPointer());
412 _cmesh->setCoordsAt(i,da);
416 med_data_type MEDFileCMeshL2::GetDataTypeCorrespondingToSpaceId(int id) throw(INTERP_KERNEL::Exception)
421 return MED_COORDINATE_AXIS1;
423 return MED_COORDINATE_AXIS2;
425 return MED_COORDINATE_AXIS3;
427 throw INTERP_KERNEL::Exception("Invalid meshdim detected in Cartesian Grid !");
431 MEDFileCLMeshL2::MEDFileCLMeshL2()
435 void MEDFileCLMeshL2::loadAll(med_idt fid, int mId, const char *mName, int dt, int it) throw(INTERP_KERNEL::Exception)
440 ParaMEDMEM::MEDCouplingMeshType meshType;
441 std::vector<std::string> infosOnComp=getAxisInfoOnMesh(fid,mId,mName,meshType,nstep,Mdim);
442 if(meshType!=CURVE_LINEAR)
443 throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected a structured one whereas in file it is not a structured !");
444 _time=CheckMeshTimeStep(fid,mName,nstep,dt,it);
448 _clmesh=MEDCouplingCurveLinearMesh::New();
449 INTERP_KERNEL::AutoPtr<int> stGrid=new int[Mdim];
450 MEDmeshGridStructRd(fid,mName,dt,it,stGrid);
451 _clmesh->setNodeGridStructure(stGrid,((int *)stGrid)+Mdim);
452 med_bool chgt=MED_FALSE,trsf=MED_FALSE;
453 int nbNodes=MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&chgt,&trsf);
454 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> da=DataArrayDouble::New();
455 da->alloc(nbNodes,infosOnComp.size());
456 da->setInfoOnComponents(infosOnComp);
457 MEDmeshNodeCoordinateRd(fid,mName,dt,it,MED_FULL_INTERLACE,da->getPointer());
458 _clmesh->setCoords(da);
461 MEDFileUMeshPermCompute::MEDFileUMeshPermCompute(const MEDFileUMeshSplitL1* st):_st(st),_mpt_time(0),_num_time(0)
466 * Warning it returns an instance to deallocate !!!!
468 MEDFileUMeshPermCompute::operator MEDCouplingUMesh *() const
470 _st->_m_by_types->updateTime();
471 _st->_num->updateTime();
472 if((MEDCouplingUMesh *)_m==0)
475 _m=static_cast<MEDCouplingUMesh *>(_st->_m_by_types->deepCpy());
476 _m->renumberCells(_st->_num->getConstPointer(),true);
481 if(_mpt_time==_st->_m_by_types->getTimeOfThis() && _num_time==_st->_num->getTimeOfThis())
486 _m=static_cast<MEDCouplingUMesh *>(_st->_m_by_types->deepCpy());
487 _m->renumberCells(_st->_num->getConstPointer(),true);
493 void MEDFileUMeshPermCompute::operator=(MEDCouplingUMesh *m)
498 void MEDFileUMeshPermCompute::updateTime() const
500 _mpt_time=_st->_m_by_types->getTimeOfThis();
501 _num_time=_st->_num->getTimeOfThis();
504 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(const MEDFileUMeshSplitL1& other):_m_by_types(other._m_by_types),_fam(other._fam),_num(other._num),_names(other._names),_rev_num(other._rev_num),_m(this)
508 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(const MEDFileUMeshL2& l2, const char *mName, int id):_m(this)
510 const std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >& v=l2.getLev(id);
514 std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> > msMSafe(sz);
515 std::vector<const MEDCouplingUMesh *> ms(sz);
516 for(int i=0;i<sz;i++)
518 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> tmp=MEDCouplingUMesh::New("",v[i]->getDim());
519 MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> tmp2=l2.getCoords();
520 tmp->setCoords(tmp2);
521 tmp->setConnectivity(const_cast<DataArrayInt *>(v[i]->getNodal()),const_cast<DataArrayInt *>(v[i]->getNodalIndex()));
522 ms[i]=tmp; msMSafe[i]=tmp;
524 _m_by_types=MEDCouplingUMesh::MergeUMeshesOnSameCoords(ms);
525 _m_by_types->setName(mName);
526 if(l2.isFamDefinedOnLev(id))
528 int nbOfCells=_m_by_types->getNumberOfCells();
529 _fam=DataArrayInt::New();
530 _fam->alloc(nbOfCells,1);
531 int *w=_fam->getPointer();
532 for(int i=0;i<sz;i++)
533 w=std::copy(v[i]->getFam()->getConstPointer(),v[i]->getFam()->getConstPointer()+v[i]->getFam()->getNumberOfTuples(),w);
535 if(l2.isNumDefinedOnLev(id))
537 int nbOfCells=_m_by_types->getNumberOfCells();
538 _num=DataArrayInt::New();
539 _num->alloc(nbOfCells,1);
540 int *w=_num->getPointer();
541 for(int i=0;i<sz;i++)
542 w=std::copy(v[i]->getNum()->getConstPointer(),v[i]->getNum()->getConstPointer()+v[i]->getNum()->getNumberOfTuples(),w);
545 if(l2.isNamesDefinedOnLev(id))
547 int nbOfCells=_m_by_types->getNumberOfCells();
548 _names=DataArrayAsciiChar::New();
549 _names->alloc(nbOfCells,MED_SNAME_SIZE);
550 char *w=_names->getPointer();
551 for(int i=0;i<sz;i++)
552 w=std::copy(v[i]->getNames()->getConstPointer(),v[i]->getNames()->getConstPointer()+v[i]->getNames()->getNbOfElems(),w);
556 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCouplingUMesh *m):_m(this)
561 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCouplingUMesh *m, bool newOrOld):_m(this)
563 assignMesh(m,newOrOld);
566 std::size_t MEDFileUMeshSplitL1::getHeapMemorySize() const
569 if((const MEDCouplingUMesh *)_m_by_types)
571 ret+=_m_by_types->getHeapMemorySize();
572 if((const DataArrayDouble *)_m_by_types->getCoords())
573 ret-=_m_by_types->getCoords()->getHeapMemorySize();
575 if((const DataArrayInt*)_fam)
576 ret+=_fam->getHeapMemorySize();
577 if((const DataArrayInt*)_num)
578 ret+=_num->getHeapMemorySize();
579 if((const DataArrayInt*)_rev_num)
580 ret+=_rev_num->getHeapMemorySize();
581 if((const DataArrayAsciiChar*)_names)
582 ret+=_names->getHeapMemorySize();
586 MEDFileUMeshSplitL1 *MEDFileUMeshSplitL1::deepCpy() const
588 MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshSplitL1> ret=new MEDFileUMeshSplitL1(*this);
589 if((const MEDCouplingUMesh*)_m_by_types)
590 ret->_m_by_types=static_cast<MEDCouplingUMesh*>(_m_by_types->deepCpy());
591 if((const DataArrayInt *)_fam)
592 ret->_fam=_fam->deepCpy();
593 if((const DataArrayInt *)_num)
594 ret->_num=_num->deepCpy();
595 if((const DataArrayInt *)_rev_num)
596 ret->_rev_num=_rev_num->deepCpy();
597 if((const DataArrayAsciiChar *)_names)
598 ret->_names=_names->deepCpy();
602 bool MEDFileUMeshSplitL1::isEqual(const MEDFileUMeshSplitL1 *other, double eps, std::string& what) const
604 const MEDCouplingUMesh *m1=_m_by_types;
605 const MEDCouplingUMesh *m2=other->_m_by_types;
606 if((m1==0 && m2!=0) || (m1!=0 && m2==0))
608 what="Presence of mesh in one sublevel and not in other!";
612 if(!m1->isEqual(m2,eps))
614 what="meshes at a sublevel are not deeply equal !";
617 const DataArrayInt *d1=_fam;
618 const DataArrayInt *d2=other->_fam;
619 if((d1==0 && d2!=0) || (d1!=0 && d2==0))
621 what="Presence of family arr in one sublevel and not in other!";
625 if(!d1->isEqual(*d2))
627 what="family arr at a sublevel are not deeply equal !";
632 if((d1==0 && d2!=0) || (d1!=0 && d2==0))
634 what="Presence of cell numbering arr in one sublevel and not in other!";
638 if(!d1->isEqual(*d2))
640 what="Numbering cell arr at a sublevel are not deeply equal !";
643 const DataArrayAsciiChar *e1=_names;
644 const DataArrayAsciiChar *e2=other->_names;
645 if((e1==0 && e2!=0) || (e1!=0 && e2==0))
647 what="Presence of cell names arr in one sublevel and not in other!";
651 if(!e1->isEqual(*e2))
653 what="Name cell arr at a sublevel are not deeply equal !";
659 void MEDFileUMeshSplitL1::synchronizeTinyInfo(const MEDFileMesh& master) const
661 const MEDCouplingUMesh *tmp=_m_by_types;
664 (const_cast<MEDCouplingUMesh *>(tmp))->setName(master.getName());
665 (const_cast<MEDCouplingUMesh *>(tmp))->setDescription(master.getDescription());
666 (const_cast<MEDCouplingUMesh *>(tmp))->setTime(master.getTimeValue(),master.getIteration(),master.getOrder());
667 (const_cast<MEDCouplingUMesh *>(tmp))->setTimeUnit(master.getTimeUnit());
670 void MEDFileUMeshSplitL1::clearNonDiscrAttributes() const
672 ClearNonDiscrAttributes(_m_by_types);
675 void MEDFileUMeshSplitL1::ClearNonDiscrAttributes(const MEDCouplingMesh *tmp)
679 (const_cast<MEDCouplingMesh *>(tmp))->setName("");
680 (const_cast<MEDCouplingMesh *>(tmp))->setDescription("");
681 (const_cast<MEDCouplingMesh *>(tmp))->setTime(0.,-1,-1);
682 (const_cast<MEDCouplingMesh *>(tmp))->setTimeUnit("");
685 void MEDFileUMeshSplitL1::assignMesh(MEDCouplingUMesh *m, bool newOrOld) throw(INTERP_KERNEL::Exception)
691 _m_by_types=(MEDCouplingUMesh *)m->deepCpy();
692 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=_m_by_types->getRenumArrForConsecutiveCellTypesSpec(typmai2,typmai2+MED_N_CELL_FIXED_GEO);
693 if(!da->isIdentity())
695 _num=da->invertArrayO2N2N2O(m->getNumberOfCells());
698 _m_by_types->renumberCells(da->getConstPointer(),false);
703 if(!m->checkConsecutiveCellTypesAndOrder(typmai2,typmai2+MED_N_CELL_FIXED_GEO))
704 throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::assignMesh : the mode of mesh setting expects to follow the MED file numbering convention ! it is not the case !");
708 _fam=DataArrayInt::New();
709 _fam->alloc(m->getNumberOfCells(),1);
710 _fam->fillWithValue(0);
713 bool MEDFileUMeshSplitL1::empty() const
715 return ((const MEDCouplingUMesh *)_m_by_types)==0;
718 bool MEDFileUMeshSplitL1::presenceOfOneFams(const std::vector<int>& ids) const
720 const DataArrayInt *fam=_fam;
723 return fam->presenceOfValue(ids);
726 int MEDFileUMeshSplitL1::getMeshDimension() const
728 return _m_by_types->getMeshDimension();
731 void MEDFileUMeshSplitL1::simpleRepr(std::ostream& oss) const
733 std::vector<int> code=_m_by_types->getDistributionOfTypes();
734 int nbOfTypes=code.size()/3;
735 for(int i=0;i<nbOfTypes;i++)
737 INTERP_KERNEL::NormalizedCellType typ=(INTERP_KERNEL::NormalizedCellType) code[3*i];
738 oss << " - Number of cells with type " << INTERP_KERNEL::CellModel::GetCellModel(typ).getRepr() << " : " << code[3*i+1] << std::endl;
742 int MEDFileUMeshSplitL1::getSize() const throw(INTERP_KERNEL::Exception)
744 if((const MEDCouplingUMesh *)_m_by_types==0)
745 throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::getSize : no mesh specified at level !");
746 return _m_by_types->getNumberOfCells();
749 MEDCouplingUMesh *MEDFileUMeshSplitL1::getFamilyPart(const int *idsBg, const int *idsEnd, bool renum) const
751 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> eltsToKeep=_fam->getIdsEqualList(idsBg,idsEnd);
752 MEDCouplingUMesh *m=(MEDCouplingUMesh *)_m_by_types->buildPartOfMySelf(eltsToKeep->getConstPointer(),eltsToKeep->getConstPointer()+eltsToKeep->getNumberOfTuples(),true);
754 return renumIfNeeded(m,eltsToKeep->getConstPointer());
758 DataArrayInt *MEDFileUMeshSplitL1::getFamilyPartArr(const int *idsBg, const int *idsEnd, bool renum) const
760 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=_fam->getIdsEqualList(idsBg,idsEnd);
762 return renumIfNeededArr(da);
766 MEDCouplingUMesh *MEDFileUMeshSplitL1::getWholeMesh(bool renum) const
768 MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> tmp;
769 if(renum && ((const DataArrayInt *)_num))
776 DataArrayInt *MEDFileUMeshSplitL1::getOrCreateAndGetFamilyField() throw(INTERP_KERNEL::Exception)
778 if((DataArrayInt *)_fam)
780 MEDCouplingUMesh *m(_m_by_types);
782 throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::getOrCreateAndGetFamilyField : impossible to create a family field array because no mesh specified on this level !");
783 int nbOfTuples=m->getNumberOfCells();
784 _fam=DataArrayInt::New(); _fam->alloc(nbOfTuples,1); _fam->fillWithZero();
788 const DataArrayInt *MEDFileUMeshSplitL1::getFamilyField() const
793 const DataArrayInt *MEDFileUMeshSplitL1::getNumberField() const
798 const DataArrayInt *MEDFileUMeshSplitL1::getRevNumberField() const
803 const DataArrayAsciiChar *MEDFileUMeshSplitL1::getNameField() const
808 void MEDFileUMeshSplitL1::eraseFamilyField()
810 _fam->fillWithZero();
814 * This method ignores _m and _m_by_types.
816 void MEDFileUMeshSplitL1::setGroupsFromScratch(const std::vector<const MEDCouplingUMesh *>& ms, std::map<std::string,int>& familyIds,
817 std::map<std::string, std::vector<std::string> >& groups) throw(INTERP_KERNEL::Exception)
819 std::vector< DataArrayInt * > corr;
820 _m=MEDCouplingUMesh::FuseUMeshesOnSameCoords(ms,0,corr);
821 std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> > corrMSafe(corr.begin(),corr.end());
822 std::vector< std::vector<int> > fidsOfGroups;
823 std::vector< const DataArrayInt * > corr2(corr.begin(),corr.end());
824 _fam=DataArrayInt::MakePartition(corr2,((MEDCouplingUMesh *)_m)->getNumberOfCells(),fidsOfGroups);
825 int nbOfCells=((MEDCouplingUMesh *)_m)->getNumberOfCells();
826 std::map<int,std::string> newfams;
827 std::map<int,int> famIdTrad;
828 TraduceFamilyNumber(fidsOfGroups,familyIds,famIdTrad,newfams);
829 int *w=_fam->getPointer();
830 for(int i=0;i<nbOfCells;i++,w++)
834 void MEDFileUMeshSplitL1::write(med_idt fid, const char *mName, int mdim) const
836 std::vector<MEDCouplingUMesh *> ms=_m_by_types->splitByType();
837 std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> > msMSafe(ms.begin(),ms.end());
839 for(std::vector<MEDCouplingUMesh *>::const_iterator it=ms.begin();it!=ms.end();it++)
841 int nbCells=(*it)->getNumberOfCells();
842 int end=start+nbCells;
843 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> fam,num;
844 MEDCouplingAutoRefCountObjectPtr<DataArrayAsciiChar> names;
845 if((const DataArrayInt *)_fam)
846 fam=_fam->substr(start,end);
847 if((const DataArrayInt *)_num)
848 num=_num->substr(start,end);
849 if((const DataArrayAsciiChar *)_names)
850 names=static_cast<DataArrayAsciiChar *>(_names->substr(start,end));
851 MEDFileUMeshPerType::write(fid,mName,mdim,(*it),fam,num,names);
856 void MEDFileUMeshSplitL1::renumberNodesInConn(const int *newNodeNumbersO2N) throw(INTERP_KERNEL::Exception)
858 MEDCouplingUMesh *m(_m_by_types);
861 m->renumberNodesInConn(newNodeNumbersO2N);
864 void MEDFileUMeshSplitL1::changeFamilyIdArr(int oldId, int newId) throw(INTERP_KERNEL::Exception)
866 DataArrayInt *arr=_fam;
868 arr->changeValue(oldId,newId);
871 void MEDFileUMeshSplitL1::setFamilyArr(DataArrayInt *famArr)
878 MEDCouplingUMesh *mbt(_m_by_types);
880 throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::setFamilyArr : no mesh defined on this level !");
881 famArr->checkNbOfTuplesAndComp(mbt->getNumberOfCells(),1,"MEDFileUMeshSplitL1::setFamilyArr : Problem in size of Family arr ! ");
886 void MEDFileUMeshSplitL1::setRenumArr(DataArrayInt *renumArr)
894 MEDCouplingUMesh *mbt(_m_by_types);
896 throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::setRenumArr : no mesh defined on this level !");
897 renumArr->checkNbOfTuplesAndComp(mbt->getNumberOfCells(),1,"MEDFileUMeshSplitL1::setRenumArr : Problem in size of numbering arr ! ");
903 void MEDFileUMeshSplitL1::setNameArr(DataArrayAsciiChar *nameArr)
910 MEDCouplingUMesh *mbt(_m_by_types);
912 throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::setNameArr : no mesh defined on this level !");
913 nameArr->checkNbOfTuplesAndComp(mbt->getNumberOfCells(),MED_SNAME_SIZE,"MEDFileUMeshSplitL1::setNameArr : Problem in size of name arr ! ");
918 MEDCouplingUMesh *MEDFileUMeshSplitL1::Renumber2(const DataArrayInt *renum, MEDCouplingUMesh *m, const int *cellIds)
923 m->renumberCells(renum->getConstPointer(),true);
926 MEDCouplingAutoRefCountObjectPtr<DataArrayInt> locnum=renum->selectByTupleId(cellIds,cellIds+m->getNumberOfCells());
927 m->renumberCells(locnum->getConstPointer(),true);
932 MEDCouplingUMesh *MEDFileUMeshSplitL1::renumIfNeeded(MEDCouplingUMesh *m, const int *cellIds) const
934 return Renumber2(_num,m,cellIds);
937 DataArrayInt *MEDFileUMeshSplitL1::Renumber(const DataArrayInt *renum, const DataArrayInt *da)
939 if((const DataArrayInt *)renum==0)
942 return const_cast<DataArrayInt *>(da);
944 return renum->selectByTupleId(da->getConstPointer(),da->getConstPointer()+da->getNumberOfTuples());
947 DataArrayInt *MEDFileUMeshSplitL1::renumIfNeededArr(const DataArrayInt *da) const
949 return Renumber(_num,da);
952 std::vector<int> MEDFileUMeshSplitL1::GetNewFamiliesNumber(int nb, const std::map<std::string,int>& families)
955 for(std::map<std::string,int>::const_iterator it=families.begin();it!=families.end();it++)
956 id=std::max(id,(*it).second);
959 std::vector<int> ret(nb);
960 for(int i=1;i<=nb;i++)
965 void MEDFileUMeshSplitL1::TraduceFamilyNumber(const std::vector< std::vector<int> >& fidsGrps, std::map<std::string,int>& familyIds,
966 std::map<int,int>& famIdTrad, std::map<int,std::string>& newfams)
968 std::set<int> allfids;
972 void MEDFileUMeshSplitL1::computeRevNum() const
975 int maxValue=_num->getMaxValue(pos);
976 _rev_num=_num->invertArrayN2O2O2N(maxValue+1);