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 MEDFileAnyTypeFieldMultiTS * fB = fields->getFieldAtPos(i);
248 MEDFileFieldMultiTS * f = dynamic_cast<MEDFileFieldMultiTS *>(fB);
250 continue;// fields on int32 not managed
251 if ( f->getMeshName() == _fileMesh->getName() )
253 vector< vector<TypeOfField> > fTypes = f->getTypesOfFieldAvailable();
254 if ( fTypes[0].size() == 1 && fTypes[0][0] == ON_NODES )
255 _nodeFields.push_back( f );
257 _cellFields.push_back( f );
262 //================================================================================
264 * \brief Adds a submesh
266 //================================================================================
268 SauvWriter::SubMesh* SauvWriter::addSubMesh(const std::string& name, int dimRelExt)
270 if ( _subs.capacity() < _subs.size() + 1 )
271 THROW_IK_EXCEPTION("SauvWriter: INTERNAL error, wrong evaluation of nb of sub-meshes");
272 _subs.resize( _subs.size() + 1 );
273 SubMesh& sm = _subs.back();
275 sm._dimRelExt = dimRelExt;
278 //================================================================================
280 * \brief Returns nb of cell types
282 //================================================================================
284 int SauvWriter::SubMesh::nbTypes() const
287 for (int i = 0; i < cellIDsByTypeSize(); ++i )
288 nb += int( !_cellIDsByType[i].empty() );
292 //================================================================================
296 //================================================================================
298 void SauvWriter::fillSubMeshes( int& nbSauvObjects, map<string,int>& nameNbMap )
300 // evaluate nb of _subs in order to avoid re-allocation of _subs
301 int nbSubs = 1; // for the very mesh
302 nbSubs += _fileMesh->getFamilyInfo().size() + 4; // + 4 zero families (for each dimRelExt)
303 nbSubs += _fileMesh->getGroupInfo().size();
304 nbSubs += evaluateNbProfileSubMeshes();
306 _subs.reserve( nbSubs );
308 fillFamilySubMeshes();
309 fillGroupSubMeshes();
310 fillProfileSubMeshes();
312 // fill names of SubMesh'es and count nb of sauv sub-meshes they will be stored into
314 map<string,int> namePrefixMap;
315 for ( size_t i = 0; i < _subs.size(); ++i )
317 SubMesh& sm = _subs[i];
319 sm._nbSauvObjects = 0;
320 if ( sm._subs.empty() )
322 sm._nbSauvObjects = sm.nbTypes();
326 sm._nbSauvObjects = 1;
329 sm._id = nbSauvObjects+1;
330 nbSauvObjects += sm._nbSauvObjects;
332 if ( sm._nbSauvObjects )
333 sm._name = addName( nameNbMap, namePrefixMap, sm._name, sm._id );
335 if ( sm._nbSauvObjects && !sm._name.empty() )
337 nameGIBItoMED aMEDName;
338 aMEDName.gibi_pile = PILE_SOUS_MAILLAGE;
339 aMEDName.gibi_id = sm._id;
340 aMEDName.med_name = sm._name;
341 _longNames[ LN_MAIL ].push_back(aMEDName);
346 //================================================================================
348 * \brief fill sub-meshes of families
350 //================================================================================
352 void SauvWriter::fillFamilySubMeshes()
354 SubMesh* nilSm = (SubMesh*) 0;
355 std::vector<int> dims = _fileMesh->getNonEmptyLevelsExt();
356 for ( size_t iDim = 0; iDim < dims.size(); ++iDim )
358 int dimRelExt = dims[ iDim ];
359 MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh > mesh = _fileMesh->getGenMeshAtLevel(dimRelExt);
360 const DataArrayInt * famIds = _fileMesh->getFamilyFieldAtLevel(dimRelExt);
361 if ( !famIds ) continue;
364 SubMesh* curSubMesh = addSubMesh( "", dimRelExt ); // submesh of zero family
365 _famIDs2Sub[0] = curSubMesh;
366 int sub0Index = _subs.size()-1;
368 const int * famID = famIds->begin(), * famIDEnd = famIds->end();
369 for ( int cellID = 0; famID < famIDEnd; ++famID, cellID++ )
371 if ( *famID != curFamID )
374 map< int, SubMesh* >::iterator f2s = _famIDs2Sub.insert( make_pair( curFamID, nilSm )).first;
376 f2s->second = addSubMesh( "", dimRelExt ); // no names for families
377 curSubMesh = f2s->second;
379 INTERP_KERNEL::NormalizedCellType cellType =
380 dimRelExt == 1 ? INTERP_KERNEL::NORM_POINT1 : mesh->getTypeOfCell( cellID );
381 curSubMesh->_cellIDsByType[ cellType ].push_back( cellID );
384 if ( dimRelExt == 1 )
386 // clear submesh of nodal zero family
387 _famIDs2Sub[0]->_cellIDsByType[ INTERP_KERNEL::NORM_POINT1 ].clear();
389 else if ( dimRelExt == 0 )
391 // make a submesh including all cells
392 if ( sub0Index == (int)(_subs.size()-1) )
394 _famIDs2Sub[0]->_name = _fileMesh->getName(); // there is the zero family only
398 curSubMesh = addSubMesh( _fileMesh->getName(), dimRelExt );
399 if ( _famIDs2Sub[0]->nbTypes() == 0 )
400 sub0Index++; // skip an empty zero family
401 for ( size_t i = sub0Index; i < _subs.size()-1; ++i )
402 curSubMesh->_subs.push_back( & _subs[i] );
408 //================================================================================
410 * \brief fill sub-meshes of groups
412 //================================================================================
414 void SauvWriter::fillGroupSubMeshes()
416 const map<string, vector<string> >& grpFams = _fileMesh->getGroupInfo();
417 map<string, vector<string> >::const_iterator g2ff = grpFams.begin();
418 for ( ; g2ff != grpFams.end(); ++g2ff )
420 const string& groupName = g2ff->first;
421 const vector<string>& famNames = g2ff->second;
422 if ( famNames.empty() ) continue;
423 std::vector<SubMesh*> famSubMeshes( famNames.size() );
425 for ( size_t i = 0; i < famNames.size(); ++i )
427 int famID = _fileMesh->getFamilyId( famNames[i].c_str() );
428 map< int, SubMesh* >::iterator i2f = _famIDs2Sub.find( famID );
429 if ( i2f != _famIDs2Sub.end() )
431 famSubMeshes[ k ] = i2f->second;
435 // if a family exists but has no element, no submesh has been found for this family
436 // => we have to resize famSubMeshes with the number of submeshes stored
437 if (k != famNames.size())
438 famSubMeshes.resize(k);
439 SubMesh* grpSubMesh = addSubMesh( groupName, famSubMeshes[0]->_dimRelExt );
440 grpSubMesh->_subs.swap( famSubMeshes );
445 //================================================================================
447 * \brief fill sub-meshes of profiles
449 //================================================================================
451 void SauvWriter::fillProfileSubMeshes()
453 _profile2Sub.clear();
454 SubMesh* nilSm = (SubMesh*) 0;
455 for ( int isOnNodes = 0; isOnNodes < 2; ++isOnNodes )
457 vector< MEDCouplingAutoRefCountObjectPtr< MEDFileFieldMultiTS > >
458 fields = isOnNodes ? _nodeFields : _cellFields;
459 for ( size_t i = 0; i < fields.size(); ++i )
461 vector< pair<int,int> > iters = fields[i]->getIterations();
463 vector<INTERP_KERNEL::NormalizedCellType> types;
464 vector< vector<TypeOfField> > typesF;
465 vector< vector<string> > pfls, locs;
466 fields[i]->getFieldSplitedByType( iters[0].first, iters[0].second,
467 _fileMesh->getName().c_str(), types, typesF, pfls, locs);
469 for ( size_t iType = 0; iType < types.size(); ++iType )
471 if ( types[iType] == INTERP_KERNEL::NORM_ERROR )
472 dimRelExt = 1; // on nodes
474 dimRelExt = getDimension( types[iType] ) - _fileMesh->getMeshDimension();
475 for ( size_t iPfl = 0; iPfl < pfls[iType].size(); ++iPfl )
477 bool isOnAll = pfls[iType][iPfl].empty();
478 if ( isOnAll ) pfls[iType][iPfl] = noProfileName( types[iType] );
479 map< string, SubMesh* >::iterator pfl2sm =
480 _profile2Sub.insert( make_pair( pfls[iType][iPfl], nilSm )).first;
481 if ( !pfl2sm->second )
483 SubMesh* sm = pfl2sm->second = addSubMesh( "", dimRelExt ); // no names for profiles
484 const DataArrayInt * pfl = isOnAll ? 0 : fields[i]->getProfile( pfls[iType][iPfl].c_str() );
485 makeProfileIDs( sm, types[iType], pfl );
493 //================================================================================
495 * \brief Return max possible nb of sub-meshes to decsribe field supports
497 //================================================================================
499 int SauvWriter::evaluateNbProfileSubMeshes() const
502 for ( size_t i = 0; i < _nodeFields.size(); ++i )
503 nb += 1 + _nodeFields[i]->getPflsReallyUsed().size();
505 for ( size_t i = 0; i < _cellFields.size(); ++i )
507 nb += _cellFields[i]->getPflsReallyUsed().size();
509 vector< pair<int,int> > iters = _cellFields[i]->getIterations();
511 vector<INTERP_KERNEL::NormalizedCellType> types;
512 vector< vector<TypeOfField> > typesF;
513 vector< vector<string> > pfls, locs;
514 _cellFields[i]->getFieldSplitedByType( iters[0].first, iters[0].second,
515 _fileMesh->getName().c_str(), types, typesF, pfls, locs);
516 nb += 2 * types.size(); // x 2 - a type can be on nodes and on cells at the same time
522 //================================================================================
524 * \brief Transorm a profile into ids of mesh elements
526 //================================================================================
528 void SauvWriter::makeProfileIDs( SubMesh* sm,
529 INTERP_KERNEL::NormalizedCellType type,
530 const DataArrayInt* profile )
532 MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh >
533 mesh = _fileMesh->getGenMeshAtLevel(sm->_dimRelExt);
534 const MEDCouplingUMesh* uMesh = dynamic_cast< const MEDCouplingUMesh* > ((const MEDCouplingMesh*) mesh );
536 if ( sm->_dimRelExt == 1 ) type = INTERP_KERNEL::NORM_POINT1;
537 vector< int >& ids = sm->_cellIDsByType[ type ];
539 if ( sm->_dimRelExt == 1 || !uMesh )
541 // profile on nodes or mesh is CARTESIAN
544 ids.assign( profile->begin(), profile->end() );
548 ids.resize( sm->_dimRelExt == 1 ? mesh->getNumberOfNodes() : mesh->getNumberOfCells() );
549 for ( size_t i = 0; i < ids.size(); ++i )
558 if ( profile ) // on profile
560 code[1] = profile->getNumberOfTuples();
565 code[1] = mesh->getNumberOfCellsWithType( type );
568 vector<const DataArrayInt *> idsPerType( 1, profile );
569 MEDCouplingAutoRefCountObjectPtr<DataArrayInt>
570 resIDs = uMesh->checkTypeConsistencyAndContig( code, idsPerType );
571 ids.assign( resIDs->begin(), resIDs->end() );
575 //================================================================================
577 * \brief Write its data into the SAUVE file
579 //================================================================================
581 void SauvWriter::write(const char* fileName)
583 std::fstream fileStream;
584 fileStream.open( fileName, ios::out);
587 ( !fileStream || !fileStream.is_open() )
589 ( !fileStream || !fileStream.rdbuf()->is_open() )
591 THROW_IK_EXCEPTION("Can't open the file |"<<fileName<<"|");
592 _sauvFile = &fileStream;
596 _profile2Sub.clear();
597 _longNames[ LN_MAIL ].clear();
598 _longNames[ LN_CHAM ].clear();
599 _longNames[ LN_COMP ].clear();
601 map<string,int> fldNamePrefixMap;
606 writeNodalFields(fldNamePrefixMap);
607 writeElemFields(fldNamePrefixMap);
613 //================================================================================
615 * \brief Writes "ENREGISTREMENT DE TYPE" 4 and 7
617 //================================================================================
619 void SauvWriter::writeFileHead()
621 MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh > mesh = _fileMesh->getGenMeshAtLevel(0);
624 << " ENREGISTREMENT DE TYPE 4" << endl
625 << " NIVEAU 16 NIVEAU ERREUR 0 DIMENSION " << mesh->getSpaceDimension() <<endl
626 << " DENSITE 0.00000E+00" << endl
627 << " ENREGISTREMENT DE TYPE 7" << endl
628 << " NOMBRE INFO CASTEM2000 8" <<endl
629 << " IFOUR -1 NIFOUR 0 IFOMOD -1 IECHO 1 IIMPI 0 IOSPI 0 ISOTYP 1" << endl
630 << " NSDPGE 0" << endl;
633 //================================================================================
635 * \brief Writes names of objects
637 //================================================================================
639 void SauvWriter::writeNames( const map<string,int>& nameNbMap )
641 if ( !nameNbMap.empty() )
643 // write names of objects
644 // * 8001 FORMAT(8(1X,A8))
645 TFieldCounter fcount( *_sauvFile, 8 );
647 map<string,int>::const_iterator nameNbIt = nameNbMap.begin();
648 for ( ; nameNbIt != nameNbMap.end(); nameNbIt++, fcount++ )
649 *_sauvFile << " " << setw(8) << nameNbIt->first;
653 // write IDs of named objects in the pile
654 // * 8000 FORMAT(10I8)
655 nameNbIt = nameNbMap.begin();
656 for ( fcount.init(10); nameNbIt != nameNbMap.end(); nameNbIt++, fcount++ )
657 *_sauvFile << setw(8) << nameNbIt->second;
661 //================================================================================
663 * \brief Writes "PILE NUMERO 1"
665 //================================================================================
667 void SauvWriter::writeSubMeshes()
670 map<string,int> nameNbMap;
671 fillSubMeshes( nbSauvObjects, nameNbMap );
673 // * 800 FORMAT (' ENREGISTREMENT DE TYPE', I4)
674 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl;
675 // * 801 FORMAT(' PILE NUMERO',I4,'NBRE OBJETS NOMMES',I8,'NBRE OBJETS',I8)
676 *_sauvFile << " PILE NUMERO 1NBRE OBJETS NOMMES" << setw(8) << nameNbMap.size() <<
677 "NBRE OBJETS" << setw(8) << nbSauvObjects <<endl;
679 writeNames( nameNbMap );
681 TFieldCounter fcount( *_sauvFile, 10 ); // 10 intergers per line
683 for ( size_t iSub = 0; iSub < _subs.size(); ++iSub )
685 SubMesh& sm = _subs[iSub];
686 if ( sm._nbSauvObjects < 1 ) continue;
688 // The first record of each sub-mesh writes
689 // - type of cells; zero means a compound object whose the 2nd record enumerates its components
690 // - number of components of a compound object
691 // - number of references; each reference means a "pointer" to this sub-mesh
692 // - number of nodes per cell
695 if ( !sm._subs.empty() )
697 writeCompoundSubMesh(iSub);
701 // write each sub-type as a SAUV sub-mesh
702 MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh >
703 mesh = _fileMesh->getGenMeshAtLevel( sm._dimRelExt );
704 MEDCouplingAutoRefCountObjectPtr< MEDCouplingUMesh>
705 umesh = mesh->buildUnstructured();
707 for ( int iType=0; iType < sm.cellIDsByTypeSize(); ++iType )
709 const vector<int>& cellIDs = sm._cellIDsByType[iType];
710 if ( cellIDs.empty() ) continue;
712 INTERP_KERNEL::NormalizedCellType
713 cellType = INTERP_KERNEL::NormalizedCellType( iType );
714 const INTERP_KERNEL::CellModel &
715 cell = INTERP_KERNEL::CellModel::GetCellModel( cellType );
716 int castemType = SauvUtilities::med2gibiGeom( cellType );
717 unsigned nbElemNodes = cell.getNumberOfNodes();
718 unsigned nbElems = cellIDs.size();
720 *_sauvFile << setw(8) << castemType
723 << setw(8) << nbElemNodes
724 << setw(8) << nbElems << endl;
726 // write color of each element
727 // * 8000 FORMAT(10I8)
728 for ( size_t i = 0; i < nbElems; ++i, fcount++ ) *_sauvFile << zeroI8;
731 // write connectivity
732 // gibi IDs are in FORTRAN mode while MEDCoupling IDs are in C mode
733 if ( sm._dimRelExt == 1 ) // nodes
735 for ( size_t i = 0; i < nbElems; ++i, fcount++ )
736 *_sauvFile << setw(8) << ( cellIDs[i] + 1 );
740 // indices to transform MED connectivity to GIBI one
741 const int * toMedConn = getGibi2MedQuadraticInterlace( cellType );
743 vector< int > cellConn( nbElemNodes ), transformedConn( nbElemNodes );
744 for ( size_t i = 0; i < nbElems; ++i )
747 umesh->getNodeIdsOfCell( cellIDs[i], cellConn );
750 for ( unsigned j = 0; j < nbElemNodes; ++j )
751 transformedConn[ toMedConn[ j ]] = cellConn[ j ];
752 cellConn.swap( transformedConn );
754 for ( unsigned j = 0; j < nbElemNodes; ++j, fcount++ )
755 *_sauvFile << setw(8) << ( cellConn[j] + 1 );
760 } // loop on cell types
761 } // not a compound object
762 } // loop on sub-meshes
765 //================================================================================
767 * \brief Writes a sum-mesh composed of other sum-meshes
768 * This submesh corresponds to a med mesh or group composed of families
770 //================================================================================
772 void SauvWriter::writeCompoundSubMesh(int iSub)
774 SubMesh& sm = _subs[iSub];
775 if ( sm._nbSauvObjects < 1 || sm._subs.empty()) return;
777 vector< int > subIDs;
778 for ( size_t i = 0; i < sm._subs.size(); ++i ) // loop on sub-meshes of families
779 for ( int j = 0; j < sm._subs[i]->_nbSauvObjects; ++j )
780 subIDs.push_back( sm._subs[i]->_id + j );
783 << setw(8) << subIDs.size()
788 TFieldCounter fcount( *_sauvFile, 10 ); // 10 intergers per line
789 for ( size_t i = 0; i < subIDs.size(); ++i, fcount++ )
790 *_sauvFile << setw(8) << subIDs[i];
793 //================================================================================
795 * \brief Write piles relating to nodes
797 //================================================================================
799 void SauvWriter::writeNodes()
801 MEDCouplingAutoRefCountObjectPtr< MEDCouplingMesh > mesh = _fileMesh->getGenMeshAtLevel( 1 );
802 MEDCouplingAutoRefCountObjectPtr< MEDCouplingUMesh > umesh = mesh->buildUnstructured();
804 // write the index connecting nodes with their coodrinates
806 const int nbNodes = umesh->getNumberOfNodes();
807 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl
808 << " PILE NUMERO 32NBRE OBJETS NOMMES 0NBRE OBJETS" << setw(8) << nbNodes << endl;
809 *_sauvFile << setw(8) << nbNodes << endl;
811 TFieldCounter fcount( *_sauvFile, 10 );// * 8000 FORMAT(10I8)
812 for ( int i = 0; i < nbNodes; ++i, fcount++ )
813 *_sauvFile << setw(8) << i + 1;
816 // write coordinates and density of nodes
818 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl;
819 *_sauvFile << " PILE NUMERO 33NBRE OBJETS NOMMES 0NBRE OBJETS 1" << endl;
821 const int dim = umesh->getSpaceDimension();
822 const int nbValues = nbNodes * ( dim + 1 );
823 *_sauvFile << setw(8) << nbValues << endl;
825 // * 8003 FORMAT(1P,3E22.14)
826 const char* density = " 0.00000000000000E+00";
828 _sauvFile->precision(14);
829 _sauvFile->setf( ios_base::scientific, ios_base::floatfield );
830 _sauvFile->setf( ios_base::uppercase );
831 MEDCouplingAutoRefCountObjectPtr< DataArrayDouble> coordArray = umesh->getCoordinatesAndOwner();
832 const double precision = 1.e-99; // PAL12077
833 for ( int i = 0; i < nbNodes; ++i)
835 for ( int j = 0; j < dim; ++j, fcount++ )
837 double coo = coordArray->getIJ( i, j );
838 bool zero = ( -precision < coo && coo < precision );
839 *_sauvFile << setw(22) << ( zero ? 0.0 : coo );
841 *_sauvFile << density;
846 //================================================================================
848 * \brief Store correspondence between GIBI (short) and MED (long) names
850 * IMP 0020434: mapping GIBI names to MED names
851 * Store correspondence between GIBI and MED names as one PILE_STRINGS and one
852 * PILE_TABLES (in three tables: MED_MAIL, MED_CHAM and MED_COMP)
854 //================================================================================
856 void SauvWriter::writeLongNames()
859 3 - _longNames[ LN_MAIL ].empty() - _longNames[ LN_CHAM ].empty() - _longNames[ LN_COMP ].empty();
860 if (nbTables == 0) return;
862 // ---------------------
863 // Write the TABLE pile
864 // ---------------------
866 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl
867 << " PILE NUMERO 10NBRE OBJETS NOMMES" << setw(8) << nbTables
868 << "NBRE OBJETS" << setw(8) << nbTables << endl;
870 if (!_longNames[ LN_MAIL ].empty()) *_sauvFile << " MED_MAIL";
871 if (!_longNames[ LN_CHAM ].empty()) *_sauvFile << " MED_CHAM";
872 if (!_longNames[ LN_COMP ].empty()) *_sauvFile << " MED_COMP";
875 for ( int i = 0; i < nbTables; ++i ) *_sauvFile << setw(8) << i+1;
878 string theWholeString; // concatenated long names
879 vector<int> theOffsets;
881 TFieldCounter fcount (*_sauvFile, 10);
883 for ( int iTbl = 0; iTbl < LN_NB; ++iTbl )
885 vector<nameGIBItoMED>& longNames = _longNames[ iTbl ];
886 if ( longNames.empty() ) continue;
887 const bool isComp = ( iTbl == LN_COMP);
889 // to assure unique MED names
890 set<string> medUniqueNames;
892 *_sauvFile << setw(8) << longNames.size()*4 << endl; // Nb of table values
894 vector<nameGIBItoMED>::iterator itGIBItoMED = longNames.begin();
895 for (; itGIBItoMED != longNames.end(); itGIBItoMED++, iStr++)
897 // PILE of i-th key (med name)
898 *_sauvFile << setw(8) << PILE_STRINGS;
900 // ID of i-th key (med name)
901 *_sauvFile << setw(8) << iStr;
903 // PILE of i-th value (gibi name)
904 *_sauvFile << setw(8) << itGIBItoMED->gibi_pile;
906 // ID of i-th value (gibi name)
907 *_sauvFile << setw(8) << ( isComp ? ++iStr : itGIBItoMED->gibi_id );
910 // add a MED name to the string (while making it be unique for sub-meshes and fields)
911 string aMedName = itGIBItoMED->med_name;
913 for (int ind = 1; !medUniqueNames.insert(aMedName).second; ++ind )
914 aMedName = itGIBItoMED->med_name + "_" + SauvUtilities::toString( ind );
915 theWholeString += aMedName;
918 theOffsets.push_back( theWholeString.size() );
921 theWholeString += itGIBItoMED->gibi_name;
922 theOffsets.push_back( theWholeString.size() );
928 // ----------------------
929 // Write the STRING pile
930 // ----------------------
932 const int nbNames = theOffsets.size();
933 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl
934 << " PILE NUMERO 27NBRE OBJETS NOMMES" << zeroI8 << "NBRE OBJETS" << setw(8) << nbNames << endl
935 << setw(8) << theWholeString.length() << setw(8) << nbNames << endl;
937 // write the whole string
938 const int fixedLength = 71;
939 for ( string::size_type aPos = 0; aPos < theWholeString.length(); aPos += fixedLength)
940 *_sauvFile << setw(72) << theWholeString.substr(aPos, fixedLength) << endl;
943 for ( size_t i = 0; i < theOffsets.size(); ++i, fcount++ )
944 *_sauvFile << setw(8) << theOffsets[i];
947 //================================================================================
949 * \brief Write beginning of field record
951 //================================================================================
953 void SauvWriter::writeFieldNames( const bool isNodal,
954 std::map<std::string,int>& fldNamePrefixMap)
956 vector< MEDCouplingAutoRefCountObjectPtr< MEDFileFieldMultiTS > >&
957 flds = isNodal ? _nodeFields : _cellFields;
958 map<string,int> nameNbMap;
960 for ( size_t iF = 0; iF < flds.size(); ++iF )
962 string name = addName( nameNbMap, fldNamePrefixMap, flds[iF]->getName(), iF+1 );
963 nameGIBItoMED aMEDName;
964 aMEDName.gibi_pile = isNodal ? PILE_NODES_FIELD : PILE_FIELD;
965 aMEDName.gibi_id = iF+1;
966 aMEDName.med_name = name;
967 _longNames[ LN_CHAM ].push_back(aMEDName);
970 *_sauvFile << " ENREGISTREMENT DE TYPE 2" << endl
971 << ( isNodal ? " PILE NUMERO 2" : " PILE NUMERO 39")
972 << "NBRE OBJETS NOMMES" << setw(8) << nameNbMap.size()
973 << "NBRE OBJETS" << setw(8) << flds.size() << endl;
974 writeNames( nameNbMap );
977 //================================================================================
979 * \brief Make short names of field components
981 * IMP 0020434: mapping GIBI names to MED names
983 //================================================================================
985 void SauvWriter::makeCompNames(const string& fieldName,
986 const vector<string>& compInfo,
987 map<string, string>& mapMedToGibi)
989 for ( size_t i = 0; i < compInfo.size(); ++i )
990 mapMedToGibi[compInfo[i]] = cleanName( compInfo[i] );
993 map<string, string>::iterator namesIt = mapMedToGibi.begin();
994 for (; namesIt != mapMedToGibi.end(); namesIt++)
996 string & compGibiName = (*namesIt).second;
997 if (compGibiName.size() > 4) {
998 // use new name in form "CXXX", where "XXX" is a number
1001 compGibiName = SauvUtilities::toString( compIndex++ );
1002 if ( compGibiName.size() < 3 )
1003 compGibiName.insert( 0, 3 - compGibiName.size(), '0' );
1004 compGibiName = "C" + compGibiName;
1006 while (mapMedToGibi.count(compGibiName) > 0); // real component name could be CXXX
1009 string compMedName = fieldName + "." + namesIt->first;
1010 nameGIBItoMED aMEDName;
1011 aMEDName.med_name = compMedName;
1012 aMEDName.gibi_pile = PILE_STRINGS;
1013 aMEDName.gibi_name = compGibiName;
1014 _longNames[ LN_COMP ].push_back(aMEDName);
1018 //================================================================================
1020 * \brief Writes "PILE NUMERO 2": fields on nodes
1022 //================================================================================
1024 void SauvWriter::writeNodalFields(map<string,int>& fldNamePrefixMap)
1026 writeFieldNames( /*isNodal=*/true, fldNamePrefixMap );
1028 TFieldCounter fcount (*_sauvFile, 10);
1030 // EXAMPLE ( with no values )
1033 // (2) -88 0 3 -89 0 1 -90 0 2 -91
1035 // (3) FX FY FZ FZ FX FY FLX
1036 // (4) 0 0 0 0 0 0 0
1037 // (5) cree par muc pri
1040 for ( size_t iF = 0; iF < _nodeFields.size(); ++iF )
1042 // (1) write nb subcomponents, nb components(total)
1043 vector< pair<int,int> > iters = _nodeFields[iF]->getIterations();
1044 const vector<string>& compInfo = _nodeFields[iF]->getInfo();
1045 const int nbSub = iters.size();
1046 const int nbComp = compInfo.size();
1047 const int totalNbComp = nbSub * nbComp;
1048 *_sauvFile << setw(8) << nbSub
1049 << setw(8) << totalNbComp
1050 << setw(8) << -1 // IFOUR
1051 << setw(8) << 0 << endl; // nb attributes
1053 // (2) for each sub-component (iteration)
1054 // write support, number of values and number of components
1056 vector< int > vals(3);
1057 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1059 pair<int,int> it = iters[iIt];
1061 vector<INTERP_KERNEL::NormalizedCellType> types;
1062 vector< vector<TypeOfField> > typesF;
1063 vector< vector<string> > pfls, locs;
1064 vector< vector< std::pair<int,int> > > valsVec;
1065 valsVec=_nodeFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName().c_str(),
1066 types, typesF, pfls, locs);
1067 // believe that there can be only one type in a nodal field,
1068 // so do not use a loop on types
1069 if ( pfls[0][0].empty() ) pfls[0][0] = noProfileName( types[0] );
1070 map< string, SubMesh* >::iterator pfl2Sub = _profile2Sub.find( pfls[0][0] );
1071 if ( pfl2Sub == _profile2Sub.end() )
1072 THROW_IK_EXCEPTION( "SauvWriter::writeNodalFields(): no sub-mesh for profile |"
1073 << pfls[0][0] << "|");
1074 vals[0] = -pfl2Sub->second->_id;
1075 vals[1] = (valsVec[0][0].second-valsVec[0][0].first);
1076 vals[2] = compInfo.size();
1077 for ( size_t i = 0; i < vals.size(); ++i, fcount++ )
1078 *_sauvFile << setw(8) << vals[i];
1082 // (3) Write names of components
1083 map<string, string> mapMedToGibi;
1084 makeCompNames( _nodeFields[iF]->getName(), compInfo, mapMedToGibi );
1087 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1088 for ( size_t i = 0; i < compInfo.size(); ++i, fcount++ )
1089 *_sauvFile << " " << setw(4) << mapMedToGibi[compInfo[i]];
1090 *_sauvFile << right;
1095 for ( size_t i = 0; i < (std::size_t)totalNbComp; ++i, fcount++ )
1096 *_sauvFile << " " << setw(8) << 0;
1099 string description = _nodeFields[iF]->getName();
1100 *_sauvFile << endl; // (5) TYPE
1101 *_sauvFile << setw(72) << description.substr(0,71) << endl; // (6) TITRE
1102 //*_sauvFile << endl; // (7) 0 attributes
1104 // write values of each component
1105 fcount.init( 3 ); // 3 values per a line
1106 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1108 pair<int,int> it = iters[iIt];
1110 vector<INTERP_KERNEL::NormalizedCellType> types;
1111 vector< vector<TypeOfField> > typesF;
1112 vector< vector<string> > pfls, locs;
1113 vector< vector< std::pair<int,int> > > valsVec;
1114 valsVec = _nodeFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName().c_str(),
1115 types, typesF, pfls, locs);
1116 // believe that there can be only one type in a nodal field,
1117 // so do not perform a loop on types
1118 const DataArrayDouble* valsArray = _nodeFields[iF]->getUndergroundDataArray(it.first, it.second);
1119 for ( size_t j = 0; j < compInfo.size(); ++j )
1121 for ( size_t i = valsVec[0][0].first; i < (std::size_t)valsVec[0][0].second; ++i, fcount++ )
1122 *_sauvFile << setw(22) << valsArray->getIJ( i, j );
1129 //================================================================================
1131 * \brief Writes "PILE NUMERO 39": fields on cells
1133 //================================================================================
1135 void SauvWriter::writeElemFields(map<string,int>& fldNamePrefixMap)
1137 writeFieldNames( /*isNodal=*/false, fldNamePrefixMap );
1139 TFieldCounter fcount (*_sauvFile, 10);
1144 // (2) CARACTERISTIQUES
1145 // (3) -15 317773 4 0 0 0 -2 0 3
1148 // (6) 317767 317761 317755 317815
1149 // (7) YOUN NU H SIGY
1150 // (8) REAL*8 REAL*8 REAL*8 REAL*8
1152 // (10) 2.00000000000000E+05
1154 // (10) 3.30000000000000E-01
1156 // (10) 1.00000000000000E+04
1158 // (10) 1.00000000000000E+02 1.00000000000000E+02 1.00000000000000E+02
1159 // (10) 1.00000000000000E+02 1.00000000000000E+02 1.00000000000000E+02
1162 for ( size_t iF = 0; iF < _cellFields.size(); ++iF )
1164 // count nb of sub-components
1165 int iSub, nbSub = 0;
1166 vector< pair<int,int> > iters = _cellFields[iF]->getIterations();
1167 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1169 pair<int,int> it = iters[iIt];
1171 vector<INTERP_KERNEL::NormalizedCellType> types;
1172 vector< vector<TypeOfField> > typesF;
1173 vector< vector<string> > pfls, locs;
1174 vector< vector< std::pair<int,int> > > valsVec;
1175 valsVec = _cellFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName().c_str(),
1176 types, typesF, pfls, locs);
1177 for ( size_t i = 0; i < valsVec.size(); ++i )
1178 nbSub += valsVec[i].size();
1180 // (1) write nb sub-components, title length
1181 *_sauvFile << setw(8) << nbSub
1182 << setw(8) << -1 // whatever
1183 << setw(8) << 6 // whatever
1184 << setw(8) << 72 << endl; // title length
1186 string title = _cellFields[iF]->getName();
1187 *_sauvFile << setw(72) << title.substr(0,71) << endl;
1188 *_sauvFile << setw(72) << " " << endl;
1190 // (3) support, nb components
1191 vector<int> vals(9, 0);
1192 const vector<string>& compInfo = _cellFields[iF]->getInfo();
1193 vals[2] = compInfo.size();
1195 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1197 pair<int,int> it = iters[iIt];
1199 vector<INTERP_KERNEL::NormalizedCellType> types;
1200 vector< vector<TypeOfField> > typesF;
1201 vector< vector<string> > pfls, locs;
1202 _cellFields[iF]->getFieldSplitedByType( it.first, it.second, _fileMesh->getName().c_str(),
1203 types, typesF, pfls, locs);
1204 for ( size_t iType = 0; iType < pfls.size(); ++iType )
1205 for ( size_t iP = 0; iP < pfls[iType].size(); ++iP )
1207 if ( pfls[iType][iP].empty() ) pfls[iType][iP] = noProfileName( types[iType] );
1208 map< string, SubMesh* >::iterator pfl2Sub = _profile2Sub.find( pfls[iType][iP] );
1209 if ( pfl2Sub == _profile2Sub.end() )
1210 THROW_IK_EXCEPTION( "SauvWriter::writeElemFields(): no sub-mesh for profile |"
1211 << pfls[iType][iP] << "|");
1212 const int supportID = pfl2Sub->second->_id;
1213 vals[0] = -supportID;
1215 for ( size_t i = 0; i < vals.size(); ++i, fcount++ )
1216 *_sauvFile << setw(8) << vals[ i ];
1221 // (4) dummy strings
1222 for ( fcount.init(4), iSub = 0; iSub < nbSub; ++iSub, fcount++ )
1226 // (5) dummy strings
1227 for ( fcount.init(8), iSub = 0; iSub < nbSub; ++iSub, fcount++ )
1231 // loop on sub-components of a field, each of which refers to
1232 // a certain support and has its own number of components
1233 for ( std::size_t iIt = 0; iIt < iters.size(); ++iIt )
1235 pair<int,int> it = iters[iIt];
1236 writeElemTimeStamp( iF, it.first, it.second );
1238 } // loop on cell fields
1241 //================================================================================
1243 * \brief Write one elemental time stamp
1245 //================================================================================
1247 void SauvWriter::writeElemTimeStamp(int iF, int iter, int order)
1249 // (6) 317767 317761 317755 317815
1250 // (7) YOUN NU H SIGY
1251 // (8) REAL*8 REAL*8 REAL*8 REAL*8
1253 // (10) 2.00000000000000E+05
1255 // (10) 3.30000000000000E-01
1257 // (10) 1.00000000000000E+04
1259 // (10) 1.00000000000000E+02 1.00000000000000E+02 1.00000000000000E+02
1260 // (10) 1.00000000000000E+02 1.00000000000000E+02 1.00000000000000E+02
1262 TFieldCounter fcount (*_sauvFile, 10);
1264 vector<INTERP_KERNEL::NormalizedCellType> types;
1265 vector< vector<TypeOfField> > typesF;
1266 vector< vector<string> > pfls, locs;
1267 vector< vector< std::pair<int,int> > > valsVec;
1268 valsVec = _cellFields[iF]->getFieldSplitedByType( iter, order, _fileMesh->getName().c_str(),
1269 types, typesF, pfls, locs);
1270 for ( size_t iType = 0; iType < pfls.size(); ++iType )
1271 for ( size_t iP = 0; iP < pfls[iType].size(); ++iP )
1273 const vector<string>& compInfo = _cellFields[iF]->getInfo();
1275 // (6) component addresses
1276 int iComp = 0, nbComp = compInfo.size();
1277 for ( fcount.init(10); iComp < nbComp; ++iComp, fcount++ )
1278 *_sauvFile << setw(8) << 777; // a good number
1281 // (7) component names
1282 map<string, string> mapMedToGibi;
1283 makeCompNames( _cellFields[iF]->getName(), compInfo, mapMedToGibi );
1285 for ( fcount.init(8), iComp = 0; iComp < nbComp; ++iComp, fcount++ )
1286 *_sauvFile << " " << setw(8) << mapMedToGibi[compInfo[iComp]];
1289 // (8) component types
1290 for ( fcount.init(4), iComp = 0; iComp < nbComp; ++iComp, fcount++ )
1291 *_sauvFile << " " << setw(17) << "REAL*8";
1293 *_sauvFile << right;
1295 // (9) nb values per element, nb of elements
1296 int nbPntPerCell = 1;
1297 if ( !locs[iType][iP].empty() )
1299 int locID = _cellFields[iF]->getLocalizationId( locs[iType][iP].c_str() );
1300 nbPntPerCell = _cellFields[iF]->getNbOfGaussPtPerCell( locID );
1302 else if ( typesF[iType][iP] == ON_GAUSS_NE )
1304 nbPntPerCell = INTERP_KERNEL::CellModel::GetCellModel(types[iType]).getNumberOfNodes();
1308 const std::pair<int,int>& bgEnd = valsVec[iType][iP];
1309 const DataArrayDouble* valArray = _cellFields[iF]->getUndergroundDataArray(iter, order);
1310 for ( iComp = 0; iComp < nbComp; ++iComp )
1312 *_sauvFile << setw(8) << nbPntPerCell
1313 << setw(8) << (bgEnd.second-bgEnd.first) / nbPntPerCell
1318 for ( size_t i = bgEnd.first; i < (size_t) bgEnd.second; ++i, fcount++ )
1319 *_sauvFile << setw(22) << valArray->getIJ( i, iComp );
1325 //================================================================================
1327 * \brief Write the last record of the SAUV file
1329 //================================================================================
1331 void SauvWriter::writeLastRecord()
1333 *_sauvFile << " ENREGISTREMENT DE TYPE 5" << endl;
1334 *_sauvFile << "LABEL AUTOMATIQUE : 1" << endl;