1 // Copyright (C) 2007-2019 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, or (at your option) any later version.
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 MEDCoupling;
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, returns 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 SauvWriter::SauvWriter():_cpy_grp_if_on_single_family(false)
219 SauvWriter* SauvWriter::New()
221 return new SauvWriter;
224 std::size_t SauvWriter::getHeapMemorySizeWithoutChildren() const
229 std::vector<const BigMemoryObject *> SauvWriter::getDirectChildrenWithNull() const
231 return std::vector<const BigMemoryObject *>();
234 void SauvWriter::setCpyGrpIfOnASingleFamilyStatus(bool status)
236 _cpy_grp_if_on_single_family=status;
239 bool SauvWriter::getCpyGrpIfOnASingleFamilyStatus() const
241 return _cpy_grp_if_on_single_family;
244 //================================================================================
246 * \brief Fills own DS by MEDFileData
248 //================================================================================
250 void SauvWriter::setMEDFileDS(const MEDFileData* medData,
253 if ( !medData) THROW_IK_EXCEPTION("NULL MEDFileData");
255 MEDFileMeshes * meshes = medData->getMeshes();
256 MEDFileFields * fields = medData->getFields();
257 if ( !meshes) THROW_IK_EXCEPTION("No meshes in MEDFileData");
259 _fileMesh = meshes->getMeshAtPos( meshIndex );
260 _fileMesh->incrRef();
263 for ( int i = 0; i < fields->getNumberOfFields(); ++i )
265 MEDFileAnyTypeFieldMultiTS * fB = fields->getFieldAtPos(i);
266 MEDFileFieldMultiTS * f = dynamic_cast<MEDFileFieldMultiTS *>(fB);
268 continue;// fields on int32 not managed
269 if ( f->getMeshName() == _fileMesh->getName() )
271 vector< vector<TypeOfField> > fTypes = f->getTypesOfFieldAvailable();
272 if ( fTypes[0].size() == 1 && fTypes[0][0] == ON_NODES )
273 _nodeFields.push_back( f );
275 _cellFields.push_back( f );
280 //================================================================================
282 * \brief Adds a submesh
284 //================================================================================
286 SauvWriter::SubMesh* SauvWriter::addSubMesh(const std::string& name, int dimRelExt)
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();
293 sm._dimRelExt = dimRelExt;
296 //================================================================================
298 * \brief Returns nb of cell types
300 //================================================================================
302 int SauvWriter::SubMesh::nbTypes() const
305 for (int i = 0; i < cellIDsByTypeSize(); ++i )
306 nb += int( !_cellIDsByType[i].empty() );
310 //================================================================================
314 //================================================================================
316 void SauvWriter::fillSubMeshes( int& nbSauvObjects, map<string,int>& nameNbMap )
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();
324 _subs.reserve( nbSubs );
326 fillFamilySubMeshes();
327 fillGroupSubMeshes();
328 fillProfileSubMeshes();
330 // fill names of SubMesh'es and count nb of sauv sub-meshes they will be stored into
332 map<string,int> namePrefixMap;
333 for ( size_t i = 0; i < _subs.size(); ++i )
335 SubMesh& sm = _subs[i];
337 sm._nbSauvObjects = 0;
338 if ( sm._subs.empty() )
340 sm._nbSauvObjects = sm.nbTypes();
344 sm._nbSauvObjects = 1;
347 sm._id = nbSauvObjects+1;
348 nbSauvObjects += sm._nbSauvObjects;
350 if ( sm._nbSauvObjects )
351 sm._name = addName( nameNbMap, namePrefixMap, sm._name, sm._id );
353 if ( sm._nbSauvObjects && !sm._name.empty() )
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);
364 //================================================================================
366 * \brief fill sub-meshes of families
368 //================================================================================
370 void SauvWriter::fillFamilySubMeshes()
372 SubMesh* nilSm = (SubMesh*) 0;
373 std::vector<int> dims = _fileMesh->getNonEmptyLevelsExt();
374 for ( size_t iDim = 0; iDim < dims.size(); ++iDim )
376 int dimRelExt = dims[ iDim ];
377 MCAuto< MEDCouplingMesh > mesh = _fileMesh->getMeshAtLevel(dimRelExt);
378 const DataArrayInt * famIds = _fileMesh->getFamilyFieldAtLevel(dimRelExt);
379 if ( !famIds ) continue;
382 SubMesh* curSubMesh = addSubMesh( "", dimRelExt ); // submesh of zero family
383 _famIDs2Sub[0] = curSubMesh;
384 int sub0Index = _subs.size()-1;
386 const int * famID = famIds->begin(), * famIDEnd = famIds->end();
387 for ( int cellID = 0; famID < famIDEnd; ++famID, cellID++ )
389 if ( *famID != curFamID )
392 map< int, SubMesh* >::iterator f2s = _famIDs2Sub.insert( make_pair( curFamID, nilSm )).first;
394 f2s->second = addSubMesh( "", dimRelExt ); // no names for families
395 curSubMesh = f2s->second;
397 INTERP_KERNEL::NormalizedCellType cellType =
398 dimRelExt == 1 ? INTERP_KERNEL::NORM_POINT1 : mesh->getTypeOfCell( cellID );
399 curSubMesh->_cellIDsByType[ cellType ].push_back( cellID );
402 if ( dimRelExt == 1 )
404 // clear submesh of nodal zero family
405 _famIDs2Sub[0]->_cellIDsByType[ INTERP_KERNEL::NORM_POINT1 ].clear();
407 else if ( dimRelExt == 0 )
409 // make a submesh including all cells
410 if ( sub0Index == (int)(_subs.size()-1) )
412 _famIDs2Sub[0]->_name = _fileMesh->getName(); // there is the zero family only
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] );
426 //================================================================================
428 * \brief fill sub-meshes of groups
430 //================================================================================
432 void SauvWriter::fillGroupSubMeshes()
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 )
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() );
443 for ( size_t i = 0; i < famNames.size(); ++i )
445 int famID = _fileMesh->getFamilyId( famNames[i].c_str() );
446 map< int, SubMesh* >::iterator i2f = _famIDs2Sub.find( famID );
447 if ( i2f != _famIDs2Sub.end() )
449 famSubMeshes[ k ] = i2f->second;
455 // if a family exists but has no element, no submesh has been found for this family
456 // => we have to resize famSubMeshes with the number of submeshes stored
457 if (k != famNames.size())
458 famSubMeshes.resize(k);
459 SubMesh* grpSubMesh = addSubMesh( groupName, famSubMeshes[0]->_dimRelExt );
460 if( ! _cpy_grp_if_on_single_family )
462 grpSubMesh->_subs.swap( famSubMeshes );
466 /* If a group sub mesh consists of only one family, the group is written as
467 * a copy of this family.
468 * A mesh composed of only one submesh may cause an issue with some Gibi operators.*/
469 if (famSubMeshes.size() == 1)
471 for(int i = 0; i < famSubMeshes[0]->cellIDsByTypeSize() ; i++)
473 grpSubMesh->_cellIDsByType[i] = famSubMeshes[0]->_cellIDsByType[i];
477 grpSubMesh->_subs.swap( famSubMeshes );
483 //================================================================================
485 * \brief fill sub-meshes of profiles
487 //================================================================================
489 void SauvWriter::fillProfileSubMeshes()
491 _profile2Sub.clear();
492 SubMesh* nilSm = (SubMesh*) 0;
493 for ( int isOnNodes = 0; isOnNodes < 2; ++isOnNodes )
495 vector< MCAuto< MEDFileFieldMultiTS > >
496 fields = isOnNodes ? _nodeFields : _cellFields;
497 for ( size_t i = 0; i < fields.size(); ++i )
499 vector< pair<int,int> > iters = fields[i]->getIterations();
501 vector<INTERP_KERNEL::NormalizedCellType> types;
502 vector< vector<TypeOfField> > typesF;
503 vector< vector<string> > pfls, locs;
504 fields[i]->getFieldSplitedByType( iters[0].first, iters[0].second,
505 _fileMesh->getName().c_str(), types, typesF, pfls, locs);
507 for ( size_t iType = 0; iType < types.size(); ++iType )
509 if ( types[iType] == INTERP_KERNEL::NORM_ERROR )
510 dimRelExt = 1; // on nodes
512 dimRelExt = getDimension( types[iType] ) - _fileMesh->getMeshDimension();
513 for ( size_t iPfl = 0; iPfl < pfls[iType].size(); ++iPfl )
515 bool isOnAll = pfls[iType][iPfl].empty();
516 if ( isOnAll ) pfls[iType][iPfl] = noProfileName( types[iType] );
517 map< string, SubMesh* >::iterator pfl2sm =
518 _profile2Sub.insert( make_pair( pfls[iType][iPfl], nilSm )).first;
519 if ( !pfl2sm->second )
521 SubMesh* sm = pfl2sm->second = addSubMesh( "", dimRelExt ); // no names for profiles
522 const DataArrayInt * pfl = isOnAll ? 0 : fields[i]->getProfile( pfls[iType][iPfl].c_str() );
523 makeProfileIDs( sm, types[iType], pfl );
531 //================================================================================
533 * \brief Return max possible nb of sub-meshes to decsribe field supports
535 //================================================================================
537 int SauvWriter::evaluateNbProfileSubMeshes() const
540 for ( size_t i = 0; i < _nodeFields.size(); ++i )
541 nb += 1 + _nodeFields[i]->getPflsReallyUsed().size();
543 for ( size_t i = 0; i < _cellFields.size(); ++i )
545 nb += _cellFields[i]->getPflsReallyUsed().size();
547 vector< pair<int,int> > iters = _cellFields[i]->getIterations();
549 vector<INTERP_KERNEL::NormalizedCellType> types;
550 vector< vector<TypeOfField> > typesF;
551 vector< vector<string> > pfls, locs;
552 _cellFields[i]->getFieldSplitedByType( iters[0].first, iters[0].second,
553 _fileMesh->getName().c_str(), types, typesF, pfls, locs);
554 nb += 2 * types.size(); // x 2 - a type can be on nodes and on cells at the same time
560 //================================================================================
562 * \brief Transform a profile into ids of mesh elements
564 //================================================================================
566 void SauvWriter::makeProfileIDs( SubMesh* sm,
567 INTERP_KERNEL::NormalizedCellType type,
568 const DataArrayInt* profile )
570 MCAuto< MEDCouplingMesh >
571 mesh = _fileMesh->getMeshAtLevel(sm->_dimRelExt);
572 const MEDCouplingUMesh* uMesh = dynamic_cast< const MEDCouplingUMesh* > ((const MEDCouplingMesh*) mesh );
574 if ( sm->_dimRelExt == 1 ) type = INTERP_KERNEL::NORM_POINT1;
575 vector< int >& ids = sm->_cellIDsByType[ type ];
577 if ( sm->_dimRelExt == 1 || !uMesh )
579 // profile on nodes or mesh is CARTESIAN
582 ids.assign( profile->begin(), profile->end() );
586 ids.resize( sm->_dimRelExt == 1 ? mesh->getNumberOfNodes() : mesh->getNumberOfCells() );
587 for ( size_t i = 0; i < ids.size(); ++i )
596 if ( profile ) // on profile
598 code[1] = profile->getNumberOfTuples();
603 code[1] = mesh->getNumberOfCellsWithType( type );
606 vector<const DataArrayInt *> idsPerType( 1, profile );
608 resIDs = uMesh->checkTypeConsistencyAndContig( code, idsPerType );
609 if (( const DataArrayInt *) resIDs )
611 ids.assign( resIDs->begin(), resIDs->end() );
613 else // mesh includes only one type
616 for ( ids.resize( nbE ); nbE; --nbE )
617 ids[ nbE-1 ] = nbE-1;
622 //================================================================================
624 * \brief Write its data into the SAUVE file
626 //================================================================================
628 void SauvWriter::write(const std::string& fileName)
630 std::fstream fileStream;
631 fileStream.open( fileName.c_str(), ios::out);
634 ( !fileStream || !fileStream.is_open() )
636 ( !fileStream || !fileStream.rdbuf()->is_open() )
638 THROW_IK_EXCEPTION("Can't open the file |"<<fileName<<"|");
639 _sauvFile = &fileStream;
643 _profile2Sub.clear();
644 _longNames[ LN_MAIL ].clear();
645 _longNames[ LN_CHAM ].clear();
646 _longNames[ LN_COMP ].clear();
648 map<string,int> fldNamePrefixMap;
653 writeNodalFields(fldNamePrefixMap);
654 writeElemFields(fldNamePrefixMap);
660 //================================================================================
662 * \brief Writes "ENREGISTREMENT DE TYPE" 4 and 7
664 //================================================================================
666 void SauvWriter::writeFileHead()
668 MCAuto< MEDCouplingMesh > mesh = _fileMesh->getMeshAtLevel(0);
671 << " ENREGISTREMENT DE TYPE 4" << endl
672 << " NIVEAU 16 NIVEAU ERREUR 0 DIMENSION " << mesh->getSpaceDimension() <<endl
673 << " DENSITE 0.00000E+00" << endl
674 << " ENREGISTREMENT DE TYPE 7" << endl
675 << " NOMBRE INFO CASTEM2000 8" <<endl
676 << " IFOUR -1 NIFOUR 0 IFOMOD -1 IECHO 1 IIMPI 0 IOSPI 0 ISOTYP 1" << endl
677 << " NSDPGE 0" << endl;
680 //================================================================================
682 * \brief Writes names of objects
684 //================================================================================
686 void SauvWriter::writeNames( const map<string,int>& nameNbMap )
688 if ( !nameNbMap.empty() )
690 // write names of objects
691 // * 8001 FORMAT(8(1X,A8))
692 TFieldCounter fcount( *_sauvFile, 8 );
694 map<string,int>::const_iterator nameNbIt = nameNbMap.begin();
695 for ( ; nameNbIt != nameNbMap.end(); nameNbIt++, fcount++ )
696 *_sauvFile << " " << setw(8) << nameNbIt->first;
700 // write IDs of named objects in the pile
701 // * 8000 FORMAT(10I8)
702 nameNbIt = nameNbMap.begin();
703 for ( fcount.init(10); nameNbIt != nameNbMap.end(); nameNbIt++, fcount++ )
704 *_sauvFile << setw(8) << nameNbIt->second;
708 //================================================================================
710 * \brief Writes "PILE NUMERO 1"
712 //================================================================================
714 void SauvWriter::writeSubMeshes()
717 map<string,int> nameNbMap;
718 fillSubMeshes( nbSauvObjects, nameNbMap );
720 // * 800 FORMAT (' ENREGISTREMENT DE TYPE', I4)
721 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl;
722 // * 801 FORMAT(' PILE NUMERO',I4,'NBRE OBJETS NOMMES',I8,'NBRE OBJETS',I8)
723 *_sauvFile << " PILE NUMERO 1NBRE OBJETS NOMMES" << setw(8) << nameNbMap.size() <<
724 "NBRE OBJETS" << setw(8) << nbSauvObjects <<endl;
726 writeNames( nameNbMap );
728 TFieldCounter fcount( *_sauvFile, 10 ); // 10 integers per line
730 for ( size_t iSub = 0; iSub < _subs.size(); ++iSub )
732 SubMesh& sm = _subs[iSub];
733 if ( sm._nbSauvObjects < 1 ) continue;
735 // The first record of each sub-mesh writes
736 // - type of cells; zero means a compound object whose the 2nd record enumerates its components
737 // - number of components of a compound object
738 // - number of references; each reference means a "pointer" to this sub-mesh
739 // - number of nodes per cell
742 if ( !sm._subs.empty() )
744 writeCompoundSubMesh(iSub);
748 // write each sub-type as a SAUV sub-mesh
749 MCAuto< MEDCouplingMesh >
750 mesh = _fileMesh->getMeshAtLevel( sm._dimRelExt );
751 MCAuto< MEDCouplingUMesh>
752 umesh = mesh->buildUnstructured();
754 for ( int iType=0; iType < sm.cellIDsByTypeSize(); ++iType )
756 const vector<int>& cellIDs = sm._cellIDsByType[iType];
757 if ( cellIDs.empty() ) continue;
759 INTERP_KERNEL::NormalizedCellType
760 cellType = INTERP_KERNEL::NormalizedCellType( iType );
761 const INTERP_KERNEL::CellModel &
762 cell = INTERP_KERNEL::CellModel::GetCellModel( cellType );
763 int castemType = SauvUtilities::med2gibiGeom( cellType );
764 unsigned nbElemNodes = cell.getNumberOfNodes();
765 unsigned nbElems = cellIDs.size();
767 *_sauvFile << setw(8) << castemType
770 << setw(8) << nbElemNodes
771 << setw(8) << nbElems << endl;
773 // write color of each element
774 // * 8000 FORMAT(10I8)
775 for ( size_t i = 0; i < nbElems; ++i, fcount++ ) *_sauvFile << zeroI8;
778 // write connectivity
779 // gibi IDs are in FORTRAN mode while MEDCoupling IDs are in C mode
780 if ( sm._dimRelExt == 1 ) // nodes
782 for ( size_t i = 0; i < nbElems; ++i, fcount++ )
783 *_sauvFile << setw(8) << ( cellIDs[i] + 1 );
787 // indices to transform MED connectivity to GIBI one
788 const int * toMedConn = getGibi2MedQuadraticInterlace( cellType );
790 vector< int > cellConn( nbElemNodes ), transformedConn( nbElemNodes );
791 for ( size_t i = 0; i < nbElems; ++i )
794 umesh->getNodeIdsOfCell( cellIDs[i], cellConn );
797 for ( unsigned j = 0; j < nbElemNodes; ++j )
798 transformedConn[ toMedConn[ j ]] = cellConn[ j ];
799 cellConn.swap( transformedConn );
801 for ( unsigned j = 0; j < nbElemNodes; ++j, fcount++ )
802 *_sauvFile << setw(8) << ( cellConn[j] + 1 );
807 } // loop on cell types
808 } // not a compound object
809 } // loop on sub-meshes
812 //================================================================================
814 * \brief Writes a sum-mesh composed of other sum-meshes
815 * This submesh corresponds to a med mesh or group composed of families
817 //================================================================================
819 void SauvWriter::writeCompoundSubMesh(int iSub)
821 SubMesh& sm = _subs[iSub];
822 if ( sm._nbSauvObjects < 1 || sm._subs.empty()) return;
824 vector< int > subIDs;
825 for ( size_t i = 0; i < sm._subs.size(); ++i ) // loop on sub-meshes of families
826 for ( int j = 0; j < sm._subs[i]->_nbSauvObjects; ++j )
827 subIDs.push_back( sm._subs[i]->_id + j );
830 << setw(8) << subIDs.size()
835 TFieldCounter fcount( *_sauvFile, 10 ); // 10 integers per line
836 for ( size_t i = 0; i < subIDs.size(); ++i, fcount++ )
837 *_sauvFile << setw(8) << subIDs[i];
840 //================================================================================
842 * \brief Write piles relating to nodes
844 //================================================================================
846 void SauvWriter::writeNodes()
848 MCAuto< MEDCouplingMesh > mesh = _fileMesh->getMeshAtLevel( 1 );
849 MCAuto< MEDCouplingUMesh > umesh = mesh->buildUnstructured();
851 // write the index connecting nodes with their coordinates
853 const int nbNodes = umesh->getNumberOfNodes();
854 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl
855 << " PILE NUMERO 32NBRE OBJETS NOMMES 0NBRE OBJETS" << setw(8) << nbNodes << endl;
856 *_sauvFile << setw(8) << nbNodes << endl;
858 TFieldCounter fcount( *_sauvFile, 10 );// * 8000 FORMAT(10I8)
859 for ( int i = 0; i < nbNodes; ++i, fcount++ )
860 *_sauvFile << setw(8) << i + 1;
863 // write coordinates and density of nodes
865 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl;
866 *_sauvFile << " PILE NUMERO 33NBRE OBJETS NOMMES 0NBRE OBJETS 1" << endl;
868 const int dim = umesh->getSpaceDimension();
869 const int nbValues = nbNodes * ( dim + 1 );
870 *_sauvFile << setw(8) << nbValues << endl;
872 // * 8003 FORMAT(1P,3E22.14)
873 const char* density = " 0.00000000000000E+00";
875 _sauvFile->precision(14);
876 _sauvFile->setf( ios_base::scientific, ios_base::floatfield );
877 _sauvFile->setf( ios_base::uppercase );
878 MCAuto< DataArrayDouble> coordArray = umesh->getCoordinatesAndOwner();
879 const double precision = 1.e-99; // PAL12077
880 for ( int i = 0; i < nbNodes; ++i)
882 for ( int j = 0; j < dim; ++j, fcount++ )
884 double coo = coordArray->getIJ( i, j );
885 bool zero = ( -precision < coo && coo < precision );
886 *_sauvFile << setw(22) << ( zero ? 0.0 : coo );
888 *_sauvFile << density;
893 //================================================================================
895 * \brief Store correspondence between GIBI (short) and MED (long) names
897 * IMP 0020434: mapping GIBI names to MED names
898 * Store correspondence between GIBI and MED names as one PILE_STRINGS and one
899 * PILE_TABLES (in three tables: MED_MAIL, MED_CHAM and MED_COMP)
901 //================================================================================
903 void SauvWriter::writeLongNames()
906 3 - _longNames[ LN_MAIL ].empty() - _longNames[ LN_CHAM ].empty() - _longNames[ LN_COMP ].empty();
907 if (nbTables == 0) return;
909 // ---------------------
910 // Write the TABLE pile
911 // ---------------------
913 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl
914 << " PILE NUMERO 10NBRE OBJETS NOMMES" << setw(8) << nbTables
915 << "NBRE OBJETS" << setw(8) << nbTables << endl;
917 if (!_longNames[ LN_MAIL ].empty()) *_sauvFile << " MED_MAIL";
918 if (!_longNames[ LN_CHAM ].empty()) *_sauvFile << " MED_CHAM";
919 if (!_longNames[ LN_COMP ].empty()) *_sauvFile << " MED_COMP";
922 for ( int i = 0; i < nbTables; ++i ) *_sauvFile << setw(8) << i+1;
925 string theWholeString; // concatenated long names
926 vector<int> theOffsets;
928 TFieldCounter fcount (*_sauvFile, 10);
930 for ( int iTbl = 0; iTbl < LN_NB; ++iTbl )
932 vector<nameGIBItoMED>& longNames = _longNames[ iTbl ];
933 if ( longNames.empty() ) continue;
934 const bool isComp = ( iTbl == LN_COMP);
936 // to assure unique MED names
937 set<string> medUniqueNames;
939 *_sauvFile << setw(8) << longNames.size()*4 << endl; // Nb of table values
941 vector<nameGIBItoMED>::iterator itGIBItoMED = longNames.begin();
942 for (; itGIBItoMED != longNames.end(); itGIBItoMED++, iStr++)
944 // PILE of i-th key (med name)
945 *_sauvFile << setw(8) << PILE_STRINGS;
947 // ID of i-th key (med name)
948 *_sauvFile << setw(8) << iStr;
950 // PILE of i-th value (gibi name)
951 *_sauvFile << setw(8) << itGIBItoMED->gibi_pile;
953 // ID of i-th value (gibi name)
954 *_sauvFile << setw(8) << ( isComp ? ++iStr : itGIBItoMED->gibi_id );
957 // add a MED name to the string (while making it be unique for sub-meshes and fields)
958 string aMedName = itGIBItoMED->med_name;
960 for (int ind = 1; !medUniqueNames.insert(aMedName).second; ++ind )
961 aMedName = itGIBItoMED->med_name + "_" + SauvUtilities::toString( ind );
962 theWholeString += aMedName;
965 theOffsets.push_back( theWholeString.size() );
968 theWholeString += itGIBItoMED->gibi_name;
969 theOffsets.push_back( theWholeString.size() );
975 // ----------------------
976 // Write the STRING pile
977 // ----------------------
979 const int nbNames = theOffsets.size();
980 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl
981 << " PILE NUMERO 27NBRE OBJETS NOMMES" << zeroI8 << "NBRE OBJETS" << setw(8) << nbNames << endl
982 << setw(8) << theWholeString.length() << setw(8) << nbNames << endl;
984 // write the whole string
985 const int fixedLength = 71;
986 for ( string::size_type aPos = 0; aPos < theWholeString.length(); aPos += fixedLength)
987 *_sauvFile << setw(72) << theWholeString.substr(aPos, fixedLength) << endl;
990 for ( size_t i = 0; i < theOffsets.size(); ++i, fcount++ )
991 *_sauvFile << setw(8) << theOffsets[i];
994 //================================================================================
996 * \brief Write beginning of field record
998 //================================================================================
1000 void SauvWriter::writeFieldNames( const bool isNodal,
1001 std::map<std::string,int>& fldNamePrefixMap)
1003 vector< MCAuto< MEDFileFieldMultiTS > >&
1004 flds = isNodal ? _nodeFields : _cellFields;
1005 map<string,int> nameNbMap;
1007 for ( size_t iF = 0; iF < flds.size(); ++iF )
1009 string name = addName( nameNbMap, fldNamePrefixMap, flds[iF]->getName(), iF+1 );
1010 nameGIBItoMED aMEDName;
1011 aMEDName.gibi_pile = isNodal ? PILE_NODES_FIELD : PILE_FIELD;
1012 aMEDName.gibi_id = iF+1;
1013 aMEDName.med_name = name;
1014 _longNames[ LN_CHAM ].push_back(aMEDName);
1017 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl
1018 << ( isNodal ? " PILE NUMERO 2" : " PILE NUMERO 39")
1019 << "NBRE OBJETS NOMMES" << setw(8) << nameNbMap.size()
1020 << "NBRE OBJETS" << setw(8) << flds.size() << endl;
1021 writeNames( nameNbMap );
1024 //================================================================================
1026 * \brief Make short names of field components
1028 * IMP 0020434: mapping GIBI names to MED names
1030 //================================================================================
1032 void SauvWriter::makeCompNames(const string& fieldName,
1033 const vector<string>& compInfo,
1034 map<string, string>& mapMedToGibi)
1036 for ( size_t i = 0; i < compInfo.size(); ++i )
1037 mapMedToGibi[compInfo[i]] = cleanName( compInfo[i] );
1040 map<string, string>::iterator namesIt = mapMedToGibi.begin();
1041 for (; namesIt != mapMedToGibi.end(); namesIt++)
1043 string & compGibiName = (*namesIt).second;
1044 if (compGibiName.size() > 4) {
1045 // use new name in form "CXXX", where "XXX" is a number
1048 compGibiName = SauvUtilities::toString( compIndex++ );
1049 if ( compGibiName.size() < 3 )
1050 compGibiName.insert( 0, 3 - compGibiName.size(), '0' );
1051 compGibiName = "C" + compGibiName;
1053 while (mapMedToGibi.count(compGibiName) > 0); // real component name could be CXXX
1056 string compMedName = fieldName + "." + namesIt->first;
1057 nameGIBItoMED aMEDName;
1058 aMEDName.med_name = compMedName;
1059 aMEDName.gibi_pile = PILE_STRINGS;
1060 aMEDName.gibi_name = compGibiName;
1061 _longNames[ LN_COMP ].push_back(aMEDName);
1065 //================================================================================
1067 * \brief Writes "PILE NUMERO 2": fields on nodes
1069 //================================================================================
1071 void SauvWriter::writeNodalFields(map<string,int>& fldNamePrefixMap)
1073 writeFieldNames( /*isNodal=*/true, fldNamePrefixMap );
1075 TFieldCounter fcount (*_sauvFile, 10);
1077 // EXAMPLE ( with no values )
1080 // (2) -88 0 3 -89 0 1 -90 0 2 -91
1082 // (3) FX FY FZ FZ FX FY FLX
1083 // (4) 0 0 0 0 0 0 0
1084 // (5) cree par muc pri
1087 for ( size_t iF = 0; iF < _nodeFields.size(); ++iF )
1089 // (1) write nb subcomponents, nb components(total)
1090 vector< pair<int,int> > iters = _nodeFields[iF]->getIterations();
1091 const vector<string>& compInfo = _nodeFields[iF]->getInfo();
1092 const int nbSub = iters.size();
1093 const int nbComp = compInfo.size();
1094 const int totalNbComp = nbSub * nbComp;
1095 *_sauvFile << setw(8) << nbSub
1096 << setw(8) << totalNbComp
1097 << setw(8) << -1 // IFOUR
1098 << setw(8) << 0 << endl; // nb attributes
1100 // (2) for each sub-component (iteration)
1101 // write support, number of values and number of components
1103 vector< int > vals(3);
1104 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1106 pair<int,int> it = iters[iIt];
1108 vector<INTERP_KERNEL::NormalizedCellType> types;
1109 vector< vector<TypeOfField> > typesF;
1110 vector< vector<string> > pfls, locs;
1111 vector< vector< std::pair<int,int> > > valsVec;
1112 valsVec=_nodeFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName().c_str(),
1113 types, typesF, pfls, locs);
1114 // believe that there can be only one type in a nodal field,
1115 // so do not use a loop on types
1116 if ( pfls[0][0].empty() ) pfls[0][0] = noProfileName( types[0] );
1117 map< string, SubMesh* >::iterator pfl2Sub = _profile2Sub.find( pfls[0][0] );
1118 if ( pfl2Sub == _profile2Sub.end() )
1119 THROW_IK_EXCEPTION( "SauvWriter::writeNodalFields(): no sub-mesh for profile |"
1120 << pfls[0][0] << "|");
1121 vals[0] = -pfl2Sub->second->_id;
1122 vals[1] = (valsVec[0][0].second-valsVec[0][0].first);
1123 vals[2] = compInfo.size();
1124 for ( size_t i = 0; i < vals.size(); ++i, fcount++ )
1125 *_sauvFile << setw(8) << vals[i];
1129 // (3) Write names of components
1130 map<string, string> mapMedToGibi;
1131 makeCompNames( _nodeFields[iF]->getName(), compInfo, mapMedToGibi );
1134 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1135 for ( size_t i = 0; i < compInfo.size(); ++i, fcount++ )
1136 *_sauvFile << " " << setw(4) << mapMedToGibi[compInfo[i]];
1137 *_sauvFile << right;
1142 for ( size_t i = 0; i < (std::size_t)totalNbComp; ++i, fcount++ )
1143 *_sauvFile << " " << setw(8) << 0;
1146 string description = _nodeFields[iF]->getName();
1147 *_sauvFile << endl; // (5) TYPE
1148 *_sauvFile << setw(72) << description.substr(0,71) << endl; // (6) TITRE
1149 //*_sauvFile << endl; // (7) 0 attributes
1151 // write values of each component
1152 fcount.init( 3 ); // 3 values per a line
1153 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1155 pair<int,int> it = iters[iIt];
1157 vector<INTERP_KERNEL::NormalizedCellType> types;
1158 vector< vector<TypeOfField> > typesF;
1159 vector< vector<string> > pfls, locs;
1160 vector< vector< std::pair<int,int> > > valsVec;
1161 valsVec = _nodeFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName().c_str(),
1162 types, typesF, pfls, locs);
1163 // believe that there can be only one type in a nodal field,
1164 // so do not perform a loop on types
1165 const DataArrayDouble* valsArray = _nodeFields[iF]->getUndergroundDataArray(it.first, it.second);
1166 for ( size_t j = 0; j < compInfo.size(); ++j )
1168 for ( size_t i = valsVec[0][0].first; i < (std::size_t)valsVec[0][0].second; ++i, fcount++ )
1169 *_sauvFile << setw(22) << valsArray->getIJ( i, j );
1176 //================================================================================
1178 * \brief Writes "PILE NUMERO 39": fields on cells
1180 //================================================================================
1182 void SauvWriter::writeElemFields(map<string,int>& fldNamePrefixMap)
1184 writeFieldNames( /*isNodal=*/false, fldNamePrefixMap );
1186 TFieldCounter fcount (*_sauvFile, 10);
1191 // (2) CARACTERISTIQUES
1192 // (3) -15 317773 4 0 0 0 -2 0 3
1195 // (6) 317767 317761 317755 317815
1196 // (7) YOUN NU H SIGY
1197 // (8) REAL*8 REAL*8 REAL*8 REAL*8
1199 // (10) 2.00000000000000E+05
1201 // (10) 3.30000000000000E-01
1203 // (10) 1.00000000000000E+04
1205 // (10) 1.00000000000000E+02 1.00000000000000E+02 1.00000000000000E+02
1206 // (10) 1.00000000000000E+02 1.00000000000000E+02 1.00000000000000E+02
1209 for ( size_t iF = 0; iF < _cellFields.size(); ++iF )
1211 // count nb of sub-components
1212 int iSub, nbSub = 0;
1213 vector< pair<int,int> > iters = _cellFields[iF]->getIterations();
1214 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1216 pair<int,int> it = iters[iIt];
1218 vector<INTERP_KERNEL::NormalizedCellType> types;
1219 vector< vector<TypeOfField> > typesF;
1220 vector< vector<string> > pfls, locs;
1221 vector< vector< std::pair<int,int> > > valsVec;
1222 valsVec = _cellFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName().c_str(),
1223 types, typesF, pfls, locs);
1224 for ( size_t i = 0; i < valsVec.size(); ++i )
1225 nbSub += valsVec[i].size();
1227 // (1) write nb sub-components, title length
1228 *_sauvFile << setw(8) << nbSub
1229 << setw(8) << -1 // whatever
1230 << setw(8) << 6 // whatever
1231 << setw(8) << 72 << endl; // title length
1233 string title = _cellFields[iF]->getName();
1234 *_sauvFile << setw(72) << title.substr(0,71) << endl;
1235 *_sauvFile << setw(72) << " " << endl;
1237 // (3) support, nb components
1238 vector<int> vals(9, 0);
1239 const vector<string>& compInfo = _cellFields[iF]->getInfo();
1240 vals[2] = compInfo.size();
1242 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1244 pair<int,int> it = iters[iIt];
1246 vector<INTERP_KERNEL::NormalizedCellType> types;
1247 vector< vector<TypeOfField> > typesF;
1248 vector< vector<string> > pfls, locs;
1249 _cellFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName().c_str(),
1250 types, typesF, pfls, locs);
1251 for ( size_t iType = 0; iType < pfls.size(); ++iType )
1252 for ( size_t iP = 0; iP < pfls[iType].size(); ++iP )
1254 if ( pfls[iType][iP].empty() ) pfls[iType][iP] = noProfileName( types[iType] );
1255 map< string, SubMesh* >::iterator pfl2Sub = _profile2Sub.find( pfls[iType][iP] );
1256 if ( pfl2Sub == _profile2Sub.end() )
1257 THROW_IK_EXCEPTION( "SauvWriter::writeElemFields(): no sub-mesh for profile |"
1258 << pfls[iType][iP] << "|");
1259 const int supportID = pfl2Sub->second->_id;
1260 vals[0] = -supportID;
1262 for ( size_t i = 0; i < vals.size(); ++i, fcount++ )
1263 *_sauvFile << setw(8) << vals[ i ];
1268 // (4) dummy strings
1269 for ( fcount.init(4), iSub = 0; iSub < nbSub; ++iSub, fcount++ )
1273 // (5) dummy strings
1274 for ( fcount.init(8), iSub = 0; iSub < nbSub; ++iSub, fcount++ )
1278 // loop on sub-components of a field, each of which refers to
1279 // a certain support and has its own number of components
1280 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1282 pair<int,int> it = iters[iIt];
1283 writeElemTimeStamp( iF, it.first, it.second );
1285 } // loop on cell fields
1288 //================================================================================
1290 * \brief Write one elemental time stamp
1292 //================================================================================
1294 void SauvWriter::writeElemTimeStamp(int iF, int iter, int order)
1296 // (6) 317767 317761 317755 317815
1297 // (7) YOUN NU H SIGY
1298 // (8) REAL*8 REAL*8 REAL*8 REAL*8
1300 // (10) 2.00000000000000E+05
1302 // (10) 3.30000000000000E-01
1304 // (10) 1.00000000000000E+04
1306 // (10) 1.00000000000000E+02 1.00000000000000E+02 1.00000000000000E+02
1307 // (10) 1.00000000000000E+02 1.00000000000000E+02 1.00000000000000E+02
1309 TFieldCounter fcount (*_sauvFile, 10);
1311 vector<INTERP_KERNEL::NormalizedCellType> types;
1312 vector< vector<TypeOfField> > typesF;
1313 vector< vector<string> > pfls, locs;
1314 vector< vector< std::pair<int,int> > > valsVec;
1315 valsVec = _cellFields[iF]->getFieldSplitedByType( iter, order, _fileMesh->getName().c_str(),
1316 types, typesF, pfls, locs);
1317 for ( size_t iType = 0; iType < pfls.size(); ++iType )
1318 for ( size_t iP = 0; iP < pfls[iType].size(); ++iP )
1320 const vector<string>& compInfo = _cellFields[iF]->getInfo();
1322 // (6) component addresses
1323 int iComp = 0, nbComp = compInfo.size();
1324 for ( fcount.init(10); iComp < nbComp; ++iComp, fcount++ )
1325 *_sauvFile << setw(8) << 777; // a good number
1328 // (7) component names
1329 map<string, string> mapMedToGibi;
1330 makeCompNames( _cellFields[iF]->getName(), compInfo, mapMedToGibi );
1332 for ( fcount.init(8), iComp = 0; iComp < nbComp; ++iComp, fcount++ )
1333 *_sauvFile << " " << setw(8) << mapMedToGibi[compInfo[iComp]];
1336 // (8) component types
1337 for ( fcount.init(4), iComp = 0; iComp < nbComp; ++iComp, fcount++ )
1338 *_sauvFile << " " << setw(17) << "REAL*8";
1340 *_sauvFile << right;
1342 // (9) nb values per element, nb of elements
1343 int nbPntPerCell = 1;
1344 if ( !locs[iType][iP].empty() )
1346 int locID = _cellFields[iF]->getLocalizationId( locs[iType][iP].c_str() );
1347 nbPntPerCell = _cellFields[iF]->getNbOfGaussPtPerCell( locID );
1349 else if ( typesF[iType][iP] == ON_GAUSS_NE )
1351 nbPntPerCell = INTERP_KERNEL::CellModel::GetCellModel(types[iType]).getNumberOfNodes();
1355 const std::pair<int,int>& bgEnd = valsVec[iType][iP];
1356 const DataArrayDouble* valArray = _cellFields[iF]->getUndergroundDataArray(iter, order);
1357 for ( iComp = 0; iComp < nbComp; ++iComp )
1359 *_sauvFile << setw(8) << nbPntPerCell
1360 << setw(8) << (bgEnd.second-bgEnd.first) / nbPntPerCell
1365 for ( size_t i = bgEnd.first; i < (size_t) bgEnd.second; ++i, fcount++ )
1366 *_sauvFile << setw(22) << valArray->getIJ( i, iComp );
1372 //================================================================================
1374 * \brief Write the last record of the SAUV file
1376 //================================================================================
1378 void SauvWriter::writeLastRecord()
1380 *_sauvFile << " ENREGISTREMENT DE TYPE 5" << endl;
1381 *_sauvFile << "LABEL AUTOMATIQUE : 1" << endl;