[5897] | 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 | // |
---|
[6304] | 12 | //NOTE: On 64 bit systems use long* instead of int* for numeric arrays |
---|
[5897] | 13 | //this will also work on 32 bit systems |
---|
| 14 | |
---|
| 15 | #include <float.h> |
---|
| 16 | |
---|
| 17 | #include "Python.h" |
---|
[6304] | 18 | #include "numpy/arrayobject.h" |
---|
[5897] | 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 | |
---|
| 39 | PyErr_SetString(PyExc_RuntimeError, |
---|
| 40 | "gradient could not parse input"); |
---|
| 41 | return NULL; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | // Call underlying routine |
---|
| 46 | _gradient(x0, y0, x1, y1, x2, y2, |
---|
| 47 | q0, q1, q2, &a, &b); |
---|
| 48 | |
---|
| 49 | // Return values a and b |
---|
| 50 | result = Py_BuildValue("dd", a, b); |
---|
| 51 | return result; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | PyObject *gradient2(PyObject *self, PyObject *args) { |
---|
| 55 | // |
---|
| 56 | // a,b = gradient2(x0, y0, x1, y1, q0, q1) |
---|
| 57 | // |
---|
| 58 | |
---|
| 59 | double x0, y0, x1, y1, q0, q1, a, b; |
---|
| 60 | PyObject *result; |
---|
| 61 | |
---|
| 62 | // Convert Python arguments to C |
---|
| 63 | if (!PyArg_ParseTuple(args, "dddddd", &x0, &y0, &x1, &y1, &q0, &q1)) { |
---|
| 64 | PyErr_SetString(PyExc_RuntimeError, |
---|
| 65 | "gradient2 could not parse input"); |
---|
| 66 | return NULL; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | // Call underlying routine |
---|
| 71 | _gradient2(x0, y0, x1, y1, q0, q1, &a, &b); |
---|
| 72 | |
---|
| 73 | // Return values a and b |
---|
| 74 | result = Py_BuildValue("dd", a, b); |
---|
| 75 | return result; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | PyObject * double_precision(PyObject * self, PyObject * args){ |
---|
| 79 | // Get the precision of the double datatype on this system. |
---|
| 80 | return Py_BuildValue("i", DBL_DIG); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | // Method table for python module |
---|
| 84 | static struct PyMethodDef MethodTable[] = { |
---|
| 85 | /* The cast of the function is necessary since PyCFunction values |
---|
| 86 | * only take two PyObject* parameters, and rotate() takes |
---|
| 87 | * three. |
---|
| 88 | */ |
---|
| 89 | |
---|
| 90 | //{"rotate", (PyCFunction)rotate, METH_VARARGS | METH_KEYWORDS, "Print out"}, |
---|
| 91 | {"gradient", gradient, METH_VARARGS, "Print out"}, |
---|
| 92 | {"gradient2", gradient2, METH_VARARGS, "Print out"}, |
---|
| 93 | {"double_precision", double_precision, METH_VARARGS, "Precision of this machine\'s \'double\' type"}, |
---|
| 94 | {NULL, NULL, 0, NULL} /* sentinel */ |
---|
| 95 | }; |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | // Module initialisation |
---|
| 100 | void initutil_ext(void){ |
---|
| 101 | Py_InitModule("util_ext", MethodTable); |
---|
| 102 | |
---|
| 103 | import_array(); //Necessary for handling of NumPY structures |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | |
---|