Skip to content
Snippets Groups Projects
Commit 7da78adb authored by Jonathan Lambrechts's avatar Jonathan Lambrechts
Browse files

script to extract all functions from gmsh.h, namespaces

parent e22bdb1e
No related branches found
No related tags found
No related merge requests found
...@@ -2095,9 +2095,9 @@ void gmsh::view::probe(const int tag, const double x, const double y, ...@@ -2095,9 +2095,9 @@ void gmsh::view::probe(const int tag, const double x, const double y,
const double z, std::vector<double> &value, const double z, std::vector<double> &value,
const int step, const int numComp, const int step, const int numComp,
const bool gradient, const double tolerance, const bool gradient, const double tolerance,
const std::vector<double> xElemCoord, const std::vector<double> &xElemCoord,
const std::vector<double> yElemCoord, const std::vector<double> &yElemCoord,
const std::vector<double> zElemCoord) const std::vector<double> &zElemCoord)
{ {
if(!_isInitialized()){ throw -1; } if(!_isInitialized()){ throw -1; }
#if defined(HAVE_POST) #if defined(HAVE_POST)
......
...@@ -692,7 +692,7 @@ namespace gmsh { // Top-level functions ...@@ -692,7 +692,7 @@ namespace gmsh { // Top-level functions
const double z, const double dx, const double z, const double dx,
const double dy, const double dz, const double dy, const double dz,
const double r, const int tag = -1, const double r, const int tag = -1,
double angle = 2*M_PI); const double angle = 2*M_PI);
GMSH_API int addCone(const double x, const double y, const double z, GMSH_API int addCone(const double x, const double y, const double z,
const double dx, const double dy, const double dz, const double dx, const double dy, const double dz,
const double r1, const double r2, const double r1, const double r2,
...@@ -849,11 +849,11 @@ namespace gmsh { // Top-level functions ...@@ -849,11 +849,11 @@ namespace gmsh { // Top-level functions
const int step = -1, const int numComp = -1, const int step = -1, const int numComp = -1,
const bool gradient = false, const bool gradient = false,
const double tolerance = 0., const double tolerance = 0.,
const std::vector<double> xElemCoord const std::vector<double> &xElemCoord
= std::vector<double>(), = std::vector<double>(),
const std::vector<double> yElemCoord const std::vector<double> &yElemCoord
= std::vector<double>(), = std::vector<double>(),
const std::vector<double> zElemCoord const std::vector<double> &zElemCoord
= std::vector<double>()); = std::vector<double>());
// Writes the view to a file. The export format is determined by the file // Writes the view to a file. The export format is determined by the file
......
import os
class arg: class arg:
def __init__(self,name,value,type_cpp,type_c,out): def __init__(self,name,value,type_cpp,type_c,out):
...@@ -9,7 +10,6 @@ class arg: ...@@ -9,7 +10,6 @@ class arg:
self.cpp_value = (" = "+value) if value else "" self.cpp_value = (" = "+value) if value else ""
n = self.type_cpp n = self.type_cpp
if n[-1] != "&":
n += " " n += " "
n += self.name n += self.name
n += self.cpp_value n += self.cpp_value
...@@ -37,7 +37,7 @@ class oint(arg): ...@@ -37,7 +37,7 @@ class oint(arg):
self.c_arg = "*"+name self.c_arg = "*"+name
def istring(name,value=None): def istring(name,value=None):
value = "\""+value+"\"" if value is not None else None #value = "\""+value+"\"" if value is not None else None
a = arg(name,value,"const std::string&","const char*",False) a = arg(name,value,"const std::string&","const char*",False)
a.python_arg = "c_char_p("+name+".encode())" a.python_arg = "c_char_p("+name+".encode())"
return a return a
...@@ -46,7 +46,7 @@ def ostring(name,value=None): ...@@ -46,7 +46,7 @@ def ostring(name,value=None):
a = arg(name,value,"std::string &","char**",True) a = arg(name,value,"std::string &","char**",True)
a.c_arg = "api_"+name+"_" a.c_arg = "api_"+name+"_"
a.c_pre = " std::string "+a.c_arg +";\n" a.c_pre = " std::string "+a.c_arg +";\n"
a.c_post = " *"+name+" = strdup("+a.c_arg+".c_str());\n" a.c_post = " *"+name+" = _strdup("+a.c_arg+".c_str());\n"
name_ = "api_"+name+"_" name_ = "api_"+name+"_"
a.python_pre = name_+" = c_char_p()" a.python_pre = name_+" = c_char_p()"
a.python_arg = "byref("+name_+")" a.python_arg = "byref("+name_+")"
...@@ -68,20 +68,33 @@ def odouble(name,value=None): ...@@ -68,20 +68,33 @@ def odouble(name,value=None):
return a return a
def ovectorpair(name,value=None): def ovectorpair(name,value=None):
a = arg(name,value,"vector_pair &","int**",True) a = arg(name,value,"gmsh::vector_pair &","int**",True)
name_ = "api_"+name+"_" name_ = "api_"+name+"_"
name_n = name_ + "n_" name_n = name_ + "n_"
a.c_arg = name_ a.c_arg = name_
a.c_pre = " vector_pair "+name_ +";\n" a.c_pre = " gmsh::vector_pair "+name_ +";\n"
a.c_post = " pairvector2intptr("+name_+"," + name + ","+name_n+");\n" a.c_post = " pairvector2intptr("+name_+"," + name + ","+name+"_n);\n"
a.c = "int** "+name+",size_t* "+name_n a.c = "int** "+name+",size_t* "+name+"_n"
a.python_pre = name_+", "+name_n+" = POINTER(c_int)(), c_size_t()" a.python_pre = name_+", "+name_n+" = POINTER(c_int)(), c_size_t()"
a.python_arg = "byref("+name_+"),byref("+name_n+")" a.python_arg = "byref("+name_+"),byref("+name_n+")"
a.python_return = "_ovectorpair("+name_+","+name_n+".value)" a.python_return = "_ovectorpair("+name_+","+name_n+".value)"
return a return a
def ovectorstring(name,value=None):
a = arg(name,value,"std::vector<std::string> &","char**",True)
name_ = "api_"+name+"_"
name_n = name_ + "n_"
a.c_arg = name_
a.c_pre = " std::vector<std::string> "+name_ +";\n"
a.c_post = " stringvector2charpp("+name_+"," + name + ","+name+"_n);\n"
a.c = "char*** "+name+",size_t* "+name+"_n"
a.python_pre = name_+", "+name_n+" = POINTER(c_char_p)(), c_size_t()"
a.python_arg = "byref("+name_+"),byref("+name_n+")"
a.python_return = "_ovectorstring("+name_+","+name_n+".value)"
return a
def ivectorpair(name,value=None): def ivectorpair(name,value=None):
a = arg(name,value,"const vector_pair &","const int*",False) a = arg(name,value,"const gmsh::vector_pair &","const int*",False)
a.c_arg = "intptr2pairvector("+name+","+name+"_n)" a.c_arg = "intptr2pairvector("+name+","+name+"_n)"
a.c = "int* "+name+",size_t "+name+"_n" a.c = "int* "+name+",size_t "+name+"_n"
a.python_pre = "api_"+name+"_, api"+name+"_n_ = _ivectorpair("+name+")" a.python_pre = "api_"+name+"_, api"+name+"_n_ = _ivectorpair("+name+")"
...@@ -114,8 +127,8 @@ def ovectorint(name,value=None): ...@@ -114,8 +127,8 @@ def ovectorint(name,value=None):
name_n = name_ + "n_" name_n = name_ + "n_"
a.c_arg = name_ a.c_arg = name_
a.c_pre = " std::vector<int> "+name_ +";\n" a.c_pre = " std::vector<int> "+name_ +";\n"
a.c_post = " vector2ptr("+name_+"," + name + ","+name_n+");\n" a.c_post = " vector2ptr("+name_+"," + name + ","+name+"_n);\n"
a.c = "int** "+name+",size_t* "+name_n a.c = "int** "+name+",size_t* "+name+"_n"
a.python_pre = name_+", "+name_n+" = POINTER(c_int)(), c_size_t()" a.python_pre = name_+", "+name_n+" = POINTER(c_int)(), c_size_t()"
a.python_arg = "byref("+name_+"),byref("+name_n+")" a.python_arg = "byref("+name_+"),byref("+name_n+")"
a.python_return = "_ovectorint("+name_+","+name_n+".value)" a.python_return = "_ovectorint("+name_+","+name_n+".value)"
...@@ -135,7 +148,7 @@ def ovectordouble(name,value=None): ...@@ -135,7 +148,7 @@ def ovectordouble(name,value=None):
name_n = name_ + "n_" name_n = name_ + "n_"
a.c_arg = name_ a.c_arg = name_
a.c_pre = " std::vector<double> "+a.c_arg +";\n" a.c_pre = " std::vector<double> "+a.c_arg +";\n"
a.c_post = " vector2ptr("+name_+"," + name + ","+name_n+";\n" a.c_post = " vector2ptr("+name_+"," + name + ","+name+"_n);\n"
a.c = "double** "+name+",size_t* "+name+"_n" a.c = "double** "+name+",size_t* "+name+"_n"
a.python_pre = name_+", "+name_n+" = POINTER(c_double)(), c_size_t()" a.python_pre = name_+", "+name_n+" = POINTER(c_double)(), c_size_t()"
a.python_arg = "byref("+name_+"),byref("+name_n+")" a.python_arg = "byref("+name_+"),byref("+name_n+")"
...@@ -149,22 +162,22 @@ def ovectorvectorint(name,value=None): ...@@ -149,22 +162,22 @@ def ovectorvectorint(name,value=None):
name_nn = name_ + "nn_" name_nn = name_ + "nn_"
a.c_arg = name_ a.c_arg = name_
a.c_pre = " std::vector<std::vector<int> > "+a.c_arg +";\n" a.c_pre = " std::vector<std::vector<int> > "+a.c_arg +";\n"
a.c_post = " vectorvector2ptrptr("+name_+"," + name + ","+name_n+","+name_nn+");\n" a.c_post = " vectorvector2ptrptr("+name_+"," + name + ","+name+"_n,"+name+"_nn);\n"
a.c = "int*** "+name+",size_t** "+name_n+",size_t *"+name_nn a.c = "int*** "+name+",size_t** "+name+"_n,size_t *"+name+"_nn"
a.python_pre = name_+", "+name_n+", "+name_nn +" = POINTER(POINTER(c_int))(), POINTER(c_size_t)(), c_size_t()" a.python_pre = name_+", "+name_n+", "+name_nn +" = POINTER(POINTER(c_int))(), POINTER(c_size_t)(), c_size_t()"
a.python_arg = "byref("+name_+"),byref("+name_n+"),byref("+name_nn+")" a.python_arg = "byref("+name_+"),byref("+name_n+"),byref("+name_nn+")"
a.python_return = "_ovectorvectorint("+name_+","+name_n+","+name_nn+")" a.python_return = "_ovectorvectorint("+name_+","+name_n+","+name_nn+")"
return a return a
def ovectorvectorpair(name,value=None): def ovectorvectorpair(name,value=None):
a = arg(name,value,"std::vector<vector_pair> &","int**",True) a = arg(name,value,"std::vector<gmsh::vector_pair> &","int**",True)
name_ = "api_"+name+"_" name_ = "api_"+name+"_"
name_n = name_ + "n_" name_n = name_ + "n_"
name_nn = name_ + "nn_" name_nn = name_ + "nn_"
a.c_arg = name_ a.c_arg = name_
a.c_pre = " std::vector<vector_pair >"+name_ +";\n" a.c_pre = " std::vector<gmsh::vector_pair >"+name_ +";\n"
a.c_post = " pairvectorvector2intptrptr("+name_+"," + name + ","+name_n+","+name_nn+";\n" a.c_post = " pairvectorvector2intptrptr("+name_+"," + name + ","+name+"_n,"+name+"_nn);\n"
a.c = "int*** "+name+",size_t** "+name_n+",size_t *"+name_nn a.c = "int*** "+name+",size_t** "+name+"_n,size_t *"+name+"_nn"
a.python_pre = name_+", "+name_n+", "+name_nn +" = POINTER(POINTER(c_int))(), POINTER(c_size_t)(), c_size_t()" a.python_pre = name_+", "+name_n+", "+name_nn +" = POINTER(POINTER(c_int))(), POINTER(c_size_t)(), c_size_t()"
a.python_arg = "byref("+name_+"),byref("+name_n+"),byref("+name_nn+")" a.python_arg = "byref("+name_+"),byref("+name_n+"),byref("+name_nn+")"
a.python_return = "_ovectorvectorpair("+name_+","+name_n+","+name_nn+")" a.python_return = "_ovectorvectorpair("+name_+","+name_n+","+name_nn+")"
...@@ -175,10 +188,17 @@ class Module: ...@@ -175,10 +188,17 @@ class Module:
def __init__(self,name): def __init__(self,name):
self.name = name self.name = name
self.fs = [] self.fs = []
self.submodules = []
def add(self,rtype,name,*args): def add(self,rtype,name,*args):
self.fs.append((rtype,name,args)) self.fs.append((rtype,name,args))
def add_module(self, name) :
module = Module(name)
self.submodules.append(module)
return module
cpp_header="""// Gmsh - Copyright (C) 1997-2017 C. Geuzaine,J.-F. Remacle cpp_header="""// Gmsh - Copyright (C) 1997-2017 C. Geuzaine,J.-F. Remacle
// //
// See the LICENSE.txt file for license information. Please report all // See the LICENSE.txt file for license information. Please report all
...@@ -212,9 +232,11 @@ cpp_header="""// Gmsh - Copyright (C) 1997-2017 C. Geuzaine,J.-F. Remacle ...@@ -212,9 +232,11 @@ cpp_header="""// Gmsh - Copyright (C) 1997-2017 C. Geuzaine,J.-F. Remacle
#define GMSH_API #define GMSH_API
#endif #endif
typedef std::vector<std::pair<int,int> > vector_pair;
GMSH_API void gmshInitialize(int argc,char **argv); namespace gmsh {
GMSH_API void initialize(int argc,char **argv);
typedef std::vector<std::pair<int,int> > vector_pair;
}
""" """
cpp_footer=""" cpp_footer="""
...@@ -254,8 +276,8 @@ c_header="""// Gmsh - Copyright (C) 1997-2017 C. Geuzaine,J.-F. Remacle ...@@ -254,8 +276,8 @@ c_header="""// Gmsh - Copyright (C) 1997-2017 C. Geuzaine,J.-F. Remacle
#include <stdlib.h> #include <stdlib.h>
GMSH_API void gmshcInitialize(char argc,char **argv); GMSH_API void gmshInitialize(char argc,char **argv);
GMSH_API void gmshcFree_(void *p); GMSH_API void gmshFree_(void *p);
""" """
c_footer=""" c_footer="""
...@@ -264,7 +286,7 @@ c_footer=""" ...@@ -264,7 +286,7 @@ c_footer="""
#endif #endif
""" """
c_header="""// Gmsh - Copyright (C) 1997-2017 C. Geuzaine,J.-F. Remacle c_cpp_header="""// Gmsh - Copyright (C) 1997-2017 C. Geuzaine,J.-F. Remacle
// //
// See the LICENSE.txt file for license information. Please report all // See the LICENSE.txt file for license information. Please report all
// bugs and problems to the public mailing list <gmsh@onelab.info>. // bugs and problems to the public mailing list <gmsh@onelab.info>.
...@@ -275,6 +297,13 @@ extern \"C\" { ...@@ -275,6 +297,13 @@ extern \"C\" {
#include "gmsh.h" #include "gmsh.h"
#include <cstring> #include <cstring>
char * _strdup(const char *i) {
size_t len = strlen(i);
char *o = (char*)malloc(sizeof(char)*(len+1));
memcpy(o,i,len+1);
return o;
}
template<typename t> template<typename t>
std::vector<t> ptr2vector(const t *p,size_t size) { std::vector<t> ptr2vector(const t *p,size_t size) {
return std::vector<t>(p,p+size); return std::vector<t>(p,p+size);
...@@ -289,7 +318,7 @@ void vector2ptr(const std::vector<t>&v,t **p,size_t *size) { ...@@ -289,7 +318,7 @@ void vector2ptr(const std::vector<t>&v,t **p,size_t *size) {
*size = v.size(); *size = v.size();
} }
void pairvector2intptr(const vector_pair &v,int **p,size_t *size) { void pairvector2intptr(const gmsh::vector_pair &v,int **p,size_t *size) {
*p = (int*)malloc(sizeof(int)*(v.size()*2)); *p = (int*)malloc(sizeof(int)*(v.size()*2));
for (size_t i = 0; i < v.size(); ++i){ for (size_t i = 0; i < v.size(); ++i){
(*p)[i*2+0] = v[i].first; (*p)[i*2+0] = v[i].first;
...@@ -298,8 +327,16 @@ void pairvector2intptr(const vector_pair &v,int **p,size_t *size) { ...@@ -298,8 +327,16 @@ void pairvector2intptr(const vector_pair &v,int **p,size_t *size) {
*size = v.size()*2; *size = v.size()*2;
} }
vector_pair intptr2pairvector(const int *p,size_t size){ void stringvector2charpp(const std::vector<std::string> &v,char ***p,size_t *size) {
vector_pair v(size/2); *p = (char**)malloc(sizeof(char*)*(v.size()*2));
for (size_t i = 0; i < v.size(); ++i){
(*p)[i] = _strdup(v[i].c_str());
}
*size = v.size();
}
gmsh::vector_pair intptr2pairvector(const int *p,size_t size){
gmsh::vector_pair v(size/2);
for (size_t i = 0; i < size/2; ++i) { for (size_t i = 0; i < size/2; ++i) {
v[i].first = p[i*2+0]; v[i].first = p[i*2+0];
v[i].second = p[i*2+1]; v[i].second = p[i*2+1];
...@@ -316,18 +353,18 @@ void vectorvector2ptrptr(const std::vector<std::vector<t> > &v,t ***p,size_t **s ...@@ -316,18 +353,18 @@ void vectorvector2ptrptr(const std::vector<std::vector<t> > &v,t ***p,size_t **s
*sizeSize = v.size(); *sizeSize = v.size();
} }
int** pairvectorvector2intptrptr(const std::vector<vector_pair > &v,int ***p,size_t **size,size_t *sizeSize) { int** pairvectorvector2intptrptr(const std::vector<gmsh::vector_pair > &v,int ***p,size_t **size,size_t *sizeSize) {
*p = (int**)malloc(sizeof(int*)*v.size()); *p = (int**)malloc(sizeof(int*)*v.size());
for (size_t i = 0; i < v.size(); ++i) for (size_t i = 0; i < v.size(); ++i)
pairvector2intptr(v[i],p[i],size[i]); pairvector2intptr(v[i],p[i],size[i]);
*sizeSize = v.size(); *sizeSize = v.size();
} }
void gmshcInitialize(char argc,char **argv){ void gmshInitialize(char argc,char **argv){
gmshInitialize(argc,argv); gmsh::initialize(argc,argv);
} }
void gmshcFree_(void *p) { void gmshFree_(void *p) {
if(p) free(p); if(p) free(p);
} }
""" """
...@@ -344,58 +381,58 @@ try : ...@@ -344,58 +381,58 @@ try :
import numpy import numpy
use_numpy = True use_numpy = True
try : try :
from weakref import finalize import weakref
except : except :
from backports.weakref import finalize from backports import weakref
except : except :
pass pass
def Initialize() : def initialize() :
lib.gmshcInitialize(c_int(0),c_voidp(None)) lib.gmshInitialize(c_int(0),c_voidp(None))
def _ostring(s) : def _ostring(s) :
sp = s.value.decode("utf-8") sp = s.value.decode("utf-8")
lib.gmshcFree_(s) lib.gmshFree_(s)
return sp return sp
def _ovectorpair(ptr,size): def _ovectorpair(ptr,size):
if use_numpy : if use_numpy :
v = numpy.ctypeslib.as_array(ptr, (size//2,2)) v = numpy.ctypeslib.as_array(ptr, (size//2,2))
finalize(v, lib.gmshcFree_, ptr) weakref.finalize(v, lib.gmshFree_, ptr)
else : else :
v = list((ptr[i*2],ptr[i*2+1]) for i in range(size//2)) v = list((ptr[i*2],ptr[i*2+1]) for i in range(size//2))
lib.gmshcFree_(ptr) lib.gmshFree_(ptr)
return v return v
def _ovectorint(ptr,size): def _ovectorint(ptr,size):
if use_numpy : if use_numpy :
v = numpy.ctypeslib.as_array(ptr, (size,)) v = numpy.ctypeslib.as_array(ptr, (size,))
finalize(v, lib.gmshcFree_, ptr) weakref.finalize(v, lib.gmshFree_, ptr)
else : else :
v = list(ptr[i] for i in range(size)) v = list(ptr[i] for i in range(size))
lib.gmshcFree_(ptr) lib.gmshFree_(ptr)
return v return v
def _ovectordouble(ptr,size): def _ovectordouble(ptr,size):
if use_numpy : if use_numpy :
v = numpy.ctypeslib.as_array(ptr, (size,)) v = numpy.ctypeslib.as_array(ptr, (size,))
finalize(v, lib.gmshcFree_, ptr) weakref.finalize(v, lib.gmshFree_, ptr)
else : else :
v = list(ptr[i] for i in range(size)) v = list(ptr[i] for i in range(size))
lib.gmshcFree_(ptr) lib.gmshFree_(ptr)
return v return v
def _ovectorvectorint(ptr,size,n): def _ovectorvectorint(ptr,size,n):
v = [_ovectorint(pointer(ptr[i].contents),size[i]) for i in range(n.value)] v = [_ovectorint(pointer(ptr[i].contents),size[i]) for i in range(n.value)]
lib.gmshcFree_(size) lib.gmshFree_(size)
lib.gmshcFree_(ptr) lib.gmshFree_(ptr)
return v return v
def _ovectorvectorpair(ptr,size,n): def _ovectorvectorpair(ptr,size,n):
v = [_ovectorpair(pointer(ptr[i].contents),size[i]) for i in range(n.value)] v = [_ovectorpair(pointer(ptr[i].contents),size[i]) for i in range(n.value)]
lib.gmshcFree_(size) lib.gmshFree_(size)
lib.gmshcFree_(ptr) lib.gmshFree_(ptr)
return v return v
def _ivectorint(o): def _ivectorint(o):
...@@ -429,81 +466,106 @@ class API: ...@@ -429,81 +466,106 @@ class API:
self.modules.append(module) self.modules.append(module)
return module return module
def write_cpp(self,filename): def write_cpp(self):
with open(filename+".h","w") as f: def write_module(module,indent) :
f.write(cpp_header) f.write(indent+"namespace "+module.name +" {\n")
for module in self.modules: indent += " "
f.write("\n")
f.write("// gmsh"+module.name+"\n")
for rtype,name,args in module.fs: for rtype,name,args in module.fs:
rt = rtype.rtype_cpp if rtype else "void" rt = rtype.rtype_cpp if rtype else "void"
f.write("GMSH_API " + rt + " gmsh" + module.name+name+"("+",".join( f.write(indent+"GMSH_API " + rt + " " + name + "("+",".join(
list((a.cpp for a in args)))+");\n") list((a.cpp for a in args)))+");\n")
for m in module.submodules :
write_module(m, indent)
f.write(indent[:-2]+"} // namespace "+ module.name+"\n")
with open("gmsh.h","w") as f:
f.write(cpp_header)
for m in self.modules:
write_module(m,"")
f.write(cpp_footer) f.write(cpp_footer)
def write_c(self,filename): def write_c(self):
with open(filename+".h","w") as f: def c_fun_name(module, f) :
f.write(c_header) n = "gmshc"
for module in self.modules: if module :
f.write("\n") n += module[0].upper() + module[1:]
f.write("/* gmsh"+module.name+" */\n") return n + f[0].upper() + f[1:]
def write_module(module,c_namespace,cpp_namespace) :
cpp_namespace += module.name+"::"
if c_namespace :
c_namespace += module.name[0].upper()+module.name[1:]
else :
c_namespace = module.name
for rtype,name,args in module.fs: for rtype,name,args in module.fs:
fname = c_namespace + name[0].upper()+name[1:]
f.write("GMSH_API "+(rtype.rtype_c if rtype else "void")) f.write("GMSH_API "+(rtype.rtype_c if rtype else "void"))
f.write(" gmshc" + module.name+name+"(" f.write(" "+fname+"("
+",".join(list((a.c for a in args+(oint("ierr"),)))) +",".join(list((a.c for a in args+(oint("ierr"),))))
+ ");\n") + ");\n")
f.write(c_footer) fc.write(rtype.rtype_c if rtype else "void")
fc.write(" "+fname+"("
with open(filename+".cc","w") as f:
f.write(c_header)
for module in self.modules:
f.write("\n")
f.write("/* gmsh"+module.name+" */\n")
for rtype,name,args in module.fs:
f.write(rtype.rtype_c if rtype else "void")
f.write(" gmshc" + module.name+name+"("
+",".join(list((a.c for a in args+(oint("ierr"),))))+"){\n") +",".join(list((a.c for a in args+(oint("ierr"),))))+"){\n")
if rtype: if rtype:
f.write(" "+ rtype.rtype_c + " result_api_;\n") fc.write(" "+ rtype.rtype_c + " result_api_;\n")
f.write(" if(ierr) *ierr = 0;\n"); fc.write(" if(ierr) *ierr = 0;\n");
f.write(" try {\n"); fc.write(" try {\n");
f.write("".join((a.c_pre for a in args))) fc.write("".join((a.c_pre for a in args)))
f.write(" ") fc.write(" ")
if rtype: if rtype:
f.write("result_api_ = ") fc.write("result_api_ = ")
f.write("gmsh" + module.name+name+"("+",".join( fc.write(cpp_namespace+name+"("+",".join(
list((a.c_arg for a in args)))+ list((a.c_arg for a in args)))+
");\n") ");\n")
f.write("".join((a.c_post for a in args))) fc.write("".join((a.c_post for a in args)))
f.write(" } catch(int api_ierr_) {if (ierr) *ierr = api_ierr_;}\n"); fc.write(" } catch(int api_ierr_) {if (ierr) *ierr = api_ierr_;}\n");
if rtype : if rtype :
f.write(" return result_api_;\n"); fc.write(" return result_api_;\n");
f.write("}\n\n") fc.write("}\n\n")
for m in module.submodules :
write_module(m, c_namespace,cpp_namespace)
def write_python(self) : with open("gmshc.h","w") as f :
with open("gmsh.py","w") as f : with open("gmshc.cc","w") as fc :
f.write(python_header) f.write(c_header)
fc.write(c_cpp_header)
for module in self.modules: for module in self.modules:
f.write("\n## gmsh"+module.name + " ##\n") write_module(module,"","")
for (rtype,name,args) in module.fs: f.write(c_footer)
def write_python(self) :
def write_function(f,rtype, name, args, modulepath,indent):
iargs = list(a for a in args if not a.out) iargs = list(a for a in args if not a.out)
oargs = list(a for a in args if a.out) oargs = list(a for a in args if a.out)
f.write("\ndef "+module.name+name+"(" f.write("\n"+indent+"def "+name+"("
+",".join((a.name for a in iargs)) +",".join((a.name for a in iargs))
+"):\n") +"):\n")
for a in args : for a in args :
if a.python_pre : f.write(" "+a.python_pre+"\n") if a.python_pre : f.write(indent+" "+a.python_pre+"\n")
f.write(" ierr = c_int()\n") f.write(indent+" ierr = c_int()\n")
f.write(" api__result__ = " if rtype is oint else " ") f.write(indent+" api__result__ = " if rtype is oint else (" "+indent))
f.write("lib.gmshc"+module.name+name+"(\n " c_name = modulepath + name[0].upper()+name[1:]
+",\n ".join(tuple((a.python_arg for a in args))+("byref(ierr)",)) f.write("lib."+c_name+"(\n "+indent
+(",\n"+indent+" ").join(tuple((a.python_arg for a in args))+("byref(ierr)",))
+")\n") +")\n")
f.write(" if ierr.value != 0 :\n") f.write(indent+" if ierr.value != 0 :\n")
f.write(" raise ValueError(\"gmsh."+module.name+name+" returned non-zero error code : \"+ str(ierr.value))\n") f.write(indent+" raise ValueError(\""+c_name+" returned non-zero error code : \"+ str(ierr.value))\n")
r = (["api__result__"]) if rtype else [] r = (["api__result__"]) if rtype else []
r += list((o.python_return for o in oargs)) r += list((o.python_return for o in oargs))
if len(r) != 0 : if len(r) != 0 :
if len(r) == 1 : if len(r) == 1 :
f.write(" return "+r[0]+"\n") f.write(indent+" return "+r[0]+"\n")
else :
f.write(indent+" return (\n "+",\n ".join(r)+")\n")
def write_module(f,m,modulepath,indent) :
if modulepath :
modulepath += m.name[0].upper()+m.name[1:]
else : else :
f.write(" return (\n "+",\n ".join(r)+")\n") modulepath = m.name
for fun in m.fs :
write_function(f,*fun,modulepath,indent)
for module in m.submodules :
f.write("\n\n"+indent + "class " + module.name + ":\n")
write_module(f,module,modulepath,indent+" ")
with open("gmsh.py","w") as f :
f.write(python_header)
for module in self.modules:
write_module(f,module,"","")
all : all :
python extract.py
python gen.py python gen.py
g++ gmshc.cc -shared -fPIC -L. -L../build -Wl,-rpath=../build -lstdc++ -lgmsh -o libgmshc.so g++ gmshc.cc -shared -fPIC -L. -L../build -Wl,-rpath=../build -lstdc++ -lgmsh -o libgmshc.so
gcc main.c -L. -Wl,-rpath=. -L../build -Wl,-rpath=../build -lgmshc -lgmsh -lstdc++ -o main gcc main.c -L. -Wl,-rpath=. -L../build -Wl,-rpath=../build -lgmshc -lgmsh -lstdc++ -o main
tmap = {
'int':'iint',
'char**':'icharpp',
'constbool':'ibool',
'constdouble':'idouble',
'constint':'iint',
'conststd::string&':'istring',
'conststd::vector<double>&':'ivectordouble',
'conststd::vector<int>&':'ivectorint',
'conststd::vector<std::vector<double>>&':'ivectorvectordouble',
'conststd::vector<std::vector<int>>&':'ivectorvectorint',
'constvector_pair&':'ivectorpair',
'double&': 'odouble',
'int&':'oint',
'std::string&':'ostring',
'std::vector<double>&':'ovectordouble',
'std::vector<int>&':'ovectorint',
'std::vector<std::string>&':'ovectorstring',
'std::vector<std::vector<int>>&':'ovectorvectorint',
'std::vector<vector_pair>&':'ovectorvectorpair',
'vector_pair&':'ovectorpair'
}
def parse_arg(a) :
value = None
eq = a.find("=")
if eq != -1 :
value = a[eq+1:].strip()
a = a[:eq].strip()
p = max(a.rfind(" "), a.rfind("*"), a.rfind("&"), a.rfind(">"))
name = a[p+1:]
typ = a[:p+1].replace(" ","")
return tmap[typ]+"('"+name+"'"+((",'"+value+"'") if value else "")+")"
def parse_fun(f,fun,namespace) :
iarg =fun.find("(")
earg =fun.rfind(")")
pre = fun[:iarg]
args = fun[iarg+1:earg].split(",")
w = pre.split()
t = w[1]
name = w[2]
ret = "None"
if t == "int" :
ret = "oint"
elif t != "void" :
raise NameError("Unknown return type : ", t)
if (args == ['']) :
args = []
if (name in ["initialize","setElements","addModelData"]) :
f.write("#")
f.write(namespace+".add("+",".join([ret,"'"+name+"'"]+[parse_arg(a) for a in args])+")\n")
namespace = ["api"]
with open("../Common/gmsh.h") as f :
with open("gen.py","w") as fo :
fo.write("from GenApi import *\n\napi = API()\n")
ls = f.readlines()
i = 0
while i < len(ls) :
l = ls[i].strip()
w = l.split()
i += 1
if not w :
continue
if w[0] == "GMSH_API" :
f = l
while f[-1] != ";" :
l = ls[i].strip()
i += 1
f += " " + l
try :
parse_fun(fo,f,namespace[-1])
except :
raise NameError(f)
elif w[0] == "namespace" :
fo.write("\n"+w[1]+" = "+namespace[-1]+".add_module('"+w[1]+"')\n")
namespace += [w[1]]
elif w[0] == "}" :
namespace.pop()
fo.write("api.write_cpp()\n")
fo.write("api.write_c()\n")
fo.write("api.write_python()\n")
This diff is collapsed.
#include <stdio.h> #include <stdio.h>
#include "gmshc.h" #include "gmshc.h"
#define chk(ierr) if (ierr != 0) {fprintf(stderr, "ERROR on line %i in function '%s': gmsh function return non-zero error code: %i\n",__LINE__, __FUNCTION__,ierr); gmshcFinalize(NULL); exit(ierr);} #define chk(ierr) if (ierr != 0) {fprintf(stderr, "ERROR on line %i in function '%s': gmsh function return non-zero error code: %i\n",__LINE__, __FUNCTION__,ierr); gmshFinalize(NULL); exit(ierr);}
void genGeometry() { void genGeometry() {
int ierr; int ierr;
gmshcModelCreate("square",&ierr);chk(ierr); gmshModelAdd("square",&ierr);chk(ierr);
gmshcModelGeoAddPoint(0,0,0,0.1,1,&ierr);chk(ierr); gmshModelGeoAddPoint(0,0,0,0.1,1,&ierr);chk(ierr);
gmshcModelGeoAddPoint(1,0,0,0.1,2,&ierr);chk(ierr); gmshModelGeoAddPoint(1,0,0,0.1,2,&ierr);chk(ierr);
gmshcModelGeoAddPoint(1,1,0,0.1,3,&ierr);chk(ierr); gmshModelGeoAddPoint(1,1,0,0.1,3,&ierr);chk(ierr);
gmshcModelGeoAddPoint(0,1,0,0.1,4,&ierr);chk(ierr); gmshModelGeoAddPoint(0,1,0,0.1,4,&ierr);chk(ierr);
gmshcModelGeoAddLine(1,2,1,&ierr); chk(ierr); gmshModelGeoAddLine(1,2,1,&ierr); chk(ierr);
gmshcModelGeoAddLine(2,3,2,&ierr); chk(ierr); gmshModelGeoAddLine(2,3,2,&ierr); chk(ierr);
gmshcModelGeoAddLine(3,4,3,&ierr); chk(ierr); gmshModelGeoAddLine(3,4,3,&ierr); chk(ierr);
// try automatic assignement of tag // try automatic assignement of tag
int line4 = gmshcModelGeoAddLine(4,1,-1,&ierr); chk(ierr); int line4 = gmshModelGeoAddLine(4,1,-1,&ierr); chk(ierr);
printf("line4 received tag %i\n\n", line4); printf("line4 received tag %i\n\n", line4);
int ll[] = {1,2,3,line4}; int ll[] = {1,2,3,line4};
gmshcModelGeoAddLineLoop(ll,4,1,&ierr); chk(ierr); gmshModelGeoAddLineLoop(ll,4,1,&ierr); chk(ierr);
int s[] = {1}; int s[] = {1};
gmshcModelGeoAddPlaneSurface(ll,1,6,&ierr); chk(ierr); gmshModelGeoAddPlaneSurface(ll,1,6,&ierr); chk(ierr);
gmshcModelGeoSynchronize(&ierr); chk(ierr); gmshModelGeoSynchronize(&ierr); chk(ierr);
} }
void printMesh() { void printMesh() {
...@@ -28,13 +28,13 @@ void printMesh() { ...@@ -28,13 +28,13 @@ void printMesh() {
int *dimTags; int *dimTags;
size_t ndimTags; size_t ndimTags;
gmshcModelGetEntities(&dimTags, &ndimTags, -1,&ierr); chk(ierr); gmshModelGetEntities(&dimTags, &ndimTags, -1,&ierr); chk(ierr);
for (size_t ie = 0; ie < ndimTags/2; ++ie) { for (size_t ie = 0; ie < ndimTags/2; ++ie) {
int *types, **elementTags, **vertexTags; int *types, **elementTags, **vertexTags;
size_t ntypes, *nelementTags, nnelementTags, *nvertexTags, nnvertexTags; size_t ntypes, *nelementTags, nnelementTags, *nvertexTags, nnvertexTags;
gmshcModelGetMeshElements(dimTags[ie*2+0], dimTags[ie*2+1], &types, &ntypes, &elementTags, &nelementTags, &nnelementTags, &vertexTags, &nvertexTags, &nnvertexTags,&ierr); chk(ierr); gmshModelMeshGetElements(dimTags[ie*2+0], dimTags[ie*2+1], &types, &ntypes, &elementTags, &nelementTags, &nnelementTags, &vertexTags, &nvertexTags, &nnvertexTags,&ierr); chk(ierr);
printf("entity %i of dim %i\n", dimTags[ie*2+1], dimTags[ie*2+0]); printf("entity %i of dim %i\n", dimTags[ie*2+1], dimTags[ie*2+0]);
for (size_t i = 0; i < nnelementTags; ++i) { for (size_t i = 0; i < nnelementTags; ++i) {
...@@ -65,16 +65,16 @@ void printMesh() { ...@@ -65,16 +65,16 @@ void printMesh() {
void genError() { void genError() {
int ierr; int ierr;
printf("\n** generate an error **\n"); printf("\n** generate an error **\n");
gmshcModelGetMeshElements(999, 999, NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,&ierr); chk(ierr); gmshModelMeshGetElements(999, 999, NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,&ierr); chk(ierr);
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
int ierr; int ierr;
gmshcInitialize(argc, argv); gmshInitialize(argc, argv);
genGeometry(); genGeometry();
gmshcModelMesh(2,&ierr); chk(ierr); gmshModelMeshGenerate(2,&ierr); chk(ierr);
gmshcExport("square.msh",&ierr); chk(ierr); gmshWrite("square.msh",&ierr); chk(ierr);
printMesh(); printMesh();
genError(); genError();
gmshcFinalize(&ierr); chk(ierr); gmshFinalize(&ierr); chk(ierr);
} }
import gmsh import gmsh
gmsh.Initialize() gmsh.initialize()
gmsh.Open("square.msh") gmsh.open("square.msh")
gmsh.ModelCreate("square") model = gmsh.model
gmsh.ModelGeoAddPoint(0,0,0,0.1,1)
gmsh.ModelGeoAddPoint(1,0,0,0.1,2) model.add("square")
gmsh.ModelGeoAddPoint(1,1,0,0.1,3) factory = model.geo
gmsh.ModelGeoAddPoint(0,1,0,0.1,4) factory.addPoint(0,0,0,0.1,1)
gmsh.ModelGeoAddLine(1,2,1) factory.addPoint(1,0,0,0.1,2)
gmsh.ModelGeoAddLine(2,3,2) factory.addPoint(1,1,0,0.1,3)
gmsh.ModelGeoAddLine(3,4,3) factory.addPoint(0,1,0,0.1,4)
line4 = gmsh.ModelGeoAddLine(4,1,-1) factory.addLine(1,2,1)
factory.addLine(2,3,2)
factory.addLine(3,4,3)
line4 = factory.addLine(4,1,-1)
print("line4 received tag ", line4) print("line4 received tag ", line4)
gmsh.ModelGeoAddLineLoop([1,2,3,line4],1) factory.addLineLoop([1,2,3,line4],1)
gmsh.ModelGeoAddPlaneSurface([1],6) factory.addPlaneSurface([1],6)
gmsh.ModelGeoSynchronize() factory.synchronize()
ptag = gmsh.ModelAddPhysicalGroup(1,[1,2,3,4],-1) ptag = model.addPhysicalGroup(1,[1,2,3,4],-1)
ent = gmsh.ModelGetEntitiesForPhysicalGroup(1,ptag) ent = model.getEntitiesForPhysicalGroup(1,ptag)
print("new physical group ",ptag,":",ent, type(ent)) print("new physical group ",ptag,":",ent, type(ent))
gmsh.ModelAddPhysicalGroup(2,[6],-1) model.addPhysicalGroup(2,[6],-1)
print(gmsh.OptionGetString("General.BuildOptions")) print(gmsh.option.getString("General.BuildOptions"))
print(gmsh.OptionGetNumber("Mesh.Algorithm")) print(gmsh.option.getNumber("Mesh.Algorithm"))
gmsh.OptionSetNumber("Mesh.Algorithm", 3.0) gmsh.option.setNumber("Mesh.Algorithm", 3.0)
print(gmsh.OptionGetNumber("Mesh.Algorithm")) print(gmsh.option.getNumber("Mesh.Algorithm"))
gmsh.ModelMesh(2) model.mesh.generate(2)
gmsh.Export("square.msh")
gmsh.write("square.msh")
print("Entities") print("Entities")
entities = gmsh.ModelGetEntities(-1) entities = model.getEntities(-1)
for e in entities : for e in entities :
print("entity ",e) print("entity ",e)
types,tags,vertices = gmsh.ModelGetMeshElements(e[0],e[1]) types,tags,vertices = model.mesh.getElements(e[0],e[1])
for i in range(len(types)): for i in range(len(types)):
print("type ", types[i]) print("type ", types[i])
print("tags : ", list(tags[i])) print("tags : ", list(tags[i]))
print("vertices : ", list(vertices[i])) print("vertices : ", list(vertices[i]))
print("Vertices") print("Vertices")
tags, coord, _ = gmsh.ModelGetMeshVertices(2,6) tags, coord, _ = model.mesh.getVertices(2,6)
print(tags) print(tags)
print(coord) print(coord)
gmsh.Finalize() gmsh.finalize()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment