#include "Python.h" #include "numpy/arrayobject.h" #include #include #include #include "util_ext.h" //Rough quicksort implementation (for build_operator_matrix) int *quicksort(int *array, int n) { int *less, *more, pivot, i, num_less, num_more, *sorted; less = malloc(sizeof(int)*n); more = malloc(sizeof(int)*n); sorted = malloc(sizeof(int)*n); if (n<2) return array; pivot = array[n-1]; //choose the last element as a pivot num_less = 0; num_more = 0; for (i=0; i data, (long *)neighbours -> data, (double *)edgelengths->data, (double *)edge_midpoint_coordinates -> data, (long *)geo_indices -> data, (double *)geo_values -> data); if (err != 0) { PyErr_SetString(PyExc_RuntimeError, "Could not build geo structure"); return NULL; } //Release the arrays Py_DECREF(centroid_coordinates); Py_DECREF(neighbours); Py_DECREF(edgelengths); Py_DECREF(edge_midpoint_coordinates); Py_DECREF(geo_indices); Py_DECREF(geo_values); return Py_BuildValue(""); } static PyObject *py_build_operator_matrix(PyObject *self, PyObject *args) { PyObject *kv_operator; int n, tot_len, err; PyArrayObject *h, *boundary_heights, *geo_indices, *geo_values, *_data, *colind; //Convert Python arguments to C if (!PyArg_ParseTuple(args, "OiiOO", &kv_operator, &n, &tot_len, &h, &boundary_heights)) { PyErr_SetString(PyExc_RuntimeError, "get_stage_height_interactions could not parse input"); return NULL; } geo_indices = get_consecutive_array(kv_operator,"geo_structure_indices"); geo_values = get_consecutive_array(kv_operator,"geo_structure_values"); _data = get_consecutive_array(kv_operator,"operator_data"); colind = get_consecutive_array(kv_operator,"operator_colind"); err = build_operator_matrix(n,tot_len, (long *)geo_indices -> data, (double *)geo_values -> data, (double *)h -> data, (double *)boundary_heights -> data, (double *)_data -> data, (long *)colind -> data); if (err != 0) { PyErr_SetString(PyExc_RuntimeError, "Could not get stage height interactions"); return NULL; } Py_DECREF(geo_indices); Py_DECREF(geo_values); Py_DECREF(_data); Py_DECREF(colind); return Py_BuildValue(""); } static struct PyMethodDef MethodTable[] = { {"build_geo_structure",py_build_geo_structure,METH_VARARGS,"Print out"}, {"build_operator_matrix",py_build_operator_matrix,METH_VARARGS,"Print out"}, {NULL,NULL,0,NULL} // sentinel }; void initkinematic_viscosity_ext(){ (void) Py_InitModule("kinematic_viscosity_ext", MethodTable); import_array(); }