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