]> SALOME platform Git repositories - modules/smesh.git/blob - test/netgen_runner_1D2D3D.py
Salome HOME
[bos #40505][CEA] Allow assign of 3D Tetra paramam on imported 2D mesh.
[modules/smesh.git] / test / netgen_runner_1D2D3D.py
1 #!/usr/bin/env python
2 import sys
3 import salome
4
5 salome.salome_init()
6
7 from os import path
8 import tempfile
9 import subprocess
10
11 import GEOM, SMESH, SALOMEDS
12
13 from salome.geom import geomBuilder
14 from salome.smesh import smeshBuilder
15
16 import math
17
18 smesh = smeshBuilder.New()
19 geompy = geomBuilder.New()
20
21 import medcoupling as mc
22
23 def create_simple2d_param_file_1D(param_file,segments,segLenght,maxArea):
24     """ Create a parameter file for runner """
25     param = """1
26 {}
27 {}
28 {}
29 0""".format(segments,segLenght,maxArea)
30     with open(param_file, "w") as ffile:
31         ffile.write(param)
32
33 def create_simple2d3d_param_file_1D(param_file,segments,segLenght,maxArea,maxVolume):
34     """ Create a parameter file for runner """
35     param = """1
36 {}
37 {}
38 {}
39 {}
40 0""".format(segments,segLenght,maxArea,maxVolume)
41
42     with open(param_file, "w") as ffile:
43         ffile.write(param)
44
45 def CommonFunction(netgen,case,segments,segLenght,maxArea,maxVolume):
46     # Building geometry
47     box = geompy.MakeBoxDXDYDZ(200, 200, 200)
48
49     # Create 1D regular elements
50     mesh_1d = smesh.Mesh(box, 'Maillage_1')
51     mesh_1d.Segment().NumberOfSegments(1)
52     isDone = mesh_1d.Compute()
53
54     if not isDone:
55         raise Exception("Error when computing Mesh")
56
57     smesh.SetName(mesh_1d, 'Maillage_1')
58     nb_points = 0
59     nb_segments = 0
60     nb_triangles = 0
61     nb_tetras = 0
62     with tempfile.TemporaryDirectory() as tmp_dir:
63         mesh_file = path.join(tmp_dir, "mesh.med")
64         shape_file = path.join(tmp_dir, "shape.brep")
65         if ( case <= 2 ):
66             param_file = path.join(tmp_dir, "param_simple2D.txt")
67         else: 
68             param_file = path.join(tmp_dir, "param_simple3D.txt")
69
70         output_mesh = path.join(tmp_dir, "meshout.med")
71
72         print("Running in folder: ", tmp_dir)
73         
74         if ( case <= 2 ):
75             create_simple2d_param_file_1D(param_file, segments, segLenght, maxArea )
76         else:
77             create_simple2d3d_param_file_1D(param_file, segments, segLenght, maxArea, maxVolume )
78         
79         mesh_1d.ExportMED(mesh_file, 0, 41, 1, mesh_1d, 1, [], '', -1, 1)
80         geompy.ExportBREP( box, shape_file )
81
82         runner = path.join("${NETGENPLUGIN_ROOT_DIR}",
83                             "bin",
84                             "salome",
85                             "NETGENPlugin_Runner")
86
87         if sys.platform == 'win32':
88             runner += ".exe"
89
90         cmd = "{runner} {NETGEN} {mesh_file} {shape_file} "\
91                 "{param_file} NONE NONE {output_mesh}"\
92                 .format(runner=runner,
93                         NETGEN=netgen,
94                         mesh_file=mesh_file,
95                         shape_file=shape_file,
96                         param_file=param_file,
97                         output_mesh=output_mesh)
98         print(cmd)
99         subprocess.check_call(cmd, shell=True)   
100         mesh_read = mc.ReadUMeshFromFile(output_mesh, "MESH", 0)
101         nb_points = mesh_read.getNumberOfNodes()
102         if (case == 1):
103             nb_segments = mesh_read.getNumberOfCellsWithType(mc.NORM_SEG2)
104         if (case == 2):
105             nb_triangles = mesh_read.getNumberOfCellsWithType(mc.NORM_TRI3)
106             mesh_read    = mc.ReadUMeshFromFile(output_mesh, "MESH", -1)
107             nb_segments  = mesh_read.getNumberOfCellsWithType(mc.NORM_SEG2)
108         if (case == 3):
109             nb_tetras = mesh_read.getNumberOfCellsWithType(mc.NORM_TETRA4)
110             mesh_read = mc.ReadUMeshFromFile(output_mesh, "MESH", -1)
111             nb_triangles = mesh_read.getNumberOfCellsWithType(mc.NORM_TRI3)
112             mesh_read = mc.ReadUMeshFromFile(output_mesh, "MESH", -2)
113             nb_segments = mesh_read.getNumberOfCellsWithType(mc.NORM_SEG2)
114             
115     return [nb_points,nb_segments,nb_triangles,nb_tetras] 
116
117 def test_netgen1d():
118     [nb_points,nb_segments,nb_triangles,nb_tetras] = CommonFunction( "NETGEN1D", 1, 1, 200, 0.0, 0.0 )
119
120     print("Nb Points:", nb_points)
121     print("Nb Segments:", nb_segments)
122     assert nb_points > 0
123     assert nb_segments > 0
124     
125
126 def test_netgen1d2d():
127     """ Test netgen1d2d mesher """
128     [nb_points,nb_segments,nb_triangles,nb_tetras] = CommonFunction( "NETGEN1D2D", 2, 1, 200, 0.0, 0.0 )
129     
130     print("Nb Triangles:", nb_triangles)
131     print("Nb Segments:", nb_segments)
132     print("Nb Points:", nb_points)
133
134     assert nb_triangles == 12
135     assert nb_points > 0
136     assert nb_segments > 0
137
138 def test_netgen1d2dMaxArea():
139     """ Test netgen1d2d mesher """
140     [nb_points,nb_segments,nb_triangles, nb_tetras] = CommonFunction( "NETGEN1D2D", 2, 5, 200, 50.0, 0.0 )
141     
142     print("Nb Triangles:", nb_triangles)
143     print("Nb Segments:", nb_segments)
144     print("Nb Points:", nb_points)
145
146     assert nb_triangles > 12
147     assert nb_points > 0
148     assert nb_segments > 0
149
150                 
151 def test_netgen1d2d3d():
152     """ Test netgen1d2d mesher """
153     [nb_points,nb_segments,nb_triangles,nb_tetras] = CommonFunction( "NETGEN1D2D3D", 3, 1, 200, 0.0, 0.0 )
154
155     print("Nb Tetras:", nb_tetras)
156     print("Nb Triangles:", nb_triangles)
157     print("Nb Segments:", nb_segments)
158     print("Nb Points:", nb_points)
159     
160     assert nb_triangles == 12
161     assert nb_tetras == 5
162     assert nb_points > 0
163     assert nb_segments > 0
164
165 def test_netgen1d2dMaxVolume():
166     """ Test netgen1d2d mesher """
167     [nb_points,nb_segments,nb_triangles,nb_tetras] = CommonFunction( "NETGEN1D2D3D", 3, 1, 200, 500.0, 500.0 )
168
169     print("Nb Tetras:", nb_tetras)
170     print("Nb Triangles:", nb_triangles)
171     print("Nb Segments:", nb_segments)
172     print("Nb Points:", nb_points)
173     
174     assert nb_triangles > 12
175     assert nb_tetras > 5
176     assert nb_points > 0
177     assert nb_segments > 0
178
179 if __name__ == "__main__":
180     if sys.platform == "win32":
181         print("Disabled on windows")
182         sys.exit(0)
183     test_netgen1d()
184     test_netgen1d2d()
185     test_netgen1d2d3d()
186     test_netgen1d2dMaxArea()
187     test_netgen1d2dMaxVolume()