def test_vib(): import os from ase import Atoms from ase.calculators.emt import EMT from ase.optimize import QuasiNewton from ase.vibrations import Vibrations from ase.thermochemistry import IdealGasThermo n2 = Atoms('N2', positions=[(0, 0, 0), (0, 0, 1.1)], calculator=EMT()) QuasiNewton(n2).run(fmax=0.01) vib = Vibrations(n2) vib.run() freqs = vib.get_frequencies() print(freqs) vib.summary() print(vib.get_mode(-1)) vib.write_mode(n=None, nimages=20) vib_energies = vib.get_energies() for image in vib.iterimages(): assert len(image) == 2 thermo = IdealGasThermo(vib_energies=vib_energies, geometry='linear', atoms=n2, symmetrynumber=2, spin=0) thermo.get_gibbs_energy(temperature=298.15, pressure=2 * 101325.) assert vib.clean(empty_files=True) == 0 assert vib.clean() == 13 assert len(list(vib.iterimages())) == 13 d = dict(vib.iterdisplace(inplace=False)) for name, atoms in vib.iterdisplace(inplace=True): assert d[name] == atoms vib = Vibrations(n2) vib.run() assert vib.combine() == 13 assert (freqs == vib.get_frequencies()).all() vib = Vibrations(n2) assert vib.split() == 1 assert (freqs == vib.get_frequencies()).all() assert vib.combine() == 13 # Read the data from other working directory dirname = os.path.basename(os.getcwd()) os.chdir('..') # Change working directory vib = Vibrations(n2, name=os.path.join(dirname, 'vib')) assert (freqs == vib.get_frequencies()).all() assert vib.clean() == 1
def test_pickle_manipulation(self, n2_emt): atoms = n2_emt vib = Vibrations(atoms, name='interrupt') vib.run() disp_file = 'interrupt.1x-.pckl' comb_file = 'interrupt.all.pckl' assert os.path.isfile(disp_file) assert not os.path.isfile(comb_file) with pytest.raises(RuntimeError): vib.split() # Build a combined file assert vib.combine() == 13 # Individual displacements should be gone, combination should exist assert not os.path.isfile(disp_file) assert os.path.isfile(comb_file) # Not allowed to run after data has been combined with pytest.raises(RuntimeError): vib.run() # But reading is allowed vib.read() # Splitting should fail if any split file already exists with open(disp_file, 'w') as f: f.write("hello") with pytest.raises(RuntimeError): vib.split() os.remove(disp_file) # Now split() for real: replace .all.pckl file with displacements vib.split() assert os.path.isfile(disp_file) assert not os.path.isfile(comb_file) # Not allowed to clobber existing combined file with open(comb_file, 'w') as f: f.write("Hello") with pytest.raises(RuntimeError): vib.combine() os.remove(comb_file) # Combining data also fails if some data is missing os.remove('interrupt.1x-.pckl') with pytest.raises(RuntimeError): vib.combine() vib.clean()
def test_json_manipulation(self, testdir, random_dimer): vib = Vibrations(random_dimer, name='interrupt') vib.run() disp_file = Path('interrupt/cache.1x-.json') comb_file = Path('interrupt/combined.json') assert disp_file.is_file() assert not comb_file.is_file() # Should do nothing harmful as files are already split # (It used to raise an error but this is no longer implemented.) vib.split() # Build a combined file assert vib.combine() == 13 # Individual displacements should be gone, combination should exist assert not disp_file.is_file() assert comb_file.is_file() # Not allowed to run after data has been combined with pytest.raises(RuntimeError): vib.run() # But reading is allowed vib.read() # Splitting should fail if any split file already exists with open(disp_file, 'w') as fd: fd.write("hello") with pytest.raises(AssertionError): vib.split() os.remove(disp_file) # Now split() for real: replace .all.json file with displacements vib.split() assert disp_file.is_file() assert not comb_file.is_file()
from ase.constraints import FixAtoms from ase.units import * from ase.vibrations import Vibrations from flare import kernels from flare.modules import gp_calculator from mc_kernels import mc_simple import pickle # load TS TS = read('dimer.traj') # fix bottommost layer fix = [atom.index for atom in TS if atom.position[2] < 9] move = [atom.index for atom in TS if atom.position[2] > 9] constraint = FixAtoms(mask=fix) TS.set_constraint(constraint) # set gp calculator gp_file = open('PdAg.gp', 'rb') gp_model = pickle.load(gp_file) gp_model.energy_force_kernel = mc_simple.two_plus_three_mc_force_en TS.set_calculator(gp_calculator.GPCalculator(gp_model)) # calculate Hessian vib = Vibrations(TS, indices=move) vib.run() # clean up vib.summary(log='gp_freq.txt') vib.combine()
potential='../CourseGitRepo/HA5_al_potential.alloy') # Set up EAM atoms.set_calculator(mishin) # Relax structure dyn = BFGS(atoms, trajectory=f'traj/Al{N}.traj') dyn.run(fmax=0.05) # Get vibrational spectrum str1 = f'-------- N={N}' str2 = ' ---------' print(str1 + str2.ljust(40 - len(str1))) ##### Using Vibrations module v = Vibrations(atoms, name=f'./vibs/vib_{N}') if not p.isfile(f'./vibs/vib_{N}.all.pckl'): print('Running vibration calculation') v.run() v.combine() # Combine pickle files # Get frequencies and DOS - i.e # of states per frequency all_freq = v.get_frequencies() # if N==38: # print(v.summary()) # print(all_freq) (freq, counts) = np.unique(all_freq, return_counts=True) fold_freq = v.fold(np.real(freq), np.real(counts), start=0, end=np.real(freq.max()), width=12, normalize=False) f_freq = np.array(fold_freq[0]) f_dos = np.array(fold_freq[1]) freq = np.array(freq)
for image in vib.iterimages(): assert len(image) == 2 thermo = IdealGasThermo(vib_energies=vib_energies, geometry='linear', atoms=n2, symmetrynumber=2, spin=0) thermo.get_gibbs_energy(temperature=298.15, pressure=2 * 101325.) assert vib.clean(empty_files=True) == 0 assert vib.clean() == 13 assert len(list(vib.iterimages())) == 13 d = dict(vib.iterdisplace(inplace=False)) for name, atoms in vib.iterdisplace(inplace=True): assert d[name] == atoms vib = Vibrations(n2) vib.run() assert vib.combine() == 13 assert (freqs == vib.get_frequencies()).all() vib = Vibrations(n2) assert vib.split() == 1 assert (freqs == vib.get_frequencies()).all() assert vib.combine() == 13 assert vib.clean() == 1