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