Created
October 23, 2015 19:19
-
-
Save jwpeterson/12e800eec520ff39e00f to your computer and use it in GitHub Desktop.
Small example demonstrating meshing of constructive solid geometry (CSG) with Netgen via libmesh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <fstream> | |
| #include <map> | |
| #include "libmesh/libmesh.h" | |
| #include "libmesh/mesh.h" | |
| #include "libmesh/point.h" | |
| #include "libmesh/cell_tet4.h" | |
| // NOTE: This example only works if you have built and installed | |
| // netgen, (see http://sourceforge.net/projects/netgen-mesher) and you | |
| // are using a Makefile that sets the appropriate include and library | |
| // paths. | |
| namespace nglib | |
| { | |
| #include <nglib.h> | |
| } | |
| using namespace libMesh; | |
| // This code adds 2D points and line segments to a Netgen Mesh object. | |
| int main (int argc, char** argv) | |
| { | |
| // Initialize libmesh. This is necessary because the library | |
| // may depend on a number of other libraries (i.e. MPI and Petsc) | |
| // that require initialization before use. | |
| LibMeshInit init(argc, argv); | |
| { | |
| // Must be called before any other nglib functions. | |
| nglib::Ng_Init(); | |
| // This code implements a slight variant of the 3D problem described in | |
| // http://www.asc.tuwien.ac.at/~lehrenfeld/xfem/exercise/ex2_twodom_poisson/twodom_poisson.pdf | |
| // The input file is in the "constructive solid geometry" format. | |
| std::string filename = "netgen_io.tmp"; | |
| { | |
| std::ofstream netgen_io(filename.c_str()); | |
| netgen_io << "algebraic3d\n"; | |
| netgen_io << "solid left = plane(-1, -1, -1; -1, 0, 0) -bc=1;\n"; | |
| netgen_io << "solid front = plane(-1, -1, -1; 0, -1, 0) -bc=3;\n"; | |
| netgen_io << "solid bottom = plane(-1, -1, -1; 0, 0, -1) -bc=5;\n"; | |
| netgen_io << "solid right = plane( 1, 1, 1; 1, 0, 0) -bc=2;\n"; | |
| netgen_io << "solid back = plane( 1, 1, 1; 0, 1, 0) -bc=4;\n"; | |
| netgen_io << "solid top = plane( 1, 1, 1; 0, 0, 1) -bc=6;\n"; | |
| netgen_io << "solid sph = sphere(0, 0, 0; 0.5); -bc=7\n"; | |
| netgen_io << "solid cube = left and right and front and back and bottom and top;\n"; | |
| // netgen_io << "solid inner = cube and sph;\n"; // uncomment this line to fill sphere | |
| netgen_io << "solid outer = cube and not sph;\n"; | |
| netgen_io << "tlo outer -maxh=0.2;\n"; | |
| // netgen_io << "tlo inner -maxh=0.15;\n"; // uncomment this line to fill sphere | |
| } | |
| // Stats for sphere-hole mesh | |
| // maxh=0.2, Points: 1172, Elements: 4665 | |
| // maxh=0.1, Points: 9677, Elements: 48091 | |
| // maxh=0.05, Points: 71264, Elements: 383019 | |
| // Call the nglib interface to generate a Mesh from the geometry | |
| // described in the file. | |
| nglib::Ng_Mesh * netgen_mesh = NULL; | |
| nglib::Ng_Meshing_Parameters mp; | |
| nglib::Ng_Result res = | |
| nglib::Ng_CSG_GenerateMeshFromGeometryFile(filename.c_str(), &netgen_mesh, &mp); | |
| // Remove the temporary file now that we are done with it. | |
| std::remove(filename.c_str()); | |
| // Build libmesh mesh to transfer points into | |
| Mesh mesh(init.comm(), 3); | |
| // Print lists of points and elements in volume mesh. | |
| { | |
| int np = nglib::Ng_GetNP(netgen_mesh); | |
| std::cout << "Points: " << np << std::endl; | |
| double point[3]; | |
| for (int i = 1; i <= np; i++) | |
| { | |
| nglib::Ng_GetPoint (netgen_mesh, i, point); | |
| // Add points to libmesh mesh, use 1-based numbering like netgen. | |
| mesh.add_point(Point(point[0], point[1], point[2]), i); | |
| } | |
| int ne = nglib::Ng_GetNE(netgen_mesh); | |
| std::cout << "Elements: " << ne << std::endl; | |
| int tet[4]; | |
| for (int i = 1; i <= ne; i++) | |
| { | |
| nglib::Ng_GetVolumeElement (netgen_mesh, i, tet); | |
| // Add tet to libmesh mesh, use 1-based numbering like | |
| // netgen. Note that we have to swap the last two entries | |
| // of the connectivity array, because Netgen uses a | |
| // clockwise numbering for some reason. | |
| Elem* elem = mesh.add_elem(new Tet4); | |
| elem->set_node(0) = mesh.node_ptr(tet[0]); | |
| elem->set_node(1) = mesh.node_ptr(tet[1]); | |
| elem->set_node(2) = mesh.node_ptr(tet[3]); | |
| elem->set_node(3) = mesh.node_ptr(tet[2]); | |
| } | |
| } | |
| // Write mesh to Netgen-style file. | |
| nglib::Ng_SaveMesh(netgen_mesh, "cube_sphere.vol"); | |
| // We're done with the Netgen mesh | |
| nglib::Ng_DeleteMesh(netgen_mesh); | |
| // We're now done calling Netgen functions | |
| nglib::Ng_Exit(); | |
| // This is necessary, otherwise I get very strange results for | |
| // some reason (points with different coordinates than they are | |
| // supposed to have! | |
| mesh.prepare_for_use(); | |
| // Write an Exodus file with the libmesh mesh | |
| mesh.write("netgen_test_04.e"); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I try to use "Ng_CSG_GenerateMeshFromGeometryFile" but i find problems. Can you say me where is this definition?
Thanks
RP