Salome HOME
MED file mesh loading on demand.
[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 #include "MEDFileMeshReadSelector.hxx"
25
26 #include "MEDCouplingUMesh.hxx"
27
28 #include "InterpKernelAutoPtr.hxx"
29 #include "CellModel.hxx"
30
31 #include <set>
32
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];
36
37 using namespace ParaMEDMEM;
38
39 MEDFileMeshL2::MEDFileMeshL2():_name(MED_NAME_SIZE),_description(MED_COMMENT_SIZE),_univ_name(MED_LNAME_SIZE),_dt_unit(MED_LNAME_SIZE)
40 {
41 }
42
43 int MEDFileMeshL2::GetMeshIdFromName(med_idt fid, const char *mname, ParaMEDMEM::MEDCouplingMeshType& meshType, int& dt, int& it, std::string& dtunit1) throw(INTERP_KERNEL::Exception)
44 {
45   med_mesh_type type_maillage;
46   char maillage_description[MED_COMMENT_SIZE+1];
47   char dtunit[MED_LNAME_SIZE+1];
48   med_int spaceDim,dim;
49   char nommaa[MED_NAME_SIZE+1];
50   med_int n=MEDnMesh(fid);
51   bool found=false;
52   int ret=-1;
53   med_sorting_type stype;
54   std::vector<std::string> ms;
55   int nstep;
56   med_axis_type axistype;
57   for(int i=0;i<n && !found;i++)
58     {
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));
65       ms.push_back(cur);
66       if(cur==mname)
67         {
68           found=true;
69           ret=i+1;
70         }
71     }
72   if(!found)
73     {
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());
78     }
79   switch(type_maillage)
80     {
81     case MED_UNSTRUCTURED_MESH:
82       meshType=UNSTRUCTURED;
83       break;
84     case MED_STRUCTURED_MESH:
85       {
86         med_grid_type gt;
87         MEDmeshGridTypeRd(fid,mname,&gt);
88         switch(gt)
89           {
90           case MED_CARTESIAN_GRID:
91             meshType=CARTESIAN;
92             break;
93           case MED_CURVILINEAR_GRID:
94             meshType=CURVE_LINEAR;
95             break;
96           default:
97             throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getMeshIdFromName : unrecognized structured mesh type ! Supported are :\n - cartesian\n - curve linear\n");
98           }
99         break;
100       }
101     default:
102       throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getMeshIdFromName : unrecognized mesh type !");
103     }
104   med_int numdt,numit;
105   med_float dtt;
106   MEDmeshComputationStepInfo(fid,mname,1,&numdt,&numit,&dtt);
107   dt=numdt; it=numit;
108   return ret;
109 }
110
111 double MEDFileMeshL2::CheckMeshTimeStep(med_idt fid, const char *mName, int nstep, int dt, int it) throw(INTERP_KERNEL::Exception)
112 {
113   bool found=false;
114   med_int numdt,numit;
115   med_float dtt;
116   std::vector< std::pair<int,int> > p(nstep);
117   for(int i=0;i<nstep;i++)
118     {
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);
122     }
123   if(!found)
124     {
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());
130     }
131   return dtt;
132 }
133
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)
135 {
136   med_mesh_type type_maillage;
137   med_int spaceDim;
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)
150     {
151     case MED_UNSTRUCTURED_MESH:
152       meshType=UNSTRUCTURED;
153       break;
154     case MED_STRUCTURED_MESH:
155       {
156         med_grid_type gt;
157         MEDmeshGridTypeRd(fid,mName,&gt);
158         switch(gt)
159           {
160           case MED_CARTESIAN_GRID:
161             meshType=CARTESIAN;
162             break;
163           case MED_CURVILINEAR_GRID:
164             meshType=CURVE_LINEAR;
165             break;
166           default:
167             throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getAxisInfoOnMesh : unrecognized structured mesh type ! Supported are :\n - cartesian\n - curve linear\n");
168           }
169         break;
170       }
171     default:
172       throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getMeshIdFromName : unrecognized mesh type !");
173     }
174   //
175   std::vector<std::string> infosOnComp(naxis);
176   for(int i=0;i<naxis;i++)
177     {
178       std::string info=MEDLoaderBase::buildUnionUnit(((char *)axisname)+i*MED_SNAME_SIZE,MED_SNAME_SIZE,((char *)axisunit)+i*MED_SNAME_SIZE,MED_SNAME_SIZE);
179       infosOnComp[i]=info;
180     }
181   return infosOnComp;
182 }
183
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)
185 {
186   if(mrs && !(mrs->isCellFamilyFieldReading() || mrs->isNodeFamilyFieldReading()))
187     return ;
188   char nomfam[MED_NAME_SIZE+1];
189   med_int numfam;
190   int nfam=MEDnFamily(fid,meshName);
191   for(int i=0;i<nfam;i++)
192     {
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++)
203         {
204           std::string groupname=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
205           grps[groupname].push_back(famName);
206         }
207     }
208 }
209
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)
211 {
212   for(std::map<std::string,int>::const_iterator it=fams.begin();it!=fams.end();it++)
213     {
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++)
216         {
217           if(std::find((*it1).second.begin(),(*it1).second.end(),(*it).first)!=(*it1).second.end())
218             grpsOfFam.push_back((*it1).first);
219         }
220       int ngro=grpsOfFam.size();
221       INTERP_KERNEL::AutoPtr<char> groName=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE*ngro);
222       int i=0;
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);
228       ret++;
229     }
230 }
231
232 MEDFileUMeshL2::MEDFileUMeshL2()
233 {
234 }
235
236 void MEDFileUMeshL2::loadAll(med_idt fid, int mId, const char *mName, int dt, int it, MEDFileMeshReadSelector *mrs)
237 {
238   _name.set(mName);
239   int nstep;
240   int Mdim;
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);
246   _iteration=dt;
247   _order=it;
248   loadConnectivity(fid,Mdim,mName,dt,it,mrs);//to improve check (dt,it) coherency
249   loadCoords(fid,mId,infosOnComp,mName,dt,it);
250 }
251
252 void MEDFileUMeshL2::loadConnectivity(med_idt fid, int mdim, const char *mName, int dt, int it, MEDFileMeshReadSelector *mrs)
253 {
254   _per_type_mesh.resize(1);
255   _per_type_mesh[0].clear();
256   for(int j=0;j<MED_N_CELL_FIXED_GEO;j++)
257     {
258       MEDFileUMeshPerType *tmp=MEDFileUMeshPerType::New(fid,mName,dt,it,mdim,typmai[j],typmai2[j],mrs);
259       if(tmp)
260         _per_type_mesh[0].push_back(tmp);
261     }
262   sortTypes();
263 }
264
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)
266 {
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)
275     {
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());
279     }
280   else
281     _fam_coords=0;
282   if(MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
283     {
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());
287     }
288   else
289     _num_coords=0;
290   if(MEDmeshnEntity(fid,mName,dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NAME,MED_NODAL,&changement,&transformation)>0)
291     {
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
296     }
297   else
298     _name_coords=0;
299   for(int i=0;i<spaceDim;i++)
300     _coords->setInfoOnComponent(i,infosOnComp[i].c_str());
301 }
302
303 void MEDFileUMeshL2::sortTypes()
304 {
305   std::set<int> mdims;
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());
310   if(mdims.empty())
311     return;
312   int mdim=*mdims.rbegin();
313   _per_type_mesh.resize(mdim+1);
314   for(int dim=mdim+1;dim!=0;dim--)
315     {
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)
319           elt.push_back(*it);
320     }
321   // suppression of contiguous empty levels at the end of _per_type_mesh.
322   int nbOfUselessLev=0;
323   bool isFirst=true;
324   for(std::vector< std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> > >::reverse_iterator it2=_per_type_mesh.rbegin();it2!=_per_type_mesh.rend();it2++)
325     {
326       if((*it2).empty() && isFirst)
327         {
328           nbOfUselessLev++;
329         }
330       else
331         isFirst=false;
332     }
333   _per_type_mesh.resize(_per_type_mesh.size()-nbOfUselessLev);
334 }
335
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)
337 {
338   if(!coords)
339     return ;
340   MEDmeshNodeCoordinateWr(fid,mname,dt,it,time,MED_FULL_INTERLACE,coords->getNumberOfTuples(),coords->getConstPointer());
341   if(famCoords)
342     MEDmeshEntityFamilyNumberWr(fid,mname,dt,it,MED_NODE,MED_NO_GEOTYPE,famCoords->getNumberOfTuples(),famCoords->getConstPointer());
343   if(numCoords)
344     MEDmeshEntityNumberWr(fid,mname,dt,it,MED_NODE,MED_NO_GEOTYPE,numCoords->getNumberOfTuples(),numCoords->getConstPointer());
345   if(nameCoords)
346     {
347       if(nameCoords->getNumberOfComponents()!=MED_SNAME_SIZE)
348         {
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());
352         }
353       MEDmeshEntityNameWr(fid,mname,dt,it,MED_NODE,MED_NO_GEOTYPE,nameCoords->getNumberOfTuples(),nameCoords->getConstPointer());
354     }
355 }
356
357 bool MEDFileUMeshL2::isFamDefinedOnLev(int levId) const
358 {
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)
361       return false;
362   return true;
363 }
364
365 bool MEDFileUMeshL2::isNumDefinedOnLev(int levId) const
366 {
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)
369       return false;
370   return true;
371 }
372
373 bool MEDFileUMeshL2::isNamesDefinedOnLev(int levId) const
374 {
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)
377       return false;
378   return true;
379 }
380
381 MEDFileCMeshL2::MEDFileCMeshL2()
382 {
383 }
384
385 void MEDFileCMeshL2::loadAll(med_idt fid, int mId, const char *mName, int dt, int it) throw(INTERP_KERNEL::Exception)
386 {
387   _name.set(mName);
388   int nstep;
389   int Mdim;
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);
395   _iteration=dt;
396   _order=it;
397   //
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++)
404     {
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);
413     }
414 }
415
416 med_data_type MEDFileCMeshL2::GetDataTypeCorrespondingToSpaceId(int id) throw(INTERP_KERNEL::Exception)
417 {
418   switch(id)
419     {
420     case 0:
421       return MED_COORDINATE_AXIS1;
422     case 1:
423       return MED_COORDINATE_AXIS2;
424     case 2:
425       return MED_COORDINATE_AXIS3;
426     default:
427       throw INTERP_KERNEL::Exception("Invalid meshdim detected in Cartesian Grid !");
428     }
429 }
430
431 MEDFileCLMeshL2::MEDFileCLMeshL2()
432 {
433 }
434
435 void MEDFileCLMeshL2::loadAll(med_idt fid, int mId, const char *mName, int dt, int it) throw(INTERP_KERNEL::Exception)
436 {
437   _name.set(mName);
438   int nstep;
439   int Mdim;
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);
445   _iteration=dt;
446   _order=it;
447   //
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);
459 }
460
461 MEDFileUMeshPermCompute::MEDFileUMeshPermCompute(const MEDFileUMeshSplitL1* st):_st(st),_mpt_time(0),_num_time(0)
462 {
463 }
464
465 /*!
466  * Warning it returns an instance to deallocate !!!!
467  */
468 MEDFileUMeshPermCompute::operator MEDCouplingUMesh *() const
469 {
470   _st->_m_by_types->updateTime();
471   _st->_num->updateTime();
472   if((MEDCouplingUMesh *)_m==0)
473     {
474       updateTime();
475       _m=static_cast<MEDCouplingUMesh *>(_st->_m_by_types->deepCpy());
476       _m->renumberCells(_st->_num->getConstPointer(),true);
477       return _m.retn();
478     }
479   else
480     {
481       if(_mpt_time==_st->_m_by_types->getTimeOfThis() && _num_time==_st->_num->getTimeOfThis())
482         return _m.retn();
483       else
484         {
485           updateTime();
486           _m=static_cast<MEDCouplingUMesh *>(_st->_m_by_types->deepCpy());
487           _m->renumberCells(_st->_num->getConstPointer(),true);
488           return _m.retn();
489         }
490     }
491 }
492
493 void MEDFileUMeshPermCompute::operator=(MEDCouplingUMesh *m)
494 {
495   _m=m;
496 }
497
498 void MEDFileUMeshPermCompute::updateTime() const
499 {
500   _mpt_time=_st->_m_by_types->getTimeOfThis();
501   _num_time=_st->_num->getTimeOfThis();
502 }
503
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)
505 {
506 }
507
508 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(const MEDFileUMeshL2& l2, const char *mName, int id):_m(this)
509 {
510   const std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >& v=l2.getLev(id);
511   if(v.empty())
512     return;
513   int sz=v.size();
514   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> > msMSafe(sz);
515   std::vector<const MEDCouplingUMesh *> ms(sz);
516   for(int i=0;i<sz;i++)
517     {
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;
523     }
524   _m_by_types=MEDCouplingUMesh::MergeUMeshesOnSameCoords(ms);
525   _m_by_types->setName(mName);
526   if(l2.isFamDefinedOnLev(id))
527     {
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);
534     }
535   if(l2.isNumDefinedOnLev(id))
536     {
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);
543       computeRevNum();
544     }
545   if(l2.isNamesDefinedOnLev(id))
546     {
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);
553     }
554 }
555
556 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCouplingUMesh *m):_m(this)
557 {
558   assignMesh(m,true);
559 }
560
561 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCouplingUMesh *m, bool newOrOld):_m(this)
562 {
563   assignMesh(m,newOrOld);
564 }
565
566 std::size_t MEDFileUMeshSplitL1::getHeapMemorySize() const
567 {
568   std::size_t ret=0;
569   if((const MEDCouplingUMesh *)_m_by_types)
570     {
571       ret+=_m_by_types->getHeapMemorySize();
572       if((const DataArrayDouble *)_m_by_types->getCoords())
573         ret-=_m_by_types->getCoords()->getHeapMemorySize();
574     }
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();
583   return ret;
584 }
585
586 MEDFileUMeshSplitL1 *MEDFileUMeshSplitL1::deepCpy() const
587 {
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();
599   return ret.retn();
600 }
601
602 bool MEDFileUMeshSplitL1::isEqual(const MEDFileUMeshSplitL1 *other, double eps, std::string& what) const
603 {
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))
607     {
608       what="Presence of mesh in one sublevel and not in other!";
609       return false;
610     }
611   if(m1)
612     if(!m1->isEqual(m2,eps))
613       {
614         what="meshes at a sublevel are not deeply equal !";
615         return false;
616       }
617   const DataArrayInt *d1=_fam;
618   const DataArrayInt *d2=other->_fam;
619   if((d1==0 && d2!=0) || (d1!=0 && d2==0))
620     {
621       what="Presence of family arr in one sublevel and not in other!";
622       return false;
623     }
624   if(d1)
625     if(!d1->isEqual(*d2))
626       {
627         what="family arr at a sublevel are not deeply equal !";
628         return false;
629       }
630   d1=_num;
631   d2=other->_num;
632   if((d1==0 && d2!=0) || (d1!=0 && d2==0))
633     {
634       what="Presence of cell numbering arr in one sublevel and not in other!";
635       return false;
636     }
637   if(d1)
638     if(!d1->isEqual(*d2))
639       {
640         what="Numbering cell arr at a sublevel are not deeply equal !";
641         return false;
642       }
643   const DataArrayAsciiChar *e1=_names;
644   const DataArrayAsciiChar *e2=other->_names;
645   if((e1==0 && e2!=0) || (e1!=0 && e2==0))
646     {
647       what="Presence of cell names arr in one sublevel and not in other!";
648       return false;
649     }
650   if(e1)
651     if(!e1->isEqual(*e2))
652       {
653         what="Name cell arr at a sublevel are not deeply equal !";
654         return false;
655       }
656   return true;
657 }
658
659 void MEDFileUMeshSplitL1::synchronizeTinyInfo(const MEDFileMesh& master) const
660 {
661   const MEDCouplingUMesh *tmp=_m_by_types;
662   if(!tmp)
663     return ;
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());
668 }
669
670 void MEDFileUMeshSplitL1::clearNonDiscrAttributes() const
671 {
672   ClearNonDiscrAttributes(_m_by_types);
673 }
674
675 void MEDFileUMeshSplitL1::ClearNonDiscrAttributes(const MEDCouplingMesh *tmp)
676 {
677   if(!tmp)
678     return ;
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("");
683 }
684
685 void MEDFileUMeshSplitL1::assignMesh(MEDCouplingUMesh *m, bool newOrOld) throw(INTERP_KERNEL::Exception)
686 {
687   if(newOrOld)
688     {
689       m->incrRef();
690       _m=m;
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())
694         {
695           _num=da->invertArrayO2N2N2O(m->getNumberOfCells());
696           _m.updateTime();
697           computeRevNum();
698           _m_by_types->renumberCells(da->getConstPointer(),false);
699         }
700     }
701   else
702     {
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 !");
705       m->incrRef();
706       _m_by_types=m;
707     }
708   _fam=DataArrayInt::New();
709   _fam->alloc(m->getNumberOfCells(),1);
710   _fam->fillWithValue(0);
711 }
712
713 bool MEDFileUMeshSplitL1::empty() const
714 {
715   return ((const MEDCouplingUMesh *)_m_by_types)==0;
716 }
717
718 bool MEDFileUMeshSplitL1::presenceOfOneFams(const std::vector<int>& ids) const
719 {
720   const DataArrayInt *fam=_fam;
721   if(!fam)
722     return false;
723   return fam->presenceOfValue(ids);
724 }
725
726 int MEDFileUMeshSplitL1::getMeshDimension() const
727 {
728   return _m_by_types->getMeshDimension();
729 }
730
731 void MEDFileUMeshSplitL1::simpleRepr(std::ostream& oss) const
732 {
733   std::vector<int> code=_m_by_types->getDistributionOfTypes();
734   int nbOfTypes=code.size()/3;
735   for(int i=0;i<nbOfTypes;i++)
736     {
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;
739     }
740 }
741
742 int MEDFileUMeshSplitL1::getSize() const throw(INTERP_KERNEL::Exception)
743 {
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();
747 }
748
749 MEDCouplingUMesh *MEDFileUMeshSplitL1::getFamilyPart(const int *idsBg, const int *idsEnd, bool renum) const
750 {
751   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> eltsToKeep=_fam->getIdsEqualList(idsBg,idsEnd);
752   MEDCouplingUMesh *m=(MEDCouplingUMesh *)_m_by_types->buildPartOfMySelf(eltsToKeep->getConstPointer(),eltsToKeep->getConstPointer()+eltsToKeep->getNumberOfTuples(),true);
753   if(renum)
754     return renumIfNeeded(m,eltsToKeep->getConstPointer());
755   return m;
756 }
757
758 DataArrayInt *MEDFileUMeshSplitL1::getFamilyPartArr(const int *idsBg, const int *idsEnd, bool renum) const
759 {
760   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=_fam->getIdsEqualList(idsBg,idsEnd);
761   if(renum)
762     return renumIfNeededArr(da);
763   return da.retn();
764 }
765
766 MEDCouplingUMesh *MEDFileUMeshSplitL1::getWholeMesh(bool renum) const
767 {
768   MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> tmp;
769   if(renum && ((const DataArrayInt *)_num))
770     tmp=_m;
771   else
772     tmp=_m_by_types;
773   return tmp.retn();
774 }
775
776 DataArrayInt *MEDFileUMeshSplitL1::getOrCreateAndGetFamilyField() throw(INTERP_KERNEL::Exception)
777 {
778   if((DataArrayInt *)_fam)
779     return _fam;
780   MEDCouplingUMesh *m(_m_by_types);
781   if(!m)
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();
785   return _fam;
786 }
787
788 const DataArrayInt *MEDFileUMeshSplitL1::getFamilyField() const
789 {
790   return _fam;
791 }
792
793 const DataArrayInt *MEDFileUMeshSplitL1::getNumberField() const
794 {
795   return _num;
796 }
797
798 const DataArrayInt *MEDFileUMeshSplitL1::getRevNumberField() const
799 {
800   return _rev_num;
801 }
802
803 const DataArrayAsciiChar *MEDFileUMeshSplitL1::getNameField() const
804 {
805   return _names;
806 }
807
808 void MEDFileUMeshSplitL1::eraseFamilyField()
809 {
810   _fam->fillWithZero();
811 }
812
813 /*!
814  * This method ignores _m and _m_by_types.
815  */
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)
818 {
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++)
831     *w=famIdTrad[*w];
832 }
833
834 void MEDFileUMeshSplitL1::write(med_idt fid, const char *mName, int mdim) const
835 {
836   std::vector<MEDCouplingUMesh *> ms=_m_by_types->splitByType();
837   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> > msMSafe(ms.begin(),ms.end());
838   int start=0;
839   for(std::vector<MEDCouplingUMesh *>::const_iterator it=ms.begin();it!=ms.end();it++)
840     {
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);
852       start=end;
853     }
854 }
855
856 void MEDFileUMeshSplitL1::renumberNodesInConn(const int *newNodeNumbersO2N) throw(INTERP_KERNEL::Exception)
857 {
858   MEDCouplingUMesh *m(_m_by_types);
859   if(!m)
860     return;
861   m->renumberNodesInConn(newNodeNumbersO2N);
862 }
863
864 void MEDFileUMeshSplitL1::changeFamilyIdArr(int oldId, int newId) throw(INTERP_KERNEL::Exception)
865 {
866   DataArrayInt *arr=_fam;
867   if(arr)
868     arr->changeValue(oldId,newId);
869 }
870
871 void MEDFileUMeshSplitL1::setFamilyArr(DataArrayInt *famArr)
872 {
873   if(!famArr)
874     {
875       _fam=0;
876       return ;
877     }
878   MEDCouplingUMesh *mbt(_m_by_types);
879   if(!mbt)
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 ! ");
882   famArr->incrRef();
883   _fam=famArr;
884 }
885
886 void MEDFileUMeshSplitL1::setRenumArr(DataArrayInt *renumArr)
887 {
888   if(!renumArr)
889     {
890       _num=0;
891       _rev_num=0;
892       return ;
893     }
894   MEDCouplingUMesh *mbt(_m_by_types);
895   if(!mbt)
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 ! ");
898   renumArr->incrRef();
899   _num=renumArr;
900   computeRevNum();
901 }
902
903 void MEDFileUMeshSplitL1::setNameArr(DataArrayAsciiChar *nameArr)
904 {
905   if(!nameArr)
906     {
907       _names=0;
908       return ;
909     }
910   MEDCouplingUMesh *mbt(_m_by_types);
911   if(!mbt)
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 ! ");
914   nameArr->incrRef();
915   _names=nameArr;
916 }
917
918 MEDCouplingUMesh *MEDFileUMeshSplitL1::Renumber2(const DataArrayInt *renum, MEDCouplingUMesh *m, const int *cellIds)
919 {
920   if(renum==0)
921     return m;
922   if(cellIds==0)
923     m->renumberCells(renum->getConstPointer(),true);
924   else
925     {
926       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> locnum=renum->selectByTupleId(cellIds,cellIds+m->getNumberOfCells());
927       m->renumberCells(locnum->getConstPointer(),true);
928     }
929   return m;
930 }
931
932 MEDCouplingUMesh *MEDFileUMeshSplitL1::renumIfNeeded(MEDCouplingUMesh *m, const int *cellIds) const
933 {
934   return Renumber2(_num,m,cellIds);
935 }
936
937 DataArrayInt *MEDFileUMeshSplitL1::Renumber(const DataArrayInt *renum, const DataArrayInt *da)
938 {
939   if((const DataArrayInt *)renum==0)
940     {
941       da->incrRef();
942       return const_cast<DataArrayInt *>(da);
943     }
944   return renum->selectByTupleId(da->getConstPointer(),da->getConstPointer()+da->getNumberOfTuples());
945 }
946
947 DataArrayInt *MEDFileUMeshSplitL1::renumIfNeededArr(const DataArrayInt *da) const
948 {
949   return Renumber(_num,da);
950 }
951
952 std::vector<int> MEDFileUMeshSplitL1::GetNewFamiliesNumber(int nb, const std::map<std::string,int>& families)
953 {
954   int id=-1;
955   for(std::map<std::string,int>::const_iterator it=families.begin();it!=families.end();it++)
956     id=std::max(id,(*it).second);
957   if(id==-1)
958     id=0;
959   std::vector<int> ret(nb);
960   for(int i=1;i<=nb;i++)
961     ret[i]=id+i;
962   return ret;
963 }
964
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)
967 {
968   std::set<int> allfids;
969   
970 }
971
972 void MEDFileUMeshSplitL1::computeRevNum() const
973 {
974   int pos;
975   int maxValue=_num->getMaxValue(pos);
976   _rev_num=_num->invertArrayN2O2O2N(maxValue+1);
977 }