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