Пример #1
0
    def removeObject(self, object):
        """
        Remove an object or a list or collection of objects from the
        collection. The object(s) to be removed must be elements of the
        collection.

        :param object: the object to be removed, or a list or collection
                       of objects whose elements are to be removed
        :raises ValueError: if the object is not an element of the collection
        """
        from MMTK.ChemicalObjects import isChemicalObject
        if isChemicalObject(object):
            self.removeChemicalObject(object)
        elif isCollection(object) or Utility.isSequenceObject(object):
            for o in object:
                self.removeObject(o)
        else:
            raise ValueError('Object not in this collection')
Пример #2
0
    def removeObject(self, object):
        """
        Remove an object or a list or collection of objects from the
        collection. The object(s) to be removed must be elements of the
        collection.

        :param object: the object to be removed, or a list or collection
                       of objects whose elements are to be removed
        :raises ValueError: if the object is not an element of the collection
        """
        from MMTK.ChemicalObjects import isChemicalObject
        if isChemicalObject(object):
            self.removeChemicalObject(object)
        elif isCollection(object) or Utility.isSequenceObject(object):
            for o in object:
                self.removeObject(o)
        else:
            raise ValueError('Object not in this collection')
Пример #3
0
    def addObject(self, object):
        """
        Add objects to the collection.

        :param object: the object(s) to be added. If it is another collection
                       or a list, all of its elements are added
        """
        from MMTK.ChemicalObjects import isChemicalObject
        if isChemicalObject(object):
            self.addChemicalObject(object)
        elif isCollection(object):
            self.addChemicalObjectList(object.objectList())
        elif Utility.isSequenceObject(object):
            if object and isChemicalObject(object[0]):
                self.addChemicalObjectList(list(object))
            else:
                for o in object:
                    self.addObject(o)
        else:
            raise TypeError('Wrong object type in collection')
Пример #4
0
    def addObject(self, object):
        """
        Add objects to the collection.

        :param object: the object(s) to be added. If it is another collection
                       or a list, all of its elements are added
        """
        from MMTK.ChemicalObjects import isChemicalObject
        if isChemicalObject(object):
            self.addChemicalObject(object)
        elif isCollection(object):
            self.addChemicalObjectList(object.objectList())
        elif Utility.isSequenceObject(object):
            if object and isChemicalObject(object[0]):
                self.addChemicalObjectList(list(object))
            else:
                for o in object:
                    self.addObject(o)
        else:
            raise TypeError('Wrong object type in collection')
Пример #5
0
    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
Пример #6
0
    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