Salome HOME
Version ok de MED avec MEDGUI.
[modules/med.git] / src / MEDMEM / MEDMEM_Med.cxx
1 # include "MEDMEM_Med.hxx"
2  
3 # include "MEDMEM_STRING.hxx"
4
5 # include "MEDMEM_Mesh.hxx"
6 # include "MEDMEM_Field.hxx"
7
8 # include "MEDMEM_Exception.hxx"
9 # include "utilities.h"
10
11 // MED constructors
12 MED::MED() {
13   const char * LOC = "MED::MED() : ";
14  
15   MESSAGE(LOC << "Construction...");
16
17 };
18
19 MED::MED(driverTypes driverType, const string & fileName) {
20   const char * LOC = "MED::MED(driverTypes driverType, const string & fileName) : ";
21   int current;
22
23   BEGIN_OF(STRING(LOC));
24   current = addDriver(driverType,fileName);
25   _drivers[current]->open();
26   _drivers[current]->readFileStruct();
27   _drivers[current]->close();
28   END_OF(STRING(LOC));
29 };
30
31 MED::~MED()
32 {
33 } ;
34
35 // ------- Drivers Management Part
36
37 MED::INSTANCE_DE<MED_MED_DRIVER> MED::inst_med ;
38 //MED::INSTANCE_DE<VTK_DRIVER> MED::inst_vtk ;
39
40 const MED::INSTANCE * const MED::instances[] = { &MED::inst_med }; //, &MED::inst_vtk } ;
41
42 int MED::addDriver(driverTypes driverType, const string & fileName="Default File Name.med") {
43
44   const char * LOC = "MED::addDriver(driverTypes driverType, const string & fileName=\"Default File Name.med\") : ";
45   GENDRIVER * driver;
46   int current;
47
48   BEGIN_OF(LOC);
49
50   driver = instances[driverType]->run(fileName, this) ;
51   _drivers.push_back(driver);
52   current = _drivers.size()-1;
53   // _drivers[current]->set... 
54
55   END_OF(LOC);
56   return current;
57 }
58
59
60 void MED::rmDriver (int index=0) {
61   const char * LOC = "MED::rmDriver (int index=0): ";
62   BEGIN_OF(LOC);
63
64   if (_drivers[index])
65     //_drivers.erase(&_drivers[index]); 
66     {}
67   else
68     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
69                                      << "The index given is invalid, index must be between 0 and |" 
70                                      << _drivers.size()
71                                      )
72                           );   
73   END_OF(LOC);
74 }
75
76
77 void MED::writeFrom (int index=0)
78 {
79   const char * LOC = "MED::write (int index=0): ";
80   BEGIN_OF(LOC);
81
82   if (_drivers[index]) {
83     _drivers[index]->open(); 
84     _drivers[index]->writeFrom(); 
85     _drivers[index]->close(); 
86   }
87   throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
88                                    << "The index given is invalid, index must be between 0 and |" 
89                                    << _drivers.size()
90                                    )
91                         ); 
92   END_OF(LOC);
93 }; 
94
95
96 void MED::write (int index=0)
97 {
98   const char * LOC = "MED::writeAll (int index=0): ";
99   BEGIN_OF(LOC);
100
101   if (_drivers[index]) {
102     _drivers[index]->open(); 
103     _drivers[index]->write(); 
104     _drivers[index]->close(); 
105   }
106   else
107     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
108                                      << "The index given is invalid, index must be between  0 and |" 
109                                      << _drivers.size()
110                                      )
111                           ); 
112   END_OF(LOC);
113 }; 
114
115
116 void MED::readFileStruct (int index=0) {
117   const char * LOC = "MED::readFileStruct (int index=0): ";
118   BEGIN_OF(LOC);
119   
120   if (_drivers[index]) {
121     _drivers[index]->open(); 
122     _drivers[index]->readFileStruct(); 
123     _drivers[index]->close(); 
124   }
125   else
126     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
127                                      << "The index given is invalid, index must be between 0 and |" 
128                                      << _drivers.size()
129                                      )
130                           );   
131   END_OF(LOC);
132 }
133
134 // void MED::read  (int index=0)
135 // {
136 //   const char * LOC = "MED::read (int index=0): ";
137 //   BEGIN_OF(LOC);
138   
139 //   if (_drivers[index])  
140 //     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
141 //                                      << "The index given is invalid, index must be between  >0 and < |" 
142 //                                      << _drivers.size()-1 
143 //                                      )
144 //                           );   
145 //   // GERER LE 0
146 //   _drivers[index]->open(); 
147 //   _drivers[index]->read(); 
148 //   _drivers[index]->close(); 
149
150 //   END_OF(LOC);
151
152 // };
153
154 // ------- End Of Drivers Management Part
155
156 int      MED::getNumberOfMeshes ( void ) const {
157
158   const char * LOC = "MED::getNumberOfMeshes ( void ) const : ";
159   BEGIN_OF(LOC);
160
161   return _meshes.size();
162
163   END_OF(LOC);
164 };   
165     
166 int      MED::getNumberOfFields ( void ) const {
167
168   const char * LOC = "MED::getNumberOfFields ( void ) const : ";
169   BEGIN_OF(LOC);
170
171   //  return _meshName.size();
172   return _fields.size(); // we get number of field with different name
173
174   END_OF(LOC);
175 };       
176
177 void MED::getMeshNames      ( string * meshNames ) const {
178
179   const char * LOC = "MED::getMeshNames ( string * ) const : ";
180   BEGIN_OF(LOC);
181   int meshNamesSize;
182   
183   if (  ( meshNamesSize = sizeof(meshNames) / sizeof(string *) )
184        != _meshes.size() )
185     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
186                                      << "Size of parameter meshNames is |" 
187                                      << meshNamesSize    << "| and should be |" 
188                                      << _meshes.size() << "| and should be |" 
189                                      )
190                           );   
191   
192   // REM : ALLOCATION D'UN TABLEAU DE POINTEURS SUR STRING FAITE PAR LE CLIENT
193   map<MESH_NAME_,MESH*>::const_iterator  currentMesh; // ??ITERATEUR CONST SUR UN OBJET NON CONST ??
194
195   int meshNamesIndex = 0;
196
197   for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
198     meshNames[meshNamesIndex]=(*currentMesh).first;
199     meshNamesIndex++;                               // CF OPTIMISATION
200   }
201
202   END_OF(LOC);
203 };
204
205 deque<string> MED::getMeshNames      () const {
206   
207   const char * LOC = "MED::getMeshNames () const : ";
208   BEGIN_OF(LOC);
209
210   deque<string> meshNames(_meshes.size());
211   
212   map<MESH_NAME_,MESH*>::const_iterator  currentMesh; // ??ITERATEUR CONST SUR UN OBJET NON CONST ??
213
214   int meshNamesIndex = 0;
215
216   for ( currentMesh=_meshes.begin();currentMesh != _meshes.end(); currentMesh++ ) {
217     meshNames[meshNamesIndex]=(*currentMesh).first;
218     meshNamesIndex++;                               // CF OPTIMISATION
219   }
220
221   END_OF(LOC);
222   return meshNames ;
223 };
224
225 MESH   * MED::getMesh           ( const string & meshName )  const {
226
227   const char * LOC = "MED::getMesh ( const string & meshName ) const : ";
228   BEGIN_OF(LOC);
229
230   map<MESH_NAME_,MESH*>::const_iterator itMeshes =  _meshes.find(meshName);
231
232   //  if ( itMeshes == _meshName.end() )
233   if ( itMeshes == _meshes.end() )
234     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
235                                      << "There is no known mesh named |" 
236                                      << meshName << "|"
237                                      )
238                           );
239   
240   return (*itMeshes).second;
241   
242   END_OF(LOC);
243 }
244
245 MESH   * MED::getMesh           (const FIELD_ * const field ) const {
246  
247   const char * LOC = "MED::getMesh ( const FIELD * field ) const : ";
248   BEGIN_OF(LOC);
249
250   FIELD_ * f = const_cast< FIELD_* > (field);     //  Comment faire mieux ?
251   map<FIELD_ *, MESH_NAME_>::const_iterator itMeshName = _meshName.find(f);
252
253   if ( itMeshName  == _meshName.end() )
254     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
255                                      << "There is no known field associated with |" 
256                                      << field << "| pointer"
257                                      )
258                           );   
259   
260   string meshName = (*itMeshName).second;
261   map<MESH_NAME_,MESH*>::const_iterator itMeshes =  _meshes.find(meshName);
262   if ( itMeshes == _meshes.end() )
263     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
264                                      << "There is no known mesh named |"
265                                      << meshName << " while it's associated with the found field |" 
266                                      << field << "| pointer"
267                                      )
268                           );   
269   
270   return (*itMeshes).second;
271   
272   END_OF(LOC);
273 };
274
275
276 void MED::getFieldNames     ( string * fieldNames ) const {
277
278   const char * LOC = "MED::getFieldNames ( string * ) const : ";
279   BEGIN_OF(LOC);
280
281   int fieldNamesSize =  sizeof(fieldNames) / sizeof(string *);
282  
283   if ( fieldNamesSize != _fields.size() )
284     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
285                                      << "Size of parameter fieldNames is |" 
286                                      << fieldNamesSize     << "| and should be |" 
287                                      << _fields.size() << "| and should be |" 
288                                      )
289                           );   
290   
291   // REM : ALLOCATION D'UN TABLEAU DE POINTEURS SUR STRING FAITE PAR LE CLIENT
292   map<FIELD_NAME_,MAP_DT_IT_>::const_iterator  currentField;
293
294   int fieldNamesIndex = 0;
295   for ( currentField=_fields.begin();currentField != _fields.end(); currentField++ ) {
296     fieldNames[fieldNamesIndex]=(*currentField).first;
297     fieldNamesIndex++;                               // CF OPTIMISATION
298   }
299
300   END_OF(LOC);
301
302 };
303
304 deque<string> MED::getFieldNames     () const {
305
306   const char * LOC = "MED::getFieldNames ( ) const : ";
307   BEGIN_OF(LOC);
308
309   deque<string> fieldNames(_fields.size());
310
311   map<FIELD_NAME_,MAP_DT_IT_>::const_iterator  currentField;
312
313   int fieldNamesIndex = 0;
314
315   for ( currentField=_fields.begin();currentField != _fields.end(); currentField++ ) {
316     fieldNames[fieldNamesIndex]=(*currentField).first;
317     fieldNamesIndex++;                               // CF OPTIMISATION
318   }
319
320   END_OF(LOC);
321   return fieldNames ;
322 };
323
324 deque<DT_IT_> MED::getFieldIteration (const string & fieldName) const {
325
326   const char * LOC = "MED::getFieldIteration ( const string & ) const : ";
327   BEGIN_OF(LOC);
328
329   map<FIELD_NAME_,MAP_DT_IT_>::const_iterator itFields = _fields.find(fieldName);
330   
331   if ( itFields == _fields.end() ) 
332     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
333                                      << "There is no known field named |" 
334                                      << fieldName << "|"
335                                      )
336                           );   
337   //  const MAP_DT_IT_ & myIterationMap =  const_cast<const MAP_DT_IT_ & > ((*itFields).second);
338   const MAP_DT_IT_ & myIterationMap =  (*itFields).second ;
339   MAP_DT_IT_::const_iterator currentIterator ;
340   
341   deque<DT_IT_> Iteration(myIterationMap.size());
342   
343   int iterationIndex = 0;
344   
345   for ( currentIterator=myIterationMap.begin();currentIterator != myIterationMap.end(); currentIterator++ ) {
346     Iteration[iterationIndex]=(*currentIterator).first;
347     iterationIndex++;                               // CF OPTIMISATION
348   }
349
350   END_OF(LOC);
351   return Iteration ;
352 };
353
354 FIELD_  * MED::getField          ( const string & fieldName, const int dt=MED_NOPDT, const int it=MED_NOPDT ) const {
355
356   const char * LOC = "MED::getField ( const string &, const int, const int ) const : ";
357   BEGIN_OF(LOC);
358
359   MESSAGE(LOC << "fieldName = "<<fieldName<<", dt ="<<dt<<", it = "<<it);
360
361   DT_IT_ dtIt;
362
363   dtIt.dt= dt;
364   dtIt.it= it;
365
366   map<FIELD_NAME_,MAP_DT_IT_>::const_iterator itFields = _fields.find(fieldName);
367
368   if ( itFields == _fields.end() ) 
369     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
370                                      << "There is no known field named |" 
371                                      << fieldName << "|"
372                                      )
373                           );   
374
375   const MAP_DT_IT_ & map_dtIt =  const_cast<const MAP_DT_IT_ & > ((*itFields).second);
376   MAP_DT_IT_::const_iterator itMap_dtIt =  map_dtIt.find(dtIt);
377
378   if ( itMap_dtIt == map_dtIt.end() )
379     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
380                                      << "There is no (dt,it) |("
381                                      << dt << "," << it << ")| associated with the found field |" 
382                                      << fieldName << "|"
383                                      )
384                           );   
385
386   END_OF(LOC);
387
388   //return _fields[fieldName][dtIt];
389   return (*itMap_dtIt).second;
390   
391 };
392
393
394 // fiend ostream & MED::operator<<(ostream &os,const MED & med) const {
395 //   return os;
396 // };
397
398
399 const map<MED_FR::med_entite_maillage,SUPPORT*> & MED::getSupports(const string & meshName) const
400 {
401   const char * LOC = "MED::getSupports ( const string ) const : ";
402   BEGIN_OF(LOC);
403
404   map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::const_iterator itSupportOnMesh = _support.find(meshName) ;
405   
406   if ( itSupportOnMesh == _support.end() )
407     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
408                                      << "There is no support on mesh named |" 
409                                      << meshName << "|"
410                                      )
411                           );
412   END_OF(LOC);
413   return (*itSupportOnMesh).second ;
414 }
415
416 SUPPORT *  MED::getSupport (const string & meshName,MED_FR::med_entite_maillage entity) const 
417 {
418   const char * LOC = "MED::getSupport ( const string, MED_FR::med_entite_maillage ) const : ";
419   BEGIN_OF(LOC);
420
421   map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::const_iterator itSupportOnMesh = _support.find(meshName) ;
422   
423   if ( itSupportOnMesh == _support.end() )
424     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
425                                      << "There is no support on mesh named |" 
426                                      << meshName << "|"
427                                      )
428                           );
429  
430   map<MED_FR::med_entite_maillage,SUPPORT *> & SupportOnMesh = (map<MED_FR::med_entite_maillage,SUPPORT *>&) ((*itSupportOnMesh).second) ;
431   map<MED_FR::med_entite_maillage,SUPPORT *>::const_iterator itSupport = SupportOnMesh.find(entity) ;
432   
433   if (itSupport == SupportOnMesh.end() )
434     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
435                                      << "There is no support on entity "
436                                      << entity << " in mesh named |" 
437                                      << meshName << "|"
438                                      )
439                           );
440   END_OF(LOC);
441   return (*itSupport).second ;
442 };
443
444 void MED::updateSupport ()
445 {
446  
447   const char * LOC = "MED::updateSupport () : ";
448   BEGIN_OF(LOC);
449
450   map<MESH_NAME_, map<MED_FR::med_entite_maillage,SUPPORT *> >::iterator itSupportOnMesh ;
451   for ( itSupportOnMesh=_support.begin();itSupportOnMesh != _support.end(); itSupportOnMesh++ ) {
452     map<MED_FR::med_entite_maillage,SUPPORT *>::iterator itSupport ;
453     for ( itSupport=(*itSupportOnMesh).second.begin();itSupport!=(*itSupportOnMesh).second.end();itSupport++)
454       try {
455         (*itSupport).second->update() ;
456       }
457       catch (MEDEXCEPTION & ex) {
458         // entity not defined in mesh -> we remove support on it !
459         delete (*itSupport).second ;
460         (*itSupportOnMesh).second.erase(itSupport) ; // that's rigth ????
461       }
462   }
463
464   END_OF(LOC);
465 }
466
467 void MED::addMesh(const MESH * ptrMesh) throw (MEDEXCEPTION)
468 {
469   const char * LOC = "MED::addMesh(const MESH * ptrMesh): ";
470
471   throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "WARNING method not yet implemented"));
472 }
473
474 void MED::addField(const FIELD_ * const ptrField) throw (MEDEXCEPTION)
475 {
476   const char * LOC = "MED::addField(const FIELD_ * const ptrField): ";
477
478   throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "WARNING method not yet implemented"));
479 }