Salome HOME
Target MEDReader
[modules/med.git] / src / MEDLoader / MEDFileFieldOverView.cxx
1 // Copyright (C) 2007-2013  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.
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 "MEDFileFieldOverView.hxx"
22 #include "MEDFileField.hxx"
23 #include "MEDFileMesh.hxx"
24
25 #include "CellModel.hxx"
26
27 using namespace ParaMEDMEM;
28
29 const char MEDFileField1TSStructItem2::NEWLY_CREATED_PFL_NAME[]="???";
30
31 MEDFileMeshStruct *MEDFileMeshStruct::New(const MEDFileMesh *mesh)
32 {
33   return new MEDFileMeshStruct(mesh);
34 }
35
36 std::size_t MEDFileMeshStruct::getHeapMemorySize() const
37 {
38   std::size_t ret(0);
39   for(std::vector< std::vector<int> >::const_iterator it0=_geo_types_distrib.begin();it0!=_geo_types_distrib.end();it0++)
40     ret+=(*it0).capacity()*sizeof(int);
41   ret+=_geo_types_distrib.capacity()*sizeof(std::vector<int>);
42   return ret;
43 }
44
45 MEDFileMeshStruct::MEDFileMeshStruct(const MEDFileMesh *mesh):_mesh(mesh)
46 {
47   std::vector<int> levs=mesh->getNonEmptyLevels();
48   _name=mesh->getName();
49   _nb_nodes=mesh->getNumberOfNodes();
50   _geo_types_distrib.resize(levs.size());
51   for(std::vector<int>::const_iterator lev=levs.begin();lev!=levs.end();lev++)
52     {
53       MEDCouplingAutoRefCountObjectPtr<MEDCouplingMesh> mLev=mesh->getGenMeshAtLevel(*lev);
54       _geo_types_distrib[-(*lev)]=mLev->getDistributionOfTypes();
55     }
56 }
57
58 int MEDFileMeshStruct::getLevelOfGeoType(INTERP_KERNEL::NormalizedCellType t) const throw(INTERP_KERNEL::Exception)
59 {
60   int j=0;
61   for(std::vector< std::vector<int> >::const_iterator it1=_geo_types_distrib.begin();it1!=_geo_types_distrib.end();it1++,j--)
62     {
63       std::size_t sz=(*it1).size();
64       if(sz%3!=0)
65         throw INTERP_KERNEL::Exception("MEDFileMeshStruct::getLevelOfGeoType : internal error in code !");
66       std::size_t nbGeo=sz/3;
67       for(std::size_t i=0;i<nbGeo;i++)
68         if((*it1)[3*i]==(int)t)
69           return j;
70     }
71   throw INTERP_KERNEL::Exception("MEDFileMeshStruct::getLevelOfGeoType : The specified geometric type is not present in the mesh structure !");
72 }
73
74 int MEDFileMeshStruct::getNumberOfElemsOfGeoType(INTERP_KERNEL::NormalizedCellType t) const throw(INTERP_KERNEL::Exception)
75 {
76   for(std::vector< std::vector<int> >::const_iterator it1=_geo_types_distrib.begin();it1!=_geo_types_distrib.end();it1++)
77     {
78       std::size_t sz=(*it1).size();
79       if(sz%3!=0)
80         throw INTERP_KERNEL::Exception("MEDFileMeshStruct::getNumberOfElemsOfGeoType : internal error in code !");
81       std::size_t nbGeo=sz/3;
82       for(std::size_t i=0;i<nbGeo;i++)
83         if((*it1)[3*i]==(int)t)
84           return (*it1)[3*i+1];
85     }
86   throw INTERP_KERNEL::Exception("The specified geometric type is not present in the mesh structure !");
87 }
88
89 int MEDFileMeshStruct::getNumberOfLevs() const
90 {
91   return (int)_geo_types_distrib.size();
92 }
93
94 int MEDFileMeshStruct::getNumberOfGeoTypesInLev(int relativeLev) const throw(INTERP_KERNEL::Exception)
95 {
96   int pos(-relativeLev);
97   if(pos<0 || pos>=_geo_types_distrib.size())
98     throw INTERP_KERNEL::Exception("MEDFileMeshStruct::getNumberOfGeoTypesInLev : invalid level specified !");
99   std::size_t sz=_geo_types_distrib[pos].size();
100   if(sz%3!=0)
101     throw INTERP_KERNEL::Exception("MEDFileMeshStruct::getNumberOfGeoTypesInLev : internal error in code !");
102   return (int)(sz/3);
103 }
104
105 //=
106
107 MEDFileField1TSStructItem2::MEDFileField1TSStructItem2()
108 {
109 }
110
111 MEDFileField1TSStructItem2::MEDFileField1TSStructItem2(INTERP_KERNEL::NormalizedCellType a, const std::pair<int,int>& b, const std::string& c, const std::string& d):_geo_type(a),_start_end(b),_pfl(DataArrayInt::New()),_loc(d),_nb_of_entity(-1)
112 {
113   _pfl->setName(c.c_str());
114 }
115
116 void MEDFileField1TSStructItem2::checkWithMeshStructForCells(const MEDFileMeshStruct *mst, const MEDFileFieldGlobsReal *globs) throw(INTERP_KERNEL::Exception)
117 {
118   int nbOfEnt=mst->getNumberOfElemsOfGeoType(_geo_type);
119   checkInRange(nbOfEnt,1,globs);
120 }
121
122 void MEDFileField1TSStructItem2::checkWithMeshStructForGaussNE(const MEDFileMeshStruct *mst, const MEDFileFieldGlobsReal *globs) throw(INTERP_KERNEL::Exception)
123 {
124   int nbOfEnt=mst->getNumberOfElemsOfGeoType(_geo_type);
125   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(_geo_type);
126   checkInRange(nbOfEnt,(int)cm.getNumberOfNodes(),globs);
127 }
128
129 void MEDFileField1TSStructItem2::checkWithMeshStructForGaussPT(const MEDFileMeshStruct *mst, const MEDFileFieldGlobsReal *globs) throw(INTERP_KERNEL::Exception)
130 {
131   if(!globs)
132     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::checkWithMeshStructForGaussPT : no globals specified !");
133   if(_loc.empty())
134     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::checkWithMeshStructForGaussPT : no localization specified !");
135   const MEDFileFieldLoc& loc=globs->getLocalization(_loc.c_str());
136   int nbOfEnt=mst->getNumberOfElemsOfGeoType(_geo_type);
137   checkInRange(nbOfEnt,loc.getNumberOfGaussPoints(),globs);
138 }
139
140 int MEDFileField1TSStructItem2::getNbOfIntegrationPts(const MEDFileFieldGlobsReal *globs) const
141 {
142   if(_loc.empty())
143     {
144       if(getPflName().empty())
145         return (_start_end.second-_start_end.first)/_nb_of_entity;
146       else
147         return (_start_end.second-_start_end.first)/getPfl(globs)->getNumberOfTuples();
148     }
149   else
150     {
151       const MEDFileFieldLoc& loc(globs->getLocalization(_loc.c_str()));
152       return loc.getNumberOfGaussPoints();
153     }
154 }
155
156 std::string MEDFileField1TSStructItem2::getPflName() const
157 {
158   return _pfl->getName();
159 }
160
161 const DataArrayInt *MEDFileField1TSStructItem2::getPfl(const MEDFileFieldGlobsReal *globs) const
162 {
163   if(!_pfl->isAllocated())
164     {
165       if(_pfl->getName().empty())
166         return 0;
167       else
168         return globs->getProfile(_pfl->getName().c_str());
169     }
170   else
171     return _pfl;
172 }
173
174 /*!
175  * \param [in] nbOfEntity - number of entity that can be either cells or nodes. Not other possiblity.
176  * \param [in] nip - number of integration points. 1 for ON_CELLS and NO_NODES
177  */
178 void MEDFileField1TSStructItem2::checkInRange(int nbOfEntity, int nip, const MEDFileFieldGlobsReal *globs) throw(INTERP_KERNEL::Exception)
179 {
180   _nb_of_entity=nbOfEntity;
181   if(_pfl->getName().empty())
182     {
183       if(nbOfEntity!=(_start_end.second-_start_end.first)/nip)
184         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::checkInRange : Mismatch between number of entities and size of node field !");
185       return ;
186     }
187   else
188     {
189       if(!globs)
190         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::checkInRange : Presence of a profile on field whereas no globals found in file !");
191       const DataArrayInt *pfl=globs->getProfile(_pfl->getName().c_str());
192       if(!pfl)
193         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::checkInRange : Presence of a profile on field whereas no such profile found in file !");
194       pfl->checkAllIdsInRange(0,nbOfEntity);
195     }
196 }
197
198 bool MEDFileField1TSStructItem2::isFastlyEqual(int& startExp, INTERP_KERNEL::NormalizedCellType gt, const char *pflName) const
199 {
200   if(startExp!=_start_end.first)
201     return false;
202   if(gt!=_geo_type)
203     return false;
204   if(getPflName()!=pflName)
205     return false;
206   startExp=_start_end.second;
207   return true;
208 }
209
210 bool MEDFileField1TSStructItem2::operator==(const MEDFileField1TSStructItem2& other) const throw(INTERP_KERNEL::Exception)
211 {
212   //_nb_of_entity is not taken into account here. It is not a bug, because no mesh consideration needed here to perform fast compare.
213   //idem for _loc. It is not an effective attribute for support comparison.
214   return _geo_type==other._geo_type && _start_end==other._start_end && _pfl->getName()==other._pfl->getName();
215 }
216
217 bool MEDFileField1TSStructItem2::isCellSupportEqual(const MEDFileField1TSStructItem2& other, const MEDFileFieldGlobsReal *globs) const throw(INTERP_KERNEL::Exception)
218 {
219   if(_geo_type!=other._geo_type)
220     return false;
221   if(_nb_of_entity!=other._nb_of_entity)
222     return false;
223   if((_pfl->getName().empty() && !other._pfl->getName().empty()) || (!_pfl->getName().empty() && other._pfl->getName().empty()))
224     return false;
225   if(_pfl->getName().empty() && other._pfl->getName().empty())
226     return true;
227   const DataArrayInt *pfl1(getPfl(globs)),*pfl2(other.getPfl(globs));
228   return pfl1->isEqualWithoutConsideringStr(*pfl2);
229 }
230
231 bool MEDFileField1TSStructItem2::isNodeSupportEqual(const MEDFileField1TSStructItem2& other, const MEDFileFieldGlobsReal *globs) const throw(INTERP_KERNEL::Exception)
232 {
233   return isCellSupportEqual(other,globs);
234 }
235
236 /*!
237  * \a objs must be non empty. \a objs should contain items having same geometric type.
238  */
239 MEDFileField1TSStructItem2 MEDFileField1TSStructItem2::BuildAggregationOf(const std::vector<const MEDFileField1TSStructItem2 *>& objs, const MEDFileFieldGlobsReal *globs) throw(INTERP_KERNEL::Exception)
240 {
241   if(objs.empty())
242     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::BuildAggregationOf : empty input !");
243   if(objs.size()==1)
244     return MEDFileField1TSStructItem2(*objs[0]);
245   INTERP_KERNEL::NormalizedCellType gt(objs[0]->_geo_type);
246   int nbEntityRef(objs[0]->_nb_of_entity);
247   std::size_t sz(objs.size());
248   std::vector<const DataArrayInt *> arrs(sz);
249   for(std::size_t i=0;i<sz;i++)
250     {
251       const MEDFileField1TSStructItem2 *obj(objs[i]);
252       if(gt!=obj->_geo_type)
253         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::BuildAggregationOf : invalid situation ! All input must have the same geo type !");
254       if(nbEntityRef!=obj->_nb_of_entity)
255         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::BuildAggregationOf : invalid situation ! All input must have the global nb of entity !");
256       if(obj->_pfl->getName().empty())
257         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::BuildAggregationOf : invalid situation ! Several same geo type chunk must all lie on profiles !");
258       arrs[i]=globs->getProfile(obj->_pfl->getName().c_str());
259     }
260   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr(DataArrayInt::Aggregate(arrs));
261   arr->sort();
262   int oldNbTuples(arr->getNumberOfTuples());
263   arr=arr->buildUnique();
264   if(oldNbTuples!=arr->getNumberOfTuples())
265     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::BuildAggregationOf : some entities are present several times !");
266   if(arr->isIdentity() && oldNbTuples==nbEntityRef)
267     {
268       std::pair<int,int> p(0,nbEntityRef);
269       std::string a,b;
270       MEDFileField1TSStructItem2 ret(gt,p,a,b);
271       ret._nb_of_entity=nbEntityRef;
272       return ret;
273     }
274   else
275     {
276       arr->setName(NEWLY_CREATED_PFL_NAME);
277       std::pair<int,int> p(0,oldNbTuples);
278       std::string a,b;
279       MEDFileField1TSStructItem2 ret(gt,p,a,b);
280       ret._nb_of_entity=nbEntityRef;
281       ret._pfl=arr;
282       return ret;
283     }
284 }
285
286 std::size_t MEDFileField1TSStructItem2::getHeapMemorySize() const
287 {
288   std::size_t ret(0);
289   const DataArrayInt *pfl(_pfl);
290   if(pfl)
291     ret+=pfl->getHeapMemorySize();
292   ret+=_loc.capacity();
293   return ret;
294 }
295
296 //=
297
298 MEDFileField1TSStructItem::MEDFileField1TSStructItem(TypeOfField a, const std::vector< MEDFileField1TSStructItem2 >& b):_computed(false),_type(a),_items(b)
299 {
300 }
301
302 void MEDFileField1TSStructItem::checkWithMeshStruct(const MEDFileMeshStruct *mst, const MEDFileFieldGlobsReal *globs) throw(INTERP_KERNEL::Exception)
303 {
304   switch(_type)
305     {
306     case ON_NODES:
307       {
308         int nbOfEnt=mst->getNumberOfNodes();
309         if(_items.size()!=1)
310           throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::checkWithMeshStruct : for nodes field only one subdivision supported !");
311         _items[0].checkInRange(nbOfEnt,1,globs);
312         break ;
313       }
314     case ON_CELLS:
315       {
316         for(std::vector< MEDFileField1TSStructItem2 >::iterator it=_items.begin();it!=_items.end();it++)
317           (*it).checkWithMeshStructForCells(mst,globs);
318         break;
319       }
320     case ON_GAUSS_NE:
321       {
322         for(std::vector< MEDFileField1TSStructItem2 >::iterator it=_items.begin();it!=_items.end();it++)
323           (*it).checkWithMeshStructForGaussNE(mst,globs);
324         break;
325       }
326     case ON_GAUSS_PT:
327       {
328         for(std::vector< MEDFileField1TSStructItem2 >::iterator it=_items.begin();it!=_items.end();it++)
329           (*it).checkWithMeshStructForGaussPT(mst,globs);
330         break;
331       }
332     default:
333       throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::checkWithMeshStruct : not managed field type !");
334     }
335 }
336
337 bool MEDFileField1TSStructItem::operator==(const MEDFileField1TSStructItem& other) const throw(INTERP_KERNEL::Exception)
338 {
339   if(_type!=other._type)
340     return false;
341   if(_items.size()!=other._items.size())
342     return false;
343   for(std::size_t i=0;i<_items.size();i++)
344     if(!(_items[i]==other._items[i]))
345       return false;
346   return true;
347 }
348
349 bool MEDFileField1TSStructItem::isCellSupportEqual(const MEDFileField1TSStructItem& other, const MEDFileFieldGlobsReal *globs) const throw(INTERP_KERNEL::Exception)
350 {
351   if(_type!=other._type)
352     return false;
353   if(_items.size()!=other._items.size())
354     return false;
355   for(std::size_t i=0;i<_items.size();i++)
356     if(!(_items[i].isCellSupportEqual(other._items[i],globs)))
357       return false;
358   return true;
359 }
360
361 bool MEDFileField1TSStructItem::isNodeSupportEqual(const MEDFileField1TSStructItem& other, const MEDFileFieldGlobsReal *globs) const throw(INTERP_KERNEL::Exception)
362 {
363   if(_type!=other._type)
364     return false;
365   if(_items.size()!=other._items.size())
366     return false;
367   for(std::size_t i=0;i<_items.size();i++)
368     if(!(_items[i].isNodeSupportEqual(other._items[i],globs)))
369       return false;
370   return true;
371 }
372
373 bool MEDFileField1TSStructItem::isEntityCell() const
374 {
375   if(_type==ON_NODES)
376     return false;
377   else
378     return true;
379 }
380
381 class CmpGeo
382 {
383 public:
384   CmpGeo(INTERP_KERNEL::NormalizedCellType geoTyp):_geo_type(geoTyp) { }
385   bool operator()(const std::pair< INTERP_KERNEL::NormalizedCellType, std::vector<std::size_t> > & v) const { return _geo_type==v.first; }
386 private:
387   INTERP_KERNEL::NormalizedCellType _geo_type;
388 };
389
390 MEDFileField1TSStructItem MEDFileField1TSStructItem::simplifyMeOnCellEntity(const MEDFileFieldGlobsReal *globs) const throw(INTERP_KERNEL::Exception)
391 {
392   if(!isEntityCell())
393     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::simplifyMeOnCellEntity : must be on ON_CELLS, ON_GAUSS_NE or ON_GAUSS_PT !");
394   std::vector< std::pair< INTERP_KERNEL::NormalizedCellType, std::vector<std::size_t> > > m;
395   std::size_t i=0;
396   for(std::vector< MEDFileField1TSStructItem2 >::const_iterator it=_items.begin();it!=_items.end();it++,i++)
397     {
398       std::vector< std::pair< INTERP_KERNEL::NormalizedCellType, std::vector<std::size_t> > >::iterator it0(std::find_if(m.begin(),m.end(),CmpGeo((*it).getGeo())));
399       if(it0==m.end())
400         m.push_back(std::pair< INTERP_KERNEL::NormalizedCellType, std::vector<std::size_t> >((*it).getGeo(),std::vector<std::size_t>(1,i)));
401       else
402         (*it0).second.push_back(i);
403     }
404   if(m.size()==_items.size())
405     {
406       MEDFileField1TSStructItem ret(*this);
407       ret._type=ON_CELLS;
408       return ret;
409     }
410   std::size_t sz(m.size());
411   std::vector< MEDFileField1TSStructItem2 > items(sz);
412   for(i=0;i<sz;i++)
413     {
414       const std::vector<std::size_t>& ids=m[i].second;
415       std::vector<const MEDFileField1TSStructItem2 *>objs(ids.size());
416       for(std::size_t j=0;j<ids.size();j++)
417         objs[j]=&_items[ids[j]];
418       items[i]=MEDFileField1TSStructItem2::BuildAggregationOf(objs,globs);
419     }
420   MEDFileField1TSStructItem ret(ON_CELLS,items);
421   ret._computed=true;
422   return ret;
423 }
424
425 /*!
426  * \a this is expected to be ON_CELLS and simplified.
427  */
428 bool MEDFileField1TSStructItem::isCompatibleWithNodesDiscr(const MEDFileField1TSStructItem& other, const MEDFileMeshStruct *meshSt, const MEDFileFieldGlobsReal *globs) const throw(INTERP_KERNEL::Exception)
429 {
430   if(other._type!=ON_NODES)
431     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isCompatibleWithNodesDiscr : other must be on nodes !");
432   if(other._items.size()!=1)
433     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isCompatibleWithNodesDiscr : other is on nodes but number of subparts !");
434   int theFirstLevFull;
435   bool ret0=isFullyOnOneLev(meshSt,theFirstLevFull);
436   const MEDFileField1TSStructItem2& otherNodeIt(other._items[0]);
437   if(otherNodeIt.getPflName().empty())
438     {//on all nodes
439       if(!ret0)
440         return false;
441       return theFirstLevFull==0;
442     }
443   else
444     {
445       const DataArrayInt *pfl=globs->getProfile(otherNodeIt.getPflName().c_str());
446       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> cpyPfl(pfl->deepCpy());
447       cpyPfl->sort();
448       int nbOfNodes(meshSt->getNumberOfNodes());
449       if(cpyPfl->isIdentity() && cpyPfl->getNumberOfTuples()==nbOfNodes)
450         {//on all nodes also !
451           if(!ret0)
452             return false;
453           return theFirstLevFull==0;
454         }
455       std::vector<bool> nodesFetched(nbOfNodes,false);
456       meshSt->getTheMesh()->whichAreNodesFetched(*this,globs,nodesFetched);
457       return cpyPfl->isFittingWith(nodesFetched);
458     }
459 }
460
461 bool MEDFileField1TSStructItem::isFullyOnOneLev(const MEDFileMeshStruct *meshSt, int& theFirstLevFull) const throw(INTERP_KERNEL::Exception)
462 {
463   if(_type!=ON_CELLS)
464     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isFullyOnOneLev : works only for ON_CELLS discretization !");
465   if(_items.empty())
466     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isFullyOnOneLev : items vector is empty !");
467   int nbOfLevs(meshSt->getNumberOfLevs());
468   if(nbOfLevs==0)
469     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isFullyOnOneLev : no levels in input mesh structure !");
470   std::vector<int> levs(nbOfLevs);
471   theFirstLevFull=1;
472   int nbOfGT=0;
473   std::set<INTERP_KERNEL::NormalizedCellType> gts;
474   for(std::vector< MEDFileField1TSStructItem2 >::const_iterator it=_items.begin();it!=_items.end();it++)
475     {
476       if(!(*it).getPflName().empty())
477         return false;
478       INTERP_KERNEL::NormalizedCellType gt((*it).getGeo());
479       if(gts.find(gt)!=gts.end())
480         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isFullyOnOneLev : internal error !");
481       gts.insert(gt);
482       int pos(meshSt->getLevelOfGeoType((*it).getGeo()));
483       levs[-pos]++;
484     }
485   for(int i=0;i<nbOfLevs;i++)
486     if(meshSt->getNumberOfGeoTypesInLev(-i)==levs[i])
487       { theFirstLevFull=-i; return true; }
488   return false;
489 }
490
491 const MEDFileField1TSStructItem2& MEDFileField1TSStructItem::operator[](std::size_t i) const throw(INTERP_KERNEL::Exception)
492 {
493   if(i<0 || i>=_items.size())
494     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::operator[] : input is not in valid range !");
495   return _items[i];
496 }
497
498 std::size_t MEDFileField1TSStructItem::getHeapMemorySize() const
499 {
500   std::size_t ret(0);
501   for(std::vector< MEDFileField1TSStructItem2 >::const_iterator it=_items.begin();it!=_items.end();it++)
502     ret+=(*it).getHeapMemorySize();
503   ret+=_items.size()*sizeof(MEDFileField1TSStructItem2);
504   return ret;
505 }
506
507 MEDMeshMultiLev *MEDFileField1TSStructItem::buildFromScratchDataSetSupportOnCells(const MEDFileMeshStruct *mst, const MEDFileFieldGlobsReal *globs) const throw(INTERP_KERNEL::Exception)
508 {
509   std::vector<INTERP_KERNEL::NormalizedCellType> a0;
510   std::vector<const DataArrayInt *> a1;
511   std::vector<int> a2;
512   std::size_t i(0);
513   for(std::vector< MEDFileField1TSStructItem2 >::const_iterator it=_items.begin();it!=_items.end();it++,i++)
514     {
515       a0[i]=(*it).getGeo();
516       a1[i]=(*it).getPfl(globs);
517       a2[i]=mst->getNumberOfElemsOfGeoType((*it).getGeo());
518     }
519   return MEDMeshMultiLev::New(mst->getTheMesh(),a0,a1,a2);
520 }
521
522 //=
523
524 MEDFileField1TSStruct *MEDFileField1TSStruct::New(const MEDFileAnyTypeField1TS *ref, MEDFileMeshStruct *mst) throw(INTERP_KERNEL::Exception)
525 {
526   return new MEDFileField1TSStruct(ref,mst);
527 }
528
529 MEDFileField1TSStruct::MEDFileField1TSStruct(const MEDFileAnyTypeField1TS *ref, MEDFileMeshStruct *mst)
530 {
531   _already_checked.push_back(BuildItemFrom(ref,mst));
532 }
533
534 void MEDFileField1TSStruct::checkWithMeshStruct(MEDFileMeshStruct *mst, const MEDFileFieldGlobsReal *globs) throw(INTERP_KERNEL::Exception)
535 {
536   if(_already_checked.empty())
537     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::checkWithMeshStruct : not correctly initialized !");
538   _already_checked.back().checkWithMeshStruct(mst,globs);
539 }
540
541 bool MEDFileField1TSStruct::isEqualConsideringThePast(const MEDFileAnyTypeField1TS *other, const MEDFileMeshStruct *mst) const throw(INTERP_KERNEL::Exception)
542 {
543   MEDFileField1TSStructItem b(BuildItemFrom(other,mst));
544   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++)
545     {
546       if((*it)==b)
547         return true;
548     }
549   return false;
550 }
551
552 /*!
553  * Not const because \a other structure will be added to the \c _already_checked attribute in case of success.
554  */
555 bool MEDFileField1TSStruct::isSupportSameAs(const MEDFileAnyTypeField1TS *other, const MEDFileMeshStruct *meshSt) throw(INTERP_KERNEL::Exception)
556 {
557   if(_already_checked.empty())
558     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::isSupportSameAs : no ref !");
559   MEDFileField1TSStructItem b(BuildItemFrom(other,meshSt));
560   if(!_already_checked[0].isEntityCell() || !b.isEntityCell())
561     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::isSupportSameAs : only available on cell entities !");
562   MEDFileField1TSStructItem other1(b.simplifyMeOnCellEntity(other));
563   int found=-1,i=0;
564   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++,i++)
565     if((*it).isComputed())
566       { found=i; break; }
567   bool ret(false);
568   if(found==-1)
569     {
570       MEDFileField1TSStructItem this1(_already_checked[0].simplifyMeOnCellEntity(other));
571       ret=this1.isCellSupportEqual(other1,other);
572       if(ret)
573         _already_checked.push_back(this1);
574     }
575   else
576     ret=_already_checked[found].isCellSupportEqual(other1,other);
577   if(ret)
578     _already_checked.push_back(b);
579   return ret;
580 }
581
582 bool MEDFileField1TSStruct::isCompatibleWithNodesDiscr(const MEDFileAnyTypeField1TS *other, const MEDFileMeshStruct *meshSt) throw(INTERP_KERNEL::Exception)
583 {
584   if(_already_checked.empty())
585     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::isCompatibleWithNodesDiscr : no ref !");
586   if(!_already_checked[0].isEntityCell())
587     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::isCompatibleWithNodesDiscr : only available on cell entities !");
588   MEDFileField1TSStructItem other1(BuildItemFrom(other,meshSt));
589   //
590   int found=-1,i=0;
591   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++,i++)
592     if((*it).isComputed())
593       { found=i; break; }
594   bool ret(false);
595   if(found==-1)
596     {
597       MEDFileField1TSStructItem this1(_already_checked[0].simplifyMeOnCellEntity(other));
598       ret=this1.isCompatibleWithNodesDiscr(other1,meshSt,other);
599       if(ret)
600         _already_checked.push_back(this1);
601     }
602   else
603     ret=_already_checked[found].isCompatibleWithNodesDiscr(other1,meshSt,other);
604   if(ret)
605     _already_checked.push_back(other1);
606   return ret;
607 }
608
609 std::size_t MEDFileField1TSStruct::getHeapMemorySize() const
610 {
611   std::size_t ret(0);
612   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++)
613     ret+=(*it).getHeapMemorySize();
614   ret+=_already_checked.capacity()*sizeof(MEDFileField1TSStructItem);
615   return ret;
616 }
617
618 MEDFileField1TSStructItem MEDFileField1TSStruct::BuildItemFrom(const MEDFileAnyTypeField1TS *ref, const MEDFileMeshStruct *meshSt)
619 {
620   TypeOfField atype;
621   std::vector< MEDFileField1TSStructItem2 > anItems;
622   //
623   std::vector< std::vector<std::string> > pfls,locs;
624   std::vector< std::vector<TypeOfField> > typesF;
625   std::vector<INTERP_KERNEL::NormalizedCellType> geoTypes;
626   std::vector< std::vector<std::pair<int,int> > > strtEnds=ref->getFieldSplitedByType(0,geoTypes,typesF,pfls,locs);
627   std::size_t nbOfGeoTypes(geoTypes.size());
628   if(nbOfGeoTypes==0)
629     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct : not null by empty ref  !");
630   bool isFirst=true;
631   for(std::size_t i=0;i<nbOfGeoTypes;i++)
632     {
633       std::size_t sz=typesF[i].size();
634       if(strtEnds[i].size()<1 || sz<1 || pfls[i].size()<1)
635         throw INTERP_KERNEL::Exception("MEDFileField1TSStruct : internal error #1 !");
636       //
637       if(isFirst)
638         atype=typesF[i][0];
639       isFirst=false;
640       //
641       for(std::size_t j=0;j<sz;j++)
642         {
643           if(atype==typesF[i][j])
644             anItems.push_back(MEDFileField1TSStructItem2(geoTypes[i],strtEnds[i][j],pfls[i][j],locs[i][j]));
645           else
646             throw INTERP_KERNEL::Exception("MEDFileField1TSStruct : can be applied only on single spatial discretization fields ! Call SplitPerDiscretization method !");
647         }
648     }
649   MEDFileField1TSStructItem ret(atype,anItems);
650   ret.checkWithMeshStruct(meshSt,ref);
651   return ret;
652 }
653
654 MEDMeshMultiLev *MEDFileField1TSStruct::buildFromScratchDataSetSupport(const MEDFileMeshStruct *mst, const MEDFileFieldGlobsReal *globs) const throw(INTERP_KERNEL::Exception)
655 {
656   if(_already_checked.empty())
657     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::buildFromScratchDataSetSupport : No outline structure in this !");
658   int pos0(-1),pos1(-1);
659   if(presenceOfCellDiscr(pos0))
660     {
661       MEDCouplingAutoRefCountObjectPtr<MEDMeshMultiLev> ret(_already_checked[pos0].buildFromScratchDataSetSupportOnCells(mst,globs));
662       if(presenceOfPartialNodeDiscr(pos1))
663         ret->setNodeReduction(_already_checked[pos1][0].getPfl(globs));
664       return ret.retn();
665     }
666   else
667     {
668       if(!presenceOfPartialNodeDiscr(pos1))
669         {//we have only all nodes, no cell definition info -> level 0;
670           std::vector<int> levs(1,0);
671           return MEDMeshMultiLev::New(mst->getTheMesh(),levs);
672         }
673       else
674         return MEDMeshMultiLev::NewOnlyOnNode(mst->getTheMesh(),_already_checked[pos1][0].getPfl(globs));
675     }
676 }
677
678 bool MEDFileField1TSStruct::isDataSetSupportFastlyEqualTo(const MEDFileField1TSStruct& other, const MEDFileFieldGlobsReal *globs) const throw(INTERP_KERNEL::Exception)
679 {
680   int b0,b1;
681   bool a0(presenceOfCellDiscr(b0)),a1(presenceOfPartialNodeDiscr(b1));
682   int d0,d1;
683   bool c0(other.presenceOfCellDiscr(d0)),c1(other.presenceOfPartialNodeDiscr(d1)); 
684   if(a0!=c0 || a1!=c1)
685     return false;
686   if(a0)
687     if(!_already_checked[b0].isCellSupportEqual(other._already_checked[d0],globs))
688       return false;
689   if(a1)
690     if(!_already_checked[b1].isNodeSupportEqual(other._already_checked[d1],globs))
691       return false;
692   return true;
693 }
694
695 /*!
696  * Returns true if presence in \a this of discretization ON_CELLS, ON_GAUSS_PT, ON_GAUSS_NE.
697  * If true is returned the pos of the easiest is returned. The easiest is the first element in \a this having the less splitted subparts.
698  */
699 bool MEDFileField1TSStruct::presenceOfCellDiscr(int& pos) const throw(INTERP_KERNEL::Exception)
700 {
701   std::size_t refSz(std::numeric_limits<std::size_t>::max());
702   bool ret(false);
703   int i(0);
704   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++,i++)
705     {
706       if((*it).getType()!=ON_NODES)
707         {
708           ret=true;
709           std::size_t sz((*it).getNumberOfItems());
710           if(refSz>sz)
711             { pos=i; refSz=sz; }
712         }
713     }
714   if(refSz==0)
715     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::presenceOfCellDiscr : an element in this on entity CELL is empty !");
716   return ret;
717 }
718
719 /*!
720  * Returns true if presence in \a this of discretization ON_NODES.
721  * If true is returned the pos of the first element containing the single subpart.
722  */
723 bool MEDFileField1TSStruct::presenceOfPartialNodeDiscr(int& pos) const throw(INTERP_KERNEL::Exception)
724 {
725   int i(0);
726   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++,i++)
727     {
728       if((*it).getType()==ON_NODES)
729         {
730           std::size_t sz((*it).getNumberOfItems());
731           if(sz==1)
732             {
733               if(!(*it)[0].getPflName().empty())
734                 { pos=i; return true; }
735             }
736           else
737             throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::presenceOfPartialNodeDiscr : an element in this on entity NODE is split into several parts !");
738         }
739     }
740   return false;
741 }
742
743 //=
744
745 MEDFileFastCellSupportComparator *MEDFileFastCellSupportComparator::New(const MEDFileMeshStruct *m, const MEDFileAnyTypeFieldMultiTS *ref) throw(INTERP_KERNEL::Exception)
746 {
747   return new MEDFileFastCellSupportComparator(m,ref);
748 }
749
750 MEDFileFastCellSupportComparator::MEDFileFastCellSupportComparator(const MEDFileMeshStruct *m, const MEDFileAnyTypeFieldMultiTS *ref)
751 {
752   if(!m)
753     throw INTERP_KERNEL::Exception("MEDFileFastCellSupportComparator constructor : null input mesh struct !");
754   _mesh_comp=const_cast<MEDFileMeshStruct *>(m); _mesh_comp->incrRef();
755   int nbPts=ref->getNumberOfTS();
756   _f1ts_cmps.resize(nbPts);
757   for(int i=0;i<nbPts;i++)
758     {
759       MEDCouplingAutoRefCountObjectPtr<MEDFileAnyTypeField1TS> elt=ref->getTimeStepAtPos(i);
760       _f1ts_cmps[i]=MEDFileField1TSStruct::New(elt,_mesh_comp);
761       _f1ts_cmps[i]->checkWithMeshStruct(_mesh_comp,elt);
762     }
763 }
764
765 std::size_t MEDFileFastCellSupportComparator::getHeapMemorySize() const
766 {
767   std::size_t ret(0);
768   const MEDFileMeshStruct *mst(_mesh_comp);
769   if(mst)
770     ret+=mst->getHeapMemorySize();
771   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileField1TSStruct> >::const_iterator it=_f1ts_cmps.begin();it!=_f1ts_cmps.end();it++)
772     {
773       const MEDFileField1TSStruct *cur(*it);
774       if(cur)
775         ret+=cur->getHeapMemorySize()+sizeof(MEDFileField1TSStruct);
776     }
777   ret+=_f1ts_cmps.capacity()*sizeof(MEDCouplingAutoRefCountObjectPtr<MEDFileField1TSStruct>);
778   return ret;
779 }
780
781 bool MEDFileFastCellSupportComparator::isEqual(const MEDFileAnyTypeFieldMultiTS *other) throw(INTERP_KERNEL::Exception)
782 {
783   int nbPts=other->getNumberOfTS();
784   if(nbPts!=(int)_f1ts_cmps.size())
785     {
786       std::ostringstream oss; oss << "MEDFileFastCellSupportComparator::isEqual : unexpected nb of time steps in  input ! Should be " << _f1ts_cmps.size() << " it is in reality " << nbPts << " !";
787       throw INTERP_KERNEL::Exception(oss.str().c_str());
788     }
789   for(int i=0;i<nbPts;i++)
790     {
791       MEDCouplingAutoRefCountObjectPtr<MEDFileAnyTypeField1TS> elt=other->getTimeStepAtPos(i);
792       if(!_f1ts_cmps[i]->isEqualConsideringThePast(elt,_mesh_comp))
793         if(!_f1ts_cmps[i]->isSupportSameAs(elt,_mesh_comp))
794           return false;
795     }
796   return true;
797 }
798
799 bool MEDFileFastCellSupportComparator::isCompatibleWithNodesDiscr(const MEDFileAnyTypeFieldMultiTS *other) throw(INTERP_KERNEL::Exception)
800 {
801   int nbPts=other->getNumberOfTS();
802   if(nbPts!=(int)_f1ts_cmps.size())
803     {
804       std::ostringstream oss; oss << "MEDFileFastCellSupportComparator::isCompatibleWithNodesDiscr : unexpected nb of time steps in  input ! Should be " << _f1ts_cmps.size() << " it is in reality " << nbPts << " !";
805       throw INTERP_KERNEL::Exception(oss.str().c_str());
806     }
807   for(int i=0;i<nbPts;i++)
808     {
809       MEDCouplingAutoRefCountObjectPtr<MEDFileAnyTypeField1TS> elt=other->getTimeStepAtPos(i);
810       if(!_f1ts_cmps[i]->isCompatibleWithNodesDiscr(elt,_mesh_comp))
811         return false;
812     }
813   return true;
814 }
815
816 MEDMeshMultiLev *MEDFileFastCellSupportComparator::buildFromScratchDataSetSupport(int timeStepId, const MEDFileFieldGlobsReal *globs) const throw(INTERP_KERNEL::Exception)
817 {
818   if(timeStepId<0 || timeStepId>=(int)_f1ts_cmps.size())
819     {
820       std::ostringstream oss; oss << "MEDFileFastCellSupportComparator::buildFromScratchDataSetSupport : requested time step id #" << timeStepId << " is not in [0," << _f1ts_cmps.size() << ") !";
821       throw INTERP_KERNEL::Exception(oss.str().c_str());
822     }
823   const MEDFileField1TSStruct *obj(_f1ts_cmps[timeStepId]);
824   if(!obj)
825     {
826       std::ostringstream oss; oss << "MEDFileFastCellSupportComparator::buildFromScratchDataSetSupport : at time step id #" << timeStepId << " no field structure overview defined !";
827       throw INTERP_KERNEL::Exception(oss.str().c_str());
828     }
829   return obj->buildFromScratchDataSetSupport(_mesh_comp,globs);
830 }
831
832 bool MEDFileFastCellSupportComparator::isDataSetSupportEqualToThePreviousOne(int timeStepId, const MEDFileFieldGlobsReal *globs) const throw(INTERP_KERNEL::Exception)
833 {
834   if(timeStepId<=0 || timeStepId>=(int)_f1ts_cmps.size())
835     {
836       std::ostringstream oss; oss << "MEDFileFastCellSupportComparator::isDataSetSupportEqualToThePreviousOne : requested time step id #" << timeStepId << " is not in [1," << _f1ts_cmps.size() << ") !";
837       throw INTERP_KERNEL::Exception(oss.str().c_str());
838     }
839   const MEDFileField1TSStruct *obj(_f1ts_cmps[timeStepId]);
840   const MEDFileField1TSStruct *objRef(_f1ts_cmps[timeStepId-1]);
841   return objRef->isDataSetSupportFastlyEqualTo(*obj,globs);
842 }
843
844 //=
845
846 std::size_t MEDMeshMultiLev::getHeapMemorySize() const
847 {
848   return 0;
849 }
850
851 MEDMeshMultiLev *MEDMeshMultiLev::New(const MEDFileMesh *m, const std::vector<int>& levs) throw(INTERP_KERNEL::Exception)
852 {
853   if(!m)
854     throw INTERP_KERNEL::Exception("MEDMeshMultiLev::New : null input pointer !");
855   const MEDFileUMesh *um(dynamic_cast<const MEDFileUMesh *>(m));
856   if(um)
857     return MEDUMeshMultiLev::New(um,levs);
858   const MEDFileCMesh *cm(dynamic_cast<const MEDFileCMesh *>(m));
859   if(cm)
860     return MEDCMeshMultiLev::New(cm,levs);
861   const MEDFileCurveLinearMesh *clm(dynamic_cast<const MEDFileCurveLinearMesh *>(m));
862   if(clm)
863     return MEDCurveLinearMeshMultiLev::New(clm,levs);
864   throw INTERP_KERNEL::Exception("MEDMeshMultiLev::New : unrecognized type of mesh ! Must be in [MEDFileUMesh,MEDFileCMesh,MEDFileCurveLinearMesh] !");
865 }
866
867 MEDMeshMultiLev *MEDMeshMultiLev::New(const MEDFileMesh *m, const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities) throw(INTERP_KERNEL::Exception)
868 {
869   if(!m)
870     throw INTERP_KERNEL::Exception("MEDMeshMultiLev::New 2 : null input pointer !");
871   const MEDFileUMesh *um(dynamic_cast<const MEDFileUMesh *>(m));
872   if(um)
873     return MEDUMeshMultiLev::New(um,gts,pfls,nbEntities);
874   const MEDFileCMesh *cm(dynamic_cast<const MEDFileCMesh *>(m));
875   if(cm)
876     return MEDCMeshMultiLev::New(cm,gts,pfls,nbEntities);
877   const MEDFileCurveLinearMesh *clm(dynamic_cast<const MEDFileCurveLinearMesh *>(m));
878   if(clm)
879     return MEDCurveLinearMeshMultiLev::New(clm,gts,pfls,nbEntities);
880   throw INTERP_KERNEL::Exception("MEDMeshMultiLev::New 2 : unrecognized type of mesh ! Must be in [MEDFileUMesh,MEDFileCMesh,MEDFileCurveLinearMesh] !");
881 }
882
883 MEDMeshMultiLev *MEDMeshMultiLev::NewOnlyOnNode(const MEDFileMesh *m, const DataArrayInt *pflOnNode) throw(INTERP_KERNEL::Exception)
884 {
885   std::vector<int> levs(1,0);
886   MEDCouplingAutoRefCountObjectPtr<MEDMeshMultiLev> ret(MEDMeshMultiLev::New(m,levs));
887   ret->selectPartOfNodes(pflOnNode);
888   return ret.retn();
889 }
890
891 void MEDMeshMultiLev::setNodeReduction(const DataArrayInt *nr)
892 {
893   if(nr)
894     nr->incrRef();
895   _node_reduction=const_cast<DataArrayInt*>(nr);
896 }
897
898 bool MEDMeshMultiLev::isFastlyTheSameStruct(const MEDFileField1TSStructItem& fst, const MEDFileFieldGlobsReal *globs) const throw(INTERP_KERNEL::Exception)
899 {
900   if(fst.getType()==ON_NODES)
901     {
902       if(fst.getNumberOfItems()!=1)
903         throw INTERP_KERNEL::Exception("MEDMeshMultiLev::isFastlyTheSameStruct : unexpected situation for nodes !");
904       const MEDFileField1TSStructItem2& p(fst[0]);
905       std::string pflName(p.getPflName());
906       const DataArrayInt *nr(_node_reduction);
907       if(pflName.empty() && !nr)
908         return true;
909       if(pflName==nr->getName())
910         return true;
911       return false;
912     }
913   else
914     {
915       std::size_t sz(fst.getNumberOfItems());
916       if(sz!=_geo_types.size())
917         return false;
918       int strt(0);
919       for(std::size_t i=0;i<sz;i++)
920         {
921           const MEDFileField1TSStructItem2& p(fst[i]);
922           if(!p.isFastlyEqual(strt,_geo_types[i],getPflNameOfId(i).c_str()))
923             return false;
924         }
925       return true;
926     }
927 }
928
929 DataArray *MEDMeshMultiLev::buildDataArray(const MEDFileField1TSStructItem& fst, const MEDFileFieldGlobsReal *globs, const DataArray *vals) const throw(INTERP_KERNEL::Exception)
930 {
931   MEDCouplingAutoRefCountObjectPtr<DataArray> ret(const_cast<DataArray *>(vals)); ret->incrRef();
932   if(isFastlyTheSameStruct(fst,globs))
933     return ret.retn();
934   else
935     return constructDataArray(fst,globs,vals);
936 }
937
938 std::string MEDMeshMultiLev::getPflNameOfId(int id) const
939 {
940   std::size_t sz(_pfls.size());
941   if(id<0 || id>=sz)
942     throw INTERP_KERNEL::Exception("MEDMeshMultiLev::getPflNameOfId : invalid input id !");
943   const DataArrayInt *pfl(_pfls[id]);
944   if(!pfl)
945     return std::string("");
946   return pfl->getName();
947 }
948
949 DataArray *MEDMeshMultiLev::constructDataArray(const MEDFileField1TSStructItem& fst, const MEDFileFieldGlobsReal *globs, const DataArray *vals) const throw(INTERP_KERNEL::Exception)
950 {
951   if(fst.getType()==ON_NODES)
952     {
953       if(fst.getNumberOfItems()!=1)
954         throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for nodes !");
955       const MEDFileField1TSStructItem2& p(fst[0]);
956       std::string pflName(p.getPflName());
957       const DataArrayInt *nr(_node_reduction);
958       if(pflName.empty() && !nr)
959         return vals->deepCpy();
960       if(pflName.empty() && nr)
961          throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for nodes 2 !");
962       if(!pflName.empty() && nr)
963         {
964           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p1(globs->getProfile(pflName.c_str())->deepCpy());
965           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p2(nr->deepCpy());
966           p1->sort(true); p2->sort(true);
967           if(!p1->isEqualWithoutConsideringStr(*p2))
968             throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for nodes 3 !");
969           p1=globs->getProfile(pflName.c_str())->checkAndPreparePermutation();
970           p2=nr->checkAndPreparePermutation();
971           p1=p1->invertArrayO2N2N2O(p1->getNumberOfTuples());
972           p2=p1->selectByTupleIdSafe(p2->begin(),p2->end());
973           MEDCouplingAutoRefCountObjectPtr<DataArray> ret(vals->deepCpy());
974           ret->renumberInPlace(p2->begin());
975           return ret.retn();
976         }
977       if(!pflName.empty() && !nr)
978         {
979           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p1(globs->getProfile(pflName.c_str())->deepCpy());
980           p1->sort(true);
981           if(!p1->isIdentity() || p1->getNumberOfTuples()!=p.getNbEntity())
982             throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for nodes 4 !");
983           MEDCouplingAutoRefCountObjectPtr<DataArray> ret(vals->deepCpy());
984           ret->renumberInPlace(globs->getProfile(pflName.c_str())->begin());
985           return ret.retn();
986         }
987       throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for nodes 5 !");
988     }
989   else
990     {
991       std::size_t sz(fst.getNumberOfItems());
992       std::vector< MEDCouplingAutoRefCountObjectPtr<DataArray> > arrSafe(sz);
993       std::vector< const DataArray *> arr(sz);
994       for(std::size_t i=0;i<sz;i++)
995         {
996           const MEDFileField1TSStructItem2& p(fst[i]);
997           const std::pair<int,int>& strtStop(p.getStartStop());
998           std::vector< INTERP_KERNEL::NormalizedCellType >::const_iterator it(std::find(_geo_types.begin(),_geo_types.end(),p.getGeo()));
999           if(it==_geo_types.end())
1000             throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for cells 1 !");
1001           if(std::find(it+1,_geo_types.end(),p.getGeo())!=_geo_types.end())
1002             throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for cells 2 !");
1003           std::size_t pos(std::distance(_geo_types.begin(),it));
1004           const DataArrayInt *thisP(_pfls[pos]),*otherP(p.getPfl(globs));
1005           MEDCouplingAutoRefCountObjectPtr<DataArray> ret(vals->selectByTupleId2(strtStop.first,strtStop.second,1));
1006           if(!thisP && !otherP)
1007             {
1008               arrSafe[i]=ret; arr[i]=ret;
1009               continue;
1010             }
1011           int nbi(p.getNbOfIntegrationPts(globs)),nc(ret->getNumberOfComponents());
1012           if(!thisP && otherP)
1013             {
1014               MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p1(otherP->deepCpy());
1015               p1->sort(true);
1016               if(!p1->isIdentity() || p1->getNumberOfTuples()!=p.getNbEntity())
1017                 throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for cells 3 !");
1018               ret->rearrange(nbi*nc); ret->renumberInPlace(otherP->begin()); ret->rearrange(nc);
1019               arrSafe[i]=ret; arr[i]=ret;
1020               continue;
1021             }
1022           if(thisP && otherP)
1023             {
1024               MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p1(otherP->deepCpy());
1025               MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p2(thisP->deepCpy());
1026               p1->sort(true); p2->sort(true);
1027               if(!p1->isEqualWithoutConsideringStr(*p2))
1028                 throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for cells 4 !");
1029               p1=otherP->checkAndPreparePermutation();
1030               p2=thisP->checkAndPreparePermutation();
1031               p1=p1->invertArrayO2N2N2O(p1->getNumberOfTuples());
1032               p2=p1->selectByTupleIdSafe(p2->begin(),p2->end());
1033               ret->rearrange(nbi*nc); ret->renumberInPlace(otherP->begin()); ret->rearrange(nc);
1034               continue;
1035             }
1036           if(thisP && !otherP)
1037             {
1038               MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p1(thisP->deepCpy());
1039               p1->sort(true);
1040               if(!p1->isIdentity() || p1->getNumberOfTuples()!=p.getNbEntity())
1041                 throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for cells 3 !");
1042               ret->rearrange(nbi*nc); ret->renumberInPlaceR(otherP->begin()); ret->rearrange(nc);
1043               arrSafe[i]=ret; arr[i]=ret;
1044               continue;
1045             }
1046           throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for cells 6 !");
1047         }
1048       return DataArray::Aggregate(arr);
1049     }
1050 }
1051
1052 MEDMeshMultiLev::MEDMeshMultiLev()
1053 {
1054 }
1055
1056 MEDMeshMultiLev::MEDMeshMultiLev(const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities):_geo_types(gts),_nb_entities(nbEntities)
1057 {
1058   std::size_t sz(_geo_types.size());
1059   if(sz!=pfls.size() || sz!=nbEntities.size())
1060     throw INTERP_KERNEL::Exception("MEDMeshMultiLev::MEDMeshMultiLev : input vector must have the same size !");
1061   _pfls.resize(sz);
1062   for(std::size_t i=0;i<sz;i++)
1063     {
1064       if(pfls[i])
1065         pfls[i]->incrRef();
1066       _pfls[i]=const_cast<DataArrayInt *>(pfls[i]);
1067     }
1068 }
1069
1070 MEDMeshMultiLev::MEDMeshMultiLev(const MEDMeshMultiLev& other):_pfls(other._pfls),_geo_types(other._geo_types),_nb_entities(other._nb_entities),_node_reduction(other._node_reduction)
1071 {
1072 }
1073
1074 //=
1075
1076 MEDUMeshMultiLev *MEDUMeshMultiLev::New(const MEDFileUMesh *m, const std::vector<int>& levs) throw(INTERP_KERNEL::Exception)
1077 {
1078   return new MEDUMeshMultiLev(m,levs);
1079 }
1080
1081 MEDUMeshMultiLev::MEDUMeshMultiLev(const MEDFileUMesh *m, const std::vector<int>& levs)
1082 {
1083   if(!m)
1084     throw INTERP_KERNEL::Exception("MEDUMeshMultiLev constructor : null input pointer !");
1085   std::vector<MEDCoupling1GTUMesh *> v;
1086   for(std::vector<int>::const_iterator it=levs.begin();it!=levs.end();it++)
1087     {
1088       std::vector<MEDCoupling1GTUMesh *> vTmp(m->getDirectUndergroundSingleGeoTypeMeshes(*it));
1089       v.insert(v.end(),vTmp.begin(),vTmp.end());
1090     }
1091   std::size_t sz(v.size());
1092   _parts.resize(sz);
1093   _pfls.resize(sz);
1094   _geo_types.resize(sz);
1095   for(std::size_t i=0;i<sz;i++)
1096     {
1097       MEDCoupling1GTUMesh *obj(v[i]);
1098       if(obj)
1099         obj->incrRef();
1100       else
1101         throw INTERP_KERNEL::Exception("MEDUMeshMultiLev constructor : presence of a null pointer !");
1102       _parts[i]=obj;
1103       _geo_types[i]=obj->getCellModelEnum();
1104     }
1105 }
1106
1107 MEDUMeshMultiLev *MEDUMeshMultiLev::New(const MEDFileUMesh *m, const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities) throw(INTERP_KERNEL::Exception)
1108 {
1109   return new MEDUMeshMultiLev(m,gts,pfls,nbEntities);
1110 }
1111
1112 MEDUMeshMultiLev::MEDUMeshMultiLev(const MEDFileUMesh *m, const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities):MEDMeshMultiLev(gts,pfls,nbEntities)
1113 {
1114   std::size_t sz(gts.size());
1115   _parts.resize(sz);
1116   for(std::size_t i=0;i<sz;i++)
1117     {
1118       MEDCoupling1GTUMesh *elt(m->getDirectUndergroundSingleGeoTypeMesh(gts[i]));
1119       if(elt)
1120         elt->incrRef();
1121       _parts[i]=elt;
1122     }
1123 }
1124
1125 void MEDUMeshMultiLev::selectPartOfNodes(const DataArrayInt *pflNodes) throw(INTERP_KERNEL::Exception)
1126 {
1127    if(!pflNodes || !pflNodes->isAllocated())
1128      return ;
1129    std::size_t sz(_parts.size());
1130    std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> > a(sz);
1131    std::vector< const DataArrayInt *> aa(sz);
1132    for(std::size_t i=0;i<sz;i++)
1133      {
1134        
1135        const DataArrayInt *pfl(_pfls[i]);
1136        MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> m(_parts[i]);
1137        if(pfl)
1138          m=dynamic_cast<MEDCoupling1GTUMesh *>(_parts[i]->buildPartOfMySelfKeepCoords(pfl->begin(),pfl->end()));
1139        DataArrayInt *cellIds=0;
1140        m->fillCellIdsToKeepFromNodeIds(pflNodes->begin(),pflNodes->end(),true,cellIds);
1141        MEDCouplingAutoRefCountObjectPtr<DataArrayInt> cellIdsSafe(cellIds);
1142        MEDCouplingAutoRefCountObjectPtr<MEDCouplingPointSet> m2(m->buildPartOfMySelfKeepCoords(cellIds->begin(),cellIds->end()));
1143        int tmp=-1;
1144        a[i]=m2->getNodeIdsInUse(tmp); aa[i]=a[i];
1145        if(pfl)
1146          _pfls[i]=pfl->selectByTupleIdSafe(cellIds->begin(),cellIds->end());
1147        else
1148          _pfls[i]=cellIdsSafe;
1149      }
1150    _node_reduction=DataArrayInt::Aggregate(aa);
1151    _node_reduction->sort(true);
1152    _node_reduction=_node_reduction->buildUnique();
1153 }
1154
1155 MEDMeshMultiLev *MEDUMeshMultiLev::prepare() const throw(INTERP_KERNEL::Exception)
1156 {
1157   return new MEDUMeshMultiLev(*this);
1158 }
1159
1160 MEDUMeshMultiLev::MEDUMeshMultiLev(const MEDUMeshMultiLev& other):MEDMeshMultiLev(other),_parts(other._parts)
1161 {
1162 }
1163
1164 MEDUMeshMultiLev::MEDUMeshMultiLev(const MEDStructuredMeshMultiLev& other, const MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh>& part):MEDMeshMultiLev(other)
1165 {
1166   _parts.resize(1);
1167   _parts[0]=part;
1168 }
1169
1170 //=
1171
1172 MEDStructuredMeshMultiLev::MEDStructuredMeshMultiLev()
1173 {
1174 }
1175
1176 MEDStructuredMeshMultiLev::MEDStructuredMeshMultiLev(const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities):MEDMeshMultiLev(gts,pfls,nbEntities)
1177 {
1178 }
1179
1180 void MEDStructuredMeshMultiLev::selectPartOfNodes(const DataArrayInt *pflNodes) throw(INTERP_KERNEL::Exception)
1181 {
1182   if(!pflNodes || !pflNodes->isAllocated())
1183     return ;
1184   std::vector<int> ngs(getNodeGridStructure());
1185   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(MEDCouplingStructuredMesh::Build1GTNodalConnectivity(&ngs[0],&ngs[0]+ngs.size()));
1186   MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> m(MEDCoupling1SGTUMesh::New("",MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension(ngs.size())));
1187   m->setNodalConnectivity(conn);
1188   const DataArrayInt *pfl(_pfls[0]);
1189   if(pfl)
1190     {
1191       m=dynamic_cast<MEDCoupling1SGTUMesh *>(m->buildPartOfMySelfKeepCoords(pfl->begin(),pfl->end()));
1192     }
1193   DataArrayInt *cellIds=0;
1194   m->fillCellIdsToKeepFromNodeIds(pflNodes->begin(),pflNodes->end(),true,cellIds);
1195   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> cellIdsSafe(cellIds);
1196   MEDCouplingAutoRefCountObjectPtr<MEDCouplingPointSet> m2(m->buildPartOfMySelfKeepCoords(cellIds->begin(),cellIds->end()));
1197   int tmp=-1;
1198   _node_reduction=m2->getNodeIdsInUse(tmp);
1199   if(pfl)
1200     _pfls[0]=pfl->selectByTupleIdSafe(cellIds->begin(),cellIds->end());
1201   else
1202     _pfls[0]=cellIdsSafe;
1203 }
1204
1205 MEDStructuredMeshMultiLev::MEDStructuredMeshMultiLev(const MEDStructuredMeshMultiLev& other):MEDMeshMultiLev(other)
1206 {
1207 }
1208
1209 //=
1210
1211 MEDCMeshMultiLev *MEDCMeshMultiLev::New(const MEDFileCMesh *m, const std::vector<int>& levs) throw(INTERP_KERNEL::Exception)
1212 {
1213   return new MEDCMeshMultiLev(m,levs);
1214 }
1215
1216 MEDCMeshMultiLev *MEDCMeshMultiLev::New(const MEDFileCMesh *m, const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities) throw(INTERP_KERNEL::Exception)
1217 {
1218   return new MEDCMeshMultiLev(m,gts,pfls,nbEntities);
1219 }
1220
1221 MEDCMeshMultiLev::MEDCMeshMultiLev(const MEDFileCMesh *m, const std::vector<int>& levs)
1222 {
1223   if(!m)
1224     throw INTERP_KERNEL::Exception("MEDCMeshMultiLev constructor : null input pointer !");
1225   if(levs.size()!=1 || levs[0]!=0)
1226     throw INTERP_KERNEL::Exception("MEDCMeshMultiLev constructor : levels supported is 0 only !");
1227   int mdim(MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension(m->getMeshDimension()));
1228   _coords.resize(mdim);
1229   for(int i=0;i<mdim;i++)
1230     {
1231       DataArrayDouble *elt(const_cast<DataArrayDouble *>(m->getMesh()->getCoordsAt(i)));
1232       if(!elt)
1233         throw INTERP_KERNEL::Exception("MEDCMeshMultiLev constructor 2 : presence of null pointer for an vector of double along an axis !");
1234       _coords[i]=elt;
1235     }
1236 }
1237
1238 MEDCMeshMultiLev::MEDCMeshMultiLev(const MEDFileCMesh *m, const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities):MEDStructuredMeshMultiLev(gts,pfls,nbEntities)
1239 {
1240   if(!m)
1241     throw INTERP_KERNEL::Exception("MEDCMeshMultiLev constructor 2 : null input pointer !");
1242   if(gts.size()!=1 || pfls.size()!=1)
1243     throw INTERP_KERNEL::Exception("MEDCMeshMultiLev constructor 2 : lengthes of gts and pfls must be equal to one !");
1244   int mdim(MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension(m->getMeshDimension()));
1245   if(mdim!=gts[0])
1246     throw INTERP_KERNEL::Exception("MEDCMeshMultiLev constructor 2 : the unique geo type is invalid regarding meshdim !");
1247   _coords.resize(mdim);
1248   for(int i=0;i<mdim;i++)
1249     {
1250       DataArrayDouble *elt(const_cast<DataArrayDouble *>(m->getMesh()->getCoordsAt(i)));
1251       if(!elt)
1252         throw INTERP_KERNEL::Exception("MEDCMeshMultiLev constructor 2 : presence of null pointer for an vector of double along an axis !");
1253       _coords[i]=elt;
1254     }
1255 }
1256
1257 MEDCMeshMultiLev::MEDCMeshMultiLev(const MEDCMeshMultiLev& other):MEDStructuredMeshMultiLev(other)
1258 {
1259 }
1260
1261 std::vector<int> MEDCMeshMultiLev::getNodeGridStructure() const throw(INTERP_KERNEL::Exception)
1262 {
1263   std::vector<int> ret(_coords.size());
1264   for(std::size_t i=0;i<_coords.size();i++)
1265     ret[i]=_coords[i]->getNumberOfTuples();
1266   return ret;
1267 }
1268
1269 MEDMeshMultiLev *MEDCMeshMultiLev::prepare() const throw(INTERP_KERNEL::Exception)
1270 {
1271   const DataArrayInt *pfl(_pfls[0]),*nr(_node_reduction);
1272   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> nnr;
1273   std::vector<int> cgs,ngs(getNodeGridStructure());
1274   cgs.resize(ngs.size());
1275   std::transform(ngs.begin(),ngs.end(),cgs.begin(),std::bind2nd(std::plus<int>(),-1));
1276   if(pfl)
1277     {
1278       std::vector< std::pair<int,int> > cellParts;
1279       if(MEDCouplingStructuredMesh::IsPartStructured(pfl->begin(),pfl->end(),cgs,cellParts))
1280         {
1281           MEDCouplingAutoRefCountObjectPtr<MEDCMeshMultiLev> ret(new MEDCMeshMultiLev(*this));
1282           if(nr)
1283             { nnr=nr->deepCpy(); nnr->sort(true); ret->setNodeReduction(nnr); }
1284           ret->_nb_entities[0]=pfl->getNumberOfTuples();
1285           ret->_pfls[0]=0;
1286           std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> > coords(_coords.size());
1287           for(std::size_t i=0;i<_coords.size();i++)
1288             coords[i]=_coords[i]->selectByTupleId2(cellParts[i].first,cellParts[i].second+1,1);
1289           ret->_coords=coords;
1290           return ret.retn();
1291         }
1292       else
1293         {
1294           MEDCouplingAutoRefCountObjectPtr<MEDCouplingCMesh> m(MEDCouplingCMesh::New());
1295           for(std::size_t i=0;i<ngs.size();i++)
1296             m->setCoordsAt(i,_coords[i]);
1297           MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> m2(m->build1SGTUnstructured());
1298           MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> m3=dynamic_cast<MEDCoupling1GTUMesh *>(m2->buildPartOfMySelfKeepCoords(pfl->begin(),pfl->end()));
1299           MEDCouplingAutoRefCountObjectPtr<MEDUMeshMultiLev> ret(new MEDUMeshMultiLev(*this,m3));
1300           if(nr)
1301             { m3->zipCoords(); nnr=nr->deepCpy(); nnr->sort(true); ret->setNodeReduction(nnr); }
1302           return ret.retn();
1303         }
1304     }
1305   else
1306     {
1307       MEDCouplingAutoRefCountObjectPtr<MEDCMeshMultiLev> ret(new MEDCMeshMultiLev(*this));
1308       if(nr)
1309         { nnr=nr->deepCpy(); nnr->sort(true); ret->setNodeReduction(nnr); }
1310       return ret.retn();
1311     }
1312 }
1313
1314 //=
1315
1316 MEDCurveLinearMeshMultiLev *MEDCurveLinearMeshMultiLev::New(const MEDFileCurveLinearMesh *m, const std::vector<int>& levs) throw(INTERP_KERNEL::Exception)
1317 {
1318   return new MEDCurveLinearMeshMultiLev(m,levs);
1319 }
1320
1321 MEDCurveLinearMeshMultiLev *MEDCurveLinearMeshMultiLev::New(const MEDFileCurveLinearMesh *m, const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities) throw(INTERP_KERNEL::Exception)
1322 {
1323   return new MEDCurveLinearMeshMultiLev(m,gts,pfls,nbEntities);
1324 }
1325
1326 MEDCurveLinearMeshMultiLev::MEDCurveLinearMeshMultiLev(const MEDFileCurveLinearMesh *m, const std::vector<int>& levs)
1327 {
1328   if(!m)
1329     throw INTERP_KERNEL::Exception("MEDCurveLinearMeshMultiLev constructor : null input pointer !");
1330   if(levs.size()!=1 || levs[0]!=0)
1331     throw INTERP_KERNEL::Exception("MEDCurveLinearMeshMultiLev constructor : levels supported is 0 only !");
1332   DataArrayDouble *coords(const_cast<DataArrayDouble *>(m->getMesh()->getCoords()));
1333   if(!coords)
1334     throw INTERP_KERNEL::Exception("MEDCurveLinearMeshMultiLev constructor 2 : no coords set !");
1335   coords->incrRef();
1336   _coords=coords;
1337   _structure=m->getMesh()->getNodeGridStructure();
1338 }
1339
1340 MEDCurveLinearMeshMultiLev::MEDCurveLinearMeshMultiLev(const MEDFileCurveLinearMesh *m, const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities):MEDStructuredMeshMultiLev(gts,pfls,nbEntities)
1341 {
1342   if(!m)
1343     throw INTERP_KERNEL::Exception("MEDCurveLinearMeshMultiLev constructor 2 : null input pointer !");
1344   if(gts.size()!=1 || pfls.size()!=1)
1345     throw INTERP_KERNEL::Exception("MEDCurveLinearMeshMultiLev constructor 2 : lengthes of gts and pfls must be equal to one !");
1346   int mdim(MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension(m->getMeshDimension()));
1347   if(mdim!=gts[0])
1348     throw INTERP_KERNEL::Exception("MEDCurveLinearMeshMultiLev constructor 2 : the unique geo type is invalid regarding meshdim !");
1349   DataArrayDouble *coords(const_cast<DataArrayDouble *>(m->getMesh()->getCoords()));
1350   if(!coords)
1351     throw INTERP_KERNEL::Exception("MEDCurveLinearMeshMultiLev constructor 2 : no coords set !");
1352   coords->incrRef();
1353   _coords=coords;
1354   _structure=m->getMesh()->getNodeGridStructure();
1355 }
1356
1357 MEDCurveLinearMeshMultiLev::MEDCurveLinearMeshMultiLev(const MEDCurveLinearMeshMultiLev& other):MEDStructuredMeshMultiLev(other),_coords(other._coords),_structure(other._structure)
1358 {
1359 }
1360
1361 std::vector<int> MEDCurveLinearMeshMultiLev::getNodeGridStructure() const throw(INTERP_KERNEL::Exception)
1362 {
1363   return _structure;
1364 }
1365
1366 MEDMeshMultiLev *MEDCurveLinearMeshMultiLev::prepare() const throw(INTERP_KERNEL::Exception)
1367 {
1368   const DataArrayInt *pfl(_pfls[0]),*nr(_node_reduction);
1369   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> nnr;
1370   std::vector<int> cgs,ngs(getNodeGridStructure());
1371   cgs.resize(ngs.size());
1372   std::transform(ngs.begin(),ngs.end(),cgs.begin(),std::bind2nd(std::plus<int>(),-1));
1373   if(pfl)
1374     {
1375       std::vector< std::pair<int,int> > cellParts,nodeParts;
1376       if(MEDCouplingStructuredMesh::IsPartStructured(pfl->begin(),pfl->end(),cgs,cellParts))
1377         {
1378           nodeParts=cellParts;
1379           std::vector<int> st(ngs.size());
1380           for(std::size_t i=0;i<ngs.size();i++)
1381             {
1382               nodeParts[i].second++;
1383               st[i]=nodeParts[i].second-nodeParts[i].first;
1384             }
1385           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p(MEDCouplingStructuredMesh::BuildExplicitIdsFrom(ngs,nodeParts));
1386           MEDCouplingAutoRefCountObjectPtr<MEDCurveLinearMeshMultiLev> ret(new MEDCurveLinearMeshMultiLev(*this));
1387           if(nr)
1388             { nnr=nr->deepCpy(); nnr->sort(true); ret->setNodeReduction(nnr); }
1389           ret->_nb_entities[0]=pfl->getNumberOfTuples();
1390           ret->_pfls[0]=0;
1391           ret->_coords=_coords->selectByTupleIdSafe(p->begin(),p->end());
1392           ret->_structure=st;
1393           return ret.retn();
1394         }
1395       else
1396         {
1397           MEDCouplingAutoRefCountObjectPtr<MEDCouplingCurveLinearMesh> m(MEDCouplingCurveLinearMesh::New());
1398           m->setCoords(_coords); m->setNodeGridStructure(&_structure[0],&_structure[0]+_structure.size());
1399           MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> m2(m->build1SGTUnstructured());
1400           MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> m3=dynamic_cast<MEDCoupling1GTUMesh *>(m2->buildPartOfMySelfKeepCoords(pfl->begin(),pfl->end()));
1401           MEDCouplingAutoRefCountObjectPtr<MEDUMeshMultiLev> ret(new MEDUMeshMultiLev(*this,m3));
1402           if(nr)
1403             { m3->zipCoords(); nnr=nr->deepCpy(); nnr->sort(true); ret->setNodeReduction(nnr); }
1404           return ret.retn();
1405         }
1406     }
1407   else
1408     {
1409       MEDCouplingAutoRefCountObjectPtr<MEDCurveLinearMeshMultiLev> ret(new MEDCurveLinearMeshMultiLev(*this));
1410       if(nr)
1411         { nnr=nr->deepCpy(); nnr->sort(true); ret->setNodeReduction(nnr); }
1412       return ret.retn();
1413     }
1414 }