NEMoSys  0.63.0
A modular, extensible resource with robust automated mesh generation, mesh quality analysis, adaptive mesh refinement, and data transfer between arbitrary meshes.
netgenGen.C
Go to the documentation of this file.
1 /*******************************************************************************
2 * Promesh *
3 * Copyright (C) 2022, IllinoisRocstar LLC. All rights reserved. *
4 * *
5 * Promesh is the property of IllinoisRocstar LLC. *
6 * *
7 * IllinoisRocstar LLC *
8 * Champaign, IL *
9 * www.illinoisrocstar.com *
10 * promesh@illinoisrocstar.com *
11 *******************************************************************************/
12 /*******************************************************************************
13 * This file is part of Promesh *
14 * *
15 * This version of Promesh is free software: you can redistribute it and/or *
16 * modify it under the terms of the GNU Lesser General Public License as *
17 * published by the Free Software Foundation, either version 3 of the License, *
18 * or (at your option) any later version. *
19 * *
20 * Promesh is distributed in the hope that it will be useful, but WITHOUT ANY *
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more *
23 * details. *
24 * *
25 * You should have received a copy of the GNU Lesser General Public License *
26 * along with this program. If not, see <https://www.gnu.org/licenses/>. *
27 * *
28 *******************************************************************************/
31 
32 #include "AuxiliaryFunctions.H"
33 
34 
36  : mp(), refine_with_geom(), refine_without_geom()
37 {
38  std::cout << "initializing netgen mesh generator" << std::endl;
39  nglib::Ng_Init();
40  mesh = nglib::Ng_NewMesh();
41 }
42 
45 {
46  std::cout << "initializing netgen mesh generator" << std::endl;
47  nglib::Ng_Init();
48  set_mp(params);
49  mesh = nglib::Ng_NewMesh();
50 }
51 
53 {
54  std::cout << "finalizing netgen mesh generator" << std::endl;
55  if (mesh)
56  {
57  nglib::Ng_DeleteMesh(mesh);
58  mesh = nullptr;
59  }
60  nglib::Ng_Exit();
61 }
62 
63 
64 void netgenGen::set_mp(const netgenParams* params)
65 {
66  mp.uselocalh = params->uselocalh;
67  mp.maxh = params->maxh;
68  mp.fineness = params->fineness;
69  mp.grading = params->grading;
70  mp.elementsperedge = params->elementsperedge;
71  mp.elementspercurve = params->elementspercurve;
72  mp.closeedgeenable = params->closeedgeenable;
73  mp.closeedgefact = params->closeedgefact;
74  mp.second_order = params->second_order;
75  mp.meshsize_filename = const_cast<char *>(params->meshsize_filename.c_str());
76  mp.quad_dominated = params->quad_dominated;
77  mp.optvolmeshenable = params->optvolmeshenable;
78  mp.optsteps_2d = params->optsteps_2d;
79  mp.optsteps_3d = params->optsteps_3d;
80  mp.invert_tets = params->invert_tets;
81  mp.invert_trigs = params->invert_trigs;
82  mp.check_overlap = params->check_overlap;
83  mp.check_overlapping_boundary = params->check_overlapping_boundary;
86 }
87 
88 int netgenGen::createMeshFromSTL(const char *fname)
89 {
90  // Define pointer to STL Geometry
91  nglib::Ng_STL_Geometry *stl_geom;
92 
93  // Result of Netgen Operations
94  nglib::Ng_Result ng_res;
95 
96  int np, ne;
97 
98  // Check that the STL file exists
99  std::ifstream inSTL(fname);
100  if (!inSTL.good()) {
101  std::cerr << "Error reading in STL file: " << fname << std::endl;
102  return 1;
103  }
104 
105  // Read in the STL File
106  stl_geom = nglib::Ng_STL_LoadGeometry(fname);
107 
108  // This object seems to always be defined. If the file does
109  // not exits, the process continues with no triangle facets
110  // loaded.
111  if (!stl_geom)
112  {
113  std::cerr << "Error reading in STL File: " << fname << std::endl;
114  return 1;
115  }
116  std::cout << "Successfully loaded STL File: " << fname << std::endl;
117 
118  std::cout << "Initialise the STL Geometry structure...." << std::endl;
119  ng_res = nglib::Ng_STL_InitSTLGeometry(stl_geom);
120  if (ng_res != nglib::NG_OK)
121  {
122  std::cerr << "Error Initialising the STL Geometry....Aborting!!" << std::endl;
123  return 1;
124  }
125 
126  std::cout << "Start Edge Meshing...." << std::endl;
127  ng_res = nglib::Ng_STL_MakeEdges(stl_geom, mesh, &mp);
128  if (ng_res != nglib::NG_OK)
129  {
130  std::cerr << "Error in Edge Meshing....Aborting!!" << std::endl;
131  return 1;
132  }
133 
134  std::cout << "Start Surface Meshing...." << std::endl;
135  ng_res = nglib::Ng_STL_GenerateSurfaceMesh(stl_geom, mesh, &mp);
136  if (ng_res != nglib::NG_OK)
137  {
138  std::cerr << "Error in Surface Meshing....Aborting!!" << std::endl;
139  return 1;
140  }
141 
142  std::cout << "Start Volume Meshing...." << std::endl;
143  ng_res = nglib::Ng_GenerateVolumeMesh(mesh, &mp);
144  if (ng_res != nglib::NG_OK)
145  {
146  std::cerr << "Error in Volume Meshing....Aborting!!" << std::endl;
147  return 1;
148  }
149 
150  std::cout << "Meshing successfully completed....!!" << std::endl;
151 
152  // volume mesh output
153  np = nglib::Ng_GetNP(mesh);
154  std::cout << "Points: " << np << std::endl;
155 
156  ne = nglib::Ng_GetNE(mesh);
157  std::cout << "Elements: " << ne << std::endl;
158 
159  // refinement without or with geometry adaption:
161  nglib::Ng_Uniform_Refinement(mesh);
162  else if (refine_with_geom)
163  nglib::Ng_STL_Uniform_Refinement(stl_geom, mesh);
164 
165  std::cout << "elements after refinement: " << nglib::Ng_GetNE(mesh)
166  << std::endl;
167  std::cout << "points after refinement: " << nglib::Ng_GetNP(mesh)
168  << std::endl;
169 
170  std::cout << "Saving Mesh in VOL Format...." << std::endl;
171  std::string newfname(fname);
172  nglib::Ng_SaveMesh(mesh, nemAux::trim_fname(newfname, ".vol").c_str());
173  return 0;
174 }
bool refine_with_geom
Definition: netgenParams.H:62
int createMeshFromSTL(const char *fname) override
Definition: netgenGen.C:88
double elementsperedge
Definition: netgenParams.H:48
bool second_order
Definition: netgenParams.H:52
bool refine_without_geom
Definition: netgenGen.H:64
bool quad_dominated
Definition: netgenParams.H:54
std::string trim_fname(const std::string &name, const std::string &ext)
nglib::Ng_Meshing_Parameters mp
Definition: netgenGen.H:61
bool check_overlapping_boundary
Definition: netgenParams.H:61
double grading
Definition: netgenParams.H:47
bool invert_trigs
Definition: netgenParams.H:59
bool refine_without_geom
Definition: netgenParams.H:63
~netgenGen() override
Definition: netgenGen.C:52
std::string meshsize_filename
Definition: netgenParams.H:53
bool check_overlap
Definition: netgenParams.H:60
bool optvolmeshenable
Definition: netgenParams.H:55
void set_mp(const netgenParams *params)
Definition: netgenGen.C:64
bool refine_with_geom
Definition: netgenGen.H:63
double closeedgefact
Definition: netgenParams.H:51
bool invert_tets
Definition: netgenParams.H:58
nglib::Ng_Mesh * mesh
Definition: netgenGen.H:62
double fineness
Definition: netgenParams.H:46
bool closeedgeenable
Definition: netgenParams.H:50
double elementspercurve
Definition: netgenParams.H:49
netgenGen()
Definition: netgenGen.C:35