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