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