Back to index

salome-med  6.5.0
MEDMEM_EnsightMedDriver.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "MEDMEM_define.hxx"
00024 #include "MEDMEM_Support.hxx"
00025 #include "MEDMEM_Mesh.hxx"
00026 #include "MEDMEM_Field.hxx"
00027 
00028 #include "MEDMEM_EnsightMedDriver.hxx"
00029 #include "MEDMEM_EnsightFieldDriver.hxx"
00030 #include "MEDMEM_EnsightMeshDriver.hxx"
00031 
00032 using namespace std;
00033 //using namespace MEDMEM;
00034 using namespace MED_EN;
00035 using namespace MEDMEM_ENSIGHT;
00036 
00037 // ================================================================================
00038 // ENSIGHT_MED_DRIVER
00039 // ================================================================================
00040 namespace MEDMEM {
00041 
00042 ENSIGHT_MED_DRIVER::ENSIGHT_MED_DRIVER():_CaseFileDriver_User()
00043 {
00044 }
00045 
00046 ENSIGHT_MED_DRIVER::ENSIGHT_MED_DRIVER(const string & fileName,
00047                                        med_mode_acces accessMode):
00048   _CaseFileDriver_User(fileName,accessMode)
00049 {
00050 }
00051 
00052 ENSIGHT_MED_DRIVER::ENSIGHT_MED_DRIVER(const ENSIGHT_MED_DRIVER & driver):
00053   _CaseFileDriver_User(driver)
00054 {
00055 }
00056 
00057 void ENSIGHT_MED_DRIVER::openConst() const
00058 {
00059   const char * LOC ="ENSIGHT_MED_DRIVER::open() : ";
00060   BEGIN_OF_MED(LOC);
00061 
00062   if ( _fileName.empty() )
00063     throw MED_EXCEPTION
00064       ( LOCALIZED( STRING(LOC) << "_fileName is empty, "
00065                    "please set a correct fileName before calling open()"));
00066 
00067   if (!canOpenFile( _fileName, getAccessMode() ))
00068     throw MED_EXCEPTION
00069       ( LOCALIZED( STRING(LOC) << "Can not open main Ensight file " << _fileName));
00070 
00071   END_OF_MED(LOC);
00072 }
00073 
00074 void ENSIGHT_MED_DRIVER::open()
00075 {
00076   openConst();
00077 }
00078 void ENSIGHT_MED_DRIVER::close()
00079 {
00080 }
00081 
00082 ENSIGHT_MED_DRIVER::~ENSIGHT_MED_DRIVER()
00083 {
00084   MESSAGE_MED("ENSIGHT_MED_DRIVER::~ENSIGHT_MED_DRIVER() has been destroyed");
00085 }
00086 
00087 // ================================================================================
00088 // WRONLY
00089 // ================================================================================
00090 
00091 ENSIGHT_MED_WRONLY_DRIVER::ENSIGHT_MED_WRONLY_DRIVER() : ENSIGHT_MED_DRIVER(), _fields(0)
00092 {
00093 }
00094 
00095 ENSIGHT_MED_WRONLY_DRIVER::ENSIGHT_MED_WRONLY_DRIVER(const string & fileName,
00096                                                      const vector< const FIELD_* >& fields)
00097   : ENSIGHT_MED_DRIVER(fileName, MED_EN::WRONLY ), _fields( fields )
00098 {
00099 }
00100 
00101 ENSIGHT_MED_WRONLY_DRIVER::ENSIGHT_MED_WRONLY_DRIVER(const ENSIGHT_MED_WRONLY_DRIVER & driver)
00102   : ENSIGHT_MED_DRIVER(driver), _fields( driver._fields )
00103 {
00104 }
00105 
00106 ENSIGHT_MED_WRONLY_DRIVER::~ENSIGHT_MED_WRONLY_DRIVER()
00107 {
00108 }
00109 
00110 GENDRIVER * ENSIGHT_MED_WRONLY_DRIVER::copy() const
00111 {
00112   return new ENSIGHT_MED_WRONLY_DRIVER(*this) ;
00113 }
00114 
00115 void ENSIGHT_MED_WRONLY_DRIVER::read() throw (MEDEXCEPTION)
00116 {
00117   throw MEDEXCEPTION("ENSIGHT_MED_WRONLY_DRIVER::read : Can't read with a WRONLY driver !");
00118 }
00119 
00120 void ENSIGHT_MED_WRONLY_DRIVER::write() const throw (MEDEXCEPTION)
00121 {
00122   const char * LOC = "ENSIGHT_MED_WRONLY_DRIVER::write() : ";
00123   BEGIN_OF_MED(LOC);
00124 
00125   openConst(); // check if can open the case file
00126 
00127   _CaseFileDriver caseFile( getCaseFileName(), this );
00128 
00129   map< const GMESH*, vector< const FIELD_* > > mesh2fields;
00130   for (unsigned i = 0; i < _fields.size(); ++i )
00131     mesh2fields[ _fields[i]->getSupport()->getMesh() ].push_back( _fields[i] );
00132 
00133   // Create drivers for all meshes and fields
00134   // and add them to be written to Case file
00135 
00136   list< GENDRIVER* > drivers;
00137   ENSIGHT_MESH_WRONLY_DRIVER *  meshDriver;
00138   ENSIGHT_FIELD_WRONLY_DRIVER * fieldDriver;
00139 
00140   map< const GMESH*, vector< const FIELD_* > >::iterator m_ff = mesh2fields.begin();
00141   for (; m_ff != mesh2fields.end(); ++m_ff )
00142   {
00143     const GMESH * mesh = m_ff->first ;
00144     meshDriver = new ENSIGHT_MESH_WRONLY_DRIVER( _fileName, mesh );
00145     caseFile.addMesh( meshDriver );
00146     drivers.push_back( meshDriver );
00147 
00148     // all fields on this mesh
00149     const vector< const FIELD_*> & fields = m_ff->second;
00150     for (unsigned j=0; j<fields.size(); j++)
00151     {
00152       fieldDriver = new ENSIGHT_FIELD_WRONLY_DRIVER( _fileName, fields[j] );
00153       caseFile.addField( fieldDriver );
00154       drivers.push_back( fieldDriver );
00155     }
00156   }
00157 
00158   // Write
00159 
00160   // case file
00161   caseFile.write();// necessary data is passed to the drivers by this method
00162 
00163   // meshes and fields
00164   list< GENDRIVER* >::iterator drv = drivers.begin();
00165   for ( ; drv != drivers.end(); ++drv ) {
00166     GENDRIVER* driver = *drv;
00167     driver->write();
00168     delete driver;
00169   }
00170 
00171   END_OF_MED(LOC);
00172 }
00173 
00174 // ================================================================================
00175 // RDONLY
00176 // ================================================================================
00177 
00178 ENSIGHT_MED_RDONLY_DRIVER::ENSIGHT_MED_RDONLY_DRIVER() :
00179   ENSIGHT_MED_DRIVER(), _fields(0), _isFileStructRead(false)
00180 {
00181 }
00182 
00183 ENSIGHT_MED_RDONLY_DRIVER::ENSIGHT_MED_RDONLY_DRIVER(const string &     fileName,
00184                                                      vector< FIELD_* >& fields)
00185   : ENSIGHT_MED_DRIVER( fileName, MED_EN::RDONLY), _fields( & fields ), _isFileStructRead(false)
00186 {
00187 }
00188 
00189 ENSIGHT_MED_RDONLY_DRIVER::ENSIGHT_MED_RDONLY_DRIVER(const ENSIGHT_MED_RDONLY_DRIVER & driver)
00190   : ENSIGHT_MED_DRIVER(driver),
00191     _fields( driver._fields ),
00192     _isFileStructRead(driver._isFileStructRead)
00193 {
00194 }
00195 
00196 ENSIGHT_MED_RDONLY_DRIVER::~ENSIGHT_MED_RDONLY_DRIVER()
00197 {
00198 }
00199 
00200 GENDRIVER * ENSIGHT_MED_RDONLY_DRIVER::copy() const
00201 {
00202   return new ENSIGHT_MED_RDONLY_DRIVER(*this) ;
00203 }
00204 
00205 void ENSIGHT_MED_RDONLY_DRIVER::write() const throw (MEDEXCEPTION)
00206 {
00207   throw MEDEXCEPTION("ENSIGHT_MED_RDONLY_DRIVER::write : Can't write with a RDONLY driver !");
00208 }
00209 
00210 void ENSIGHT_MED_RDONLY_DRIVER::read()
00211 {
00212   const char * LOC = "ENSIGHT_MED_RDONLY_DRIVER::read() : " ;
00213   BEGIN_OF_MED(LOC);
00214 
00215   if ( _isFileStructRead )
00216   {
00217     for ( unsigned i = 0; i < _fields->size(); ++i )
00218     {
00219       const GMESH* mesh = _fields->at(i)->getSupport()->getMesh();
00220       if ( mesh->getNumberOfNodes() < 1 )
00221         const_cast<GMESH*>( mesh )->read( getId() );
00222       _fields->at(i)->read( getId() );
00223     }
00224   }
00225   else
00226   {
00227     open(); // check if can open the case file
00228 
00229     _CaseFileDriver caseFile( getCaseFileName(), this );
00230 
00231     caseFile.read();
00232 
00233     _fields->clear();
00234     int nbOfFields = caseFile.getNbVariables();
00235     if ( nbOfFields < 1 )
00236       return;
00237 
00238     int nbOfMeshes = caseFile.getNbMeshes();
00239     vector<MESH*> meshes;
00240     for ( int i = 1; i <= nbOfMeshes; ++i )
00241     {
00242       MESH* mesh = new MESH;
00243       ENSIGHT_MESH_RDONLY_DRIVER meshDriver(_fileName, mesh, i);
00244       caseFile.setDataFileName( i, &meshDriver );
00245       int drv = mesh->addDriver( meshDriver );
00246       mesh->read( drv );
00247       meshes.push_back( mesh );
00248     }
00249 
00250     for ( int i = 1; i <= nbOfFields; i++ )
00251     {
00252       int nbSteps = caseFile.getNbVarSteps( i );
00253       for ( int step = 1; step <= nbSteps; ++step )
00254       {
00255         FIELD_* field = new FIELD<double>();
00256         ENSIGHT_FIELD_RDONLY_DRIVER fieldDriver( _fileName, field );
00257         /*int meshIndex =*/ caseFile.setDataFileName( i, step, &fieldDriver );
00258         fieldDriver.open();
00259         fieldDriver.read();
00260         _fields->push_back( field );
00261       }
00262     }
00263   }
00264 }
00265 
00266 //================================================================================
00270 //================================================================================
00271 
00272 void ENSIGHT_MED_RDONLY_DRIVER::readFileStruct()
00273 {
00274   const char * LOC = "ENSIGHT_MED_RDONLY_DRIVER::read() : " ;
00275   BEGIN_OF_MED(LOC);
00276 
00277   open(); // check if can open the case file
00278 
00279   _CaseFileDriver caseFile( getCaseFileName(), this );
00280 
00281   caseFile.read();
00282 
00283   int nbOfMeshes = caseFile.getNbMeshes();
00284   vector<MESH*> meshes( nbOfMeshes+1, (MESH*)0 );
00285   for ( int i = 1; i <= nbOfMeshes; ++i )
00286   {
00287     MESH* mesh = meshes[ i ] = new MESH;
00288     ENSIGHT_MESH_RDONLY_DRIVER meshDriver(_fileName, mesh, i);
00289     caseFile.setDataFileName( i, &meshDriver ); // retrieve mesh name
00290     setId( mesh->addDriver( meshDriver ));
00291   }
00292   _isFileStructRead = true;
00293 
00294   _fields->clear();
00295   int nbOfFields = caseFile.getNbVariables();
00296   for ( int i = 1; i <= nbOfFields; i++ )
00297   {
00298     int nbSteps = caseFile.getNbVarSteps( i );
00299     for ( int step = 1; step <= nbSteps; ++step )
00300     {
00301       FIELD<double>* field = new FIELD<double>();
00302       ENSIGHT_FIELD_RDONLY_DRIVER fieldDriver( _fileName, field, step );
00303       int meshIndex = caseFile.setDataFileName( i, step, &fieldDriver ); // retrieve field name
00304       field->addDriver( fieldDriver );
00305       if ( const SUPPORT* sup = field->getSupport() ) {
00306         if ( meshIndex > nbOfMeshes || !meshes[ meshIndex ])
00307           meshIndex = 1;
00308         ((SUPPORT*) sup)->setMesh( meshes[ meshIndex ]);
00309       }
00310       _fields->push_back( field );
00311     }
00312   }
00313 }
00314 }