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 std::size_t SauvWriter::getHeapMemorySizeWithoutChildren() const
231 std::vector<const BigMemoryObject *> SauvWriter::getDirectChildren() const
233 return std::vector<const BigMemoryObject *>();
236 //================================================================================
238 * \brief Fills own DS by MEDFileData
240 //================================================================================
242 void SauvWriter::setMEDFileDS(const MEDFileData* medData,
245 if ( !medData) THROW_IK_EXCEPTION("NULL MEDFileData");
247 MEDFileMeshes * meshes = medData->getMeshes();
248 MEDFileFields * fields = medData->getFields();
249 if ( !meshes) THROW_IK_EXCEPTION("No meshes in MEDFileData");
251 _fileMesh = meshes->getMeshAtPos( meshIndex );
252 _fileMesh->incrRef();
255 for ( int i = 0; i < fields->getNumberOfFields(); ++i )
257 MEDFileAnyTypeFieldMultiTS * fB = fields->getFieldAtPos(i);
258 MEDFileFieldMultiTS * f = dynamic_cast<MEDFileFieldMultiTS *>(fB);
260 continue;// fields on int32 not managed
261 if ( f->getMeshName() == _fileMesh->getName() )
263 vector< vector<TypeOfField> > fTypes = f->getTypesOfFieldAvailable();
264 if ( fTypes[0].size() == 1 && fTypes[0][0] == ON_NODES )
265 _nodeFields.push_back( f );
267 _cellFields.push_back( f );
272 //================================================================================
274 * \brief Adds a submesh
276 //================================================================================
278 SauvWriter::SubMesh* SauvWriter::addSubMesh(const std::string& name, int dimRelExt)
280 if ( _subs.capacity() < _subs.size() + 1 )
281 THROW_IK_EXCEPTION("SauvWriter: INTERNAL error, wrong evaluation of nb of sub-meshes");
282 _subs.resize( _subs.size() + 1 );
283 SubMesh& sm = _subs.back();
285 sm._dimRelExt = dimRelExt;
288 //================================================================================
290 * \brief Returns nb of cell types
292 //================================================================================
294 int SauvWriter::SubMesh::nbTypes() const
297 for (int i = 0; i < cellIDsByTypeSize(); ++i )
298 nb += int( !_cellIDsByType[i].empty() );
302 //================================================================================
306 //================================================================================
308 void SauvWriter::fillSubMeshes( int& nbSauvObjects, map<string,int>& nameNbMap )
310 // evaluate nb of _subs in order to avoid re-allocation of _subs
311 int nbSubs = 1; // for the very mesh
312 nbSubs += _fileMesh->getFamilyInfo().size() + 4; // + 4 zero families (for each dimRelExt)
313 nbSubs += _fileMesh->getGroupInfo().size();
314 nbSubs += evaluateNbProfileSubMeshes();
316 _subs.reserve( nbSubs );
318 fillFamilySubMeshes();
319 fillGroupSubMeshes();
320 fillProfileSubMeshes();
322 // fill names of SubMesh'es and count nb of sauv sub-meshes they will be stored into
324 map<string,int> namePrefixMap;
325 for ( size_t i = 0; i < _subs.size(); ++i )
327 SubMesh& sm = _subs[i];
329 sm._nbSauvObjects = 0;
330 if ( sm._subs.empty() )
332 sm._nbSauvObjects = sm.nbTypes();
336 sm._nbSauvObjects = 1;
339 sm._id = nbSauvObjects+1;
340 nbSauvObjects += sm._nbSauvObjects;
342 if ( sm._nbSauvObjects )
343 sm._name = addName( nameNbMap, namePrefixMap, sm._name, sm._id );
345 if ( sm._nbSauvObjects && !sm._name.empty() )
347 nameGIBItoMED aMEDName;
348 aMEDName.gibi_pile = PILE_SOUS_MAILLAGE;
349 aMEDName.gibi_id = sm._id;
350 aMEDName.med_name = sm._name;
351 _longNames[ LN_MAIL ].push_back(aMEDName);
356 //================================================================================
358 * \brief fill sub-meshes of families
360 //================================================================================
362 void SauvWriter::fillFamilySubMeshes()
364 SubMesh* nilSm = (SubMesh*) 0;
365 std::vector<int> dims = _fileMesh->getNonEmptyLevelsExt();
366 for ( size_t iDim = 0; iDim < dims.size(); ++iDim )
368 int dimRelExt = dims[ iDim ];
369 MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh > mesh = _fileMesh->getGenMeshAtLevel(dimRelExt);
370 const DataArrayInt * famIds = _fileMesh->getFamilyFieldAtLevel(dimRelExt);
371 if ( !famIds ) continue;
374 SubMesh* curSubMesh = addSubMesh( "", dimRelExt ); // submesh of zero family
375 _famIDs2Sub[0] = curSubMesh;
376 int sub0Index = _subs.size()-1;
378 const int * famID = famIds->begin(), * famIDEnd = famIds->end();
379 for ( int cellID = 0; famID < famIDEnd; ++famID, cellID++ )
381 if ( *famID != curFamID )
384 map< int, SubMesh* >::iterator f2s = _famIDs2Sub.insert( make_pair( curFamID, nilSm )).first;
386 f2s->second = addSubMesh( "", dimRelExt ); // no names for families
387 curSubMesh = f2s->second;
389 INTERP_KERNEL::NormalizedCellType cellType =
390 dimRelExt == 1 ? INTERP_KERNEL::NORM_POINT1 : mesh->getTypeOfCell( cellID );
391 curSubMesh->_cellIDsByType[ cellType ].push_back( cellID );
394 if ( dimRelExt == 1 )
396 // clear submesh of nodal zero family
397 _famIDs2Sub[0]->_cellIDsByType[ INTERP_KERNEL::NORM_POINT1 ].clear();
399 else if ( dimRelExt == 0 )
401 // make a submesh including all cells
402 if ( sub0Index == (int)(_subs.size()-1) )
404 _famIDs2Sub[0]->_name = _fileMesh->getName(); // there is the zero family only
408 curSubMesh = addSubMesh( _fileMesh->getName(), dimRelExt );
409 if ( _famIDs2Sub[0]->nbTypes() == 0 )
410 sub0Index++; // skip an empty zero family
411 for ( size_t i = sub0Index; i < _subs.size()-1; ++i )
412 curSubMesh->_subs.push_back( & _subs[i] );
418 //================================================================================
420 * \brief fill sub-meshes of groups
422 //================================================================================
424 void SauvWriter::fillGroupSubMeshes()
426 const map<string, vector<string> >& grpFams = _fileMesh->getGroupInfo();
427 map<string, vector<string> >::const_iterator g2ff = grpFams.begin();
428 for ( ; g2ff != grpFams.end(); ++g2ff )
430 const string& groupName = g2ff->first;
431 const vector<string>& famNames = g2ff->second;
432 if ( famNames.empty() ) continue;
433 std::vector<SubMesh*> famSubMeshes( famNames.size() );
435 for ( size_t i = 0; i < famNames.size(); ++i )
437 int famID = _fileMesh->getFamilyId( famNames[i].c_str() );
438 map< int, SubMesh* >::iterator i2f = _famIDs2Sub.find( famID );
439 if ( i2f != _famIDs2Sub.end() )
441 famSubMeshes[ k ] = i2f->second;
445 // if a family exists but has no element, no submesh has been found for this family
446 // => we have to resize famSubMeshes with the number of submeshes stored
447 if (k != famNames.size())
448 famSubMeshes.resize(k);
449 SubMesh* grpSubMesh = addSubMesh( groupName, famSubMeshes[0]->_dimRelExt );
450 grpSubMesh->_subs.swap( famSubMeshes );
455 //================================================================================
457 * \brief fill sub-meshes of profiles
459 //================================================================================
461 void SauvWriter::fillProfileSubMeshes()
463 _profile2Sub.clear();
464 SubMesh* nilSm = (SubMesh*) 0;
465 for ( int isOnNodes = 0; isOnNodes < 2; ++isOnNodes )
467 vector< MEDCouplingAutoRefCountObjectPtr< MEDFileFieldMultiTS > >
468 fields = isOnNodes ? _nodeFields : _cellFields;
469 for ( size_t i = 0; i < fields.size(); ++i )
471 vector< pair<int,int> > iters = fields[i]->getIterations();
473 vector<INTERP_KERNEL::NormalizedCellType> types;
474 vector< vector<TypeOfField> > typesF;
475 vector< vector<string> > pfls, locs;
476 fields[i]->getFieldSplitedByType( iters[0].first, iters[0].second,
477 _fileMesh->getName().c_str(), types, typesF, pfls, locs);
479 for ( size_t iType = 0; iType < types.size(); ++iType )
481 if ( types[iType] == INTERP_KERNEL::NORM_ERROR )
482 dimRelExt = 1; // on nodes
484 dimRelExt = getDimension( types[iType] ) - _fileMesh->getMeshDimension();
485 for ( size_t iPfl = 0; iPfl < pfls[iType].size(); ++iPfl )
487 bool isOnAll = pfls[iType][iPfl].empty();
488 if ( isOnAll ) pfls[iType][iPfl] = noProfileName( types[iType] );
489 map< string, SubMesh* >::iterator pfl2sm =
490 _profile2Sub.insert( make_pair( pfls[iType][iPfl], nilSm )).first;
491 if ( !pfl2sm->second )
493 SubMesh* sm = pfl2sm->second = addSubMesh( "", dimRelExt ); // no names for profiles
494 const DataArrayInt * pfl = isOnAll ? 0 : fields[i]->getProfile( pfls[iType][iPfl].c_str() );
495 makeProfileIDs( sm, types[iType], pfl );
503 //================================================================================
505 * \brief Return max possible nb of sub-meshes to decsribe field supports
507 //================================================================================
509 int SauvWriter::evaluateNbProfileSubMeshes() const
512 for ( size_t i = 0; i < _nodeFields.size(); ++i )
513 nb += 1 + _nodeFields[i]->getPflsReallyUsed().size();
515 for ( size_t i = 0; i < _cellFields.size(); ++i )
517 nb += _cellFields[i]->getPflsReallyUsed().size();
519 vector< pair<int,int> > iters = _cellFields[i]->getIterations();
521 vector<INTERP_KERNEL::NormalizedCellType> types;
522 vector< vector<TypeOfField> > typesF;
523 vector< vector<string> > pfls, locs;
524 _cellFields[i]->getFieldSplitedByType( iters[0].first, iters[0].second,
525 _fileMesh->getName().c_str(), types, typesF, pfls, locs);
526 nb += 2 * types.size(); // x 2 - a type can be on nodes and on cells at the same time
532 //================================================================================
534 * \brief Transorm a profile into ids of mesh elements
536 //================================================================================
538 void SauvWriter::makeProfileIDs( SubMesh* sm,
539 INTERP_KERNEL::NormalizedCellType type,
540 const DataArrayInt* profile )
542 MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh >
543 mesh = _fileMesh->getGenMeshAtLevel(sm->_dimRelExt);
544 const MEDCouplingUMesh* uMesh = dynamic_cast< const MEDCouplingUMesh* > ((const MEDCouplingMesh*) mesh );
546 if ( sm->_dimRelExt == 1 ) type = INTERP_KERNEL::NORM_POINT1;
547 vector< int >& ids = sm->_cellIDsByType[ type ];
549 if ( sm->_dimRelExt == 1 || !uMesh )
551 // profile on nodes or mesh is CARTESIAN
554 ids.assign( profile->begin(), profile->end() );
558 ids.resize( sm->_dimRelExt == 1 ? mesh->getNumberOfNodes() : mesh->getNumberOfCells() );
559 for ( size_t i = 0; i < ids.size(); ++i )
568 if ( profile ) // on profile
570 code[1] = profile->getNumberOfTuples();
575 code[1] = mesh->getNumberOfCellsWithType( type );
578 vector<const DataArrayInt *> idsPerType( 1, profile );
579 MEDCouplingAutoRefCountObjectPtr<DataArrayInt>
580 resIDs = uMesh->checkTypeConsistencyAndContig( code, idsPerType );
581 ids.assign( resIDs->begin(), resIDs->end() );
585 //================================================================================
587 * \brief Write its data into the SAUVE file
589 //================================================================================
591 void SauvWriter::write(const char* fileName)
593 std::fstream fileStream;
594 fileStream.open( fileName, ios::out);
597 ( !fileStream || !fileStream.is_open() )
599 ( !fileStream || !fileStream.rdbuf()->is_open() )
601 THROW_IK_EXCEPTION("Can't open the file |"<<fileName<<"|");
602 _sauvFile = &fileStream;
606 _profile2Sub.clear();
607 _longNames[ LN_MAIL ].clear();
608 _longNames[ LN_CHAM ].clear();
609 _longNames[ LN_COMP ].clear();
611 map<string,int> fldNamePrefixMap;
616 writeNodalFields(fldNamePrefixMap);
617 writeElemFields(fldNamePrefixMap);
623 //================================================================================
625 * \brief Writes "ENREGISTREMENT DE TYPE" 4 and 7
627 //================================================================================
629 void SauvWriter::writeFileHead()
631 MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh > mesh = _fileMesh->getGenMeshAtLevel(0);
634 << " ENREGISTREMENT DE TYPE 4" << endl
635 << " NIVEAU 16 NIVEAU ERREUR 0 DIMENSION " << mesh->getSpaceDimension() <<endl
636 << " DENSITE 0.00000E+00" << endl
637 << " ENREGISTREMENT DE TYPE 7" << endl
638 << " NOMBRE INFO CASTEM2000 8" <<endl
639 << " IFOUR -1 NIFOUR 0 IFOMOD -1 IECHO 1 IIMPI 0 IOSPI 0 ISOTYP 1" << endl
640 << " NSDPGE 0" << endl;
643 //================================================================================
645 * \brief Writes names of objects
647 //================================================================================
649 void SauvWriter::writeNames( const map<string,int>& nameNbMap )
651 if ( !nameNbMap.empty() )
653 // write names of objects
654 // * 8001 FORMAT(8(1X,A8))
655 TFieldCounter fcount( *_sauvFile, 8 );
657 map<string,int>::const_iterator nameNbIt = nameNbMap.begin();
658 for ( ; nameNbIt != nameNbMap.end(); nameNbIt++, fcount++ )
659 *_sauvFile << " " << setw(8) << nameNbIt->first;
663 // write IDs of named objects in the pile
664 // * 8000 FORMAT(10I8)
665 nameNbIt = nameNbMap.begin();
666 for ( fcount.init(10); nameNbIt != nameNbMap.end(); nameNbIt++, fcount++ )
667 *_sauvFile << setw(8) << nameNbIt->second;
671 //================================================================================
673 * \brief Writes "PILE NUMERO 1"
675 //================================================================================
677 void SauvWriter::writeSubMeshes()
680 map<string,int> nameNbMap;
681 fillSubMeshes( nbSauvObjects, nameNbMap );
683 // * 800 FORMAT (' ENREGISTREMENT DE TYPE', I4)
684 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl;
685 // * 801 FORMAT(' PILE NUMERO',I4,'NBRE OBJETS NOMMES',I8,'NBRE OBJETS',I8)
686 *_sauvFile << " PILE NUMERO 1NBRE OBJETS NOMMES" << setw(8) << nameNbMap.size() <<
687 "NBRE OBJETS" << setw(8) << nbSauvObjects <<endl;
689 writeNames( nameNbMap );
691 TFieldCounter fcount( *_sauvFile, 10 ); // 10 intergers per line
693 for ( size_t iSub = 0; iSub < _subs.size(); ++iSub )
695 SubMesh& sm = _subs[iSub];
696 if ( sm._nbSauvObjects < 1 ) continue;
698 // The first record of each sub-mesh writes
699 // - type of cells; zero means a compound object whose the 2nd record enumerates its components
700 // - number of components of a compound object
701 // - number of references; each reference means a "pointer" to this sub-mesh
702 // - number of nodes per cell
705 if ( !sm._subs.empty() )
707 writeCompoundSubMesh(iSub);
711 // write each sub-type as a SAUV sub-mesh
712 MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh >
713 mesh = _fileMesh->getGenMeshAtLevel( sm._dimRelExt );
714 MEDCouplingAutoRefCountObjectPtr< MEDCouplingUMesh>
715 umesh = mesh->buildUnstructured();
717 for ( int iType=0; iType < sm.cellIDsByTypeSize(); ++iType )
719 const vector<int>& cellIDs = sm._cellIDsByType[iType];
720 if ( cellIDs.empty() ) continue;
722 INTERP_KERNEL::NormalizedCellType
723 cellType = INTERP_KERNEL::NormalizedCellType( iType );
724 const INTERP_KERNEL::CellModel &
725 cell = INTERP_KERNEL::CellModel::GetCellModel( cellType );
726 int castemType = SauvUtilities::med2gibiGeom( cellType );
727 unsigned nbElemNodes = cell.getNumberOfNodes();
728 unsigned nbElems = cellIDs.size();
730 *_sauvFile << setw(8) << castemType
733 << setw(8) << nbElemNodes
734 << setw(8) << nbElems << endl;
736 // write color of each element
737 // * 8000 FORMAT(10I8)
738 for ( size_t i = 0; i < nbElems; ++i, fcount++ ) *_sauvFile << zeroI8;
741 // write connectivity
742 // gibi IDs are in FORTRAN mode while MEDCoupling IDs are in C mode
743 if ( sm._dimRelExt == 1 ) // nodes
745 for ( size_t i = 0; i < nbElems; ++i, fcount++ )
746 *_sauvFile << setw(8) << ( cellIDs[i] + 1 );
750 // indices to transform MED connectivity to GIBI one
751 const int * toMedConn = getGibi2MedQuadraticInterlace( cellType );
753 vector< int > cellConn( nbElemNodes ), transformedConn( nbElemNodes );
754 for ( size_t i = 0; i < nbElems; ++i )
757 umesh->getNodeIdsOfCell( cellIDs[i], cellConn );
760 for ( unsigned j = 0; j < nbElemNodes; ++j )
761 transformedConn[ toMedConn[ j ]] = cellConn[ j ];
762 cellConn.swap( transformedConn );
764 for ( unsigned j = 0; j < nbElemNodes; ++j, fcount++ )
765 *_sauvFile << setw(8) << ( cellConn[j] + 1 );
770 } // loop on cell types
771 } // not a compound object
772 } // loop on sub-meshes
775 //================================================================================
777 * \brief Writes a sum-mesh composed of other sum-meshes
778 * This submesh corresponds to a med mesh or group composed of families
780 //================================================================================
782 void SauvWriter::writeCompoundSubMesh(int iSub)
784 SubMesh& sm = _subs[iSub];
785 if ( sm._nbSauvObjects < 1 || sm._subs.empty()) return;
787 vector< int > subIDs;
788 for ( size_t i = 0; i < sm._subs.size(); ++i ) // loop on sub-meshes of families
789 for ( int j = 0; j < sm._subs[i]->_nbSauvObjects; ++j )
790 subIDs.push_back( sm._subs[i]->_id + j );
793 << setw(8) << subIDs.size()
798 TFieldCounter fcount( *_sauvFile, 10 ); // 10 intergers per line
799 for ( size_t i = 0; i < subIDs.size(); ++i, fcount++ )
800 *_sauvFile << setw(8) << subIDs[i];
803 //================================================================================
805 * \brief Write piles relating to nodes
807 //================================================================================
809 void SauvWriter::writeNodes()
811 MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh > mesh = _fileMesh->getGenMeshAtLevel( 1 );
812 MEDCouplingAutoRefCountObjectPtr< MEDCouplingUMesh > umesh = mesh->buildUnstructured();
814 // write the index connecting nodes with their coodrinates
816 const int nbNodes = umesh->getNumberOfNodes();
817 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl
818 << " PILE NUMERO 32NBRE OBJETS NOMMES 0NBRE OBJETS" << setw(8) << nbNodes << endl;
819 *_sauvFile << setw(8) << nbNodes << endl;
821 TFieldCounter fcount( *_sauvFile, 10 );// * 8000 FORMAT(10I8)
822 for ( int i = 0; i < nbNodes; ++i, fcount++ )
823 *_sauvFile << setw(8) << i + 1;
826 // write coordinates and density of nodes
828 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl;
829 *_sauvFile << " PILE NUMERO 33NBRE OBJETS NOMMES 0NBRE OBJETS 1" << endl;
831 const int dim = umesh->getSpaceDimension();
832 const int nbValues = nbNodes * ( dim + 1 );
833 *_sauvFile << setw(8) << nbValues << endl;
835 // * 8003 FORMAT(1P,3E22.14)
836 const char* density = " 0.00000000000000E+00";
838 _sauvFile->precision(14);
839 _sauvFile->setf( ios_base::scientific, ios_base::floatfield );
840 _sauvFile->setf( ios_base::uppercase );
841 MEDCouplingAutoRefCountObjectPtr< DataArrayDouble> coordArray = umesh->getCoordinatesAndOwner();
842 const double precision = 1.e-99; // PAL12077
843 for ( int i = 0; i < nbNodes; ++i)
845 for ( int j = 0; j < dim; ++j, fcount++ )
847 double coo = coordArray->getIJ( i, j );
848 bool zero = ( -precision < coo && coo < precision );
849 *_sauvFile << setw(22) << ( zero ? 0.0 : coo );
851 *_sauvFile << density;
856 //================================================================================
858 * \brief Store correspondence between GIBI (short) and MED (long) names
860 * IMP 0020434: mapping GIBI names to MED names
861 * Store correspondence between GIBI and MED names as one PILE_STRINGS and one
862 * PILE_TABLES (in three tables: MED_MAIL, MED_CHAM and MED_COMP)
864 //================================================================================
866 void SauvWriter::writeLongNames()
869 3 - _longNames[ LN_MAIL ].empty() - _longNames[ LN_CHAM ].empty() - _longNames[ LN_COMP ].empty();
870 if (nbTables == 0) return;
872 // ---------------------
873 // Write the TABLE pile
874 // ---------------------
876 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl
877 << " PILE NUMERO 10NBRE OBJETS NOMMES" << setw(8) << nbTables
878 << "NBRE OBJETS" << setw(8) << nbTables << endl;
880 if (!_longNames[ LN_MAIL ].empty()) *_sauvFile << " MED_MAIL";
881 if (!_longNames[ LN_CHAM ].empty()) *_sauvFile << " MED_CHAM";
882 if (!_longNames[ LN_COMP ].empty()) *_sauvFile << " MED_COMP";
885 for ( int i = 0; i < nbTables; ++i ) *_sauvFile << setw(8) << i+1;
888 string theWholeString; // concatenated long names
889 vector<int> theOffsets;
891 TFieldCounter fcount (*_sauvFile, 10);
893 for ( int iTbl = 0; iTbl < LN_NB; ++iTbl )
895 vector<nameGIBItoMED>& longNames = _longNames[ iTbl ];
896 if ( longNames.empty() ) continue;
897 const bool isComp = ( iTbl == LN_COMP);
899 // to assure unique MED names
900 set<string> medUniqueNames;
902 *_sauvFile << setw(8) << longNames.size()*4 << endl; // Nb of table values
904 vector<nameGIBItoMED>::iterator itGIBItoMED = longNames.begin();
905 for (; itGIBItoMED != longNames.end(); itGIBItoMED++, iStr++)
907 // PILE of i-th key (med name)
908 *_sauvFile << setw(8) << PILE_STRINGS;
910 // ID of i-th key (med name)
911 *_sauvFile << setw(8) << iStr;
913 // PILE of i-th value (gibi name)
914 *_sauvFile << setw(8) << itGIBItoMED->gibi_pile;
916 // ID of i-th value (gibi name)
917 *_sauvFile << setw(8) << ( isComp ? ++iStr : itGIBItoMED->gibi_id );
920 // add a MED name to the string (while making it be unique for sub-meshes and fields)
921 string aMedName = itGIBItoMED->med_name;
923 for (int ind = 1; !medUniqueNames.insert(aMedName).second; ++ind )
924 aMedName = itGIBItoMED->med_name + "_" + SauvUtilities::toString( ind );
925 theWholeString += aMedName;
928 theOffsets.push_back( theWholeString.size() );
931 theWholeString += itGIBItoMED->gibi_name;
932 theOffsets.push_back( theWholeString.size() );
938 // ----------------------
939 // Write the STRING pile
940 // ----------------------
942 const int nbNames = theOffsets.size();
943 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl
944 << " PILE NUMERO 27NBRE OBJETS NOMMES" << zeroI8 << "NBRE OBJETS" << setw(8) << nbNames << endl
945 << setw(8) << theWholeString.length() << setw(8) << nbNames << endl;
947 // write the whole string
948 const int fixedLength = 71;
949 for ( string::size_type aPos = 0; aPos < theWholeString.length(); aPos += fixedLength)
950 *_sauvFile << setw(72) << theWholeString.substr(aPos, fixedLength) << endl;
953 for ( size_t i = 0; i < theOffsets.size(); ++i, fcount++ )
954 *_sauvFile << setw(8) << theOffsets[i];
957 //================================================================================
959 * \brief Write beginning of field record
961 //================================================================================
963 void SauvWriter::writeFieldNames( const bool isNodal,
964 std::map<std::string,int>& fldNamePrefixMap)
966 vector< MEDCouplingAutoRefCountObjectPtr< MEDFileFieldMultiTS > >&
967 flds = isNodal ? _nodeFields : _cellFields;
968 map<string,int> nameNbMap;
970 for ( size_t iF = 0; iF < flds.size(); ++iF )
972 string name = addName( nameNbMap, fldNamePrefixMap, flds[iF]->getName(), iF+1 );
973 nameGIBItoMED aMEDName;
974 aMEDName.gibi_pile = isNodal ? PILE_NODES_FIELD : PILE_FIELD;
975 aMEDName.gibi_id = iF+1;
976 aMEDName.med_name = name;
977 _longNames[ LN_CHAM ].push_back(aMEDName);
980 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl
981 << ( isNodal ? " PILE NUMERO 2" : " PILE NUMERO 39")
982 << "NBRE OBJETS NOMMES" << setw(8) << nameNbMap.size()
983 << "NBRE OBJETS" << setw(8) << flds.size() << endl;
984 writeNames( nameNbMap );
987 //================================================================================
989 * \brief Make short names of field components
991 * IMP 0020434: mapping GIBI names to MED names
993 //================================================================================
995 void SauvWriter::makeCompNames(const string& fieldName,
996 const vector<string>& compInfo,
997 map<string, string>& mapMedToGibi)
999 for ( size_t i = 0; i < compInfo.size(); ++i )
1000 mapMedToGibi[compInfo[i]] = cleanName( compInfo[i] );
1003 map<string, string>::iterator namesIt = mapMedToGibi.begin();
1004 for (; namesIt != mapMedToGibi.end(); namesIt++)
1006 string & compGibiName = (*namesIt).second;
1007 if (compGibiName.size() > 4) {
1008 // use new name in form "CXXX", where "XXX" is a number
1011 compGibiName = SauvUtilities::toString( compIndex++ );
1012 if ( compGibiName.size() < 3 )
1013 compGibiName.insert( 0, 3 - compGibiName.size(), '0' );
1014 compGibiName = "C" + compGibiName;
1016 while (mapMedToGibi.count(compGibiName) > 0); // real component name could be CXXX
1019 string compMedName = fieldName + "." + namesIt->first;
1020 nameGIBItoMED aMEDName;
1021 aMEDName.med_name = compMedName;
1022 aMEDName.gibi_pile = PILE_STRINGS;
1023 aMEDName.gibi_name = compGibiName;
1024 _longNames[ LN_COMP ].push_back(aMEDName);
1028 //================================================================================
1030 * \brief Writes "PILE NUMERO 2": fields on nodes
1032 //================================================================================
1034 void SauvWriter::writeNodalFields(map<string,int>& fldNamePrefixMap)
1036 writeFieldNames( /*isNodal=*/true, fldNamePrefixMap );
1038 TFieldCounter fcount (*_sauvFile, 10);
1040 // EXAMPLE ( with no values )
1043 // (2) -88 0 3 -89 0 1 -90 0 2 -91
1045 // (3) FX FY FZ FZ FX FY FLX
1046 // (4) 0 0 0 0 0 0 0
1047 // (5) cree par muc pri
1050 for ( size_t iF = 0; iF < _nodeFields.size(); ++iF )
1052 // (1) write nb subcomponents, nb components(total)
1053 vector< pair<int,int> > iters = _nodeFields[iF]->getIterations();
1054 const vector<string>& compInfo = _nodeFields[iF]->getInfo();
1055 const int nbSub = iters.size();
1056 const int nbComp = compInfo.size();
1057 const int totalNbComp = nbSub * nbComp;
1058 *_sauvFile << setw(8) << nbSub
1059 << setw(8) << totalNbComp
1060 << setw(8) << -1 // IFOUR
1061 << setw(8) << 0 << endl; // nb attributes
1063 // (2) for each sub-component (iteration)
1064 // write support, number of values and number of components
1066 vector< int > vals(3);
1067 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1069 pair<int,int> it = iters[iIt];
1071 vector<INTERP_KERNEL::NormalizedCellType> types;
1072 vector< vector<TypeOfField> > typesF;
1073 vector< vector<string> > pfls, locs;
1074 vector< vector< std::pair<int,int> > > valsVec;
1075 valsVec=_nodeFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName().c_str(),
1076 types, typesF, pfls, locs);
1077 // believe that there can be only one type in a nodal field,
1078 // so do not use a loop on types
1079 if ( pfls[0][0].empty() ) pfls[0][0] = noProfileName( types[0] );
1080 map< string, SubMesh* >::iterator pfl2Sub = _profile2Sub.find( pfls[0][0] );
1081 if ( pfl2Sub == _profile2Sub.end() )
1082 THROW_IK_EXCEPTION( "SauvWriter::writeNodalFields(): no sub-mesh for profile |"
1083 << pfls[0][0] << "|");
1084 vals[0] = -pfl2Sub->second->_id;
1085 vals[1] = (valsVec[0][0].second-valsVec[0][0].first);
1086 vals[2] = compInfo.size();
1087 for ( size_t i = 0; i < vals.size(); ++i, fcount++ )
1088 *_sauvFile << setw(8) << vals[i];
1092 // (3) Write names of components
1093 map<string, string> mapMedToGibi;
1094 makeCompNames( _nodeFields[iF]->getName(), compInfo, mapMedToGibi );
1097 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1098 for ( size_t i = 0; i < compInfo.size(); ++i, fcount++ )
1099 *_sauvFile << " " << setw(4) << mapMedToGibi[compInfo[i]];
1100 *_sauvFile << right;
1105 for ( size_t i = 0; i < (std::size_t)totalNbComp; ++i, fcount++ )
1106 *_sauvFile << " " << setw(8) << 0;
1109 string description = _nodeFields[iF]->getName();
1110 *_sauvFile << endl; // (5) TYPE
1111 *_sauvFile << setw(72) << description.substr(0,71) << endl; // (6) TITRE
1112 //*_sauvFile << endl; // (7) 0 attributes
1114 // write values of each component
1115 fcount.init( 3 ); // 3 values per a line
1116 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1118 pair<int,int> it = iters[iIt];
1120 vector<INTERP_KERNEL::NormalizedCellType> types;
1121 vector< vector<TypeOfField> > typesF;
1122 vector< vector<string> > pfls, locs;
1123 vector< vector< std::pair<int,int> > > valsVec;
1124 valsVec = _nodeFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName().c_str(),
1125 types, typesF, pfls, locs);
1126 // believe that there can be only one type in a nodal field,
1127 // so do not perform a loop on types
1128 const DataArrayDouble* valsArray = _nodeFields[iF]->getUndergroundDataArray(it.first, it.second);
1129 for ( size_t j = 0; j < compInfo.size(); ++j )
1131 for ( size_t i = valsVec[0][0].first; i < (std::size_t)valsVec[0][0].second; ++i, fcount++ )
1132 *_sauvFile << setw(22) << valsArray->getIJ( i, j );
1139 //================================================================================
1141 * \brief Writes "PILE NUMERO 39": fields on cells
1143 //================================================================================
1145 void SauvWriter::writeElemFields(map<string,int>& fldNamePrefixMap)
1147 writeFieldNames( /*isNodal=*/false, fldNamePrefixMap );
1149 TFieldCounter fcount (*_sauvFile, 10);
1154 // (2) CARACTERISTIQUES
1155 // (3) -15 317773 4 0 0 0 -2 0 3
1158 // (6) 317767 317761 317755 317815
1159 // (7) YOUN NU H SIGY
1160 // (8) REAL*8 REAL*8 REAL*8 REAL*8
1162 // (10) 2.00000000000000E+05
1164 // (10) 3.30000000000000E-01
1166 // (10) 1.00000000000000E+04
1168 // (10) 1.00000000000000E+02 1.00000000000000E+02 1.00000000000000E+02
1169 // (10) 1.00000000000000E+02 1.00000000000000E+02 1.00000000000000E+02
1172 for ( size_t iF = 0; iF < _cellFields.size(); ++iF )
1174 // count nb of sub-components
1175 int iSub, nbSub = 0;
1176 vector< pair<int,int> > iters = _cellFields[iF]->getIterations();
1177 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1179 pair<int,int> it = iters[iIt];
1181 vector<INTERP_KERNEL::NormalizedCellType> types;
1182 vector< vector<TypeOfField> > typesF;
1183 vector< vector<string> > pfls, locs;
1184 vector< vector< std::pair<int,int> > > valsVec;
1185 valsVec = _cellFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName().c_str(),
1186 types, typesF, pfls, locs);
1187 for ( size_t i = 0; i < valsVec.size(); ++i )
1188 nbSub += valsVec[i].size();
1190 // (1) write nb sub-components, title length
1191 *_sauvFile << setw(8) << nbSub
1192 << setw(8) << -1 // whatever
1193 << setw(8) << 6 // whatever
1194 << setw(8) << 72 << endl; // title length
1196 string title = _cellFields[iF]->getName();
1197 *_sauvFile << setw(72) << title.substr(0,71) << endl;
1198 *_sauvFile << setw(72) << " " << endl;
1200 // (3) support, nb components
1201 vector<int> vals(9, 0);
1202 const vector<string>& compInfo = _cellFields[iF]->getInfo();
1203 vals[2] = compInfo.size();
1205 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1207 pair<int,int> it = iters[iIt];
1209 vector<INTERP_KERNEL::NormalizedCellType> types;
1210 vector< vector<TypeOfField> > typesF;
1211 vector< vector<string> > pfls, locs;
1212 _cellFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName().c_str(),
1213 types, typesF, pfls, locs);
1214 for ( size_t iType = 0; iType < pfls.size(); ++iType )
1215 for ( size_t iP = 0; iP < pfls[iType].size(); ++iP )
1217 if ( pfls[iType][iP].empty() ) pfls[iType][iP] = noProfileName( types[iType] );
1218 map< string, SubMesh* >::iterator pfl2Sub = _profile2Sub.find( pfls[iType][iP] );
1219 if ( pfl2Sub == _profile2Sub.end() )
1220 THROW_IK_EXCEPTION( "SauvWriter::writeElemFields(): no sub-mesh for profile |"
1221 << pfls[iType][iP] << "|");
1222 const int supportID = pfl2Sub->second->_id;
1223 vals[0] = -supportID;
1225 for ( size_t i = 0; i < vals.size(); ++i, fcount++ )
1226 *_sauvFile << setw(8) << vals[ i ];
1231 // (4) dummy strings
1232 for ( fcount.init(4), iSub = 0; iSub < nbSub; ++iSub, fcount++ )
1236 // (5) dummy strings
1237 for ( fcount.init(8), iSub = 0; iSub < nbSub; ++iSub, fcount++ )
1241 // loop on sub-components of a field, each of which refers to
1242 // a certain support and has its own number of components
1243 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1245 pair<int,int> it = iters[iIt];
1246 writeElemTimeStamp( iF, it.first, it.second );
1248 } // loop on cell fields
1251 //================================================================================
1253 * \brief Write one elemental time stamp
1255 //================================================================================
1257 void SauvWriter::writeElemTimeStamp(int iF, int iter, int order)
1259 // (6) 317767 317761 317755 317815
1260 // (7) YOUN NU H SIGY
1261 // (8) REAL*8 REAL*8 REAL*8 REAL*8
1263 // (10) 2.00000000000000E+05
1265 // (10) 3.30000000000000E-01
1267 // (10) 1.00000000000000E+04
1269 // (10) 1.00000000000000E+02 1.00000000000000E+02 1.00000000000000E+02
1270 // (10) 1.00000000000000E+02 1.00000000000000E+02 1.00000000000000E+02
1272 TFieldCounter fcount (*_sauvFile, 10);
1274 vector<INTERP_KERNEL::NormalizedCellType> types;
1275 vector< vector<TypeOfField> > typesF;
1276 vector< vector<string> > pfls, locs;
1277 vector< vector< std::pair<int,int> > > valsVec;
1278 valsVec = _cellFields[iF]->getFieldSplitedByType( iter, order, _fileMesh->getName().c_str(),
1279 types, typesF, pfls, locs);
1280 for ( size_t iType = 0; iType < pfls.size(); ++iType )
1281 for ( size_t iP = 0; iP < pfls[iType].size(); ++iP )
1283 const vector<string>& compInfo = _cellFields[iF]->getInfo();
1285 // (6) component addresses
1286 int iComp = 0, nbComp = compInfo.size();
1287 for ( fcount.init(10); iComp < nbComp; ++iComp, fcount++ )
1288 *_sauvFile << setw(8) << 777; // a good number
1291 // (7) component names
1292 map<string, string> mapMedToGibi;
1293 makeCompNames( _cellFields[iF]->getName(), compInfo, mapMedToGibi );
1295 for ( fcount.init(8), iComp = 0; iComp < nbComp; ++iComp, fcount++ )
1296 *_sauvFile << " " << setw(8) << mapMedToGibi[compInfo[iComp]];
1299 // (8) component types
1300 for ( fcount.init(4), iComp = 0; iComp < nbComp; ++iComp, fcount++ )
1301 *_sauvFile << " " << setw(17) << "REAL*8";
1303 *_sauvFile << right;
1305 // (9) nb values per element, nb of elements
1306 int nbPntPerCell = 1;
1307 if ( !locs[iType][iP].empty() )
1309 int locID = _cellFields[iF]->getLocalizationId( locs[iType][iP].c_str() );
1310 nbPntPerCell = _cellFields[iF]->getNbOfGaussPtPerCell( locID );
1312 else if ( typesF[iType][iP] == ON_GAUSS_NE )
1314 nbPntPerCell = INTERP_KERNEL::CellModel::GetCellModel(types[iType]).getNumberOfNodes();
1318 const std::pair<int,int>& bgEnd = valsVec[iType][iP];
1319 const DataArrayDouble* valArray = _cellFields[iF]->getUndergroundDataArray(iter, order);
1320 for ( iComp = 0; iComp < nbComp; ++iComp )
1322 *_sauvFile << setw(8) << nbPntPerCell
1323 << setw(8) << (bgEnd.second-bgEnd.first) / nbPntPerCell
1328 for ( size_t i = bgEnd.first; i < (size_t) bgEnd.second; ++i, fcount++ )
1329 *_sauvFile << setw(22) << valArray->getIJ( i, iComp );
1335 //================================================================================
1337 * \brief Write the last record of the SAUV file
1339 //================================================================================
1341 void SauvWriter::writeLastRecord()
1343 *_sauvFile << " ENREGISTREMENT DE TYPE 5" << endl;
1344 *_sauvFile << "LABEL AUTOMATIQUE : 1" << endl;