]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_EnsightMedDriver.cxx
Salome HOME
Fix problem of make distcheck
[modules/med.git] / src / MEDMEM / MEDMEM_EnsightMedDriver.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "MEDMEM_define.hxx"
24 #include "MEDMEM_Support.hxx"
25 #include "MEDMEM_Mesh.hxx"
26 #include "MEDMEM_Field.hxx"
27
28 #include "MEDMEM_EnsightMedDriver.hxx"
29 #include "MEDMEM_EnsightFieldDriver.hxx"
30 #include "MEDMEM_EnsightMeshDriver.hxx"
31
32 using namespace std;
33 //using namespace MEDMEM;
34 using namespace MED_EN;
35 using namespace MEDMEM_ENSIGHT;
36
37 // ================================================================================
38 // ENSIGHT_MED_DRIVER
39 // ================================================================================
40 namespace MEDMEM {
41
42 ENSIGHT_MED_DRIVER::ENSIGHT_MED_DRIVER():_CaseFileDriver_User()
43 {
44 }
45
46 ENSIGHT_MED_DRIVER::ENSIGHT_MED_DRIVER(const string & fileName,
47                                        med_mode_acces accessMode):
48   _CaseFileDriver_User(fileName,accessMode)
49 {
50 }
51
52 ENSIGHT_MED_DRIVER::ENSIGHT_MED_DRIVER(const ENSIGHT_MED_DRIVER & driver):
53   _CaseFileDriver_User(driver)
54 {
55 }
56
57 void ENSIGHT_MED_DRIVER::openConst() const
58 {
59   const char * LOC ="ENSIGHT_MED_DRIVER::open() : ";
60   BEGIN_OF_MED(LOC);
61
62   if ( _fileName.empty() )
63     throw MED_EXCEPTION
64       ( LOCALIZED( STRING(LOC) << "_fileName is empty, "
65                    "please set a correct fileName before calling open()"));
66
67   if (!canOpenFile( _fileName, getAccessMode() ))
68     throw MED_EXCEPTION
69       ( LOCALIZED( STRING(LOC) << "Can not open main Ensight file " << _fileName));
70
71   END_OF_MED(LOC);
72 }
73
74 void ENSIGHT_MED_DRIVER::open()
75 {
76   openConst();
77 }
78 void ENSIGHT_MED_DRIVER::close()
79 {
80 }
81
82 ENSIGHT_MED_DRIVER::~ENSIGHT_MED_DRIVER()
83 {
84   MESSAGE_MED("ENSIGHT_MED_DRIVER::~ENSIGHT_MED_DRIVER() has been destroyed");
85 }
86
87 // ================================================================================
88 // WRONLY
89 // ================================================================================
90
91 ENSIGHT_MED_WRONLY_DRIVER::ENSIGHT_MED_WRONLY_DRIVER() : ENSIGHT_MED_DRIVER(), _fields(0)
92 {
93 }
94
95 ENSIGHT_MED_WRONLY_DRIVER::ENSIGHT_MED_WRONLY_DRIVER(const string & fileName,
96                                                      const vector< const FIELD_* >& fields)
97   : ENSIGHT_MED_DRIVER(fileName, MED_EN::WRONLY ), _fields( fields )
98 {
99 }
100
101 ENSIGHT_MED_WRONLY_DRIVER::ENSIGHT_MED_WRONLY_DRIVER(const ENSIGHT_MED_WRONLY_DRIVER & driver)
102   : ENSIGHT_MED_DRIVER(driver), _fields( driver._fields )
103 {
104 }
105
106 ENSIGHT_MED_WRONLY_DRIVER::~ENSIGHT_MED_WRONLY_DRIVER()
107 {
108 }
109
110 GENDRIVER * ENSIGHT_MED_WRONLY_DRIVER::copy() const
111 {
112   return new ENSIGHT_MED_WRONLY_DRIVER(*this) ;
113 }
114
115 void ENSIGHT_MED_WRONLY_DRIVER::read() throw (MEDEXCEPTION)
116 {
117   throw MEDEXCEPTION("ENSIGHT_MED_WRONLY_DRIVER::read : Can't read with a WRONLY driver !");
118 }
119
120 void ENSIGHT_MED_WRONLY_DRIVER::write() const throw (MEDEXCEPTION)
121 {
122   const char * LOC = "ENSIGHT_MED_WRONLY_DRIVER::write() : ";
123   BEGIN_OF_MED(LOC);
124
125   openConst(); // check if can open the case file
126
127   _CaseFileDriver caseFile( getCaseFileName(), this );
128
129   map< const GMESH*, vector< const FIELD_* > > mesh2fields;
130   for (unsigned i = 0; i < _fields.size(); ++i )
131     mesh2fields[ _fields[i]->getSupport()->getMesh() ].push_back( _fields[i] );
132
133   // Create drivers for all meshes and fields
134   // and add them to be written to Case file
135
136   list< GENDRIVER* > drivers;
137   ENSIGHT_MESH_WRONLY_DRIVER *  meshDriver;
138   ENSIGHT_FIELD_WRONLY_DRIVER * fieldDriver;
139
140   map< const GMESH*, vector< const FIELD_* > >::iterator m_ff = mesh2fields.begin();
141   for (; m_ff != mesh2fields.end(); ++m_ff )
142   {
143     const GMESH * mesh = m_ff->first ;
144     meshDriver = new ENSIGHT_MESH_WRONLY_DRIVER( _fileName, mesh );
145     caseFile.addMesh( meshDriver );
146     drivers.push_back( meshDriver );
147
148     // all fields on this mesh
149     const vector< const FIELD_*> & fields = m_ff->second;
150     for (unsigned j=0; j<fields.size(); j++)
151     {
152       fieldDriver = new ENSIGHT_FIELD_WRONLY_DRIVER( _fileName, fields[j] );
153       caseFile.addField( fieldDriver );
154       drivers.push_back( fieldDriver );
155     }
156   }
157
158   // Write
159
160   // case file
161   caseFile.write();// necessary data is passed to the drivers by this method
162
163   // meshes and fields
164   list< GENDRIVER* >::iterator drv = drivers.begin();
165   for ( ; drv != drivers.end(); ++drv ) {
166     GENDRIVER* driver = *drv;
167     driver->write();
168     delete driver;
169   }
170
171   END_OF_MED(LOC);
172 }
173
174 // ================================================================================
175 // RDONLY
176 // ================================================================================
177
178 ENSIGHT_MED_RDONLY_DRIVER::ENSIGHT_MED_RDONLY_DRIVER() :
179   ENSIGHT_MED_DRIVER(), _fields(0), _isFileStructRead(false)
180 {
181 }
182
183 ENSIGHT_MED_RDONLY_DRIVER::ENSIGHT_MED_RDONLY_DRIVER(const string &     fileName,
184                                                      vector< FIELD_* >& fields)
185   : ENSIGHT_MED_DRIVER( fileName, MED_EN::RDONLY), _fields( & fields ), _isFileStructRead(false)
186 {
187 }
188
189 ENSIGHT_MED_RDONLY_DRIVER::ENSIGHT_MED_RDONLY_DRIVER(const ENSIGHT_MED_RDONLY_DRIVER & driver)
190   : ENSIGHT_MED_DRIVER(driver),
191     _fields( driver._fields ),
192     _isFileStructRead(driver._isFileStructRead)
193 {
194 }
195
196 ENSIGHT_MED_RDONLY_DRIVER::~ENSIGHT_MED_RDONLY_DRIVER()
197 {
198 }
199
200 GENDRIVER * ENSIGHT_MED_RDONLY_DRIVER::copy() const
201 {
202   return new ENSIGHT_MED_RDONLY_DRIVER(*this) ;
203 }
204
205 void ENSIGHT_MED_RDONLY_DRIVER::write() const throw (MEDEXCEPTION)
206 {
207   throw MEDEXCEPTION("ENSIGHT_MED_RDONLY_DRIVER::write : Can't write with a RDONLY driver !");
208 }
209
210 void ENSIGHT_MED_RDONLY_DRIVER::read()
211 {
212   const char * LOC = "ENSIGHT_MED_RDONLY_DRIVER::read() : " ;
213   BEGIN_OF_MED(LOC);
214
215   if ( _isFileStructRead )
216   {
217     for ( unsigned i = 0; i < _fields->size(); ++i )
218     {
219       const GMESH* mesh = _fields->at(i)->getSupport()->getMesh();
220       if ( mesh->getNumberOfNodes() < 1 )
221         const_cast<GMESH*>( mesh )->read( getId() );
222       _fields->at(i)->read( getId() );
223     }
224   }
225   else
226   {
227     open(); // check if can open the case file
228
229     _CaseFileDriver caseFile( getCaseFileName(), this );
230
231     caseFile.read();
232
233     _fields->clear();
234     int nbOfFields = caseFile.getNbVariables();
235     if ( nbOfFields < 1 )
236       return;
237
238     int nbOfMeshes = caseFile.getNbMeshes();
239     vector<MESH*> meshes;
240     for ( int i = 1; i <= nbOfMeshes; ++i )
241     {
242       MESH* mesh = new MESH;
243       ENSIGHT_MESH_RDONLY_DRIVER meshDriver(_fileName, mesh, i);
244       caseFile.setDataFileName( i, &meshDriver );
245       int drv = mesh->addDriver( meshDriver );
246       mesh->read( drv );
247       meshes.push_back( mesh );
248     }
249
250     for ( int i = 1; i <= nbOfFields; i++ )
251     {
252       int nbSteps = caseFile.getNbVarSteps( i );
253       for ( int step = 1; step <= nbSteps; ++step )
254       {
255         FIELD_* field = new FIELD<double>();
256         ENSIGHT_FIELD_RDONLY_DRIVER fieldDriver( _fileName, field );
257         /*int meshIndex =*/ caseFile.setDataFileName( i, step, &fieldDriver );
258         fieldDriver.open();
259         fieldDriver.read();
260         _fields->push_back( field );
261       }
262     }
263   }
264 }
265
266 //================================================================================
267 /*!
268  * \brief Create all meshes and fields but not read them
269  */
270 //================================================================================
271
272 void ENSIGHT_MED_RDONLY_DRIVER::readFileStruct()
273 {
274   const char * LOC = "ENSIGHT_MED_RDONLY_DRIVER::read() : " ;
275   BEGIN_OF_MED(LOC);
276
277   open(); // check if can open the case file
278
279   _CaseFileDriver caseFile( getCaseFileName(), this );
280
281   caseFile.read();
282
283   int nbOfMeshes = caseFile.getNbMeshes();
284   vector<MESH*> meshes( nbOfMeshes+1, (MESH*)0 );
285   for ( int i = 1; i <= nbOfMeshes; ++i )
286   {
287     MESH* mesh = meshes[ i ] = new MESH;
288     ENSIGHT_MESH_RDONLY_DRIVER meshDriver(_fileName, mesh, i);
289     caseFile.setDataFileName( i, &meshDriver ); // retrieve mesh name
290     setId( mesh->addDriver( meshDriver ));
291   }
292   _isFileStructRead = true;
293
294   _fields->clear();
295   int nbOfFields = caseFile.getNbVariables();
296   for ( int i = 1; i <= nbOfFields; i++ )
297   {
298     int nbSteps = caseFile.getNbVarSteps( i );
299     for ( int step = 1; step <= nbSteps; ++step )
300     {
301       FIELD<double>* field = new FIELD<double>();
302       ENSIGHT_FIELD_RDONLY_DRIVER fieldDriver( _fileName, field, step );
303       int meshIndex = caseFile.setDataFileName( i, step, &fieldDriver ); // retrieve field name
304       field->addDriver( fieldDriver );
305       if ( const SUPPORT* sup = field->getSupport() ) {
306         if ( meshIndex > nbOfMeshes || !meshes[ meshIndex ])
307           meshIndex = 1;
308         ((SUPPORT*) sup)->setMesh( meshes[ meshIndex ]);
309       }
310       _fields->push_back( field );
311     }
312   }
313 }
314 }