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