Salome HOME
Merge branch 'hydro/imps_2015' into V7_dev
[modules/smesh.git] / src / DriverUNV / DriverUNV_R_SMDS_Mesh.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
23 #include "DriverUNV_R_SMDS_Mesh.h"
24 #include "SMDS_Mesh.hxx"
25 #include "SMDS_MeshGroup.hxx"
26
27 #include "utilities.h"
28
29 #include "UNV164_Structure.hxx"
30 #include "UNV2411_Structure.hxx"
31 #include "UNV2412_Structure.hxx"
32 #include "UNV2417_Structure.hxx"
33 #include "UNV2420_Structure.hxx"
34 #include "UNV_Utilities.hxx"
35
36 #include <Basics_Utils.hxx>
37
38 using namespace std;
39
40
41 #ifdef _DEBUG_
42 static int MYDEBUG = 1;
43 #else
44 static int MYDEBUG = 0;
45 #endif
46
47 namespace
48 {
49   /*!
50    * \brief Move node coordinates to the global Cartesian CS
51    */
52   void transformNodes( UNV2411::TDataSet::const_iterator fromNode,
53                        UNV2411::TDataSet::const_iterator endNode,
54                        const UNV2420::TRecord &          csRecord )
55   {
56     const int csLabel = fromNode->exp_coord_sys_num;
57
58     UNV2411::TDataSet::const_iterator nodeIt;
59
60     // apply Transformation Matrix
61     if ( !csRecord.isIdentityMatrix() )
62     {
63       for ( nodeIt = fromNode; nodeIt != endNode; ++nodeIt )
64       {
65         const UNV2411::TRecord& nodeRec = *nodeIt;
66         if ( nodeRec.exp_coord_sys_num == csLabel )
67           csRecord.ApplyMatrix( (double*) nodeRec.coord );
68       }
69     }
70
71     // transform from Cylindrical CS
72     if ( csRecord.coord_sys_type == UNV2420::Cylindrical )
73     {
74       for ( nodeIt = fromNode; nodeIt != endNode; ++nodeIt )
75       {
76         const UNV2411::TRecord& nodeRec = *nodeIt;
77         if ( nodeRec.exp_coord_sys_num == csLabel )
78           csRecord.FromCylindricalCS( (double*) nodeRec.coord );
79       }
80     }
81     // transform from Spherical CS
82     else if ( csRecord.coord_sys_type == UNV2420::Spherical )
83     {
84       for ( nodeIt = fromNode; nodeIt != endNode; ++nodeIt )
85       {
86         const UNV2411::TRecord& nodeRec = *nodeIt;
87         if ( nodeRec.exp_coord_sys_num == csLabel )
88           csRecord.FromSphericalCS( (double*) nodeRec.coord );
89       }
90     }
91   }
92 }
93
94 DriverUNV_R_SMDS_Mesh::~DriverUNV_R_SMDS_Mesh()
95 {
96   if (myGroup != 0) 
97     delete myGroup;
98 }
99
100
101 Driver_Mesh::Status DriverUNV_R_SMDS_Mesh::Perform()
102 {
103   Kernel_Utils::Localizer loc;
104   Status aResult = DRS_OK;
105   std::ifstream in_stream(myFile.c_str());
106   try
107   {
108     {
109       // Read Units
110       UNV164::TRecord aUnitsRecord;
111       UNV164::Read( in_stream, aUnitsRecord );
112
113       // Read Coordinate systems
114       UNV2420::TDataSet aCoordSysDataSet;
115       UNV2420::Read(in_stream, myMeshName, aCoordSysDataSet);
116
117       // Read nodes
118       using namespace UNV2411;
119       TDataSet aDataSet2411;
120       UNV2411::Read(in_stream,aDataSet2411);
121       if(MYDEBUG) MESSAGE("Perform - aDataSet2411.size() = "<<aDataSet2411.size());
122
123       // Move nodes in a global CS
124       if ( !aCoordSysDataSet.empty() )
125       {
126         UNV2420::TDataSet::const_iterator csIter = aCoordSysDataSet.begin();
127         for ( ; csIter != aCoordSysDataSet.end(); ++csIter )
128         {
129           // find any node in this CS
130           TDataSet::const_iterator nodeIter = aDataSet2411.begin();
131           for (; nodeIter != aDataSet2411.end(); nodeIter++)
132             if ( nodeIter->exp_coord_sys_num == csIter->coord_sys_label )
133             {
134               transformNodes( nodeIter, aDataSet2411.end(), *csIter );
135               break;
136             }
137         }
138       }
139       // Move nodes to SI unit system
140       const double lenFactor = aUnitsRecord.factors[ UNV164::LENGTH_FACTOR ]; 
141       if ( lenFactor != 1. )
142       {
143         TDataSet::iterator nodeIter = aDataSet2411.begin(), nodeEnd;
144         for ( nodeEnd = aDataSet2411.end(); nodeIter != nodeEnd; nodeIter++)
145         {
146           UNV2411::TRecord& nodeRec = *nodeIter;
147           nodeRec.coord[0] *= lenFactor;
148           nodeRec.coord[1] *= lenFactor;
149           nodeRec.coord[2] *= lenFactor;
150         }
151       }
152
153       // Create nodes in the mesh
154       TDataSet::const_iterator anIter = aDataSet2411.begin();
155       for(; anIter != aDataSet2411.end(); anIter++)
156       {
157         const TRecord& aRec = *anIter;
158         myMesh->AddNodeWithID(aRec.coord[0],aRec.coord[1],aRec.coord[2],aRec.label);
159       }
160     }
161     {
162       using namespace UNV2412;
163       TDataSet aDataSet2412;
164       UNV2412::Read(in_stream,aDataSet2412);
165       TDataSet::const_iterator anIter = aDataSet2412.begin();
166       if(MYDEBUG) MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
167       for(; anIter != aDataSet2412.end(); anIter++)
168       {
169         SMDS_MeshElement* anElement = NULL;
170         const TRecord& aRec = *anIter;
171         if(IsBeam(aRec.fe_descriptor_id)) {
172           switch ( aRec.node_labels.size() ) {
173           case 2: // edge with two nodes
174             //MESSAGE("add edge " << aLabel << " " << aRec.node_labels[0] << " " << aRec.node_labels[1]);
175             anElement = myMesh->AddEdgeWithID(aRec.node_labels[0],
176                                               aRec.node_labels[1],
177                                               aRec.label);
178             break;
179           case 3: // quadratic edge (with 3 nodes)
180             //MESSAGE("add edge " << aRec.label << " " << aRec.node_labels[0] << " " << aRec.node_labels[1] << " " << aRec.node_labels[2]);
181             anElement = myMesh->AddEdgeWithID(aRec.node_labels[0],
182                                               aRec.node_labels[2],
183                                               aRec.node_labels[1],
184                                               aRec.label);
185           }
186         }
187         else if(IsFace(aRec.fe_descriptor_id)) {
188           //MESSAGE("add face " << aRec.label);
189           switch(aRec.fe_descriptor_id){
190           case 41: // Plane Stress Linear Triangle      
191           case 51: // Plane Strain Linear Triangle      
192           case 61: // Plate Linear Triangle             
193           case 74: // Membrane Linear Triangle          
194           case 81: // Axisymetric Solid Linear Triangle 
195           case 91: // Thin Shell Linear Triangle        
196             anElement = myMesh->AddFaceWithID(aRec.node_labels[0],
197                                               aRec.node_labels[1],
198                                               aRec.node_labels[2],
199                                               aRec.label);
200             break;
201
202           case 42: //  Plane Stress Parabolic Triangle       
203           case 52: //  Plane Strain Parabolic Triangle       
204           case 62: //  Plate Parabolic Triangle              
205           case 72: //  Membrane Parabolic Triangle           
206           case 82: //  Axisymetric Solid Parabolic Triangle  
207           case 92: //  Thin Shell Parabolic Triangle
208             if ( aRec.node_labels.size() == 7 )
209               anElement = myMesh->AddFaceWithID(aRec.node_labels[0],
210                                                 aRec.node_labels[2],
211                                                 aRec.node_labels[4],
212                                                 aRec.node_labels[1],
213                                                 aRec.node_labels[3],
214                                                 aRec.node_labels[5],
215                                                 aRec.node_labels[6],
216                                                 aRec.label);
217             else
218               anElement = myMesh->AddFaceWithID(aRec.node_labels[0],
219                                                 aRec.node_labels[2],
220                                                 aRec.node_labels[4],
221                                                 aRec.node_labels[1],
222                                                 aRec.node_labels[3],
223                                                 aRec.node_labels[5],
224                                                 aRec.label);
225             break;
226
227           case 44: // Plane Stress Linear Quadrilateral     
228           case 54: // Plane Strain Linear Quadrilateral     
229           case 64: // Plate Linear Quadrilateral            
230           case 71: // Membrane Linear Quadrilateral         
231           case 84: // Axisymetric Solid Linear Quadrilateral
232           case 94: // Thin Shell Linear Quadrilateral       
233             anElement = myMesh->AddFaceWithID(aRec.node_labels[0],
234                                               aRec.node_labels[1],
235                                               aRec.node_labels[2],
236                                               aRec.node_labels[3],
237                                               aRec.label);
238             break;
239
240           case 45: // Plane Stress Parabolic Quadrilateral      
241           case 55: // Plane Strain Parabolic Quadrilateral      
242           case 65: // Plate Parabolic Quadrilateral             
243           case 75: // Membrane Parabolic Quadrilateral          
244           case 85: // Axisymetric Solid Parabolic Quadrilateral 
245           case 95: // Thin Shell Parabolic Quadrilateral        
246             if ( aRec.node_labels.size() == 9 )
247               anElement = myMesh->AddFaceWithID(aRec.node_labels[0],
248                                                 aRec.node_labels[2],
249                                                 aRec.node_labels[4],
250                                                 aRec.node_labels[6],
251                                                 aRec.node_labels[1],
252                                                 aRec.node_labels[3],
253                                                 aRec.node_labels[5],
254                                                 aRec.node_labels[7],
255                                                 aRec.node_labels[8],
256                                                 aRec.label);
257             else
258               anElement = myMesh->AddFaceWithID(aRec.node_labels[0],
259                                                 aRec.node_labels[2],
260                                                 aRec.node_labels[4],
261                                                 aRec.node_labels[6],
262                                                 aRec.node_labels[1],
263                                                 aRec.node_labels[3],
264                                                 aRec.node_labels[5],
265                                                 aRec.node_labels[7],
266                                                 aRec.label);
267             break;
268           }
269         }
270         else if(IsVolume(aRec.fe_descriptor_id)){
271           //MESSAGE("add volume " << aRec.label);
272           switch(aRec.fe_descriptor_id){
273
274           case 111: // Solid Linear Tetrahedron - TET4
275             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
276                                                 aRec.node_labels[2],
277                                                 aRec.node_labels[1],
278                                                 aRec.node_labels[3],
279                                                 aRec.label);
280             break;
281
282           case 118: // Solid Quadratic Tetrahedron - TET10
283             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
284                                                 aRec.node_labels[4],
285                                                 aRec.node_labels[2],
286
287                                                 aRec.node_labels[9],
288
289                                                 aRec.node_labels[5],
290                                                 aRec.node_labels[3],
291                                                 aRec.node_labels[1],
292
293                                                 aRec.node_labels[6],
294                                                 aRec.node_labels[8],
295                                                 aRec.node_labels[7],
296                                                 aRec.label);
297             break;
298             
299           case 112: // Solid Linear Prism - PRISM6
300             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
301                                                 aRec.node_labels[2],
302                                                 aRec.node_labels[1],
303                                                 aRec.node_labels[3],
304                                                 aRec.node_labels[5],
305                                                 aRec.node_labels[4],
306                                                 aRec.label);
307             break;
308             
309           case 113: // Solid Quadratic Prism - PRISM15
310             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
311                                                 aRec.node_labels[4],
312                                                 aRec.node_labels[2],
313
314                                                 aRec.node_labels[9],
315                                                 aRec.node_labels[13],
316                                                 aRec.node_labels[11],
317
318                                                 aRec.node_labels[5],
319                                                 aRec.node_labels[3],
320                                                 aRec.node_labels[1],
321
322                                                 aRec.node_labels[14],
323                                                 aRec.node_labels[12],
324                                                 aRec.node_labels[10],
325
326                                                 aRec.node_labels[6],
327                                                 aRec.node_labels[8],
328                                                 aRec.node_labels[7],
329                                                 aRec.label);
330             break;
331             
332           case 115: // Solid Linear Brick - HEX8
333             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
334                                                 aRec.node_labels[3],
335                                                 aRec.node_labels[2],
336                                                 aRec.node_labels[1],
337                                                 aRec.node_labels[4],
338                                                 aRec.node_labels[7],
339                                                 aRec.node_labels[6],
340                                                 aRec.node_labels[5],
341                                                 aRec.label);
342             break;
343
344           case 116: // Solid Quadratic Brick - HEX20
345             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
346                                                 aRec.node_labels[6],
347                                                 aRec.node_labels[4],
348                                                 aRec.node_labels[2],
349
350                                                 aRec.node_labels[12],
351                                                 aRec.node_labels[18],
352                                                 aRec.node_labels[16],
353                                                 aRec.node_labels[14],
354
355                                                 aRec.node_labels[7],
356                                                 aRec.node_labels[5],
357                                                 aRec.node_labels[3],
358                                                 aRec.node_labels[1],
359
360                                                 aRec.node_labels[19],
361                                                 aRec.node_labels[17],
362                                                 aRec.node_labels[15],
363                                                 aRec.node_labels[13],
364
365                                                 aRec.node_labels[8],
366                                                 aRec.node_labels[11],
367                                                 aRec.node_labels[10],
368                                                 aRec.node_labels[9],
369                                                 aRec.label);
370             break;
371
372           case 114: // pyramid of 13 nodes (quadratic) - PIRA13
373             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
374                                                 aRec.node_labels[6],
375                                                 aRec.node_labels[4],
376                                                 aRec.node_labels[2],
377                                                 aRec.node_labels[7],
378                                                 aRec.node_labels[5],
379                                                 aRec.node_labels[3],
380                                                 aRec.node_labels[1],
381
382                                                 aRec.node_labels[8],
383                                                 aRec.node_labels[11],
384                                                 aRec.node_labels[10],
385                                                 aRec.node_labels[9],
386                                                 aRec.node_labels[12],
387                                                 aRec.label);
388             break;
389
390           }
391         }
392         if(!anElement)
393           MESSAGE("DriverUNV_R_SMDS_Mesh::Perform - can not add element with ID = "<<aRec.label<<" and type = "<<aRec.fe_descriptor_id);
394       }
395     }
396     {
397       using namespace UNV2417;      
398       TDataSet aDataSet2417;
399       UNV2417::Read(in_stream,aDataSet2417);
400       if(MYDEBUG) MESSAGE("Perform - aDataSet2417.size() = "<<aDataSet2417.size());
401       if  (aDataSet2417.size() > 0) {
402         myGroup = new SMDS_MeshGroup(myMesh);
403         TDataSet::const_iterator anIter = aDataSet2417.begin();
404         for(; anIter != aDataSet2417.end(); anIter++){
405           const TGroupId& aLabel = anIter->first;
406           const TRecord& aRec = anIter->second;
407
408           int aNodesNb = aRec.NodeList.size();
409           int aElementsNb = aRec.ElementList.size();
410
411           bool useSuffix = ((aNodesNb > 0) && (aElementsNb > 0));
412           int i;
413           if (aNodesNb > 0) {
414             SMDS_MeshGroup* aNodesGroup = (SMDS_MeshGroup*) myGroup->AddSubGroup(SMDSAbs_Node);
415             std::string aGrName = (useSuffix) ? aRec.GroupName + "_Nodes" : aRec.GroupName;
416             int i = aGrName.find( "\r" );
417             if (i > 0)
418               aGrName.erase (i, 2);
419             myGroupNames.insert(TGroupNamesMap::value_type(aNodesGroup, aGrName));
420             myGroupId.insert(TGroupIdMap::value_type(aNodesGroup, aLabel));
421
422             for (i = 0; i < aNodesNb; i++) {
423               const SMDS_MeshNode* aNode = myMesh->FindNode(aRec.NodeList[i]);
424               if (aNode)
425                 aNodesGroup->Add(aNode);
426             }
427           }
428           if (aElementsNb > 0){
429             SMDS_MeshGroup* aEdgesGroup = 0;
430             SMDS_MeshGroup* aFacesGroup = 0;
431             SMDS_MeshGroup* aVolumeGroup = 0;
432             bool createdGroup = false;
433
434             for (i = 0; i < aElementsNb; i++) {
435               const SMDS_MeshElement* aElement = myMesh->FindElement(aRec.ElementList[i]);
436               if (aElement) {
437                 switch (aElement->GetType()) {
438
439                 case SMDSAbs_Edge:
440                   if (!aEdgesGroup) {
441                     aEdgesGroup = (SMDS_MeshGroup*) myGroup->AddSubGroup(SMDSAbs_Edge);
442                     if (!useSuffix && createdGroup) useSuffix = true;
443                     std::string aEdgesGrName = (useSuffix) ? aRec.GroupName + "_Edges" : aRec.GroupName;
444                     int i = aEdgesGrName.find( "\r" );
445                     if (i > 0)
446                       aEdgesGrName.erase (i, 2);
447                     myGroupNames.insert(TGroupNamesMap::value_type(aEdgesGroup, aEdgesGrName));
448                     myGroupId.insert(TGroupIdMap::value_type(aEdgesGroup, aLabel));
449                     createdGroup = true;
450                   }
451                   aEdgesGroup->Add(aElement);
452                   break;
453
454                 case SMDSAbs_Face:
455                   if (!aFacesGroup) {
456                     aFacesGroup = (SMDS_MeshGroup*) myGroup->AddSubGroup(SMDSAbs_Face);
457                     if (!useSuffix && createdGroup) useSuffix = true;
458                     std::string aFacesGrName = (useSuffix) ? aRec.GroupName + "_Faces" : aRec.GroupName;
459                     int i = aFacesGrName.find( "\r" );
460                     if (i > 0)
461                       aFacesGrName.erase (i, 2);
462                     myGroupNames.insert(TGroupNamesMap::value_type(aFacesGroup, aFacesGrName));
463                     myGroupId.insert(TGroupIdMap::value_type(aFacesGroup, aLabel));
464                     createdGroup = true;
465                   }
466                   aFacesGroup->Add(aElement);
467                   break;
468
469                 case SMDSAbs_Volume:
470                   if (!aVolumeGroup) {
471                     aVolumeGroup = (SMDS_MeshGroup*) myGroup->AddSubGroup(SMDSAbs_Volume);
472                     if (!useSuffix && createdGroup) useSuffix = true;
473                     std::string aVolumeGrName = (useSuffix) ? aRec.GroupName + "_Volumes" : aRec.GroupName;
474                     int i = aVolumeGrName.find( "\r" );
475                     if (i > 0)
476                       aVolumeGrName.erase (i, 2);
477                     myGroupNames.insert(TGroupNamesMap::value_type(aVolumeGroup, aVolumeGrName));
478                     myGroupId.insert(TGroupIdMap::value_type(aVolumeGroup, aLabel));
479                     createdGroup = true;
480                   }
481                   aVolumeGroup->Add(aElement);
482                   break;
483
484                 default:;
485                 }
486               } 
487             }
488           }
489         }
490       }
491     } 
492   }
493   catch(const std::exception& exc){
494     INFOS("Follow exception was cought:\n\t"<<exc.what());
495   }
496   catch(...){
497     INFOS("Unknown exception was cought !!!");
498   }
499   if (myMesh)
500     myMesh->compactMesh();
501   return aResult;
502 }