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"
19 MESSAGE("MED::MED()");
25 MED::MED(driverTypes driverType, const string & fileName)
27 const char * LOC = "MED::MED(driverTypes driverType, const string & fileName) : ";
30 MESSAGE(LOC << "driverType = " << driverType);
32 MED_MED_RDONLY_DRIVER * myDriver = new MED_MED_RDONLY_DRIVER(fileName,this) ;
33 int current = addDriver(*myDriver);
34 //int current= addDriver(driverType,fileName);
36 _drivers[current]->open();
37 _drivers[current]->readFileStruct();
38 _drivers[current]->close();
48 const char * LOC = "MED::~MED() : ";
51 // Analysis of the object MED
55 map<FIELD_ *, MESH_NAME_>::const_iterator currentField;
57 for ( currentField=_meshName.begin();currentField != _meshName.end(); currentField++ ) {
58 if ( (*currentField).first != NULL) index++;
61 MESSAGE(LOC << " there is(are) " << index << " field(s):");
62 for ( currentField=_meshName.begin();currentField != _meshName.end(); currentField++ ) {
63 if ( (*currentField).first != NULL) MESSAGE(" " << ((*currentField).first)->getName().c_str());
66 map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::iterator itSupportOnMesh ;
68 for ( itSupportOnMesh=_support.begin();itSupportOnMesh != _support.end(); itSupportOnMesh++ ) {
69 map<MED_FR::med_entite_maillage,SUPPORT *>::iterator itSupport ;
70 for ( itSupport=(*itSupportOnMesh).second.begin();itSupport!=(*itSupportOnMesh).second.end();itSupport++)
74 MESSAGE(LOC << " there is(are) " << index << " support(s):");
76 map<MESH_NAME_,MESH*>::const_iterator currentMesh;
78 for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
79 if ( (*currentMesh).second != NULL)
83 MESSAGE(LOC << " there is(are) " << index << " meshe(s):");
84 // for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
85 // if ( (*currentMesh).second != NULL)
87 // SCRUTE((*currentMesh).second);
89 // string meshName = ((*currentMesh).second)->getName();
91 // MESSAGE(" " << meshName);
95 // delete all ? : PG : YES !
96 // map<FIELD_ *, MESH_NAME_>::const_iterator currentField;
97 for ( currentField=_meshName.begin();currentField != _meshName.end(); currentField++ ) {
98 if ( (*currentField).first != NULL) {
99 // cast in right type to delete it !
100 switch ((*currentField).first->getValueType()) {
102 delete (FIELD<int>*) (*currentField).first ;
105 delete (FIELD<double>*) (*currentField).first ;
108 MESSAGE(LOC << "Field has type different of int or double, could not destroy its values array !") ;
109 delete (*currentField).first;
113 // map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::iterator itSupportOnMesh ;
114 for ( itSupportOnMesh=_support.begin();itSupportOnMesh != _support.end(); itSupportOnMesh++ ) {
115 map<MED_FR::med_entite_maillage,SUPPORT *>::iterator itSupport ;
116 for ( itSupport=(*itSupportOnMesh).second.begin();itSupport!=(*itSupportOnMesh).second.end();itSupport++)
117 delete (*itSupport).second ;
120 // map<MESH_NAME_,MESH*>::const_iterator currentMesh;
121 for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
122 if ( (*currentMesh).second != NULL)
124 if (!((*currentMesh).second)->getIsAGrid())
125 delete (*currentMesh).second;
127 delete (GRID *) (*currentMesh).second;
131 index =_drivers.size();
133 MESSAGE(LOC << "In this object MED there is(are) " << index << " driver(s):");
135 for (unsigned int ind=0; ind < _drivers.size(); ind++ )
137 SCRUTE(_drivers[ind]);
138 if ( _drivers[ind] != NULL) delete _drivers[ind];
146 // ------- Drivers Management Part
148 // Add your new driver instance declaration here (step 3-1)
149 MED::INSTANCE_DE<MED_MED_RDWR_DRIVER> MED::inst_med ;
150 MED::INSTANCE_DE<VTK_MED_DRIVER> MED::inst_vtk ;
152 // Add your new driver instance in the MED instance list (step 3-2)
153 const MED::INSTANCE * const MED::instances[] = { &MED::inst_med, &MED::inst_vtk };
156 Create the specified driver and return its index reference to path to
157 read or write methods.
159 int MED::addDriver(driverTypes driverType, const string & fileName="Default File Name.med") {
161 const char * LOC = "MED::addDriver(driverTypes driverType, const string & fileName=\"Default File Name.med\") : ";
164 int itDriver = (int) NO_DRIVER;
168 MESSAGE(LOC << " the file name is " << fileName);
172 SCRUTE(instances[driverType]);
177 itDriver = (int) driverType ;
187 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"));
192 throw MED_EXCEPTION (LOCALIZED(STRING(LOC)<< "NO_DRIVER has been specified to the method which is not allowed"));
197 if (itDriver == ((int) NO_DRIVER))
198 throw MED_EXCEPTION (LOCALIZED(STRING(LOC)<< "NO_DRIVER has been specified to the method which is not allowed"));
200 driver = instances[itDriver]->run(fileName, this) ;
202 current = _drivers.size()-1;
204 driver->setId(current);
212 Duplicate the given driver and return its index reference to path to
213 read or write methods.
215 int MED::addDriver(GENDRIVER & driver) {
216 const char * LOC = "MED::addDriver(GENDRIVER &) : ";
221 SCRUTE(_drivers.size());
223 _drivers.push_back(&driver);
225 SCRUTE(_drivers.size());
230 current = _drivers.size()-1;
231 driver.setId(current);
239 Remove the driver referenced by its index.
241 void MED::rmDriver (int index/*=0*/)
242 throw (MED_EXCEPTION)
244 const char * LOC = "MED::rmDriver (int index=0): ";
248 //_drivers.erase(&_drivers[index]);
251 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
252 << "The index given is invalid, index must be between 0 and |"
260 ??? to do comment ???
262 void MED::writeFrom (int index/*=0*/)
263 throw (MED_EXCEPTION)
265 const char * LOC = "MED::write (int index=0): ";
268 if (_drivers[index]) {
269 // open and close are made by all objects !
270 _drivers[index]->writeFrom();
272 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
273 << "The index given is invalid, index must be between 0 and |"
281 Write all objects with the driver given by its index.
283 void MED::write (int index/*=0*/)
284 throw (MED_EXCEPTION)
286 const char * LOC = "MED::write (int index=0): ";
289 if (_drivers[index]) {
290 // open and close are made by the subsequent objects !
291 _drivers[index]->write();
294 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
295 << "The index given is invalid, index must be between 0 and |"
303 Parse all the file and generate empty object.
305 All object must be read explicitly later with their own method read
306 or use MED::read to read all.
308 This method is automaticaly call by constructor with driver information.
310 void MED::readFileStruct (int index/*=0*/)
311 throw (MED_EXCEPTION)
313 const char * LOC = "MED::readFileStruct (int index=0): ";
316 if (_drivers[index]) {
317 _drivers[index]->open();
318 _drivers[index]->readFileStruct();
319 _drivers[index]->close();
322 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
323 << "The index given is invalid, index must be between 0 and |"
331 Read all objects in the file specified in the driver given by its index.
333 void MED::read (int index/*=0*/)
334 throw (MED_EXCEPTION)
336 const char * LOC = "MED::read (int index=0): ";
339 if (_drivers[index]) {
340 // open and close are made by all objects !
342 SCRUTE(_drivers[index]);
343 SCRUTE(&_drivers[index]);
344 // _drivers[index]->open();
345 _drivers[index]->read();
346 // _drivers[index]->close();
349 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
350 << "The index given is invalid, index must be between >0 and < |"
358 // ------- End Of Drivers Management Part
361 Get the number of MESH objects.
363 int MED::getNumberOfMeshes ( void ) const {
365 const char * LOC = "MED::getNumberOfMeshes ( void ) const : ";
368 return _meshes.size();
374 Get the number of FIELD objects.
376 int MED::getNumberOfFields ( void ) const {
378 const char * LOC = "MED::getNumberOfFields ( void ) const : ";
381 return _fields.size(); // we get number of field with different name
387 Get the names of all MESH objects.
389 meshNames is an in/out argument.
391 It is a string array of size the
392 number of MESH objects. It must be allocated before calling
393 this method. All names are put in it.
395 void MED::getMeshNames ( string * meshNames ) const
396 throw (MED_EXCEPTION)
398 const char * LOC = "MED::getMeshNames ( string * ) const : ";
400 unsigned int meshNamesSize;
402 if ( ( meshNamesSize = sizeof(meshNames) / sizeof(string *) )
404 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
405 << "Size of parameter meshNames is |"
406 << meshNamesSize << "| and should be |"
407 << _meshes.size() << "| and should be |"
411 // REM : ALLOCATION D'UN TABLEAU DE POINTEURS SUR STRING FAITE PAR LE CLIENT
412 map<MESH_NAME_,MESH*>::const_iterator currentMesh; // ??ITERATEUR CONST SUR UN OBJET NON CONST ??
414 int meshNamesIndex = 0;
416 for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
417 meshNames[meshNamesIndex]=(*currentMesh).first;
418 meshNamesIndex++; // CF OPTIMISATION
425 Get the names of all MESH objects.
427 Return a deque<string> object which contain the name of all MESH objects.
429 deque<string> MED::getMeshNames () const {
431 const char * LOC = "MED::getMeshNames () const : ";
434 deque<string> meshNames(_meshes.size());
436 map<MESH_NAME_,MESH*>::const_iterator currentMesh; // ??ITERATEUR CONST SUR UN OBJET NON CONST ??
438 int meshNamesIndex = 0;
440 for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
441 meshNames[meshNamesIndex]=(*currentMesh).first;
442 meshNamesIndex++; // CF OPTIMISATION
451 Return a reference to the MESH object named meshName.
453 MESH * MED::getMesh ( const string & meshName ) const
454 throw (MED_EXCEPTION)
457 const char * LOC = "MED::getMesh ( const string & meshName ) const : ";
460 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 |"
469 return (*itMeshes).second;
475 \internal Return a reference to the MESH object associated with
478 MESH * MED::getMesh (const FIELD_ * const field ) const
479 throw (MED_EXCEPTION)
482 const char * LOC = "MED::getMesh ( const FIELD * field ) const : ";
485 FIELD_ * f = const_cast< FIELD_* > (field); // Comment faire mieux ?
486 map<FIELD_ *, MESH_NAME_>::const_iterator itMeshName = _meshName.find(f);
488 if ( itMeshName == _meshName.end() )
489 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
490 << "There is no known mesh associated with |"
491 << field << "| pointer"
495 string meshName = (*itMeshName).second;
496 map<MESH_NAME_,MESH*>::const_iterator itMeshes = _meshes.find(meshName);
497 if ( itMeshes == _meshes.end() )
498 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
499 << "There is no known mesh named |"
500 << meshName << " while it's associated with the found field |"
501 << field << "| pointer"
505 return (*itMeshes).second;
512 Get the names of all FIELD objects.
514 fieldNames is an in/out argument.
516 It is an array of string of size the
517 number of FIELD objects. It must be allocated before calling
518 this method. All names are put in it.
520 void MED::getFieldNames ( string * fieldNames ) const
521 throw (MED_EXCEPTION)
523 const char * LOC = "MED::getFieldNames ( string * ) const : ";
526 unsigned int fieldNamesSize = sizeof(fieldNames) / sizeof(string *);
528 if ( fieldNamesSize != _fields.size() )
529 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
530 << "Size of parameter fieldNames is |"
531 << fieldNamesSize << "| and should be |"
532 << _fields.size() << "| and should be |"
536 // REM : ALLOCATION D'UN TABLEAU DE POINTEURS SUR STRING FAITE PAR LE CLIENT
537 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator currentField;
539 int fieldNamesIndex = 0;
540 for ( currentField=_fields.begin();currentField != _fields.end(); currentField++ ) {
541 fieldNames[fieldNamesIndex]=(*currentField).first;
542 fieldNamesIndex++; // CF OPTIMISATION
550 Get the names of all FIELD objects.
552 Return a deque<string> object which contain the name of all FIELD objects.
554 deque<string> MED::getFieldNames () const {
556 const char * LOC = "MED::getFieldNames ( ) const : ";
559 deque<string> fieldNames(_fields.size());
561 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator currentField;
563 int fieldNamesIndex = 0;
565 for ( currentField=_fields.begin();currentField != _fields.end(); currentField++ ) {
566 fieldNames[fieldNamesIndex]=(*currentField).first;
567 fieldNamesIndex++; // CF OPTIMISATION
575 Return a deque<DT_IT_> which contain all iteration step for the FIELD
576 identified by its name.
578 deque<DT_IT_> MED::getFieldIteration (const string & fieldName) const
579 throw (MED_EXCEPTION)
582 const char * LOC = "MED::getFieldIteration ( const string & ) const : ";
585 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator itFields = _fields.find(fieldName);
587 if ( itFields == _fields.end() )
588 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
589 << "There is no known field named |"
593 // const MAP_DT_IT_ & myIterationMap = const_cast<const MAP_DT_IT_ & > ((*itFields).second);
594 const MAP_DT_IT_ & myIterationMap = (*itFields).second ;
595 MAP_DT_IT_::const_iterator currentIterator ;
597 deque<DT_IT_> Iteration(myIterationMap.size());
599 int iterationIndex = 0;
601 for ( currentIterator=myIterationMap.begin();currentIterator != myIterationMap.end(); currentIterator++ ) {
602 Iteration[iterationIndex]=(*currentIterator).first;
603 iterationIndex++; // CF OPTIMISATION
611 Return a reference to the FIELD object named fieldName with
612 time step number dt and order number it.
614 FIELD_ * MED::getField ( const string & fieldName, const int dt=MED_NOPDT, const int it=MED_NOPDT ) const
615 throw (MED_EXCEPTION)
618 const char * LOC = "MED::getField ( const string &, const int, const int ) const : ";
621 MESSAGE(LOC << "fieldName = "<<fieldName<<", dt ="<<dt<<", it = "<<it);
628 map<FIELD_NAME_,MAP_DT_IT_>::const_iterator itFields = _fields.find(fieldName);
630 if ( itFields == _fields.end() )
631 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
632 << "There is no known field named |"
637 const MAP_DT_IT_ & map_dtIt = const_cast<const MAP_DT_IT_ & > ((*itFields).second);
638 MAP_DT_IT_::const_iterator itMap_dtIt = map_dtIt.find(dtIt);
640 if ( itMap_dtIt == map_dtIt.end() )
641 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
642 << "There is no (dt,it) |("
643 << dt << "," << it << ")| associated with the found field |"
650 //return _fields[fieldName][dtIt];
651 return (*itMap_dtIt).second;
656 // fiend ostream & MED::operator<<(ostream &os,const MED & med) const {
661 Return a map<MED_FR::med_entite_maillage,SUPPORT*> which contain
662 foreach entity, a reference to the SUPPORT on all elements.
664 const map<MED_FR::med_entite_maillage,SUPPORT*> & MED::getSupports(const string & meshName) const
665 throw (MED_EXCEPTION)
667 const char * LOC = "MED::getSupports ( const string ) const : ";
670 map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::const_iterator itSupportOnMesh = _support.find(meshName) ;
672 if ( itSupportOnMesh == _support.end() )
673 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
674 << "There is no support on mesh named |"
679 return (*itSupportOnMesh).second ;
683 Return a reference to the SUPPORT object on all elements of entity
684 for the MESH named meshName.
686 SUPPORT * MED::getSupport (const string & meshName,MED_FR::med_entite_maillage entity) const
687 throw (MED_EXCEPTION)
689 const char * LOC = "MED::getSupport ( const string, MED_FR::med_entite_maillage ) const : ";
693 for (map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::const_iterator const_itSupportOnMesh=_support.begin(); const_itSupportOnMesh != _support.end();
694 const_itSupportOnMesh++ )
696 map<MED_FR::med_entite_maillage,SUPPORT *>::const_iterator const_itSupport ;
697 for (const_itSupport=(*const_itSupportOnMesh).second.begin();
698 const_itSupport!=(*const_itSupportOnMesh).second.end();const_itSupport++) index++;
701 MESSAGE(LOC << "In this MED object there is(are) " << index << " support(s):");
703 for (map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::const_iterator const_itSupportOnMesh=_support.begin();const_itSupportOnMesh != _support.end(); const_itSupportOnMesh++ )
705 map<MED_FR::med_entite_maillage,SUPPORT *>::const_iterator const_itSupport ;
706 for (const_itSupport=(*const_itSupportOnMesh).second.begin();
707 const_itSupport!=(*const_itSupportOnMesh).second.end();const_itSupport++)
709 MESSAGE(LOC << "Support on mesh " << (*const_itSupportOnMesh).first << " on entity " << (*const_itSupport).first << " : " << *((*const_itSupport).second));
714 map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::const_iterator const_itSupportOnMesh = _support.find(meshName) ;
716 if ( const_itSupportOnMesh == _support.end() )
717 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
718 << "There is no support on mesh named |"
723 // map<MED_FR::med_entite_maillage,SUPPORT *> & SupportOnMesh = (map<MED_FR::med_entite_maillage,SUPPORT *>&) ((*itSupportOnMesh).second) ;
724 // map<MED_FR::med_entite_maillage,SUPPORT *>::const_iterator itSupport = SupportOnMesh.find(entity) ;
726 // if (itSupport == SupportOnMesh.end() )
727 // throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
728 // << "There is no support on entity "
729 // << entity << " in mesh named |"
730 // << meshName << "|"
735 map<MED_FR::med_entite_maillage,SUPPORT *> SupportOnMesh = ((*const_itSupportOnMesh).second);
737 map<MED_FR::med_entite_maillage,SUPPORT *>::const_iterator itSupport = SupportOnMesh.find(entity) ;
739 if (itSupport == SupportOnMesh.end() )
740 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
741 << "There is no support on entity "
742 << entity << " in mesh named |"
747 return (*itSupport).second ;
751 Temporary method : when all MESH objects are read, this methods
752 update all SUPPORT objects with the rigth dimension.
754 void MED::updateSupport ()
757 const char * LOC = "MED::updateSupport () : ";
760 map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::iterator itSupportOnMesh ;
761 for ( itSupportOnMesh=_support.begin();itSupportOnMesh != _support.end(); itSupportOnMesh++ ) {
762 map<MED_FR::med_entite_maillage,SUPPORT *>::iterator itSupport ;
763 for ( itSupport=(*itSupportOnMesh).second.begin();itSupport!=(*itSupportOnMesh).second.end();itSupport++)
765 (*itSupport).second->update() ;
767 catch (MEDEXCEPTION & ex) {
768 // entity not defined in mesh -> we remove support on it !
769 MESSAGE(LOC<<ex.what());
770 delete (*itSupport).second ;
771 (*itSupportOnMesh).second.erase(itSupport) ; // that's rigth ????
780 Add the given MESH object. MED object control it,
781 and destroy it, so you must not destroy it after.
783 The meshName is given by the MESH object.
785 void MED::addMesh( MESH * const ptrMesh)
786 throw (MED_EXCEPTION)
788 const char * LOC = "MED::addMesh(const MESH * ptrMesh): ";
792 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrMesh must not be NULL !"));
795 if ( ! ( meshName = ptrMesh->getName()).size() )
796 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrMesh->_name must not be NULL !"));
798 // MESH * meshToMed = new MESH(*ptrMesh); DO WE HAVE TO COPY THE ENTRY MESH OR NOT ????? (NB)
800 _meshes[meshName] = ptrMesh; // if ptrMesh->meshName already exists it is modified
802 // _meshes[meshName] = meshToMed;
808 Add the given FIELD object. MED object control it,
809 and destroy it, so you must not destroy it after.
811 The fieldName is given by the FIELD object.
813 void MED::addField( FIELD_ * const ptrField)
814 throw (MED_EXCEPTION)
816 const char * LOC = "MED::addField(const FIELD_ * const ptrField): ";
820 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField must not be NULL !"));
823 if ( ! (fieldName = ptrField->getName()).size() )
824 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_name must not be NULL !"));
826 SUPPORT * ptrSupport;
827 if ( ! ( ptrSupport = (SUPPORT * ) ptrField->getSupport()) )
828 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_support must not be NULL !"));
831 if ( ! ( ptrMesh = ptrSupport->getMesh()) )
832 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_support->_mesh must not be NULL !"));
835 if ( ! ( meshName = ptrMesh->getName()).size() )
836 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "ptrField->_support->_mesh->_name must not be NULL !"));
839 dtIt.dt = ptrField->getIterationNumber();
840 dtIt.it = ptrField->getOrderNumber();
842 _fields [fieldName][dtIt] = ptrField; // if it already exists it is replaced
843 _meshName [ptrField] = meshName; // if it already exists it is replaced
844 _meshes [meshName] = ptrMesh; // if it already exists it is replaced
846 // int numberOfTypes = ptrSupport->getNumberOfTypes(); !! UNUSED VARIABLE !!
847 _support [meshName][ (MED_FR::med_entite_maillage) ptrSupport->getEntity()] = ptrSupport;// if it already exists it is replaced