pymolfile.h 5.77 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* 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
 *
 * This program is under BSD LICENSE
 */

/*
 * 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"
#include "Python.h"
#include "structmember.h"
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>


enum { pluginOK, pluginNOINIT, pluginCLOSE, pluginNOMEM, 
       pluginENDOFFILE, pluginFILENOTFOUND, pluginFORMATERROR };

#ifndef MAXPLUGINS
#define MAXPLUGINS 200
#endif

struct MolObject {
    PyObject_HEAD
    molfile_plugin_t* plugin;
    void* file_handle;
    int natoms;
    MolObject(void) {}
};

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)
{
    molfile_plugin_t *plugin = NULL;
    void *file_handle = NULL;
    molfile_plugin_t *tmp1 = NULL;
    void *tmp2 = NULL;

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

static molfile_plugin_t* MolObject_plugin(MolObject* self)
{
        return self->plugin;
}

static void* MolObject_file_handle(MolObject* self)
{
        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"},
123
    {NULL, 0, 0, 0, NULL}  /* Sentinel */
124
125
126
127
128
129
130
131
132
133
134
135
};

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"
    },
136
    {NULL, 0, 0, NULL}  /* Sentinel */
137
138
139
140
141
142
143
144
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
};


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

PyObject * read_fill_structure(PyObject* molpack, PyObject* prototype);
188
189
190
191
192
PyObject * read_fill_bonds(PyObject* molpack);
PyObject * read_fill_angles(PyObject* molpack);
PyObject * read_fill_next_timestep(PyObject* molpack);
PyObject * is_plugin_same(PyObject* molpack_a, PyObject* molpack_b);
PyObject * is_filehandle_same(PyObject* molpack_a, PyObject* molpack_b);
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

molfile_plugin_t* get_plugin(molfile_plugin_t** plug_list, int plug_no);

molfile_plugin_t** molfile_plugin_list(int maxsize);

int molfile_init(void);

int molfile_finish(void);

#ifdef __cplusplus
}
#endif

#endif /* _MOLFILE_H_ */