def test_constraint_and_momenta(): a = Atoms('H2', positions=[(0, 0, 0), (0, 0, 1)], momenta=[(1, 0, 0), (0, 0, 0)]) a.constraints = [FixBondLength(0, 1)] with Trajectory('constraint.traj', 'w', a) as t: t.write() b = read('constraint.traj') assert not (b.get_momenta() - a.get_momenta()).any()
def _set_leaving_group(self, mol, neighbors_indexes): ''' Manually set the molecule leaving group from the ASE GUI, imposing a constraint on the desired atom. ''' if self.leaving_group_index is None: from ase import Atoms from ase.gui.gui import GUI from ase.gui.images import Images atoms = Atoms(mol.atomnos, positions=mol.atomcoords[0]) while True: print(('\nPlease, manually select the leaving group atom for molecule %s' '\nbonded to the sp3 reactive atom with index %s.' '\nRotate with right click and select atoms by clicking.' '\nThen go to Tools -> Constraints -> Constrain, and close the GUI.') % (mol.name, self.index)) GUI(images=Images([atoms]), show_bonds=True).run() if atoms.constraints != []: if len(list(atoms.constraints[0].get_indices())) == 1: if list(atoms.constraints[0].get_indices())[0] in neighbors_indexes: self.leaving_group_index = list(atoms.constraints[0].get_indices())[0] break else: print('\nSeems that the atom you selected is not bonded to the reactive center or is the reactive atom itself.\nThis is probably an error, please try again.') atoms.constraints = [] else: print('\nPlease only select one leaving group atom.') atoms.constraints = [] return self.others[neighbors_indexes.index(self.leaving_group_index)]
def test_fixatoms(): """Test Atoms.__delitem__ with FixAtoms constraint.""" from ase import Atoms from ase.constraints import FixAtoms for i, j in [(slice(0, -1), None), (slice(0, 1), [0]), (slice(0, None), None), (0, [0]), (1, [0]), (2, [0, 1]), (-1, [0, 1])]: a = Atoms('H3') a.constraints = FixAtoms(indices=[0, 1]) del a[i] print(i, j, a.constraints) if j is None: assert len(a.constraints) == 0 else: assert (a.constraints[0].index == j).all()
def test_fixbonds(): """Test Atoms.__delitem__ with FixAtoms constraint.""" from ase import Atoms from ase.constraints import FixBondLengths a = Atoms('H3') a.constraints = FixBondLengths([(1, 2)]) assert (a[:].constraints[0].pairs == [(1, 2)]).all() assert (a[1:].constraints[0].pairs == [(0, 1)]).all() assert len(a[2:].constraints) == 0 assert len(a[1:2].constraints) == 0 assert len(a[:2].constraints) == 0 assert len(a[:1].constraints) == 0 # Execise Atoms.__init__: Atoms(a)
def write(): """Run this with an old version of ASE. Did it with 3.18.1. """ a1 = Atoms('H') a1.constraints = FixAtoms(indices=[0]) a2 = Atoms('HLi', cell=[1, 2, 3, 90, 80, 70], pbc=True) with Trajectory('old.traj', 'w') as traj: traj.write(a1) traj.write(a2) b = Path('old.traj').read_bytes() data = b64encode(b) print('data = {!r} # noqa'.format(data))
masses = atoms.get_masses() masses[::3] = m_me atoms.set_masses(masses) # Determine side length of a box with the density of acetonitrile at 298 K d = 0.776 / 1e24 # Density in g/Ang3 (https://pubs.acs.org/doi/10.1021/je00001a006) L = ((masses.sum() / units.mol) / d)**(1 / 3.) # Set up box of 27 acetonitrile molecules atoms.set_cell((L, L, L)) atoms.center() atoms = atoms.repeat((3, 3, 3)) atoms.set_pbc(True) # Set constraints for rigid triatomic molecules nm = 27 atoms.constraints = FixLinearTriatomic(triples=[(3 * i, 3 * i + 1, 3 * i + 2) for i in range(nm)]) tag = 'acn_27mol_300K' atoms.calc = ACN(rc=np.min(np.diag(atoms.cell)) / 2) # Create Langevin object md = Langevin(atoms, 1 * units.fs, temperature=300 * units.kB, friction=0.01, logfile=tag + '.log') traj = Trajectory(tag + '.traj', 'w', atoms) md.attach(traj.write, interval=1) md.run(5000)
def test_trajectory(): import pytest import os from ase import Atom, Atoms from ase.io import Trajectory, read from ase.constraints import FixBondLength from ase.calculators.calculator import PropertyNotImplementedError co = Atoms([Atom('C', (0, 0, 0)), Atom('O', (0, 0, 1.2))]) traj = Trajectory('1.traj', 'w', co) written = [] for i in range(5): co.positions[:, 2] += 0.1 traj.write() written.append(co.copy()) traj = Trajectory('1.traj', 'a') co = read('1.traj') print(co.positions) co.positions[:] += 1 traj.write(co) written.append(co.copy()) for a in Trajectory('1.traj'): print(1, a.positions[-1, 2]) co.positions[:] += 1 t = Trajectory('1.traj', 'a') t.write(co) written.append(co.copy()) assert len(t) == 7 co[0].number = 1 t.write(co) written.append(co.copy()) co[0].number = 6 co.pbc = True t.write(co) written.append(co.copy()) co.pbc = False o = co.pop(1) t.write(co) written.append(co.copy()) co.append(o) t.write(co) written.append(co.copy()) imgs = read('1.traj', index=':') assert len(imgs) == len(written) for img1, img2 in zip(imgs, written): assert img1 == img2 # Verify slicing works. read_traj = Trajectory('1.traj', 'r') sliced_traj = read_traj[3:8] assert len(sliced_traj) == 5 sliced_again = sliced_traj[1:-1] assert len(sliced_again) == 3 assert sliced_traj[1] == sliced_again[0] # append to a nonexisting file: fname = '2.traj' if os.path.isfile(fname): os.remove(fname) t = Trajectory(fname, 'a', co) t.close() os.remove(fname) t = Trajectory('empty.traj', 'w') t.close() t = Trajectory('empty.traj', 'r') assert len(t) == 0 t = Trajectory('fake.traj', 'w') t.write(Atoms('H'), energy=-42.0, forces=[[1, 2, 3]]) t = Trajectory('only-energy.traj', 'w', properties=['energy']) a = read('fake.traj') t.write(a) b = read('only-energy.traj') e = b.get_potential_energy() assert e + 42 == 0 with pytest.raises(PropertyNotImplementedError): b.get_forces() # Make sure constraints play well with momenta: a = Atoms('H2', positions=[(0, 0, 0), (0, 0, 1)], momenta=[(1, 0, 0), (0, 0, 0)]) a.constraints = [FixBondLength(0, 1)] t = Trajectory('constraint.traj', 'w', a) t.write() b = read('constraint.traj') assert not (b.get_momenta() - a.get_momenta()).any()
def test_qmmm_tip4p(): from math import cos, sin, pi import numpy as np #import matplotlib.pyplot as plt import ase.units as units from ase import Atoms from ase.calculators.tip4p import TIP4P, epsilon0, sigma0, rOH, angleHOH from ase.calculators.qmmm import (SimpleQMMM, EIQMMM, LJInteractions, LJInteractionsGeneral) from ase.constraints import FixInternals from ase.optimize import BFGS r = rOH a = angleHOH * pi / 180 # From https://doi.org/10.1063/1.445869 eexp = 6.24 * units.kcal / units.mol dexp = 2.75 aexp = 46 D = np.linspace(2.5, 3.5, 30) i = LJInteractions({('O', 'O'): (epsilon0, sigma0)}) # General LJ interaction object sigma_mm = np.array([sigma0, 0, 0]) epsilon_mm = np.array([epsilon0, 0, 0]) sigma_qm = np.array([sigma0, 0, 0]) epsilon_qm = np.array([epsilon0, 0, 0]) ig = LJInteractionsGeneral(sigma_qm, epsilon_qm, sigma_mm, epsilon_mm, 3) for calc in [ TIP4P(), SimpleQMMM([0, 1, 2], TIP4P(), TIP4P(), TIP4P()), SimpleQMMM([0, 1, 2], TIP4P(), TIP4P(), TIP4P(), vacuum=3.0), EIQMMM([0, 1, 2], TIP4P(), TIP4P(), i), EIQMMM([3, 4, 5], TIP4P(), TIP4P(), i, vacuum=3.0), EIQMMM([0, 1, 2], TIP4P(), TIP4P(), i, vacuum=3.0), EIQMMM([0, 1, 2], TIP4P(), TIP4P(), ig), EIQMMM([3, 4, 5], TIP4P(), TIP4P(), ig, vacuum=3.0), EIQMMM([0, 1, 2], TIP4P(), TIP4P(), ig, vacuum=3.0) ]: dimer = Atoms('OH2OH2', [(0, 0, 0), (r * cos(a), 0, r * sin(a)), (r, 0, 0), (0, 0, 0), (r * cos(a / 2), r * sin(a / 2), 0), (r * cos(a / 2), -r * sin(a / 2), 0)]) dimer.calc = calc E = [] F = [] for d in D: dimer.positions[3:, 0] += d - dimer.positions[3, 0] E.append(dimer.get_potential_energy()) F.append(dimer.get_forces()) F = np.array(F) #plt.plot(D, E) F1 = np.polyval(np.polyder(np.polyfit(D, E, 7)), D) F2 = F[:, :3, 0].sum(1) error = abs(F1 - F2).max() assert error < 0.01 dimer.constraints = FixInternals(bonds=[(r, (0, 1)), (r, (0, 2)), (r, (3, 4)), (r, (3, 5))], angles=[(a, (2, 0, 1)), (a, (5, 3, 4))]) opt = BFGS(dimer, maxstep=0.04, trajectory=calc.name + '.traj', logfile=calc.name + 'd.log') opt.run(0.01) e0 = dimer.get_potential_energy() d0 = dimer.get_distance(0, 3) R = dimer.positions v1 = R[2] - R[3] v2 = R[3] - (R[5] + R[4]) / 2 a0 = np.arccos( np.dot(v1, v2) / (np.dot(v1, v1) * np.dot(v2, v2))**0.5) / np.pi * 180 fmt = '{0:>23}: {1:.3f} {2:.3f} {3:.3f} {4:.1f}' print(fmt.format(calc.name, -min(E), -e0, d0, a0)) assert abs(e0 + eexp) < 0.002 assert abs(d0 - dexp) < 0.006 assert abs(a0 - aexp) < 2.5 print(fmt.format('reference', 9.999, eexp, dexp, aexp))
t = Trajectory(fname, 'a', co) t.close() os.remove(fname) t = Trajectory('empty.traj', 'w') t.close() t = Trajectory('empty.traj', 'r') assert len(t) == 0 t = Trajectory('fake.traj', 'w') t.write(Atoms('H'), energy=-42.0, forces=[[1, 2, 3]]) t = Trajectory('only-energy.traj', 'w', properties=['energy']) a = read('fake.traj') t.write(a) b = read('only-energy.traj') e = b.get_potential_energy() assert e + 42 == 0 with must_raise(PropertyNotImplementedError): f = b.get_forces() # Make sure constraints play well with momenta: a = Atoms('H2', positions=[(0, 0, 0), (0, 0, 1)], momenta=[(1, 0, 0), (0, 0, 0)]) a.constraints = [FixBondLength(0, 1)] t = Trajectory('constraint.traj', 'w', a) t.write() b = read('constraint.traj') assert not (b.get_momenta() - a.get_momenta()).any()
F = [] for d in D: dimer.positions[3:, 0] += d - dimer.positions[5, 0] E.append(dimer.get_potential_energy()) F.append(dimer.get_forces()) F = np.array(F) # plt.plot(D, E) F1 = np.polyval(np.polyder(np.polyfit(D, E, 7)), D) F2 = F[:, :3, 0].sum(1) error = abs(F1 - F2).max() dimer.constraints = FixInternals( bonds=[(r, (0, 2)), (r, (1, 2)), (r, (3, 5)), (r, (4, 5))], angles=[(a, (0, 2, 1)), (a, (3, 5, 4))]) opt = BFGS(dimer, trajectory=calc.name + '.traj', logfile=calc.name + 'd.log') opt.run(0.01) e0 = dimer.get_potential_energy() d0 = dimer.get_distance(2, 5) R = dimer.positions v1 = R[1] - R[5] v2 = R[5] - (R[3] + R[4]) / 2 a0 = np.arccos(np.dot(v1, v2) / (np.dot(v1, v1) * np.dot(v2, v2))**0.5) / np.pi * 180 fmt = '{0:>20}: {1:.3f} {2:.3f} {3:.3f} {4:.1f}' print(fmt.format(calc.name, -min(E), -e0, d0, a0)) assert abs(e0 + eexp) < 0.002
from ase.optimize.precon import PreconLBFGS, Exp from gpaw import GPAW r = rOH a = angleHOH / 180 * pi interaction = LJInteractions({('O', 'O'): (epsilon0, sigma0)}) for selection in [[0, 1, 2], [3, 4, 5]]: name = ''.join(str(i) for i in selection) dimer = Atoms('OH2OH2', [(0, 0, 0), (-r * cos(a / 2), r * sin(a / 2), 0), (-r * cos(a / 2), -r * sin(a / 2), 0), (0, 0, 0), (-r * cos(a), 0, r * sin(a)), (-r, 0, 0)]) dimer.positions[3:, 0] += 2.8 dimer.constraints = FixBondLengths([ ((selection[i] + 3) % 6, (selection[i - 1] + 3) % 6) for i in range(3) ]) dimer.calc = EIQMMM(selection, GPAW(txt=name + '.txt', h=0.16), TIP4P(), interaction, vacuum=4, embedding=Embedding(rc=0.2, rc2=20, width=1), output=name + '.out') opt = LBFGS(dimer, trajectory=name + '.traj') opt.run(0.02) monomer = dimer[selection] monomer.center(vacuum=4) monomer.calc = GPAW(txt=name + 'M.txt', h=0.16)
F = [] for d in D: dimer.positions[3:, 0] += d - dimer.positions[5, 0] E.append(dimer.get_potential_energy()) F.append(dimer.get_forces()) F = np.array(F) # plt.plot(D, E) F1 = np.polyval(np.polyder(np.polyfit(D, E, 7)), D) F2 = F[:, :3, 0].sum(1) error = abs(F1 - F2).max() dimer.constraints = FixInternals( bonds=[(r, (0, 2)), (r, (1, 2)), (r, (3, 5)), (r, (4, 5))], angles=[(a, (0, 2, 1)), (a, (3, 5, 4))]) opt = GPMin(dimer, trajectory=calc.name + '.traj', logfile=calc.name + 'd.log') opt.run(0.01) e0 = dimer.get_potential_energy() d0 = dimer.get_distance(2, 5) R = dimer.positions v1 = R[1] - R[5] v2 = R[5] - (R[3] + R[4]) / 2 a0 = np.arccos(np.dot(v1, v2) / (np.dot(v1, v1) * np.dot(v2, v2))**0.5) / np.pi * 180 fmt = '{0:>20}: {1:.3f} {2:.3f} {3:.3f} {4:.1f}' print(fmt.format(calc.name, -min(E), -e0, d0, a0)) assert abs(e0 + eexp) < 0.002
"""Test Atoms.__delitem__ with FixAtoms constraint.""" from ase import Atoms from ase.constraints import FixBondLengths a = Atoms('H3') a.constraints = FixBondLengths([(1, 2)]) assert (a[:].constraints[0].pairs == [(1, 2)]).all() assert (a[1:].constraints[0].pairs == [(0, 1)]).all() assert len(a[2:].constraints) == 0 assert len(a[1:2].constraints) == 0 assert len(a[:2].constraints) == 0 assert len(a[:1].constraints) == 0 # Execise Atoms.__init__: Atoms(a)
def test_qmmm(testdir): r = rOH a = angleHOH * pi / 180 # From https://doi.org/10.1063/1.445869 eexp = 6.50 * units.kcal / units.mol dexp = 2.74 aexp = 27 D = np.linspace(2.5, 3.5, 30) i = LJInteractions({('O', 'O'): (epsilon0, sigma0)}) # General LJ interaction object sigma_mm = np.array([0, 0, sigma0]) epsilon_mm = np.array([0, 0, epsilon0]) sigma_qm = np.array([0, 0, sigma0]) epsilon_qm = np.array([0, 0, epsilon0]) ig = LJInteractionsGeneral(sigma_qm, epsilon_qm, sigma_mm, epsilon_mm, 3) for calc in [ TIP3P(), SimpleQMMM([0, 1, 2], TIP3P(), TIP3P(), TIP3P()), SimpleQMMM([0, 1, 2], TIP3P(), TIP3P(), TIP3P(), vacuum=3.0), EIQMMM([0, 1, 2], TIP3P(), TIP3P(), i), EIQMMM([3, 4, 5], TIP3P(), TIP3P(), i, vacuum=3.0), EIQMMM([0, 1, 2], TIP3P(), TIP3P(), i, vacuum=3.0), EIQMMM([0, 1, 2], TIP3P(), TIP3P(), ig), EIQMMM([3, 4, 5], TIP3P(), TIP3P(), ig, vacuum=3.0), EIQMMM([0, 1, 2], TIP3P(), TIP3P(), ig, vacuum=3.0) ]: dimer = Atoms('H2OH2O', [(r * cos(a), 0, r * sin(a)), (r, 0, 0), (0, 0, 0), (r * cos(a / 2), r * sin(a / 2), 0), (r * cos(a / 2), -r * sin(a / 2), 0), (0, 0, 0)]) dimer.calc = calc E = [] F = [] for d in D: dimer.positions[3:, 0] += d - dimer.positions[5, 0] E.append(dimer.get_potential_energy()) F.append(dimer.get_forces()) F = np.array(F) F1 = np.polyval(np.polyder(np.polyfit(D, E, 7)), D) F2 = F[:, :3, 0].sum(1) error = abs(F1 - F2).max() assert error < 0.01 dimer.constraints = FixInternals(bonds=[(r, (0, 2)), (r, (1, 2)), (r, (3, 5)), (r, (4, 5))], angles_deg=[ (np.degrees(a), (0, 2, 1)), (np.degrees(a), (3, 5, 4)) ]) opt = GPMin(dimer, trajectory=calc.name + '.traj', logfile=calc.name + 'd.log') opt.run(0.01) e0 = dimer.get_potential_energy() d0 = dimer.get_distance(2, 5) R = dimer.positions v1 = R[1] - R[5] v2 = R[5] - (R[3] + R[4]) / 2 a0 = np.arccos( np.dot(v1, v2) / (np.dot(v1, v1) * np.dot(v2, v2))**0.5) / np.pi * 180 fmt = '{0:>20}: {1:.3f} {2:.3f} {3:.3f} {4:.1f}' print(fmt.format(calc.name, -min(E), -e0, d0, a0)) assert abs(e0 + eexp) < 0.002 assert abs(d0 - dexp) < 0.01 assert abs(a0 - aexp) < 4 print(fmt.format('reference', 9.999, eexp, dexp, aexp))
if os.path.isfile(fname): os.remove(fname) t = Trajectory(fname, 'a', co) os.remove(fname) t = Trajectory('empty.traj', 'w') t.close() assert os.path.getsize('empty.traj') == 0 t = Trajectory('fake.traj', 'w') t.write(Atoms('H'), energy=-42.0, forces=[[1, 2, 3]]) t = Trajectory('only-energy.traj', 'w', properties=['energy']) a = read('fake.traj') t.write(a) b = read('only-energy.traj') e = b.get_potential_energy() assert e + 42 == 0 with must_raise(NotImplementedError): f = b.get_forces() # Make sure constraints play well with momenta: a = Atoms('H2', positions=[(0, 0, 0), (0, 0, 1)], momenta=[(1, 0, 0), (0, 0, 0)]) a.constraints = [FixBondLength(0, 1)] t = Trajectory('constraint.traj', 'w', a) t.write() b = read('constraint.traj') assert not (b.get_momenta() - a.get_momenta()).any()
# Set up water box at 20 deg C density x = angleHOH * np.pi / 180 / 2 pos = [[0, 0, 0], [0, rOH * np.cos(x), rOH * np.sin(x)], [0, rOH * np.cos(x), -rOH * np.sin(x)]] atoms = Atoms('OH2', positions=pos) vol = ((18.01528 / 6.022140857e23) / (0.9982 / 1e24))**(1 / 3.) atoms.set_cell((vol, vol, vol)) atoms.center() atoms = atoms.repeat((3, 3, 3)) atoms.set_pbc(True) # RATTLE-type constraints on O-H1, O-H2, H1-H2. atoms.constraints = FixBondLengths([(3 * i + j, 3 * i + (j + 1) % 3) for i in range(3**3) for j in [0, 1, 2]]) tag = 'tip3p_27mol_equil' atoms.calc = TIP3P(rc=4.5) md = Langevin(atoms, 1 * units.fs, temperature=300 * units.kB, friction=0.01, logfile=tag + '.log') traj = Trajectory(tag + '.traj', 'w', atoms) md.attach(traj.write, interval=1) md.run(4000) # Repeat box and equilibrate further. tag = 'tip3p_216mol_equil'
return StringIO(fd.getvalue()) def v2(a): """Create new version-2 trajectory.""" fd = StringIO() t = PickleTrajectory(fd, 'w') t.write(a) return StringIO(fd.getvalue()) class MyFixAtoms(FixAtoms): pass a.constraints = FixAtoms(indices=[2]) t1 = v1(a) t2 = v2(a) # Read old trajectory: c1 = PickleTrajectory(t1)[0].constraints assert c1[0].index[0] == 2 # Read new trajectory: c2 = PickleTrajectory(t2)[0].constraints assert c2[0].index[0] == 2 a.constraints = MyFixAtoms(indices=[1]) t3 = v2(a) # Read new trajectory with missing constraint class. This should
def test_turbomole_qmmm(): """Test the Turbomole calculator in simple QMMM and explicit interaction QMMM simulations.""" r = rOH a = angleHOH * pi / 180 D = np.linspace(2.5, 3.5, 30) interaction = LJInteractions({('O', 'O'): (epsilon0, sigma0)}) qm_par = {'esp fit': 'kollman', 'multiplicity': 1} for calc in [ TIP3P(), SimpleQMMM([0, 1, 2], Turbomole(**qm_par), TIP3P(), TIP3P()), SimpleQMMM([0, 1, 2], Turbomole(**qm_par), TIP3P(), TIP3P(), vacuum=3.0), EIQMMM([0, 1, 2], Turbomole(**qm_par), TIP3P(), interaction), EIQMMM([3, 4, 5], Turbomole(**qm_par), TIP3P(), interaction, vacuum=3.0), EIQMMM([0, 1, 2], Turbomole(**qm_par), TIP3P(), interaction, vacuum=3.0) ]: dimer = Atoms('H2OH2O', [(r * cos(a), 0, r * sin(a)), (r, 0, 0), (0, 0, 0), (r * cos(a / 2), r * sin(a / 2), 0), (r * cos(a / 2), -r * sin(a / 2), 0), (0, 0, 0)]) dimer.calc = calc E = [] F = [] for d in D: dimer.positions[3:, 0] += d - dimer.positions[5, 0] E.append(dimer.get_potential_energy()) F.append(dimer.get_forces()) F = np.array(F) # plt.plot(D, E) F1 = np.polyval(np.polyder(np.polyfit(D, E, 7)), D) F2 = F[:, :3, 0].sum(1) error = abs(F1 - F2).max() assert error < 0.01 dimer.constraints = FixInternals(bonds=[(r, (0, 2)), (r, (1, 2)), (r, (3, 5)), (r, (4, 5))], angles=[(a, (0, 2, 1)), (a, (3, 5, 4))]) opt = BFGS(dimer, trajectory=calc.name + '.traj', logfile=calc.name + 'd.log') opt.run(0.01) e0 = dimer.get_potential_energy() d0 = dimer.get_distance(2, 5) R = dimer.positions v1 = R[1] - R[5] v2 = R[5] - (R[3] + R[4]) / 2 a0 = np.arccos( np.dot(v1, v2) / (np.dot(v1, v1) * np.dot(v2, v2))**0.5) / np.pi * 180 fmt = '{0:>20}: {1:.3f} {2:.3f} {3:.3f} {4:.1f}' print(fmt.format(calc.name, -min(E), -e0, d0, a0))
E = [] F = [] for d in D: dimer.positions[3:, 0] += d - dimer.positions[3, 0] E.append(dimer.get_potential_energy()) F.append(dimer.get_forces()) F = np.array(F) # plt.plot(D, E) F1 = np.polyval(np.polyder(np.polyfit(D, E, 7)), D) F2 = F[:, :3, 0].sum(1) error = abs(F1 - F2).max() dimer.constraints = FixBondLengths([(3 * i + j, 3 * i + (j + 1) % 3) for i in range(2) for j in [0, 1, 2]]) opt = BFGS(dimer, trajectory=calc.name + '.traj', logfile=calc.name + 'd.log') opt.run(0.001) if calc.name == 'tip4p': # save optimized geom for EIQMMM test tip4pdimer = dimer.copy() e0 = dimer.get_potential_energy() d0 = dimer.get_distance(0, 3) R = dimer.positions v1 = R[2] - R[3] v2 = R[3] - (R[4] + R[5]) / 2 a0 = np.arccos(np.dot(v1, v2) / (np.dot(v1, v1) * np.dot(v2, v2))**0.5) / np.pi * 180
return BytesIO(fd.getvalue()) def v2(a): """Create new version-2 trajectory.""" fd = BytesIO() t = Trajectory(fd, 'w') t.write(a) return BytesIO(fd.getvalue()) class MyFixAtoms(FixAtoms): pass a.constraints = FixAtoms(indices=[2]) t1 = v1(a) t2 = v2(a) # Read old trajectory: c1 = Trajectory(t1)[0].constraints assert c1[0].index[0] == 2 # Read new trajectory: c2 = Trajectory(t2)[0].constraints assert c2[0].index[0] == 2 a.constraints = MyFixAtoms(indices=[1]) t3 = v2(a) # Read new trajectory with missing constraint class. This should
F = [] for d in D: dimer.positions[3:, 0] += d - dimer.positions[3, 0] E.append(dimer.get_potential_energy()) F.append(dimer.get_forces()) F = np.array(F) #plt.plot(D, E) F1 = np.polyval(np.polyder(np.polyfit(D, E, 7)), D) F2 = F[:, :3, 0].sum(1) error = abs(F1 - F2).max() dimer.constraints = FixInternals(bonds=[(r, (0, 1)), (r, (0, 2)), (r, (3, 4)), (r, (3, 5))], angles=[(a, (2, 0, 1)), (a, (5, 3, 4))]) opt = BFGS(dimer, trajectory=calc.name + '.traj', logfile=calc.name + 'd.log') opt.run(0.01) e0 = dimer.get_potential_energy() d0 = dimer.get_distance(0, 3) R = dimer.positions v1 = R[2] - R[3] v2 = R[3] - (R[5] + R[4]) / 2 a0 = np.arccos(np.dot(v1, v2) / (np.dot(v1, v1) * np.dot(v2, v2))**0.5) / np.pi * 180 fmt = '{0:>23}: {1:.3f} {2:.3f} {3:.3f} {4:.1f}' print(fmt.format(calc.name, -min(E), -e0, d0, a0))
F = [] for d in D: dimer.positions[3:, 0] += d - dimer.positions[3, 0] E.append(dimer.get_potential_energy()) F.append(dimer.get_forces()) F = np.array(F) # plt.plot(D, E) F1 = np.polyval(np.polyder(np.polyfit(D, E, 7)), D) F2 = F[:, :3, 0].sum(1) error = abs(F1 - F2).max() dimer.constraints = FixBondLengths([(3 * i + j, 3 * i + (j + 1) % 3) for i in range(2) for j in [0, 1, 2]]) opt = BFGS(dimer, trajectory=calc.name + '.traj', logfile=calc.name + 'd.log') opt.run(0.001) if calc.name == 'tip4p': # save optimized geom for EIQMMM test tip4pdimer = dimer.copy() e0 = dimer.get_potential_energy() d0 = dimer.get_distance(0, 3) R = dimer.positions v1 = R[2] - R[3] v2 = R[3] - (R[4] + R[5]) / 2 a0 = np.arccos(np.dot(v1, v2) / (np.dot(v1, v1) * np.dot(v2, v2))**0.5) / np.pi * 180
"""Test Atoms.__delitem__ with FixAtoms constraint.""" from ase import Atoms from ase.constraints import FixAtoms for i, j in [(slice(0, -1), None), (slice(0, 1), [0]), (slice(0, None), None), (0, [0]), (1, [0]), (2, [0, 1]), (-1, [0, 1])]: a = Atoms('H3') a.constraints = FixAtoms(indices=[0, 1]) del a[i] print(i, j, a.constraints) if j is None: assert len(a.constraints) == 0 else: assert (a.constraints[0].index == j).all()