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()
def read_vasp(filename='CONTCAR'): """Import POSCAR/CONTCAR type file. Reads unitcell, atom positions and constraints from the POSCAR/CONTCAR file and tries to read atom types from POSCAR/CONTCAR header, if this fails the atom types are read from OUTCAR or POTCAR file. """ from ase_ext import Atoms, Atom from ase_ext.constraints import FixAtoms, FixScaled from ase_ext.data import chemical_symbols import numpy as np if isinstance(filename, str): f = open(filename) else: # Assume it's a file-like object f = filename # First line should contain the atom symbols , eg. "Ag Ge" in # the same order # as later in the file (and POTCAR for the full vasp run) atomtypes = f.readline().split() lattice_constant = float(f.readline()) # Now the lattice vectors a = [] for ii in range(3): s = f.readline().split() floatvect = float(s[0]), float(s[1]), float(s[2]) a.append(floatvect) basis_vectors = np.array(a) * lattice_constant # Number of atoms. Again this must be in the same order as # in the first line # or in the POTCAR or OUTCAR file atom_symbols = [] numofatoms = f.readline().split() #vasp5.1 has an additional line which gives the atom types #the following try statement skips this line try: int(numofatoms[0]) except ValueError: numofatoms = f.readline().split() numsyms = len(numofatoms) if len(atomtypes) < numsyms: # First line in POSCAR/CONTCAR didn't contain enough symbols. atomtypes = atomtypes_outpot(f.name, numsyms) else: try: for atype in atomtypes[:numsyms]: if not atype in chemical_symbols: raise KeyError except KeyError: atomtypes = atomtypes_outpot(f.name, numsyms) for i, num in enumerate(numofatoms): numofatoms[i] = int(num) [atom_symbols.append(atomtypes[i]) for na in range(numofatoms[i])] # Check if Selective dynamics is switched on sdyn = f.readline() selective_dynamics = sdyn[0].lower() == "s" # Check if atom coordinates are cartesian or direct if selective_dynamics: ac_type = f.readline() else: ac_type = sdyn cartesian = ac_type[0].lower() == "c" or ac_type[0].lower() == "k" tot_natoms = sum(numofatoms) atoms_pos = np.empty((tot_natoms, 3)) if selective_dynamics: selective_flags = np.empty((tot_natoms, 3), dtype=bool) for atom in range(tot_natoms): ac = f.readline().split() atoms_pos[atom] = (float(ac[0]), float(ac[1]), float(ac[2])) if selective_dynamics: curflag = [] for flag in ac[3:6]: curflag.append(flag == 'F') selective_flags[atom] = curflag # Done with all reading if type(filename) == str: f.close() if cartesian: atoms_pos *= lattice_constant atoms = Atoms(symbols=atom_symbols, cell=basis_vectors, pbc=True) if cartesian: atoms.set_positions(atoms_pos) else: atoms.set_scaled_positions(atoms_pos) if selective_dynamics: constraints = [] indices = [] for ind, sflags in enumerate(selective_flags): if sflags.any() and not sflags.all(): constraints.append(FixScaled(atoms.get_cell(), ind, sflags)) elif sflags.all(): indices.append(ind) if indices: constraints.append(FixAtoms(indices)) if constraints: atoms.set_constraint(constraints) 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)])
from ase_ext.io import write, PickleTrajectory from ase_ext.calculators.emt import ASAP # 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)