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