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