]> SALOME platform Git repositories - modules/paravis.git/blob - src/Plugins/MedReader/IO/vtkMedDriver.cxx
Salome HOME
Merge from BR_PORTING_VTK6 01/03/2013
[modules/paravis.git] / src / Plugins / MedReader / IO / vtkMedDriver.cxx
1 #include "vtkMedDriver.h"
2
3 #include "vtkObjectFactory.h"
4 #include "vtkStringArray.h"
5 #include "vtkDataArray.h"
6 #include "vtkIdTypeArray.h"
7 #include "vtkMath.h"
8
9 #include "vtkMedFile.h"
10 #include "vtkMedCartesianGrid.h"
11 #include "vtkMedPolarGrid.h"
12 #include "vtkMedCurvilinearGrid.h"
13 #include "vtkMedUnstructuredGrid.h"
14 #include "vtkMedField.h"
15 #include "vtkMedMesh.h"
16 #include "vtkMedFamily.h"
17 #include "vtkMedUtilities.h"
18 #include "vtkMedEntityArray.h"
19 #include "vtkMedLocalization.h"
20 #include "vtkMedProfile.h"
21 #include "vtkMedFieldOverEntity.h"
22 #include "vtkMedFieldStep.h"
23 #include "vtkMedGroup.h"
24 #include "vtkMedIntArray.h"
25 #include "vtkMedLink.h"
26
27 #ifdef MedReader_HAVE_PARALLEL_INFRASTRUCTURE
28 #include "vtkMultiProcessController.h"
29 #include "vtkMPIController.h"
30 #include <vtkMPICommunicator.h>
31 #include <vtkMPI.h>
32 #endif
33
34 vtkCxxSetObjectMacro(vtkMedDriver, MedFile, vtkMedFile);
35
36 //vtkCxxRevisionMacro(vtkMedDriver, "$Revision$")
37 vtkStandardNewMacro(vtkMedDriver)
38
39 vtkMedDriver::vtkMedDriver()
40 {
41   this->MedFile = NULL;
42   this->OpenLevel = 0;
43   this->FileId = -1;
44 }
45
46 vtkMedDriver::~vtkMedDriver()
47 {
48   if (this->OpenLevel > 0)
49     {
50     vtkWarningMacro("The file has not be closed before destructor.")
51     this->OpenLevel = 1;
52     this->Close();
53     }
54   this->SetMedFile(NULL);
55 }
56
57 int vtkMedDriver::RestrictedOpen()
58 {
59   int res = 0;
60   if (this->MedFile == NULL || this->MedFile->GetFileName() == NULL)
61     {
62     vtkDebugMacro("Error : FileName has not been set ");
63     return -1;
64     }
65
66   if (this->OpenLevel <= 0)
67     {
68
69     med_bool hdfok;
70     med_bool medok;
71
72     med_err conforme = MEDfileCompatibility(this->MedFile->GetFileName(),
73                                             &hdfok, &medok);
74     if (!hdfok)
75       {
76       vtkErrorMacro("The file " << this->MedFile->GetFileName()
77           << " is not a HDF5 file, aborting.");
78       return -1;
79       }
80
81     if (!medok)
82       {
83       vtkErrorMacro("The file " << this->MedFile->GetFileName()
84           << " has not been written with the"
85           << " same version as the one currently used to read it, this may lead"
86           << " to errors. Please use the medimport tool.");
87       return -1;
88       }
89
90     if(conforme < 0)
91       {
92       vtkErrorMacro("The file " << this->MedFile->GetFileName()
93                     << " is not compatible, please import it to the new version using medimport.");
94       return -1;
95       }
96
97     this->FileId = MEDfileOpen(this->MedFile->GetFileName(), MED_ACC_RDONLY);
98     if (this->FileId < 0)
99       {
100       vtkDebugMacro("Error : unable to open file "
101                     << this->MedFile->GetFileName());
102       res = -2;
103       }
104     this->OpenLevel = 0;
105
106     } // OpenLevel
107   this->OpenLevel++;
108   this->ParallelFileId = -1;
109   return res;
110 }
111
112 int vtkMedDriver::Open()
113 {
114   int res = 0;
115   if (this->MedFile == NULL || this->MedFile->GetFileName() == NULL)
116     {
117     vtkDebugMacro("Error : FileName has not been set ");
118     return -1;
119     }
120
121   if (this->OpenLevel <= 0)
122     {
123
124     med_bool hdfok;
125     med_bool medok;
126
127     med_err conforme = MEDfileCompatibility(this->MedFile->GetFileName(),
128                                             &hdfok, &medok);
129     if (!hdfok)
130       {
131       vtkErrorMacro("The file " << this->MedFile->GetFileName()
132           << " is not a HDF5 file, aborting.");
133       return -1;
134       }
135
136     if (!medok)
137       {
138       vtkErrorMacro("The file " << this->MedFile->GetFileName()
139           << " has not been written with the"
140           << " same version as the one currently used to read it, this may lead"
141           << " to errors. Please use the medimport tool.");
142       return -1;
143       }
144
145     if(conforme < 0)
146       {
147       vtkErrorMacro("The file " << this->MedFile->GetFileName()
148                     << " is not compatible, please import it to the new version using medimport.");
149       return -1;
150       }
151
152     this->FileId = MEDfileOpen(this->MedFile->GetFileName(), MED_ACC_RDONLY);
153     if (this->FileId < 0)
154       {
155       vtkDebugMacro("Error : unable to open file "
156                     << this->MedFile->GetFileName());
157       res = -2;
158       }
159     this->OpenLevel = 0;
160
161     this->ParallelFileId = -1;
162
163 #ifdef MedReader_HAVE_PARALLEL_INFRASTRUCTURE
164     // the following code opens the file in parallel
165     vtkMultiProcessController* controller =
166                 vtkMultiProcessController::GetGlobalController();
167     int lpID = 0;
168     if (controller == NULL)
169       {
170     return -3;
171       }
172     else
173       {
174       lpID = controller->GetLocalProcessId();
175       }
176
177     vtkMPICommunicator* commu = vtkMPICommunicator::SafeDownCast(
178                   controller->GetCommunicator() );
179     if (commu == NULL)
180       {
181       //vtkErrorMacro("Communicator is NULL in Open");
182       return -3;
183       }
184     MPI_Comm* mpi_com = NULL;
185     mpi_com = commu->GetMPIComm()->GetHandle();
186     if (mpi_com == NULL)
187       {
188       vtkErrorMacro("MPI communicator is NULL in Open");
189       return -3;
190       }
191
192     if (controller->GetNumberOfProcesses() > 1)
193       {
194       int major, minor, release;
195       if (MEDfileNumVersionRd(this->FileId, &major, &minor, &release) < 0)
196         {
197         vtkErrorMacro("Impossible to read the version of this file");
198         return -1;
199         }
200
201     if (major >= 3)
202       {
203         this->ParallelFileId = MEDparFileOpen(this->MedFile->GetFileName(),
204                             MED_ACC_RDONLY,
205                             *mpi_com,
206                             MPI_INFO_NULL);
207         }
208     else
209         {
210         vtkErrorMacro("Parallel access is not allowed in MED files prior to version 3");
211         return -1;
212         }
213       }
214
215     if (this->ParallelFileId < 0)
216       {
217       vtkDebugMacro("Error : unable to parallel-open file "
218                     << this->MedFile->GetFileName());
219       }
220 #endif
221
222     } // OpenLevel
223   this->OpenLevel++;
224   return res;
225 }
226
227 void vtkMedDriver::Close()
228 {
229   this->OpenLevel--;
230   if (this->OpenLevel == 0)
231     {
232     if (MEDfileClose(this->FileId) < 0)
233       {
234       vtkErrorMacro("Error: unable to close the current file.");
235       }
236     this->FileId = -1;
237
238     if (this->ParallelFileId != -1)
239     {
240       if (MEDfileClose(this->ParallelFileId) < 0)
241       {
242       vtkErrorMacro("Error: unable to parallel-close the current file.");
243       }
244     }
245     this->ParallelFileId = -1;
246     }
247 }
248
249 bool vtkMedDriver::CanReadFile()
250 {
251   bool canRead = (this->RestrictedOpen() >= 0);
252   this->Close();
253   return canRead;
254 }
255
256 void vtkMedDriver::ReadFileVersion(int* major, int* minor, int* release)
257 {
258   FileRestrictedOpen open(this);
259
260   med_int amajor, aminor, arelease;
261   if (MEDfileNumVersionRd(this->FileId, &amajor, &aminor, &arelease) < 0)
262     {
263     vtkErrorMacro("Impossible to read the version of this file");
264     return;
265     }
266   *major = amajor;
267   *minor = aminor;
268   *release = arelease;
269 }
270
271 void vtkMedDriver::ReadRegularGridInformation(vtkMedRegularGrid* grid)
272 {
273   vtkErrorMacro("vtkMedDriver::ReadInformation not Implemented !");
274   return;
275 }
276
277 void vtkMedDriver::ReadCurvilinearGridInformation(vtkMedCurvilinearGrid* grid)
278 {
279   vtkErrorMacro("vtkMedDriver::ReadInformation not Implemented !");
280   return;
281 }
282
283 void vtkMedDriver::ReadUnstructuredGridInformation(vtkMedUnstructuredGrid* grid)
284 {
285   vtkErrorMacro("vtkMedDriver::ReadInformation not Implemented !");
286   return;
287 }
288
289 // Description:
290 // load all Information data associated with this standard grid.
291 void vtkMedDriver::ReadGridInformation(vtkMedGrid* grid)
292 {
293   if(vtkMedRegularGrid::SafeDownCast(grid) != NULL)
294     {
295     this->ReadRegularGridInformation(vtkMedRegularGrid::SafeDownCast(grid));
296     }
297   if(vtkMedCurvilinearGrid::SafeDownCast(grid) != NULL)
298     {
299     this->ReadCurvilinearGridInformation(vtkMedCurvilinearGrid::SafeDownCast(grid));
300     }
301   if(vtkMedUnstructuredGrid::SafeDownCast(grid) != NULL)
302     {
303     this->ReadUnstructuredGridInformation(vtkMedUnstructuredGrid::SafeDownCast(grid));
304     }
305 }
306
307 void vtkMedDriver::ReadFamilyInformation(vtkMedMesh* mesh, vtkMedFamily* family)
308 {
309   vtkErrorMacro("vtkMedDriver::ReadFamilyInformation not Implemented !");
310   return;
311 }
312
313 void vtkMedDriver::ReadFileInformation(vtkMedFile* file)
314 {
315   vtkErrorMacro("vtkMedDriver::ReadFileInformation not Implemented !");
316   return;
317 }
318
319 void vtkMedDriver::ReadProfileInformation(vtkMedProfile* profile)
320 {
321   vtkErrorMacro("vtkMedDriver::ReadProfileInformation not Implemented !");
322   return;
323 }
324
325 void vtkMedDriver::ReadFieldInformation(vtkMedField* field)
326 {
327   vtkErrorMacro("vtkMedDriver::ReadFieldInformation not Implemented !");
328   return;
329 }
330
331 void vtkMedDriver::ReadFieldOverEntityInformation(vtkMedFieldOverEntity* fieldOverEntity)
332 {
333   vtkErrorMacro("vtkMedDriver::ReadFieldOverEntityInformation not Implemented !");
334   return;
335 }
336
337 void vtkMedDriver::ReadMeshInformation(vtkMedMesh* mesh)
338 {
339   vtkErrorMacro("vtkMedDriver::ReadMeshInformation not Implemented !");
340   return;
341 }
342
343 void vtkMedDriver::ReadLocalizationInformation(vtkMedLocalization* loc)
344 {
345   vtkErrorMacro("vtkMedDriver::ReadLocalizationInformation not Implemented !");
346   return;
347 }
348
349 void vtkMedDriver::ReadInterpolationInformation(vtkMedInterpolation* interp)
350 {
351   vtkErrorMacro("vtkMedDriver::ReadInterpolationInformation not Implemented !");
352   return;
353 }
354
355 void vtkMedDriver::ReadFieldStepInformation(vtkMedFieldStep* step, bool readAllEntityInfo)
356 {
357   vtkErrorMacro("vtkMedDriver::ReadFieldStepInformation not Implemented !");
358   return;
359 }
360
361 void vtkMedDriver::ReadFieldOnProfileInformation(vtkMedFieldOnProfile* fop)
362 {
363   vtkErrorMacro("vtkMedDriver::ReadFieldOnProfileInformation not Implemented !");
364   return;
365 }
366
367 void vtkMedDriver::ReadStructElementInformation(
368     vtkMedStructElement*)
369 {
370   vtkErrorMacro("vtkMedDriver::ReadStructElementInformation not Implemented !");
371   return;
372 }
373
374 void vtkMedDriver::ReadSupportMeshInformation(
375     vtkMedMesh*)
376 {
377   vtkErrorMacro("vtkMedDriver::ReadSupportMeshInformation not Implemented !");
378   return;
379 }
380
381 void vtkMedDriver::ReadConstantAttributeInformation(vtkMedConstantAttribute*)
382 {
383   vtkErrorMacro("vtkMedDriver::ReadConstantAttributeInformation not Implemented !");
384   return;
385 }
386
387 void vtkMedDriver::ReadVariableAttributeInformation(vtkMedVariableAttribute*)
388 {
389   vtkErrorMacro("vtkMedDriver::ReadVariableAttributeInformation not Implemented !");
390   return;
391 }
392
393 void vtkMedDriver::LoadPointGlobalIds(vtkMedGrid* grid)
394 {
395   vtkErrorMacro("vtkMedDriver::LoadPointGlobalIds not Implemented !");
396   return;
397 }
398
399 void vtkMedDriver::LoadFamilyIds(vtkMedEntityArray* array)
400 {
401   vtkErrorMacro("vtkMedDriver::LoadFamilyIds not Implemented !");
402   return;
403 }
404
405 void vtkMedDriver::LoadCoordinates(vtkMedGrid* grid)
406 {
407   vtkErrorMacro("vtkMedDriver::LoadCoordinates not Implemented !");
408   return;
409 }
410
411 void vtkMedDriver::LoadProfile(vtkMedProfile* profile)
412 {
413   vtkErrorMacro("vtkMedDriver::LoadProfile not Implemented !");
414   return;
415 }
416
417 void vtkMedDriver::LoadConnectivity(vtkMedEntityArray* array)
418 {
419   vtkErrorMacro("vtkMedDriver::LoadConnectivity not Implemented !");
420   return;
421 }
422
423 void vtkMedDriver::LoadCellGlobalIds(vtkMedEntityArray* array)
424 {
425   vtkErrorMacro("vtkMedDriver::LoadGlobalIds not Implemented !");
426   return;
427 }
428
429 void vtkMedDriver::LoadField(vtkMedFieldOnProfile* foe, med_storage_mode mode)
430 {
431   vtkErrorMacro("vtkMedDriver::LoadFieldOnProfile not Implemented !");
432   return;
433 }
434
435 void vtkMedDriver::LoadVariableAttribute(vtkMedVariableAttribute*,
436                                          vtkMedEntityArray*)
437 {
438   vtkErrorMacro("vtkMedDriver::LoadVariableAttribute not Implemented !");
439   return;
440 }
441
442 void vtkMedDriver::PrintSelf(ostream& os, vtkIndent indent)
443 {
444   this->Superclass::PrintSelf(os, indent);
445   PRINT_IVAR(os, indent, OpenLevel);
446   PRINT_IVAR(os, indent, FileId);
447 }