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