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