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