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