Salome HOME
Merge from V6_main 13/12/2012
[modules/med.git] / src / MEDLoader / MEDFileMeshLL.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDFileMeshLL.hxx"
22 #include "MEDFileMesh.hxx"
23 #include "MEDLoaderBase.hxx"
24
25 #include "MEDCouplingUMesh.hxx"
26
27 #include "InterpKernelAutoPtr.hxx"
28 #include "CellModel.hxx"
29
30 #include <set>
31
32 extern med_geometry_type typmai[MED_N_CELL_FIXED_GEO];
33 extern INTERP_KERNEL::NormalizedCellType typmai2[MED_N_CELL_FIXED_GEO];
34 extern med_geometry_type typmainoeud[1];
35
36 using namespace ParaMEDMEM;
37
38 MEDFileMeshL2::MEDFileMeshL2():_name(MED_NAME_SIZE),_description(MED_COMMENT_SIZE),_dt_unit(MED_LNAME_SIZE)
39 {
40 }
41
42 int MEDFileMeshL2::GetMeshIdFromName(med_idt fid, const char *mname, ParaMEDMEM::MEDCouplingMeshType& meshType, int& dt, int& it, std::string& dtunit1) throw(INTERP_KERNEL::Exception)
43 {
44   med_mesh_type type_maillage;
45   char maillage_description[MED_COMMENT_SIZE+1];
46   char dtunit[MED_LNAME_SIZE+1];
47   med_int spaceDim,dim;
48   char nommaa[MED_NAME_SIZE+1];
49   med_int n=MEDnMesh(fid);
50   bool found=false;
51   int ret=-1;
52   med_sorting_type stype;
53   std::vector<std::string> ms;
54   int nstep;
55   med_axis_type axistype;
56   for(int i=0;i<n && !found;i++)
57     {
58       int naxis=MEDmeshnAxis(fid,i+1);
59       INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
60       INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
61       MEDmeshInfo(fid,i+1,nommaa,&spaceDim,&dim,&type_maillage,maillage_description,dtunit,&stype,&nstep,&axistype,axisname,axisunit);
62       dtunit1=MEDLoaderBase::buildStringFromFortran(dtunit,sizeof(dtunit));
63       std::string cur=MEDLoaderBase::buildStringFromFortran(nommaa,sizeof(nommaa));
64       ms.push_back(cur);
65       if(cur==mname)
66         {
67           found=true;
68           ret=i+1;
69         }
70     }
71   if(!found)
72     {
73       std::ostringstream oss;
74       oss << "No such meshname (" << mname <<  ") in file ! Must be in :";
75       std::copy(ms.begin(),ms.end(),std::ostream_iterator<std::string>(oss,", "));
76       throw INTERP_KERNEL::Exception(oss.str().c_str());
77     }
78   switch(type_maillage)
79     {
80     case MED_UNSTRUCTURED_MESH:
81       meshType=UNSTRUCTURED;
82       break;
83     case MED_STRUCTURED_MESH:
84       meshType=CARTESIAN;
85       break;
86     default:
87       throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getMeshIdFromName : unrecognized mesh type !");
88     }
89   med_int numdt,numit;
90   med_float dtt;
91   MEDmeshComputationStepInfo(fid,mname,1,&numdt,&numit,&dtt);
92   dt=numdt; it=numit;
93   return ret;
94 }
95
96 double MEDFileMeshL2::CheckMeshTimeStep(med_idt fid, const char *mName, int nstep, int dt, int it) throw(INTERP_KERNEL::Exception)
97 {
98   bool found=false;
99   med_int numdt,numit;
100   med_float dtt;
101   std::vector< std::pair<int,int> > p(nstep);
102   for(int i=0;i<nstep;i++)
103     {
104       MEDmeshComputationStepInfo(fid,mName,i+1,&numdt,&numit,&dtt);
105       p[i]=std::make_pair<int,int>(numdt,numit);
106       found=(numdt==dt) && (numit==numit);
107     }
108   if(!found)
109     {
110       std::ostringstream oss; oss << "No such iteration=" << dt << ",order=" << it << " numbers found for mesh '" << mName << "' ! ";
111       oss << "Possibilities are : ";
112       for(int i=0;i<nstep;i++)
113         oss << "(" << p[i].first << "," << p[i].second << "), ";
114       throw INTERP_KERNEL::Exception(oss.str().c_str());
115     }
116   return dtt;
117 }
118
119 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)
120 {
121   med_mesh_type type_maillage;
122   med_int spaceDim;
123   med_sorting_type stype;
124   med_axis_type axistype;
125   int naxis=MEDmeshnAxis(fid,mId);
126   INTERP_KERNEL::AutoPtr<char> nameTmp=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
127   INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
128   INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
129   if(MEDmeshInfo(fid,mId,nameTmp,&spaceDim,&Mdim,&type_maillage,_description.getPointer(),_dt_unit.getPointer(),
130                  &stype,&nstep,&axistype,axisname,axisunit)!=0)
131     throw INTERP_KERNEL::Exception("A problem has been detected when trying to get info on mesh !");
132   switch(type_maillage)
133     {
134     case MED_UNSTRUCTURED_MESH:
135       meshType=UNSTRUCTURED;
136       break;
137     case MED_STRUCTURED_MESH:
138       meshType=CARTESIAN;
139       break;
140     default:
141       throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getMeshIdFromName : unrecognized mesh type !");
142     }
143   //
144   std::vector<std::string> infosOnComp(naxis);
145   for(int i=0;i<naxis;i++)
146     {
147       std::string info=MEDLoaderBase::buildUnionUnit(((char *)axisname)+i*MED_SNAME_SIZE,MED_SNAME_SIZE,((char *)axisunit)+i*MED_SNAME_SIZE,MED_SNAME_SIZE);
148       infosOnComp[i]=info;
149     }
150   return infosOnComp;
151 }
152
153 void MEDFileMeshL2::ReadFamiliesAndGrps(med_idt fid, const char *meshName, std::map<std::string,int>& fams, std::map<std::string, std::vector<std::string> >& grps)
154 {
155   char nomfam[MED_NAME_SIZE+1];
156   med_int numfam;
157   int nfam=MEDnFamily(fid,meshName);
158   for(int i=0;i<nfam;i++)
159     {
160       int ngro=MEDnFamilyGroup(fid,meshName,i+1);
161       med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
162       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
163       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
164       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
165       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
166       MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
167       std::string famName=MEDLoaderBase::buildStringFromFortran(nomfam,MED_NAME_SIZE);
168       fams[famName]=numfam;
169       for(int j=0;j<ngro;j++)
170         {
171           std::string groupname=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
172           grps[groupname].push_back(famName);
173         }
174     }
175 }
176
177 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)
178 {
179   for(std::map<std::string,int>::const_iterator it=fams.begin();it!=fams.end();it++)
180     {
181       std::vector<std::string> grpsOfFam;
182       for(std::map<std::string, std::vector<std::string> >::const_iterator it1=grps.begin();it1!=grps.end();it1++)
183         {
184           if(std::find((*it1).second.begin(),(*it1).second.end(),(*it).first)!=(*it1).second.end())
185             grpsOfFam.push_back((*it1).first);
186         }
187       int ngro=grpsOfFam.size();
188       INTERP_KERNEL::AutoPtr<char> groName=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE*ngro);
189       int i=0;
190       for(std::vector<std::string>::const_iterator it2=grpsOfFam.begin();it2!=grpsOfFam.end();it2++,i++)
191         MEDLoaderBase::safeStrCpy2((*it2).c_str(),MED_LNAME_SIZE-1,groName+i*MED_LNAME_SIZE,tooLongStrPol);
192       INTERP_KERNEL::AutoPtr<char> famName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
193       MEDLoaderBase::safeStrCpy((*it).first.c_str(),MED_NAME_SIZE,famName,tooLongStrPol);
194       int ret=MEDfamilyCr(fid,mname,famName,(*it).second,ngro,groName);
195       ret++;
196     }
197 }
198
199 MEDFileUMeshL2::MEDFileUMeshL2()
200 {
201 }
202
203 void MEDFileUMeshL2::loadAll(med_idt fid, int mId, const char *mName, int dt, int it)
204 {
205   _name.set(mName);
206   int nstep;
207   int Mdim;
208   ParaMEDMEM::MEDCouplingMeshType meshType;
209   std::vector<std::string> infosOnComp=getAxisInfoOnMesh(fid,mId,mName,meshType,nstep,Mdim);
210   if(meshType!=UNSTRUCTURED)
211     throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected an unstructured one whereas in file it is not an unstructured !");
212   _time=CheckMeshTimeStep(fid,mName,nstep,dt,it);
213   _iteration=dt;
214   _order=it;
215   loadConnectivity(fid,Mdim,mName,dt,it);//to improve check (dt,it) coherency
216   loadCoords(fid,mId,infosOnComp,mName,dt,it);
217 }
218
219 void MEDFileUMeshL2::loadConnectivity(med_idt fid, int mdim, const char *mName, int dt, int it)
220 {
221   _per_type_mesh.resize(1);
222   _per_type_mesh[0].clear();
223   for(int j=0;j<MED_N_CELL_FIXED_GEO;j++)
224     {
225       MEDFileUMeshPerType *tmp=MEDFileUMeshPerType::New(fid,mName,dt,it,mdim,typmai[j],typmai2[j]);
226       if(tmp)
227         _per_type_mesh[0].push_back(tmp);
228     }
229   sortTypes();
230 }
231
232 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)
233 {
234   int spaceDim=infosOnComp.size();
235   med_bool changement,transformation;
236   int nCoords=MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation);
237   _coords=DataArrayDouble::New();
238   _coords->alloc(nCoords,spaceDim);
239   double *coordsPtr=_coords->getPointer();
240   MEDmeshNodeCoordinateRd(fid,mName,dt,it,MED_FULL_INTERLACE,coordsPtr);
241   _fam_coords=DataArrayInt::New();
242   _fam_coords->alloc(nCoords,1);
243   _num_coords=DataArrayInt::New();
244   _num_coords->alloc(nCoords,1);
245   if(MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
246     MEDmeshEntityFamilyNumberRd(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,_fam_coords->getPointer());
247   else
248     _fam_coords=0;
249   if(MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
250     MEDmeshEntityNumberRd(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,_num_coords->getPointer());
251   else
252     _num_coords=0;
253   for(int i=0;i<spaceDim;i++)
254     _coords->setInfoOnComponent(i,infosOnComp[i].c_str());
255 }
256
257 void MEDFileUMeshL2::sortTypes()
258 {
259   std::set<int> mdims;
260   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> > tmp(_per_type_mesh[0]);
261   _per_type_mesh.clear();
262   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=tmp.begin();it!=tmp.end();it++)
263     mdims.insert((*it)->getDim());
264   if(mdims.empty())
265     return;
266   int mdim=*mdims.rbegin();
267   _per_type_mesh.resize(mdim+1);
268   for(int dim=mdim+1;dim!=0;dim--)
269     {
270       std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >& elt=_per_type_mesh[mdim+1-dim];
271       for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=tmp.begin();it!=tmp.end();it++)
272         if((*it)->getDim()==dim-1)
273           elt.push_back(*it);
274     }
275   // suppression of contiguous empty levels at the end of _per_type_mesh.
276   int nbOfUselessLev=0;
277   bool isFirst=true;
278   for(std::vector< std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> > >::reverse_iterator it2=_per_type_mesh.rbegin();it2!=_per_type_mesh.rend();it2++)
279     {
280       if((*it2).empty() && isFirst)
281         {
282           nbOfUselessLev++;
283         }
284       else
285         isFirst=false;
286     }
287   _per_type_mesh.resize(_per_type_mesh.size()-nbOfUselessLev);
288 }
289
290 void MEDFileUMeshL2::WriteCoords(med_idt fid, const char *mname, int dt, int it, double time, const DataArrayDouble *coords, const DataArrayInt *famCoords, const DataArrayInt *numCoords)
291 {
292   if(!coords)
293     return ;
294   MEDmeshNodeCoordinateWr(fid,mname,dt,it,time,MED_FULL_INTERLACE,coords->getNumberOfTuples(),coords->getConstPointer());
295   if(famCoords)
296     MEDmeshEntityFamilyNumberWr(fid,mname,dt,it,MED_NODE,MED_NO_GEOTYPE,famCoords->getNumberOfTuples(),famCoords->getConstPointer());
297   if(numCoords)
298     MEDmeshEntityNumberWr(fid,mname,dt,it,MED_NODE,MED_NO_GEOTYPE,numCoords->getNumberOfTuples(),numCoords->getConstPointer());
299 }
300
301 bool MEDFileUMeshL2::isFamDefinedOnLev(int levId) const
302 {
303   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
304     if((*it)->getFam()==0)
305       return false;
306   return true;
307 }
308
309 bool MEDFileUMeshL2::isNumDefinedOnLev(int levId) const
310 {
311   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
312     if((*it)->getNum()==0)
313       return false;
314   return true;
315 }
316
317 MEDFileCMeshL2::MEDFileCMeshL2()
318 {
319 }
320
321 void MEDFileCMeshL2::loadAll(med_idt fid, int mId, const char *mName, int dt, int it) throw(INTERP_KERNEL::Exception)
322 {
323   _name.set(mName);
324   int nstep;
325   int Mdim;
326   ParaMEDMEM::MEDCouplingMeshType meshType;
327   std::vector<std::string> infosOnComp=getAxisInfoOnMesh(fid,mId,mName,meshType,nstep,Mdim);
328   if(meshType!=CARTESIAN)
329     throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected a structured one whereas in file it is not a structured !");
330   _time=CheckMeshTimeStep(fid,mName,nstep,dt,it);
331   _iteration=dt;
332   _order=it;
333   //
334   med_grid_type gridtype;
335   MEDmeshGridTypeRd(fid,mName,&gridtype);
336   if(gridtype!=MED_CARTESIAN_GRID)
337     throw INTERP_KERNEL::Exception("Invalid cartesion mesh type ! Only Cartesian Grid supported ! Curvilinear grid will come soon !");
338   _cmesh=MEDCouplingCMesh::New();
339   for(int i=0;i<Mdim;i++)
340     {
341       med_data_type dataTypeReq=GetDataTypeCorrespondingToSpaceId(i);
342       med_bool chgt=MED_FALSE,trsf=MED_FALSE;
343       int nbOfElt=MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NONE,dataTypeReq,MED_NO_CMODE,&chgt,&trsf);
344       MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> da=DataArrayDouble::New();
345       da->alloc(nbOfElt,1);
346       da->setInfoOnComponent(0,infosOnComp[i].c_str());
347       MEDmeshGridIndexCoordinateRd(fid,mName,dt,it,i+1,da->getPointer());
348       _cmesh->setCoordsAt(i,da);
349     }
350 }
351
352 med_data_type MEDFileCMeshL2::GetDataTypeCorrespondingToSpaceId(int id) throw(INTERP_KERNEL::Exception)
353 {
354   switch(id)
355     {
356     case 0:
357       return MED_COORDINATE_AXIS1;
358     case 1:
359       return MED_COORDINATE_AXIS2;
360     case 2:
361       return MED_COORDINATE_AXIS3;
362     default:
363       throw INTERP_KERNEL::Exception("Invalid meshdim detected in Cartesian Grid !");
364     }
365 }
366
367 MEDFileUMeshPermCompute::MEDFileUMeshPermCompute(const MEDFileUMeshSplitL1* st):_st(st),_mpt_time(0),_num_time(0)
368 {
369 }
370
371 /*!
372  * Warning it returns an instance to deallocate !!!!
373  */
374 MEDFileUMeshPermCompute::operator MEDCouplingUMesh *() const
375 {
376   _st->_m_by_types->updateTime();
377   _st->_num->updateTime();
378   if((MEDCouplingUMesh *)_m==0)
379     {
380       updateTime();
381       MEDCouplingUMesh *ret=(MEDCouplingUMesh *)_st->_m_by_types->deepCpy();
382       _m=ret;
383       _m->renumberCells(_st->_num->getConstPointer(),true);
384       ret->incrRef();
385       return ret;
386     }
387   else
388     {
389       if(_mpt_time==_st->_m_by_types->getTimeOfThis() && _num_time==_st->_num->getTimeOfThis())
390         {
391           _m->incrRef();
392           return _m;
393         }
394       else
395         {
396           updateTime();
397           MEDCouplingUMesh *ret=(MEDCouplingUMesh *)_st->_m_by_types->deepCpy();
398           _m=ret;
399           _m->renumberCells(_st->_num->getConstPointer(),true);
400           ret->incrRef();
401           return ret;
402         }
403     }
404 }
405
406 void MEDFileUMeshPermCompute::operator=(MEDCouplingUMesh *m)
407 {
408   _m=m;
409 }
410
411 void MEDFileUMeshPermCompute::updateTime() const
412 {
413   _mpt_time=_st->_m_by_types->getTimeOfThis();
414   _num_time=_st->_num->getTimeOfThis();
415 }
416
417 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(const MEDFileUMeshL2& l2, const char *mName, int id):_m(this)
418 {
419   const std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >& v=l2.getLev(id);
420   if(v.empty())
421     return;
422   int sz=v.size();
423   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> > msMSafe(sz);
424   std::vector<const MEDCouplingUMesh *> ms(sz);
425   for(int i=0;i<sz;i++)
426     {
427       MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> tmp=MEDCouplingUMesh::New("",v[i]->getDim());
428       MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> tmp2=l2.getCoords();
429       tmp->setCoords(tmp2);
430       tmp->setConnectivity(const_cast<DataArrayInt *>(v[i]->getNodal()),const_cast<DataArrayInt *>(v[i]->getNodalIndex()));
431       ms[i]=tmp; msMSafe[i]=tmp;
432     }
433   _m_by_types=MEDCouplingUMesh::MergeUMeshesOnSameCoords(ms);
434   _m_by_types->setName(mName);
435   if(l2.isFamDefinedOnLev(id))
436     {
437       int nbOfCells=_m_by_types->getNumberOfCells();
438       _fam=DataArrayInt::New();
439       _fam->alloc(nbOfCells,1);
440       int *w=_fam->getPointer();
441       for(int i=0;i<sz;i++)
442         w=std::copy(v[i]->getFam()->getConstPointer(),v[i]->getFam()->getConstPointer()+v[i]->getFam()->getNumberOfTuples(),w);
443     }
444   if(l2.isNumDefinedOnLev(id))
445     {
446       int nbOfCells=_m_by_types->getNumberOfCells();
447       _num=DataArrayInt::New();
448       _num->alloc(nbOfCells,1);
449       int *w=_num->getPointer();
450       for(int i=0;i<sz;i++)
451         w=std::copy(v[i]->getNum()->getConstPointer(),v[i]->getNum()->getConstPointer()+v[i]->getNum()->getNumberOfTuples(),w);
452       computeRevNum();
453     }
454 }
455
456 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCouplingUMesh *m):_m(this)
457 {
458   assignMesh(m,true);
459 }
460
461 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCouplingUMesh *m, bool newOrOld):_m(this)
462 {
463   assignMesh(m,newOrOld);
464 }
465
466 bool MEDFileUMeshSplitL1::isEqual(const MEDFileUMeshSplitL1 *other, double eps, std::string& what) const
467 {
468   const MEDCouplingUMesh *m1=_m_by_types;
469   const MEDCouplingUMesh *m2=other->_m_by_types;
470   if((m1==0 && m2!=0) || (m1!=0 && m2==0))
471     {
472       what="Presence of mesh in one sublevel and not in other!";
473       return false;
474     }
475   if(m1)
476     if(!m1->isEqual(m2,eps))
477       {
478         what="meshes at a sublevel are not deeply equal !";
479         return false;
480       }
481   const DataArrayInt *d1=_fam;
482   const DataArrayInt *d2=other->_fam;
483   if((d1==0 && d2!=0) || (d1!=0 && d2==0))
484     {
485       what="Presence of family arr in one sublevel and not in other!";
486       return false;
487     }
488   if(d1)
489     if(!d1->isEqual(*d2))
490       {
491         what="family arr at a sublevel are not deeply equal !";
492         return false;
493       }
494   d1=_num;
495   d2=other->_num;
496   if((d1==0 && d2!=0) || (d1!=0 && d2==0))
497     {
498       what="Presence of cell numbering arr in one sublevel and not in other!";
499       return false;
500     }
501   if(d1)
502     if(!d1->isEqual(*d2))
503       {
504         what="Numbering cell arr at a sublevel are not deeply equal !";
505         return false;
506       }
507   return true;
508 }
509
510 void MEDFileUMeshSplitL1::synchronizeTinyInfo(const MEDFileMesh& master) const
511 {
512   const MEDCouplingUMesh *tmp=_m_by_types;
513   if(!tmp)
514     return ;
515   (const_cast<MEDCouplingUMesh *>(tmp))->setName(master.getName());
516   (const_cast<MEDCouplingUMesh *>(tmp))->setDescription(master.getDescription());
517   (const_cast<MEDCouplingUMesh *>(tmp))->setTime(master.getTimeValue(),master.getIteration(),master.getOrder());
518   (const_cast<MEDCouplingUMesh *>(tmp))->setTimeUnit(master.getTimeUnit());
519 }
520
521 void MEDFileUMeshSplitL1::clearNonDiscrAttributes() const
522 {
523   ClearNonDiscrAttributes(_m_by_types);
524 }
525
526 void MEDFileUMeshSplitL1::ClearNonDiscrAttributes(const MEDCouplingMesh *tmp)
527 {
528   if(!tmp)
529     return ;
530   (const_cast<MEDCouplingMesh *>(tmp))->setName("");
531   (const_cast<MEDCouplingMesh *>(tmp))->setDescription("");
532   (const_cast<MEDCouplingMesh *>(tmp))->setTime(0.,-1,-1);
533   (const_cast<MEDCouplingMesh *>(tmp))->setTimeUnit("");
534 }
535
536 void MEDFileUMeshSplitL1::assignMesh(MEDCouplingUMesh *m, bool newOrOld) throw(INTERP_KERNEL::Exception)
537 {
538   if(newOrOld)
539     {
540       m->incrRef();
541       _m=m;
542       _m_by_types=(MEDCouplingUMesh *)m->deepCpy();
543       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=_m_by_types->getRenumArrForConsecutiveCellTypesSpec(typmai2,typmai2+MED_N_CELL_FIXED_GEO);
544       if(!da->isIdentity())
545         {
546           _num=da->invertArrayO2N2N2O(m->getNumberOfCells());
547           _m.updateTime();
548           computeRevNum();
549           _m_by_types->renumberCells(da->getConstPointer(),false);
550         }
551     }
552   else
553     {
554       if(!m->checkConsecutiveCellTypesAndOrder(typmai2,typmai2+MED_N_CELL_FIXED_GEO))
555         throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::assignMesh : the mode of mesh setting expects to follow the MED file numbering convention ! it is not the case !");
556       m->incrRef();
557       _m_by_types=m;
558     }
559   _fam=DataArrayInt::New();
560   _fam->alloc(m->getNumberOfCells(),1);
561   _fam->fillWithValue(0);
562 }
563
564 bool MEDFileUMeshSplitL1::empty() const
565 {
566   return ((const MEDCouplingUMesh *)_m_by_types)==0;
567 }
568
569 bool MEDFileUMeshSplitL1::presenceOfOneFams(const std::vector<int>& ids) const
570 {
571   const DataArrayInt *fam=_fam;
572   if(!fam)
573     return false;
574   return fam->presenceOfValue(ids);
575 }
576
577 int MEDFileUMeshSplitL1::getMeshDimension() const
578 {
579   return _m_by_types->getMeshDimension();
580 }
581
582 void MEDFileUMeshSplitL1::simpleRepr(std::ostream& oss) const
583 {
584   std::vector<int> code=_m_by_types->getDistributionOfTypes();
585   int nbOfTypes=code.size()/3;
586   for(int i=0;i<nbOfTypes;i++)
587     {
588       INTERP_KERNEL::NormalizedCellType typ=(INTERP_KERNEL::NormalizedCellType) code[3*i];
589       oss << "    - Number of cells with type " << INTERP_KERNEL::CellModel::GetCellModel(typ).getRepr() << " : " << code[3*i+1] << std::endl;
590     }
591 }
592
593 int MEDFileUMeshSplitL1::getSize() const throw(INTERP_KERNEL::Exception)
594 {
595   if((const MEDCouplingUMesh *)_m_by_types==0)
596     throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::getSize : no mesh specified at level !");
597   return _m_by_types->getNumberOfCells();
598 }
599
600 MEDCouplingUMesh *MEDFileUMeshSplitL1::getFamilyPart(const int *idsBg, const int *idsEnd, bool renum) const
601 {
602   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> eltsToKeep=_fam->getIdsEqualList(idsBg,idsEnd);
603   MEDCouplingUMesh *m=(MEDCouplingUMesh *)_m_by_types->buildPartOfMySelf(eltsToKeep->getConstPointer(),eltsToKeep->getConstPointer()+eltsToKeep->getNumberOfTuples(),true);
604   if(renum)
605     return renumIfNeeded(m,eltsToKeep->getConstPointer());
606   return m;
607 }
608
609 DataArrayInt *MEDFileUMeshSplitL1::getFamilyPartArr(const int *idsBg, const int *idsEnd, bool renum) const
610 {
611   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=_fam->getIdsEqualList(idsBg,idsEnd);
612   if(renum)
613     return renumIfNeededArr(da);
614   da->incrRef();
615   return da;
616 }
617
618 MEDCouplingUMesh *MEDFileUMeshSplitL1::getWholeMesh(bool renum) const
619 {
620   MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> tmp;
621   if(renum)
622     tmp=_m;
623   else
624     tmp=_m_by_types;
625   tmp->incrRef();
626   return tmp;
627 }
628
629 const DataArrayInt *MEDFileUMeshSplitL1::getFamilyField() const
630 {
631   return _fam;
632 }
633
634 const DataArrayInt *MEDFileUMeshSplitL1::getNumberField() const
635 {
636   return _num;
637 }
638
639 const DataArrayInt *MEDFileUMeshSplitL1::getRevNumberField() const
640 {
641   return _rev_num;
642 }
643
644 void MEDFileUMeshSplitL1::eraseFamilyField()
645 {
646   _fam->fillWithZero();
647 }
648
649 /*!
650  * This method ignores _m and _m_by_types.
651  */
652 void MEDFileUMeshSplitL1::setGroupsFromScratch(const std::vector<const MEDCouplingUMesh *>& ms, std::map<std::string,int>& familyIds,
653                                                std::map<std::string, std::vector<std::string> >& groups) throw(INTERP_KERNEL::Exception)
654 {
655   std::vector< DataArrayInt * > corr;
656   _m=MEDCouplingUMesh::FuseUMeshesOnSameCoords(ms,0,corr);
657   std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> > corrMSafe(corr.begin(),corr.end());
658   std::vector< std::vector<int> > fidsOfGroups;
659   std::vector< const DataArrayInt * > corr2(corr.begin(),corr.end());
660   _fam=DataArrayInt::MakePartition(corr2,((MEDCouplingUMesh *)_m)->getNumberOfCells(),fidsOfGroups);
661   int nbOfCells=((MEDCouplingUMesh *)_m)->getNumberOfCells();
662   std::map<int,std::string> newfams;
663   std::map<int,int> famIdTrad;
664   TraduceFamilyNumber(fidsOfGroups,familyIds,famIdTrad,newfams);
665   int *w=_fam->getPointer();
666   for(int i=0;i<nbOfCells;i++,w++)
667     *w=famIdTrad[*w];
668 }
669
670 void MEDFileUMeshSplitL1::write(med_idt fid, const char *mName, int mdim) const
671 {
672   std::vector<MEDCouplingUMesh *> ms=_m_by_types->splitByType();
673   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> > msMSafe(ms.begin(),ms.end());
674   int start=0;
675   for(std::vector<MEDCouplingUMesh *>::const_iterator it=ms.begin();it!=ms.end();it++)
676     {
677       int nbCells=(*it)->getNumberOfCells();
678       int end=start+nbCells;
679       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> fam,num;
680       if((const DataArrayInt *)_fam)
681         fam=_fam->substr(start,end);
682       if((const DataArrayInt *)_num)
683         num=_num->substr(start,end);
684       MEDFileUMeshPerType::write(fid,mName,mdim,(*it),fam,num);
685       start=end;
686     }
687 }
688
689 void MEDFileUMeshSplitL1::changeFamilyIdArr(int oldId, int newId) throw(INTERP_KERNEL::Exception)
690 {
691   DataArrayInt *arr=_fam;
692   if(arr)
693     arr->changeValue(oldId,newId);
694 }
695
696 void MEDFileUMeshSplitL1::setFamilyArr(DataArrayInt *famArr)
697 {
698   if(!famArr)
699     {
700       _fam=0;
701       return ;
702     }
703   MEDCouplingUMesh *mbt(_m_by_types);
704   if(!mbt)
705     throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::setFamilyArr : no mesh defined on this level !");
706   famArr->checkNbOfTuplesAndComp(mbt->getNumberOfCells(),1,"MEDFileUMeshSplitL1::setFamilyArr : Problem in size of Family arr ! ");
707   famArr->incrRef();
708   _fam=famArr;
709 }
710
711 void MEDFileUMeshSplitL1::setRenumArr(DataArrayInt *renumArr)
712 {
713   if(!renumArr)
714     {
715       _num=0;
716       _rev_num=0;
717       return ;
718     }
719   MEDCouplingUMesh *mbt(_m_by_types);
720   if(!mbt)
721     throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::setRenumArr : no mesh defined on this level !");
722   renumArr->checkNbOfTuplesAndComp(mbt->getNumberOfCells(),1,"MEDFileUMeshSplitL1::setRenumArr : Problem in size of numbering arr ! ");
723   renumArr->incrRef();
724   _num=renumArr;
725   computeRevNum();
726 }
727
728 MEDCouplingUMesh *MEDFileUMeshSplitL1::Renumber2(const DataArrayInt *renum, MEDCouplingUMesh *m, const int *cellIds)
729 {
730   if(renum==0)
731     return m;
732   if(cellIds==0)
733     m->renumberCells(renum->getConstPointer(),true);
734   else
735     {
736       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> locnum=renum->selectByTupleId(cellIds,cellIds+m->getNumberOfCells());
737       m->renumberCells(locnum->getConstPointer(),true);
738     }
739   return m;
740 }
741
742 MEDCouplingUMesh *MEDFileUMeshSplitL1::renumIfNeeded(MEDCouplingUMesh *m, const int *cellIds) const
743 {
744   return Renumber2(_num,m,cellIds);
745 }
746
747 DataArrayInt *MEDFileUMeshSplitL1::Renumber(const DataArrayInt *renum, const DataArrayInt *da)
748 {
749   if((const DataArrayInt *)renum==0)
750     {
751       da->incrRef();
752       return const_cast<DataArrayInt *>(da);
753     }
754   return renum->selectByTupleId(da->getConstPointer(),da->getConstPointer()+da->getNumberOfTuples());
755 }
756
757 DataArrayInt *MEDFileUMeshSplitL1::renumIfNeededArr(const DataArrayInt *da) const
758 {
759   return Renumber(_num,da);
760 }
761
762 std::vector<int> MEDFileUMeshSplitL1::GetNewFamiliesNumber(int nb, const std::map<std::string,int>& families)
763 {
764   int id=-1;
765   for(std::map<std::string,int>::const_iterator it=families.begin();it!=families.end();it++)
766     id=std::max(id,(*it).second);
767   if(id==-1)
768     id=0;
769   std::vector<int> ret(nb);
770   for(int i=1;i<=nb;i++)
771     ret[i]=id+i;
772   return ret;
773 }
774
775 void MEDFileUMeshSplitL1::TraduceFamilyNumber(const std::vector< std::vector<int> >& fidsGrps, std::map<std::string,int>& familyIds,
776                                               std::map<int,int>& famIdTrad, std::map<int,std::string>& newfams)
777 {
778   std::set<int> allfids;
779   
780 }
781
782 void MEDFileUMeshSplitL1::computeRevNum() const
783 {
784   int pos;
785   int maxValue=_num->getMaxValue(pos);
786   _rev_num=_num->invertArrayN2O2O2N(maxValue+1);
787 }