def interp_by_neb(initial, final, n_images, interp='idpp', calculator=None, constraint=None, fmax=0.5, steps=10, **kwargs): """Interpolate between the initial and final points by NEB method. :param initial: The initial image. :param final: The final image. :param int n_images: The number of image between the initial and final images. :param string interp: The interpolation method. :param Callable calculator: The callable to generate calculators for the images. It can be set to None if no real optimization is intended. :param constraint: The constraint for the images. :param fmax: The maximal force to stop the optimization. :param steps: The number of optimization steps allowed. :param kwargs: All other keyword arguments are forwarded to the initializer of the ASE NEB class. :return: The list of images between the points. """ # To circumvent the error from interpolation when we have no middle images. if n_images == 0: return [initial, final] images = [initial] for _ in range(n_images): image = initial.copy() images.append(image) if calculator is not None: image.set_calculator(calculator()) if constraint is not None: image.set_constraint(constraint) continue images.append(final) neb = NEB(images, **kwargs) neb.interpolate(method=interp) if calculator is not None: dyn = MDMin(neb) dyn.run(fmax=fmax, steps=steps) return neb.images
# Make a mask of zeros and ones that select fixed atoms (the # two bottom layers): mask = initial.positions[:, 2] - min(initial.positions[:, 2]) < 1.5 * h constraint = FixAtoms(mask=mask) print(mask) for image in images: # Let all images use an EMT calculator: image.set_calculator(EMT()) image.set_constraint(constraint) # Relax the initial and final states: QuasiNewton(initial).run(fmax=0.05) QuasiNewton(final).run(fmax=0.05) # Create a Nudged Elastic Band: neb = NEB(images) # Make a starting guess for the minimum energy path (a straight line # from the initial to the final state): neb.interpolate() # Relax the NEB path: minimizer = MDMin(neb) minimizer.run(fmax=0.05) # Write the path to a trajectory: view(images) # 564 meV write('jump2.traj', images)
atoms = FaceCenteredCubic(size=(size,size,size), symbol="Cu", pbc=True) defsize = atoms.get_volume() atoms.set_cell(atoms.get_cell() * 1.1, scale_atoms=True) atoms.set_calculator(EMT()) def printvol(a): print "Volume:", a.get_volume(), " energy:",\ atoms.get_potential_energy() + atoms.get_kinetic_energy(),\ " stress:", atoms.get_stress()[:3] f = StrainFilter(atoms, [1, 1, 1, 0, 0, 0]) opt = MDMin(f, logfile="/dev/null", dt=0.01/(atoms.get_cell()[0,0])) printvol(atoms) opt.attach(printvol, 10, atoms) opt.run(0.01) printvol(atoms) print "Original vol:", defsize print stress = atoms.get_stress() for i in range(6): ReportTest("Stress component %d (T=0)" % (i,), stress[0], 0.0, 1e-5) atoms = FaceCenteredCubic(size=(size,size,size), symbol="Cu", pbc=True) atoms.set_calculator(EMT()) dyn = Langevin(atoms, 10*units.fs, 500*units.kB, 0.02) dyn.attach(printvol, 100, atoms) dyn.run(500) print
def main(): """Adopted from ase/test/stress.py""" # setup a Fist Lennard-Jones Potential inp = """&FORCE_EVAL &MM &FORCEFIELD &SPLINE EMAX_ACCURACY 500.0 EMAX_SPLINE 1000.0 EPS_SPLINE 1.0E-9 &END &NONBONDED &LENNARD-JONES atoms Ar Ar EPSILON [eV] 1.0 SIGMA [angstrom] 1.0 RCUT [angstrom] 10.0 &END LENNARD-JONES &END NONBONDED &CHARGE ATOM Ar CHARGE 0.0 &END CHARGE &END FORCEFIELD &POISSON &EWALD EWALD_TYPE none &END EWALD &END POISSON &END MM &END FORCE_EVAL""" calc = CP2K(label="test_stress", inp=inp, force_eval_method="Fist") # Theoretical infinite-cutoff LJ FCC unit cell parameters vol0 = 4 * 0.91615977036 # theoretical minimum a0 = vol0**(1 / 3) a = bulk('Ar', 'fcc', a=a0) cell0 = a.get_cell() a.calc = calc a.set_cell(np.dot(a.cell, [[1.02, 0, 0.03], [0, 0.99, -0.02], [0.1, -0.01, 1.03]]), scale_atoms=True) a *= (1, 2, 3) cell0 *= np.array([1, 2, 3])[:, np.newaxis] a.rattle() # Verify analytical stress tensor against numerical value s_analytical = a.get_stress() s_numerical = a.calc.calculate_numerical_stress(a, 1e-5) s_p_err = 100 * (s_numerical - s_analytical) / s_numerical print("Analytical stress:\n", s_analytical) print("Numerical stress:\n", s_numerical) print("Percent error in stress:\n", s_p_err) assert np.all(abs(s_p_err) < 1e-5) # Minimize unit cell opt = MDMin(UnitCellFilter(a), dt=0.01) opt.run(fmax=1e-3) # Verify minimized unit cell using Niggli tensors g_minimized = np.dot(a.cell, a.cell.T) g_theory = np.dot(cell0, cell0.T) g_p_err = 100 * (g_minimized - g_theory) / g_theory print("Minimized Niggli tensor:\n", g_minimized) print("Theoretical Niggli tensor:\n", g_theory) print("Percent error in Niggli tensor:\n", g_p_err) assert np.all(abs(g_p_err) < 1) print('passed test "stress"')
def main(): """Adopted from ase/test/stress.py""" if "ASE_CP2K_COMMAND" not in os.environ: raise NotAvailable('$ASE_CP2K_COMMAND not defined') # setup a Fist Lennard-Jones Potential inp = """&FORCE_EVAL &MM &FORCEFIELD &SPLINE EMAX_ACCURACY 500.0 EMAX_SPLINE 1000.0 EPS_SPLINE 1.0E-9 &END &NONBONDED &LENNARD-JONES atoms Ar Ar EPSILON [eV] 1.0 SIGMA [angstrom] 1.0 RCUT [angstrom] 10.0 &END LENNARD-JONES &END NONBONDED &CHARGE ATOM Ar CHARGE 0.0 &END CHARGE &END FORCEFIELD &POISSON &EWALD EWALD_TYPE none &END EWALD &END POISSON &END MM &END FORCE_EVAL""" calc = CP2K(label="test_stress", inp=inp, force_eval_method="Fist") vol0 = 4 * 0.91615977036 # theoretical minimum a0 = vol0 ** (1 / 3) a = bulk('Ar', 'fcc', a=a0) a.calc = calc a.set_cell(np.dot(a.cell, [[1.02, 0, 0.03], [0, 0.99, -0.02], [0.1, -0.01, 1.03]]), scale_atoms=True) a *= (1, 2, 3) a.rattle() sigma_vv = a.get_stress(voigt=False) # print(sigma_vv) # print(a.get_potential_energy() / len(a)) vol = a.get_volume() # compare stress tensor with numeric derivative deps = 1e-5 cell = a.cell.copy() for v in range(3): x = np.eye(3) x[v, v] += deps a.set_cell(np.dot(cell, x), scale_atoms=True) ep = a.calc.get_potential_energy(a, force_consistent=True) x[v, v] -= 2 * deps a.set_cell(np.dot(cell, x), scale_atoms=True) em = a.calc.get_potential_energy(a, force_consistent=True) s = (ep - em) / 2 / deps / vol # print(v, s, abs(s - sigma_vv[v, v])) assert abs(s - sigma_vv[v, v]) < 1e-7 for v1 in range(3): v2 = (v1 + 1) % 3 x = np.eye(3) x[v1, v2] = deps x[v2, v1] = deps a.set_cell(np.dot(cell, x), scale_atoms=True) ep = a.calc.get_potential_energy(a, force_consistent=True) x[v1, v2] = -deps x[v2, v1] = -deps a.set_cell(np.dot(cell, x), scale_atoms=True) em = a.calc.get_potential_energy(a, force_consistent=True) s = (ep - em) / deps / 4 / vol # print(v1, v2, s, abs(s - sigma_vv[v1, v2])) assert abs(s - sigma_vv[v1, v2]) < 1e-7 # run a cell optimization, see if it finds back original crystal structure opt = MDMin(UnitCellFilter(a), dt=0.01, logfile=None) opt.run(fmax=0.1) # print(a.cell) for i in range(3): for j in range(3): x = np.dot(a.cell[i], a.cell[j]) y = (i + 1) * (j + 1) * a0 ** 2 / 2 if i != j: y /= 2 # print(i, j, x, (x - y) / x) assert abs((x - y) / x) < 0.01 print('passed test "stress"')
# Make a mask of zeros and ones that select fixed atoms (the # two bottom layers): mask = initial.positions[:, 2] - min(initial.positions[:, 2]) < 1.5 * h constraint = FixAtoms(mask=mask) print(mask) for image in images: # Let all images use an EMT calculator: image.calc = EMT() image.set_constraint(constraint) # Relax the initial and final states: QuasiNewton(initial).run(fmax=0.05) QuasiNewton(final).run(fmax=0.05) # Create a Nudged Elastic Band: neb = NEB(images) # Make a starting guess for the minimum energy path (a straight line # from the initial to the final state): neb.interpolate() # Relax the NEB path: minimizer = MDMin(neb) minimizer.run(fmax=0.05) # Write the path to a trajectory: view(images) # 564 meV write('jump2.traj', images)
from ase.structure import molecule from ase.structure import nanotube from ase.optimize import BFGS, MDMin from asap3 import BrennerPotential from asap3.testtools import ReportTest import numpy as np atoms = nanotube(5, 5, length=10) atoms.set_calculator(BrennerPotential()) uc = atoms.get_cell() l0 = uc[2,2] e = -1e100 for eps in np.linspace(0, 0.5, 201): uc[2,2] = l0 * (1 + eps) atoms.set_cell(uc, scale_atoms=True) dyn = MDMin(atoms, logfile=None, dt=0.05) dyn.run(fmax=0.01, steps=100) epot = atoms.get_potential_energy() if epot > e: e = epot print "%.3f %f" % (eps, epot) print "Maximal energy:", e ReportTest("Maximal energy", e, -969.0, 5.0) if abs(e - -620.98) < 1.0: print "Bug 42 is back!"
em = a.calc.get_potential_energy(a, force_consistent=True) s = (ep - em) / 2 / deps / vol print(v, s, abs(s - sigma_vv[v, v])) assert abs(s - sigma_vv[v, v]) < 1e-7 for v1 in range(3): v2 = (v1 + 1) % 3 x = np.eye(3) x[v1, v2] = deps x[v2, v1] = deps a.set_cell(np.dot(cell, x), scale_atoms=True) ep = a.calc.get_potential_energy(a, force_consistent=True) x[v1, v2] = -deps x[v2, v1] = -deps a.set_cell(np.dot(cell, x), scale_atoms=True) em = a.calc.get_potential_energy(a, force_consistent=True) s = (ep - em) / deps / 4 / vol print(v1, v2, s, abs(s - sigma_vv[v1, v2])) assert abs(s - sigma_vv[v1, v2]) < 1e-7 opt = MDMin(UnitCellFilter(a), dt=0.01) opt.run(fmax=0.5) print(a.cell) for i in range(3): for j in range(3): x = np.dot(a.cell[i], a.cell[j]) y = (i + 1) * (j + 1) * a0**2 / 2 if i != j: y /= 2 print(i, j, x, (x - y) / x) assert abs((x - y) / x) < 0.01
scale_atoms=True) a *= (1, 2, 3) cell0 *= np.array([1, 2, 3])[:, np.newaxis] a.rattle() # Verify analytical stress tensor against numerical value s_analytical = a.get_stress() s_numerical = a.calc.calculate_numerical_stress(a, 1e-5) s_p_err = 100 * (s_numerical - s_analytical) / s_numerical print("Analytical stress:\n", s_analytical) print("Numerical stress:\n", s_numerical) print("Percent error in stress:\n", s_p_err) assert np.all(abs(s_p_err) < 1e-5) # Minimize unit cell opt = MDMin(UnitCellFilter(a), dt=0.01) opt.run(fmax=1e-3) # Verify minimized unit cell using Niggli tensors g_minimized = np.dot(a.cell, a.cell.T) g_theory = np.dot(cell0, cell0.T) g_p_err = 100 * (g_minimized - g_theory) / g_theory print("Minimized Niggli tensor:\n", g_minimized) print("Theoretical Niggli tensor:\n", g_theory) print("Percent error in Niggli tensor:\n", g_p_err) assert np.all(abs(g_p_err) < 1)
def main(): """Adopted from ase/test/stress.py""" if "ASE_CP2K_COMMAND" not in os.environ: raise NotAvailable('$ASE_CP2K_COMMAND not defined') # setup a Fist Lennard-Jones Potential inp = """&FORCE_EVAL &MM &FORCEFIELD &SPLINE EMAX_ACCURACY 500.0 EMAX_SPLINE 1000.0 EPS_SPLINE 1.0E-9 &END &NONBONDED &LENNARD-JONES atoms Ar Ar EPSILON [eV] 1.0 SIGMA [angstrom] 1.0 RCUT [angstrom] 10.0 &END LENNARD-JONES &END NONBONDED &CHARGE ATOM Ar CHARGE 0.0 &END CHARGE &END FORCEFIELD &POISSON &EWALD EWALD_TYPE none &END EWALD &END POISSON &END MM &END FORCE_EVAL""" calc = CP2K(label="test_stress", inp=inp, force_eval_method="Fist") vol0 = 4 * 0.91615977036 # theoretical minimum a0 = vol0**(1 / 3) a = bulk('Ar', 'fcc', a=a0) a.calc = calc a.set_cell(np.dot(a.cell, [[1.02, 0, 0.03], [0, 0.99, -0.02], [0.1, -0.01, 1.03]]), scale_atoms=True) a *= (1, 2, 3) a.rattle() sigma_vv = a.get_stress(voigt=False) # print(sigma_vv) # print(a.get_potential_energy() / len(a)) vol = a.get_volume() # compare stress tensor with numeric derivative deps = 1e-5 cell = a.cell.copy() for v in range(3): x = np.eye(3) x[v, v] += deps a.set_cell(np.dot(cell, x), scale_atoms=True) ep = a.calc.get_potential_energy(a, force_consistent=True) x[v, v] -= 2 * deps a.set_cell(np.dot(cell, x), scale_atoms=True) em = a.calc.get_potential_energy(a, force_consistent=True) s = (ep - em) / 2 / deps / vol # print(v, s, abs(s - sigma_vv[v, v])) assert abs(s - sigma_vv[v, v]) < 1e-7 for v1 in range(3): v2 = (v1 + 1) % 3 x = np.eye(3) x[v1, v2] = deps x[v2, v1] = deps a.set_cell(np.dot(cell, x), scale_atoms=True) ep = a.calc.get_potential_energy(a, force_consistent=True) x[v1, v2] = -deps x[v2, v1] = -deps a.set_cell(np.dot(cell, x), scale_atoms=True) em = a.calc.get_potential_energy(a, force_consistent=True) s = (ep - em) / deps / 4 / vol # print(v1, v2, s, abs(s - sigma_vv[v1, v2])) assert abs(s - sigma_vv[v1, v2]) < 1e-7 # run a cell optimization, see if it finds back original crystal structure opt = MDMin(UnitCellFilter(a), dt=0.01, logfile=None) opt.run(fmax=0.5) # print(a.cell) for i in range(3): for j in range(3): x = np.dot(a.cell[i], a.cell[j]) y = (i + 1) * (j + 1) * a0**2 / 2 if i != j: y /= 2 # print(i, j, x, (x - y) / x) assert abs((x - y) / x) < 0.01 print('passed test "stress"')
ase_calculator = copy.deepcopy(ase_calculator) images_ase = [initial_ase] for i in range(1, n_images-1): image = initial_ase.copy() image.set_calculator(copy.deepcopy(ase_calculator)) images_ase.append(image) images_ase.append(final_ase) neb_ase = NEB(images_ase, climb=True) neb_ase.interpolate(method='idpp') qn_ase = MDMin(neb_ase, trajectory='neb_ase.traj') qn_ase.run(fmax=0.05) # 2.B. NEB using CatLearn neb_catlearn = MLNEB(start='initial.traj', end='final.traj', ase_calc=copy.deepcopy(ase_calculator), n_images=n_images, interpolation='idpp', restart=False) neb_catlearn.run(fmax=0.05, trajectory='ML-NEB.traj') # 3. Summary of the results # NEB ASE: print('\nSummary of the results: \n')
def main(): """Adopted from ase/test/stress.py""" if "ASE_CP2K_COMMAND" not in os.environ: raise NotAvailable('$ASE_CP2K_COMMAND not defined') # setup a Fist Lennard-Jones Potential inp = """&FORCE_EVAL &MM &FORCEFIELD &SPLINE EMAX_ACCURACY 500.0 EMAX_SPLINE 1000.0 EPS_SPLINE 1.0E-9 &END &NONBONDED &LENNARD-JONES atoms Ar Ar EPSILON [eV] 1.0 SIGMA [angstrom] 1.0 RCUT [angstrom] 10.0 &END LENNARD-JONES &END NONBONDED &CHARGE ATOM Ar CHARGE 0.0 &END CHARGE &END FORCEFIELD &POISSON &EWALD EWALD_TYPE none &END EWALD &END POISSON &END MM &END FORCE_EVAL""" calc = CP2K(label="test_stress", inp=inp, force_eval_method="Fist") # Theoretical infinite-cutoff LJ FCC unit cell parameters vol0 = 4 * 0.91615977036 # theoretical minimum a0 = vol0 ** (1 / 3) a = bulk('Ar', 'fcc', a=a0) cell0 = a.get_cell() a.calc = calc a.set_cell(np.dot(a.cell, [[1.02, 0, 0.03], [0, 0.99, -0.02], [0.1, -0.01, 1.03]]), scale_atoms=True) a *= (1, 2, 3) cell0 *= np.array([1, 2, 3])[:, np.newaxis] a.rattle() # Verify analytical stress tensor against numerical value s_analytical = a.get_stress() s_numerical = a.calc.calculate_numerical_stress(a, 1e-5) s_p_err = 100 * (s_numerical - s_analytical) / s_numerical print("Analytical stress:\n", s_analytical) print("Numerical stress:\n", s_numerical) print("Percent error in stress:\n", s_p_err) assert np.all(abs(s_p_err) < 1e-5) # Minimize unit cell opt = MDMin(UnitCellFilter(a), dt=0.01) opt.run(fmax=1e-3) # Verify minimized unit cell using Niggli tensors g_minimized = np.dot(a.cell, a.cell.T) g_theory = np.dot(cell0, cell0.T) g_p_err = 100 * (g_minimized - g_theory) / g_theory print("Minimized Niggli tensor:\n", g_minimized) print("Theoretical Niggli tensor:\n", g_theory) print("Percent error in Niggli tensor:\n", g_p_err) assert np.all(abs(g_p_err) < 1) print('passed test "stress"')
images = [Atoms(initial)] images += [Atoms(initial.copy()) for i in range(n_images)] images += [Atoms(final)] neb = NEB(images, k=0.1) neb.interpolate() for img in images: img.set_calculator(model.calculator) ase.io.write(sys.stdout, img, 'extxyz') # perturb intermediate images for img in images[1:-1]: img.rattle(0.05) # optimizer = FIRE(neb) optimizer = MDMin(neb, dt=0.05) optimizer.run(fmax=fmax) neb_traj = open('model-' + model.name + '-vacancy-path.neb.xyz', 'w') ase.io.write(neb_traj, neb.images, format='extxyz') neb_traj.close() Es = [] for img in images: Es.append(img.get_potential_energy() - e_bulk_per_atom * len(img)) ase.io.write(sys.stdout, img, 'extxyz') # dictionary of computed properties - this is output of this test, to # be compared with other models properties = {'vacancy_path': Es}
temp.translate([x * a, y * a, z * a]) for i in range(4): print((x, y, z, i)) initial.append(temp[i]) tempPos = initial[0].position del initial[0] final = initial.copy() final[1].position = tempPos #QuasiNewton(initial).run(fmax=0.05) #QuasiNewton(final).run(fmax=0.05) images = [initial] images += [initial.copy() for i in range(3)] images += [final] neb = NEB(images) neb.interpolate() for image in images[1:4]: image.set_calculator(calc) print("its go time") optimizer = MDMin(neb, trajectory='neb.traj') optimizer.run(fmax=0.04) time = datetime.now().time() print(time)