def get_atoms_surf(): a = 2.70 c = 1.59 * a h = 1.85 d = 1.10 slab = Atoms('2Cu', [(0., 0., 0.), (1 / 3., 1 / 3., -0.5 * c)], tags=(0, 1), pbc=(1, 1, 0)) slab.set_cell([(a, 0, 0), (a / 2, 3**0.5 * a / 2, 0), (0, 0, 1)]) slab = slab.repeat((4, 4, 1)) mask = [a.tag == 1 for a in slab] slab.set_constraint(FixAtoms(mask=mask)) return slab
def get_atoms(): srf = Atoms('Cu64', [(1.2763, 1.2763, 4.0000), (3.8290, 1.2763, 4.0000), (6.3816, 1.2763, 4.0000), (8.9343, 1.2763, 4.0000), (1.2763, 3.8290, 4.0000), (3.8290, 3.8290, 4.0000), (6.3816, 3.8290, 4.0000), (8.9343, 3.8290, 4.0000), (1.2763, 6.3816, 4.0000), (3.8290, 6.3816, 4.0000), (6.3816, 6.3816, 4.0000), (8.9343, 6.3816, 4.0000), (1.2763, 8.9343, 4.0000), (3.8290, 8.9343, 4.0000), (6.3816, 8.9343, 4.0000), (8.9343, 8.9343, 4.0000), (0.0000, 0.0000, 5.8050), (2.5527, 0.0000, 5.8050), (5.1053, 0.0000, 5.8050), (7.6580, 0.0000, 5.8050), (0.0000, 2.5527, 5.8050), (2.5527, 2.5527, 5.8050), (5.1053, 2.5527, 5.8050), (7.6580, 2.5527, 5.8050), (0.0000, 5.1053, 5.8050), (2.5527, 5.1053, 5.8050), (5.1053, 5.1053, 5.8050), (7.6580, 5.1053, 5.8050), (0.0000, 7.6580, 5.8050), (2.5527, 7.6580, 5.8050), (5.1053, 7.6580, 5.8050), (7.6580, 7.6580, 5.8050), (1.2409, 1.2409, 7.6081), (3.7731, 1.2803, 7.6603), (6.3219, 1.3241, 7.6442), (8.8935, 1.2669, 7.6189), (1.2803, 3.7731, 7.6603), (3.8188, 3.8188, 7.5870), (6.3457, 3.8718, 7.6649), (8.9174, 3.8340, 7.5976), (1.3241, 6.3219, 7.6442), (3.8718, 6.3457, 7.6649), (6.3945, 6.3945, 7.6495), (8.9576, 6.3976, 7.6213), (1.2669, 8.8935, 7.6189), (3.8340, 8.9174, 7.5976), (6.3976, 8.9576, 7.6213), (8.9367, 8.9367, 7.6539), (0.0582, 0.0582, 9.4227), (2.5965, -0.2051, 9.4199), (5.1282, 0.0663, 9.4037), (7.6808, -0.0157, 9.4235), (-0.2051, 2.5965, 9.4199), (2.1913, 2.1913, 9.6123), (5.0046, 2.5955, 9.4873), (7.5409, 2.5336, 9.4126), (0.0663, 5.1282, 9.4037), (2.5955, 5.0046, 9.4873), (5.3381, 5.3381, 9.6106), (7.8015, 5.0682, 9.4237), (-0.0157, 7.6808, 9.4235), (2.5336, 7.5409, 9.4126), (5.0682, 7.8015, 9.4237), (7.6155, 7.6155, 9.4317)]) c2 = Atoms('C2', [(3.2897, 3.2897, 10.6627), (4.2113, 4.2113, 10.6493)]) srf.extend(c2) srf.pbc = (1, 1, 0) srf.set_cell([10.2106, 10.2106, 20.6572], scale_atoms=False) mask = [a.index < 32 for a in srf] c1 = FixedPlane(-1, (1 / np.sqrt(2), 1 / np.sqrt(2), 1)) c2 = FixedPlane(-2, (1 / np.sqrt(2), 1 / np.sqrt(2), 1)) constraint = FixAtoms(mask=mask) srf.set_constraint([constraint, c1, c2]) return srf
import numpy as np from ase_ext import Atoms from ase_ext.calculators.emt import EMT from ase_ext.io import PickleTrajectory Cu = Atoms('Cu', pbc=(1, 0, 0), calculator=EMT()) traj = PickleTrajectory('Cu.traj', 'w') for a in np.linspace(2.0, 4.0, 20): Cu.set_cell([a, 1, 1], scale_atoms=True) traj.write(Cu)
def read_aims_output(filename, index=-1): """ Import FHI-aims output files with all data available, i.e. relaxations, MD information, force information etc etc etc. """ from ase_ext import Atoms, Atom from ase_ext.calculators.singlepoint import SinglePointCalculator from ase_ext.units import Ang, fs molecular_dynamics = False fd = open(filename, 'r') cell = [] images = [] n_periodic = -1 f = None pbc = False found_aims_calculator = False v_unit = Ang / (1000.0 * fs) while True: line = fd.readline() if not line: break if "List of parameters used to initialize the calculator:" in line: fd.readline() calc = read_aims_calculator(fd) calc.out = filename found_aims_calculator = True if "Number of atoms" in line: inp = line.split() n_atoms = int(inp[5]) if "| Unit cell:" in line: if not pbc: pbc = True for i in range(3): inp = fd.readline().split() cell.append([inp[1], inp[2], inp[3]]) if "Atomic structure:" in line and not molecular_dynamics: fd.readline() atoms = Atoms() for i in range(n_atoms): inp = fd.readline().split() atoms.append(Atom(inp[3], (inp[4], inp[5], inp[6]))) if "Complete information for previous time-step:" in line: molecular_dynamics = True if "Updated atomic structure:" in line and not molecular_dynamics: fd.readline() atoms = Atoms() velocities = [] for i in range(n_atoms): inp = fd.readline().split() atoms.append(Atom(inp[4], (inp[1], inp[2], inp[3]))) if molecular_dynamics: inp = fd.readline().split() if "Atomic structure (and velocities)" in line: fd.readline() atoms = Atoms() velocities = [] for i in range(n_atoms): inp = fd.readline().split() atoms.append(Atom(inp[4], (inp[1], inp[2], inp[3]))) inp = fd.readline().split() velocities += [[ float(inp[1]) * v_unit, float(inp[2]) * v_unit, float(inp[3]) * v_unit ]] atoms.set_velocities(velocities) images.append(atoms) if "Total atomic forces" in line: f = [] for i in range(n_atoms): inp = fd.readline().split() f.append([float(inp[2]), float(inp[3]), float(inp[4])]) if not found_aims_calculator: e = images[-1].get_potential_energy() images[-1].set_calculator( SinglePointCalculator(e, f, None, None, atoms)) e = None f = None if "Total energy corrected" in line: e = float(line.split()[5]) if pbc: atoms.set_cell(cell) atoms.pbc = True if not found_aims_calculator: atoms.set_calculator( SinglePointCalculator(e, None, None, None, atoms)) if not molecular_dynamics: images.append(atoms) e = None if found_aims_calculator: calc.set_results(images[-1]) images[-1].set_calculator(calc) fd.close() if molecular_dynamics: images = images[1:] # return requested images, code borrowed from ase_ext.io/trajectory.py if isinstance(index, int): return images[index] else: step = index.step or 1 if step > 0: start = index.start or 0 if start < 0: start += len(images) stop = index.stop or len(images) if stop < 0: stop += len(images) else: if index.start is None: start = len(images) - 1 else: start = index.start if start < 0: start += len(images) if index.stop is None: stop = -1 else: stop = index.stop if stop < 0: stop += len(images) return [images[i] for i in range(start, stop, step)]
def read_aims(filename): """Import FHI-aims geometry type files. Reads unitcell, atom positions and constraints from a geometry.in file. """ from ase_ext import Atoms from ase_ext.constraints import FixAtoms, FixCartesian import numpy as np atoms = Atoms() fd = open(filename, 'r') lines = fd.readlines() fd.close() positions = [] cell = [] symbols = [] fix = [] fix_cart = [] xyz = np.array([0, 0, 0]) i = -1 n_periodic = -1 periodic = np.array([False, False, False]) for n, line in enumerate(lines): inp = line.split() if inp == []: continue if inp[0] == 'atom': if xyz.all(): fix.append(i) elif xyz.any(): print(1) fix_cart.append(FixCartesian(i, xyz)) floatvect = float(inp[1]), float(inp[2]), float(inp[3]) positions.append(floatvect) symbols.append(inp[-1]) i += 1 xyz = np.array([0, 0, 0]) elif inp[0] == 'lattice_vector': floatvect = float(inp[1]), float(inp[2]), float(inp[3]) cell.append(floatvect) n_periodic = n_periodic + 1 periodic[n_periodic] = True if inp[0] == 'constrain_relaxation': if inp[1] == '.true.': fix.append(i) elif inp[1] == 'x': xyz[0] = 1 elif inp[1] == 'y': xyz[1] = 1 elif inp[1] == 'z': xyz[2] = 1 if xyz.all(): fix.append(i) elif xyz.any(): fix_cart.append(FixCartesian(i, xyz)) atoms = Atoms(symbols, positions) if periodic.all(): atoms.set_cell(cell) atoms.set_pbc(periodic) if len(fix): atoms.set_constraint([FixAtoms(indices=fix)] + fix_cart) else: atoms.set_constraint(fix_cart) return atoms
import numpy as np from ase_ext.calculators.emt import EMT from ase_ext import Atoms a = 3.60 b = a / 2 cu = Atoms('Cu', positions=[(0, 0, 0)], cell=[(0, b, b), (b, 0, b), (b, b, 0)], pbc=1, calculator=EMT()) e0 = cu.get_potential_energy() print(e0) cu.set_cell(cu.get_cell() * 1.001, scale_atoms=True) e1 = cu.get_potential_energy() V = a**3 / 4 B = 2 * (e1 - e0) / 0.003**2 / V * 160.2 print(B) for i in range(4): x = 0.001 * i A = np.array([(x, b, b + x), (b, 0, b), (b, b, 0)]) cu.set_cell(A, scale_atoms=True) e = cu.get_potential_energy() - e0 if i == 0: print(i, e) else: print(i, e, e / x**2) A = np.array([(0, b, b), (b, 0, b), (6 * b, 6 * b, 0)])
def read_vasp_out(filename='OUTCAR', index=-1): """Import OUTCAR type file. Reads unitcell, atom positions, energies, and forces from the OUTCAR file. - does not (yet) read any constraints """ import os import numpy as np from ase_ext.calculators.singlepoint import SinglePointCalculator from ase_ext import Atoms, Atom if isinstance(filename, str): f = open(filename) else: # Assume it's a file-like object f = filename data = f.readlines() natoms = 0 images = [] atoms = Atoms(pbc=True) energy = 0 species = [] species_num = [] symbols = [] for n, line in enumerate(data): if 'POSCAR:' in line: temp = line.split() species = temp[1:] if 'ions per type' in line: temp = line.split() for ispecies in range(len(species)): species_num += [int(temp[ispecies + 4])] natoms += species_num[-1] for iatom in range(species_num[-1]): symbols += [species[ispecies]] if 'direct lattice vectors' in line: cell = [] for i in range(3): temp = data[n + 1 + i].split() cell += [[float(temp[0]), float(temp[1]), float(temp[2])]] atoms.set_cell(cell) if 'FREE ENERGIE OF THE ION-ELECTRON SYSTEM' in line: energy = float(data[n + 2].split()[4]) if 'POSITION ' in line: forces = [] for iatom in range(natoms): temp = data[n + 2 + iatom].split() atoms += Atom(symbols[iatom], [float(temp[0]), float(temp[1]), float(temp[2])]) forces += [[float(temp[3]), float(temp[4]), float(temp[5])]] atoms.set_calculator( SinglePointCalculator(energy, forces, None, None, atoms)) images += [atoms] atoms = Atoms(pbc=True) # return requested images, code borrowed from ase_ext.io/trajectory.py if isinstance(index, int): return images[index] else: step = index.step or 1 if step > 0: start = index.start or 0 if start < 0: start += len(images) stop = index.stop or len(images) if stop < 0: stop += len(images) else: if index.start is None: start = len(images) - 1 else: start = index.start if start < 0: start += len(images) if index.stop is None: stop = -1 else: stop = index.stop if stop < 0: stop += len(images) return [images[i] for i in range(start, stop, step)]
# Distance between Cu atoms on a (100) surface: d = 2.74 h1 = d * sqrt(3) / 2 h2 = d * sqrt(2.0 / 3) initial = Atoms( symbols='Pt', positions=[ (0, 0, 0) ], #(1.37,0.79,2.24),(2.74,1.58,4.48),(0,0,6.72),(1.37,0.79,8.96),(2.74,1.58,11.2)], cell=([(d, 0, 0), (d / 2, h1, 0), (d / 2, h1 / 3, -h2)]), pbc=(True, True, True)) initial *= (7, 8, 6) # 5x5 (100) surface-cell cell = initial.get_cell() cell[2] = (0, 0, 22) initial.set_cell(cell) #initial.set_pbc((True,True,False)) # Approximate height of Ag atom on Cu(100) surfece: h0 = 2.2373 initial += Atom('Pt', (10.96, 11.074, h0)) initial += Atom('Pt', (13.7, 11.074, h0)) initial += Atom('Pt', (9.59, 8.701, h0)) initial += Atom('Pt', (12.33, 8.701, h0)) initial += Atom('Pt', (15.07, 8.701, h0)) initial += Atom('Pt', (10.96, 6.328, h0)) initial += Atom('Pt', (13.7, 6.328, h0)) if 0: view(initial) # Make band: