3 # include "MEDMEM_MedMedDriver.hxx"
5 # include "MEDMEM_DriversDef.hxx"
7 # include "MEDMEM_Mesh.hxx"
8 # include "MEDMEM_Field.hxx"
9 // EN ATTENDANT L'utilisation de MedFieldDriver.hxx ds Field.hxx
10 # include "MEDMEM_MedFieldDriver.hxx"
11 # include "MEDMEM_Med.hxx"
13 # include "MEDMEM_define.hxx"
15 using namespace MED_FR;
17 MED_MED_DRIVER::MED_MED_DRIVER(): GENDRIVER(),
18 _ptrMed((MED * const)MED_NULL),
22 MED_MED_DRIVER::MED_MED_DRIVER(const string & fileName, MED * const ptrMed):
23 GENDRIVER(fileName,MED_EN::MED_RDWR), _ptrMed(ptrMed), _medIdt(MED_INVALID)
26 MED_MED_DRIVER::MED_MED_DRIVER(const string & fileName,
28 MED_EN::med_mode_acces accessMode):
29 GENDRIVER(fileName,accessMode), _ptrMed(ptrMed), _medIdt(MED_INVALID)
32 //REM : As t'on besoin du champ _status : _medIdt <-> _status ? Oui
35 void MED_MED_DRIVER::open() {
37 const char * LOC ="MED_MED_DRIVER::open() : ";
40 // REFLECHIR SUR CE TEST PAR RAPPORT A L'OUVERTURE/FERMETURE
41 if ( _medIdt != MED_INVALID )
42 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
43 << "_medIdt is already in use, please close the file |"
44 << _fileName << "| before calling open()"
48 if ( _status != MED_CLOSED )
49 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
50 << "_status is closed, please close the file |"
51 << _fileName << "| before calling open()"
55 if ( _fileName == "" )
56 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
57 << "_fileName is |\"\"|, please set a correct fileName before calling open()"
61 _medIdt = MEDouvrir( (const_cast <char *> (_fileName.c_str())), (MED_FR::med_mode_acces) _accessMode);
63 if (_medIdt > 0) _status=MED_OPENED;
66 _medIdt = MED_INVALID;
67 throw MED_EXCEPTION (LOCALIZED( STRING(LOC)
68 << "Can't open |" << _fileName
69 << "|, _medIdt : " << _medIdt
78 void MED_MED_DRIVER::close() {
79 MED_FR::med_int err = 0;
80 const char * LOC = "MED_MED_DRIVER::close() : ";
83 if ( _status == MED_CLOSED)
84 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << ": the file |"
85 << _fileName << "| is already closed"
89 if ( _medIdt == MED_INVALID )
90 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "_medIdt invalid, but the file |"
91 << _fileName << "| seems to be openned !"
95 err=MEDfermer(_medIdt);
98 _medIdt = MED_INVALID;
100 if (err != MED_VALID)
101 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "the file |"
102 << _fileName << "| couldn't be closed"
111 void MED_MED_DRIVER::readFileStruct( void )
113 const char * LOC = "MED_MED_DRIVER::readFileStruct() : ";
118 if ( _medIdt == MED_INVALID )
119 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
120 << "_medIdt is invalid, please open the file |"
121 << _fileName << "| before calling readFileStruct()"
124 // Read number of meshes and their associated name
127 char meshName[MED_TAILLE_NOM+1]="";
130 MED_MESH_RDWR_DRIVER * ptrDriver;
132 numberOfMeshes = MEDnMaa(_medIdt) ;
133 if ( numberOfMeshes <= 0 )
134 MESSAGE(LOC << "Be careful there is no mesh in file |"<<_fileName<<"| !");
136 MESH_ENTITIES::const_iterator currentEntity;
137 for (i=1;i<=numberOfMeshes;i++) {
138 err = MEDmaaInfo(_medIdt, i ,meshName, &meshDim) ;
139 if (err != MED_VALID)
140 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << ": can't get information about the mesh n°"
141 << i <<" of the file |" << _fileName << "| !"
144 MESSAGE(LOC<<": Mesh n°"<<i<<" nammed "<<meshName);
146 ptrMesh = new MESH();
147 // NO : create a MED_MESH_RDWR_DRIVER with the currently used
148 // _medIdt which remains VALID as long as _fileName is openned
149 // NO: then as long as the MED driver remains open
150 MED_MESH_RDWR_DRIVER * _ptrDriver = new MED_MESH_RDWR_DRIVER(_fileName, ptrMesh);
151 _ptrDriver->setMeshName(meshName);
152 ptrMesh->addDriver(*_ptrDriver);
153 _ptrMed->_meshes[meshName] = ptrMesh;
154 // we create all global support (for each entity type :
155 for (currentEntity=meshEntities.begin();currentEntity != meshEntities.end(); currentEntity++) {
156 string supportName="SupportOnAll_" ;
157 supportName+=entNames[(MED_FR::med_entite_maillage)(*currentEntity).first] ;
158 (_ptrMed->_support)[meshName][(MED_FR::med_entite_maillage)(*currentEntity).first]=new SUPPORT(ptrMesh,supportName,(MED_EN::medEntityMesh) (*currentEntity).first) ;
164 // Read number of fields, their associated name and their list of (timeStepNumber,iterationNumber)
166 int numberOfFields = 0; //MED_INVALID
167 // char fieldName[MED_TAILLE_NOM+1] = "";
168 char fieldName[MED_TAILLE_NOM+1] ;
169 int numberOfComponents = 0;
170 char * componentName = (char *) MED_NULL;
171 char * unitName = (char *) MED_NULL;
172 // char meshName[MED_TAILLE_NOM+1] = "";
173 char meshName[MED_TAILLE_NOM+1] ;
174 MED_FR::med_type_champ type;
175 MESH * ptrMesh = (MESH *) MED_NULL;
176 FIELD_ * ptrField = (FIELD_ *) MED_NULL;
177 //MED_FIELD_RDWR_DRIVER * ptrDriver = (MED_FIELD_RDWR_DRIVER * ) MED_NULL;
178 GENDRIVER * ptrDriver = (GENDRIVER * ) MED_NULL;
179 SUPPORT * ptrSupport = (SUPPORT * ) MED_NULL;
180 MESH_ENTITIES::const_iterator currentEntity;
181 list<MED_FR::med_geometrie_element>::const_iterator currentGeometry;
182 int NbOfGaussPts = 0;
183 int numberOfTimeSteps = -1;
184 int timeStepNumber = -1;
185 // char timeStepUnit[MED_TAILLE_PNOM]= "";
186 char timeStepUnit[MED_TAILLE_PNOM+1] ;
187 double timeStep = 0.0;
188 int orderNumber = -1; //???init?????
189 map<MESH_NAME_,MESH*> & _meshes = _ptrMed->_meshes;
190 map<FIELD_NAME_,MAP_DT_IT_> & _fields = _ptrMed->_fields;
191 map<FIELD_ *, MESH_NAME_> & _meshName = _ptrMed->_meshName;
192 map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> > & _support = _ptrMed->_support;
195 numberOfFields = MEDnChamp(_medIdt,0) ;
196 if ( numberOfFields <= 0 )
197 MESSAGE(LOC << "Be careful there is no field in file |"<<_fileName<<"| !");
199 for (i=1;i<=numberOfFields;i++) {
201 numberOfComponents = MEDnChamp(_medIdt,i) ;
202 if ( numberOfComponents <= 0 )
203 if (err != MED_VALID)
204 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "Be careful there is no compound for field n°"
205 << i << "in file |"<<_fileName<<"| !"));
207 componentName = new char[numberOfComponents*MED_TAILLE_PNOM+1] ;
208 unitName = new char[numberOfComponents*MED_TAILLE_PNOM+1] ;
210 err = MEDchampInfo(_medIdt, i, fieldName, &type, componentName,
211 unitName, numberOfComponents) ;
213 if (err != MED_VALID)
214 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
215 << ": can't get information about the field n°"
216 << i <<" of the file |" << _fileName << "| !"));
218 MESSAGE(LOC << "Field n°"<<i<<" nammed "<< fieldName
219 << " ,component(s) : " << componentName
220 <<" ,unit(s) : " << unitName);
223 // Loop on all (entity type,geometry type) until you find an existing one then
224 // get the (n°dt,n°it) pairs list for the current (field,entity type,geometry type)
225 // We suppose there is the same list whatever the existing (entity type,geometry type) pair
227 for (currentEntity=meshEntities.begin();currentEntity != meshEntities.end(); currentEntity++) {
228 // numberOfTimeSteps MUST be given by MEDchampInfo !!!!!
229 for (currentGeometry = (*currentEntity).second.begin();currentGeometry != (*currentEntity).second.end(); currentGeometry++) {
230 MESSAGE("Field information with Entity,Geom = "<<(*currentEntity).first<<","<<(*currentGeometry));
231 numberOfTimeSteps = MEDnPasdetemps(_medIdt, fieldName,
232 (MED_FR::med_entite_maillage)(*currentEntity).first,
233 (MED_FR::med_geometrie_element) (*currentGeometry) );
234 MESSAGE("Field information 2 : NumberOfTimeStep :"<<numberOfTimeSteps);
235 if ( numberOfTimeSteps > MED_VALID )
236 break ; // There are value for some med_geometrie_element of this med_entite_maillage.
238 if (numberOfTimeSteps>0) // we have at least one
240 for (currentGeometry = (*currentEntity).second.begin();currentGeometry != (*currentEntity).second.end(); currentGeometry++) {
242 MESSAGE("Field information 3 : Geom : "<<(*currentGeometry));
243 for (j=1;j <= numberOfTimeSteps; j++) {
245 MESSAGE("Field information 4 : time step j = "<<j);
246 err = MEDpasdetempsInfo( _medIdt, fieldName,
247 (MED_FR::med_entite_maillage) (*currentEntity).first,
248 (*currentGeometry),j,
249 meshName, &NbOfGaussPts,
250 &timeStepNumber, timeStepUnit, &timeStep,
252 if (err == MED_VALID) { // we have found for (*currentEntity).first and (*currentGeometry)
254 MESSAGE("Field information 5 ;: NumberOfGaussPoint : "<<NbOfGaussPts<<", timeStepNumber : "<<timeStepNumber);
255 // CORRECT a bug in MEDpasdetempsInfo :
256 // we get a value n'importe quoi in NbOfGaussPts !!!!
258 if (NbOfGaussPts>100)
260 if (timeStepNumber<0)
264 // ATTENTION TRAITER L'EXCEPTION !!!!!!!!!!!!
266 // Il faudra traiter le cas d'un champ qui utilise +sieurs (entity,geom) voir le travail de patrick
267 // Il faudra traiter le cas des profils...
268 // ptrField = new FIELD();
269 // _ptrDriver = new MED_FIELD_RDWR_DRIVER(_fileName, ptrField);
270 // ptrField->addDriver(_ptrDriver);
271 // _fields[fieldName]=ptrField;
273 // Verify meshName is already known
275 map<MESH_NAME_,MESH*>::iterator _meshesIt = _meshes.find(meshName);
276 if ( _meshesIt == _meshes.end() ) {
277 MESSAGE(LOC << "There is no mesh |"
278 << meshName <<"| in the file |"
279 << _fileName <<"|, but |" << meshName <<"| is referenced by field |"
280 << fieldName <<"|, entity : |"
281 << entNames [ (MED_FR::med_entite_maillage) (*currentEntity).first] <<"|, geometric element of type |"
282 << geoNames [ (MED_FR::med_geometrie_element) (*currentGeometry)] <<"|"
284 } // POURQUOI SI JE NE MET PAS DE BLOCK J'AI UN PARSE ERROR : PG : c'est la macro MESSAGE qui fait ca !
286 ptrMesh = _meshes[meshName];
288 ptrSupport = _support[meshName][(MED_FR::med_entite_maillage) (*currentEntity).first];
289 if (NbOfGaussPts != 1)
290 throw MEDEXCEPTION(LOCALIZED( STRING(LOC) <<"Number of Gauss Point must be equal to 1 for instance")) ;
292 // init to null to prevent some error if not correctly allocated !
293 ptrField = (FIELD_*)NULL ;
294 ptrDriver = (GENDRIVER*)NULL ;
297 case MED_FR::MED_INT64 :
298 if ( sizeof(MED_FR::med_int) != 8 )
299 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) <<
300 " The Field type of |"
301 << fieldName <<"|, entity : |"
302 << entNames [(MED_FR::med_entite_maillage) (*currentEntity).first] <<"|, geometric element of type |"
303 << geoNames [(MED_FR::med_geometrie_element) (*currentGeometry) ] <<
304 "| is MED_INT64 but size of med_int is not equal to 8 bytes !"
308 case MED_FR::MED_INT32 : {
309 // throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) <<
310 // "NOT IMPLEMENTED : BUG IN STL !")
312 // ptrField = new FIELD<MED_FR::med_int> ( ptrSupport,numberOfComponents ); // Les valeurs du champ ne doivent pas être lue pour l'instant
314 ptrField = new FIELD<MED_FR::med_int> ( ); // Les valeurs du champ ne doivent pas être lue pour l'instant
315 ((FIELD<MED_FR::med_int>*) ptrField)->setSupport(ptrSupport);
316 ((FIELD<MED_FR::med_int>*) ptrField)->setNumberOfComponents(numberOfComponents);
317 ((FIELD<MED_FR::med_int>*) ptrField)->setName(fieldName) ; //provisoire, pour debug
318 MESSAGE("#### SET NAME in FIELD : "<<fieldName);
319 ptrDriver = new MED_FIELD_RDWR_DRIVER<MED_FR::med_int> (_fileName, (FIELD<MED_FR::med_int> *) ptrField);
322 case MED_FR::MED_REEL64 : {
323 // ptrField = new FIELD<MED_FR::med_float> ( ptrSupport,numberOfComponents ); // Les valeurs du champ ne doivent pas être lue pour l'instant
324 ptrField = new FIELD<MED_FR::med_float> ( ); // Les valeurs du champ ne doivent pas être lue pour l'instant
325 ((FIELD<MED_FR::med_float>*) ptrField)->setSupport(ptrSupport);
326 ((FIELD<MED_FR::med_float>*) ptrField)->setNumberOfComponents(numberOfComponents);
327 ((FIELD<MED_FR::med_float>*) ptrField)->setName(fieldName) ; //provisoire, pour debug
328 MESSAGE("#### SET NAME in FIELD : "<<fieldName);
330 ptrDriver = new MED_FIELD_RDWR_DRIVER<MED_FR::med_float>(_fileName, (FIELD<MED_FR::med_float> *) ptrField);
334 if ( numberOfTimeSteps > 1)
335 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) <<
336 " The Field type of |"
337 << fieldName <<"|, entity : |"
338 << entNames [(MED_FR::med_entite_maillage) (*currentEntity).first]
339 <<"|, geometric element of type |"
340 << geoNames [(MED_FR::med_geometrie_element) (*currentGeometry)]
341 <<"| is neither MED_INT, MED_INT32, MED_INT64 nor MED_REEL64 !"
347 ptrField->setValueType((MED_EN::med_type_champ) type) ; // need to write field !
349 MESSAGE("timeStepNumber :"<<timeStepNumber<<",orderNumber :"<<orderNumber);
350 ptrField->setIterationNumber ( timeStepNumber); // A ajouter dans la classe FIELD
351 ptrField->setOrderNumber ( orderNumber);
352 ptrField->setTime ( timeStep);
354 // Create a driver for this (field n°dt,n°it)
356 MESSAGE("###### ptrDriver->setFieldName : #"<<fieldName<<"#");
357 ptrDriver->setFieldName(fieldName);
359 ptrField->addDriver(*ptrDriver);
362 dtIt.dt = timeStepNumber;
363 dtIt.it = orderNumber;
365 (_fields [fieldName])[dtIt] = ptrField;
366 _meshName[ptrField ] = meshName;
371 delete[] componentName ;
376 // read profil count and their names
377 int support_count_= 0 ;
378 // il faut lire les champs pour avoir les profils stockes !!!
379 // il faudrait implémenter la lecture des profils dans med !!!
385 void MED_MED_DRIVER::read( void ) {
387 const char * LOC = "MED_MED_DRIVER::read() : ";
390 MESSAGE("METHODE PAS ENCORE IMPLEMENTEE !!! ");
394 void MED_MED_DRIVER::writeFrom( void) const {
396 const char * LOC = "MED_MED_DRIVER::writeFrom() : ";
400 const map<MESH_NAME_, MESH*> & _meshes = const_cast<const map<MESH_NAME_, MESH*>& > (_ptrMed->_meshes);
401 map<MESH_NAME_,MESH*>::const_iterator currentMesh;
403 const map<FIELD_ *, MESH_NAME_> & _meshName = const_cast<const map<FIELD_ *, MESH_NAME_>& > (_ptrMed->_meshName);
404 map<FIELD_ *, MESH_NAME_>::const_iterator currentField;
406 for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
408 (*currentMesh).second->write(*this);
409 // A CREER pour les objects MESH ET FIELD le write(GENDRIVER *) et le == ds GENDRIVER avec eventuellement 1 id
411 catch ( const MED_DRIVER_NOT_FOUND_EXCEPTION & ex ) {
414 // catch (const MED_EXCEPTION & ex) {
415 // throw ex; // DOIT-ON CREER UNE NOUVELLE EXCEPTION AVEC UN MESSAGE INDIQUANT LA PILE
419 for ( currentField=_meshName.begin();currentField != _meshName.end(); currentField++ ) {
421 (*currentField).first->write(*this);
423 catch ( const MED_DRIVER_NOT_FOUND_EXCEPTION & ex ) {
432 void MED_MED_DRIVER::write(void ) const {
434 const char * LOC = "MED_MED_DRIVER::write() : ";
438 // BCLE SUR LES DRIVERS AVEC APPELS WriteFrom
443 MED_MED_RDONLY_DRIVER::MED_MED_RDONLY_DRIVER(const string & fileName, MED * const ptrMed):
444 MED_MED_DRIVER(fileName,ptrMed,MED_EN::MED_RDONLY)
446 MESSAGE("MED_MED_RDONLY_DRIVER::MED_MED_RDONLY_DRIVER(const string & fileName, MED * const ptrMed) Constructeur read only");
449 void MED_MED_RDONLY_DRIVER::open() {
450 BEGIN_OF("MED_MED_RDONLY_DRIVER::open()");
451 MED_MED_DRIVER::open();
452 END_OF("MED_MED_RDONLY_DRIVER::open()");
455 void MED_MED_RDONLY_DRIVER::close() {
456 BEGIN_OF("MED_MED_RDONLY_DRIVER::close()");
457 MED_MED_DRIVER::close();
458 END_OF("MED_MED_RDONLY_DRIVER::clode()");
461 void MED_MED_RDONLY_DRIVER::read(void) {
462 BEGIN_OF("MED_MED_RDONLY_DRIVER::read(void)");
463 MED_MED_DRIVER::read();
464 END_OF("MED_MED_RDONLY_DRIVER::read(void)");
467 void MED_MED_RDONLY_DRIVER::readFileStruct(void) {
468 BEGIN_OF("MED_MED_RDONLY_DRIVER::readFileStruct(void)");
469 MED_MED_DRIVER::readFileStruct();
470 END_OF("MED_MED_RDONLY_DRIVER::readFileStruct(void)");
473 MED_MED_WRONLY_DRIVER::MED_MED_WRONLY_DRIVER(const string & fileName, MED * const ptrMed):
474 MED_MED_DRIVER(fileName,ptrMed)
477 void MED_MED_WRONLY_DRIVER::open() {
478 BEGIN_OF("MED_MED_WRONLY_DRIVER::open()");
479 MED_MED_DRIVER::open();
480 END_OF("MED_MED_WRONLY_DRIVER::open()");
483 void MED_MED_WRONLY_DRIVER::close() {
484 BEGIN_OF("MED_MED_WRONLY_DRIVER::close()");
485 MED_MED_DRIVER::close();
486 END_OF("MED_MED_WRONLY_DRIVER::clode()");
489 void MED_MED_WRONLY_DRIVER::write(void) const {
490 BEGIN_OF("MED_MED_WRONLY_DRIVER::write(void) const");
491 MED_MED_DRIVER::write();
492 END_OF("MED_MED_WRONLY_DRIVER::write(void) const");
495 void MED_MED_WRONLY_DRIVER::writeFrom(void) const {
496 BEGIN_OF("MED_MED_WRONLY_DRIVER::writeFrom(void) const");
497 MED_MED_DRIVER::writeFrom();
498 END_OF("MED_MED_WRONLY_DRIVER::writeFrom(void) const");
501 MED_MED_RDWR_DRIVER::MED_MED_RDWR_DRIVER(const string & fileName, MED * const ptrMed):
502 MED_MED_DRIVER(fileName,ptrMed)
505 void MED_MED_RDWR_DRIVER::open() {
506 BEGIN_OF("MED_MED_RDWR_DRIVER::open()");
507 MED_MED_DRIVER::open();
508 END_OF("MED_MED_RDWR_DRIVER::open()");
511 void MED_MED_RDWR_DRIVER::close() {
512 BEGIN_OF("MED_MED_RDWR_DRIVER::close()");
513 MED_MED_DRIVER::close();
514 END_OF("MED_MED_RDWR_DRIVER::clode()");
517 void MED_MED_RDWR_DRIVER::read(void) {
518 BEGIN_OF("MED_MED_RDWR_DRIVER::read(void)");
519 MED_MED_RDONLY_DRIVER::read();
520 END_OF("MED_MED_RDWR_DRIVER::read(void)");
523 void MED_MED_RDWR_DRIVER::readFileStruct(void) {
524 BEGIN_OF("MED_MED_RDWR_DRIVER::readFileStruct(void)");
525 MED_MED_RDONLY_DRIVER::readFileStruct();
526 END_OF("MED_MED_RDWR_DRIVER::readFileStruct(void)");
529 void MED_MED_RDWR_DRIVER::write(void) const {
530 BEGIN_OF("MED_MED_RDWR_DRIVER::write(void) const");
531 MED_MED_WRONLY_DRIVER::write();
532 END_OF("MED_MED_RDWR_DRIVER::write(void) const");
535 void MED_MED_RDWR_DRIVER::writeFrom(void) const {
536 BEGIN_OF("MED_MED_RDWR_DRIVER::writeFrom(void) const");
537 MED_MED_WRONLY_DRIVER::writeFrom();
538 END_OF("MED_MED_RDWR_DRIVER::writeFrom(void) const");