Skip to content

Commit

Permalink
Merge pull request #213 from stoiver/anuga_py3
Browse files Browse the repository at this point in the history
Anuga py3 CI installs
  • Loading branch information
stoiver committed Nov 8, 2019
2 parents 007f8ec + 9fa8de9 commit 0886b72
Show file tree
Hide file tree
Showing 12 changed files with 923 additions and 2,117 deletions.
89 changes: 4 additions & 85 deletions anuga/abstract_2d_finite_volumes/neighbour_table_ext.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "Python.h"
#include "numpy/arrayobject.h"

#include <cstdio> /* gets */
#include <cstdlib> /* atoi, malloc */
#include <cstring> /* strcpy */
//#include <cmath> /* math!!! */

// Hack to avoid ::hypot error using mingw on windows
#define CYTHON_CCOMPLEX 0

// This could be replaced with fast drop-inreplacements
// that are around and open. like https://github.com/greg7mdp/sparsepp
// TODO: But lets see if performance of unordered_map is any problem first.
Expand All @@ -14,7 +14,7 @@
#include <functional> /* std::hash */

//Shared code snippets
#include "util_ext.h" /* in utilities */
//#include "util_ext.h" /* in utilities */

// basic type used for keys and counters
// should be the same type as triangle/coordinate ids
Expand Down Expand Up @@ -208,84 +208,3 @@ int _build_neighbour_structure(keyint N, keyint M,
return err;
}


//==============================================================================
// Python method Wrapper
//==============================================================================

PyObject *build_neighbour_structure(PyObject *self, PyObject *args) {

/*
* Update neighbours array using triangles array
*
* N is number of nodes (vertices)
* triangle nodes defining triangles
* neighbour across edge_id
* neighbour_edges edge_id of edge in neighbouring triangle
* number_of_boundaries
*/

PyArrayObject *neighbours, *neighbour_edges, *number_of_boundaries;
PyArrayObject *triangles;

keyint N; // Number of nodes (read in)
keyint M; // Number of triangles (calculated from triangle array)
int err;


// Convert Python arguments to C
if (!PyArg_ParseTuple(args, "iOOOO", &N,
&triangles,
&neighbours,
&neighbour_edges,
&number_of_boundaries
)) {
PyErr_SetString(PyExc_RuntimeError,
"hashtable.c: create_neighbours could not parse input");
return NULL;
}


CHECK_C_CONTIG(triangles);
CHECK_C_CONTIG(neighbours);
CHECK_C_CONTIG(neighbour_edges);
CHECK_C_CONTIG(number_of_boundaries);


M = triangles -> dimensions[0];


err = _build_neighbour_structure(N, M,
(long*) triangles -> data,
(long*) neighbours -> data,
(long*) neighbour_edges -> data,
(long*) number_of_boundaries -> data);


if (err != 0) {
PyErr_SetString(PyExc_RuntimeError,
"Duplicate Edge");
return NULL;
}


return Py_BuildValue("");
}


//==============================================================================
// Structures to allow calling from python
//==============================================================================
extern "C" {
// Method table for python module
static struct PyMethodDef MethodTable[] = {
{"build_neighbour_structure", build_neighbour_structure, METH_VARARGS, "Print out"},
{NULL, NULL, 0, NULL} // sentinel
};

// Module initialisation
void initneighbour_table_ext(void){
Py_InitModule("neighbour_table_ext", MethodTable);
import_array(); // Necessary for handling of NumPY structures
}
};
31 changes: 31 additions & 0 deletions anuga/abstract_2d_finite_volumes/neighbour_table_interface.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# cython: boundscheck=False
# cython: wraparound=False
import cython

# import both numpy and the Cython declarations for numpy
import numpy as np
cimport numpy as np

ctypedef long keyint

# declare the interface to the C code
cdef extern from "neighbour_table_ext.cpp":
int _build_neighbour_structure(keyint N, keyint M, long* triangles, long* neighbours, long* neighbour_edges, long* number_of_boundaries)

def build_neighbour_structure(keyint N,\
np.ndarray[long, ndim=2, mode="c"] triangles not None,\
np.ndarray[long, ndim=2, mode="c"] neighbours not None,\
np.ndarray[long, ndim=2, mode="c"] neighbour_edges not None,\
np.ndarray[long, ndim=1, mode="c"] number_of_boundaries not None):

cdef keyint M
cdef int err

M = triangles.shape[0]

err = _build_neighbour_structure(N, M, &triangles[0,0], &neighbours[0,0], &neighbour_edges[0,0], &number_of_boundaries[0])

assert err == 0, "Duplicate Edge"



Loading

0 comments on commit 0886b72

Please sign in to comment.