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