From 5a7695f3dd5d7b44a85886e4d933b4d2ec9ddeeb Mon Sep 17 00:00:00 2001 From: Jonathan Lambrechts <jonathan.lambrechts@uclouvain.be> Date: Tue, 11 Dec 2012 15:05:08 +0000 Subject: [PATCH] add simpleFunctionPython --- Mesh/DivideAndConquer.h | 1 + Mesh/surfaceFiller.cpp | 1 + Numeric/simpleFunction.h | 2 +- Numeric/simpleFunctionPython.h | 37 ++++++++++++++++++++++++++++++++++ contrib/bamg/bamglib/Mesh2.h | 1 + wrappers/gmshpy/gmshNumeric.i | 5 +++++ 6 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 Numeric/simpleFunctionPython.h diff --git a/Mesh/DivideAndConquer.h b/Mesh/DivideAndConquer.h index 1907f2dcf1..30fc2ecb3e 100644 --- a/Mesh/DivideAndConquer.h +++ b/Mesh/DivideAndConquer.h @@ -14,6 +14,7 @@ #include "SPoint2.h" #include "simpleFunction.h" #include "Octree.h" +#include "MElement.h" class GFace; typedef struct _CDLIST DListRecord, *DListPeek; diff --git a/Mesh/surfaceFiller.cpp b/Mesh/surfaceFiller.cpp index c531b3d8ae..097588743a 100644 --- a/Mesh/surfaceFiller.cpp +++ b/Mesh/surfaceFiller.cpp @@ -13,6 +13,7 @@ #endif #include "MVertex.h" +#include "MElement.h" //#include "directions3D.h" #include "BackgroundMesh.h" #include "intersectCurveSurface.h" diff --git a/Numeric/simpleFunction.h b/Numeric/simpleFunction.h index 09fce8f485..4b56eb33c2 100644 --- a/Numeric/simpleFunction.h +++ b/Numeric/simpleFunction.h @@ -7,7 +7,7 @@ #define _SIMPLE_FUNCTION_H_ // FIXME: Numeric/ should not depend on Geo/ -#include "MElement.h" +class MElement; template <class scalar> class simpleFunction { diff --git a/Numeric/simpleFunctionPython.h b/Numeric/simpleFunctionPython.h new file mode 100644 index 0000000000..f3f0549e6c --- /dev/null +++ b/Numeric/simpleFunctionPython.h @@ -0,0 +1,37 @@ +#ifndef _SIMPLE_FUNCTION_PYTHON_H_ +#define _SIMPLE_FUNCTION_PYTHON_H_ +#include "Python.h" +#include "simpleFunction.h" + +class simpleFunctionPython : public simpleFunction<double> { + PyObject *_pycallback; + public: + simpleFunctionPython(PyObject *callback): + _pycallback(callback) + { + Py_INCREF(_pycallback); + } + ~simpleFunctionPython() + { + Py_DECREF(_pycallback); + } + double operator()(double x, double y, double z) const + { + PyObject *pyargs = Py_BuildValue("(ddd)", x, y, z); + PyObject *result = PyEval_CallObject(_pycallback, pyargs); + double r = 0; + if (result) { + int ok = PyArg_Parse(result, "d", &r); + if (not ok) + Msg::Error("The python function did not return a double."); + Py_DECREF(result); + } + else { + PyErr_Print(); + Msg::Error("An error occurs in the python simple function."); + } + Py_DECREF(pyargs); + return r; + } +}; +#endif diff --git a/contrib/bamg/bamglib/Mesh2.h b/contrib/bamg/bamglib/Mesh2.h index 4d02159a19..ce475c0006 100644 --- a/contrib/bamg/bamglib/Mesh2.h +++ b/contrib/bamg/bamglib/Mesh2.h @@ -30,6 +30,7 @@ #include <math.h> #include <limits.h> #include <time.h> +#include "MElement.h" #if (defined(unix) || defined(__unix)) && !defined(__AIX) #define SYSTIMES #include <sys/times.h> diff --git a/wrappers/gmshpy/gmshNumeric.i b/wrappers/gmshpy/gmshNumeric.i index 11a968608f..b57c4182a7 100644 --- a/wrappers/gmshpy/gmshNumeric.i +++ b/wrappers/gmshpy/gmshNumeric.i @@ -11,6 +11,8 @@ #include "JacobianBasis.h" #include "fullMatrix.h" #include "nodalBasis.h" + #include "simpleFunction.h" + #include "simpleFunctionPython.h" #include "polynomialBasis.h" #include "pyramidalBasis.h" %} @@ -18,8 +20,11 @@ %include "GaussIntegration.h" %include "JacobianBasis.h" %include "fullMatrix.h" +%include "simpleFunction.h" %template(fullMatrixDouble) fullMatrix<double>; %template(fullVectorDouble) fullVector<double>; +%template(simpleFunctionDouble) simpleFunction<double>; +%include "simpleFunctionPython.h" %include "nodalBasis.h" %include "polynomialBasis.h" %include "pyramidalBasis.h" -- GitLab