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