Skip to content

Commit

Permalink
update C extensions to be compatible with numpy > 1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
abelcarreras committed Oct 25, 2024
1 parent 2b4b789 commit 35ad696
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
24 changes: 14 additions & 10 deletions c/correlation.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
#include <math.h>
#include <stdlib.h>
#include <complex.h>
#include <numpy/arrayobject.h>

#if defined(ENABLE_OPENMP)
#include <omp.h>
#endif


#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>

#undef I

// Cross compatibility mingw - gcc
Expand Down Expand Up @@ -109,26 +111,28 @@ static PyObject* correlation_par (PyObject* self, PyObject *arg, PyObject *keywo
static char *kwlist[] = {"frequency", "velocity", "time_step", "step", "integration_method", NULL};
if (!PyArg_ParseTupleAndKeywords(arg, keywords, "OOd|ii", kwlist, &frequency_obj, &velocity_obj, &TimeStep, &Increment, &IntMethod)) return NULL;

PyObject *velocity_array = PyArray_FROM_OTF(velocity_obj, NPY_CDOUBLE, NPY_IN_ARRAY);
PyObject *frequency_array = PyArray_FROM_OTF(frequency_obj, NPY_DOUBLE, NPY_IN_ARRAY);
PyObject *velocity_array = PyArray_FROM_OTF(velocity_obj, NPY_CDOUBLE, NPY_ARRAY_IN_ARRAY);
PyObject *frequency_array = PyArray_FROM_OTF(frequency_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);

if (velocity_array == NULL || frequency_array == NULL ) {
Py_XDECREF(velocity_array);
Py_XDECREF(frequency_array);
return NULL;
}

_Dcomplex *Velocity = (_Dcomplex *)PyArray_DATA(velocity_array);
double *Frequency = (double*)PyArray_DATA(frequency_array);
int NumberOfData = (int)PyArray_DIM(velocity_array, 0);
int NumberOfFrequencies = (int)PyArray_DIM(frequency_array, 0);
_Dcomplex *Velocity = (_Dcomplex *)PyArray_DATA((PyArrayObject *)velocity_array);
double *Frequency = (double*)PyArray_DATA((PyArrayObject *)frequency_array);
int NumberOfData = (int)PyArray_DIM((PyArrayObject *)velocity_array, 0);
int NumberOfFrequencies = (int)PyArray_DIM((PyArrayObject *)frequency_array, 0);


//Create new numpy array for storing result
PyArrayObject *PowerSpectrum_object;
int dims[1]={NumberOfFrequencies};
PowerSpectrum_object = (PyArrayObject *) PyArray_FromDims(1,dims,NPY_DOUBLE);
double *PowerSpectrum = (double*)PyArray_DATA(PowerSpectrum_object);
npy_intp dims[1]={NumberOfFrequencies};
//PowerSpectrum_object = (PyArrayObject *) PyArray_FromDims(1, dims,NPY_DOUBLE);
PowerSpectrum_object = (PyArrayObject *) PyArray_SimpleNew(1, dims, NPY_DOUBLE);

double *PowerSpectrum = (double*)PyArray_DATA((PyArrayObject *)PowerSpectrum_object);

// Maximum Entropy Method Algorithm
if (IntMethod < 0 || IntMethod > 1) {
Expand Down
39 changes: 26 additions & 13 deletions c/displacements.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include <math.h>
#include <stdlib.h>
#include <complex.h>


#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>


Expand Down Expand Up @@ -99,9 +102,9 @@ static PyObject *atomic_displacements(PyObject *self, PyObject *arg, PyObject *k
static char *kwlist[] = {"trajectory", "positions", "cell", NULL};
if (!PyArg_ParseTupleAndKeywords(arg, keywords, "OOO", kwlist, &Trajectory_obj, &Positions_obj, &Cell_obj)) return NULL;

PyObject *Trajectory_array = PyArray_FROM_OTF(Trajectory_obj, NPY_CDOUBLE, NPY_IN_ARRAY);
PyObject *Positions_array = PyArray_FROM_OTF(Positions_obj, NPY_DOUBLE, NPY_IN_ARRAY);
PyObject *Cell_array = PyArray_FROM_OTF(Cell_obj, NPY_DOUBLE, NPY_IN_ARRAY);
PyObject *Trajectory_array = PyArray_FROM_OTF(Trajectory_obj, NPY_CDOUBLE, NPY_ARRAY_IN_ARRAY);
PyObject *Positions_array = PyArray_FROM_OTF(Positions_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
PyObject *Cell_array = PyArray_FROM_OTF(Cell_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);

if (Cell_array == NULL || Trajectory_array == NULL) {
Py_XDECREF(Cell_array);
Expand All @@ -112,11 +115,11 @@ static PyObject *atomic_displacements(PyObject *self, PyObject *arg, PyObject *k


// double _Complex *Cell = (double _Complex*)PyArray_DATA(Cell_array);
_Dcomplex *Trajectory = (_Dcomplex*)PyArray_DATA(Trajectory_array);
double *Positions = (double*)PyArray_DATA(Positions_array);
_Dcomplex *Trajectory = (_Dcomplex*)PyArray_DATA((PyArrayObject *)Trajectory_array);
double *Positions = (double*)PyArray_DATA((PyArrayObject *)Positions_array);

int NumberOfData = (int)PyArray_DIM(Trajectory_array, 0);
int NumberOfDimensions = (int)PyArray_DIM(Cell_array, 0);
int NumberOfData = (int)PyArray_DIM((PyArrayObject *)Trajectory_array, 0);
int NumberOfDimensions = (int)PyArray_DIM((PyArrayObject *)Cell_array, 0);

// Create new Numpy array to store the result
_Dcomplex **Displacement;
Expand Down Expand Up @@ -196,11 +199,18 @@ static PyObject *atomic_displacements(PyObject *self, PyObject *arg, PyObject *k

static _Dcomplex **pymatrix_to_c_array_complex(PyArrayObject *array) {

long n=(*array).dimensions[0];
long m=(*array).dimensions[1];
//long n=(*array).dimensions[0];
//long m=(*array).dimensions[1];

long n = PyArray_DIMS(array)[0];
long m = PyArray_DIMS(array)[1];

_Dcomplex ** c = malloc(n*sizeof(_Dcomplex));

_Dcomplex *a = (_Dcomplex *) (*array).data; /* pointer to array data as double _Complex */
//_Dcomplex *a = (_Dcomplex *) (*array).data; /* pointer to array data as double _Complex */

_Dcomplex *a = (_Dcomplex *) PyArray_DATA(array);

for ( int i=0; i<n; i++) {
c[i]=a+i*m;
}
Expand All @@ -211,12 +221,15 @@ static _Dcomplex **pymatrix_to_c_array_complex(PyArrayObject *array) {

static double **pymatrix_to_c_array_real(PyArrayObject *array) {

long n=(*array).dimensions[0];
long m=(*array).dimensions[1];
// long n=(*array).dimensions[0];
// long m=(*array).dimensions[1];
long n = PyArray_DIMS(array)[0];
long m = PyArray_DIMS(array)[1];
//PyObject *transpose_array = PyArray_Transpose(array, dims);

double ** c = malloc(n*sizeof(double));
double *a = (double *) (*array).data;
// double *a = (double *) (*array).data;
double *a = (double *) PyArray_DATA(array);

for ( int i=0; i<n; i++) {
double *b = malloc(m*sizeof(double));
Expand Down
18 changes: 11 additions & 7 deletions c/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
#include <math.h>
#include <stdlib.h>
#include <complex.h>
#include <numpy/arrayobject.h>

#if defined(ENABLE_OPENMP)
#include <omp.h>
#endif


#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>


#undef I

// Cross compatibility mingw - gcc
Expand Down Expand Up @@ -107,19 +111,19 @@ static PyObject* MaximumEntropyMethod (PyObject* self, PyObject *arg, PyObject *
static char *kwlist[] = {"frequency", "velocity", "time_step", "coefficients", NULL};
if (!PyArg_ParseTupleAndKeywords(arg, keywords, "OOd|i", kwlist, &frequency_obj, &velocity_obj, &TimeStep, &NumberOfCoefficients)) return NULL;

PyObject *velocity_array = PyArray_FROM_OTF(velocity_obj, NPY_CDOUBLE, NPY_IN_ARRAY);
PyObject *frequency_array = PyArray_FROM_OTF(frequency_obj, NPY_DOUBLE, NPY_IN_ARRAY);
PyObject *velocity_array = PyArray_FROM_OTF( velocity_obj, NPY_CDOUBLE, NPY_ARRAY_IN_ARRAY);
PyObject *frequency_array = PyArray_FROM_OTF( frequency_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);

if (velocity_array == NULL || frequency_array == NULL ) {
Py_XDECREF(velocity_array);
Py_XDECREF(frequency_array);
return NULL;
}

_Dcomplex *Velocity = (_Dcomplex *)PyArray_DATA(velocity_array);
double *Frequency = (double*)PyArray_DATA(frequency_array);
int NumberOfData = (int)PyArray_DIM(velocity_array, 0);
int NumberOfFrequencies = (int)PyArray_DIM(frequency_array, 0);
_Dcomplex *Velocity = (_Dcomplex *)PyArray_DATA((PyArrayObject *)velocity_array);
double *Frequency = (double*)PyArray_DATA((PyArrayObject *)frequency_array);
int NumberOfData = (int)PyArray_DIM((PyArrayObject *)velocity_array, 0);
int NumberOfFrequencies = (int)PyArray_DIM((PyArrayObject *)frequency_array, 0);


//Create new numpy array for storing result
Expand Down

0 comments on commit 35ad696

Please sign in to comment.