def run(self, formula, system, filename): """Calculate energy of specified system and save to file.""" system.get_potential_energy() # Won't create .bak file: traj = PickleTrajectory(open(filename, 'w'), 'w') traj.write(system) traj.close()
def run(self, formula, system, filename): """Calculate bond length of a dimer. This will calculate total energies for varying atomic separations close to the g2 bond length, allowing determination of bond length by fitting. """ if len(system) != 2: raise ValueError('Not a dimer') traj = PickleTrajectory(open(filename, 'w'), 'w') pos = system.positions d = np.linalg.norm(pos[1] - pos[0]) for x in range(-2, 3): system.set_distance(0, 1, d * (1.0 + x * 0.02)) traj.write(system) traj.close()
def retrieve(self, formula, filename): traj = PickleTrajectory(filename, 'r') distances = np.array( [np.linalg.norm(a.positions[1] - a.positions[0]) for a in traj]) energies = np.array([a.get_potential_energy() for a in traj]) polynomial = np.polyfit(distances, energies, 2) # or maybe 3rd order? # With 3rd order it is not always obvious which root is right pderiv = np.polyder(polynomial, 1) d0 = np.roots(pderiv) e0 = np.polyval(energies, d0) return distances, energies, d0, e0, polynomial
def read(self, filename, filetype=None): """Read from a file""" ftfe = filetype_from_ending(filename) aft = ase_filetype(filename) if ftfe == 'vmol': self.read_viewmol(filename) elif aft == 'traj': PickleTrajectory.__init__(self, filename) elif aft == 'gpaw-text': # this is strange code, but I don't know how to create the full # slice XXXXX last = read_gpaw_text(filename) self.__init__(atoms=last) s = slice(-1) rest = read_gpaw_text(filename, s) rest.append(last) self.append(rest) else: raise NotImplementedError('unknown file type "' + aft + '"')
def read(self, filename, filetype=None): """Read from a file""" ftfe = filetype_from_ending(filename) aft = ase_filetype(filename) if ftfe == 'vmol': self.read_viewmol(filename) elif aft == 'traj': PickleTrajectory.__init__(self, filename) elif aft == 'gpaw-text': # this is strange code, but I don't know how to create the full # slice XXXXX last = read_gpaw_text(filename) self.__init__(atoms=last) s = slice(-1) rest = read_gpaw_text(filename, s) rest.append(last) self.append(rest) else: raise NotImplementedError('unknown file type "'+ aft +'"')
def __iter__(self): if hasattr(self, 'offsets'): return PickleTrajectory.__iter__(self) else: return self.images.__iter__()
def __getitem__(self, i=-1): if hasattr(self, 'offsets'): return PickleTrajectory.__getitem__(self, i) else: return self.images[i]
def __len__(self): if hasattr(self, 'offsets'): return PickleTrajectory.__len__(self) else: return len(self.images)
def next(self): if hasattr(self, 'offsets'): return PickleTrajectory.next(self) else: return self.images.next()
import os import sys from ase import Atoms, Atom, QuasiNewton, PickleTrajectory from gpaw import * from gpaw.cluster import Cluster from gpaw.utilities.viewmol import ViewmolTrajectory, write_viewmol s = Cluster([Atom('H'), Atom('H', (0, 0, 3))]) s.minimal_box(2) c = GPAW(h=0.3, nbands=2) s.set_calculator(c) vfname = 'traj.vmol' pfname = 'traj.pickle' vmt = ViewmolTrajectory(s, vfname) traj = PickleTrajectory(pfname, 'w', s) #c.attach(vmt.add, 100000) #sys.exit() # Find the theoretical bond length: dyn = QuasiNewton(s) dyn.attach(traj.write) dyn.attach(vmt.add) dyn.run(fmax=0.05) traj = PickleTrajectory(pfname, 'r') vfname2 = 'pickle.vmol' write_viewmol(traj, vfname2)
def __next__(self): if hasattr(self, 'offsets'): return PickleTrajectory.next(self) else: return next(self.images)