Salome HOME
On the road to MEDReader
[tools/medcoupling.git] / src / MEDLoader / MEDFileFieldOverView.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #include "MEDFileFieldOverView.hxx"
22 #include "MEDFileField.hxx"
23 #include "MEDFileMesh.hxx"
24
25 #include "CellModel.hxx"
26
27 using namespace ParaMEDMEM;
28
29 MEDFileMeshStruct *MEDFileMeshStruct::New(const MEDFileMesh *mesh)
30 {
31   return new MEDFileMeshStruct(mesh);
32 }
33
34 std::size_t MEDFileMeshStruct::getHeapMemorySize() const
35 {
36   std::size_t ret(0);
37   for(std::vector< std::vector<int> >::const_iterator it0=_geo_types_distrib.begin();it0!=_geo_types_distrib.end();it0++)
38     ret+=(*it0).capacity()*sizeof(int);
39   ret+=_geo_types_distrib.capacity()*sizeof(std::vector<int>);
40   return ret;
41 }
42
43 MEDFileMeshStruct::MEDFileMeshStruct(const MEDFileMesh *mesh):_mesh(mesh)
44 {
45   std::vector<int> levs=mesh->getNonEmptyLevels();
46   _name=mesh->getName();
47   _nb_nodes=mesh->getNumberOfNodes();
48   _geo_types_distrib.resize(levs.size());
49   for(std::vector<int>::const_iterator lev=levs.begin();lev!=levs.end();lev++)
50     {
51       MEDCouplingAutoRefCountObjectPtr<MEDCouplingMesh> mLev=mesh->getGenMeshAtLevel(*lev);
52       _geo_types_distrib[-(*lev)]=mLev->getDistributionOfTypes();
53     }
54 }
55
56 int MEDFileMeshStruct::getLevelOfGeoType(INTERP_KERNEL::NormalizedCellType t) const throw(INTERP_KERNEL::Exception)
57 {
58   int j=0;
59   for(std::vector< std::vector<int> >::const_iterator it1=_geo_types_distrib.begin();it1!=_geo_types_distrib.end();it1++,j--)
60     {
61       std::size_t sz=(*it1).size();
62       if(sz%3!=0)
63         throw INTERP_KERNEL::Exception("MEDFileMeshStruct::getLevelOfGeoType : internal error in code !");
64       std::size_t nbGeo=sz/3;
65       for(std::size_t i=0;i<nbGeo;i++)
66         if((*it1)[3*i]==(int)t)
67           return j;
68     }
69   throw INTERP_KERNEL::Exception("MEDFileMeshStruct::getLevelOfGeoType : The specified geometric type is not present in the mesh structure !");
70 }
71
72 int MEDFileMeshStruct::getNumberOfElemsOfGeoType(INTERP_KERNEL::NormalizedCellType t) const throw(INTERP_KERNEL::Exception)
73 {
74   for(std::vector< std::vector<int> >::const_iterator it1=_geo_types_distrib.begin();it1!=_geo_types_distrib.end();it1++)
75     {
76       std::size_t sz=(*it1).size();
77       if(sz%3!=0)
78         throw INTERP_KERNEL::Exception("MEDFileMeshStruct::getNumberOfElemsOfGeoType : internal error in code !");
79       std::size_t nbGeo=sz/3;
80       for(std::size_t i=0;i<nbGeo;i++)
81         if((*it1)[3*i]==(int)t)
82           return (*it1)[3*i+1];
83     }
84   throw INTERP_KERNEL::Exception("The specified geometric type is not present in the mesh structure !");
85 }
86
87 int MEDFileMeshStruct::getNumberOfLevs() const
88 {
89   return (int)_geo_types_distrib.size();
90 }
91
92 int MEDFileMeshStruct::getNumberOfGeoTypesInLev(int relativeLev) const throw(INTERP_KERNEL::Exception)
93 {
94   int pos(-relativeLev);
95   if(pos<0 || pos>=_geo_types_distrib.size())
96     throw INTERP_KERNEL::Exception("MEDFileMeshStruct::getNumberOfGeoTypesInLev : invalid level specified !");
97   std::size_t sz=_geo_types_distrib[pos].size();
98   if(sz%3!=0)
99     throw INTERP_KERNEL::Exception("MEDFileMeshStruct::getNumberOfGeoTypesInLev : internal error in code !");
100   return (int)(sz/3);
101 }
102
103 //=
104
105 MEDFileField1TSStructItem2::MEDFileField1TSStructItem2()
106 {
107 }
108
109 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)
110 {
111   _pfl->setName(c.c_str());
112 }
113
114 void MEDFileField1TSStructItem2::checkWithMeshStructForCells(const MEDFileMeshStruct *mst, const MEDFileFieldGlobs *globs) throw(INTERP_KERNEL::Exception)
115 {
116   int nbOfEnt=mst->getNumberOfElemsOfGeoType(_geo_type);
117   checkInRange(nbOfEnt,1,globs);
118 }
119
120 void MEDFileField1TSStructItem2::checkWithMeshStructForGaussNE(const MEDFileMeshStruct *mst, const MEDFileFieldGlobs *globs) throw(INTERP_KERNEL::Exception)
121 {
122   int nbOfEnt=mst->getNumberOfElemsOfGeoType(_geo_type);
123   const INTERP_KERNEL::CellModel& cm=INTERP_KERNEL::CellModel::GetCellModel(_geo_type);
124   checkInRange(nbOfEnt,(int)cm.getNumberOfNodes(),globs);
125 }
126
127 void MEDFileField1TSStructItem2::checkWithMeshStructForGaussPT(const MEDFileMeshStruct *mst, const MEDFileFieldGlobs *globs) throw(INTERP_KERNEL::Exception)
128 {
129   if(!globs)
130     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::checkWithMeshStructForGaussPT : no globals specified !");
131   if(_loc.empty())
132     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::checkWithMeshStructForGaussPT : no localization specified !");
133   const MEDFileFieldLoc& loc=globs->getLocalization(_loc.c_str());
134   int nbOfEnt=mst->getNumberOfElemsOfGeoType(_geo_type);
135   checkInRange(nbOfEnt,loc.getNumberOfGaussPoints(),globs);
136 }
137
138 std::string MEDFileField1TSStructItem2::getPflName() const
139 {
140   return _pfl->getName();
141 }
142
143 /*!
144  * \param [in] nbOfEntity - number of entity that can be either cells or nodes. Not other possiblity.
145  * \param [in] nip - number of integration points. 1 for ON_CELLS and NO_NODES
146  */
147 void MEDFileField1TSStructItem2::checkInRange(int nbOfEntity, int nip, const MEDFileFieldGlobs *globs) throw(INTERP_KERNEL::Exception)
148 {
149   _nb_of_entity=nbOfEntity;
150   if(_pfl->getName().empty())
151     {
152       if(nbOfEntity!=(_start_end.second-_start_end.first)/nip)
153         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::checkInRange : Mismatch between number of entities and size of node field !");
154       return ;
155     }
156   else
157     {
158       if(!globs)
159         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::checkInRange : Presence of a profile on field whereas no globals found in file !");
160       const DataArrayInt *pfl=globs->getProfile(_pfl->getName().c_str());
161       if(!pfl)
162         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::checkInRange : Presence of a profile on field whereas no such profile found in file !");
163       pfl->checkAllIdsInRange(0,nbOfEntity);
164     }
165 }
166
167 bool MEDFileField1TSStructItem2::operator==(const MEDFileField1TSStructItem2& other) const throw(INTERP_KERNEL::Exception)
168 {
169   //_nb_of_entity is not taken into account here. It is not a bug, because no mesh consideration needed here to perform fast compare.
170   //idem for _loc. It is not an effective attribute for support comparison.
171   return _geo_type==other._geo_type && _start_end==other._start_end && _pfl->getName()==other._pfl->getName();
172 }
173
174 bool MEDFileField1TSStructItem2::isCellSupportEqual(const MEDFileField1TSStructItem2& other) const throw(INTERP_KERNEL::Exception)
175 {
176   if(_geo_type!=other._geo_type)
177     return false;
178   if(_nb_of_entity!=other._nb_of_entity)
179     return false;
180   if((_pfl->getName().empty() && !other._pfl->getName().empty()) || (!_pfl->getName().empty() && other._pfl->getName().empty()))
181     return false;
182   if(_pfl->getName().empty() && other._pfl->getName().empty())
183     return true;
184   return _pfl->isEqualWithoutConsideringStr(*other._pfl);
185 }
186
187 /*!
188  * \a objs must be non empty. \a objs should contain items having same geometric type.
189  */
190 MEDFileField1TSStructItem2 MEDFileField1TSStructItem2::BuildAggregationOf(const std::vector<const MEDFileField1TSStructItem2 *>& objs, const MEDFileFieldGlobs *globs) throw(INTERP_KERNEL::Exception)
191 {
192   if(objs.empty())
193     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::BuildAggregationOf : empty input !");
194   if(objs.size()==1)
195     return MEDFileField1TSStructItem2(*objs[0]);
196   INTERP_KERNEL::NormalizedCellType gt(objs[0]->_geo_type);
197   int nbEntityRef(objs[0]->_nb_of_entity);
198   std::size_t sz(objs.size());
199   std::vector<const DataArrayInt *> arrs(sz);
200   for(std::size_t i=0;i<sz;i++)
201     {
202       const MEDFileField1TSStructItem2 *obj(objs[i]);
203       if(gt!=obj->_geo_type)
204         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::BuildAggregationOf : invalid situation ! All input must have the same geo type !");
205       if(nbEntityRef!=obj->_nb_of_entity)
206         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::BuildAggregationOf : invalid situation ! All input must have the global nb of entity !");
207       if(obj->_pfl->getName().empty())
208         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::BuildAggregationOf : invalid situation ! Several same geo type chunk must all lie on profiles !");
209       arrs[i]=globs->getProfile(obj->_pfl->getName().c_str());
210     }
211   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> arr(DataArrayInt::Aggregate(arrs));
212   arr->sort();
213   int oldNbTuples(arr->getNumberOfTuples());
214   arr=arr->buildUnique();
215   if(oldNbTuples!=arr->getNumberOfTuples())
216     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem2::BuildAggregationOf : some entities are present several times !");
217   if(arr->isIdentity() && oldNbTuples==nbEntityRef)
218     {
219       std::pair<int,int> p(0,nbEntityRef);
220       std::string a,b;
221       MEDFileField1TSStructItem2 ret(gt,p,a,b);
222       ret._nb_of_entity=nbEntityRef;
223       return ret;
224     }
225   else
226     {
227       arr->setName("???");
228       std::pair<int,int> p(0,oldNbTuples);
229       std::string a,b;
230       MEDFileField1TSStructItem2 ret(gt,p,a,b);
231       ret._nb_of_entity=nbEntityRef;
232       ret._pfl=arr;
233       return ret;
234     }
235 }
236
237 std::size_t MEDFileField1TSStructItem2::getHeapMemorySize() const
238 {
239   std::size_t ret(0);
240   const DataArrayInt *pfl(_pfl);
241   if(pfl)
242     ret+=pfl->getHeapMemorySize();
243   ret+=_loc.capacity();
244   return ret;
245 }
246
247 //=
248
249 MEDFileField1TSStructItem::MEDFileField1TSStructItem(TypeOfField a, const std::vector< MEDFileField1TSStructItem2 >& b):_computed(false),_type(a),_items(b)
250 {
251 }
252
253 void MEDFileField1TSStructItem::checkWithMeshStruct(const MEDFileMeshStruct *mst, const MEDFileFieldGlobs *globs) throw(INTERP_KERNEL::Exception)
254 {
255   switch(_type)
256     {
257     case ON_NODES:
258       {
259         int nbOfEnt=mst->getNumberOfNodes();
260         if(_items.size()!=1)
261           throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::checkWithMeshStruct : for nodes field only one subdivision supported !");
262         _items[0].checkInRange(nbOfEnt,1,globs);
263         break ;
264       }
265     case ON_CELLS:
266       {
267         for(std::vector< MEDFileField1TSStructItem2 >::iterator it=_items.begin();it!=_items.end();it++)
268           (*it).checkWithMeshStructForCells(mst,globs);
269         break;
270       }
271     case ON_GAUSS_NE:
272       {
273         for(std::vector< MEDFileField1TSStructItem2 >::iterator it=_items.begin();it!=_items.end();it++)
274           (*it).checkWithMeshStructForGaussNE(mst,globs);
275         break;
276       }
277     case ON_GAUSS_PT:
278       {
279         for(std::vector< MEDFileField1TSStructItem2 >::iterator it=_items.begin();it!=_items.end();it++)
280           (*it).checkWithMeshStructForGaussPT(mst,globs);
281         break;
282       }
283     default:
284       throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::checkWithMeshStruct : not managed field type !");
285     }
286 }
287
288 bool MEDFileField1TSStructItem::operator==(const MEDFileField1TSStructItem& other) const throw(INTERP_KERNEL::Exception)
289 {
290   if(_type!=other._type)
291     return false;
292   if(_items.size()!=other._items.size())
293     return false;
294   for(std::size_t i=0;i<_items.size();i++)
295     if(!(_items[i]==other._items[i]))
296       return false;
297   return true;
298 }
299
300 bool MEDFileField1TSStructItem::isCellSupportEqual(const MEDFileField1TSStructItem& other) const throw(INTERP_KERNEL::Exception)
301 {
302   if(_type!=other._type)
303     return false;
304   if(_items.size()!=other._items.size())
305     return false;
306   for(std::size_t i=0;i<_items.size();i++)
307     if(!(_items[i].isCellSupportEqual(other._items[i])))
308       return false;
309   return true;
310 }
311
312 bool MEDFileField1TSStructItem::isEntityCell() const
313 {
314   if(_type==ON_NODES)
315     return false;
316   else
317     return true;
318 }
319
320 class CmpGeo
321 {
322 public:
323   CmpGeo(INTERP_KERNEL::NormalizedCellType geoTyp):_geo_type(geoTyp) { }
324   bool operator()(const std::pair< INTERP_KERNEL::NormalizedCellType, std::vector<std::size_t> > & v) const { return _geo_type==v.first; }
325 private:
326   INTERP_KERNEL::NormalizedCellType _geo_type;
327 };
328
329 MEDFileField1TSStructItem MEDFileField1TSStructItem::simplifyMeOnCellEntity(const MEDFileFieldGlobs *globs) const throw(INTERP_KERNEL::Exception)
330 {
331   if(!isEntityCell())
332     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::simplifyMeOnCellEntity : must be on ON_CELLS, ON_GAUSS_NE or ON_GAUSS_PT !");
333   std::vector< std::pair< INTERP_KERNEL::NormalizedCellType, std::vector<std::size_t> > > m;
334   std::size_t i=0;
335   for(std::vector< MEDFileField1TSStructItem2 >::const_iterator it=_items.begin();it!=_items.end();it++,i++)
336     {
337       std::vector< std::pair< INTERP_KERNEL::NormalizedCellType, std::vector<std::size_t> > >::iterator it0(std::find_if(m.begin(),m.end(),CmpGeo((*it).getGeo())));
338       if(it0==m.end())
339         m.push_back(std::pair< INTERP_KERNEL::NormalizedCellType, std::vector<std::size_t> >((*it).getGeo(),std::vector<std::size_t>(1,i)));
340       else
341         (*it0).second.push_back(i);
342     }
343   if(m.size()==_items.size())
344     {
345       MEDFileField1TSStructItem ret(*this);
346       ret._type=ON_CELLS;
347       return ret;
348     }
349   std::size_t sz(m.size());
350   std::vector< MEDFileField1TSStructItem2 > items(sz);
351   for(i=0;i<sz;i++)
352     {
353       const std::vector<std::size_t>& ids=m[i].second;
354       std::vector<const MEDFileField1TSStructItem2 *>objs(ids.size());
355       for(std::size_t j=0;j<ids.size();j++)
356         objs[j]=&_items[ids[j]];
357       items[i]=MEDFileField1TSStructItem2::BuildAggregationOf(objs,globs);
358     }
359   MEDFileField1TSStructItem ret(ON_CELLS,items);
360   ret._computed=true;
361   return ret;
362 }
363
364 /*!
365  * \a this is expected to be ON_CELLS and simplified.
366  */
367 bool MEDFileField1TSStructItem::isCompatibleWithNodesDiscr(const MEDFileField1TSStructItem& other, const MEDFileMeshStruct *meshSt, const MEDFileFieldGlobs *globs) const throw(INTERP_KERNEL::Exception)
368 {
369   if(other._type!=ON_NODES)
370     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isCompatibleWithNodesDiscr : other must be on nodes !");
371   if(other._items.size()!=1)
372     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isCompatibleWithNodesDiscr : other is on nodes but number of subparts !");
373   int theFirstLevFull;
374   bool ret0=isFullyOnOneLev(meshSt,theFirstLevFull);
375   const MEDFileField1TSStructItem2& otherNodeIt(other._items[0]);
376   if(otherNodeIt.getPflName().empty())
377     {//on all nodes
378       if(!ret0)
379         return false;
380       return theFirstLevFull==0;
381     }
382   else
383     {
384       const DataArrayInt *pfl=globs->getProfile(otherNodeIt.getPflName().c_str());
385       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> cpyPfl(pfl->deepCpy());
386       cpyPfl->sort();
387       int nbOfNodes(meshSt->getNumberOfNodes());
388       if(cpyPfl->isIdentity() && cpyPfl->getNumberOfTuples()==nbOfNodes)
389         {//on all nodes also !
390           if(!ret0)
391             return false;
392           return theFirstLevFull==0;
393         }
394       std::vector<bool> nodesFetched(nbOfNodes,false);
395       meshSt->getTheMesh()->whichAreNodesFetched(*this,globs,nodesFetched);
396       return cpyPfl->isFittingWith(nodesFetched);
397     }
398 }
399
400 bool MEDFileField1TSStructItem::isFullyOnOneLev(const MEDFileMeshStruct *meshSt, int& theFirstLevFull) const throw(INTERP_KERNEL::Exception)
401 {
402   if(_type!=ON_CELLS)
403     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isFullyOnOneLev : works only for ON_CELLS discretization !");
404   if(_items.empty())
405     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isFullyOnOneLev : items vector is empty !");
406   int nbOfLevs(meshSt->getNumberOfLevs());
407   if(nbOfLevs==0)
408     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isFullyOnOneLev : no levels in input mesh structure !");
409   std::vector<int> levs(nbOfLevs);
410   theFirstLevFull=1;
411   int nbOfGT=0;
412   std::set<INTERP_KERNEL::NormalizedCellType> gts;
413   for(std::vector< MEDFileField1TSStructItem2 >::const_iterator it=_items.begin();it!=_items.end();it++)
414     {
415       if(!(*it).getPflName().empty())
416         return false;
417       INTERP_KERNEL::NormalizedCellType gt((*it).getGeo());
418       if(gts.find(gt)!=gts.end())
419         throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::isFullyOnOneLev : internal error !");
420       gts.insert(gt);
421       int pos(meshSt->getLevelOfGeoType((*it).getGeo()));
422       levs[-pos]++;
423     }
424   for(int i=0;i<nbOfLevs;i++)
425     if(meshSt->getNumberOfGeoTypesInLev(-i)==levs[i])
426       { theFirstLevFull=-i; return true; }
427   return false;
428 }
429
430 const MEDFileField1TSStructItem2& MEDFileField1TSStructItem::operator[](std::size_t i) const throw(INTERP_KERNEL::Exception)
431 {
432   if(i<0 || i>=_items.size())
433     throw INTERP_KERNEL::Exception("MEDFileField1TSStructItem::operator[] : input is not in valid range !");
434   return _items[i];
435 }
436
437 std::size_t MEDFileField1TSStructItem::getHeapMemorySize() const
438 {
439   std::size_t ret(0);
440   for(std::vector< MEDFileField1TSStructItem2 >::const_iterator it=_items.begin();it!=_items.end();it++)
441     ret+=(*it).getHeapMemorySize();
442   ret+=_items.size()*sizeof(MEDFileField1TSStructItem2);
443   return ret;
444 }
445
446 MEDCouplingMesh *MEDFileField1TSStructItem::buildFromScratchDataSetSupportOnCells(const MEDFileMeshStruct *mst, const MEDFileFieldGlobsReal *globs) const throw(INTERP_KERNEL::Exception)
447 {
448   return 0;//tony
449 }
450
451 //=
452
453 MEDFileField1TSStruct *MEDFileField1TSStruct::New(const MEDFileAnyTypeField1TS *ref, MEDFileMeshStruct *mst) throw(INTERP_KERNEL::Exception)
454 {
455   return new MEDFileField1TSStruct(ref,mst);
456 }
457
458 MEDFileField1TSStruct::MEDFileField1TSStruct(const MEDFileAnyTypeField1TS *ref, MEDFileMeshStruct *mst)
459 {
460   _already_checked.push_back(BuildItemFrom(ref,mst));
461 }
462
463 void MEDFileField1TSStruct::checkWithMeshStruct(MEDFileMeshStruct *mst, const MEDFileFieldGlobs *globs) throw(INTERP_KERNEL::Exception)
464 {
465   if(_already_checked.empty())
466     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::checkWithMeshStruct : not correctly initialized !");
467   _already_checked.back().checkWithMeshStruct(mst,globs);
468 }
469
470 bool MEDFileField1TSStruct::isEqualConsideringThePast(const MEDFileAnyTypeField1TS *other, const MEDFileMeshStruct *mst) const throw(INTERP_KERNEL::Exception)
471 {
472   MEDFileField1TSStructItem b(BuildItemFrom(other,mst));
473   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++)
474     {
475       if((*it)==b)
476         return true;
477     }
478   return false;
479 }
480
481 /*!
482  * Not const because \a other structure will be added to the \c _already_checked attribute in case of success.
483  */
484 bool MEDFileField1TSStruct::isSupportSameAs(const MEDFileAnyTypeField1TS *other, const MEDFileMeshStruct *meshSt) throw(INTERP_KERNEL::Exception)
485 {
486   if(_already_checked.empty())
487     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::isSupportSameAs : no ref !");
488   MEDFileField1TSStructItem b(BuildItemFrom(other,meshSt));
489   if(!_already_checked[0].isEntityCell() || !b.isEntityCell())
490     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::isSupportSameAs : only available on cell entities !");
491   MEDFileField1TSStructItem other1(b.simplifyMeOnCellEntity(other->contentNotNull()));
492   int found=-1,i=0;
493   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++,i++)
494     if((*it).isComputed())
495       { found=i; break; }
496   bool ret(false);
497   if(found==-1)
498     {
499       MEDFileField1TSStructItem this1(_already_checked[0].simplifyMeOnCellEntity(other->contentNotNull()));
500       ret=this1.isCellSupportEqual(other1);
501       if(ret)
502         _already_checked.push_back(this1);
503     }
504   else
505     ret=_already_checked[found].isCellSupportEqual(other1);
506   if(ret)
507     _already_checked.push_back(b);
508   return ret;
509 }
510
511 bool MEDFileField1TSStruct::isCompatibleWithNodesDiscr(const MEDFileAnyTypeField1TS *other, const MEDFileMeshStruct *meshSt) throw(INTERP_KERNEL::Exception)
512 {
513   if(_already_checked.empty())
514     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::isCompatibleWithNodesDiscr : no ref !");
515   if(!_already_checked[0].isEntityCell())
516     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::isCompatibleWithNodesDiscr : only available on cell entities !");
517   MEDFileField1TSStructItem other1(BuildItemFrom(other,meshSt));
518   //
519   int found=-1,i=0;
520   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++,i++)
521     if((*it).isComputed())
522       { found=i; break; }
523   bool ret(false);
524   if(found==-1)
525     {
526       MEDFileField1TSStructItem this1(_already_checked[0].simplifyMeOnCellEntity(other->contentNotNull()));
527       ret=this1.isCompatibleWithNodesDiscr(other1,meshSt,other->contentNotNull());
528       if(ret)
529         _already_checked.push_back(this1);
530     }
531   else
532     ret=_already_checked[found].isCompatibleWithNodesDiscr(other1,meshSt,other->contentNotNull());
533   if(ret)
534     _already_checked.push_back(other1);
535   return ret;
536 }
537
538 std::size_t MEDFileField1TSStruct::getHeapMemorySize() const
539 {
540   std::size_t ret(0);
541   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++)
542     ret+=(*it).getHeapMemorySize();
543   ret+=_already_checked.capacity()*sizeof(MEDFileField1TSStructItem);
544   return ret;
545 }
546
547 MEDFileField1TSStructItem MEDFileField1TSStruct::BuildItemFrom(const MEDFileAnyTypeField1TS *ref, const MEDFileMeshStruct *meshSt)
548 {
549   TypeOfField atype;
550   std::vector< MEDFileField1TSStructItem2 > anItems;
551   //
552   std::vector< std::vector<std::string> > pfls,locs;
553   std::vector< std::vector<TypeOfField> > typesF;
554   std::vector<INTERP_KERNEL::NormalizedCellType> geoTypes;
555   std::vector< std::vector<std::pair<int,int> > > strtEnds=ref->getFieldSplitedByType(0,geoTypes,typesF,pfls,locs);
556   std::size_t nbOfGeoTypes(geoTypes.size());
557   if(nbOfGeoTypes==0)
558     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct : not null by empty ref  !");
559   bool isFirst=true;
560   for(std::size_t i=0;i<nbOfGeoTypes;i++)
561     {
562       std::size_t sz=typesF[i].size();
563       if(strtEnds[i].size()<1 || sz<1 || pfls[i].size()<1)
564         throw INTERP_KERNEL::Exception("MEDFileField1TSStruct : internal error #1 !");
565       //
566       if(isFirst)
567         atype=typesF[i][0];
568       isFirst=false;
569       //
570       for(std::size_t j=0;j<sz;j++)
571         {
572           if(atype==typesF[i][j])
573             anItems.push_back(MEDFileField1TSStructItem2(geoTypes[i],strtEnds[i][j],pfls[i][j],locs[i][j]));
574           else
575             throw INTERP_KERNEL::Exception("MEDFileField1TSStruct : can be applied only on single spatial discretization fields ! Call SplitPerDiscretization method !");
576         }
577     }
578   MEDFileField1TSStructItem ret(atype,anItems);
579   ret.checkWithMeshStruct(meshSt,ref->contentNotNull());
580   return ret;
581 }
582
583 MEDCouplingMesh *MEDFileField1TSStruct::buildFromScratchDataSetSupport(const MEDFileMeshStruct *mst, const MEDFileFieldGlobsReal *globs) const throw(INTERP_KERNEL::Exception)
584 {
585   if(_already_checked.empty())
586     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::buildFromScratchDataSetSupport : No outline structure in this !");
587   int pos0(-1),pos1(-1);
588   if(presenceOfCellDiscr(pos0))
589     {
590       if(!presenceOfPartialNodeDiscr(pos1))
591         return _already_checked[pos0].buildFromScratchDataSetSupportOnCells(mst,globs);
592       else
593         throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::buildFromScratchDataSetSupport : tony do it very soon !");
594     }
595   else
596     {
597       if(!presenceOfPartialNodeDiscr(pos1))
598         {//we have only all nodes, no cell definition info -> level 0;
599           return mst->getTheMesh()->getGenMeshAtLevel(0);
600         }
601     }
602 }
603
604 /*!
605  * Returns true if presence in \a this of discretization ON_CELLS, ON_GAUSS_PT, ON_GAUSS_NE.
606  * 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.
607  */
608 bool MEDFileField1TSStruct::presenceOfCellDiscr(int& pos) const throw(INTERP_KERNEL::Exception)
609 {
610   std::size_t refSz(std::numeric_limits<std::size_t>::max());
611   bool ret(false);
612   int i(0);
613   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++,i++)
614     {
615       if((*it).getType()!=ON_NODES)
616         {
617           ret=true;
618           std::size_t sz((*it).getNumberOfItems());
619           if(refSz>sz)
620             { pos=i; refSz=sz; }
621         }
622     }
623   if(refSz==0)
624     throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::presenceOfCellDiscr : an element in this on entity CELL is empty !");
625   return ret;
626 }
627
628 /*!
629  * Returns true if presence in \a this of discretization ON_NODES.
630  * If true is returned the pos of the first element containing the single subpart.
631  */
632 bool MEDFileField1TSStruct::presenceOfPartialNodeDiscr(int& pos) const throw(INTERP_KERNEL::Exception)
633 {
634   int i(0);
635   for(std::vector<MEDFileField1TSStructItem>::const_iterator it=_already_checked.begin();it!=_already_checked.end();it++,i++)
636     {
637       if((*it).getType()==ON_NODES)
638         {
639           std::size_t sz((*it).getNumberOfItems());
640           if(sz==1)
641             {
642               if(!(*it)[0].getPflName().empty())
643                 { pos=i; return true; }
644             }
645           else
646             throw INTERP_KERNEL::Exception("MEDFileField1TSStruct::presenceOfPartialNodeDiscr : an element in this on entity NODE is split into several parts !");
647         }
648     }
649   return false;
650 }
651
652 //=
653
654 MEDFileFastCellSupportComparator *MEDFileFastCellSupportComparator::New(const MEDFileMesh *m, const MEDFileAnyTypeFieldMultiTS *ref) throw(INTERP_KERNEL::Exception)
655 {
656   return new MEDFileFastCellSupportComparator(m,ref);
657 }
658
659 MEDFileFastCellSupportComparator::MEDFileFastCellSupportComparator(const MEDFileMesh *m, const MEDFileAnyTypeFieldMultiTS *ref)
660 {
661   _mesh_comp=MEDFileMeshStruct::New(m);
662   int nbPts=ref->getNumberOfTS();
663   _f1ts_cmps.resize(nbPts);
664   for(int i=0;i<nbPts;i++)
665     {
666       MEDCouplingAutoRefCountObjectPtr<MEDFileAnyTypeField1TS> elt=ref->getTimeStepAtPos(i);
667       _f1ts_cmps[i]=MEDFileField1TSStruct::New(elt,_mesh_comp);
668       _f1ts_cmps[i]->checkWithMeshStruct(_mesh_comp,elt->contentNotNull());
669     }
670 }
671
672 std::size_t MEDFileFastCellSupportComparator::getHeapMemorySize() const
673 {
674   std::size_t ret(0);
675   const MEDFileMeshStruct *mst(_mesh_comp);
676   if(mst)
677     ret+=mst->getHeapMemorySize();
678   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileField1TSStruct> >::const_iterator it=_f1ts_cmps.begin();it!=_f1ts_cmps.end();it++)
679     {
680       const MEDFileField1TSStruct *cur(*it);
681       if(cur)
682         ret+=cur->getHeapMemorySize()+sizeof(MEDFileField1TSStruct);
683     }
684   ret+=_f1ts_cmps.capacity()*sizeof(MEDCouplingAutoRefCountObjectPtr<MEDFileField1TSStruct>);
685   return ret;
686 }
687
688 bool MEDFileFastCellSupportComparator::isEqual(const MEDFileAnyTypeFieldMultiTS *other) throw(INTERP_KERNEL::Exception)
689 {
690   int nbPts=other->getNumberOfTS();
691   if(nbPts!=(int)_f1ts_cmps.size())
692     {
693       std::ostringstream oss; oss << "MEDFileFastCellSupportComparator::isEqual : unexpected nb of time steps in  input ! Should be " << _f1ts_cmps.size() << " it is in reality " << nbPts << " !";
694       throw INTERP_KERNEL::Exception(oss.str().c_str());
695     }
696   for(int i=0;i<nbPts;i++)
697     {
698       MEDCouplingAutoRefCountObjectPtr<MEDFileAnyTypeField1TS> elt=other->getTimeStepAtPos(i);
699       if(!_f1ts_cmps[i]->isEqualConsideringThePast(elt,_mesh_comp))
700         if(!_f1ts_cmps[i]->isSupportSameAs(elt,_mesh_comp))
701           return false;
702     }
703   return true;
704 }
705
706 bool MEDFileFastCellSupportComparator::isCompatibleWithNodesDiscr(const MEDFileAnyTypeFieldMultiTS *other) throw(INTERP_KERNEL::Exception)
707 {
708   int nbPts=other->getNumberOfTS();
709   if(nbPts!=(int)_f1ts_cmps.size())
710     {
711       std::ostringstream oss; oss << "MEDFileFastCellSupportComparator::isCompatibleWithNodesDiscr : unexpected nb of time steps in  input ! Should be " << _f1ts_cmps.size() << " it is in reality " << nbPts << " !";
712       throw INTERP_KERNEL::Exception(oss.str().c_str());
713     }
714   for(int i=0;i<nbPts;i++)
715     {
716       MEDCouplingAutoRefCountObjectPtr<MEDFileAnyTypeField1TS> elt=other->getTimeStepAtPos(i);
717       if(!_f1ts_cmps[i]->isCompatibleWithNodesDiscr(elt,_mesh_comp))
718         return false;
719     }
720   return true;
721 }
722
723 MEDCouplingMesh *MEDFileFastCellSupportComparator::buildFromScratchDataSetSupport(int timeStepId, const MEDFileFieldGlobsReal *globs) const throw(INTERP_KERNEL::Exception)
724 {
725   if(timeStepId<0 || timeStepId>=(int)_f1ts_cmps.size())
726     {
727       std::ostringstream oss; oss << "MEDFileFastCellSupportComparator::buildFromScratchDataSetSupport : requested time step id #" << timeStepId << " is not in [0," << _f1ts_cmps.size() << ") !";
728       throw INTERP_KERNEL::Exception(oss.str().c_str());
729     }
730   const MEDFileField1TSStruct *obj(_f1ts_cmps[timeStepId]);
731   if(!obj)
732     {
733       std::ostringstream oss; oss << "MEDFileFastCellSupportComparator::buildFromScratchDataSetSupport : at time step id #" << timeStepId << " no field structure overview defined !";
734       throw INTERP_KERNEL::Exception(oss.str().c_str());
735     }
736   return obj->buildFromScratchDataSetSupport(_mesh_comp,globs);
737 }