summaryrefslogtreecommitdiff
path: root/cad/src/atombase.pyx
blob: 2e4f2b503fef93e3e3e0d497a27466d01b69cc3d (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# Copyright 2006-2007 Nanorex, Inc.  See LICENSE file for details. 
"""
$Id$

make atombase.so ; valgrind python atombasetests.py >& OUCH; less OUCH

"""

__author__ = "Will"

import types
import Numeric
from foundation.inval import InvalMixin

cdef extern from "atombasehelp.c":

    cdef struct key_thing:
        int key
        void *self

    cdef struct pointerlist:
        int size
        int arraysize
        void **lst

    cdef struct xyz:
        double x, y, z

    cdef struct atomstruct:
        int key
        void *self
        int _eltnum, _atomtype
        xyz _posn
        pointerlist *sets

    cdef struct bondstruct:
        int key
        void *self
        int v6
        pointerlist *sets

    cdef struct setstruct:
        int key
        void *self
        pointerlist *members

    pointerlist *new_pointerlist()
    pointerlist *has_key(pointerlist *n, unsigned int key)
    add_to_pointerlist(pointerlist *head, key_thing *other)
    remove_from_pointerlist(pointerlist *head, unsigned int otherkey)
    extract_list(pointerlist *root, int values)
    free_list(pointerlist *root)
    pointerlist_lookup(pointerlist *root, unsigned int key)
    int _init_bitmap_font()

cdef extern from "Numeric/arrayobject.h":
    struct PyArray_Descr:
        int type_um, elsize
        char type
    ctypedef class Numeric.ArrayType [object PyArrayObject]:
        cdef char *data
        cdef int nd
        cdef int *dimensions, *strides
        cdef object base
        cdef PyArray_Descr *descr
        cdef int flags

cdef extern from "string.h":
    int strcmp(char *s1, char *s2)

# this is a handy diagnostic for printing addresses of things
cdef addr(void *p):
    pl = long(<int> p)
    if pl < 0:
        return '%x' % (pl + (long(1) << 32))
    else:
        return '%x' % pl

##############################################################
##############################################################
##############################################################
##############################################################

cdef class _BaseItem:
    # this is polymorphic to _AtomBase and _BondBase
    cdef key_thing data

##############################################################
##############################################################
##############################################################
##############################################################

cdef class _BaseDictClass:

    cdef setstruct data

    # Define __setitem__ and __delitem__ for the kind of item in the set

    def __init__(self):
        self.data.key = 0
        self.data.self = <void*> self
        self.data.members = new_pointerlist()
    def __setattr__(self, name, value):
        if name == "key":
            self.data.key = value
        else:
            self.__dict__[name] = value
    def __getattr__(self, char *name):
        if strcmp(name, "key") == 0:
            return self.data.key
        else:
            raise AttributeError, name
    def __getitem__(self, key):
        return pointerlist_lookup(self.data.members, key)
    def __len__(self):
        return len(self.values())
    def __contains__(self, unsigned int key):
        try:
            pointerlist_lookup(self.data.members, key)
            return 1
        except KeyError:
            return 0
    def has_key(self, unsigned int key):
        return self.__contains__(key)
    def clear(self):
        for k in self.keys():
            self.remove(self[k])
    def keys(self):
        return extract_list(self.data.members, 0)
    def add(self, guy):
        assert guy.key != 0
        self[guy.key] = guy
    def remove(self, guy):
        del self[guy.key]
    def update(self, other):
        for k in other.keys():
            self[k] = other[k]
    def values(self):
        return extract_list(self.data.members, 1)
    def items(self):
        lst = [ ]
        for k in self.keys():
            lst.append((k, self[k]))
        return lst

##############################################################
##############################################################
##############################################################
##############################################################

cdef class _AtomBase:

    cdef atomstruct data

    def __init__(self):
        self.data.key = 0
        self.data.self = <void*> self
        self.data._eltnum = 0
        self.data._posn.x = 0.0
        self.data._posn.y = 0.0
        self.data._posn.z = 0.0
        self.data._atomtype = 0
        self.data.sets = new_pointerlist()

    def diffableAttributes(self):
        return ("_eltnum", "_atomtype",
                "_posn", "sets")

    def __getattr__(self, char *name):
        if strcmp(name, "key") == 0:
            return self.data.key
        elif strcmp(name, "_eltnum") == 0:
            return self.data._eltnum
        elif strcmp(name, "_atomtype") == 0:
            return self.data._atomtype
        elif strcmp(name, "_posn") == 0:
            return Numeric.array((self.data._posn.x,
                                  self.data._posn.y,
                                  self.data._posn.z))
        elif strcmp(name, "x") == 0:
            return self.data._posn.x
        elif strcmp(name, "y") == 0:
            return self.data._posn.y
        elif strcmp(name, "z") == 0:
            return self.data._posn.z
        elif strcmp(name, "sets") == 0:
            return extract_list(self.data.sets, 0)
        else:
            raise AttributeError, name

    def _setposn(self, ArrayType ary):
        if chr(ary.descr.type) != "d" or ary.nd != 1 or ary.dimensions[0] != 3:
            raise TypeError("Array of three doubles required")
        self.data._posn.x = (<double *> ary.data)[0]
        self.data._posn.y = (<double *> ary.data)[1]
        self.data._posn.z = (<double *> ary.data)[2]

    def __setattr__(self, char *name, value):
        if strcmp(name, "key") == 0:
            self.data.key = value
        elif strcmp(name, "_eltnum") == 0:
            self.data._eltnum = value
        elif strcmp(name, "_atomtype") == 0:
            self.data._atomtype = value
        elif strcmp(name, "_posn") == 0:
            self._setposn(value)
        else:
            self.__dict__[name] = value

    def addDict(self, _BaseDictClass other):
        add_to_pointerlist(self.data.sets, <key_thing*> &other.data)

    def removeDict(self, _BaseDictClass other):
        remove_from_pointerlist(self.data.sets, other.data.key)

class AtomBase(_AtomBase):
    pass

##############################################################
##############################################################
##############################################################
##############################################################

cdef class _AtomDictBase(_BaseDictClass):

    def __setitem__(self, unsigned int key, atom):
        cdef atomstruct *adata
        adata = &(<_AtomBase> atom).data
        # Warn if key mismatches
        if key != adata.key:
            raise KeyError, (key, adata.key)
        add_to_pointerlist(self.data.members, <key_thing*> adata)
        assert (<long>adata.sets) != 0
        add_to_pointerlist(adata.sets, <key_thing *>&self.data)
    def __delitem__(self, unsigned int key):
        cdef atomstruct *adata
        x = self[key]
        adata = &(<_AtomBase> x).data
        assert adata.key == key  # remove this eventually
        remove_from_pointerlist(self.data.members, key)
        remove_from_pointerlist(adata.sets, self.data.key)
    def atomInfo(self):
        ar = Numeric.zeros((len(self), 5), 'd')
        i = 0
        for k in self.keys():
            atm = self[k]
            ar[i][0] = atm._eltnum
            ar[i][1] = atm._atomtype
            ar[i][2] = atm.x
            ar[i][3] = atm.y
            ar[i][4] = atm.z
            i = i + 1
        return ar

class AtomDictBase(_AtomDictBase):
    pass

##############################################################
##############################################################
##############################################################
##############################################################

cdef class _BondBase:

    cdef bondstruct data

    def __init__(self):
        self.data.key = 0
        self.data.self = <void*> self
        self.data.v6 = 0
        self.data.sets = new_pointerlist()

    def diffableAttributes(self):
        return ("v6",)

    def __getattr__(self, char *name):
        # ord('_') => 95. If the first char of the attribute name is
        # an underscore, give up immediately.
        if name[0] == 95:
            raise AttributeError, name
        if strcmp(name, "bond_key") == 0:
            return self.data.key
        elif strcmp(name, "v6") == 0:
            return self.data.v6
        elif strcmp(name, "sets") == 0:
            return extract_list(self.data.sets, 0)
        else:
            raise AttributeError, name

    def __setattr__(self, name, value):
        if name == "bond_key":
            self.data.key = value
        elif name == "v6":
            self.data.v6 = value
        else:
            self.__dict__[name] = value

    def addDict(self, _BaseDictClass other):
        add_to_pointerlist(self.data.sets, <key_thing*> &other.data)

    def removeDict(self, _BaseDictClass other):
        remove_from_pointerlist(self.data.sets, other.data.key)

class BondBase(_BondBase):
    pass

##############################################################
##############################################################
##############################################################
##############################################################

cdef class _BondDictBase(_BaseDictClass):

    def __setitem__(self, key, _BondBase bond):
        if key != bond.key:
            raise KeyError
        add_to_pointerlist(self.data.members, <key_thing *>&bond.data)
        add_to_pointerlist(bond.data.sets, <key_thing *>&self.data)
    def __delitem__(self, key):
        cdef bondstruct adata
        x = self[key]
        adata = (<_BondBase> x).data
        remove_from_pointerlist(self.data.members, adata.key)
        remove_from_pointerlist(adata.sets, self.data.key)
    def __contains__(self, unsigned int key):
        try:
            pointerlist_lookup(self.data.members, key)
            return 1
        except KeyError:
            return 0
    def bondInfo(self):
        ary = Numeric.zeros((len(self.keys()), 3), 'i')
        i = 0
        for k in self.keys():
            bnd = self[k]
            ary[i][0] = bnd.atomkey1
            ary[i][1] = bnd.atomkey2
            ary[i][2] = bnd.v6
            i = i + 1
        return ary

class BondDictBase(_BondDictBase):
    pass

##############################################################
##############################################################
##############################################################
##############################################################

cdef class _DiffObjectBase:
    def __init__(self, attributes, objlist, previous, current):
        for attr in attributes:
            keylist = [ ]
            oldlist = [ ]
            newlist = [ ]
            for x in objlist:
                if attr in x.diffableAttributes():
                    value = current[(attr, x.key)]
                    oldvalue = previous[(attr, x.key)]
                    if type(value) == Numeric.arraytype: veq = value.tolist()
                    else: veq = value
                    if type(oldvalue) == Numeric.arraytype: oveq = oldvalue.tolist()
                    else: oveq = oldvalue
                    if veq != oveq:
                        keylist.append(x.key)
                        oldlist.append(oldvalue)
                        newlist.append(value)
            if len(newlist) > 0 and \
               type(newlist[0]) in (types.IntType, types.FloatType):
                # we can pack these guys efficiently
                setattr(self, attr, (Numeric.array(keylist),
                                     Numeric.array(oldlist),
                                     Numeric.array(newlist)))
            else:
                # these guys can't be packed efficiently
                setattr(self, attr, (Numeric.array(keylist),
                                     oldlist,
                                     newlist))

class DiffObjectBase(_DiffObjectBase):
    pass

##############################################################
##############################################################
##############################################################
##############################################################

cdef class _DiffFactoryBase:
    def __init__(self, objList):
        self.attrs = [ ]
        for x in objList:
            for attr in x.diffableAttributes():
                if attr not in self.attrs:
                    self.attrs.append(attr)
        self.lst = objList
        self.current = None
        self.snapshot()
    def _getCurrentAttributes(self, x):
        for attr in x.diffableAttributes():
            self.current[(attr, x.key)] = getattr(x, attr)
    def addObject(self, x):
        self.lst.append(x)
        self._getCurrentAttributes(x)
    def snapshot(self):
        previous = self.current
        self.current = { }
        for x in self.lst:
            self._getCurrentAttributes(x)
        if previous != None:
            return DiffObjectBase(self.attrs, self.lst,
                                  previous, self.current)

class DiffFactoryBase(_DiffFactoryBase):
    pass

##############################################################
##############################################################
##############################################################
##############################################################

def init_bitmap_font():
    return _init_bitmap_font()