Salome HOME
Avoid MED file reading failure caused by low memory
[modules/smesh.git] / src / MEDWrapper / Factory / MED_Test.cxx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
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 #include "MED_Utilities.hxx"
23 #include "MED_Algorithm.hxx"
24 #include "MED_GaussUtils.hxx"
25 #include "MED_Factory.hxx"
26
27 #ifdef _DEBUG_
28 static int MYDEBUG = 1;
29 #else
30 // static int MYDEBUG = 0;
31 #endif
32
33 static int MYWRITEDEBUG = 1;
34
35 using namespace MED;
36
37 #undef _DEBUG_
38
39 void CheckMed(const std::string& theFileName)
40 {
41   MSG(MYDEBUG,"CheckMed - theFileName = '"<<theFileName<<"'");
42   {
43     //TErr anErr;
44     PWrapper aMed = CrWrapper(theFileName);
45
46     TKey2Gauss aKey2Gauss = GetKey2Gauss(aMed);
47     TMKey2Profile aMKey2Profile = GetMKey2Profile(aMed);
48     INITMSG(MYDEBUG,"aMed->GetNbProfiles() = "<<aMed->GetNbProfiles()<<std::endl);
49
50     TInt aNbMeshes = aMed->GetNbMeshes();
51     BEGMSG(MYDEBUG,"GetNbMeshes() = "<<aNbMeshes<<std::endl);
52     for(TInt iMesh = 1; iMesh <= aNbMeshes; iMesh++){
53       
54       PMeshInfo aMeshInfo = aMed->GetPMeshInfo(iMesh);
55       INITMSG(MYDEBUG,"aMeshInfo->GetName() = '"<<aMeshInfo->GetName()<<"'"<<std::endl);
56
57       TEntityInfo aEntityInfo = aMed->GetEntityInfo(aMeshInfo);
58       
59       TEntity2TGeom2ElemInfo anEntity2TGeom2ElemInfo = 
60         GetEntity2TGeom2ElemInfo(aMed,aMeshInfo,aEntityInfo);
61
62       TFieldInfo2TimeStampInfoSet aFieldInfo2TimeStampInfoSet = 
63         GetFieldInfo2TimeStampInfoSet(aMed,aMeshInfo,aEntityInfo);
64       
65       TEntite2TFieldInfo2TimeStampInfoSet anEntite2TFieldInfo2TimeStampInfoSet = 
66         GetEntite2TFieldInfo2TimeStampInfoSet(aFieldInfo2TimeStampInfoSet);
67
68       TEntite2TFieldInfo2TimeStampInfoSet::const_iterator anEntite2TFieldInfo2TimeStampInfoSetIter = 
69         anEntite2TFieldInfo2TimeStampInfoSet.begin();
70       for(; anEntite2TFieldInfo2TimeStampInfoSetIter != anEntite2TFieldInfo2TimeStampInfoSet.end(); anEntite2TFieldInfo2TimeStampInfoSetIter++){
71         const TFieldInfo2TimeStampInfoSet& aFieldInfo2TimeStampInfoSet = anEntite2TFieldInfo2TimeStampInfoSetIter->second;
72         TFieldInfo2TimeStampInfoSet::const_iterator aFieldInfo2TimeStampInfoSetIter = aFieldInfo2TimeStampInfoSet.begin();
73         for(; aFieldInfo2TimeStampInfoSetIter != aFieldInfo2TimeStampInfoSet.end(); aFieldInfo2TimeStampInfoSetIter++){
74           PFieldInfo aFieldInfo = aFieldInfo2TimeStampInfoSetIter->first;
75           INITMSG(MYDEBUG,
76                   "GetPFieldInfo "<<
77                   "- aName = '"<<aFieldInfo->GetName()<<"'"<<
78                   "; aType = "<<aFieldInfo->GetType()<<
79                   "; aNbComp = "<<aFieldInfo->GetNbComp()<<
80                   std::endl);
81           const TTimeStampInfoSet& aTimeStampInfoSet = aFieldInfo2TimeStampInfoSetIter->second;
82           TTimeStampInfoSet::const_iterator aTimeStampInfoSettIter = aTimeStampInfoSet.begin();
83           for(; aTimeStampInfoSettIter != aTimeStampInfoSet.end(); aTimeStampInfoSettIter++){
84             PTimeStampInfo aTimeStampInfo = *aTimeStampInfoSettIter;
85             INITMSG(MYDEBUG,
86                     "GetPTimeStampInfo "<<
87                     "- anEntity = "<<aTimeStampInfo->GetEntity()<<
88                     "; aNumDt = "<<aTimeStampInfo->GetNumDt()<<
89                     std::endl);
90             PTimeStampValueBase aTimeStampValue = 
91               aMed->GetPTimeStampValue(aTimeStampInfo,
92                                        aMKey2Profile,
93                                        aKey2Gauss);
94           }
95         }
96       }
97
98       continue;
99
100       TFamilyInfoSet aFamilyInfoSet = GetFamilyInfoSet(aMed,aMeshInfo);
101       
102       TEntity2FamilySet aEntity2FamilySet = 
103         GetEntity2FamilySet(aMed,anEntity2TGeom2ElemInfo,aFamilyInfoSet);
104       
105       TGroupInfo aGroupInfo = GetGroupInfo(aFamilyInfoSet);
106       
107       PNodeInfo aNodeInfo = aMed->GetPNodeInfo(aMeshInfo);
108       TEntity2TGeom2ElemInfo::const_iterator anIter = anEntity2TGeom2ElemInfo.begin();
109       for(; anIter != anEntity2TGeom2ElemInfo.end(); anIter++){
110         const EEntiteMaillage& anEntity = anIter->first;
111         if(anEntity != eNOEUD){
112           const TGeom2ElemInfo& aGeom2ElemInfo = anIter->second;
113           TKey2Gauss::const_iterator anIter2 = aKey2Gauss.begin();
114           for(; anIter2 != aKey2Gauss.end(); anIter2++){
115             const TGaussInfo::TKey& aKey = anIter2->first;
116             EGeometrieElement aGeom = boost::get<0>(aKey);
117             TGeom2ElemInfo::const_iterator anIter3 = aGeom2ElemInfo.find(aGeom);
118             if(anIter3 != aGeom2ElemInfo.end()){
119               if(PCellInfo aCellInfo = anIter3->second){
120                 PGaussInfo aGaussInfo = anIter2->second;
121                 TGaussCoord aGaussCoord;
122                 GetGaussCoord3D(aGaussInfo,aCellInfo,aNodeInfo,aGaussCoord);
123               }
124             }
125           }
126         }
127       }
128       
129     }
130   }
131   MSG(MYDEBUG,"OK");
132 }
133
134
135 void CopyMed(const PWrapper& theMed, 
136              const PWrapper& theMed2, 
137              int theIncr)
138 {
139   TKey2Gauss aKey2Gauss = GetKey2Gauss(theMed);
140   TMKey2Profile aMKey2Profile = GetMKey2Profile(theMed);
141
142   TInt aNbMeshes = theMed->GetNbMeshes();
143   MSG(MYDEBUG,"aNbMeshes = "<<aNbMeshes);
144   std::string aName;
145   for(TInt iMesh = 0; iMesh < aNbMeshes; iMesh++){
146     PMeshInfo aMeshInfo = theMed->GetPMeshInfo(iMesh+1);
147 #ifdef _DEBUG_
148     TInt aDim = aMeshInfo->myDim;
149     aName = aMeshInfo->GetName();
150     INITMSG(MYDEBUG,"GetMeshInfo - aName = '"<<aName<<"'; aDim = "<<aDim<<"\n");
151 #endif
152     PMeshInfo aMeshInfo2 = theMed2->CrMeshInfo(aMeshInfo);
153     if(MYWRITEDEBUG){
154       aName = aMeshInfo2->GetName();
155
156       aName[0] += theIncr;
157       aMeshInfo2->SetName(aName);
158       theMed2->SetMeshInfo(aMeshInfo2);
159       BEGMSG(MYDEBUG,"aNbMeshes2 = "<<theMed2->GetNbMeshes()<<"\n");
160     }
161     
162     TEntityInfo aEntityInfo = theMed->GetEntityInfo(aMeshInfo);
163
164     //continue;
165
166     TInt aNbFields = theMed->GetNbFields(); 
167     MSG(MYDEBUG,"GetNbFields() = "<<aNbFields);
168     for(TInt iField = 0; iField < aNbFields; iField++){
169       PFieldInfo aFieldInfo = theMed->GetPFieldInfo(aMeshInfo,iField+1);
170 #ifdef _DEBUG_
171       TInt aNbComp = aFieldInfo->GetNbComp();
172       INITMSG(MYDEBUG,"aName = '"<<aFieldInfo->GetName()<<"'; aNbComp = "<<aNbComp<<";\n");
173 #endif
174       PFieldInfo aFieldInfo2 = theMed->CrFieldInfo(aMeshInfo2,aFieldInfo);
175
176       if(MYWRITEDEBUG){
177         aName = aFieldInfo->GetName();
178         aName[0] += theIncr;
179         aFieldInfo2->SetName(aName);
180         theMed2->SetFieldInfo(aFieldInfo2);
181       }
182
183       EEntiteMaillage anEntity;
184       TGeom2Size aGeom2Size;
185       TInt aNbTimeStamps = 
186         theMed->GetNbTimeStamps(aFieldInfo,aEntityInfo,anEntity,aGeom2Size);
187       {
188         INITMSG(MYDEBUG,"GetNbTimeStamps = "<<aNbTimeStamps<<std::endl);
189         for(TInt iTimeStamp = 0; iTimeStamp < aNbTimeStamps; iTimeStamp++){
190           PTimeStampInfo aTimeStampInfo = 
191           theMed->GetPTimeStampInfo(aFieldInfo,anEntity,aGeom2Size,iTimeStamp+1);
192 #ifdef _DEBUG_
193           TInt aNumDt = aTimeStampInfo->GetNumDt();
194           INITMSG(MYDEBUG,"aNumDt = "<<aNumDt<<"\n");
195 #endif
196           
197           PTimeStampInfo aTimeStampInfo2 = 
198             theMed->CrTimeStampInfo(aFieldInfo2,aTimeStampInfo);
199           
200           PTimeStampValueBase aTimeStampValue = 
201             theMed->GetPTimeStampValue(aTimeStampInfo,
202                                        aMKey2Profile,
203                                        aKey2Gauss);
204           
205           PTimeStampValueBase aTimeStampValue2 = 
206             theMed->CrTimeStampValue(aTimeStampInfo2,
207                                      aTimeStampValue);
208           
209           if(MYWRITEDEBUG) theMed2->SetTimeStamp(aTimeStampValue2);
210         }
211       }
212     }
213
214     TInt aNbFam = theMed->GetNbFamilies(aMeshInfo);
215     MSG(MYDEBUG,"GetNbFamilies() = "<<aNbFam);
216     for(TInt iFam = 0; iFam < aNbFam; iFam++){
217       PFamilyInfo aFamilyInfo = theMed->GetPFamilyInfo(aMeshInfo,iFam+1);
218       TInt aNbGroup = aFamilyInfo->GetNbGroup();
219 #ifdef _DEBUG_
220       TInt aNbAttr = aFamilyInfo->GetNbAttr();
221 #endif
222       TInt anId = aFamilyInfo->GetId();
223       if(anId == 0)
224         continue;
225
226       aName = aFamilyInfo->GetName();
227 #ifdef _DEBUG_
228       INITMSG(MYDEBUG,"aName = '"<<aName<<"'; anId = "<<anId<<
229               "; aNbAttr = "<<aNbAttr<<"; aNbGroup = "<<aNbGroup<<"\n");
230 #endif
231       PFamilyInfo aFamilyInfo2 = theMed->CrFamilyInfo(aMeshInfo2,aFamilyInfo);
232       for(TInt iGroup = 0; iGroup < aNbGroup; iGroup++){
233         aName = aFamilyInfo->GetGroupName(iGroup);
234         INITMSG(MYDEBUG,"aGroupName = '"<<aName<<"'\n");
235         aName[0] += theIncr;
236         aFamilyInfo2->SetGroupName(iGroup,aName);
237       }
238
239       if(MYWRITEDEBUG){
240         aName = aFamilyInfo->GetName();
241         aName[0] += theIncr;
242         aFamilyInfo2->SetName(aName);
243         theMed2->SetFamilyInfo(aFamilyInfo2);
244         INITMSG(MYDEBUG,"GetNbFamilies = "<<theMed2->GetNbFamilies(aMeshInfo2)<<std::endl);
245       }
246     }
247
248     MSG(MYDEBUG,"GetEntityInfo - aNbEntities = "<<aEntityInfo.size());
249     TEntityInfo::iterator anEntityInfoIter = aEntityInfo.begin();
250     for(; anEntityInfoIter != aEntityInfo.end(); anEntityInfoIter++){
251       const EEntiteMaillage& anEntity = anEntityInfoIter->first;
252       INITMSG(MYDEBUG,"anEntity = "<<anEntity<<"\n");
253       if(anEntity == eNOEUD){
254         PNodeInfo aNodeInfo = theMed->GetPNodeInfo(aMeshInfo);
255         PNodeInfo aNodeInfo2 = theMed->CrNodeInfo(aMeshInfo2,aNodeInfo);
256         if(MYWRITEDEBUG) theMed2->SetNodeInfo(aNodeInfo2);
257         continue;
258       }
259       TGeom2Size& aGeom2Size = anEntityInfoIter->second;
260       TGeom2Size::iterator aGeomIter = aGeom2Size.begin();
261       for(; aGeomIter != aGeom2Size.end(); aGeomIter++){
262         const EGeometrieElement& aGeom = aGeomIter->first;
263 #ifdef _DEBUG_
264         const TInt& aNbElem = aGeomIter->second;
265         INITMSG(MYDEBUG,"aGeom = "<<aGeom<<"; aNbElem = "<<aNbElem<<": ");
266 #endif
267         switch(aGeom){
268         case ePOLYGONE: {
269           PPolygoneInfo aPolygoneInfo = theMed->GetPPolygoneInfo(aMeshInfo,anEntity,aGeom);
270           PPolygoneInfo aPolygoneInfo2 = theMed->CrPolygoneInfo(aMeshInfo2,aPolygoneInfo);
271           if(MYWRITEDEBUG) theMed2->SetPolygoneInfo(aPolygoneInfo2);
272           break;
273         }
274         case ePOLYEDRE: {
275           PPolyedreInfo aPolyedreInfo = theMed->GetPPolyedreInfo(aMeshInfo,anEntity,aGeom);
276           PPolyedreInfo aPolyedreInfo2 = theMed->CrPolyedreInfo(aMeshInfo2,aPolyedreInfo);
277           if(MYWRITEDEBUG) theMed2->SetPolyedreInfo(aPolyedreInfo2);
278           break;
279         }
280         default:
281           PCellInfo aCellInfo = theMed->GetPCellInfo(aMeshInfo,anEntity,aGeom);
282           PCellInfo aCellInfo2 = theMed2->CrCellInfo(aMeshInfo2,aCellInfo);
283           if(MYWRITEDEBUG) theMed2->SetCellInfo(aCellInfo2);
284         }
285       }
286     }
287     
288   }
289   MSG(MYDEBUG,"OK");
290 }
291
292
293 void CopyMed(const std::string& theFileName, 
294              const std::string& theFileName2, 
295              MED::EVersion theVersion,
296              int theNbCopy)
297 {
298   MSG(MYDEBUG,"CopyMed - theFileName = '"<<theFileName<<"'; theFileName2 = '"<<theFileName2<<"', theVersion = "<<theVersion);
299
300   PWrapper aMed = CrWrapper(theFileName);
301
302   PWrapper aMed2 = CrWrapper(theFileName2,theVersion);
303
304   for(int i = 0; i < theNbCopy; i++) 
305     CopyMed(aMed,aMed2,i);
306 }
307
308
309 void ReadMed(const char* theFileName, 
310              const char* theFileName2,
311              MED::EVersion theVersion = eV2_2,
312              int theNbCopy = 1)
313 {
314   MSG(MYDEBUG,"theFileName = '"<<theFileName<<"'; "<<
315       "theFileName2 = '"<<theFileName2<<"'; "<<
316       "theVersion = "<<theVersion<<"; "<<
317       "theNbCopy = "<<theNbCopy);
318
319   CopyMed(theFileName,theFileName2,theVersion,theNbCopy);
320 }
321
322 #ifdef WIN32
323 #pragma warning(disable:4101) // exc unreferenced ... ??
324 #endif
325
326 int main(int argc, char** argv){ 
327 #ifndef _DEBUG_
328   try{
329 #endif
330     if(argc == 2)
331       CheckMed(argv[1]);
332     if(argc == 3)
333       ReadMed(argv[1],argv[2]);
334     if(argc == 4)
335       ReadMed(argv[1],argv[2],MED::EVersion(atoi(argv[3])));
336     if(argc == 5)
337       ReadMed(argv[1],argv[2],MED::EVersion(atoi(argv[3])),atoi(argv[4]));
338     return 0;
339 #ifndef _DEBUG_
340   }catch(std::exception& exc){
341     MSG(MYDEBUG,"Follow exception was accured :\n"<<exc.what());
342   }catch(...){
343     MSG(MYDEBUG,"Unknown exception was accured");
344   } 
345 #endif 
346   return 1;
347 }