Salome HOME
[EDF29571] : Fix problem of performance of MEDCoupling1GTUMesh::getCellsContainingPoints
[tools/medcoupling.git] / MEDLoader / MEDFileMeshLL.cxx
1 // Copyright (C) 2007-2020  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   mcIdType nMin(ToIdType(std::distance(fetchedNodeIds.begin(),std::find(fetchedNodeIds.begin(),fetchedNodeIds.end(),true))));
564   mcIdType nMax(ToIdType(std::distance(fetchedNodeIds.rbegin(),std::find(fetchedNodeIds.rbegin(),fetchedNodeIds.rend(),true))));
565   nMax=nCoords-nMax;
566   for(std::vector< std::vector< MCAuto<MEDFileUMeshPerType> > >::const_iterator it0=_per_type_mesh.begin();it0!=_per_type_mesh.end();it0++)
567     for(std::vector< MCAuto<MEDFileUMeshPerType> >::const_iterator it1=(*it0).begin();it1!=(*it0).end();it1++)
568       (*it1)->getMesh()->renumberNodesWithOffsetInConn(-nMin);
569   loadPartCoords(fid,infosOnComp,mName,dt,it,nMin,nMax);
570 }
571
572 void MEDFileUMeshL2::loadConnectivity(med_idt fid, int mdim, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs)
573 {
574   _per_type_mesh.resize(1);
575   _per_type_mesh[0].clear();
576   for(int j=0;j<MED_N_CELL_FIXED_GEO;j++)
577     {
578       MEDFileUMeshPerType *tmp(MEDFileUMeshPerType::New(fid,mName.c_str(),dt,it,mdim,typmai[j],typmai2[j],mrs));
579       if(tmp)
580         _per_type_mesh[0].push_back(tmp);
581     }
582   sortTypes();
583 }
584
585 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)
586 {
587   std::size_t nbOfTypes(types.size());
588   if(slicPerTyp.size()!=3*nbOfTypes)
589     throw INTERP_KERNEL::Exception("MEDFileUMeshL2::loadPartOfConnectivity : The size of slicPerTyp array is expected to be equal to 3 times size of array types !");
590   std::set<INTERP_KERNEL::NormalizedCellType> types2(types.begin(),types.end());
591   if(types2.size()!=nbOfTypes)
592     throw INTERP_KERNEL::Exception("MEDFileUMeshL2::loadPartOfConnectivity : the geometric types in types array must appear once !");
593   _per_type_mesh.resize(1);
594   _per_type_mesh[0].clear();
595   for(std::size_t ii=0;ii<nbOfTypes;ii++)
596     {
597       mcIdType strt(slicPerTyp[3*ii+0]),stp(slicPerTyp[3*ii+1]),step(slicPerTyp[3*ii+2]);
598       MCAuto<MEDFileUMeshPerType> tmp(MEDFileUMeshPerType::NewPart(fid,mName.c_str(),dt,it,mdim,types[ii],strt,stp,step,mrs));
599       _per_type_mesh[0].push_back(tmp);
600     }
601   sortTypes();
602 }
603
604 void MEDFileUMeshL2::loadCoords(med_idt fid, const std::vector<std::string>& infosOnComp, const std::string& mName, int dt, int it)
605 {
606   int spaceDim((int)infosOnComp.size());
607   med_bool changement,transformation;
608   med_int nCoords(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation));
609   _coords=DataArrayDouble::New();
610   _coords->alloc(nCoords,spaceDim);
611   double *coordsPtr(_coords->getPointer());
612   if (nCoords)
613     MEDFILESAFECALLERRD0(MEDmeshNodeCoordinateRd,(fid,mName.c_str(),dt,it,MED_FULL_INTERLACE,coordsPtr));
614   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
615     {
616       MCAuto<DataArrayMedInt> miFamCoord=DataArrayMedInt::New();
617       miFamCoord->alloc(nCoords,1);
618       MEDFILESAFECALLERRD0(MEDmeshEntityFamilyNumberRd,(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,miFamCoord->getPointer()));
619       _fam_coords=FromMedIntArray<mcIdType>(miFamCoord);
620     }
621   else
622     _fam_coords=0;
623   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
624     {
625       MCAuto<DataArrayMedInt> miNumCoord=DataArrayMedInt::New();
626       miNumCoord->alloc(nCoords,1);
627       MEDFILESAFECALLERRD0(MEDmeshEntityNumberRd,(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,miNumCoord->getPointer()));
628       _num_coords=FromMedIntArray<mcIdType>(miNumCoord);
629     }
630   else
631     _num_coords=0;
632   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NAME,MED_NODAL,&changement,&transformation)>0)
633     {
634       _name_coords=DataArrayAsciiChar::New();
635       _name_coords->alloc(nCoords+1,MED_SNAME_SIZE);//not a bug to avoid the memory corruption due to last \0 at the end
636       MEDFILESAFECALLERRD0(MEDmeshEntityNameRd,(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,_name_coords->getPointer()));
637       _name_coords->reAlloc(nCoords);//not a bug to avoid the memory corruption due to last \0 at the end
638     }
639   else
640     _name_coords=0;
641   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_GLOBAL_NUMBER,MED_NODAL,&changement,&transformation)>0)
642     {
643       MCAuto<DataArrayMedInt> miNumCoord=DataArrayMedInt::New();
644       miNumCoord->alloc(nCoords,1);
645       MEDFILESAFECALLERRD0(MEDmeshGlobalNumberRd,(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,miNumCoord->getPointer()));
646       _global_num_coords=FromMedIntArray<mcIdType>(miNumCoord);
647     }
648   for(int i=0;i<spaceDim;i++)
649     _coords->setInfoOnComponent(i,infosOnComp[i]);
650 }
651
652 void MEDFileUMeshL2::LoadPartCoords(med_idt fid, const std::vector<std::string>& infosOnComp, const std::string& mName, int dt, int it, mcIdType nMin, mcIdType nMax,
653 MCAuto<DataArrayDouble>& _coords, MCAuto<PartDefinition>& _part_coords, MCAuto<DataArrayIdType>& _fam_coords, MCAuto<DataArrayIdType>& _num_coords, MCAuto<DataArrayAsciiChar>& _name_coords)
654 {
655   med_bool changement,transformation;
656   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));
657   _coords=DataArrayDouble::New();
658   mcIdType nbNodesToLoad(nMax-nMin);
659   _coords->alloc(nbNodesToLoad,spaceDim);
660   med_filter filter=MED_FILTER_INIT,filter2=MED_FILTER_INIT;
661   MEDfilterBlockOfEntityCr(fid,/*nentity*/nCoords,/*nvaluesperentity*/1,/*nconstituentpervalue*/spaceDim,
662                            MED_ALL_CONSTITUENT,MED_FULL_INTERLACE,MED_COMPACT_STMODE,MED_NO_PROFILE,
663                            /*start*/ToMedInt(nMin+1),/*stride*/1,/*count*/1,/*blocksize*/ToMedInt(nbNodesToLoad),
664                            /*lastblocksize=useless because count=1*/0,&filter);
665   MEDFILESAFECALLERRD0(MEDmeshNodeCoordinateAdvancedRd,(fid,mName.c_str(),dt,it,&filter,_coords->getPointer()));
666   _part_coords=PartDefinition::New(nMin,nMax,1);
667   MEDfilterClose(&filter);
668   MEDfilterBlockOfEntityCr(fid,nCoords,1,1,MED_ALL_CONSTITUENT,MED_FULL_INTERLACE,MED_COMPACT_STMODE,
669                            MED_NO_PROFILE,ToMedInt(nMin+1),1,1,ToMedInt(nbNodesToLoad),0,&filter2);
670   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
671     {
672       MCAuto<DataArrayMedInt> miFamCoord=DataArrayMedInt::New();
673       miFamCoord->alloc(nbNodesToLoad,1);
674       MEDFILESAFECALLERRD0(MEDmeshEntityAttributeAdvancedRd,(fid,mName.c_str(),MED_FAMILY_NUMBER,dt,it,MED_NODE,MED_NO_GEOTYPE,&filter2,miFamCoord->getPointer()));
675       _fam_coords=FromMedIntArray<mcIdType>(miFamCoord);
676     }
677   else
678     _fam_coords=nullptr;
679   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
680     {
681       MCAuto<DataArrayMedInt> miNumCoord=DataArrayMedInt::New();
682       miNumCoord->alloc(nbNodesToLoad,1);
683       MEDFILESAFECALLERRD0(MEDmeshEntityAttributeAdvancedRd,(fid,mName.c_str(),MED_NUMBER,dt,it,MED_NODE,MED_NO_GEOTYPE,&filter2,miNumCoord->getPointer()));
684       _num_coords=FromMedIntArray<mcIdType>(miNumCoord);
685     }
686   else
687     _num_coords=nullptr;
688   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NAME,MED_NODAL,&changement,&transformation)>0)
689     {
690       _name_coords=DataArrayAsciiChar::New();
691       _name_coords->alloc(nbNodesToLoad+1,MED_SNAME_SIZE);//not a bug to avoid the memory corruption due to last \0 at the end
692       MEDFILESAFECALLERRD0(MEDmeshEntityAttributeAdvancedRd,(fid,mName.c_str(),MED_NAME,dt,it,MED_NODE,MED_NO_GEOTYPE,&filter2,_name_coords->getPointer()));
693       _name_coords->reAlloc(nbNodesToLoad);//not a bug to avoid the memory corruption due to last \0 at the end
694     }
695   else
696     _name_coords=nullptr;
697   MEDfilterClose(&filter2);
698   _coords->setInfoOnComponents(infosOnComp);
699 }
700
701 void MEDFileUMeshL2::loadPartCoords(med_idt fid, const std::vector<std::string>& infosOnComp, const std::string& mName, int dt, int it, mcIdType nMin, mcIdType nMax)
702 {
703   LoadPartCoords(fid,infosOnComp,mName,dt,it,nMin,nMax,_coords,_part_coords,_fam_coords,_num_coords,_name_coords);
704 }
705
706 void MEDFileUMeshL2::sortTypes()
707 {
708   std::set<int> mdims;
709   std::vector< MCAuto<MEDFileUMeshPerType> > tmp(_per_type_mesh[0]);
710   _per_type_mesh.clear();
711   for(std::vector< MCAuto<MEDFileUMeshPerType> >::const_iterator it=tmp.begin();it!=tmp.end();it++)
712     mdims.insert((*it)->getDim());
713   if(mdims.empty())
714     return;
715   int mdim=*mdims.rbegin();
716   _per_type_mesh.resize(mdim+1);
717   for(int dim=mdim+1;dim!=0;dim--)
718     {
719       std::vector< MCAuto<MEDFileUMeshPerType> >& elt=_per_type_mesh[mdim+1-dim];
720       for(std::vector< MCAuto<MEDFileUMeshPerType> >::const_iterator it=tmp.begin();it!=tmp.end();it++)
721         if((*it)->getDim()==dim-1)
722           elt.push_back(*it);
723     }
724   // suppression of contiguous empty levels at the end of _per_type_mesh.
725   int nbOfUselessLev=0;
726   bool isFirst=true;
727   for(std::vector< std::vector< MCAuto<MEDFileUMeshPerType> > >::reverse_iterator it2=_per_type_mesh.rbegin();it2!=_per_type_mesh.rend();it2++)
728     {
729       if((*it2).empty() && isFirst)
730         {
731           nbOfUselessLev++;
732         }
733       else
734         isFirst=false;
735     }
736   _per_type_mesh.resize(_per_type_mesh.size()-nbOfUselessLev);
737 }
738
739 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)
740 {
741   if(!coords)
742     return ;
743   MEDFILESAFECALLERWR0(MEDmeshNodeCoordinateWr,(fid,mname.c_str(),dt,it,time,MED_FULL_INTERLACE,ToMedInt(coords->getNumberOfTuples()),coords->begin()));
744   if(famCoords)
745     MEDFILESAFECALLERWR0(MEDmeshEntityFamilyNumberWr,(fid,mname.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,ToMedInt(famCoords->getNumberOfTuples()),ToMedIntArray<mcIdType>(famCoords)->begin()));
746   if(numCoords)
747     MEDFILESAFECALLERWR0(MEDmeshEntityNumberWr,(fid,mname.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,ToMedInt(numCoords->getNumberOfTuples()),ToMedIntArray<mcIdType>(numCoords)->begin()));
748   if(nameCoords)
749     {
750       if(nameCoords->getNumberOfComponents()!=MED_SNAME_SIZE)
751         {
752           std::ostringstream oss; oss << " MEDFileUMeshL2::WriteCoords : expected a name field on nodes with number of components set to " << MED_SNAME_SIZE;
753           oss << " ! The array has " << nameCoords->getNumberOfComponents() << " components !";
754           throw INTERP_KERNEL::Exception(oss.str().c_str());
755         }
756       MEDFILESAFECALLERWR0(MEDmeshEntityNameWr,(fid,mname.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,ToMedInt(nameCoords->getNumberOfTuples()),nameCoords->begin()));
757     }
758   if(globalNumCoords)
759     MEDFILESAFECALLERWR0(MEDmeshGlobalNumberWr,(fid,mname.c_str(),dt,it,MED_NODE,MED_NONE,ToMedInt(globalNumCoords->getNumberOfTuples()),ToMedIntArray<mcIdType>(globalNumCoords)->begin()));
760 }
761
762 bool MEDFileUMeshL2::isFamDefinedOnLev(int levId) const
763 {
764   for(std::vector< MCAuto<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
765     if((*it)->getFam()==0)
766       return false;
767   return true;
768 }
769
770 bool MEDFileUMeshL2::isNumDefinedOnLev(int levId) const
771 {
772   for(std::vector< MCAuto<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
773     if((*it)->getNum()==0)
774       return false;
775   return true;
776 }
777
778 bool MEDFileUMeshL2::isNamesDefinedOnLev(int levId) const
779 {
780   for(std::vector< MCAuto<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
781     if((*it)->getNames()==0)
782       return false;
783   return true;
784 }
785
786 MEDFileCMeshL2::MEDFileCMeshL2():_ax_type(AX_CART)
787 {
788 }
789
790 void MEDFileCMeshL2::loadAll(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, int dt, int it)
791 {
792   _name.set(mName.c_str());
793   int nstep;
794   int Mdim;
795   MEDCoupling::MEDCouplingMeshType meshType;
796   MEDCoupling::MEDCouplingAxisType dummy3;
797   std::vector<std::string> infosOnComp(getAxisInfoOnMesh(fid,mId,mName.c_str(),meshType,dummy3,nstep,Mdim));
798   if(meshType!=CARTESIAN)
799     throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected a structured one whereas in file it is not a structured !");
800   _time=mId->checkMeshTimeStep(fid,mName,nstep,dt,it);
801   _iteration=dt;
802   _order=it;
803   //
804   med_grid_type gridtype;
805   MEDFILESAFECALLERRD0(MEDmeshGridTypeRd,(fid,mName.c_str(),&gridtype));
806   if(gridtype!=MED_CARTESIAN_GRID && gridtype!=MED_POLAR_GRID)
807     throw INTERP_KERNEL::Exception("Invalid rectilinear mesh ! Only cartesian and polar are supported !");
808   _ax_type=TraduceAxisTypeStruct(gridtype);
809   _cmesh=MEDCouplingCMesh::New();
810   for(int i=0;i<Mdim;i++)
811     {
812       med_data_type dataTypeReq=GetDataTypeCorrespondingToSpaceId(i);
813       med_bool chgt=MED_FALSE,trsf=MED_FALSE;
814       med_int nbOfElt(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NONE,dataTypeReq,MED_NO_CMODE,&chgt,&trsf));
815       MCAuto<DataArrayDouble> da=DataArrayDouble::New();
816       da->alloc(nbOfElt,1);
817       da->setInfoOnComponent(0,infosOnComp[i]);
818       MEDFILESAFECALLERRD0(MEDmeshGridIndexCoordinateRd,(fid,mName.c_str(),dt,it,i+1,da->getPointer()));
819       _cmesh->setCoordsAt(i,da);
820     }
821 }
822
823 med_data_type MEDFileCMeshL2::GetDataTypeCorrespondingToSpaceId(int id)
824 {
825   switch(id)
826   {
827     case 0:
828       return MED_COORDINATE_AXIS1;
829     case 1:
830       return MED_COORDINATE_AXIS2;
831     case 2:
832       return MED_COORDINATE_AXIS3;
833     default:
834       throw INTERP_KERNEL::Exception("Invalid meshdim detected in Cartesian Grid !");
835   }
836 }
837
838 MEDFileCLMeshL2::MEDFileCLMeshL2()
839 {
840 }
841
842 void MEDFileCLMeshL2::loadAll(med_idt fid, const MeshOrStructMeshCls *mId, const std::string& mName, int dt, int it)
843 {
844   _name.set(mName.c_str());
845   int nstep;
846   int Mdim;
847   MEDCoupling::MEDCouplingMeshType meshType;
848   MEDCoupling::MEDCouplingAxisType dummy3;
849   std::vector<std::string> infosOnComp(getAxisInfoOnMesh(fid,mId,mName,meshType,dummy3,nstep,Mdim));
850   if(meshType!=CURVE_LINEAR)
851     throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected a structured one whereas in file it is not a structured !");
852   _time=mId->checkMeshTimeStep(fid,mName,nstep,dt,it);
853   _iteration=dt;
854   _order=it;
855   //
856   _clmesh=MEDCouplingCurveLinearMesh::New();
857   MCAuto<DataArrayMedInt> miStGrid=DataArrayMedInt::New();
858   miStGrid->alloc(Mdim,1);
859   MEDFILESAFECALLERRD0(MEDmeshGridStructRd,(fid,mName.c_str(),dt,it,miStGrid->getPointer()));
860   MCAuto<DataArrayIdType> stGrid=FromMedIntArray<mcIdType>(miStGrid);
861   _clmesh->setNodeGridStructure(stGrid->begin(),stGrid->end());
862   med_bool chgt=MED_FALSE,trsf=MED_FALSE;
863   med_int nbNodes(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&chgt,&trsf));
864   MCAuto<DataArrayDouble> da=DataArrayDouble::New();
865   da->alloc(nbNodes,infosOnComp.size());
866   da->setInfoOnComponents(infosOnComp);
867   MEDFILESAFECALLERRD0(MEDmeshNodeCoordinateRd,(fid,mName.c_str(),dt,it,MED_FULL_INTERLACE,da->getPointer()));
868   _clmesh->setCoords(da);
869 }
870
871 MEDFileUMeshPermCompute::MEDFileUMeshPermCompute(const MEDFileUMeshSplitL1* st):_st(st),_mpt_time(0),_num_time(0)
872 {
873 }
874
875 /*!
876  * Warning it returns an instance to deallocate !!!!
877  */
878 MEDFileUMeshPermCompute::operator MEDCouplingUMesh *() const
879 {
880   _st->_num->updateTime();
881   if((MEDCouplingUMesh *)_m==0)
882     {
883       updateTime();
884       _m=static_cast<MEDCouplingUMesh *>(_st->_m_by_types.getUmesh()->deepCopy());
885       _m->renumberCells(_st->_num->begin(),true);
886       return _m.retn();
887     }
888   else
889     {
890       if(_mpt_time==_st->_m_by_types.getTimeOfThis() && _num_time==_st->_num->getTimeOfThis())
891         return _m.retn();
892       else
893         {
894           updateTime();
895           _m=static_cast<MEDCouplingUMesh *>(_st->_m_by_types.getUmesh()->deepCopy());
896           _m->renumberCells(_st->_num->begin(),true);
897           return _m.retn();
898         }
899     }
900 }
901
902 void MEDFileUMeshPermCompute::operator=(MEDCouplingUMesh *m)
903 {
904   _m=m;
905 }
906
907 void MEDFileUMeshPermCompute::updateTime() const
908 {
909   _mpt_time=_st->_m_by_types.getTimeOfThis();
910   _num_time=_st->_num->getTimeOfThis();
911 }
912
913 std::vector<const BigMemoryObject *> MEDFileUMeshPermCompute::getDirectChildrenWithNull() const
914 {
915   std::vector<const BigMemoryObject *> ret;
916   ret.push_back((const MEDCouplingUMesh *)_m);
917   return ret;
918 }
919
920 std::size_t MEDFileUMeshPermCompute::getHeapMemorySizeWithoutChildren() const
921 {
922   return sizeof(MEDFileUMeshPermCompute);
923 }
924
925 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)
926 {
927 }
928
929 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(const MEDFileUMeshL2& l2, const std::string& mName, int id):_m(this)
930 {
931   const std::vector< MCAuto<MEDFileUMeshPerType> >& v=l2.getLev(id);
932   if(v.empty())
933     return;
934   std::size_t sz=v.size();
935   std::vector<const MEDCoupling1GTUMesh *> ms(sz);
936   std::vector<const DataArrayIdType *> fams(sz),nums(sz);
937   std::vector<const DataArrayChar *> names(sz);
938   std::vector<const PartDefinition *> pds(sz);
939   for(std::size_t i=0;i<sz;i++)
940     {
941       MEDCoupling1GTUMesh *elt(v[i]->getMesh());
942       MCAuto<DataArrayDouble> tmp2=l2.getCoords();
943       elt->setCoords(tmp2);
944       ms[i]=elt;
945       pds[i]=v[i]->getPartDef();
946     }
947   _m_by_types.assignParts(ms);
948   _m_by_types.assignDefParts(pds);
949   if(l2.isFamDefinedOnLev(id))
950     {
951       for(std::size_t i=0;i<sz;i++)
952         fams[i]=v[i]->getFam();
953       if(sz!=1)
954         _fam=DataArrayIdType::Aggregate(fams);
955       else
956         {
957           fams[0]->incrRef();
958           _fam=const_cast<DataArrayIdType *>(fams[0]);
959         }
960     }
961   if(l2.isNumDefinedOnLev(id))
962     {
963       for(std::size_t i=0;i<sz;i++)
964         nums[i]=v[i]->getNum();
965       if(sz!=1)
966         _num=DataArrayIdType::Aggregate(nums);
967       else
968         {
969           nums[0]->incrRef();
970           _num=const_cast<DataArrayIdType *>(nums[0]);
971         }
972       computeRevNum();
973     }
974   if(l2.isNamesDefinedOnLev(id))
975     {
976       for(std::size_t i=0;i<sz;i++)
977         names[i]=v[i]->getNames();
978       _names=dynamic_cast<DataArrayAsciiChar *>(DataArrayChar::Aggregate(names));
979     }
980 }
981
982 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCoupling1GTUMesh *m):_m(this)
983 {
984   std::vector< const MEDCoupling1GTUMesh * > v(1);
985   v[0]=m;
986   assignParts(v);
987 }
988
989 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCouplingUMesh *m):_m(this)
990 {
991   assignMesh(m,true);
992 }
993
994 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCouplingUMesh *m, bool newOrOld):_m(this)
995 {
996   assignMesh(m,newOrOld);
997 }
998
999 void MEDFileUMeshSplitL1::setName(const std::string& name)
1000 {
1001   _m_by_types.setName(name);
1002 }
1003
1004 std::size_t MEDFileUMeshSplitL1::getHeapMemorySizeWithoutChildren() const
1005 {
1006   return 0;
1007 }
1008
1009 std::vector<const BigMemoryObject *> MEDFileUMeshSplitL1::getDirectChildrenWithNull() const
1010 {
1011   std::vector<const BigMemoryObject *> ret;
1012   ret.push_back(&_m_by_types);
1013   ret.push_back(&_m);
1014   ret.push_back((const DataArrayIdType*)_fam);
1015   ret.push_back((const DataArrayIdType*)_num);
1016   ret.push_back((const DataArrayIdType*)_rev_num);
1017   ret.push_back((const DataArrayAsciiChar*)_names);
1018   return ret;
1019 }
1020
1021 MEDFileUMeshSplitL1 *MEDFileUMeshSplitL1::shallowCpyUsingCoords(DataArrayDouble *coords) const
1022 {
1023   MCAuto<MEDFileUMeshSplitL1> ret(new MEDFileUMeshSplitL1(*this));
1024   ret->_m_by_types.shallowCpyMeshes();
1025   ret->_m_by_types.setCoords(coords);
1026   return ret.retn();
1027 }
1028
1029 MEDFileUMeshSplitL1 *MEDFileUMeshSplitL1::deepCopy(DataArrayDouble *coords) const
1030 {
1031   MCAuto<MEDFileUMeshSplitL1> ret(new MEDFileUMeshSplitL1(*this));
1032   ret->_m_by_types=_m_by_types.deepCopy(coords);
1033   if((const DataArrayIdType *)_fam)
1034     ret->_fam=_fam->deepCopy();
1035   if((const DataArrayIdType *)_num)
1036     ret->_num=_num->deepCopy();
1037   if((const DataArrayIdType *)_rev_num)
1038     ret->_rev_num=_rev_num->deepCopy();
1039   if((const DataArrayAsciiChar *)_names)
1040     ret->_names=_names->deepCopy();
1041   return ret.retn();
1042 }
1043
1044 void MEDFileUMeshSplitL1::checkConsistency() const
1045 {
1046   if (!_fam || _fam->getNumberOfTuples() != getSize())
1047     throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::checkConsistency(): internal family array has an invalid size!");
1048   mcIdType nbCells = getSize();
1049   if (_num)
1050     {
1051       _num->checkNbOfTuplesAndComp(nbCells,1,"MEDFileUMeshSplitL1::checkConsistency(): inconsistent internal node numbering array!");
1052       mcIdType pos;
1053       mcIdType maxValue=_num->getMaxValue(pos);
1054       if (!_rev_num || _rev_num->getNumberOfTuples() != (maxValue+1))
1055         throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::checkConsistency(): inconsistent internal revert node numbering array!");
1056     }
1057   if ((_num && !_rev_num) || (!_num && _rev_num))
1058     throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::checkConsistency(): inconsistent internal numbering arrays (one is null)!");
1059   if (_num && !_num->hasUniqueValues())
1060     throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::checkConsistency(): inconsistent internal node numbering array: duplicates found!");
1061   if (_names)
1062     _names->checkNbOfTuplesAndComp(nbCells,1,"MEDFileUMeshSplitL1::checkConsistency(): internal cell naming array has an invalid size!");
1063
1064   _m_by_types.checkConsistency();
1065 }
1066
1067 bool MEDFileUMeshSplitL1::isEqual(const MEDFileUMeshSplitL1 *other, double eps, std::string& what) const
1068 {
1069   if(!_m_by_types.isEqual(other->_m_by_types,eps,what))
1070     return false;
1071   const DataArrayIdType *d1=_fam;
1072   const DataArrayIdType *d2=other->_fam;
1073   if((d1==0 && d2!=0) || (d1!=0 && d2==0))
1074     {
1075       what="Presence of family arr in one sublevel and not in other!";
1076       return false;
1077     }
1078   if(d1)
1079     if(!d1->isEqual(*d2))
1080       {
1081         what="family arr at a sublevel are not deeply equal !";
1082         return false;
1083       }
1084   d1=_num;
1085   d2=other->_num;
1086   if((d1==0 && d2!=0) || (d1!=0 && d2==0))
1087     {
1088       what="Presence of cell numbering arr in one sublevel and not in other!";
1089       return false;
1090     }
1091   if(d1)
1092     if(!d1->isEqual(*d2))
1093       {
1094         what="Numbering cell arr at a sublevel are not deeply equal !";
1095         return false;
1096       }
1097   const DataArrayAsciiChar *e1=_names;
1098   const DataArrayAsciiChar *e2=other->_names;
1099   if((e1==0 && e2!=0) || (e1!=0 && e2==0))
1100     {
1101       what="Presence of cell names arr in one sublevel and not in other!";
1102       return false;
1103     }
1104   if(e1)
1105     if(!e1->isEqual(*e2))
1106       {
1107         what="Name cell arr at a sublevel are not deeply equal !";
1108         return false;
1109       }
1110   return true;
1111 }
1112
1113 void MEDFileUMeshSplitL1::synchronizeTinyInfo(const MEDFileMesh& master) const
1114 {
1115   _m_by_types.synchronizeTinyInfo(master);
1116 }
1117
1118 void MEDFileUMeshSplitL1::clearNonDiscrAttributes() const
1119 {
1120   _m_by_types.clearNonDiscrAttributes();
1121 }
1122
1123 void MEDFileUMeshSplitL1::ClearNonDiscrAttributes(const MEDCouplingMesh *tmp)
1124 {
1125   if(!tmp)
1126     return ;
1127   (const_cast<MEDCouplingMesh *>(tmp))->setName("");
1128   (const_cast<MEDCouplingMesh *>(tmp))->setDescription("");
1129   (const_cast<MEDCouplingMesh *>(tmp))->setTime(0.,-1,-1);
1130   (const_cast<MEDCouplingMesh *>(tmp))->setTimeUnit("");
1131 }
1132
1133 void MEDFileUMeshSplitL1::setCoords(DataArrayDouble *coords)
1134 {
1135   _m_by_types.setCoords(coords);
1136 }
1137
1138 void MEDFileUMeshSplitL1::assignMesh(MEDCouplingUMesh *m, bool newOrOld)
1139 {
1140   if(newOrOld)
1141     {
1142       m->incrRef();
1143       _m=m;
1144       _m_by_types.assignUMesh(dynamic_cast<MEDCouplingUMesh *>(m->deepCopy()));
1145       MCAuto<DataArrayIdType> da=_m_by_types.getUmesh()->getRenumArrForConsecutiveCellTypesSpec(typmai2,typmai2+MED_N_CELL_FIXED_GEO);
1146       if(!da->isIota(m->getNumberOfCells()))
1147         {
1148           _num=da->invertArrayO2N2N2O(m->getNumberOfCells());
1149           _m.updateTime();
1150           computeRevNum();
1151           _m_by_types.getUmesh()->renumberCells(da->begin(),false);
1152         }
1153     }
1154   else
1155     {
1156       if(!m->checkConsecutiveCellTypesAndOrder(typmai2,typmai2+MED_N_CELL_FIXED_GEO))
1157         throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::assignMesh(): the mesh does not follow the MED file numbering convention! Invoke sortCellsInMEDFileFrmt() first!");
1158       m->incrRef();
1159       _m_by_types.assignUMesh(m);
1160     }
1161   assignCommonPart();
1162 }
1163
1164 void MEDFileUMeshSplitL1::forceComputationOfParts() const
1165 {
1166   _m_by_types.forceComputationOfPartsFromUMesh();
1167 }
1168
1169 void MEDFileUMeshSplitL1::assignParts(const std::vector< const MEDCoupling1GTUMesh * >& mParts)
1170 {
1171   _m_by_types.assignParts(mParts);
1172   assignCommonPart();
1173 }
1174
1175 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1():_m(this)
1176 {
1177 }
1178
1179 void MEDFileUMeshSplitL1::assignCommonPart()
1180 {
1181   _fam=DataArrayIdType::New();
1182   _fam->alloc(_m_by_types.getSize(),1);
1183   _fam->fillWithValue(0);
1184 }
1185
1186 bool MEDFileUMeshSplitL1::empty() const
1187 {
1188   return _m_by_types.empty();
1189 }
1190
1191 bool MEDFileUMeshSplitL1::presenceOfOneFams(const std::vector<mcIdType>& ids) const
1192 {
1193   const DataArrayIdType *fam=_fam;
1194   if(!fam)
1195     return false;
1196   return fam->presenceOfValue(ids);
1197 }
1198
1199 int MEDFileUMeshSplitL1::getMeshDimension() const
1200 {
1201   return _m_by_types.getMeshDimension();
1202 }
1203
1204 void MEDFileUMeshSplitL1::simpleRepr(std::ostream& oss) const
1205 {
1206   std::vector<mcIdType> code=_m_by_types.getDistributionOfTypes();
1207   std::size_t nbOfTypes=code.size()/3;
1208   for(std::size_t i=0;i<nbOfTypes;i++)
1209     {
1210       INTERP_KERNEL::NormalizedCellType typ=(INTERP_KERNEL::NormalizedCellType) code[3*i];
1211       oss << "    - Number of cells with type " << INTERP_KERNEL::CellModel::GetCellModel(typ).getRepr() << " : " << code[3*i+1] << std::endl;
1212     }
1213 }
1214
1215 mcIdType MEDFileUMeshSplitL1::getSize() const
1216 {
1217   return _m_by_types.getSize();
1218 }
1219
1220 MEDCouplingUMesh *MEDFileUMeshSplitL1::getFamilyPart(const mcIdType *idsBg, const mcIdType *idsEnd, bool renum) const
1221 {
1222   MCAuto<DataArrayIdType> eltsToKeep=_fam->findIdsEqualList(idsBg,idsEnd);
1223   MEDCouplingUMesh *m=(MEDCouplingUMesh *)_m_by_types.getUmesh()->buildPartOfMySelf(eltsToKeep->begin(),eltsToKeep->end(),true);
1224   if(renum)
1225     return renumIfNeeded(m,eltsToKeep->begin());
1226   return m;
1227 }
1228
1229 DataArrayIdType *MEDFileUMeshSplitL1::getFamilyPartArr(const mcIdType *idsBg, const mcIdType *idsEnd, bool renum) const
1230 {
1231   MCAuto<DataArrayIdType> da=_fam->findIdsEqualList(idsBg,idsEnd);
1232   if(renum)
1233     return renumIfNeededArr(da);
1234   return da.retn();
1235 }
1236
1237 std::vector<INTERP_KERNEL::NormalizedCellType> MEDFileUMeshSplitL1::getGeoTypes() const
1238 {
1239   return _m_by_types.getGeoTypes();
1240 }
1241
1242 mcIdType MEDFileUMeshSplitL1::getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType ct) const
1243 {
1244   return _m_by_types.getNumberOfCellsWithType(ct);
1245 }
1246
1247 MEDCouplingUMesh *MEDFileUMeshSplitL1::getWholeMesh(bool renum) const
1248 {
1249   MCAuto<MEDCouplingUMesh> tmp;
1250   if(renum && ((const DataArrayIdType *)_num))
1251     tmp=_m;
1252   else
1253     { tmp=_m_by_types.getUmesh(); if(tmp) tmp->incrRef(); }
1254   return tmp.retn();
1255 }
1256
1257 mcIdType MEDFileUMeshSplitL1::getNumberOfCells() const
1258 {
1259   return _m_by_types.getNumberOfCells();
1260 }
1261
1262 DataArrayIdType *MEDFileUMeshSplitL1::extractFamilyFieldOnGeoType(INTERP_KERNEL::NormalizedCellType gt) const
1263 {
1264   const DataArrayIdType *fam(_fam);
1265   if(!fam)
1266     return 0;
1267   mcIdType start(0),stop(0);
1268   _m_by_types.getStartStopOfGeoTypeWithoutComputation(gt,start,stop);
1269   return fam->selectByTupleIdSafeSlice(start,stop,1);
1270 }
1271
1272 DataArrayIdType *MEDFileUMeshSplitL1::extractNumberFieldOnGeoType(INTERP_KERNEL::NormalizedCellType gt) const
1273 {
1274   const DataArrayIdType *num(_num);
1275   if(!num)
1276     return 0;
1277   mcIdType start(0),stop(0);
1278   _m_by_types.getStartStopOfGeoTypeWithoutComputation(gt,start,stop);
1279   return num->selectByTupleIdSafeSlice(start,stop,1);
1280 }
1281
1282 DataArrayIdType *MEDFileUMeshSplitL1::getOrCreateAndGetFamilyField()
1283 {
1284   if((DataArrayIdType *)_fam)
1285     return _fam;
1286   mcIdType nbOfTuples=_m_by_types.getSize();
1287   _fam=DataArrayIdType::New(); _fam->alloc(nbOfTuples,1); _fam->fillWithZero();
1288   return _fam;
1289 }
1290
1291 const DataArrayIdType *MEDFileUMeshSplitL1::getFamilyField() const
1292 {
1293   return _fam;
1294 }
1295
1296 const DataArrayIdType *MEDFileUMeshSplitL1::getNumberField() const
1297 {
1298   return _num;
1299 }
1300
1301 const DataArrayIdType *MEDFileUMeshSplitL1::getRevNumberField() const
1302 {
1303   return _rev_num;
1304 }
1305
1306 const DataArrayAsciiChar *MEDFileUMeshSplitL1::getNameField() const
1307 {
1308   return _names;
1309 }
1310
1311 const PartDefinition *MEDFileUMeshSplitL1::getPartDef(INTERP_KERNEL::NormalizedCellType gt) const
1312 {
1313   return _m_by_types.getPartDefOfWithoutComputation(gt);
1314 }
1315
1316 void MEDFileUMeshSplitL1::eraseFamilyField()
1317 {
1318   _fam->fillWithZero();
1319 }
1320
1321 /*!
1322  * This method ignores _m and _m_by_types.
1323  */
1324 void MEDFileUMeshSplitL1::setGroupsFromScratch(const std::vector<const MEDCouplingUMesh *>& ms, std::map<std::string,mcIdType>& familyIds,
1325                                                std::map<std::string, std::vector<std::string> >& groups)
1326 {
1327   std::vector< DataArrayIdType * > corr;
1328   _m=MEDCouplingUMesh::FuseUMeshesOnSameCoords(ms,0,corr);
1329   std::vector< MCAuto<DataArrayIdType> > corrMSafe(corr.begin(),corr.end());
1330   std::vector< std::vector<mcIdType> > fidsOfGroups;
1331   std::vector< const DataArrayIdType * > corr2(corr.begin(),corr.end());
1332   _fam=DataArrayIdType::MakePartition(corr2,((MEDCouplingUMesh *)_m)->getNumberOfCells(),fidsOfGroups);
1333   mcIdType nbOfCells=((MEDCouplingUMesh *)_m)->getNumberOfCells();
1334   std::map<mcIdType,std::string> newfams;
1335   std::map<mcIdType,mcIdType> famIdTrad;
1336   TraduceFamilyNumber(fidsOfGroups,familyIds,famIdTrad,newfams);
1337   mcIdType *w=_fam->getPointer();
1338   for(mcIdType i=0;i<nbOfCells;i++,w++)
1339     *w=famIdTrad[*w];
1340 }
1341
1342 void MEDFileUMeshSplitL1::write(med_idt fid, const std::string& mName, int mdim) const
1343 {
1344   std::vector<MEDCoupling1GTUMesh *> ms(_m_by_types.getParts());
1345   mcIdType start=0;
1346   for(std::vector<MEDCoupling1GTUMesh *>::const_iterator it=ms.begin();it!=ms.end();it++)
1347     {
1348       mcIdType nbCells=(*it)->getNumberOfCells();
1349       mcIdType end=start+nbCells;
1350       MCAuto<DataArrayIdType> fam,num;
1351       MCAuto<DataArrayAsciiChar> names;
1352       if((const DataArrayIdType *)_fam)
1353         fam=_fam->subArray(start,end);
1354       if((const DataArrayIdType *)_num)
1355         num=_num->subArray(start,end);
1356       if((const DataArrayAsciiChar *)_names)
1357         names=static_cast<DataArrayAsciiChar *>(_names->subArray(start,end));
1358       MEDFileUMeshPerType::Write(fid,mName,mdim,(*it),fam,num,names);
1359       start=end;
1360     }
1361 }
1362
1363 void MEDFileUMeshSplitL1::renumberNodesInConn(const mcIdType *newNodeNumbersO2N)
1364 {
1365   _m_by_types.renumberNodesInConnWithoutComputation(newNodeNumbersO2N);
1366 }
1367
1368 void MEDFileUMeshSplitL1::serialize(std::vector<mcIdType>& tinyInt, std::vector< MCAuto<DataArrayIdType> >& bigArraysI) const
1369 {
1370   bigArraysI.push_back(_fam);
1371   bigArraysI.push_back(_num);
1372   _m_by_types.serialize(tinyInt,bigArraysI);
1373 }
1374
1375 void MEDFileUMeshSplitL1::unserialize(const std::string& name, DataArrayDouble *coo, std::vector<mcIdType>& tinyInt, std::vector< MCAuto<DataArrayIdType> >& bigArraysI)
1376 {
1377   _fam=bigArraysI.back(); bigArraysI.pop_back();
1378   _num=bigArraysI.back(); bigArraysI.pop_back();
1379   _m_by_types.unserialize(name,coo,tinyInt,bigArraysI);
1380 }
1381
1382 void MEDFileUMeshSplitL1::changeFamilyIdArr(mcIdType oldId, mcIdType newId)
1383 {
1384   DataArrayIdType *arr=_fam;
1385   if(arr)
1386     arr->changeValue(oldId,newId);
1387 }
1388
1389 void MEDFileUMeshSplitL1::setFamilyArr(DataArrayIdType *famArr)
1390 {
1391   if(!famArr)
1392     {
1393       _fam=0;
1394       return ;
1395     }
1396   mcIdType sz(_m_by_types.getSize());
1397   famArr->checkNbOfTuplesAndComp(sz,1,"MEDFileUMeshSplitL1::setFamilyArr : Problem in size of Family arr ! ");
1398   famArr->incrRef();
1399   _fam=famArr;
1400 }
1401
1402 DataArrayIdType *MEDFileUMeshSplitL1::getFamilyField()
1403 {
1404   return _fam;
1405 }
1406
1407 void MEDFileUMeshSplitL1::setRenumArr(DataArrayIdType *renumArr)
1408 {
1409   if(!renumArr)
1410     {
1411       _num=0;
1412       _rev_num=0;
1413       return ;
1414     }
1415   mcIdType sz(_m_by_types.getSize());
1416   renumArr->checkNbOfTuplesAndComp(sz,1,"MEDFileUMeshSplitL1::setRenumArr : Problem in size of numbering arr ! ");
1417   renumArr->incrRef();
1418   _num=renumArr;
1419   computeRevNum();
1420 }
1421
1422 void MEDFileUMeshSplitL1::setNameArr(DataArrayAsciiChar *nameArr)
1423 {
1424   if(!nameArr)
1425     {
1426       _names=0;
1427       return ;
1428     }
1429   mcIdType sz(_m_by_types.getSize());
1430   nameArr->checkNbOfTuplesAndComp(sz,MED_SNAME_SIZE,"MEDFileUMeshSplitL1::setNameArr : Problem in size of name arr ! ");
1431   nameArr->incrRef();
1432   _names=nameArr;
1433 }
1434
1435 MEDCouplingUMesh *MEDFileUMeshSplitL1::Renumber2(const DataArrayIdType *renum, MEDCouplingUMesh *m, const mcIdType *cellIds)
1436 {
1437   if(renum==0)
1438     return m;
1439   if(cellIds==0)
1440     m->renumberCells(renum->begin(),true);
1441   else
1442     {
1443       MCAuto<DataArrayIdType> locnum=renum->selectByTupleId(cellIds,cellIds+m->getNumberOfCells());
1444       m->renumberCells(locnum->begin(),true);
1445     }
1446   return m;
1447 }
1448
1449 MEDFileUMeshSplitL1 *MEDFileUMeshSplitL1::Unserialize(const std::string& name, DataArrayDouble *coo, std::vector<mcIdType>& tinyInt, std::vector< MCAuto<DataArrayIdType> >& bigArraysI)
1450 {
1451   MCAuto<MEDFileUMeshSplitL1> ret(new MEDFileUMeshSplitL1);
1452   ret->unserialize(name,coo,tinyInt,bigArraysI);
1453   return ret.retn();
1454 }
1455
1456 MEDCouplingUMesh *MEDFileUMeshSplitL1::renumIfNeeded(MEDCouplingUMesh *m, const mcIdType *cellIds) const
1457 {
1458   return Renumber2(_num,m,cellIds);
1459 }
1460
1461 DataArrayIdType *MEDFileUMeshSplitL1::Renumber(const DataArrayIdType *renum, const DataArrayIdType *da)
1462 {
1463   if((const DataArrayIdType *)renum==0)
1464     {
1465       da->incrRef();
1466       return const_cast<DataArrayIdType *>(da);
1467     }
1468   return renum->selectByTupleId(da->begin(),da->end());
1469 }
1470
1471 DataArrayIdType *MEDFileUMeshSplitL1::renumIfNeededArr(const DataArrayIdType *da) const
1472 {
1473   return Renumber(_num,da);
1474 }
1475
1476 std::vector<mcIdType> MEDFileUMeshSplitL1::GetNewFamiliesNumber(mcIdType nb, const std::map<std::string,mcIdType>& families)
1477 {
1478   mcIdType id=-1;
1479   for(std::map<std::string,mcIdType>::const_iterator it=families.begin();it!=families.end();it++)
1480     id=std::max(id,(*it).second);
1481   if(id==-1)
1482     id=0;
1483   std::vector<mcIdType> ret(nb);
1484   for(mcIdType i=1;i<=nb;i++)
1485     ret[i]=id+i;
1486   return ret;
1487 }
1488
1489 void MEDFileUMeshSplitL1::TraduceFamilyNumber(const std::vector< std::vector<mcIdType> >& fidsGrps, std::map<std::string,mcIdType>& familyIds,
1490                                               std::map<mcIdType,mcIdType>& famIdTrad, std::map<mcIdType,std::string>& newfams)
1491 {
1492   std::set<mcIdType> allfids;
1493   //tony
1494 }
1495
1496 void MEDFileUMeshSplitL1::computeRevNum() const
1497 {
1498   mcIdType pos;
1499   mcIdType maxValue=_num->getMaxValue(pos);
1500   _rev_num=_num->invertArrayN2O2O2N(maxValue+1);
1501 }
1502
1503 //=
1504
1505 MEDFileUMeshAggregateCompute::MEDFileUMeshAggregateCompute():_mp_time(0),_m_time(0)
1506 {
1507 }
1508
1509 void MEDFileUMeshAggregateCompute::setName(const std::string& name)
1510 {
1511   if(_m_time>=_mp_time)
1512     {
1513       MEDCouplingUMesh *um(_m);
1514       if(um)
1515         um->setName(name);
1516     }
1517   if(_mp_time>=_m_time)
1518     {
1519       for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1520         {
1521           MEDCoupling1GTUMesh *tmp(*it);
1522           if(tmp)
1523             tmp->setName(name);
1524         }
1525     }
1526 }
1527
1528 void MEDFileUMeshAggregateCompute::assignParts(const std::vector< const MEDCoupling1GTUMesh * >& mParts)
1529 {
1530   std::size_t sz(mParts.size());
1531   std::vector< MCAuto<MEDCoupling1GTUMesh> > ret(sz);
1532   for(std::size_t i=0;i<sz;i++)
1533     {
1534       const MEDCoupling1GTUMesh *elt(mParts[i]);
1535       if(!elt)
1536         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::assignParts : presence of null pointer !");
1537       ret[i]=const_cast<MEDCoupling1GTUMesh *>(elt); elt->incrRef();
1538     }
1539   _m_parts=ret;
1540   _part_def.clear(); _part_def.resize(sz);
1541   _mp_time=std::max(_mp_time,_m_time)+1;
1542   _m=0;
1543 }
1544
1545 void MEDFileUMeshAggregateCompute::assignDefParts(const std::vector<const PartDefinition *>& partDefs)
1546 {
1547   if(_mp_time<_m_time)
1548     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::assignDefParts : the parts require a computation !");
1549   std::size_t sz(partDefs.size());
1550   if(_part_def.size()!=partDefs.size() || _part_def.size()!=_m_parts.size())
1551     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::assignDefParts : sizes of vectors of part definition mismatch !");
1552   for(std::size_t i=0;i<sz;i++)
1553     {
1554       const PartDefinition *elt(partDefs[i]);
1555       if(elt)
1556         elt->incrRef();
1557       _part_def[i]=const_cast<PartDefinition*>(elt);
1558     }
1559 }
1560
1561 void MEDFileUMeshAggregateCompute::assignUMesh(MEDCouplingUMesh *m)
1562 {
1563   _m=m;
1564   _m_parts.clear();
1565   _m_time=std::max(_mp_time,_m_time)+1;
1566 }
1567
1568 MEDCouplingUMesh *MEDFileUMeshAggregateCompute::getUmesh() const
1569 {
1570   if(_mp_time<=_m_time)
1571     return _m;
1572   std::vector< const MEDCoupling1GTUMesh *> mp(_m_parts.size());
1573   std::copy(_m_parts.begin(),_m_parts.end(),mp.begin());
1574   _m=MEDCoupling1GTUMesh::AggregateOnSameCoordsToUMesh(mp);
1575   _m_parts.clear();//to avoid memory peak !
1576   _m_time=_mp_time+1;//+1 is important ! That is to say that only _m is OK not _m_parts because cleared !
1577   return _m;
1578 }
1579
1580 mcIdType MEDFileUMeshAggregateCompute::getNumberOfCells() const
1581 {
1582   if(_mp_time<=_m_time)
1583     return _m->getNumberOfCells();
1584   mcIdType ret(0);
1585   for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1586     ret+=(*it)->getNumberOfCells();
1587   return ret;
1588 }
1589
1590 std::vector<INTERP_KERNEL::NormalizedCellType> MEDFileUMeshAggregateCompute::getGeoTypes() const
1591 {
1592   if(_mp_time>=_m_time)
1593     {
1594       std::size_t sz(_m_parts.size());
1595       std::vector<INTERP_KERNEL::NormalizedCellType> ret(sz);
1596       for(std::size_t i=0;i<sz;i++)
1597         ret[i]=_m_parts[i]->getCellModelEnum();
1598       return ret;
1599     }
1600   else
1601     return _m->getAllGeoTypesSorted();
1602 }
1603
1604 mcIdType MEDFileUMeshAggregateCompute::getNumberOfCellsWithType(INTERP_KERNEL::NormalizedCellType ct) const
1605 {
1606   if(_mp_time>=_m_time)
1607     {
1608       for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1609         {
1610           const MEDCoupling1GTUMesh *elt(*it);
1611           if(elt && elt->getCellModelEnum()==ct)
1612             return elt->getNumberOfCells();
1613         }
1614       return 0;
1615     }
1616   else
1617     return _m->getNumberOfCellsWithType(ct);
1618 }
1619
1620 std::vector<MEDCoupling1GTUMesh *> MEDFileUMeshAggregateCompute::retrievePartsWithoutComputation() const
1621 {
1622   if(_mp_time<_m_time)
1623     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getPartsWithoutComputation : the parts require a computation !");
1624   //
1625   std::vector<MEDCoupling1GTUMesh *> ret(_m_parts.size());
1626   std::size_t i(0);
1627   for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++,i++)
1628     {
1629       const MEDCoupling1GTUMesh *elt(*it);
1630       ret[i]=const_cast<MEDCoupling1GTUMesh *>(elt);
1631     }
1632   return ret;
1633 }
1634
1635 std::vector<MEDCoupling1GTUMesh *> MEDFileUMeshAggregateCompute::getParts() const
1636 {
1637   if(_mp_time<_m_time)
1638     forceComputationOfPartsFromUMesh();
1639   return retrievePartsWithoutComputation();
1640 }
1641
1642 void MEDFileUMeshAggregateCompute::highlightUsedNodes(std::vector<bool>& nodesToBeHighlighted) const
1643 {
1644   if(_mp_time<_m_time)
1645     forceComputationOfPartsFromUMesh();
1646   for(auto part : this->_m_parts)
1647   {
1648     part->computeNodeIdsAlg(nodesToBeHighlighted);
1649   }
1650 }
1651
1652 MEDCoupling1GTUMesh *MEDFileUMeshAggregateCompute::retrievePartWithoutComputation(INTERP_KERNEL::NormalizedCellType gt) const
1653 {
1654   std::vector<MEDCoupling1GTUMesh *> v(retrievePartsWithoutComputation());
1655   std::size_t sz(v.size());
1656   for(std::size_t i=0;i<sz;i++)
1657     {
1658       if(v[i])
1659         if(v[i]->getCellModelEnum()==gt)
1660           return v[i];
1661     }
1662   throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getPartWithoutComputation : the geometric type is not existing !");
1663 }
1664
1665 void MEDFileUMeshAggregateCompute::getStartStopOfGeoTypeWithoutComputation(INTERP_KERNEL::NormalizedCellType gt, mcIdType& start, mcIdType& stop) const
1666 {
1667   start=0; stop=0;
1668   std::vector<MEDCoupling1GTUMesh *> v(retrievePartsWithoutComputation());
1669   std::size_t sz(v.size());
1670   for(std::size_t i=0;i<sz;i++)
1671     {
1672       if(v[i])
1673         {
1674           if(v[i]->getCellModelEnum()==gt)
1675             {
1676               stop=start+v[i]->getNumberOfCells();
1677               return;
1678             }
1679           else
1680             start+=v[i]->getNumberOfCells();
1681         }
1682     }
1683   throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getStartStopOfGeoTypeWithoutComputation : the geometric type is not existing !");
1684 }
1685
1686 void MEDFileUMeshAggregateCompute::renumberNodesInConnWithoutComputation(const mcIdType *newNodeNumbersO2N)
1687 {
1688   if(_mp_time>_m_time)
1689     {
1690       for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1691         {
1692           MEDCoupling1GTUMesh *m(*it);
1693           if(m)
1694             m->renumberNodesInConn(newNodeNumbersO2N);
1695         }
1696     }
1697   else
1698     {
1699       MEDCouplingUMesh *m(getUmesh());
1700       if(!m)
1701         return;
1702       m->renumberNodesInConn(newNodeNumbersO2N);
1703     }
1704 }
1705
1706 void MEDFileUMeshAggregateCompute::forceComputationOfPartsFromUMesh() const
1707 {
1708   const MEDCouplingUMesh *m(_m);
1709   if(!m)
1710     {
1711       if(_m_parts.empty())
1712         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::forceComputationOfPartsFromUMesh : null UMesh !");
1713       else
1714         return ;// no needs to compte parts they are already here !
1715     }
1716   std::vector<MEDCouplingUMesh *> ms(m->splitByType());
1717   std::vector< MCAuto<MEDCouplingUMesh> > msMSafe(ms.begin(),ms.end());
1718   std::size_t sz(msMSafe.size());
1719   _m_parts.resize(sz);
1720   for(std::size_t i=0;i<sz;i++)
1721     _m_parts[i]=MEDCoupling1GTUMesh::New(ms[i]);
1722   _part_def.clear();
1723   _part_def.resize(_m_parts.size());
1724   _mp_time=std::max(_mp_time,_m_time);
1725 }
1726
1727 const PartDefinition *MEDFileUMeshAggregateCompute::getPartDefOfWithoutComputation(INTERP_KERNEL::NormalizedCellType gt) const
1728 {
1729   if(_mp_time<_m_time)
1730     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getPartDefOfWithoutComputation : the parts require a computation !");
1731   if(_m_parts.size()!=_part_def.size())
1732     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getPartDefOfWithoutComputation : size of arrays are expected to be the same !");
1733   std::size_t sz(_m_parts.size());
1734   for(std::size_t i=0;i<sz;i++)
1735     {
1736       const MEDCoupling1GTUMesh *mesh(_m_parts[i]);
1737       if(mesh)
1738         if(mesh->getCellModelEnum()==gt)
1739           return _part_def[i];
1740     }
1741   throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getPartDefOfWithoutComputation : The input geo type is not existing in this !");
1742 }
1743
1744 void MEDFileUMeshAggregateCompute::serialize(std::vector<mcIdType>& tinyInt, std::vector< MCAuto<DataArrayIdType> >& bigArraysI) const
1745 {
1746   if(_mp_time<_m_time)
1747     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::serialize : the parts require a computation !");
1748   std::size_t sz(_m_parts.size());
1749   tinyInt.push_back((mcIdType)sz);
1750   for(std::size_t i=0;i<sz;i++)
1751     {
1752       const MEDCoupling1GTUMesh *mesh(_m_parts[i]);
1753       if(!mesh)
1754         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::serialize : one part is empty !");
1755       tinyInt.push_back(mesh->getCellModelEnum());
1756       const MEDCoupling1SGTUMesh *mesh1(dynamic_cast<const MEDCoupling1SGTUMesh *>(mesh));
1757       const MEDCoupling1DGTUMesh *mesh2(dynamic_cast<const MEDCoupling1DGTUMesh *>(mesh));
1758       if(mesh1)
1759         {
1760           DataArrayIdType *elt(mesh1->getNodalConnectivity());
1761           if(elt)
1762             elt->incrRef();
1763           MCAuto<DataArrayIdType> elt1(elt);
1764           bigArraysI.push_back(elt1);
1765         }
1766       else if(mesh2)
1767         {
1768           DataArrayIdType *elt1(mesh2->getNodalConnectivity()),*elt2(mesh2->getNodalConnectivityIndex());
1769           if(elt1)
1770             elt1->incrRef();
1771           if(elt2)
1772             elt2->incrRef();
1773           MCAuto<DataArrayIdType> elt11(elt1),elt22(elt2);
1774           bigArraysI.push_back(elt11); bigArraysI.push_back(elt22);
1775         }
1776       else
1777         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::serialize : unrecognized single geo type mesh !");
1778       const PartDefinition *pd(_part_def[i]);
1779       if(!pd)
1780         tinyInt.push_back(-1);
1781       else
1782         {
1783           std::vector<mcIdType> tinyTmp;
1784           pd->serialize(tinyTmp,bigArraysI);
1785           tinyInt.push_back((mcIdType)tinyTmp.size());
1786           tinyInt.insert(tinyInt.end(),tinyTmp.begin(),tinyTmp.end());
1787         }
1788     }
1789 }
1790
1791 void MEDFileUMeshAggregateCompute::unserialize(const std::string& name, DataArrayDouble *coo, std::vector<mcIdType>& tinyInt, std::vector< MCAuto<DataArrayIdType> >& bigArraysI)
1792 {
1793   mcIdType nbParts(tinyInt.back()); tinyInt.pop_back();
1794   _part_def.clear(); _part_def.resize(nbParts);
1795   _m_parts.clear(); _m_parts.resize(nbParts);
1796   for(mcIdType i=0;i<nbParts;i++)
1797     {
1798       INTERP_KERNEL::NormalizedCellType tp((INTERP_KERNEL::NormalizedCellType) tinyInt.back()); tinyInt.pop_back();
1799       MCAuto<MEDCoupling1GTUMesh> mesh(MEDCoupling1GTUMesh::New(name,tp));
1800       mesh->setCoords(coo);
1801       MEDCoupling1SGTUMesh *mesh1(dynamic_cast<MEDCoupling1SGTUMesh *>((MEDCoupling1GTUMesh *) mesh));
1802       MEDCoupling1DGTUMesh *mesh2(dynamic_cast<MEDCoupling1DGTUMesh *>((MEDCoupling1GTUMesh *) mesh));
1803       if(mesh1)
1804         {
1805           mesh1->setNodalConnectivity(bigArraysI.back()); bigArraysI.pop_back();
1806         }
1807       else if(mesh2)
1808         {
1809           MCAuto<DataArrayIdType> elt0,elt1;
1810           elt0=bigArraysI.back(); bigArraysI.pop_back();
1811           elt1=bigArraysI.back(); bigArraysI.pop_back();
1812           mesh2->setNodalConnectivity(elt0,elt1);
1813         }
1814       else
1815         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::unserialize : unrecognized single geo type mesh !");
1816       _m_parts[i]=mesh;
1817       mcIdType pdid(tinyInt.back()); tinyInt.pop_back();
1818       if(pdid!=-1)
1819         _part_def[i]=PartDefinition::Unserialize(tinyInt,bigArraysI);
1820       _mp_time=std::max(_mp_time,_m_time)+1;
1821     }
1822 }
1823
1824 /*!
1825  * This method returns true if \a this is stored split by type false if stored in a merged unstructured mesh.
1826  */
1827 bool MEDFileUMeshAggregateCompute::isStoredSplitByType() const
1828 {
1829   return _mp_time>=_m_time;
1830 }
1831
1832 std::size_t MEDFileUMeshAggregateCompute::getTimeOfThis() const
1833 {
1834   if(_mp_time>_m_time)
1835     return getTimeOfParts();
1836   if(_m_time>_mp_time)
1837     return getTimeOfUMesh();
1838   return std::max(getTimeOfParts(),getTimeOfUMesh());
1839 }
1840
1841 std::size_t MEDFileUMeshAggregateCompute::getTimeOfParts() const
1842 {
1843   std::size_t ret(0);
1844   for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1845     {
1846       const MEDCoupling1GTUMesh *elt(*it);
1847       if(!elt)
1848         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getTimeOfParts : null obj in parts !");
1849       ret=std::max(ret,elt->getTimeOfThis());
1850     }
1851   if(ret==0)
1852     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getTimeOfParts : parts is empty !");
1853   return ret;
1854 }
1855
1856 std::size_t MEDFileUMeshAggregateCompute::getTimeOfUMesh() const
1857 {
1858   const MEDCouplingUMesh *m(_m);
1859   if(!m)
1860     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getTimeOfUMesh : unmesh is null !");
1861   return m->getTimeOfThis();
1862 }
1863
1864 std::size_t MEDFileUMeshAggregateCompute::getHeapMemorySizeWithoutChildren() const
1865 {
1866   std::size_t ret(_m_parts.size()*sizeof(MCAuto<MEDCoupling1GTUMesh>));
1867   return ret;
1868 }
1869
1870 std::vector<const BigMemoryObject *> MEDFileUMeshAggregateCompute::getDirectChildrenWithNull() const
1871 {
1872   std::vector<const BigMemoryObject *> ret;
1873   for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1874     ret.push_back((const MEDCoupling1GTUMesh *)*it);
1875   ret.push_back((const MEDCouplingUMesh *)_m);
1876   return ret;
1877 }
1878
1879 MEDFileUMeshAggregateCompute MEDFileUMeshAggregateCompute::deepCopy(DataArrayDouble *coords) const
1880 {
1881   MEDFileUMeshAggregateCompute ret;
1882   ret._m_parts.resize(_m_parts.size());
1883   for(std::size_t i=0;i<_m_parts.size();i++)
1884     {
1885       const MEDCoupling1GTUMesh *elt(_m_parts[i]);
1886       if(elt)
1887         {
1888           ret._m_parts[i]=static_cast<MEDCoupling::MEDCoupling1GTUMesh*>(elt->deepCopy());
1889           ret._m_parts[i]->setCoords(coords);
1890         }
1891     }
1892   ret._mp_time=_mp_time; ret._m_time=_m_time;
1893   if((const MEDCouplingUMesh *)_m)
1894     {
1895       ret._m=static_cast<MEDCoupling::MEDCouplingUMesh*>(_m->deepCopy());
1896       ret._m->setCoords(coords);
1897     }
1898   std::size_t sz(_part_def.size());
1899   ret._part_def.clear(); ret._part_def.resize(sz);
1900   for(std::size_t i=0;i<sz;i++)
1901     {
1902       const PartDefinition *elt(_part_def[i]);
1903       if(elt)
1904         ret._part_def[i]=elt->deepCopy();
1905     }
1906   return ret;
1907 }
1908
1909 void MEDFileUMeshAggregateCompute::shallowCpyMeshes()
1910 {
1911   for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1912     {
1913       const MEDCoupling1GTUMesh *elt(*it);
1914       if(elt)
1915         {
1916           MCAuto<MEDCouplingMesh> elt2(elt->clone(false));
1917           *it=DynamicCastSafe<MEDCouplingMesh,MEDCoupling1GTUMesh>(elt2);
1918         }
1919     }
1920   const MEDCouplingUMesh *m(_m);
1921   if(m)
1922     _m=m->clone(false);
1923 }
1924
1925 bool MEDFileUMeshAggregateCompute::isEqual(const MEDFileUMeshAggregateCompute& other, double eps, std::string& what) const
1926 {
1927   const MEDCouplingUMesh *m1(getUmesh());
1928   const MEDCouplingUMesh *m2(other.getUmesh());
1929   if((m1==0 && m2!=0) || (m1!=0 && m2==0))
1930     {
1931       what="Presence of mesh in one sublevel and not in other!";
1932       return false;
1933     }
1934   if(m1)
1935     {
1936       std::string what2;
1937       if(!m1->isEqualIfNotWhy(m2,eps,what2))
1938         {
1939           what=std::string("meshes at a sublevel are not deeply equal (")+what2+std::string(")!");
1940           return false;
1941         }
1942     }
1943   std::size_t sz(_part_def.size());
1944   if(sz!=other._part_def.size())
1945     {
1946       what=std::string("number of subdivision per geo type for part definition is not the same !");
1947       return false;
1948     }
1949   for(std::size_t i=0;i<sz;i++)
1950     {
1951       const PartDefinition *pd0(_part_def[i]),*pd1(other._part_def[i]);
1952       if(!pd0 && !pd1)
1953         continue;
1954       if((!pd0 && pd1) || (pd0 && !pd1))
1955         {
1956           what=std::string("a cell part def is defined only for one among this or other !");
1957           return false;
1958         }
1959       bool ret(pd0->isEqual(pd1,what));
1960       if(!ret)
1961         return false;
1962     }
1963   return true;
1964 }
1965
1966 void MEDFileUMeshAggregateCompute::checkConsistency() const
1967 {
1968   if(_mp_time >= _m_time)
1969     for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();
1970         it!=_m_parts.end(); it++)
1971       (*it)->checkConsistency();
1972   else
1973     _m->checkConsistency();
1974 }
1975
1976 void MEDFileUMeshAggregateCompute::clearNonDiscrAttributes() const
1977 {
1978   for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1979     MEDFileUMeshSplitL1::ClearNonDiscrAttributes(*it);
1980   MEDFileUMeshSplitL1::ClearNonDiscrAttributes(_m);
1981 }
1982
1983 void MEDFileUMeshAggregateCompute::synchronizeTinyInfo(const MEDFileMesh& master) const
1984 {
1985   for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1986     {
1987       const MEDCoupling1GTUMesh *tmp(*it);
1988       if(tmp)
1989         {
1990           (const_cast<MEDCoupling1GTUMesh *>(tmp))->setName(master.getName().c_str());
1991           (const_cast<MEDCoupling1GTUMesh *>(tmp))->setDescription(master.getDescription().c_str());
1992           (const_cast<MEDCoupling1GTUMesh *>(tmp))->setTime(master.getTimeValue(),master.getIteration(),master.getOrder());
1993           (const_cast<MEDCoupling1GTUMesh *>(tmp))->setTimeUnit(master.getTimeUnit());
1994         }
1995     }
1996   const MEDCouplingUMesh *m(_m);
1997   if(m)
1998     {
1999       (const_cast<MEDCouplingUMesh *>(m))->setName(master.getName().c_str());
2000       (const_cast<MEDCouplingUMesh *>(m))->setDescription(master.getDescription().c_str());
2001       (const_cast<MEDCouplingUMesh *>(m))->setTime(master.getTimeValue(),master.getIteration(),master.getOrder());
2002       (const_cast<MEDCouplingUMesh *>(m))->setTimeUnit(master.getTimeUnit());
2003     }
2004 }
2005
2006 bool MEDFileUMeshAggregateCompute::empty() const
2007 {
2008   if(_mp_time<_m_time)
2009     return ((const MEDCouplingUMesh *)_m)==0;
2010   //else _mp_time>=_m_time)
2011   return _m_parts.empty();
2012 }
2013
2014 int MEDFileUMeshAggregateCompute::getMeshDimension() const
2015 {
2016   if(_mp_time<_m_time)
2017     {
2018       const MEDCouplingUMesh *m(_m);
2019       if(!m)
2020         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getMeshDimension : no umesh in this !");
2021       return m->getMeshDimension();
2022     }
2023   else
2024     {
2025       if(_m_parts.empty())
2026         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getMeshDimension : part mesh is empty !");
2027       const MEDCoupling1GTUMesh *m(_m_parts[0]);
2028       if(!m)
2029         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getMeshDimension : part mesh contains null instance !");
2030       return m->getMeshDimension();
2031     }
2032 }
2033
2034 std::vector<mcIdType> MEDFileUMeshAggregateCompute::getDistributionOfTypes() const
2035 {
2036   if(_mp_time<_m_time)
2037     {
2038       const MEDCouplingUMesh *m(_m);
2039       if(!m)
2040         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getDistributionOfTypes : no umesh in this !");
2041       return m->getDistributionOfTypes();
2042     }
2043   else
2044     {
2045       std::vector<mcIdType> ret;
2046       for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
2047         {
2048           const MEDCoupling1GTUMesh *tmp(*it);
2049           if(!tmp)
2050             throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getDistributionOfTypes : part mesh contains null instance !");
2051           std::vector<mcIdType> ret0(tmp->getDistributionOfTypes());
2052           ret.insert(ret.end(),ret0.begin(),ret0.end());
2053         }
2054       return ret;
2055     }
2056 }
2057
2058 mcIdType MEDFileUMeshAggregateCompute::getSize() const
2059 {
2060   if(_mp_time<_m_time)
2061     {
2062       const MEDCouplingUMesh *m(_m);
2063       if(!m)
2064         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getSize : no umesh in this !");
2065       return m->getNumberOfCells();
2066     }
2067   else
2068     {
2069       mcIdType ret=0;
2070       for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
2071         {
2072           const MEDCoupling1GTUMesh *m(*it);
2073           if(!m)
2074             throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getSize : part mesh contains null instance !");
2075           ret+=m->getNumberOfCells();
2076         }
2077       return ret;
2078     }
2079 }
2080
2081 void MEDFileUMeshAggregateCompute::setCoords(DataArrayDouble *coords)
2082 {
2083   for(std::vector< MCAuto<MEDCoupling1GTUMesh> >::iterator it=_m_parts.begin();it!=_m_parts.end();it++)
2084     {
2085       MEDCoupling1GTUMesh *tmp(*it);
2086       if(tmp)
2087         (*it)->setCoords(coords);
2088     }
2089   MEDCouplingUMesh *m(_m);
2090   if(m)
2091     m->setCoords(coords);
2092 }
2093
2094 MEDFileEltStruct4Mesh *MEDFileEltStruct4Mesh::New(med_idt fid, const std::string& mName, int dt, int it, int iterOnStEltOfMesh, MEDFileMeshReadSelector *mrs)
2095 {
2096   return new MEDFileEltStruct4Mesh(fid,mName,dt,it,iterOnStEltOfMesh,mrs);
2097 }
2098
2099 std::size_t MEDFileEltStruct4Mesh::getHeapMemorySizeWithoutChildren() const
2100 {
2101   return _geo_type_name.capacity()+_vars.capacity()*sizeof(MCAuto<DataArray>);
2102 }
2103
2104 std::vector<const MEDCoupling::BigMemoryObject*> MEDFileEltStruct4Mesh::getDirectChildrenWithNull() const
2105 {
2106   std::vector<const MEDCoupling::BigMemoryObject*> ret;
2107   ret.push_back(_conn);
2108   ret.push_back(_common);
2109   for(std::vector< MCAuto<DataArray> >::const_iterator it=_vars.begin();it!=_vars.end();it++)
2110     ret.push_back(*it);
2111   return ret;
2112 }
2113
2114 MEDFileEltStruct4Mesh::MEDFileEltStruct4Mesh(med_idt fid, const std::string& mName, int dt, int it, int iterOnStEltOfMesh, MEDFileMeshReadSelector *mrs)
2115 {
2116   med_geometry_type geoType;
2117   INTERP_KERNEL::AutoPtr<char> geoTypeName(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE));
2118   MEDFILESAFECALLERRD0(MEDmeshEntityInfo,(fid,mName.c_str(),dt,it,MED_STRUCT_ELEMENT,iterOnStEltOfMesh+1,geoTypeName,&geoType));
2119   _geo_type=geoType;
2120   _geo_type_name=MEDLoaderBase::buildStringFromFortran(geoTypeName,MED_NAME_SIZE);
2121   mcIdType nCells(0);
2122   {
2123     med_bool chgt=MED_FALSE,trsf=MED_FALSE;
2124     nCells=MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_STRUCT_ELEMENT,geoType,MED_CONNECTIVITY,MED_NODAL,&chgt,&trsf);
2125   }
2126   MCAuto<MEDFileMeshSupports> mss(MEDFileMeshSupports::New(fid));
2127   MCAuto<MEDFileStructureElements> mse(MEDFileStructureElements::New(fid,mss));
2128   mcIdType nbEntities(mse->getNumberOfNodesPerSE(_geo_type_name));
2129   MCAuto<DataArrayMedInt> miConn=DataArrayMedInt::New(); miConn->alloc(nCells*nbEntities);
2130   MEDFILESAFECALLERRD0(MEDmeshElementConnectivityRd,(fid,mName.c_str(),dt,it,MED_STRUCT_ELEMENT,_geo_type,MED_NODAL,MED_FULL_INTERLACE,miConn->getPointer()));
2131   _conn=FromMedIntArray<mcIdType>(miConn);
2132   _conn->applyLin(1,-1);
2133   _conn->rearrange(nbEntities);
2134   _common=MEDFileUMeshPerTypeCommon::New();
2135   _common->loadCommonPart(fid,mName.c_str(),dt,it,nCells,geoType,MED_STRUCT_ELEMENT,mrs);
2136   std::vector<std::string> vns(mse->getVarAttsOf(_geo_type_name));
2137   std::size_t sz(vns.size());
2138   _vars.resize(sz);
2139   for(std::size_t i=0;i<sz;i++)
2140     {
2141       const MEDFileSEVarAtt *var(mse->getVarAttOf(_geo_type_name,vns[i]));
2142       MCAuto<DataArray> gen(var->getGenerator());
2143       MCAuto<DataArray> arr(gen->buildNewEmptyInstance());
2144       arr->alloc(nCells,var->getNbOfComponents());
2145       arr->setName(vns[i]);
2146       MEDFILESAFECALLERRD0(MEDmeshStructElementVarAttRd,(fid,mName.c_str(),dt,it,_geo_type,vns[i].c_str(),arr->getVoidStarPointer()));
2147       _vars[i]=arr;
2148     }
2149 }