[gmx-developers] Re: readxtc() -> SWIG -> Python

Kaihsu Tai kaihsu at biop.ox.ac.uk
Wed Oct 22 16:26:39 CEST 2003


All right, attached are 3 files:
  xdrfile.i     SWIG 1.3 interface file for wrapping xtcfile.c and xtcfile.h
  Makefile      for make; the paths will have to be modified
  read.py       example of how to use the wrapped

A bit of a hack, but it works for now.  I am sending it to
the Gromacs developers list so nobody else has to do it from
scratch again.  Cheers.

David, 2003-10-21 20:56:23+0200:
> On Tue, 2003-10-21 at 18:57, Kaihsu Tai wrote:
> > (Sorry for the monologue.)  It appears that xtcio.c depends
> > on a lot of other things; I suppose the putative readxtc.c
> > does not?  Thanks
> 
> yes, you will have to link with gromacs for the time being. You only
> need to wrap a few functions though.
> 
> read_xtc
> write_xtc
> open_xtc
> close_xtc
> 
> Erik and me actually planned to separate the I/O routines from the rest
> of the code, however there would still be considerable overhead from
> lots of small functions, so it's probably just as easy to link to
> gromacs.
-- 
United Nations Day, 24 October (this Friday). http://www.un.org/events/unday/
-------------- next part --------------
%module xdrfile
%import typemaps.i
%import carrays.i
%{
#include "xdrfile.h"
%}

%array_functions(int, intArray)
%array_functions(float, floatArray)

extern XDRFILE* xdrfile_open(const char *path, const char *mode);
extern int xdrfile_close(XDRFILE *fp);
extern int xdrfile_read_int(int *ptr, int ndata, XDRFILE* xfp);
extern int xdrfile_read_float(float *ptr, int ndata, XDRFILE* xfp);
extern int 
xdrfile_read_compr_coord_float(float    *ptr,
			       int      *INPUT,
			       float    *precision,
			       XDRFILE*  xfp);

/* ******** functions not wrapped as yet but probably should be ********
extern int xdrfile_read_u_int(unsigned int *ptr, int ndata, XDRFILE* xfp);
extern int xdrfile_read_char(char *ptr, int ndata, XDRFILE* xfp);
extern int xdrfile_read_u_char(unsigned char *ptr, int ndata, XDRFILE* xfp);
extern int xdrfile_read_short(short *ptr, int ndata, XDRFILE* xfp);
extern int xdrfile_read_u_short(unsigned short *ptr, int ndata, XDRFILE* xfp);
extern int xdrfile_read_double(double *ptr, int ndata, XDRFILE* xfp);
extern int xdrfile_read_string(char *ptr, int maxlen, XDRFILE* xfp);
extern int xdrfile_read_compr_coord_double(double   *ptr,
				int      *ncoord,
				double   *precision,
				XDRFILE*  xfp);
*/
-------------- next part --------------
_xdrfile.so: xdrfile_wrap.c
	gcc -c xdrfile.c xdrfile_wrap.c -I/usr/include/python2.2/
	ld -shared xdrfile.o xdrfile_wrap.o -o _xdrfile.so

xdrfile_wrap.c: \
 /usr/local/lib/swig1.3/swig.swg \
 /usr/local/lib/swig1.3/python/python.swg \
 xdrfile.i \
 /usr/local/lib/swig1.3/python/typemaps.i \
 /usr/local/lib/swig1.3/python/fragments.i \
 /usr/local/lib/swig1.3/carrays.i
	swig -python xdrfile.i

clean:
	rm -rf xdrfile_wrap.c _xdrfile.so *.pyc *.o xdrfile.py
-------------- next part --------------
from xdrfile import *

hi = xdrfile_open('hi.xtc','r')

ptr = new_intArray(3)
print xdrfile_read_int(ptr, 3, hi)
print intArray_getitem(ptr, 0)
print "nAtoms = " + str(intArray_getitem(ptr, 1))
natoms = intArray_getitem(ptr, 1)
print intArray_getitem(ptr, 2)
delete_intArray(ptr)

ptr = new_floatArray(10)
print xdrfile_read_float(ptr, 10, hi)
for i in range(0, 10):
  print floatArray_getitem(ptr, i)
delete_floatArray(ptr)

ptr = new_floatArray(natoms*3)
prec = new_floatArray(1)
print xdrfile_read_compr_coord_float(ptr, natoms, prec, hi)
for i in range(0, natoms):
  print str(i) + ": (" + \
    str(floatArray_getitem(ptr, 3 * i + 0)) + ", " + \
    str(floatArray_getitem(ptr, 3 * i + 1)) + ", " + \
    str(floatArray_getitem(ptr, 3 * i + 2)) + ")"
delete_floatArray(ptr)


More information about the gromacs.org_gmx-developers mailing list