0

I am writing a C module for python to manipulate coordinates. The coordinates are a 2D Numpy matrix like this:

[[x1 y1 z1] [x2 y2 z2] ... [x231, y231, z231]]

My code, currently, is here (I left out the PyMethodDef, the init and the python script because they work fine):

#include <stdio.h>
#include <stdlib.h>
#include <python2.7/Python.h>
#include <math.h>
#include "numpy/arrayobject.h"
#include "spin.h"

static PyObject *get_spinangle_traj(PyObject *self, PyObject *args){

/* So, the first one will be the matrix of trajectory coordinates */
PyObject *coords_matrix;
/* And the second one is the matrix of the reference coordinates */
PyObject *ref_matrix;
/* How many atoms ? */
int num_atoms;
/* debug */
float debug=0.0;

/* Should get the python objects */
if (!PyArg_ParseTuple(args,"O!O!i",&PyArray_Type,&coords_matrix,&PyArray_Type,&ref_matrix,&num_atoms)){
    PyErr_SetString(PyExc_ValueError,"Error while parsing the trajectory coordinates in get_spinangle_traj");
    return NULL;
}

/* Get pointers to the data as C-types. */
float *coords    = (float*)PyArray_DATA(coords_matrix);
float *ref    = (float*)PyArray_DATA(ref_matrix);

/* Call the external C function to get the spinangle */
float spinangle = get_spinangle(coords, ref, num_atoms, debug);

/* Build the output tuple */
PyObject *ret = Py_BuildValue("f", spinangle);
return ret;
}

float get_spinangle(float *coords, float *ref, int num_atoms, float debug){
/* Do stuff now, and hope everything went smoothly */

/* Initialize stuff */
float angle=0.0;
float quat_matrix[4][4];

/* Build the quaternion matrix */
build_matrix(coords, ref, quat_matrix, num_atoms, debug);

return debug;
}

float build_matrix(float *coords, float *ref, float quat_matrix[4][4], int num_atoms, float debug){
/* Build the quaternion matrix - first with the correlation matrix */
float cor_matrix[3][3];
int i;

for (i=0; i<num_atoms; i++){
    debug += coords[i];
    printf("coord %f\n", coords[i]);
}

return debug;

Basically, in that last function, I'm trying to get to the coordinates to do stuff with them. At the same time, in the python script, I'm printing the coordinates and they don't match at all. I'm guessing the problematic line is

float *coords    = (float*)PyArray_DATA(coords_matrix);

But I don't have a really good understanding of these arrays to figure out what is going on.

Once I can access the coordinates in a reliable way, I can do what I want with them, this won't be a problem. Accessing them (and maybe returning a python array after) is a much bigger problem.

Marlon
  • 47
  • 2
  • 7
  • 1
    In C, `float` is single precision; it corresponds to the numpy type `float32`. The default type for numpy floating points arrays is `float64`. Unless you know that you are passing in arrays with type `float32`, I suspect your C `float` declarations should be changed to `double` (the C type for 64 bit floating point, a.k.a double precision). – Warren Weckesser Nov 09 '16 at 17:04
  • https://docs.scipy.org/doc/numpy/reference/c-api.array.html#data-access – DavidW Nov 10 '16 at 09:39
  • Thanks, I could access the coordinates with double instead of float. Now to access them 3 by 3, but I'll handle the rest, thanks again :) – Marlon Nov 14 '16 at 14:35

0 Answers0