Salome HOME
88d17854d50b701112c1bf957f5d4de1b3df536e
[tools/medcoupling.git] / src / MEDLoader / SauvWriter.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 // File      : SauvWriter.cxx
20 // Created   : Wed Aug 24 12:55:55 2011
21 // Author    : Edward AGAPOV (eap)
22
23 #include "SauvWriter.hxx"
24
25 #include "InterpKernelException.hxx"
26 #include "MEDFileMesh.hxx"
27 #include "MEDFileField.hxx"
28 #include "MEDFileData.hxx"
29 #include "CellModel.hxx"
30
31 #include <fstream>
32 #include <sstream>
33 #include <iostream>
34 #include <cstdlib>
35 #include <iomanip>
36
37 using namespace ParaMEDMEM;
38 using namespace SauvUtilities;
39 using namespace std;
40
41 #define INFOS_MED(txt) cout << txt << endl;
42
43 namespace
44 {
45   const char* zeroI8 = "       0"; // FORMAT(I8)
46
47   // ============================================================
48   // the class writes endl to the file as soon as <limit> fields
49   // have been written after the last endl
50   // ============================================================
51
52   class TFieldCounter
53   {
54     fstream& _file;
55     int _count, _limit;
56   public:
57     TFieldCounter(fstream& f, int limit=0): _file(f), _limit(limit) { init(); }
58     void init(int limit=0) // init, is done by stop() as well
59     { if (limit) _limit = limit; _count = 0; }
60     void operator++(int) // next
61     { if ( ++_count == _limit ) { _file << endl; init(); }}
62     void stop() // init() and write endl if there was no endl after the last written field
63     { if ( _count ) _file << endl; init(); }
64     ~TFieldCounter() { stop(); }
65   };
66
67   //================================================================================
68   /*!
69    * \brief Return a name of a field support on all elements
70    */
71   //================================================================================
72
73   string noProfileName( INTERP_KERNEL::NormalizedCellType type )
74   {
75     return "INTERP_KERNEL::NormalizedCellType_" + SauvUtilities::toString( type );
76   }
77
78   //================================================================================
79   /*!
80    * \brief Remove white spaces from the head and tail
81    */
82   //================================================================================
83
84   string cleanName( const string& theName )
85   {
86     string name = theName;
87     if ( !name.empty() )
88       {
89         // cut off leading white spaces
90         string::size_type firstChar = name.find_first_not_of(" \t");
91         if (firstChar < name.length())
92           {
93             name = name.substr(firstChar);
94           }
95         else
96           {
97             name = ""; // only whitespaces there - remove them
98           }
99         // cut off trailing white spaces
100         string::size_type lastChar = name.find_last_not_of(" \t");
101         if (lastChar < name.length())
102           name = name.substr(0, lastChar + 1);
103       }
104     return name;
105   }
106
107   //================================================================================
108   /*!
109    * \brief Converts MED long names into SAUVE short ones, returnes a healed long name
110    */
111   //================================================================================
112
113   string addName (map<string,int>& nameMap,
114                   map<string,int>& namePrefixesMap,
115                   const string&    theName,
116                   const int        index)
117   {
118     // Converts names like:
119     // MED:                       GIBI:     
120     //   TEMPERATURE_FLUIDE   ->    TEMPE001
121     //   TEMPERATURE_SOLIDE   ->    TEMPE002
122     //   PRESSION             ->    PRESSION
123     //   NU                   ->    NU      
124     //   VOLUM001             ->    VOLUM001
125     //   VOLUMOFOBJECT        ->    VOLUM003
126     //   VOLUM002             ->    VOLUM002
127     string healedName = cleanName(theName);
128     int ind = index;
129
130     if (!healedName.empty())
131       {
132         string name = healedName;
133         int len = name.length();
134         for (int i = 0; i < len; ++i)
135           name[i] = toupper(name[i]);
136
137         bool doResave = false; // only for tracing
138
139         // I. Save a short name as it is
140         if (len <= 8)
141           {
142             INFOS_MED("Save <" << theName << "> as <" << name << ">");
143
144             map<string,int>::iterator it = nameMap.find(name);
145             if (it != nameMap.end())
146               {
147                 // There is already such name in the map.
148
149                 // a. Replace in the map the old pair by the current one
150                 int old_ind = nameMap[name];
151                 nameMap[name] = ind;
152                 // b. Rebuild the old pair (which was in the map,
153                 //    it seems to be built automatically by step II)
154                 ind = old_ind;
155                 // continue with step II
156                 doResave = true; // only for tracing
157               }
158             else
159               {
160                 // Save in the map
161                 nameMap.insert(make_pair(name, ind));
162
163                 // Update loc_index for this name (if last free characters represents a number)
164                 // to avoid conflicts with long names, same in first 5 characters
165                 if (len == 8)
166                   {
167                     int new_loc_index = atoi(name.c_str() + 5);
168                     if (new_loc_index > 0)
169                       {
170                         // prefix
171                         string str = name.substr(0,5);
172                         if (namePrefixesMap.find(str) != namePrefixesMap.end())
173                           {
174                             int old_loc_index = namePrefixesMap[str];
175                             if (new_loc_index < old_loc_index) new_loc_index = old_loc_index;
176                           }
177                         namePrefixesMap[str] = new_loc_index;
178                       }
179                   }
180                 return healedName;
181               }
182           } // if (len <= 8)
183
184         // II. Cut long name and add a numeric suffix
185
186         // first 5 or less characters of the name
187         if (len > 5) name = name.substr(0,5);
188
189         // numeric suffix
190         map<string,int>::iterator name2ind = namePrefixesMap.insert( make_pair( name, 0 )).first;
191         string numSuffix = SauvUtilities::toString( ++(name2ind->second) );
192
193         if ( numSuffix.size() + name.size() > 8 )
194           THROW_IK_EXCEPTION("Can't write not unique name: " << healedName);
195
196         if ( numSuffix.size() < 3 )
197           numSuffix.insert( 0, 3 - numSuffix.size(), '0' );
198
199         name += numSuffix;
200         nameMap.insert(make_pair(name, ind));
201
202         if (doResave)
203           {
204             INFOS_MED("Resave previous <" << healedName << "> as <" << name << ">");
205           }
206         else
207           {
208             INFOS_MED("Save <" << theName << "> as <" << name << ">");
209           }
210       }
211     return healedName;
212   }
213 }
214
215 //================================================================================
216 /*!
217  * \brief Creates SauvWriter
218  */
219 //================================================================================
220
221 SauvWriter* SauvWriter::New()
222 {
223   return new SauvWriter;
224 }
225
226 //================================================================================
227 /*!
228  * \brief Fills own DS by MEDFileData
229  */
230 //================================================================================
231
232 void SauvWriter::setMEDFileDS(const MEDFileData* medData,
233                               unsigned           meshIndex)
234 {
235   if ( !medData) THROW_IK_EXCEPTION("NULL MEDFileData");
236
237   MEDFileMeshes * meshes = medData->getMeshes();
238   MEDFileFields * fields = medData->getFields();
239   if ( !meshes) THROW_IK_EXCEPTION("No meshes in MEDFileData");
240
241   _fileMesh = meshes->getMeshAtPos( meshIndex );
242   _fileMesh->incrRef();
243
244   if ( fields )
245     for ( int i = 0; i < fields->getNumberOfFields(); ++i )
246       {
247         MEDFileFieldMultiTS * f = fields->getFieldAtPos(i);
248         if ( f->getMeshName() == _fileMesh->getName() )
249           {
250             vector< vector<TypeOfField> > fTypes = f->getTypesOfFieldAvailable();
251             if ( fTypes[0].size() == 1 && fTypes[0][0] == ON_NODES )
252               _nodeFields.push_back( f );
253             else
254               _cellFields.push_back( f );
255           }
256       }
257 }
258
259 //================================================================================
260 /*!
261  * \brief Adds a submesh
262  */
263 //================================================================================
264
265 SauvWriter::SubMesh* SauvWriter::addSubMesh(const std::string& name, int dimRelExt)
266 {
267   if ( _subs.capacity() < _subs.size() + 1 )
268     THROW_IK_EXCEPTION("SauvWriter: INTERNAL error, wrong evaluation of nb of sub-meshes");
269   _subs.resize( _subs.size() + 1 );
270   SubMesh& sm = _subs.back();
271   sm._name = name;
272   sm._dimRelExt = dimRelExt;
273   return &sm;
274 }
275 //================================================================================
276 /*!
277  * \brief Returns nb of cell types
278  */
279 //================================================================================
280
281 int SauvWriter::SubMesh::nbTypes() const
282 {
283   int nb = 0;
284   for (int i = 0; i < cellIDsByTypeSize(); ++i )
285     nb += int( !_cellIDsByType[i].empty() );
286   return nb;
287 }
288
289 //================================================================================
290 /*!
291  * \brief Fill _subs
292  */
293 //================================================================================
294
295 void SauvWriter::fillSubMeshes( int& nbSauvObjects, map<string,int>& nameNbMap )
296 {
297   // evaluate nb of _subs in order to avoid re-allocation of _subs
298   int nbSubs = 1; // for the very mesh
299   nbSubs += _fileMesh->getFamilyInfo().size() + 4;  // + 4 zero families (for each dimRelExt)
300   nbSubs += _fileMesh->getGroupInfo().size();
301   nbSubs += evaluateNbProfileSubMeshes();
302   _subs.clear();
303   _subs.reserve( nbSubs );
304
305   fillFamilySubMeshes();
306   fillGroupSubMeshes();
307   fillProfileSubMeshes();
308
309   // fill names of SubMesh'es and count nb of sauv sub-meshes they will be stored into
310   nbSauvObjects = 0;
311   map<string,int> namePrefixMap;
312   for ( size_t i = 0; i < _subs.size(); ++i )
313     {
314       SubMesh& sm = _subs[i];
315
316       sm._nbSauvObjects = 0;
317       if ( sm._subs.empty() )
318         {
319           sm._nbSauvObjects = sm.nbTypes();
320         }
321       else
322         {
323           sm._nbSauvObjects = 1;
324         }
325
326       sm._id = nbSauvObjects+1;
327       nbSauvObjects += sm._nbSauvObjects;
328
329       if ( sm._nbSauvObjects )
330         sm._name = addName( nameNbMap, namePrefixMap, sm._name, sm._id );
331
332       if ( sm._nbSauvObjects && !sm._name.empty() )
333         {
334           nameGIBItoMED aMEDName;
335           aMEDName.gibi_pile = PILE_SOUS_MAILLAGE;
336           aMEDName.gibi_id   = sm._id;
337           aMEDName.med_name  = sm._name;
338           _longNames[ LN_MAIL ].push_back(aMEDName);
339         }
340     }
341 }
342
343 //================================================================================
344 /*!
345  * \brief fill sub-meshes of families
346  */
347 //================================================================================
348
349 void SauvWriter::fillFamilySubMeshes()
350 {
351   SubMesh* nilSm = (SubMesh*) 0;
352   std::vector<int> dims = _fileMesh->getNonEmptyLevelsExt();
353   for ( size_t iDim = 0; iDim < dims.size(); ++iDim )
354     {
355       int dimRelExt = dims[ iDim ];
356       MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh > mesh = _fileMesh->getGenMeshAtLevel(dimRelExt);
357       const DataArrayInt * famIds = _fileMesh->getFamilyFieldAtLevel(dimRelExt);
358       if ( !famIds ) continue;
359
360       int curFamID = 0;
361       SubMesh* curSubMesh = addSubMesh( "", dimRelExt ); // submesh of zero family
362       _famIDs2Sub[0] = curSubMesh;
363       int sub0Index = _subs.size()-1;
364
365       const int * famID = famIds->begin(), * famIDEnd = famIds->end();
366       for ( int cellID = 0; famID < famIDEnd; ++famID, cellID++ )
367         {
368           if ( *famID != curFamID )
369             {
370               curFamID = *famID;
371               map< int, SubMesh* >::iterator f2s = _famIDs2Sub.insert( make_pair( curFamID, nilSm )).first;
372               if ( !f2s->second )
373                 f2s->second = addSubMesh( "", dimRelExt ); // no names for families
374               curSubMesh = f2s->second;
375             }
376           INTERP_KERNEL::NormalizedCellType cellType =
377             dimRelExt == 1 ? INTERP_KERNEL::NORM_POINT1 : mesh->getTypeOfCell( cellID );
378           curSubMesh->_cellIDsByType[ cellType ].push_back( cellID );
379         }
380
381       if ( dimRelExt == 1 )
382         {
383           // clear submesh of nodal zero family
384           _famIDs2Sub[0]->_cellIDsByType[ INTERP_KERNEL::NORM_POINT1 ].clear();
385         }
386       else if ( dimRelExt == 0 )
387         {
388           // make a submesh including all cells
389           if ( sub0Index == (int)(_subs.size()-1) )
390             {
391               _famIDs2Sub[0]->_name = _fileMesh->getName(); // there is the zero family only
392             }
393           else
394             {
395               curSubMesh = addSubMesh( _fileMesh->getName(), dimRelExt );
396               if ( _famIDs2Sub[0]->nbTypes() == 0 )
397                 sub0Index++; // skip an empty zero family
398               for ( size_t i = sub0Index; i < _subs.size()-1; ++i )
399                 curSubMesh->_subs.push_back( & _subs[i] );
400             }
401         }
402     }
403 }
404
405 //================================================================================
406 /*!
407  * \brief fill sub-meshes of groups
408  */
409 //================================================================================
410
411 void SauvWriter::fillGroupSubMeshes()
412 {
413   const map<string, vector<string> >& grpFams = _fileMesh->getGroupInfo();
414   map<string, vector<string> >::const_iterator g2ff = grpFams.begin();
415   for ( ; g2ff != grpFams.end(); ++g2ff )
416     {
417       const string&        groupName = g2ff->first;
418       const vector<string>& famNames = g2ff->second;
419       if ( famNames.empty() ) continue;
420       std::vector<SubMesh*> famSubMeshes( famNames.size() );
421       std::size_t k = 0;
422       for ( size_t i = 0; i < famNames.size(); ++i )
423         {
424           int famID = _fileMesh->getFamilyId( famNames[i].c_str() );
425           map< int, SubMesh* >::iterator i2f = _famIDs2Sub.find( famID );
426           if ( i2f != _famIDs2Sub.end() )
427             {
428               famSubMeshes[ k ] = i2f->second;
429               ++k;
430             }
431         }
432       // if a family exists but has no element, no submesh has been found for this family
433       // => we have to resize famSubMeshes with the number of submeshes stored
434       if (k != famNames.size())
435           famSubMeshes.resize(k);
436       SubMesh* grpSubMesh = addSubMesh( groupName, famSubMeshes[0]->_dimRelExt );
437       grpSubMesh->_subs.swap( famSubMeshes );
438     }
439 }
440
441
442 //================================================================================
443 /*!
444  * \brief fill sub-meshes of profiles
445  */
446 //================================================================================
447
448 void SauvWriter::fillProfileSubMeshes()
449 {
450   _profile2Sub.clear();
451   SubMesh* nilSm = (SubMesh*) 0;
452   for ( int isOnNodes = 0; isOnNodes < 2; ++isOnNodes )
453     {
454       vector< MEDCouplingAutoRefCountObjectPtr< MEDFileFieldMultiTS > >
455         fields = isOnNodes ? _nodeFields : _cellFields;
456       for ( size_t i = 0; i < fields.size(); ++i )
457         {
458           vector< pair<int,int> > iters = fields[i]->getIterations();
459
460           vector<INTERP_KERNEL::NormalizedCellType> types;
461           vector< vector<TypeOfField> > typesF;
462           vector< vector<string> > pfls, locs;
463           fields[i]->getFieldSplitedByType( iters[0].first, iters[0].second,
464                                             _fileMesh->getName(), types, typesF, pfls, locs);
465           int dimRelExt;
466           for ( size_t iType = 0; iType < types.size(); ++iType )
467             {
468               if ( types[iType] == INTERP_KERNEL::NORM_ERROR )
469                 dimRelExt = 1; // on nodes
470               else
471                 dimRelExt = getDimension( types[iType] ) - _fileMesh->getMeshDimension();
472               for ( size_t iPfl = 0; iPfl < pfls[iType].size(); ++iPfl )
473                 {
474                   bool isOnAll = pfls[iType][iPfl].empty();
475                   if ( isOnAll ) pfls[iType][iPfl] = noProfileName( types[iType] );
476                   map< string, SubMesh* >::iterator pfl2sm =
477                     _profile2Sub.insert( make_pair( pfls[iType][iPfl], nilSm )).first;
478                   if ( !pfl2sm->second )
479                     {
480                       SubMesh* sm = pfl2sm->second = addSubMesh( "", dimRelExt ); // no names for profiles
481                       const DataArrayInt * pfl = isOnAll ? 0 : fields[i]->getProfile( pfls[iType][iPfl].c_str() );
482                       makeProfileIDs( sm, types[iType], pfl );
483                     }
484                 }
485             }
486         }
487     }
488 }
489
490 //================================================================================
491 /*!
492  * \brief Return max possible nb of sub-meshes to decsribe field supports
493  */
494 //================================================================================
495
496 int SauvWriter::evaluateNbProfileSubMeshes() const
497 {
498   int nb = 0;
499   for ( size_t i = 0; i < _nodeFields.size(); ++i )
500     nb += 1 + _nodeFields[i]->getPflsReallyUsed().size();
501
502   for ( size_t i = 0; i < _cellFields.size(); ++i )
503     {
504       nb += _cellFields[i]->getPflsReallyUsed().size();
505
506       vector< pair<int,int> > iters = _cellFields[i]->getIterations();
507
508       vector<INTERP_KERNEL::NormalizedCellType> types;
509       vector< vector<TypeOfField> > typesF;
510       vector< vector<string> > pfls, locs;
511       _cellFields[i]->getFieldSplitedByType( iters[0].first, iters[0].second,
512                                              _fileMesh->getName(), types, typesF, pfls, locs);
513       nb += 2 * types.size(); // x 2 - a type can be on nodes and on cells at the same time
514     }
515
516   return nb;
517 }
518
519 //================================================================================
520 /*!
521  * \brief Transorm a profile into ids of mesh elements
522  */
523 //================================================================================
524
525 void SauvWriter::makeProfileIDs( SubMesh*                          sm,
526                                  INTERP_KERNEL::NormalizedCellType type,
527                                  const DataArrayInt*               profile )
528 {
529   MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh >
530     mesh = _fileMesh->getGenMeshAtLevel(sm->_dimRelExt);
531   const MEDCouplingUMesh* uMesh = dynamic_cast< const MEDCouplingUMesh* > ((const MEDCouplingMesh*) mesh );
532
533   if ( sm->_dimRelExt == 1 ) type = INTERP_KERNEL::NORM_POINT1;
534   vector< int >& ids = sm->_cellIDsByType[ type ];
535
536   if ( sm->_dimRelExt == 1 || !uMesh )
537     {
538       // profile on nodes or mesh is CARTESIAN
539       if ( profile )
540         {
541           ids.assign( profile->begin(), profile->end() );
542         }
543       else // on all
544         {
545           ids.resize( sm->_dimRelExt == 1 ? mesh->getNumberOfNodes() : mesh->getNumberOfCells() );
546           for ( size_t i = 0; i < ids.size(); ++i )
547             ids[i]=i;
548         }
549     }
550   else
551     {
552       // profile on cells
553       vector<int> code(3);
554       code[0] = type;
555       if ( profile ) // on profile
556         {
557           code[1] = profile->getNumberOfTuples();
558           code[2] = 0;
559         }
560       else // on all cells
561         {
562           code[1] = mesh->getNumberOfCellsWithType( type );
563           code[2] = -1;
564         }
565       vector<const DataArrayInt *> idsPerType( 1, profile );
566       MEDCouplingAutoRefCountObjectPtr<DataArrayInt>
567         resIDs = uMesh->checkTypeConsistencyAndContig( code, idsPerType );
568       ids.assign( resIDs->begin(), resIDs->end() );
569     }
570 }
571
572 //================================================================================
573 /*!
574  * \brief Write its data into the SAUVE file
575  */
576 //================================================================================
577
578 void SauvWriter::write(const char* fileName)
579 {
580   std::fstream fileStream;
581   fileStream.open( fileName, ios::out);
582   if
583 #ifdef WIN32
584     ( !fileStream || !fileStream.is_open() )
585 #else
586     ( !fileStream || !fileStream.rdbuf()->is_open() )
587 #endif
588       THROW_IK_EXCEPTION("Can't open the file |"<<fileName<<"|");
589   _sauvFile = &fileStream;
590
591   _subs.clear();
592   _famIDs2Sub.clear();
593   _profile2Sub.clear();
594   _longNames[ LN_MAIL ].clear();
595   _longNames[ LN_CHAM ].clear();
596   _longNames[ LN_COMP ].clear();
597
598   map<string,int> fldNamePrefixMap;
599
600   writeFileHead();
601   writeSubMeshes();
602   writeNodes();
603   writeNodalFields(fldNamePrefixMap);
604   writeElemFields(fldNamePrefixMap);
605   writeLongNames();
606   writeLastRecord();
607
608   _sauvFile->close();
609 }
610 //================================================================================
611 /*!
612  * \brief Writes "ENREGISTREMENT DE TYPE" 4 and 7
613  */
614 //================================================================================
615
616 void SauvWriter::writeFileHead()
617 {
618   MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh > mesh = _fileMesh->getGenMeshAtLevel(0);
619
620   *_sauvFile
621     << " ENREGISTREMENT DE TYPE   4" << endl
622     << " NIVEAU  16 NIVEAU ERREUR   0 DIMENSION   " << mesh->getSpaceDimension() <<endl
623     << " DENSITE 0.00000E+00" << endl
624     << " ENREGISTREMENT DE TYPE   7" << endl
625     << " NOMBRE INFO CASTEM2000   8" <<endl
626     << " IFOUR  -1 NIFOUR   0 IFOMOD  -1 IECHO   1 IIMPI   0 IOSPI   0 ISOTYP   1" << endl
627     << " NSDPGE     0" << endl;
628 }
629
630 //================================================================================
631 /*!
632  * \brief Writes names of objects
633  */
634 //================================================================================
635
636 void SauvWriter::writeNames( const map<string,int>& nameNbMap )
637 {
638   if ( !nameNbMap.empty() )
639   {
640     // write names of objects
641     // * 8001       FORMAT(8(1X,A8))
642     TFieldCounter fcount( *_sauvFile, 8 );
643     *_sauvFile << left;
644     map<string,int>::const_iterator nameNbIt = nameNbMap.begin();
645     for ( ; nameNbIt != nameNbMap.end(); nameNbIt++, fcount++ )
646       *_sauvFile << " " << setw(8) << nameNbIt->first;
647     fcount.stop();
648     *_sauvFile << right;
649
650     // write IDs of named objects in the pile
651     // *  8000 FORMAT(10I8)
652     nameNbIt = nameNbMap.begin();
653     for ( fcount.init(10); nameNbIt != nameNbMap.end(); nameNbIt++, fcount++ )
654       *_sauvFile << setw(8) << nameNbIt->second;
655   }
656 }
657
658 //================================================================================
659 /*!
660  * \brief Writes "PILE NUMERO   1"
661  */
662 //================================================================================
663
664 void SauvWriter::writeSubMeshes()
665 {
666   int nbSauvObjects;
667   map<string,int> nameNbMap;
668   fillSubMeshes( nbSauvObjects, nameNbMap );
669
670   // * 800   FORMAT (' ENREGISTREMENT DE TYPE', I4)
671   *_sauvFile << " ENREGISTREMENT DE TYPE   2" << endl;
672   // * 801     FORMAT(' PILE NUMERO',I4,'NBRE OBJETS NOMMES',I8,'NBRE OBJETS',I8)
673   *_sauvFile << " PILE NUMERO   1NBRE OBJETS NOMMES" << setw(8) << nameNbMap.size() <<
674     "NBRE OBJETS" << setw(8) << nbSauvObjects <<endl;
675
676   writeNames( nameNbMap );
677
678   TFieldCounter fcount( *_sauvFile, 10 ); // 10 intergers per line
679
680   for ( size_t iSub = 0; iSub < _subs.size(); ++iSub )
681     {
682       SubMesh& sm = _subs[iSub];
683       if ( sm._nbSauvObjects < 1 ) continue;
684
685       // The first record of each sub-mesh writes
686       // - type of cells; zero means a compound object whose the 2nd record enumerates its components
687       // - number of components of a compound object
688       // - number of references; each reference means a "pointer" to this sub-mesh
689       // - number of nodes per cell
690       // - number of cells
691
692       if ( !sm._subs.empty() )
693         {
694           writeCompoundSubMesh(iSub);
695         }
696       else
697         {
698           // write each sub-type as a SAUV sub-mesh
699           MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh >
700             mesh = _fileMesh->getGenMeshAtLevel( sm._dimRelExt );
701           MEDCouplingAutoRefCountObjectPtr< MEDCouplingUMesh>
702             umesh = mesh->buildUnstructured();
703
704           for ( int iType=0; iType < sm.cellIDsByTypeSize(); ++iType )
705             {
706               const vector<int>& cellIDs = sm._cellIDsByType[iType];
707               if ( cellIDs.empty() ) continue;
708
709               INTERP_KERNEL::NormalizedCellType
710                 cellType = INTERP_KERNEL::NormalizedCellType( iType );
711               const INTERP_KERNEL::CellModel &
712                 cell = INTERP_KERNEL::CellModel::GetCellModel( cellType );
713               int castemType       = SauvUtilities::med2gibiGeom( cellType );
714               unsigned nbElemNodes = cell.getNumberOfNodes();
715               unsigned nbElems     = cellIDs.size();
716
717               *_sauvFile << setw(8) << castemType
718                         << zeroI8
719                         << zeroI8
720                         << setw(8) << nbElemNodes
721                         << setw(8) << nbElems << endl;
722
723               // write color of each element
724               // * 8000 FORMAT(10I8)
725               for ( size_t i = 0; i < nbElems; ++i, fcount++ ) *_sauvFile << zeroI8;
726               fcount.stop();
727
728               // write connectivity
729               // gibi IDs are in FORTRAN mode while MEDCoupling IDs are in C mode
730               if ( sm._dimRelExt == 1 ) // nodes
731                 {
732                   for ( size_t i = 0; i < nbElems; ++i, fcount++ )
733                     *_sauvFile << setw(8) << ( cellIDs[i] + 1 );
734                 }
735               else
736                 {
737                   // indices to transform MED connectivity to GIBI one
738                   const int * toMedConn = getGibi2MedQuadraticInterlace( cellType );
739
740                   vector< int > cellConn( nbElemNodes ), transformedConn( nbElemNodes );
741                   for ( size_t i = 0; i < nbElems; ++i )
742                     {
743                       cellConn.clear();
744                       umesh->getNodeIdsOfCell( cellIDs[i], cellConn );
745                       if ( toMedConn )
746                         {
747                           for ( unsigned j = 0; j < nbElemNodes; ++j )
748                             transformedConn[ toMedConn[ j ]] = cellConn[ j ];
749                           cellConn.swap( transformedConn );
750                         }
751                       for ( unsigned j = 0; j < nbElemNodes; ++j, fcount++ )
752                         *_sauvFile << setw(8) << ( cellConn[j] + 1 );
753                     }
754                 }
755               fcount.stop();
756
757             } // loop on cell types
758         } // not a compound object
759     } // loop on sub-meshes
760 }
761
762 //================================================================================
763 /*!
764  * \brief Writes a sum-mesh composed of other sum-meshes
765  * This submesh corresponds to a med mesh or group composed of families
766  */
767 //================================================================================
768
769 void SauvWriter::writeCompoundSubMesh(int iSub)
770 {
771   SubMesh& sm = _subs[iSub];
772   if ( sm._nbSauvObjects < 1 || sm._subs.empty()) return;
773
774   vector< int > subIDs;
775   for ( size_t i = 0; i < sm._subs.size(); ++i ) // loop on sub-meshes of families
776     for ( int j = 0; j < sm._subs[i]->_nbSauvObjects; ++j )
777       subIDs.push_back( sm._subs[i]->_id + j );
778       
779   *_sauvFile << zeroI8
780              << setw(8) << subIDs.size()
781              << zeroI8
782              << zeroI8
783              << zeroI8 << endl;
784
785   TFieldCounter fcount( *_sauvFile, 10 ); // 10 intergers per line
786   for ( size_t i = 0; i < subIDs.size(); ++i, fcount++ )
787     *_sauvFile << setw(8) << subIDs[i];
788 }
789
790 //================================================================================
791 /*!
792  * \brief Write piles relating to nodes
793  */
794 //================================================================================
795
796 void SauvWriter::writeNodes()
797 {
798   MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh > mesh = _fileMesh->getGenMeshAtLevel( 1 );
799   MEDCouplingAutoRefCountObjectPtr< MEDCouplingUMesh > umesh = mesh->buildUnstructured();
800
801   // write the index connecting nodes with their coodrinates
802
803   const int nbNodes = umesh->getNumberOfNodes();
804   *_sauvFile << " ENREGISTREMENT DE TYPE   2" << endl
805              << " PILE NUMERO  32NBRE OBJETS NOMMES       0NBRE OBJETS" << setw(8) << nbNodes << endl;
806   *_sauvFile << setw(8) << nbNodes << endl;
807   //
808   TFieldCounter fcount( *_sauvFile, 10 );// * 8000 FORMAT(10I8)
809   for ( int i = 0; i < nbNodes; ++i, fcount++ )
810     *_sauvFile << setw(8) << i + 1; 
811   fcount.stop();
812
813   // write coordinates and density of nodes
814
815   *_sauvFile << " ENREGISTREMENT DE TYPE   2" << endl;
816   *_sauvFile << " PILE NUMERO  33NBRE OBJETS NOMMES       0NBRE OBJETS       1" << endl;
817   // 
818   const int dim = umesh->getSpaceDimension();
819   const int nbValues = nbNodes * ( dim + 1 );
820   *_sauvFile << setw(8) << nbValues << endl;
821
822   // * 8003   FORMAT(1P,3E22.14)
823   const char* density = "  0.00000000000000E+00";
824   fcount.init(3);
825   _sauvFile->precision(14);
826   _sauvFile->setf( ios_base::scientific, ios_base::floatfield );
827   _sauvFile->setf( ios_base::uppercase );
828   MEDCouplingAutoRefCountObjectPtr< DataArrayDouble> coordArray = umesh->getCoordinatesAndOwner();
829   const double precision = 1.e-99; // PAL12077
830   for ( int i = 0; i < nbNodes; ++i)
831   {
832     for ( int j = 0; j < dim; ++j, fcount++ )
833       {
834         double coo = coordArray->getIJ( i, j );
835         bool  zero = ( -precision < coo && coo < precision );
836         *_sauvFile << setw(22) << ( zero ? 0.0 : coo );
837       }
838     *_sauvFile << density;
839     fcount++;
840   }
841 }
842
843 //================================================================================
844 /*!
845  * \brief Store correspondence between GIBI (short) and MED (long) names
846  *
847  * IMP 0020434: mapping GIBI names to MED names
848  * Store correspondence between GIBI and MED names as one PILE_STRINGS and one
849  * PILE_TABLES (in three tables: MED_MAIL, MED_CHAM and MED_COMP)
850  */
851 //================================================================================
852
853 void SauvWriter::writeLongNames()
854 {
855   int nbTables =
856     3 - _longNames[ LN_MAIL ].empty() - _longNames[ LN_CHAM ].empty() - _longNames[ LN_COMP ].empty();
857   if (nbTables == 0) return;
858
859   // ---------------------
860   // Write the TABLE pile
861   // ---------------------
862
863   *_sauvFile << " ENREGISTREMENT DE TYPE   2" << endl
864         << " PILE NUMERO  10NBRE OBJETS NOMMES" << setw(8) << nbTables
865         << "NBRE OBJETS" << setw(8) << nbTables << endl;
866   // table names
867   if (!_longNames[ LN_MAIL ].empty()) *_sauvFile << " MED_MAIL";
868   if (!_longNames[ LN_CHAM ].empty()) *_sauvFile << " MED_CHAM";
869   if (!_longNames[ LN_COMP ].empty()) *_sauvFile << " MED_COMP";
870   *_sauvFile << endl;
871   // table indices
872   for ( int i = 0; i < nbTables; ++i ) *_sauvFile << setw(8) << i+1;
873   *_sauvFile << endl;
874
875   string theWholeString; // concatenated long names
876   vector<int> theOffsets;
877   int iStr = 1;
878   TFieldCounter fcount (*_sauvFile, 10);
879
880   for ( int iTbl = 0; iTbl < LN_NB; ++iTbl )
881     {
882       vector<nameGIBItoMED>& longNames = _longNames[ iTbl ];
883       if ( longNames.empty() ) continue;
884       const bool isComp = ( iTbl == LN_COMP);
885
886       // to assure unique MED names
887       set<string> medUniqueNames;
888
889       *_sauvFile << setw(8) << longNames.size()*4 << endl; // Nb of table values
890
891       vector<nameGIBItoMED>::iterator itGIBItoMED = longNames.begin();
892       for (; itGIBItoMED != longNames.end(); itGIBItoMED++, iStr++)
893         {
894           // PILE of i-th key (med name)
895           *_sauvFile << setw(8) << PILE_STRINGS;
896           fcount++;
897           // ID of i-th key (med name)
898           *_sauvFile << setw(8) << iStr;
899           fcount++;
900           // PILE of i-th value (gibi name)
901           *_sauvFile << setw(8) << itGIBItoMED->gibi_pile;
902           fcount++;
903           // ID of i-th value (gibi name)
904           *_sauvFile << setw(8) << ( isComp ? ++iStr : itGIBItoMED->gibi_id );
905           fcount++;
906
907           // add a MED name to the string (while making it be unique for sub-meshes and fields)
908           string aMedName = itGIBItoMED->med_name;
909           if ( !isComp )
910             for (int ind = 1; !medUniqueNames.insert(aMedName).second; ++ind )
911               aMedName = itGIBItoMED->med_name + "_" + SauvUtilities::toString( ind );
912           theWholeString += aMedName;
913
914           // add an offset
915           theOffsets.push_back( theWholeString.size() );
916           if ( isComp )
917             {
918               theWholeString += itGIBItoMED->gibi_name;
919               theOffsets.push_back( theWholeString.size() );
920             }
921         }
922       fcount.stop();
923     }
924
925   // ----------------------
926   // Write the STRING pile
927   // ----------------------
928
929   const int nbNames = theOffsets.size();
930   *_sauvFile << " ENREGISTREMENT DE TYPE   2" << endl
931         << " PILE NUMERO  27NBRE OBJETS NOMMES" << zeroI8 << "NBRE OBJETS" << setw(8) << nbNames << endl
932         << setw(8) << theWholeString.length() << setw(8) << nbNames << endl;
933
934   // write the whole string
935   const int fixedLength = 71;
936   for ( string::size_type aPos = 0; aPos < theWholeString.length(); aPos += fixedLength)
937     *_sauvFile << setw(72) << theWholeString.substr(aPos, fixedLength) << endl;
938
939   // write the offsets
940   for ( size_t i = 0; i < theOffsets.size(); ++i, fcount++ )
941     *_sauvFile << setw(8) << theOffsets[i];
942 }
943
944 //================================================================================
945 /*!
946  * \brief Write beginning of field record
947  */
948 //================================================================================
949
950 void SauvWriter::writeFieldNames( const bool                 isNodal,
951                                   std::map<std::string,int>& fldNamePrefixMap)
952 {
953   vector< MEDCouplingAutoRefCountObjectPtr< MEDFileFieldMultiTS > >&
954     flds = isNodal ? _nodeFields : _cellFields;
955   map<string,int> nameNbMap;
956
957   for ( size_t iF = 0; iF < flds.size(); ++iF )
958     {
959       string name = addName( nameNbMap, fldNamePrefixMap, flds[iF]->getName(), iF+1 );
960       nameGIBItoMED aMEDName;
961       aMEDName.gibi_pile = isNodal ? PILE_NODES_FIELD : PILE_FIELD;
962       aMEDName.gibi_id   = iF+1;
963       aMEDName.med_name  = name;
964       _longNames[ LN_CHAM ].push_back(aMEDName);
965     }
966
967   *_sauvFile << " ENREGISTREMENT DE TYPE   2" << endl
968              << ( isNodal ? " PILE NUMERO   2" : " PILE NUMERO  39")
969              << "NBRE OBJETS NOMMES" << setw(8) << nameNbMap.size()
970              << "NBRE OBJETS"        << setw(8) << flds.size() << endl;
971   writeNames( nameNbMap );
972 }
973
974 //================================================================================
975 /*!
976  * \brief Make short names of field components
977  *
978  * IMP 0020434: mapping GIBI names to MED names
979  */
980 //================================================================================
981
982 void SauvWriter::makeCompNames(const string&         fieldName,
983                                const vector<string>& compInfo,
984                                map<string, string>&  mapMedToGibi)
985 {
986   for ( size_t i = 0; i < compInfo.size(); ++i )
987     mapMedToGibi[compInfo[i]] = cleanName( compInfo[i] );
988
989   int compIndex = 1;
990   map<string, string>::iterator namesIt = mapMedToGibi.begin();
991   for (; namesIt != mapMedToGibi.end(); namesIt++)
992     {
993       string & compGibiName = (*namesIt).second;
994       if (compGibiName.size() > 4) {
995         // use new name in form "CXXX", where "XXX" is a number
996         do
997           {
998             compGibiName = SauvUtilities::toString( compIndex++ );
999             if ( compGibiName.size() < 3 )
1000               compGibiName.insert( 0, 3 - compGibiName.size(), '0' );
1001             compGibiName = "C" + compGibiName;
1002           }
1003         while (mapMedToGibi.count(compGibiName) > 0); // real component name could be CXXX
1004       }
1005
1006       string compMedName = fieldName + "." + namesIt->first;
1007       nameGIBItoMED aMEDName;
1008       aMEDName.med_name  = compMedName;
1009       aMEDName.gibi_pile = PILE_STRINGS;
1010       aMEDName.gibi_name = compGibiName;
1011       _longNames[ LN_COMP ].push_back(aMEDName);
1012     }
1013 }
1014
1015 //================================================================================
1016 /*!
1017  * \brief Writes "PILE NUMERO   2": fields on nodes
1018  */
1019 //================================================================================
1020
1021 void SauvWriter::writeNodalFields(map<string,int>& fldNamePrefixMap)
1022 {
1023   writeFieldNames( /*isNodal=*/true, fldNamePrefixMap );
1024
1025   TFieldCounter fcount (*_sauvFile, 10);
1026
1027   // EXAMPLE ( with no values )
1028
1029   // (1)       4       7       2       1
1030   // (2)     -88       0       3     -89       0       1     -90       0       2     -91
1031   // (2)       0       1
1032   // (3) FX   FY   FZ   FZ   FX   FY   FLX
1033   // (4)       0       0       0       0       0       0       0
1034   // (5)           cree  par  muc pri
1035   // (6)
1036   // (7)       2
1037   for ( size_t iF = 0; iF < _nodeFields.size(); ++iF )
1038     {
1039       // (1) write nb subcomponents, nb components(total)
1040       vector< pair<int,int> >  iters = _nodeFields[iF]->getIterations();
1041       const vector<string>& compInfo = _nodeFields[iF]->getInfo();
1042       const int nbSub = iters.size();
1043       const int nbComp = compInfo.size();
1044       const int totalNbComp = nbSub * nbComp;
1045       *_sauvFile << setw(8) << nbSub
1046                  << setw(8) << totalNbComp
1047                  << setw(8) << -1         // IFOUR
1048                  << setw(8) << 0 << endl; // nb attributes
1049
1050       // (2) for each sub-component (iteration)
1051       // write support, number of values and number of components
1052       fcount.init(10);
1053       vector< int > vals(3);
1054       for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1055         {
1056           pair<int,int> it = iters[iIt];
1057
1058           vector<INTERP_KERNEL::NormalizedCellType> types;
1059           vector< vector<TypeOfField> > typesF;
1060           vector< vector<string> > pfls, locs;
1061           vector< vector< std::pair<int,int> > > valsVec;
1062           valsVec=_nodeFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName(),
1063                                                           types, typesF, pfls, locs);
1064           // believe that there can be only one type in a nodal field,
1065           // so do not use a loop on types
1066           if ( pfls[0][0].empty() ) pfls[0][0] = noProfileName( types[0] );
1067           map< string, SubMesh* >::iterator pfl2Sub = _profile2Sub.find( pfls[0][0] );
1068           if ( pfl2Sub == _profile2Sub.end() )
1069             THROW_IK_EXCEPTION( "SauvWriter::writeNodalFields(): no sub-mesh for profile |"
1070                                 << pfls[0][0] << "|");
1071           vals[0] = -pfl2Sub->second->_id;
1072           vals[1] = (valsVec[0][0].second-valsVec[0][0].first);
1073           vals[2] = compInfo.size();
1074           for ( size_t i = 0; i < vals.size(); ++i, fcount++ )
1075             *_sauvFile << setw(8) << vals[i];
1076         }
1077       fcount.stop();
1078
1079       // (3) Write names of components
1080       map<string, string> mapMedToGibi;
1081       makeCompNames( _nodeFields[iF]->getName(), compInfo, mapMedToGibi );
1082       fcount.init(8);
1083       *_sauvFile << left;
1084       for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1085         for ( size_t i = 0; i < compInfo.size(); ++i, fcount++ )
1086           *_sauvFile << " "  << setw(4) << mapMedToGibi[compInfo[i]];
1087       *_sauvFile << right;
1088       fcount.stop();
1089
1090       // (4) nb harmonics
1091       fcount.init(10);
1092       for ( size_t i = 0; i < (std::size_t)totalNbComp; ++i, fcount++ )
1093         *_sauvFile << " "  << setw(8) << 0;
1094       fcount.stop();
1095
1096       string description = _nodeFields[iF]->getName();
1097       *_sauvFile << endl;                                         // (5) TYPE
1098       *_sauvFile << setw(72) << description.substr(0,71) << endl; // (6) TITRE
1099       //*_sauvFile << endl;                                         // (7) 0 attributes
1100
1101       // write values of each component
1102       fcount.init( 3 ); // 3 values per a line
1103       for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1104         {
1105           pair<int,int> it = iters[iIt];
1106
1107           vector<INTERP_KERNEL::NormalizedCellType> types;
1108           vector< vector<TypeOfField> > typesF;
1109           vector< vector<string> > pfls, locs;
1110           vector< vector< std::pair<int,int> > > valsVec;
1111           valsVec = _nodeFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName(),
1112                                                             types, typesF, pfls, locs);
1113           // believe that there can be only one type in a nodal field,
1114           // so do not perform a loop on types
1115           const DataArrayDouble* valsArray = _nodeFields[iF]->getUndergroundDataArray(it.first, it.second);
1116           for ( size_t j = 0; j < compInfo.size(); ++j )
1117             {
1118               for ( size_t i = valsVec[0][0].first; i < (std::size_t)valsVec[0][0].second; ++i, fcount++ )
1119                 *_sauvFile << setw(22) << valsArray->getIJ( i, j );
1120               fcount.stop();
1121             }
1122         }
1123     } // loop on fiels
1124 }
1125
1126 //================================================================================
1127 /*!
1128  * \brief Writes "PILE NUMERO  39": fields on cells
1129  */
1130 //================================================================================
1131
1132 void SauvWriter::writeElemFields(map<string,int>& fldNamePrefixMap)
1133 {
1134   writeFieldNames( /*isNodal=*/false, fldNamePrefixMap );
1135
1136   TFieldCounter fcount (*_sauvFile, 10);
1137
1138   // REAL EXAMPLE
1139
1140   // (1)        1       2       6      16
1141   // (2)                                                         CARACTERISTIQUES
1142   // (3)      -15  317773       4       0       0       0      -2       0       3
1143   // (4)             317581
1144   // (5)  0
1145   // (6)   317767  317761  317755  317815
1146   // (7)  YOUN     NU       H        SIGY
1147   // (8)  REAL*8            REAL*8            REAL*8            REAL*8
1148   // (9)        1       1       0       0
1149   // (10)  2.00000000000000E+05
1150   // (9)       1       1       0       0
1151   // (10)  3.30000000000000E-01
1152   // (9)       1       1       0       0
1153   // (10)  1.00000000000000E+04
1154   // (9)       6     706       0       0
1155   // (10)  1.00000000000000E+02  1.00000000000000E+02  1.00000000000000E+02
1156   // (10)  1.00000000000000E+02  1.00000000000000E+02  1.00000000000000E+02
1157   // (10)  ...
1158
1159   for ( size_t iF = 0; iF < _cellFields.size(); ++iF )
1160     {
1161       // count nb of sub-components
1162       int iSub, nbSub = 0;
1163       vector< pair<int,int> >  iters = _cellFields[iF]->getIterations();
1164       for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1165         {
1166           pair<int,int> it = iters[iIt];
1167
1168           vector<INTERP_KERNEL::NormalizedCellType> types;
1169           vector< vector<TypeOfField> > typesF;
1170           vector< vector<string> > pfls, locs;
1171           vector< vector< std::pair<int,int> > > valsVec;
1172           valsVec = _cellFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName(),
1173                                                             types, typesF, pfls, locs);
1174           for ( size_t i = 0; i < valsVec.size(); ++i )
1175             nbSub += valsVec[i].size();
1176         }
1177       // (1) write nb sub-components, title length
1178       *_sauvFile << setw(8) << nbSub
1179                  << setw(8) << -1 // whatever
1180                  << setw(8) << 6  // whatever
1181                  << setw(8) << 72 << endl; // title length
1182       // (2) title
1183       string title = _cellFields[iF]->getName();
1184       *_sauvFile << setw(72) << title.substr(0,71) << endl;
1185       *_sauvFile << setw(72) << " " << endl;
1186
1187       // (3) support, nb components
1188       vector<int> vals(9, 0);
1189       const vector<string>& compInfo = _cellFields[iF]->getInfo();
1190       vals[2] = compInfo.size();
1191       fcount.init(10);
1192       for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1193         {
1194           pair<int,int> it = iters[iIt];
1195
1196           vector<INTERP_KERNEL::NormalizedCellType> types;
1197           vector< vector<TypeOfField> > typesF;
1198           vector< vector<string> > pfls, locs;
1199           _cellFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName(),
1200                                                   types, typesF, pfls, locs);
1201           for ( size_t iType = 0; iType < pfls.size(); ++iType )
1202             for ( size_t iP = 0; iP < pfls[iType].size(); ++iP )
1203               {
1204                 if ( pfls[iType][iP].empty() ) pfls[iType][iP] = noProfileName( types[iType] );
1205                 map< string, SubMesh* >::iterator pfl2Sub = _profile2Sub.find( pfls[iType][iP] );
1206                 if ( pfl2Sub == _profile2Sub.end() )
1207                   THROW_IK_EXCEPTION( "SauvWriter::writeElemFields(): no sub-mesh for profile |"
1208                                       << pfls[iType][iP] << "|");
1209                 const int supportID = pfl2Sub->second->_id;
1210                 vals[0] = -supportID;
1211
1212                 for ( size_t i = 0; i < vals.size(); ++i, fcount++ )
1213                   *_sauvFile << setw(8) << vals[ i ];
1214               }
1215         }
1216       fcount.stop();
1217
1218       // (4) dummy strings
1219       for ( fcount.init(4), iSub = 0; iSub < nbSub; ++iSub, fcount++ )
1220         *_sauvFile << "                  ";
1221       fcount.stop();
1222
1223       // (5) dummy strings
1224       for ( fcount.init(8), iSub = 0; iSub < nbSub; ++iSub, fcount++ )
1225         *_sauvFile << "         ";
1226       fcount.stop();
1227
1228       // loop on sub-components of a field, each of which refers to
1229       // a certain support and has its own number of components
1230       for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1231         {
1232           pair<int,int> it = iters[iIt];
1233           writeElemTimeStamp( iF, it.first, it.second );
1234         }
1235     } // loop on cell fields
1236 }
1237
1238 //================================================================================
1239 /*!
1240  * \brief Write one elemental time stamp
1241  */
1242 //================================================================================
1243
1244 void SauvWriter::writeElemTimeStamp(int iF, int iter, int order)
1245 {
1246   // (6)   317767  317761  317755  317815
1247   // (7)  YOUN     NU       H        SIGY
1248   // (8)  REAL*8            REAL*8            REAL*8            REAL*8
1249   // (9)        1       1       0       0
1250   // (10)  2.00000000000000E+05
1251   // (9)       1       1       0       0
1252   // (10)  3.30000000000000E-01
1253   // (9)       1       1       0       0
1254   // (10)  1.00000000000000E+04
1255   // (9)       6     706       0       0
1256   // (10)  1.00000000000000E+02  1.00000000000000E+02  1.00000000000000E+02
1257   // (10)  1.00000000000000E+02  1.00000000000000E+02  1.00000000000000E+02
1258
1259   TFieldCounter fcount (*_sauvFile, 10);
1260
1261   vector<INTERP_KERNEL::NormalizedCellType> types;
1262   vector< vector<TypeOfField> > typesF;
1263   vector< vector<string> > pfls, locs;
1264   vector< vector< std::pair<int,int> > > valsVec;
1265   valsVec = _cellFields[iF]->getFieldSplitedByType( iter, order, _fileMesh->getName(),
1266                                                     types, typesF, pfls, locs);
1267   for ( size_t iType = 0; iType < pfls.size(); ++iType )
1268     for ( size_t iP = 0; iP < pfls[iType].size(); ++iP )
1269       {
1270         const vector<string>& compInfo = _cellFields[iF]->getInfo();
1271
1272         // (6) component addresses
1273         int iComp = 0, nbComp = compInfo.size();
1274         for ( fcount.init(10); iComp < nbComp; ++iComp, fcount++ )
1275           *_sauvFile << setw(8) << 777; // a good number
1276         fcount.stop();
1277
1278         // (7) component names
1279         map<string, string> mapMedToGibi;
1280         makeCompNames( _cellFields[iF]->getName(), compInfo, mapMedToGibi );
1281         *_sauvFile << left;
1282         for ( fcount.init(8), iComp = 0; iComp < nbComp; ++iComp, fcount++ )
1283           *_sauvFile << " "  << setw(8) << mapMedToGibi[compInfo[iComp]];
1284         fcount.stop();
1285
1286         // (8) component types
1287         for ( fcount.init(4), iComp = 0; iComp < nbComp; ++iComp, fcount++ )
1288           *_sauvFile << " "  << setw(17) << "REAL*8";
1289         fcount.stop();
1290         *_sauvFile << right;
1291
1292         // (9) nb values per element, nb of elements
1293         int nbPntPerCell = 1;
1294         if ( !locs[iType][iP].empty() )
1295           {
1296             int locID = _cellFields[iF]->getLocalizationId( locs[iType][iP].c_str() );
1297             nbPntPerCell = _cellFields[iF]->getNbOfGaussPtPerCell( locID );
1298           }
1299         else if ( typesF[iType][iP] == ON_GAUSS_NE )
1300           {
1301             nbPntPerCell = INTERP_KERNEL::CellModel::GetCellModel(types[iType]).getNumberOfNodes();
1302           }
1303
1304         // (10) values
1305         const std::pair<int,int>& bgEnd = valsVec[iType][iP];
1306         const DataArrayDouble* valArray = _cellFields[iF]->getUndergroundDataArray(iter, order);
1307         for ( iComp = 0; iComp < nbComp; ++iComp )
1308           {
1309             *_sauvFile << setw(8) << nbPntPerCell
1310                        << setw(8) << (bgEnd.second-bgEnd.first) / nbPntPerCell
1311                        << setw(8) << 0
1312                        << setw(8) << 0
1313                        << endl;
1314             fcount.init(3);
1315             for ( size_t i = bgEnd.first; i < (size_t) bgEnd.second; ++i, fcount++ )
1316               *_sauvFile << setw(22) << valArray->getIJ( i, iComp );
1317             fcount.stop();
1318           }
1319       }
1320 }
1321
1322 //================================================================================
1323 /*!
1324  * \brief Write the last record of the SAUV file
1325  */
1326 //================================================================================
1327
1328 void SauvWriter::writeLastRecord()
1329 {
1330   *_sauvFile << " ENREGISTREMENT DE TYPE   5" << endl;
1331   *_sauvFile << "LABEL AUTOMATIQUE :   1" << endl;
1332 }