Salome HOME
Update of CheckDone
[modules/smesh.git] / src / MEDWrapper / MED_Algorithm.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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
23 #include "MED_Algorithm.hxx"
24 #include "MED_Wrapper.hxx"
25 #include "MED_Utilities.hxx"
26
27 namespace MED
28 {
29   //---------------------------------------------------------------
30   TEntity2TGeom2ElemInfo
31   GetEntity2TGeom2ElemInfo(const PWrapper& theWrapper,
32                            const PMeshInfo& theMeshInfo,
33                            const MED::TEntityInfo& theEntityInfo)
34   {
35     MSG("GetElemsByEntity(...)");
36     TEntity2TGeom2ElemInfo anEntity2TGeom2ElemInfo;
37     MED::TEntityInfo::const_iterator anIter = theEntityInfo.begin();
38     PElemInfo anElemInfo;
39     TErr anErr;
40     for(; anIter != theEntityInfo.end(); anIter++){
41       const EEntiteMaillage& anEntity = anIter->first;
42       const TGeom2Size& aGeom2Size = anIter->second;
43       TGeom2ElemInfo& aGeom2ElemInfo = anEntity2TGeom2ElemInfo[anEntity];
44
45       if(anEntity == eNOEUD){
46         aGeom2ElemInfo[ePOINT1] = theWrapper->GetPElemInfo(theMeshInfo);
47         continue;
48       }
49
50       TGeom2Size::const_iterator anIter2 = aGeom2Size.begin();
51       for(; anIter2 != aGeom2Size.end(); anIter2++){
52         const EGeometrieElement& aGeom = anIter2->first;
53         aGeom2ElemInfo[aGeom] = theWrapper->GetPElemInfo(theMeshInfo,anEntity,aGeom,MED::eNOD,&anErr);
54       }
55     }
56     ADDMSG("\n");
57     return anEntity2TGeom2ElemInfo;
58   }
59
60   //---------------------------------------------------------------
61   TFamilyInfoSet
62   GetFamilyInfoSet(const PWrapper& theWrapper,
63                    const PMeshInfo& theMeshInfo)
64   {
65     MSG("GetFamilies(...)");
66     TErr anErr;
67     TFamilyInfoSet aFamilyInfoSet;
68     TInt aNbFam = theWrapper->GetNbFamilies(*theMeshInfo);
69     INITMSG("GetNbFamilies() = "<<aNbFam<<"\n");
70     for(TInt iFam = 1; iFam <= aNbFam; iFam++){
71       PFamilyInfo aFamilyInfo = theWrapper->GetPFamilyInfo(theMeshInfo,iFam,&anErr);
72       if(anErr >= 0)
73         aFamilyInfoSet.insert(aFamilyInfo);
74     }
75     ADDMSG("\n");
76     return aFamilyInfoSet;
77   }
78
79   //---------------------------------------------------------------
80   TGroupInfo
81   GetGroupInfo(const TFamilyInfoSet& theFamilyInfoSet)
82   {
83     MSG("GetFamiliesByGroup(...)");
84     TGroupInfo aGroup;
85     TFamilyInfoSet::const_iterator anIter = theFamilyInfoSet.begin();
86     for(; anIter != theFamilyInfoSet.end(); anIter++){
87       const PFamilyInfo& aFamilyInfo = *anIter;
88       TInt aNbGroup = aFamilyInfo->GetNbGroup();
89       for(TInt iGroup = 0; iGroup < aNbGroup; iGroup++){
90         aGroup[aFamilyInfo->GetGroupName(iGroup)].insert(aFamilyInfo);
91       }
92     }
93
94     if(SALOME::VerbosityActivated()){
95       TGroupInfo::const_iterator anIter = aGroup.begin();
96       for(; anIter != aGroup.end(); anIter++){
97         const std::string& aName = anIter->first;
98         INITMSG("aGroupName = '"<<aName<<"'\n");
99         const TFamilyInfoSet& aFamilyInfoSet = anIter->second;
100         TFamilyInfoSet::const_iterator anFamIter = aFamilyInfoSet.begin();
101         for(; anFamIter != aFamilyInfoSet.end(); anFamIter++){
102           const PFamilyInfo& aFamilyInfo = *anFamIter;
103           INITMSG("aFamilyName = '"<<aFamilyInfo->GetName()<<"'\n");
104         }
105       }
106       ADDMSG("\n");
107     }
108
109     return aGroup;
110   }
111
112   //---------------------------------------------------------------
113   TFieldInfo2TimeStampInfoSet
114   GetFieldInfo2TimeStampInfoSet(const PWrapper& theWrapper,
115                                 const PMeshInfo& theMeshInfo,
116                                 const MED::TEntityInfo& theEntityInfo)
117   {
118     MSG("GetFieldsByEntity(...)");
119     TFieldInfo2TimeStampInfoSet aFieldInfo2TimeStampInfoSet;
120     TInt aNbFields = theWrapper->GetNbFields();
121     INITMSG("GetNbFields() = "<<aNbFields<<"\n");
122     for(TInt iField = 1; iField <= aNbFields; iField++){
123       PFieldInfo aFieldInfo = theWrapper->GetPFieldInfo(theMeshInfo,iField);
124       INITMSG("aFieldName = '"<<aFieldInfo->GetName()<<
125               "'; aNbComp = "<<aFieldInfo->GetNbComp()<<"; ");
126       TGeom2Size aGeom2Size;
127       EEntiteMaillage anEntity = EEntiteMaillage(-1);
128       TInt aNbTimeStamps = theWrapper->GetNbTimeStamps(aFieldInfo,theEntityInfo,anEntity,aGeom2Size);
129       ADDMSG("anEntity = "<<anEntity<<"; GetNbTimeStamps = "<<aNbTimeStamps<<"\n");
130       for(TInt iTimeStamp = 1; iTimeStamp <= aNbTimeStamps; iTimeStamp++){
131         PTimeStampInfo aTimeStamp =
132           theWrapper->GetPTimeStampInfo(aFieldInfo,anEntity,aGeom2Size,iTimeStamp);
133         aFieldInfo2TimeStampInfoSet[aFieldInfo].insert(aTimeStamp);
134         INITMSG("aDt = "<<aTimeStamp->GetDt()<<", Unit = \'"<<aTimeStamp->GetUnitDt()<<"\n");
135       }
136     }
137     ADDMSG("\n");
138     return aFieldInfo2TimeStampInfoSet;
139   }
140
141   //---------------------------------------------------------------
142   TEntite2TFieldInfo2TimeStampInfoSet
143   GetEntite2TFieldInfo2TimeStampInfoSet(const TFieldInfo2TimeStampInfoSet& theFieldInfo2TimeStampInfoSet)
144   {
145     TEntite2TFieldInfo2TimeStampInfoSet anEntite2TFieldInfo2TimeStampInfoSet;
146     TFieldInfo2TimeStampInfoSet::const_iterator anIter = theFieldInfo2TimeStampInfoSet.begin();
147     for(; anIter != theFieldInfo2TimeStampInfoSet.end(); anIter++){
148       const TTimeStampInfoSet& aTimeStampInfoSet = anIter->second;
149       //const PFieldInfo& aFieldInfo = anIter->first;
150       if(aTimeStampInfoSet.empty())
151         continue;
152       const PTimeStampInfo& aTimeStampInfo = *aTimeStampInfoSet.begin();
153       anEntite2TFieldInfo2TimeStampInfoSet[ConvertEntity(aTimeStampInfo->GetEntity())].insert(*anIter);
154     }
155     return anEntite2TFieldInfo2TimeStampInfoSet;
156   }
157
158   //---------------------------------------------------------------
159   bool
160   operator<(const TFamilyTSize& theLeft, const TFamilyTSize& theRight)
161   {
162     const MED::PFamilyInfo& aLeftInfo = boost::get<0>(theLeft);
163     const MED::PFamilyInfo& aRightInfo = boost::get<0>(theRight);
164     return aLeftInfo->GetId() < aRightInfo->GetId();
165   }
166
167   //---------------------------------------------------------------
168   TEntity2FamilySet
169   GetEntity2FamilySet(const PWrapper& /*theWrapper*/,
170                       const TEntity2TGeom2ElemInfo& theEntity2TGeom2ElemInfo,
171                       const TFamilyInfoSet& theFamilyInfoSet)
172   {
173     MSG("GetFamiliesByEntity(...)");
174     TEntity2FamilySet anEntity2FamilySet;
175
176     typedef std::map<TInt,PFamilyInfo> TId2Family;
177     TId2Family anId2Family;
178     TFamilyInfoSet::const_iterator anIter = theFamilyInfoSet.begin();
179     for(; anIter != theFamilyInfoSet.end(); anIter++){
180       const PFamilyInfo& aFamilyInfo = *anIter;
181       anId2Family.insert(TId2Family::value_type(aFamilyInfo->GetId(),aFamilyInfo));
182     }
183
184     if(!anId2Family.empty()){
185       typedef std::map<TInt,TInt> TFamilyID2Size;
186       typedef std::map<EEntiteMaillage,TFamilyID2Size> TEntity2FamilyID;
187       TEntity2FamilyID anEntity2FamilyID;
188
189       if(!theEntity2TGeom2ElemInfo.empty()){
190         TEntity2TGeom2ElemInfo::const_iterator anIter = theEntity2TGeom2ElemInfo.begin();
191         for(; anIter != theEntity2TGeom2ElemInfo.end(); anIter++){
192           const EEntiteMaillage& anEntity = anIter->first;
193           TFamilyID2Size& aFamilyID2Size = anEntity2FamilyID[anEntity];
194           const TGeom2ElemInfo& aGeom2ElemInfo = anIter->second;
195           TGeom2ElemInfo::const_iterator aGeom2ElemInfoIter = aGeom2ElemInfo.begin();
196           for(; aGeom2ElemInfoIter != aGeom2ElemInfo.end(); aGeom2ElemInfoIter++){
197             const PElemInfo& aElemInfo = aGeom2ElemInfoIter->second;
198             if(TInt aNbElem = aElemInfo->GetNbElem()){
199               for(TInt i = 0; i < aNbElem; i++){
200                 aFamilyID2Size[aElemInfo->GetFamNum(i)] += 1;
201               }
202             }
203           }
204         }
205       }
206
207       if(!anEntity2FamilyID.empty()){
208         TEntity2FamilyID::const_iterator anIter = anEntity2FamilyID.begin();
209         for(; anIter != anEntity2FamilyID.end(); anIter++){
210           const EEntiteMaillage& anEntity = anIter->first;
211           INITMSG("anEntity = "<<anEntity<<":\n");
212           const TFamilyID2Size& aFamilyID2Size = anIter->second;
213           TFamilyID2Size::const_iterator anIter2 = aFamilyID2Size.begin();
214           for(; anIter2 != aFamilyID2Size.end(); anIter2++){
215             TInt anId = anIter2->first;
216             TInt aSize = anIter2->second;
217             TId2Family::const_iterator anIter3 = anId2Family.find(anId);
218             if(anIter3 != anId2Family.end()){
219               const PFamilyInfo& aFamilyInfo = anIter3->second;
220               anEntity2FamilySet[anEntity].insert(TFamilyTSize(aFamilyInfo,aSize));
221               INITMSG("aFamilyName = '"<<aFamilyInfo->GetName()<<
222                       "' anId = "<<aFamilyInfo->GetId()<<"\n");
223             }
224           }
225         }
226       }
227     }
228     ADDMSG("\n");
229     return anEntity2FamilySet;
230   }
231
232   //---------------------------------------------------------------
233   TKey2Gauss
234   GetKey2Gauss(const PWrapper& theWrapper,
235                TErr* theErr,
236                EModeSwitch theMode)
237   {
238     INITMSG("GetKey2Gauss - theMode = "<<theMode<<std::endl);
239     TKey2Gauss aKey2Gauss;
240     TInt aNbGauss = theWrapper->GetNbGauss(theErr);
241     for(TInt anId = 1; anId <= aNbGauss; anId++){
242       TGaussInfo::TInfo aPreInfo = theWrapper->GetGaussPreInfo(anId);
243       PGaussInfo anInfo = theWrapper->CrGaussInfo(aPreInfo,theMode);
244       theWrapper->GetGaussInfo(anId,anInfo,theErr);
245       TGaussInfo::TKey aKey = boost::get<0>(aPreInfo);
246       aKey2Gauss[aKey] = anInfo;
247
248       if (SALOME::VerbosityActivated())
249       {
250         const EGeometrieElement& aGeom = boost::get<0>(aKey);
251         const std::string& aName = boost::get<1>(aKey);
252         INITMSG("- aGeom = "<<aGeom<<"; aName = '"<<aName<<"'"<<std::endl);
253       }
254     }
255     
256     return aKey2Gauss;
257   }
258
259   //---------------------------------------------------------------
260   PProfileInfo
261   GetProfileInfo(const PWrapper& theWrapper,
262                  const std::string& theProfileName,
263                  TErr* theErr,
264                  EModeProfil theMode)
265   {
266     PProfileInfo anInfo;
267     TInt aNbProfiles = theWrapper->GetNbProfiles(theErr);
268     for(TInt anId = 1; anId <= aNbProfiles; anId++){
269       TProfileInfo::TInfo aPreInfo = theWrapper->GetProfilePreInfo(anId);
270       const std::string& aName = boost::get<0>(aPreInfo);
271       if(aName == theProfileName)
272         return theWrapper->GetPProfileInfo(anId,theMode,theErr);
273     }
274     return anInfo;
275   }
276
277   //---------------------------------------------------------------
278   TMKey2Profile
279   GetMKey2Profile(const PWrapper& theWrapper,
280                   TErr* theErr,
281                   EModeProfil theMode)
282   {
283     INITMSG("GetMKey2Profile - theMode = "<<theMode<<std::endl);
284     TKey2Profile aKey2Profile;
285     TInt aNbProfiles = theWrapper->GetNbProfiles(theErr);
286     for(TInt anId = 1; anId <= aNbProfiles; anId++){
287       TProfileInfo::TInfo aPreInfo = theWrapper->GetProfilePreInfo(anId);
288       PProfileInfo anInfo = theWrapper->GetPProfileInfo(anId,theMode,theErr);
289       const std::string& aName = boost::get<0>(aPreInfo);
290       aKey2Profile[aName] = anInfo;
291
292       if(SALOME::VerbosityActivated()){
293         INITMSG("- aName = '"<<aName<<"'"<<" : "<<std::endl);
294         TInt aNbElem = anInfo->GetSize();
295
296         for(TInt iElem = 0; iElem < aNbElem; iElem++){
297           ADDMSG(anInfo->GetElemNum(iElem)<<", ");
298         }
299
300         ADDMSG(std::endl);
301       }
302     }
303     return TMKey2Profile(theMode,aKey2Profile);
304   }
305
306   //---------------------------------------------------------------
307   EEntiteMaillage
308   GetEntityByFamilyId(PGrilleInfo& theInfo,
309                       TInt theId)
310   {
311     TElemNum::iterator aNodeFamIter = (theInfo->myFamNumNode).begin();
312     for(;aNodeFamIter != (theInfo->myFamNumNode).end(); aNodeFamIter++){
313       if(theId == *aNodeFamIter)
314         return eNOEUD;
315     }
316     TElemNum::iterator aCellFamIter = (theInfo->myFamNum).begin();
317     for(;aCellFamIter != (theInfo->myFamNum).end(); aCellFamIter++){
318       if(theId == *aCellFamIter)
319         return eMAILLE;
320     }
321     EXCEPTION(std::runtime_error, "GetEntityByFamilyId - fails");
322     return EEntiteMaillage(-1);
323   }
324
325   //---------------------------------------------------------------
326   TFamilyID2NbCells
327   GetFamilyID2NbCells(PGrilleInfo& theInfo)
328   {
329     TFamilyID2NbCells aFamily2NbCells;
330     TInt aNbNodes = theInfo->myFamNumNode.size();
331     TInt aNbCells = theInfo->myFamNum.size();
332     for(TInt i=0; i<aNbNodes; i++) aFamily2NbCells[theInfo->GetFamNumNode(i)] = 0;
333     for(TInt i=0; i<aNbCells; i++) aFamily2NbCells[theInfo->GetFamNum(i)] = 0;
334     for(TInt i=0; i<aNbNodes; i++) aFamily2NbCells[theInfo->GetFamNumNode(i)] += 1;
335     for(TInt i=0; i<aNbCells; i++) aFamily2NbCells[theInfo->GetFamNum(i)] += 1;
336     return aFamily2NbCells;
337   }
338
339   //---------------------------------------------------------------
340   EEntiteMaillage
341   ConvertEntity(const EEntiteMaillage& aEntity)
342   {
343     switch( aEntity ){
344     case eNOEUD_ELEMENT:
345     case eMAILLE:
346       return eMAILLE; // eNOEUD_ELEMENT is eMAILLE
347     case eFACE:
348     case eARETE:
349     case eNOEUD:
350       return aEntity;
351     default:
352       break;
353     }
354     return EEntiteMaillage(-1);
355   }
356 }