Salome HOME
241cd422e2ae7484fcf427a13a86f41526e4639d
[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,255,255,255,255,10,14,13,255,12,255,24,255,16,27,255,26,255,29,255,255,25,42,36,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::getHeapMemorySizeWithoutChildren() 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 std::vector<const BigMemoryObject *> MEDFileMeshStruct::getDirectChildren() const
49 {
50   return std::vector<const BigMemoryObject *>();
51 }
52
53 MEDFileMeshStruct::MEDFileMeshStruct(const MEDFileMesh *mesh):_mesh(mesh)
54 {
55   std::vector<int> levs(mesh->getNonEmptyLevels());
56   _name=mesh->getName();
57   _nb_nodes=mesh->getNumberOfNodes();
58   if(!levs.empty())
59     {
60       _geo_types_distrib.resize(-(*std::min_element(levs.begin(),levs.end()))+1);
61       for(std::vector<int>::const_iterator lev=levs.begin();lev!=levs.end();lev++)
62         _geo_types_distrib[-(*lev)]=mesh->getDistributionOfTypes(*lev);
63     }
64 }
65
66 int MEDFileMeshStruct::getLevelOfGeoType(INTERP_KERNEL::NormalizedCellType t) const
67 {
68   int j=0;
69   for(std::vector< std::vector<int> >::const_iterator it1=_geo_types_distrib.begin();it1!=_geo_types_distrib.end();it1++,j--)
70     {
71       std::size_t sz=(*it1).size();
72       if(sz%3!=0)
73         throw INTERP_KERNEL::Exception("MEDFileMeshStruct::getLevelOfGeoType : internal error in code !");
74       std::size_t nbGeo=sz/3;
75       for(std::size_t i=0;i<nbGeo;i++)
76         if((*it1)[3*i]==(int)t)
77           return j;
78     }
79   throw INTERP_KERNEL::Exception("MEDFileMeshStruct::getLevelOfGeoType : The specified geometric type is not present in the mesh structure !");
80 }
81
82 int MEDFileMeshStruct::getNumberOfElemsOfGeoType(INTERP_KERNEL::NormalizedCellType t) const
83 {
84   for(std::vector< std::vector<int> >::const_iterator it1=_geo_types_distrib.begin();it1!=_geo_types_distrib.end();it1++)
85     {
86       std::size_t sz=(*it1).size();
87       if(sz%3!=0)
88         throw INTERP_KERNEL::Exception("MEDFileMeshStruct::getNumberOfElemsOfGeoType : internal error in code !");
89       std::size_t nbGeo=sz/3;
90       for(std::size_t i=0;i<nbGeo;i++)
91         if((*it1)[3*i]==(int)t)
92           return (*it1)[3*i+1];
93     }
94   throw INTERP_KERNEL::Exception("The specified geometric type is not present in the mesh structure !");
95 }
96
97 int MEDFileMeshStruct::getNumberOfLevs() const
98 {
99   return (int)_geo_types_distrib.size();
100 }
101
102 int MEDFileMeshStruct::getNumberOfGeoTypesInLev(int relativeLev) const
103 {
104   int pos(-relativeLev);
105   if(pos<0 || pos>=(int)_geo_types_distrib.size())
106     throw INTERP_KERNEL::Exception("MEDFileMeshStruct::getNumberOfGeoTypesInLev : invalid level specified !");
107   std::size_t sz=_geo_types_distrib[pos].size();
108   if(sz%3!=0)
109     throw INTERP_KERNEL::Exception("MEDFileMeshStruct::getNumberOfGeoTypesInLev : internal error in code !");
110   return (int)(sz/3);
111 }
112
113 //=
114
115 std::size_t MEDMeshMultiLev::getHeapMemorySizeWithoutChildren() const
116 {
117   return 0;
118 }
119
120 std::vector<const BigMemoryObject *> MEDMeshMultiLev::getDirectChildren() const
121 {
122   return std::vector<const BigMemoryObject *>();
123 }
124
125 MEDMeshMultiLev *MEDMeshMultiLev::New(const MEDFileMesh *m, const std::vector<int>& levs)
126 {
127   if(!m)
128     throw INTERP_KERNEL::Exception("MEDMeshMultiLev::New : null input pointer !");
129   const MEDFileUMesh *um(dynamic_cast<const MEDFileUMesh *>(m));
130   if(um)
131     return MEDUMeshMultiLev::New(um,levs);
132   const MEDFileCMesh *cm(dynamic_cast<const MEDFileCMesh *>(m));
133   if(cm)
134     return MEDCMeshMultiLev::New(cm,levs);
135   const MEDFileCurveLinearMesh *clm(dynamic_cast<const MEDFileCurveLinearMesh *>(m));
136   if(clm)
137     return MEDCurveLinearMeshMultiLev::New(clm,levs);
138   throw INTERP_KERNEL::Exception("MEDMeshMultiLev::New : unrecognized type of mesh ! Must be in [MEDFileUMesh,MEDFileCMesh,MEDFileCurveLinearMesh] !");
139 }
140
141 MEDMeshMultiLev *MEDMeshMultiLev::New(const MEDFileMesh *m, const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities)
142 {
143   if(!m)
144     throw INTERP_KERNEL::Exception("MEDMeshMultiLev::New 2 : null input pointer !");
145   const MEDFileUMesh *um(dynamic_cast<const MEDFileUMesh *>(m));
146   if(um)
147     return MEDUMeshMultiLev::New(um,gts,pfls,nbEntities);
148   const MEDFileCMesh *cm(dynamic_cast<const MEDFileCMesh *>(m));
149   if(cm)
150     return MEDCMeshMultiLev::New(cm,gts,pfls,nbEntities);
151   const MEDFileCurveLinearMesh *clm(dynamic_cast<const MEDFileCurveLinearMesh *>(m));
152   if(clm)
153     return MEDCurveLinearMeshMultiLev::New(clm,gts,pfls,nbEntities);
154   throw INTERP_KERNEL::Exception("MEDMeshMultiLev::New 2 : unrecognized type of mesh ! Must be in [MEDFileUMesh,MEDFileCMesh,MEDFileCurveLinearMesh] !");
155 }
156
157 MEDMeshMultiLev *MEDMeshMultiLev::NewOnlyOnNode(const MEDFileMesh *m, const DataArrayInt *pflOnNode)
158 {
159   MEDCouplingAutoRefCountObjectPtr<MEDMeshMultiLev> ret(MEDMeshMultiLev::New(m,m->getNonEmptyLevels()));
160   ret->selectPartOfNodes(pflOnNode);
161   return ret.retn();
162 }
163
164 void MEDMeshMultiLev::setNodeReduction(const DataArrayInt *nr)
165 {
166   if(nr)
167     nr->incrRef();
168   _node_reduction=const_cast<DataArrayInt*>(nr);
169 }
170
171 bool MEDMeshMultiLev::isFastlyTheSameStruct(const MEDFileField1TSStructItem& fst, const MEDFileFieldGlobsReal *globs) const
172 {
173   if(fst.getType()==ON_NODES)
174     {
175       if(fst.getNumberOfItems()!=1)
176         throw INTERP_KERNEL::Exception("MEDMeshMultiLev::isFastlyTheSameStruct : unexpected situation for nodes !");
177       const MEDFileField1TSStructItem2& p(fst[0]);
178       std::string pflName(p.getPflName());
179       const DataArrayInt *nr(_node_reduction);
180       if(pflName.empty() && !nr)
181         return true;
182       if(!pflName.empty() && !nr)
183         return false;
184       if(pflName==nr->getName())
185         return true;
186       return false;
187     }
188   else
189     {
190       std::size_t sz(fst.getNumberOfItems());
191       if(sz!=_geo_types.size())
192         return false;
193       int strt(0);
194       for(std::size_t i=0;i<sz;i++)
195         {
196           const MEDFileField1TSStructItem2& p(fst[i]);
197           if(!p.isFastlyEqual(strt,_geo_types[i],getPflNameOfId(i).c_str()))
198             return false;
199         }
200       return true;
201     }
202 }
203
204 DataArray *MEDMeshMultiLev::buildDataArray(const MEDFileField1TSStructItem& fst, const MEDFileFieldGlobsReal *globs, const DataArray *vals) const
205 {
206   MEDCouplingAutoRefCountObjectPtr<DataArray> ret(const_cast<DataArray *>(vals)); ret->incrRef();
207   if(isFastlyTheSameStruct(fst,globs))
208     return ret.retn();
209   else
210     return constructDataArray(fst,globs,vals);
211 }
212
213 /*!
214  * \param [out] famIds - Can be null. If not null the instance has to be dealt by the caller (decrRef).
215  * \param [out] isWithoutCopy - When true the returned instance \a famIds if not null is directly those in the data structure.
216  */
217 void MEDMeshMultiLev::retrieveFamilyIdsOnCells(DataArrayInt *& famIds, bool& isWithoutCopy) const
218 {
219   const DataArrayInt *fids(_cell_fam_ids);
220   if(!fids)
221     { famIds=0; isWithoutCopy=true; return ; }
222   std::size_t sz(_geo_types.size());
223   bool presenceOfPfls(false);
224   for(std::size_t i=0;i<sz && !presenceOfPfls;i++)
225     {
226       const DataArrayInt *pfl(_pfls[i]);
227       if(pfl)
228         presenceOfPfls=true;
229     }
230   if(!presenceOfPfls)
231     { famIds=const_cast<DataArrayInt *>(fids); famIds->incrRef(); isWithoutCopy=_cell_fam_ids_nocpy; return ; }
232   //bad luck the slowest part
233   isWithoutCopy=false;
234   std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> > retSafe(sz);
235   std::vector< const DataArrayInt *> ret(sz);
236   int start(0);
237   for(std::size_t i=0;i<sz;i++)
238     {
239       const DataArrayInt *pfl(_pfls[i]);
240       int lgth(_nb_entities[i]);
241       if(pfl)
242         {
243           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp(fids->selectByTupleId2(start,start+lgth,1));
244           retSafe[i]=tmp->selectByTupleIdSafe(pfl->begin(),pfl->end());
245         }
246       else
247         {
248           retSafe[i]=fids->selectByTupleId2(start,start+lgth,1);
249         }
250       ret[i]=retSafe[i];
251       start+=lgth;
252     }
253   famIds=DataArrayInt::Aggregate(ret);
254 }
255
256 /*!
257  * \param [out] numIds - Can be null. If not null the instance has to be dealt by the caller (decrRef).
258  * \param [out] isWithoutCopy - When true the returned instance \a numIds if not null is directly those in the data structure.
259  */
260 void MEDMeshMultiLev::retrieveNumberIdsOnCells(DataArrayInt *& numIds, bool& isWithoutCopy) const
261 {
262   const DataArrayInt *nids(_cell_num_ids);
263   if(!nids)
264     { numIds=0; isWithoutCopy=true; return ; }
265   std::size_t sz(_geo_types.size());
266   bool presenceOfPfls(false);
267   for(std::size_t i=0;i<sz && !presenceOfPfls;i++)
268     {
269       const DataArrayInt *pfl(_pfls[i]);
270       if(pfl)
271         presenceOfPfls=true;
272     }
273   if(!presenceOfPfls)
274     { numIds=const_cast<DataArrayInt *>(nids); numIds->incrRef(); isWithoutCopy=_cell_num_ids_nocpy; return ; }
275   //bad luck the slowest part
276   isWithoutCopy=false;
277   std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> > retSafe(sz);
278   std::vector< const DataArrayInt *> ret(sz);
279   int start(0);
280   for(std::size_t i=0;i<sz;i++)
281     {
282       const DataArrayInt *pfl(_pfls[i]);
283       int lgth(_nb_entities[i]);
284       if(pfl)
285         {
286           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp(nids->selectByTupleId2(start,start+lgth,1));
287           retSafe[i]=tmp->selectByTupleIdSafe(pfl->begin(),pfl->end());
288         }
289       else
290         {
291           retSafe[i]=nids->selectByTupleId2(start,start+lgth,1);
292         }
293       ret[i]=retSafe[i];
294       start+=lgth;
295     }
296   numIds=DataArrayInt::Aggregate(ret);
297 }
298
299 /*!
300  * \param [out] famIds - Can be null. If not null the instance has to be dealt by the caller (decrRef).
301  * \param [out] isWithoutCopy - When true the returned instance \a famIds if not null is directly those in the data structure.
302  */
303 void MEDMeshMultiLev::retrieveFamilyIdsOnNodes(DataArrayInt *& famIds, bool& isWithoutCopy) const
304 {
305   const DataArrayInt *fids(_node_fam_ids);
306   if(!fids)
307     { famIds=0; isWithoutCopy=true; return ; }
308   const DataArrayInt *nr(_node_reduction);
309   if(nr)
310     {
311       isWithoutCopy=false;
312       famIds=fids->selectByTupleIdSafe(nr->begin(),nr->end());
313     }
314   else
315     {
316       isWithoutCopy=_node_fam_ids_nocpy;
317       famIds=const_cast<DataArrayInt *>(fids); famIds->incrRef();
318     }
319 }
320
321 /*!
322  * \param [out] numIds - Can be null. If not null the instance has to be dealt by the caller (decrRef).
323  * \param [out] isWithoutCopy - When true the returned instance \a numIds if not null is directly those in the data structure.
324  */
325 void MEDMeshMultiLev::retrieveNumberIdsOnNodes(DataArrayInt *& numIds, bool& isWithoutCopy) const
326 {
327   const DataArrayInt *fids(_node_num_ids);
328   if(!fids)
329     { numIds=0; isWithoutCopy=true; return ; }
330   const DataArrayInt *nr(_node_reduction);
331   if(nr)
332     {
333       isWithoutCopy=false;
334       numIds=fids->selectByTupleIdSafe(nr->begin(),nr->end());
335     }
336   else
337     {
338       isWithoutCopy=_node_num_ids_nocpy;
339       numIds=const_cast<DataArrayInt *>(fids); numIds->incrRef();
340     }
341 }
342
343 std::vector< INTERP_KERNEL::NormalizedCellType > MEDMeshMultiLev::getGeoTypes() const
344 {
345   return _geo_types;
346 }
347
348 void MEDMeshMultiLev::setFamilyIdsOnCells(DataArrayInt *famIds, bool isNoCopy)
349 {
350   _cell_fam_ids=famIds;
351   if(famIds)
352     famIds->incrRef();
353   _cell_fam_ids_nocpy=isNoCopy;
354 }
355
356 void MEDMeshMultiLev::setNumberIdsOnCells(DataArrayInt *numIds, bool isNoCopy)
357 {
358   _cell_num_ids=numIds;
359   if(numIds)
360     numIds->incrRef();
361   _cell_num_ids_nocpy=isNoCopy;
362 }
363
364 void MEDMeshMultiLev::setFamilyIdsOnNodes(DataArrayInt *famIds, bool isNoCopy)
365 {
366   _node_fam_ids=famIds;
367   if(famIds)
368     famIds->incrRef();
369   _node_fam_ids_nocpy=isNoCopy;
370 }
371
372 void MEDMeshMultiLev::setNumberIdsOnNodes(DataArrayInt *numIds, bool isNoCopy)
373 {
374   _node_num_ids=numIds;
375   if(numIds)
376     numIds->incrRef();
377   _node_num_ids_nocpy=isNoCopy;
378 }
379
380 std::string MEDMeshMultiLev::getPflNameOfId(int id) const
381 {
382   std::size_t sz(_pfls.size());
383   if(id<0 || id>=(int)sz)
384     throw INTERP_KERNEL::Exception("MEDMeshMultiLev::getPflNameOfId : invalid input id !");
385   const DataArrayInt *pfl(_pfls[id]);
386   if(!pfl)
387     return std::string("");
388   return pfl->getName();
389 }
390
391 /*!
392  * Returns the number of cells having geometric type \a t.
393  * The profiles are **NOT** taken into account here.
394  */
395 int MEDMeshMultiLev::getNumberOfCells(INTERP_KERNEL::NormalizedCellType t) const
396 {
397   std::size_t sz(_nb_entities.size());
398   for(std::size_t i=0;i<sz;i++)
399     if(_geo_types[i]==t)
400         return _nb_entities[i];
401   throw INTERP_KERNEL::Exception("MEDMeshMultiLev::getNumberOfCells : not existing geometric type in this !");
402 }
403
404 int MEDMeshMultiLev::getNumberOfNodes() const
405 {
406   return _nb_nodes;
407 }
408
409 DataArray *MEDMeshMultiLev::constructDataArray(const MEDFileField1TSStructItem& fst, const MEDFileFieldGlobsReal *globs, const DataArray *vals) const
410 {
411   if(fst.getType()==ON_NODES)
412     {
413       if(fst.getNumberOfItems()!=1)
414         throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for nodes !");
415       const MEDFileField1TSStructItem2& p(fst[0]);
416       std::string pflName(p.getPflName());
417       const DataArrayInt *nr(_node_reduction);
418       if(pflName.empty() && !nr)
419         return vals->deepCpy();
420       if(pflName.empty() && nr)
421          throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for nodes 2 !");
422       if(!pflName.empty() && nr)
423         {
424           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p1(globs->getProfile(pflName.c_str())->deepCpy());
425           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p2(nr->deepCpy());
426           p1->sort(true); p2->sort(true);
427           if(!p1->isEqualWithoutConsideringStr(*p2))
428             throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : it appears that a profile on nodes does not cover the cells correctly !");
429           p1=DataArrayInt::FindPermutationFromFirstToSecond(globs->getProfile(pflName.c_str()),nr);
430           MEDCouplingAutoRefCountObjectPtr<DataArray> ret(vals->deepCpy());
431           ret->renumberInPlace(p1->begin());
432           return ret.retn();
433         }
434       if(!pflName.empty() && !nr)
435         {
436           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p1(globs->getProfile(pflName.c_str())->deepCpy());
437           p1->sort(true);
438           if(!p1->isIdentity() || p1->getNumberOfTuples()!=getNumberOfNodes())
439             throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for nodes 4 !");
440           MEDCouplingAutoRefCountObjectPtr<DataArray> ret(vals->deepCpy());
441           ret->renumberInPlace(globs->getProfile(pflName.c_str())->begin());
442           return ret.retn();
443         }
444       throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for nodes 5 !");
445     }
446   else
447     {
448       std::size_t sz(fst.getNumberOfItems());
449       std::set<INTERP_KERNEL::NormalizedCellType> s(_geo_types.begin(),_geo_types.end());
450       if(s.size()!=_geo_types.size())
451         throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for cells 2 !");
452       std::vector< const DataArray *> arr(s.size());
453       std::vector< MEDCouplingAutoRefCountObjectPtr<DataArray> > arrSafe(s.size());
454       int iii(0);
455       int nc(vals->getNumberOfComponents());
456       std::vector<std::string> compInfo(vals->getInfoOnComponents());
457       for(std::vector< INTERP_KERNEL::NormalizedCellType >::const_iterator it=_geo_types.begin();it!=_geo_types.end();it++,iii++)
458         {
459           const DataArrayInt *thisP(_pfls[iii]);
460           std::vector<const MEDFileField1TSStructItem2 *> ps;
461           for(std::size_t i=0;i<sz;i++)
462             {
463               const MEDFileField1TSStructItem2& p(fst[i]);
464               if(p.getGeo()==*it)
465                 ps.push_back(&p);
466             }
467           if(ps.empty())
468             throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for cells 1 !");
469           if(ps.size()==1)
470             {
471               int nbi(ps[0]->getNbOfIntegrationPts(globs));
472               const DataArrayInt *otherP(ps[0]->getPfl(globs));
473               const std::pair<int,int>& strtStop(ps[0]->getStartStop());
474               MEDCouplingAutoRefCountObjectPtr<DataArray> ret(vals->selectByTupleId2(strtStop.first,strtStop.second,1));
475               if(!thisP && !otherP)
476                 {
477                   arrSafe[iii]=ret; arr[iii]=ret;
478                   continue;
479                 }
480               if(thisP && otherP)
481                 {
482                   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p1(otherP->invertArrayN2O2O2N(getNumberOfCells(ps[0]->getGeo())));
483                   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p2(thisP->deepCpy());
484                   p2->transformWithIndArr(p1->begin(),p1->end());
485                   //p1=p2->getIdsNotEqual(-1);
486                   //p1=p2->selectByTupleIdSafe(p1->begin(),p1->end());
487                   ret->rearrange(nbi*nc); ret=ret->selectByTupleIdSafe(p2->begin(),p2->end()); ret->rearrange(nc); ret->setInfoOnComponents(compInfo);
488                   arrSafe[iii]=ret; arr[iii]=ret;
489                   continue;
490                 }
491               if(!thisP && otherP)
492                 {
493                   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p1(otherP->deepCpy());
494                   p1->sort(true);
495                   p1->checkAllIdsInRange(0,getNumberOfCells(ps[0]->getGeo()));
496                   p1=DataArrayInt::FindPermutationFromFirstToSecond(otherP,p1);
497                   ret->rearrange(nbi*nc); ret->renumberInPlace(p1->begin()); ret->rearrange(nc); ret->setInfoOnComponents(compInfo);
498                   arrSafe[iii]=ret; arr[iii]=ret;
499                   continue;
500                 }
501               throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for cells 3 !");
502             }
503           else
504             {
505               std::vector< const DataArrayInt * >otherPS(ps.size());
506               std::vector< const DataArray * > arr2(ps.size());
507               std::vector< MEDCouplingAutoRefCountObjectPtr<DataArray> > arr2Safe(ps.size());
508               std::vector< const DataArrayInt * > nbis(ps.size());
509               std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> > nbisSafe(ps.size());
510               int jj(0);
511               for(std::vector<const MEDFileField1TSStructItem2 *>::const_iterator it2=ps.begin();it2!=ps.end();it2++,jj++)
512                 {
513                   int nbi((*it2)->getNbOfIntegrationPts(globs));
514                   const DataArrayInt *otherPfl((*it2)->getPfl(globs));
515                   const std::pair<int,int>& strtStop((*it2)->getStartStop());
516                   MEDCouplingAutoRefCountObjectPtr<DataArray> ret2(vals->selectByTupleId2(strtStop.first,strtStop.second,1));
517                   if(!otherPfl)
518                     throw INTERP_KERNEL::Exception("MEDMeshMultiLev::constructDataArray : unexpected situation for cells 4 !");
519                   arr2[jj]=ret2; arr2Safe[jj]=ret2; otherPS[jj]=otherPfl;
520                   nbisSafe[jj]=DataArrayInt::New(); nbisSafe[jj]->alloc(otherPfl->getNumberOfTuples(),1); nbisSafe[jj]->fillWithValue(nbi);
521                   nbis[jj]=nbisSafe[jj];
522                 }
523               MEDCouplingAutoRefCountObjectPtr<DataArray> arr3(DataArray::Aggregate(arr2));
524               MEDCouplingAutoRefCountObjectPtr<DataArrayInt> otherP(DataArrayInt::Aggregate(otherPS));
525               MEDCouplingAutoRefCountObjectPtr<DataArrayInt> zenbis(DataArrayInt::Aggregate(nbis));
526               MEDCouplingAutoRefCountObjectPtr<DataArrayInt> otherPN(otherP->invertArrayN2O2O2N(getNumberOfCells(*it)));
527               MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p1;
528               if(thisP)
529                 p1=DataArrayInt::FindPermutationFromFirstToSecond(otherP,thisP);
530               else
531                 p1=otherP->deepCpy();
532               MEDCouplingAutoRefCountObjectPtr<DataArrayInt> zenbisN(zenbis->renumber(p1->begin()));
533               zenbisN->computeOffsets2();
534               jj=0;
535               for(std::vector<const MEDFileField1TSStructItem2 *>::const_iterator it2=ps.begin();it2!=ps.end();it2++,jj++)
536                 {
537                   //int nbi((*it2)->getNbOfIntegrationPts(globs));
538                   const DataArrayInt *otherPfl((*it2)->getPfl(globs));
539                   const std::pair<int,int>& strtStop((*it2)->getStartStop());
540                   MEDCouplingAutoRefCountObjectPtr<DataArray> ret2(vals->selectByTupleId2(strtStop.first,strtStop.second,1));
541                   //
542                   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p2(otherPfl->deepCpy());
543                   p2->transformWithIndArr(otherPN->begin(),otherPN->end());
544                   p2->transformWithIndArr(p1->begin(),p1->end());
545                   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> idsN(p2->buildExplicitArrByRanges(zenbisN));
546                   arr3->setPartOfValuesBase3(ret2,idsN->begin(),idsN->end(),0,nc,1);
547                 }
548               arrSafe[iii]=arr3; arr[iii]=arr3;
549               continue;
550             }
551         }
552       return DataArray::Aggregate(arr);
553     }
554 }
555
556 /*!
557  * This method is called to add NORM_POINT1 cells in \a this so that orphan nodes in \a verticesToAdd will be fetched.
558  */
559 void MEDMeshMultiLev::appendVertices(const DataArrayInt *verticesToAdd, DataArrayInt *nr)
560 {
561   int nbOfVertices(verticesToAdd->getNumberOfTuples());
562   std::size_t sz(_pfls.size());
563   _pfls.resize(sz+1);
564   _geo_types.resize(sz+1,INTERP_KERNEL::NORM_POINT1);
565   _nb_entities.resize(sz+1,nbOfVertices);
566   _node_reduction=nr; nr->incrRef();
567   _nb_nodes+=nbOfVertices;
568   const DataArrayInt *cf(_cell_fam_ids),*cn(_cell_num_ids),*nf(_node_fam_ids),*nn(_node_num_ids);
569   if(cf)
570     {
571       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp;
572       std::vector<const DataArrayInt *> a(2);
573       a[0]=cf;
574       if(nf)
575         tmp=nf->selectByTupleIdSafe(verticesToAdd->begin(),verticesToAdd->end());
576       else
577         {
578           tmp=DataArrayInt::New(); tmp->alloc(nbOfVertices,1); tmp->fillWithZero();
579         }
580       a[1]=tmp;
581       _cell_fam_ids=DataArrayInt::Aggregate(a);
582       _cell_fam_ids_nocpy=false;
583     }
584   if(cn)
585     {
586       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp;
587       std::vector<const DataArrayInt *> a(2);
588       a[0]=cn;
589       if(nn)
590         tmp=nn->selectByTupleIdSafe(verticesToAdd->begin(),verticesToAdd->end());
591       else
592         {
593           tmp=DataArrayInt::New(); tmp->alloc(nbOfVertices,1); tmp->fillWithZero();
594         }
595       a[1]=tmp;
596       _cell_num_ids=DataArrayInt::Aggregate(a);
597       _cell_num_ids_nocpy=false;
598     }
599 }
600
601 MEDMeshMultiLev::MEDMeshMultiLev():_nb_nodes(0),_cell_fam_ids_nocpy(false)
602 {
603 }
604
605 MEDMeshMultiLev::MEDMeshMultiLev(int nbNodes, const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities):_geo_types(gts),_nb_entities(nbEntities),_nb_nodes(nbNodes),_cell_fam_ids_nocpy(false),_cell_num_ids_nocpy(false),_node_fam_ids_nocpy(false),_node_num_ids_nocpy(false)
606 {
607   std::size_t sz(_geo_types.size());
608   if(sz!=pfls.size() || sz!=nbEntities.size())
609     throw INTERP_KERNEL::Exception("MEDMeshMultiLev::MEDMeshMultiLev : input vector must have the same size !");
610   _pfls.resize(sz);
611   for(std::size_t i=0;i<sz;i++)
612     {
613       if(pfls[i])
614         pfls[i]->incrRef();
615       _pfls[i]=const_cast<DataArrayInt *>(pfls[i]);
616     }
617 }
618
619 MEDMeshMultiLev::MEDMeshMultiLev(const MEDMeshMultiLev& other):RefCountObject(other),_pfls(other._pfls),_geo_types(other._geo_types),_nb_entities(other._nb_entities),_node_reduction(other._node_reduction),_nb_nodes(other._nb_nodes),_cell_fam_ids(other._cell_fam_ids),_cell_fam_ids_nocpy(other._cell_fam_ids_nocpy),_cell_num_ids(other._cell_num_ids),_cell_num_ids_nocpy(other._cell_num_ids_nocpy),_node_fam_ids(other._node_fam_ids),_node_fam_ids_nocpy(other._node_fam_ids_nocpy),_node_num_ids(other._node_num_ids),_node_num_ids_nocpy(other._node_num_ids_nocpy)
620 {
621 }
622
623 //=
624
625 MEDUMeshMultiLev *MEDUMeshMultiLev::New(const MEDFileUMesh *m, const std::vector<int>& levs)
626 {
627   return new MEDUMeshMultiLev(m,levs);
628 }
629
630 MEDUMeshMultiLev::MEDUMeshMultiLev(const MEDFileUMesh *m, const std::vector<int>& levs)
631 {
632   if(!m)
633     throw INTERP_KERNEL::Exception("MEDUMeshMultiLev constructor : null input pointer !");
634   std::vector<MEDCoupling1GTUMesh *> v;
635   for(std::vector<int>::const_iterator it=levs.begin();it!=levs.end();it++)
636     {
637       std::vector<MEDCoupling1GTUMesh *> vTmp(m->getDirectUndergroundSingleGeoTypeMeshes(*it));
638       v.insert(v.end(),vTmp.begin(),vTmp.end());
639     }
640   std::size_t sz(v.size());
641   if(v.empty())
642     {
643       _coords=m->getCoords(); _coords->incrRef();
644     }
645   _parts.resize(sz);
646   _pfls.resize(sz);
647   _geo_types.resize(sz);
648   _nb_entities.resize(sz);
649   for(std::size_t i=0;i<sz;i++)
650     {
651       MEDCoupling1GTUMesh *obj(v[i]);
652       if(obj)
653         obj->incrRef();
654       else
655         throw INTERP_KERNEL::Exception("MEDUMeshMultiLev constructor : presence of a null pointer !");
656       _parts[i]=obj;
657       _geo_types[i]=obj->getCellModelEnum();
658       _nb_entities[i]=obj->getNumberOfCells();
659     }
660   // ids fields management
661   _cell_fam_ids_nocpy=(levs.size()==1);
662   if(_cell_fam_ids_nocpy)
663     {
664       const DataArrayInt *tmp(m->getFamilyFieldAtLevel(levs[0]));
665       if(tmp)
666         {
667           tmp->incrRef();
668           _cell_fam_ids=(const_cast<DataArrayInt *>(tmp));
669         }
670     }
671   else
672     {
673       std::vector<const DataArrayInt *> tmps(levs.size());
674       bool f(true);
675       for(std::size_t i=0;i<levs.size();i++)
676         {
677           tmps[i]=m->getFamilyFieldAtLevel(levs[i]);
678           if(!tmps[i])
679             f=false;
680         }
681       if(f && !tmps.empty())
682         _cell_fam_ids=DataArrayInt::Aggregate(tmps);
683     }
684   _cell_num_ids_nocpy=(levs.size()==1);
685   if(_cell_num_ids_nocpy)
686     {
687       const DataArrayInt *tmp(m->getNumberFieldAtLevel(levs[0]));
688       if(tmp)
689         {
690           tmp->incrRef();
691           _cell_num_ids=(const_cast<DataArrayInt *>(tmp));
692         }
693     }
694   else
695     {
696       std::vector<const DataArrayInt *> tmps(levs.size());
697       bool n(true);
698       for(std::size_t i=0;i<levs.size();i++)
699         {
700           tmps[i]=m->getNumberFieldAtLevel(levs[i]);
701           if(!tmps[i])
702             n=false;
703         }
704       if(n && !tmps.empty())
705         _cell_num_ids=DataArrayInt::Aggregate(tmps);
706     }
707   // node part
708   _node_fam_ids_nocpy=true;
709   {
710     const DataArrayInt *tmp(m->getFamilyFieldAtLevel(1));
711     if(tmp)
712       {
713         tmp->incrRef();
714         _node_fam_ids=(const_cast<DataArrayInt *>(tmp));
715       }
716   }
717   _node_num_ids_nocpy=true;
718   {
719     const DataArrayInt *tmp(m->getNumberFieldAtLevel(1));
720     if(tmp)
721       {
722         tmp->incrRef();
723         _node_num_ids=(const_cast<DataArrayInt *>(tmp));
724       }
725   }
726 }
727
728 MEDUMeshMultiLev *MEDUMeshMultiLev::New(const MEDFileUMesh *m, const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities)
729 {
730   return new MEDUMeshMultiLev(m,gts,pfls,nbEntities);
731 }
732
733 MEDUMeshMultiLev::MEDUMeshMultiLev(const MEDFileUMesh *m, const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities):MEDMeshMultiLev(m->getNumberOfNodes(),gts,pfls,nbEntities)
734 {
735   std::size_t sz(gts.size());
736   if(sz<1)
737     throw INTERP_KERNEL::Exception("constructor of MEDUMeshMultiLev : number of different geo type must be >= 1 !");
738   unsigned dim(INTERP_KERNEL::CellModel::GetCellModel(gts[0]).getDimension());
739   _parts.resize(sz);
740   bool isSameDim(true),isNoPfl(true);
741   for(std::size_t i=0;i<sz;i++)
742     {
743       MEDCoupling1GTUMesh *elt(m->getDirectUndergroundSingleGeoTypeMesh(gts[i]));
744       if(INTERP_KERNEL::CellModel::GetCellModel(gts[i]).getDimension()!=dim)
745         isSameDim=false;
746       if(pfls[i])
747         isNoPfl=false;
748       if(elt)
749         elt->incrRef();
750       _parts[i]=elt;
751     }
752   // ids fields management
753   int lev((int)dim-m->getMeshDimension());
754   if(isSameDim && isNoPfl && m->getGeoTypesAtLevel(lev)==gts)//optimized part
755     {
756       _cell_fam_ids_nocpy=true;
757       const DataArrayInt *famIds(m->getFamilyFieldAtLevel(lev));
758       if(famIds)
759         { _cell_fam_ids=const_cast<DataArrayInt*>(famIds); famIds->incrRef(); }
760       _cell_num_ids_nocpy=true;
761       const DataArrayInt *numIds(m->getNumberFieldAtLevel(lev));
762       if(numIds)
763         { _cell_num_ids=const_cast<DataArrayInt*>(numIds); numIds->incrRef(); }
764       _node_fam_ids_nocpy=true;
765       famIds=m->getFamilyFieldAtLevel(1);
766       if(famIds)
767         { _node_fam_ids=const_cast<DataArrayInt*>(famIds); famIds->incrRef(); }
768       _node_num_ids_nocpy=true;
769       numIds=m->getNumberFieldAtLevel(1);
770       if(numIds)
771         { _node_num_ids=const_cast<DataArrayInt*>(numIds); numIds->incrRef(); }
772       return ;
773     }
774   //
775   _cell_fam_ids_nocpy=false;
776   std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> > famIdsSafe(sz);
777   std::vector<const DataArrayInt *> famIds(sz);
778   bool f(true);
779   for(std::size_t i=0;i<sz;i++)
780     {
781       famIdsSafe[i]=m->extractFamilyFieldOnGeoType(gts[i]);
782       famIds[i]=famIdsSafe[i];
783       if(!famIds[i])
784         f=false;
785     }
786   if(f)
787     _cell_fam_ids=DataArrayInt::Aggregate(famIds);
788   _cell_num_ids_nocpy=false;
789   std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> > numIdsSafe(sz);
790   std::vector<const DataArrayInt *> numIds(sz);
791   bool n(true);
792   for(std::size_t i=0;i<sz;i++)
793     {
794       numIdsSafe[i]=m->extractNumberFieldOnGeoType(gts[i]);
795       numIds[i]=numIdsSafe[i];
796       if(!numIds[i])
797         n=false;
798     }
799   if(n)
800     _cell_num_ids=DataArrayInt::Aggregate(numIds);
801   // node ids management
802   _node_fam_ids_nocpy=true;
803   const DataArrayInt *nodeFamIds(m->getFamilyFieldAtLevel(1));
804   if(nodeFamIds)
805     { _node_fam_ids=const_cast<DataArrayInt*>(nodeFamIds); nodeFamIds->incrRef(); }
806   _node_num_ids_nocpy=true;
807   const DataArrayInt *nodeNumIds(m->getNumberFieldAtLevel(1));
808   if(nodeNumIds)
809     { _node_num_ids=const_cast<DataArrayInt*>(nodeNumIds); nodeNumIds->incrRef(); }
810 }
811
812 void MEDUMeshMultiLev::selectPartOfNodes(const DataArrayInt *pflNodes)
813 {
814    if(!pflNodes || !pflNodes->isAllocated())
815      return ;
816    std::size_t sz(_parts.size());
817    std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayInt> > a(sz);
818    std::vector< const DataArrayInt *> aa(sz);
819    for(std::size_t i=0;i<sz;i++)
820      {
821        const DataArrayInt *pfl(_pfls[i]);
822        MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> m(_parts[i]);
823        if(pfl)
824          m=dynamic_cast<MEDCoupling1GTUMesh *>(_parts[i]->buildPartOfMySelfKeepCoords(pfl->begin(),pfl->end()));
825        DataArrayInt *cellIds=0;
826        m->fillCellIdsToKeepFromNodeIds(pflNodes->begin(),pflNodes->end(),true,cellIds);
827        MEDCouplingAutoRefCountObjectPtr<DataArrayInt> cellIdsSafe(cellIds);
828        MEDCouplingAutoRefCountObjectPtr<MEDCouplingPointSet> m2(m->buildPartOfMySelfKeepCoords(cellIds->begin(),cellIds->end()));
829        int tmp=-1;
830        MEDCouplingAutoRefCountObjectPtr<DataArrayInt> o2n(m2->getNodeIdsInUse(tmp));
831        a[i]=o2n->invertArrayO2N2N2O(tmp); aa[i]=a[i];
832        if(pfl)
833          _pfls[i]=pfl->selectByTupleIdSafe(cellIds->begin(),cellIds->end());
834        else
835          _pfls[i]=cellIdsSafe;
836      }
837    if(!aa.empty())
838      _node_reduction=DataArrayInt::Aggregate(aa);//general case
839    else
840      _node_reduction=pflNodes->deepCpy();//case where no cells in read mesh.
841    _node_reduction->sort(true);
842    _node_reduction=_node_reduction->buildUnique();
843    if(_node_reduction->getNumberOfTuples()==pflNodes->getNumberOfTuples())
844      return ;//This is the classical case where the input node profile corresponds perfectly to a subset of cells in _parts
845    if(_node_reduction->getNumberOfTuples()>pflNodes->getNumberOfTuples())
846      throw INTERP_KERNEL::Exception("MEDUMeshMultiLev::selectPartOfNodes : internal error in MEDCoupling during cell select from a list of nodes !");
847    // Here the cells available in _parts is not enough to cover all the nodes in pflNodes. So adding vertices cells in _parts...
848    MEDCouplingAutoRefCountObjectPtr<DataArrayInt> pflNodes2(pflNodes->deepCpy());
849    pflNodes2->sort(true);
850    MEDCouplingAutoRefCountObjectPtr<DataArrayInt> diff(pflNodes2->buildSubstractionOptimized(_node_reduction));
851    appendVertices(diff,pflNodes2);
852 }
853
854 MEDMeshMultiLev *MEDUMeshMultiLev::prepare() const
855 {
856   return new MEDUMeshMultiLev(*this);
857 }
858
859 MEDUMeshMultiLev::MEDUMeshMultiLev(const MEDUMeshMultiLev& other):MEDMeshMultiLev(other),_parts(other._parts),_coords(other._coords)
860 {
861 }
862
863 MEDUMeshMultiLev::MEDUMeshMultiLev(const MEDStructuredMeshMultiLev& other, const MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh>& part):MEDMeshMultiLev(other)
864 {
865   _parts.resize(1);
866   _parts[0]=part;
867   _geo_types.resize(1); _geo_types[0]=part->getCellModelEnum();
868   _nb_entities.resize(1); _nb_entities[0]=part->getNumberOfCells();
869   _pfls.resize(1); _pfls[0]=0;
870 }
871
872 /*! 
873  * If returned value is false output pointer \a coords is not the internal pointer. If returned value is true output pointer \a coords is directly the internal pointer.
874  * If true is returned, the \a coords output parameter should be used with care (non const method call) to avoid to change the internal state of MEDFileUMesh instance.
875  */
876 bool MEDUMeshMultiLev::buildVTUArrays(DataArrayDouble *& coords, DataArrayByte *&types, DataArrayInt *&cellLocations, DataArrayInt *& cells, DataArrayInt *&faceLocations, DataArrayInt *&faces) const
877 {
878   const DataArrayDouble *tmp(0);
879   if(_parts.empty())
880     tmp=_coords;
881   else
882     tmp=_parts[0]->getCoords();
883   if(!tmp)
884     throw INTERP_KERNEL::Exception("MEDUMeshMultiLev::getVTUArrays : the coordinates are null !");
885   MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> a(const_cast<DataArrayDouble *>(tmp)); tmp->incrRef();
886   int szBCE(0),szD(0),szF(0);
887   bool isPolyh(false);
888   int iii(0);
889   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::const_iterator it=_parts.begin();it!=_parts.end();it++,iii++)
890     {
891       const MEDCoupling1GTUMesh *cur(*it);
892       if(!cur)
893         throw INTERP_KERNEL::Exception("MEDUMeshMultiLev::getVTUArrays : a part is null !");
894       //
895       const DataArrayInt *pfl(_pfls[iii]);
896       MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> cur2;
897       if(!pfl)
898         { cur2=const_cast<MEDCoupling1GTUMesh *>(cur); cur2->incrRef(); }
899       else
900         { cur2=dynamic_cast<MEDCoupling1GTUMesh *>(cur->buildPartOfMySelfKeepCoords(pfl->begin(),pfl->end())); cur=cur2; }
901       //
902       int curNbCells(cur->getNumberOfCells());
903       szBCE+=curNbCells;
904       if((*it)->getCellModelEnum()!=INTERP_KERNEL::NORM_POLYHED)
905         szD+=cur->getNodalConnectivity()->getNumberOfTuples()+curNbCells;
906       else
907         {
908           isPolyh=true;
909           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp2(cur->computeEffectiveNbOfNodesPerCell());
910           szD+=tmp2->accumulate(0)+curNbCells;
911           szF+=2*curNbCells+cur->getNodalConnectivity()->getNumberOfTuples();
912         }
913     }
914   MEDCouplingAutoRefCountObjectPtr<DataArrayByte> b(DataArrayByte::New()); b->alloc(szBCE,1); char *bPtr(b->getPointer());
915   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> c(DataArrayInt::New()); c->alloc(szBCE,1); int *cPtr(c->getPointer());
916   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> d(DataArrayInt::New()); d->alloc(szD,1); int *dPtr(d->getPointer());
917   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> e(DataArrayInt::New()),f(DataArrayInt::New()); int *ePtr(0),*fPtr(0);
918   if(isPolyh)
919     { e->alloc(szBCE,1); ePtr=e->getPointer(); f->alloc(szF,1); fPtr=f->getPointer(); }
920   int k(0);
921   iii=0;
922   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> >::const_iterator it=_parts.begin();it!=_parts.end();it++,iii++)
923     {
924       const MEDCoupling1GTUMesh *cur(*it);
925       //
926       const DataArrayInt *pfl(_pfls[iii]);
927       MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> cur2;
928       if(!pfl)
929         { cur2=const_cast<MEDCoupling1GTUMesh *>(cur); cur2->incrRef(); }
930       else
931         { cur2=dynamic_cast<MEDCoupling1GTUMesh *>(cur->buildPartOfMySelfKeepCoords(pfl->begin(),pfl->end())); cur=cur2; }
932       //
933       int curNbCells(cur->getNumberOfCells());
934       int gt((int)cur->getCellModelEnum());
935       if(gt<0 || gt>=PARAMEDMEM_2_VTKTYPE_LGTH)
936         throw INTERP_KERNEL::Exception("MEDUMeshMultiLev::getVTUArrays : invalid geometric type !");
937       unsigned char gtvtk(PARAMEDMEM_2_VTKTYPE[gt]);
938       if(gtvtk==255)
939         throw INTERP_KERNEL::Exception("MEDUMeshMultiLev::getVTUArrays : no VTK type for the requested INTERP_KERNEL geometric type !");
940       std::fill(bPtr,bPtr+curNbCells,gtvtk); bPtr+=curNbCells;
941       const MEDCoupling1SGTUMesh *scur(dynamic_cast<const MEDCoupling1SGTUMesh *>(cur));
942       const MEDCoupling1DGTUMesh *dcur(dynamic_cast<const MEDCoupling1DGTUMesh *>(cur));
943       const int *connPtr(cur->getNodalConnectivity()->begin());
944       if(!scur && !dcur)
945         throw INTERP_KERNEL::Exception("MEDUMeshMultiLev::getVTUArrays : internal error !");
946       if(scur)
947         {
948           int nnpc(scur->getNumberOfNodesPerCell());
949           for(int i=0;i<curNbCells;i++,connPtr+=nnpc)
950             {
951               *dPtr++=nnpc;
952               dPtr=std::copy(connPtr,connPtr+nnpc,dPtr);
953               *cPtr++=k; k+=nnpc+1;
954             }
955           if(isPolyh)
956             { std::fill(ePtr,ePtr+curNbCells,-1); ePtr+=curNbCells; }
957         }
958       else
959         {
960           const int *connIPtr(dcur->getNodalConnectivityIndex()->begin());
961           if(cur->getCellModelEnum()!=INTERP_KERNEL::NORM_POLYHED)
962             {
963               for(int i=0;i<curNbCells;i++,connIPtr++)
964                 {
965                   *dPtr++=connIPtr[1]-connIPtr[0];
966                   dPtr=std::copy(connPtr+connIPtr[0],connPtr+connIPtr[1],dPtr);
967                   *cPtr++=k; k+=connIPtr[1]-connIPtr[0];
968                 }
969             }
970           else
971             {
972               for(int i=0;i<curNbCells;i++,connIPtr++)
973                 {
974                   std::set<int> s(connPtr+connIPtr[0],connPtr+connIPtr[1]); s.erase(-1);
975                   *dPtr++=(int)s.size();
976                   dPtr=std::copy(s.begin(),s.end(),dPtr);
977                   *cPtr++=k; k+=(int)s.size()+1;
978                 }
979             }
980           if(isPolyh)
981             {
982               connIPtr=dcur->getNodalConnectivityIndex()->begin();
983               if(cur->getCellModelEnum()!=INTERP_KERNEL::NORM_POLYHED)
984                 { std::fill(ePtr,ePtr+curNbCells,-1); ePtr+=curNbCells; }
985               else
986                 {
987                   int kk(0);
988                   for(int i=0;i<curNbCells;i++,connIPtr++)
989                     {
990                       int nbFace(std::count(connPtr+connIPtr[0],connPtr+connIPtr[1],-1)+1);
991                       *fPtr++=nbFace;
992                       const int *work(connPtr+connIPtr[0]);
993                       for(int j=0;j<nbFace;j++)
994                         {
995                           const int *work2=std::find(work,connPtr+connIPtr[1],-1);
996                           *fPtr++=std::distance(work,work2);
997                           fPtr=std::copy(work,work2,fPtr);
998                           work=work2+1;
999                         }
1000                       *ePtr++=kk; kk+=connIPtr[1]-connIPtr[0]+2;
1001                     }
1002                 }
1003             }
1004         }
1005     }
1006   if(!isPolyh)
1007     reorderNodesIfNecessary(a,d,0);
1008   else
1009     reorderNodesIfNecessary(a,d,f);
1010   if(a->getNumberOfComponents()!=3)
1011     a=a->changeNbOfComponents(3,0.);
1012   coords=a.retn(); types=b.retn(); cellLocations=c.retn(); cells=d.retn();
1013   if(!isPolyh)
1014     { faceLocations=0; faces=0; }
1015   else
1016     { faceLocations=e.retn(); faces=f.retn(); }
1017   return tmp==((DataArrayDouble *)a);
1018 }
1019
1020 void MEDUMeshMultiLev::reorderNodesIfNecessary(MEDCouplingAutoRefCountObjectPtr<DataArrayDouble>& coords, DataArrayInt *nodalConnVTK, DataArrayInt *polyhedNodalConnVTK) const
1021 {
1022   const DataArrayInt *nr(_node_reduction);
1023   if(!nr)
1024     return ;
1025   if(nodalConnVTK->empty() && !polyhedNodalConnVTK)
1026     {
1027       coords=(coords->selectByTupleIdSafe(nr->begin(),nr->end()));
1028       return ;
1029     }
1030   int sz(coords->getNumberOfTuples());
1031   std::vector<bool> b(sz,false);
1032   const int *work(nodalConnVTK->begin()),*endW(nodalConnVTK->end());
1033   while(work!=endW)
1034     {
1035       int nb(*work++);
1036       for(int i=0;i<nb && work!=endW;i++,work++)
1037         {
1038           if(*work>=0 && *work<sz)
1039             b[*work]=true;
1040           else
1041             throw INTERP_KERNEL::Exception("MEDUMeshMultiLev::reorderNodesIfNecessary : internal error !");
1042         }
1043     }
1044   if(polyhedNodalConnVTK)
1045     {
1046       work=polyhedNodalConnVTK->begin(); endW=polyhedNodalConnVTK->end();
1047       while(work!=endW)
1048         {
1049           int nb(*work++);
1050           for(int i=0;i<nb && work!=endW;i++)
1051             {
1052               int nb2(*work++);
1053               for(int j=0;j<nb2 && work!=endW;j++,work++)
1054                 {
1055                   if(*work>=0 && *work<sz)
1056                     b[*work]=true;
1057                   else
1058                     throw INTERP_KERNEL::Exception("MEDUMeshMultiLev::reorderNodesIfNecessary : internal error #2 !");
1059                 }
1060             }
1061         }
1062     }
1063   int szExp(std::count(b.begin(),b.end(),true));
1064   if(szExp!=nr->getNumberOfTuples())
1065     throw INTERP_KERNEL::Exception("MEDUMeshMultiLev::reorderNodesIfNecessary : internal error #3 !");
1066   // Go renumbering !
1067   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> o2n(DataArrayInt::New()); o2n->alloc(sz,1);
1068   int *o2nPtr(o2n->getPointer());
1069   int newId(0);
1070   for(int i=0;i<sz;i++,o2nPtr++)
1071     if(b[i]) *o2nPtr=newId++; else *o2nPtr=-1;
1072   const int *o2nPtrc(o2n->begin());
1073   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> n2o(o2n->invertArrayO2N2N2O(nr->getNumberOfTuples()));
1074   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> perm(DataArrayInt::FindPermutationFromFirstToSecond(n2o,nr));
1075   const int *permPtr(perm->begin());
1076   int *work2(nodalConnVTK->getPointer()),*endW2(nodalConnVTK->getPointer()+nodalConnVTK->getNumberOfTuples());
1077   while(work2!=endW2)
1078     {
1079       int nb(*work2++);
1080       for(int i=0;i<nb && work2!=endW2;i++,work2++)
1081         *work2=permPtr[o2nPtrc[*work2]];
1082     }
1083   if(polyhedNodalConnVTK)
1084     {
1085       work2=polyhedNodalConnVTK->getPointer(); endW2=polyhedNodalConnVTK->getPointer()+polyhedNodalConnVTK->getNumberOfTuples();
1086       while(work2!=endW2)
1087         {
1088           int nb(*work2++);
1089           for(int i=0;i<nb && work2!=endW2;i++)
1090             {
1091               int nb2(*work2++);
1092               for(int j=0;j<nb2 && work2!=endW2;j++,work2++)
1093                 *work2=permPtr[o2nPtrc[*work2]];
1094             }
1095         }
1096     }
1097   coords=(coords->selectByTupleIdSafe(nr->begin(),nr->end()));
1098 }
1099
1100
1101 void MEDUMeshMultiLev::appendVertices(const DataArrayInt *verticesToAdd, DataArrayInt *nr)
1102 {
1103   int nbOfCells(verticesToAdd->getNumberOfTuples());//it is not a bug cells are NORM_POINT1
1104   MEDMeshMultiLev::appendVertices(verticesToAdd,nr);
1105   MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> elt(MEDCoupling1SGTUMesh::New("",INTERP_KERNEL::NORM_POINT1));
1106   elt->allocateCells(nbOfCells);
1107   for(int i=0;i<nbOfCells;i++)
1108     {
1109       int pt(verticesToAdd->getIJ(i,0));
1110       elt->insertNextCell(&pt,&pt+1);
1111     }
1112   if(_parts.empty())
1113     throw INTERP_KERNEL::Exception("MEDUMeshMultiLev::appendVertices : parts are empty !");
1114   elt->setCoords(_parts[0]->getCoords());
1115   MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> elt2((MEDCoupling1SGTUMesh *)elt); elt2->incrRef();
1116   _parts.push_back(elt2);
1117 }
1118
1119 //=
1120
1121 MEDStructuredMeshMultiLev::MEDStructuredMeshMultiLev():_is_internal(true)
1122 {
1123 }
1124
1125 MEDStructuredMeshMultiLev::MEDStructuredMeshMultiLev(const MEDFileStructuredMesh *m, const std::vector<int>& lev):_is_internal(true)
1126 {
1127   // ids fields management
1128   _cell_fam_ids_nocpy=true; _cell_num_ids_nocpy=true;
1129   const DataArrayInt *tmp(0);
1130   tmp=m->getFamilyFieldAtLevel(0);
1131   if(tmp)
1132     {
1133       tmp->incrRef();
1134       _cell_fam_ids=const_cast<DataArrayInt *>(tmp);
1135     }
1136   tmp=m->getNumberFieldAtLevel(0);
1137   if(tmp)
1138     {
1139       tmp->incrRef();
1140       _cell_num_ids=const_cast<DataArrayInt *>(tmp);
1141     }
1142   //
1143   _node_fam_ids_nocpy=true; _node_num_ids_nocpy=true;
1144   tmp=0;
1145   tmp=m->getFamilyFieldAtLevel(1);
1146   if(tmp)
1147     {
1148       tmp->incrRef();
1149       _node_fam_ids=const_cast<DataArrayInt *>(tmp);
1150     }
1151   tmp=m->getNumberFieldAtLevel(1);
1152   if(tmp)
1153     {
1154       tmp->incrRef();
1155       _node_num_ids=const_cast<DataArrayInt *>(tmp);
1156     }
1157 }
1158
1159 MEDStructuredMeshMultiLev::MEDStructuredMeshMultiLev(const MEDFileStructuredMesh *m, int nbOfNodes, const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities):MEDMeshMultiLev(nbOfNodes,gts,pfls,nbEntities),_is_internal(true)
1160 {
1161   // ids fields management
1162   _cell_fam_ids_nocpy=true; _cell_num_ids_nocpy=true;
1163   const DataArrayInt *tmp(0);
1164   tmp=m->getFamilyFieldAtLevel(0);
1165   if(tmp)
1166     {
1167       tmp->incrRef();
1168       _cell_fam_ids=const_cast<DataArrayInt *>(tmp);
1169     }
1170   tmp=m->getNumberFieldAtLevel(0);
1171   if(tmp)
1172     {
1173       tmp->incrRef();
1174       _cell_num_ids=const_cast<DataArrayInt *>(tmp);
1175     }
1176   //
1177   _node_fam_ids_nocpy=true; _node_num_ids_nocpy=true;
1178   tmp=0;
1179   tmp=m->getFamilyFieldAtLevel(1);
1180   if(tmp)
1181     {
1182       tmp->incrRef();
1183       _node_fam_ids=const_cast<DataArrayInt *>(tmp);
1184     }
1185   tmp=m->getNumberFieldAtLevel(1);
1186   if(tmp)
1187     {
1188       tmp->incrRef();
1189       _node_num_ids=const_cast<DataArrayInt *>(tmp);
1190     }
1191 }
1192
1193 MEDStructuredMeshMultiLev::MEDStructuredMeshMultiLev(const MEDStructuredMeshMultiLev& other):MEDMeshMultiLev(other),_is_internal(true)
1194 {
1195 }
1196
1197 void MEDStructuredMeshMultiLev::selectPartOfNodes(const DataArrayInt *pflNodes)
1198 {
1199   if(!pflNodes || !pflNodes->isAllocated())
1200     return ;
1201   std::vector<int> ngs(getNodeGridStructure());
1202   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> conn(MEDCouplingStructuredMesh::Build1GTNodalConnectivity(&ngs[0],&ngs[0]+ngs.size()));
1203   MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> m(MEDCoupling1SGTUMesh::New("",MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension(ngs.size())));
1204   m->setNodalConnectivity(conn);
1205   const DataArrayInt *pfl(_pfls[0]);
1206   if(pfl)
1207     {
1208       m=dynamic_cast<MEDCoupling1SGTUMesh *>(m->buildPartOfMySelfKeepCoords(pfl->begin(),pfl->end()));
1209     }
1210   DataArrayInt *cellIds=0;
1211   m->fillCellIdsToKeepFromNodeIds(pflNodes->begin(),pflNodes->end(),true,cellIds);
1212   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> cellIdsSafe(cellIds);
1213   MEDCouplingAutoRefCountObjectPtr<MEDCouplingPointSet> m2(m->buildPartOfMySelfKeepCoords(cellIds->begin(),cellIds->end()));
1214   int tmp=-1;
1215   _node_reduction=m2->getNodeIdsInUse(tmp);
1216   if(pfl)
1217     _pfls[0]=pfl->selectByTupleIdSafe(cellIds->begin(),cellIds->end());
1218   else
1219     _pfls[0]=cellIdsSafe;
1220 }
1221
1222 //=
1223
1224 MEDCMeshMultiLev *MEDCMeshMultiLev::New(const MEDFileCMesh *m, const std::vector<int>& levs)
1225 {
1226   return new MEDCMeshMultiLev(m,levs);
1227 }
1228
1229 MEDCMeshMultiLev *MEDCMeshMultiLev::New(const MEDFileCMesh *m, const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities)
1230 {
1231   return new MEDCMeshMultiLev(m,gts,pfls,nbEntities);
1232 }
1233
1234 MEDCMeshMultiLev::MEDCMeshMultiLev(const MEDFileCMesh *m, const std::vector<int>& levs):MEDStructuredMeshMultiLev(m,levs)
1235 {
1236   if(!m)
1237     throw INTERP_KERNEL::Exception("MEDCMeshMultiLev constructor : null input pointer !");
1238   if(levs.size()!=1 || levs[0]!=0)
1239     throw INTERP_KERNEL::Exception("MEDCMeshMultiLev constructor : levels supported is 0 only !");
1240   int sdim(m->getSpaceDimension());
1241   _coords.resize(sdim);
1242   for(int i=0;i<sdim;i++)
1243     {
1244       DataArrayDouble *elt(const_cast<DataArrayDouble *>(m->getMesh()->getCoordsAt(i)));
1245       if(!elt)
1246         throw INTERP_KERNEL::Exception("MEDCMeshMultiLev constructor 2 : presence of null pointer for an vector of double along an axis !");
1247       elt->incrRef();
1248       _coords[i]=elt;
1249     }
1250 }
1251
1252 MEDCMeshMultiLev::MEDCMeshMultiLev(const MEDFileCMesh *m, const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities):MEDStructuredMeshMultiLev(m,m->getNumberOfNodes(),gts,pfls,nbEntities)
1253 {
1254   if(!m)
1255     throw INTERP_KERNEL::Exception("MEDCMeshMultiLev constructor 2 : null input pointer !");
1256   if(gts.size()!=1 || pfls.size()!=1)
1257     throw INTERP_KERNEL::Exception("MEDCMeshMultiLev constructor 2 : lengthes of gts and pfls must be equal to one !");
1258   int mdim(m->getMeshDimension());
1259   INTERP_KERNEL::NormalizedCellType gt(MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension(mdim));
1260   if(gt!=gts[0])
1261     throw INTERP_KERNEL::Exception("MEDCMeshMultiLev constructor 2 : the unique geo type is invalid regarding meshdim !");
1262   _coords.resize(mdim);
1263   for(int i=0;i<mdim;i++)
1264     {
1265       DataArrayDouble *elt(const_cast<DataArrayDouble *>(m->getMesh()->getCoordsAt(i)));
1266       if(!elt)
1267         throw INTERP_KERNEL::Exception("MEDCMeshMultiLev constructor 2 : presence of null pointer for an vector of double along an axis !");
1268       _coords[i]=elt; _coords[i]->incrRef();
1269     }
1270 }
1271
1272 MEDCMeshMultiLev::MEDCMeshMultiLev(const MEDCMeshMultiLev& other):MEDStructuredMeshMultiLev(other),_coords(other._coords)
1273 {
1274 }
1275
1276 std::vector<int> MEDCMeshMultiLev::getNodeGridStructure() const
1277 {
1278   std::vector<int> ret(_coords.size());
1279   for(std::size_t i=0;i<_coords.size();i++)
1280     ret[i]=_coords[i]->getNumberOfTuples();
1281   return ret;
1282 }
1283
1284 MEDMeshMultiLev *MEDCMeshMultiLev::prepare() const
1285 {
1286   const DataArrayInt *pfl(0),*nr(_node_reduction);
1287   if(!_pfls.empty())
1288     pfl=_pfls[0];
1289   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> nnr;
1290   std::vector<int> cgs,ngs(getNodeGridStructure());
1291   cgs.resize(ngs.size());
1292   std::transform(ngs.begin(),ngs.end(),cgs.begin(),std::bind2nd(std::plus<int>(),-1));
1293   if(pfl)
1294     {
1295       std::vector< std::pair<int,int> > cellParts;
1296       MEDCouplingAutoRefCountObjectPtr<MEDMeshMultiLev> ret2;
1297       if(MEDCouplingStructuredMesh::IsPartStructured(pfl->begin(),pfl->end(),cgs,cellParts))
1298         {
1299           MEDCouplingAutoRefCountObjectPtr<MEDCMeshMultiLev> ret(new MEDCMeshMultiLev(*this));
1300           ret->_is_internal=false;
1301           if(nr)
1302             { nnr=nr->deepCpy(); nnr->sort(true); ret->setNodeReduction(nnr); }
1303           ret->_nb_entities[0]=pfl->getNumberOfTuples();
1304           ret->_pfls[0]=0;
1305           std::vector< MEDCouplingAutoRefCountObjectPtr<DataArrayDouble> > coords(_coords.size());
1306           for(std::size_t i=0;i<_coords.size();i++)
1307             coords[i]=_coords[i]->selectByTupleId2(cellParts[i].first,cellParts[i].second+1,1);
1308           ret->_coords=coords;
1309           ret2=(MEDCMeshMultiLev *)ret; ret2->incrRef();
1310         }
1311       else
1312         {
1313           MEDCouplingAutoRefCountObjectPtr<MEDCouplingCMesh> m(MEDCouplingCMesh::New());
1314           for(std::size_t i=0;i<ngs.size();i++)
1315             m->setCoordsAt(i,_coords[i]);
1316           MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> m2(m->build1SGTUnstructured());
1317           MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> m3=dynamic_cast<MEDCoupling1GTUMesh *>(m2->buildPartOfMySelfKeepCoords(pfl->begin(),pfl->end()));
1318           MEDCouplingAutoRefCountObjectPtr<MEDUMeshMultiLev> ret(new MEDUMeshMultiLev(*this,m3));
1319           if(nr)
1320             { m3->zipCoords(); nnr=nr->deepCpy(); nnr->sort(true); ret->setNodeReduction(nnr); }
1321           ret2=(MEDUMeshMultiLev *)ret; ret2->incrRef();
1322         }
1323       const DataArrayInt *famIds(_cell_fam_ids),*numIds(_cell_num_ids);
1324       if(famIds)
1325         {
1326           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp(famIds->selectByTupleIdSafe(pfl->begin(),pfl->end()));
1327           ret2->setFamilyIdsOnCells(tmp,false);
1328         }
1329       if(numIds)
1330         {
1331           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp(numIds->selectByTupleIdSafe(pfl->begin(),pfl->end()));
1332           ret2->setNumberIdsOnCells(tmp,false);
1333         }
1334       return ret2.retn();
1335       
1336     }
1337   else
1338     {
1339       MEDCouplingAutoRefCountObjectPtr<MEDCMeshMultiLev> ret(new MEDCMeshMultiLev(*this));
1340       if(nr)
1341         { nnr=nr->deepCpy(); nnr->sort(true); ret->setNodeReduction(nnr); }
1342       return ret.retn();
1343     }
1344 }
1345
1346 /*!
1347  * \a param [out] isInternal if true the returned pointers are those in main data structure. If false those pointers have been built espacially for that method.
1348  */
1349 std::vector< DataArrayDouble * > MEDCMeshMultiLev::buildVTUArrays(bool& isInternal) const
1350 {
1351   isInternal=_is_internal;
1352   std::size_t sz(_coords.size());
1353   std::vector< DataArrayDouble * > ret(sz);
1354   for(std::size_t i=0;i<sz;i++)
1355     {
1356       ret[i]=const_cast<DataArrayDouble *>((const DataArrayDouble *)_coords[i]);
1357       ret[i]->incrRef();
1358     }
1359   return ret;
1360 }
1361
1362 //=
1363
1364 MEDCurveLinearMeshMultiLev *MEDCurveLinearMeshMultiLev::New(const MEDFileCurveLinearMesh *m, const std::vector<int>& levs)
1365 {
1366   return new MEDCurveLinearMeshMultiLev(m,levs);
1367 }
1368
1369 MEDCurveLinearMeshMultiLev *MEDCurveLinearMeshMultiLev::New(const MEDFileCurveLinearMesh *m, const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities)
1370 {
1371   return new MEDCurveLinearMeshMultiLev(m,gts,pfls,nbEntities);
1372 }
1373
1374 MEDCurveLinearMeshMultiLev::MEDCurveLinearMeshMultiLev(const MEDFileCurveLinearMesh *m, const std::vector<int>& levs):MEDStructuredMeshMultiLev(m,levs)
1375 {
1376   if(!m)
1377     throw INTERP_KERNEL::Exception("MEDCurveLinearMeshMultiLev constructor : null input pointer !");
1378   if(levs.size()!=1 || levs[0]!=0)
1379     throw INTERP_KERNEL::Exception("MEDCurveLinearMeshMultiLev constructor : levels supported is 0 only !");
1380   DataArrayDouble *coords(const_cast<DataArrayDouble *>(m->getMesh()->getCoords()));
1381   if(!coords)
1382     throw INTERP_KERNEL::Exception("MEDCurveLinearMeshMultiLev constructor 2 : no coords set !");
1383   coords->incrRef();
1384   _coords=coords;
1385   _structure=m->getMesh()->getNodeGridStructure();
1386 }
1387
1388 MEDCurveLinearMeshMultiLev::MEDCurveLinearMeshMultiLev(const MEDFileCurveLinearMesh *m, const std::vector<INTERP_KERNEL::NormalizedCellType>& gts, const std::vector<const DataArrayInt *>& pfls, const std::vector<int>& nbEntities):MEDStructuredMeshMultiLev(m,m->getNumberOfNodes(),gts,pfls,nbEntities)
1389 {
1390   if(!m)
1391     throw INTERP_KERNEL::Exception("MEDCurveLinearMeshMultiLev constructor 2 : null input pointer !");
1392   if(gts.size()!=1 || pfls.size()!=1)
1393     throw INTERP_KERNEL::Exception("MEDCurveLinearMeshMultiLev constructor 2 : lengthes of gts and pfls must be equal to one !");
1394   int mdim(MEDCouplingStructuredMesh::GetGeoTypeGivenMeshDimension(m->getMeshDimension()));
1395   if(mdim!=gts[0])
1396     throw INTERP_KERNEL::Exception("MEDCurveLinearMeshMultiLev constructor 2 : the unique geo type is invalid regarding meshdim !");
1397   DataArrayDouble *coords(const_cast<DataArrayDouble *>(m->getMesh()->getCoords()));
1398   if(!coords)
1399     throw INTERP_KERNEL::Exception("MEDCurveLinearMeshMultiLev constructor 2 : no coords set !");
1400   coords->incrRef();
1401   _coords=coords;
1402   _structure=m->getMesh()->getNodeGridStructure();
1403 }
1404
1405 MEDCurveLinearMeshMultiLev::MEDCurveLinearMeshMultiLev(const MEDCurveLinearMeshMultiLev& other):MEDStructuredMeshMultiLev(other),_coords(other._coords),_structure(other._structure)
1406 {
1407 }
1408
1409 std::vector<int> MEDCurveLinearMeshMultiLev::getNodeGridStructure() const
1410 {
1411   return _structure;
1412 }
1413
1414 MEDMeshMultiLev *MEDCurveLinearMeshMultiLev::prepare() const
1415 {
1416   const DataArrayInt *pfl(0),*nr(_node_reduction);
1417   if(!_pfls.empty())
1418     pfl=_pfls[0];
1419   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> nnr;
1420   std::vector<int> cgs,ngs(getNodeGridStructure());
1421   cgs.resize(ngs.size());
1422   std::transform(ngs.begin(),ngs.end(),cgs.begin(),std::bind2nd(std::plus<int>(),-1));
1423   if(pfl)
1424     {
1425       std::vector< std::pair<int,int> > cellParts,nodeParts;
1426       MEDCouplingAutoRefCountObjectPtr<MEDMeshMultiLev> ret2;
1427       if(MEDCouplingStructuredMesh::IsPartStructured(pfl->begin(),pfl->end(),cgs,cellParts))
1428         {
1429           nodeParts=cellParts;
1430           std::vector<int> st(ngs.size());
1431           for(std::size_t i=0;i<ngs.size();i++)
1432             {
1433               nodeParts[i].second++;
1434               st[i]=nodeParts[i].second-nodeParts[i].first;
1435             }
1436           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> p(MEDCouplingStructuredMesh::BuildExplicitIdsFrom(ngs,nodeParts));
1437           MEDCouplingAutoRefCountObjectPtr<MEDCurveLinearMeshMultiLev> ret(new MEDCurveLinearMeshMultiLev(*this));
1438           ret->_is_internal=false;
1439           if(nr)
1440             { nnr=nr->deepCpy(); nnr->sort(true); ret->setNodeReduction(nnr); }
1441           ret->_nb_entities[0]=pfl->getNumberOfTuples();
1442           ret->_pfls[0]=0;
1443           ret->_coords=_coords->selectByTupleIdSafe(p->begin(),p->end());
1444           ret->_structure=st;
1445           ret2=(MEDCurveLinearMeshMultiLev *)ret; ret2->incrRef();
1446         }
1447       else
1448         {
1449           MEDCouplingAutoRefCountObjectPtr<MEDCouplingCurveLinearMesh> m(MEDCouplingCurveLinearMesh::New());
1450           m->setCoords(_coords); m->setNodeGridStructure(&_structure[0],&_structure[0]+_structure.size());
1451           MEDCouplingAutoRefCountObjectPtr<MEDCoupling1SGTUMesh> m2(m->build1SGTUnstructured());
1452           MEDCouplingAutoRefCountObjectPtr<MEDCoupling1GTUMesh> m3=dynamic_cast<MEDCoupling1GTUMesh *>(m2->buildPartOfMySelfKeepCoords(pfl->begin(),pfl->end()));
1453           MEDCouplingAutoRefCountObjectPtr<MEDUMeshMultiLev> ret(new MEDUMeshMultiLev(*this,m3));
1454           if(nr)
1455             { m3->zipCoords(); nnr=nr->deepCpy(); nnr->sort(true); ret->setNodeReduction(nnr); }
1456           ret2=(MEDUMeshMultiLev *)ret; ret2->incrRef();
1457         }
1458       const DataArrayInt *famIds(_cell_fam_ids),*numIds(_cell_num_ids);
1459       if(famIds)
1460         {
1461           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp(famIds->selectByTupleIdSafe(pfl->begin(),pfl->end()));
1462           ret2->setFamilyIdsOnCells(tmp,false);
1463         }
1464       if(numIds)
1465         {
1466           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp(numIds->selectByTupleIdSafe(pfl->begin(),pfl->end()));
1467           ret2->setNumberIdsOnCells(tmp,false);
1468         }
1469       return ret2.retn();
1470     }
1471   else
1472     {
1473       MEDCouplingAutoRefCountObjectPtr<MEDCurveLinearMeshMultiLev> ret(new MEDCurveLinearMeshMultiLev(*this));
1474       if(nr)
1475         { nnr=nr->deepCpy(); nnr->sort(true); ret->setNodeReduction(nnr); }
1476       return ret.retn();
1477     }
1478 }
1479
1480 void MEDCurveLinearMeshMultiLev::buildVTUArrays(DataArrayDouble *&coords, std::vector<int>& nodeStrct, bool& isInternal) const
1481 {
1482   isInternal=_is_internal;
1483   nodeStrct=_structure;
1484   const DataArrayDouble *coo(_coords);
1485   if(!coo)
1486     throw INTERP_KERNEL::Exception("MEDCurveLinearMeshMultiLev::buildVTUArrays : null pointer on coordinates !");
1487   coords=const_cast<DataArrayDouble *>(coo); coords->incrRef();
1488 }
1489
1490 //=
1491
1492 MEDFileField1TSStructItem2::MEDFileField1TSStructItem2()
1493 {
1494 }
1495
1496 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)
1497 {
1498   _pfl->setName(c.c_str());
1499 }
1500
1501 void MEDFileField1TSStructItem2::checkWithMeshStructForCells(const MEDFileMeshStruct *mst, const MEDFileFieldGlobsReal *globs)
1502 {
1503   int nbOfEnt=mst->getNumberOfElemsOfGeoType(_geo_type);
1504   checkInRange(nbOfEnt,1,globs);
1505 }
1506
1507 void MEDFileField1TSStructItem2::checkWithMeshStructForGaussNE(const MEDFileMeshStruct *mst, const MEDFileFieldGlobsReal *globs)
1508 {
1509   int nbOfEnt=mst->getNumberOfElemsOfGeoType(_geo_type);
1510   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(_geo_type);
1511   checkInRange(nbOfEnt,(int)cm.getNumberOfNodes(),globs);
1512 }
1513
1514 void MEDFileField1TSStructItem2::checkWithMeshStructForGaussPT(const MEDFileMeshStruct *mst, const MEDFileFieldGlobsReal *globs)
1515 {
1516   if(!globs)
1517     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::checkWithMeshStructForGaussPT : no globals specified !");
1518   if(_loc.empty())
1519     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::checkWithMeshStructForGaussPT : no localization specified !");
1520   const MEDFileFieldLoc& loc=globs->getLocalization(_loc.c_str());
1521   int nbOfEnt=mst->getNumberOfElemsOfGeoType(_geo_type);
1522   checkInRange(nbOfEnt,loc.getNumberOfGaussPoints(),globs);
1523 }
1524
1525 int MEDFileField1TSStructItem2::getNbOfIntegrationPts(const MEDFileFieldGlobsReal *globs) const
1526 {
1527   if(_loc.empty())
1528     {
1529       if(getPflName().empty())
1530         return (_start_end.second-_start_end.first)/_nb_of_entity;
1531       else
1532         return (_start_end.second-_start_end.first)/getPfl(globs)->getNumberOfTuples();
1533     }
1534   else
1535     {
1536       const MEDFileFieldLoc& loc(globs->getLocalization(_loc.c_str()));
1537       return loc.getNumberOfGaussPoints();
1538     }
1539 }
1540
1541 std::string MEDFileField1TSStructItem2::getPflName() const
1542 {
1543   return _pfl->getName();
1544 }
1545
1546 const DataArrayInt *MEDFileField1TSStructItem2::getPfl(const MEDFileFieldGlobsReal *globs) const
1547 {
1548   if(!_pfl->isAllocated())
1549     {
1550       if(_pfl->getName().empty())
1551         return 0;
1552       else
1553         return globs->getProfile(_pfl->getName().c_str());
1554     }
1555   else
1556     return _pfl;
1557 }
1558
1559 /*!
1560  * \param [in] nbOfEntity - number of entity that can be either cells or nodes. Not other possiblity.
1561  * \param [in] nip - number of integration points. 1 for ON_CELLS and NO_NODES
1562  */
1563 void MEDFileField1TSStructItem2::checkInRange(int nbOfEntity, int nip, const MEDFileFieldGlobsReal *globs)
1564 {
1565   _nb_of_entity=nbOfEntity;
1566   if(_pfl->getName().empty())
1567     {
1568       if(nbOfEntity!=(_start_end.second-_start_end.first)/nip)
1569         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::checkInRange : Mismatch between number of entities and size of node field !");
1570       return ;
1571     }
1572   else
1573     {
1574       if(!globs)
1575         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::checkInRange : Presence of a profile on field whereas no globals found in file !");
1576       const DataArrayInt *pfl=globs->getProfile(_pfl->getName().c_str());
1577       if(!pfl)
1578         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::checkInRange : Presence of a profile on field whereas no such profile found in file !");
1579       pfl->checkAllIdsInRange(0,nbOfEntity);
1580     }
1581 }
1582
1583 bool MEDFileField1TSStructItem2::isFastlyEqual(int& startExp, INTERP_KERNEL::NormalizedCellType gt, const std::string& pflName) const
1584 {
1585   if(startExp!=_start_end.first)
1586     return false;
1587   if(gt!=_geo_type)
1588     return false;
1589   if(getPflName()!=pflName)
1590     return false;
1591   startExp=_start_end.second;
1592   return true;
1593 }
1594
1595 bool MEDFileField1TSStructItem2::operator==(const MEDFileField1TSStructItem2& other) const throw(INTERP_KERNEL::Exception)
1596 {
1597   //_nb_of_entity is not taken into account here. It is not a bug, because no mesh consideration needed here to perform fast compare.
1598   //idem for _loc. It is not an effective attribute for support comparison.
1599   return _geo_type==other._geo_type && _start_end==other._start_end && _pfl->getName()==other._pfl->getName();
1600 }
1601
1602 bool MEDFileField1TSStructItem2::isCellSupportEqual(const MEDFileField1TSStructItem2& other, const MEDFileFieldGlobsReal *globs) const
1603 {
1604   if(_geo_type!=other._geo_type)
1605     return false;
1606   if(_nb_of_entity!=other._nb_of_entity)
1607     return false;
1608   if((_pfl->getName().empty() && !other._pfl->getName().empty()) || (!_pfl->getName().empty() && other._pfl->getName().empty()))
1609     return false;
1610   if(_pfl->getName().empty() && other._pfl->getName().empty())
1611     return true;
1612   const DataArrayInt *pfl1(getPfl(globs)),*pfl2(other.getPfl(globs));
1613   return pfl1->isEqualWithoutConsideringStr(*pfl2);
1614 }
1615
1616 bool MEDFileField1TSStructItem2::isNodeSupportEqual(const MEDFileField1TSStructItem2& other, const MEDFileFieldGlobsReal *globs) const
1617 {
1618   return isCellSupportEqual(other,globs);
1619 }
1620
1621 /*!
1622  * \a objs must be non empty. \a objs should contain items having same geometric type.
1623  */
1624 MEDFileField1TSStructItem2 MEDFileField1TSStructItem2::BuildAggregationOf(const std::vector<const MEDFileField1TSStructItem2 *>& objs, const MEDFileFieldGlobsReal *globs)
1625 {
1626   if(objs.empty())
1627     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::BuildAggregationOf : empty input !");
1628   if(objs.size()==1)
1629     return MEDFileField1TSStructItem2(*objs[0]);
1630   INTERP_KERNEL::NormalizedCellType gt(objs[0]->_geo_type);
1631   int nbEntityRef(objs[0]->_nb_of_entity);
1632   std::size_t sz(objs.size());
1633   std::vector<const DataArrayInt *> arrs(sz);
1634   for(std::size_t i=0;i<sz;i++)
1635     {
1636       const MEDFileField1TSStructItem2 *obj(objs[i]);
1637       if(gt!=obj->_geo_type)
1638         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::BuildAggregationOf : invalid situation ! All input must have the same geo type !");
1639       if(nbEntityRef!=obj->_nb_of_entity)
1640         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::BuildAggregationOf : invalid situation ! All input must have the global nb of entity !");
1641       if(obj->_pfl->getName().empty())
1642         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::BuildAggregationOf : invalid situation ! Several same geo type chunk must all lie on profiles !");
1643       arrs[i]=globs->getProfile(obj->_pfl->getName().c_str());
1644     }
1645   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr(DataArrayInt::Aggregate(arrs));
1646   arr->sort();
1647   int oldNbTuples(arr->getNumberOfTuples());
1648   arr=arr->buildUnique();
1649   if(oldNbTuples!=arr->getNumberOfTuples())
1650     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::BuildAggregationOf : some entities are present several times !");
1651   if(arr->isIdentity() && oldNbTuples==nbEntityRef)
1652     {
1653       std::pair<int,int> p(0,nbEntityRef);
1654       std::string a,b;
1655       MEDFileField1TSStructItem2 ret(gt,p,a,b);
1656       ret._nb_of_entity=nbEntityRef;
1657       return ret;
1658     }
1659   else
1660     {
1661       arr->setName(NEWLY_CREATED_PFL_NAME);
1662       std::pair<int,int> p(0,oldNbTuples);
1663       std::string a,b;
1664       MEDFileField1TSStructItem2 ret(gt,p,a,b);
1665       ret._nb_of_entity=nbEntityRef;
1666       ret._pfl=arr;
1667       return ret;
1668     }
1669 }
1670
1671 std::size_t MEDFileField1TSStructItem2::getHeapMemorySizeWithoutChildren() const
1672 {
1673   std::size_t ret(_loc.capacity());
1674   return ret;
1675 }
1676
1677 std::vector<const BigMemoryObject *> MEDFileField1TSStructItem2::getDirectChildren() const
1678 {
1679   std::vector<const BigMemoryObject *> ret;
1680   const DataArrayInt *pfl(_pfl);
1681   if(pfl)
1682     ret.push_back(pfl);
1683   return ret;
1684 }
1685
1686 //=
1687
1688 MEDFileField1TSStructItem::MEDFileField1TSStructItem(TypeOfField a, const std::vector< MEDFileField1TSStructItem2 >& b):_computed(false),_type(a),_items(b)
1689 {
1690 }
1691
1692 void MEDFileField1TSStructItem::checkWithMeshStruct(const MEDFileMeshStruct *mst, const MEDFileFieldGlobsReal *globs)
1693 {
1694   switch(_type)
1695     {
1696     case ON_NODES:
1697       {
1698         int nbOfEnt=mst->getNumberOfNodes();
1699         if(_items.size()!=1)
1700           throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::checkWithMeshStruct : for nodes field only one subdivision supported !");
1701         _items[0].checkInRange(nbOfEnt,1,globs);
1702         break ;
1703       }
1704     case ON_CELLS:
1705       {
1706         for(std::vector< MEDFileField1TSStructItem2 >::iterator it=_items.begin();it!=_items.end();it++)
1707           (*it).checkWithMeshStructForCells(mst,globs);
1708         break;
1709       }
1710     case ON_GAUSS_NE:
1711       {
1712         for(std::vector< MEDFileField1TSStructItem2 >::iterator it=_items.begin();it!=_items.end();it++)
1713           (*it).checkWithMeshStructForGaussNE(mst,globs);
1714         break;
1715       }
1716     case ON_GAUSS_PT:
1717       {
1718         for(std::vector< MEDFileField1TSStructItem2 >::iterator it=_items.begin();it!=_items.end();it++)
1719           (*it).checkWithMeshStructForGaussPT(mst,globs);
1720         break;
1721       }
1722     default:
1723       throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::checkWithMeshStruct : not managed field type !");
1724     }
1725 }
1726
1727 bool MEDFileField1TSStructItem::operator==(const MEDFileField1TSStructItem& other) const throw(INTERP_KERNEL::Exception)
1728 {
1729   if(_type!=other._type)
1730     return false;
1731   if(_items.size()!=other._items.size())
1732     return false;
1733   for(std::size_t i=0;i<_items.size();i++)
1734     if(!(_items[i]==other._items[i]))
1735       return false;
1736   return true;
1737 }
1738
1739 bool MEDFileField1TSStructItem::isCellSupportEqual(const MEDFileField1TSStructItem& other, const MEDFileFieldGlobsReal *globs) const
1740 {
1741   if(_type!=other._type)
1742     return false;
1743   if(_items.size()!=other._items.size())
1744     return false;
1745   for(std::size_t i=0;i<_items.size();i++)
1746     if(!(_items[i].isCellSupportEqual(other._items[i],globs)))
1747       return false;
1748   return true;
1749 }
1750
1751 bool MEDFileField1TSStructItem::isNodeSupportEqual(const MEDFileField1TSStructItem& other, const MEDFileFieldGlobsReal *globs) const
1752 {
1753   if(_type!=other._type)
1754     return false;
1755   if(_items.size()!=other._items.size())
1756     return false;
1757   for(std::size_t i=0;i<_items.size();i++)
1758     if(!(_items[i].isNodeSupportEqual(other._items[i],globs)))
1759       return false;
1760   return true;
1761 }
1762
1763 bool MEDFileField1TSStructItem::isEntityCell() const
1764 {
1765   if(_type==ON_NODES)
1766     return false;
1767   else
1768     return true;
1769 }
1770
1771 class CmpGeo
1772 {
1773 public:
1774   CmpGeo(INTERP_KERNEL::NormalizedCellType geoTyp):_geo_type(geoTyp) { }
1775   bool operator()(const std::pair< INTERP_KERNEL::NormalizedCellType, std::vector<std::size_t> > & v) const { return _geo_type==v.first; }
1776 private:
1777   INTERP_KERNEL::NormalizedCellType _geo_type;
1778 };
1779
1780 MEDFileField1TSStructItem MEDFileField1TSStructItem::simplifyMeOnCellEntity(const MEDFileFieldGlobsReal *globs) const
1781 {
1782   if(!isEntityCell())
1783     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::simplifyMeOnCellEntity : must be on ON_CELLS, ON_GAUSS_NE or ON_GAUSS_PT !");
1784   std::vector< std::pair< INTERP_KERNEL::NormalizedCellType, std::vector<std::size_t> > > m;
1785   std::size_t i=0;
1786   for(std::vector< MEDFileField1TSStructItem2 >::const_iterator it=_items.begin();it!=_items.end();it++,i++)
1787     {
1788       std::vector< std::pair< INTERP_KERNEL::NormalizedCellType, std::vector<std::size_t> > >::iterator it0(std::find_if(m.begin(),m.end(),CmpGeo((*it).getGeo())));
1789       if(it0==m.end())
1790         m.push_back(std::pair< INTERP_KERNEL::NormalizedCellType, std::vector<std::size_t> >((*it).getGeo(),std::vector<std::size_t>(1,i)));
1791       else
1792         (*it0).second.push_back(i);
1793     }
1794   if(m.size()==_items.size())
1795     {
1796       MEDFileField1TSStructItem ret(*this);
1797       ret._type=ON_CELLS;
1798       return ret;
1799     }
1800   std::size_t sz(m.size());
1801   std::vector< MEDFileField1TSStructItem2 > items(sz);
1802   for(i=0;i<sz;i++)
1803     {
1804       const std::vector<std::size_t>& ids=m[i].second;
1805       std::vector<const MEDFileField1TSStructItem2 *>objs(ids.size());
1806       for(std::size_t j=0;j<ids.size();j++)
1807         objs[j]=&_items[ids[j]];
1808       items[i]=MEDFileField1TSStructItem2::BuildAggregationOf(objs,globs);
1809     }
1810   MEDFileField1TSStructItem ret(ON_CELLS,items);
1811   ret._computed=true;
1812   return ret;
1813 }
1814
1815 /*!
1816  * \a this is expected to be ON_CELLS and simplified.
1817  */
1818 bool MEDFileField1TSStructItem::isCompatibleWithNodesDiscr(const MEDFileField1TSStructItem& other, const MEDFileMeshStruct *meshSt, const MEDFileFieldGlobsReal *globs) const
1819 {
1820   if(other._type!=ON_NODES)
1821     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isCompatibleWithNodesDiscr : other must be on nodes !");
1822   if(other._items.size()!=1)
1823     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isCompatibleWithNodesDiscr : other is on nodes but number of subparts !");
1824   int theFirstLevFull;
1825   bool ret0=isFullyOnOneLev(meshSt,theFirstLevFull);
1826   const MEDFileField1TSStructItem2& otherNodeIt(other._items[0]);
1827   if(otherNodeIt.getPflName().empty())
1828     {//on all nodes
1829       if(!ret0)
1830         return false;
1831       return theFirstLevFull==0;
1832     }
1833   else
1834     {
1835       const DataArrayInt *pfl=globs->getProfile(otherNodeIt.getPflName().c_str());
1836       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> cpyPfl(pfl->deepCpy());
1837       cpyPfl->sort();
1838       int nbOfNodes(meshSt->getNumberOfNodes());
1839       if(cpyPfl->isIdentity() && cpyPfl->getNumberOfTuples()==nbOfNodes)
1840         {//on all nodes also !
1841           if(!ret0)
1842             return false;
1843           return theFirstLevFull==0;
1844         }
1845       std::vector<bool> nodesFetched(nbOfNodes,false);
1846       meshSt->getTheMesh()->whichAreNodesFetched(*this,globs,nodesFetched);
1847       return cpyPfl->isFittingWith(nodesFetched);
1848     }
1849 }
1850
1851 bool MEDFileField1TSStructItem::isFullyOnOneLev(const MEDFileMeshStruct *meshSt, int& theFirstLevFull) const
1852 {
1853   if(_type!=ON_CELLS)
1854     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isFullyOnOneLev : works only for ON_CELLS discretization !");
1855   if(_items.empty())
1856     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isFullyOnOneLev : items vector is empty !");
1857   int nbOfLevs(meshSt->getNumberOfLevs());
1858   if(nbOfLevs==0)
1859     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isFullyOnOneLev : no levels in input mesh structure !");
1860   std::vector<int> levs(nbOfLevs);
1861   theFirstLevFull=1;
1862   std::set<INTERP_KERNEL::NormalizedCellType> gts;
1863   for(std::vector< MEDFileField1TSStructItem2 >::const_iterator it=_items.begin();it!=_items.end();it++)
1864     {
1865       if(!(*it).getPflName().empty())
1866         return false;
1867       INTERP_KERNEL::NormalizedCellType gt((*it).getGeo());
1868       if(gts.find(gt)!=gts.end())
1869         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isFullyOnOneLev : internal error !");
1870       gts.insert(gt);
1871       int pos(meshSt->getLevelOfGeoType((*it).getGeo()));
1872       levs[-pos]++;
1873     }
1874   for(int i=0;i<nbOfLevs;i++)
1875     if(meshSt->getNumberOfGeoTypesInLev(-i)==levs[i])
1876       { theFirstLevFull=-i; return true; }
1877   return false;
1878 }
1879
1880 const MEDFileField1TSStructItem2& MEDFileField1TSStructItem::operator[](std::size_t i) const throw(INTERP_KERNEL::Exception)
1881 {
1882   if(i>=_items.size())
1883     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::operator[] : input is not in valid range !");
1884   return _items[i];
1885 }
1886
1887 std::size_t MEDFileField1TSStructItem::getHeapMemorySizeWithoutChildren() const
1888 {
1889   std::size_t ret(_items.size()*sizeof(MEDFileField1TSStructItem2));
1890   return ret;
1891 }
1892
1893 std::vector<const BigMemoryObject *> MEDFileField1TSStructItem::getDirectChildren() const
1894 {
1895   std::vector<const BigMemoryObject *> ret;
1896   for(std::vector< MEDFileField1TSStructItem2 >::const_iterator it=_items.begin();it!=_items.end();it++)
1897     ret.push_back(&(*it));
1898   return ret;
1899 }
1900
1901 MEDMeshMultiLev *MEDFileField1TSStructItem::buildFromScratchDataSetSupportOnCells(const MEDFileMeshStruct *mst, const MEDFileFieldGlobsReal *globs) const
1902 {
1903   std::size_t sz(_items.size());
1904   std::vector<INTERP_KERNEL::NormalizedCellType> a0(sz);
1905   std::vector<const DataArrayInt *> a1(sz);
1906   std::vector<int> a2(sz);
1907   std::size_t i(0);
1908   for(std::vector< MEDFileField1TSStructItem2 >::const_iterator it=_items.begin();it!=_items.end();it++,i++)
1909     {
1910       a0[i]=(*it).getGeo();
1911       a1[i]=(*it).getPfl(globs);
1912       a2[i]=mst->getNumberOfElemsOfGeoType((*it).getGeo());
1913     }
1914   return MEDMeshMultiLev::New(mst->getTheMesh(),a0,a1,a2);
1915 }
1916
1917 std::vector<INTERP_KERNEL::NormalizedCellType> MEDFileField1TSStructItem::getGeoTypes(const MEDFileMesh *m) const
1918 {
1919   std::vector<INTERP_KERNEL::NormalizedCellType> ret;
1920   if(_type==ON_NODES)
1921     {
1922       if(!_items.empty() && _items[0].getPflName().empty())
1923         {
1924           if(m)
1925             return m->getAllGeoTypes();
1926           else
1927             return ret;
1928         }
1929       else
1930         return ret;
1931     }
1932   for(std::vector< MEDFileField1TSStructItem2 >::const_iterator it=_items.begin();it!=_items.end();it++)
1933     {
1934       INTERP_KERNEL::NormalizedCellType elt((*it).getGeo());
1935       std::vector<INTERP_KERNEL::NormalizedCellType>::iterator it2(std::find(ret.begin(),ret.end(),elt));
1936       if(it2==ret.end())
1937         ret.push_back(elt);
1938     }
1939   return ret;
1940 }
1941
1942 MEDFileField1TSStructItem MEDFileField1TSStructItem::BuildItemFrom(const MEDFileAnyTypeField1TS *ref, const MEDFileMeshStruct *meshSt)
1943 {
1944   TypeOfField atype;
1945   std::vector< MEDFileField1TSStructItem2 > anItems;
1946   //
1947   std::vector< std::vector<std::string> > pfls,locs;
1948   std::vector< std::vector<TypeOfField> > typesF;
1949   std::vector<INTERP_KERNEL::NormalizedCellType> geoTypes;
1950   std::vector< std::vector<std::pair<int,int> > > strtEnds=ref->getFieldSplitedByType(std::string(),geoTypes,typesF,pfls,locs);
1951   std::size_t nbOfGeoTypes(geoTypes.size());
1952   if(nbOfGeoTypes==0)
1953     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct : not null by empty ref  !");
1954   bool isFirst=true;
1955   for(std::size_t i=0;i<nbOfGeoTypes;i++)
1956     {
1957       std::size_t sz=typesF[i].size();
1958       if(strtEnds[i].size()<1 || sz<1 || pfls[i].size()<1)
1959         throw INTERP_KERNEL::Exception("MEDFileField1TSStruct : internal error #1 !");
1960       //
1961       if(isFirst)
1962         atype=typesF[i][0];
1963       isFirst=false;
1964       //
1965       for(std::size_t j=0;j<sz;j++)
1966         {
1967           if(atype==typesF[i][j])
1968             anItems.push_back(MEDFileField1TSStructItem2(geoTypes[i],strtEnds[i][j],pfls[i][j],locs[i][j]));
1969           else
1970             throw INTERP_KERNEL::Exception("MEDFileField1TSStruct : can be applied only on single spatial discretization fields ! Call SplitPerDiscretization method !");
1971         }
1972     }
1973   MEDFileField1TSStructItem ret(atype,anItems);
1974   ret.checkWithMeshStruct(meshSt,ref);
1975   return ret;
1976 }
1977
1978 //=
1979
1980 MEDFileField1TSStruct *MEDFileField1TSStruct::New(const MEDFileAnyTypeField1TS *ref, MEDFileMeshStruct *mst)
1981 {
1982   return new MEDFileField1TSStruct(ref,mst);
1983 }
1984
1985 MEDFileField1TSStruct::MEDFileField1TSStruct(const MEDFileAnyTypeField1TS *ref, MEDFileMeshStruct *mst)
1986 {
1987   _already_checked.push_back(MEDFileField1TSStructItem::BuildItemFrom(ref,mst));
1988 }
1989
1990 void MEDFileField1TSStruct::checkWithMeshStruct(MEDFileMeshStruct *mst, const MEDFileFieldGlobsReal *globs)
1991 {
1992   if(_already_checked.empty())
1993     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::checkWithMeshStruct : not correctly initialized !");
1994   _already_checked.back().checkWithMeshStruct(mst,globs);
1995 }
1996
1997 bool MEDFileField1TSStruct::isEqualConsideringThePast(const MEDFileAnyTypeField1TS *other, const MEDFileMeshStruct *mst) const
1998 {
1999   MEDFileField1TSStructItem b(MEDFileField1TSStructItem::BuildItemFrom(other,mst));
2000   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++)
2001     {
2002       if((*it)==b)
2003         return true;
2004     }
2005   return false;
2006 }
2007
2008 /*!
2009  * Not const because \a other structure will be added to the \c _already_checked attribute in case of success.
2010  */
2011 bool MEDFileField1TSStruct::isSupportSameAs(const MEDFileAnyTypeField1TS *other, const MEDFileMeshStruct *meshSt)
2012 {
2013   if(_already_checked.empty())
2014     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::isSupportSameAs : no ref !");
2015   MEDFileField1TSStructItem b(MEDFileField1TSStructItem::BuildItemFrom(other,meshSt));
2016   if(!_already_checked[0].isEntityCell() || !b.isEntityCell())
2017     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::isSupportSameAs : only available on cell entities !");
2018   MEDFileField1TSStructItem other1(b.simplifyMeOnCellEntity(other));
2019   int found=-1,i=0;
2020   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++,i++)
2021     if((*it).isComputed())
2022       { found=i; break; }
2023   bool ret(false);
2024   if(found==-1)
2025     {
2026       MEDFileField1TSStructItem this1(_already_checked[0].simplifyMeOnCellEntity(other));
2027       ret=this1.isCellSupportEqual(other1,other);
2028       if(ret)
2029         _already_checked.push_back(this1);
2030     }
2031   else
2032     ret=_already_checked[found].isCellSupportEqual(other1,other);
2033   if(ret)
2034     _already_checked.push_back(b);
2035   return ret;
2036 }
2037
2038 /*!
2039  * \param [in] other - a field with only one spatial discretization : ON_NODES.
2040  */
2041 bool MEDFileField1TSStruct::isCompatibleWithNodesDiscr(const MEDFileAnyTypeField1TS *other, const MEDFileMeshStruct *meshSt)
2042 {
2043   if(_already_checked.empty())
2044     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::isCompatibleWithNodesDiscr : no ref !");
2045   MEDFileField1TSStructItem other1(MEDFileField1TSStructItem::BuildItemFrom(other,meshSt));
2046   if(_already_checked[0].isEntityCell())
2047     {
2048       int found=-1,i=0;
2049       for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++,i++)
2050         if((*it).isComputed())
2051           { found=i; break; }
2052       bool ret(false);
2053       if(found==-1)
2054         {
2055           MEDFileField1TSStructItem this1(_already_checked[0].simplifyMeOnCellEntity(other));
2056           ret=this1.isCompatibleWithNodesDiscr(other1,meshSt,other);
2057           if(ret)
2058             _already_checked.push_back(this1);
2059         }
2060       else
2061         ret=_already_checked[found].isCompatibleWithNodesDiscr(other1,meshSt,other);
2062       if(ret)
2063         _already_checked.push_back(other1);
2064       return ret;
2065     }
2066   else
2067     return _already_checked[0].isNodeSupportEqual(other1,other);
2068 }
2069
2070 std::size_t MEDFileField1TSStruct::getHeapMemorySizeWithoutChildren() const
2071 {
2072   std::size_t ret(_already_checked.capacity()*sizeof(MEDFileField1TSStructItem));
2073   return ret;
2074 }
2075
2076 std::vector<const BigMemoryObject *> MEDFileField1TSStruct::getDirectChildren() const
2077 {
2078   std::vector<const BigMemoryObject *> ret;
2079   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++)
2080     ret.push_back(&(*it));
2081   return ret;
2082 }
2083
2084 MEDMeshMultiLev *MEDFileField1TSStruct::buildFromScratchDataSetSupport(const MEDFileMeshStruct *mst, const MEDFileFieldGlobsReal *globs) const
2085 {
2086   if(_already_checked.empty())
2087     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::buildFromScratchDataSetSupport : No outline structure in this !");
2088   int pos0(-1),pos1(-1);
2089   if(presenceOfCellDiscr(pos0))
2090     {
2091       MEDCouplingAutoRefCountObjectPtr<MEDMeshMultiLev> ret(_already_checked[pos0].buildFromScratchDataSetSupportOnCells(mst,globs));
2092       if(presenceOfPartialNodeDiscr(pos1))
2093         ret->setNodeReduction(_already_checked[pos1][0].getPfl(globs));
2094       return ret.retn();
2095     }
2096   else
2097     {
2098       if(!presenceOfPartialNodeDiscr(pos1))
2099         {//we have only all nodes, no cell definition info -> all existing levels !;
2100           return MEDMeshMultiLev::New(mst->getTheMesh(),mst->getTheMesh()->getNonEmptyLevels());
2101         }
2102       else
2103         return MEDMeshMultiLev::NewOnlyOnNode(mst->getTheMesh(),_already_checked[pos1][0].getPfl(globs));
2104     }
2105 }
2106
2107 bool MEDFileField1TSStruct::isDataSetSupportFastlyEqualTo(const MEDFileField1TSStruct& other, const MEDFileFieldGlobsReal *globs) const
2108 {
2109   int b0,b1;
2110   bool a0(presenceOfCellDiscr(b0)),a1(presenceOfPartialNodeDiscr(b1));
2111   int d0,d1;
2112   bool c0(other.presenceOfCellDiscr(d0)),c1(other.presenceOfPartialNodeDiscr(d1)); 
2113   if(a0!=c0 || a1!=c1)
2114     return false;
2115   if(a0)
2116     if(!_already_checked[b0].isCellSupportEqual(other._already_checked[d0],globs))
2117       return false;
2118   if(a1)
2119     if(!_already_checked[b1].isNodeSupportEqual(other._already_checked[d1],globs))
2120       return false;
2121   return true;
2122 }
2123
2124 std::vector<INTERP_KERNEL::NormalizedCellType> MEDFileField1TSStruct::getGeoTypes(const MEDFileMesh *m) const
2125 {
2126   std::vector<INTERP_KERNEL::NormalizedCellType> ret;
2127   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++)
2128     {
2129       std::vector<INTERP_KERNEL::NormalizedCellType> ret2((*it).getGeoTypes(m));
2130       for(std::vector<INTERP_KERNEL::NormalizedCellType>::const_iterator it2=ret2.begin();it2!=ret2.end();it2++)
2131         {
2132           if(*it2==INTERP_KERNEL::NORM_ERROR)
2133             continue;
2134           std::vector<INTERP_KERNEL::NormalizedCellType>::iterator it3(std::find(ret.begin(),ret.end(),*it2));
2135           if(it3==ret.end())
2136             ret.push_back(*it2);
2137         }
2138     }
2139   return ret;
2140 }
2141
2142 /*!
2143  * Returns true if presence in \a this of discretization ON_CELLS, ON_GAUSS_PT, ON_GAUSS_NE.
2144  * 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.
2145  */
2146 bool MEDFileField1TSStruct::presenceOfCellDiscr(int& pos) const
2147 {
2148   std::size_t refSz(std::numeric_limits<std::size_t>::max());
2149   bool ret(false);
2150   int i(0);
2151   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++,i++)
2152     {
2153       if((*it).getType()!=ON_NODES)
2154         {
2155           ret=true;
2156           std::size_t sz((*it).getNumberOfItems());
2157           if(refSz>sz)
2158             { pos=i; refSz=sz; }
2159         }
2160     }
2161   if(refSz==0)
2162     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::presenceOfCellDiscr : an element in this on entity CELL is empty !");
2163   return ret;
2164 }
2165
2166 /*!
2167  * Returns true if presence in \a this of discretization ON_NODES.
2168  * If true is returned the pos of the first element containing the single subpart.
2169  */
2170 bool MEDFileField1TSStruct::presenceOfPartialNodeDiscr(int& pos) const
2171 {
2172   int i(0);
2173   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++,i++)
2174     {
2175       if((*it).getType()==ON_NODES)
2176         {
2177           std::size_t sz((*it).getNumberOfItems());
2178           if(sz==1)
2179             {
2180               if(!(*it)[0].getPflName().empty())
2181                 { pos=i; return true; }
2182             }
2183           else
2184             throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::presenceOfPartialNodeDiscr : an element in this on entity NODE is split into several parts !");
2185         }
2186     }
2187   return false;
2188 }
2189
2190 //=
2191
2192 MEDFileFastCellSupportComparator *MEDFileFastCellSupportComparator::New(const MEDFileMeshStruct *m, const MEDFileAnyTypeFieldMultiTS *ref)
2193 {
2194   return new MEDFileFastCellSupportComparator(m,ref);
2195 }
2196
2197 MEDFileFastCellSupportComparator::MEDFileFastCellSupportComparator(const MEDFileMeshStruct *m, const MEDFileAnyTypeFieldMultiTS *ref)
2198 {
2199   if(!m)
2200     throw INTERP_KERNEL::Exception("MEDFileFastCellSupportComparator constructor : null input mesh struct !");
2201   _mesh_comp=const_cast<MEDFileMeshStruct *>(m); _mesh_comp->incrRef();
2202   int nbPts=ref->getNumberOfTS();
2203   _f1ts_cmps.resize(nbPts);
2204   for(int i=0;i<nbPts;i++)
2205     {
2206       MEDCouplingAutoRefCountObjectPtr<MEDFileAnyTypeField1TS> elt=ref->getTimeStepAtPos(i);
2207       _f1ts_cmps[i]=MEDFileField1TSStruct::New(elt,_mesh_comp);
2208       _f1ts_cmps[i]->checkWithMeshStruct(_mesh_comp,elt);
2209     }
2210 }
2211
2212 std::size_t MEDFileFastCellSupportComparator::getHeapMemorySizeWithoutChildren() const
2213 {
2214   std::size_t ret(_f1ts_cmps.capacity()*sizeof(MEDCouplingAutoRefCountObjectPtr<MEDFileField1TSStruct>));
2215   return ret;
2216 }
2217
2218 std::vector<const BigMemoryObject *> MEDFileFastCellSupportComparator::getDirectChildren() const
2219 {
2220   std::vector<const BigMemoryObject *> ret;
2221   const MEDFileMeshStruct *mst(_mesh_comp);
2222   if(mst)
2223     ret.push_back(mst);
2224   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileField1TSStruct> >::const_iterator it=_f1ts_cmps.begin();it!=_f1ts_cmps.end();it++)
2225     {
2226       const MEDFileField1TSStruct *cur(*it);
2227       if(cur)
2228         ret.push_back(cur);
2229     }
2230   return ret;
2231 }
2232
2233 bool MEDFileFastCellSupportComparator::isEqual(const MEDFileAnyTypeFieldMultiTS *other)
2234 {
2235   int nbPts=other->getNumberOfTS();
2236   if(nbPts!=(int)_f1ts_cmps.size())
2237     {
2238       std::ostringstream oss; oss << "MEDFileFastCellSupportComparator::isEqual : unexpected nb of time steps in  input ! Should be " << _f1ts_cmps.size() << " it is in reality " << nbPts << " !";
2239       throw INTERP_KERNEL::Exception(oss.str().c_str());
2240     }
2241   for(int i=0;i<nbPts;i++)
2242     {
2243       MEDCouplingAutoRefCountObjectPtr<MEDFileAnyTypeField1TS> elt=other->getTimeStepAtPos(i);
2244       if(!_f1ts_cmps[i]->isEqualConsideringThePast(elt,_mesh_comp))
2245         if(!_f1ts_cmps[i]->isSupportSameAs(elt,_mesh_comp))
2246           return false;
2247     }
2248   return true;
2249 }
2250
2251 bool MEDFileFastCellSupportComparator::isCompatibleWithNodesDiscr(const MEDFileAnyTypeFieldMultiTS *other)
2252 {
2253   int nbPts=other->getNumberOfTS();
2254   if(nbPts!=(int)_f1ts_cmps.size())
2255     {
2256       std::ostringstream oss; oss << "MEDFileFastCellSupportComparator::isCompatibleWithNodesDiscr : unexpected nb of time steps in  input ! Should be " << _f1ts_cmps.size() << " it is in reality " << nbPts << " !";
2257       throw INTERP_KERNEL::Exception(oss.str().c_str());
2258     }
2259   for(int i=0;i<nbPts;i++)
2260     {
2261       MEDCouplingAutoRefCountObjectPtr<MEDFileAnyTypeField1TS> elt=other->getTimeStepAtPos(i);
2262       if(!_f1ts_cmps[i]->isCompatibleWithNodesDiscr(elt,_mesh_comp))
2263         return false;
2264     }
2265   return true;
2266 }
2267
2268 MEDMeshMultiLev *MEDFileFastCellSupportComparator::buildFromScratchDataSetSupport(int timeStepId, const MEDFileFieldGlobsReal *globs) const
2269 {
2270   if(timeStepId<0 || timeStepId>=(int)_f1ts_cmps.size())
2271     {
2272       std::ostringstream oss; oss << "MEDFileFastCellSupportComparator::buildFromScratchDataSetSupport : requested time step id #" << timeStepId << " is not in [0," << _f1ts_cmps.size() << ") !";
2273       throw INTERP_KERNEL::Exception(oss.str().c_str());
2274     }
2275   const MEDFileField1TSStruct *obj(_f1ts_cmps[timeStepId]);
2276   if(!obj)
2277     {
2278       std::ostringstream oss; oss << "MEDFileFastCellSupportComparator::buildFromScratchDataSetSupport : at time step id #" << timeStepId << " no field structure overview defined !";
2279       throw INTERP_KERNEL::Exception(oss.str().c_str());
2280     }
2281   return obj->buildFromScratchDataSetSupport(_mesh_comp,globs);
2282 }
2283
2284 bool MEDFileFastCellSupportComparator::isDataSetSupportEqualToThePreviousOne(int timeStepId, const MEDFileFieldGlobsReal *globs) const
2285 {
2286   if(timeStepId<=0 || timeStepId>=(int)_f1ts_cmps.size())
2287     {
2288       std::ostringstream oss; oss << "MEDFileFastCellSupportComparator::isDataSetSupportEqualToThePreviousOne : requested time step id #" << timeStepId << " is not in [1," << _f1ts_cmps.size() << ") !";
2289       throw INTERP_KERNEL::Exception(oss.str().c_str());
2290     }
2291   const MEDFileField1TSStruct *obj(_f1ts_cmps[timeStepId]);
2292   const MEDFileField1TSStruct *objRef(_f1ts_cmps[timeStepId-1]);
2293   return objRef->isDataSetSupportFastlyEqualTo(*obj,globs);
2294 }
2295
2296 int MEDFileFastCellSupportComparator::getNumberOfTS() const
2297 {
2298   return _f1ts_cmps.size();
2299 }
2300
2301 std::vector<INTERP_KERNEL::NormalizedCellType> MEDFileFastCellSupportComparator::getGeoTypesAt(int timeStepId, const MEDFileMesh *m) const
2302 {
2303   if(timeStepId<0 || timeStepId>=(int)_f1ts_cmps.size())
2304     {
2305       std::ostringstream oss; oss << "MEDFileFastCellSupportComparator::getGeoTypesAt : requested time step id #" << timeStepId << " is not in [0," << _f1ts_cmps.size() << ") !";
2306       throw INTERP_KERNEL::Exception(oss.str().c_str());
2307     }
2308   const MEDFileField1TSStruct *elt(_f1ts_cmps[timeStepId]);
2309   if(!elt)
2310     {
2311       std::ostringstream oss; oss << "MEDFileFastCellSupportComparator::getGeoTypesAt : requested time step id #" << timeStepId << " points to a NULL pointer !";
2312       throw INTERP_KERNEL::Exception(oss.str().c_str());
2313     }
2314   return elt->getGeoTypes(m);
2315 }