Generating different mesh using .geo file and C++ API
Hello, I'm using the Ubuntu 22.04.4 LTS system and I downloaded the GMSH SDK files version 4.12.2. I added the include/gmsh.h and include/gmshc.h files to /usr/include and the lib/libgmsh.so, lib/libgmsh.so.4.12 and lib/libgmsh.so.4.12.2 files to /usr/lib.
I created the following test.geo file:
SetFactory("OpenCASCADE");
lc1 = 500;
lc2 = 1000;Point(1) = {392752,7649844,-2686, lc1};
Point(2) = {393138,7650304,-2686, lc1};
Point(3) = {392908,7650496,-2686, lc1};
Point(4) = {392523,7650037,-2686, lc1};Point(5) = {393516,7648420,-2748, lc2};
Point(6) = {394673,7649799,-2748, lc2};
Point(7) = {393064,7651149,-2748, lc2};
Point(8) = {392357,7650306,-2748, lc2};
Point(9) = {391974,7650628,-2748, lc2};
Point(10) = {390407,7649071,-2748, lc2};
Point(11) = {390790,7648749,-2748, lc2};
Point(12) = {392532,7649115,-2748, lc2};Line(1) = {1, 2};
Line(2) = {2, 3};
Line(3) = {3, 4};
Line(4) = {4, 1};Line(5) = {5, 6};
Line(6) = {6, 7};
Line(7) = {7, 8};
Line(8) = {8, 9};
Line(9) = {9, 10};
Line(10) = {10, 11};
Line(11) = {11, 12};
Line(12) = {12, 5};Curve Loop(1) = {1, 2, 3, 4};
Surface(1) = {1};Line(13) = {3, 7};
Line(14) = {4, 8};Curve Loop(3) = {14, 8, 9, 10, 11, 12, 5, 6, -13, -2, -1, -4};
Surface(2) = {3};
Curve Loop(5) = {13, 7, -14, -3};
Surface(3) = {5};Curve Loop(7) = {-12, -11, -10, -9, -8, -7 ,-6 ,-5};
Surface(4) = {7};
Running GMSH and opening the file and creating the 2D mesh, we obtain the following mesh with 55 nodes and 143 elements:
As I need the geometry to be built automatically given points, I did a simple test using the GMSH API C++. I wrote the following test.cpp file:
#include
#include <gmsh.h>using namespace std;
int main(int argc, char** argv)
{gmsh::initialize(argc, argv);
double lc1 = 500.0;
double lc2 = 1000.0;gmsh::model::occ::addPoint(392752,7649844,-2686, lc1, 1);
gmsh::model::occ::addPoint(393138,7650304,-2686, lc1, 2);
gmsh::model::occ::addPoint(392908,7650496,-2686, lc1, 3);
gmsh::model::occ::addPoint(392523,7650037,-2686, lc1, 4);gmsh::model::occ::addPoint(393516,7648420,-2748, lc2, 5 );
gmsh::model::occ::addPoint(394673,7649799,-2748, lc2, 6 );
gmsh::model::occ::addPoint(393064,7651149,-2748, lc2, 7 );
gmsh::model::occ::addPoint(392357,7650306,-2748, lc2, 8 );
gmsh::model::occ::addPoint(391974,7650628,-2748, lc2, 9 );
gmsh::model::occ::addPoint(390407,7649071,-2748, lc2, 10);
gmsh::model::occ::addPoint(390790,7648749,-2748, lc2, 11);
gmsh::model::occ::addPoint(392532,7649115,-2748, lc2, 12);gmsh::model::occ::addLine(1, 2, 1);
gmsh::model::occ::addLine(2, 3, 2);
gmsh::model::occ::addLine(3, 4, 3);
gmsh::model::occ::addLine(4, 1, 4);gmsh::model::occ::addLine(5, 6 , 5);
gmsh::model::occ::addLine(6, 7 , 6);
gmsh::model::occ::addLine(7, 8 , 7);
gmsh::model::occ::addLine(8, 9 , 8);
gmsh::model::occ::addLine(9, 10, 9);
gmsh::model::occ::addLine(10, 11, 10);
gmsh::model::occ::addLine(11, 12, 11);
gmsh::model::occ::addLine(12, 5, 12);gmsh::model::occ::addCurveLoop({1, 2, 3, 4}, 1);
gmsh::model::occ::addSurfaceFilling({1}, 1);gmsh::model::occ::addLine(3, 7, 13);
gmsh::model::occ::addLine(4, 8, 14);gmsh::model::occ::addCurveLoop({14, 8, 9, 10, 11, 12, 5, 6, -13, -2, -1, -4}, 3);
gmsh::model::occ::addSurfaceFilling({3}, 2);gmsh::model::occ::addCurveLoop({13, 7, -14, -3}, 5);
gmsh::model::occ::addSurfaceFilling({5}, 3);gmsh::model::occ::addCurveLoop({-12, -11, -10, -9, -8, -7, -6, -5}, 7);
gmsh::model::occ::addSurfaceFilling({7}, 4);gmsh::model::occ::synchronize();
gmsh::model::mesh::generate(2);
gmsh::write("Files/Jubarte_teste.msh");set args(argv, argv + argc);
if (!args.count("-nopopup"))
gmsh::fltk::run();gmsh::finalize();
return 0;}
I compiled the file using:
g++ -o test test.cpp -lgmsh
I ran:
./test
Generated the following mesh with 56 nodes and 145 elements:
Note that the number of nodes and elements are different, and the mesh generated by the .cpp file has a raised relief that should not appear. We can see the desired mesh correctly generated by the .geo file.
I would like to know what I am doing wrong that the mesh generated by the C++ API is not correct.
Once I had GMSH version 4.6.0, I opened the test.geo file in it and generated the 2D mesh. Interestingly, the result in this version generated a mesh with 56 nodes and 145 elements and the same format as that generated by API 4.12.2.
I believe something was fixed in recent versions of GMSH that in the API is not right.