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