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