]> SALOME platform Git repositories - plugins/netgenplugin.git/blob - src/NETGENPlugin/run_mesher.cxx
Salome HOME
partial work for netgen2d in run_mesher + corrections for netgen3d nodeVec + restorin...
[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
168   // TODO: Check result
169 }
170
171 /**
172  * @brief Main function
173  *
174  * @param argc Number of arguments
175  * @param argv Arguments
176  *
177  * @return error code
178  */
179 int main(int argc, char *argv[]){
180
181   if(argc!=9||(argc==2 && (argv[1] == "-h" || argv[1]=="--help"))){
182     std::cout << "Error in number of argument"<<std::endl;
183     std::cout << "Syntax:"<<std::endl;
184     std::cout << "run_mesher MESHER INPUT_MESH_FILE SHAPE_FILE HYPO_FILE" << std::endl;
185     std::cout << "           ELEM_ORIENT_FILE NEW_ELEMENT_FILE OUTPUT_MESH_FILE" << std::endl;
186     std::cout << std::endl;
187     std::cout << "Args:" << std::endl;
188     std::cout << "  MESHER: mesher to use from (NETGEN3D, NETGEN2D)" << std::endl;
189     std::cout << "  INPUT_MESH_FILE: MED File containing lower-dimension-elements already meshed" << std::endl;
190     std::cout << "  SHAPE_FILE: STEP file containing the shape to mesh" << std::endl;
191     std::cout << "  HYPO_FILE: Ascii file containint the list of parameters" << std::endl;
192     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;
193     std::cout << "  NEW_ELEMENT_FILE: (out) contains elements and nodes added by the meshing" << std::endl;
194     std::cout << "  OUTPUT_MESH: If !=0 will export mesh into OUTPUT_MESH_FILE " << std::endl;
195     std::cout << "  OUTPUT_MESH_FILE: MED File containing the mesh after the run of the mesher" << std::endl;
196     return 0;
197   }
198   std::string mesher=argv[1];
199   std::string input_mesh_file=argv[2];
200   std::string shape_file=argv[3];
201   std::string hypo_file=argv[4];
202   std::string element_orientation_file=argv[5];
203   std::string new_element_file=argv[6];
204   bool output_mesh = std::stoi(argv[7]) != 0;
205   std::string output_mesh_file=argv[8];
206
207   if (mesher=="test"){
208     std::cout << "Running tests" << std::endl;
209     test_shape();
210     test_mesh();
211     test_netgen_params();
212     test_netgen3d();
213   } else if (mesher=="NETGEN3D"){
214     auto begin = std::chrono::high_resolution_clock::now();
215     netgen3d(input_mesh_file,
216              shape_file,
217              hypo_file,
218              element_orientation_file,
219              new_element_file,
220              output_mesh,
221              output_mesh_file);
222     auto end = std::chrono::high_resolution_clock::now();
223     auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
224     std::cout << "Time elapsed: " << elapsed.count()*1e-9 << std::endl;
225   } else if (mesher=="NETGEN2D"){
226     netgen2d(input_mesh_file,
227              shape_file,
228              hypo_file,
229              element_orientation_file,
230              new_element_file,
231              output_mesh,
232              output_mesh_file);
233   } else {
234     std::cerr << "Unknown mesher:" << mesher << std::endl;
235   }
236   return 0;
237 }