Back to index

salome-paravis  6.5.0
vtkMedDriver.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2010-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "vtkMedDriver.h"
00021 
00022 #include "vtkObjectFactory.h"
00023 #include "vtkStringArray.h"
00024 #include "vtkDataArray.h"
00025 #include "vtkIdTypeArray.h"
00026 #include "vtkMath.h"
00027 
00028 #include "vtkMedFile.h"
00029 #include "vtkMedCartesianGrid.h"
00030 #include "vtkMedPolarGrid.h"
00031 #include "vtkMedCurvilinearGrid.h"
00032 #include "vtkMedUnstructuredGrid.h"
00033 #include "vtkMedField.h"
00034 #include "vtkMedMesh.h"
00035 #include "vtkMedFamily.h"
00036 #include "vtkMedUtilities.h"
00037 #include "vtkMedEntityArray.h"
00038 #include "vtkMedLocalization.h"
00039 #include "vtkMedProfile.h"
00040 #include "vtkMedFieldOverEntity.h"
00041 #include "vtkMedFieldStep.h"
00042 #include "vtkMedGroup.h"
00043 #include "vtkMedIntArray.h"
00044 #include "vtkMedLink.h"
00045 
00046 #ifdef MedReader_HAVE_PARALLEL_INFRASTRUCTURE
00047 #include "vtkMultiProcessController.h"
00048 #include "vtkMPIController.h"
00049 #include <vtkMPICommunicator.h>
00050 #include <vtkMPI.h>
00051 #endif
00052 
00053 vtkCxxSetObjectMacro(vtkMedDriver, MedFile, vtkMedFile);
00054 
00055 vtkCxxRevisionMacro(vtkMedDriver, "$Revision: 1.1.4.8.2.1 $")
00056 vtkStandardNewMacro(vtkMedDriver)
00057 
00058 vtkMedDriver::vtkMedDriver()
00059 {
00060   this->MedFile = NULL;
00061   this->OpenLevel = 0;
00062   this->FileId = -1;
00063 }
00064 
00065 vtkMedDriver::~vtkMedDriver()
00066 {
00067   if (this->OpenLevel > 0)
00068     {
00069     vtkWarningMacro("The file has not be closed before destructor.")
00070     this->OpenLevel = 1;
00071     this->Close();
00072     }
00073   this->SetMedFile(NULL);
00074 }
00075 
00076 int vtkMedDriver::RestrictedOpen()
00077 {
00078   int res = 0;
00079   if (this->MedFile == NULL || this->MedFile->GetFileName() == NULL)
00080     {
00081     vtkDebugMacro("Error : FileName has not been set ");
00082     return -1;
00083     }
00084 
00085   if (this->OpenLevel <= 0)
00086     {
00087 
00088     med_bool hdfok;
00089     med_bool medok;
00090 
00091     med_err conforme = MEDfileCompatibility(this->MedFile->GetFileName(),
00092                                             &hdfok, &medok);
00093     if (!hdfok)
00094       {
00095       vtkErrorMacro("The file " << this->MedFile->GetFileName()
00096           << " is not a HDF5 file, aborting.");
00097       return -1;
00098       }
00099 
00100     if (!medok)
00101       {
00102       vtkErrorMacro("The file " << this->MedFile->GetFileName()
00103           << " has not been written with the"
00104           << " same version as the one currently used to read it, this may lead"
00105           << " to errors. Please use the medimport tool.");
00106       return -1;
00107       }
00108 
00109     if(conforme < 0)
00110       {
00111       vtkErrorMacro("The file " << this->MedFile->GetFileName()
00112                     << " is not compatible, please import it to the new version using medimport.");
00113       return -1;
00114       }
00115 
00116     this->FileId = MEDfileOpen(this->MedFile->GetFileName(), MED_ACC_RDONLY);
00117     if (this->FileId < 0)
00118       {
00119       vtkDebugMacro("Error : unable to open file "
00120                     << this->MedFile->GetFileName());
00121       res = -2;
00122       }
00123     this->OpenLevel = 0;
00124 
00125     } // OpenLevel
00126   this->OpenLevel++;
00127   this->ParallelFileId = -1;
00128   return res;
00129 }
00130 
00131 int vtkMedDriver::Open()
00132 {
00133   int res = 0;
00134   if (this->MedFile == NULL || this->MedFile->GetFileName() == NULL)
00135     {
00136     vtkDebugMacro("Error : FileName has not been set ");
00137     return -1;
00138     }
00139 
00140   if (this->OpenLevel <= 0)
00141     {
00142 
00143     med_bool hdfok;
00144     med_bool medok;
00145 
00146     med_err conforme = MEDfileCompatibility(this->MedFile->GetFileName(),
00147                                             &hdfok, &medok);
00148     if (!hdfok)
00149       {
00150       vtkErrorMacro("The file " << this->MedFile->GetFileName()
00151           << " is not a HDF5 file, aborting.");
00152       return -1;
00153       }
00154 
00155     if (!medok)
00156       {
00157       vtkErrorMacro("The file " << this->MedFile->GetFileName()
00158           << " has not been written with the"
00159           << " same version as the one currently used to read it, this may lead"
00160           << " to errors. Please use the medimport tool.");
00161       return -1;
00162       }
00163 
00164     if(conforme < 0)
00165       {
00166       vtkErrorMacro("The file " << this->MedFile->GetFileName()
00167                     << " is not compatible, please import it to the new version using medimport.");
00168       return -1;
00169       }
00170 
00171     this->FileId = MEDfileOpen(this->MedFile->GetFileName(), MED_ACC_RDONLY);
00172     if (this->FileId < 0)
00173       {
00174       vtkDebugMacro("Error : unable to open file "
00175                     << this->MedFile->GetFileName());
00176       res = -2;
00177       }
00178     this->OpenLevel = 0;
00179 
00180     this->ParallelFileId = -1;
00181 
00182 #ifdef MedReader_HAVE_PARALLEL_INFRASTRUCTURE
00183     // the following code opens the file in parallel
00184     vtkMultiProcessController* controller =
00185               vtkMultiProcessController::GetGlobalController();
00186     int lpID = 0;
00187     if (controller == NULL)
00188       {
00189     return -3;
00190       }
00191     else
00192       {
00193       lpID = controller->GetLocalProcessId();
00194       }
00195 
00196     vtkMPICommunicator* commu = vtkMPICommunicator::SafeDownCast(
00197                   controller->GetCommunicator() );
00198     if (commu == NULL)
00199       {
00200       //vtkErrorMacro("Communicator is NULL in Open");
00201       return -3;
00202       }
00203     MPI_Comm* mpi_com = NULL;
00204     mpi_com = commu->GetMPIComm()->GetHandle();
00205     if (mpi_com == NULL)
00206       {
00207       vtkErrorMacro("MPI communicator is NULL in Open");
00208       return -3;
00209       }
00210 
00211     if (controller->GetNumberOfProcesses() > 1)
00212       {
00213       int major, minor, release;
00214       if (MEDfileNumVersionRd(this->FileId, &major, &minor, &release) < 0)
00215         {
00216         vtkErrorMacro("Impossible to read the version of this file");
00217         return -1;
00218         }
00219 
00220     if (major >= 3)
00221       {
00222         this->ParallelFileId = MEDparFileOpen(this->MedFile->GetFileName(),
00223                             MED_ACC_RDONLY,
00224                             *mpi_com,
00225                             MPI_INFO_NULL);
00226         }
00227     else
00228         {
00229         vtkErrorMacro("Parallel access is not allowed in MED files prior to version 3");
00230         return -1;
00231         }
00232       }
00233 
00234     if (this->ParallelFileId < 0)
00235       {
00236       vtkDebugMacro("Error : unable to parallel-open file "
00237                     << this->MedFile->GetFileName());
00238       }
00239 #endif
00240 
00241     } // OpenLevel
00242   this->OpenLevel++;
00243   return res;
00244 }
00245 
00246 void vtkMedDriver::Close()
00247 {
00248   this->OpenLevel--;
00249   if (this->OpenLevel == 0)
00250     {
00251     if (MEDfileClose(this->FileId) < 0)
00252       {
00253       vtkErrorMacro("Error: unable to close the current file.");
00254       }
00255     this->FileId = -1;
00256 
00257     if (this->ParallelFileId != -1)
00258     {
00259       if (MEDfileClose(this->ParallelFileId) < 0)
00260       {
00261       vtkErrorMacro("Error: unable to parallel-close the current file.");
00262       }
00263     }
00264     this->ParallelFileId = -1;
00265     }
00266 }
00267 
00268 bool vtkMedDriver::CanReadFile()
00269 {
00270   bool canRead = (this->RestrictedOpen() >= 0);
00271   this->Close();
00272   return canRead;
00273 }
00274 
00275 void vtkMedDriver::ReadFileVersion(int* major, int* minor, int* release)
00276 {
00277   FileRestrictedOpen open(this);
00278 
00279   med_int amajor, aminor, arelease;
00280   if (MEDfileNumVersionRd(this->FileId, &amajor, &aminor, &arelease) < 0)
00281     {
00282     vtkErrorMacro("Impossible to read the version of this file");
00283     return;
00284     }
00285   *major = amajor;
00286   *minor = aminor;
00287   *release = arelease;
00288 }
00289 
00290 void vtkMedDriver::ReadRegularGridInformation(vtkMedRegularGrid* grid)
00291 {
00292   vtkErrorMacro("vtkMedDriver::ReadInformation not Implemented !");
00293   return;
00294 }
00295 
00296 void vtkMedDriver::ReadCurvilinearGridInformation(vtkMedCurvilinearGrid* grid)
00297 {
00298   vtkErrorMacro("vtkMedDriver::ReadInformation not Implemented !");
00299   return;
00300 }
00301 
00302 void vtkMedDriver::ReadUnstructuredGridInformation(vtkMedUnstructuredGrid* grid)
00303 {
00304   vtkErrorMacro("vtkMedDriver::ReadInformation not Implemented !");
00305   return;
00306 }
00307 
00308 // Description:
00309 // load all Information data associated with this standard grid.
00310 void vtkMedDriver::ReadGridInformation(vtkMedGrid* grid)
00311 {
00312   if(vtkMedRegularGrid::SafeDownCast(grid) != NULL)
00313     {
00314     this->ReadRegularGridInformation(vtkMedRegularGrid::SafeDownCast(grid));
00315     }
00316   if(vtkMedCurvilinearGrid::SafeDownCast(grid) != NULL)
00317     {
00318     this->ReadCurvilinearGridInformation(vtkMedCurvilinearGrid::SafeDownCast(grid));
00319     }
00320   if(vtkMedUnstructuredGrid::SafeDownCast(grid) != NULL)
00321     {
00322     this->ReadUnstructuredGridInformation(vtkMedUnstructuredGrid::SafeDownCast(grid));
00323     }
00324 }
00325 
00326 void vtkMedDriver::ReadFamilyInformation(vtkMedMesh* mesh, vtkMedFamily* family)
00327 {
00328   vtkErrorMacro("vtkMedDriver::ReadFamilyInformation not Implemented !");
00329   return;
00330 }
00331 
00332 void vtkMedDriver::ReadFileInformation(vtkMedFile* file)
00333 {
00334   vtkErrorMacro("vtkMedDriver::ReadFileInformation not Implemented !");
00335   return;
00336 }
00337 
00338 void vtkMedDriver::ReadProfileInformation(vtkMedProfile* profile)
00339 {
00340   vtkErrorMacro("vtkMedDriver::ReadProfileInformation not Implemented !");
00341   return;
00342 }
00343 
00344 void vtkMedDriver::ReadFieldInformation(vtkMedField* field)
00345 {
00346   vtkErrorMacro("vtkMedDriver::ReadFieldInformation not Implemented !");
00347   return;
00348 }
00349 
00350 void vtkMedDriver::ReadFieldOverEntityInformation(vtkMedFieldOverEntity* fieldOverEntity)
00351 {
00352   vtkErrorMacro("vtkMedDriver::ReadFieldOverEntityInformation not Implemented !");
00353   return;
00354 }
00355 
00356 void vtkMedDriver::ReadMeshInformation(vtkMedMesh* mesh)
00357 {
00358   vtkErrorMacro("vtkMedDriver::ReadMeshInformation not Implemented !");
00359   return;
00360 }
00361 
00362 void vtkMedDriver::ReadLocalizationInformation(vtkMedLocalization* loc)
00363 {
00364   vtkErrorMacro("vtkMedDriver::ReadLocalizationInformation not Implemented !");
00365   return;
00366 }
00367 
00368 void vtkMedDriver::ReadInterpolationInformation(vtkMedInterpolation* interp)
00369 {
00370   vtkErrorMacro("vtkMedDriver::ReadInterpolationInformation not Implemented !");
00371   return;
00372 }
00373 
00374 void vtkMedDriver::ReadFieldStepInformation(vtkMedFieldStep* step, bool readAllEntityInfo)
00375 {
00376   vtkErrorMacro("vtkMedDriver::ReadFieldStepInformation not Implemented !");
00377   return;
00378 }
00379 
00380 void vtkMedDriver::ReadFieldOnProfileInformation(vtkMedFieldOnProfile* fop)
00381 {
00382   vtkErrorMacro("vtkMedDriver::ReadFieldOnProfileInformation not Implemented !");
00383   return;
00384 }
00385 
00386 void vtkMedDriver::ReadStructElementInformation(
00387     vtkMedStructElement*)
00388 {
00389   vtkErrorMacro("vtkMedDriver::ReadStructElementInformation not Implemented !");
00390   return;
00391 }
00392 
00393 void vtkMedDriver::ReadSupportMeshInformation(
00394     vtkMedMesh*)
00395 {
00396   vtkErrorMacro("vtkMedDriver::ReadSupportMeshInformation not Implemented !");
00397   return;
00398 }
00399 
00400 void vtkMedDriver::ReadConstantAttributeInformation(vtkMedConstantAttribute*)
00401 {
00402   vtkErrorMacro("vtkMedDriver::ReadConstantAttributeInformation not Implemented !");
00403   return;
00404 }
00405 
00406 void vtkMedDriver::ReadVariableAttributeInformation(vtkMedVariableAttribute*)
00407 {
00408   vtkErrorMacro("vtkMedDriver::ReadVariableAttributeInformation not Implemented !");
00409   return;
00410 }
00411 
00412 void vtkMedDriver::LoadPointGlobalIds(vtkMedGrid* grid)
00413 {
00414   vtkErrorMacro("vtkMedDriver::LoadPointGlobalIds not Implemented !");
00415   return;
00416 }
00417 
00418 void vtkMedDriver::LoadFamilyIds(vtkMedEntityArray* array)
00419 {
00420   vtkErrorMacro("vtkMedDriver::LoadFamilyIds not Implemented !");
00421   return;
00422 }
00423 
00424 void vtkMedDriver::LoadCoordinates(vtkMedGrid* grid)
00425 {
00426   vtkErrorMacro("vtkMedDriver::LoadCoordinates not Implemented !");
00427   return;
00428 }
00429 
00430 void vtkMedDriver::LoadProfile(vtkMedProfile* profile)
00431 {
00432   vtkErrorMacro("vtkMedDriver::LoadProfile not Implemented !");
00433   return;
00434 }
00435 
00436 void vtkMedDriver::LoadConnectivity(vtkMedEntityArray* array)
00437 {
00438   vtkErrorMacro("vtkMedDriver::LoadConnectivity not Implemented !");
00439   return;
00440 }
00441 
00442 void vtkMedDriver::LoadCellGlobalIds(vtkMedEntityArray* array)
00443 {
00444   vtkErrorMacro("vtkMedDriver::LoadGlobalIds not Implemented !");
00445   return;
00446 }
00447 
00448 void vtkMedDriver::LoadField(vtkMedFieldOnProfile* foe, med_storage_mode mode)
00449 {
00450   vtkErrorMacro("vtkMedDriver::LoadFieldOnProfile not Implemented !");
00451   return;
00452 }
00453 
00454 void vtkMedDriver::LoadVariableAttribute(vtkMedVariableAttribute*,
00455                                          vtkMedEntityArray*)
00456 {
00457   vtkErrorMacro("vtkMedDriver::LoadVariableAttribute not Implemented !");
00458   return;
00459 }
00460 
00461 void vtkMedDriver::PrintSelf(ostream& os, vtkIndent indent)
00462 {
00463   this->Superclass::PrintSelf(os, indent);
00464   PRINT_IVAR(os, indent, OpenLevel);
00465   PRINT_IVAR(os, indent, FileId);
00466 }