This file is indexed.

/usr/share/pyshared/MMTK/Subspace.py is in python-mmtk 2.7.9-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# This module implements subspaces for motion analysis etc.
#
# Written by Konrad Hinsen
#

"""
Linear subspaces for infinitesimal motions

This module implements subspaces for infinitesimal (or finite
small-amplitude) atomic motions. They can be used in normal mode
calculations or for analyzing complex motions. For an explanation, see:

  |  K. Hinsen and G.R. Kneller
  |  Projection methods for the analysis of complex motions in macromolecules
  |  Mol. Sim. 23:275-292 (2000)
"""

__docformat__ = 'restructuredtext'

from MMTK import Utility, ParticleProperties
from Scientific.Geometry import Vector, ex, ey, ez
from Scientific import N
import copy

#
# Import LAPACK routines
#
try:
    array_package = N.package
except AttributeError:
    array_package = "Numeric"

dgesdd = None
try:
    if array_package == "Numeric":
        from lapack_lite import dgesdd, LapackError
    else:
        from numpy.linalg.lapack_lite import dgesdd, LapackError
except ImportError: pass
if dgesdd is None:
    try:
        # PyLAPACK
        from lapack_dge import dgesdd
    except ImportError: pass
if dgesdd:
    n = 1
    array = N.zeros((n, n), N.Float)
    sv = N.zeros((n,), N.Float)
    u = N.zeros((n, n), N.Float)
    vt = N.zeros((n, n), N.Float)
    work = N.zeros((1,), N.Float)
    int_types = [N.Int, N.Int8, N.Int16, N.Int32]
    try:
        int_types.append(N.Int64)
        int_types.append(N.Int128)
    except AttributeError:
        pass
    for int_type in int_types:
        iwork = N.zeros((1,), int_type)
        try:
            dgesdd('S', n, n, array, n, sv, u, n, vt, n, work, -1, iwork, 0)
            break
        except LapackError:
            pass
    del n, array, sv, u, vt, work, iwork, int_types

del array_package

#
# A set of particle vectors that define a subspace
#
class ParticleVectorSet(object):

    def __init__(self, universe, data):
        self.universe = universe
        if type(data) == N.arraytype:
            self.n = data.shape[0]
            self.array = data
        else:
            self.n = data
            self.array = N.zeros((self.n, universe.numberOfAtoms(), 3),
                                 N.Float)

    def __len__(self):
        return self.n

    def __getitem__(self, i):
        if i >= self.n:
            raise IndexError
        return ParticleProperties.ParticleVector(self.universe, self.array[i])


