pymolfile.h 7.8 KB
Newer Older
Berk Onat's avatar
Berk Onat committed
1 2 3 4 5
/* Hey emacs this is -*- C -*- and this is my editor vim.
 * 
 * molfile.c : C and Fortran interfaces for molfile_plugins
 * Copyright (c) Berk Onat <b.onat@warwick.ac.uk> 2017
 *
6
 * This program is under UIUC LICENSE
Berk Onat's avatar
Berk Onat committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 */

/*
 * The code is written following the plugin test 
 * context of f77_molfile.c by Axel Kohlmeyer and 
 * in molfile_plugin/src/f77 and catdcd.c by 
 * Justin Gullingsrud of VMD plugins.
 */

#ifndef _MOLFILE_H_
#define _MOLFILE_H_

#ifdef __cplusplus
extern "C"
{

#endif

25 26 27 28 29 30
#if PY_VERSION_HEX >= 0x03000000
#define NUMPY_IMPORT_ARRAY_RETVAL NULL
#else
#define NUMPY_IMPORT_ARRAY_RETVAL
#endif

Berk Onat's avatar
Berk Onat committed
31 32 33
#include "molfile_plugin.h"
#include "libmolfile_plugin.h"
#include "vmdplugin.h"
34 35 36 37
#include "Python.h"
#include "structmember.h"
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>
Berk Onat's avatar
Berk Onat committed
38

39

Berk Onat's avatar
Berk Onat committed
40 41 42 43
#ifndef MAXPLUGINS
#define MAXPLUGINS 200
#endif

44 45 46 47 48 49 50 51
struct MolObject {
    PyObject_HEAD
    PyObject * plugin;
    PyObject * file_handle;
    int natoms;
    MolObject(void) {}
};
/*
52 53 54 55 56 57 58
struct MolObject {
    PyObject_HEAD
    molfile_plugin_t* plugin;
    void* file_handle;
    int natoms;
    MolObject(void) {}
};
59
*/
60

61 62 63 64 65 66 67 68 69 70 71 72 73

#if PY_VERSION_HEX >= 0x03000000
#define PyInt_AsSsize_t PyLong_AsSsize_t
//#define PyInt_AsLong PyLong_AsLong
#define PyArray_Check(op) PyObject_TypeCheck(op, &PyArray_Type)
//#define PyString_FromString PyBytes_FromString
#define PyUString_Check PyUnicode_Check
#define PyUString_GET_SIZE PyUnicode_GET_SIZE
#define PyUString_FromFormat PyUnicode_FromFormat
//#define PyInt_FromLong PyLong_FromLong
#define PyString_Type PyBytes_Type
#define PyInt_Type PyLong_Type

74 75 76 77 78
void del_molfile_plugin_list(PyObject* molcapsule);
void del_molfile_file_handle(PyObject* molcapsule);

static void * PyMolfileCapsule_AsVoidPtr(PyObject *obj);

79 80
static void * PyMolfileCapsule_AsVoidPtr(PyObject *obj)
{
81
    void *ret = PyCapsule_GetPointer(obj, "plugin_handle");
82 83 84 85 86 87
    if (ret == NULL) {
        PyErr_Clear();
    }   
    return ret;
}

88
static PyObject * PyMolfileCapsule_FromVoidPtr(void *ptr, void (*destr)(PyObject *));
89 90
static PyObject * PyMolfileCapsule_FromVoidPtr(void *ptr, void (*destr)(PyObject *))
{
91
    PyObject *ret = PyCapsule_New(ptr, "plugin_handle", destr);
92 93 94 95 96 97 98 99 100
    if (ret == NULL) {
        PyErr_Clear();
    }
    return ret;
}

#else
#define PyBytes_FromString PyString_FromString

101 102 103 104 105
void del_molfile_plugin_list(void* molcapsule);
void del_molfile_file_handle(void* molcapsule);

static void * PyMolfileCapsule_AsVoidPtr(PyObject *obj);

106 107 108 109 110
static void * PyMolfileCapsule_AsVoidPtr(PyObject *obj)
{
    return PyCObject_AsVoidPtr(obj);
}

111 112
static PyObject * PyMolfileCapsule_FromVoidPtr(void *ptr, void (*destr)(void *));

113 114 115 116 117 118 119
static PyObject * PyMolfileCapsule_FromVoidPtr(void *ptr, void (*destr)(void *))
{
    return PyCObject_FromVoidPtr(ptr, destr);
}

#endif

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
static void MolObject_dealloc(MolObject* self)
{
    Py_XDECREF(self->plugin);
    Py_XDECREF(self->file_handle);
    Py_TYPE(self)->tp_free((PyObject*)self);
}

static PyObject * MolObject_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    MolObject *self;

    self = (MolObject *)type->tp_alloc(type, 0);
    if (self != NULL) {
        self->plugin = NULL;
        self->file_handle = NULL;
        self->natoms = 0;
    }

    return (PyObject *)self;
}

