def makecyclicalkane(n): assert n >= 3 r = CClen/2.0 / math.sin(math.pi/n) coords = [] for i in range(n): angle = math.pi * 2.0 * i / n coords.append([r * math.cos(angle), r * math.sin(angle), random()*0.1]) mol = molecule.Molecule() C = atom.Atom(6, get_c_type(n)) H = atom.Atom(1,5) for i in range(n): mol.addatom(C, coords[i]) for i in range(n): mol.addatom(H, mol.coords[i] + numpy.array([random()*0.5,random()*0.5, random()*0.5])) mol.addatom(H, mol.coords[i] + numpy.array([random()*0.5,random()*0.5, random()*0.5])) for i in range(n): mol.buildconnect(i, (i+1)%n) mol.buildconnect(i, n+i*2) mol.buildconnect(i, n+i*2+1) #CCS2.shakeH(mol.coords, CCS2.getshakeHdata(mol)) # newmol, newene = Tinker.minimizemol(mol, "mm2") write.writexyz(mol, sys.stdout)
def removepbc(xyzfname, pbc): for mol in mtxyz.Mtxyz(file(xyzfname)): coords = mol.coords origin = coords[0] for i in range(1, len(coords)): coords[i] = nearestmirror(coords[i], origin, pbc) write.writexyz(mol)
def _writemoltotempfile(mol, dir=None): if dir is None: dir = os.getcwd() ofile = tempfile.NamedTemporaryFile(dir=dir) write.writexyz(mol, ofile) ofile.flush() return ofile
def gjf2xyz(gjffname, ofile): if gjffname == '-': ifile = sys.stdin else: ifile = file(gjffname) write.writexyz(read.readgjf(ifile), ofile) ofile.close()
def settype(xyzfile, typefile): mol = read.readxyz(xyzfile) types = tuple(_gettypes(typefile)) assert len(mol) == len(types), "%s - %s" % (len(mol), len(types)) for idx, atype in enumerate(types): mol.settype(idx, atype) write.writexyz(mol, sys.stdout)
def mirrormol(): if len(sys.argv) != 2: sys.stderr.write('Usage: %s <xyzfname>\n' % os.path.basename(sys.argv[0])) sys.exit(1) mol = read.readxyz(file(sys.argv[1])) mol.coords = -mol.coords write.writexyz(mol)
def test_1(self): mol = read.readxyz(StringIO(test_in)) mol = loads(dumps(mol)) ofile = StringIO() write.writexyz(mol, ofile) for line1, line2 in zip(ofile.getvalue().splitlines(), test_in.splitlines()): self.assertEqual(line1, line2)
def mtxyz2txyz(ifile, ofilenamepattern): i = 1 while 1: try: ofname = ofilenamepattern % i mol = read.readxyz(ifile) write.writexyz(mol, file(ofname, 'w')) except: break i += 1
def dmddat2mtxyz(dmddatfile, molfile, ofile, select_frames=None): aDmddat = dmddat.Dmddat(dmddatfile) mol = read.readxyz(molfile) if select_frames is None: select_frames = range(aDmddat.framenum) for frame_idx in select_frames: aDmddat.seek_frame(frame_idx) frame = aDmddat.next().coords for i in range(len(frame)): mol.coords[i] = molecule.CoordType(frame[i]) write.writexyz(mol, ofile, comment = 'frame: %i' % (frame_idx+1))
def shake1(mol, dats, ofile): for i in range(100): newmol = new_struct(mol) err_dats = get_status(newmol, dats) for j in range(500): if not err_dats: write.writexyz(newmol, ofile) return True print i, j, len(err_dats) random.shuffle(err_dats) newmol, err_dats = shake2(newmol, dats, err_dats) print "failed." return False
def newton_mol(self, mol, forcefield, converge = 0.01, prefix=None): """newton_mol(mol, forcefield, converge = 0.01, prefix=None) -> (Molecule, Float) optimized the mol with newton, and return the optimized energy """ forcefield = getparam(forcefield) if curdir: if prefix is None: ifname = 'tinker.xyz' else: ifname = prefix + '.xyz' ofile = file(ifname, 'w') else: ofile = tempfile.NamedTemporaryFile(suffix='.xyz') ifname = ofile.name write.writexyz(mol, ofile) ofile.flush() ofname = ifname + '_2' ifile = subprocess.Popen(['newton', ifname, forcefield, 'A', 'A', str(converge)], stdout=subprocess.PIPE).communicate()[0] result = None lines = ifile.splitlines() if self.log_iter: self.do_log_iter(lines) for line in lines: if line.startswith(' Final Function'): result = float(line.split()[-1]) break if result is None: sys.stdout.writelines(lines) raise RuntimeError, ifname try: newmol = read.readxyz(file(ofname)) except: print ifname, ofname raise ofile.close() os.remove(ofname) if curdir: os.remove(ifname) return newmol, result
def __call__(self, ifile, ofile, idxs): mol = read.readxyz(ifile) ns = [] for i in range(len(mol)): if mol.atoms[i].no == 7: ns.append(i) for i in idxs: assert i < len(ns) self.add_one_methyl(mol, ns[i]) write.writexyz(mol, ofile)
def optimize_minimize_mol(cmdname, mol, forcefield, converge = 0.01, prefix=None): """optimizemol(mol, forcefield, converge = 0.01) -> (Molecule, Float) optimized the mol, and return the optimized energy """ progpath = os.path.join(TINKERDIR, cmdname) forcefield = getparam(forcefield) if curdir: if prefix is None: ifname = 'tinker.xyz' else: ifname = prefix + '.xyz' ofile = file(ifname, 'w') else: ofile = tempfile.NamedTemporaryFile(suffix='.xyz') ifname = ofile.name write.writexyz(mol, ofile) ofile.flush() ofname = ifname + '_2' lines = subprocess.Popen([progpath, ifname, forcefield, str(converge)], stdout=subprocess.PIPE).communicate()[0].splitlines() result = None for line in lines: if line.find('Function') != -1: result = float(line.split()[-1]) break if result is None: sys.stdout.writelines(lines) raise RuntimeError, ifname try: newmol = read.readxyz(file(ofname)) except: print ifname, ofname raise ofile.close() os.remove(ofname) if curdir: os.remove(ifname) return newmol, result
def rotate_to(idx_ifile, mol_ifile, ofile): mol = read.readxyz(mol_ifile) idxs = [] coords = [] for line in idx_ifile: if not line.strip() or line.strip()[0] == '#': continue words = line.split() assert len(words) == 4 idx = int(words[0]) - 1 assert 0 <= idx < len(mol) idxs.append(idx) coords.append([float(x) for x in words[1:]]) coords = numpy.array(coords) res = _rmsd.rmsd2(coords, mol.coords.take(idxs, axis=0)) mat = numpy.array(res[1]) mol.coords = numpy.dot(mat[:3,:3], mol.coords.transpose()).transpose() + mat[:3,3] write.writexyz(mol, ofile)
def scalexyz(ifname, scaleratio): mol = read.readxyz(file(ifname)) mol.coords = [coord * scaleratio for coord in mol.coords] write.writexyz(mol)
def writemol(self, mol, ene): write.writexyz(mol, self.tmp_mtxyz_file, '%.4f' % ene)