pymolfile.h 7.68 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
25
26
27
 */

/*
 * 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

#include "molfile_plugin.h"
#include "libmolfile_plugin.h"
#include "vmdplugin.h"
28
29
30
31
#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
32

33

Berk Onat's avatar
Berk Onat committed
34
35
36
37
#ifndef MAXPLUGINS
#define MAXPLUGINS 200
#endif

38
39
40
41
42
43
44
45
struct MolObject {
    PyObject_HEAD
    PyObject * plugin;
    PyObject * file_handle;
    int natoms;
    MolObject(void) {}
};
/*
46
47
48
49
50
51
52
struct MolObject {
    PyObject_HEAD
    molfile_plugin_t* plugin;
    void* file_handle;
    int natoms;
    MolObject(void) {}
};
53
*/
54

55
56
57
58
59
60
61
62
63
64
65
66
67

#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

68
69
70
71
72
void del_molfile_plugin_list(PyObject* molcapsule);
void del_molfile_file_handle(PyObject* molcapsule);

static void * PyMolfileCapsule_AsVoidPtr(PyObject *obj);

73
74
static void * PyMolfileCapsule_AsVoidPtr(PyObject *obj)
{
75
    void *ret = PyCapsule_GetPointer(obj, "plugin_handle");
76
77
78
79
80
81
    if (ret == NULL) {
        PyErr_Clear();
    }   
    return ret;
}

82
static PyObject * PyMolfileCapsule_FromVoidPtr(void *ptr, void (*destr)(PyObject *));
83
84
static PyObject * PyMolfileCapsule_FromVoidPtr(void *ptr, void (*destr)(PyObject *))
{
85
    PyObject *ret = PyCapsule_New(ptr, "plugin_handle", destr);
86
87
88
89
90
91
92
93
94
    if (ret == NULL) {
        PyErr_Clear();
    }
    return ret;
}

#else
#define PyBytes_FromString PyString_FromString

95
96
97
98
99
void del_molfile_plugin_list(void* molcapsule);
void del_molfile_file_handle(void* molcapsule);

static void * PyMolfileCapsule_AsVoidPtr(PyObject *obj);

100
101
102
103
104
static void * PyMolfileCapsule_AsVoidPtr(PyObject *obj)
{
    return PyCObject_AsVoidPtr(obj);
}

105
106
static PyObject * PyMolfileCapsule_FromVoidPtr(void *ptr, void (*destr)(void *));

107
108
109
110
111
112
113
static PyObject * PyMolfileCapsule_FromVoidPtr(void *ptr, void (*destr)(void *))
{
    return PyCObject_FromVoidPtr(ptr, destr);
}

#endif

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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)
{
137
138
139
140
141
142
143
144
    //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;
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

    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;
}

170
171
//static molfile_plugin_t* MolObject_plugin(MolObject* self)
static PyObject* MolObject_plugin(MolObject* self)
172
173
174
175
{
        return self->plugin;
}

176
177
//static void* MolObject_file_handle(MolObject* self)
static PyObject* MolObject_file_handle(MolObject* self)
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
{
        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"},
194
    {NULL, 0, 0, 0, NULL}  /* Sentinel */
195
196
197
198
199
200
201
202
203
204
205
206
};

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"
    },
207
    {NULL, 0, 0, NULL}  /* Sentinel */
208
209
210
211
212
213
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
};


#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 */
};

258
PyObject * molfile_plugin_info(PyObject* molcapsule, int plugin_no);
259
PyObject * read_fill_structure(PyObject* molpack, PyObject* prototype);
Berk Onat's avatar
Berk Onat committed
260
261
PyObject * read_fill_bonds(PyObject* molpack);
PyObject * read_fill_angles(PyObject* molpack);
Berk Onat's avatar
Berk Onat committed
262
PyObject * read_fill_next_timestep(PyObject* molpack);
263
264
PyObject * are_plugins_same(PyObject* molpack_a, PyObject* molpack_b);
PyObject * are_filehandles_same(PyObject* molpack_a, PyObject* molpack_b);
265
266
PyObject * get_plugin(PyObject* molcapsule, int plug_no);
PyObject * molfile_plugin_list(int maxsize);
Berk Onat's avatar
Berk Onat committed
267
268
269
270
271
272
273
274
275
276
277

int molfile_init(void);

int molfile_finish(void);

#ifdef __cplusplus
}
#endif

#endif /* _MOLFILE_H_ */