diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c586aa7edd53b1bad347532232fa9e82ed13b31..11b440aab8c247839ab27e27c67dcd05d1114daa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1125,9 +1125,8 @@ if(ENABLE_SWIG_EXPERIMENTAL) FIND_PACKAGE(SWIG REQUIRED) INCLUDE(${SWIG_USE_FILE}) FIND_PACKAGE(PythonLibs) - INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR};${PYTHON_INCLUDE_DIR};../Common;${CMAKE_CURRENT_BINARY_DIR}/../Common) - SET_SOURCE_FILES_PROPERTIES(Geo/GModel.i PROPERTIES CPLUSPLUS ON) - #SET_SOURCE_FILES_PROPERTIES(Geo/GModel.i SWIG_FLAGS "-includeall -importall") - SWIG_ADD_MODULE(gmshPython python Geo/GModel.i ${GMSH_SRC}) - SWIG_LINK_LIBRARIES(gmshPython ${PYTHON_LIBRARIES} ${EXTERNAL_LIBRARIES} ${LINK_LIBRARIES}) + INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_DIR}) + SET_SOURCE_FILES_PROPERTIES(Common/gmshpy.i PROPERTIES CPLUSPLUS ON) + SWIG_ADD_MODULE(gmshpy python Common/gmshpy.i) + SWIG_LINK_LIBRARIES(gmshpy ${PYTHON_LIBRARIES} ${EXTERNAL_LIBRARIES} ${LINK_LIBRARIES} shared) endif(ENABLE_SWIG_EXPERIMENTAL) diff --git a/Common/gmshpy.i b/Common/gmshpy.i new file mode 100644 index 0000000000000000000000000000000000000000..90472b63e19b25aa4025fd7a9df4748c5a698c78 --- /dev/null +++ b/Common/gmshpy.i @@ -0,0 +1,82 @@ +%feature("autodoc", "1"); +%module gmshpy +%include std_string.i +%include std_vector.i +%{ + #include "GModel.h" + #include "fullMatrix.h" + #include "function.h" + #include "dofManager.h" + #include "linearSystem.h" + #include "linearSystemPETSc.h" + #include "linearSystemPETSc.cpp" // needed for the specialization + #include "GEntity.h" + #include "GVertex.h" + #include "GEdge.h" + #include "GFace.h" + #include "GRegion.h" + #include "GPoint.h" + #include "MElement.h" + #include "MVertex.h" + #include "MTriangle.h" + #include "MPrism.h" + #include "MQuadrangle.h" + #include "MLine.h" + #include "GmshMessage.h" + #include "polynomialBasis.h" + #include "Gauss.h" + #include "meshPartitionOptions.h" + #include "linearSystemCSR.h" + #include "elasticitySolver.h" + #include "PView.h" + #include "PViewData.h" + #include "PViewFactory.h" + #include "DivideAndConquer.h" + #include "Gmsh.h" + #include "functionPython.h" +%} + +namespace std { + %template(IntVector) vector<int>; + %template(DoubleVector) vector<double, std::allocator<double> >; + %template(VectorFunctionConst) vector<const function*, std::allocator<const function*> >; +} + +%include "fullMatrix.h" +%include "dofManager.h" +%include "GModel.h" +%include "function.h" +%include "linearSystem.h" +%include "linearSystemPETSc.h" +%include "GEntity.h" +%include "GVertex.h" +%include "GEdge.h" +%include "GFace.h" +%include "GRegion.h" +%include "GPoint.h" +%include "MElement.h" +%include "MVertex.h" +%include "MTriangle.h" +%include "MPrism.h" +%include "MQuadrangle.h" +%include "MLine.h" +%include "GmshMessage.h" +%include "polynomialBasis.h" +%include "Gauss.h" +%include "meshPartitionOptions.h" +%include "linearSystemCSR.h" +%include "elasticitySolver.h" +%include "PView.h" +%include "PViewData.h" +%include "PViewFactory.h" +%include "DivideAndConquer.h" +%include "Gmsh.h" + +%template(dofManagerDouble) dofManager<double>; +%template(linearSystemDouble) linearSystem<double>; +%template(linearSystemFullMatrixDouble) linearSystem<fullMatrix<double> >; +%template(linearSystemPETScDouble) linearSystemPETSc<double>; +%template(fullMatrixDouble) fullMatrix<double>; +%template(linearSystemPETScBlockDouble) linearSystemPETSc<fullMatrix<double> >; +%include "functionPython.h" + diff --git a/Solver/functionPython.h b/Solver/functionPython.h new file mode 100644 index 0000000000000000000000000000000000000000..75465278247f694bf105979c16642c27af8128e7 --- /dev/null +++ b/Solver/functionPython.h @@ -0,0 +1,40 @@ +#ifndef _FUNCTION_PYTHON_H_ +#define _FUNCTION_PYTHON_H_ +#include "Python.h" +class functionPython : public function { + PyObject *_pycallback, *_pyargs; + PyObject *_swigR, *_swigCacheMap; + std::vector<PyObject*> _swigA; + std::string _luaFunctionName; + std::vector<fullMatrix<double> > args; + fullMatrix<double> R; + public: + void call (dataCacheMap *m, fullMatrix<double> &res) + { + R.setAsProxy(res); + PyEval_CallObject(_pycallback, _pyargs); + } + functionPython (int nbCol, PyObject *callback, std::vector<const function*> dependencies) + : function(nbCol), _pycallback(callback) + { + args.resize(dependencies.size()); + _swigA.resize(args.size()); + _swigR = SWIG_NewPointerObj((void*)&R,SWIGTYPE_p_fullMatrixT_double_t, 0); + for (int i = 0; i < dependencies.size(); i++) { + setArgument(args[i], dependencies[i]); + _swigA[i] = SWIG_NewPointerObj((void*)&args[i],SWIGTYPE_p_fullMatrixT_double_t, 0); + } + 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 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"); + } + } +}; +#endif