4 # include "MEDMEM_Med.hxx"
5 # include "MEDMEM_DriverFactory.hxx"
6 # include "MEDMEM_STRING.hxx"
8 # include "MEDMEM_Mesh.hxx"
9 # include "MEDMEM_Grid.hxx"
10 # include "MEDMEM_Field.hxx"
12 # include "MEDMEM_Exception.hxx"
13 # include "utilities.h"
14 using namespace MEDMEM;
20 MESSAGE("MED::MED()");
26 MED::MED(driverTypes driverType, const string & fileName)
28 const char * LOC = "MED::MED(driverTypes driverType, const string & fileName) : ";
31 MESSAGE(LOC << "driverType = " << driverType);
33 int current = addDriver(driverType,fileName);
35 _drivers[current]->open();
36 _drivers[current]->readFileStruct();
37 _drivers[current]->close();
47 const char * LOC = "MED::~MED() : ";
50 // Analysis of the object MED
54 map<FIELD_ *, MESH_NAME_>::const_iterator currentField;
56 for ( currentField=_meshName.begin();currentField != _meshName.end(); currentField++ ) {
57 if ( (*currentField).first != NULL) index++;
60 MESSAGE(LOC << " there is(are) " << index << " field(s):");
61 for ( currentField=_meshName.begin();currentField != _meshName.end(); currentField++ ) {
62 if ( (*currentField).first != NULL) MESSAGE(" " << ((*currentField).first)->getName().c_str());
65 map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::iterator itSupportOnMesh ;
67 for ( itSupportOnMesh=_support.begin();itSupportOnMesh != _support.end(); itSupportOnMesh++ ) {
68 map<MED_FR::med_entite_maillage,SUPPORT *>::iterator itSupport ;
69 for ( itSupport=(*itSupportOnMesh).second.begin();itSupport!=(*itSupportOnMesh).second.end();itSupport++)
73 MESSAGE(LOC << " there is(are) " << index << " support(s):");
75 map<MESH_NAME_,MESH*>::const_iterator currentMesh;
77 for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
78 if ( (*currentMesh).second != NULL)
82 MESSAGE(LOC << " there is(are) " << index << " meshe(s):");
83 // for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
84 // if ( (*currentMesh).second != NULL)
86 // SCRUTE((*currentMesh).second);
88 // string meshName = ((*currentMesh).second)->getName();
90 // MESSAGE(" " << meshName);
94 // delete all ? : PG : YES !
95 // map<FIELD_ *, MESH_NAME_>::const_iterator currentField;
96 for ( currentField=_meshName.begin();currentField != _meshName.end(); currentField++ ) {
97 if ( (*currentField).first != NULL) {
98 // cast in right type to delete it !
99 switch ((*currentField).first->getValueType()) {
101 delete (FIELD<int>*) (*currentField).first ;
104 delete (FIELD<double>*) (*currentField).first ;
107 MESSAGE(LOC << "Field has type different of int or double, could not destroy its values array !") ;
108 delete (*currentField).first;
112 // map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::iterator itSupportOnMesh ;
113 for ( itSupportOnMesh=_support.begin();itSupportOnMesh != _support.end(); itSupportOnMesh++ ) {
114 map<MED_FR::med_entite_maillage,SUPPORT *>::iterator itSupport ;
115 for ( itSupport=(*itSupportOnMesh).second.begin();itSupport!=(*itSupportOnMesh).second.end();itSupport++)
116 delete (*itSupport).second ;
119 // map<MESH_NAME_,MESH*>::const_iterator currentMesh;
120 for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
121 if ( (*currentMesh).second != NULL)
123 if (!((*currentMesh).second)->getIsAGrid())
124 delete (*currentMesh).second;
126 delete (GRID *) (*currentMesh).second;
130 index =_drivers.size();
132 MESSAGE(LOC << "In this object MED there is(are) " << index << " driver(s):");
134 for (unsigned int ind=0; ind < _drivers.size(); ind++ )
136 SCRUTE(_drivers[ind]);
137 if ( _drivers[ind] != NULL) delete _drivers[ind];
147 Create the specified driver and return its index reference to path to
148 read or write methods.
150 int MED::addDriver(driverTypes driverType, const string & fileName="Default File Name.med") {
152 const char * LOC = "MED::addDriver(driverTypes driverType, const string & fileName=\"Default File Name.med\") : ";
156 MESSAGE(LOC << " the file name is " << fileName);
160 GENDRIVER *driver = DRIVERFACTORY::buildDriverForMed(driverType,fileName,this);
162 _drivers.push_back(driver);
164 int current = _drivers.size()-1;
166 driver->setId(current);
174 Duplicate the given driver and return its index reference to path to
175 read or write methods.
177 int MED::addDriver(GENDRIVER & driver) {
178 const char * LOC = "MED::addDriver(GENDRIVER &) : ";
183 SCRUTE(_drivers.size());
185 _drivers.push_back(&driver);
187 SCRUTE(_drivers.size());
192 current = _drivers.size()-1;
194 driver.setId(current);
203 Remove the driver referenced by its index.
205 void MED::rmDriver (int index/*=0*/)
206 throw (MED_EXCEPTION)
208 const char * LOC = "MED::rmDriver (int index=0): ";
212 //_drivers.erase(&_drivers[index]);
215 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
216 << "The index given is invalid, index must be between 0 and |"
224 ??? to do comment ???
226 void MED::writeFrom (int index/*=0*/)
227 throw (MED_EXCEPTION)
229 const char * LOC = "MED::write (int index=0): ";
232 if (_drivers[index]) {
233 // open and close are made by all objects !
234 _drivers[index]->writeFrom();
236 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
237 << "The index given is invalid, index must be between 0 and |"
245 Write all objects with the driver given by its index.
247 void MED::write (int index/*=0*/)
248 throw (MED_EXCEPTION)
250 const char * LOC = "MED::write (int index=0): ";
253 if (_drivers[index]) {
254 // open and close are made by the subsequent objects !
255 _drivers[index]->write();
258 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
259 << "The index given is invalid, index must be between 0 and |"
267 Parse all the file and generate empty object.
269 All object must be read explicitly later with their own method read
270 or use MED::read to read all.
272 This method is automaticaly call by constructor with driver information.
274 void MED::readFileStruct (int index/*=0*/)
275 throw (MED_EXCEPTION)
277 const char * LOC = "MED::readFileStruct (int index=0): ";
280 if (_drivers[index]) {
281 _drivers[index]->open();
282 _drivers[index]->readFileStruct();
283 _drivers[index]->close();
286 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
287 << "The index given is invalid, index must be between 0 and |"
295 Read all objects in the file specified in the driver given by its index.
297 void MED::read (int index/*=0*/)
298 throw (MED_EXCEPTION)
300 const char * LOC = "MED::read (int index=0): ";
303 if (_drivers[index]) {
304 // open and close are made by all objects !
306 SCRUTE(_drivers[index]);
307 SCRUTE(&_drivers[index]);
308 // _drivers[index]->open();
309 _drivers[index]->read();
310 // _drivers[index]->close();
313 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
314 << "The index given is invalid, index must be between >0 and < |"
322 // ------- End Of Drivers Management Part
325 Get the number of MESH objects.
327 int MED::getNumberOfMeshes ( void ) const {
329 const char * LOC = "MED::getNumberOfMeshes ( void ) const : ";
332 return _meshes.size();
338 Get the number of FIELD objects.
340 int MED::getNumberOfFields ( void ) const {
342 const char * LOC = "MED::getNumberOfFields ( void ) const : ";
345 return _fields.size(); // we get number of field with different name
351 Get the names of all MESH objects.
353 meshNames is an in/out argument.
355 It is a string array of size the
356 number of MESH objects. It must be allocated before calling
357 this method. All names are put in it.
359 void MED::getMeshNames ( string * meshNames ) const
360 throw (MED_EXCEPTION)
362 const char * LOC = "MED::getMeshNames ( string * ) const : ";
364 unsigned int meshNamesSize;
366 if ( ( meshNamesSize = sizeof(meshNames) / sizeof(string *) )
368 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
369 << "Size of parameter meshNames is |"
370 << meshNamesSize << "| and should be |"
371 << _meshes.size() << "| and should be |"
375 // REM : ALLOCATION D'UN TABLEAU DE POINTEURS SUR STRING FAITE PAR LE CLIENT
376 map<MESH_NAME_,MESH*>::const_iterator currentMesh; // ??ITERATEUR CONST SUR UN OBJET NON CONST ??
378 int meshNamesIndex = 0;
380 for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
381 meshNames[meshNamesIndex]=(*currentMesh).first;
382 meshNamesIndex++; // CF OPTIMISATION
389 Get the names of all MESH objects.
391 Return a deque<string> object which contain the name of all MESH objects.
393 deque<string> MED::getMeshNames () const {
395 const char * LOC = "MED::getMeshNames () const : ";
398 deque<string> meshNames(_meshes.size());
400 map<MESH_NAME_,MESH*>::const_iterator currentMesh; // ??ITERATEUR CONST SUR UN OBJET NON CONST ??
402 int meshNamesIndex = 0;
404 for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
405 meshNames[meshNamesIndex]=(*currentMesh).first;
406 meshNamesIndex++; // CF OPTIMISATION
415 Return a reference to the MESH object named meshName.
417 MESH * MED::getMesh ( const string & meshName ) const
418 throw (MED_EXCEPTION)
421 const char * LOC = "MED::getMesh ( const string & meshName ) const : ";
424 map<MESH_NAME_,MESH*>::const_iterator itMeshes = _meshes.find(meshName);
426 if ( itMeshes == _meshes.end() )
427 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
428 << "There is no known mesh named |"
433 return (*itMeshes).second;
439 \internal Return a reference to the MESH object associated with
442 MESH * MED::getMesh (const FIELD_ * const field ) const
443 throw (MED_EXCEPTION)
446 const char * LOC = "MED::getMesh ( const FIELD * field ) const : ";
449 FIELD_ * f = const_cast< FIELD_* > (field); // Comment faire mieux ?
450 map<FIELD_ *, MESH_NAME_>::const_iterator itMeshName = _meshName.find(f);
452 if ( itMeshName == _meshName.end() )
453 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
454 << "There is no known mesh associated with |"
455 << field << "| pointer"
459 string meshName = (*itMeshName).second;
460 map<MESH_NAME_,MESH*>::const_iterator itMeshes = _meshes.find(meshName);
461 if ( itMeshes == _meshes.end() )
462 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
463 << "There is no known mesh named |"
464 << meshName << " while it's associated with the found field |"
465 << field << "| pointer"
469 return (*itMeshes).second;
476 Get the names of all FIELD objects.
478 fieldNames is an in/out argument.
480 It is an array of string of size the
481 number of FIELD objects. It must be allocated before calling
482 this method. All names are put in it.
484 void MED::getFieldNames ( string * fieldNames ) const
485 throw (MED_EXCEPTION)
487 const char * LOC = "MED::getFieldNames ( string * ) const : ";
490 unsigned int fieldNamesSize = sizeof(fieldNames) / sizeof(string *);
492 if ( fieldNamesSize != _fields.size() )
493 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
494 << "Size of parameter fieldNames is |"
495 << fieldNamesSize << "| and should be |"
496 << _fields.size() << "| and should be |"
500 // REM : ALLOCATION D'UN TABLEAU DE POINTEURS SUR STRING FAITE PAR LE CLIENT
501 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator currentField;
503 int fieldNamesIndex = 0;
504 for ( currentField=_fields.begin();currentField != _fields.end(); currentField++ ) {
505 fieldNames[fieldNamesIndex]=(*currentField).first;
506 fieldNamesIndex++; // CF OPTIMISATION
514 Get the names of all FIELD objects.
516 Return a deque<string> object which contain the name of all FIELD objects.
518 deque<string> MED::getFieldNames () const {
520 const char * LOC = "MED::getFieldNames ( ) const : ";
523 deque<string> fieldNames(_fields.size());
525 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator currentField;
527 int fieldNamesIndex = 0;
529 for ( currentField=_fields.begin();currentField != _fields.end(); currentField++ ) {
530 fieldNames[fieldNamesIndex]=(*currentField).first;
531 fieldNamesIndex++; // CF OPTIMISATION
539 Return a deque<DT_IT_> which contain all iteration step for the FIELD
540 identified by its name.
542 deque<DT_IT_> MED::getFieldIteration (const string & fieldName) const
543 throw (MED_EXCEPTION)
546 const char * LOC = "MED::getFieldIteration ( const string & ) const : ";
549 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator itFields = _fields.find(fieldName);
551 if ( itFields == _fields.end() )
552 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
553 << "There is no known field named |"
557 // const MAP_DT_IT_ & myIterationMap = const_cast<const MAP_DT_IT_ & > ((*itFields).second);
558 const MAP_DT_IT_ & myIterationMap = (*itFields).second ;
559 MAP_DT_IT_::const_iterator currentIterator ;
561 int iterationSize = myIterationMap.size();
563 SCRUTE(iterationSize);
565 deque<DT_IT_> Iteration(iterationSize);
567 int iterationIndex = 0;
569 for (currentIterator = myIterationMap.begin();
570 currentIterator != myIterationMap.end(); currentIterator++ )
572 SCRUTE(((*currentIterator).first).dt);
573 SCRUTE(((*currentIterator).first).it);
575 Iteration[iterationIndex].dt = ((*currentIterator).first).dt;
576 Iteration[iterationIndex].it = ((*currentIterator).first).it;
578 // Iteration[iterationIndex]=(*currentIterator).first;
579 SCRUTE(Iteration[iterationIndex].dt);
580 SCRUTE(Iteration[iterationIndex].it);
581 iterationIndex++; // CF OPTIMISATION
589 Return a reference to the FIELD object named fieldName with
590 time step number dt and order number it.
592 FIELD_ * MED::getField ( const string & fieldName, const int dt=MED_NOPDT, const int it=MED_NOPDT ) const
593 throw (MED_EXCEPTION)
596 const char * LOC = "MED::getField ( const string &, const int, const int ) const : ";
599 MESSAGE(LOC << "fieldName = "<<fieldName<<", dt ="<<dt<<", it = "<<it);
606 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator itFields = _fields.find(fieldName);
608 if ( itFields == _fields.end() )
609 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
610 << "There is no known field named |"
615 const MAP_DT_IT_ & map_dtIt = const_cast<const MAP_DT_IT_ & > ((*itFields).second);
616 MAP_DT_IT_::const_iterator itMap_dtIt = map_dtIt.find(dtIt);
618 if ( itMap_dtIt == map_dtIt.end() )
619 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
620 << "There is no (dt,it) |("
621 << dt << "," << it << ")| associated with the found field |"
628 //return _fields[fieldName][dtIt];
629 return (*itMap_dtIt).second;
634 // fiend ostream & MED::operator<<(ostream &os,const MED & med) const {
639 Return a map<MED_FR::med_entite_maillage,SUPPORT*> which contain
640 foreach entity, a reference to the SUPPORT on all elements.
642 const map<MED_FR::med_entite_maillage,SUPPORT*> & MED::getSupports(const string & meshName) const
643 throw (MED_EXCEPTION)
645 const char * LOC = "MED::getSupports ( const string ) const : ";
648 map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::const_iterator itSupportOnMesh = _support.find(meshName) ;
650 if ( itSupportOnMesh == _support.end() )
651 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
652 << "There is no support on mesh named |"
657 return (*itSupportOnMesh).second ;
661 Return a reference to the SUPPORT object on all elements of entity
662 for the MESH named meshName.
664 SUPPORT * MED::getSupport (const string & meshName,MED_FR::med_entite_maillage entity) const
665 throw (MED_EXCEPTION)
667 const char * LOC = "MED::getSupport ( const string, MED_FR::med_entite_maillage ) const : ";
671 for (map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::const_iterator const_itSupportOnMesh=_support.begin(); const_itSupportOnMesh != _support.end();
672 const_itSupportOnMesh++ )
674 map<MED_FR::med_entite_maillage,SUPPORT *>::const_iterator const_itSupport ;
675 for (const_itSupport=(*const_itSupportOnMesh).second.begin();
676 const_itSupport!=(*const_itSupportOnMesh).second.end();const_itSupport++) index++;
679 MESSAGE(LOC << "In this MED object there is(are) " << index << " support(s):");
681 for (map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::const_iterator const_itSupportOnMesh=_support.begin();const_itSupportOnMesh != _support.end(); const_itSupportOnMesh++ )
683 map<MED_FR::med_entite_maillage,SUPPORT *>::const_iterator const_itSupport ;
684 for (const_itSupport=(*const_itSupportOnMesh).second.begin();
685 const_itSupport!=(*const_itSupportOnMesh).second.end();const_itSupport++)
687 MESSAGE(LOC << "Support on mesh " << (*const_itSupportOnMesh).first << " on entity " << (*const_itSupport).first << " : " << *((*const_itSupport).second));
692 map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::const_iterator const_itSupportOnMesh = _support.find(meshName) ;
694 if ( const_itSupportOnMesh == _support.end() )
695 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
696 << "There is no support on mesh named |"
701 // map<MED_FR::med_entite_maillage,SUPPORT *> & SupportOnMesh = (map<MED_FR::med_entite_maillage,SUPPORT *>&) ((*itSupportOnMesh).second) ;
702 // map<MED_FR::med_entite_maillage,SUPPORT *>::const_iterator itSupport = SupportOnMesh.find(entity) ;
704 // if (itSupport == SupportOnMesh.end() )
705 // throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
706 // << "There is no support on entity "
707 // << entity << " in mesh named |"
708 // << meshName << "|"
713 map<MED_FR::med_entite_maillage,SUPPORT *> SupportOnMesh = ((*const_itSupportOnMesh).second);
715 map<MED_FR::med_entite_maillage,SUPPORT *>::const_iterator itSupport = SupportOnMesh.find(entity) ;
717 if (itSupport == SupportOnMesh.end() )
718 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
719 << "There is no support on entity "
720 << entity << " in mesh named |"
725 return (*itSupport).second ;
729 Temporary method : when all MESH objects are read, this methods
730 update all SUPPORT objects with the rigth dimension.
732 void MED::updateSupport ()
735 const char * LOC = "MED::updateSupport () : ";
738 map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::iterator itSupportOnMesh ;
739 for ( itSupportOnMesh=_support.begin();itSupportOnMesh != _support.end(); itSupportOnMesh++ ) {
740 map<MED_FR::med_entite_maillage,SUPPORT *>::iterator itSupport ;
741 for ( itSupport=(*itSupportOnMesh).second.begin();itSupport!=(*itSupportOnMesh).second.end();itSupport++)
743 (*itSupport).second->update() ;
745 catch (MEDEXCEPTION & ex) {
746 // entity not defined in mesh -> we remove support on it !
747 MESSAGE(LOC<<ex.what());
748 delete (*itSupport).second ;
749 (*itSupportOnMesh).second.erase(itSupport) ; // that's rigth ????
758 Add the given MESH object. MED object control it,
759 and destroy it, so you must not destroy it after.
761 The meshName is given by the MESH object.
763 void MED::addMesh( MESH * const ptrMesh)
764 throw (MED_EXCEPTION)
766 const char * LOC = "MED::addMesh(const MESH * ptrMesh): ";
770 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrMesh must not be NULL !"));
773 if ( ! ( meshName = ptrMesh->getName()).size() )
774 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrMesh->_name must not be NULL !"));
776 // MESH * meshToMed = new MESH(*ptrMesh); DO WE HAVE TO COPY THE ENTRY MESH OR NOT ????? (NB)
778 _meshes[meshName] = ptrMesh; // if ptrMesh->meshName already exists it is modified
780 // _meshes[meshName] = meshToMed;
786 Add the given FIELD object. MED object control it,
787 and destroy it, so you must not destroy it after.
789 The fieldName is given by the FIELD object.
791 void MED::addField( FIELD_ * const ptrField)
792 throw (MED_EXCEPTION)
794 const char * LOC = "MED::addField(const FIELD_ * const ptrField): ";
798 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField must not be NULL !"));
801 if ( ! (fieldName = ptrField->getName()).size() )
802 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_name must not be NULL !"));
804 SUPPORT * ptrSupport;
805 if ( ! ( ptrSupport = (SUPPORT * ) ptrField->getSupport()) )
806 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_support must not be NULL !"));
809 if ( ! ( ptrMesh = ptrSupport->getMesh()) )
810 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_support->_mesh must not be NULL !"));
813 if ( ! ( meshName = ptrMesh->getName()).size() )
814 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_support->_mesh->_name must not be NULL !"));
817 dtIt.dt = ptrField->getIterationNumber();
818 dtIt.it = ptrField->getOrderNumber();
820 _fields [fieldName][dtIt] = ptrField; // if it already exists it is replaced
821 _meshName [ptrField] = meshName; // if it already exists it is replaced
822 _meshes [meshName] = ptrMesh; // if it already exists it is replaced
824 // int numberOfTypes = ptrSupport->getNumberOfTypes(); !! UNUSED VARIABLE !!
825 _support [meshName][ (MED_FR::med_entite_maillage) ptrSupport->getEntity()] = ptrSupport;// if it already exists it is replaced