static int MolObject_init(MolObject *self, PyObject *args, PyObject *kwds)
{
143 144 145 146 147 148 149 150
    //molfile_plugin_t *plugin = NULL;
    //void *file_handle = NULL;
    //molfile_plugin_t *tmp1 = NULL;
    //void *tmp2 = NULL;
    PyObject *plugin = NULL;
    PyObject *file_handle = NULL;
    PyObject *tmp1 = NULL;
    PyObject *tmp2 = NULL;
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

    static char *kwlist[] = {"plugin", "file_handle", "natoms", NULL};

    if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist,
                                      &plugin, &file_handle,
                                      &self->natoms))
        return -1;

    if (plugin) {
        tmp1 = self->plugin;
        Py_INCREF(plugin);
        self->plugin = plugin;
        Py_XDECREF(tmp1);
    }

    if (file_handle) {
        tmp2 = self->file_handle;
        Py_INCREF(file_handle);
        self->file_handle = file_handle;
        Py_XDECREF(tmp2);
    }

    return 0;
}

176 177
//static molfile_plugin_t* MolObject_plugin(MolObject* self)
static PyObject* MolObject_plugin(MolObject* self)
178 179 180 181
{
        return self->plugin;
}

182 183
//static void* MolObject_file_handle(MolObject* self)
static PyObject* MolObject_file_handle(MolObject* self)
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
{
        return self->file_handle;
}

static PyObject* MolObject_natoms(MolObject* self)
{
        return PyLong_FromLong((long)self->natoms);
}

static PyMemberDef MolObject_members[] = {
    {"plugin", T_OBJECT_EX, offsetof(MolObject, plugin), 0,
     "molfile_plugin_t type plugin"},
    {"file_handle", T_OBJECT_EX, offsetof(MolObject, file_handle), 0,
     "file handle for plugin"},
    {"natoms", T_INT, offsetof(MolObject, natoms), 0,
     "number of atoms"},
200
    {NULL, 0, 0, 0, NULL}  /* Sentinel */
201 202 203 204 205 206 207 208 209 210 211 212
};

static PyMethodDef MolObject_methods[] = {
    {"get_plugin", (PyCFunction)MolObject_plugin, METH_NOARGS,
     "Return the plugin"
    },
    {"get_file_handle", (PyCFunction)MolObject_file_handle, METH_NOARGS,
     "Return the plugin"
    },
    {"get_natoms", (PyCFunction)MolObject_natoms, METH_NOARGS,
     "Return the number of atoms"
    },
213
    {NULL, 0, 0, NULL}  /* Sentinel */
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
};


#ifndef PyVarObject_HEAD_INIT
    #define PyVarObject_HEAD_INIT(type, size) \
        PyObject_HEAD_INIT(type) size,
#endif

static PyTypeObject MolObjectType = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "molobject",                 /*tp_name*/
    sizeof(MolObject),          /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)MolObject_dealloc, /*tp_dealloc*/
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_reserved*/
    0,                         /*tp_repr*/
    0,                         /*tp_as_number*/
    0,                         /*tp_as_sequence*/
    0,                         /*tp_as_mapping*/
    0,                         /*tp_hash */
    0,                         /*tp_call*/
    0,                         /*tp_str*/
    0,                         /*tp_getattro*/
    0,                         /*tp_setattro*/
    0,                         /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT | 
	    Py_TPFLAGS_BASETYPE,   /* tp_flags */
    "molobject objects",           /* tp_doc */
    0,                         /* tp_traverse */
    0,                         /* tp_clear */
    0,                         /* tp_richcompare */
    0,                         /* tp_weaklistoffset */
    0,                         /* tp_iter */
    0,                         /* tp_iternext */
    MolObject_methods,         /* tp_methods */
    MolObject_members,         /* tp_members */
    0,                         /* tp_getset */
    0,                         /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
    (initproc)MolObject_init,      /* tp_init */
    0,                         /* tp_alloc */	
    MolObject_new,                 /* tp_new */
};

264
PyObject * molfile_plugin_info(PyObject* molcapsule, int plugin_no);
265
PyObject * read_fill_structure(PyObject* molpack, PyObject* prototype);
Berk Onat's avatar
Berk Onat committed
266 267
PyObject * read_fill_bonds(PyObject* molpack);
PyObject * read_fill_angles(PyObject* molpack);
Berk Onat's avatar
Berk Onat committed
268
PyObject * read_fill_next_timestep(PyObject* molpack);
269 270
PyObject * are_plugins_same(PyObject* molpack_a, PyObject* molpack_b);
PyObject * are_filehandles_same(PyObject* molpack_a, PyObject* molpack_b);
271 272
PyObject * get_plugin(PyObject* molcapsule, int plug_no);
PyObject * molfile_plugin_list(int maxsize);
Berk Onat's avatar
Berk Onat committed
273 274 275 276 277 278 279 280 281 282 283

int molfile_init(void);

int molfile_finish(void);

#ifdef __cplusplus
}
#endif

#endif /* _MOLFILE_H_ */