def gen_mol_from_fasta(fasta_str): temp_dir = os.getenv('TEMP_DIR') if temp_dir: temp_dir += "/" + fasta_str else: temp_dir = os.path.join("/tmp", fasta_str) try: os.mkdir(temp_dir) except: pass fdfn = os.path.join(temp_dir, fasta_str) with open(fdfn + ".fasta", 'w') as f: f.write(fasta_str + "\n") mol2out = fdfn + ".mol2" mol2cout = fdfn + "-cut.mol2" babel = os.getenv("BABEL_EXE") subprocess.call( shlex.split('%s -ifasta %s.fasta --join -omol2 %s' % (babel, fdfn, mol2out))) init_mol2 = io.read(mol2out, format="mol2") nat = init_mol2.get_number_of_atoms() bonds = init_mol2.get_bonds() newbonds = [] ie = 18 #this is the atom number of Q iv = nat - 2 #this is the atom number of the last G for b in bonds: if (b[0] > ie - 1 and b[0] < iv) and (b[1] > ie - 1 and b[1] < iv): newbonds.append([b[0] - ie, b[1] - ie, b[2]]) newmol = init_mol2[ie:iv] newmol.set_bonds(newbonds) newmol.set_ffsymbols(init_mol2.get_ffsymbols()[ie:iv]) newmol.write(mol2cout, format="mol2", symbols=newmol.get_ffsymbols()) drei_param = os.path.join(DREI_DIR, "DREI_PARAM") drei_outdir = os.path.join(temp_dir, "drei") try: os.mkdir(drei_outdir) except: pass DFF.create_input(mol2cout, lammpsdrei_template, drei_param, drei_outdir) drei_comm = os.getenv("LAMMPS_EXE") #subprocess.call(shlex.split('bash -c "ls %s; cd %s; %s < in.drei |tee out"'%(drei_comm, drei_outdir, drei_comm))) subprocess.call( shlex.split('bash -c "cd %s; %s < in.drei > out"' % (drei_outdir, drei_comm))) #subprocess.call(shlex.split('bash -c "cp %s ./"' % os.path.join(drei_outdir, relaxed_xyz))) relaxed_xyz = io.read(os.path.join(drei_outdir, fn_relaxed_xyz), 0)[0] return relaxed_xyz, init_mol2, fasta_str[1:-1]
def import_atoms(self, filename, cur_frame): if filename: filename = filename[0] old_a = self.get_atoms(cur_frame) imp_a = read(filename, -1) new_a = old_a + imp_a self.initialize([new_a], [filename])
def __init__(self, images, k=0.1, climb=False): if isinstance(images, str): # this is a filename traj = read(images, '0:') images = [] for atoms in traj: images.append(atoms) NEB.__init__(self, images, k, climb, False) self.calculators = [None] * self.nimages self.energies_ok = False
def read(self, filenames, index=-1): images = [] names = [] for filename in filenames: i = read(filename, index) if not isinstance(i, list): i = [i] images.extend(i) names.extend([filename] * len(i)) self.initialize(images, names)
def create_input(mol2_input, drei_input_temp, Paramfile, outdir): mol = io.read(mol2_input, format="mol2") data, chemical_symbols = CreateLammps(mol, Paramfile) with open(os.path.join(outdir, "data"), "w") as f: f.write(data) if type(drei_input_temp) == type("str"): lammps_temp = Template(drei_input_temp) else: lammps_temp = Template(open(drei_input_temp, 'r').read()) with open(os.path.join(outdir, "in.drei"), "w") as f: f.write(lammps_temp.substitute(SYM=chemical_symbols))
def vasp_vol_relax(): Al = bulk('Al', 'fcc', a=4.5, cubic=True) calc = Vasp(xc='LDA', isif=7, nsw=5, ibrion=1, ediffg=-1e-3, lwave=False, lcharg=False) calc.calculate(Al) # Explicitly parse atomic position output file from Vasp CONTCAR_Al = io.read('CONTCAR', format='vasp') print('Stress after relaxation:\n', calc.read_stress()) print('Al cell post relaxation from calc:\n', calc.get_atoms().get_cell()) print('Al cell post relaxation from atoms:\n', Al.get_cell()) print('Al cell post relaxation from CONTCAR:\n', CONTCAR_Al.get_cell()) # All the cells should be the same. assert (calc.get_atoms().get_cell() == CONTCAR_Al.get_cell()).all() assert (Al.get_cell() == CONTCAR_Al.get_cell()).all() return Al
import numpy as np from ase_ext.io import read, PickleTrajectory from ase_ext.structure import bulk from ase_ext.calculators.emt import EMT a0 = 3.52 / np.sqrt(2) c0 = np.sqrt(8 / 3.0) * a0 print('%.4f %.3f' % (a0, c0 / a0)) for i in range(3): traj = PickleTrajectory('Ni.traj', 'w') eps = 0.01 for a in a0 * np.linspace(1 - eps, 1 + eps, 4): for c in c0 * np.linspace(1 - eps, 1 + eps, 4): ni = bulk('Ni', 'hcp', a=a, covera=c / a) ni.set_calculator(EMT()) ni.get_potential_energy() traj.write(ni) configs = read('Ni.traj@:') energies = [config.get_potential_energy() for config in configs] ac = [(config.cell[0, 0], config.cell[2, 2]) for config in configs] from ase_ext.optimize import polyfit p = polyfit(ac, energies) from scipy.optimize import fmin_bfgs a0, c0 = fmin_bfgs(p, (a0, c0)) print('%.4f %.3f' % (a0, c0 / a0)) assert abs(a0 - 2.469) < 0.001 assert abs(c0 / a0 - 1.624) < 0.005
a.z) return strc if len(sys.argv) != 5: print( """\n Converts a coordinate file (xyz, pdb, mol2 ...) into lammps reax input \n\nUsage: \n\t> python3%s xyz reaxtemplateFile xyzmodelid outputdir \n\n\te.g. python3 %s molecule.xyz in.reax.temp 0 Reax-relax\n""" % (sys.argv[0], sys.argv[0])) exit() finxyz = sys.argv[1] finp = sys.argv[2] nm = int(sys.argv[3]) fdir = sys.argv[4] mol = read(finxyz, nm) pos = mol.get_positions() mins = pos.min(axis=0) maxs = pos.max(axis=0) #cell = get_cell(pos) #maxs = (maxs - mins)/2 symlist = set(mol.get_chemical_symbols()) symlist = sorted(symlist) syms = ase.Atoms(symlist) indatoms = syms.get_atomic_numbers() masses, chemical_symbols = get_masses(syms) with open(os.path.join(fdir, "data"), "w") as f: f.write(reax_data_temp.substitute(NAT=mol.get_number_of_atoms(),\
from ase_ext import Atoms from ase_ext.io import read, write atoms = Atoms('HH', [[.0,.0,.0], [.0,.0,.74]], pbc=True, cell=[5, 5, 5]) atoms.set_initial_magnetic_moments([1, -1]) moms = atoms.get_initial_magnetic_moments() write('test.traj',atoms) atoms = read('test.traj') assert (atoms.get_initial_magnetic_moments() == moms).all()
except ImportError: pass else: w += ['png', 'eps'] for format in w: print(format, 'O', end=' ') fname1 = 'io-test.1.' + format fname2 = 'io-test.2.' + format write(fname1, atoms, format=format) if format not in ['cube', 'png', 'eps', 'cfg', 'struct']: write(fname2, images, format=format) if format in r: print('I') a1 = read(fname1) assert np.all( np.abs(a1.get_positions() - atoms.get_positions()) < 1e-6) if format in ['traj', 'cube', 'cfg', 'struct']: assert np.all(np.abs(a1.get_cell() - atoms.get_cell()) < 1e-6) if format in ['cfg']: assert np.all( np.abs(a1.get_array('extra') - atoms.get_array('extra')) < 1e-6) if format not in ['cube', 'png', 'eps', 'cfg', 'struct']: a2 = read(fname2) a3 = read(fname2, index=0) a4 = read(fname2, index=slice(None)) else: print()
from string import Template from numpy import * from scipy import spatial import sys, os PYYAEHMOP_DIR = os.getenv("PYYAEHMOP_DIR") YAEHMOP_DIR = os.getenv("YAEHMOP_DIR") sys.path.append(PYYAEHMOP_DIR) from Yaehmop import * ASE_DIR = os.getenv("ASE_DIR") sys.path.append(ASE_DIR) from ase_ext.io import read import ase_ext as ase if len(sys.argv) != 3: print( """\n Converts a coordinate file (xyz, pdb, mol2 ...) into YAEHMOP input \n\nUsage: \n\t> python3%s xyz xyzmodelid \n\n\te.g. python3 %s molecule.xyz 0 \n""" % (sys.argv[0], sys.argv[0])) exit() fn = sys.argv[1] id = sys.argv[2] mol = read(fn, id) ymol = Yaehmop(mol=mol, yaehmop_binary=os.path.join(YAEHMOP_DIR, "bind"), param_file=os.path.join(YAEHMOP_DIR, "eht_param.dat")) ymol.create_input(just_matrices=False) ymol.run()
import numpy as np from ase_ext import Atoms from ase_ext.units import fs from ase_ext.calculators.test import TestPotential from ase_ext.calculators.emt import EMT from ase_ext.md import VelocityVerlet from ase_ext.io import PickleTrajectory, read from ase_ext.optimize import QuasiNewton np.seterr(all='raise') a = Atoms('4X', masses=[1, 2, 3, 4], positions=[(0, 0, 0), (1, 0, 0), (0, 1, 0), (0.1, 0.2, 0.7)], calculator=TestPotential()) print(a.get_forces()) md = VelocityVerlet(a, dt=0.5 * fs, logfile='-', loginterval=500) traj = PickleTrajectory('4N.traj', 'w', a) md.attach(traj.write, 100) e0 = a.get_total_energy() md.run(steps=10000) del traj assert abs(read('4N.traj').get_total_energy() - e0) < 0.0001 qn = QuasiNewton(a) qn.run(0.001) assert abs(a.get_potential_energy() - 1.0) < 0.000002
from ase_ext import Atoms from ase_ext.io import read, write from ase_ext.calculators import Exciting from ase_ext.units import Bohr, Hartree from ase_ext.test import NotAvailable try: import lxml except ImportError: raise NotAvailable('This test need lxml module.') a = Atoms('N3O', [(0, 0, 0), (1, 0, 0), (0, 0, 1), (0.5, 0.5, 0.5)], pbc=True) raise NotAvailable('Problem with lxml module.') write('geo.exi', a) b = read('geo.exi') print(a) print(a.get_positions()) print(b) print(b.get_positions()) calculator = Exciting(dir='excitingtestfiles', kpts=(4, 4, 3), maxscl=3, #bin='/fshome/chm/git/exciting/bin/excitingser' )
from ase_ext import Atoms from ase_ext.calculators.lj import LennardJones from ase_ext.optimize.basin import BasinHopping from ase_ext.io import PickleTrajectory, read from ase_ext.units import kB N = 7 R = N**(1. / 3.) pos = np.random.uniform(-R, R, (N, 3)) s = Atoms('He' + str(N), positions=pos) s.set_calculator(LennardJones()) ftraj = 'lowest.traj' traj = PickleTrajectory(ftraj, 'w', s) bh = BasinHopping(s, temperature=100 * kB, dr=0.5, optimizer_logfile=None) bh.attach(traj) bh.run(10) Emin, smin = bh.get_minimum() # recalc energy smin.set_calculator(LennardJones()) E = smin.get_potential_energy() assert abs(E - Emin) < 1e-15 traj.close() smim = read(ftraj) E = smin.get_potential_energy() assert abs(E - Emin) < 1e-15 #view(smin)