import numpy as np from ase_ext import Atoms, Atom a = Atoms([Atom('Cu')]) a.positions[:] += 1.0 print(a.get_positions(), a.positions) a = a + a a += a a.append(Atom('C')) a += Atoms([]) a += Atom('H', magmom=1) print(a.get_initial_magnetic_moments()) print(a[0].number) print(a[[0, 1]].get_atomic_numbers()) print(a[np.array([1, 1, 0, 0, 1], bool)].get_atomic_numbers()) print(a[::2].get_atomic_numbers()) print(a.get_chemical_symbols()) del a[2] print(a.get_chemical_symbols()) del a[-2:] print(a.get_chemical_symbols())
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)]
from ase_ext import Atoms, Atom, view from gpaw import GPAW logo = """\ H HH HHH H H H H HHH H HH H H H H H H HH HHH""" d = 0.8 atoms = Atoms() for y, line in enumerate(logo.split('\n')): for x, c in enumerate(line): if c == 'H': atoms.append(Atom('H', [d * x, -d * y, 0])) atoms.center(vacuum=2.0) view(atoms) if 0: calc = GPAW(nbands=30) atoms.set_calculator(calc) atoms.get_potential_energy() calc.write('ase-logo.gpw') else: calc = GPAW('ase-logo.gpw', txt=None) density = calc.get_pseudo_density() image = density[..., density.shape[2] // 2] if 1: # scale colors to wiki background / foreground