class Subspace(object):

    """
    Subspace of infinitesimal atomic motions
    """

    def __init__(self, universe, vectors):
        """
        :param universe: the universe for which the subspace is created
        :type universe: :class:`~MMTK.Universe.Universe`
        :param vectors: a list of :class:`~MMTK.ParticleProperties.ParticleVector`
                        objects that define the subspace. They need not be
                        orthogonal or linearly independent.
        :type vectors: list
        """
        self.universe = universe
        self.vectors = vectors
        self._basis = None

    def __add__(self, other):
        return Subspace(self.vectors+other.vectors)

    def __len__(self):
        return len(self.vectors)

    def __getitem__(self, item):
        return self.vectors[item]

    def getBasis(self):
        """
        Construct a basis for the subspace by orthonormalization of
        the input vectors using Singular Value Decomposition. The
        basis consists of a sequence of
        :class:`~MMTK.ParticleProperties.ParticleVector`
        objects that are orthonormal in configuration space.
        :returns: the basis
        """
        if self._basis is None:
            basis = N.array([v.array for v in self.vectors], N.Float)
            shape = basis.shape
            nvectors = shape[0]
            natoms = shape[1]
            basis.shape = (nvectors, 3*natoms)
            sv = N.zeros((min(nvectors, 3*natoms),), N.Float)
            min_n_m = min(3*natoms, nvectors)
            vt = N.zeros((nvectors, min_n_m), N.Float)
            work = N.zeros((1,), N.Float)
            iwork = N.zeros((8*min_n_m,), int_type)
            if 3*natoms >= nvectors:
                result = dgesdd('O', 3*natoms, nvectors, basis, 3*natoms,
                                sv, basis, 3*natoms, vt, min_n_m,
                                work, -1, iwork, 0)
                work = N.zeros((int(work[0]),), N.Float)
                result = dgesdd('O', 3*natoms, nvectors, basis, 3*natoms,
                                sv, basis, 3*natoms, vt, min_n_m,
                                work, work.shape[0], iwork, 0)
                u = basis
            else:
                u = N.zeros((min_n_m, 3*natoms), N.Float)
                result = dgesdd('S', 3*natoms, nvectors, basis, 3*natoms,
                                sv, u, 3*natoms, vt, min_n_m,
                                work, -1, iwork, 0)
                work = N.zeros((int(work[0]),), N.Float)
                result = dgesdd('S', 3*natoms, nvectors, basis, 3*natoms,
                                sv, u, 3*natoms, vt, min_n_m,
                                work, work.shape[0], iwork, 0)
            if result['info'] != 0:
                raise ValueError('Lapack SVD: ' + `result['info']`)
            svmax = N.maximum.reduce(sv)
            nvectors = N.add.reduce(N.greater(sv, 1.e-10*svmax))
            u = u[:nvectors]
            u.shape = (nvectors, natoms, 3)
            self._basis = ParticleVectorSet(self.universe, u)
        return self._basis

    def projectionOf(self, vector):
        """
        :param vector: a particle vector
        :type vector: :class:`~MMTK.ParticleProperties.ParticleVector`
        :returns: the projection of the vector onto the subspace.
        """
        vector = vector.array
        basis = self.getBasis().array
        p = N.zeros(vector.shape, N.Float)
        for bv in basis:
            N.add(N.add.reduce(N.ravel(bv*vector))*bv, p, p)
        return ParticleProperties.ParticleVector(self.universe, p)

    def projectionComplementOf(self, vector):
        """
        :param vector: a particle vector
        :type vector: :class:`~MMTK.ParticleProperties.ParticleVector`
        :returns: the projection of the vector onto the orthogonal complement
                  of the subspace.
        """
        return vector - self.projectionOf(vector)

    def complement(self):
        """
        :returns: the orthogonal complement subspace
        :rtype: :class:`~MMTK.Subspace.Subspace`
        """
        basis = []
        for i in range(self.universe.numberOfAtoms()):
            for e in [ex, ey, ez]:
                p = ParticleProperties.ParticleVector(self.universe)
                p[i] = e
                basis.append(self.projectionComplementOf(p))
        return Subspace(self.universe, basis)


class LinkedRigidBodyMotionSubspace(Subspace):
    
    """
    Subspace for the motion of linked rigid bodies

    This class describes the same subspace as RigidBodyMotionSubspace,
    and is used by the latter. For collections of several chains of
    linked rigid bodies, RigidBodyMotionSubspace is more efficient.
    """
    def __init__(self, universe, rigid_bodies):
        """
        :param universe: the universe for which the subspace is created
        :type universe: :class:`~MMTK.Universe.Universe`
        :param rigid_bodies: a list or set of rigid bodies
                             with some common atoms
        """
        ex_ey_ez = [Vector(1.,0.,0.), Vector(0.,1.,0.), Vector(0.,0.,1.)]
        # Constructs
        # 1) a list of vectors describing the rigid-body motions of each
        #    rigid body as if it were independent.
        # 2) a list of pair-distance constraint vectors for all pairs of
        #    atoms inside a rigid body.
        # The LRB subspace is constructed from the projections of the
        # first set of vectors onto the orthogonal complement of the
        # subspace generated by the second set of vectors.
        vectors = []
        c_vectors = []
        for rb in rigid_bodies:
            atoms = rb.atomList()
            for d in ex_ey_ez:
                v = ParticleProperties.ParticleVector(universe)
                for a in atoms:
                    v[a] = d
                vectors.append(v)
            if len(atoms) > 1:
                center = rb.centerOfMass()
                iv = len(vectors)-3
                for d in ex_ey_ez:
                    v = ParticleProperties.ParticleVector(universe)
                    for a in atoms:
                        v[a] = d.cross(a.position()-center)
                    for vt in vectors[iv:]:
                        v -= v.dotProduct(vt)*vt
                    if v.dotProduct(v) > 0.:
                        vectors.append(v)
            for a1, a2 in Utility.pairs(atoms):
                distance = universe.distanceVector(a1.position(),
                                                   a2.position())
                v = ParticleProperties.ParticleVector(universe)
                v[a1] = distance
                v[a2] = -distance
                c_vectors.append(v)
        if c_vectors:
            constraints = Subspace(universe, c_vectors)
            vectors = [constraints.projectionComplementOf(v)
                       for v in vectors]
        Subspace.__init__(self, universe, vectors)


