4 # include "MEDMEM_Med.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 MED_MED_RDONLY_DRIVER * myDriver = new MED_MED_RDONLY_DRIVER(fileName,this) ;
34 int current = addDriver(*myDriver);
35 //int current= addDriver(driverType,fileName);
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_FR::med_entite_maillage,SUPPORT *> >::iterator itSupportOnMesh ;
69 for ( itSupportOnMesh=_support.begin();itSupportOnMesh != _support.end(); itSupportOnMesh++ ) {
70 map<MED_FR::med_entite_maillage,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_FR::med_entite_maillage,SUPPORT *> >::iterator itSupportOnMesh ;
115 for ( itSupportOnMesh=_support.begin();itSupportOnMesh != _support.end(); itSupportOnMesh++ ) {
116 map<MED_FR::med_entite_maillage,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];
147 // ------- Drivers Management Part
149 // Add your new driver instance declaration here (step 3-1)
150 MED::INSTANCE_DE<MED_MED_RDWR_DRIVER> MED::inst_med ;
151 MED::INSTANCE_DE<VTK_MED_DRIVER> MED::inst_vtk ;
153 // Add your new driver instance in the MED instance list (step 3-2)
154 const MED::INSTANCE * const MED::instances[] = { &MED::inst_med, &MED::inst_vtk };
157 Create the specified driver and return its index reference to path to
158 read or write methods.
160 int MED::addDriver(driverTypes driverType, const string & fileName="Default File Name.med") {
162 const char * LOC = "MED::addDriver(driverTypes driverType, const string & fileName=\"Default File Name.med\") : ";
165 int itDriver = (int) NO_DRIVER;
169 MESSAGE(LOC << " the file name is " << fileName);
173 SCRUTE(instances[driverType]);
178 itDriver = (int) driverType ;
188 throw MED_EXCEPTION (LOCALIZED(STRING(LOC)<< "GIBI_DRIVER has been specified to the method which is not allowed because there is no GIBI driver for the MED object"));
193 throw MED_EXCEPTION (LOCALIZED(STRING(LOC)<< "NO_DRIVER has been specified to the method which is not allowed"));
198 if (itDriver == ((int) NO_DRIVER))
199 throw MED_EXCEPTION (LOCALIZED(STRING(LOC)<< "NO_DRIVER has been specified to the method which is not allowed"));
201 driver = instances[itDriver]->run(fileName, this) ;
203 current = _drivers.size()-1;
205 driver->setId(current);
213 Duplicate the given driver and return its index reference to path to
214 read or write methods.
216 int MED::addDriver(GENDRIVER & driver) {
217 const char * LOC = "MED::addDriver(GENDRIVER &) : ";
222 SCRUTE(_drivers.size());
224 _drivers.push_back(&driver);
226 SCRUTE(_drivers.size());
231 current = _drivers.size()-1;
232 driver.setId(current);
240 Remove the driver referenced by its index.
242 void MED::rmDriver (int index/*=0*/)
243 throw (MED_EXCEPTION)
245 const char * LOC = "MED::rmDriver (int index=0): ";
249 //_drivers.erase(&_drivers[index]);
252 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
253 << "The index given is invalid, index must be between 0 and |"
261 ??? to do comment ???
263 void MED::writeFrom (int index/*=0*/)
264 throw (MED_EXCEPTION)
266 const char * LOC = "MED::write (int index=0): ";
269 if (_drivers[index]) {
270 // open and close are made by all objects !
271 _drivers[index]->writeFrom();
273 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
274 << "The index given is invalid, index must be between 0 and |"
282 Write all objects with the driver given by its index.
284 void MED::write (int index/*=0*/)
285 throw (MED_EXCEPTION)
287 const char * LOC = "MED::write (int index=0): ";
290 if (_drivers[index]) {
291 // open and close are made by the subsequent objects !
292 _drivers[index]->write();
295 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
296 << "The index given is invalid, index must be between 0 and |"
304 Parse all the file and generate empty object.
306 All object must be read explicitly later with their own method read
307 or use MED::read to read all.
309 This method is automaticaly call by constructor with driver information.
311 void MED::readFileStruct (int index/*=0*/)
312 throw (MED_EXCEPTION)
314 const char * LOC = "MED::readFileStruct (int index=0): ";
317 if (_drivers[index]) {
318 _drivers[index]->open();
319 _drivers[index]->readFileStruct();
320 _drivers[index]->close();
323 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
324 << "The index given is invalid, index must be between 0 and |"
332 Read all objects in the file specified in the driver given by its index.
334 void MED::read (int index/*=0*/)
335 throw (MED_EXCEPTION)
337 const char * LOC = "MED::read (int index=0): ";
340 if (_drivers[index]) {
341 // open and close are made by all objects !
343 SCRUTE(_drivers[index]);
344 SCRUTE(&_drivers[index]);
345 // _drivers[index]->open();
346 _drivers[index]->read();
347 // _drivers[index]->close();
350 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
351 << "The index given is invalid, index must be between >0 and < |"
359 // ------- End Of Drivers Management Part
362 Get the number of MESH objects.
364 int MED::getNumberOfMeshes ( void ) const {
366 const char * LOC = "MED::getNumberOfMeshes ( void ) const : ";
369 int size = _meshes.size();
379 Get the number of FIELD objects.
381 int MED::getNumberOfFields ( void ) const {
383 const char * LOC = "MED::getNumberOfFields ( void ) const : ";
386 return _fields.size(); // we get number of field with different name
392 Get the names of all MESH objects.
394 meshNames is an in/out argument.
396 It is a string array of size the
397 number of MESH objects. It must be allocated before calling
398 this method. All names are put in it.
400 void MED::getMeshNames ( string * meshNames ) const
401 throw (MED_EXCEPTION)
403 const char * LOC = "MED::getMeshNames ( string * ) const : ";
405 unsigned int meshNamesSize;
407 if ( ( meshNamesSize = sizeof(meshNames) / sizeof(string *) )
409 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
410 << "Size of parameter meshNames is |"
411 << meshNamesSize << "| and should be |"
412 << _meshes.size() << "| and should be |"
416 // REM : ALLOCATION D'UN TABLEAU DE POINTEURS SUR STRING FAITE PAR LE CLIENT
417 map<MESH_NAME_,MESH*>::const_iterator currentMesh; // ??ITERATEUR CONST SUR UN OBJET NON CONST ??
419 int meshNamesIndex = 0;
421 for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
422 meshNames[meshNamesIndex]=(*currentMesh).first;
423 meshNamesIndex++; // CF OPTIMISATION
430 Get the names of all MESH objects.
432 Return a deque<string> object which contain the name of all MESH objects.
434 deque<string> MED::getMeshNames () const {
436 const char * LOC = "MED::getMeshNames () const : ";
439 deque<string> meshNames(_meshes.size());
441 map<MESH_NAME_,MESH*>::const_iterator currentMesh; // ??ITERATEUR CONST SUR UN OBJET NON CONST ??
443 int meshNamesIndex = 0;
445 for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
446 meshNames[meshNamesIndex]=(*currentMesh).first;
447 meshNamesIndex++; // CF OPTIMISATION
456 Return a reference to the MESH object named meshName.
458 MESH * MED::getMesh ( const string & meshName ) const
459 throw (MED_EXCEPTION)
461 const char * LOC = "MED::getMesh ( const string & meshName ) const : ";
466 map<MESH_NAME_,MESH*>::const_iterator itMeshes = _meshes.find(meshName);
468 if ( itMeshes == _meshes.end() )
469 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
470 << "There is no known mesh named |"
475 return (*itMeshes).second;
481 \internal Return a reference to the MESH object associated with
484 MESH * MED::getMesh (const FIELD_ * const field ) const
485 throw (MED_EXCEPTION)
488 const char * LOC = "MED::getMesh ( const FIELD * field ) const : ";
491 FIELD_ * f = const_cast< FIELD_* > (field); // Comment faire mieux ?
492 map<FIELD_ *, MESH_NAME_>::const_iterator itMeshName = _meshName.find(f);
494 if ( itMeshName == _meshName.end() )
495 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
496 << "There is no known mesh associated with |"
497 << field << "| pointer"
501 string meshName = (*itMeshName).second;
502 map<MESH_NAME_,MESH*>::const_iterator itMeshes = _meshes.find(meshName);
503 if ( itMeshes == _meshes.end() )
504 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
505 << "There is no known mesh named |"
506 << meshName << " while it's associated with the found field |"
507 << field << "| pointer"
511 return (*itMeshes).second;
518 Get the names of all FIELD objects.
520 fieldNames is an in/out argument.
522 It is an array of string of size the
523 number of FIELD objects. It must be allocated before calling
524 this method. All names are put in it.
526 void MED::getFieldNames ( string * fieldNames ) const
527 throw (MED_EXCEPTION)
529 const char * LOC = "MED::getFieldNames ( string * ) const : ";
532 unsigned int fieldNamesSize = sizeof(fieldNames) / sizeof(string *);
534 if ( fieldNamesSize != _fields.size() )
535 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
536 << "Size of parameter fieldNames is |"
537 << fieldNamesSize << "| and should be |"
538 << _fields.size() << "| and should be |"
542 // REM : ALLOCATION D'UN TABLEAU DE POINTEURS SUR STRING FAITE PAR LE CLIENT
543 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator currentField;
545 int fieldNamesIndex = 0;
546 for ( currentField=_fields.begin();currentField != _fields.end(); currentField++ ) {
547 fieldNames[fieldNamesIndex]=(*currentField).first;
548 fieldNamesIndex++; // CF OPTIMISATION
556 Get the names of all FIELD objects.
558 Return a deque<string> object which contain the name of all FIELD objects.
560 deque<string> MED::getFieldNames () const {
562 const char * LOC = "MED::getFieldNames ( ) const : ";
565 deque<string> fieldNames(_fields.size());
567 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator currentField;
569 int fieldNamesIndex = 0;
571 for ( currentField=_fields.begin();currentField != _fields.end(); currentField++ ) {
572 fieldNames[fieldNamesIndex]=(*currentField).first;
573 fieldNamesIndex++; // CF OPTIMISATION
581 Return a deque<DT_IT_> which contain all iteration step for the FIELD
582 identified by its name.
584 deque<DT_IT_> MED::getFieldIteration (const string & fieldName) const
585 throw (MED_EXCEPTION)
588 const char * LOC = "MED::getFieldIteration ( const string & ) const : ";
591 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator itFields = _fields.find(fieldName);
593 if ( itFields == _fields.end() )
594 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
595 << "There is no known field named |"
599 // const MAP_DT_IT_ & myIterationMap = const_cast<const MAP_DT_IT_ & > ((*itFields).second);
600 const MAP_DT_IT_ & myIterationMap = (*itFields).second ;
601 MAP_DT_IT_::const_iterator currentIterator ;
603 deque<DT_IT_> Iteration(myIterationMap.size());
605 int iterationIndex = 0;
607 for ( currentIterator=myIterationMap.begin();currentIterator != myIterationMap.end(); currentIterator++ ) {
608 Iteration[iterationIndex]=(*currentIterator).first;
609 iterationIndex++; // CF OPTIMISATION
617 Return a reference to the FIELD object named fieldName with
618 time step number dt and order number it.
620 FIELD_ * MED::getField ( const string & fieldName, const int dt=MED_NOPDT, const int it=MED_NOPDT ) const
621 throw (MED_EXCEPTION)
624 const char * LOC = "MED::getField ( const string &, const int, const int ) const : ";
627 MESSAGE(LOC << "fieldName = "<<fieldName<<", dt ="<<dt<<", it = "<<it);
634 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator itFields = _fields.find(fieldName);
636 if ( itFields == _fields.end() )
637 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
638 << "There is no known field named |"
643 const MAP_DT_IT_ & map_dtIt = const_cast<const MAP_DT_IT_ & > ((*itFields).second);
644 MAP_DT_IT_::const_iterator itMap_dtIt = map_dtIt.find(dtIt);
646 if ( itMap_dtIt == map_dtIt.end() )
647 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
648 << "There is no (dt,it) |("
649 << dt << "," << it << ")| associated with the found field |"
656 //return _fields[fieldName][dtIt];
657 return (*itMap_dtIt).second;
662 // fiend ostream & MED::operator<<(ostream &os,const MED & med) const {
667 Return a map<MED_FR::med_entite_maillage,SUPPORT*> which contain
668 foreach entity, a reference to the SUPPORT on all elements.
670 const map<MED_FR::med_entite_maillage,SUPPORT*> & MED::getSupports(const string & meshName) const
671 throw (MED_EXCEPTION)
673 const char * LOC = "MED::getSupports ( const string ) const : ";
676 map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::const_iterator itSupportOnMesh = _support.find(meshName) ;
678 if ( itSupportOnMesh == _support.end() )
679 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
680 << "There is no support on mesh named |"
685 return (*itSupportOnMesh).second ;
689 Return a reference to the SUPPORT object on all elements of entity
690 for the MESH named meshName.
692 SUPPORT * MED::getSupport (const string & meshName,MED_FR::med_entite_maillage entity) const
693 throw (MED_EXCEPTION)
695 const char * LOC = "MED::getSupport ( const string, MED_FR::med_entite_maillage ) const : ";
699 for (map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::const_iterator const_itSupportOnMesh=_support.begin(); const_itSupportOnMesh != _support.end();
700 const_itSupportOnMesh++ )
702 map<MED_FR::med_entite_maillage,SUPPORT *>::const_iterator const_itSupport ;
703 for (const_itSupport=(*const_itSupportOnMesh).second.begin();
704 const_itSupport!=(*const_itSupportOnMesh).second.end();const_itSupport++) index++;
707 MESSAGE(LOC << "In this MED object there is(are) " << index << " support(s):");
709 for (map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::const_iterator const_itSupportOnMesh=_support.begin();const_itSupportOnMesh != _support.end(); const_itSupportOnMesh++ )
711 map<MED_FR::med_entite_maillage,SUPPORT *>::const_iterator const_itSupport ;
712 for (const_itSupport=(*const_itSupportOnMesh).second.begin();
713 const_itSupport!=(*const_itSupportOnMesh).second.end();const_itSupport++)
715 MESSAGE(LOC << "Support on mesh " << (*const_itSupportOnMesh).first << " on entity " << (*const_itSupport).first << " : " << *((*const_itSupport).second));
720 map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::const_iterator const_itSupportOnMesh = _support.find(meshName) ;
722 if ( const_itSupportOnMesh == _support.end() )
723 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
724 << "There is no support on mesh named |"
729 // map<MED_FR::med_entite_maillage,SUPPORT *> & SupportOnMesh = (map<MED_FR::med_entite_maillage,SUPPORT *>&) ((*itSupportOnMesh).second) ;
730 // map<MED_FR::med_entite_maillage,SUPPORT *>::const_iterator itSupport = SupportOnMesh.find(entity) ;
732 // if (itSupport == SupportOnMesh.end() )
733 // throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
734 // << "There is no support on entity "
735 // << entity << " in mesh named |"
736 // << meshName << "|"
741 map<MED_FR::med_entite_maillage,SUPPORT *> SupportOnMesh = ((*const_itSupportOnMesh).second);
743 map<MED_FR::med_entite_maillage,SUPPORT *>::const_iterator itSupport = SupportOnMesh.find(entity) ;
745 if (itSupport == SupportOnMesh.end() )
746 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
747 << "There is no support on entity "
748 << entity << " in mesh named |"
753 return (*itSupport).second ;
757 Temporary method : when all MESH objects are read, this methods
758 update all SUPPORT objects with the rigth dimension.
760 void MED::updateSupport ()
763 const char * LOC = "MED::updateSupport () : ";
766 map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::iterator itSupportOnMesh ;
767 for ( itSupportOnMesh=_support.begin();itSupportOnMesh != _support.end(); itSupportOnMesh++ ) {
768 map<MED_FR::med_entite_maillage,SUPPORT *>::iterator itSupport ;
769 for ( itSupport=(*itSupportOnMesh).second.begin();itSupport!=(*itSupportOnMesh).second.end();itSupport++)
771 (*itSupport).second->update() ;
773 catch (MEDEXCEPTION & ex) {
774 // entity not defined in mesh -> we remove support on it !
775 MESSAGE(LOC<<ex.what());
776 delete (*itSupport).second ;
777 (*itSupportOnMesh).second.erase(itSupport) ; // that's rigth ????
786 Add the given MESH object. MED object control it,
787 and destroy it, so you must not destroy it after.
789 The meshName is given by the MESH object.
791 void MED::addMesh( MESH * const ptrMesh)
792 throw (MED_EXCEPTION)
794 const char * LOC = "MED::addMesh(const MESH * ptrMesh): ";
798 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrMesh must not be NULL !"));
801 if ( ! ( meshName = ptrMesh->getName()).size() )
802 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrMesh->_name must not be NULL !"));
804 // MESH * meshToMed = new MESH(*ptrMesh); DO WE HAVE TO COPY THE ENTRY MESH OR NOT ????? (NB)
806 _meshes[meshName] = ptrMesh; // if ptrMesh->meshName already exists it is modified
808 // _meshes[meshName] = meshToMed;
814 Add the given FIELD object. MED object control it,
815 and destroy it, so you must not destroy it after.
817 The fieldName is given by the FIELD object.
819 void MED::addField( FIELD_ * const ptrField)
820 throw (MED_EXCEPTION)
822 const char * LOC = "MED::addField(const FIELD_ * const ptrField): ";
826 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField must not be NULL !"));
829 if ( ! (fieldName = ptrField->getName()).size() )
830 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_name must not be NULL !"));
832 SUPPORT * ptrSupport;
833 if ( ! ( ptrSupport = (SUPPORT * ) ptrField->getSupport()) )
834 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_support must not be NULL !"));
837 if ( ! ( ptrMesh = ptrSupport->getMesh()) )
838 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_support->_mesh must not be NULL !"));
841 if ( ! ( meshName = ptrMesh->getName()).size() )
842 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_support->_mesh->_name must not be NULL !"));
845 dtIt.dt = ptrField->getIterationNumber();
846 dtIt.it = ptrField->getOrderNumber();
848 _fields [fieldName][dtIt] = ptrField; // if it already exists it is replaced
849 _meshName [ptrField] = meshName; // if it already exists it is replaced
850 _meshes [meshName] = ptrMesh; // if it already exists it is replaced
852 // int numberOfTypes = ptrSupport->getNumberOfTypes(); !! UNUSED VARIABLE !!
853 _support [meshName][ (MED_FR::med_entite_maillage) ptrSupport->getEntity()] = ptrSupport;// if it already exists it is replaced