1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // File : SauvWriter.cxx
20 // Created : Wed Aug 24 12:55:55 2011
21 // Author : Edward AGAPOV (eap)
23 #include "SauvWriter.hxx"
25 #include "InterpKernelException.hxx"
26 #include "MEDFileMesh.hxx"
27 #include "MEDFileField.hxx"
28 #include "MEDFileData.hxx"
29 #include "CellModel.hxx"
37 using namespace ParaMEDMEM;
38 using namespace SauvUtilities;
41 #define INFOS_MED(txt) cout << txt << endl;
45 const char* zeroI8 = " 0"; // FORMAT(I8)
47 // ============================================================
48 // the class writes endl to the file as soon as <limit> fields
49 // have been written after the last endl
50 // ============================================================
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(); }
67 //================================================================================
69 * \brief Return a name of a field support on all elements
71 //================================================================================
73 string noProfileName( INTERP_KERNEL::NormalizedCellType type )
75 return "INTERP_KERNEL::NormalizedCellType_" + SauvUtilities::toString( type );
78 //================================================================================
80 * \brief Remove white spaces from the head and tail
82 //================================================================================
84 string cleanName( const string& theName )
86 string name = theName;
89 // cut off leading white spaces
90 string::size_type firstChar = name.find_first_not_of(" \t");
91 if (firstChar < name.length())
93 name = name.substr(firstChar);
97 name = ""; // only whitespaces there - remove them
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);
107 //================================================================================
109 * \brief Converts MED long names into SAUVE short ones, returnes a healed long name
111 //================================================================================
113 string addName (map<string,int>& nameMap,
114 map<string,int>& namePrefixesMap,
115 const string& theName,
118 // Converts names like:
120 // TEMPERATURE_FLUIDE -> TEMPE001
121 // TEMPERATURE_SOLIDE -> TEMPE002
122 // PRESSION -> PRESSION
124 // VOLUM001 -> VOLUM001
125 // VOLUMOFOBJECT -> VOLUM003
126 // VOLUM002 -> VOLUM002
127 string healedName = cleanName(theName);
130 if (!healedName.empty())
132 string name = healedName;
133 int len = name.length();
134 for (int i = 0; i < len; ++i)
135 name[i] = toupper(name[i]);
137 bool doResave = false; // only for tracing
139 // I. Save a short name as it is
142 INFOS_MED("Save <" << theName << "> as <" << name << ">");
144 map<string,int>::iterator it = nameMap.find(name);
145 if (it != nameMap.end())
147 // There is already such name in the map.
149 // a. Replace in the map the old pair by the current one
150 int old_ind = nameMap[name];
152 // b. Rebuild the old pair (which was in the map,
153 // it seems to be built automatically by step II)
155 // continue with step II
156 doResave = true; // only for tracing
161 nameMap.insert(make_pair(name, ind));
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
167 int new_loc_index = atoi(name.c_str() + 5);
168 if (new_loc_index > 0)
171 string str = name.substr(0,5);
172 if (namePrefixesMap.find(str) != namePrefixesMap.end())
174 int old_loc_index = namePrefixesMap[str];
175 if (new_loc_index < old_loc_index) new_loc_index = old_loc_index;
177 namePrefixesMap[str] = new_loc_index;
184 // II. Cut long name and add a numeric suffix
186 // first 5 or less characters of the name
187 if (len > 5) name = name.substr(0,5);
190 map<string,int>::iterator name2ind = namePrefixesMap.insert( make_pair( name, 0 )).first;
191 string numSuffix = SauvUtilities::toString( ++(name2ind->second) );
193 if ( numSuffix.size() + name.size() > 8 )
194 THROW_IK_EXCEPTION("Can't write not unique name: " << healedName);
196 if ( numSuffix.size() < 3 )
197 numSuffix.insert( 0, 3 - numSuffix.size(), '0' );
200 nameMap.insert(make_pair(name, ind));
204 INFOS_MED("Resave previous <" << healedName << "> as <" << name << ">");
208 INFOS_MED("Save <" << theName << "> as <" << name << ">");
215 //================================================================================
217 * \brief Creates SauvWriter
219 //================================================================================
221 SauvWriter* SauvWriter::New()
223 return new SauvWriter;
226 //================================================================================
228 * \brief Fills own DS by MEDFileData
230 //================================================================================
232 void SauvWriter::setMEDFileDS(const MEDFileData* medData,
235 if ( !medData) THROW_IK_EXCEPTION("NULL MEDFileData");
237 MEDFileMeshes * meshes = medData->getMeshes();
238 MEDFileFields * fields = medData->getFields();
239 if ( !meshes) THROW_IK_EXCEPTION("No meshes in MEDFileData");
241 _fileMesh = meshes->getMeshAtPos( meshIndex );
242 _fileMesh->incrRef();
245 for ( int i = 0; i < fields->getNumberOfFields(); ++i )
247 MEDFileFieldMultiTS * f = fields->getFieldAtPos(i);
248 if ( f->getMeshName() == _fileMesh->getName() )
250 vector< vector<TypeOfField> > fTypes = f->getTypesOfFieldAvailable();
251 if ( fTypes[0].size() == 1 && fTypes[0][0] == ON_NODES )
252 _nodeFields.push_back( f );
254 _cellFields.push_back( f );
259 //================================================================================
261 * \brief Adds a submesh
263 //================================================================================
265 SauvWriter::SubMesh* SauvWriter::addSubMesh(const std::string& name, int dimRelExt)
267 if ( _subs.capacity() < _subs.size() + 1 )
268 THROW_IK_EXCEPTION("SauvWriter: INTERNAL error, wrong evaluation of nb of sub-meshes");
269 _subs.resize( _subs.size() + 1 );
270 SubMesh& sm = _subs.back();
272 sm._dimRelExt = dimRelExt;
275 //================================================================================
277 * \brief Returns nb of cell types
279 //================================================================================
281 int SauvWriter::SubMesh::nbTypes() const
284 for (int i = 0; i < cellIDsByTypeSize(); ++i )
285 nb += int( !_cellIDsByType[i].empty() );
289 //================================================================================
293 //================================================================================
295 void SauvWriter::fillSubMeshes( int& nbSauvObjects, map<string,int>& nameNbMap )
297 // evaluate nb of _subs in order to avoid re-allocation of _subs
298 int nbSubs = 1; // for the very mesh
299 nbSubs += _fileMesh->getFamilyInfo().size() + 4; // + 4 zero families (for each dimRelExt)
300 nbSubs += _fileMesh->getGroupInfo().size();
301 nbSubs += evaluateNbProfileSubMeshes();
303 _subs.reserve( nbSubs );
305 fillFamilySubMeshes();
306 fillGroupSubMeshes();
307 fillProfileSubMeshes();
309 // fill names of SubMesh'es and count nb of sauv sub-meshes they will be stored into
311 map<string,int> namePrefixMap;
312 for ( size_t i = 0; i < _subs.size(); ++i )
314 SubMesh& sm = _subs[i];
316 sm._nbSauvObjects = 0;
317 if ( sm._subs.empty() )
319 sm._nbSauvObjects = sm.nbTypes();
323 sm._nbSauvObjects = 1;
326 sm._id = nbSauvObjects+1;
327 nbSauvObjects += sm._nbSauvObjects;
329 if ( sm._nbSauvObjects )
330 sm._name = addName( nameNbMap, namePrefixMap, sm._name, sm._id );
332 if ( sm._nbSauvObjects && !sm._name.empty() )
334 nameGIBItoMED aMEDName;
335 aMEDName.gibi_pile = PILE_SOUS_MAILLAGE;
336 aMEDName.gibi_id = sm._id;
337 aMEDName.med_name = sm._name;
338 _longNames[ LN_MAIL ].push_back(aMEDName);
343 //================================================================================
345 * \brief fill sub-meshes of families
347 //================================================================================
349 void SauvWriter::fillFamilySubMeshes()
351 SubMesh* nilSm = (SubMesh*) 0;
352 std::vector<int> dims = _fileMesh->getNonEmptyLevelsExt();
353 for ( size_t iDim = 0; iDim < dims.size(); ++iDim )
355 int dimRelExt = dims[ iDim ];
356 MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh > mesh = _fileMesh->getGenMeshAtLevel(dimRelExt);
357 const DataArrayInt * famIds = _fileMesh->getFamilyFieldAtLevel(dimRelExt);
358 if ( !famIds ) continue;
361 SubMesh* curSubMesh = addSubMesh( "", dimRelExt ); // submesh of zero family
362 _famIDs2Sub[0] = curSubMesh;
363 int sub0Index = _subs.size()-1;
365 const int * famID = famIds->begin(), * famIDEnd = famIds->end();
366 for ( int cellID = 0; famID < famIDEnd; ++famID, cellID++ )
368 if ( *famID != curFamID )
371 map< int, SubMesh* >::iterator f2s = _famIDs2Sub.insert( make_pair( curFamID, nilSm )).first;
373 f2s->second = addSubMesh( "", dimRelExt ); // no names for families
374 curSubMesh = f2s->second;
376 INTERP_KERNEL::NormalizedCellType cellType =
377 dimRelExt == 1 ? INTERP_KERNEL::NORM_POINT1 : mesh->getTypeOfCell( cellID );
378 curSubMesh->_cellIDsByType[ cellType ].push_back( cellID );
381 if ( dimRelExt == 1 )
383 // clear submesh of nodal zero family
384 _famIDs2Sub[0]->_cellIDsByType[ INTERP_KERNEL::NORM_POINT1 ].clear();
386 else if ( dimRelExt == 0 )
388 // make a submesh including all cells
389 if ( sub0Index == (int)(_subs.size()-1) )
391 _famIDs2Sub[0]->_name = _fileMesh->getName(); // there is the zero family only
395 curSubMesh = addSubMesh( _fileMesh->getName(), dimRelExt );
396 if ( _famIDs2Sub[0]->nbTypes() == 0 )
397 sub0Index++; // skip an empty zero family
398 for ( size_t i = sub0Index; i < _subs.size()-1; ++i )
399 curSubMesh->_subs.push_back( & _subs[i] );
405 //================================================================================
407 * \brief fill sub-meshes of groups
409 //================================================================================
411 void SauvWriter::fillGroupSubMeshes()
413 const map<string, vector<string> >& grpFams = _fileMesh->getGroupInfo();
414 map<string, vector<string> >::const_iterator g2ff = grpFams.begin();
415 for ( ; g2ff != grpFams.end(); ++g2ff )
417 const string& groupName = g2ff->first;
418 const vector<string>& famNames = g2ff->second;
419 if ( famNames.empty() ) continue;
420 std::vector<SubMesh*> famSubMeshes( famNames.size() );
422 for ( size_t i = 0; i < famNames.size(); ++i )
424 int famID = _fileMesh->getFamilyId( famNames[i].c_str() );
425 map< int, SubMesh* >::iterator i2f = _famIDs2Sub.find( famID );
426 if ( i2f != _famIDs2Sub.end() )
428 famSubMeshes[ k ] = i2f->second;
432 // if a family exists but has no element, no submesh has been found for this family
433 // => we have to resize famSubMeshes with the number of submeshes stored
434 if (k != famNames.size())
435 famSubMeshes.resize(k);
436 SubMesh* grpSubMesh = addSubMesh( groupName, famSubMeshes[0]->_dimRelExt );
437 grpSubMesh->_subs.swap( famSubMeshes );
442 //================================================================================
444 * \brief fill sub-meshes of profiles
446 //================================================================================
448 void SauvWriter::fillProfileSubMeshes()
450 _profile2Sub.clear();
451 SubMesh* nilSm = (SubMesh*) 0;
452 for ( int isOnNodes = 0; isOnNodes < 2; ++isOnNodes )
454 vector< MEDCouplingAutoRefCountObjectPtr< MEDFileFieldMultiTS > >
455 fields = isOnNodes ? _nodeFields : _cellFields;
456 for ( size_t i = 0; i < fields.size(); ++i )
458 vector< pair<int,int> > iters = fields[i]->getIterations();
460 vector<INTERP_KERNEL::NormalizedCellType> types;
461 vector< vector<TypeOfField> > typesF;
462 vector< vector<string> > pfls, locs;
463 fields[i]->getFieldSplitedByType( iters[0].first, iters[0].second,
464 _fileMesh->getName(), types, typesF, pfls, locs);
466 for ( size_t iType = 0; iType < types.size(); ++iType )
468 if ( types[iType] == INTERP_KERNEL::NORM_ERROR )
469 dimRelExt = 1; // on nodes
471 dimRelExt = getDimension( types[iType] ) - _fileMesh->getMeshDimension();
472 for ( size_t iPfl = 0; iPfl < pfls[iType].size(); ++iPfl )
474 bool isOnAll = pfls[iType][iPfl].empty();
475 if ( isOnAll ) pfls[iType][iPfl] = noProfileName( types[iType] );
476 map< string, SubMesh* >::iterator pfl2sm =
477 _profile2Sub.insert( make_pair( pfls[iType][iPfl], nilSm )).first;
478 if ( !pfl2sm->second )
480 SubMesh* sm = pfl2sm->second = addSubMesh( "", dimRelExt ); // no names for profiles
481 const DataArrayInt * pfl = isOnAll ? 0 : fields[i]->getProfile( pfls[iType][iPfl].c_str() );
482 makeProfileIDs( sm, types[iType], pfl );
490 //================================================================================
492 * \brief Return max possible nb of sub-meshes to decsribe field supports
494 //================================================================================
496 int SauvWriter::evaluateNbProfileSubMeshes() const
499 for ( size_t i = 0; i < _nodeFields.size(); ++i )
500 nb += 1 + _nodeFields[i]->getPflsReallyUsed().size();
502 for ( size_t i = 0; i < _cellFields.size(); ++i )
504 nb += _cellFields[i]->getPflsReallyUsed().size();
506 vector< pair<int,int> > iters = _cellFields[i]->getIterations();
508 vector<INTERP_KERNEL::NormalizedCellType> types;
509 vector< vector<TypeOfField> > typesF;
510 vector< vector<string> > pfls, locs;
511 _cellFields[i]->getFieldSplitedByType( iters[0].first, iters[0].second,
512 _fileMesh->getName(), types, typesF, pfls, locs);
513 nb += 2 * types.size(); // x 2 - a type can be on nodes and on cells at the same time
519 //================================================================================
521 * \brief Transorm a profile into ids of mesh elements
523 //================================================================================
525 void SauvWriter::makeProfileIDs( SubMesh* sm,
526 INTERP_KERNEL::NormalizedCellType type,
527 const DataArrayInt* profile )
529 MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh >
530 mesh = _fileMesh->getGenMeshAtLevel(sm->_dimRelExt);
531 const MEDCouplingUMesh* uMesh = dynamic_cast< const MEDCouplingUMesh* > ((const MEDCouplingMesh*) mesh );
533 if ( sm->_dimRelExt == 1 ) type = INTERP_KERNEL::NORM_POINT1;
534 vector< int >& ids = sm->_cellIDsByType[ type ];
536 if ( sm->_dimRelExt == 1 || !uMesh )
538 // profile on nodes or mesh is CARTESIAN
541 ids.assign( profile->begin(), profile->end() );
545 ids.resize( sm->_dimRelExt == 1 ? mesh->getNumberOfNodes() : mesh->getNumberOfCells() );
546 for ( size_t i = 0; i < ids.size(); ++i )
555 if ( profile ) // on profile
557 code[1] = profile->getNumberOfTuples();
562 code[1] = mesh->getNumberOfCellsWithType( type );
565 vector<const DataArrayInt *> idsPerType( 1, profile );
566 MEDCouplingAutoRefCountObjectPtr<DataArrayInt>
567 resIDs = uMesh->checkTypeConsistencyAndContig( code, idsPerType );
568 ids.assign( resIDs->begin(), resIDs->end() );
572 //================================================================================
574 * \brief Write its data into the SAUVE file
576 //================================================================================
578 void SauvWriter::write(const char* fileName)
580 std::fstream fileStream;
581 fileStream.open( fileName, ios::out);
584 ( !fileStream || !fileStream.is_open() )
586 ( !fileStream || !fileStream.rdbuf()->is_open() )
588 THROW_IK_EXCEPTION("Can't open the file |"<<fileName<<"|");
589 _sauvFile = &fileStream;
593 _profile2Sub.clear();
594 _longNames[ LN_MAIL ].clear();
595 _longNames[ LN_CHAM ].clear();
596 _longNames[ LN_COMP ].clear();
598 map<string,int> fldNamePrefixMap;
603 writeNodalFields(fldNamePrefixMap);
604 writeElemFields(fldNamePrefixMap);
610 //================================================================================
612 * \brief Writes "ENREGISTREMENT DE TYPE" 4 and 7
614 //================================================================================
616 void SauvWriter::writeFileHead()
618 MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh > mesh = _fileMesh->getGenMeshAtLevel(0);
621 << " ENREGISTREMENT DE TYPE 4" << endl
622 << " NIVEAU 16 NIVEAU ERREUR 0 DIMENSION " << mesh->getSpaceDimension() <<endl
623 << " DENSITE 0.00000E+00" << endl
624 << " ENREGISTREMENT DE TYPE 7" << endl
625 << " NOMBRE INFO CASTEM2000 8" <<endl
626 << " IFOUR -1 NIFOUR 0 IFOMOD -1 IECHO 1 IIMPI 0 IOSPI 0 ISOTYP 1" << endl
627 << " NSDPGE 0" << endl;
630 //================================================================================
632 * \brief Writes names of objects
634 //================================================================================
636 void SauvWriter::writeNames( const map<string,int>& nameNbMap )
638 if ( !nameNbMap.empty() )
640 // write names of objects
641 // * 8001 FORMAT(8(1X,A8))
642 TFieldCounter fcount( *_sauvFile, 8 );
644 map<string,int>::const_iterator nameNbIt = nameNbMap.begin();
645 for ( ; nameNbIt != nameNbMap.end(); nameNbIt++, fcount++ )
646 *_sauvFile << " " << setw(8) << nameNbIt->first;
650 // write IDs of named objects in the pile
651 // * 8000 FORMAT(10I8)
652 nameNbIt = nameNbMap.begin();
653 for ( fcount.init(10); nameNbIt != nameNbMap.end(); nameNbIt++, fcount++ )
654 *_sauvFile << setw(8) << nameNbIt->second;
658 //================================================================================
660 * \brief Writes "PILE NUMERO 1"
662 //================================================================================
664 void SauvWriter::writeSubMeshes()
667 map<string,int> nameNbMap;
668 fillSubMeshes( nbSauvObjects, nameNbMap );
670 // * 800 FORMAT (' ENREGISTREMENT DE TYPE', I4)
671 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl;
672 // * 801 FORMAT(' PILE NUMERO',I4,'NBRE OBJETS NOMMES',I8,'NBRE OBJETS',I8)
673 *_sauvFile << " PILE NUMERO 1NBRE OBJETS NOMMES" << setw(8) << nameNbMap.size() <<
674 "NBRE OBJETS" << setw(8) << nbSauvObjects <<endl;
676 writeNames( nameNbMap );
678 TFieldCounter fcount( *_sauvFile, 10 ); // 10 intergers per line
680 for ( size_t iSub = 0; iSub < _subs.size(); ++iSub )
682 SubMesh& sm = _subs[iSub];
683 if ( sm._nbSauvObjects < 1 ) continue;
685 // The first record of each sub-mesh writes
686 // - type of cells; zero means a compound object whose the 2nd record enumerates its components
687 // - number of components of a compound object
688 // - number of references; each reference means a "pointer" to this sub-mesh
689 // - number of nodes per cell
692 if ( !sm._subs.empty() )
694 writeCompoundSubMesh(iSub);
698 // write each sub-type as a SAUV sub-mesh
699 MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh >
700 mesh = _fileMesh->getGenMeshAtLevel( sm._dimRelExt );
701 MEDCouplingAutoRefCountObjectPtr< MEDCouplingUMesh>
702 umesh = mesh->buildUnstructured();
704 for ( int iType=0; iType < sm.cellIDsByTypeSize(); ++iType )
706 const vector<int>& cellIDs = sm._cellIDsByType[iType];
707 if ( cellIDs.empty() ) continue;
709 INTERP_KERNEL::NormalizedCellType
710 cellType = INTERP_KERNEL::NormalizedCellType( iType );
711 const INTERP_KERNEL::CellModel &
712 cell = INTERP_KERNEL::CellModel::GetCellModel( cellType );
713 int castemType = SauvUtilities::med2gibiGeom( cellType );
714 unsigned nbElemNodes = cell.getNumberOfNodes();
715 unsigned nbElems = cellIDs.size();
717 *_sauvFile << setw(8) << castemType
720 << setw(8) << nbElemNodes
721 << setw(8) << nbElems << endl;
723 // write color of each element
724 // * 8000 FORMAT(10I8)
725 for ( size_t i = 0; i < nbElems; ++i, fcount++ ) *_sauvFile << zeroI8;
728 // write connectivity
729 // gibi IDs are in FORTRAN mode while MEDCoupling IDs are in C mode
730 if ( sm._dimRelExt == 1 ) // nodes
732 for ( size_t i = 0; i < nbElems; ++i, fcount++ )
733 *_sauvFile << setw(8) << ( cellIDs[i] + 1 );
737 // indices to transform MED connectivity to GIBI one
738 const int * toMedConn = getGibi2MedQuadraticInterlace( cellType );
740 vector< int > cellConn( nbElemNodes ), transformedConn( nbElemNodes );
741 for ( size_t i = 0; i < nbElems; ++i )
744 umesh->getNodeIdsOfCell( cellIDs[i], cellConn );
747 for ( unsigned j = 0; j < nbElemNodes; ++j )
748 transformedConn[ toMedConn[ j ]] = cellConn[ j ];
749 cellConn.swap( transformedConn );
751 for ( unsigned j = 0; j < nbElemNodes; ++j, fcount++ )
752 *_sauvFile << setw(8) << ( cellConn[j] + 1 );
757 } // loop on cell types
758 } // not a compound object
759 } // loop on sub-meshes
762 //================================================================================
764 * \brief Writes a sum-mesh composed of other sum-meshes
765 * This submesh corresponds to a med mesh or group composed of families
767 //================================================================================
769 void SauvWriter::writeCompoundSubMesh(int iSub)
771 SubMesh& sm = _subs[iSub];
772 if ( sm._nbSauvObjects < 1 || sm._subs.empty()) return;
774 vector< int > subIDs;
775 for ( size_t i = 0; i < sm._subs.size(); ++i ) // loop on sub-meshes of families
776 for ( int j = 0; j < sm._subs[i]->_nbSauvObjects; ++j )
777 subIDs.push_back( sm._subs[i]->_id + j );
780 << setw(8) << subIDs.size()
785 TFieldCounter fcount( *_sauvFile, 10 ); // 10 intergers per line
786 for ( size_t i = 0; i < subIDs.size(); ++i, fcount++ )
787 *_sauvFile << setw(8) << subIDs[i];
790 //================================================================================
792 * \brief Write piles relating to nodes
794 //================================================================================
796 void SauvWriter::writeNodes()
798 MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh > mesh = _fileMesh->getGenMeshAtLevel( 1 );
799 MEDCouplingAutoRefCountObjectPtr< MEDCouplingUMesh > umesh = mesh->buildUnstructured();
801 // write the index connecting nodes with their coodrinates
803 const int nbNodes = umesh->getNumberOfNodes();
804 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl
805 << " PILE NUMERO 32NBRE OBJETS NOMMES 0NBRE OBJETS" << setw(8) << nbNodes << endl;
806 *_sauvFile << setw(8) << nbNodes << endl;
808 TFieldCounter fcount( *_sauvFile, 10 );// * 8000 FORMAT(10I8)
809 for ( int i = 0; i < nbNodes; ++i, fcount++ )
810 *_sauvFile << setw(8) << i + 1;
813 // write coordinates and density of nodes
815 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl;
816 *_sauvFile << " PILE NUMERO 33NBRE OBJETS NOMMES 0NBRE OBJETS 1" << endl;
818 const int dim = umesh->getSpaceDimension();
819 const int nbValues = nbNodes * ( dim + 1 );
820 *_sauvFile << setw(8) << nbValues << endl;
822 // * 8003 FORMAT(1P,3E22.14)
823 const char* density = " 0.00000000000000E+00";
825 _sauvFile->precision(14);
826 _sauvFile->setf( ios_base::scientific, ios_base::floatfield );
827 _sauvFile->setf( ios_base::uppercase );
828 MEDCouplingAutoRefCountObjectPtr< DataArrayDouble> coordArray = umesh->getCoordinatesAndOwner();
829 const double precision = 1.e-99; // PAL12077
830 for ( int i = 0; i < nbNodes; ++i)
832 for ( int j = 0; j < dim; ++j, fcount++ )
834 double coo = coordArray->getIJ( i, j );
835 bool zero = ( -precision < coo && coo < precision );
836 *_sauvFile << setw(22) << ( zero ? 0.0 : coo );
838 *_sauvFile << density;
843 //================================================================================
845 * \brief Store correspondence between GIBI (short) and MED (long) names
847 * IMP 0020434: mapping GIBI names to MED names
848 * Store correspondence between GIBI and MED names as one PILE_STRINGS and one
849 * PILE_TABLES (in three tables: MED_MAIL, MED_CHAM and MED_COMP)
851 //================================================================================
853 void SauvWriter::writeLongNames()
856 3 - _longNames[ LN_MAIL ].empty() - _longNames[ LN_CHAM ].empty() - _longNames[ LN_COMP ].empty();
857 if (nbTables == 0) return;
859 // ---------------------
860 // Write the TABLE pile
861 // ---------------------
863 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl
864 << " PILE NUMERO 10NBRE OBJETS NOMMES" << setw(8) << nbTables
865 << "NBRE OBJETS" << setw(8) << nbTables << endl;
867 if (!_longNames[ LN_MAIL ].empty()) *_sauvFile << " MED_MAIL";
868 if (!_longNames[ LN_CHAM ].empty()) *_sauvFile << " MED_CHAM";
869 if (!_longNames[ LN_COMP ].empty()) *_sauvFile << " MED_COMP";
872 for ( int i = 0; i < nbTables; ++i ) *_sauvFile << setw(8) << i+1;
875 string theWholeString; // concatenated long names
876 vector<int> theOffsets;
878 TFieldCounter fcount (*_sauvFile, 10);
880 for ( int iTbl = 0; iTbl < LN_NB; ++iTbl )
882 vector<nameGIBItoMED>& longNames = _longNames[ iTbl ];
883 if ( longNames.empty() ) continue;
884 const bool isComp = ( iTbl == LN_COMP);
886 // to assure unique MED names
887 set<string> medUniqueNames;
889 *_sauvFile << setw(8) << longNames.size()*4 << endl; // Nb of table values
891 vector<nameGIBItoMED>::iterator itGIBItoMED = longNames.begin();
892 for (; itGIBItoMED != longNames.end(); itGIBItoMED++, iStr++)
894 // PILE of i-th key (med name)
895 *_sauvFile << setw(8) << PILE_STRINGS;
897 // ID of i-th key (med name)
898 *_sauvFile << setw(8) << iStr;
900 // PILE of i-th value (gibi name)
901 *_sauvFile << setw(8) << itGIBItoMED->gibi_pile;
903 // ID of i-th value (gibi name)
904 *_sauvFile << setw(8) << ( isComp ? ++iStr : itGIBItoMED->gibi_id );
907 // add a MED name to the string (while making it be unique for sub-meshes and fields)
908 string aMedName = itGIBItoMED->med_name;
910 for (int ind = 1; !medUniqueNames.insert(aMedName).second; ++ind )
911 aMedName = itGIBItoMED->med_name + "_" + SauvUtilities::toString( ind );
912 theWholeString += aMedName;
915 theOffsets.push_back( theWholeString.size() );
918 theWholeString += itGIBItoMED->gibi_name;
919 theOffsets.push_back( theWholeString.size() );
925 // ----------------------
926 // Write the STRING pile
927 // ----------------------
929 const int nbNames = theOffsets.size();
930 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl
931 << " PILE NUMERO 27NBRE OBJETS NOMMES" << zeroI8 << "NBRE OBJETS" << setw(8) << nbNames << endl
932 << setw(8) << theWholeString.length() << setw(8) << nbNames << endl;
934 // write the whole string
935 const int fixedLength = 71;
936 for ( string::size_type aPos = 0; aPos < theWholeString.length(); aPos += fixedLength)
937 *_sauvFile << setw(72) << theWholeString.substr(aPos, fixedLength) << endl;
940 for ( size_t i = 0; i < theOffsets.size(); ++i, fcount++ )
941 *_sauvFile << setw(8) << theOffsets[i];
944 //================================================================================
946 * \brief Write beginning of field record
948 //================================================================================
950 void SauvWriter::writeFieldNames( const bool isNodal,
951 std::map<std::string,int>& fldNamePrefixMap)
953 vector< MEDCouplingAutoRefCountObjectPtr< MEDFileFieldMultiTS > >&
954 flds = isNodal ? _nodeFields : _cellFields;
955 map<string,int> nameNbMap;
957 for ( size_t iF = 0; iF < flds.size(); ++iF )
959 string name = addName( nameNbMap, fldNamePrefixMap, flds[iF]->getName(), iF+1 );
960 nameGIBItoMED aMEDName;
961 aMEDName.gibi_pile = isNodal ? PILE_NODES_FIELD : PILE_FIELD;
962 aMEDName.gibi_id = iF+1;
963 aMEDName.med_name = name;
964 _longNames[ LN_CHAM ].push_back(aMEDName);
967 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl
968 << ( isNodal ? " PILE NUMERO 2" : " PILE NUMERO 39")
969 << "NBRE OBJETS NOMMES" << setw(8) << nameNbMap.size()
970 << "NBRE OBJETS" << setw(8) << flds.size() << endl;
971 writeNames( nameNbMap );
974 //================================================================================
976 * \brief Make short names of field components
978 * IMP 0020434: mapping GIBI names to MED names
980 //================================================================================
982 void SauvWriter::makeCompNames(const string& fieldName,
983 const vector<string>& compInfo,
984 map<string, string>& mapMedToGibi)
986 for ( size_t i = 0; i < compInfo.size(); ++i )
987 mapMedToGibi[compInfo[i]] = cleanName( compInfo[i] );
990 map<string, string>::iterator namesIt = mapMedToGibi.begin();
991 for (; namesIt != mapMedToGibi.end(); namesIt++)
993 string & compGibiName = (*namesIt).second;
994 if (compGibiName.size() > 4) {
995 // use new name in form "CXXX", where "XXX" is a number
998 compGibiName = SauvUtilities::toString( compIndex++ );
999 if ( compGibiName.size() < 3 )
1000 compGibiName.insert( 0, 3 - compGibiName.size(), '0' );
1001 compGibiName = "C" + compGibiName;
1003 while (mapMedToGibi.count(compGibiName) > 0); // real component name could be CXXX
1006 string compMedName = fieldName + "." + namesIt->first;
1007 nameGIBItoMED aMEDName;
1008 aMEDName.med_name = compMedName;
1009 aMEDName.gibi_pile = PILE_STRINGS;
1010 aMEDName.gibi_name = compGibiName;
1011 _longNames[ LN_COMP ].push_back(aMEDName);
1015 //================================================================================
1017 * \brief Writes "PILE NUMERO 2": fields on nodes
1019 //================================================================================
1021 void SauvWriter::writeNodalFields(map<string,int>& fldNamePrefixMap)
1023 writeFieldNames( /*isNodal=*/true, fldNamePrefixMap );
1025 TFieldCounter fcount (*_sauvFile, 10);
1027 // EXAMPLE ( with no values )
1030 // (2) -88 0 3 -89 0 1 -90 0 2 -91
1032 // (3) FX FY FZ FZ FX FY FLX
1033 // (4) 0 0 0 0 0 0 0
1034 // (5) cree par muc pri
1037 for ( size_t iF = 0; iF < _nodeFields.size(); ++iF )
1039 // (1) write nb subcomponents, nb components(total)
1040 vector< pair<int,int> > iters = _nodeFields[iF]->getIterations();
1041 const vector<string>& compInfo = _nodeFields[iF]->getInfo();
1042 const int nbSub = iters.size();
1043 const int nbComp = compInfo.size();
1044 const int totalNbComp = nbSub * nbComp;
1045 *_sauvFile << setw(8) << nbSub
1046 << setw(8) << totalNbComp
1047 << setw(8) << -1 // IFOUR
1048 << setw(8) << 0 << endl; // nb attributes
1050 // (2) for each sub-component (iteration)
1051 // write support, number of values and number of components
1053 vector< int > vals(3);
1054 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1056 pair<int,int> it = iters[iIt];
1058 vector<INTERP_KERNEL::NormalizedCellType> types;
1059 vector< vector<TypeOfField> > typesF;
1060 vector< vector<string> > pfls, locs;
1061 vector< vector< std::pair<int,int> > > valsVec;
1062 valsVec=_nodeFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName(),
1063 types, typesF, pfls, locs);
1064 // believe that there can be only one type in a nodal field,
1065 // so do not use a loop on types
1066 if ( pfls[0][0].empty() ) pfls[0][0] = noProfileName( types[0] );
1067 map< string, SubMesh* >::iterator pfl2Sub = _profile2Sub.find( pfls[0][0] );
1068 if ( pfl2Sub == _profile2Sub.end() )
1069 THROW_IK_EXCEPTION( "SauvWriter::writeNodalFields(): no sub-mesh for profile |"
1070 << pfls[0][0] << "|");
1071 vals[0] = -pfl2Sub->second->_id;
1072 vals[1] = (valsVec[0][0].second-valsVec[0][0].first);
1073 vals[2] = compInfo.size();
1074 for ( size_t i = 0; i < vals.size(); ++i, fcount++ )
1075 *_sauvFile << setw(8) << vals[i];
1079 // (3) Write names of components
1080 map<string, string> mapMedToGibi;
1081 makeCompNames( _nodeFields[iF]->getName(), compInfo, mapMedToGibi );
1084 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1085 for ( size_t i = 0; i < compInfo.size(); ++i, fcount++ )
1086 *_sauvFile << " " << setw(4) << mapMedToGibi[compInfo[i]];
1087 *_sauvFile << right;
1092 for ( size_t i = 0; i < (std::size_t)totalNbComp; ++i, fcount++ )
1093 *_sauvFile << " " << setw(8) << 0;
1096 string description = _nodeFields[iF]->getName();
1097 *_sauvFile << endl; // (5) TYPE
1098 *_sauvFile << setw(72) << description.substr(0,71) << endl; // (6) TITRE
1099 //*_sauvFile << endl; // (7) 0 attributes
1101 // write values of each component
1102 fcount.init( 3 ); // 3 values per a line
1103 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1105 pair<int,int> it = iters[iIt];
1107 vector<INTERP_KERNEL::NormalizedCellType> types;
1108 vector< vector<TypeOfField> > typesF;
1109 vector< vector<string> > pfls, locs;
1110 vector< vector< std::pair<int,int> > > valsVec;
1111 valsVec = _nodeFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName(),
1112 types, typesF, pfls, locs);
1113 // believe that there can be only one type in a nodal field,
1114 // so do not perform a loop on types
1115 const DataArrayDouble* valsArray = _nodeFields[iF]->getUndergroundDataArray(it.first, it.second);
1116 for ( size_t j = 0; j < compInfo.size(); ++j )
1118 for ( size_t i = valsVec[0][0].first; i < (std::size_t)valsVec[0][0].second; ++i, fcount++ )
1119 *_sauvFile << setw(22) << valsArray->getIJ( i, j );
1126 //================================================================================
1128 * \brief Writes "PILE NUMERO 39": fields on cells
1130 //================================================================================
1132 void SauvWriter::writeElemFields(map<string,int>& fldNamePrefixMap)
1134 writeFieldNames( /*isNodal=*/false, fldNamePrefixMap );
1136 TFieldCounter fcount (*_sauvFile, 10);
1141 // (2) CARACTERISTIQUES
1142 // (3) -15 317773 4 0 0 0 -2 0 3
1145 // (6) 317767 317761 317755 317815
1146 // (7) YOUN NU H SIGY
1147 // (8) REAL*8 REAL*8 REAL*8 REAL*8
1149 // (10) 2.00000000000000E+05
1151 // (10) 3.30000000000000E-01
1153 // (10) 1.00000000000000E+04
1155 // (10) 1.00000000000000E+02 1.00000000000000E+02 1.00000000000000E+02
1156 // (10) 1.00000000000000E+02 1.00000000000000E+02 1.00000000000000E+02
1159 for ( size_t iF = 0; iF < _cellFields.size(); ++iF )
1161 // count nb of sub-components
1162 int iSub, nbSub = 0;
1163 vector< pair<int,int> > iters = _cellFields[iF]->getIterations();
1164 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1166 pair<int,int> it = iters[iIt];
1168 vector<INTERP_KERNEL::NormalizedCellType> types;
1169 vector< vector<TypeOfField> > typesF;
1170 vector< vector<string> > pfls, locs;
1171 vector< vector< std::pair<int,int> > > valsVec;
1172 valsVec = _cellFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName(),
1173 types, typesF, pfls, locs);
1174 for ( size_t i = 0; i < valsVec.size(); ++i )
1175 nbSub += valsVec[i].size();
1177 // (1) write nb sub-components, title length
1178 *_sauvFile << setw(8) << nbSub
1179 << setw(8) << -1 // whatever
1180 << setw(8) << 6 // whatever
1181 << setw(8) << 72 << endl; // title length
1183 string title = _cellFields[iF]->getName();
1184 *_sauvFile << setw(72) << title.substr(0,71) << endl;
1185 *_sauvFile << setw(72) << " " << endl;
1187 // (3) support, nb components
1188 vector<int> vals(9, 0);
1189 const vector<string>& compInfo = _cellFields[iF]->getInfo();
1190 vals[2] = compInfo.size();
1192 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1194 pair<int,int> it = iters[iIt];
1196 vector<INTERP_KERNEL::NormalizedCellType> types;
1197 vector< vector<TypeOfField> > typesF;
1198 vector< vector<string> > pfls, locs;
1199 _cellFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName(),
1200 types, typesF, pfls, locs);
1201 for ( size_t iType = 0; iType < pfls.size(); ++iType )
1202 for ( size_t iP = 0; iP < pfls[iType].size(); ++iP )
1204 if ( pfls[iType][iP].empty() ) pfls[iType][iP] = noProfileName( types[iType] );
1205 map< string, SubMesh* >::iterator pfl2Sub = _profile2Sub.find( pfls[iType][iP] );
1206 if ( pfl2Sub == _profile2Sub.end() )
1207 THROW_IK_EXCEPTION( "SauvWriter::writeElemFields(): no sub-mesh for profile |"
1208 << pfls[iType][iP] << "|");
1209 const int supportID = pfl2Sub->second->_id;
1210 vals[0] = -supportID;
1212 for ( size_t i = 0; i < vals.size(); ++i, fcount++ )
1213 *_sauvFile << setw(8) << vals[ i ];
1218 // (4) dummy strings
1219 for ( fcount.init(4), iSub = 0; iSub < nbSub; ++iSub, fcount++ )
1223 // (5) dummy strings
1224 for ( fcount.init(8), iSub = 0; iSub < nbSub; ++iSub, fcount++ )
1228 // loop on sub-components of a field, each of which refers to
1229 // a certain support and has its own number of components
1230 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1232 pair<int,int> it = iters[iIt];
1233 writeElemTimeStamp( iF, it.first, it.second );
1235 } // loop on cell fields
1238 //================================================================================
1240 * \brief Write one elemental time stamp
1242 //================================================================================
1244 void SauvWriter::writeElemTimeStamp(int iF, int iter, int order)
1246 // (6) 317767 317761 317755 317815
1247 // (7) YOUN NU H SIGY
1248 // (8) REAL*8 REAL*8 REAL*8 REAL*8
1250 // (10) 2.00000000000000E+05
1252 // (10) 3.30000000000000E-01
1254 // (10) 1.00000000000000E+04
1256 // (10) 1.00000000000000E+02 1.00000000000000E+02 1.00000000000000E+02
1257 // (10) 1.00000000000000E+02 1.00000000000000E+02 1.00000000000000E+02
1259 TFieldCounter fcount (*_sauvFile, 10);
1261 vector<INTERP_KERNEL::NormalizedCellType> types;
1262 vector< vector<TypeOfField> > typesF;
1263 vector< vector<string> > pfls, locs;
1264 vector< vector< std::pair<int,int> > > valsVec;
1265 valsVec = _cellFields[iF]->getFieldSplitedByType( iter, order, _fileMesh->getName(),
1266 types, typesF, pfls, locs);
1267 for ( size_t iType = 0; iType < pfls.size(); ++iType )
1268 for ( size_t iP = 0; iP < pfls[iType].size(); ++iP )
1270 const vector<string>& compInfo = _cellFields[iF]->getInfo();
1272 // (6) component addresses
1273 int iComp = 0, nbComp = compInfo.size();
1274 for ( fcount.init(10); iComp < nbComp; ++iComp, fcount++ )
1275 *_sauvFile << setw(8) << 777; // a good number
1278 // (7) component names
1279 map<string, string> mapMedToGibi;
1280 makeCompNames( _cellFields[iF]->getName(), compInfo, mapMedToGibi );
1282 for ( fcount.init(8), iComp = 0; iComp < nbComp; ++iComp, fcount++ )
1283 *_sauvFile << " " << setw(8) << mapMedToGibi[compInfo[iComp]];
1286 // (8) component types
1287 for ( fcount.init(4), iComp = 0; iComp < nbComp; ++iComp, fcount++ )
1288 *_sauvFile << " " << setw(17) << "REAL*8";
1290 *_sauvFile << right;
1292 // (9) nb values per element, nb of elements
1293 int nbPntPerCell = 1;
1294 if ( !locs[iType][iP].empty() )
1296 int locID = _cellFields[iF]->getLocalizationId( locs[iType][iP].c_str() );
1297 nbPntPerCell = _cellFields[iF]->getNbOfGaussPtPerCell( locID );
1299 else if ( typesF[iType][iP] == ON_GAUSS_NE )
1301 nbPntPerCell = INTERP_KERNEL::CellModel::GetCellModel(types[iType]).getNumberOfNodes();
1305 const std::pair<int,int>& bgEnd = valsVec[iType][iP];
1306 const DataArrayDouble* valArray = _cellFields[iF]->getUndergroundDataArray(iter, order);
1307 for ( iComp = 0; iComp < nbComp; ++iComp )
1309 *_sauvFile << setw(8) << nbPntPerCell
1310 << setw(8) << (bgEnd.second-bgEnd.first) / nbPntPerCell
1315 for ( size_t i = bgEnd.first; i < (size_t) bgEnd.second; ++i, fcount++ )
1316 *_sauvFile << setw(22) << valArray->getIJ( i, iComp );
1322 //================================================================================
1324 * \brief Write the last record of the SAUV file
1326 //================================================================================
1328 void SauvWriter::writeLastRecord()
1330 *_sauvFile << " ENREGISTREMENT DE TYPE 5" << endl;
1331 *_sauvFile << "LABEL AUTOMATIQUE : 1" << endl;