Salome HOME
[Intersect2D] Keep flexibility on orientation of mesh2
[tools/medcoupling.git] / src / MEDLoader / MEDFileMeshLL.cxx
1 // Copyright (C) 2007-2021  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, or (at your option) any later version.
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 "MEDFileSafeCaller.txx"
25 #include "MEDFileMeshReadSelector.hxx"
26 #include "MEDFileStructureElement.hxx"
27 #include "MEDFileMeshSupport.hxx"
28
29 #include "MEDCouplingUMesh.hxx"
30
31 #include "InterpKernelAutoPtr.hxx"
32 #include "CellModel.hxx"
33
34 #include <set>
35 #include <iomanip>
36
37 // From MEDLOader.cxx TU
38 extern med_geometry_type typmai[MED_N_CELL_FIXED_GEO];
39 extern INTERP_KERNEL::NormalizedCellType typmai2[MED_N_CELL_FIXED_GEO];
40 extern med_geometry_type typmainoeud[1];
41
42 using namespace MEDCoupling;
43
44 const char MEDFileMeshL2::ZE_SEP_FOR_FAMILY_KILLERS[]="!/__\\!";//important start by - because ord('!')==33 the smallest (!=' ') to preserve orders at most.
45
46 int MEDFileMeshL2::ZE_SEP2_FOR_FAMILY_KILLERS=4;
47
48 std::vector<std::string> MeshCls::getAxisInfoOnMesh(med_idt fid, const std::string& mName, MEDCoupling::MEDCouplingMeshType& meshType, MEDCoupling::MEDCouplingAxisType& axType, int& nstep, int& Mdim, MEDFileString& description, MEDFileString& dtunit, MEDFileString& univName) const
49 {
50   med_mesh_type type_maillage;
51   med_int spaceDim, meshDim, nbSteps;
52   med_sorting_type stype;
53   med_axis_type axistype;
54   med_int naxis(MEDmeshnAxis(fid,getID()));
55   INTERP_KERNEL::AutoPtr<char> nameTmp(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE));
56   INTERP_KERNEL::AutoPtr<char> axisname(MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE));
57   INTERP_KERNEL::AutoPtr<char> axisunit(MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE));
58   INTERP_KERNEL::AutoPtr<char> univTmp(MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE));
59   if(MEDmeshInfo(fid,getID(),nameTmp,&spaceDim,&meshDim,&type_maillage,description.getPointer(),dtunit.getPointer(),
60       &stype,&nbSteps,&axistype,axisname,axisunit)!=0)
61     throw INTERP_KERNEL::Exception("A problem has been detected when trying to get info on mesh !");
62   Mdim=FromMedInt<int>(meshDim);
63   nstep=FromMedInt<int>(nbSteps);
64   MEDmeshUniversalNameRd(fid,nameTmp,univName.getPointer());// do not protect  MEDFILESAFECALLERRD0 call : Thanks to fra.med.
65   axType=MEDFileMeshL2::TraduceAxisType(axistype);
66   switch(type_maillage)
67   {
68     case MED_UNSTRUCTURED_MESH:
69       meshType=UNSTRUCTURED;
70       break;
71     case MED_STRUCTURED_MESH:
72       {
73         med_grid_type gt;
74         MEDFILESAFECALLERRD0(MEDmeshGridTypeRd,(fid,mName.c_str(),&gt));
75         switch(gt)
76         {
77           case MED_CARTESIAN_GRID:
78             meshType=CARTESIAN;
79             break;
80           case MED_CURVILINEAR_GRID:
81             meshType=CURVE_LINEAR;
82             break;
83         case MED_POLAR_GRID:// this is not a bug. A MED file POLAR_GRID is deal by CARTESIAN MEDLoader
84             meshType=CARTESIAN;
85             break;
86           default:
87             throw INTERP_KERNEL::Exception("MEDFileMeshL2::getAxisInfoOnMesh : unrecognized structured mesh type ! Supported are :\n - cartesian\n - curve linear\n");
88         }
89         break;
90       }
91     default:
92       throw INTERP_KERNEL::Exception("MEDFileMeshL2::getMeshIdFromName : unrecognized mesh type !");
93   }
94   //
95   std::vector<std::string> infosOnComp(naxis);
96   for(int i=0;i<naxis;i++)
97     {
98       std::string info(MEDLoaderBase::buildUnionUnit(((char *)axisname)+i*MED_SNAME_SIZE,MED_SNAME_SIZE,((char *)axisunit)+i*MED_SNAME_SIZE,MED_SNAME_SIZE));
99       infosOnComp[i]=info;
100     }
101   return infosOnComp;
102 }
103
104 double MeshCls::checkMeshTimeStep(med_idt fid, const std::string& mName, int nstep, int dt, int it) const
105 {
106   bool found=false;
107   med_int numdt,numit;
108   med_float dtt=0.0;
109   std::vector< std::pair<int,int> > p(nstep);
110   for(int i=0;i<nstep;i++)
111     {
112       MEDFILESAFECALLERRD0(MEDmeshComputationStepInfo,(fid,mName.c_str(),i+1,&numdt,&numit,&dtt));
113       p[i]=std::make_pair((int)numdt,(int)numit);
114       found=(numdt==dt) && (numit==it);
115       if (found) break;
116     }
117   if(!found)
118     {
119       std::ostringstream oss; oss << "No such iteration=" << dt << ",order=" << it << " numbers found for mesh '" << mName << "' ! ";
120       oss << "Possibilities are : ";
121       for(int i=0;i<nstep;i++)
122         oss << "(" << p[i].first << "," << p[i].second << "), ";
123       throw INTERP_KERNEL::Exception(oss.str().c_str());
124     }
125   return dtt;
126 }
127
128 std::vector<std::string> StructMeshCls::getAxisInfoOnMesh(med_idt fid, const std::string& mName, MEDCoupling::MEDCouplingMeshType& meshType, MEDCoupling::MEDCouplingAxisType& axType, int& nstep, int& Mdim, MEDFileString& description, MEDFileString& dtunit, MEDFileString& univName) const
129 {
130   INTERP_KERNEL::AutoPtr<char> msn(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE));
131   INTERP_KERNEL::AutoPtr<char> zeDescription(MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE));
132   med_axis_type medAxType;
133   med_int nAxis(MEDsupportMeshnAxis(fid,getID()));
134   INTERP_KERNEL::AutoPtr<char> axisName(new char[MED_SNAME_SIZE*nAxis+1]),axisUnit(new char[MED_SNAME_SIZE*nAxis+1]);
135   med_int spaceDim(0),meshDim(0);
136   MEDFILESAFECALLERRD0(MEDsupportMeshInfo,(fid,getID(),msn,&spaceDim,&meshDim,zeDescription,&medAxType,axisName,axisUnit));
137   std::string descriptionCpp(MEDLoaderBase::buildStringFromFortran(zeDescription,MED_COMMENT_SIZE));
138   description.set(descriptionCpp.c_str());
139   dtunit.clear(); univName.clear(); meshType=UNSTRUCTURED; nstep=1;
140   axType=MEDFileMeshL2::TraduceAxisType(medAxType);
141   //int nmodels(0);
142   //med_bool chgt=MED_FALSE,trsf=MED_FALSE;
143   //nmodels=MEDmeshnEntity(fid,_name.c_str(),MED_NO_DT,MED_NO_IT,MED_STRUCT_ELEMENT,MED_GEO_ALL,MED_CONNECTIVITY,MED_NODAL,&chgt,&trsf);
144   std::vector<std::string> ret;
145   for(int i=0;i<nAxis;i++)
146     {
147       std::string info(DataArray::BuildInfoFromVarAndUnit(MEDLoaderBase::buildStringFromFortran(axisName+i*MED_SNAME_SIZE,MED_SNAME_SIZE),
148                                                           MEDLoaderBase::buildStringFromFortran(axisUnit+i*MED_SNAME_SIZE,MED_SNAME_SIZE)));
149       ret.push_back(info);
150     }
151   return ret;
152 }
153
154 double StructMeshCls::checkMeshTimeStep(med_idt fid, const std::string& mName, int nstep, int dt, int it) const
155 {
156   return 0.;
157 }
158
159 MEDFileMeshL2::MEDFileMeshL2():_name(MED_NAME_SIZE),_description(MED_COMMENT_SIZE),_univ_name(MED_LNAME_SIZE),_dt_unit(MED_LNAME_SIZE)
160 {
161 }
162
163 std::size_t MEDFileMeshL2::getHeapMemorySizeWithoutChildren() const
164 {
165   return 0;
166 }
167
168 std::vector<const BigMemoryObject *> MEDFileMeshL2::getDirectChildrenWithNull() const
169 {
170   return std::vector<const BigMemoryObject *>();
171 }
172
173 INTERP_KERNEL::AutoCppPtr<MeshOrStructMeshCls> MEDFileMeshL2::GetMeshIdFromName(med_idt fid, const std::string& mName, MEDCoupling::MEDCouplingMeshType& meshType, MEDCoupling::MEDCouplingAxisType& axType, int& dt, int& it, std::string& dtunit1)
174 {
175   med_mesh_type type_maillage=MED_UNDEF_MESH_TYPE;
176   char maillage_description[MED_COMMENT_SIZE+1];
177   char dtunit[MED_LNAME_SIZE+1];
178   med_int spaceDim,dim;
179   char nommaa[MED_NAME_SIZE+1];
180   med_int n=MEDnMesh(fid);
181   char found(0);
182   int ret=-1;
183   med_sorting_type stype;
184   std::vector<std::string> ms;
185   med_int nstep;
186   med_axis_type axistype=MED_UNDEF_AXIS_TYPE;
187   for(int i=0;i<n && found==0;i++)
188     {
189       med_int naxis(MEDmeshnAxis(fid,i+1));
190       INTERP_KERNEL::AutoPtr<char> axisname(MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE)),axisunit(MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE));
191       MEDFILESAFECALLERRD0(MEDmeshInfo,(fid,i+1,nommaa,&spaceDim,&dim,&type_maillage,maillage_description,dtunit,&stype,&nstep,&axistype,axisname,axisunit));      
192       dtunit1=MEDLoaderBase::buildStringFromFortran(dtunit,sizeof(dtunit));
193       std::string cur(MEDLoaderBase::buildStringFromFortran(nommaa,sizeof(nommaa)));
194       ms.push_back(cur);
195       if(cur==mName)
196         {
197           found=1;
198           ret=i+1;
199         }
200     }
201   if(found==0)
202     {//last chance ! Is it a support mesh ?
203       med_int nbSM(MEDnSupportMesh(fid));
204       for(int i=0;i<nbSM && found==0;i++)
205         {
206           med_int naxis(MEDsupportMeshnAxis(fid,i+1));
207           INTERP_KERNEL::AutoPtr<char> axisname(MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE)),axisunit(MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE));
208           MEDFILESAFECALLERRD0(MEDsupportMeshInfo,(fid,i+1,nommaa,&spaceDim,&dim,maillage_description,&axistype,axisname,axisunit));
209           std::string cur(MEDLoaderBase::buildStringFromFortran(nommaa,sizeof(nommaa)));
210           ms.push_back(cur);
211           if(cur==mName)
212             {
213               found=2;
214               ret=i+1;
215             }
216         }
217     }
218   ////////////////////////
219   switch(found)
220     {
221     case 1:
222       {
223         axType=TraduceAxisType(axistype);
224         switch(type_maillage)
225           {
226           case MED_UNSTRUCTURED_MESH:
227             meshType=UNSTRUCTURED;
228             break;
229           case MED_STRUCTURED_MESH:
230             {
231               med_grid_type gt;
232               MEDFILESAFECALLERRD0(MEDmeshGridTypeRd,(fid,mName.c_str(),&gt));
233               switch(gt)
234                 {
235                 case MED_CARTESIAN_GRID:
236                   meshType=CARTESIAN;
237                   break;
238                 case MED_CURVILINEAR_GRID:
239                   meshType=CURVE_LINEAR;
240                   break;
241                 case MED_POLAR_GRID:// this is not a bug. A MED file POLAR_GRID is deal by CARTESIAN MEDLoader
242                   meshType=CARTESIAN;
243                   break;
244                 default:
245                   throw INTERP_KERNEL::Exception("MEDFileMeshL2::getMeshIdFromName : unrecognized structured mesh type ! Supported are :\n - cartesian\n - curve linear\n");
246                 }
247               break;
248             }
249           default:
250             throw INTERP_KERNEL::Exception("MEDFileMeshL2::getMeshIdFromName : unrecognized mesh type !");
251           }
252         med_int numdt,numit;
253         med_float dtt;
254         MEDFILESAFECALLERRD0(MEDmeshComputationStepInfo,(fid,mName.c_str(),1,&numdt,&numit,&dtt));
255         dt=FromMedInt<int>(numdt); it=FromMedInt<int>(numit);
256         return new MeshCls(ret);
257       }
258     case 2:
259       {
260         meshType=UNSTRUCTURED;
261         dt=MED_NO_DT; it=MED_NO_IT; dtunit1.clear();
262         axType=TraduceAxisType(axistype);
263         return new StructMeshCls(ret);
264       }
265     default:
266       {
267         std::ostringstream oss;
268         oss << "No such meshname (" << mName <<  ") in file ! Must be in : ";
269         std::copy(ms.begin(),ms.end(),std::ostream_iterator<std::string>(oss,", "));
270         throw INTERP_KERNEL::Exception(oss.str().c_str());
271       }
272     }
273   
274 }
275
276 /*!
277  * non static and non const method because _description, _dt_unit... are set in this method.
278  */
279 std::vector<std::string> MEDFileMeshL2::getAxisInfoOnMesh(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, MEDCoupling::MEDCouplingMeshType& meshType, MEDCoupling::MEDCouplingAxisType& axType, int& nstep, int& Mdim)
280 {
281   return mId->getAxisInfoOnMesh(fid,mName,meshType,axType,nstep,Mdim,_description,_dt_unit,_univ_name);
282 }
283
284 void MEDFileMeshL2::ReadFamiliesAndGrps(med_idt fid, const std::string& meshName, std::map<std::string,mcIdType>& fams, std::map<std::string, std::vector<std::string> >& grps, MEDFileMeshReadSelector *mrs)
285 {
286   if(mrs && !(mrs->isCellFamilyFieldReading() || mrs->isNodeFamilyFieldReading()))
287     return ;
288   char nomfam[MED_NAME_SIZE+1];
289   med_int numfam;
290   med_int nfam=MEDnFamily(fid,meshName.c_str());
291   std::vector< std::pair<std::string,std::pair<mcIdType,std::vector<std::string> > > > crudeFams(nfam);
292   for(int i=0;i<nfam;i++)
293     {
294       med_int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
295       med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
296       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
297       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
298       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
299       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
300       MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
301       std::string famName(MEDLoaderBase::buildStringFromFortran(nomfam,MED_NAME_SIZE));
302       std::vector<std::string> grps2(ngro);
303       for(int j=0;j<ngro;j++)
304         grps2[j]=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
305       crudeFams[i]=std::pair<std::string,std::pair<mcIdType,std::vector<std::string> > >(famName,std::pair<mcIdType,std::vector<std::string> >(numfam,grps2));
306     }
307   RenameFamiliesFromFileToMemInternal(crudeFams);
308   for(std::vector< std::pair<std::string,std::pair<mcIdType,std::vector<std::string> > > >::const_iterator it0=crudeFams.begin();it0!=crudeFams.end();it0++)
309     {
310       fams[(*it0).first]=(*it0).second.first;
311       for(std::vector<std::string>::const_iterator it1=(*it0).second.second.begin();it1!=(*it0).second.second.end();it1++)
312         grps[*it1].push_back((*it0).first);
313     }
314 }
315
316 void MEDFileMeshL2::WriteFamiliesAndGrps(med_idt fid, const std::string& mname, const std::map<std::string,mcIdType>& fams, const std::map<std::string, std::vector<std::string> >& grps, int tooLongStrPol)
317 {
318   std::vector< std::pair<std::string,std::pair<mcIdType,std::vector<std::string> > > > crudeFams(fams.size());
319   std::size_t ii(0);
320   for(std::map<std::string,mcIdType>::const_iterator it=fams.begin();it!=fams.end();it++,ii++)
321     {
322       std::vector<std::string> grpsOfFam;
323       for(std::map<std::string, std::vector<std::string> >::const_iterator it1=grps.begin();it1!=grps.end();it1++)
324         {
325           if(std::find((*it1).second.begin(),(*it1).second.end(),(*it).first)!=(*it1).second.end())
326             grpsOfFam.push_back((*it1).first);
327         }
328       crudeFams[ii]=std::pair<std::string,std::pair<mcIdType,std::vector<std::string> > >((*it).first,std::pair<mcIdType,std::vector<std::string> >((*it).second,grpsOfFam));
329     }
330   RenameFamiliesFromMemToFileInternal(crudeFams);
331   for(std::vector< std::pair<std::string,std::pair<mcIdType,std::vector<std::string> > > >::const_iterator it=crudeFams.begin();it!=crudeFams.end();it++)
332     {
333       std::size_t ngro((*it).second.second.size());
334       INTERP_KERNEL::AutoPtr<char> groName=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE*ngro);
335       int i=0;
336       for(std::vector<std::string>::const_iterator it2=(*it).second.second.begin();it2!=(*it).second.second.end();it2++,i++)
337         MEDLoaderBase::safeStrCpy2((*it2).c_str(),MED_LNAME_SIZE,groName+i*MED_LNAME_SIZE,tooLongStrPol);
338       INTERP_KERNEL::AutoPtr<char> famName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
339       MEDLoaderBase::safeStrCpy((*it).first.c_str(),MED_NAME_SIZE,famName,tooLongStrPol);
340       med_int ret=MEDfamilyCr(fid,mname.c_str(),famName,ToMedInt((*it).second.first),ToMedInt(ngro),groName);
341       ret++;
342     }
343 }
344
345 void MEDFileMeshL2::RenameFamiliesPatternInternal(std::vector< std::pair<std::string,std::pair<mcIdType,std::vector<std::string> > > >& crudeFams, RenameFamiliesPatternFunc func)
346 {
347   std::size_t ii(0);
348   std::vector<std::string> fams(crudeFams.size());
349   for(std::vector< std::pair<std::string,std::pair<mcIdType,std::vector<std::string> > > >::const_iterator it=crudeFams.begin();it!=crudeFams.end();it++,ii++)
350     fams[ii]=(*it).first;
351   if(!func(fams))
352     return ;
353   ii=0;
354   for(std::vector< std::pair<std::string,std::pair<mcIdType,std::vector<std::string> > > >::iterator it=crudeFams.begin();it!=crudeFams.end();it++,ii++)
355     (*it).first=fams[ii];
356 }
357
358 /*!
359  * This method is dedicated to the killers that use a same family name to store different family ids. MED file API authorizes it.
360  * So this method renames families (if needed generally not !) in order to have a discriminant name for families.
361  */
362 void MEDFileMeshL2::RenameFamiliesFromFileToMemInternal(std::vector< std::pair<std::string,std::pair<mcIdType,std::vector<std::string> > > >& crudeFams)
363 {
364   RenameFamiliesPatternInternal(crudeFams,RenameFamiliesFromFileToMem);
365 }
366
367 bool MEDFileMeshL2::RenameFamiliesFromFileToMem(std::vector< std::string >& famNames)
368 {
369   std::map<std::string,mcIdType> m;
370   std::set<std::string> s;
371   for(std::vector< std::string >::const_iterator it=famNames.begin();it!=famNames.end();it++)
372     {
373       if(s.find(*it)!=s.end())
374         m[*it]=0;
375       s.insert(*it);
376     }
377   if(m.empty())
378     return false;// the general case !
379   for(std::vector< std::string >::iterator it=famNames.begin();it!=famNames.end();it++)
380     {
381       std::map<std::string,mcIdType>::iterator it2(m.find(*it));
382       if(it2!=m.end())
383         {
384           std::ostringstream oss; oss << *it << ZE_SEP_FOR_FAMILY_KILLERS << std::setfill('0') << std::setw(ZE_SEP2_FOR_FAMILY_KILLERS) << (*it2).second++;
385           *it=oss.str();
386         }
387     }
388   return true;
389 }
390
391 /*!
392  * This method is dedicated to the killers that use a same family name to store different family ids. MED file API authorizes it.
393  * So this method renames families (if needed generally not !) in order to have a discriminant name for families.
394  */
395 void MEDFileMeshL2::RenameFamiliesFromMemToFileInternal(std::vector< std::pair<std::string,std::pair<mcIdType,std::vector<std::string> > > >& crudeFams)
396 {
397   RenameFamiliesPatternInternal(crudeFams,RenameFamiliesFromMemToFile);
398 }
399
400 bool MEDFileMeshL2::RenameFamiliesFromMemToFile(std::vector< std::string >& famNames)
401 {
402   bool isSmthingStrange(false);
403   for(std::vector< std::string >::const_iterator it=famNames.begin();it!=famNames.end();it++)
404     {
405       std::size_t found((*it).find(ZE_SEP_FOR_FAMILY_KILLERS));
406       if(found!=std::string::npos)
407         isSmthingStrange=true;
408     }
409   if(!isSmthingStrange)
410     return false;
411   // pattern matching
412   std::map< std::string, std::vector<std::string> > m;
413   for(std::vector< std::string >::const_iterator it=famNames.begin();it!=famNames.end();it++)
414     {
415       std::size_t found((*it).find(ZE_SEP_FOR_FAMILY_KILLERS));
416       if(found!=std::string::npos && found>=1)
417         {
418           std::string s1((*it).substr(found+sizeof(ZE_SEP_FOR_FAMILY_KILLERS)-1));
419           if((int)s1.size()!=ZE_SEP2_FOR_FAMILY_KILLERS)
420             continue;
421           int k(-1);
422           std::istringstream iss(s1);
423           iss >> k;
424           bool isOK((iss.rdstate() & ( std::istream::failbit | std::istream::eofbit)) == std::istream::eofbit);
425           if(isOK && k>=0)
426             {
427               std::string s0((*it).substr(0,found));
428               m[s0].push_back(*it);
429             }
430         }
431     }
432   if(m.empty())
433     return false;
434   // filtering
435   std::map<std::string,std::string> zeMap;
436   for(std::map< std::string, std::vector<std::string> >::const_iterator it=m.begin();it!=m.end();it++)
437     {
438       if((*it).second.size()==1)
439         continue;
440       for(std::vector<std::string>::const_iterator it1=(*it).second.begin();it1!=(*it).second.end();it1++)
441         zeMap[*it1]=(*it).first;
442     }
443   if(zeMap.empty())
444     return false;
445   // traduce
446   for(std::vector< std::string >::iterator it=famNames.begin();it!=famNames.end();it++)
447     {
448       std::map<std::string,std::string>::iterator it1(zeMap.find(*it));
449       if(it1!=zeMap.end())
450         *it=(*it1).second;
451     }    
452   return true;
453 }
454
455 MEDCoupling::MEDCouplingAxisType MEDFileMeshL2::TraduceAxisType(med_axis_type at)
456 {
457   switch(at)
458     {
459     case MED_CARTESIAN:
460       return AX_CART;
461     case MED_CYLINDRICAL:
462       return AX_CYL;
463     case MED_SPHERICAL:
464       return AX_SPHER;
465     case MED_UNDEF_AXIS_TYPE:
466       return AX_CART;
467     default:
468       throw INTERP_KERNEL::Exception("MEDFileMeshL2::TraduceAxisType : unrecognized axis type !");
469     }
470 }
471
472 MEDCoupling::MEDCouplingAxisType MEDFileMeshL2::TraduceAxisTypeStruct(med_grid_type gt)
473 {
474   switch(gt)
475     {
476     case MED_CARTESIAN_GRID:
477       return AX_CART;
478     case MED_POLAR_GRID:
479       return AX_CYL;
480     default:
481       throw INTERP_KERNEL::Exception("MEDFileMeshL2::TraduceAxisTypeStruct : only Cartesian and Cylindrical supported by MED file !");
482     }
483 }
484
485 med_axis_type MEDFileMeshL2::TraduceAxisTypeRev(MEDCoupling::MEDCouplingAxisType at)
486 {
487   switch(at)
488     {
489     case AX_CART:
490       return MED_CARTESIAN;
491     case AX_CYL:
492       return MED_CYLINDRICAL;
493     case AX_SPHER:
494       return MED_SPHERICAL;
495     default:
496       throw INTERP_KERNEL::Exception("MEDFileMeshL2::TraduceAxisTypeRev : unrecognized axis type !");
497     }
498 }
499
500 med_grid_type MEDFileMeshL2::TraduceAxisTypeRevStruct(MEDCoupling::MEDCouplingAxisType at)
501 {
502   switch(at)
503     {
504     case AX_CART:
505       return MED_CARTESIAN_GRID;
506     case AX_CYL:
507       return MED_POLAR_GRID;
508     case AX_SPHER:
509       return MED_POLAR_GRID;
510     default:
511       throw INTERP_KERNEL::Exception("MEDFileMeshL2::TraduceAxisTypeRevStruct : unrecognized axis type !");
512     }
513 }
514
515 MEDFileUMeshL2::MEDFileUMeshL2()
516 {
517 }
518
519 std::vector<std::string> MEDFileUMeshL2::loadCommonPart(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, int dt, int it, int& Mdim)
520 {
521   Mdim=-3;
522   _name.set(mName.c_str());
523   int nstep;
524   MEDCoupling::MEDCouplingMeshType meshType;
525   MEDCoupling::MEDCouplingAxisType dummy3;
526   std::vector<std::string> ret(getAxisInfoOnMesh(fid,mId,mName.c_str(),meshType,dummy3,nstep,Mdim));
527   if(nstep==0)
528     {
529       Mdim=-4;
530       return std::vector<std::string>();
531     }
532   if(meshType!=UNSTRUCTURED)
533     throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected an unstructured one whereas in file it is not an unstructured !");
534   _time=mId->checkMeshTimeStep(fid,mName,nstep,dt,it);
535   _iteration=dt;
536   _order=it;
537   return ret;
538 }
539
540 void MEDFileUMeshL2::loadAll(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs)
541 {
542   int Mdim;
543   std::vector<std::string> infosOnComp(loadCommonPart(fid,mId,mName,dt,it,Mdim));
544   if(Mdim==-4)
545     return ;
546   loadConnectivity(fid,Mdim,mName,dt,it,mrs);//to improve check (dt,it) coherency
547   loadCoords(fid,infosOnComp,mName,dt,it);
548 }
549
550 void MEDFileUMeshL2::loadPart(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, const std::vector<INTERP_KERNEL::NormalizedCellType>& types, const std::vector<mcIdType>& slicPerTyp, int dt, int it, MEDFileMeshReadSelector *mrs)
551 {
552   int Mdim;
553   std::vector<std::string> infosOnComp(loadCommonPart(fid,mId,mName,dt,it,Mdim));
554   if(Mdim==-4)
555     return ;
556   loadPartOfConnectivity(fid,Mdim,mName,types,slicPerTyp,dt,it,mrs);
557   med_bool changement,transformation;
558   mcIdType nCoords(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation));
559   std::vector<bool> fetchedNodeIds(nCoords,false);
560   for(std::vector< std::vector< MCAuto<MEDFileUMeshPerType> > >::const_iterator it0=_per_type_mesh.begin();it0!=_per_type_mesh.end();it0++)
561     for(std::vector< MCAuto<MEDFileUMeshPerType> >::const_iterator it1=(*it0).begin();it1!=(*it0).end();it1++)
562       (*it1)->getMesh()->computeNodeIdsAlg(fetchedNodeIds);
563   if(!mrs || mrs->getNumberOfCoordsLoadSessions()==1)
564   {
565     mcIdType nMin(ToIdType(std::distance(fetchedNodeIds.begin(),std::find(fetchedNodeIds.begin(),fetchedNodeIds.end(),true))));
566     mcIdType nMax(ToIdType(std::distance(fetchedNodeIds.rbegin(),std::find(fetchedNodeIds.rbegin(),fetchedNodeIds.rend(),true))));
567     nMax=nCoords-nMax;
568     for(std::vector< std::vector< MCAuto<MEDFileUMeshPerType> > >::const_iterator it0=_per_type_mesh.begin();it0!=_per_type_mesh.end();it0++)
569       for(std::vector< MCAuto<MEDFileUMeshPerType> >::const_iterator it1=(*it0).begin();it1!=(*it0).end();it1++)
570         (*it1)->getMesh()->renumberNodesWithOffsetInConn(-nMin);
571     this->loadPartCoords(fid,infosOnComp,mName,dt,it,nMin,nMax);
572   }
573   else
574   {
575     mcIdType nbOfCooLS(mrs->getNumberOfCoordsLoadSessions());
576     MCAuto<DataArrayIdType> fni(DataArrayIdType::BuildListOfSwitchedOn(fetchedNodeIds));
577     MCAuto< MapKeyVal<mcIdType, mcIdType> > o2n(fni->invertArrayN2O2O2NOptimized());
578     for(std::vector< std::vector< MCAuto<MEDFileUMeshPerType> > >::const_iterator it0=_per_type_mesh.begin();it0!=_per_type_mesh.end();it0++)
579       for(std::vector< MCAuto<MEDFileUMeshPerType> >::const_iterator it1=(*it0).begin();it1!=(*it0).end();it1++)
580         (*it1)->getMesh()->renumberNodesInConn(o2n->data());
581     this->loadPartCoordsSlice(fid,infosOnComp,mName,dt,it,fni,nbOfCooLS);
582   }
583 }
584
585 void MEDFileUMeshL2::loadConnectivity(med_idt fid, int mdim, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs)
586 {
587   _per_type_mesh.resize(1);
588   _per_type_mesh[0].clear();
589   for(int j=0;j<MED_N_CELL_FIXED_GEO;j++)
590     {
591       MEDFileUMeshPerType *tmp(MEDFileUMeshPerType::New(fid,mName.c_str(),dt,it,mdim,typmai[j],typmai2[j],mrs));
592       if(tmp)
593         _per_type_mesh[0].push_back(tmp);
594     }
595   sortTypes();
596 }
597
598 void MEDFileUMeshL2::loadPartOfConnectivity(med_idt fid, int mdim, const std::string& mName, const std::vector<INTERP_KERNEL::NormalizedCellType>& types, const std::vector<mcIdType>& slicPerTyp, int dt, int it, MEDFileMeshReadSelector *mrs)
599 {
600   std::size_t nbOfTypes(types.size());
601   if(slicPerTyp.size()!=3*nbOfTypes)
602     throw INTERP_KERNEL::Exception("MEDFileUMeshL2::loadPartOfConnectivity : The size of slicPerTyp array is expected to be equal to 3 times size of array types !");
603   std::set<INTERP_KERNEL::NormalizedCellType> types2(types.begin(),types.end());
604   if(types2.size()!=nbOfTypes)
605     throw INTERP_KERNEL::Exception("MEDFileUMeshL2::loadPartOfConnectivity : the geometric types in types array must appear once !");
606   _per_type_mesh.resize(1);
607   _per_type_mesh[0].clear();
608   for(std::size_t ii=0;ii<nbOfTypes;ii++)
609     {
610       mcIdType strt(slicPerTyp[3*ii+0]),stp(slicPerTyp[3*ii+1]),step(slicPerTyp[3*ii+2]);
611       MCAuto<MEDFileUMeshPerType> tmp(MEDFileUMeshPerType::NewPart(fid,mName.c_str(),dt,it,mdim,types[ii],strt,stp,step,mrs));
612       _per_type_mesh[0].push_back(tmp);
613     }
614   sortTypes();
615 }
616
617 void MEDFileUMeshL2::loadCoords(med_idt fid, const std::vector<std::string>& infosOnComp, const std::string& mName, int dt, int it)
618 {
619   int spaceDim((int)infosOnComp.size());
620   med_bool changement,transformation;
621   med_int nCoords(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation));
622   _coords=DataArrayDouble::New();
623   _coords->alloc(nCoords,spaceDim);
624   double *coordsPtr(_coords->getPointer());
625   if (nCoords)
626     MEDFILESAFECALLERRD0(MEDmeshNodeCoordinateRd,(fid,mName.c_str(),dt,it,MED_FULL_INTERLACE,coordsPtr));
627   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
628     {
629       MCAuto<DataArrayMedInt> miFamCoord=DataArrayMedInt::New();
630       miFamCoord->alloc(nCoords,1);
631       MEDFILESAFECALLERRD0(MEDmeshEntityFamilyNumberRd,(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,miFamCoord->getPointer()));
632       _fam_coords=FromMedIntArray<mcIdType>(miFamCoord);
633     }
634   else
635     _fam_coords=0;
636   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
637     {
638       MCAuto<DataArrayMedInt> miNumCoord=DataArrayMedInt::New();
639       miNumCoord->alloc(nCoords,1);
640       MEDFILESAFECALLERRD0(MEDmeshEntityNumberRd,(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,miNumCoord->getPointer()));
641       _num_coords=FromMedIntArray<mcIdType>(miNumCoord);
642     }
643   else
644     _num_coords=0;
645   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NAME,MED_NODAL,&changement,&transformation)>0)
646     {
647       _name_coords=DataArrayAsciiChar::New();
648       _name_coords->alloc(nCoords+1,MED_SNAME_SIZE);//not a bug to avoid the memory corruption due to last \0 at the end
649       MEDFILESAFECALLERRD0(MEDmeshEntityNameRd,(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,_name_coords->getPointer()));
650       _name_coords->reAlloc(nCoords);//not a bug to avoid the memory corruption due to last \0 at the end
651     }
652   else
653     _name_coords=0;
654   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_GLOBAL_NUMBER,MED_NODAL,&changement,&transformation)>0)
655     {
656       MCAuto<DataArrayMedInt> miNumCoord=DataArrayMedInt::New();
657       miNumCoord->alloc(nCoords,1);
658       MEDFILESAFECALLERRD0(MEDmeshGlobalNumberRd,(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,miNumCoord->getPointer()));
659       _global_num_coords=FromMedIntArray<mcIdType>(miNumCoord);
660     }
661   for(int i=0;i<spaceDim;i++)
662     _coords->setInfoOnComponent(i,infosOnComp[i]);
663 }
664
665 void MEDFileUMeshL2::LoadPartCoords(med_idt fid, const std::vector<std::string>& infosOnComp, const std::string& mName, int dt, int it, mcIdType nMin, mcIdType nMax,
666 MCAuto<DataArrayDouble>& _coords, MCAuto<PartDefinition>& _part_coords, MCAuto<DataArrayIdType>& _fam_coords, MCAuto<DataArrayIdType>& _num_coords, MCAuto<DataArrayAsciiChar>& _name_coords)
667 {
668   med_bool changement,transformation;
669   med_int spaceDim((int)infosOnComp.size()),nCoords(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation));
670   _coords=DataArrayDouble::New();
671   mcIdType nbNodesToLoad(nMax-nMin);
672   _coords->alloc(nbNodesToLoad,spaceDim);
673   med_filter filter=MED_FILTER_INIT,filter2=MED_FILTER_INIT;
674   MEDfilterBlockOfEntityCr(fid,/*nentity*/nCoords,/*nvaluesperentity*/1,/*nconstituentpervalue*/spaceDim,
675                            MED_ALL_CONSTITUENT,MED_FULL_INTERLACE,MED_COMPACT_STMODE,MED_NO_PROFILE,
676                            /*start*/ToMedInt(nMin+1),/*stride*/1,/*count*/1,/*blocksize*/ToMedInt(nbNodesToLoad),
677                            /*lastblocksize=useless because count=1*/0,&filter);
678   MEDFILESAFECALLERRD0(MEDmeshNodeCoordinateAdvancedRd,(fid,mName.c_str(),dt,it,&filter,_coords->getPointer()));
679   _part_coords=PartDefinition::New(nMin,nMax,1);
680   MEDfilterClose(&filter);
681   MEDfilterBlockOfEntityCr(fid,nCoords,1,1,MED_ALL_CONSTITUENT,MED_FULL_INTERLACE,MED_COMPACT_STMODE,
682                            MED_NO_PROFILE,ToMedInt(nMin+1),1,1,ToMedInt(nbNodesToLoad),0,&filter2);
683   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
684     {
685       MCAuto<DataArrayMedInt> miFamCoord=DataArrayMedInt::New();
686       miFamCoord->alloc(nbNodesToLoad,1);
687       MEDFILESAFECALLERRD0(MEDmeshEntityAttributeAdvancedRd,(fid,mName.c_str(),MED_FAMILY_NUMBER,dt,it,MED_NODE,MED_NO_GEOTYPE,&filter2,miFamCoord->getPointer()));
688       _fam_coords=FromMedIntArray<mcIdType>(miFamCoord);
689     }
690   else
691     _fam_coords=nullptr;
692   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
693     {
694       MCAuto<DataArrayMedInt> miNumCoord=DataArrayMedInt::New();
695       miNumCoord->alloc(nbNodesToLoad,1);
696       MEDFILESAFECALLERRD0(MEDmeshEntityAttributeAdvancedRd,(fid,mName.c_str(),MED_NUMBER,dt,it,MED_NODE,MED_NO_GEOTYPE,&filter2,miNumCoord->getPointer()));
697       _num_coords=FromMedIntArray<mcIdType>(miNumCoord);
698     }
699   else
700     _num_coords=nullptr;
701   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NAME,MED_NODAL,&changement,&transformation)>0)
702     {
703       _name_coords=DataArrayAsciiChar::New();
704       _name_coords->alloc(nbNodesToLoad+1,MED_SNAME_SIZE);//not a bug to avoid the memory corruption due to last \0 at the end
705       MEDFILESAFECALLERRD0(MEDmeshEntityAttributeAdvancedRd,(fid,mName.c_str(),MED_NAME,dt,it,MED_NODE,MED_NO_GEOTYPE,&filter2,_name_coords->getPointer()));
706       _name_coords->reAlloc(nbNodesToLoad);//not a bug to avoid the memory corruption due to last \0 at the end
707     }
708   else
709     _name_coords=nullptr;
710   MEDfilterClose(&filter2);
711   _coords->setInfoOnComponents(infosOnComp);
712 }
713
714 /*!
715  * For performance reasons LoadPartCoordsArray method calls LoadPartCoords
716  */
717 void MEDFileUMeshL2::LoadPartCoordsArray(med_idt fid, const std::vector<std::string>& infosOnComp, const std::string& mName, int dt, int it, const DataArrayIdType *nodeIds,
718 MCAuto<DataArrayDouble>& _coords, MCAuto<DataArrayIdType>& _fam_coords, MCAuto<DataArrayIdType>& _num_coords, MCAuto<DataArrayAsciiChar>& _name_coords)
719 {
720   MCAuto<PartDefinition> useless;
721   nodeIds->checkAllocated();
722   nodeIds->checkNbOfComps(1,"loadPartCoordsSlice : Only one component expected !");
723   mcIdType nMin(0),nMax(0);
724   if(!nodeIds->empty())
725   { nMin = nodeIds->front(); nMax = nodeIds->back()+1; }
726   LoadPartCoords(fid,infosOnComp,mName,dt,it,nMin,nMax,_coords,useless,_fam_coords,_num_coords,_name_coords);
727   if(nodeIds->empty())
728     return ;
729   MCAuto<DataArrayIdType> nodeIds2(nodeIds->deepCopy());
730   nodeIds2->applyLin(1,-nMin);
731   _coords = _coords->selectByTupleIdSafe(nodeIds2->begin(),nodeIds2->end());
732   if(_fam_coords.isNotNull())
733     _fam_coords = _fam_coords->selectByTupleIdSafe(nodeIds2->begin(),nodeIds2->end());
734   if(_num_coords.isNotNull())
735     _num_coords = _num_coords->selectByTupleIdSafe(nodeIds2->begin(),nodeIds2->end());
736   if(_name_coords.isNotNull())
737     {
738       MCAuto<DataArrayChar> tmp(_name_coords->selectByTupleIdSafe(nodeIds2->begin(),nodeIds2->end()));
739       _name_coords = DynamicCastSafe<DataArrayChar,DataArrayAsciiChar>( tmp );
740     }
741 }
742
743 void MEDFileUMeshL2::loadPartCoords(med_idt fid, const std::vector<std::string>& infosOnComp, const std::string& mName, int dt, int it, mcIdType nMin, mcIdType nMax)
744 {
745   LoadPartCoords(fid,infosOnComp,mName,dt,it,nMin,nMax,_coords,_part_coords,_fam_coords,_num_coords,_name_coords);
746 }
747
748 void MEDFileUMeshL2::loadPartCoordsSlice(med_idt fid, const std::vector<std::string>& infosOnComp, const std::string& mName, int dt, int it, const DataArrayIdType *nodeIds, mcIdType nbOfCoordLS)
749 {
750   nodeIds->checkAllocated();
751   nodeIds->checkNbOfComps(1,"loadPartCoordsSlice : Only one component expected !");
752   if(nodeIds->empty())
753     return ;
754   if( nbOfCoordLS<1 )
755     throw INTERP_KERNEL::Exception("MEDFileUMeshL2::loadPartCoordsSlice : nb of coords load session must be >=1 !");
756   mcIdType nMin(nodeIds->front()),nMax(nodeIds->back()+1);
757   std::vector< MCAuto<DataArrayDouble> > coords(nbOfCoordLS);
758   std::vector< MCAuto<DataArrayIdType> > famCoords(nbOfCoordLS);
759   std::vector< MCAuto<DataArrayIdType> > numCoords(nbOfCoordLS);
760   std::vector< MCAuto<DataArrayAsciiChar> > nameCoords(nbOfCoordLS);
761   for(mcIdType ipart = 0 ; ipart < nbOfCoordLS ; ++ipart)
762     {
763       mcIdType partStart,partStop;
764       DataArray::GetSlice(nMin,nMax,1,ipart,nbOfCoordLS,partStart,partStop);
765       MCAuto<DataArrayIdType> idsNodeIdsToKeep(nodeIds->findIdsInRange(partStart,partStop));
766       MCAuto<DataArrayIdType> nodeIdsToKeep( nodeIds->selectByTupleIdSafe(idsNodeIdsToKeep->begin(),idsNodeIdsToKeep->end()) );
767       LoadPartCoordsArray(fid,infosOnComp,mName,dt,it,nodeIdsToKeep,coords[ipart],famCoords[ipart],numCoords[ipart],nameCoords[ipart]);
768     }
769   _coords = DataArrayDouble::Aggregate(ToConstVect<DataArrayDouble>(coords));
770   if(famCoords[0].isNotNull())
771     _fam_coords = DataArrayIdType::Aggregate(ToConstVect<DataArrayIdType>(famCoords));
772   if(numCoords[0].isNotNull())
773     _num_coords = DataArrayIdType::Aggregate(ToConstVect<DataArrayIdType>(numCoords));
774   if(nameCoords[0].isNotNull())
775   {
776     std::vector< MCAuto<DataArrayChar> > nameCoords2(nameCoords.begin(),nameCoords.end());
777     std::for_each(nameCoords2.begin(),nameCoords2.end(),[](MCAuto<DataArrayChar>& elt){ elt->incrRef(); });
778     MCAuto<DataArrayChar> tmp( DataArrayChar::Aggregate(ToConstVect<DataArrayChar>(nameCoords2)) );
779     _name_coords = DynamicCastSafe<DataArrayChar,DataArrayAsciiChar>( tmp );
780   }
781   _part_coords = DataArrayPartDefinition::New( const_cast<DataArrayIdType *>(nodeIds) );
782 }
783
784 void MEDFileUMeshL2::sortTypes()
785 {
786   std::set<int> mdims;
787   std::vector< MCAuto<MEDFileUMeshPerType> > tmp(_per_type_mesh[0]);
788   _per_type_mesh.clear();
789   for(std::vector< MCAuto<MEDFileUMeshPerType> >::const_iterator it=tmp.begin();it!=tmp.end();it++)
790     mdims.insert((*it)->getDim());
791   if(mdims.empty())
792     return;
793   int mdim=*mdims.rbegin();
794   _per_type_mesh.resize(mdim+1);
795   for(int dim=mdim+1;dim!=0;dim--)
796     {
797       std::vector< MCAuto<MEDFileUMeshPerType> >& elt=_per_type_mesh[mdim+1-dim];
798       for(std::vector< MCAuto<MEDFileUMeshPerType> >::const_iterator it=tmp.begin();it!=tmp.end();it++)
799         if((*it)->getDim()==dim-1)
800           elt.push_back(*it);
801     }
802   // suppression of contiguous empty levels at the end of _per_type_mesh.
803   int nbOfUselessLev=0;
804   bool isFirst=true;
805   for(std::vector< std::vector< MCAuto<MEDFileUMeshPerType> > >::reverse_iterator it2=_per_type_mesh.rbegin();it2!=_per_type_mesh.rend();it2++)
806     {
807       if((*it2).empty() && isFirst)
808         {
809           nbOfUselessLev++;
810         }
811       else
812         isFirst=false;
813     }
814   _per_type_mesh.resize(_per_type_mesh.size()-nbOfUselessLev);
815 }
816
817 void MEDFileUMeshL2::WriteCoords(med_idt fid, const std::string& mname, int dt, int it, double time, const DataArrayDouble *coords, const DataArrayIdType *famCoords, const DataArrayIdType *numCoords, const DataArrayAsciiChar *nameCoords, const DataArrayIdType *globalNumCoords)
818 {
819   if(!coords)
820     return ;
821   MEDFILESAFECALLERWR0(MEDmeshNodeCoordinateWr,(fid,mname.c_str(),dt,it,time,MED_FULL_INTERLACE,ToMedInt(coords->getNumberOfTuples()),coords->begin()));
822   if(famCoords)
823     MEDFILESAFECALLERWR0(MEDmeshEntityFamilyNumberWr,(fid,mname.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,ToMedInt(famCoords->getNumberOfTuples()),ToMedIntArray<mcIdType>(famCoords)->begin()));
824   if(numCoords)
825     MEDFILESAFECALLERWR0(MEDmeshEntityNumberWr,(fid,mname.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,ToMedInt(numCoords->getNumberOfTuples()),ToMedIntArray<mcIdType>(numCoords)->begin()));
826   if(nameCoords)
827     {
828       if(nameCoords->getNumberOfComponents()!=MED_SNAME_SIZE)
829         {
830           std::ostringstream oss; oss << " MEDFileUMeshL2::WriteCoords : expected a name field on nodes with number of components set to " << MED_SNAME_SIZE;
831           oss << " ! The array has " << nameCoords->getNumberOfComponents() << " components !";
832           throw INTERP_KERNEL::Exception(oss.str().c_str());
833         }
834       MEDFILESAFECALLERWR0(MEDmeshEntityNameWr,(fid,mname.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,ToMedInt(nameCoords->getNumberOfTuples()),nameCoords->begin()));
835     }
836   if(globalNumCoords)
837     MEDFILESAFECALLERWR0(MEDmeshGlobalNumberWr,(fid,mname.c_str(),dt,it,MED_NODE,MED_NONE,ToMedInt(globalNumCoords->getNumberOfTuples()),ToMedIntArray<mcIdType>(globalNumCoords)->begin()));
838 }
839
840 bool MEDFileUMeshL2::isFamDefinedOnLev(int levId) const
841 {
842   for(std::vector< MCAuto<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
843     if((*it)->getFam()==0)
844       return false;
845   return true;
846 }
847
848 bool MEDFileUMeshL2::isNumDefinedOnLev(int levId) const
849 {
850   for(std::vector< MCAuto<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
851     if((*it)->getNum()==0)
852       return false;
853   return true;
854 }
855
856 bool MEDFileUMeshL2::isNamesDefinedOnLev(int levId) const
857 {
858   for(std::vector< MCAuto<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
859     if((*it)->getNames()==0)
860       return false;
861   return true;
862 }
863
864 MEDFileCMeshL2::MEDFileCMeshL2():_ax_type(AX_CART)
865 {
866 }
867
868 void MEDFileCMeshL2::loadAll(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, int dt, int it)
869 {
870   _name.set(mName.c_str());
871   int nstep;
872   int Mdim;
873   MEDCoupling::MEDCouplingMeshType meshType;
874   MEDCoupling::MEDCouplingAxisType dummy3;
875   std::vector<std::string> infosOnComp(getAxisInfoOnMesh(fid,mId,mName.c_str(),meshType,dummy3,nstep,Mdim));
876   if(meshType!=CARTESIAN)
877     throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected a structured one whereas in file it is not a structured !");
878   _time=mId->checkMeshTimeStep(fid,mName,nstep,dt,it);
879   _iteration=dt;
880   _order=it;
881   //
882   med_grid_type gridtype;
883   MEDFILESAFECALLERRD0(MEDmeshGridTypeRd,(fid,mName.c_str(),&gridtype));
884   if(gridtype!=MED_CARTESIAN_GRID && gridtype!=MED_POLAR_GRID)
885     throw INTERP_KERNEL::Exception("Invalid rectilinear mesh ! Only cartesian and polar are supported !");
886   _ax_type=TraduceAxisTypeStruct(gridtype);
887   _cmesh=MEDCouplingCMesh::New();
888   for(int i=0;i<Mdim;i++)
889     {
890       med_data_type dataTypeReq=GetDataTypeCorrespondingToSpaceId(i);
891       med_bool chgt=MED_FALSE,trsf=MED_FALSE;
892       med_int nbOfElt(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NONE,dataTypeReq,MED_NO_CMODE,&chgt,&trsf));
893       MCAuto<DataArrayDouble> da=DataArrayDouble::New();
894       da->alloc(nbOfElt,1);
895       da->setInfoOnComponent(0,infosOnComp[i]);
896       MEDFILESAFECALLERRD0(MEDmeshGridIndexCoordinateRd,(fid,mName.c_str(),dt,it,i+1,da->getPointer()));
897       _cmesh->setCoordsAt(i,da);
898     }
899 }
900
901 med_data_type MEDFileCMeshL2::GetDataTypeCorrespondingToSpaceId(int id)
902 {
903   switch(id)
904   {
905     case 0:
906       return MED_COORDINATE_AXIS1;
907     case 1:
908       return MED_COORDINATE_AXIS2;
909     case 2:
910       return MED_COORDINATE_AXIS3;
911     default:
912       throw INTERP_KERNEL::Exception("Invalid meshdim detected in Cartesian Grid !");
913   }
914 }
915
916 MEDFileCLMeshL2::MEDFileCLMeshL2()
917 {
918 }
919
920 void MEDFileCLMeshL2::loadAll(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, int dt, int it)
921 {
922   _name.set(mName.c_str());
923   int nstep;
924   int Mdim;
925   MEDCoupling::MEDCouplingMeshType meshType;
926   MEDCoupling::MEDCouplingAxisType dummy3;
927   std::vector<std::string> infosOnComp(getAxisInfoOnMesh(fid,mId,mName,meshType,dummy3,nstep,Mdim));
928   if(meshType!=CURVE_LINEAR)
929     throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected a structured one whereas in file it is not a structured !");
930   _time=mId->checkMeshTimeStep(fid,mName,nstep,dt,it);
931   _iteration=dt;
932   _order=it;
933   //
934   _clmesh=MEDCouplingCurveLinearMesh::New();
935   MCAuto<DataArrayMedInt> miStGrid=DataArrayMedInt::New();
936   miStGrid->alloc(Mdim,1);
937   MEDFILESAFECALLERRD0(MEDmeshGridStructRd,(fid,mName.c_str(),dt,it,miStGrid->getPointer()));
938   MCAuto<DataArrayIdType> stGrid=FromMedIntArray<mcIdType>(miStGrid);
939   _clmesh->setNodeGridStructure(stGrid->begin(),stGrid->end());
940   med_bool chgt=MED_FALSE,trsf=MED_FALSE;
941   med_int nbNodes(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&chgt,&trsf));
942   MCAuto<DataArrayDouble> da=DataArrayDouble::New();
943   da->alloc(nbNodes,infosOnComp.size());
944   da->setInfoOnComponents(infosOnComp);
945   MEDFILESAFECALLERRD0(MEDmeshNodeCoordinateRd,(fid,mName.c_str(),dt,it,MED_FULL_INTERLACE,da->getPointer()));
946   _clmesh->setCoords(da);
947 }
948
949 MEDFileUMeshPermCompute::MEDFileUMeshPermCompute(const MEDFileUMeshSplitL1* st):_st(st),_mpt_time(0),_num_time(0)
950 {
951 }
952
953 /*!
954  * Warning it returns an instance to deallocate !!!!
955  */
956 MEDFileUMeshPermCompute::operator MEDCouplingUMesh *() const
957 {
958   _st->_num->updateTime();
959   if((MEDCouplingUMesh *)_m==0)
960     {
961       updateTime();
962       _m=static_cast<MEDCouplingUMesh *>(_st->_m_by_types.getUmesh()->deepCopy());
963       _m->renumberCells(_st->_num->begin(),true);
964       return _m.retn();
965     }
966   else
967     {
968       if(_mpt_time==_st->_m_by_types.getTimeOfThis() && _num_time==_st->_num->getTimeOfThis())
969         return _m.retn();
970       else
971         {
972           updateTime();
973           _m=static_cast<MEDCouplingUMesh *>(_st->_m_by_types.getUmesh()->deepCopy());
974           _m->renumberCells(_st->_num->begin(),true);
975           return _m.retn();
976         }
977     }
978 }
979
980 void MEDFileUMeshPermCompute::operator=(MEDCouplingUMesh *m)
981 {
982   _m=m;
983 }
984
985 void MEDFileUMeshPermCompute::updateTime() const
986 {
987   _mpt_time=_st->_m_by_types.getTimeOfThis();
988   _num_time=_st->_num->getTimeOfThis();
989 }
990
991 std::vector<const BigMemoryObject *> MEDFileUMeshPermCompute::getDirectChildrenWithNull() const
992 {
993   std::vector<const BigMemoryObject *> ret;
994   ret.push_back((const MEDCouplingUMesh *)_m);
995   return ret;
996 }
997
998 std::size_t MEDFileUMeshPermCompute::getHeapMemorySizeWithoutChildren() const
999 {
1000   return sizeof(MEDFileUMeshPermCompute);
1001 }
1002
1003 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(const MEDFileUMeshSplitL1& other):RefCountObject(other),_m_by_types(other._m_by_types),_fam(other._fam),_num(other._num),_names(other._names),_rev_num(other._rev_num),_m(this)
1004 {
1005 }
1006
1007 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(const MEDFileUMeshL2& l2, const std::string& mName, int id):_m(this)
1008 {
1009   const std::vector< MCAuto<MEDFileUMeshPerType> >& v=l2.getLev(id);
1010   if(v.empty())
1011     return;
1012   std::size_t sz=v.size();
1013   std::vector<const MEDCoupling1GTUMesh *> ms(sz);
1014   std::vector<const DataArrayIdType *> fams(sz),nums(sz);
1015   std::vector<const DataArrayChar *> names(sz);
1016   std::vector<const PartDefinition *> pds(sz);
1017   for(std::size_t i=0;i<sz;i++)
1018     {
1019       MEDCoupling1GTUMesh *elt(v[i]->getMesh());
1020       MCAuto<DataArrayDouble> tmp2=l2.getCoords();
1021       elt->setCoords(tmp2);
1022       ms[i]=elt;
1023       pds[i]=v[i]->getPartDef();
1024     }
1025   _m_by_types.assignParts(ms);
1026   _m_by_types.assignDefParts(pds);
1027   if(l2.isFamDefinedOnLev(id))
1028     {
1029       for(std::size_t i=0;i<sz;i++)
1030         fams[i]=v[i]->getFam();
1031       if(sz!=1)
1032         _fam=DataArrayIdType::Aggregate(fams);
1033       else
1034         {
1035           fams[0]->incrRef();
1036           _fam=const_cast<DataArrayIdType *>(fams[0]);
1037         }
1038     }
1039   if(l2.isNumDefinedOnLev(id))
1040     {
1041       for(std::size_t i=0;i<sz;i++)
1042         nums[i]=v[i]->getNum();
1043       if(sz!=1)
1044         _num=DataArrayIdType::Aggregate(nums);
1045       else
1046         {
1047           nums[0]->incrRef();
1048           _num=const_cast<DataArrayIdType *>(nums[0]);
1049         }
1050       computeRevNum();
1051     }
1052   if(l2.isNamesDefinedOnLev(id))
1053     {
1054       for(std::size_t i=0;i<sz;i++)
1055         names[i]=v[i]->getNames();
1056       _names=dynamic_cast<DataArrayAsciiChar *>(DataArrayChar::Aggregate(names));
1057     }
1058 }
1059
1060 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCoupling1GTUMesh *m):_m(this)
1061 {
1062   std::vector< const MEDCoupling1GTUMesh * > v(1);
1063   v[0]=m;
1064   assignParts(v);
1065 }
1066
1067 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCouplingUMesh *m):_m(this)
1068 {
1069   assignMesh(m,true);
1070 }
1071
1072 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCouplingUMesh *m, bool newOrOld):_m(this)
1073 {
1074   assignMesh(m,newOrOld);
1075 }
1076
1077 void MEDFileUMeshSplitL1::setName(const std::string& name)
1078 {
1079   _m_by_types.setName(name);
1080 }
1081
1082 std::size_t MEDFileUMeshSplitL1::getHeapMemorySizeWithoutChildren() const
1083 {
1084   return 0;
1085 }
1086
1087 std::vector<const BigMemoryObject *> MEDFileUMeshSplitL1::getDirectChildrenWithNull() const
1088 {
1089   std::vector<const BigMemoryObject *> ret;
1090   ret.push_back(&_m_by_types);
1091   ret.push_back(&_m);
1092   ret.push_back((const DataArrayIdType*)_fam);
1093   ret.push_back((const DataArrayIdType*)_num);
1094   ret.push_back((const DataArrayIdType*)_rev_num);
1095   ret.push_back((const DataArrayAsciiChar*)_names);
1096   return ret;
1097 }
1098
1099 MEDFileUMeshSplitL1 *MEDFileUMeshSplitL1::shallowCpyUsingCoords(DataArrayDouble *coords) const
1100 {
1101   MCAuto<MEDFileUMeshSplitL1> ret(new MEDFileUMeshSplitL1(*this));
1102   ret->_m_by_types.shallowCpyMeshes();
1103   ret->_m_by_types.setCoords(coords);
1104   return ret.retn();
1105 }
1106
1107 MEDFileUMeshSplitL1 *MEDFileUMeshSplitL1::deepCopy(DataArrayDouble *coords) const
1108 {
1109   MCAuto<MEDFileUMeshSplitL1> ret(new MEDFileUMeshSplitL1(*this));
1110   ret->_m_by_types=_m_by_types.deepCopy(coords);
1111   if((const DataArrayIdType *)_fam)
1112     ret->_fam=_fam->deepCopy();
1113   if((const DataArrayIdType *)_num)
1114     ret->_num=_num->deepCopy();
1115   if((const DataArrayIdType *)_rev_num)
1116     ret->_rev_num=_rev_num->deepCopy();
1117   if((const DataArrayAsciiChar *)_names)
1118     ret->_names=_names->deepCopy();
1119   return ret.retn();
1120 }
1121
1122 void MEDFileUMeshSplitL1::checkConsistency() const
1123 {
1124   if (!_fam || _fam->getNumberOfTuples() != getSize())
1125     throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::checkConsistency(): internal family array has an invalid size!");
1126   mcIdType nbCells = getSize();
1127   if (_num)
1128     {
1129       _num->checkNbOfTuplesAndComp(nbCells,1,"MEDFileUMeshSplitL1::checkConsistency(): inconsistent internal node numbering array!");
1130       mcIdType pos;
1131       mcIdType maxValue=_num->getMaxValue(pos);
1132       if (!_rev_num || _rev_num->getNumberOfTuples() != (maxValue+1))
1133         throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::checkConsistency(): inconsistent internal revert node numbering array!");
1134     }
1135   if ((_num && !_rev_num) || (!_num && _rev_num))
1136     throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::checkConsistency(): inconsistent internal numbering arrays (one is null)!");
1137   if (_num && !_num->hasUniqueValues())
1138     throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::checkConsistency(): inconsistent internal node numbering array: duplicates found!");
1139   if (_names)
1140     _names->checkNbOfTuplesAndComp(nbCells,1,"MEDFileUMeshSplitL1::checkConsistency(): internal cell naming array has an invalid size!");
1141
1142   _m_by_types.checkConsistency();
1143 }
1144
1145 bool MEDFileUMeshSplitL1::isEqual(const MEDFileUMeshSplitL1 *other, double eps, std::string& what) const
1146 {
1147   if(!_m_by_types.isEqual(other->_m_by_types,eps,what))
1148     return false;
1149   const DataArrayIdType *d1=_fam;
1150   const DataArrayIdType *d2=other->_fam;
1151   if((d1==0 && d2!=0) || (d1!=0 && d2==0))
1152     {
1153       what="Presence of family arr in one sublevel and not in other!";
1154       return false;
1155     }
1156   if(d1)
1157     if(!d1->isEqual(*d2))
1158       {
1159         what="family arr at a sublevel are not deeply equal !";
1160         return false;
1161       }
1162   d1=_num;
1163   d2=other->_num;
1164   if((d1==0 && d2!=0) || (d1!=0 && d2==0))
1165     {
1166       what="Presence of cell numbering arr in one sublevel and not in other!";
1167       return false;
1168     }
1169   if(d1)
1170     if(!d1->isEqual(*d2))
1171       {
1172         what="Numbering cell arr at a sublevel are not deeply equal !";
1173         return false;
1174       }
1175   const DataArrayAsciiChar *e1=_names;
1176   const DataArrayAsciiChar *e2=other->_names;
1177   if((e1==0 && e2!=0) || (e1!=0 && e2==0))
1178     {
1179       what="Presence of cell names arr in one sublevel and not in other!";
1180       return false;
1181     }
1182   if(e1)
1183     if(!e1->isEqual(*e2))
1184       {
1185         what="Name cell arr at a sublevel are not deeply equal !";
1186         return false;
1187       }
1188   return true;
1189 }
1190
1191 void MEDFileUMeshSplitL1::synchronizeTinyInfo(const MEDFileMesh& master) const
1192 {
1193   _m_by_types.synchronizeTinyInfo(master);
1194 }
1195
1196 void MEDFileUMeshSplitL1::clearNonDiscrAttributes() const
1197 {
1198   _m_by_types.clearNonDiscrAttributes();
1199 }
1200
1201 void MEDFileUMeshSplitL1::ClearNonDiscrAttributes(const MEDCouplingMesh *tmp)
1202 {
1203   if(!tmp)
1204     return ;
1205   (const_cast<MEDCouplingMesh *>(tmp))->setName("");
1206   (const_cast<MEDCouplingMesh *>(tmp))->setDescription("");
1207   (const_cast<MEDCouplingMesh *>(tmp))->setTime(0.,-1,-1);
1208   (const_cast<MEDCouplingMesh *>(tmp))->setTimeUnit("");
1209 }
1210
1211 void MEDFileUMeshSplitL1::setCoords(DataArrayDouble *coords)
1212 {
1213   _m_by_types.setCoords(coords);
1214 }
1215
1216 void MEDFileUMeshSplitL1::assignMesh(MEDCouplingUMesh *m, bool newOrOld)
1217 {
1218   if(newOrOld)
1219     {
1220       m->incrRef();
1221       _m=m;
1222       _m_by_types.assignUMesh(dynamic_cast<MEDCouplingUMesh *>(m->deepCopy()));
1223       MCAuto<DataArrayIdType> da=_m_by_types.getUmesh()->getRenumArrForConsecutiveCellTypesSpec(typmai2,typmai2+MED_N_CELL_FIXED_GEO);
1224       if(!da->isIota(m->getNumberOfCells()))
1225         {
1226           _num=da->invertArrayO2N2N2O(m->getNumberOfCells());
1227           _m.updateTime();
1228           computeRevNum();
1229           _m_by_types.getUmesh()->renumberCells(da->begin(),false);
1230         }
1231     }
1232   else
1233     {
1234       if(!m->checkConsecutiveCellTypesAndOrder(typmai2,typmai2+MED_N_CELL_FIXED_GEO))
1235         throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::assignMesh(): the mesh does not follow the MED file numbering convention! Invoke sortCellsInMEDFileFrmt() first!");
1236       m->incrRef();
1237       _m_by_types.assignUMesh(m);
1238     }
1239   assignCommonPart();
1240 }
1241
1242 void MEDFileUMeshSplitL1::forceComputationOfParts() const
1243 {
1244   _m_by_types.forceComputationOfPartsFromUMesh();
1245 }
1246
1247 void MEDFileUMeshSplitL1::assignParts(const std::vector< const MEDCoupling1GTUMesh * >& mParts)
1248 {
1249   _m_by_types.assignParts(mParts);
1250   assignCommonPart();
1251 }
1252
1253 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1():_m(this)
1254 {
1255 }
1256
1257 void MEDFileUMeshSplitL1::assignCommonPart()
1258 {
1259   _fam=DataArrayIdType::New();
1260   _fam->alloc(_m_by_types.getSize(),1);
1261   _fam->fillWithValue(0);
1262 }
1263
1264 bool MEDFileUMeshSplitL1::empty() const
1265 {
1266   return _m_by_types.empty();
1267 }
1268
1269 bool MEDFileUMeshSplitL1::presenceOfOneFams(const std::vector<mcIdType>& ids) const
1270 {
1271   const DataArrayIdType *fam=_fam;
1272   if(!fam)
1273     return false;
1274   return fam->presenceOfValue(ids);
1275 }
1276
1277 int MEDFileUMeshSplitL1::getMeshDimension() const
1278 {
1279   return _m_by_types.getMeshDimension();
1280 }
1281
1282 void MEDFileUMeshSplitL1::simpleRepr(std::ostream& oss) const
1283 {
1284   std::vector<mcIdType> code=_m_by_types.getDistributionOfTypes();
1285   std::size_t nbOfTypes=code.size()/3;
1286   for(std::size_t i=0;i<nbOfTypes;i++)
1287     {
1288       INTERP_KERNEL::NormalizedCellType typ=(INTERP_KERNEL::NormalizedCellType) code[3*i];
1289       oss << "    - Number of cells with type " << INTERP_KERNEL::CellModel::GetCellModel(typ).getRepr() << " : " << code[3*i+1] << std::endl;
1290     }
1291 }
1292
1293 mcIdType MEDFileUMeshSplitL1::getSize() const
1294 {
1295   return _m_by_types.getSize();
1296 }
1297
1298 MEDCouplingUMesh *MEDFileUMeshSplitL1::getFamilyPart(const mcIdType *idsBg, const mcIdType *idsEnd, bool renum) const
1299 {
1300   MCAuto<DataArrayIdType> eltsToKeep=_fam->findIdsEqualList(idsBg,idsEnd);
1301   MEDCouplingUMesh *m=(MEDCouplingUMesh *)_m_by_types.getUmesh()->buildPartOfMySelf(eltsToKeep->begin(),eltsToKeep->end(),true);
1302   if(renum)
1303     return renumIfNeeded(m,eltsToKeep->begin());
1304   return m;
1305 }
1306
1307 DataArrayIdType *MEDFileUMeshSplitL1::getFamilyPartArr(const mcIdType *idsBg, const mcIdType *idsEnd, bool renum) const
1308 {
1309   MCAuto<DataArrayIdType> da=_fam->findIdsEqualList(idsBg,idsEnd);
1310   if(renum)
1311     return renumIfNeededArr(da);
1312   return da.retn();
1313 }
1314
1315 std::vector<INTERP_KERNEL::NormalizedCellType> MEDFileUMeshSplitL1::getGeoTypes() const
1316 {
1317   return _m_by_types.getGeoTypes();
1318 }
1319
1320 mcIdType MEDFileUMeshSplitL1::getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType ct) const
1321 {
1322   return _m_by_types.getNumberOfCellsWithType(ct);
1323 }
1324
1325 MEDCouplingUMesh *MEDFileUMeshSplitL1::getWholeMesh(bool renum) const
1326 {
1327   MCAuto<MEDCouplingUMesh> tmp;
1328   if(renum && ((const DataArrayIdType *)_num))
1329     tmp=_m;
1330   else
1331     { tmp=_m_by_types.getUmesh(); if(tmp) tmp->incrRef(); }
1332   return tmp.retn();
1333 }
1334
1335 mcIdType MEDFileUMeshSplitL1::getNumberOfCells() const
1336 {
1337   return _m_by_types.getNumberOfCells();
1338 }
1339
1340 DataArrayIdType *MEDFileUMeshSplitL1::extractFamilyFieldOnGeoType(INTERP_KERNEL::NormalizedCellType gt) const
1341 {
1342   const DataArrayIdType *fam(_fam);
1343   if(!fam)
1344     return 0;
1345   mcIdType start(0),stop(0);
1346   _m_by_types.getStartStopOfGeoTypeWithoutComputation(gt,start,stop);
1347   return fam->selectByTupleIdSafeSlice(start,stop,1);
1348 }
1349
1350 DataArrayIdType *MEDFileUMeshSplitL1::extractNumberFieldOnGeoType(INTERP_KERNEL::NormalizedCellType gt) const
1351 {
1352   const DataArrayIdType *num(_num);
1353   if(!num)
1354     return 0;
1355   mcIdType start(0),stop(0);
1356   _m_by_types.getStartStopOfGeoTypeWithoutComputation(gt,start,stop);
1357   return num->selectByTupleIdSafeSlice(start,stop,1);
1358 }
1359
1360 DataArrayIdType *MEDFileUMeshSplitL1::getOrCreateAndGetFamilyField()
1361 {
1362   if((DataArrayIdType *)_fam)
1363     return _fam;
1364   mcIdType nbOfTuples=_m_by_types.getSize();
1365   _fam=DataArrayIdType::New(); _fam->alloc(nbOfTuples,1); _fam->fillWithZero();
1366   return _fam;
1367 }
1368
1369 const DataArrayIdType *MEDFileUMeshSplitL1::getFamilyField() const
1370 {
1371   return _fam;
1372 }
1373
1374 const DataArrayIdType *MEDFileUMeshSplitL1::getNumberField() const
1375 {
1376   return _num;
1377 }
1378
1379 const DataArrayIdType *MEDFileUMeshSplitL1::getRevNumberField() const
1380 {
1381   return _rev_num;
1382 }
1383
1384 const DataArrayAsciiChar *MEDFileUMeshSplitL1::getNameField() const
1385 {
1386   return _names;
1387 }
1388
1389 const PartDefinition *MEDFileUMeshSplitL1::getPartDef(INTERP_KERNEL::NormalizedCellType gt) const
1390 {
1391   return _m_by_types.getPartDefOfWithoutComputation(gt);
1392 }
1393
1394 void MEDFileUMeshSplitL1::eraseFamilyField()
1395 {
1396   _fam->fillWithZero();
1397 }
1398
1399 /*!
1400  * This method ignores _m and _m_by_types.
1401  */
1402 void MEDFileUMeshSplitL1::setGroupsFromScratch(const std::vector<const MEDCouplingUMesh *>& ms, std::map<std::string,mcIdType>& familyIds,
1403                                                std::map<std::string, std::vector<std::string> >& groups)
1404 {
1405   std::vector< DataArrayIdType * > corr;
1406   _m=MEDCouplingUMesh::FuseUMeshesOnSameCoords(ms,0,corr);
1407   std::vector< MCAuto<DataArrayIdType> > corrMSafe(corr.begin(),corr.end());
1408   std::vector< std::vector<mcIdType> > fidsOfGroups;
1409   std::vector< const DataArrayIdType * > corr2(corr.begin(),corr.end());
1410   _fam=DataArrayIdType::MakePartition(corr2,((MEDCouplingUMesh *)_m)->getNumberOfCells(),fidsOfGroups);
1411   mcIdType nbOfCells=((MEDCouplingUMesh *)_m)->getNumberOfCells();
1412   std::map<mcIdType,std::string> newfams;
1413   std::map<mcIdType,mcIdType> famIdTrad;
1414   TraduceFamilyNumber(fidsOfGroups,familyIds,famIdTrad,newfams);
1415   mcIdType *w=_fam->getPointer();
1416   for(mcIdType i=0;i<nbOfCells;i++,w++)
1417     *w=famIdTrad[*w];
1418 }
1419
1420 void MEDFileUMeshSplitL1::write(med_idt fid, const std::string& mName, int mdim) const
1421 {
1422   std::vector<MEDCoupling1GTUMesh *> ms(_m_by_types.getParts());
1423   mcIdType start=0;
1424   for(std::vector<MEDCoupling1GTUMesh *>::const_iterator it=ms.begin();it!=ms.end();it++)
1425     {
1426       mcIdType nbCells=(*it)->getNumberOfCells();
1427       mcIdType end=start+nbCells;
1428       MCAuto<DataArrayIdType> fam,num;
1429       MCAuto<DataArrayAsciiChar> names;
1430       if((const DataArrayIdType *)_fam)
1431         fam=_fam->subArray(start,end);
1432       if((const DataArrayIdType *)_num)
1433         num=_num->subArray(start,end);
1434       if((const DataArrayAsciiChar *)_names)
1435         names=static_cast<DataArrayAsciiChar *>(_names->subArray(start,end));
1436       MEDFileUMeshPerType::Write(fid,mName,mdim,(*it),fam,num,names);
1437       start=end;
1438     }
1439 }
1440
1441 void MEDFileUMeshSplitL1::renumberNodesInConn(const mcIdType *newNodeNumbersO2N)
1442 {
1443   _m_by_types.renumberNodesInConnWithoutComputation(newNodeNumbersO2N);
1444 }
1445
1446 void MEDFileUMeshSplitL1::serialize(std::vector<mcIdType>& tinyInt, std::vector< MCAuto<DataArrayIdType> >& bigArraysI) const
1447 {
1448   bigArraysI.push_back(_fam);
1449   bigArraysI.push_back(_num);
1450   _m_by_types.serialize(tinyInt,bigArraysI);
1451 }
1452
1453 void MEDFileUMeshSplitL1::unserialize(const std::string& name, DataArrayDouble *coo, std::vector<mcIdType>& tinyInt, std::vector< MCAuto<DataArrayIdType> >& bigArraysI)
1454 {
1455   _fam=bigArraysI.back(); bigArraysI.pop_back();
1456   _num=bigArraysI.back(); bigArraysI.pop_back();
1457   _m_by_types.unserialize(name,coo,tinyInt,bigArraysI);
1458 }
1459
1460 void MEDFileUMeshSplitL1::changeFamilyIdArr(mcIdType oldId, mcIdType newId)
1461 {
1462   DataArrayIdType *arr=_fam;
1463   if(arr)
1464     arr->changeValue(oldId,newId);
1465 }
1466
1467 void MEDFileUMeshSplitL1::setFamilyArr(DataArrayIdType *famArr)
1468 {
1469   if(!famArr)
1470     {
1471       _fam=0;
1472       return ;
1473     }
1474   mcIdType sz(_m_by_types.getSize());
1475   famArr->checkNbOfTuplesAndComp(sz,1,"MEDFileUMeshSplitL1::setFamilyArr : Problem in size of Family arr ! ");
1476   famArr->incrRef();
1477   _fam=famArr;
1478 }
1479
1480 DataArrayIdType *MEDFileUMeshSplitL1::getFamilyField()
1481 {
1482   return _fam;
1483 }
1484
1485 void MEDFileUMeshSplitL1::setRenumArr(DataArrayIdType *renumArr)
1486 {
1487   if(!renumArr)
1488     {
1489       _num=0;
1490       _rev_num=0;
1491       return ;
1492     }
1493   mcIdType sz(_m_by_types.getSize());
1494   renumArr->checkNbOfTuplesAndComp(sz,1,"MEDFileUMeshSplitL1::setRenumArr : Problem in size of numbering arr ! ");
1495   renumArr->incrRef();
1496   _num=renumArr;
1497   computeRevNum();
1498 }
1499
1500 void MEDFileUMeshSplitL1::setNameArr(DataArrayAsciiChar *nameArr)
1501 {
1502   if(!nameArr)
1503     {
1504       _names=0;
1505       return ;
1506     }
1507   mcIdType sz(_m_by_types.getSize());
1508   nameArr->checkNbOfTuplesAndComp(sz,MED_SNAME_SIZE,"MEDFileUMeshSplitL1::setNameArr : Problem in size of name arr ! ");
1509   nameArr->incrRef();
1510   _names=nameArr;
1511 }
1512
1513 MEDCouplingUMesh *MEDFileUMeshSplitL1::Renumber2(const DataArrayIdType *renum, MEDCouplingUMesh *m, const mcIdType *cellIds)
1514 {
1515   if(renum==0)
1516     return m;
1517   if(cellIds==0)
1518     m->renumberCells(renum->begin(),true);
1519   else
1520     {
1521       MCAuto<DataArrayIdType> locnum=renum->selectByTupleId(cellIds,cellIds+m->getNumberOfCells());
1522       m->renumberCells(locnum->begin(),true);
1523     }
1524   return m;
1525 }
1526
1527 MEDFileUMeshSplitL1 *MEDFileUMeshSplitL1::Unserialize(const std::string& name, DataArrayDouble *coo, std::vector<mcIdType>& tinyInt, std::vector< MCAuto<DataArrayIdType> >& bigArraysI)
1528 {
1529   MCAuto<MEDFileUMeshSplitL1> ret(new MEDFileUMeshSplitL1);
1530   ret->unserialize(name,coo,tinyInt,bigArraysI);
1531   return ret.retn();
1532 }
1533
1534 MEDCouplingUMesh *MEDFileUMeshSplitL1::renumIfNeeded(MEDCouplingUMesh *m, const mcIdType *cellIds) const
1535 {
1536   return Renumber2(_num,m,cellIds);
1537 }
1538
1539 DataArrayIdType *MEDFileUMeshSplitL1::Renumber(const DataArrayIdType *renum, const DataArrayIdType *da)
1540 {
1541   if((const DataArrayIdType *)renum==0)
1542     {
1543       da->incrRef();
1544       return const_cast<DataArrayIdType *>(da);
1545     }
1546   return renum->selectByTupleId(da->begin(),da->end());
1547 }
1548
1549 DataArrayIdType *MEDFileUMeshSplitL1::renumIfNeededArr(const DataArrayIdType *da) const
1550 {
1551   return Renumber(_num,da);
1552 }
1553
1554 std::vector<mcIdType> MEDFileUMeshSplitL1::GetNewFamiliesNumber(mcIdType nb, const std::map<std::string,mcIdType>& families)
1555 {
1556   mcIdType id=-1;
1557   for(std::map<std::string,mcIdType>::const_iterator it=families.begin();it!=families.end();it++)
1558     id=std::max(id,(*it).second);
1559   if(id==-1)
1560     id=0;
1561   std::vector<mcIdType> ret(nb);
1562   for(mcIdType i=1;i<=nb;i++)
1563     ret[i]=id+i;
1564   return ret;
1565 }
1566
1567 void MEDFileUMeshSplitL1::TraduceFamilyNumber(const std::vector< std::vector<mcIdType> >& fidsGrps, std::map<std::string,mcIdType>& familyIds,
1568                                               std::map<mcIdType,mcIdType>& famIdTrad, std::map<mcIdType,std::string>& newfams)
1569 {
1570   std::set<mcIdType> allfids;
1571   //tony
1572 }
1573
1574 void MEDFileUMeshSplitL1::computeRevNum() const
1575 {
1576   mcIdType pos;
1577   if(!_num->empty())
1578   {
1579     mcIdType maxValue=_num->getMaxValue(pos);
1580     _rev_num=_num->invertArrayN2O2O2N(maxValue+1);
1581   }
1582   else
1583   {
1584     _rev_num = DataArrayIdType::New();
1585     _rev_num->alloc(0,1);
1586   }
1587   
1588 }
1589
1590 //=
1591
1592 MEDFileUMeshAggregateCompute::MEDFileUMeshAggregateCompute():_mp_time(0),_m_time(0)
1593 {
1594 }
1595
1596 void MEDFileUMeshAggregateCompute::setName(const std::string& name)
1597 {
1598   if(_m_time>=_mp_time)
1599     {
1600       MEDCouplingUMesh *um(_m);
1601       if(um)
1602         um->setName(name);
1603     }
1604   if(_mp_time>=_m_time)
1605     {
1606       for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1607         {
1608           MEDCoupling1GTUMesh *tmp(*it);
1609           if(tmp)
1610             tmp->setName(name);
1611         }
1612     }
1613 }
1614
1615 void MEDFileUMeshAggregateCompute::assignParts(const std::vector< const MEDCoupling1GTUMesh * >& mParts)
1616 {
1617   std::size_t sz(mParts.size());
1618   std::vector< MCAuto<MEDCoupling1GTUMesh> > ret(sz);
1619   for(std::size_t i=0;i<sz;i++)
1620     {
1621       const MEDCoupling1GTUMesh *elt(mParts[i]);
1622       if(!elt)
1623         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::assignParts : presence of null pointer !");
1624       ret[i]=const_cast<MEDCoupling1GTUMesh *>(elt); elt->incrRef();
1625     }
1626   _m_parts=ret;
1627   _part_def.clear(); _part_def.resize(sz);
1628   _mp_time=std::max(_mp_time,_m_time)+1;
1629   _m=0;
1630 }
1631
1632 void MEDFileUMeshAggregateCompute::assignDefParts(const std::vector<const PartDefinition *>& partDefs)
1633 {
1634   if(_mp_time<_m_time)
1635     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::assignDefParts : the parts require a computation !");
1636   std::size_t sz(partDefs.size());
1637   if(_part_def.size()!=partDefs.size() || _part_def.size()!=_m_parts.size())
1638     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::assignDefParts : sizes of vectors of part definition mismatch !");
1639   for(std::size_t i=0;i<sz;i++)
1640     {
1641       const PartDefinition *elt(partDefs[i]);
1642       if(elt)
1643         elt->incrRef();
1644       _part_def[i]=const_cast<PartDefinition*>(elt);
1645     }
1646 }
1647
1648 void MEDFileUMeshAggregateCompute::assignUMesh(MEDCouplingUMesh *m)
1649 {
1650   _m=m;
1651   _m_parts.clear();
1652   _m_time=std::max(_mp_time,_m_time)+1;
1653 }
1654
1655 MEDCouplingUMesh *MEDFileUMeshAggregateCompute::getUmesh() const
1656 {
1657   if(_mp_time<=_m_time)
1658     return _m;
1659   std::vector< const MEDCoupling1GTUMesh *> mp(_m_parts.size());
1660   std::copy(_m_parts.begin(),_m_parts.end(),mp.begin());
1661   _m=MEDCoupling1GTUMesh::AggregateOnSameCoordsToUMesh(mp);
1662   _m_parts.clear();//to avoid memory peak !
1663   _m_time=_mp_time+1;//+1 is important ! That is to say that only _m is OK not _m_parts because cleared !
1664   return _m;
1665 }
1666
1667 mcIdType MEDFileUMeshAggregateCompute::getNumberOfCells() const
1668 {
1669   if(_mp_time<=_m_time)
1670     return _m->getNumberOfCells();
1671   mcIdType ret(0);
1672   for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1673     ret+=(*it)->getNumberOfCells();
1674   return ret;
1675 }
1676
1677 std::vector<INTERP_KERNEL::NormalizedCellType> MEDFileUMeshAggregateCompute::getGeoTypes() const
1678 {
1679   if(_mp_time>=_m_time)
1680     {
1681       std::size_t sz(_m_parts.size());
1682       std::vector<INTERP_KERNEL::NormalizedCellType> ret(sz);
1683       for(std::size_t i=0;i<sz;i++)
1684         ret[i]=_m_parts[i]->getCellModelEnum();
1685       return ret;
1686     }
1687   else
1688     return _m->getAllGeoTypesSorted();
1689 }
1690
1691 mcIdType MEDFileUMeshAggregateCompute::getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType ct) const
1692 {
1693   if(_mp_time>=_m_time)
1694     {
1695       for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1696         {
1697           const MEDCoupling1GTUMesh *elt(*it);
1698           if(elt && elt->getCellModelEnum()==ct)
1699             return elt->getNumberOfCells();
1700         }
1701       return 0;
1702     }
1703   else
1704     return _m->getNumberOfCellsWithType(ct);
1705 }
1706
1707 std::vector<MEDCoupling1GTUMesh *> MEDFileUMeshAggregateCompute::retrievePartsWithoutComputation() const
1708 {
1709   if(_mp_time<_m_time)
1710     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getPartsWithoutComputation : the parts require a computation !");
1711   //
1712   std::vector<MEDCoupling1GTUMesh *> ret(_m_parts.size());
1713   std::size_t i(0);
1714   for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++,i++)
1715     {
1716       const MEDCoupling1GTUMesh *elt(*it);
1717       ret[i]=const_cast<MEDCoupling1GTUMesh *>(elt);
1718     }
1719   return ret;
1720 }
1721
1722 std::vector<MEDCoupling1GTUMesh *> MEDFileUMeshAggregateCompute::getParts() const
1723 {
1724   if(_mp_time<_m_time)
1725     forceComputationOfPartsFromUMesh();
1726   return retrievePartsWithoutComputation();
1727 }
1728
1729 void MEDFileUMeshAggregateCompute::highlightUsedNodes(std::vector<bool>& nodesToBeHighlighted) const
1730 {
1731   if(_mp_time<_m_time)
1732     forceComputationOfPartsFromUMesh();
1733   for(auto part : this->_m_parts)
1734   {
1735     part->computeNodeIdsAlg(nodesToBeHighlighted);
1736   }
1737 }
1738
1739 MEDCoupling1GTUMesh *MEDFileUMeshAggregateCompute::retrievePartWithoutComputation(INTERP_KERNEL::NormalizedCellType gt) const
1740 {
1741   std::vector<MEDCoupling1GTUMesh *> v(retrievePartsWithoutComputation());
1742   std::size_t sz(v.size());
1743   for(std::size_t i=0;i<sz;i++)
1744     {
1745       if(v[i])
1746         if(v[i]->getCellModelEnum()==gt)
1747           return v[i];
1748     }
1749   throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getPartWithoutComputation : the geometric type is not existing !");
1750 }
1751
1752 void MEDFileUMeshAggregateCompute::getStartStopOfGeoTypeWithoutComputation(INTERP_KERNEL::NormalizedCellType gt, mcIdType& start, mcIdType& stop) const
1753 {
1754   start=0; stop=0;
1755   std::vector<MEDCoupling1GTUMesh *> v(retrievePartsWithoutComputation());
1756   std::size_t sz(v.size());
1757   for(std::size_t i=0;i<sz;i++)
1758     {
1759       if(v[i])
1760         {
1761           if(v[i]->getCellModelEnum()==gt)
1762             {
1763               stop=start+v[i]->getNumberOfCells();
1764               return;
1765             }
1766           else
1767             start+=v[i]->getNumberOfCells();
1768         }
1769     }
1770   throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getStartStopOfGeoTypeWithoutComputation : the geometric type is not existing !");
1771 }
1772
1773 void MEDFileUMeshAggregateCompute::renumberNodesInConnWithoutComputation(const mcIdType *newNodeNumbersO2N)
1774 {
1775   if(_mp_time>_m_time)
1776     {
1777       for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1778         {
1779           MEDCoupling1GTUMesh *m(*it);
1780           if(m)
1781             m->renumberNodesInConn(newNodeNumbersO2N);
1782         }
1783     }
1784   else
1785     {
1786       MEDCouplingUMesh *m(getUmesh());
1787       if(!m)
1788         return;
1789       m->renumberNodesInConn(newNodeNumbersO2N);
1790       // if _mp_time == _m_time notify for future clients that _m_parts is obsolete
1791       _m_parts.clear();
1792       _m_time = std::max(_m_time,_mp_time+1);
1793     }
1794 }
1795
1796 void MEDFileUMeshAggregateCompute::forceComputationOfPartsFromUMesh() const
1797 {
1798   const MEDCouplingUMesh *m(_m);
1799   if(!m)
1800     {
1801       if(_m_parts.empty())
1802         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::forceComputationOfPartsFromUMesh : null UMesh !");
1803       else
1804         return ;// no needs to compte parts they are already here !
1805     }
1806   std::vector<MEDCouplingUMesh *> ms(m->splitByType());
1807   std::vector< MCAuto<MEDCouplingUMesh> > msMSafe(ms.begin(),ms.end());
1808   std::size_t sz(msMSafe.size());
1809   _m_parts.resize(sz);
1810   for(std::size_t i=0;i<sz;i++)
1811     _m_parts[i]=MEDCoupling1GTUMesh::New(ms[i]);
1812   _part_def.clear();
1813   _part_def.resize(_m_parts.size());
1814   _mp_time=std::max(_mp_time,_m_time);
1815 }
1816
1817 const PartDefinition *MEDFileUMeshAggregateCompute::getPartDefOfWithoutComputation(INTERP_KERNEL::NormalizedCellType gt) const
1818 {
1819   if(_mp_time<_m_time)
1820     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getPartDefOfWithoutComputation : the parts require a computation !");
1821   if(_m_parts.size()!=_part_def.size())
1822     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getPartDefOfWithoutComputation : size of arrays are expected to be the same !");
1823   std::size_t sz(_m_parts.size());
1824   for(std::size_t i=0;i<sz;i++)
1825     {
1826       const MEDCoupling1GTUMesh *mesh(_m_parts[i]);
1827       if(mesh)
1828         if(mesh->getCellModelEnum()==gt)
1829           return _part_def[i];
1830     }
1831   throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getPartDefOfWithoutComputation : The input geo type is not existing in this !");
1832 }
1833
1834 void MEDFileUMeshAggregateCompute::serialize(std::vector<mcIdType>& tinyInt, std::vector< MCAuto<DataArrayIdType> >& bigArraysI) const
1835 {
1836   if(_mp_time<_m_time)
1837     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::serialize : the parts require a computation !");
1838   std::size_t sz(_m_parts.size());
1839   tinyInt.push_back((mcIdType)sz);
1840   for(std::size_t i=0;i<sz;i++)
1841     {
1842       const MEDCoupling1GTUMesh *mesh(_m_parts[i]);
1843       if(!mesh)
1844         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::serialize : one part is empty !");
1845       tinyInt.push_back(mesh->getCellModelEnum());
1846       const MEDCoupling1SGTUMesh *mesh1(dynamic_cast<const MEDCoupling1SGTUMesh *>(mesh));
1847       const MEDCoupling1DGTUMesh *mesh2(dynamic_cast<const MEDCoupling1DGTUMesh *>(mesh));
1848       if(mesh1)
1849         {
1850           DataArrayIdType *elt(mesh1->getNodalConnectivity());
1851           if(elt)
1852             elt->incrRef();
1853           MCAuto<DataArrayIdType> elt1(elt);
1854           bigArraysI.push_back(elt1);
1855         }
1856       else if(mesh2)
1857         {
1858           DataArrayIdType *elt1(mesh2->getNodalConnectivity()),*elt2(mesh2->getNodalConnectivityIndex());
1859           if(elt1)
1860             elt1->incrRef();
1861           if(elt2)
1862             elt2->incrRef();
1863           MCAuto<DataArrayIdType> elt11(elt1),elt22(elt2);
1864           bigArraysI.push_back(elt11); bigArraysI.push_back(elt22);
1865         }
1866       else
1867         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::serialize : unrecognized single geo type mesh !");
1868       const PartDefinition *pd(_part_def[i]);
1869       if(!pd)
1870         tinyInt.push_back(-1);
1871       else
1872         {
1873           std::vector<mcIdType> tinyTmp;
1874           pd->serialize(tinyTmp,bigArraysI);
1875           tinyInt.push_back((mcIdType)tinyTmp.size());
1876           tinyInt.insert(tinyInt.end(),tinyTmp.begin(),tinyTmp.end());
1877         }
1878     }
1879 }
1880
1881 void MEDFileUMeshAggregateCompute::unserialize(const std::string& name, DataArrayDouble *coo, std::vector<mcIdType>& tinyInt, std::vector< MCAuto<DataArrayIdType> >& bigArraysI)
1882 {
1883   mcIdType nbParts(tinyInt.back()); tinyInt.pop_back();
1884   _part_def.clear(); _part_def.resize(nbParts);
1885   _m_parts.clear(); _m_parts.resize(nbParts);
1886   for(mcIdType i=0;i<nbParts;i++)
1887     {
1888       INTERP_KERNEL::NormalizedCellType tp((INTERP_KERNEL::NormalizedCellType) tinyInt.back()); tinyInt.pop_back();
1889       MCAuto<MEDCoupling1GTUMesh> mesh(MEDCoupling1GTUMesh::New(name,tp));
1890       mesh->setCoords(coo);
1891       MEDCoupling1SGTUMesh *mesh1(dynamic_cast<MEDCoupling1SGTUMesh *>((MEDCoupling1GTUMesh *) mesh));
1892       MEDCoupling1DGTUMesh *mesh2(dynamic_cast<MEDCoupling1DGTUMesh *>((MEDCoupling1GTUMesh *) mesh));
1893       if(mesh1)
1894         {
1895           mesh1->setNodalConnectivity(bigArraysI.back()); bigArraysI.pop_back();
1896         }
1897       else if(mesh2)
1898         {
1899           MCAuto<DataArrayIdType> elt0,elt1;
1900           elt0=bigArraysI.back(); bigArraysI.pop_back();
1901           elt1=bigArraysI.back(); bigArraysI.pop_back();
1902           mesh2->setNodalConnectivity(elt0,elt1);
1903         }
1904       else
1905         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::unserialize : unrecognized single geo type mesh !");
1906       _m_parts[i]=mesh;
1907       mcIdType pdid(tinyInt.back()); tinyInt.pop_back();
1908       if(pdid!=-1)
1909         _part_def[i]=PartDefinition::Unserialize(tinyInt,bigArraysI);
1910       _mp_time=std::max(_mp_time,_m_time)+1;
1911     }
1912 }
1913
1914 /*!
1915  * This method returns true if \a this is stored split by type false if stored in a merged unstructured mesh.
1916  */
1917 bool MEDFileUMeshAggregateCompute::isStoredSplitByType() const
1918 {
1919   return _mp_time>=_m_time;
1920 }
1921
1922 std::size_t MEDFileUMeshAggregateCompute::getTimeOfThis() const
1923 {
1924   if(_mp_time>_m_time)
1925     return getTimeOfParts();
1926   if(_m_time>_mp_time)
1927     return getTimeOfUMesh();
1928   return std::max(getTimeOfParts(),getTimeOfUMesh());
1929 }
1930
1931 std::size_t MEDFileUMeshAggregateCompute::getTimeOfParts() const
1932 {
1933   std::size_t ret(0);
1934   for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1935     {
1936       const MEDCoupling1GTUMesh *elt(*it);
1937       if(!elt)
1938         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getTimeOfParts : null obj in parts !");
1939       ret=std::max(ret,elt->getTimeOfThis());
1940     }
1941   if(ret==0)
1942     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getTimeOfParts : parts is empty !");
1943   return ret;
1944 }
1945
1946 std::size_t MEDFileUMeshAggregateCompute::getTimeOfUMesh() const
1947 {
1948   const MEDCouplingUMesh *m(_m);
1949   if(!m)
1950     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getTimeOfUMesh : unmesh is null !");
1951   return m->getTimeOfThis();
1952 }
1953
1954 std::size_t MEDFileUMeshAggregateCompute::getHeapMemorySizeWithoutChildren() const
1955 {
1956   std::size_t ret(_m_parts.size()*sizeof(MCAuto<MEDCoupling1GTUMesh>));
1957   return ret;
1958 }
1959
1960 std::vector<const BigMemoryObject *> MEDFileUMeshAggregateCompute::getDirectChildrenWithNull() const
1961 {
1962   std::vector<const BigMemoryObject *> ret;
1963   for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1964     ret.push_back((const MEDCoupling1GTUMesh *)*it);
1965   ret.push_back((const MEDCouplingUMesh *)_m);
1966   return ret;
1967 }
1968
1969 MEDFileUMeshAggregateCompute MEDFileUMeshAggregateCompute::deepCopy(DataArrayDouble *coords) const
1970 {
1971   MEDFileUMeshAggregateCompute ret;
1972   ret._m_parts.resize(_m_parts.size());
1973   for(std::size_t i=0;i<_m_parts.size();i++)
1974     {
1975       const MEDCoupling1GTUMesh *elt(_m_parts[i]);
1976       if(elt)
1977         {
1978           ret._m_parts[i]=static_cast<MEDCoupling::MEDCoupling1GTUMesh*>(elt->deepCopy());
1979           ret._m_parts[i]->setCoords(coords);
1980         }
1981     }
1982   ret._mp_time=_mp_time; ret._m_time=_m_time;
1983   if((const MEDCouplingUMesh *)_m)
1984     {
1985       ret._m=static_cast<MEDCoupling::MEDCouplingUMesh*>(_m->deepCopy());
1986       ret._m->setCoords(coords);
1987     }
1988   std::size_t sz(_part_def.size());
1989   ret._part_def.clear(); ret._part_def.resize(sz);
1990   for(std::size_t i=0;i<sz;i++)
1991     {
1992       const PartDefinition *elt(_part_def[i]);
1993       if(elt)
1994         ret._part_def[i]=elt->deepCopy();
1995     }
1996   return ret;
1997 }
1998
1999 void MEDFileUMeshAggregateCompute::shallowCpyMeshes()
2000 {
2001   for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::iterator it=_m_parts.begin();it!=_m_parts.end();it++)
2002     {
2003       const MEDCoupling1GTUMesh *elt(*it);
2004       if(elt)
2005         {
2006           MCAuto<MEDCouplingMesh> elt2(elt->clone(false));
2007           *it=DynamicCastSafe<MEDCouplingMesh,MEDCoupling1GTUMesh>(elt2);
2008         }
2009     }
2010   const MEDCouplingUMesh *m(_m);
2011   if(m)
2012     _m=m->clone(false);
2013 }
2014
2015 bool MEDFileUMeshAggregateCompute::isEqual(const MEDFileUMeshAggregateCompute& other, double eps, std::string& what) const
2016 {
2017   const MEDCouplingUMesh *m1(getUmesh());
2018   const MEDCouplingUMesh *m2(other.getUmesh());
2019   if((m1==0 && m2!=0) || (m1!=0 && m2==0))
2020     {
2021       what="Presence of mesh in one sublevel and not in other!";
2022       return false;
2023     }
2024   if(m1)
2025     {
2026       std::string what2;
2027       if(!m1->isEqualIfNotWhy(m2,eps,what2))
2028         {
2029           what=std::string("meshes at a sublevel are not deeply equal (")+what2+std::string(")!");
2030           return false;
2031         }
2032     }
2033   std::size_t sz(_part_def.size());
2034   if(sz!=other._part_def.size())
2035     {
2036       what=std::string("number of subdivision per geo type for part definition is not the same !");
2037       return false;
2038     }
2039   for(std::size_t i=0;i<sz;i++)
2040     {
2041       const PartDefinition *pd0(_part_def[i]),*pd1(other._part_def[i]);
2042       if(!pd0 && !pd1)
2043         continue;
2044       if((!pd0 && pd1) || (pd0 && !pd1))
2045         {
2046           what=std::string("a cell part def is defined only for one among this or other !");
2047           return false;
2048         }
2049       bool ret(pd0->isEqual(pd1,what));
2050       if(!ret)
2051         return false;
2052     }
2053   return true;
2054 }
2055
2056 void MEDFileUMeshAggregateCompute::checkConsistency() const
2057 {
2058   if(_mp_time >= _m_time)
2059     for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();
2060         it!=_m_parts.end(); it++)
2061       (*it)->checkConsistency();
2062   else
2063     _m->checkConsistency();
2064 }
2065
2066 void MEDFileUMeshAggregateCompute::clearNonDiscrAttributes() const
2067 {
2068   for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
2069     MEDFileUMeshSplitL1::ClearNonDiscrAttributes(*it);
2070   MEDFileUMeshSplitL1::ClearNonDiscrAttributes(_m);
2071 }
2072
2073 void MEDFileUMeshAggregateCompute::synchronizeTinyInfo(const MEDFileMesh& master) const
2074 {
2075   for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
2076     {
2077       const MEDCoupling1GTUMesh *tmp(*it);
2078       if(tmp)
2079         {
2080           (const_cast<MEDCoupling1GTUMesh *>(tmp))->setName(master.getName().c_str());
2081           (const_cast<MEDCoupling1GTUMesh *>(tmp))->setDescription(master.getDescription().c_str());
2082           (const_cast<MEDCoupling1GTUMesh *>(tmp))->setTime(master.getTimeValue(),master.getIteration(),master.getOrder());
2083           (const_cast<MEDCoupling1GTUMesh *>(tmp))->setTimeUnit(master.getTimeUnit());
2084         }
2085     }
2086   const MEDCouplingUMesh *m(_m);
2087   if(m)
2088     {
2089       (const_cast<MEDCouplingUMesh *>(m))->setName(master.getName().c_str());
2090       (const_cast<MEDCouplingUMesh *>(m))->setDescription(master.getDescription().c_str());
2091       (const_cast<MEDCouplingUMesh *>(m))->setTime(master.getTimeValue(),master.getIteration(),master.getOrder());
2092       (const_cast<MEDCouplingUMesh *>(m))->setTimeUnit(master.getTimeUnit());
2093     }
2094 }
2095
2096 bool MEDFileUMeshAggregateCompute::empty() const
2097 {
2098   if(_mp_time<_m_time)
2099     return ((const MEDCouplingUMesh *)_m)==0;
2100   //else _mp_time>=_m_time)
2101   return _m_parts.empty();
2102 }
2103
2104 int MEDFileUMeshAggregateCompute::getMeshDimension() const
2105 {
2106   if(_mp_time<_m_time)
2107     {
2108       const MEDCouplingUMesh *m(_m);
2109       if(!m)
2110         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getMeshDimension : no umesh in this !");
2111       return m->getMeshDimension();
2112     }
2113   else
2114     {
2115       if(_m_parts.empty())
2116         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getMeshDimension : part mesh is empty !");
2117       const MEDCoupling1GTUMesh *m(_m_parts[0]);
2118       if(!m)
2119         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getMeshDimension : part mesh contains null instance !");
2120       return m->getMeshDimension();
2121     }
2122 }
2123
2124 std::vector<mcIdType> MEDFileUMeshAggregateCompute::getDistributionOfTypes() const
2125 {
2126   if(_mp_time<_m_time)
2127     {
2128       const MEDCouplingUMesh *m(_m);
2129       if(!m)
2130         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getDistributionOfTypes : no umesh in this !");
2131       return m->getDistributionOfTypes();
2132     }
2133   else
2134     {
2135       std::vector<mcIdType> ret;
2136       for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
2137         {
2138           const MEDCoupling1GTUMesh *tmp(*it);
2139           if(!tmp)
2140             throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getDistributionOfTypes : part mesh contains null instance !");
2141           std::vector<mcIdType> ret0(tmp->getDistributionOfTypes());
2142           ret.insert(ret.end(),ret0.begin(),ret0.end());
2143         }
2144       return ret;
2145     }
2146 }
2147
2148 mcIdType MEDFileUMeshAggregateCompute::getSize() const
2149 {
2150   if(_mp_time<_m_time)
2151     {
2152       const MEDCouplingUMesh *m(_m);
2153       if(!m)
2154         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getSize : no umesh in this !");
2155       return m->getNumberOfCells();
2156     }
2157   else
2158     {
2159       mcIdType ret=0;
2160       for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
2161         {
2162           const MEDCoupling1GTUMesh *m(*it);
2163           if(!m)
2164             throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getSize : part mesh contains null instance !");
2165           ret+=m->getNumberOfCells();
2166         }
2167       return ret;
2168     }
2169 }
2170
2171 void MEDFileUMeshAggregateCompute::setCoords(DataArrayDouble *coords)
2172 {
2173   for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::iterator it=_m_parts.begin();it!=_m_parts.end();it++)
2174     {
2175       MEDCoupling1GTUMesh *tmp(*it);
2176       if(tmp)
2177         (*it)->setCoords(coords);
2178     }
2179   MEDCouplingUMesh *m(_m);
2180   if(m)
2181     m->setCoords(coords);
2182 }
2183
2184 MEDFileEltStruct4Mesh *MEDFileEltStruct4Mesh::New(med_idt fid, const std::string& mName, int dt, int it, int iterOnStEltOfMesh, MEDFileMeshReadSelector *mrs)
2185 {
2186   return new MEDFileEltStruct4Mesh(fid,mName,dt,it,iterOnStEltOfMesh,mrs);
2187 }
2188
2189 std::size_t MEDFileEltStruct4Mesh::getHeapMemorySizeWithoutChildren() const
2190 {
2191   return _geo_type_name.capacity()+_vars.capacity()*sizeof(MCAuto<DataArray>);
2192 }
2193
2194 std::vector<const MEDCoupling::BigMemoryObject*> MEDFileEltStruct4Mesh::getDirectChildrenWithNull() const
2195 {
2196   std::vector<const MEDCoupling::BigMemoryObject*> ret;
2197   ret.push_back(_conn);
2198   ret.push_back(_common);
2199   for(std::vector< MCAuto<DataArray> >::const_iterator it=_vars.begin();it!=_vars.end();it++)
2200     ret.push_back(*it);
2201   return ret;
2202 }
2203
2204 MEDFileEltStruct4Mesh::MEDFileEltStruct4Mesh(med_idt fid, const std::string& mName, int dt, int it, int iterOnStEltOfMesh, MEDFileMeshReadSelector *mrs)
2205 {
2206   med_geometry_type geoType;
2207   INTERP_KERNEL::AutoPtr<char> geoTypeName(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE));
2208   MEDFILESAFECALLERRD0(MEDmeshEntityInfo,(fid,mName.c_str(),dt,it,MED_STRUCT_ELEMENT,iterOnStEltOfMesh+1,geoTypeName,&geoType));
2209   _geo_type=geoType;
2210   _geo_type_name=MEDLoaderBase::buildStringFromFortran(geoTypeName,MED_NAME_SIZE);
2211   mcIdType nCells(0);
2212   {
2213     med_bool chgt=MED_FALSE,trsf=MED_FALSE;
2214     nCells=MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_STRUCT_ELEMENT,geoType,MED_CONNECTIVITY,MED_NODAL,&chgt,&trsf);
2215   }
2216   MCAuto<MEDFileMeshSupports> mss(MEDFileMeshSupports::New(fid));
2217   MCAuto<MEDFileStructureElements> mse(MEDFileStructureElements::New(fid,mss));
2218   mcIdType nbEntities(mse->getNumberOfNodesPerSE(_geo_type_name));
2219   MCAuto<DataArrayMedInt> miConn=DataArrayMedInt::New(); miConn->alloc(nCells*nbEntities);
2220   MEDFILESAFECALLERRD0(MEDmeshElementConnectivityRd,(fid,mName.c_str(),dt,it,MED_STRUCT_ELEMENT,_geo_type,MED_NODAL,MED_FULL_INTERLACE,miConn->getPointer()));
2221   _conn=FromMedIntArray<mcIdType>(miConn);
2222   _conn->applyLin(1,-1);
2223   _conn->rearrange(nbEntities);
2224   _common=MEDFileUMeshPerTypeCommon::New();
2225   _common->loadCommonPart(fid,mName.c_str(),dt,it,nCells,geoType,MED_STRUCT_ELEMENT,mrs);
2226   std::vector<std::string> vns(mse->getVarAttsOf(_geo_type_name));
2227   std::size_t sz(vns.size());
2228   _vars.resize(sz);
2229   for(std::size_t i=0;i<sz;i++)
2230     {
2231       const MEDFileSEVarAtt *var(mse->getVarAttOf(_geo_type_name,vns[i]));
2232       MCAuto<DataArray> gen(var->getGenerator());
2233       MCAuto<DataArray> arr(gen->buildNewEmptyInstance());
2234       arr->alloc(nCells,var->getNbOfComponents());
2235       arr->setName(vns[i]);
2236       MEDFILESAFECALLERRD0(MEDmeshStructElementVarAttRd,(fid,mName.c_str(),dt,it,_geo_type,vns[i].c_str(),arr->getVoidStarPointer()));
2237       _vars[i]=arr;
2238     }
2239 }