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
#include <iostream>
#include <fstream>
#include <gmsh/Gmsh.h>
#include <gmsh/GModel.h>

class Elem
{
public:
  char geoFileName[256];
  char mshFileName[256];

  void Init(int Number)
  {
    char ch[8];
    itoa(Number, ch, 10);
    strcpy(geoFileName, ch);
    strcat(geoFileName, ".geo");
    strcpy(mshFileName, ch);
    strcat(mshFileName, ".msh");
  }

  void CreateGeo()
  {
    std::ofstream out(geoFileName);
    out << "Point(1) = {0, 0, 0, 0.1};\n";
    out << "Point(2) = {1, 0, 0, 0.1};\n";
    out << "Point(3) = {0, 1, 0, 0.1};\n";
    out << "Line(1) = {1, 2};\n";
    out << "Line(2) = {2, 3};\n";
    out << "Line(3) = {3, 1};\n";
    out << "Line Loop(4) = {1, 2, 3};\n";
    out << "Plane Surface(1) = {4};\n";
    out.close();
  }

  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;
}
#include <iostream>
#include <fstream>
#include <gmsh/Gmsh.h>
#include <gmsh/GModel.h>
#include <omp.h>

class Elem
{
public:
  char geoFileName[256];
  char mshFileName[256];

  void Init(int Number)
  {
    char ch[8];
    itoa(Number, ch, 10);
    strcpy(geoFileName, ch);
    strcat(geoFileName, ".geo");
    strcpy(mshFileName, ch);
    strcat(mshFileName, ".msh");
  }

  void CreateGeo()
  {
    std::ofstream out(geoFileName);
    out << "Point(1) = {0, 0, 0, 0.1};\n";
    out << "Point(2) = {1, 0, 0, 0.1};\n";
    out << "Point(3) = {0, 1, 0, 0.1};\n";
    out << "Line(1) = {1, 2};\n";
    out << "Line(2) = {2, 3};\n";
    out << "Line(3) = {3, 1};\n";
    out << "Line Loop(4) = {1, 2, 3};\n";
    out << "Plane Surface(1) = {4};\n";
    out.close();
  }

  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];

  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;
}
_______________________________________________
gmsh mailing list
[email protected]
http://www.geuz.org/mailman/listinfo/gmsh

Reply via email to