Salome HOME
Merge last changes for 0021856: [CEA 663] Documenting API of MEDCoupling and MEDLoade...
[modules/med.git] / src / MEDLoader / MEDFileMeshLL.cxx
1 // Copyright (C) 2007-2013  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),_univ_name(MED_LNAME_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       {
85         med_grid_type gt;
86         MEDmeshGridTypeRd(fid,mname,&gt);
87         switch(gt)
88           {
89           case MED_CARTESIAN_GRID:
90             meshType=CARTESIAN;
91             break;
92           case MED_CURVILINEAR_GRID:
93             meshType=CURVE_LINEAR;
94             break;
95           default:
96             throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getMeshIdFromName : unrecognized structured mesh type ! Supported are :\n - cartesian\n - curve linear\n");
97           }
98         break;
99       }
100     default:
101       throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getMeshIdFromName : unrecognized mesh type !");
102     }
103   med_int numdt,numit;
104   med_float dtt;
105   MEDmeshComputationStepInfo(fid,mname,1,&numdt,&numit,&dtt);
106   dt=numdt; it=numit;
107   return ret;
108 }
109
110 double MEDFileMeshL2::CheckMeshTimeStep(med_idt fid, const char *mName, int nstep, int dt, int it) throw(INTERP_KERNEL::Exception)
111 {
112   bool found=false;
113   med_int numdt,numit;
114   med_float dtt;
115   std::vector< std::pair<int,int> > p(nstep);
116   for(int i=0;i<nstep;i++)
117     {
118       MEDmeshComputationStepInfo(fid,mName,i+1,&numdt,&numit,&dtt);
119       p[i]=std::make_pair<int,int>(numdt,numit);
120       found=(numdt==dt) && (numit==numit);
121     }
122   if(!found)
123     {
124       std::ostringstream oss; oss << "No such iteration=" << dt << ",order=" << it << " numbers found for mesh '" << mName << "' ! ";
125       oss << "Possibilities are : ";
126       for(int i=0;i<nstep;i++)
127         oss << "(" << p[i].first << "," << p[i].second << "), ";
128       throw INTERP_KERNEL::Exception(oss.str().c_str());
129     }
130   return dtt;
131 }
132
133 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)
134 {
135   med_mesh_type type_maillage;
136   med_int spaceDim;
137   med_sorting_type stype;
138   med_axis_type axistype;
139   int naxis=MEDmeshnAxis(fid,mId);
140   INTERP_KERNEL::AutoPtr<char> nameTmp=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
141   INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
142   INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
143   INTERP_KERNEL::AutoPtr<char> univTmp=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
144   if(MEDmeshInfo(fid,mId,nameTmp,&spaceDim,&Mdim,&type_maillage,_description.getPointer(),_dt_unit.getPointer(),
145                  &stype,&nstep,&axistype,axisname,axisunit)!=0)
146     throw INTERP_KERNEL::Exception("A problem has been detected when trying to get info on mesh !");
147   MEDmeshUniversalNameRd(fid,nameTmp,_univ_name.getPointer());
148   switch(type_maillage)
149     {
150     case MED_UNSTRUCTURED_MESH:
151       meshType=UNSTRUCTURED;
152       break;
153     case MED_STRUCTURED_MESH:
154       {
155         med_grid_type gt;
156         MEDmeshGridTypeRd(fid,mName,&gt);
157         switch(gt)
158           {
159           case MED_CARTESIAN_GRID:
160             meshType=CARTESIAN;
161             break;
162           case MED_CURVILINEAR_GRID:
163             meshType=CURVE_LINEAR;
164             break;
165           default:
166             throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getAxisInfoOnMesh : unrecognized structured mesh type ! Supported are :\n - cartesian\n - curve linear\n");
167           }
168         break;
169       }
170     default:
171       throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getMeshIdFromName : unrecognized mesh type !");
172     }
173   //
174   std::vector<std::string> infosOnComp(naxis);
175   for(int i=0;i<naxis;i++)
176     {
177       std::string info=MEDLoaderBase::buildUnionUnit(((char *)axisname)+i*MED_SNAME_SIZE,MED_SNAME_SIZE,((char *)axisunit)+i*MED_SNAME_SIZE,MED_SNAME_SIZE);
178       infosOnComp[i]=info;
179     }
180   return infosOnComp;
181 }
182
183 void MEDFileMeshL2::ReadFamiliesAndGrps(med_idt fid, const char *meshName, std::map<std::string,int>& fams, std::map<std::string, std::vector<std::string> >& grps)
184 {
185   char nomfam[MED_NAME_SIZE+1];
186   med_int numfam;
187   int nfam=MEDnFamily(fid,meshName);
188   for(int i=0;i<nfam;i++)
189     {
190       int ngro=MEDnFamilyGroup(fid,meshName,i+1);
191       med_int natt=MEDnFamily23Attribute(fid,meshName,i+1);
192       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
193       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
194       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
195       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
196       MEDfamily23Info(fid,meshName,i+1,nomfam,attide,attval,attdes,&numfam,gro);
197       std::string famName=MEDLoaderBase::buildStringFromFortran(nomfam,MED_NAME_SIZE);
198       fams[famName]=numfam;
199       for(int j=0;j<ngro;j++)
200         {
201           std::string groupname=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
202           grps[groupname].push_back(famName);
203         }
204     }
205 }
206
207 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)
208 {
209   for(std::map<std::string,int>::const_iterator it=fams.begin();it!=fams.end();it++)
210     {
211       std::vector<std::string> grpsOfFam;
212       for(std::map<std::string, std::vector<std::string> >::const_iterator it1=grps.begin();it1!=grps.end();it1++)
213         {
214           if(std::find((*it1).second.begin(),(*it1).second.end(),(*it).first)!=(*it1).second.end())
215             grpsOfFam.push_back((*it1).first);
216         }
217       int ngro=grpsOfFam.size();
218       INTERP_KERNEL::AutoPtr<char> groName=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE*ngro);
219       int i=0;
220       for(std::vector<std::string>::const_iterator it2=grpsOfFam.begin();it2!=grpsOfFam.end();it2++,i++)
221         MEDLoaderBase::safeStrCpy2((*it2).c_str(),MED_LNAME_SIZE-1,groName+i*MED_LNAME_SIZE,tooLongStrPol);
222       INTERP_KERNEL::AutoPtr<char> famName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
223       MEDLoaderBase::safeStrCpy((*it).first.c_str(),MED_NAME_SIZE,famName,tooLongStrPol);
224       int ret=MEDfamilyCr(fid,mname,famName,(*it).second,ngro,groName);
225       ret++;
226     }
227 }
228
229 MEDFileUMeshL2::MEDFileUMeshL2()
230 {
231 }
232
233 void MEDFileUMeshL2::loadAll(med_idt fid, int mId, const char *mName, int dt, int it)
234 {
235   _name.set(mName);
236   int nstep;
237   int Mdim;
238   ParaMEDMEM::MEDCouplingMeshType meshType;
239   std::vector<std::string> infosOnComp=getAxisInfoOnMesh(fid,mId,mName,meshType,nstep,Mdim);
240   if(meshType!=UNSTRUCTURED)
241     throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected an unstructured one whereas in file it is not an unstructured !");
242   _time=CheckMeshTimeStep(fid,mName,nstep,dt,it);
243   _iteration=dt;
244   _order=it;
245   loadConnectivity(fid,Mdim,mName,dt,it);//to improve check (dt,it) coherency
246   loadCoords(fid,mId,infosOnComp,mName,dt,it);
247 }
248
249 void MEDFileUMeshL2::loadConnectivity(med_idt fid, int mdim, const char *mName, int dt, int it)
250 {
251   _per_type_mesh.resize(1);
252   _per_type_mesh[0].clear();
253   for(int j=0;j<MED_N_CELL_FIXED_GEO;j++)
254     {
255       MEDFileUMeshPerType *tmp=MEDFileUMeshPerType::New(fid,mName,dt,it,mdim,typmai[j],typmai2[j]);
256       if(tmp)
257         _per_type_mesh[0].push_back(tmp);
258     }
259   sortTypes();
260 }
261
262 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)
263 {
264   int spaceDim=infosOnComp.size();
265   med_bool changement,transformation;
266   int nCoords=MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation);
267   _coords=DataArrayDouble::New();
268   _coords->alloc(nCoords,spaceDim);
269   double *coordsPtr=_coords->getPointer();
270   MEDmeshNodeCoordinateRd(fid,mName,dt,it,MED_FULL_INTERLACE,coordsPtr);
271   if(MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
272     {
273       _fam_coords=DataArrayInt::New();
274       _fam_coords->alloc(nCoords,1);
275       MEDmeshEntityFamilyNumberRd(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,_fam_coords->getPointer());
276     }
277   else
278     _fam_coords=0;
279   if(MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
280     {
281       _num_coords=DataArrayInt::New();
282       _num_coords->alloc(nCoords,1);
283       MEDmeshEntityNumberRd(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,_num_coords->getPointer());
284     }
285   else
286     _num_coords=0;
287   if(MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NAME,MED_NODAL,&changement,&transformation)>0)
288     {
289       _name_coords=DataArrayAsciiChar::New();
290       _name_coords->alloc(nCoords+1,MED_SNAME_SIZE);//not a bug to avoid the memory corruption due to last \0 at the end
291       MEDmeshEntityNameRd(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,_name_coords->getPointer());
292       _name_coords->reAlloc(nCoords);//not a bug to avoid the memory corruption due to last \0 at the end
293     }
294   else
295     _name_coords=0;
296   for(int i=0;i<spaceDim;i++)
297     _coords->setInfoOnComponent(i,infosOnComp[i].c_str());
298 }
299
300 void MEDFileUMeshL2::sortTypes()
301 {
302   std::set<int> mdims;
303   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> > tmp(_per_type_mesh[0]);
304   _per_type_mesh.clear();
305   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=tmp.begin();it!=tmp.end();it++)
306     mdims.insert((*it)->getDim());
307   if(mdims.empty())
308     return;
309   int mdim=*mdims.rbegin();
310   _per_type_mesh.resize(mdim+1);
311   for(int dim=mdim+1;dim!=0;dim--)
312     {
313       std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >& elt=_per_type_mesh[mdim+1-dim];
314       for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=tmp.begin();it!=tmp.end();it++)
315         if((*it)->getDim()==dim-1)
316           elt.push_back(*it);
317     }
318   // suppression of contiguous empty levels at the end of _per_type_mesh.
319   int nbOfUselessLev=0;
320   bool isFirst=true;
321   for(std::vector< std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> > >::reverse_iterator it2=_per_type_mesh.rbegin();it2!=_per_type_mesh.rend();it2++)
322     {
323       if((*it2).empty() && isFirst)
324         {
325           nbOfUselessLev++;
326         }
327       else
328         isFirst=false;
329     }
330   _per_type_mesh.resize(_per_type_mesh.size()-nbOfUselessLev);
331 }
332
333 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)
334 {
335   if(!coords)
336     return ;
337   MEDmeshNodeCoordinateWr(fid,mname,dt,it,time,MED_FULL_INTERLACE,coords->getNumberOfTuples(),coords->getConstPointer());
338   if(famCoords)
339     MEDmeshEntityFamilyNumberWr(fid,mname,dt,it,MED_NODE,MED_NO_GEOTYPE,famCoords->getNumberOfTuples(),famCoords->getConstPointer());
340   if(numCoords)
341     MEDmeshEntityNumberWr(fid,mname,dt,it,MED_NODE,MED_NO_GEOTYPE,numCoords->getNumberOfTuples(),numCoords->getConstPointer());
342   if(nameCoords)
343     {
344       if(nameCoords->getNumberOfComponents()!=MED_SNAME_SIZE)
345         {
346           std::ostringstream oss; oss << " MEDFileUMeshL2::WriteCoords : expected a name field on nodes with number of components set to " << MED_SNAME_SIZE;
347           oss << " ! The array has " << nameCoords->getNumberOfComponents() << " components !";
348           throw INTERP_KERNEL::Exception(oss.str().c_str());
349         }
350       MEDmeshEntityNameWr(fid,mname,dt,it,MED_NODE,MED_NO_GEOTYPE,nameCoords->getNumberOfTuples(),nameCoords->getConstPointer());
351     }
352 }
353
354 bool MEDFileUMeshL2::isFamDefinedOnLev(int levId) const
355 {
356   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
357     if((*it)->getFam()==0)
358       return false;
359   return true;
360 }
361
362 bool MEDFileUMeshL2::isNumDefinedOnLev(int levId) const
363 {
364   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
365     if((*it)->getNum()==0)
366       return false;
367   return true;
368 }
369
370 bool MEDFileUMeshL2::isNamesDefinedOnLev(int levId) const
371 {
372   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
373     if((*it)->getNames()==0)
374       return false;
375   return true;
376 }
377
378 MEDFileCMeshL2::MEDFileCMeshL2()
379 {
380 }
381
382 void MEDFileCMeshL2::loadAll(med_idt fid, int mId, const char *mName, int dt, int it) throw(INTERP_KERNEL::Exception)
383 {
384   _name.set(mName);
385   int nstep;
386   int Mdim;
387   ParaMEDMEM::MEDCouplingMeshType meshType;
388   std::vector<std::string> infosOnComp=getAxisInfoOnMesh(fid,mId,mName,meshType,nstep,Mdim);
389   if(meshType!=CARTESIAN)
390     throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected a structured one whereas in file it is not a structured !");
391   _time=CheckMeshTimeStep(fid,mName,nstep,dt,it);
392   _iteration=dt;
393   _order=it;
394   //
395   med_grid_type gridtype;
396   MEDmeshGridTypeRd(fid,mName,&gridtype);
397   if(gridtype!=MED_CARTESIAN_GRID)
398     throw INTERP_KERNEL::Exception("Invalid structured mesh ! Expected cartesian mesh type !");
399   _cmesh=MEDCouplingCMesh::New();
400   for(int i=0;i<Mdim;i++)
401     {
402       med_data_type dataTypeReq=GetDataTypeCorrespondingToSpaceId(i);
403       med_bool chgt=MED_FALSE,trsf=MED_FALSE;
404       int nbOfElt=MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NONE,dataTypeReq,MED_NO_CMODE,&chgt,&trsf);
405       MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> da=DataArrayDouble::New();
406       da->alloc(nbOfElt,1);
407       da->setInfoOnComponent(0,infosOnComp[i].c_str());
408       MEDmeshGridIndexCoordinateRd(fid,mName,dt,it,i+1,da->getPointer());
409       _cmesh->setCoordsAt(i,da);
410     }
411 }
412
413 med_data_type MEDFileCMeshL2::GetDataTypeCorrespondingToSpaceId(int id) throw(INTERP_KERNEL::Exception)
414 {
415   switch(id)
416     {
417     case 0:
418       return MED_COORDINATE_AXIS1;
419     case 1:
420       return MED_COORDINATE_AXIS2;
421     case 2:
422       return MED_COORDINATE_AXIS3;
423     default:
424       throw INTERP_KERNEL::Exception("Invalid meshdim detected in Cartesian Grid !");
425     }
426 }
427
428 MEDFileCLMeshL2::MEDFileCLMeshL2()
429 {
430 }
431
432 void MEDFileCLMeshL2::loadAll(med_idt fid, int mId, const char *mName, int dt, int it) throw(INTERP_KERNEL::Exception)
433 {
434   _name.set(mName);
435   int nstep;
436   int Mdim;
437   ParaMEDMEM::MEDCouplingMeshType meshType;
438   std::vector<std::string> infosOnComp=getAxisInfoOnMesh(fid,mId,mName,meshType,nstep,Mdim);
439   if(meshType!=CURVE_LINEAR)
440     throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected a structured one whereas in file it is not a structured !");
441   _time=CheckMeshTimeStep(fid,mName,nstep,dt,it);
442   _iteration=dt;
443   _order=it;
444   //
445   _clmesh=MEDCouplingCurveLinearMesh::New();
446   INTERP_KERNEL::AutoPtr<int> stGrid=new int[Mdim];
447   MEDmeshGridStructRd(fid,mName,dt,it,stGrid);
448   _clmesh->setNodeGridStructure(stGrid,((int *)stGrid)+Mdim);
449   med_bool chgt=MED_FALSE,trsf=MED_FALSE;
450   int nbNodes=MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&chgt,&trsf);
451   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> da=DataArrayDouble::New();
452   da->alloc(nbNodes,infosOnComp.size());
453   da->setInfoOnComponents(infosOnComp);
454   MEDmeshNodeCoordinateRd(fid,mName,dt,it,MED_FULL_INTERLACE,da->getPointer());
455   _clmesh->setCoords(da);
456 }
457
458 MEDFileUMeshPermCompute::MEDFileUMeshPermCompute(const MEDFileUMeshSplitL1* st):_st(st),_mpt_time(0),_num_time(0)
459 {
460 }
461
462 /*!
463  * Warning it returns an instance to deallocate !!!!
464  */
465 MEDFileUMeshPermCompute::operator MEDCouplingUMesh *() const
466 {
467   _st->_m_by_types->updateTime();
468   _st->_num->updateTime();
469   if((MEDCouplingUMesh *)_m==0)
470     {
471       updateTime();
472       _m=static_cast<MEDCouplingUMesh *>(_st->_m_by_types->deepCpy());
473       _m->renumberCells(_st->_num->getConstPointer(),true);
474       return _m.retn();
475     }
476   else
477     {
478       if(_mpt_time==_st->_m_by_types->getTimeOfThis() && _num_time==_st->_num->getTimeOfThis())
479         return _m.retn();
480       else
481         {
482           updateTime();
483           _m=static_cast<MEDCouplingUMesh *>(_st->_m_by_types->deepCpy());
484           _m->renumberCells(_st->_num->getConstPointer(),true);
485           return _m.retn();
486         }
487     }
488 }
489
490 void MEDFileUMeshPermCompute::operator=(MEDCouplingUMesh *m)
491 {
492   _m=m;
493 }
494
495 void MEDFileUMeshPermCompute::updateTime() const
496 {
497   _mpt_time=_st->_m_by_types->getTimeOfThis();
498   _num_time=_st->_num->getTimeOfThis();
499 }
500
501 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)
502 {
503 }
504
505 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(const MEDFileUMeshL2& l2, const char *mName, int id):_m(this)
506 {
507   const std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >& v=l2.getLev(id);
508   if(v.empty())
509     return;
510   int sz=v.size();
511   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> > msMSafe(sz);
512   std::vector<const MEDCouplingUMesh *> ms(sz);
513   for(int i=0;i<sz;i++)
514     {
515       MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> tmp=MEDCouplingUMesh::New("",v[i]->getDim());
516       MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> tmp2=l2.getCoords();
517       tmp->setCoords(tmp2);
518       tmp->setConnectivity(const_cast<DataArrayInt *>(v[i]->getNodal()),const_cast<DataArrayInt *>(v[i]->getNodalIndex()));
519       ms[i]=tmp; msMSafe[i]=tmp;
520     }
521   _m_by_types=MEDCouplingUMesh::MergeUMeshesOnSameCoords(ms);
522   _m_by_types->setName(mName);
523   if(l2.isFamDefinedOnLev(id))
524     {
525       int nbOfCells=_m_by_types->getNumberOfCells();
526       _fam=DataArrayInt::New();
527       _fam->alloc(nbOfCells,1);
528       int *w=_fam->getPointer();
529       for(int i=0;i<sz;i++)
530         w=std::copy(v[i]->getFam()->getConstPointer(),v[i]->getFam()->getConstPointer()+v[i]->getFam()->getNumberOfTuples(),w);
531     }
532   if(l2.isNumDefinedOnLev(id))
533     {
534       int nbOfCells=_m_by_types->getNumberOfCells();
535       _num=DataArrayInt::New();
536       _num->alloc(nbOfCells,1);
537       int *w=_num->getPointer();
538       for(int i=0;i<sz;i++)
539         w=std::copy(v[i]->getNum()->getConstPointer(),v[i]->getNum()->getConstPointer()+v[i]->getNum()->getNumberOfTuples(),w);
540       computeRevNum();
541     }
542   if(l2.isNamesDefinedOnLev(id))
543     {
544       int nbOfCells=_m_by_types->getNumberOfCells();
545       _names=DataArrayAsciiChar::New();
546       _names->alloc(nbOfCells,MED_SNAME_SIZE);
547       char *w=_names->getPointer();
548       for(int i=0;i<sz;i++)
549         w=std::copy(v[i]->getNames()->getConstPointer(),v[i]->getNames()->getConstPointer()+v[i]->getNames()->getNbOfElems(),w);
550     }
551 }
552
553 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCouplingUMesh *m):_m(this)
554 {
555   assignMesh(m,true);
556 }
557
558 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCouplingUMesh *m, bool newOrOld):_m(this)
559 {
560   assignMesh(m,newOrOld);
561 }
562
563 std::size_t MEDFileUMeshSplitL1::getHeapMemorySize() const
564 {
565   std::size_t ret=0;
566   if((const MEDCouplingUMesh *)_m_by_types)
567     {
568       ret+=_m_by_types->getHeapMemorySize();
569       if((const DataArrayDouble *)_m_by_types->getCoords())
570         ret-=_m_by_types->getCoords()->getHeapMemorySize();
571     }
572   if((const DataArrayInt*)_fam)
573     ret+=_fam->getHeapMemorySize();
574   if((const DataArrayInt*)_num)
575     ret+=_num->getHeapMemorySize();
576   if((const DataArrayInt*)_rev_num)
577     ret+=_rev_num->getHeapMemorySize();
578   if((const DataArrayAsciiChar*)_names)
579     ret+=_names->getHeapMemorySize();
580   return ret;
581 }
582
583 MEDFileUMeshSplitL1 *MEDFileUMeshSplitL1::deepCpy() const
584 {
585   MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshSplitL1> ret=new MEDFileUMeshSplitL1(*this);
586   if((const MEDCouplingUMesh*)_m_by_types)
587     ret->_m_by_types=static_cast<MEDCouplingUMesh*>(_m_by_types->deepCpy());
588   if((const DataArrayInt *)_fam)
589     ret->_fam=_fam->deepCpy();
590   if((const DataArrayInt *)_num)
591     ret->_num=_num->deepCpy();
592   if((const DataArrayInt *)_rev_num)
593     ret->_rev_num=_rev_num->deepCpy();
594   if((const DataArrayAsciiChar *)_names)
595     ret->_names=_names->deepCpy();
596   return ret.retn();
597 }
598
599 bool MEDFileUMeshSplitL1::isEqual(const MEDFileUMeshSplitL1 *other, double eps, std::string& what) const
600 {
601   const MEDCouplingUMesh *m1=_m_by_types;
602   const MEDCouplingUMesh *m2=other->_m_by_types;
603   if((m1==0 && m2!=0) || (m1!=0 && m2==0))
604     {
605       what="Presence of mesh in one sublevel and not in other!";
606       return false;
607     }
608   if(m1)
609     if(!m1->isEqual(m2,eps))
610       {
611         what="meshes at a sublevel are not deeply equal !";
612         return false;
613       }
614   const DataArrayInt *d1=_fam;
615   const DataArrayInt *d2=other->_fam;
616   if((d1==0 && d2!=0) || (d1!=0 && d2==0))
617     {
618       what="Presence of family arr in one sublevel and not in other!";
619       return false;
620     }
621   if(d1)
622     if(!d1->isEqual(*d2))
623       {
624         what="family arr at a sublevel are not deeply equal !";
625         return false;
626       }
627   d1=_num;
628   d2=other->_num;
629   if((d1==0 && d2!=0) || (d1!=0 && d2==0))
630     {
631       what="Presence of cell numbering arr in one sublevel and not in other!";
632       return false;
633     }
634   if(d1)
635     if(!d1->isEqual(*d2))
636       {
637         what="Numbering cell arr at a sublevel are not deeply equal !";
638         return false;
639       }
640   const DataArrayAsciiChar *e1=_names;
641   const DataArrayAsciiChar *e2=other->_names;
642   if((e1==0 && e2!=0) || (e1!=0 && e2==0))
643     {
644       what="Presence of cell names arr in one sublevel and not in other!";
645       return false;
646     }
647   if(e1)
648     if(!e1->isEqual(*e2))
649       {
650         what="Name cell arr at a sublevel are not deeply equal !";
651         return false;
652       }
653   return true;
654 }
655
656 void MEDFileUMeshSplitL1::synchronizeTinyInfo(const MEDFileMesh& master) const
657 {
658   const MEDCouplingUMesh *tmp=_m_by_types;
659   if(!tmp)
660     return ;
661   (const_cast<MEDCouplingUMesh *>(tmp))->setName(master.getName());
662   (const_cast<MEDCouplingUMesh *>(tmp))->setDescription(master.getDescription());
663   (const_cast<MEDCouplingUMesh *>(tmp))->setTime(master.getTimeValue(),master.getIteration(),master.getOrder());
664   (const_cast<MEDCouplingUMesh *>(tmp))->setTimeUnit(master.getTimeUnit());
665 }
666
667 void MEDFileUMeshSplitL1::clearNonDiscrAttributes() const
668 {
669   ClearNonDiscrAttributes(_m_by_types);
670 }
671
672 void MEDFileUMeshSplitL1::ClearNonDiscrAttributes(const MEDCouplingMesh *tmp)
673 {
674   if(!tmp)
675     return ;
676   (const_cast<MEDCouplingMesh *>(tmp))->setName("");
677   (const_cast<MEDCouplingMesh *>(tmp))->setDescription("");
678   (const_cast<MEDCouplingMesh *>(tmp))->setTime(0.,-1,-1);
679   (const_cast<MEDCouplingMesh *>(tmp))->setTimeUnit("");
680 }
681
682 void MEDFileUMeshSplitL1::assignMesh(MEDCouplingUMesh *m, bool newOrOld) throw(INTERP_KERNEL::Exception)
683 {
684   if(newOrOld)
685     {
686       m->incrRef();
687       _m=m;
688       _m_by_types=(MEDCouplingUMesh *)m->deepCpy();
689       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=_m_by_types->getRenumArrForConsecutiveCellTypesSpec(typmai2,typmai2+MED_N_CELL_FIXED_GEO);
690       if(!da->isIdentity())
691         {
692           _num=da->invertArrayO2N2N2O(m->getNumberOfCells());
693           _m.updateTime();
694           computeRevNum();
695           _m_by_types->renumberCells(da->getConstPointer(),false);
696         }
697     }
698   else
699     {
700       if(!m->checkConsecutiveCellTypesAndOrder(typmai2,typmai2+MED_N_CELL_FIXED_GEO))
701         throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::assignMesh : the mode of mesh setting expects to follow the MED file numbering convention ! it is not the case !");
702       m->incrRef();
703       _m_by_types=m;
704     }
705   _fam=DataArrayInt::New();
706   _fam->alloc(m->getNumberOfCells(),1);
707   _fam->fillWithValue(0);
708 }
709
710 bool MEDFileUMeshSplitL1::empty() const
711 {
712   return ((const MEDCouplingUMesh *)_m_by_types)==0;
713 }
714
715 bool MEDFileUMeshSplitL1::presenceOfOneFams(const std::vector<int>& ids) const
716 {
717   const DataArrayInt *fam=_fam;
718   if(!fam)
719     return false;
720   return fam->presenceOfValue(ids);
721 }
722
723 int MEDFileUMeshSplitL1::getMeshDimension() const
724 {
725   return _m_by_types->getMeshDimension();
726 }
727
728 void MEDFileUMeshSplitL1::simpleRepr(std::ostream& oss) const
729 {
730   std::vector<int> code=_m_by_types->getDistributionOfTypes();
731   int nbOfTypes=code.size()/3;
732   for(int i=0;i<nbOfTypes;i++)
733     {
734       INTERP_KERNEL::NormalizedCellType typ=(INTERP_KERNEL::NormalizedCellType) code[3*i];
735       oss << "    - Number of cells with type " << INTERP_KERNEL::CellModel::GetCellModel(typ).getRepr() << " : " << code[3*i+1] << std::endl;
736     }
737 }
738
739 int MEDFileUMeshSplitL1::getSize() const throw(INTERP_KERNEL::Exception)
740 {
741   if((const MEDCouplingUMesh *)_m_by_types==0)
742     throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::getSize : no mesh specified at level !");
743   return _m_by_types->getNumberOfCells();
744 }
745
746 MEDCouplingUMesh *MEDFileUMeshSplitL1::getFamilyPart(const int *idsBg, const int *idsEnd, bool renum) const
747 {
748   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> eltsToKeep=_fam->getIdsEqualList(idsBg,idsEnd);
749   MEDCouplingUMesh *m=(MEDCouplingUMesh *)_m_by_types->buildPartOfMySelf(eltsToKeep->getConstPointer(),eltsToKeep->getConstPointer()+eltsToKeep->getNumberOfTuples(),true);
750   if(renum)
751     return renumIfNeeded(m,eltsToKeep->getConstPointer());
752   return m;
753 }
754
755 DataArrayInt *MEDFileUMeshSplitL1::getFamilyPartArr(const int *idsBg, const int *idsEnd, bool renum) const
756 {
757   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=_fam->getIdsEqualList(idsBg,idsEnd);
758   if(renum)
759     return renumIfNeededArr(da);
760   return da.retn();
761 }
762
763 MEDCouplingUMesh *MEDFileUMeshSplitL1::getWholeMesh(bool renum) const
764 {
765   MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> tmp;
766   if(renum)
767     tmp=_m;
768   else
769     tmp=_m_by_types;
770   return tmp.retn();
771 }
772
773 DataArrayInt *MEDFileUMeshSplitL1::getOrCreateAndGetFamilyField() throw(INTERP_KERNEL::Exception)
774 {
775   if((DataArrayInt *)_fam)
776     return _fam;
777   MEDCouplingUMesh *m(_m_by_types);
778   if(!m)
779     throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::getOrCreateAndGetFamilyField : impossible to create a family field array because no mesh specified on this level !");
780   int nbOfTuples=m->getNumberOfCells();
781   _fam=DataArrayInt::New(); _fam->alloc(nbOfTuples,1); _fam->fillWithZero();
782   return _fam;
783 }
784
785 const DataArrayInt *MEDFileUMeshSplitL1::getFamilyField() const
786 {
787   return _fam;
788 }
789
790 const DataArrayInt *MEDFileUMeshSplitL1::getNumberField() const
791 {
792   return _num;
793 }
794
795 const DataArrayInt *MEDFileUMeshSplitL1::getRevNumberField() const
796 {
797   return _rev_num;
798 }
799
800 const DataArrayAsciiChar *MEDFileUMeshSplitL1::getNameField() const
801 {
802   return _names;
803 }
804
805 void MEDFileUMeshSplitL1::eraseFamilyField()
806 {
807   _fam->fillWithZero();
808 }
809
810 /*!
811  * This method ignores _m and _m_by_types.
812  */
813 void MEDFileUMeshSplitL1::setGroupsFromScratch(const std::vector<const MEDCouplingUMesh *>& ms, std::map<std::string,int>& familyIds,
814                                                std::map<std::string, std::vector<std::string> >& groups) throw(INTERP_KERNEL::Exception)
815 {
816   std::vector< DataArrayInt * > corr;
817   _m=MEDCouplingUMesh::FuseUMeshesOnSameCoords(ms,0,corr);
818   std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> > corrMSafe(corr.begin(),corr.end());
819   std::vector< std::vector<int> > fidsOfGroups;
820   std::vector< const DataArrayInt * > corr2(corr.begin(),corr.end());
821   _fam=DataArrayInt::MakePartition(corr2,((MEDCouplingUMesh *)_m)->getNumberOfCells(),fidsOfGroups);
822   int nbOfCells=((MEDCouplingUMesh *)_m)->getNumberOfCells();
823   std::map<int,std::string> newfams;
824   std::map<int,int> famIdTrad;
825   TraduceFamilyNumber(fidsOfGroups,familyIds,famIdTrad,newfams);
826   int *w=_fam->getPointer();
827   for(int i=0;i<nbOfCells;i++,w++)
828     *w=famIdTrad[*w];
829 }
830
831 void MEDFileUMeshSplitL1::write(med_idt fid, const char *mName, int mdim) const
832 {
833   std::vector<MEDCouplingUMesh *> ms=_m_by_types->splitByType();
834   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> > msMSafe(ms.begin(),ms.end());
835   int start=0;
836   for(std::vector<MEDCouplingUMesh *>::const_iterator it=ms.begin();it!=ms.end();it++)
837     {
838       int nbCells=(*it)->getNumberOfCells();
839       int end=start+nbCells;
840       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> fam,num;
841       MEDCouplingAutoRefCountObjectPtr<DataArrayAsciiChar> names;
842       if((const DataArrayInt *)_fam)
843         fam=_fam->substr(start,end);
844       if((const DataArrayInt *)_num)
845         num=_num->substr(start,end);
846       if((const DataArrayAsciiChar *)_names)
847         names=static_cast<DataArrayAsciiChar *>(_names->substr(start,end));
848       MEDFileUMeshPerType::write(fid,mName,mdim,(*it),fam,num,names);
849       start=end;
850     }
851 }
852
853 void MEDFileUMeshSplitL1::renumberNodesInConn(const int *newNodeNumbersO2N) throw(INTERP_KERNEL::Exception)
854 {
855   MEDCouplingUMesh *m(_m_by_types);
856   if(!m)
857     return;
858   m->renumberNodesInConn(newNodeNumbersO2N);
859 }
860
861 void MEDFileUMeshSplitL1::changeFamilyIdArr(int oldId, int newId) throw(INTERP_KERNEL::Exception)
862 {
863   DataArrayInt *arr=_fam;
864   if(arr)
865     arr->changeValue(oldId,newId);
866 }
867
868 void MEDFileUMeshSplitL1::setFamilyArr(DataArrayInt *famArr)
869 {
870   if(!famArr)
871     {
872       _fam=0;
873       return ;
874     }
875   MEDCouplingUMesh *mbt(_m_by_types);
876   if(!mbt)
877     throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::setFamilyArr : no mesh defined on this level !");
878   famArr->checkNbOfTuplesAndComp(mbt->getNumberOfCells(),1,"MEDFileUMeshSplitL1::setFamilyArr : Problem in size of Family arr ! ");
879   famArr->incrRef();
880   _fam=famArr;
881 }
882
883 void MEDFileUMeshSplitL1::setRenumArr(DataArrayInt *renumArr)
884 {
885   if(!renumArr)
886     {
887       _num=0;
888       _rev_num=0;
889       return ;
890     }
891   MEDCouplingUMesh *mbt(_m_by_types);
892   if(!mbt)
893     throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::setRenumArr : no mesh defined on this level !");
894   renumArr->checkNbOfTuplesAndComp(mbt->getNumberOfCells(),1,"MEDFileUMeshSplitL1::setRenumArr : Problem in size of numbering arr ! ");
895   renumArr->incrRef();
896   _num=renumArr;
897   computeRevNum();
898 }
899
900 void MEDFileUMeshSplitL1::setNameArr(DataArrayAsciiChar *nameArr)
901 {
902   if(!nameArr)
903     {
904       _names=0;
905       return ;
906     }
907   MEDCouplingUMesh *mbt(_m_by_types);
908   if(!mbt)
909     throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::setNameArr : no mesh defined on this level !");
910   nameArr->checkNbOfTuplesAndComp(mbt->getNumberOfCells(),MED_SNAME_SIZE,"MEDFileUMeshSplitL1::setNameArr : Problem in size of name arr ! ");
911   nameArr->incrRef();
912   _names=nameArr;
913 }
914
915 MEDCouplingUMesh *MEDFileUMeshSplitL1::Renumber2(const DataArrayInt *renum, MEDCouplingUMesh *m, const int *cellIds)
916 {
917   if(renum==0)
918     return m;
919   if(cellIds==0)
920     m->renumberCells(renum->getConstPointer(),true);
921   else
922     {
923       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> locnum=renum->selectByTupleId(cellIds,cellIds+m->getNumberOfCells());
924       m->renumberCells(locnum->getConstPointer(),true);
925     }
926   return m;
927 }
928
929 MEDCouplingUMesh *MEDFileUMeshSplitL1::renumIfNeeded(MEDCouplingUMesh *m, const int *cellIds) const
930 {
931   return Renumber2(_num,m,cellIds);
932 }
933
934 DataArrayInt *MEDFileUMeshSplitL1::Renumber(const DataArrayInt *renum, const DataArrayInt *da)
935 {
936   if((const DataArrayInt *)renum==0)
937     {
938       da->incrRef();
939       return const_cast<DataArrayInt *>(da);
940     }
941   return renum->selectByTupleId(da->getConstPointer(),da->getConstPointer()+da->getNumberOfTuples());
942 }
943
944 DataArrayInt *MEDFileUMeshSplitL1::renumIfNeededArr(const DataArrayInt *da) const
945 {
946   return Renumber(_num,da);
947 }
948
949 std::vector<int> MEDFileUMeshSplitL1::GetNewFamiliesNumber(int nb, const std::map<std::string,int>& families)
950 {
951   int id=-1;
952   for(std::map<std::string,int>::const_iterator it=families.begin();it!=families.end();it++)
953     id=std::max(id,(*it).second);
954   if(id==-1)
955     id=0;
956   std::vector<int> ret(nb);
957   for(int i=1;i<=nb;i++)
958     ret[i]=id+i;
959   return ret;
960 }
961
962 void MEDFileUMeshSplitL1::TraduceFamilyNumber(const std::vector< std::vector<int> >& fidsGrps, std::map<std::string,int>& familyIds,
963                                               std::map<int,int>& famIdTrad, std::map<int,std::string>& newfams)
964 {
965   std::set<int> allfids;
966   
967 }
968
969 void MEDFileUMeshSplitL1::computeRevNum() const
970 {
971   int pos;
972   int maxValue=_num->getMaxValue(pos);
973   _rev_num=_num->invertArrayN2O2O2N(maxValue+1);
974 }