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