Salome HOME
Improvements linked to debug EDF 7972
[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::getDirectChildren() 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 void MEDFileUMeshL2::loadAll(med_idt fid, int mId, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs)
247 {
248   _name.set(mName.c_str());
249   int nstep;
250   int Mdim;
251   ParaMEDMEM::MEDCouplingMeshType meshType;
252   std::vector<std::string> infosOnComp=getAxisInfoOnMesh(fid,mId,mName.c_str(),meshType,nstep,Mdim);
253   if(nstep==0)
254     return ;
255   if(meshType!=UNSTRUCTURED)
256     throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected an unstructured one whereas in file it is not an unstructured !");
257   _time=CheckMeshTimeStep(fid,mName,nstep,dt,it);
258   _iteration=dt;
259   _order=it;
260   loadConnectivity(fid,Mdim,mName,dt,it,mrs);//to improve check (dt,it) coherency
261   loadCoords(fid,mId,infosOnComp,mName,dt,it);
262 }
263
264 void MEDFileUMeshL2::loadConnectivity(med_idt fid, int mdim, const std::string& mName, int dt, int it, MEDFileMeshReadSelector *mrs)
265 {
266   _per_type_mesh.resize(1);
267   _per_type_mesh[0].clear();
268   for(int j=0;j<MED_N_CELL_FIXED_GEO;j++)
269     {
270       MEDFileUMeshPerType *tmp=MEDFileUMeshPerType::New(fid,mName.c_str(),dt,it,mdim,typmai[j],typmai2[j],mrs);
271       if(tmp)
272         _per_type_mesh[0].push_back(tmp);
273     }
274   sortTypes();
275 }
276
277 void MEDFileUMeshL2::loadCoords(med_idt fid, int mId, const std::vector<std::string>& infosOnComp, const std::string& mName, int dt, int it)
278 {
279   int spaceDim=infosOnComp.size();
280   med_bool changement,transformation;
281   int nCoords=MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&changement,&transformation);
282   _coords=DataArrayDouble::New();
283   _coords->alloc(nCoords,spaceDim);
284   double *coordsPtr=_coords->getPointer();
285   MEDmeshNodeCoordinateRd(fid,mName.c_str(),dt,it,MED_FULL_INTERLACE,coordsPtr);
286   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_FAMILY_NUMBER,MED_NODAL,&changement,&transformation)>0)
287     {
288       _fam_coords=DataArrayInt::New();
289       _fam_coords->alloc(nCoords,1);
290       MEDmeshEntityFamilyNumberRd(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,_fam_coords->getPointer());
291     }
292   else
293     _fam_coords=0;
294   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NUMBER,MED_NODAL,&changement,&transformation)>0)
295     {
296       _num_coords=DataArrayInt::New();
297       _num_coords->alloc(nCoords,1);
298       MEDmeshEntityNumberRd(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,_num_coords->getPointer());
299     }
300   else
301     _num_coords=0;
302   if(MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,MED_NAME,MED_NODAL,&changement,&transformation)>0)
303     {
304       _name_coords=DataArrayAsciiChar::New();
305       _name_coords->alloc(nCoords+1,MED_SNAME_SIZE);//not a bug to avoid the memory corruption due to last \0 at the end
306       MEDmeshEntityNameRd(fid,mName.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,_name_coords->getPointer());
307       _name_coords->reAlloc(nCoords);//not a bug to avoid the memory corruption due to last \0 at the end
308     }
309   else
310     _name_coords=0;
311   for(int i=0;i<spaceDim;i++)
312     _coords->setInfoOnComponent(i,infosOnComp[i]);
313 }
314
315 void MEDFileUMeshL2::sortTypes()
316 {
317   std::set<int> mdims;
318   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> > tmp(_per_type_mesh[0]);
319   _per_type_mesh.clear();
320   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=tmp.begin();it!=tmp.end();it++)
321     mdims.insert((*it)->getDim());
322   if(mdims.empty())
323     return;
324   int mdim=*mdims.rbegin();
325   _per_type_mesh.resize(mdim+1);
326   for(int dim=mdim+1;dim!=0;dim--)
327     {
328       std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >& elt=_per_type_mesh[mdim+1-dim];
329       for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=tmp.begin();it!=tmp.end();it++)
330         if((*it)->getDim()==dim-1)
331           elt.push_back(*it);
332     }
333   // suppression of contiguous empty levels at the end of _per_type_mesh.
334   int nbOfUselessLev=0;
335   bool isFirst=true;
336   for(std::vector< std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> > >::reverse_iterator it2=_per_type_mesh.rbegin();it2!=_per_type_mesh.rend();it2++)
337     {
338       if((*it2).empty() && isFirst)
339         {
340           nbOfUselessLev++;
341         }
342       else
343         isFirst=false;
344     }
345   _per_type_mesh.resize(_per_type_mesh.size()-nbOfUselessLev);
346 }
347
348 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)
349 {
350   if(!coords)
351     return ;
352   MEDmeshNodeCoordinateWr(fid,mname.c_str(),dt,it,time,MED_FULL_INTERLACE,coords->getNumberOfTuples(),coords->getConstPointer());
353   if(famCoords)
354     MEDmeshEntityFamilyNumberWr(fid,mname.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,famCoords->getNumberOfTuples(),famCoords->getConstPointer());
355   if(numCoords)
356     MEDmeshEntityNumberWr(fid,mname.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,numCoords->getNumberOfTuples(),numCoords->getConstPointer());
357   if(nameCoords)
358     {
359       if(nameCoords->getNumberOfComponents()!=MED_SNAME_SIZE)
360         {
361           std::ostringstream oss; oss << " MEDFileUMeshL2::WriteCoords : expected a name field on nodes with number of components set to " << MED_SNAME_SIZE;
362           oss << " ! The array has " << nameCoords->getNumberOfComponents() << " components !";
363           throw INTERP_KERNEL::Exception(oss.str().c_str());
364         }
365       MEDmeshEntityNameWr(fid,mname.c_str(),dt,it,MED_NODE,MED_NO_GEOTYPE,nameCoords->getNumberOfTuples(),nameCoords->getConstPointer());
366     }
367 }
368
369 bool MEDFileUMeshL2::isFamDefinedOnLev(int levId) const
370 {
371   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
372     if((*it)->getFam()==0)
373       return false;
374   return true;
375 }
376
377 bool MEDFileUMeshL2::isNumDefinedOnLev(int levId) const
378 {
379   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
380     if((*it)->getNum()==0)
381       return false;
382   return true;
383 }
384
385 bool MEDFileUMeshL2::isNamesDefinedOnLev(int levId) const
386 {
387   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >::const_iterator it=_per_type_mesh[levId].begin();it!=_per_type_mesh[levId].end();it++)
388     if((*it)->getNames()==0)
389       return false;
390   return true;
391 }
392
393 MEDFileCMeshL2::MEDFileCMeshL2()
394 {
395 }
396
397 void MEDFileCMeshL2::loadAll(med_idt fid, int mId, const std::string& mName, int dt, int it)
398 {
399   _name.set(mName.c_str());
400   int nstep;
401   int Mdim;
402   ParaMEDMEM::MEDCouplingMeshType meshType;
403   std::vector<std::string> infosOnComp=getAxisInfoOnMesh(fid,mId,mName.c_str(),meshType,nstep,Mdim);
404   if(meshType!=CARTESIAN)
405     throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected a structured one whereas in file it is not a structured !");
406   _time=CheckMeshTimeStep(fid,mName,nstep,dt,it);
407   _iteration=dt;
408   _order=it;
409   //
410   med_grid_type gridtype;
411   MEDmeshGridTypeRd(fid,mName.c_str(),&gridtype);
412   if(gridtype!=MED_CARTESIAN_GRID)
413     throw INTERP_KERNEL::Exception("Invalid structured mesh ! Expected cartesian mesh type !");
414   _cmesh=MEDCouplingCMesh::New();
415   for(int i=0;i<Mdim;i++)
416     {
417       med_data_type dataTypeReq=GetDataTypeCorrespondingToSpaceId(i);
418       med_bool chgt=MED_FALSE,trsf=MED_FALSE;
419       int nbOfElt=MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NONE,dataTypeReq,MED_NO_CMODE,&chgt,&trsf);
420       MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> da=DataArrayDouble::New();
421       da->alloc(nbOfElt,1);
422       da->setInfoOnComponent(0,infosOnComp[i]);
423       MEDmeshGridIndexCoordinateRd(fid,mName.c_str(),dt,it,i+1,da->getPointer());
424       _cmesh->setCoordsAt(i,da);
425     }
426 }
427
428 med_data_type MEDFileCMeshL2::GetDataTypeCorrespondingToSpaceId(int id)
429 {
430   switch(id)
431   {
432     case 0:
433       return MED_COORDINATE_AXIS1;
434     case 1:
435       return MED_COORDINATE_AXIS2;
436     case 2:
437       return MED_COORDINATE_AXIS3;
438     default:
439       throw INTERP_KERNEL::Exception("Invalid meshdim detected in Cartesian Grid !");
440   }
441 }
442
443 MEDFileCLMeshL2::MEDFileCLMeshL2()
444 {
445 }
446
447 void MEDFileCLMeshL2::loadAll(med_idt fid, int mId, const std::string& mName, int dt, int it)
448 {
449   _name.set(mName.c_str());
450   int nstep;
451   int Mdim;
452   ParaMEDMEM::MEDCouplingMeshType meshType;
453   std::vector<std::string> infosOnComp=getAxisInfoOnMesh(fid,mId,mName,meshType,nstep,Mdim);
454   if(meshType!=CURVE_LINEAR)
455     throw INTERP_KERNEL::Exception("Invalid mesh type ! You are expected a structured one whereas in file it is not a structured !");
456   _time=CheckMeshTimeStep(fid,mName,nstep,dt,it);
457   _iteration=dt;
458   _order=it;
459   //
460   _clmesh=MEDCouplingCurveLinearMesh::New();
461   INTERP_KERNEL::AutoPtr<int> stGrid=new int[Mdim];
462   MEDmeshGridStructRd(fid,mName.c_str(),dt,it,stGrid);
463   _clmesh->setNodeGridStructure(stGrid,((int *)stGrid)+Mdim);
464   med_bool chgt=MED_FALSE,trsf=MED_FALSE;
465   int nbNodes=MEDmeshnEntity(fid,mName.c_str(),dt,it,MED_NODE,MED_NONE,MED_COORDINATE,MED_NO_CMODE,&chgt,&trsf);
466   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> da=DataArrayDouble::New();
467   da->alloc(nbNodes,infosOnComp.size());
468   da->setInfoOnComponents(infosOnComp);
469   MEDmeshNodeCoordinateRd(fid,mName.c_str(),dt,it,MED_FULL_INTERLACE,da->getPointer());
470   _clmesh->setCoords(da);
471 }
472
473 MEDFileUMeshPermCompute::MEDFileUMeshPermCompute(const MEDFileUMeshSplitL1* st):_st(st),_mpt_time(0),_num_time(0)
474 {
475 }
476
477 /*!
478  * Warning it returns an instance to deallocate !!!!
479  */
480 MEDFileUMeshPermCompute::operator MEDCouplingUMesh *() const
481 {
482   _st->_num->updateTime();
483   if((MEDCouplingUMesh *)_m==0)
484     {
485       updateTime();
486       _m=static_cast<MEDCouplingUMesh *>(_st->_m_by_types.getUmesh()->deepCpy());
487       _m->renumberCells(_st->_num->getConstPointer(),true);
488       return _m.retn();
489     }
490   else
491     {
492       if(_mpt_time==_st->_m_by_types.getTimeOfThis() && _num_time==_st->_num->getTimeOfThis())
493         return _m.retn();
494       else
495         {
496           updateTime();
497           _m=static_cast<MEDCouplingUMesh *>(_st->_m_by_types.getUmesh()->deepCpy());
498           _m->renumberCells(_st->_num->getConstPointer(),true);
499           return _m.retn();
500         }
501     }
502 }
503
504 void MEDFileUMeshPermCompute::operator=(MEDCouplingUMesh *m)
505 {
506   _m=m;
507 }
508
509 void MEDFileUMeshPermCompute::updateTime() const
510 {
511   _mpt_time=_st->_m_by_types.getTimeOfThis();
512   _num_time=_st->_num->getTimeOfThis();
513 }
514
515 std::vector<const BigMemoryObject *> MEDFileUMeshPermCompute::getDirectChildren() const
516 {
517   std::vector<const BigMemoryObject *> ret;
518   const MEDCouplingUMesh *elt(_m);
519   if(elt)
520     ret.push_back(elt);
521   return ret;
522 }
523
524 std::size_t MEDFileUMeshPermCompute::getHeapMemorySizeWithoutChildren() const
525 {
526   return sizeof(MEDFileUMeshPermCompute);
527 }
528
529 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)
530 {
531 }
532
533 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(const MEDFileUMeshL2& l2, const std::string& mName, int id):_m(this)
534 {
535   const std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshPerType> >& v=l2.getLev(id);
536   if(v.empty())
537     return;
538   int sz=v.size();
539   std::vector<const MEDCoupling1GTUMesh *> ms(sz);
540   std::vector<const DataArrayInt *> fams(sz),nums(sz);
541   std::vector<const DataArrayChar *> names(sz); 
542   for(int i=0;i<sz;i++)
543     {
544       MEDCoupling1GTUMesh *elt(v[i]->getMesh());
545       MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> tmp2=l2.getCoords();
546       elt->setCoords(tmp2);
547       ms[i]=elt;
548     }
549   _m_by_types.assignParts(ms);
550   if(l2.isFamDefinedOnLev(id))
551     {
552       for(int i=0;i<sz;i++)
553         fams[i]=v[i]->getFam();
554       if(sz!=1)
555         _fam=DataArrayInt::Aggregate(fams);
556       else
557         {
558           fams[0]->incrRef();
559           _fam=const_cast<DataArrayInt *>(fams[0]);
560         }
561     }
562   if(l2.isNumDefinedOnLev(id))
563     {
564       for(int i=0;i<sz;i++)
565         nums[i]=v[i]->getNum();
566       if(sz!=1)
567         _num=DataArrayInt::Aggregate(nums);
568       else
569         {
570           nums[0]->incrRef();
571           _num=const_cast<DataArrayInt *>(nums[0]);
572         }
573       computeRevNum();
574     }
575   if(l2.isNamesDefinedOnLev(id))
576     {
577       for(int i=0;i<sz;i++)
578         names[i]=v[i]->getNames();
579       _names=dynamic_cast<DataArrayAsciiChar *>(DataArrayChar::Aggregate(names));
580     }
581 }
582
583 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCoupling1GTUMesh *m):_m(this)
584 {
585   std::vector< const MEDCoupling1GTUMesh * > v(1);
586   v[0]=m;
587   assignParts(v);
588 }
589
590 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCouplingUMesh *m):_m(this)
591 {
592   assignMesh(m,true);
593 }
594
595 MEDFileUMeshSplitL1::MEDFileUMeshSplitL1(MEDCouplingUMesh *m, bool newOrOld):_m(this)
596 {
597   assignMesh(m,newOrOld);
598 }
599
600 void MEDFileUMeshSplitL1::setName(const std::string& name)
601 {
602   _m_by_types.setName(name);
603 }
604
605 std::size_t MEDFileUMeshSplitL1::getHeapMemorySizeWithoutChildren() const
606 {
607   return 0;
608 }
609
610 std::vector<const BigMemoryObject *> MEDFileUMeshSplitL1::getDirectChildren() const
611 {
612   std::vector<const BigMemoryObject *> ret;
613   ret.push_back(&_m_by_types);
614   ret.push_back(&_m);
615   if((const DataArrayInt*)_fam)
616     ret.push_back((const DataArrayInt*)_fam);
617   if((const DataArrayInt*)_num)
618     ret.push_back((const DataArrayInt*)_num);
619   if((const DataArrayInt*)_rev_num)
620     ret.push_back((const DataArrayInt*)_rev_num);
621   if((const DataArrayAsciiChar*)_names)
622     ret.push_back((const DataArrayAsciiChar*)_names);
623   return ret;
624 }
625
626 MEDFileUMeshSplitL1 *MEDFileUMeshSplitL1::deepCpy(DataArrayDouble *coords) const
627 {
628   MEDCouplingAutoRefCountObjectPtr<MEDFileUMeshSplitL1> ret=new MEDFileUMeshSplitL1(*this);
629   ret->_m_by_types=_m_by_types.deepCpy(coords);
630   if((const DataArrayInt *)_fam)
631     ret->_fam=_fam->deepCpy();
632   if((const DataArrayInt *)_num)
633     ret->_num=_num->deepCpy();
634   if((const DataArrayInt *)_rev_num)
635     ret->_rev_num=_rev_num->deepCpy();
636   if((const DataArrayAsciiChar *)_names)
637     ret->_names=_names->deepCpy();
638   return ret.retn();
639 }
640
641 bool MEDFileUMeshSplitL1::isEqual(const MEDFileUMeshSplitL1 *other, double eps, std::string& what) const
642 {
643   if(!_m_by_types.isEqual(other->_m_by_types,eps,what))
644     return false;
645   const DataArrayInt *d1=_fam;
646   const DataArrayInt *d2=other->_fam;
647   if((d1==0 && d2!=0) || (d1!=0 && d2==0))
648     {
649       what="Presence of family arr in one sublevel and not in other!";
650       return false;
651     }
652   if(d1)
653     if(!d1->isEqual(*d2))
654       {
655         what="family arr at a sublevel are not deeply equal !";
656         return false;
657       }
658   d1=_num;
659   d2=other->_num;
660   if((d1==0 && d2!=0) || (d1!=0 && d2==0))
661     {
662       what="Presence of cell numbering arr in one sublevel and not in other!";
663       return false;
664     }
665   if(d1)
666     if(!d1->isEqual(*d2))
667       {
668         what="Numbering cell arr at a sublevel are not deeply equal !";
669         return false;
670       }
671   const DataArrayAsciiChar *e1=_names;
672   const DataArrayAsciiChar *e2=other->_names;
673   if((e1==0 && e2!=0) || (e1!=0 && e2==0))
674     {
675       what="Presence of cell names arr in one sublevel and not in other!";
676       return false;
677     }
678   if(e1)
679     if(!e1->isEqual(*e2))
680       {
681         what="Name cell arr at a sublevel are not deeply equal !";
682         return false;
683       }
684   return true;
685 }
686
687 void MEDFileUMeshSplitL1::synchronizeTinyInfo(const MEDFileMesh& master) const
688 {
689   _m_by_types.synchronizeTinyInfo(master);
690 }
691
692 void MEDFileUMeshSplitL1::clearNonDiscrAttributes() const
693 {
694   _m_by_types.clearNonDiscrAttributes();
695 }
696
697 void MEDFileUMeshSplitL1::ClearNonDiscrAttributes(const MEDCouplingMesh *tmp)
698 {
699   if(!tmp)
700     return ;
701   (const_cast<MEDCouplingMesh *>(tmp))->setName("");
702   (const_cast<MEDCouplingMesh *>(tmp))->setDescription("");
703   (const_cast<MEDCouplingMesh *>(tmp))->setTime(0.,-1,-1);
704   (const_cast<MEDCouplingMesh *>(tmp))->setTimeUnit("");
705 }
706
707 void MEDFileUMeshSplitL1::setCoords(DataArrayDouble *coords)
708 {
709   _m_by_types.setCoords(coords);
710 }
711
712 void MEDFileUMeshSplitL1::assignMesh(MEDCouplingUMesh *m, bool newOrOld)
713 {
714   if(newOrOld)
715     {
716       m->incrRef();
717       _m=m;
718       _m_by_types.assignUMesh(dynamic_cast<MEDCouplingUMesh *>(m->deepCpy()));
719       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=_m_by_types.getUmesh()->getRenumArrForConsecutiveCellTypesSpec(typmai2,typmai2+MED_N_CELL_FIXED_GEO);
720       if(!da->isIdentity())
721         {
722           _num=da->invertArrayO2N2N2O(m->getNumberOfCells());
723           _m.updateTime();
724           computeRevNum();
725           _m_by_types.getUmesh()->renumberCells(da->getConstPointer(),false);
726         }
727     }
728   else
729     {
730       if(!m->checkConsecutiveCellTypesAndOrder(typmai2,typmai2+MED_N_CELL_FIXED_GEO))
731         throw INTERP_KERNEL::Exception("MEDFileUMeshSplitL1::assignMesh : the mode of mesh setting expects to follow the MED file numbering convention ! it is not the case !");
732       m->incrRef();
733       _m_by_types.assignUMesh(m);
734     }
735   assignCommonPart();
736 }
737
738 void MEDFileUMeshSplitL1::forceComputationOfParts() const
739 {
740   _m_by_types.forceComputationOfPartsFromUMesh();
741 }
742
743 void MEDFileUMeshSplitL1::assignParts(const std::vector< const MEDCoupling1GTUMesh * >& mParts)
744 {
745   _m_by_types.assignParts(mParts);
746   assignCommonPart();
747 }
748
749 void MEDFileUMeshSplitL1::assignCommonPart()
750 {
751   _fam=DataArrayInt::New();
752   _fam->alloc(_m_by_types.getSize(),1);
753   _fam->fillWithValue(0);
754 }
755
756 bool MEDFileUMeshSplitL1::empty() const
757 {
758   return _m_by_types.empty();
759 }
760
761 bool MEDFileUMeshSplitL1::presenceOfOneFams(const std::vector<int>& ids) const
762 {
763   const DataArrayInt *fam=_fam;
764   if(!fam)
765     return false;
766   return fam->presenceOfValue(ids);
767 }
768
769 int MEDFileUMeshSplitL1::getMeshDimension() const
770 {
771   return _m_by_types.getMeshDimension();
772 }
773
774 void MEDFileUMeshSplitL1::simpleRepr(std::ostream& oss) const
775 {
776   std::vector<int> code=_m_by_types.getDistributionOfTypes();
777   int nbOfTypes=code.size()/3;
778   for(int i=0;i<nbOfTypes;i++)
779     {
780       INTERP_KERNEL::NormalizedCellType typ=(INTERP_KERNEL::NormalizedCellType) code[3*i];
781       oss << "    - Number of cells with type " << INTERP_KERNEL::CellModel::GetCellModel(typ).getRepr() << " : " << code[3*i+1] << std::endl;
782     }
783 }
784
785 int MEDFileUMeshSplitL1::getSize() const
786 {
787   return _m_by_types.getSize();
788 }
789
790 MEDCouplingUMesh *MEDFileUMeshSplitL1::getFamilyPart(const int *idsBg, const int *idsEnd, bool renum) const
791 {
792   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> eltsToKeep=_fam->getIdsEqualList(idsBg,idsEnd);
793   MEDCouplingUMesh *m=(MEDCouplingUMesh *)_m_by_types.getUmesh()->buildPartOfMySelf(eltsToKeep->getConstPointer(),eltsToKeep->getConstPointer()+eltsToKeep->getNumberOfTuples(),true);
794   if(renum)
795     return renumIfNeeded(m,eltsToKeep->getConstPointer());
796   return m;
797 }
798
799 DataArrayInt *MEDFileUMeshSplitL1::getFamilyPartArr(const int *idsBg, const int *idsEnd, bool renum) const
800 {
801   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da=_fam->getIdsEqualList(idsBg,idsEnd);
802   if(renum)
803     return renumIfNeededArr(da);
804   return da.retn();
805 }
806
807 std::vector<INTERP_KERNEL::NormalizedCellType> MEDFileUMeshSplitL1::getGeoTypes() const
808 {
809   return _m_by_types.getGeoTypes();
810 }
811
812 MEDCouplingUMesh *MEDFileUMeshSplitL1::getWholeMesh(bool renum) const
813 {
814   MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> tmp;
815   if(renum && ((const DataArrayInt *)_num))
816     tmp=_m;
817   else
818     { tmp=_m_by_types.getUmesh(); if(tmp) tmp->incrRef(); }
819   return tmp.retn();
820 }
821
822 DataArrayInt *MEDFileUMeshSplitL1::extractFamilyFieldOnGeoType(INTERP_KERNEL::NormalizedCellType gt) const
823 {
824   const DataArrayInt *fam(_fam);
825   if(!fam)
826     return 0;
827   int start(0),stop(0);
828   _m_by_types.getStartStopOfGeoTypeWithoutComputation(gt,start,stop);
829   return fam->selectByTupleId2(start,stop,1);
830 }
831
832 DataArrayInt *MEDFileUMeshSplitL1::extractNumberFieldOnGeoType(INTERP_KERNEL::NormalizedCellType gt) const
833 {
834   const DataArrayInt *num(_num);
835   if(!num)
836     return 0;
837   int start(0),stop(0);
838   _m_by_types.getStartStopOfGeoTypeWithoutComputation(gt,start,stop);
839   return num->selectByTupleId2(start,stop,1);
840 }
841
842 DataArrayInt *MEDFileUMeshSplitL1::getOrCreateAndGetFamilyField()
843 {
844   if((DataArrayInt *)_fam)
845     return _fam;
846   int nbOfTuples=_m_by_types.getSize();
847   _fam=DataArrayInt::New(); _fam->alloc(nbOfTuples,1); _fam->fillWithZero();
848   return _fam;
849 }
850
851 const DataArrayInt *MEDFileUMeshSplitL1::getFamilyField() const
852 {
853   return _fam;
854 }
855
856 const DataArrayInt *MEDFileUMeshSplitL1::getNumberField() const
857 {
858   return _num;
859 }
860
861 const DataArrayInt *MEDFileUMeshSplitL1::getRevNumberField() const
862 {
863   return _rev_num;
864 }
865
866 const DataArrayAsciiChar *MEDFileUMeshSplitL1::getNameField() const
867 {
868   return _names;
869 }
870
871 void MEDFileUMeshSplitL1::eraseFamilyField()
872 {
873   _fam->fillWithZero();
874 }
875
876 /*!
877  * This method ignores _m and _m_by_types.
878  */
879 void MEDFileUMeshSplitL1::setGroupsFromScratch(const std::vector<const MEDCouplingUMesh *>& ms, std::map<std::string,int>& familyIds,
880                                                std::map<std::string, std::vector<std::string> >& groups)
881 {
882   std::vector< DataArrayInt * > corr;
883   _m=MEDCouplingUMesh::FuseUMeshesOnSameCoords(ms,0,corr);
884   std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> > corrMSafe(corr.begin(),corr.end());
885   std::vector< std::vector<int> > fidsOfGroups;
886   std::vector< const DataArrayInt * > corr2(corr.begin(),corr.end());
887   _fam=DataArrayInt::MakePartition(corr2,((MEDCouplingUMesh *)_m)->getNumberOfCells(),fidsOfGroups);
888   int nbOfCells=((MEDCouplingUMesh *)_m)->getNumberOfCells();
889   std::map<int,std::string> newfams;
890   std::map<int,int> famIdTrad;
891   TraduceFamilyNumber(fidsOfGroups,familyIds,famIdTrad,newfams);
892   int *w=_fam->getPointer();
893   for(int i=0;i<nbOfCells;i++,w++)
894     *w=famIdTrad[*w];
895 }
896
897 void MEDFileUMeshSplitL1::write(med_idt fid, const std::string& mName, int mdim) const
898 {
899   std::vector<MEDCoupling1GTUMesh *> ms(_m_by_types.getParts());
900   int start=0;
901   for(std::vector<MEDCoupling1GTUMesh *>::const_iterator it=ms.begin();it!=ms.end();it++)
902     {
903       int nbCells=(*it)->getNumberOfCells();
904       int end=start+nbCells;
905       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> fam,num;
906       MEDCouplingAutoRefCountObjectPtr<DataArrayAsciiChar> names;
907       if((const DataArrayInt *)_fam)
908         fam=_fam->substr(start,end);
909       if((const DataArrayInt *)_num)
910         num=_num->substr(start,end);
911       if((const DataArrayAsciiChar *)_names)
912         names=static_cast<DataArrayAsciiChar *>(_names->substr(start,end));
913       MEDFileUMeshPerType::Write(fid,mName,mdim,(*it),fam,num,names);
914       start=end;
915     }
916 }
917
918 void MEDFileUMeshSplitL1::renumberNodesInConn(const int *newNodeNumbersO2N)
919 {
920   MEDCouplingUMesh *m(_m_by_types.getUmesh());
921   if(!m)
922     return;
923   m->renumberNodesInConn(newNodeNumbersO2N);
924 }
925
926 void MEDFileUMeshSplitL1::changeFamilyIdArr(int oldId, int newId)
927 {
928   DataArrayInt *arr=_fam;
929   if(arr)
930     arr->changeValue(oldId,newId);
931 }
932
933 void MEDFileUMeshSplitL1::setFamilyArr(DataArrayInt *famArr)
934 {
935   if(!famArr)
936     {
937       _fam=0;
938       return ;
939     }
940   int sz(_m_by_types.getSize());
941   famArr->checkNbOfTuplesAndComp(sz,1,"MEDFileUMeshSplitL1::setFamilyArr : Problem in size of Family arr ! ");
942   famArr->incrRef();
943   _fam=famArr;
944 }
945
946 void MEDFileUMeshSplitL1::setRenumArr(DataArrayInt *renumArr)
947 {
948   if(!renumArr)
949     {
950       _num=0;
951       _rev_num=0;
952       return ;
953     }
954   int sz(_m_by_types.getSize());
955   renumArr->checkNbOfTuplesAndComp(sz,1,"MEDFileUMeshSplitL1::setRenumArr : Problem in size of numbering arr ! ");
956   renumArr->incrRef();
957   _num=renumArr;
958   computeRevNum();
959 }
960
961 void MEDFileUMeshSplitL1::setNameArr(DataArrayAsciiChar *nameArr)
962 {
963   if(!nameArr)
964     {
965       _names=0;
966       return ;
967     }
968   int sz(_m_by_types.getSize());
969   nameArr->checkNbOfTuplesAndComp(sz,MED_SNAME_SIZE,"MEDFileUMeshSplitL1::setNameArr : Problem in size of name arr ! ");
970   nameArr->incrRef();
971   _names=nameArr;
972 }
973
974 MEDCouplingUMesh *MEDFileUMeshSplitL1::Renumber2(const DataArrayInt *renum, MEDCouplingUMesh *m, const int *cellIds)
975 {
976   if(renum==0)
977     return m;
978   if(cellIds==0)
979     m->renumberCells(renum->getConstPointer(),true);
980   else
981     {
982       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> locnum=renum->selectByTupleId(cellIds,cellIds+m->getNumberOfCells());
983       m->renumberCells(locnum->getConstPointer(),true);
984     }
985   return m;
986 }
987
988 MEDCouplingUMesh *MEDFileUMeshSplitL1::renumIfNeeded(MEDCouplingUMesh *m, const int *cellIds) const
989 {
990   return Renumber2(_num,m,cellIds);
991 }
992
993 DataArrayInt *MEDFileUMeshSplitL1::Renumber(const DataArrayInt *renum, const DataArrayInt *da)
994 {
995   if((const DataArrayInt *)renum==0)
996     {
997       da->incrRef();
998       return const_cast<DataArrayInt *>(da);
999     }
1000   return renum->selectByTupleId(da->getConstPointer(),da->getConstPointer()+da->getNumberOfTuples());
1001 }
1002
1003 DataArrayInt *MEDFileUMeshSplitL1::renumIfNeededArr(const DataArrayInt *da) const
1004 {
1005   return Renumber(_num,da);
1006 }
1007
1008 std::vector<int> MEDFileUMeshSplitL1::GetNewFamiliesNumber(int nb, const std::map<std::string,int>& families)
1009 {
1010   int id=-1;
1011   for(std::map<std::string,int>::const_iterator it=families.begin();it!=families.end();it++)
1012     id=std::max(id,(*it).second);
1013   if(id==-1)
1014     id=0;
1015   std::vector<int> ret(nb);
1016   for(int i=1;i<=nb;i++)
1017     ret[i]=id+i;
1018   return ret;
1019 }
1020
1021 void MEDFileUMeshSplitL1::TraduceFamilyNumber(const std::vector< std::vector<int> >& fidsGrps, std::map<std::string,int>& familyIds,
1022                                               std::map<int,int>& famIdTrad, std::map<int,std::string>& newfams)
1023 {
1024   std::set<int> allfids;
1025   //tony
1026 }
1027
1028 void MEDFileUMeshSplitL1::computeRevNum() const
1029 {
1030   int pos;
1031   int maxValue=_num->getMaxValue(pos);
1032   _rev_num=_num->invertArrayN2O2O2N(maxValue+1);
1033 }
1034
1035 //=
1036
1037 MEDFileUMeshAggregateCompute::MEDFileUMeshAggregateCompute():_mp_time(0),_m_time(0)
1038 {
1039 }
1040
1041 void MEDFileUMeshAggregateCompute::setName(const std::string& name)
1042 {
1043   if(_m_time>=_mp_time)
1044     {
1045       MEDCouplingUMesh *um(_m);
1046       if(um)
1047         um->setName(name);
1048     }
1049   if(_mp_time>=_m_time)
1050     {
1051       for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1052         {
1053           MEDCoupling1GTUMesh *tmp(*it);
1054           if(tmp)
1055             tmp->setName(name);
1056         }
1057     }
1058 }
1059
1060 void MEDFileUMeshAggregateCompute::assignParts(const std::vector< const MEDCoupling1GTUMesh * >& mParts)
1061 {
1062   std::size_t sz(mParts.size());
1063   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> > ret(sz);
1064   for(std::size_t i=0;i<sz;i++)
1065     {
1066       const MEDCoupling1GTUMesh *elt(mParts[i]);
1067       if(!elt)
1068         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::assignParts : presence of null pointer !");
1069       ret[i]=const_cast<MEDCoupling1GTUMesh *>(elt); elt->incrRef();
1070     }
1071   _m_parts=ret;
1072   _mp_time=std::max(_mp_time,_m_time)+1;
1073   _m=0;
1074 }
1075
1076 void MEDFileUMeshAggregateCompute::assignUMesh(MEDCouplingUMesh *m)
1077 {
1078   _m=m;
1079   _m_parts.clear();
1080   _m_time=std::max(_mp_time,_m_time)+1;
1081 }
1082
1083 MEDCouplingUMesh *MEDFileUMeshAggregateCompute::getUmesh() const
1084 {
1085   if(_mp_time<=_m_time)
1086     return _m;
1087   std::vector< const MEDCoupling1GTUMesh *> mp(_m_parts.size());
1088   std::copy(_m_parts.begin(),_m_parts.end(),mp.begin());
1089   _m=MEDCoupling1GTUMesh::AggregateOnSameCoordsToUMesh(mp);
1090   _m_parts.clear();//to avoid memory peak !
1091   _m_time=_mp_time+1;//+1 is important ! That is to say that only _m is OK not _m_parts because cleared !
1092   return _m;
1093 }
1094
1095 std::vector<INTERP_KERNEL::NormalizedCellType> MEDFileUMeshAggregateCompute::getGeoTypes() const
1096 {
1097   if(_mp_time>=_m_time)
1098     {
1099       std::size_t sz(_m_parts.size());
1100       std::vector<INTERP_KERNEL::NormalizedCellType> ret(sz);
1101       for(std::size_t i=0;i<sz;i++)
1102         ret[i]=_m_parts[i]->getCellModelEnum();
1103       return ret;
1104     }
1105   else
1106     return _m->getAllGeoTypesSorted();
1107 }
1108
1109 std::vector<MEDCoupling1GTUMesh *> MEDFileUMeshAggregateCompute::getPartsWithoutComputation() const
1110 {
1111   if(_mp_time<_m_time)
1112     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getPartsWithoutComputation : the parts require a computation !");
1113   //
1114   std::vector<MEDCoupling1GTUMesh *> ret(_m_parts.size());
1115   std::size_t i(0);
1116   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++,i++)
1117     {
1118       const MEDCoupling1GTUMesh *elt(*it);
1119       ret[i]=const_cast<MEDCoupling1GTUMesh *>(elt);
1120     }
1121   return ret;
1122 }
1123
1124 std::vector<MEDCoupling1GTUMesh *> MEDFileUMeshAggregateCompute::getParts() const
1125 {
1126   if(_mp_time<_m_time)
1127     forceComputationOfPartsFromUMesh();
1128   return getPartsWithoutComputation();
1129 }
1130
1131 MEDCoupling1GTUMesh *MEDFileUMeshAggregateCompute::getPartWithoutComputation(INTERP_KERNEL::NormalizedCellType gt) const
1132 {
1133   std::vector<MEDCoupling1GTUMesh *> v(getPartsWithoutComputation());
1134   std::size_t sz(v.size());
1135   for(std::size_t i=0;i<sz;i++)
1136     {
1137       if(v[i])
1138         if(v[i]->getCellModelEnum()==gt)
1139           return v[i];
1140     }
1141   throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getPartWithoutComputation : the geometric type is not existing !");
1142 }
1143
1144 void MEDFileUMeshAggregateCompute::getStartStopOfGeoTypeWithoutComputation(INTERP_KERNEL::NormalizedCellType gt, int& start, int& stop) const
1145 {
1146   start=0; stop=0;
1147   std::vector<MEDCoupling1GTUMesh *> v(getPartsWithoutComputation());
1148   std::size_t sz(v.size());
1149   for(std::size_t i=0;i<sz;i++)
1150     {
1151       if(v[i])
1152         {
1153           if(v[i]->getCellModelEnum()==gt)
1154             {
1155               stop=start+v[i]->getNumberOfCells();
1156               return;
1157             }
1158           else
1159             start+=v[i]->getNumberOfCells();
1160         }
1161     }
1162   throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getStartStopOfGeoTypeWithoutComputation : the geometric type is not existing !");
1163 }
1164
1165 void MEDFileUMeshAggregateCompute::forceComputationOfPartsFromUMesh() const
1166 {
1167   const MEDCouplingUMesh *m(_m);
1168   if(!m)
1169     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::forceComputationOfPartsFromUMesh : null UMesh !");
1170   std::vector<MEDCouplingUMesh *> ms(m->splitByType());
1171   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCouplingUMesh> > msMSafe(ms.begin(),ms.end());
1172   std::size_t sz(msMSafe.size());
1173   _m_parts.resize(sz);
1174   for(std::size_t i=0;i<sz;i++)
1175     _m_parts[i]=MEDCoupling1GTUMesh::New(ms[i]);
1176   _mp_time=std::max(_mp_time,_m_time);
1177 }
1178
1179 std::size_t MEDFileUMeshAggregateCompute::getTimeOfThis() const
1180 {
1181   if(_mp_time>_m_time)
1182     return getTimeOfParts();
1183   if(_m_time>_mp_time)
1184     return getTimeOfUMesh();
1185   return std::max(getTimeOfParts(),getTimeOfUMesh());
1186 }
1187
1188 std::size_t MEDFileUMeshAggregateCompute::getTimeOfParts() const
1189 {
1190   std::size_t ret(0);
1191   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1192     {
1193       const MEDCoupling1GTUMesh *elt(*it);
1194       if(!elt)
1195         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getTimeOfParts : null obj in parts !");
1196       ret=std::max(ret,elt->getTimeOfThis());
1197     }
1198   if(ret==0)
1199     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getTimeOfParts : parts is empty !");
1200   return ret;
1201 }
1202
1203 std::size_t MEDFileUMeshAggregateCompute::getTimeOfUMesh() const
1204 {
1205   const MEDCouplingUMesh *m(_m);
1206   if(!m)
1207     throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getTimeOfUMesh : unmesh is null !");
1208   return m->getTimeOfThis();
1209 }
1210
1211 std::size_t MEDFileUMeshAggregateCompute::getHeapMemorySizeWithoutChildren() const
1212 {
1213   std::size_t ret(_m_parts.size()*sizeof(MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh>));
1214   return ret;
1215 }
1216
1217 std::vector<const BigMemoryObject *> MEDFileUMeshAggregateCompute::getDirectChildren() const
1218 {
1219   std::vector<const BigMemoryObject *> ret;
1220   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1221     {
1222       const MEDCoupling1GTUMesh *cur(*it);
1223       if(cur)
1224         ret.push_back(cur);
1225     }
1226   const MEDCouplingUMesh *m(_m);
1227   if(m)
1228     ret.push_back(m);
1229   return ret;
1230 }
1231
1232 MEDFileUMeshAggregateCompute MEDFileUMeshAggregateCompute::deepCpy(DataArrayDouble *coords) const
1233 {
1234   MEDFileUMeshAggregateCompute ret;
1235   ret._m_parts.resize(_m_parts.size());
1236   for(std::size_t i=0;i<_m_parts.size();i++)
1237     {
1238       const MEDCoupling1GTUMesh *elt(_m_parts[i]);
1239       if(elt)
1240         {
1241           ret._m_parts[i]=static_cast<ParaMEDMEM::MEDCoupling1GTUMesh*>(elt->deepCpy());
1242           ret._m_parts[i]->setCoords(coords);
1243         }
1244     }
1245   ret._mp_time=_mp_time; ret._m_time=_m_time;
1246   if((const MEDCouplingUMesh *)_m)
1247     {
1248       ret._m=static_cast<ParaMEDMEM::MEDCouplingUMesh*>(_m->deepCpy());
1249       ret._m->setCoords(coords);
1250     }
1251   return ret;
1252 }
1253
1254 bool MEDFileUMeshAggregateCompute::isEqual(const MEDFileUMeshAggregateCompute& other, double eps, std::string& what) const
1255 {
1256   const MEDCouplingUMesh *m1(getUmesh());
1257   const MEDCouplingUMesh *m2(other.getUmesh());
1258   if((m1==0 && m2!=0) || (m1!=0 && m2==0))
1259     {
1260       what="Presence of mesh in one sublevel and not in other!";
1261       return false;
1262     }
1263   if(m1)
1264     {
1265       std::string what2;
1266       if(!m1->isEqualIfNotWhy(m2,eps,what2))
1267         {
1268           what=std::string("meshes at a sublevel are not deeply equal (")+what2+std::string(")!");
1269           return false;
1270         }
1271     }
1272   return true;
1273 }
1274
1275 void MEDFileUMeshAggregateCompute::clearNonDiscrAttributes() const
1276 {
1277   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1278     MEDFileUMeshSplitL1::ClearNonDiscrAttributes(*it);
1279   MEDFileUMeshSplitL1::ClearNonDiscrAttributes(_m);
1280 }
1281
1282 void MEDFileUMeshAggregateCompute::synchronizeTinyInfo(const MEDFileMesh& master) const
1283 {
1284   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1285     {
1286       const MEDCoupling1GTUMesh *tmp(*it);
1287       if(tmp)
1288         {
1289           (const_cast<MEDCoupling1GTUMesh *>(tmp))->setName(master.getName().c_str());
1290           (const_cast<MEDCoupling1GTUMesh *>(tmp))->setDescription(master.getDescription().c_str());
1291           (const_cast<MEDCoupling1GTUMesh *>(tmp))->setTime(master.getTimeValue(),master.getIteration(),master.getOrder());
1292           (const_cast<MEDCoupling1GTUMesh *>(tmp))->setTimeUnit(master.getTimeUnit());
1293         }
1294     }
1295   const MEDCouplingUMesh *m(_m);
1296   if(m)
1297     {
1298       (const_cast<MEDCouplingUMesh *>(m))->setName(master.getName().c_str());
1299       (const_cast<MEDCouplingUMesh *>(m))->setDescription(master.getDescription().c_str());
1300       (const_cast<MEDCouplingUMesh *>(m))->setTime(master.getTimeValue(),master.getIteration(),master.getOrder());
1301       (const_cast<MEDCouplingUMesh *>(m))->setTimeUnit(master.getTimeUnit());
1302     }
1303 }
1304
1305 bool MEDFileUMeshAggregateCompute::empty() const
1306 {
1307   if(_mp_time<_m_time)
1308     return ((const MEDCouplingUMesh *)_m)==0;
1309   //else _mp_time>=_m_time)
1310   return _m_parts.empty();
1311 }
1312
1313 int MEDFileUMeshAggregateCompute::getMeshDimension() const
1314 {
1315   if(_mp_time<_m_time)
1316     {
1317       const MEDCouplingUMesh *m(_m);
1318       if(!m)
1319         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getMeshDimension : no umesh in this !");
1320       return m->getMeshDimension();
1321     }
1322   else
1323     {
1324       if(_m_parts.empty())
1325         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getMeshDimension : part mesh is empty !");
1326       const MEDCoupling1GTUMesh *m(_m_parts[0]);
1327       if(!m)
1328         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getMeshDimension : part mesh contains null instance !");
1329       return m->getMeshDimension();
1330     }
1331 }
1332
1333 std::vector<int> MEDFileUMeshAggregateCompute::getDistributionOfTypes() const
1334 {
1335   if(_mp_time<_m_time)
1336     {
1337       const MEDCouplingUMesh *m(_m);
1338       if(!m)
1339         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getDistributionOfTypes : no umesh in this !");
1340       return m->getDistributionOfTypes();
1341     }
1342   else
1343     {
1344       std::vector<int> ret;
1345       for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1346         {
1347           const MEDCoupling1GTUMesh *tmp(*it);
1348           if(!tmp)
1349             throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getDistributionOfTypes : part mesh contains null instance !");
1350           std::vector<int> ret0(tmp->getDistributionOfTypes());
1351           ret.insert(ret.end(),ret0.begin(),ret0.end());
1352         }
1353       return ret;
1354     }
1355 }
1356
1357 int MEDFileUMeshAggregateCompute::getSize() const
1358 {
1359   if(_mp_time<_m_time)
1360     {
1361       const MEDCouplingUMesh *m(_m);
1362       if(!m)
1363         throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getSize : no umesh in this !");
1364       return m->getNumberOfCells();
1365     }
1366   else
1367     {
1368       int ret=0;
1369       for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::const_iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1370         {
1371           const MEDCoupling1GTUMesh *m(*it);
1372           if(!m)
1373             throw INTERP_KERNEL::Exception("MEDFileUMeshAggregateCompute::getSize : part mesh contains null instance !");
1374           ret+=m->getNumberOfCells();
1375         }
1376       return ret;
1377     }
1378 }
1379
1380 void MEDFileUMeshAggregateCompute::setCoords(DataArrayDouble *coords)
1381 {
1382   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::iterator it=_m_parts.begin();it!=_m_parts.end();it++)
1383     {
1384       MEDCoupling1GTUMesh *tmp(*it);
1385       if(tmp)
1386         (*it)->setCoords(coords);
1387     }
1388   MEDCouplingUMesh *m(_m);
1389   if(m)
1390     m->setCoords(coords);
1391 }