class RigidMotionSubspace(Subspace):

    """
    Subspace of rigid-body motions

    A rigid-body motion subspace is the subspace which contains
    the rigid-body motions of any number of chemical objects.
    """

    def __init__(self, universe, objects):
        """
        :param universe: the universe for which the subspace is created
        :type universe: :class:`~MMTK.Universe.Universe`
        :param objects: a sequence of objects whose rigid-body motion is
                        included in the subspace
        """
        if not Utility.isSequenceObject(objects):
            objects = [objects]
        else:
            objects = copy.copy(objects)
        # Identify connected sets of linked rigid bodies and remove
        # them from the plain rigid body list.
        atom_map = {}
        for o in objects:
            for a in o.atomIterator():
                am = atom_map.get(a, [])
                am.append(o)
                atom_map[a] = am
        rb_map = {}
        for rbs in atom_map.values():
            if len(rbs) > 1:
                for rb in rbs:
                    rb_map[rb] = rb_map.get(rb, frozenset()) \
                                 .union(frozenset(rbs))
        for rb in rb_map.keys():
            objects.remove(rb)
        while True:
            changed = False
            for rbs in rb_map.values():
                for rb in rbs:
                    s = rb_map[rb]
                    rb_map[rb] = s.union(rbs)
                    if s != rb_map[rb]:
                        changed = True
            if not changed:
                break
        lrbs = frozenset(rb_map.values())

        # Generate the subspace vectors for the isolated rigid bodies.
        ex_ey_ez = [Vector(1.,0.,0.), Vector(0.,1.,0.), Vector(0.,0.,1.)]
        vectors = []
        for o in objects:
            rb_atoms = o.atomList()
            for d in ex_ey_ez:
                v = ParticleProperties.ParticleVector(universe)
                for a in rb_atoms:
                    v[a] = d
                vectors.append(v/N.sqrt(len(rb_atoms)))
            if len(rb_atoms) > 1:
                center = o.centerOfMass()
                iv = len(vectors)-3
                for d in ex_ey_ez:
                    v = ParticleProperties.ParticleVector(universe)
                    for a in rb_atoms:
                        v[a] = d.cross(a.position()-center)
                    for vt in vectors[iv:]:
                        v -= v.dotProduct(vt)*vt
                    norm_sq = N.sqrt(v.dotProduct(v))
                    if norm_sq > 0.:
                        vectors.append(v/norm_sq)

        # Generate the subspace vectors for the linked rigid bodies.
        for lrb in lrbs:
            lrb_ss = LinkedRigidBodyMotionSubspace(universe, lrb)
            for v in lrb_ss.getBasis():
                vectors.append(v)

        Subspace.__init__(self, universe, vectors)
        # The vector set is already orthonormal by construction,
        # so we can skip the lengthy SVD procedure.
        self._basis = ParticleVectorSet(universe, len(vectors))
        for i in range(len(vectors)):
            self._basis.array[i] = vectors[i].array


class PairDistanceSubspace(Subspace):

    """
    Subspace of pair-distance motions

    A pair-distance motion subspace is the subspace which contains
    the relative motions of any number of atom pairs along
    their distance vector, e.g. bond elongation between two
    bonded atoms.
    """

    def __init__(self, universe, atom_pairs):
        """
        :param universe: the universe for which the subspace is created
        :type universe: :class:`~MMTK.Universe.Universe`
        :param atom_pairs: a sequence of atom pairs whose distance-vector
                           motion is included in the subspace
        """
        vectors = []
        for atom1, atom2 in atom_pairs:
            v = ParticleProperties.ParticleVector(universe)
            distance = atom1.position()-atom2.position()
            v[atom1] = distance
            v[atom2] = -distance
            vectors.append(v)
        Subspace.__init__(self, universe, vectors)