source: inundation/pymetis/pymetis/metis.c @ 2071

Last change on this file since 2071 was 2071, checked in by jack, 18 years ago

Improved documentation quality

File size: 2.5 KB
Line 
1#include <Python.h>
2#include <Numeric/arrayobject.h>
3
4/* This must be the same as the metis idxtype */
5typedef int idxtype;
6
7#include "bridge.h"
8
9static PyObject * metis_partMeshNodal(PyObject *, PyObject *);
10
11static PyMethodDef methods[] = {
12  {"partMeshNodal", metis_partMeshNodal, METH_VARARGS, "METIS_PartMeshNodal"},
13  {NULL, NULL, 0, NULL}
14};
15
16PyMODINIT_FUNC initmetis(void){
17  (void) Py_InitModule("metis", methods);
18
19  import_array();
20}
21
22/* Run the metis METIS_PartMeshNodal function
23 * expected args:
24 * ne: number of elements
25 * nn: number of nodes
26 * elmnts: element array
27 * etype: type of mesh elements:
28 ** 1 - triangle
29 ** 2 - tetrahedra
30 ** 3 - hexahedra
31 ** 4 - quadrilaterals
32 * nparts: number of partitions
33 * returns:
34 * edgecut: number of cut edges
35 * epart: partitioning of the elements
36 * npart: partitioning of the nodes.
37 *
38 * Note that while the metis data file format indexes verticies from 1,
39 * the library calls (including this one) use vericies indexed from 0.
40 * Unsure if this is because of the num_flag option, perhaps calling
41 * from a FORTRAN program will have 1-indexed verticies?
42 */
43static PyObject * metis_partMeshNodal(PyObject * self, PyObject * args){
44  int ne;
45  int nn;
46  int etype;
47  int nparts;
48  int edgecut;
49  int numflag = 0; // The metis routine requires an int * for numflag.
50  int dims[1]; // PyArray_FromDimsAndData needs an int[] of array sizes.
51
52  PyObject * elements;
53  PyArrayObject * elem_arr;
54  PyArrayObject * epart_pyarr;
55  PyArrayObject * npart_pyarr;
56
57  /* These are all of the metis idxtype */
58  idxtype * epart;
59  idxtype * npart;
60  if(!PyArg_ParseTuple(args, "iiOii", &ne, &nn, &elements, &etype, &nparts))
61    return NULL;
62
63  elem_arr = (PyArrayObject *) PyArray_ContiguousFromObject(elements, PyArray_INT, 1, 1);
64
65  if(!elem_arr)
66    return NULL;
67
68  epart = (idxtype *)malloc(ne * sizeof(idxtype));
69  if(epart == NULL){
70    Py_DECREF(elem_arr);
71    return NULL;
72  }
73  npart = (idxtype *)malloc(nn * sizeof(idxtype));
74  if(npart == NULL){
75    free(epart);
76    Py_DECREF(elem_arr);
77    return NULL;
78  }
79  bridge_partMeshNodal(&ne, &nn, (idxtype *)elem_arr->data, &etype, &numflag, &nparts, &edgecut, epart, npart);
80
81  dims[0] = ne;
82  epart_pyarr = (PyArrayObject *)PyArray_FromDimsAndData(1, dims, PyArray_INT, (char *)epart);
83  dims[0] = nn;
84  npart_pyarr = (PyArrayObject *)PyArray_FromDimsAndData(1, dims, PyArray_INT, (char *)npart);
85
86  Py_DECREF(elem_arr);
87
88  return Py_BuildValue("iOO", edgecut, (PyObject *)epart_pyarr, (PyObject *)npart_pyarr);
89}
Note: See TracBrowser for help on using the repository browser.