]> SALOME platform Git repositories - tools/medcoupling.git/blob - src/MEDLoader/MEDFileEquivalence.cxx
Salome HOME
Implementation of equivalences in MEDFileMesh.
[tools/medcoupling.git] / src / MEDLoader / MEDFileEquivalence.cxx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
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 (EDF R&D)
20
21 #include "MEDFileEquivalence.hxx"
22 #include "MEDFileSafeCaller.txx"
23 #include "MEDCouplingMemArray.hxx"
24 #include "MEDLoaderBase.hxx"
25 #include "MEDFileMesh.hxx"
26 #include "InterpKernelAutoPtr.hxx"
27
28 extern med_geometry_type typmai[MED_N_CELL_FIXED_GEO];
29 extern INTERP_KERNEL::NormalizedCellType typmai2[MED_N_CELL_FIXED_GEO];
30 extern med_geometry_type typmai3[34];
31 extern med_geometry_type typmainoeud[1];
32
33 using namespace ParaMEDMEM;
34
35 MEDFileEquivalencePair *MEDFileEquivalencePair::Load(MEDFileEquivalences *father, med_idt fid, const std::string& name, const std::string &desc)
36 {
37   if(!father)
38     throw INTERP_KERNEL::Exception("MEDFileEquivalencePair::Load : father is NULL ! Should not !");
39   MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalencePair> ret(new MEDFileEquivalencePair(father,name,desc));
40   ret->load(fid);
41   return ret.retn();
42 }
43
44 void MEDFileEquivalencePair::write(med_idt fid) const
45 {
46   std::string meshName(getFather()->getMeshName());
47   INTERP_KERNEL::AutoPtr<char> meshName2(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE));
48   INTERP_KERNEL::AutoPtr<char> name(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE));
49   INTERP_KERNEL::AutoPtr<char> desc(MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE));
50   MEDLoaderBase::safeStrCpy(meshName.c_str(),MED_NAME_SIZE,meshName2,getFather()->getMesh()->getTooLongStrPolicy());
51   MEDLoaderBase::safeStrCpy(_name.c_str(),MED_NAME_SIZE,name,getFather()->getMesh()->getTooLongStrPolicy());
52   MEDLoaderBase::safeStrCpy(_description.c_str(),MED_COMMENT_SIZE,desc,getFather()->getMesh()->getTooLongStrPolicy());
53   MEDFILESAFECALLERWR0(MEDequivalenceCr,(fid,meshName2,name,desc));
54   const MEDFileEquivalenceCell *cell(_cell);
55   if(cell)
56     cell->write(fid);
57   const MEDFileEquivalenceNode *node(_node);
58   if(node)
59     node->write(fid);
60 }
61
62 const MEDFileMesh *MEDFileEquivalencePair::getMesh() const
63 {
64   return getFather()->getMesh();
65 }
66
67 MEDFileMesh *MEDFileEquivalencePair::getMesh()
68 {
69   return getFather()->getMesh();
70 }
71
72 MEDFileEquivalencePair *MEDFileEquivalencePair::deepCpy(MEDFileEquivalences *father) const
73 {
74   MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalencePair> ret(new MEDFileEquivalencePair(father,_name,_description));
75   const MEDFileEquivalenceCell *cell(_cell);
76   if(cell)
77     ret->_cell=cell->deepCpy(const_cast<MEDFileEquivalencePair *>(this));
78   const MEDFileEquivalenceNode *node(_node);
79   if(node)
80     ret->_node=node->deepCpy(const_cast<MEDFileEquivalencePair *>(this));
81   return ret.retn();
82 }
83
84 bool MEDFileEquivalencePair::isEqual(const MEDFileEquivalencePair *other, std::string& what) const
85 {
86   if(_name!=other->_name)
87     {
88       std::ostringstream oss; oss << "Names differs : " << _name << " != " << other->_name << " !";
89       what=oss.str();
90       return false;
91     }
92   if(_description!=other->_description)
93     {
94        std::ostringstream oss; oss << "Description differs : " << _description << " != " << other->_description << " !";
95        what=oss.str();
96        return false;
97     }
98   const MEDFileEquivalenceCell *c1(_cell),*c2(other->_cell);
99   if((c1 && !c2) || (!c1 && c2))
100     {
101       std::ostringstream oss; oss << "Cell def of Equiv " << _name << " are defined for this and not for other (or reversely) !";
102       what=oss.str();
103       return false;
104     }
105   if(c1 && c2)
106     if(!c1->isEqual(c2,what))
107       return false;
108   const MEDFileEquivalenceNode *n1(_node),*n2(other->_node);
109   if((n1 && !n2) || (!n1 && n2))
110     {
111       std::ostringstream oss; oss << "Node def of Equiv " << _name << " are defined for this and not for other (or reversely) !";
112       what=oss.str();
113       return false;
114     }
115   if(n1 && n2)
116     if(!n1->isEqual(n2,what))
117       return false;
118   return true;
119 }
120
121 void MEDFileEquivalencePair::getRepr(std::ostream& oss) const
122 {
123   const MEDFileEquivalenceNode *node(_node);
124   const MEDFileEquivalenceCell *cell(_cell);
125   oss << std::endl << "  name of equivalence : " << _name << std::endl;
126   oss << "  description of equivalence : " << _description << std::endl;
127   oss << "  Node : ";
128   if(!node)
129     oss << "None" << std::endl;
130   else
131     node->getRepr(oss);
132   oss << "  Cell : ";
133   if(!cell)
134     oss << "None" << std::endl;
135   else
136     cell->getRepr(oss);
137 }
138
139 MEDFileEquivalencePair *MEDFileEquivalencePair::New(MEDFileEquivalences *father, const std::string& name)
140 {
141   return new MEDFileEquivalencePair(father,name,std::string());
142 }
143
144 std::vector<const BigMemoryObject *> MEDFileEquivalencePair::getDirectChildrenWithNull() const
145 {
146   std::vector<const BigMemoryObject *> ret(2);
147   ret[0]=_cell; ret[1]=_node;
148   return ret;
149 }
150
151 void MEDFileEquivalencePair::setArray(int meshDimRelToMaxExt, DataArrayInt *da)
152 {
153   if(meshDimRelToMaxExt>1)
154     throw INTERP_KERNEL::Exception("MEDFileEquivalencePair::setArray : meshDimRelToMaxExt must be in [1,0,-1,-2,-3] at most !");
155   if(meshDimRelToMaxExt==1)
156     {
157       MEDFileEquivalenceNode *node(_node);
158       if(!node)
159         {
160           _node=new MEDFileEquivalenceNode(this,0);
161           node=_node;
162         }
163       node->setArray(da);
164     }
165   else
166     {
167       MEDFileEquivalenceCell *cell(_cell);
168       if(!cell)
169         {
170           _cell=new MEDFileEquivalenceCell(this);
171           cell=_cell;
172         }
173       cell->setArray(meshDimRelToMaxExt,da);
174     }
175 }
176
177 /*!
178  * The returned pointer is a borrowed pointer.
179  */
180 MEDFileEquivalenceCell *MEDFileEquivalencePair::initCell()
181 {
182   _cell=new MEDFileEquivalenceCell(this);
183   return _cell;
184 }
185
186 /*!
187  * The returned pointer is a borrowed pointer.
188  */
189 MEDFileEquivalenceNode *MEDFileEquivalencePair::initNode()
190 {
191   _node=new MEDFileEquivalenceNode(this,0);
192   return _node;
193 }
194
195 std::size_t MEDFileEquivalencePair::getHeapMemorySizeWithoutChildren() const
196 {
197   return 0;
198 }
199
200 void MEDFileEquivalencePair::load(med_idt fid)
201 {
202   std::string meshName(_father->getMeshName());
203   int dt,it;
204   _father->getDtIt(dt,it);
205   med_int ncor;
206   MEDFILESAFECALLERRD0(MEDequivalenceCorrespondenceSize,(fid,meshName.c_str(),_name.c_str(),dt,it,MED_NODE,MED_NONE,&ncor));
207   if(ncor>0)
208     {
209       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da(DataArrayInt::New());
210       da->alloc(ncor*2);
211       MEDFILESAFECALLERRD0(MEDequivalenceCorrespondenceRd,(fid,meshName.c_str(),_name.c_str(),dt,it,MED_NODE,MED_NONE,da->getPointer()));
212       da->applyLin(1,-1);
213       da->rearrange(2);
214       MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalenceNode> node(new MEDFileEquivalenceNode(this,da));
215       _node=node;
216     }
217   _cell=MEDFileEquivalenceCell::Load(fid,this);
218 }
219
220 std::vector<const BigMemoryObject *> MEDFileEquivalences::getDirectChildrenWithNull() const
221 {
222   std::size_t sz(_equ.size());
223   std::vector<const BigMemoryObject *> ret(sz);
224   for(std::size_t i=0;i<sz;i++)
225     ret[i]=_equ[i];
226   return ret;
227 }
228
229 std::size_t MEDFileEquivalences::getHeapMemorySizeWithoutChildren() const
230 {
231   return sizeof(MEDFileEquivalences)+_equ.capacity()*sizeof(MEDFileEquivalencePair);
232 }
233
234 void MEDFileEquivalences::getDtIt(int &dt, int &it) const
235 {
236   dt=_owner->getIteration(); it=_owner->getOrder();
237 }
238
239 std::string MEDFileEquivalences::getMeshName() const
240 {
241   return _owner->getName();
242 }
243
244 void MEDFileEquivalences::pushEquivalence(MEDFileEquivalencePair *elt)
245 {
246   MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalencePair> elta(elt);
247   if(elt)
248     elt->incrRef();
249   _equ.push_back(elta);
250 }
251
252 MEDFileEquivalencePair *MEDFileEquivalences::getEquivalence(int i)
253 {
254   int sz(size());
255   if(i<0 || i>=sz)
256     {
257       std::ostringstream oss; oss << "MEDFileEquivalences::getEquivalence : invalid id ! Must be in [0," << sz << ") !";
258       throw INTERP_KERNEL::Exception(oss.str().c_str());
259     }
260   return _equ[i];
261 }
262
263 MEDFileEquivalencePair *MEDFileEquivalences::getEquivalenceWithName(const std::string& name)
264 {
265   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalencePair> >::iterator it=_equ.begin();it!=_equ.end();it++)
266     {
267       MEDFileEquivalencePair *elt(*it);
268       if(elt)
269         {
270           if(elt->getName()==name)
271             return elt;
272         }
273     }
274   std::ostringstream oss; oss << "MEDFileEquivalences::getEquivalenceWithName : no equivalence with name \"" << name << "\" ! Must be in [ ";
275   std::vector<std::string> eqs(getEquivalenceNames());
276   std::copy(eqs.begin(),eqs.end(),std::ostream_iterator<std::string>(oss,", "));
277   oss << "] !";
278   throw INTERP_KERNEL::Exception(oss.str().c_str());
279 }
280
281 int MEDFileEquivalences::size() const
282 {
283   return _equ.size();
284 }
285
286 std::vector<std::string> MEDFileEquivalences::getEquivalenceNames() const
287 {
288   std::vector<std::string> ret;
289   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalencePair> >::const_iterator it=_equ.begin();it!=_equ.end();it++)
290     {
291       const MEDFileEquivalencePair *elt(*it);
292       if(elt)
293         {
294           ret.push_back(elt->getName());
295         }
296     }
297   return ret;
298 }
299
300 MEDFileEquivalencePair *MEDFileEquivalences::appendEmptyEquivalenceWithName(const std::string& name)
301 {
302   MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalencePair> elt(MEDFileEquivalencePair::New(this,name));
303   _equ.push_back(elt);
304   return elt;
305 }
306
307 MEDFileEquivalences *MEDFileEquivalences::deepCpy(MEDFileMesh *owner) const
308 {
309   MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalences> ret(new MEDFileEquivalences(owner));
310   ret->deepCpyFrom(*this);
311   return ret.retn();
312 }
313
314 bool MEDFileEquivalences::isEqual(const MEDFileEquivalences *other, std::string& what) const
315 {
316   std::size_t sz(_equ.size());
317   if(sz!=other->_equ.size())
318     {
319       what="Equivalences differs : not same number !";
320       return false;
321     }
322   for(std::size_t i=0;i<sz;i++)
323     {
324       const MEDFileEquivalencePair *thisp(_equ[i]),*otherp(other->_equ[i]);
325       if(!thisp && !otherp)
326         continue;
327       if(thisp && otherp)
328         {
329           if(!thisp->isEqual(otherp,what))
330             {
331               std::ostringstream oss; oss << "At Eq #" << i << " there is a difference !";
332               what=oss.str()+what;
333               return false;
334             }
335         }
336       else
337         {
338           std::ostringstream oss; oss << "At Eq #" << i << " defined in this not is other (or reversely) !";
339           what=oss.str()+what;
340           return false;
341         }
342     }
343 }
344
345 void MEDFileEquivalences::getRepr(std::ostream& oss) const
346 {
347   std::size_t ii(0);
348   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalencePair> >::const_iterator it=_equ.begin();it!=_equ.end();it++,ii++)
349     {
350       const MEDFileEquivalencePair *elt(*it);
351       oss << "Equivalence #" << ii << " : " ;
352       if(elt)
353         elt->getRepr(oss);
354       else
355         oss << "None" << std::endl;
356     }
357 }
358
359 void MEDFileEquivalences::killEquivalenceWithName(const std::string& name)
360 {
361   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalencePair> >::iterator it(_equ.begin());
362   for(;it!=_equ.end();it++)
363     {
364       const MEDFileEquivalencePair *elt(*it);
365       if(elt && elt->getName()==name)
366         break;
367     }
368   if(it==_equ.end())
369     {
370       std::ostringstream oss; oss << "MEDFileEquivalences::killEquivalenceWithName : Equivalence with name \"" << name << "\" not found !";
371       throw INTERP_KERNEL::Exception(oss.str().c_str());
372     }
373   _equ.erase(it);
374 }
375
376 void MEDFileEquivalences::killEquivalenceAt(int i)
377 {
378   int sz(size());
379   if(i<0 || i>=sz)
380     {
381       std::ostringstream oss; oss << "MEDFileEquivalences::killEquivalenceAt : Id must be in [0," << sz << ") !";
382       throw INTERP_KERNEL::Exception(oss.str().c_str());
383     }
384   std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalencePair> >::iterator it(_equ.begin());
385   for(int j=0;j<i;it++,j++);
386   _equ.erase(it);
387 }
388
389 void MEDFileEquivalences::clear()
390 {
391   _equ.clear();
392 }
393
394 void MEDFileEquivalences::write(med_idt fid) const
395 {
396   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalencePair> >::const_iterator it=_equ.begin();it!=_equ.end();it++)
397     {
398       const MEDFileEquivalencePair *elt(*it);
399       if(elt)
400         elt->write(fid);
401     }
402 }
403
404 int MEDFileEquivalences::PresenceOfEquivalences(med_idt fid, const std::string& meshName)
405 {
406   med_int nequ(MEDnEquivalence(fid,meshName.c_str()));
407   return nequ;
408 }
409
410 MEDFileEquivalences *MEDFileEquivalences::Load(med_idt fid, int nbOfEq, MEDFileMesh *owner)
411 {
412   MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalences> ret(new MEDFileEquivalences(owner));
413   if(!owner)
414     throw INTERP_KERNEL::Exception("MEDFileEquivalences::Load : owner is NULL !");
415   std::string meshName(owner->getName());
416   for(int i=0;i<nbOfEq;i++)
417     {
418       INTERP_KERNEL::AutoPtr<char> equ(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE));
419       INTERP_KERNEL::AutoPtr<char> desc(MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE));
420       int nstep,nocstpncor;
421       MEDFILESAFECALLERRD0(MEDequivalenceInfo,(fid,meshName.c_str(),i+1,equ,desc,&nstep,&nocstpncor));
422       std::string eqName(MEDLoaderBase::buildStringFromFortran(equ,MED_NAME_SIZE)),eqDescName(MEDLoaderBase::buildStringFromFortran(desc,MED_COMMENT_SIZE));
423       MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalencePair> eqv(MEDFileEquivalencePair::Load(ret,fid,eqName,eqDescName));
424       ret->pushEquivalence(eqv);
425     }
426   return ret.retn();
427 }
428
429 void MEDFileEquivalences::CheckDataArray(const DataArrayInt *data)
430 {
431   if(!data)
432     return;
433   data->checkAllocated();
434   if(data->getNumberOfComponents()!=2)
435     {
436       std::ostringstream oss; oss << "MEDFileEquivalences::CheckDataArray : Input DataArray must have 2 components !";
437       throw INTERP_KERNEL::Exception(oss.str().c_str());
438     }
439 }
440
441 void MEDFileEquivalences::deepCpyFrom(const MEDFileEquivalences& other)
442 {
443   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalencePair> >::const_iterator it=other._equ.begin();it!=other._equ.end();it++)
444     {
445       const MEDFileEquivalencePair *elt(*it);
446       MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalencePair> eltCpy;
447       if(elt)
448         {
449           eltCpy=elt->deepCpy(this);
450         }
451       _equ.push_back(eltCpy);
452     }
453 }
454
455 MEDFileEquivalenceBase::MEDFileEquivalenceBase(MEDFileEquivalencePair *father):_father(father)
456 {
457 }
458
459 MEDFileEquivalenceData::MEDFileEquivalenceData(MEDFileEquivalencePair *owner, DataArrayInt *data):MEDFileEquivalenceBase(owner),_data(data)
460 {
461   if(data)
462     data->incrRef();
463 }
464
465 void MEDFileEquivalenceData::setArray(DataArrayInt *data)
466 {
467   MEDFileEquivalences::CheckDataArray(data);
468   _data=data;
469   if(data)
470     data->incrRef();
471 }
472
473 std::vector<const BigMemoryObject *> MEDFileEquivalenceData::getDirectChildrenWithNull() const
474 {
475   std::vector<const BigMemoryObject *> ret(1);
476   ret[0]=_data;
477   return ret;
478 }
479
480 bool MEDFileEquivalenceData::isEqual(const MEDFileEquivalenceData *other, std::string& what) const
481 {
482   const DataArrayInt *d1(_data),*d2(other->_data);
483   if((!d1 && d2) || (d1 && !d2))
484     {
485       what="Data array is defined in this not in other (or reversely) !";
486       return false;
487     }
488   if(d1 && d2)
489     {
490       if(!d1->isEqualIfNotWhy(*d2,what))
491         return false;
492     }
493   return true;
494 }
495
496 void MEDFileEquivalenceData::writeLL(med_idt fid, med_entity_type medtype, med_geometry_type medgt) const
497 {
498   
499   const DataArrayInt *da(getArray());
500   if(!da)
501     return ;
502   MEDFileEquivalences::CheckDataArray(da);
503   const MEDFileMesh *mesh(getFather()->getMesh());
504   int dt,it;
505   mesh->getTime(dt,it);
506   std::string meshName(mesh->getName());
507   std::string equName(getFather()->getName());
508   INTERP_KERNEL::AutoPtr<char> meshName2(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE));
509   INTERP_KERNEL::AutoPtr<char> name(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE));
510   MEDLoaderBase::safeStrCpy(meshName.c_str(),MED_NAME_SIZE,meshName2,getFather()->getMesh()->getTooLongStrPolicy());
511   MEDLoaderBase::safeStrCpy(equName.c_str(),MED_NAME_SIZE,name,getFather()->getMesh()->getTooLongStrPolicy());
512   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da2(da->deepCpy()); da2->rearrange(1); da2->applyLin(1,1); da2->rearrange(2);
513   MEDFILESAFECALLERWR0(MEDequivalenceCorrespondenceWr,(fid,meshName2,name,dt,it,medtype,medgt,da2->getNumberOfTuples(),da2->begin()));
514 }
515
516 std::size_t MEDFileEquivalenceCellType::getHeapMemorySizeWithoutChildren() const
517 {
518   return sizeof(MEDFileEquivalenceCellType);
519 }
520
521 MEDFileEquivalenceCellType *MEDFileEquivalenceCellType::deepCpy(MEDFileEquivalencePair *owner) const
522 {
523   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da;
524   if(getArray())
525     da=getArray()->deepCpy();
526   return new MEDFileEquivalenceCellType(owner,_type,da);
527 }
528
529 bool MEDFileEquivalenceCellType::isEqual(const MEDFileEquivalenceCellType *other, std::string& what) const
530 {
531   if(_type!=other->_type)
532     {
533       what="Geo types differs !";
534       return false;
535     }
536   return MEDFileEquivalenceData::isEqual(other,what);
537 }
538
539 void MEDFileEquivalenceCellType::getRepr(std::ostream& oss) const
540 {
541   const INTERP_KERNEL::CellModel& cm(INTERP_KERNEL::CellModel::GetCellModel(_type));
542   const DataArrayInt *da(getArray());
543   oss << cm.getRepr() << ":";
544   if(da)
545     oss << da->getNumberOfTuples() << " tuples";
546   else
547     oss << "no dataarray";
548   oss << ",";
549 }
550
551 void MEDFileEquivalenceCellType::write(med_idt fid) const
552 {
553   writeLL(fid,MED_CELL,typmai3[_type]);
554 }
555
556 std::vector<const BigMemoryObject *> MEDFileEquivalenceCell::getDirectChildrenWithNull() const
557 {
558   std::size_t sz(_types.size());
559   std::vector<const BigMemoryObject *> ret(sz);
560   for(std::size_t i=0;i<sz;i++)
561     ret[i]=_types[i];
562   return ret;
563 }
564
565 std::size_t MEDFileEquivalenceCell::getHeapMemorySizeWithoutChildren() const
566 {
567   return sizeof(MEDFileEquivalenceCell)+_types.capacity()*sizeof(MEDFileEquivalenceCellType);
568 }
569
570 MEDFileEquivalenceCell *MEDFileEquivalenceCell::Load(med_idt fid, MEDFileEquivalencePair *owner)
571 {
572   MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalenceCell> ret(new MEDFileEquivalenceCell(owner));
573   ret->load(fid);
574   if(ret->size()>0)
575     return ret.retn();
576   else
577     return 0;
578 }
579
580 void MEDFileEquivalenceCell::write(med_idt fid) const
581 {
582   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalenceCellType> >::const_iterator it=_types.begin();it!=_types.end();it++)
583     {
584       const MEDFileEquivalenceCellType *ct(*it);
585       if(ct)
586         ct->write(fid);
587     }
588 }
589
590 MEDFileEquivalenceCell *MEDFileEquivalenceCell::deepCpy(MEDFileEquivalencePair *owner) const
591 {
592   MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalenceCell> ret(new MEDFileEquivalenceCell(owner));
593   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalenceCellType> >::const_iterator it=_types.begin();it!=_types.end();it++)
594     {
595       const MEDFileEquivalenceCellType *elt(*it);
596       MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalenceCellType> eltCpy;
597       if(elt)
598         eltCpy=elt->deepCpy(owner);
599       ret->_types.push_back(eltCpy);
600     }
601   return ret.retn();
602 }
603
604 bool MEDFileEquivalenceCell::isEqual(const MEDFileEquivalenceCell *other, std::string& what) const
605 {
606   std::size_t sz(_types.size());
607   if(sz!=other->_types.size())
608     {
609       std::ostringstream oss; oss << "Nb of geo types differs : " << sz << " != " << other->_types.size();
610       what=oss.str();
611       return false;
612     }
613   for(std::size_t i=0;i<sz;i++)
614     {
615       const MEDFileEquivalenceCellType *ct1(_types[i]),*ct2(other->_types[i]);
616       if((ct1 && !ct2) || (!ct1 && ct2))
617         {
618           std::ostringstream oss; oss << "At gt #" << i << " this is defined not other (or reversely !)";
619           what=oss.str();
620           return false;
621         }
622       if(ct1 && ct2)
623         {
624           if(!ct1->isEqual(ct2,what))
625             {
626               std::ostringstream oss; oss << "At gt #" << i << " of Eq " << getFather()->getName() << " it differs !";
627               what=oss.str()+what;
628               return false;
629             }
630         }
631     }
632   return true;
633 }
634
635 void MEDFileEquivalenceCell::getRepr(std::ostream& oss) const
636 {
637   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalenceCellType> >::const_iterator it=_types.begin();it!=_types.end();it++)
638     {
639       const MEDFileEquivalenceCellType *elt(*it);
640       if(elt)
641         elt->getRepr(oss);
642     }
643 }
644
645 DataArrayInt *MEDFileEquivalenceCell::getArray(INTERP_KERNEL::NormalizedCellType type)
646 {
647   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalenceCellType> >::iterator it=_types.begin();it!=_types.end();it++)
648     {
649       MEDFileEquivalenceCellType *elt(*it);
650       if(elt && elt->getType()==type)
651         return elt->getArray();
652     }
653   std::ostringstream oss; oss << "MEDFileEquivalenceCell::getArray : In Equivalence \"" << getFather()->getName() << "\" the geotype " << type << " is not available !";
654   throw INTERP_KERNEL::Exception(oss.str().c_str());
655 }
656
657 void MEDFileEquivalenceCell::setArray(int meshDimRelToMax, DataArrayInt *da)
658 {
659   if(!da)
660     return ;
661   MEDFileEquivalences::CheckDataArray(da);
662   MEDFileMesh *mm(getMesh());
663   int totalNbOfCells(mm->getNumberOfCellsAtLevel(meshDimRelToMax));
664   //
665   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> tmp(da->deepCpy()); tmp->rearrange(1);
666   int maxv,minv;
667   tmp->getMinMaxValues(minv,maxv);
668   if((minv<0 || minv>=totalNbOfCells) || (maxv<0 || maxv>=totalNbOfCells))
669     {
670       std::ostringstream oss; oss << "MEDFileEquivalenceCell::setArray : Input 2 component DataArray has incorrect values ! all values must be in [0," << totalNbOfCells << ") !";
671       throw INTERP_KERNEL::Exception(oss.str().c_str());
672     }
673   //
674   std::vector<INTERP_KERNEL::NormalizedCellType> gts(mm->getGeoTypesAtLevel(meshDimRelToMax));
675   int startId(0),endId;
676   std::vector<int> compS(1,0);
677   for(std::vector<INTERP_KERNEL::NormalizedCellType>::const_iterator it=gts.begin();it!=gts.end();it++)
678     {
679       endId=startId+mm->getNumberOfCellsWithType(*it);
680       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da0(da->keepSelectedComponents(compS));
681       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ids(da0->getIdsInRange(startId,endId));
682       MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da1(da->selectByTupleIdSafe(ids->begin(),ids->end()));
683       da1->applyLin(1,-startId);
684       setArrayForType(*it,da1);
685       startId=endId;
686     }
687 }
688
689 void MEDFileEquivalenceCell::setArrayForType(INTERP_KERNEL::NormalizedCellType type, DataArrayInt *da)
690 {
691   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalenceCellType> >::iterator it=_types.begin();it!=_types.end();it++)
692     {
693       MEDFileEquivalenceCellType *elt(*it);
694       if(elt && elt->getType()==type)
695         {
696           elt->setArray(da);
697           return ;
698         }
699     }
700   MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalenceCellType> newElt(new MEDFileEquivalenceCellType(getFather(),type,da));
701   _types.push_back(newElt);
702 }
703
704 std::vector<INTERP_KERNEL::NormalizedCellType> MEDFileEquivalenceCell::getTypes() const
705 {
706   std::vector<INTERP_KERNEL::NormalizedCellType> ret;
707   for(std::vector< MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalenceCellType> >::const_iterator it=_types.begin();it!=_types.end();it++)
708     {
709       const MEDFileEquivalenceCellType *elt(*it);
710       if(elt)
711         ret.push_back(elt->getType());
712     }
713   return ret;
714 }
715
716 void MEDFileEquivalenceCell::load(med_idt fid)
717 {
718   std::string meshName(getFather()->getFather()->getMeshName()),name(getName());
719   int dt,it;
720   getFather()->getFather()->getDtIt(dt,it);
721   for(int i=0;i<MED_N_CELL_FIXED_GEO;i++)
722     {
723       med_int ncor;
724       MEDFILESAFECALLERRD0(MEDequivalenceCorrespondenceSize,(fid,meshName.c_str(),name.c_str(),dt,it,MED_CELL,typmai[i],&ncor));
725       if(ncor>0)
726         {
727           MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da(DataArrayInt::New());
728           da->alloc(ncor*2);
729           MEDFILESAFECALLERRD0(MEDequivalenceCorrespondenceRd,(fid,meshName.c_str(),name.c_str(),dt,it,MED_CELL,typmai[i],da->getPointer()));
730           da->applyLin(1,-1);
731           da->rearrange(2);
732           MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalenceCellType> ct(new MEDFileEquivalenceCellType(getFather(),typmai2[i],da));
733           _types.push_back(ct);
734         }
735     }
736 }
737
738 std::size_t MEDFileEquivalenceNode::getHeapMemorySizeWithoutChildren() const
739 {
740   return sizeof(MEDFileEquivalenceNode);
741 }
742
743 void MEDFileEquivalenceNode::write(med_idt fid) const
744 {
745   writeLL(fid,MED_NODE,MED_NONE);
746 }
747
748 MEDFileEquivalenceNode *MEDFileEquivalenceNode::deepCpy(MEDFileEquivalencePair *owner) const
749 {
750   MEDCouplingAutoRefCountObjectPtr<DataArrayInt> da;
751   if(getArray())
752     da=getArray()->deepCpy();
753   MEDCouplingAutoRefCountObjectPtr<MEDFileEquivalenceNode> ret(new MEDFileEquivalenceNode(owner,da));
754   return ret.retn();
755 }
756
757 bool MEDFileEquivalenceNode::isEqual(const MEDFileEquivalenceNode *other, std::string& what) const
758 {
759   return MEDFileEquivalenceData::isEqual(other,what);
760 }
761
762 void MEDFileEquivalenceNode::getRepr(std::ostream& oss) const
763 {
764   const DataArrayInt *da(getArray());
765   if(!da)
766     oss << " No dataarray defined !" << std::endl;
767   else
768     oss << da->getNumberOfTuples() << " tuples in node equivalence." << std::endl;
769 }