Salome HOME
Improve performance on // read of a MED file using block filter for nodes instead...
[modules/med.git] / src / MEDLoader / MEDFileMeshLL.cxx
1 // Copyright (C) 2007-2014  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 "MEDFileMeshReadSelector.hxx"
25
26 #include "MEDCouplingUMesh.hxx"
27
28 #include "InterpKernelAutoPtr.hxx"
29 #include "CellModel.hxx"
30
31 #include <set>
32
33 extern med_geometry_type typmai[MED_N_CELL_FIXED_GEO];
34 extern INTERP_KERNEL::NormalizedCellType typmai2[MED_N_CELL_FIXED_GEO];
35 extern med_geometry_type typmainoeud[1];
36
37 using namespace ParaMEDMEM;
38
39 MEDFileMeshL2::MEDFileMeshL2():_name(MED_NAME_SIZE),_description(MED_COMMENT_SIZE),_univ_name(MED_LNAME_SIZE),_dt_unit(MED_LNAME_SIZE)
40 {
41 }
42
43 std::size_t MEDFileMeshL2::getHeapMemorySizeWithoutChildren() const
44 {
45   return 0;
46 }
47
48 std::vector<const BigMemoryObject *> MEDFileMeshL2::getDirectChildrenWithNull() const
49 {
50   return std::vector<const BigMemoryObject *>();
51 }
52
53 int MEDFileMeshL2::GetMeshIdFromName(med_idt fid, const std::string& mname, ParaMEDMEM::MEDCouplingMeshType& meshType, int& dt, int& it, std::string& dtunit1)
54 {
55   med_mesh_type type_maillage;
56   char maillage_description[MED_COMMENT_SIZE+1];
57   char dtunit[MED_LNAME_SIZE+1];
58   med_int spaceDim,dim;
59   char nommaa[MED_NAME_SIZE+1];
60   med_int n=MEDnMesh(fid);
61   bool found=false;
62   int ret=-1;
63   med_sorting_type stype;
64   std::vector<std::string> ms;
65   int nstep;
66   med_axis_type axistype;
67   for(int i=0;i<n && !found;i++)
68     {
69       int naxis=MEDmeshnAxis(fid,i+1);
70       INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
71       INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
72       MEDmeshInfo(fid,i+1,nommaa,&spaceDim,&dim,&type_maillage,maillage_description,dtunit,&stype,&nstep,&axistype,axisname,axisunit);
73       dtunit1=MEDLoaderBase::buildStringFromFortran(dtunit,sizeof(dtunit));
74       std::string cur=MEDLoaderBase::buildStringFromFortran(nommaa,sizeof(nommaa));
75       ms.push_back(cur);
76       if(cur==mname)
77         {
78           found=true;
79           ret=i+1;
80         }
81     }
82   if(!found)
83     {
84       std::ostringstream oss;
85       oss << "No such meshname (" << mname <<  ") in file ! Must be in : ";
86       std::copy(ms.begin(),ms.end(),std::ostream_iterator<std::string>(oss,", "));
87       throw INTERP_KERNEL::Exception(oss.str().c_str());
88     }
89   switch(type_maillage)
90   {
91     case MED_UNSTRUCTURED_MESH:
92       meshType=UNSTRUCTURED;
93       break;
94     case MED_STRUCTURED_MESH:
95       {
96         med_grid_type gt;
97         MEDmeshGridTypeRd(fid,mname.c_str(),&gt);
98         switch(gt)
99         {
100           case MED_CARTESIAN_GRID:
101             meshType=CARTESIAN;
102             break;
103           case MED_CURVILINEAR_GRID:
104             meshType=CURVE_LINEAR;
105             break;
106           default:
107             throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getMeshIdFromName : unrecognized structured mesh type ! Supported are :\n - cartesian\n - curve linear\n");
108         }
109         break;
110       }
111     default:
112       throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getMeshIdFromName : unrecognized mesh type !");
113   }
114   med_int numdt,numit;
115   med_float dtt;
116   MEDmeshComputationStepInfo(fid,mname.c_str(),1,&numdt,&numit,&dtt);
117   dt=numdt; it=numit;
118   return ret;
119 }
120
121 double MEDFileMeshL2::CheckMeshTimeStep(med_idt fid, const std::string& mName, int nstep, int dt, int it)
122 {
123   bool found=false;
124   med_int numdt,numit;
125   med_float dtt;
126   std::vector< std::pair<int,int> > p(nstep);
127   for(int i=0;i<nstep;i++)
128     {
129       MEDmeshComputationStepInfo(fid,mName.c_str(),i+1,&numdt,&numit,&dtt);
130       p[i]=std::make_pair<int,int>(numdt,numit);
131       found=(numdt==dt) && (numit==numit);
132     }
133   if(!found)
134     {
135       std::ostringstream oss; oss << "No such iteration=" << dt << ",order=" << it << " numbers found for mesh '" << mName << "' ! ";
136       oss << "Possibilities are : ";
137       for(int i=0;i<nstep;i++)
138         oss << "(" << p[i].first << "," << p[i].second << "), ";
139       throw INTERP_KERNEL::Exception(oss.str().c_str());
140     }
141   return dtt;
142 }
143
144 std::vector<std::string> MEDFileMeshL2::getAxisInfoOnMesh(med_idt fid, int mId, const std::string& mName, ParaMEDMEM::MEDCouplingMeshType& meshType, int& nstep, int& Mdim)
145 {
146   med_mesh_type type_maillage;
147   med_int spaceDim;
148   med_sorting_type stype;
149   med_axis_type axistype;
150   int naxis=MEDmeshnAxis(fid,mId);
151   INTERP_KERNEL::AutoPtr<char> nameTmp=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
152   INTERP_KERNEL::AutoPtr<char> axisname=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
153   INTERP_KERNEL::AutoPtr<char> axisunit=MEDLoaderBase::buildEmptyString(naxis*MED_SNAME_SIZE);
154   INTERP_KERNEL::AutoPtr<char> univTmp=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE);
155   if(MEDmeshInfo(fid,mId,nameTmp,&spaceDim,&Mdim,&type_maillage,_description.getPointer(),_dt_unit.getPointer(),
156       &stype,&nstep,&axistype,axisname,axisunit)!=0)
157     throw INTERP_KERNEL::Exception("A problem has been detected when trying to get info on mesh !");
158   MEDmeshUniversalNameRd(fid,nameTmp,_univ_name.getPointer());
159   switch(type_maillage)
160   {
161     case MED_UNSTRUCTURED_MESH:
162       meshType=UNSTRUCTURED;
163       break;
164     case MED_STRUCTURED_MESH:
165       {
166         med_grid_type gt;
167         MEDmeshGridTypeRd(fid,mName.c_str(),&gt);
168         switch(gt)
169         {
170           case MED_CARTESIAN_GRID:
171             meshType=CARTESIAN;
172             break;
173           case MED_CURVILINEAR_GRID:
174             meshType=CURVE_LINEAR;
175             break;
176           default:
177             throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getAxisInfoOnMesh : unrecognized structured mesh type ! Supported are :\n - cartesian\n - curve linear\n");
178         }
179         break;
180       }
181     default:
182       throw INTERP_KERNEL::Exception("MEDFileUMeshL2::getMeshIdFromName : unrecognized mesh type !");
183   }
184   //
185   std::vector<std::string> infosOnComp(naxis);
186   for(int i=0;i<naxis;i++)
187     {
188       std::string info=MEDLoaderBase::buildUnionUnit(((char *)axisname)+i*MED_SNAME_SIZE,MED_SNAME_SIZE,((char *)axisunit)+i*MED_SNAME_SIZE,MED_SNAME_SIZE);
189       infosOnComp[i]=info;
190     }
191   return infosOnComp;
192 }
193
194 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)
195 {
196   if(mrs && !(mrs->isCellFamilyFieldReading() || mrs->isNodeFamilyFieldReading()))
197     return ;
198   char nomfam[MED_NAME_SIZE+1];
199   med_int numfam;
200   int nfam=MEDnFamily(fid,meshName.c_str());
201   for(int i=0;i<nfam;i++)
202     {
203       int ngro=MEDnFamilyGroup(fid,meshName.c_str(),i+1);
204       med_int natt=MEDnFamily23Attribute(fid,meshName.c_str(),i+1);
205       INTERP_KERNEL::AutoPtr<med_int> attide=new med_int[natt];
206       INTERP_KERNEL::AutoPtr<med_int> attval=new med_int[natt];
207       INTERP_KERNEL::AutoPtr<char> attdes=new char[MED_COMMENT_SIZE*natt+1];
208       INTERP_KERNEL::AutoPtr<char> gro=new char[MED_LNAME_SIZE*ngro+1];
209       MEDfamily23Info(fid,meshName.c_str(),i+1,nomfam,attide,attval,attdes,&numfam,gro);
210       std::string famName=MEDLoaderBase::buildStringFromFortran(nomfam,MED_NAME_SIZE);
211       fams[famName]=numfam;
212       for(int j=0;j<ngro;j++)
213         {
214           std::string groupname=MEDLoaderBase::buildStringFromFortran(gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
215           grps[groupname].push_back(famName);
216         }
217     }
218 }
219
220 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)
221 {
222   for(std::map<std::string,int>::const_iterator it=fams.begin();it!=fams.end();it++)
223     {
224       std::vector<std::string> grpsOfFam;
225       for(std::map<std::string, std::vector<std::string> >::const_iterator it1=grps.begin();it1!=grps.end();it1++)
226         {
227           if(std::find((*it1).second.begin(),(*it1).second.end(),(*it).first)!=(*it1).second.end())
228             grpsOfFam.push_back((*it1).first);
229         }
230       int ngro=grpsOfFam.size();
231       INTERP_KERNEL::AutoPtr<char> groName=MEDLoaderBase::buildEmptyString(MED_LNAME_SIZE*ngro);
232       int i=0;
233       for(std::vector<std::string>::const_iterator it2=grpsOfFam.begin();it2!=grpsOfFam.end();it2++,i++)
234         MEDLoaderBase::safeStrCpy2((*it2).c_str(),MED_LNAME_SIZE-1,groName+i*MED_LNAME_SIZE,tooLongStrPol);
235       INTERP_KERNEL::AutoPtr<char> famName=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
236       MEDLoaderBase::safeStrCpy((*it).first.c_str(),MED_NAME_SIZE,famName,tooLongStrPol);
237       int ret=MEDfamilyCr(fid,mname.c_str(),famName,(*it).second,ngro,groName);
238       ret++;
239     }
240 }
241
242 MEDFileUMeshL2::MEDFileUMeshL2()
243 {
244 }
245
246 std::vector<std::string> MEDFileUMeshL2::loadCommonPart(med_idt fid, int mId, const std::string& mName, int dt, int it, int& Mdim)
247 {
248   Mdim=-3;
249   _name.set(mName.c_str());
250   int nstep;
251   ParaMEDMEM::MEDCouplingMeshType meshType;
252   std::vector<std::string> ret(getAxisInfoOnMesh(fid,mId,mName.c_str(),meshType,nstep,Mdim));
253   if(nstep==0)
254     {
255       Mdim=-4;
256       return std::vector<std::string>();
257     }
258   if(meshType!=UNSTRUCTURED)
259     throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected an unstructured one whereas in file it is not an unstructured !");
260   _time=CheckMeshTimeStep(fid,mName,nstep,dt,it);
261   _iteration=dt;
262   _order=it;
263   return ret;
264 }
265
266 void MEDFileUMeshL2::loadAll(med_idt fid, int mId, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs)
267 {
268   int Mdim;
269   std::vector<std::string> infosOnComp(loadCommonPart(fid,mId,mName,dt,it,Mdim));
270   if(Mdim==-4)
271     return ;
272   loadConnectivity(fid,Mdim,mName,dt,it,mrs);//to improve check (dt,it) coherency
273   loadCoords(fid,mId,infosOnComp,mName,dt,it);
274 }
275
276 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)
277 {
278   int Mdim;
279   std::vector<std::string> infosOnComp(loadCommonPart(fid,mId,mName,dt,it,Mdim));
280   if(Mdim==-4)
281     return ;
282   loadPartOfConnectivity(fid,Mdim,mName,types,slicPerTyp,dt,it,mrs);
283   med_bool changement,transformation;
284   int nCoords(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation));
285   std::vector<bool> fetchedNodeIds(nCoords,false);
286   for(std::vector< std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> > >::const_iterator it0=_per_type_mesh.begin();it0!=_per_type_mesh.end();it0++)
287     for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it1=(*it0).begin();it1!=(*it0).end();it1++)
288       (*it1)->getMesh()->computeNodeIdsAlg(fetchedNodeIds);
289   int nMin(std::distance(fetchedNodeIds.begin(),std::find(fetchedNodeIds.begin(),fetchedNodeIds.end(),true)));
290   int nMax(std::distance(fetchedNodeIds.rbegin(),std::find(fetchedNodeIds.rbegin(),fetchedNodeIds.rend(),true)));
291   nMax=nCoords-nMax-1;
292   for(std::vector< std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> > >::const_iterator it0=_per_type_mesh.begin();it0!=_per_type_mesh.end();it0++)
293     for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it1=(*it0).begin();it1!=(*it0).end();it1++)
294       (*it1)->getMesh()->renumberNodesWithOffsetInConn(-nMin);
295   loadPartCoords(fid,mId,infosOnComp,mName,dt,it,nMin,nMax);
296 }
297
298 void MEDFileUMeshL2::loadConnectivity(med_idt fid, int mdim, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs)
299 {
300   _per_type_mesh.resize(1);
301   _per_type_mesh[0].clear();
302   for(int j=0;j<MED_N_CELL_FIXED_GEO;j++)
303     {
304       MEDFileUMeshPerType *tmp(MEDFileUMeshPerType::New(fid,mName.c_str(),dt,it,mdim,typmai[j],typmai2[j],mrs));
305       if(tmp)
306         _per_type_mesh[0].push_back(tmp);
307     }
308   sortTypes();
309 }
310
311 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)
312 {
313   std::size_t nbOfTypes(types.size());
314   if(slicPerTyp.size()!=3*nbOfTypes)
315     throw INTERP_KERNEL::Exception("MEDFileUMeshL2::loadPartOfConnectivity : The size of slicPerTyp array is expected to be equal to 3 times size of array types !");
316   std::set<INTERP_KERNEL::NormalizedCellType> types2(types.begin(),types.end());
317   if(types2.size()!=nbOfTypes)
318     throw INTERP_KERNEL::Exception("MEDFileUMeshL2::loadPartOfConnectivity : the geometric types in types array must appear once !");
319   _per_type_mesh.resize(1);
320   _per_type_mesh[0].clear();
321   for(std::size_t ii=0;ii<nbOfTypes;ii++)
322     {
323       int strt(slicPerTyp[3*ii+0]),stp(slicPerTyp[3*ii+1]),step(slicPerTyp[3*ii+2]);
324       MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> tmp(MEDFileUMeshPerType::NewPart(fid,mName.c_str(),dt,it,mdim,types[ii],strt,stp,step,mrs));
325       _per_type_mesh[0].push_back(tmp);
326     }
327   sortTypes();
328 }
329
330 void MEDFileUMeshL2::loadCoords(med_idt fid, int mId, const std::vector<std::string>& infosOnComp, const std::string& mName, int dt, int it)
331 {
332   int spaceDim((int)infosOnComp.size());
333   med_bool changement,transformation;
334   int nCoords(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation));
335   _coords=DataArrayDouble::New();
336   _coords->alloc(nCoords,spaceDim);
337   double *coordsPtr(_coords->getPointer());
338   MEDmeshNodeCoordinateRd(fid,mName.c_str(),dt,it,MED_FULL_INTERLACE,coordsPtr);
339   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
340     {
341       _fam_coords=DataArrayInt::New();
342       _fam_coords->alloc(nCoords,1);
343       MEDmeshEntityFamilyNumberRd(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,_fam_coords->getPointer());
344     }
345   else
346     _fam_coords=0;
347   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
348     {
349       _num_coords=DataArrayInt::New();
350       _num_coords->alloc(nCoords,1);
351       MEDmeshEntityNumberRd(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,_num_coords->getPointer());
352     }
353   else
354     _num_coords=0;
355   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NAME,MED_NODAL,&changement,&transformation)>0)
356     {
357       _name_coords=DataArrayAsciiChar::New();
358       _name_coords->alloc(nCoords+1,MED_SNAME_SIZE);//not a bug to avoid the memory corruption due to last \0 at the end
359       MEDmeshEntityNameRd(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,_name_coords->getPointer());
360       _name_coords->reAlloc(nCoords);//not a bug to avoid the memory corruption due to last \0 at the end
361     }
362   else
363     _name_coords=0;
364   for(int i=0;i<spaceDim;i++)
365     _coords->setInfoOnComponent(i,infosOnComp[i]);
366 }
367
368 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)
369 {
370   med_bool changement,transformation;
371   int spaceDim((int)infosOnComp.size()),nCoords(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation));
372   _coords=DataArrayDouble::New();
373   int nbNodesToLoad(nMax-nMin+1);
374   _coords->alloc(nbNodesToLoad,spaceDim);
375   med_filter filter=MED_FILTER_INIT,filter2=MED_FILTER_INIT;
376   MEDfilterBlockOfEntityCr(fid,/*nentity*/nCoords,/*nvaluesperentity*/1,/*nconstituentpervalue*/spaceDim,
377                            MED_ALL_CONSTITUENT,MED_FULL_INTERLACE,MED_COMPACT_STMODE,MED_NO_PROFILE,
378                            /*start*/nMin+1,/*stride*/1,/*count*/1,/*blocksize*/nbNodesToLoad,
379                            /*lastblocksize=useless because count=1*/0,&filter);
380   MEDmeshNodeCoordinateAdvancedRd(fid,mName.c_str(),dt,it,&filter,_coords->getPointer());
381   MEDfilterClose(&filter);
382   MEDfilterBlockOfEntityCr(fid,nCoords,1,1,MED_ALL_CONSTITUENT,MED_FULL_INTERLACE,MED_COMPACT_STMODE,
383                            MED_NO_PROFILE,nMin+1,1,1,nbNodesToLoad,0,&filter2);
384   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
385     {
386       _fam_coords=DataArrayInt::New();
387       _fam_coords->alloc(nbNodesToLoad,1);
388       MEDmeshEntityAttributeAdvancedRd(fid,mName.c_str(),MED_FAMILY_NUMBER,dt,it,MED_NODE,MED_NO_GEOTYPE,&filter2,_fam_coords->getPointer());
389     }
390   else
391     _fam_coords=0;
392   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
393     {
394       _num_coords=DataArrayInt::New();
395       _num_coords->alloc(nbNodesToLoad,1);
396       MEDmeshEntityAttributeAdvancedRd(fid,mName.c_str(),MED_NUMBER,dt,it,MED_NODE,MED_NO_GEOTYPE,&filter2,_num_coords->getPointer());
397     }
398   else
399     _num_coords=0;
400   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NAME,MED_NODAL,&changement,&transformation)>0)
401     {
402       _name_coords=DataArrayAsciiChar::New();
403       _name_coords->alloc(nbNodesToLoad+1,MED_SNAME_SIZE);//not a bug to avoid the memory corruption due to last \0 at the end
404       MEDmeshEntityAttributeAdvancedRd(fid,mName.c_str(),MED_NAME,dt,it,MED_NODE,MED_NO_GEOTYPE,&filter2,_name_coords->getPointer());
405       _name_coords->reAlloc(nbNodesToLoad);//not a bug to avoid the memory corruption due to last \0 at the end
406     }
407   else
408     _name_coords=0;
409   MEDfilterClose(&filter2);
410   _coords->setInfoOnComponents(infosOnComp);
411 }
412
413 void MEDFileUMeshL2::sortTypes()
414 {
415   std::set<int> mdims;
416   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> > tmp(_per_type_mesh[0]);
417   _per_type_mesh.clear();
418   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=tmp.begin();it!=tmp.end();it++)
419     mdims.insert((*it)->getDim());
420   if(mdims.empty())
421     return;
422   int mdim=*mdims.rbegin();
423   _per_type_mesh.resize(mdim+1);
424   for(int dim=mdim+1;dim!=0;dim--)
425     {
426       std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >& elt=_per_type_mesh[mdim+1-dim];
427       for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=tmp.begin();it!=tmp.end();it++)
428         if((*it)->getDim()==dim-1)
429           elt.push_back(*it);
430     }
431   // suppression of contiguous empty levels at the end of _per_type_mesh.
432   int nbOfUselessLev=0;
433   bool isFirst=true;
434   for(std::vector< std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> > >::reverse_iterator it2=_per_type_mesh.rbegin();it2!=_per_type_mesh.rend();it2++)
435     {
436       if((*it2).empty() && isFirst)
437         {
438           nbOfUselessLev++;
439         }
440       else
441         isFirst=false;
442     }
443   _per_type_mesh.resize(_per_type_mesh.size()-nbOfUselessLev);
444 }
445
446 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)
447 {
448   if(!coords)
449     return ;
450   MEDmeshNodeCoordinateWr(fid,mname.c_str(),dt,it,time,MED_FULL_INTERLACE,coords->getNumberOfTuples(),coords->getConstPointer());
451   if(famCoords)
452     MEDmeshEntityFamilyNumberWr(fid,mname.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,famCoords->getNumberOfTuples(),famCoords->getConstPointer());
453   if(numCoords)
454     MEDmeshEntityNumberWr(fid,mname.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,numCoords->getNumberOfTuples(),numCoords->getConstPointer());
455   if(nameCoords)
456     {
457       if(nameCoords->getNumberOfComponents()!=MED_SNAME_SIZE)
458         {
459           std::ostringstream oss; oss << " MEDFileUMeshL2::WriteCoords : expected a name field on nodes with number of components set to " << MED_SNAME_SIZE;
460           oss << " ! The array has " << nameCoords->getNumberOfComponents() << " components !";
461           throw INTERP_KERNEL::Exception(oss.str().c_str());
462         }
463       MEDmeshEntityNameWr(fid,mname.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,nameCoords->getNumberOfTuples(),nameCoords->getConstPointer());
464     }
465 }
466
467 bool MEDFileUMeshL2::isFamDefinedOnLev(int levId) const
468 {
469   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
470     if((*it)->getFam()==0)
471       return false;
472   return true;
473 }
474
475 bool MEDFileUMeshL2::isNumDefinedOnLev(int levId) const
476 {
477   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
478     if((*it)->getNum()==0)
479       return false;
480   return true;
481 }
482
483 bool MEDFileUMeshL2::isNamesDefinedOnLev(int levId) const
484 {
485   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
486     if((*it)->getNames()==0)
487       return false;
488   return true;
489 }
490
491 MEDFileCMeshL2::MEDFileCMeshL2()
492 {
493 }
494
495 void MEDFileCMeshL2::loadAll(med_idt fid, int mId, const std::string& mName, int dt, int it)
496 {
497   _name.set(mName.c_str());
498   int nstep;
499   int Mdim;
500   ParaMEDMEM::MEDCouplingMeshType meshType;
501   std::vector<std::string> infosOnComp=getAxisInfoOnMesh(fid,mId,mName.c_str(),meshType,nstep,Mdim);
502   if(meshType!=CARTESIAN)
503     throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected a structured one whereas in file it is not a structured !");
504   _time=CheckMeshTimeStep(fid,mName,nstep,dt,it);
505   _iteration=dt;
506   _order=it;
507   //
508   med_grid_type gridtype;
509   MEDmeshGridTypeRd(fid,mName.c_str(),&gridtype);
510   if(gridtype!=MED_CARTESIAN_GRID)
511     throw INTERP_KERNEL::Exception("Invalid structured mesh ! Expected cartesian mesh type !");
512   _cmesh=MEDCouplingCMesh::New();
513   for(int i=0;i<Mdim;i++)
514     {
515       med_data_type dataTypeReq=GetDataTypeCorrespondingToSpaceId(i);
516       med_bool chgt=MED_FALSE,trsf=MED_FALSE;
517       int nbOfElt=MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NONE,dataTypeReq,MED_NO_CMODE,&chgt,&trsf);
518       MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> da=DataArrayDouble::New();
519       da->alloc(nbOfElt,1);
520       da->setInfoOnComponent(0,infosOnComp[i]);
521       MEDmeshGridIndexCoordinateRd(fid,mName.c_str(),dt,it,i+1,da->getPointer());
522       _cmesh->setCoordsAt(i,da);
523     }
524 }
525
526 med_data_type MEDFileCMeshL2::GetDataTypeCorrespondingToSpaceId(int id)
527 {
528   switch(id)
529   {
530     case 0:
531       return MED_COORDINATE_AXIS1;
532     case 1:
533       return MED_COORDINATE_AXIS2;
534     case 2:
535       return MED_COORDINATE_AXIS3;
536     default:
537       throw INTERP_KERNEL::Exception("Invalid meshdim detected in Cartesian Grid !");
538   }
539 }
540
541 MEDFileCLMeshL2::MEDFileCLMeshL2()
542 {
543 }
544
545 void MEDFileCLMeshL2::loadAll(med_idt fid, int mId, const std::string& mName, int dt, int it)
546 {
547   _name.set(mName.c_str());
548   int nstep;
549   int Mdim;
550   ParaMEDMEM::MEDCouplingMeshType meshType;
551   std::vector<std::string> infosOnComp=getAxisInfoOnMesh(fid,mId,mName,meshType,nstep,Mdim);
552   if(meshType!=CURVE_LINEAR)
553     throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected a structured one whereas in file it is not a structured !");
554   _time=CheckMeshTimeStep(fid,mName,nstep,dt,it);
555   _iteration=dt;
556   _order=it;
557   //
558   _clmesh=MEDCouplingCurveLinearMesh::New();
559   INTERP_KERNEL::AutoPtr<int> stGrid=new int[Mdim];
560   MEDmeshGridStructRd(fid,mName.c_str(),dt,it,stGrid);
561   _clmesh->setNodeGridStructure(stGrid,((int *)stGrid)+Mdim);
562   med_bool chgt=MED_FALSE,trsf=MED_FALSE;
563   int nbNodes=MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&chgt,&trsf);
564   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> da=DataArrayDouble::New();
565   da->alloc(nbNodes,infosOnComp.size());
566   da->setInfoOnComponents(infosOnComp);
567   MEDmeshNodeCoordinateRd(fid,mName.c_str(),dt,it,MED_FULL_INTERLACE,da->getPointer());
568   _clmesh->setCoords(da);
569 }
570
571 MEDFileUMeshPermCompute::MEDFileUMeshPermCompute(const MEDFileUMeshSplitL1* st):_st(st),_mpt_time(0),_num_time(0)
572 {
573 }
574
575 /*!
576  * Warning it returns an instance to deallocate !!!!
577  */
578 MEDFileUMeshPermCompute::operator MEDCouplingUMesh *() const
579 {
580   _st->_num->updateTime();
581   if((MEDCouplingUMesh *)_m==0)
582     {
583       updateTime();
584       _m=static_cast<MEDCouplingUMesh *>(_st->_m_by_types.getUmesh()->deepCpy());
585       _m->renumberCells(_st->_num->getConstPointer(),true);
586       return _m.retn();
587     }
588   else
589     {
590       if(_mpt_time==_st->_m_by_types.getTimeOfThis() && _num_time==_st->_num->getTimeOfThis())
591         return _m.retn();
592       else
593         {
594           updateTime();
595           _m=static_cast<MEDCouplingUMesh *>(_st->_m_by_types.getUmesh()->deepCpy());
596           _m->renumberCells(_st->_num->getConstPointer(),true);
597           return _m.retn();
598         }
599     }
600 }
601
602 void MEDFileUMeshPermCompute::operator=(MEDCouplingUMesh *m)
603 {
604   _m=m;
605 }
606
607 void MEDFileUMeshPermCompute::updateTime() const
608 {
609   _mpt_time=_st->_m_by_types.getTimeOfThis();
610   _num_time=_st->_num->getTimeOfThis();
611 }
612
613 std::vector<const BigMemoryObject *> MEDFileUMeshPermCompute::getDirectChildrenWithNull() const
614 {
615   std::vector<const BigMemoryObject *> ret;
616   ret.push_back((const MEDCouplingUMesh *)_m);
617   return ret;
618 }
619
620 std::size_t MEDFileUMeshPermCompute::getHeapMemorySizeWithoutChildren() const
621 {
622   return sizeof(MEDFileUMeshPermCompute);
623 }
624
625 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)
626 {
627 }
628
629 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(const MEDFileUMeshL2& l2, const std::string& mName, int id):_m(this)
630 {
631   const std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >& v=l2.getLev(id);
632   if(v.empty())
633     return;
634   int sz=v.size();
635   std::vector<const MEDCoupling1GTUMesh *> ms(sz);
636   std::vector<const DataArrayInt *> fams(sz),nums(sz);
637   std::vector<const DataArrayChar *> names(sz); 
638   for(int i=0;i<sz;i++)
639     {
640       MEDCoupling1GTUMesh *elt(v[i]->getMesh());
641       MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> tmp2=l2.getCoords();
642       elt->setCoords(tmp2);
643       ms[i]=elt;
644     }
645   _m_by_types.assignParts(ms);
646   if(l2.isFamDefinedOnLev(id))
647     {
648       for(int i=0;i<sz;i++)
649         fams[i]=v[i]->getFam();
650       if(sz!=1)
651         _fam=DataArrayInt::Aggregate(fams);
652       else
653         {
654           fams[0]->incrRef();
655           _fam=const_cast<DataArrayInt *>(fams[0]);
656         }
657     }
658   if(l2.isNumDefinedOnLev(id))
659     {
660       for(int i=0;i<sz;i++)
661         nums[i]=v[i]->getNum();
662       if(sz!=1)
663         _num=DataArrayInt::Aggregate(nums);
664       else
665         {
666           nums[0]->incrRef();
667           _num=const_cast<DataArrayInt *>(nums[0]);
668         }
669       computeRevNum();
670     }
671   if(l2.isNamesDefinedOnLev(id))
672     {
673       for(int i=0;i<sz;i++)
674         names[i]=v[i]->getNames();
675       _names=dynamic_cast<DataArrayAsciiChar *>(DataArrayChar::Aggregate(names));
676     }
677 }
678
679 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCoupling1GTUMesh *m):_m(this)
680 {
681   std::vector< const MEDCoupling1GTUMesh * > v(1);
682   v[0]=m;
683   assignParts(v);
684 }
685
686 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCouplingUMesh *m):_m(this)
687 {
688   assignMesh(m,true);
689 }
690
691 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCouplingUMesh *m, bool newOrOld):_m(this)
692 {
693   assignMesh(m,newOrOld);
694 }
695
696 void MEDFileUMeshSplitL1::setName(const std::string& name)
697 {
698   _m_by_types.setName(name);
699 }
700
701 std::size_t MEDFileUMeshSplitL1::getHeapMemorySizeWithoutChildren() const
702 {
703   return 0;
704 }
705
706 std::vector<const BigMemoryObject *> MEDFileUMeshSplitL1::getDirectChildrenWithNull() const
707 {
708   std::vector<const BigMemoryObject *> ret;
709   ret.push_back(&_m_by_types);
710   ret.push_back(&_m);
711   ret.push_back((const DataArrayInt*)_fam);
712   ret.push_back((const DataArrayInt*)_num);
713   ret.push_back((const DataArrayInt*)_rev_num);
714   ret.push_back((const DataArrayAsciiChar*)_names);
715   return ret;
716 }
717
718 MEDFileUMeshSplitL1 *MEDFileUMeshSplitL1::deepCpy(DataArrayDouble *coords) const
719 {
720   MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshSplitL1> ret=new MEDFileUMeshSplitL1(*this);
721   ret->_m_by_types=_m_by_types.deepCpy(coords);
722   if((const DataArrayInt *)_fam)
723     ret->_fam=_fam->deepCpy();
724   if((const DataArrayInt *)_num)
725     ret->_num=_num->deepCpy();
726   if((const DataArrayInt *)_rev_num)
727     ret->_rev_num=_rev_num->deepCpy();
728   if((const DataArrayAsciiChar *)_names)
729     ret->_names=_names->deepCpy();
730   return ret.retn();
731 }
732
733 bool MEDFileUMeshSplitL1::isEqual(const MEDFileUMeshSplitL1 *other, double eps, std::string& what) const
734 {
735   if(!_m_by_types.isEqual(other->_m_by_types,eps,what))
736     return false;
737   const DataArrayInt *d1=_fam;
738   const DataArrayInt *d2=other->_fam;
739   if((d1==0 && d2!=0) || (d1!=0 && d2==0))
740     {
741       what="Presence of family arr in one sublevel and not in other!";
742       return false;
743     }
744   if(d1)
745     if(!d1->isEqual(*d2))
746       {
747         what="family arr at a sublevel are not deeply equal !";
748         return false;
749       }
750   d1=_num;
751   d2=other->_num;
752   if((d1==0 && d2!=0) || (d1!=0 && d2==0))
753     {
754       what="Presence of cell numbering arr in one sublevel and not in other!";
755       return false;
756     }
757   if(d1)
758     if(!d1->isEqual(*d2))
759       {
760         what="Numbering cell arr at a sublevel are not deeply equal !";
761         return false;
762       }
763   const DataArrayAsciiChar *e1=_names;
764   const DataArrayAsciiChar *e2=other->_names;
765   if((e1==0 && e2!=0) || (e1!=0 && e2==0))
766     {
767       what="Presence of cell names arr in one sublevel and not in other!";
768       return false;
769     }
770   if(e1)
771     if(!e1->isEqual(*e2))
772       {
773         what="Name cell arr at a sublevel are not deeply equal !";
774         return false;
775       }
776   return true;
777 }
778
779 void MEDFileUMeshSplitL1::synchronizeTinyInfo(const MEDFileMesh& master) const
780 {
781   _m_by_types.synchronizeTinyInfo(master);
782 }
783
784 void MEDFileUMeshSplitL1::clearNonDiscrAttributes() const
785 {
786   _m_by_types.clearNonDiscrAttributes();
787 }
788
789 void MEDFileUMeshSplitL1::ClearNonDiscrAttributes(const MEDCouplingMesh *tmp)
790 {
791   if(!tmp)
792     return ;
793   (const_cast<MEDCouplingMesh *>(tmp))->setName("");
794   (const_cast<MEDCouplingMesh *>(tmp))->setDescription("");
795   (const_cast<MEDCouplingMesh *>(tmp))->setTime(0.,-1,-1);
796   (const_cast<MEDCouplingMesh *>(tmp))->setTimeUnit("");
797 }
798
799 void MEDFileUMeshSplitL1::setCoords(DataArrayDouble *coords)
800 {
801   _m_by_types.setCoords(coords);
802 }
803
804 void MEDFileUMeshSplitL1::assignMesh(MEDCouplingUMesh *m, bool newOrOld)
805 {
806   if(newOrOld)
807     {
808       m->incrRef();
809       _m=m;
810       _m_by_types.assignUMesh(dynamic_cast<MEDCouplingUMesh *>(m->deepCpy()));
811       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=_m_by_types.getUmesh()->getRenumArrForConsecutiveCellTypesSpec(typmai2,typmai2+MED_N_CELL_FIXED_GEO);
812       if(!da->isIdentity())
813         {
814           _num=da->invertArrayO2N2N2O(m->getNumberOfCells());
815           _m.updateTime();
816           computeRevNum();
817           _m_by_types.getUmesh()->renumberCells(da->getConstPointer(),false);
818         }
819     }
820   else
821     {
822       if(!m->checkConsecutiveCellTypesAndOrder(typmai2,typmai2+MED_N_CELL_FIXED_GEO))
823         throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::assignMesh : the mode of mesh setting expects to follow the MED file numbering convention ! it is not the case !");
824       m->incrRef();
825       _m_by_types.assignUMesh(m);
826     }
827   assignCommonPart();
828 }
829
830 void MEDFileUMeshSplitL1::forceComputationOfParts() const
831 {
832   _m_by_types.forceComputationOfPartsFromUMesh();
833 }
834
835 void MEDFileUMeshSplitL1::assignParts(const std::vector< const MEDCoupling1GTUMesh * >& mParts)
836 {
837   _m_by_types.assignParts(mParts);
838   assignCommonPart();
839 }
840
841 void MEDFileUMeshSplitL1::assignCommonPart()
842 {
843   _fam=DataArrayInt::New();
844   _fam->alloc(_m_by_types.getSize(),1);
845   _fam->fillWithValue(0);
846 }
847
848 bool MEDFileUMeshSplitL1::empty() const
849 {
850   return _m_by_types.empty();
851 }
852
853 bool MEDFileUMeshSplitL1::presenceOfOneFams(const std::vector<int>& ids) const
854 {
855   const DataArrayInt *fam=_fam;
856   if(!fam)
857     return false;
858   return fam->presenceOfValue(ids);
859 }
860
861 int MEDFileUMeshSplitL1::getMeshDimension() const
862 {
863   return _m_by_types.getMeshDimension();
864 }
865
866 void MEDFileUMeshSplitL1::simpleRepr(std::ostream& oss) const
867 {
868   std::vector<int> code=_m_by_types.getDistributionOfTypes();
869   int nbOfTypes=code.size()/3;
870   for(int i=0;i<nbOfTypes;i++)
871     {
872       INTERP_KERNEL::NormalizedCellType typ=(INTERP_KERNEL::NormalizedCellType) code[3*i];
873       oss << "    - Number of cells with type " << INTERP_KERNEL::CellModel::GetCellModel(typ).getRepr() << " : " << code[3*i+1] << std::endl;
874     }
875 }
876
877 int MEDFileUMeshSplitL1::getSize() const
878 {
879   return _m_by_types.getSize();
880 }
881
882 MEDCouplingUMesh *MEDFileUMeshSplitL1::getFamilyPart(const int *idsBg, const int *idsEnd, bool renum) const
883 {
884   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> eltsToKeep=_fam->getIdsEqualList(idsBg,idsEnd);
885   MEDCouplingUMesh *m=(MEDCouplingUMesh *)_m_by_types.getUmesh()->buildPartOfMySelf(eltsToKeep->getConstPointer(),eltsToKeep->getConstPointer()+eltsToKeep->getNumberOfTuples(),true);
886   if(renum)
887     return renumIfNeeded(m,eltsToKeep->getConstPointer());
888   return m;
889 }
890
891 DataArrayInt *MEDFileUMeshSplitL1::getFamilyPartArr(const int *idsBg, const int *idsEnd, bool renum) const
892 {
893   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=_fam->getIdsEqualList(idsBg,idsEnd);
894   if(renum)
895     return renumIfNeededArr(da);
896   return da.retn();
897 }
898
899 std::vector<INTERP_KERNEL::NormalizedCellType> MEDFileUMeshSplitL1::getGeoTypes() const
900 {
901   return _m_by_types.getGeoTypes();
902 }
903
904 MEDCouplingUMesh *MEDFileUMeshSplitL1::getWholeMesh(bool renum) const
905 {
906   MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> tmp;
907   if(renum && ((const DataArrayInt *)_num))
908     tmp=_m;
909   else
910     { tmp=_m_by_types.getUmesh(); if(tmp) tmp->incrRef(); }
911   return tmp.retn();
912 }
913
914 DataArrayInt *MEDFileUMeshSplitL1::extractFamilyFieldOnGeoType(INTERP_KERNEL::NormalizedCellType gt) const
915 {
916   const DataArrayInt *fam(_fam);
917   if(!fam)
918     return 0;
919   int start(0),stop(0);
920   _m_by_types.getStartStopOfGeoTypeWithoutComputation(gt,start,stop);
921   return fam->selectByTupleId2(start,stop,1);
922 }
923
924 DataArrayInt *MEDFileUMeshSplitL1::extractNumberFieldOnGeoType(INTERP_KERNEL::NormalizedCellType gt) const
925 {
926   const DataArrayInt *num(_num);
927   if(!num)
928     return 0;
929   int start(0),stop(0);
930   _m_by_types.getStartStopOfGeoTypeWithoutComputation(gt,start,stop);
931   return num->selectByTupleId2(start,stop,1);
932 }
933
934 DataArrayInt *MEDFileUMeshSplitL1::getOrCreateAndGetFamilyField()
935 {
936   if((DataArrayInt *)_fam)
937     return _fam;
938   int nbOfTuples=_m_by_types.getSize();
939   _fam=DataArrayInt::New(); _fam->alloc(nbOfTuples,1); _fam->fillWithZero();
940   return _fam;
941 }
942
943 const DataArrayInt *MEDFileUMeshSplitL1::getFamilyField() const
944 {
945   return _fam;
946 }
947
948 const DataArrayInt *MEDFileUMeshSplitL1::getNumberField() const
949 {
950   return _num;
951 }
952
953 const DataArrayInt *MEDFileUMeshSplitL1::getRevNumberField() const
954 {
955   return _rev_num;
956 }
957
958 const DataArrayAsciiChar *MEDFileUMeshSplitL1::getNameField() const
959 {
960   return _names;
961 }
962
963 void MEDFileUMeshSplitL1::eraseFamilyField()
964 {
965   _fam->fillWithZero();
966 }
967
968 /*!
969  * This method ignores _m and _m_by_types.
970  */
971 void MEDFileUMeshSplitL1::setGroupsFromScratch(const std::vector<const MEDCouplingUMesh *>& ms, std::map<std::string,int>& familyIds,
972                                                std::map<std::string, std::vector<std::string> >& groups)
973 {
974   std::vector< DataArrayInt * > corr;
975   _m=MEDCouplingUMesh::FuseUMeshesOnSameCoords(ms,0,corr);
976   std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> > corrMSafe(corr.begin(),corr.end());
977   std::vector< std::vector<int> > fidsOfGroups;
978   std::vector< const DataArrayInt * > corr2(corr.begin(),corr.end());
979   _fam=DataArrayInt::MakePartition(corr2,((MEDCouplingUMesh *)_m)->getNumberOfCells(),fidsOfGroups);
980   int nbOfCells=((MEDCouplingUMesh *)_m)->getNumberOfCells();
981   std::map<int,std::string> newfams;
982   std::map<int,int> famIdTrad;
983   TraduceFamilyNumber(fidsOfGroups,familyIds,famIdTrad,newfams);
984   int *w=_fam->getPointer();
985   for(int i=0;i<nbOfCells;i++,w++)
986     *w=famIdTrad[*w];
987 }
988
989 void MEDFileUMeshSplitL1::write(med_idt fid, const std::string& mName, int mdim) const
990 {
991   std::vector<MEDCoupling1GTUMesh *> ms(_m_by_types.getParts());
992   int start=0;
993   for(std::vector<MEDCoupling1GTUMesh *>::const_iterator it=ms.begin();it!=ms.end();it++)
994     {
995       int nbCells=(*it)->getNumberOfCells();
996       int end=start+nbCells;
997       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> fam,num;
998       MEDCouplingAutoRefCountObjectPtr<DataArrayAsciiChar> names;
999       if((const DataArrayInt *)_fam)
1000         fam=_fam->substr(start,end);
1001       if((const DataArrayInt *)_num)
1002         num=_num->substr(start,end);
1003       if((const DataArrayAsciiChar *)_names)
1004         names=static_cast<DataArrayAsciiChar *>(_names->substr(start,end));
1005       MEDFileUMeshPerType::Write(fid,mName,mdim,(*it),fam,num,names);
1006       start=end;
1007     }
1008 }
1009
1010 void MEDFileUMeshSplitL1::renumberNodesInConn(const int *newNodeNumbersO2N)
1011 {
1012   MEDCouplingUMesh *m(_m_by_types.getUmesh());
1013   if(!m)
1014     return;
1015   m->renumberNodesInConn(newNodeNumbersO2N);
1016 }
1017
1018 void MEDFileUMeshSplitL1::changeFamilyIdArr(int oldId, int newId)
1019 {
1020   DataArrayInt *arr=_fam;
1021   if(arr)
1022     arr->changeValue(oldId,newId);
1023 }
1024
1025 void MEDFileUMeshSplitL1::setFamilyArr(DataArrayInt *famArr)
1026 {
1027   if(!famArr)
1028     {
1029       _fam=0;
1030       return ;
1031     }
1032   int sz(_m_by_types.getSize());
1033   famArr->checkNbOfTuplesAndComp(sz,1,"MEDFileUMeshSplitL1::setFamilyArr : Problem in size of Family arr ! ");
1034   famArr->incrRef();
1035   _fam=famArr;
1036 }
1037
1038 void MEDFileUMeshSplitL1::setRenumArr(DataArrayInt *renumArr)
1039 {
1040   if(!renumArr)
1041     {
1042       _num=0;
1043       _rev_num=0;
1044       return ;
1045     }
1046   int sz(_m_by_types.getSize());
1047   renumArr->checkNbOfTuplesAndComp(sz,1,"MEDFileUMeshSplitL1::setRenumArr : Problem in size of numbering arr ! ");
1048   renumArr->incrRef();
1049   _num=renumArr;
1050   computeRevNum();
1051 }
1052
1053 void MEDFileUMeshSplitL1::setNameArr(DataArrayAsciiChar *nameArr)
1054 {
1055   if(!nameArr)
1056     {
1057       _names=0;
1058       return ;
1059     }
1060   int sz(_m_by_types.getSize());
1061   nameArr->checkNbOfTuplesAndComp(sz,MED_SNAME_SIZE,"MEDFileUMeshSplitL1::setNameArr : Problem in size of name arr ! ");
1062   nameArr->incrRef();
1063   _names=nameArr;
1064 }
1065
1066 MEDCouplingUMesh *MEDFileUMeshSplitL1::Renumber2(const DataArrayInt *renum, MEDCouplingUMesh *m, const int *cellIds)
1067 {
1068   if(renum==0)
1069     return m;
1070   if(cellIds==0)
1071     m->renumberCells(renum->getConstPointer(),true);
1072   else
1073     {
1074       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> locnum=renum->selectByTupleId(cellIds,cellIds+m->getNumberOfCells());
1075       m->renumberCells(locnum->getConstPointer(),true);
1076     }
1077   return m;
1078 }
1079
1080 MEDCouplingUMesh *MEDFileUMeshSplitL1::renumIfNeeded(MEDCouplingUMesh *m, const int *cellIds) const
1081 {
1082   return Renumber2(_num,m,cellIds);
1083 }
1084
1085 DataArrayInt *MEDFileUMeshSplitL1::Renumber(const DataArrayInt *renum, const DataArrayInt *da)
1086 {
1087   if((const DataArrayInt *)renum==0)
1088     {
1089       da->incrRef();
1090       return const_cast<DataArrayInt *>(da);
1091     }
1092   return renum->selectByTupleId(da->getConstPointer(),da->getConstPointer()+da->getNumberOfTuples());
1093 }
1094
1095 DataArrayInt *MEDFileUMeshSplitL1::renumIfNeededArr(const DataArrayInt *da) const
1096 {
1097   return Renumber(_num,da);
1098 }
1099
1100 std::vector<int> MEDFileUMeshSplitL1::GetNewFamiliesNumber(int nb, const std::map<std::string,int>& families)
1101 {
1102   int id=-1;
1103   for(std::map<std::string,int>::const_iterator it=families.begin();it!=families.end();it++)
1104     id=std::max(id,(*it).second);
1105   if(id==-1)
1106     id=0;
1107   std::vector<int> ret(nb);
1108   for(int i=1;i<=nb;i++)
1109     ret[i]=id+i;
1110   return ret;
1111 }
1112
1113 void MEDFileUMeshSplitL1::TraduceFamilyNumber(const std::vector< std::vector<int> >& fidsGrps, std::map<std::string,int>& familyIds,
1114                                               std::map<int,int>& famIdTrad, std::map<int,std::string>& newfams)
1115 {
1116   std::set<int> allfids;
1117   //tony
1118 }
1119
1120 void MEDFileUMeshSplitL1::computeRevNum() const
1121 {
1122   int pos;
1123   int maxValue=_num->getMaxValue(pos);
1124   _rev_num=_num->invertArrayN2O2O2N(maxValue+1);
1125 }
1126
1127 //=
1128
1129 MEDFileUMeshAggregateCompute::MEDFileUMeshAggregateCompute():_mp_time(0),_m_time(0)
1130 {
1131 }
1132
1133 void MEDFileUMeshAggregateCompute::setName(const std::string& name)
1134 {
1135   if(_m_time>=_mp_time)
1136     {
1137       MEDCouplingUMesh *um(_m);
1138       if(um)
1139         um->setName(name);
1140     }
1141   if(_mp_time>=_m_time)
1142     {
1143       for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1144         {
1145           MEDCoupling1GTUMesh *tmp(*it);
1146           if(tmp)
1147             tmp->setName(name);
1148         }
1149     }
1150 }
1151
1152 void MEDFileUMeshAggregateCompute::assignParts(const std::vector< const MEDCoupling1GTUMesh * >& mParts)
1153 {
1154   std::size_t sz(mParts.size());
1155   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> > ret(sz);
1156   for(std::size_t i=0;i<sz;i++)
1157     {
1158       const MEDCoupling1GTUMesh *elt(mParts[i]);
1159       if(!elt)
1160         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::assignParts : presence of null pointer !");
1161       ret[i]=const_cast<MEDCoupling1GTUMesh *>(elt); elt->incrRef();
1162     }
1163   _m_parts=ret;
1164   _mp_time=std::max(_mp_time,_m_time)+1;
1165   _m=0;
1166 }
1167
1168 void MEDFileUMeshAggregateCompute::assignUMesh(MEDCouplingUMesh *m)
1169 {
1170   _m=m;
1171   _m_parts.clear();
1172   _m_time=std::max(_mp_time,_m_time)+1;
1173 }
1174
1175 MEDCouplingUMesh *MEDFileUMeshAggregateCompute::getUmesh() const
1176 {
1177   if(_mp_time<=_m_time)
1178     return _m;
1179   std::vector< const MEDCoupling1GTUMesh *> mp(_m_parts.size());
1180   std::copy(_m_parts.begin(),_m_parts.end(),mp.begin());
1181   _m=MEDCoupling1GTUMesh::AggregateOnSameCoordsToUMesh(mp);
1182   _m_parts.clear();//to avoid memory peak !
1183   _m_time=_mp_time+1;//+1 is important ! That is to say that only _m is OK not _m_parts because cleared !
1184   return _m;
1185 }
1186
1187 std::vector<INTERP_KERNEL::NormalizedCellType> MEDFileUMeshAggregateCompute::getGeoTypes() const
1188 {
1189   if(_mp_time>=_m_time)
1190     {
1191       std::size_t sz(_m_parts.size());
1192       std::vector<INTERP_KERNEL::NormalizedCellType> ret(sz);
1193       for(std::size_t i=0;i<sz;i++)
1194         ret[i]=_m_parts[i]->getCellModelEnum();
1195       return ret;
1196     }
1197   else
1198     return _m->getAllGeoTypesSorted();
1199 }
1200
1201 std::vector<MEDCoupling1GTUMesh *> MEDFileUMeshAggregateCompute::getPartsWithoutComputation() const
1202 {
1203   if(_mp_time<_m_time)
1204     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getPartsWithoutComputation : the parts require a computation !");
1205   //
1206   std::vector<MEDCoupling1GTUMesh *> ret(_m_parts.size());
1207   std::size_t i(0);
1208   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++,i++)
1209     {
1210       const MEDCoupling1GTUMesh *elt(*it);
1211       ret[i]=const_cast<MEDCoupling1GTUMesh *>(elt);
1212     }
1213   return ret;
1214 }
1215
1216 std::vector<MEDCoupling1GTUMesh *> MEDFileUMeshAggregateCompute::getParts() const
1217 {
1218   if(_mp_time<_m_time)
1219     forceComputationOfPartsFromUMesh();
1220   return getPartsWithoutComputation();
1221 }
1222
1223 MEDCoupling1GTUMesh *MEDFileUMeshAggregateCompute::getPartWithoutComputation(INTERP_KERNEL::NormalizedCellType gt) const
1224 {
1225   std::vector<MEDCoupling1GTUMesh *> v(getPartsWithoutComputation());
1226   std::size_t sz(v.size());
1227   for(std::size_t i=0;i<sz;i++)
1228     {
1229       if(v[i])
1230         if(v[i]->getCellModelEnum()==gt)
1231           return v[i];
1232     }
1233   throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getPartWithoutComputation : the geometric type is not existing !");
1234 }
1235
1236 void MEDFileUMeshAggregateCompute::getStartStopOfGeoTypeWithoutComputation(INTERP_KERNEL::NormalizedCellType gt, int& start, int& stop) const
1237 {
1238   start=0; stop=0;
1239   std::vector<MEDCoupling1GTUMesh *> v(getPartsWithoutComputation());
1240   std::size_t sz(v.size());
1241   for(std::size_t i=0;i<sz;i++)
1242     {
1243       if(v[i])
1244         {
1245           if(v[i]->getCellModelEnum()==gt)
1246             {
1247               stop=start+v[i]->getNumberOfCells();
1248               return;
1249             }
1250           else
1251             start+=v[i]->getNumberOfCells();
1252         }
1253     }
1254   throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getStartStopOfGeoTypeWithoutComputation : the geometric type is not existing !");
1255 }
1256
1257 void MEDFileUMeshAggregateCompute::forceComputationOfPartsFromUMesh() const
1258 {
1259   const MEDCouplingUMesh *m(_m);
1260   if(!m)
1261     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::forceComputationOfPartsFromUMesh : null UMesh !");
1262   std::vector<MEDCouplingUMesh *> ms(m->splitByType());
1263   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> > msMSafe(ms.begin(),ms.end());
1264   std::size_t sz(msMSafe.size());
1265   _m_parts.resize(sz);
1266   for(std::size_t i=0;i<sz;i++)
1267     _m_parts[i]=MEDCoupling1GTUMesh::New(ms[i]);
1268   _mp_time=std::max(_mp_time,_m_time);
1269 }
1270
1271 std::size_t MEDFileUMeshAggregateCompute::getTimeOfThis() const
1272 {
1273   if(_mp_time>_m_time)
1274     return getTimeOfParts();
1275   if(_m_time>_mp_time)
1276     return getTimeOfUMesh();
1277   return std::max(getTimeOfParts(),getTimeOfUMesh());
1278 }
1279
1280 std::size_t MEDFileUMeshAggregateCompute::getTimeOfParts() const
1281 {
1282   std::size_t ret(0);
1283   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1284     {
1285       const MEDCoupling1GTUMesh *elt(*it);
1286       if(!elt)
1287         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getTimeOfParts : null obj in parts !");
1288       ret=std::max(ret,elt->getTimeOfThis());
1289     }
1290   if(ret==0)
1291     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getTimeOfParts : parts is empty !");
1292   return ret;
1293 }
1294
1295 std::size_t MEDFileUMeshAggregateCompute::getTimeOfUMesh() const
1296 {
1297   const MEDCouplingUMesh *m(_m);
1298   if(!m)
1299     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getTimeOfUMesh : unmesh is null !");
1300   return m->getTimeOfThis();
1301 }
1302
1303 std::size_t MEDFileUMeshAggregateCompute::getHeapMemorySizeWithoutChildren() const
1304 {
1305   std::size_t ret(_m_parts.size()*sizeof(MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh>));
1306   return ret;
1307 }
1308
1309 std::vector<const BigMemoryObject *> MEDFileUMeshAggregateCompute::getDirectChildrenWithNull() const
1310 {
1311   std::vector<const BigMemoryObject *> ret;
1312   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1313     ret.push_back((const MEDCoupling1GTUMesh *)*it);
1314   ret.push_back((const MEDCouplingUMesh *)_m);
1315   return ret;
1316 }
1317
1318 MEDFileUMeshAggregateCompute MEDFileUMeshAggregateCompute::deepCpy(DataArrayDouble *coords) const
1319 {
1320   MEDFileUMeshAggregateCompute ret;
1321   ret._m_parts.resize(_m_parts.size());
1322   for(std::size_t i=0;i<_m_parts.size();i++)
1323     {
1324       const MEDCoupling1GTUMesh *elt(_m_parts[i]);
1325       if(elt)
1326         {
1327           ret._m_parts[i]=static_cast<ParaMEDMEM::MEDCoupling1GTUMesh*>(elt->deepCpy());
1328           ret._m_parts[i]->setCoords(coords);
1329         }
1330     }
1331   ret._mp_time=_mp_time; ret._m_time=_m_time;
1332   if((const MEDCouplingUMesh *)_m)
1333     {
1334       ret._m=static_cast<ParaMEDMEM::MEDCouplingUMesh*>(_m->deepCpy());
1335       ret._m->setCoords(coords);
1336     }
1337   return ret;
1338 }
1339
1340 bool MEDFileUMeshAggregateCompute::isEqual(const MEDFileUMeshAggregateCompute& other, double eps, std::string& what) const
1341 {
1342   const MEDCouplingUMesh *m1(getUmesh());
1343   const MEDCouplingUMesh *m2(other.getUmesh());
1344   if((m1==0 && m2!=0) || (m1!=0 && m2==0))
1345     {
1346       what="Presence of mesh in one sublevel and not in other!";
1347       return false;
1348     }
1349   if(m1)
1350     {
1351       std::string what2;
1352       if(!m1->isEqualIfNotWhy(m2,eps,what2))
1353         {
1354           what=std::string("meshes at a sublevel are not deeply equal (")+what2+std::string(")!");
1355           return false;
1356         }
1357     }
1358   return true;
1359 }
1360
1361 void MEDFileUMeshAggregateCompute::clearNonDiscrAttributes() const
1362 {
1363   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1364     MEDFileUMeshSplitL1::ClearNonDiscrAttributes(*it);
1365   MEDFileUMeshSplitL1::ClearNonDiscrAttributes(_m);
1366 }
1367
1368 void MEDFileUMeshAggregateCompute::synchronizeTinyInfo(const MEDFileMesh& master) const
1369 {
1370   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1371     {
1372       const MEDCoupling1GTUMesh *tmp(*it);
1373       if(tmp)
1374         {
1375           (const_cast<MEDCoupling1GTUMesh *>(tmp))->setName(master.getName().c_str());
1376           (const_cast<MEDCoupling1GTUMesh *>(tmp))->setDescription(master.getDescription().c_str());
1377           (const_cast<MEDCoupling1GTUMesh *>(tmp))->setTime(master.getTimeValue(),master.getIteration(),master.getOrder());
1378           (const_cast<MEDCoupling1GTUMesh *>(tmp))->setTimeUnit(master.getTimeUnit());
1379         }
1380     }
1381   const MEDCouplingUMesh *m(_m);
1382   if(m)
1383     {
1384       (const_cast<MEDCouplingUMesh *>(m))->setName(master.getName().c_str());
1385       (const_cast<MEDCouplingUMesh *>(m))->setDescription(master.getDescription().c_str());
1386       (const_cast<MEDCouplingUMesh *>(m))->setTime(master.getTimeValue(),master.getIteration(),master.getOrder());
1387       (const_cast<MEDCouplingUMesh *>(m))->setTimeUnit(master.getTimeUnit());
1388     }
1389 }
1390
1391 bool MEDFileUMeshAggregateCompute::empty() const
1392 {
1393   if(_mp_time<_m_time)
1394     return ((const MEDCouplingUMesh *)_m)==0;
1395   //else _mp_time>=_m_time)
1396   return _m_parts.empty();
1397 }
1398
1399 int MEDFileUMeshAggregateCompute::getMeshDimension() const
1400 {
1401   if(_mp_time<_m_time)
1402     {
1403       const MEDCouplingUMesh *m(_m);
1404       if(!m)
1405         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getMeshDimension : no umesh in this !");
1406       return m->getMeshDimension();
1407     }
1408   else
1409     {
1410       if(_m_parts.empty())
1411         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getMeshDimension : part mesh is empty !");
1412       const MEDCoupling1GTUMesh *m(_m_parts[0]);
1413       if(!m)
1414         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getMeshDimension : part mesh contains null instance !");
1415       return m->getMeshDimension();
1416     }
1417 }
1418
1419 std::vector<int> MEDFileUMeshAggregateCompute::getDistributionOfTypes() const
1420 {
1421   if(_mp_time<_m_time)
1422     {
1423       const MEDCouplingUMesh *m(_m);
1424       if(!m)
1425         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getDistributionOfTypes : no umesh in this !");
1426       return m->getDistributionOfTypes();
1427     }
1428   else
1429     {
1430       std::vector<int> ret;
1431       for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1432         {
1433           const MEDCoupling1GTUMesh *tmp(*it);
1434           if(!tmp)
1435             throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getDistributionOfTypes : part mesh contains null instance !");
1436           std::vector<int> ret0(tmp->getDistributionOfTypes());
1437           ret.insert(ret.end(),ret0.begin(),ret0.end());
1438         }
1439       return ret;
1440     }
1441 }
1442
1443 int MEDFileUMeshAggregateCompute::getSize() const
1444 {
1445   if(_mp_time<_m_time)
1446     {
1447       const MEDCouplingUMesh *m(_m);
1448       if(!m)
1449         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getSize : no umesh in this !");
1450       return m->getNumberOfCells();
1451     }
1452   else
1453     {
1454       int ret=0;
1455       for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1456         {
1457           const MEDCoupling1GTUMesh *m(*it);
1458           if(!m)
1459             throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getSize : part mesh contains null instance !");
1460           ret+=m->getNumberOfCells();
1461         }
1462       return ret;
1463     }
1464 }
1465
1466 void MEDFileUMeshAggregateCompute::setCoords(DataArrayDouble *coords)
1467 {
1468   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1469     {
1470       MEDCoupling1GTUMesh *tmp(*it);
1471       if(tmp)
1472         (*it)->setCoords(coords);
1473     }
1474   MEDCouplingUMesh *m(_m);
1475   if(m)
1476     m->setCoords(coords);
1477 }