Salome HOME
a23c7e9ce590816fa5026dfe48ba522f86362481
[modules/smesh.git] / test / netgen_runner.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_param_file(param_file):
24     """ Create a parameter file for runner """
25     param="""1
26 34.64
27 0.14
28 16
29 0.15
30 1.5
31 0
32 0
33 1
34 5
35 1
36 1
37 -1
38 3
39 3
40 0.2
41 2
42 1
43 0
44 0
45 2
46 0
47
48 0
49 0"""
50     with open(param_file, "w") as ffile:
51         ffile.write(param)
52
53
54 def test_netgen3d():
55     """ Test netgen3d mesher """
56     # Building geometry
57     box = geompy.MakeBoxDXDYDZ(200, 200, 200)
58
59     geompy.ExtractShapes(box, geompy.ShapeType["FACE"], True)
60     Groupe_1 = geompy.CreateGroup(box, geompy.ShapeType["FACE"])
61     geompy.UnionIDs(Groupe_1, [3, 13, 23, 27, 31, 33])
62
63     # TODO: useful ?
64     [_, _, _, _, _, _, Groupe_1] = geompy.GetExistingSubObjects(box, False)
65
66     # Creating 2D mesh
67     NETGEN_2D_Parameters_1 = smesh.CreateHypothesisByAverageLength(
68         'NETGEN_Parameters_2D', 'NETGENEngine', 34.641, 0)
69     Mesh2D = smesh.Mesh(Groupe_1, 'Maillage_1')
70     status = Mesh2D.AddHypothesis(Groupe_1, NETGEN_2D_Parameters_1)
71     NETGEN_1D_2D = Mesh2D.Triangle(algo=smeshBuilder.NETGEN_1D2D)
72     isDone = Mesh2D.Compute()
73     smesh.SetName(Mesh2D, 'Maillage_1')
74
75     # tmp_dir = tempfile.mkdtemp()
76     with tempfile.TemporaryDirectory() as tmp_dir:
77       mesh_file = path.join(tmp_dir, "mesh.med")
78       shape_file = path.join(tmp_dir, "shape.step")
79       param_file = path.join(tmp_dir, "param.txt")
80       output_mesh = path.join(tmp_dir, "mesh3D.med")
81
82       print("Running in folder: ", tmp_dir)
83       create_param_file(param_file)
84
85       Mesh2D.ExportMED(mesh_file, 0, 41, 1, Mesh2D, 1, [], '', -1, 1)
86       geompy.ExportSTEP(box, shape_file, GEOM.LU_METER)
87
88       runner = path.join("${NETGENPLUGIN_ROOT_DIR}",
89                           "bin",
90                           "salome",
91                           "NETGENPlugin_Runner")
92
93       cmd = "{runner} NETGEN3D {mesh_file} {shape_file} "\
94             "{param_file} NONE 2 NONE {output_mesh}"\
95             .format(runner=runner,
96                     mesh_file=mesh_file,
97                     shape_file=shape_file,
98                     param_file=param_file,
99                     output_mesh=output_mesh)
100       print(cmd)
101       subprocess.check_call(cmd, shell=True)
102
103       meshRead = mc.ReadUMeshFromFile (output_mesh, "MESH", 0)
104
105       nbTetras = meshRead.getNumberOfCellsWithType(mc.NORM_TETRA4)
106       nbPoints = meshRead.getNumberOfNodes()
107
108       meshRead = mc.ReadUMeshFromFile (output_mesh, "MESH", -1)
109       nbTriangles = meshRead.getNumberOfCellsWithType(mc.NORM_TRI3)
110
111       meshRead = mc.ReadUMeshFromFile (output_mesh, "MESH", -2)
112       nbSegments = meshRead.getNumberOfCellsWithType(mc.NORM_SEG2)
113
114       print("Nb Tetras:", nbTetras)
115       print("Nb Triangles:", nbTriangles)
116       print("Nb Segments:", nbSegments)
117       print("Nb Points:", nbPoints)
118
119       assert(nbPoints > 0)
120       assert(nbSegments > 0)
121       assert(nbTriangles > 0)
122       assert(nbTetras > 0)
123
124 if __name__ == "__main__":
125     test_netgen3d()