Salome HOME
b7388ec4a6530b01bde9ab194571acc631133550
[modules/smesh.git] / test / SMESH_controls_scaled_jacobian.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2016-2023  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 #  File   : SMESH_controls_scaled_jacobian.py
22 #  Author : Cesar Conopoima
23 #  Module : SMESH
24 #
25 import salome
26 import math
27 salome.salome_init_without_session()
28
29 import GEOM
30 import  SMESH
31 from salome.geom import geomBuilder
32 from salome.smesh import smeshBuilder
33
34 def assertWithDelta( refval, testvals, delta ):
35   return ( refval <= testvals+delta and refval >= testvals-delta ) 
36
37 geompy = geomBuilder.New()
38 smesh_builder = smeshBuilder.New()
39
40 Box_1 = geompy.MakeBoxDXDYDZ(10, 10, 10)
41 geompy.addToStudy( Box_1, 'Box_1' )
42
43 smesh = smeshBuilder.New()
44
45 Mesh_1 = smesh.Mesh(Box_1,'Mesh_1')
46 NETGEN_1D_2D_3D = Mesh_1.Tetrahedron(algo=smeshBuilder.NETGEN_1D2D3D)
47 Done = Mesh_1.Compute()
48
49 if not Done:
50   raise Exception("Error when computing NETGEN_1D2D3D Mesh for quality control test")
51
52 #For tetra elements
53 perfect   = 1.0
54 externals = math.sqrt( 2.0 )/2.0
55 notPerfectElements  = smesh.GetFilter(SMESH.VOLUME, SMESH.FT_ScaledJacobian, SMESH.FT_LessThan, perfect - 1e-12 )
56 perfectElements     = smesh.GetFilter(SMESH.VOLUME, SMESH.FT_ScaledJacobian, SMESH.FT_EqualTo, perfect )
57 externalElements    = smesh.GetFilter(SMESH.VOLUME, SMESH.FT_ScaledJacobian, SMESH.FT_EqualTo, externals )
58
59 notPerfectIds = Mesh_1.GetIdsFromFilter(notPerfectElements)
60 perfectIds    = Mesh_1.GetIdsFromFilter(perfectElements)
61 externalsIds  = Mesh_1.GetIdsFromFilter(externalElements)
62
63 assert( len(notPerfectIds)  == 4 )
64 assert( len(perfectIds)     == 1 )
65 assert( len(externalsIds)   == 4 )
66
67 # Test GetScaledJacobian by elementId
68 for id in range(len(perfectIds)):
69   assert( assertWithDelta( perfect, Mesh_1.GetScaledJacobian( perfectIds[ id ] ), 1e-12) )
70
71 for id in range(len(externalsIds)):
72   assert( assertWithDelta( externals, Mesh_1.GetScaledJacobian( externalsIds[ id ] ), 1e-12) )
73
74 #For hexa elements
75 Mesh_2 = smesh.Mesh(Box_1,'Mesh_2')
76 Cartesian_3D = Mesh_2.BodyFitted()
77 Body_Fitting_Parameters_1 = Cartesian_3D.SetGrid([ [ '5.0' ], [ 0, 1 ]],[ [ '5.0' ], [ 0, 1 ]],[ [ '5.0' ], [ 0, 1 ]],4,0)
78 Done = Mesh_2.Compute()
79
80 if not Done:
81   raise Exception("Error when computing BodyFitted Mesh for quality control test")
82
83 notPerfectIds = Mesh_2.GetIdsFromFilter(notPerfectElements)
84 perfectIds    = Mesh_2.GetIdsFromFilter(perfectElements)
85
86 assert( len(notPerfectIds)  == 0 )
87 assert( len(perfectIds)     == 8 )
88
89 # Test GetScaledJacobian by elementId
90 for id in range(len(perfectIds)):
91   assert( assertWithDelta( perfect, Mesh_2.GetScaledJacobian( perfectIds[ id ] ), 1e-12) )
92
93 #For hexa elements with poor quality
94 Mesh_3 = smesh.Mesh(Box_1,'Mesh_3')
95 Cartesian_3D = Mesh_3.BodyFitted()
96 Body_Fitting_Parameters_1 = Cartesian_3D.SetGrid([ [ '5.0' ], [ 0, 1 ]],[ [ '5.0' ], [ 0, 1 ]],[ [ '5.0' ], [ 0, 1 ]],4,0)
97 Body_Fitting_Parameters_1.SetAxesDirs( SMESH.DirStruct( SMESH.PointStruct ( 1, 0, 1 )), SMESH.DirStruct( SMESH.PointStruct ( 0, 1, 0 )), SMESH.DirStruct( SMESH.PointStruct ( 0, 0, 1 )) )
98 Done = Mesh_3.Compute()
99
100 if not Done:
101   raise Exception("Error when computing BodyFitted Distorted Mesh for quality control test")
102
103 #Polyhedrons return zero scaled jacobian because of lack for a decomposition into simpler forms
104 Polys   = 0.0
105 #Hexahedrons that are distorted by an angle of 45 
106 # Scaled Jacobian which is a measure of elements distortion 
107 # will return cos(45) = math.sqrt( 2.0 )/2.0 
108 distorted = math.sqrt( 2.0 )/2.0 
109 polysElements     = smesh.GetFilter(SMESH.VOLUME, SMESH.FT_ScaledJacobian, SMESH.FT_EqualTo, Polys )
110 distortedElements = smesh.GetFilter(SMESH.VOLUME, SMESH.FT_ScaledJacobian, SMESH.FT_EqualTo, distorted )
111
112 polysIds        = Mesh_3.GetIdsFromFilter(polysElements)
113 distortedIds    = Mesh_3.GetIdsFromFilter(distortedElements)
114
115 assert( len(polysIds)     == 4 )
116 assert( len(distortedIds) == 8 )
117
118 # Test GetScaledJacobian by elementId
119 for id in range(len(distortedIds)):
120   assert( assertWithDelta( distorted, Mesh_3.GetScaledJacobian( distortedIds[ id ] ), 1e-12 ) )
121
122 #Test the pentahedron
123 Mesh_4 = smesh.Mesh(Box_1,'Mesh_4')
124 Cartesian_3D = Mesh_4.BodyFitted()
125 Body_Fitting_Parameters_1 = Cartesian_3D.SetGrid([ [ '4' ], [ 0, 1 ]],[ [ '4' ], [ 0, 1 ]],[ [ '4' ], [ 0, 1 ]],4,0)
126 Body_Fitting_Parameters_1.SetAxesDirs( SMESH.DirStruct( SMESH.PointStruct ( 1, 0, 1 )), SMESH.DirStruct( SMESH.PointStruct ( 0, 1, 0 )), SMESH.DirStruct( SMESH.PointStruct ( 0, 0, 1 )) )
127 Body_Fitting_Parameters_1.SetSizeThreshold( 4 )
128 Body_Fitting_Parameters_1.SetToAddEdges( 0 )
129 Body_Fitting_Parameters_1.SetGridSpacing( [ '2' ], [ 0, 1 ], 0 )
130 Body_Fitting_Parameters_1.SetGridSpacing( [ '2' ], [ 0, 1 ], 1 )
131 Body_Fitting_Parameters_1.SetGridSpacing( [ '2' ], [ 0, 1 ], 2 )
132 Done = Mesh_4.Compute()
133
134 if not Done:
135   raise Exception("Error when computing BodyFitted Distorted Mesh for quality control test")
136
137 pentahedrons = 0.6
138 pentasAndPolys     = smesh.GetFilter(SMESH.VOLUME, SMESH.FT_ScaledJacobian, SMESH.FT_LessThan, pentahedrons )
139 #Distorted hexas
140
141 polysIds          = Mesh_4.GetIdsFromFilter(polysElements)
142 pentasAndPolysIds = Mesh_4.GetIdsFromFilter(pentasAndPolys)
143
144 assert( len(pentasAndPolysIds) - len(polysIds) == 10 )