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 _setupBasis(self): if type(self.basis) is type(()): excluded, basis = self.basis else: excluded = [] basis = self.basis nexcluded = len(excluded) nmodes = len(basis) ntotal = nexcluded + nmodes natoms = len(basis[0]) sv = N.zeros((min(ntotal, 3 * natoms), ), N.Float) work_size = N.zeros((1, ), N.Float) if nexcluded > 0: self.basis = N.zeros((ntotal, 3 * natoms), N.Float) for i in range(nexcluded): self.basis[i] = N.ravel(excluded[i].array * self.weights) min_n_m = min(3 * natoms, nexcluded) vt = N.zeros((nexcluded, min_n_m), N.Float) iwork = N.zeros((8 * min_n_m, ), int_type) if 3 * natoms >= nexcluded: result = dgesdd('O', 3 * natoms, nexcluded, self.basis, 3 * natoms, sv, self.basis, 3 * natoms, vt, min_n_m, work_size, -1, iwork, 0) work = N.zeros((int(work_size[0]), ), N.Float) result = dgesdd('O', 3 * natoms, nexcluded, self.basis, 3 * natoms, sv, self.basis, 3 * natoms, vt, min_n_m, work, work.shape[0], iwork, 0) else: u = N.zeros((min_n_m, 3 * natoms), N.Float) result = dgesdd('S', 3 * natoms, nexcluded, self.basis, 3 * natoms, sv, u, 3 * natoms, vt, min_n_m, work_size, -1, iwork, 0) work = N.zeros((int(work_size[0]), ), N.Float) result = dgesdd('S', 3 * natoms, nexcluded, self.basis, 3 * natoms, sv, u, 3 * natoms, vt, min_n_m, work, work.shape[0], iwork, 0) self.basis[:min_n_m] = u if result['info'] != 0: raise ValueError('Lapack SVD: ' + ` result['info'] `) svmax = N.maximum.reduce(sv) nexcluded = N.add.reduce(N.greater(sv, 1.e-10 * svmax)) ntotal = nexcluded + nmodes for i in range(nmodes): self.basis[i + nexcluded] = N.ravel(basis[i].array * self.weights) min_n_m = min(3 * natoms, ntotal) vt = N.zeros((ntotal, min_n_m), N.Float) if 3 * natoms >= ntotal: result = dgesdd('O', 3 * natoms, ntotal, self.basis, 3 * natoms, sv, self.basis, 3 * natoms, vt, min_n_m, work_size, -1, iwork, 0) if int(work_size[0]) > work.shape[0]: work = N.zeros((int(work_size[0]), ), N.Float) result = dgesdd('O', 3 * natoms, ntotal, self.basis, 3 * natoms, sv, self.basis, 3 * natoms, vt, min_n_m, work, work.shape[0], iwork, 0) else: u = N.zeros((min_n_m, 3 * natoms), N.Float) result = dgesdd('S', 3 * natoms, ntotal, self.basis, 3 * natoms, sv, u, 3 * natoms, vt, min_n_m, work_size, -1, iwork, 0) if int(work_size[0]) > work.shape[0]: work = N.zeros((int(work_size[0]), ), N.Float) result = dgesdd('S', 3 * natoms, ntotal, self.basis, 3 * natoms, sv, u, 3 * natoms, vt, min_n_m, work, work.shape[0], iwork, 0) self.basis[:min_n_m] = u if result['info'] != 0: raise ValueError('Lapack SVD: ' + ` result['info'] `) svmax = N.maximum.reduce(sv) ntotal = N.add.reduce(N.greater(sv, 1.e-10 * svmax)) nmodes = ntotal - nexcluded else: if hasattr(self.basis, 'may_modify') and \ hasattr(self.basis, 'array'): self.basis = self.basis.array else: self.basis = N.array(map(lambda v: v.array, basis)) N.multiply(self.basis, self.weights, self.basis) self.basis.shape = (nmodes, 3 * natoms) min_n_m = min(3 * natoms, nmodes) vt = N.zeros((nmodes, min_n_m), N.Float) iwork = N.zeros((8 * min_n_m, ), int_type) if 3 * natoms >= nmodes: result = dgesdd('O', 3 * natoms, nmodes, self.basis, 3 * natoms, sv, self.basis, 3 * natoms, vt, min_n_m, work_size, -1, iwork, 0) work = N.zeros((int(work_size[0]), ), N.Float) result = dgesdd('O', 3 * natoms, nmodes, self.basis, 3 * natoms, sv, self.basis, 3 * natoms, vt, min_n_m, work, work.shape[0], iwork, 0) else: u = N.zeros((min_n_m, 3 * natoms), N.Float) result = dgesdd('S', 3 * natoms, nmodes, self.basis, 3 * natoms, sv, u, 3 * natoms, vt, min_n_m, work_size, -1, iwork, 0) work = N.zeros((int(work_size[0]), ), N.Float) result = dgesdd('S', 3 * natoms, nmodes, self.basis, 3 * natoms, sv, u, 3 * natoms, vt, min_n_m, work, work.shape[0], iwork, 0) self.basis[:min_n_m] = u if result['info'] != 0: raise ValueError('Lapack SVD: ' + ` result['info'] `) svmax = N.maximum.reduce(sv) nmodes = N.add.reduce(N.greater(sv, 1.e-10 * svmax)) ntotal = nmodes self.basis = self.basis[nexcluded:ntotal, :]
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:
def _setupBasis(self): if type(self.basis) is type(()): excluded, basis = self.basis else: excluded = [] basis = self.basis nexcluded = len(excluded) nmodes = len(basis) ntotal = nexcluded + nmodes natoms = len(basis[0]) sv = N.zeros((min(ntotal, 3*natoms),), N.Float) work_size = N.zeros((1,), N.Float) if nexcluded > 0: self.basis = N.zeros((ntotal, 3*natoms), N.Float) for i in range(nexcluded): self.basis[i] = N.ravel(excluded[i].array*self.weights) min_n_m = min(3*natoms, nexcluded) vt = N.zeros((nexcluded, min_n_m), N.Float) iwork = N.zeros((8*min_n_m,), int_type) if 3*natoms >= nexcluded: result = dgesdd('O', 3*natoms, nexcluded, self.basis, 3*natoms, sv, self.basis, 3*natoms, vt, min_n_m, work_size, -1, iwork, 0) work = N.zeros((int(work_size[0]),), N.Float) result = dgesdd('O', 3*natoms, nexcluded, self.basis, 3*natoms, sv, self.basis, 3*natoms, vt, min_n_m, work, work.shape[0], iwork, 0) else: u = N.zeros((min_n_m, 3*natoms), N.Float) result = dgesdd('S', 3*natoms, nexcluded, self.basis, 3*natoms, sv, u, 3*natoms, vt, min_n_m, work_size, -1, iwork, 0) work = N.zeros((int(work_size[0]),), N.Float) result = dgesdd('S', 3*natoms, nexcluded, self.basis, 3*natoms, sv, u, 3*natoms, vt, min_n_m, work, work.shape[0], iwork, 0) self.basis[:min_n_m] = u if result['info'] != 0: raise ValueError('Lapack SVD: ' + `result['info']`) svmax = N.maximum.reduce(sv) nexcluded = N.add.reduce(N.greater(sv, 1.e-10*svmax)) ntotal = nexcluded + nmodes for i in range(nmodes): self.basis[i+nexcluded] = N.ravel(basis[i].array*self.weights) min_n_m = min(3*natoms, ntotal) vt = N.zeros((ntotal, min_n_m), N.Float) if 3*natoms >= ntotal: result = dgesdd('O', 3*natoms, ntotal, self.basis, 3*natoms, sv, self.basis, 3*natoms, vt, min_n_m, work_size, -1, iwork, 0) if int(work_size[0]) > work.shape[0]: work = N.zeros((int(work_size[0]),), N.Float) result = dgesdd('O', 3*natoms, ntotal, self.basis, 3*natoms, sv, self.basis, 3*natoms, vt, min_n_m, work, work.shape[0], iwork, 0) else: u = N.zeros((min_n_m, 3*natoms), N.Float) result = dgesdd('S', 3*natoms, ntotal, self.basis, 3*natoms, sv, u, 3*natoms, vt, min_n_m, work_size, -1, iwork, 0) if int(work_size[0]) > work.shape[0]: work = N.zeros((int(work_size[0]),), N.Float) result = dgesdd('S', 3*natoms, ntotal, self.basis, 3*natoms, sv, u, 3*natoms, vt, min_n_m, work, work.shape[0], iwork, 0) self.basis[:min_n_m] = u if result['info'] != 0: raise ValueError('Lapack SVD: ' + `result['info']`) svmax = N.maximum.reduce(sv) ntotal = N.add.reduce(N.greater(sv, 1.e-10*svmax)) nmodes = ntotal - nexcluded else: if hasattr(self.basis, 'may_modify') and \ hasattr(self.basis, 'array'): self.basis = self.basis.array else: self.basis = N.array(map(lambda v: v.array, basis)) N.multiply(self.basis, self.weights, self.basis) self.basis.shape = (nmodes, 3*natoms) min_n_m = min(3*natoms, nmodes) vt = N.zeros((nmodes, min_n_m), N.Float) iwork = N.zeros((8*min_n_m,), int_type) if 3*natoms >= nmodes: result = dgesdd('O', 3*natoms, nmodes, self.basis, 3*natoms, sv, self.basis, 3*natoms, vt, min_n_m, work_size, -1, iwork, 0) work = N.zeros((int(work_size[0]),), N.Float) result = dgesdd('O', 3*natoms, nmodes, self.basis, 3*natoms, sv, self.basis, 3*natoms, vt, min_n_m, work, work.shape[0], iwork, 0) else: u = N.zeros((min_n_m, 3*natoms), N.Float) result = dgesdd('S', 3*natoms, nmodes, self.basis, 3*natoms, sv, u, 3*natoms, vt, min_n_m, work_size, -1, iwork, 0) work = N.zeros((int(work_size[0]),), N.Float) result = dgesdd('S', 3*natoms, nmodes, self.basis, 3*natoms, sv, u, 3*natoms, vt, min_n_m, work, work.shape[0], iwork, 0) self.basis[:min_n_m] = u if result['info'] != 0: raise ValueError('Lapack SVD: ' + `result['info']`) svmax = N.maximum.reduce(sv) nmodes = N.add.reduce(N.greater(sv, 1.e-10*svmax)) ntotal = nmodes self.basis = self.basis[nexcluded:ntotal, :]