]> SALOME platform Git repositories - plugins/netgenplugin.git/blob - src/NETGENPlugin/run_mesher.cxx
Salome HOME
Adding support of number of thread for the mesher
[plugins/netgenplugin.git] / src / NETGENPlugin / run_mesher.cxx
1 // Copyright (C) 2007-2021  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 //  File   : run_mesher.cxx
24 //  Author : Yoann AUDOUIN, EDF
25 //  Module : SMESH
26 //
27
28 #include "DriverStep.hxx"
29 #include "DriverMesh.hxx"
30 #include "netgen_param.hxx"
31 #include "netgen_mesher.hxx"
32
33 #include <SMESH_Mesh.hxx>
34 #include <SMESH_Gen.hxx>
35
36 #include <TopoDS_Shape.hxx>
37 #include <iostream>
38
39 #include <chrono>
40
41 /**
42  * @brief Test of shape Import/Export
43  *
44  * @param shape_file
45  */
46 void test_shape(){
47
48   std::cout << "Testing Shape Import/Export" << std::endl;
49   std::string shape_file = "box.step";
50
51   TopoDS_Shape myShape;
52
53   // Test 1 import -> export cmp files
54   std::string shape_file2 = "/tmp/shape.step";
55
56   import_shape(shape_file, myShape);
57   export_shape(shape_file2, myShape);
58
59   assert(diff_step_file(shape_file, shape_file2));
60
61   // Test 2 import->export->import cmp TopoDS_Shape
62   std::string shape_file3 = "/tmp/shape2.step";
63   TopoDS_Shape myShape1, myShape2;
64
65   import_shape(shape_file, myShape1);
66
67   export_shape(shape_file3, myShape1);
68
69   import_shape(shape_file3, myShape2);
70
71   // TODO: See why this does not work
72   // TShape seems to be different
73   //assert(myShape1.IsSame(myShape2));
74
75 }
76
77 /**
78  * @brief test of mesh import/export
79  *
80  * @param mesh_file
81  */
82 void test_mesh(){
83
84   std::cout << "Testing Mesh Import/Export" << std::endl;
85   std::string mesh_file = "box.med";
86   SMESH_Gen gen;
87
88   SMESH_Mesh *myMesh = gen.CreateMesh(false);
89   std::string mesh_name = "Maillage_1";
90
91   // Test 1 import -> export cmp files
92   std::string mesh_file2 = "/tmp/mesh.med";
93
94   import_mesh(mesh_file, *myMesh, mesh_name);
95   export_mesh(mesh_file2, *myMesh, mesh_name);
96
97   assert(diff_med_file(mesh_file, mesh_file2, mesh_name));
98
99   // TODO: Compare the two med files via dump ?
100
101   // Test 2 import->export->import cmp TopoDS_Shape
102   std::string mesh_file3 = "/tmp/mesh2.med";
103   SMESH_Mesh *myMesh1 = gen.CreateMesh(false);
104   SMESH_Mesh *myMesh2 = gen.CreateMesh(false);
105
106   import_mesh(mesh_file, *myMesh1, mesh_name);
107
108   export_mesh(mesh_file3, *myMesh1, mesh_name);
109
110   import_mesh(mesh_file3, *myMesh2, mesh_name);
111
112   // TODO: Compare SMESH_Mesh
113   //assert(myMesh1==myMesh2);
114 }
115
116 /**
117  * @brief Test of import/export of netgen param
118  *
119  */
120 void test_netgen_params(){
121
122   std::string param_file = "/tmp/netgen_param.txt";
123   netgen_params myParams, myParams2;
124   myParams.has_netgen_param = true;
125   myParams.maxh = 34.64;
126   myParams.minh = 0.14;
127   myParams.segmentsperedge = 15;
128   myParams.grading = 0.2;
129   myParams.curvaturesafety = 1.5;
130   myParams.secondorder = false;
131   myParams.quad = false;
132   myParams.optimize = true;
133   myParams.fineness = 5;
134   myParams.uselocalh = true;
135   myParams.merge_solids = true;
136   myParams.chordalError = -1;
137   myParams.optsteps2d = 3;
138   myParams.optsteps3d = 3;
139   myParams.elsizeweight = 0.2;
140   myParams.opterrpow = 2;
141   myParams.delaunay = true;
142   myParams.checkoverlap = true;
143   myParams.checkchartboundary = false;
144   myParams.closeedgefac = 2;
145   myParams.has_local_size = false;
146   myParams.meshsizefilename = "";
147   myParams.has_maxelementvolume_hyp = false;
148   myParams.maxElementVolume = 0.0;
149
150   export_netgen_params(param_file, myParams);
151   import_netgen_params(param_file, myParams2);
152
153   assert(diff_netgen_params(myParams, myParams2));
154
155 };
156
157 void test_netgen3d(){
158
159   std::cout << "Testing NETGEN 3D mesher" << std::endl;
160   netgen3d("box_partial2D1D-2.med",
161            "box-2.step",
162            "box_param.txt",
163            "element_orient.dat",
164            "new_element.dat",
165            true,
166            "box_with3D.med",
167            1);
168
169   // TODO: Check result
170 }
171
172 /**
173  * @brief Main function
174  *
175  * @param argc Number of arguments
176  * @param argv Arguments
177  *
178  * @return error code
179  */
180 int main(int argc, char *argv[]){
181
182   if(argc!=10||(argc==2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help")==0))){
183     std::cout << "Error in number of argument"<<std::endl;
184     std::cout << "Syntax:"<<std::endl;
185     std::cout << "run_mesher MESHER INPUT_MESH_FILE SHAPE_FILE HYPO_FILE" << std::endl;
186     std::cout << "           ELEM_ORIENT_FILE NB_THREADS" << std::endl;
187     std::cout << "           NEW_ELEMENT_FILE OUTPUT_MESH_FILE" << std::endl;
188     std::cout << std::endl;
189     std::cout << "Args:" << std::endl;
190     std::cout << "  MESHER: mesher to use from (NETGEN3D, NETGEN2D)" << std::endl;
191     std::cout << "  INPUT_MESH_FILE: MED File containing lower-dimension-elements already meshed" << std::endl;
192     std::cout << "  SHAPE_FILE: STEP file containing the shape to mesh" << std::endl;
193     std::cout << "  HYPO_FILE: Ascii file containint the list of parameters" << std::endl;
194     std::cout << "  ELEM_ORIENT_FILE: binary file containing the list of element from INPUT_MESH_FILE associated to the shape and their orientation" << std::endl;
195     std::cout << "  NB_THREADS: Number of thread to use for the mesher" << std::endl;
196     std::cout << "  NEW_ELEMENT_FILE: (out) contains elements and nodes added by the meshing" << std::endl;
197     std::cout << "  OUTPUT_MESH: If !=0 will export mesh into OUTPUT_MESH_FILE " << std::endl;
198     std::cout << "  OUTPUT_MESH_FILE: MED File containing the mesh after the run of the mesher" << std::endl;
199     return 0;
200   }
201   std::string mesher=argv[1];
202   std::string input_mesh_file=argv[2];
203   std::string shape_file=argv[3];
204   std::string hypo_file=argv[4];
205   std::string element_orientation_file=argv[5];
206   int nbThreads=std::stoi(argv[6]);
207   std::string new_element_file=argv[7];
208   bool output_mesh = std::stoi(argv[8]) != 0;
209   std::string output_mesh_file=argv[9];
210
211   if (mesher=="test"){
212     std::cout << "Running tests" << std::endl;
213     test_shape();
214     test_mesh();
215     test_netgen_params();
216     test_netgen3d();
217   } else if (mesher=="NETGEN3D"){
218     auto begin = std::chrono::high_resolution_clock::now();
219     netgen3d(input_mesh_file,
220              shape_file,
221              hypo_file,
222              element_orientation_file,
223              new_element_file,
224              output_mesh,
225              output_mesh_file,
226              nbThreads);
227     auto end = std::chrono::high_resolution_clock::now();
228     auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
229     std::cout << "Time elapsed: " << elapsed.count()*1e-9 << std::endl;
230   } else if (mesher=="NETGEN2D"){
231     netgen2d(input_mesh_file,
232              shape_file,
233              hypo_file,
234              element_orientation_file,
235              new_element_file,
236              output_mesh,
237              output_mesh_file);
238   } else {
239     std::cerr << "Unknown mesher:" << mesher << std::endl;
240   }
241   return 0;
242 }