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