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);
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, const string & fileName="Default File Name.med") {
154 const char * LOC = "MED::addDriver(driverTypes driverType, const string & fileName=\"Default File Name.med\") : ";
158 MESSAGE(LOC << " the file name is " << fileName);
162 GENDRIVER *driver = DRIVERFACTORY::buildDriverForMed(driverType,fileName,this);
164 _drivers.push_back(driver);
166 int current = _drivers.size()-1;
168 driver->setId(current);
176 Duplicate the given driver and return its index reference to path to
177 read or write methods.
179 int MED::addDriver(GENDRIVER & driver) {
180 const char * LOC = "MED::addDriver(GENDRIVER &) : ";
185 SCRUTE(_drivers.size());
187 _drivers.push_back(&driver);
189 SCRUTE(_drivers.size());
194 current = _drivers.size()-1;
196 driver.setId(current);
205 Remove the driver referenced by its index.
207 void MED::rmDriver (int index/*=0*/)
208 throw (MED_EXCEPTION)
210 const char * LOC = "MED::rmDriver (int index=0): ";
214 //_drivers.erase(&_drivers[index]);
217 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
218 << "The index given is invalid, index must be between 0 and |"
226 ??? to do comment ???
228 void MED::writeFrom (int index/*=0*/)
229 throw (MED_EXCEPTION)
231 const char * LOC = "MED::write (int index=0): ";
234 if (_drivers[index]) {
235 // open and close are made by all objects !
236 _drivers[index]->writeFrom();
238 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
239 << "The index given is invalid, index must be between 0 and |"
247 Write all objects with the driver given by its index.
249 void MED::write (int index/*=0*/)
250 throw (MED_EXCEPTION)
252 const char * LOC = "MED::write (int index=0): ";
255 if (_drivers[index]) {
256 // open and close are made by the subsequent objects !
257 _drivers[index]->write();
260 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
261 << "The index given is invalid, index must be between 0 and |"
269 Parse all the file and generate empty object.
271 All object must be read explicitly later with their own method read
272 or use MED::read to read all.
274 This method is automaticaly call by constructor with driver information.
276 void MED::readFileStruct (int index/*=0*/)
277 throw (MED_EXCEPTION)
279 const char * LOC = "MED::readFileStruct (int index=0): ";
282 if (_drivers[index]) {
283 _drivers[index]->open();
284 _drivers[index]->readFileStruct();
285 _drivers[index]->close();
288 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
289 << "The index given is invalid, index must be between 0 and |"
296 Read all objects in the file specified in the driver given by its index.
298 void MED::read (int index/*=0*/)
299 throw (MED_EXCEPTION)
301 const char * LOC = "MED::read (int index=0): ";
304 if (_drivers[index]) {
305 // open and close are made by all objects !
307 SCRUTE(_drivers[index]);
308 SCRUTE(&_drivers[index]);
309 // _drivers[index]->open();
310 _drivers[index]->read();
311 // _drivers[index]->close();
314 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
315 << "The index given is invalid, index must be between >0 and < |"
323 // ------- End Of Drivers Management Part
326 Get the number of MESH objects.
328 int MED::getNumberOfMeshes ( void ) const {
330 const char * LOC = "MED::getNumberOfMeshes ( void ) const : ";
333 return _meshes.size();
337 Get the number of FIELD objects.
339 int MED::getNumberOfFields ( void ) const {
341 const char * LOC = "MED::getNumberOfFields ( void ) const : ";
344 return _fields.size(); // we get number of field with different name
348 Get the names of all MESH objects.
350 meshNames is an in/out argument.
352 It is a string array of size the
353 number of MESH objects. It must be allocated before calling
354 this method. All names are put in it.
356 void MED::getMeshNames ( string * meshNames ) const
357 throw (MED_EXCEPTION)
359 const char * LOC = "MED::getMeshNames ( string * ) const : ";
361 unsigned int meshNamesSize;
363 if ( ( meshNamesSize = sizeof(meshNames) / sizeof(string *) )
365 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
366 << "Size of parameter meshNames is |"
367 << meshNamesSize << "| and should be |"
368 << _meshes.size() << "| and should be |"
372 // REM : ALLOCATION D'UN TABLEAU DE POINTEURS SUR STRING FAITE PAR LE CLIENT
373 map<MESH_NAME_,MESH*>::const_iterator currentMesh; // ??ITERATEUR CONST SUR UN OBJET NON CONST ??
375 int meshNamesIndex = 0;
377 for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
378 meshNames[meshNamesIndex]=(*currentMesh).first;
379 meshNamesIndex++; // CF OPTIMISATION
386 Get the names of all MESH objects.
388 Return a deque<string> object which contain the name of all MESH objects.
390 deque<string> MED::getMeshNames () const {
392 const char * LOC = "MED::getMeshNames () const : ";
395 deque<string> meshNames(_meshes.size());
397 map<MESH_NAME_,MESH*>::const_iterator currentMesh; // ??ITERATEUR CONST SUR UN OBJET NON CONST ??
399 int meshNamesIndex = 0;
401 for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
402 meshNames[meshNamesIndex]=(*currentMesh).first;
403 meshNamesIndex++; // CF OPTIMISATION
412 Return a reference to the MESH object named meshName.
414 MESH * MED::getMesh ( const string & meshName ) const
415 throw (MED_EXCEPTION)
418 const char * LOC = "MED::getMesh ( const string & meshName ) const : ";
421 map<MESH_NAME_,MESH*>::const_iterator itMeshes = _meshes.find(meshName);
423 if ( itMeshes == _meshes.end() )
424 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
425 << "There is no known mesh named |"
430 return (*itMeshes).second;
436 \internal Return a reference to the MESH object associated with
439 MESH * MED::getMesh (const FIELD_ * const field ) const
440 throw (MED_EXCEPTION)
443 const char * LOC = "MED::getMesh ( const FIELD * field ) const : ";
446 FIELD_ * f = const_cast< FIELD_* > (field); // Comment faire mieux ?
447 map<FIELD_ *, MESH_NAME_>::const_iterator itMeshName = _meshName.find(f);
449 if ( itMeshName == _meshName.end() )
450 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
451 << "There is no known mesh associated with |"
452 << field << "| pointer"
456 string meshName = (*itMeshName).second;
457 map<MESH_NAME_,MESH*>::const_iterator itMeshes = _meshes.find(meshName);
458 if ( itMeshes == _meshes.end() )
459 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
460 << "There is no known mesh named |"
461 << meshName << " while it's associated with the found field |"
462 << field << "| pointer"
466 return (*itMeshes).second;
473 Get the names of all FIELD objects.
475 fieldNames is an in/out argument.
477 It is an array of string of size the
478 number of FIELD objects. It must be allocated before calling
479 this method. All names are put in it.
481 void MED::getFieldNames ( string * fieldNames ) const
482 throw (MED_EXCEPTION)
484 const char * LOC = "MED::getFieldNames ( string * ) const : ";
487 unsigned int fieldNamesSize = sizeof(fieldNames) / sizeof(string *);
489 if ( fieldNamesSize != _fields.size() )
490 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
491 << "Size of parameter fieldNames is |"
492 << fieldNamesSize << "| and should be |"
493 << _fields.size() << "| and should be |"
497 // REM : ALLOCATION D'UN TABLEAU DE POINTEURS SUR STRING FAITE PAR LE CLIENT
498 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator currentField;
500 int fieldNamesIndex = 0;
501 for ( currentField=_fields.begin();currentField != _fields.end(); currentField++ ) {
502 fieldNames[fieldNamesIndex]=(*currentField).first;
503 fieldNamesIndex++; // CF OPTIMISATION
511 Get the names of all FIELD objects.
513 Return a deque<string> object which contain the name of all FIELD objects.
515 deque<string> MED::getFieldNames () const {
517 const char * LOC = "MED::getFieldNames ( ) const : ";
520 deque<string> fieldNames(_fields.size());
522 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator currentField;
524 int fieldNamesIndex = 0;
526 for ( currentField=_fields.begin();currentField != _fields.end(); currentField++ ) {
527 fieldNames[fieldNamesIndex]=(*currentField).first;
528 fieldNamesIndex++; // CF OPTIMISATION
536 Return a deque<DT_IT_> which contain all iteration step for the FIELD
537 identified by its name.
539 deque<DT_IT_> MED::getFieldIteration (const string & fieldName) const
540 throw (MED_EXCEPTION)
543 const char * LOC = "MED::getFieldIteration ( const string & ) const : ";
546 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator itFields = _fields.find(fieldName);
548 if ( itFields == _fields.end() )
549 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
550 << "There is no known field named |"
554 // const MAP_DT_IT_ & myIterationMap = const_cast<const MAP_DT_IT_ & > ((*itFields).second);
555 const MAP_DT_IT_ & myIterationMap = (*itFields).second ;
556 MAP_DT_IT_::const_iterator currentIterator ;
558 int iterationSize = myIterationMap.size();
560 SCRUTE(iterationSize);
562 deque<DT_IT_> Iteration(iterationSize);
564 int iterationIndex = 0;
566 for (currentIterator = myIterationMap.begin();
567 currentIterator != myIterationMap.end(); currentIterator++ )
569 SCRUTE(((*currentIterator).first).dt);
570 SCRUTE(((*currentIterator).first).it);
572 Iteration[iterationIndex].dt = ((*currentIterator).first).dt;
573 Iteration[iterationIndex].it = ((*currentIterator).first).it;
575 // Iteration[iterationIndex]=(*currentIterator).first;
576 SCRUTE(Iteration[iterationIndex].dt);
577 SCRUTE(Iteration[iterationIndex].it);
578 iterationIndex++; // CF OPTIMISATION
586 Return a reference to the FIELD object named fieldName with
587 time step number dt and order number it.
589 FIELD_ * MED::getField ( const string & fieldName, const int dt=MED_NOPDT, const int it=MED_NOPDT ) const
590 throw (MED_EXCEPTION)
593 const char * LOC = "MED::getField ( const string &, const int, const int ) const : ";
596 MESSAGE(LOC << "fieldName = "<<fieldName<<", dt ="<<dt<<", it = "<<it);
603 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator itFields = _fields.find(fieldName);
605 if ( itFields == _fields.end() )
606 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
607 << "There is no known field named |"
612 const MAP_DT_IT_ & map_dtIt = const_cast<const MAP_DT_IT_ & > ((*itFields).second);
613 MAP_DT_IT_::const_iterator itMap_dtIt = map_dtIt.find(dtIt);
615 if ( itMap_dtIt == map_dtIt.end() )
616 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
617 << "There is no (dt,it) |("
618 << dt << "," << it << ")| associated with the found field |"
625 //return _fields[fieldName][dtIt];
626 return (*itMap_dtIt).second;
631 // fiend ostream & MED::operator<<(ostream &os,const MED & med) const {
636 Return a map<MED_EN::medEntityMesh,SUPPORT*> which contain
637 foreach entity, a reference to the SUPPORT on all elements.
639 const map<MED_EN::medEntityMesh,SUPPORT*> & MED::getSupports(const string & meshName) const
640 throw (MED_EXCEPTION)
642 const char * LOC = "MED::getSupports ( const string ) const : ";
645 map<MESH_NAME_, map<MED_EN::medEntityMesh,SUPPORT *> >::const_iterator itSupportOnMesh = _support.find(meshName) ;
647 if ( itSupportOnMesh == _support.end() )
648 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
649 << "There is no support on mesh named |"
654 return (*itSupportOnMesh).second ;
658 Return a reference to the SUPPORT object on all elements of entity
659 for the MESH named meshName.
661 SUPPORT * MED::getSupport (const string & meshName,MED_EN::medEntityMesh entity) const
662 throw (MED_EXCEPTION)
664 const char * LOC = "MED::getSupport ( const string, MED_EN::medEntityMesh ) const : ";
668 for (map<MESH_NAME_, map<MED_EN::medEntityMesh,SUPPORT *> >::const_iterator const_itSupportOnMesh=_support.begin(); const_itSupportOnMesh != _support.end();
669 const_itSupportOnMesh++ )
671 map<MED_EN::medEntityMesh,SUPPORT *>::const_iterator const_itSupport ;
672 for (const_itSupport=(*const_itSupportOnMesh).second.begin();
673 const_itSupport!=(*const_itSupportOnMesh).second.end();const_itSupport++) index++;
676 MESSAGE(LOC << "In this MED object there is(are) " << index << " support(s):");
678 for (map<MESH_NAME_, map<MED_EN::medEntityMesh,SUPPORT *> >::const_iterator const_itSupportOnMesh=_support.begin();const_itSupportOnMesh != _support.end(); const_itSupportOnMesh++ )
680 map<MED_EN::medEntityMesh,SUPPORT *>::const_iterator const_itSupport ;
681 for (const_itSupport=(*const_itSupportOnMesh).second.begin();
682 const_itSupport!=(*const_itSupportOnMesh).second.end();const_itSupport++)
684 MESSAGE(LOC << "Support on mesh " << (*const_itSupportOnMesh).first << " on entity " << (*const_itSupport).first << " : " << *((*const_itSupport).second));
689 map<MESH_NAME_, map<MED_EN::medEntityMesh,SUPPORT *> >::const_iterator const_itSupportOnMesh = _support.find(meshName) ;
691 if ( const_itSupportOnMesh == _support.end() )
692 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
693 << "There is no support on mesh named |"
698 // map<MED_EN::medEntityMesh,SUPPORT *> & SupportOnMesh = (map<MED_EN::medEntityMesh,SUPPORT *>&) ((*itSupportOnMesh).second) ;
699 // map<MED_EN::medEntityMesh,SUPPORT *>::const_iterator itSupport = SupportOnMesh.find(entity) ;
701 // if (itSupport == SupportOnMesh.end() )
702 // throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
703 // << "There is no support on entity "
704 // << entity << " in mesh named |"
705 // << meshName << "|"
710 map<MED_EN::medEntityMesh,SUPPORT *> SupportOnMesh = ((*const_itSupportOnMesh).second);
712 map<MED_EN::medEntityMesh,SUPPORT *>::const_iterator itSupport = SupportOnMesh.find(entity) ;
714 if (itSupport == SupportOnMesh.end() )
715 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
716 << "There is no support on entity "
717 << entity << " in mesh named |"
722 return (*itSupport).second ;
726 Temporary method : when all MESH objects are read, this methods
727 update all SUPPORT objects with the rigth dimension.
729 void MED::updateSupport ()
732 const char * LOC = "MED::updateSupport () : ";
735 map<MESH_NAME_, map<MED_EN::medEntityMesh,SUPPORT *> >::iterator itSupportOnMesh ;
736 for ( itSupportOnMesh=_support.begin();itSupportOnMesh != _support.end(); itSupportOnMesh++ ) {
737 map<MED_EN::medEntityMesh,SUPPORT *>::iterator itSupport ;
738 for ( itSupport=(*itSupportOnMesh).second.begin();itSupport!=(*itSupportOnMesh).second.end();itSupport++)
740 (*itSupport).second->update() ;
742 catch (MEDEXCEPTION & ex) {
743 // entity not defined in mesh -> we remove support on it !
744 MESSAGE(LOC<<ex.what());
745 delete (*itSupport).second ;
746 (*itSupportOnMesh).second.erase(itSupport) ; // that's rigth ????
755 Add the given MESH object. MED object control it,
756 and destroy it, so you must not destroy it after.
758 The meshName is given by the MESH object.
760 void MED::addMesh( MESH * const ptrMesh)
761 throw (MED_EXCEPTION)
763 const char * LOC = "MED::addMesh(const MESH * ptrMesh): ";
767 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrMesh must not be NULL !"));
770 if ( ! ( meshName = ptrMesh->getName()).size() )
771 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrMesh->_name must not be NULL !"));
773 // MESH * meshToMed = new MESH(*ptrMesh); DO WE HAVE TO COPY THE ENTRY MESH OR NOT ????? (NB)
775 _meshes[meshName] = ptrMesh; // if ptrMesh->meshName already exists it is modified
777 // _meshes[meshName] = meshToMed;
783 Add the given FIELD object. MED object control it,
784 and destroy it, so you must not destroy it after.
786 The fieldName is given by the FIELD object.
788 void MED::addField( FIELD_ * const ptrField)
789 throw (MED_EXCEPTION)
791 const char * LOC = "MED::addField(const FIELD_ * const ptrField): ";
795 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField must not be NULL !"));
798 if ( ! (fieldName = ptrField->getName()).size() )
799 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_name must not be NULL !"));
801 SUPPORT * ptrSupport;
802 if ( ! ( ptrSupport = (SUPPORT * ) ptrField->getSupport()) )
803 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_support must not be NULL !"));
806 if ( ! ( ptrMesh = ptrSupport->getMesh()) )
807 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_support->_mesh must not be NULL !"));
810 if ( ! ( meshName = ptrMesh->getName()).size() )
811 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_support->_mesh->_name must not be NULL !"));
814 dtIt.dt = ptrField->getIterationNumber();
815 dtIt.it = ptrField->getOrderNumber();
817 _fields [fieldName][dtIt] = ptrField; // if it already exists it is replaced
818 _meshName [ptrField] = meshName; // if it already exists it is replaced
819 _meshes [meshName] = ptrMesh; // if it already exists it is replaced
821 // int numberOfTypes = ptrSupport->getNumberOfTypes(); !! UNUSED VARIABLE !!
822 _support [meshName][ptrSupport->getEntity()] = ptrSupport;// if it already exists it is replaced