[1093] | 1 | // Python - C extension for finite_volumes util module. |
---|
| 2 | // |
---|
| 3 | // To compile (Python2.3): |
---|
| 4 | // gcc -c util_ext.c -I/usr/include/python2.3 -o util_ext.o -Wall -O |
---|
| 5 | // gcc -shared util_ext.o -o util_ext.so |
---|
| 6 | // |
---|
| 7 | // See the module util.py |
---|
| 8 | // |
---|
| 9 | // |
---|
| 10 | // Ole Nielsen, GA 2004 |
---|
| 11 | // |
---|
| 12 | //NOTE: On 64 bit systems use long* instead of int* for Numeric arrays |
---|
| 13 | //this will also work on 32 bit systems |
---|
| 14 | |
---|
[2738] | 15 | #include <float.h> |
---|
[1093] | 16 | |
---|
| 17 | #include "Python.h" |
---|
| 18 | #include "Numeric/arrayobject.h" |
---|
| 19 | #include "math.h" |
---|
| 20 | |
---|
| 21 | //Shared snippets |
---|
| 22 | #include "util_ext.h" |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | PyObject *gradient(PyObject *self, PyObject *args) { |
---|
| 28 | // |
---|
| 29 | // a,b = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
| 30 | // |
---|
| 31 | |
---|
| 32 | double x0, y0, x1, y1, x2, y2, q0, q1, q2, a, b; |
---|
| 33 | PyObject *result; |
---|
| 34 | |
---|
| 35 | // Convert Python arguments to C |
---|
| 36 | if (!PyArg_ParseTuple(args, "ddddddddd", &x0, &y0, &x1, &y1, &x2, &y2, |
---|
| 37 | &q0, &q1, &q2)) |
---|
| 38 | return NULL; |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | // Call underlying routine |
---|
| 42 | _gradient(x0, y0, x1, y1, x2, y2, |
---|
| 43 | q0, q1, q2, &a, &b); |
---|
| 44 | |
---|
| 45 | // Return values a and b |
---|
| 46 | result = Py_BuildValue("dd", a, b); |
---|
| 47 | return result; |
---|
| 48 | } |
---|
| 49 | |
---|
[1486] | 50 | PyObject *gradient2(PyObject *self, PyObject *args) { |
---|
| 51 | // |
---|
| 52 | // a,b = gradient2(x0, y0, x1, y1, q0, q1) |
---|
| 53 | // |
---|
[1093] | 54 | |
---|
[1486] | 55 | double x0, y0, x1, y1, q0, q1, a, b; |
---|
| 56 | PyObject *result; |
---|
| 57 | |
---|
| 58 | // Convert Python arguments to C |
---|
| 59 | if (!PyArg_ParseTuple(args, "dddddd", &x0, &y0, &x1, &y1, &q0, &q1)) |
---|
| 60 | return NULL; |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | // Call underlying routine |
---|
| 64 | _gradient2(x0, y0, x1, y1, q0, q1, &a, &b); |
---|
| 65 | |
---|
| 66 | // Return values a and b |
---|
| 67 | result = Py_BuildValue("dd", a, b); |
---|
| 68 | return result; |
---|
| 69 | } |
---|
| 70 | |
---|
[2738] | 71 | PyObject * double_precision(PyObject * self, PyObject * args){ |
---|
| 72 | // Get the precision of the double datatype on this system. |
---|
| 73 | return Py_BuildValue("i", DBL_DIG); |
---|
| 74 | } |
---|
| 75 | |
---|
[1093] | 76 | // Method table for python module |
---|
| 77 | static struct PyMethodDef MethodTable[] = { |
---|
| 78 | /* The cast of the function is necessary since PyCFunction values |
---|
| 79 | * only take two PyObject* parameters, and rotate() takes |
---|
| 80 | * three. |
---|
| 81 | */ |
---|
| 82 | |
---|
| 83 | //{"rotate", (PyCFunction)rotate, METH_VARARGS | METH_KEYWORDS, "Print out"}, |
---|
| 84 | {"gradient", gradient, METH_VARARGS, "Print out"}, |
---|
[1486] | 85 | {"gradient2", gradient2, METH_VARARGS, "Print out"}, |
---|
[2738] | 86 | {"double_precision", double_precision, METH_VARARGS, "Precision of this machine\'s \'double\' type"}, |
---|
[1093] | 87 | {NULL, NULL, 0, NULL} /* sentinel */ |
---|
| 88 | }; |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | // Module initialisation |
---|
| 93 | void initutil_ext(void){ |
---|
| 94 | Py_InitModule("util_ext", MethodTable); |
---|
| 95 | |
---|
| 96 | import_array(); //Necessary for handling of NumPY structures |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | |
---|