Salome HOME
Step 8 : meshes have been managed, fields still remaining.
[tools/medcoupling.git] / src / MEDLoader / MEDFileJoint.cxx
1 // Copyright (C) 2007-2016  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
20 #include "MEDFileJoint.hxx"
21 #include "MEDLoader.hxx"
22 #include "MEDLoaderBase.hxx"
23 #include "MEDFileSafeCaller.txx"
24
25 #include "CellModel.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
32 using namespace MEDCoupling;
33
34 std::size_t MEDFileJointCorrespondence::getHeapMemorySizeWithoutChildren() const
35 {
36   return sizeof(MCAuto<DataArrayInt>);
37 }
38
39 std::vector<const BigMemoryObject *> MEDFileJointCorrespondence::getDirectChildrenWithNull() const
40 {
41   return std::vector<const BigMemoryObject *>();
42 }
43
44 MEDFileJointCorrespondence::MEDFileJointCorrespondence():
45   _is_nodal( true ),
46   _loc_geo_type( INTERP_KERNEL::NORM_ERROR ),
47   _rem_geo_type( INTERP_KERNEL::NORM_ERROR )
48 {
49 }
50
51 /*!
52  * Constructor.
53  *  \param [in] correspondence - correspondence.
54  *  \param [in] is_nodal - is the correspondence of cells or nodes.
55  *  \param [in] loc_geo_type - the local geometry type of correspondence.
56  *  \param [in] rem_geo_type - the remote geometry type of correspondence.
57  */
58 MEDFileJointCorrespondence::MEDFileJointCorrespondence(DataArrayInt* correspondence,
59                                                        bool          isNodal,
60                                                        INTERP_KERNEL::NormalizedCellType loc_geo_type,
61                                                        INTERP_KERNEL::NormalizedCellType rem_geo_type):
62   _is_nodal( isNodal ),
63   _loc_geo_type( loc_geo_type ),
64   _rem_geo_type( rem_geo_type )
65 {
66   MEDFileJointCorrespondence::setCorrespondence( correspondence );
67 }
68
69 MEDFileJointCorrespondence* MEDFileJointCorrespondence::New(DataArrayInt* correspondence,
70                                                             INTERP_KERNEL::NormalizedCellType loc_geo_type,
71                                                             INTERP_KERNEL::NormalizedCellType rem_geo_type)
72 {
73   return new MEDFileJointCorrespondence(correspondence, /*isNodal=*/false, loc_geo_type, rem_geo_type );
74 }
75
76 /*!
77  * Returns a new MEDFileJointCorrespondence of nodes
78  */
79 MEDFileJointCorrespondence *MEDFileJointCorrespondence::New(DataArrayInt* correspondence)
80 {
81   return new MEDFileJointCorrespondence(correspondence);
82 }
83
84 /*!
85  * Returns a new undefined MEDFileJointCorrespondence
86  */
87
88 MEDFileJointCorrespondence *MEDFileJointCorrespondence::New()
89 {
90   return new MEDFileJointCorrespondence();
91 }
92
93 /*!
94  * Writes \a this joint into a MED file specified by its name.
95  *  \param [in] fileName - the MED file name.
96  *  \param [in] mode - the writing mode. For more on \a mode, see \ref AdvMEDLoaderBasics.
97  *          - 2 - erase; an existing file is removed.
98  *          - 1 - append; same data should not be present in an existing file.
99  *          - 0 - overwrite; same data present in an existing file is overwritten.
100  *  \param [in] order - order.
101  *  \param [in] iteration - iteration.
102  *  \throw If the mesh name is not set.
103  *  \throw If \a mode == 1 and the same data is present in an existing file.
104  */
105 void MEDFileJointCorrespondence::write(const std::string& fileName, int mode, const std::string& localMeshName, const std::string& jointName, int order, int iteration) const
106 {
107   med_access_mode medmod=MEDFileUtilities::TraduceWriteMode(mode);
108   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),medmod);
109
110   std::ostringstream oss; oss << "MEDFileJointCorrespondence : error on attempt to write in file : \"" << fileName << "\"";
111   MEDFileUtilities::CheckMEDCode(fid,fid,oss.str());
112
113   if (( !_is_nodal ) &&
114       ( _loc_geo_type == INTERP_KERNEL::NORM_ERROR ||
115         _rem_geo_type == INTERP_KERNEL::NORM_ERROR ))
116     {
117       throw INTERP_KERNEL::Exception( "Geometric type not specified for a cell Joint" );
118     }
119
120   if ( (const DataArrayInt *)_correspondence )
121     {
122       writeLL(fid, localMeshName, jointName, order, iteration);
123     }
124   else
125     {
126       throw INTERP_KERNEL::Exception("MEDFileJointCorrespondence::write : correspondence array not defined");
127     }
128 }
129
130 void MEDFileJointCorrespondence::writeLL(med_idt fid, const std::string& localMeshName, const std::string& jointName, int order, int iteration) const
131 {
132   if ( _is_nodal )
133     {
134       MEDFILESAFECALLERWR0(MEDsubdomainCorrespondenceWr,(fid, localMeshName.c_str(), jointName.c_str(),
135                                                          order, iteration,
136                                                          MED_NODE, MED_NONE,
137                                                          MED_NODE, MED_NONE,
138                                                          _correspondence->getNbOfElems()/2,
139                                                          _correspondence->getConstPointer()));
140     }
141   else
142     {
143       MEDFILESAFECALLERWR0(MEDsubdomainCorrespondenceWr,(fid, localMeshName.c_str(), jointName.c_str(),
144                                                          order, iteration,
145                                                          MED_CELL, typmai3[ _loc_geo_type ],
146                                                          MED_CELL, typmai3[ _rem_geo_type ],
147                                                          _correspondence->getNbOfElems()/2,
148                                                          _correspondence->getConstPointer()));
149     }
150 }
151
152 void MEDFileJointCorrespondence::setCorrespondence(DataArrayInt *corr)
153 {
154   _correspondence=corr;
155   if ( corr )
156     corr->incrRef();
157 }
158
159 /*!
160  * Checks if \a this and another mesh are equal.
161  *  \param [in] other - the mesh to compare with.
162  *  \return bool - \c true if the meshes are equal, \c false, else.
163  */
164 bool MEDFileJointCorrespondence::isEqual(const MEDFileJointCorrespondence *other) const
165 {
166   if(_is_nodal!=other->_is_nodal)
167     return false;
168   if(_loc_geo_type!=other->_loc_geo_type)
169     return false;
170   if(_rem_geo_type!=other->_rem_geo_type)
171     return false;
172   if(!_correspondence->isEqual(*other->_correspondence))
173     return false;
174   return true;
175 }
176
177 MEDFileJointCorrespondence *MEDFileJointCorrespondence::deepCopy() const
178 {
179   MCAuto<MEDFileJointCorrespondence> ret=new MEDFileJointCorrespondence(*this);
180   return ret.retn();
181 }
182
183 MEDFileJointCorrespondence *MEDFileJointCorrespondence::shallowCpy() const
184 {
185   MCAuto<MEDFileJointCorrespondence> ret=new MEDFileJointCorrespondence(*this);
186   return ret.retn();
187 }
188
189 /*!
190  * Returns a string describing \a this mesh. This description includes the correspondence and
191  * the number correspondence.
192  *  \return std::string - the joint information string.
193  */
194 std::string MEDFileJointCorrespondence::simpleRepr() const
195 {
196   std::ostringstream oss;
197   oss << "(*************************************)\n(* JOINT_CORRESPOND INFORMATION: *)\n(*************************************)\n";
198   oss << "- entity type of the correspondence : " << ( getIsNodal() ? "NODE" : "CELL" ) << "\n";
199   oss << "- Local geometry type of the correspondence : " << INTERP_KERNEL::CellModel::GetCellModel( _loc_geo_type ).getRepr() << "\n";
200   oss << "- Remote geometry type of the correspondence : " << INTERP_KERNEL::CellModel::GetCellModel( _rem_geo_type ).getRepr() << "\n";
201   if ( (const DataArrayInt *)_correspondence )
202     {
203       oss << "- Number entity of the correspondence : " << getCorrespondence()->getNumberOfTuples() << "\n";
204
205       const DataArrayInt* tmp=getCorrespondence();
206       oss << "- Correspondence : <<";
207       for(const int *it=tmp->begin();it!=tmp->end();it++)
208         oss<< *it << " ";
209     }
210   else
211     {
212       oss << "- Number entity of the correspondence : 0\n";
213     }
214   oss << std::endl;
215   return oss.str();
216 }
217
218
219 MEDFileJointOneStep::MEDFileJointOneStep():_order(-1),_iteration(-1)
220 {
221 }
222
223 std::size_t MEDFileJointOneStep::getHeapMemorySizeWithoutChildren() const
224 {
225   return _correspondences.capacity()*sizeof(MCAuto<DataArrayInt>);
226 }
227
228 std::vector<const BigMemoryObject *> MEDFileJointOneStep::getDirectChildrenWithNull() const
229 {
230   return std::vector<const BigMemoryObject *>();
231 }
232
233 MEDFileJointOneStep *MEDFileJointOneStep::New(int dt, int it)
234 {
235   MEDFileJointOneStep* j = new MEDFileJointOneStep();
236   j->setOrder( dt );
237   j->setIteration( it );
238   return j;
239 }
240
241 /*!
242  * Returns a new MEDFileJointOneStep.
243  *  \param [in] fileName - the name of MED file to read.
244  *  \param [in] mName - the name of the mesh to read.
245  *  \param [in] jointName - the joint name.
246  *  \param [in] num - the number of an iteration.
247  *  \return MEDFileMesh * - a new instance of MEDFileJointOneStep.
248  *  \throw If the file is not readable.
249  *  \throw If there is no mesh with given attributes in the file.
250  */
251 MEDFileJointOneStep *MEDFileJointOneStep::New(const std::string& fileName, const std::string& mName, const std::string& jointName, int num)
252 {
253   MEDFileUtilities::CheckFileForRead(fileName);
254   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(), MED_ACC_RDONLY);
255   return new MEDFileJointOneStep(fid, mName, jointName, num);
256 }
257
258 MEDFileJointOneStep* MEDFileJointOneStep::New(med_idt fid, const std::string& mName, const std::string& jointName, int num)
259 {
260   return new MEDFileJointOneStep( fid, mName, jointName, num);
261 }
262
263 MEDFileJointOneStep::MEDFileJointOneStep(med_idt fid, const std::string& mName, const std::string& jointName, int num)
264 {
265   int order, iteration, ncorrespondence;
266   MEDFILESAFECALLERRD0(MEDsubdomainComputingStepInfo,(fid, mName.c_str(), jointName.c_str(), num, &order, &iteration, &ncorrespondence));
267   MEDFileJointOneStep::setOrder(order);
268   MEDFileJointOneStep::setIteration(iteration);
269   for ( int cur_it = 1; cur_it <= ncorrespondence; ++cur_it )
270     {
271       int num_entity;
272       med_entity_type loc_ent_type, rem_ent_type;
273       med_geometry_type loc_geo_type, rem_geo_type;
274       MEDFILESAFECALLERRD0(MEDsubdomainCorrespondenceSizeInfo,(fid, mName.c_str(), jointName.c_str(), order, iteration, cur_it,
275                                                                &loc_ent_type, &loc_geo_type, &rem_ent_type, &rem_geo_type, &num_entity));
276       if ( num_entity > 0 )
277         {
278           MCAuto<DataArrayInt> correspondence=DataArrayInt::New();
279           correspondence->alloc(num_entity*2, 1);
280           MEDFILESAFECALLERRD0(MEDsubdomainCorrespondenceRd,(fid, mName.c_str(), jointName.c_str(), order, iteration, loc_ent_type,
281                                                              loc_geo_type, rem_ent_type, rem_geo_type, correspondence->getPointer()));
282           MEDFileJointCorrespondence *cor=MEDFileJointCorrespondence::New();
283           cor->setIsNodal( loc_ent_type == MED_NODE );
284           cor->setLocalGeometryType ( convertGeometryType( loc_geo_type ));
285           cor->setRemoteGeometryType( convertGeometryType( rem_geo_type ));
286           cor->setCorrespondence( correspondence );
287           _correspondences.push_back(cor);
288         }
289     }
290 }
291
292 /*!
293  * Writes \a this joint into a MED file specified by its name.
294  *  \param [in] fileName - the MED file name.
295  *  \param [in] mode - the writing mode. For more on \a mode, see \ref AdvMEDLoaderBasics.
296  * - 2 - erase; an existing file is removed.
297  * - 1 - append; same data should not be present in an existing file.
298  * - 0 - overwrite; same data present in an existing file is overwritten.
299  *  \throw If the mesh name is not set.
300  *  \throw If \a mode == 1 and the same data is present in an existing file.
301  */
302 void MEDFileJointOneStep::write(const std::string& fileName, int mode, const std::string& localMeshName, const std::string& jointName) const
303 {
304   med_access_mode medmod=MEDFileUtilities::TraduceWriteMode(mode);
305   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(),medmod);
306   std::ostringstream oss; oss << "MEDFileJointOneStep : error on attempt to write in file : \"" << fileName << "\"";
307   MEDFileUtilities::CheckMEDCode(fid,fid,oss.str());
308   if ( _correspondences.empty() )
309     throw INTERP_KERNEL::Exception("MEDFileJointOneStep::write : no correspondences defined !");
310   writeLL(fid, localMeshName, jointName);
311 }
312
313 void MEDFileJointOneStep::writeLL(med_idt fid, const std::string& localMeshName, const std::string& jointName) const
314 {
315   for(std::vector< MCAuto<MEDFileJointCorrespondence> >::const_iterator it=_correspondences.begin();it!=_correspondences.end();it++)
316     {
317       (*it)->writeLL(fid, localMeshName, jointName, getOrder(), getIteration());
318     }
319 }
320
321 void MEDFileJointOneStep::pushCorrespondence(MEDFileJointCorrespondence* correspondence)
322 {
323   if(!correspondence)
324     throw INTERP_KERNEL::Exception("MEDFileJointCorrespondence::pushCorrespondence : invalid input pointer ! should be different from 0 !");
325   _correspondences.push_back(correspondence);
326   correspondence->incrRef();
327 }
328
329 int MEDFileJointOneStep::getNumberOfCorrespondences() const
330 {
331   return _correspondences.size();
332 }
333
334 /** Return a borrowed reference (caller is not responsible) */
335 MEDFileJointCorrespondence *MEDFileJointOneStep::getCorrespondenceAtPos(int i) const
336 {
337   if(i<0 || i>=(int)_correspondences.size())
338     {
339       std::ostringstream oss; oss << "MEDFileJointOneStep::getCorrespondenceAtPos : invalid correspondence id given in parameter ! Should be in [0;" << _correspondences.size() << ") !";
340       throw INTERP_KERNEL::Exception(oss.str().c_str());
341     }
342   const MEDFileJointCorrespondence* ret = _correspondences[i];
343   return const_cast<MEDFileJointCorrespondence *>( ret );
344 }
345
346 /*!
347  * Checks if \a this and another Joint are equal.
348  *  \param [in] other - the Joint to compare with.
349  *  \return bool - \c true if the Joints are equal, \c false, else.
350  */
351 bool MEDFileJointOneStep::isEqual(const MEDFileJointOneStep *other) const
352 {
353   if(_order!=other->_order)
354     return false;
355   if(_iteration!=other->_iteration)
356     return false;
357   if ( getNumberOfCorrespondences() != other->getNumberOfCorrespondences() )
358     return false;
359
360   std::vector<int> found( getNumberOfCorrespondences(), false );
361   for(int i=0; i<getNumberOfCorrespondences(); i++)
362     {
363       int j;
364       for(j=0; j<getNumberOfCorrespondences(); j++)
365         {
366           if ( !found[ j ] &&
367                getCorrespondenceAtPos(i)->isEqual(other->getCorrespondenceAtPos(j)))
368             {
369               found[ j ] = true;
370               break;
371             }
372         }
373       if ( j == getNumberOfCorrespondences() )
374         return false;
375     }
376   return true;
377 }
378
379 MEDFileJointOneStep *MEDFileJointOneStep::deepCopy() const
380 {
381   std::vector< MCAuto<MEDFileJointCorrespondence> > correspondences(_correspondences.size());
382   std::size_t i=0;
383   for(std::vector< MCAuto<MEDFileJointCorrespondence> >::const_iterator it=_correspondences.begin();it!=_correspondences.end();it++,i++)
384     if((const MEDFileJointCorrespondence *)*it)
385       correspondences[i]=(*it)->deepCopy();
386   MCAuto<MEDFileJointOneStep> ret= new MEDFileJointOneStep;
387   ret->_correspondences=correspondences;
388   return ret.retn();
389 }
390
391 MEDFileJointOneStep *MEDFileJointOneStep::shallowCpy() const
392 {
393   MCAuto<MEDFileJointOneStep> ret=new MEDFileJointOneStep(*this);
394   return ret.retn();
395 }
396
397 /*!
398  * Returns a string describing \a this Joint. This description includes the correspondence and
399  * the number of correspondences.
400  *  \return std::string - the joint information string.
401  */
402 std::string MEDFileJointOneStep::simpleRepr() const
403 {
404   std::ostringstream oss;
405   oss << "(*************************************)\n(* JOINT_ONE_STEP INFORMATION: *)\n(*************************************)\n";
406   oss << "- Number of the correspondences : <<" << _correspondences.size() << ">>\n";
407   for(std::vector< MCAuto<MEDFileJointCorrespondence> >::const_iterator it=_correspondences.begin();it!=_correspondences.end();it++)
408     {
409       oss << (*it)->simpleRepr();
410     }
411   return oss.str();
412 }
413 INTERP_KERNEL::NormalizedCellType MEDFileJointOneStep::convertGeometryType(med_geometry_type geotype)
414 {
415   INTERP_KERNEL::NormalizedCellType result=INTERP_KERNEL::NORM_ERROR;
416   for(int i=0; i<MED_N_CELL_FIXED_GEO; i++)
417     {
418       if (typmai[i]==geotype)
419         {
420           result=typmai2[i];
421           break;
422         }
423     }
424   return result;
425 }
426 std::size_t MEDFileJoint::getHeapMemorySizeWithoutChildren() const
427 {
428   return _joint.capacity()*sizeof(MCAuto<MEDFileJointOneStep>);
429 }
430
431 std::vector<const BigMemoryObject *> MEDFileJoint::getDirectChildrenWithNull() const
432 {
433   return std::vector<const BigMemoryObject *>();
434 }
435
436 MEDFileJoint *MEDFileJoint::New()
437 {
438   return new MEDFileJoint();
439 }
440
441 MEDFileJoint *MEDFileJoint::New(const std::string& fileName, const std::string& mName, int curJoint)
442 {
443   MEDFileUtilities::CheckFileForRead(fileName);
444   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(), MED_ACC_RDONLY);
445   return new MEDFileJoint(fid,mName,curJoint);
446 }
447
448 MEDFileJoint *MEDFileJoint::New(med_idt fid, const std::string& mName, int curJoint)
449 {
450   return new MEDFileJoint(fid,mName,curJoint);
451 }
452
453 MEDFileJoint *MEDFileJoint::New(const std::string& jointName, const std::string& locMeshName, const std::string& remoteMeshName, int remoteMeshNum)
454 {
455   MEDFileJoint* j = new MEDFileJoint();
456   j->setJointName( jointName );
457   j->setLocalMeshName( locMeshName );
458   j->setRemoteMeshName( remoteMeshName );
459   j->setDomainNumber( remoteMeshNum );
460   return j;
461 }
462
463 MEDFileJoint::MEDFileJoint()
464 {
465 }
466
467 /*!
468  * Returns a new MEDFileJoint holding the mesh data that has been read from a given MED
469  * file. The Joint to load is specified by mesh name and a Joint iteration.
470  *  \param [in] fileName - the name of MED file to read.
471  *  \param [in] mName - the name of the mesh to read.
472  *  \param [in] curJoint - the iteration number of current joint.
473  *  \return MEDFileJoint * - a new instance of MEDFileJoint.
474  *  \throw If the file is not readable.
475  *  \throw If there is no mesh with given attributes in the file.
476  */
477 MEDFileJoint::MEDFileJoint(med_idt fid, const std::string& mName, int curJoint)
478 {
479   INTERP_KERNEL::AutoPtr<char> joint_name=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
480   INTERP_KERNEL::AutoPtr<char> desc_name=MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE);
481   INTERP_KERNEL::AutoPtr<char> rem_mesh_name=MEDLoaderBase::buildEmptyString(MED_NAME_SIZE);
482   int domain_number=0, nstep=0, nocstpncorrespondence=0;
483   MEDFILESAFECALLERRD0(MEDsubdomainJointInfo,(fid,mName.c_str(), curJoint, joint_name, desc_name, &domain_number,rem_mesh_name,
484                                               &nstep, &nocstpncorrespondence));
485   setLocalMeshName(mName);
486   setRemoteMeshName(MEDLoaderBase::buildStringFromFortran(rem_mesh_name,MED_NAME_SIZE));
487   setDescription(MEDLoaderBase::buildStringFromFortran(desc_name,MED_COMMENT_SIZE));
488   setJointName(MEDLoaderBase::buildStringFromFortran(joint_name,MED_NAME_SIZE));
489   setDomainNumber(domain_number);
490   for(int cur_step=1; cur_step <= nstep; ++cur_step)
491     {
492       MEDFileJointOneStep *cor=MEDFileJointOneStep::New(fid, mName.c_str(), getJointName(), cur_step);
493       _joint.push_back(cor);
494     }
495 }
496
497 void MEDFileJoint::writeLL(med_idt fid) const
498 {
499   // if ( _loc_mesh_name.empty() )
500   //   throw INTERP_KERNEL::Exception("MEDFileJoint::write : name of a local mesh not defined!");
501   MEDFILESAFECALLERWR0(MEDsubdomainJointCr,(fid,getLocalMeshName().c_str(),getJointName().c_str(),getDescription().c_str(),getDomainNumber(),getRemoteMeshName().c_str()));
502   for(std::vector< MCAuto<MEDFileJointOneStep> >::const_iterator it=_joint.begin();it!=_joint.end();it++)
503     (*it)->writeLL(fid, getLocalMeshName(),getJointName());
504 }
505
506 void MEDFileJoint::pushStep(MEDFileJointOneStep* step)
507 {
508   if(!step)
509     throw INTERP_KERNEL::Exception("MEDFileJoint::pushStep : invalid input pointer ! should be different from 0 !");
510   _joint.push_back(step);
511   step->incrRef();
512 }
513
514 int MEDFileJoint::getNumberOfSteps() const
515 {
516   return _joint.size();
517 }
518
519 /** Return a borrowed reference (caller is not responsible) */
520 MEDFileJointOneStep *MEDFileJoint::getStepAtPos(int i) const
521 {
522   if(i<0 || i>=(int)_joint.size())
523     {
524       std::ostringstream oss; oss << "MEDFileJoint::getStepAtPos : invalid step id given in parameter ! Should be in [0;" << _joint.size() << ") !";
525       throw INTERP_KERNEL::Exception(oss.str().c_str());
526     }
527   const MEDFileJointOneStep* ret = _joint[i];
528   return const_cast<MEDFileJointOneStep *>( ret );
529 }
530
531 /*!
532  * Checks if \a this and another Joint are equal.
533  *  \param [in] other - the Joint to compare with.
534  *  \return bool - \c true if the Joints are equal, \c false, else.
535  */
536 bool MEDFileJoint::isEqual(const MEDFileJoint *other) const
537 {
538   if(_loc_mesh_name!=other->_loc_mesh_name)
539     return false;
540   if(_joint_name!=other->_joint_name)
541     return false;
542   if(_desc_name!=other->_desc_name)
543       return false;
544   if(_rem_mesh_name!=other->_rem_mesh_name)
545     return false;
546   if(_domain_number!=other->_domain_number)
547     return false;
548   std::vector<int> found( getNumberOfSteps(), false );
549   for(int i=0; i<getNumberOfSteps(); i++)
550     {
551       int j;
552       for(j=0; j<getNumberOfSteps(); j++)
553         {
554           if ( !found[ j ] &&
555             getStepAtPos(i)->isEqual(other->getStepAtPos(j)))
556             {
557               found[ j ] = true;
558               break;
559             }
560         }
561       if ( j == getNumberOfSteps() )
562         return false;
563     }
564   return true;
565 }
566
567 MEDFileJoint *MEDFileJoint::deepCopy() const
568 {
569   std::vector< MCAuto<MEDFileJointOneStep> > joint(_joint.size());
570   std::size_t i=0;
571   for(std::vector< MCAuto<MEDFileJointOneStep> >::const_iterator it=_joint.begin();it!=_joint.end();it++,i++)
572     if((const MEDFileJointOneStep *)*it)
573       joint[i]=(*it)->deepCopy();
574   MCAuto<MEDFileJoint> ret=MEDFileJoint::New();
575   ret->_joint=joint;
576   return ret.retn();
577 }
578
579 MEDFileJoint *MEDFileJoint::shallowCpy() const
580 {
581   MCAuto<MEDFileJoint> ret=new MEDFileJoint(*this);
582   return ret.retn();
583 }
584
585 bool MEDFileJoint::changeJointNames(const std::vector< std::pair<std::string,std::string> >& modifTab)
586 {
587   for(std::vector< std::pair<std::string,std::string> >::const_iterator it=modifTab.begin();it!=modifTab.end();it++)
588     {
589       if((*it).first==_joint_name)
590         {
591           _joint_name=(*it).second;
592           return true;
593         }
594     }
595   return false;
596 }
597
598 /*!
599  * Returns a string describing \a this mesh. This description includes the correspondence and
600  * the number correspondence.
601  *  \return std::string - the joint information string.
602  */
603 std::string MEDFileJoint::simpleRepr() const
604 {
605   std::ostringstream oss;
606   oss << "(*************************************)\n(* JOINT INFORMATION: *)\n(*************************************)\n";
607   oss << "- Local Mesh name : <<" << getLocalMeshName() << ">>\n";
608   oss << "- Remote Mesh name : <<" << getRemoteMeshName() << ">>\n";
609   oss << "- Description : <<" << getDescription() << ">>\n";
610   oss << "- Joint name : <<" << getJointName() << ">>\n";
611   oss << "- Domain number : " << getDomainNumber() << "\n";
612   for(std::vector< MCAuto<MEDFileJointOneStep> >::const_iterator it=_joint.begin();it!=_joint.end();it++)
613     {
614       oss << (*it)->simpleRepr();
615     }
616   return oss.str();
617 }
618
619 MEDFileJoints *MEDFileJoints::New()
620 {
621   return new MEDFileJoints;
622 }
623
624 MEDFileJoints *MEDFileJoints::New(const std::string& fileName, const std::string& meshName)
625 {
626   MEDFileUtilities::CheckFileForRead(fileName);
627   MEDFileUtilities::AutoFid fid=MEDfileOpen(fileName.c_str(), MED_ACC_RDONLY);
628   return new MEDFileJoints( fid, meshName );
629 }
630
631 MEDFileJoints *MEDFileJoints::New(med_idt fid, const std::string& meshName)
632 {
633   return new MEDFileJoints( fid, meshName );
634 }
635
636 void MEDFileJoints::writeLL(med_idt fid) const
637 {
638   for(std::vector< MCAuto<MEDFileJoint> >::const_iterator it=_joints.begin();it!=_joints.end();it++)
639     (*it)->writeLL(fid);
640 }
641
642 std::string MEDFileJoints::getMeshName() const
643 {
644   for ( size_t i = 0; i <= _joints.size(); ++i )
645     if ( (const MEDFileJoint*) _joints[i] )
646       return _joints[i]->getLocalMeshName();
647
648   return "";
649 }
650
651 int MEDFileJoints::getNumberOfJoints() const
652 {
653   return _joints.size();
654 }
655
656 /** Return a borrowed reference (caller is not responsible) */
657 MEDFileJoint *MEDFileJoints::getJointAtPos(int i) const
658 {
659   if(i<0 || i>=(int)_joints.size())
660     {
661       std::ostringstream oss; oss << "MEDFileJoints::getJointAtPos : invalid joint id given in parameter ! Should be in [0;" << _joints.size() << ") !";
662       throw INTERP_KERNEL::Exception(oss.str().c_str());
663     }
664   const MEDFileJoint* ret = _joints[i];
665   return const_cast<MEDFileJoint *>( ret );
666 }
667
668
669 /** Return a borrowed reference (caller is not responsible) */
670 MEDFileJoint *MEDFileJoints::getJointWithName(const std::string& jname) const
671 {
672   std::vector<std::string> js=getJointsNames();
673   std::vector<std::string>::iterator it=std::find(js.begin(),js.end(),jname);
674   if(it==js.end())
675     {
676       std::ostringstream oss; oss << "MEDFileJoints::getJointWithName : Joint  \"" << jname << "\" does not exist in this ! Existing are : ";
677       std::copy(js.begin(),js.end(),std::ostream_iterator<std::string>(oss," "));
678       throw INTERP_KERNEL::Exception(oss.str().c_str());
679     }
680   return getJointAtPos((int)std::distance(js.begin(),it));
681 }
682
683 std::vector<std::string> MEDFileJoints::getJointsNames() const
684 {
685   std::vector<std::string> ret(_joints.size());
686   int i=0;
687   for(std::vector< MCAuto<MEDFileJoint> >::const_iterator it=_joints.begin();it!=_joints.end();it++,i++)
688     {
689       const MEDFileJoint *f=(*it);
690       if(f)
691         {
692           ret[i]=f->getJointName();
693         }
694       else
695         {
696           std::ostringstream oss; oss << "MEDFileJoints::getJointsNames : At rank #" << i << " joint is not defined !";
697           throw INTERP_KERNEL::Exception(oss.str().c_str());
698         }
699     }
700   return ret;
701 }
702
703 bool MEDFileJoints::changeJointNames(const std::vector< std::pair<std::string,std::string> >& modifTab)
704 {
705   bool ret=false;
706   for(std::vector< MCAuto<MEDFileJoint> >::iterator it=_joints.begin();it!=_joints.end();it++)
707     {
708       MEDFileJoint *cur(*it);
709       if(cur)
710         ret=cur->changeJointNames(modifTab) || ret;
711     }
712   return ret;
713 }
714
715 void MEDFileJoints::resize(int newSize)
716 {
717   _joints.resize(newSize);
718 }
719
720 void MEDFileJoints::pushJoint(MEDFileJoint *joint)
721 {
722   if(!joint)
723     throw INTERP_KERNEL::Exception("MEDFileJoints::pushJoint() : invalid input pointer ! should be different from 0 !");
724   if ( !_joints.empty() &&
725        _joints[0]->getLocalMeshName() != joint->getLocalMeshName() )
726     throw INTERP_KERNEL::Exception("MEDFileJoints::pushJoint() : different names of local meshes ! should be equal !");
727
728   _joints.push_back(joint);
729   joint->incrRef();
730 }
731
732 void MEDFileJoints::setJointAtPos(int i, MEDFileJoint *joint)
733 {
734   if(!joint)
735     throw INTERP_KERNEL::Exception("MEDFileJoints::setJointAtPos : invalid input pointer ! should be different from 0 !");
736   if(i>=(int)_joints.size())
737     _joints.resize(i+1);
738   _joints[i]=joint;
739   joint->incrRef();
740 }
741
742 void MEDFileJoints::destroyJointAtPos(int i)
743 {
744   if(i<0 || i>=(int)_joints.size())
745     {
746       std::ostringstream oss; oss << "MEDFileJoints::destroyJointAtPos : Invalid given id in input (" << i << ") should be in [0," << _joints.size() << ") !";
747       throw INTERP_KERNEL::Exception(oss.str().c_str());
748     }
749   _joints.erase(_joints.begin()+i);
750 }
751
752 MEDFileJoints::MEDFileJoints()
753 {
754 }
755
756 MEDFileJoints::MEDFileJoints(med_idt fid, const std::string& meshName)
757 {
758   int num_joint=MEDnSubdomainJoint(fid, meshName.c_str() );
759   for(int i = 1; i <= num_joint; i++)
760     _joints.push_back(MEDFileJoint::New(fid,meshName,i));
761 }
762
763 MEDFileJoints *MEDFileJoints::deepCopy() const
764 {
765   std::vector< MCAuto<MEDFileJoint> > joints(_joints.size());
766   std::size_t i=0;
767   for(std::vector< MCAuto<MEDFileJoint> >::const_iterator it=_joints.begin();it!=_joints.end();it++,i++)
768     if((const MEDFileJoint *)*it)
769       joints[i]=(*it)->deepCopy();
770   MCAuto<MEDFileJoints> ret=MEDFileJoints::New();
771   ret->_joints=joints;
772   return ret.retn();
773 }
774
775 std::size_t MEDFileJoints::getHeapMemorySizeWithoutChildren() const
776 {
777   return _joints.capacity()*(sizeof(MCAuto<MEDFileJoint>));
778 }
779
780 std::vector<const BigMemoryObject *> MEDFileJoints::getDirectChildrenWithNull() const
781 {
782   std::vector<const BigMemoryObject *> ret;
783   for(std::vector< MCAuto<MEDFileJoint> >::const_iterator it=_joints.begin();it!=_joints.end();it++)
784     ret.push_back((const MEDFileJoint *)*it);
785   return ret;
786 }
787
788 std::string MEDFileJoints::simpleRepr() const
789 {
790   std::ostringstream oss;
791   oss << "(*****************)\n(* MEDFileJoints *)\n(*****************)\n\n";
792   simpleReprWithoutHeader(oss);
793   return oss.str();
794 }
795
796 void MEDFileJoints::simpleReprWithoutHeader(std::ostream& oss) const
797 {
798   int nbOfJoints=getNumberOfJoints();
799   oss << "There are " << nbOfJoints << " joints with the following names : \n";
800   std::vector<std::string> jns=getJointsNames();
801   for(int i=0;i<nbOfJoints;i++)
802     oss << "  - #" << i << " \"" << jns[i] << "\"\n";
803   for(std::vector< MCAuto<MEDFileJoint> >::const_iterator it=_joints.begin();it!=_joints.end();it++)
804     {
805       oss << (*it)->simpleRepr();
806     }
807 }