1

I'm analyzing molecular dynamics simulation data, and I'd like to compute neutron scattering spectra from atom trajectories to compare with my experiment.

I wrote initially something in pure Python but it's quite long so I wanted to code this part in C and call the C function from Python.

For this, I need an array of atom coordinates for each time frame, an array of reciprocal space q vectors to average, and the periodic cell dimensions. Everything is put in a complex exponential and averaged over q vectors, time origins and atoms.

Here comes the problem, how to use an array of complex numbers when calling a C function from Python? I always get these syntax errors with nmake in PowerShell:

package/lib/src\compIntScatFunc.h(8): error C2143: erreur de syntaxe : absence de ')' avant '*'
package/lib/src\compIntScatFunc.h(8): error C2143: erreur de syntaxe : absence de '{' avant '*'
package/lib/src\compIntScatFunc.h(8): error C2059: erreur de syntaxe : 'type'
package/lib/src\compIntScatFunc.h(9): error C2059: erreur de syntaxe : ')'
package/lib/pycompIntScatFunc.c(1107): error C2061: erreur de syntaxe : identificateur '__pyx_t_double_complex'
package/lib/pycompIntScatFunc.c(1107): error C2059: erreur de syntaxe : ';'
package/lib/pycompIntScatFunc.c(1112): error C2061: erreur de syntaxe : identificateur '__pyx_t_double_complex_from_parts'
package/lib/pycompIntScatFunc.c(1112): error C2059: erreur de syntaxe : ';'
package/lib/pycompIntScatFunc.c(1112): error C2059: erreur de syntaxe : '<parameter-list>'
package/lib/pycompIntScatFunc.c(1119): error C2061: erreur de syntaxe : identificateur '__pyx_t_float_complex'
package/lib/pycompIntScatFunc.c(1119): error C2059: erreur de syntaxe : ';'
package/lib/pycompIntScatFunc.c(1124): error C2061: erreur de syntaxe : identificateur '__pyx_t_float_complex_from_parts'
package/lib/pycompIntScatFunc.c(1124): error C2059: erreur de syntaxe : ';'
package/lib/pycompIntScatFunc.c(1124): error C2059: erreur de syntaxe : '<parameter-list>'
package/lib/pycompIntScatFunc.c(1644): error C2065: '__pyx_t_double_complex' : identificateur non déclaré
package/lib/pycompIntScatFunc.c(1921): warning C4013: 'compIntScatFunc' non défini(e) ; extern retournant int pris par défaut
package/lib/pycompIntScatFunc.c(1921): error C2065: '__pyx_t_double_complex' : identificateur non déclaré
package/lib/pycompIntScatFunc.c(1921): error C2059: erreur de syntaxe : ')'
package/lib/pycompIntScatFunc.c(6580): error C2061: erreur de syntaxe : identificateur '__pyx_t_double_complex_from_parts'
package/lib/pycompIntScatFunc.c(6580): error C2059: erreur de syntaxe : ';'
package/lib/pycompIntScatFunc.c(6580): error C2059: erreur de syntaxe : '<parameter-list>'
package/lib/pycompIntScatFunc.c(6779): error C2061: erreur de syntaxe : identificateur '__pyx_t_float_complex_from_parts'
package/lib/pycompIntScatFunc.c(6779): error C2059: erreur de syntaxe : ';'
package/lib/pycompIntScatFunc.c(6779): error C2059: erreur de syntaxe : '<parameter-list>'

Here are my files (the .c and .h files compiles just fine with gcc):

compIntScatFunc.h

#ifndef COMPINTSCATFUNC_H
#define COMPINTSCATFUNC_H

#include <complex.h>

void compIntScatFunc(double *atomPos, int atomPos_dim0, int atomPos_dim1, int atomPos_dim2, 
                     double *qVecs, int qVecs_dim0, int qVecs_dim1, int qVecs_dim2, 
                     double complex *out, int out_dim0, int out_dim1, 
                     double *cellDims, int nbrBins, int minFrames, int maxFrames);

#endif

pycompIntScatFunc.pyx

cimport numpy as np
cimport cython

np.import_array()


cdef extern from "compIntScatFunc.h":
    void compIntScatFunc(double *atomPos, int atomPos_dim0, int atomPos_dim1, int atomPos_dim2, 
                         double *qVecs, int qVecs_dim0, int qVecs_dim1, int qVecs_dim2, 
                         double complex *out, int out_dim0, int out_dim1, 
                         double *cellDims, int nbrBins, int minFrames, int maxFrames)



@cython.boundscheck(False)
@cython.wraparound(False)
def py_compIntScatFunc( np.ndarray[double, ndim=3, mode="c"] atomPos not None,
                        np.ndarray[double, ndim=3, mode="c"] qVecs not None,
                        np.ndarray[np.complex128_t, ndim=2, mode="c"] out not None,
                        np.ndarray[double, ndim=1, mode="c"] cellDims not None,
                        nbrBins, minFrames, maxFrames ):
    compIntScatFunc(<double*> np.PyArray_DATA(atomPos), atomPos.shape[0], atomPos.shape[1], atomPos.shape[2],
                    <double*> np.PyArray_DATA(qVecs), qVecs.shape[0], qVecs.shape[1], qVecs.shape[2],
                    <double complex*> np.PyArray_DATA(out), out.shape[0], out.shape[1],
                    <double*> np.PyArray_DATA(cellDims), nbrBins, minFrames, maxFrames )

setup.py

from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy as np


with open('README.md', 'r') as f:
    description = f.read()


packagesList = [    'package',
                    'package.dataManipulation',
                    'package.dataParsers',
                    'package.dataConverters',
                    'package.helpersFunctions',
                    'package.test'  ]


pycompIntScatFunc_ext = Extension( "pycompIntScatFunc", 
                                    ["package/lib/pycompIntScatFunc.pyx", "package/lib/src/compIntScatFunc.c"],
                                    include_dirs=["package/lib/src", np.get_include()],
                                    libraries=["compIntScatFunc"],
                                    library_dirs=["pacakge/lib/"] )



setup(  name='NAMDAnalyzer',
        version='alpha',
        description=description,
        author='Kevin Pounot',
        author_email='kpounot@hotmail.fr',
        url='github.com/kpounot/NAMDAnalyzer',
        packages=packagesList,
        ext_modules=cythonize( [pycompIntScatFunc_ext] )  )
  • 1
    Which compiler is used when you compile your extension? MSVC? – ead Feb 13 '19 at 18:25
  • Yes indeed, cl is called for compilation. And I get this error with gcc: "Unkown MS compiler version 1900" (I see there are some patches for that). Is there a way to make MSVC work? Do I need to install python using the visual Studio tools? – Kévin POUNOT Feb 14 '19 at 07:42

1 Answers1

1

Ok,actually, adding the following:

[build]
compiler=gcc

And using the patch method described here: ValueError: Unknown MS Compiler version 1900 solved the problem.

Now it's compiled, thanks ead.