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