3 # include "MEDMEM_Med.hxx"
4 # include "MEDMEM_DriverFactory.hxx"
5 # include "MEDMEM_STRING.hxx"
7 # include "MEDMEM_Mesh.hxx"
8 # include "MEDMEM_Grid.hxx"
9 # include "MEDMEM_Field.hxx"
11 # include "MEDMEM_Exception.hxx"
12 # include "utilities.h"
15 using namespace MEDMEM;
16 using namespace MED_EN;
22 MESSAGE("MED::MED()");
28 MED::MED(driverTypes driverType, const string & fileName)
30 const char * LOC = "MED::MED(driverTypes driverType, const string & fileName) : ";
33 MESSAGE(LOC << "driverType = " << driverType);
35 int current = addDriver(driverType,fileName,MED_LECT);
37 _drivers[current]->open();
38 _drivers[current]->readFileStruct();
39 _drivers[current]->close();
49 const char * LOC = "MED::~MED() : ";
52 // Analysis of the object MED
56 map<FIELD_ *, MESH_NAME_>::const_iterator currentField;
58 for ( currentField=_meshName.begin();currentField != _meshName.end(); currentField++ ) {
59 if ( (*currentField).first != NULL) index++;
62 MESSAGE(LOC << " there is(are) " << index << " field(s):");
63 for ( currentField=_meshName.begin();currentField != _meshName.end(); currentField++ ) {
64 if ( (*currentField).first != NULL) MESSAGE(" " << ((*currentField).first)->getName().c_str());
67 map<MESH_NAME_, map<MED_EN::medEntityMesh,SUPPORT *> >::iterator itSupportOnMesh ;
69 for ( itSupportOnMesh=_support.begin();itSupportOnMesh != _support.end(); itSupportOnMesh++ ) {
70 map<MED_EN::medEntityMesh,SUPPORT *>::iterator itSupport ;
71 for ( itSupport=(*itSupportOnMesh).second.begin();itSupport!=(*itSupportOnMesh).second.end();itSupport++)
75 MESSAGE(LOC << " there is(are) " << index << " support(s):");
77 map<MESH_NAME_,MESH*>::const_iterator currentMesh;
79 for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
80 if ( (*currentMesh).second != NULL)
84 MESSAGE(LOC << " there is(are) " << index << " meshe(s):");
85 // for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
86 // if ( (*currentMesh).second != NULL)
88 // SCRUTE((*currentMesh).second);
90 // string meshName = ((*currentMesh).second)->getName();
92 // MESSAGE(" " << meshName);
96 // delete all ? : PG : YES !
97 // map<FIELD_ *, MESH_NAME_>::const_iterator currentField;
98 for ( currentField=_meshName.begin();currentField != _meshName.end(); currentField++ ) {
99 if ( (*currentField).first != NULL) {
100 // cast in right type to delete it !
101 switch ((*currentField).first->getValueType()) {
103 delete (FIELD<int>*) (*currentField).first ;
106 delete (FIELD<double>*) (*currentField).first ;
109 MESSAGE(LOC << "Field has type different of int or double, could not destroy its values array !") ;
110 delete (*currentField).first;
114 // map<MESH_NAME_, map<MED_EN::medEntityMesh,SUPPORT *> >::iterator itSupportOnMesh ;
115 for ( itSupportOnMesh=_support.begin();itSupportOnMesh != _support.end(); itSupportOnMesh++ ) {
116 map<MED_EN::medEntityMesh,SUPPORT *>::iterator itSupport ;
117 for ( itSupport=(*itSupportOnMesh).second.begin();itSupport!=(*itSupportOnMesh).second.end();itSupport++)
118 delete (*itSupport).second ;
121 // map<MESH_NAME_,MESH*>::const_iterator currentMesh;
122 for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
123 if ( (*currentMesh).second != NULL)
125 if (!((*currentMesh).second)->getIsAGrid())
126 delete (*currentMesh).second;
128 delete (GRID *) (*currentMesh).second;
132 index =_drivers.size();
134 MESSAGE(LOC << "In this object MED there is(are) " << index << " driver(s):");
136 for (unsigned int ind=0; ind < _drivers.size(); ind++ )
138 SCRUTE(_drivers[ind]);
139 if ( _drivers[ind] != NULL) delete _drivers[ind];
149 Create the specified driver and return its index reference to path to
150 read or write methods.
152 int MED::addDriver(driverTypes driverType,
153 const string & fileName="Default File Name.med",
154 MED_EN::med_mode_acces access) {
156 const char * LOC = "MED::addDriver(driverTypes driverType, const string & fileName=\"Default File Name.med\") : ";
160 MESSAGE(LOC << " the file name is " << fileName);
165 GENDRIVER *driver = DRIVERFACTORY::buildDriverForMed(driverType,fileName,
168 _drivers.push_back(driver);
170 int current = _drivers.size()-1;
172 driver->setId(current);
180 Duplicate the given driver and return its index reference to path to
181 read or write methods.
183 int MED::addDriver(GENDRIVER & driver) {
184 const char * LOC = "MED::addDriver(GENDRIVER &) : ";
189 SCRUTE(_drivers.size());
191 _drivers.push_back(&driver);
193 SCRUTE(_drivers.size());
198 current = _drivers.size()-1;
200 driver.setId(current);
209 Remove the driver referenced by its index.
211 void MED::rmDriver (int index/*=0*/)
212 throw (MED_EXCEPTION)
214 const char * LOC = "MED::rmDriver (int index=0): ";
218 //_drivers.erase(&_drivers[index]);
221 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
222 << "The index given is invalid, index must be between 0 and |"
230 ??? to do comment ???
232 void MED::writeFrom (int index/*=0*/)
233 throw (MED_EXCEPTION)
235 const char * LOC = "MED::write (int index=0): ";
238 if (_drivers[index]) {
239 // open and close are made by all objects !
240 _drivers[index]->writeFrom();
242 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
243 << "The index given is invalid, index must be between 0 and |"
251 Write all objects with the driver given by its index.
253 void MED::write (int index/*=0*/)
254 throw (MED_EXCEPTION)
256 const char * LOC = "MED::write (int index=0): ";
259 if (_drivers[index]) {
260 // open and close are made by the subsequent objects !
261 _drivers[index]->write();
264 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
265 << "The index given is invalid, index must be between 0 and |"
273 Parse all the file and generate empty object.
275 All object must be read explicitly later with their own method read
276 or use MED::read to read all.
278 This method is automaticaly call by constructor with driver information.
280 void MED::readFileStruct (int index/*=0*/)
281 throw (MED_EXCEPTION)
283 const char * LOC = "MED::readFileStruct (int index=0): ";
286 if (_drivers[index]) {
287 _drivers[index]->open();
288 _drivers[index]->readFileStruct();
289 _drivers[index]->close();
292 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
293 << "The index given is invalid, index must be between 0 and |"
300 Read all objects in the file specified in the driver given by its index.
302 void MED::read (int index/*=0*/)
303 throw (MED_EXCEPTION)
305 const char * LOC = "MED::read (int index=0): ";
308 if (_drivers[index]) {
309 // open and close are made by all objects !
311 SCRUTE(_drivers[index]);
312 SCRUTE(&_drivers[index]);
313 // _drivers[index]->open();
314 _drivers[index]->read();
315 // _drivers[index]->close();
318 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
319 << "The index given is invalid, index must be between >0 and < |"
327 // ------- End Of Drivers Management Part
330 Get the number of MESH objects.
332 int MED::getNumberOfMeshes ( void ) const {
334 const char * LOC = "MED::getNumberOfMeshes ( void ) const : ";
337 return _meshes.size();
341 Get the number of FIELD objects.
343 int MED::getNumberOfFields ( void ) const {
345 const char * LOC = "MED::getNumberOfFields ( void ) const : ";
348 return _fields.size(); // we get number of field with different name
352 Get the names of all MESH objects.
354 meshNames is an in/out argument.
356 It is a string array of size the
357 number of MESH objects. It must be allocated before calling
358 this method. All names are put in it.
360 void MED::getMeshNames ( string * meshNames ) const
361 throw (MED_EXCEPTION)
363 const char * LOC = "MED::getMeshNames ( string * ) const : ";
365 unsigned int meshNamesSize;
367 if ( ( meshNamesSize = sizeof(meshNames) / sizeof(string *) )
369 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
370 << "Size of parameter meshNames is |"
371 << meshNamesSize << "| and should be |"
372 << _meshes.size() << "| and should be |"
376 // REM : ALLOCATION D'UN TABLEAU DE POINTEURS SUR STRING FAITE PAR LE CLIENT
377 map<MESH_NAME_,MESH*>::const_iterator currentMesh; // ??ITERATEUR CONST SUR UN OBJET NON CONST ??
379 int meshNamesIndex = 0;
381 for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
382 meshNames[meshNamesIndex]=(*currentMesh).first;
383 meshNamesIndex++; // CF OPTIMISATION
390 Get the names of all MESH objects.
392 Return a deque<string> object which contain the name of all MESH objects.
394 deque<string> MED::getMeshNames () const {
396 const char * LOC = "MED::getMeshNames () const : ";
399 deque<string> meshNames(_meshes.size());
401 map<MESH_NAME_,MESH*>::const_iterator currentMesh; // ??ITERATEUR CONST SUR UN OBJET NON CONST ??
403 int meshNamesIndex = 0;
405 for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
406 meshNames[meshNamesIndex]=(*currentMesh).first;
407 meshNamesIndex++; // CF OPTIMISATION
416 Return a reference to the MESH object named meshName.
418 MESH * MED::getMesh ( const string & meshName ) const
419 throw (MED_EXCEPTION)
422 const char * LOC = "MED::getMesh ( const string & meshName ) const : ";
425 map<MESH_NAME_,MESH*>::const_iterator itMeshes = _meshes.find(meshName);
427 if ( itMeshes == _meshes.end() )
428 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
429 << "There is no known mesh named |"
434 return (*itMeshes).second;
440 \internal Return a reference to the MESH object associated with
443 MESH * MED::getMesh (const FIELD_ * const field ) const
444 throw (MED_EXCEPTION)
447 const char * LOC = "MED::getMesh ( const FIELD * field ) const : ";
450 FIELD_ * f = const_cast< FIELD_* > (field); // Comment faire mieux ?
451 map<FIELD_ *, MESH_NAME_>::const_iterator itMeshName = _meshName.find(f);
453 if ( itMeshName == _meshName.end() )
454 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
455 << "There is no known mesh associated with |"
456 << field << "| pointer"
460 string meshName = (*itMeshName).second;
461 map<MESH_NAME_,MESH*>::const_iterator itMeshes = _meshes.find(meshName);
462 if ( itMeshes == _meshes.end() )
463 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
464 << "There is no known mesh named |"
465 << meshName << " while it's associated with the found field |"
466 << field << "| pointer"
470 return (*itMeshes).second;
477 Get the names of all FIELD objects.
479 fieldNames is an in/out argument.
481 It is an array of string of size the
482 number of FIELD objects. It must be allocated before calling
483 this method. All names are put in it.
485 void MED::getFieldNames ( string * fieldNames ) const
486 throw (MED_EXCEPTION)
488 const char * LOC = "MED::getFieldNames ( string * ) const : ";
491 unsigned int fieldNamesSize = sizeof(fieldNames) / sizeof(string *);
493 if ( fieldNamesSize != _fields.size() )
494 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
495 << "Size of parameter fieldNames is |"
496 << fieldNamesSize << "| and should be |"
497 << _fields.size() << "| and should be |"
501 // REM : ALLOCATION D'UN TABLEAU DE POINTEURS SUR STRING FAITE PAR LE CLIENT
502 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator currentField;
504 int fieldNamesIndex = 0;
505 for ( currentField=_fields.begin();currentField != _fields.end(); currentField++ ) {
506 fieldNames[fieldNamesIndex]=(*currentField).first;
507 fieldNamesIndex++; // CF OPTIMISATION
515 Get the names of all FIELD objects.
517 Return a deque<string> object which contain the name of all FIELD objects.
519 deque<string> MED::getFieldNames () const {
521 const char * LOC = "MED::getFieldNames ( ) const : ";
524 deque<string> fieldNames(_fields.size());
526 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator currentField;
528 int fieldNamesIndex = 0;
530 for ( currentField=_fields.begin();currentField != _fields.end(); currentField++ ) {
531 fieldNames[fieldNamesIndex]=(*currentField).first;
532 fieldNamesIndex++; // CF OPTIMISATION
540 Return a deque<DT_IT_> which contain all iteration step for the FIELD
541 identified by its name.
543 deque<DT_IT_> MED::getFieldIteration (const string & fieldName) const
544 throw (MED_EXCEPTION)
547 const char * LOC = "MED::getFieldIteration ( const string & ) const : ";
550 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator itFields = _fields.find(fieldName);
552 if ( itFields == _fields.end() )
553 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
554 << "There is no known field named |"
558 // const MAP_DT_IT_ & myIterationMap = const_cast<const MAP_DT_IT_ & > ((*itFields).second);
559 const MAP_DT_IT_ & myIterationMap = (*itFields).second ;
560 MAP_DT_IT_::const_iterator currentIterator ;
562 int iterationSize = myIterationMap.size();
564 SCRUTE(iterationSize);
566 deque<DT_IT_> Iteration(iterationSize);
568 int iterationIndex = 0;
570 for (currentIterator = myIterationMap.begin();
571 currentIterator != myIterationMap.end(); currentIterator++ )
573 SCRUTE(((*currentIterator).first).dt);
574 SCRUTE(((*currentIterator).first).it);
576 Iteration[iterationIndex].dt = ((*currentIterator).first).dt;
577 Iteration[iterationIndex].it = ((*currentIterator).first).it;
579 // Iteration[iterationIndex]=(*currentIterator).first;
580 SCRUTE(Iteration[iterationIndex].dt);
581 SCRUTE(Iteration[iterationIndex].it);
582 iterationIndex++; // CF OPTIMISATION
590 Return a reference to the FIELD object named fieldName with
591 time step number dt and order number it.
593 FIELD_ * MED::getField ( const string & fieldName, const int dt=MED_NOPDT, const int it=MED_NOPDT ) const
594 throw (MED_EXCEPTION)
597 const char * LOC = "MED::getField ( const string &, const int, const int ) const : ";
600 MESSAGE(LOC << "fieldName = "<<fieldName<<", dt ="<<dt<<", it = "<<it);
607 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator itFields = _fields.find(fieldName);
609 if ( itFields == _fields.end() )
610 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
611 << "There is no known field named |"
616 const MAP_DT_IT_ & map_dtIt = const_cast<const MAP_DT_IT_ & > ((*itFields).second);
617 MAP_DT_IT_::const_iterator itMap_dtIt = map_dtIt.find(dtIt);
619 if ( itMap_dtIt == map_dtIt.end() )
620 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
621 << "There is no (dt,it) |("
622 << dt << "," << it << ")| associated with the found field |"
629 //return _fields[fieldName][dtIt];
630 return (*itMap_dtIt).second;
635 // fiend ostream & MED::operator<<(ostream &os,const MED & med) const {
640 Return a map<MED_EN::medEntityMesh,SUPPORT*> which contain
641 foreach entity, a reference to the SUPPORT on all elements.
643 const map<MED_EN::medEntityMesh,SUPPORT*> & MED::getSupports(const string & meshName) const
644 throw (MED_EXCEPTION)
646 const char * LOC = "MED::getSupports ( const string ) const : ";
649 map<MESH_NAME_, map<MED_EN::medEntityMesh,SUPPORT *> >::const_iterator itSupportOnMesh = _support.find(meshName) ;
651 if ( itSupportOnMesh == _support.end() )
652 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
653 << "There is no support on mesh named |"
658 return (*itSupportOnMesh).second ;
662 Return a reference to the SUPPORT object on all elements of entity
663 for the MESH named meshName.
665 SUPPORT * MED::getSupport (const string & meshName,MED_EN::medEntityMesh entity) const
666 throw (MED_EXCEPTION)
668 const char * LOC = "MED::getSupport ( const string, MED_EN::medEntityMesh ) const : ";
672 for (map<MESH_NAME_, map<MED_EN::medEntityMesh,SUPPORT *> >::const_iterator const_itSupportOnMesh=_support.begin(); const_itSupportOnMesh != _support.end();
673 const_itSupportOnMesh++ )
675 map<MED_EN::medEntityMesh,SUPPORT *>::const_iterator const_itSupport ;
676 for (const_itSupport=(*const_itSupportOnMesh).second.begin();
677 const_itSupport!=(*const_itSupportOnMesh).second.end();const_itSupport++) index++;
680 MESSAGE(LOC << "In this MED object there is(are) " << index << " support(s):");
682 for (map<MESH_NAME_, map<MED_EN::medEntityMesh,SUPPORT *> >::const_iterator const_itSupportOnMesh=_support.begin();const_itSupportOnMesh != _support.end(); const_itSupportOnMesh++ )
684 map<MED_EN::medEntityMesh,SUPPORT *>::const_iterator const_itSupport ;
685 for (const_itSupport=(*const_itSupportOnMesh).second.begin();
686 const_itSupport!=(*const_itSupportOnMesh).second.end();const_itSupport++)
688 MESSAGE(LOC << "Support on mesh " << (*const_itSupportOnMesh).first << " on entity " << (*const_itSupport).first << " : " << *((*const_itSupport).second));
693 map<MESH_NAME_, map<MED_EN::medEntityMesh,SUPPORT *> >::const_iterator const_itSupportOnMesh = _support.find(meshName) ;
695 if ( const_itSupportOnMesh == _support.end() )
696 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
697 << "There is no support on mesh named |"
702 // map<MED_EN::medEntityMesh,SUPPORT *> & SupportOnMesh = (map<MED_EN::medEntityMesh,SUPPORT *>&) ((*itSupportOnMesh).second) ;
703 // map<MED_EN::medEntityMesh,SUPPORT *>::const_iterator itSupport = SupportOnMesh.find(entity) ;
705 // if (itSupport == SupportOnMesh.end() )
706 // throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
707 // << "There is no support on entity "
708 // << entity << " in mesh named |"
709 // << meshName << "|"
714 map<MED_EN::medEntityMesh,SUPPORT *> SupportOnMesh = ((*const_itSupportOnMesh).second);
716 map<MED_EN::medEntityMesh,SUPPORT *>::const_iterator itSupport = SupportOnMesh.find(entity) ;
718 if (itSupport == SupportOnMesh.end() )
719 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
720 << "There is no support on entity "
721 << entity << " in mesh named |"
726 return (*itSupport).second ;
730 Temporary method : when all MESH objects are read, this methods
731 update all SUPPORT objects with the rigth dimension.
733 void MED::updateSupport ()
736 const char * LOC = "MED::updateSupport () : ";
739 map<MESH_NAME_, map<MED_EN::medEntityMesh,SUPPORT *> >::iterator itSupportOnMesh ;
740 for ( itSupportOnMesh=_support.begin();itSupportOnMesh != _support.end(); itSupportOnMesh++ ) {
741 map<MED_EN::medEntityMesh,SUPPORT *>::iterator itSupport ;
742 for ( itSupport=(*itSupportOnMesh).second.begin();itSupport!=(*itSupportOnMesh).second.end();itSupport++)
744 (*itSupport).second->update() ;
746 catch (MEDEXCEPTION & ex) {
747 // entity not defined in mesh -> we remove support on it !
748 MESSAGE(LOC<<ex.what());
749 delete (*itSupport).second ;
750 (*itSupportOnMesh).second.erase(itSupport) ; // that's rigth ????
759 Add the given MESH object. MED object control it,
760 and destroy it, so you must not destroy it after.
762 The meshName is given by the MESH object.
764 void MED::addMesh( MESH * const ptrMesh)
765 throw (MED_EXCEPTION)
767 const char * LOC = "MED::addMesh(const MESH * ptrMesh): ";
771 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrMesh must not be NULL !"));
774 if ( ! ( meshName = ptrMesh->getName()).size() )
775 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrMesh->_name must not be NULL !"));
777 // MESH * meshToMed = new MESH(*ptrMesh); DO WE HAVE TO COPY THE ENTRY MESH OR NOT ????? (NB)
779 _meshes[meshName] = ptrMesh; // if ptrMesh->meshName already exists it is modified
781 // _meshes[meshName] = meshToMed;
787 Add the given FIELD object. MED object control it,
788 and destroy it, so you must not destroy it after.
790 The fieldName is given by the FIELD object.
792 void MED::addField( FIELD_ * const ptrField)
793 throw (MED_EXCEPTION)
795 const char * LOC = "MED::addField(const FIELD_ * const ptrField): ";
799 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField must not be NULL !"));
802 if ( ! (fieldName = ptrField->getName()).size() )
803 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_name must not be NULL !"));
805 SUPPORT * ptrSupport;
806 if ( ! ( ptrSupport = (SUPPORT * ) ptrField->getSupport()) )
807 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_support must not be NULL !"));
810 if ( ! ( ptrMesh = ptrSupport->getMesh()) )
811 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_support->_mesh must not be NULL !"));
814 if ( ! ( meshName = ptrMesh->getName()).size() )
815 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_support->_mesh->_name must not be NULL !"));
818 dtIt.dt = ptrField->getIterationNumber();
819 dtIt.it = ptrField->getOrderNumber();
821 _fields [fieldName][dtIt] = ptrField; // if it already exists it is replaced
822 _meshName [ptrField] = meshName; // if it already exists it is replaced
823 _meshes [meshName] = ptrMesh; // if it already exists it is replaced
825 // int numberOfTypes = ptrSupport->getNumberOfTypes(); !! UNUSED VARIABLE !!
826 _support [meshName][ptrSupport->getEntity()] = ptrSupport;// if it already exists it is replaced