diff --git a/Common/gmshpy.i b/Common/gmshpy.i index 90472b63e19b25aa4025fd7a9df4748c5a698c78..0aef8c5cfa5ada49df8f56a6d5c592aba75488c1 100644 --- a/Common/gmshpy.i +++ b/Common/gmshpy.i @@ -34,6 +34,24 @@ #include "DivideAndConquer.h" #include "Gmsh.h" #include "functionPython.h" + class errorHandler: public GmshMessage { + void operator()(std::string level, std::string message){ + //const char *color = colorDefault; + std::cout<<level<<" : "<<message<<std::endl; + if (level=="Error" || level == "Fatal") { + //color = colorRed; + //color confuses ctest/cdash + //std::cout<<color<<level<<" : "<<message<<colorDefault<<"\n"; + throw; + } + } + }; +%} + +%init %{ + errorHandler *eH = new errorHandler; + Msg::SetCallback(eH); + GmshInitialize(); %} namespace std { diff --git a/Solver/function.cpp b/Solver/function.cpp index 2517fc72a08a7a8091e189283983351f3bca6acc..07c7f050a5310675453a8e347d03ddaf304b4698 100644 --- a/Solver/function.cpp +++ b/Solver/function.cpp @@ -71,25 +71,19 @@ function *function::getSolution() // Get Solution Gradient + Additionnal class -class functionSolutionGradient : public function { - static functionSolutionGradient *_instance; - // constructor is private only 1 instance can exists, call get to - // access the instance - functionSolutionGradient():function(0){} - public: - void call(dataCacheMap *m, fullMatrix<double> &sol) - { - Msg::Error("a function requires the gradient of the solution but " - "this algorithm does not provide the gradient of the solution"); - throw; - } - static function *get() - { - if(!_instance) - _instance = new functionSolutionGradient(); - return _instance; - } -}; +functionSolutionGradient::functionSolutionGradient():function(0){} +void functionSolutionGradient::call(dataCacheMap *m, fullMatrix<double> &sol) +{ + Msg::Error("a function requires the gradient of the solution but " + "this algorithm does not provide the gradient of the solution"); + throw; +} +function *functionSolutionGradient::get() +{ + if(!_instance) + _instance = new functionSolutionGradient(); + return _instance; +} functionSolutionGradient *functionSolutionGradient::_instance = NULL; @@ -606,92 +600,87 @@ class functionLua : public function { // functionC -class functionC : public function { - std::vector<fullMatrix<double> > args; - void (*callback)(void); - public: - static void buildLibrary(std::string code, std::string filename) - { - //todo use CMAKE_CXX_COMPILER - //todo use clean temporary file names - //todo work on windows :-) - //todo if DG_BUILD_DIR is not defined, use the directory used at compilation time - FILE *tmpSrc = fopen("_tmpSrc.cpp","w"); - fprintf(tmpSrc, "%s\n",code.c_str()); - fclose(tmpSrc); - FILE *tmpMake = fopen("_tmpMake","w"); - fprintf(tmpMake, "include $(DG_BUILD_DIR)/CMakeFiles/dg.dir/flags.make\n" - "%s : %s\n" - "\tg++ -fPIC -shared -o $@ $(CXX_FLAGS) $(CXX_DEFINES) $<\n", - filename.c_str(), "_tmpSrc.cpp"); - fclose(tmpMake); - if(system("make -f _tmpMake")) - Msg::Error("make command failed\n"); - UnlinkFile("_tmpSrc.cpp"); - UnlinkFile("_tmpMake.cpp"); - } - void call (dataCacheMap *m, fullMatrix<double> &val) - { - switch (args.size()) { - case 0 : - ((void (*)(dataCacheMap*, fullMatrix<double> &))(callback))(m, val); - break; - case 1 : - ((void (*)(dataCacheMap*, fullMatrix<double> &, const fullMatrix<double>&)) - (callback)) (m, val, args[0]); - break; - case 2 : - ((void (*)(dataCacheMap*, fullMatrix<double> &, const fullMatrix<double>&, - const fullMatrix<double> &)) - (callback)) (m, val, args[0], args[1]); - break; - case 3 : - ((void (*)(dataCacheMap*, fullMatrix<double> &, const fullMatrix<double>&, - const fullMatrix<double>&, const fullMatrix<double>&)) - (callback)) (m, val, args[0], args[1], args[2]); - break; - case 4 : - ((void (*)(dataCacheMap*, fullMatrix<double> &, const fullMatrix<double>&, - const fullMatrix<double>&, const fullMatrix<double>&, - const fullMatrix<double>&)) - (callback)) (m, val, args[0], args[1], args[2], args[3]); - break; - case 5 : - ((void (*)(dataCacheMap*, fullMatrix<double> &, const fullMatrix<double>&, - const fullMatrix<double>&, const fullMatrix<double>&, - const fullMatrix<double>&, const fullMatrix<double>&)) - (callback)) (m, val, args[0], args[1], args[2], args[3], args[4]); - break; - case 6 : - ((void (*)(dataCacheMap*, fullMatrix<double> &, const fullMatrix<double>&, - const fullMatrix<double>&, const fullMatrix<double>&, - const fullMatrix<double>&, const fullMatrix<double>&, - const fullMatrix<double>&)) - (callback)) (m, val, args[0], args[1], args[2], args[3], args[4], args[5]); - break; - default : - Msg::Error("C callback not implemented for %i argurments", args.size()); - } +void functionC::buildLibrary(std::string code, std::string filename) +{ + //todo use CMAKE_CXX_COMPILER + //todo use clean temporary file names + //todo work on windows :-) + //todo if DG_BUILD_DIR is not defined, use the directory used at compilation time + FILE *tmpSrc = fopen("_tmpSrc.cpp","w"); + fprintf(tmpSrc, "%s\n",code.c_str()); + fclose(tmpSrc); + FILE *tmpMake = fopen("_tmpMake","w"); + fprintf(tmpMake, "include $(DG_BUILD_DIR)/CMakeFiles/dg.dir/flags.make\n" + "%s : %s\n" + "\tg++ -fPIC -shared -o $@ $(CXX_FLAGS) $(CXX_DEFINES) $<\n", + filename.c_str(), "_tmpSrc.cpp"); + fclose(tmpMake); + if(system("make -f _tmpMake")) + Msg::Error("make command failed\n"); + UnlinkFile("_tmpSrc.cpp"); + UnlinkFile("_tmpMake.cpp"); +} +void functionC::call (dataCacheMap *m, fullMatrix<double> &val) +{ + switch (args.size()) { + case 0 : + ((void (*)(dataCacheMap*, fullMatrix<double> &))(callback))(m, val); + break; + case 1 : + ((void (*)(dataCacheMap*, fullMatrix<double> &, const fullMatrix<double>&)) + (callback)) (m, val, args[0]); + break; + case 2 : + ((void (*)(dataCacheMap*, fullMatrix<double> &, const fullMatrix<double>&, + const fullMatrix<double> &)) + (callback)) (m, val, args[0], args[1]); + break; + case 3 : + ((void (*)(dataCacheMap*, fullMatrix<double> &, const fullMatrix<double>&, + const fullMatrix<double>&, const fullMatrix<double>&)) + (callback)) (m, val, args[0], args[1], args[2]); + break; + case 4 : + ((void (*)(dataCacheMap*, fullMatrix<double> &, const fullMatrix<double>&, + const fullMatrix<double>&, const fullMatrix<double>&, + const fullMatrix<double>&)) + (callback)) (m, val, args[0], args[1], args[2], args[3]); + break; + case 5 : + ((void (*)(dataCacheMap*, fullMatrix<double> &, const fullMatrix<double>&, + const fullMatrix<double>&, const fullMatrix<double>&, + const fullMatrix<double>&, const fullMatrix<double>&)) + (callback)) (m, val, args[0], args[1], args[2], args[3], args[4]); + break; + case 6 : + ((void (*)(dataCacheMap*, fullMatrix<double> &, const fullMatrix<double>&, + const fullMatrix<double>&, const fullMatrix<double>&, + const fullMatrix<double>&, const fullMatrix<double>&, + const fullMatrix<double>&)) + (callback)) (m, val, args[0], args[1], args[2], args[3], args[4], args[5]); + break; + default : + Msg::Error("C callback not implemented for %i argurments", args.size()); } - functionC (std::string file, std::string symbol, int nbCol, - std::vector<const function *> dependencies): - function(nbCol) - { +} +functionC::functionC (std::string file, std::string symbol, int nbCol, + std::vector<const function *> dependencies): + function(nbCol) +{ #if defined(HAVE_DLOPEN) - args.resize(dependencies.size()); - for(int i=0; i < dependencies.size(); i++) { - setArgument(args[i], dependencies[i]); - } - void *dlHandler; - dlHandler = dlopen(file.c_str(),RTLD_NOW); - callback = (void(*)(void))dlsym(dlHandler, symbol.c_str()); - if(!callback) - Msg::Error("Cannot get the callback to the compiled C function"); + args.resize(dependencies.size()); + for(int i=0; i < dependencies.size(); i++) { + setArgument(args[i], dependencies[i]); + } + void *dlHandler; + dlHandler = dlopen(file.c_str(),RTLD_NOW); + callback = (void(*)(void))dlsym(dlHandler, symbol.c_str()); + if(!callback) + Msg::Error("Cannot get the callback to the compiled C function"); #else - Msg::Error("Cannot construct functionC without dlopen"); + Msg::Error("Cannot construct functionC without dlopen"); #endif - } -}; +} void function::registerBindings(binding *b) diff --git a/Solver/function.h b/Solver/function.h index c53801f1144cf31c321024bd44519361d531cee4..73214942056053804bc319ff5501e478c770a2b8 100644 --- a/Solver/function.h +++ b/Solver/function.h @@ -110,6 +110,16 @@ class functionSolution : public function { } }; +class functionSolutionGradient : public function { + static functionSolutionGradient *_instance; + // constructor is private only 1 instance can exists, call get to + // access the instance + functionSolutionGradient(); + public: + void call(dataCacheMap *m, fullMatrix<double> &sol); + static function *get(); +}; + class functionReplaceCache { public: dataCacheMap *map; @@ -270,6 +280,16 @@ class functionConstant : public function { void set(double val); }; +class functionC : public function { + std::vector<fullMatrix<double> > args; + void (*callback)(void); + public: + static void buildLibrary(std::string code, std::string filename) ; + void call (dataCacheMap *m, fullMatrix<double> &val) ; + functionC (std::string file, std::string symbol, int nbCol, + std::vector<const function *> dependencies); +}; + functionConstant *functionConstantNew(const std::vector<double>&); functionConstant *functionConstantNew(double); diff --git a/Solver/functionPython.h b/Solver/functionPython.h index 75465278247f694bf105979c16642c27af8128e7..68c2a4f3b215fb23bf667e9c4f73a55e0706dfd4 100644 --- a/Solver/functionPython.h +++ b/Solver/functionPython.h @@ -26,14 +26,15 @@ class functionPython : public function { } switch(args.size()) { case 0 : _pyargs = Py_BuildValue("(O)", _swigR); break; - case 1 : _pyargs = Py_BuildValue("(OO)", _swigR, _swigA[0], _swigA[1], _swigA[2], _swigA[3], _swigA[4], _swigA[5], _swigA[6]); break; - case 2 : _pyargs = Py_BuildValue("(OOO)", _swigR, _swigA[0], _swigA[1], _swigA[2], _swigA[3], _swigA[4], _swigA[5], _swigA[6]); break; - case 3 : _pyargs = Py_BuildValue("(OOOO)", _swigR, _swigA[0], _swigA[1], _swigA[2], _swigA[3], _swigA[4], _swigA[5], _swigA[6]); break; - case 4 : _pyargs = Py_BuildValue("(OOOOO)", _swigR, _swigA[0], _swigA[1], _swigA[2], _swigA[3], _swigA[4], _swigA[5], _swigA[6]); break; - case 5 : _pyargs = Py_BuildValue("(OOOOOO)", _swigR, _swigA[0], _swigA[1], _swigA[2], _swigA[3], _swigA[4], _swigA[5], _swigA[6]); break; - case 6 : _pyargs = Py_BuildValue("(OOOOOOO)", _swigR, _swigA[0], _swigA[1], _swigA[2], _swigA[3], _swigA[4], _swigA[5], _swigA[6]); break; + case 1 : _pyargs = Py_BuildValue("(OO)", _swigR, _swigA[0]); break; + case 2 : _pyargs = Py_BuildValue("(OOO)", _swigR, _swigA[0], _swigA[1]); break; + case 3 : _pyargs = Py_BuildValue("(OOOO)", _swigR, _swigA[0], _swigA[1], _swigA[2]); break; + case 4 : _pyargs = Py_BuildValue("(OOOOO)", _swigR, _swigA[0], _swigA[1], _swigA[2], _swigA[3]); break; + case 5 : _pyargs = Py_BuildValue("(OOOOOO)", _swigR, _swigA[0], _swigA[1], _swigA[2], _swigA[3], _swigA[4]); break; + case 6 : _pyargs = Py_BuildValue("(OOOOOOO)", _swigR, _swigA[0], _swigA[1], _swigA[2], _swigA[3], _swigA[4], _swigA[5]); break; case 7 : _pyargs = Py_BuildValue("(OOOOOOOO)", _swigR, _swigA[0], _swigA[1], _swigA[2], _swigA[3], _swigA[4], _swigA[5], _swigA[6]); break; - default:Msg::Error("python function not implemented for more than 7 arguments"); + case 8 : _pyargs = Py_BuildValue("(OOOOOOOOO)", _swigR, _swigA[0], _swigA[1], _swigA[2], _swigA[3], _swigA[4], _swigA[5], _swigA[6], _swigA[7]); break; + default:Msg::Error("python function not implemented for more than 8 arguments"); } } };