[Gmsh] Build the mesh in my own program

Mikhail Artemiev artemiev.mikhail at ngs.ru
Wed Dec 1 05:36:43 CET 2010


Hi everybody!
I continue to learn Gmsh API functions.
This is a simple example of how I use these funtions:

// --- main.cpp ---

//#include suitable files

class Elem // abstract geometrical element
{
public:
   char geoFileName[256];
   char mshFileName[256];
   void Init(int Number)
   {
     // initialization of the names
     // of .geo file (geoFileName) and .msh file 
(mshFileName)
   }
   void CreateGeo()
   {
     // write .geo file
   }
   // create 2D mesh using Gmsh API functions
   void CreateMsh()
   {
     GmshInitialize();
     GModel *m = new GModel();
     m->readGEO(geoFileName);
     m->mesh(2);
     m->writeMSH(mshFileName, 2.1, true);
     delete m;
     GmshFinalize();
   }
};

int main(int argc, char **argv)
{
   int i, N = 3;
   Elem *elem = new Elem[N];
   for (i = 0; i < N; i++) {
     elem[i].Init(i);
     elem[i].CreateGeo();
     elem[i].CreateMsh();
   }
   delete[] elem;
}

// --- end of main.cpp ---

The full text of main.cpp is attached.

Now I encountered such trouble: if I use Gmsh API in 
parallel version of my code, my program crushes under 
execution.
For parallelization I use OpenMP pragmas:

// --- main_omp.cpp ---

// the part of main.cpp without changes

int main(int argc, char **argv)
{
   int i, N = 3;
   Elem *elem = new Elem[N];
   // parallelization with OpenMP
   omp_set_num_threads(N);
   #pragma omp parallel for private(i)
   for (i = 0; i < N; i++) {
     elem[i].Init(i);
     elem[i].CreateGeo();
     elem[i].CreateMsh();
   }
   delete[] elem;
}

// --- end of main_omp.cpp ---

The full text of main_omp.cpp is attached too.

I tried to use funtions GmshInitialize() and 
GmshFinalize() in main funtion to avoid these global 
instructions in parallel part of code, but it didn't work. 
How can I use Gmsh API in my parallel code?

Thanks
Mikhail Artemiev, PhD student, NSTU
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: main.cpp
URL: <http://www.geuz.org/pipermail/gmsh/attachments/20101201/b8dd32b4/attachment.cpp>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: main_omp.cpp
URL: <http://www.geuz.org/pipermail/gmsh/attachments/20101201/b8dd32b4/attachment-0001.cpp>