def test_water(): from ase.calculators.gaussian import Gaussian from ase.atoms import Atoms from ase.optimize.lbfgs import LBFGS from ase.io import read # First test to make sure Gaussian works calc = Gaussian(xc='pbe', chk='water.chk', label='water') calc.clean() water = Atoms('OHH', positions=[(0, 0, 0), (1, 0, 0), (0, 1, 0)], calculator=calc) opt = LBFGS(water) opt.run(fmax=0.05) forces = water.get_forces() energy = water.get_potential_energy() positions = water.get_positions() # Then test the IO routines water2 = read('water.log') forces2 = water2.get_forces() energy2 = water2.get_potential_energy() positions2 = water2.get_positions() # Compare distances since positions are different in standard orientation. dist = water.get_all_distances() dist2 = read('water.log', index=-1).get_all_distances() assert abs(energy - energy2) < 1e-7 assert abs(forces - forces2).max() < 1e-9 assert abs(positions - positions2).max() < 1e-6 assert abs(dist - dist2).max() < 1e-6
def __init__(self, atoms: Atoms, oszicar: Oszicar, name: str=None, time: float=None): if name is None: self.name = str(self.atoms) else: self.name = name self.time = time self.atoms = atoms self.potential_energy = atoms.get_potential_energy() self.kinetic_energy = atoms.get_kinetic_energy() self.total_energy = atoms.get_total_energy() self.temperature = atoms.get_temperature() # self.magmom = atoms.get_magnetic_moment() self.elements = Counter(atoms.get_chemical_symbols()) self.set_area() self.set_oszicar(oszicar) n = len(atoms) if self.get('F'): self.F_n = self.F / n else: self.F_n = self.potential_energy / n
def test_build_diag(self): direction = np.array([1., 2., 3.]) direction /= np.linalg.norm(direction) r_train = np.linspace(0.5, 7, 11) energies_train = [] images_train = [] for ri in r_train: image = Atoms(['C', 'O'], positions=np.array( [-0.5 * ri * direction, 0.5 * ri * direction])) image.set_calculator(EMT()) energies_train.append(image.get_potential_energy()) images_train.append(image) cut = 6.5 with SymmetryFunctionSet(["C", "O"], cutoff=cut) as sfs: sfs.add_Artrith_Kolpak_set() kernel = RBFKernel(constant=100.0, length_scale=1e-2) calc = GAPCalculator(descriptor_set=sfs, kernel=kernel, C1=1e8, C2=1e8, opt_restarts=0) [calc.add_data(im) for im in images_train] calc.fit() np.testing.assert_allclose( calc.build_kernel_diagonal((calc.Gs_norm, calc.dGs_norm)), np.diag(calc.build_kernel_matrix()))
def test_gfn2xtb_velocityverlet(): """Perform molecular dynamics with GFN2-xTB and Velocity Verlet Integrator""" thr = 1.0e-5 atoms = Atoms( symbols="NHCHC2H3OC2H3ONHCH3", positions=np.array([ [1.40704587284727, -1.26605342016611, -1.93713466561923], [1.85007200612454, -0.46824072777417, -1.50918242392545], [-0.03362432532150, -1.39269245193812, -1.74003582081606], [-0.56857009928108, -1.01764444489068, -2.61263467107342], [-0.44096297340282, -2.84337808903410, -1.48899734014499], [-0.47991761226058, -0.55230954385212, -0.55520222968656], [-1.51566045903090, -2.89187354810876, -1.32273881320610], [-0.18116520746778, -3.45187805987944, -2.34920431470368], [0.06989722340461, -3.23298998903001, -0.60872832703814], [-1.56668253918793, 0.00552120970194, -0.52884675001441], [1.99245341064342, -1.73097165236442, -3.08869239114486], [3.42884244212567, -1.30660069291348, -3.28712665743189], [3.87721962540768, -0.88843123009431, -2.38921453037869], [3.46548545761151, -0.56495308290988, -4.08311788302584], [4.00253374168514, -2.16970938132208, -3.61210068365649], [1.40187968630565, -2.43826111827818, -3.89034127398078], [0.40869198386066, -0.49101709352090, 0.47992424955574], [1.15591901335007, -1.16524842262351, 0.48740266650199], [0.00723492494701, 0.11692276177442, 1.73426297572793], [0.88822128447468, 0.28499001838229, 2.34645658013686], [-0.47231557768357, 1.06737634000561, 1.52286682546986], [-0.70199987915174, -0.50485938116399, 2.28058247845421], ]), ) calc = XTB(method="GFN2-xTB", cache_api=False) atoms.set_calculator(calc) dyn = VelocityVerlet(atoms, timestep=1.0 * fs) dyn.run(20) assert approx(atoms.get_potential_energy(), thr) == -896.9772346260584 assert approx(atoms.get_kinetic_energy(), thr) == 0.022411127028842362 atoms.calc.set(cache_api=True) dyn.run(20) assert approx(atoms.get_potential_energy(), thr) == -896.9913862530841 assert approx(atoms.get_kinetic_energy(), thr) == 0.036580471363852810
def test_gfn1_xtb_3d(): """Test ASE interface to GFN1-xTB""" thr = 5.0e-6 atoms = Atoms( symbols='C4O8', positions=np.array([ [0.9441259872, 0.9437851680, 0.9543505632], [3.7179966528, 0.9556570368, 3.7316862240], [3.7159517376, 3.7149292800, 0.9692330016], [0.9529872864, 3.7220864832, 3.7296981120], [1.6213905408, 1.6190616096, 1.6313879040], [0.2656685664, 0.2694175776, 0.2776540416], [4.3914553920, 1.6346256864, 3.0545920000], [3.0440834880, 0.2764611744, 4.4080419264], [4.3910577696, 3.0416409504, 0.2881058304], [3.0399936576, 4.3879335936, 1.6497353376], [0.2741322432, 4.4003734944, 3.0573754368], [1.6312174944, 3.0434586528, 4.4023048032], ]), cell=np.array([5.68032, 5.68032, 5.68032]), pbc=np.array([True, True, True]), ) forces = np.array([ [0.05008078, 0.06731033, 0.06324782], [-0.03885473, 0.07550136, -0.06667888], [-0.06455676, -0.04199831, 0.06908718], [0.04672903, -0.06303119, -0.06002863], [-1.9460667, -1.94514641, -1.94923488], [1.92953942, 1.91109506, 1.92038457], [-1.91269913, -1.95500822, 1.94675148], [1.94009239, 1.91238163, -1.93489981], [-1.90757165, 1.94211445, 1.94655816], [1.94283273, -1.90965163, -1.95863335], [1.91207771, -1.94256232, 1.9337591], [-1.95160309, 1.94899525, -1.91031277], ]) charges = np.array([ 0.74256902, 0.74308482, 0.74305612, 0.74300613, -0.37010244, -0.37234708, -0.37134504, -0.37177066, -0.37176288, -0.37133667, -0.37178059, -0.37127074, ]) calc = XTB(method="GFN1-xTB") atoms.set_calculator(calc) assert atoms.pbc.all() assert approx(atoms.get_potential_energy(), abs=thr) == -1256.768167202048 assert approx(atoms.get_forces(), abs=thr) == forces assert approx(atoms.get_charges(), abs=thr) == charges
def test_co_potential_curve(self): direction = np.array([1., 2., 3.]) direction /= np.linalg.norm(direction) r_train = np.linspace(0.5, 7, 11) energies_train = [] images_train = [] for ri in r_train: image = Atoms(['C', 'O'], positions=np.array( [-0.5 * ri * direction, 0.5 * ri * direction])) image.set_calculator(EMT()) energies_train.append(image.get_potential_energy()) images_train.append(image) cut = 6.5 with SymmetryFunctionSet(["C", "O"], cutoff=cut) as sfs: # Parameters from Artrith and Kolpak Nano Lett. 2014, 14, 2670 etas = [0.0009, 0.01, 0.02, 0.035, 0.06, 0.1, 0.2] rss = np.linspace(0, 6.5, 7) for t1 in sfs.atomtypes: for t2 in sfs.atomtypes: for eta, rs in zip(etas, rss): sfs.add_TwoBodySymmetryFunction(t1, t2, 'BehlerG2', [eta, rs], cuttype='cos') ang_etas = [0.0001, 0.003, 0.008] zetas = [1.0, 4.0] for ti in sfs.atomtypes: for (tj, tk) in combinations_with_replacement(sfs.atomtypes, 2): for eta in ang_etas: for lamb in [-1.0, 1.0]: for zeta in zetas: sfs.add_ThreeBodySymmetryFunction( ti, tj, tk, "BehlerG3", [lamb, zeta, eta], cuttype='cos') # TODO: Find solution for setting the random seed. Since # NNCalculator builds a custom graph this does not work: tf.set_random_seed(1234) calc = NNCalculator(descriptor_set=sfs, C1=1e8, C2=1e8, layers=[[5], [5]], model_dir='./.tmp/') [calc.add_data(im) for im in images_train] calc.fit() np.testing.assert_allclose( energies_train, [calc.predict(im)[0] for im in images_train], rtol=1e-3, atol=5e-3)
def test_md(cp2k_factory): calc = cp2k_factory.calc(label='test_H2_MD') positions = [(0, 0, 0), (0, 0, 0.7245595)] atoms = Atoms('HH', positions=positions, calculator=calc) atoms.center(vacuum=2.0) # Run MD MaxwellBoltzmannDistribution(atoms, 0.5 * 300 * units.kB, force_temp=True) energy_start = atoms.get_potential_energy() + atoms.get_kinetic_energy() dyn = VelocityVerlet(atoms, 0.5 * units.fs) #def print_md(): # energy = atoms.get_potential_energy() + atoms.get_kinetic_energy() # print("MD total-energy: %.10feV" % energy) #dyn.attach(print_md, interval=1) dyn.run(20) energy_end = atoms.get_potential_energy() + atoms.get_kinetic_energy() assert energy_start - energy_end < 1e-4 print('passed test "H2_MD"')
def test_gfn1_xtb_3d(): """Test ASE interface to GFN1-xTB""" thr = 5.0e-6 atoms = Atoms( symbols="C4O8", positions=np.array([ [0.9441259872, 0.9437851680, 0.9543505632], [3.7179966528, 0.9556570368, 3.7316862240], [3.7159517376, 3.7149292800, 0.9692330016], [0.9529872864, 3.7220864832, 3.7296981120], [1.6213905408, 1.6190616096, 1.6313879040], [0.2656685664, 0.2694175776, 0.2776540416], [4.3914553920, 1.6346256864, 3.0545920000], [3.0440834880, 0.2764611744, 4.4080419264], [4.3910577696, 3.0416409504, 0.2881058304], [3.0399936576, 4.3879335936, 1.6497353376], [0.2741322432, 4.4003734944, 3.0573754368], [1.6312174944, 3.0434586528, 4.4023048032], ]), cell=np.array([5.68032, 5.68032, 5.68032]), pbc=np.array([True, True, True]), ) forces = np.array([ [-0.08831700, -0.07001294, -0.07468651], [-0.03556765, -0.02242341, +0.03047788], [+0.03228896, -0.03948204, -0.02892694], [-0.02569098, +0.03373080, -0.03161988], [-1.90306812, -1.90236730, -1.90612450], [+1.98861177, +1.96958105, +1.97849378], [-1.88898997, -1.93509024, +1.91692369], [+1.92988257, +1.95061533, -1.94116843], [-1.93844982, +1.93069254, +1.96026641], [+1.91146115, -1.88620597, -1.93901862], [+1.94936349, -1.94760369, +1.92150003], [-1.93152440, +1.91856587, -1.88611692], ]) stress = np.array([ +4.49045792e-02, +4.49168887e-02, +4.49566951e-02, +3.38245641e-05, +1.52117499e-05, +1.13328271e-04, ]) atoms.calc = TBLite(method="GFN1-xTB") assert atoms.pbc.all() assert approx(atoms.get_potential_energy(), abs=thr) == -1257.0801067985549 assert approx(atoms.get_forces(), abs=thr) == forces assert approx(atoms.get_stress(), abs=thr) == stress
def main(): if "ASE_CP2K_COMMAND" not in os.environ: raise NotAvailable('$ASE_CP2K_COMMAND not defined') calc = CP2K(label='test_H2_MD') positions = [(0, 0, 0), (0, 0, 0.7245595)] atoms = Atoms('HH', positions=positions, calculator=calc) atoms.center(vacuum=2.0) # Run MD MaxwellBoltzmannDistribution(atoms, 0.5 * 300 * units.kB, force_temp=True) energy_start = atoms.get_potential_energy() + atoms.get_kinetic_energy() dyn = VelocityVerlet(atoms, 0.5 * units.fs) #def print_md(): # energy = atoms.get_potential_energy() + atoms.get_kinetic_energy() # print("MD total-energy: %.10feV" % energy) #dyn.attach(print_md, interval=1) dyn.run(20) energy_end = atoms.get_potential_energy() + atoms.get_kinetic_energy() assert energy_start - energy_end < 1e-4 print('passed test "H2_MD"')
def test_gfn2_xtb_3d(): """Test ASE interface to GFN2-xTB, should fail""" thr = 5.0e-6 atoms = Atoms( symbols='C4O8', positions=np.array([ [0.9441259872, 0.9437851680, 0.9543505632], [3.7179966528, 0.9556570368, 3.7316862240], [3.7159517376, 3.7149292800, 0.9692330016], [0.9529872864, 3.7220864832, 3.7296981120], [1.6213905408, 1.6190616096, 1.6313879040], [0.2656685664, 0.2694175776, 0.2776540416], [4.3914553920, 1.6346256864, 3.0545920000], [3.0440834880, 0.2764611744, 4.4080419264], [4.3910577696, 3.0416409504, 0.2881058304], [3.0399936576, 4.3879335936, 1.6497353376], [0.2741322432, 4.4003734944, 3.0573754368], [1.6312174944, 3.0434586528, 4.4023048032], ]), cell=np.array([5.68032, 5.68032, 5.68032]), pbc=np.array([True, True, True]), ) calc = XTB(method="GFN2-xTB") atoms.set_calculator(calc) with raises(CalculationFailed): atoms.get_potential_energy() # make structure molecular atoms.set_pbc(False) assert approx(atoms.get_potential_energy(), abs=thr) == -1121.9196707084955 with raises(InputError): atoms.positions = np.zeros((len(atoms), 3)) calc.calculate(atoms=atoms, system_changes=["positions"])
def test_no_data_file_wrap(): """ If 'create_atoms' hasn't been given the appropriate 'remap yes' option, atoms falling outside of a periodic cell are not actually created. The lammpsrun calculator will then look at the thermo output and determine a discrepancy the number of atoms reported compared to the length of the ASE Atoms object and raise a RuntimeError. This problem can only possibly arise when the 'no_data_file' option for the calculator is set to True. Furthermore, note that if atoms fall outside of the box along non-periodic dimensions, create_atoms is going to refuse to create them no matter what, so you simply can't use the 'no_data_file' option if you want to allow for that scenario. """ from ase.atoms import Atoms from ase.calculators.lammpsrun import LAMMPS # Make a periodic box and put one atom outside of it pos = [[0.0, 0.0, 0.0], [-2.0, 0.0, 0.0]] atoms = Atoms(symbols=["Ar"] * 2, positions=pos, cell=[10.0, 10.0, 10.0], pbc=True) # Set parameters for calculator params = {} params["pair_style"] = "lj/cut 8.0" params["pair_coeff"] = ["1 1 0.0108102 3.345"] # Don't write a data file string. This will force # ase.calculators.lammps.inputwriter.write_lammps_in to write a bunch of # 'create_atoms' commands into the LAMMPS input file params["no_data_file"] = True with LAMMPS(specorder=["Ar"], **params) as calc: atoms.calc = calc atoms.get_potential_energy()
def test_gfn1_xtb_0d(): """Test ASE interface to GFN1-xTB""" thr = 1.0e-5 atoms = Atoms( symbols="CHOCH2CH4CH2OH", positions=np.array([ [+1.578385, +0.147690, +0.343809], [+1.394750, +0.012968, +1.413545], [+1.359929, -1.086203, -0.359782], [+0.653845, +1.215099, -0.221322], [+1.057827, +2.180283, +0.093924], [+0.729693, +1.184864, -1.311438], [-0.817334, +1.152127, +0.208156], [-1.303525, +2.065738, -0.145828], [-0.883765, +1.159762, +1.299260], [+1.984120, -1.734446, -0.021385], [+2.616286, +0.458948, +0.206544], [-1.627725, -0.034052, -0.311301], [-2.684229, +0.151015, -0.118566], [-1.501868, -0.118146, -1.397506], [-1.324262, -1.260154, +0.333377], [-0.417651, -1.475314, +0.076637], ]), ) forces = np.array([ [-0.37070590, -0.51067739, -0.27981764], [-0.04339461, -0.09290876, +0.22940156], [+0.11141234, +0.46678720, +0.24552625], [+0.04255709, +0.19019316, -0.23531997], [-0.01897377, +0.10810803, +0.05314982], [-0.07150720, -0.05182148, -0.08413638], [+0.06631826, +0.10587709, +0.29833479], [-0.01062355, +0.02301460, -0.04964730], [+0.06610108, -0.02724994, +0.09234280], [+0.06519070, -0.19311773, -0.01152205], [+0.23879786, +0.09871398, -0.04009526], [-0.04381577, -0.49997745, +0.08672818], [-0.23259608, +0.13735636, +0.06783414], [+0.08297636, -0.09566973, -0.20602954], [-0.23686052, +0.57454371, -0.17194215], [+0.35512370, -0.23317164, +0.00519275], ]) atoms.calc = TBLite(method="GFN1-xTB") assert approx(atoms.get_potential_energy(), abs=thr) == -632.7363734598027 assert approx(atoms.get_forces(), abs=thr) == forces
def test_gfn2_xtb_0d(): """Test ASE interface to GFN2-xTB""" thr = 1.0e-5 atoms = Atoms( symbols="CHOCH2CH4CH2OH", positions=np.array([ [+1.578385, +0.147690, +0.343809], [+1.394750, +0.012968, +1.413545], [+1.359929, -1.086203, -0.359782], [+0.653845, +1.215099, -0.221322], [+1.057827, +2.180283, +0.093924], [+0.729693, +1.184864, -1.311438], [-0.817334, +1.152127, +0.208156], [-1.303525, +2.065738, -0.145828], [-0.883765, +1.159762, +1.299260], [+1.984120, -1.734446, -0.021385], [+2.616286, +0.458948, +0.206544], [-1.627725, -0.034052, -0.311301], [-2.684229, +0.151015, -0.118566], [-1.501868, -0.118146, -1.397506], [-1.324262, -1.260154, +0.333377], [-0.417651, -1.475314, +0.076637], ]), ) forces = np.array([ [-0.28561308, -0.48587499, -0.28392027], [-0.05526371, -0.07403870, +0.09666022], [+0.18830054, +0.39163807, +0.33884887], [-0.01165266, +0.24651648, -0.16778052], [-0.05367208, +0.03368724, +0.05115729], [-0.09605395, -0.10000312, +0.01096077], [+0.09422811, +0.18571407, +0.17796656], [+0.02651446, -0.04073478, -0.03656254], [+0.07886459, -0.05806164, +0.00649897], [-0.00786081, -0.07975814, -0.03179608], [+0.13328259, +0.03208869, -0.04638662], [+0.08197190, -0.39156437, +0.12108031], [-0.11454175, -0.01484728, +0.09974090], [+0.09786247, -0.09131053, -0.05738535], [-0.26638845, +0.47604034, -0.27858358], [+0.19002181, -0.02949135, -0.00049892], ]) calc = TBLite(method="GFN2-xTB", atoms=atoms) assert approx(atoms.get_potential_energy(), abs=thr) == -592.6794366990786 assert approx(atoms.get_forces(), abs=thr) == forces
def test_orca(): from ase.optimize import BFGS from ase.atoms import Atoms from ase.calculators.orca import ORCA atoms = Atoms('OHH', positions=[(0, 0, 0), (1, 0, 0), (0, 1, 0)]) atoms.calc = ORCA(label='water', orcasimpleinput='BLYP def2-SVP') opt = BFGS(atoms) opt.run(fmax=0.05) final_energy = atoms.get_potential_energy() print(final_energy) assert abs(final_energy + 2077.24420) < 1.0
def test_molecule(): from ase.optimize import BFGS from ase.atoms import Atoms from ase.calculators.crystal import CRYSTAL with open('basis', 'w') as fd: fd.write("""6 4 0 0 6 2.0 1.0 3048.0 0.001826 456.4 0.01406 103.7 0.06876 29.23 0.2304 9.349 0.4685 3.189 0.3628 0 1 2 4.0 1.0 3.665 -0.3959 0.2365 0.7705 1.216 0.8606 0 1 1 0.0 1.0 0.26 1.0 1.0 0 3 1 0.0 1.0 0.8 1.0 """) geom = Atoms('OHH', positions=[(0, 0, 0), (1, 0, 0), (0, 1, 0)]) geom.calc = CRYSTAL( label='water', guess=True, basis='sto-3g', xc='PBE', otherkeys=['scfdir', 'anderson', ['maxcycles', '500'], ['toldee', '6'], ['tolinteg', '7 7 7 7 14'], ['fmixing', '90']]) opt = BFGS(geom) opt.run(fmax=0.05) final_energy = geom.get_potential_energy() assert abs(final_energy + 2047.34531091) < 1.0
async def ase_fio_task(cid, calc, atcor, ian, cell=None, pbc=None): """Base task for calculators implementing the `FileIOCalculator`_ interface (e.g. Gaussian, Orca, QE). .. _FileIOCalculator: https://gitlab.com/ase/ase/-/blob/master/ase/calculators/calculator.py Class ``FileIOCalculator`` is patched to enable async calculation through async subprocess. See ``monkey_patch.py`` for details. Args: cid (str): Calculation ID, for time record only calc: The calculator instance atcor (ndarray[(N,3), float]): Atom coordinate ian (ndarray[(N,), int]): Atomic numbers pbc (bool or (bool, bool, bool)): Periodic boundary conditions along each axis. Arg for ``ase.atoms.Atoms`` cell (ndarray[(3,3), float] or see ASE doc): Unit cell size. Arg for ``ase.atoms.Atoms`` Returns: Energy (in hartree) and forces (in hartree/angstrom) .. note:: Any task calling this should make sure that the calculator itself's ``calculate`` method get handled, as this only takes care of ``FileIOCalculator.calculate``. """ from ase.atoms import Atoms from ase.units import Hartree as hart2ev, Bohr as b2a from .util import timeblock mol = Atoms(numbers=ian, positions=atcor, cell=cell, pbc=pbc) mol.calc = calc with timeblock(cid): await mol._calc.acalculate(mol, ['energy', 'forces'], []) return ( mol.get_potential_energy() / hart2ev, mol.get_forces() * b2a / hart2ev, )
def test_gfn2xtb_lbfgs(): """Perform geometry optimization with GFN2-xTB and L-BFGS""" thr = 1.0e-5 atoms = Atoms( symbols="NHCHC2H3OC2H3ONHCH3", positions=np.array([ [1.40704587284727, -1.26605342016611, -1.93713466561923], [1.85007200612454, -0.46824072777417, -1.50918242392545], [-0.03362432532150, -1.39269245193812, -1.74003582081606], [-0.56857009928108, -1.01764444489068, -2.61263467107342], [-0.44096297340282, -2.84337808903410, -1.48899734014499], [-0.47991761226058, -0.55230954385212, -0.55520222968656], [-1.51566045903090, -2.89187354810876, -1.32273881320610], [-0.18116520746778, -3.45187805987944, -2.34920431470368], [0.06989722340461, -3.23298998903001, -0.60872832703814], [-1.56668253918793, 0.00552120970194, -0.52884675001441], [1.99245341064342, -1.73097165236442, -3.08869239114486], [3.42884244212567, -1.30660069291348, -3.28712665743189], [3.87721962540768, -0.88843123009431, -2.38921453037869], [3.46548545761151, -0.56495308290988, -4.08311788302584], [4.00253374168514, -2.16970938132208, -3.61210068365649], [1.40187968630565, -2.43826111827818, -3.89034127398078], [0.40869198386066, -0.49101709352090, 0.47992424955574], [1.15591901335007, -1.16524842262351, 0.48740266650199], [0.00723492494701, 0.11692276177442, 1.73426297572793], [0.88822128447468, 0.28499001838229, 2.34645658013686], [-0.47231557768357, 1.06737634000561, 1.52286682546986], [-0.70199987915174, -0.50485938116399, 2.28058247845421], ]), ) calc = XTB(method="GFN2-xTB", solvent="water", accuracy=0.1) atoms.set_calculator(calc) opt = LBFGS(atoms) opt.run(fmax=0.1) assert approx(atoms.get_potential_energy(), thr) == -897.4533662470938 assert approx(np.linalg.norm(atoms.get_forces(), ord=2), thr) == 0.19359647527783497
def test_co_potential_curve(self): direction = np.array([1., 2., 3.]) direction /= np.linalg.norm(direction) r_train = np.linspace(0.5, 7, 11) energies_train = [] images_train = [] for ri in r_train: image = Atoms( ['C', 'O'], positions=np.array([-0.5*ri*direction, 0.5*ri*direction])) image.set_calculator(EMT()) energies_train.append(image.get_potential_energy()) images_train.append(image) kernel = RBFKernel(constant=100.0, length_scale=1e-1) calc = GPRCalculator(kernel=kernel, C1=1e8, C2=1e8, opt_restarts=0) [calc.add_data(im) for im in images_train] calc.fit() np.testing.assert_allclose( energies_train, [calc.predict(im)[0] for im in images_train])
def test_build_diag(self): direction = np.array([1., 2., 3.]) direction /= np.linalg.norm(direction) r_train = np.linspace(0.5, 7, 11) energies_train = [] images_train = [] for ri in r_train: image = Atoms(['C', 'O'], positions=np.array( [-0.5 * ri * direction, 0.5 * ri * direction])) image.set_calculator(EMT()) energies_train.append(image.get_potential_energy()) images_train.append(image) def to_radius(x): xyzs = x.get_positions() r = np.sqrt(np.sum((xyzs[1, :] - xyzs[0, :])**2)) dr = np.zeros((1, 6)) dr[0, 0] = (xyzs[0, 0] - xyzs[1, 0]) / r dr[0, 1] = (xyzs[0, 1] - xyzs[1, 1]) / r dr[0, 2] = (xyzs[0, 2] - xyzs[1, 2]) / r dr[0, 3] = (xyzs[1, 0] - xyzs[0, 0]) / r dr[0, 4] = (xyzs[1, 1] - xyzs[0, 1]) / r dr[0, 5] = (xyzs[1, 2] - xyzs[0, 2]) / r return [r], dr kernel = RBFKernel(constant=100.0, length_scale=1e-1) calc = NCGPRCalculator(input_transform=to_radius, kernel=kernel, C1=1e8, C2=1e8, opt_restarts=0) [calc.add_data(im) for im in images_train] calc.fit() np.testing.assert_allclose( calc.build_kernel_diagonal((calc.q_train, calc.dq_train)), np.diag(calc.build_kernel_matrix()))
number_of_atoms = [] scat.set_processor(proc, alg) type_list.append((proc, alg)) nrg_l = [] f_l = [] try: for i in sizes: atoms = build_sphere_np('/mnt/work-data/dev/pyIID/benchmarks/1100138.cif', float(i) / 2) atoms.rattle() print len(atoms), i/10. number_of_atoms.append(len(atoms)) calc = PDFCalc(obs_data=pdf, scatter=scat, conv=1, potential='rw') atoms.set_calculator(calc) s = time.time() nrg = atoms.get_potential_energy() # scat.get_fq(atoms) f = time.time() nrg_l.append(f-s) s = time.time() # force = atoms.get_forces() # scat.get_grad_fq(atoms) f = time.time() f_l.append(f-s) except: print traceback.format_exc() break time_list.append((nrg_l, f_l))
basis='sto-3g', force='force', nproc=1, chk='water.chk', label='water') calc.clean() water = Atoms('OHH', positions=[(0, 0, 0), (1, 0, 0), (0, 1, 0)], calculator=calc) opt = LBFGS(water) opt.run(fmax=0.05) forces = water.get_forces() energy = water.get_potential_energy() positions = water.get_positions() # Then test the IO routines from ase.io import read water2 = read('water.log') forces2 = water2.get_forces() energy2 = water2.get_potential_energy() positions2 = water2.get_positions() #compare distances since positions are different in standard orientation dist = water.get_all_distances() dist2 = read('water.log', quantity='structures')[-1].get_all_distances() assert abs(energy - energy2) < 1e-7 assert abs(forces - forces2).max() < 1e-9 assert abs(positions - positions2).max() < 1e-6
class TestCalculator_SW_Potential(quippytest.QuippyTestCase): def setUp(self): self.xml = """ <SW_params n_types="2" label="PRB_31_plus_H"> <comment> Stillinger and Weber, Phys. Rev. B 31 p 5262 (1984), extended for other elements </comment> <per_type_data type="1" atomic_num="1" /> <per_type_data type="2" atomic_num="14" /> <per_pair_data atnum_i="1" atnum_j="1" AA="0.0" BB="0.0" p="0" q="0" a="1.0" sigma="1.0" eps="0.0" /> <per_pair_data atnum_i="1" atnum_j="14" AA="8.581214" BB="0.0327827" p="4" q="0" a="1.25" sigma="2.537884" eps="2.1672" /> <per_pair_data atnum_i="14" atnum_j="14" AA="7.049556277" BB="0.6022245584" p="4" q="0" a="1.80" sigma="2.0951" eps="2.1675" /> <!-- triplet terms: atnum_c is the center atom, neighbours j and k --> <per_triplet_data atnum_c="1" atnum_j="1" atnum_k="1" lambda="21.0" gamma="1.20" eps="2.1675" /> <per_triplet_data atnum_c="1" atnum_j="1" atnum_k="14" lambda="21.0" gamma="1.20" eps="2.1675" /> <per_triplet_data atnum_c="1" atnum_j="14" atnum_k="14" lambda="21.0" gamma="1.20" eps="2.1675" /> <per_triplet_data atnum_c="14" atnum_j="1" atnum_k="1" lambda="21.0" gamma="1.20" eps="2.1675" /> <per_triplet_data atnum_c="14" atnum_j="1" atnum_k="14" lambda="21.0" gamma="1.20" eps="2.1675" /> <per_triplet_data atnum_c="14" atnum_j="14" atnum_k="14" lambda="21.0" gamma="1.20" eps="2.1675" /> </SW_params> """ quippy.system_module.system_reseed_rng(2065775975) self.pot_calculator = Potential('IP SW', param_str=self.xml) self.at = Atoms('Si8', positions=diamond_pos, pbc=True, cell=[5.44, 5.44, 5.44]) self.f = np.zeros((3, len(self.at)), order='F') self.df = np.zeros((3, len(self.at)), order='F') self.v = np.zeros((3, 3), order='F') self.energy_ref = -34.5038375509 self.forces_ref = np.array([[0.89920374, -0.38025157, -0.38727027], [0.36623356, -0.52403757, 0.7200206], [-0.36952654, 0.12899529, 0.00458111], [-0.19912365, -0.1632057, 1.08509495], [-0.67565314, -0.59410498, -0.47921521], [0.17097454, 0.5847822, -0.31088749], [0.43613712, 0.90811269, 0.1600328], [-0.62824563, 0.03970963, -0.79235649]]) self.virial_ref = np.array([[-0.34103601, 0.60925144, -0.02138795], [0.60925144, -0.36145702, -0.19375487], [-0.02138795, -0.19375487, -0.34640615]]).T # Voigt notation by hand from virial self.stress_ref = -np.array([ -0.34103601, -0.36145702, -0.34640615, -0.19375487, -0.02138795, 0.60925144 ]) / self.at.get_volume() self.at.set_calculator(self.pot_calculator) def test_energy(self): self.assertAlmostEqual(self.at.get_potential_energy(), self.energy_ref) def test_forces(self): self.assertArrayAlmostEqual(self.at.get_forces(), self.forces_ref, tol=1E-06) def test_stress(self): self.assertArrayAlmostEqual(self.at.get_stress(), self.stress_ref) def test_virial(self): self.assertArrayAlmostEqual(self.pot_calculator.get_virial(self.at), self.virial_ref, tol=1E-06) def test_calc_args_1(self): self.pot_calculator.calculate(self.at, propertes=["forces"], calc_args="do_rescale_E E_scale=1.01") f = self.pot_calculator.results['forces'] self.assertArrayAlmostEqual(f, self.forces_ref * 1.01, tol=1E-06) def test_calc_args_2(self): self.pot_calculator.calculate(self.at, properties=["forces"], do_rescale_E=True, E_scale=1.01) f = self.pot_calculator.results['forces'] self.assertArrayAlmostEqual(f, self.forces_ref * 1.01, tol=1E-06) def test_calc_args_3(self): pot2 = Potential('IP SW', param_str=self.xml, calc_args="do_rescale_E E_scale=1.01") f = pot2.get_forces(self.at) self.assertArrayAlmostEqual(f, self.forces_ref * 1.01, tol=1E-06) def test_calc_args_4(self): pot2 = Potential('IP SW', param_str=self.xml, calc_args={ 'do_rescale_E': True, 'E_scale': 1.01 }) f = pot2.get_forces(self.at) self.assertArrayAlmostEqual(f, self.forces_ref * 1.01, tol=1E-06)
class execution(object): def __init__(self, executionInput, xyz=None, stateFile='state.json'): ''' Setup a new execution Name: input file xyz: xyz coordinates (for future automated execution) stateFile: state file to use for tracking of executions TODO: - only ground state in non-periodic cell supported NOW, more to add! - inputs from a dict to better handle automated executions ''' if isinstance(executionInput, str): self.inp = self.json(executionInput) self.inputFileName = executionInput if isinstance(executionInput, dict): self.inp = executionInput self.inputFileName = 'dict' # TODO: generate meaningful name here (how?) self.setup() def setup(self): '''setup''' self.functional = self.inp.get('functional') or 'LDA' self.runtype = self.inp.get('runtype') or 'calc' # this actually can't be none! _xyz = self.inp.get('xyz') if isinstance(_xyz, str): self.inputXYZ = _xyz self.atoms = read(self.inputXYZ) else: # support reading frm a dict (TODO: other type of objects - ?) from ase.atoms import Atoms from ase.atom import Atom self.atoms = \ Atoms([Atom(a[0],(a[1],a[2],a[3])) for a in _xyz]) # TODO: support multiple cell types self.atoms.cell = (self.inp['cell']['x'], self.inp['cell']['y'], self.inp['cell']['z']) # Mixer setup - only "broyden" supported self.mixer = self.inp.get('mixer') if self.mixer is None: self.mixer = Mixer else: self.mixer = BroydenMixer # Custom ID: allow to trace parameter set using a custom id self.custom_id = self.inp.get('custom_id') # Periodic Boundary Conditions self.atoms.pbc = False # This should not be done always self.atoms.center() self.nbands = self.inp.get('nbands') self.gpts = None self.setup_gpts() self.max_iterations = self.inp.get('maxiter') or 333 # Name: to be set by __str__ first time it is run self.name = None # self.logExtension = 'txt' self.dataExtension = 'gpw' # Timing / statistics self.lastElapsed = None # def get_optimizer(self, name='QuasiNewton'): from importlib import import_module name = name.lower() _optimMap = \ { 'mdmin': 'MDMin', 'hesslbfgs': 'HessLBFGS', 'linelbfgs': 'LineLBFGS', 'fire': 'FIRE', 'lbfgs': 'LBFGS', 'lbfgsls': 'LBFGSLineSearch', 'bfgsls': 'BFGSLineSearch', 'bfgs': 'BFGS', 'quasinewton': 'QuasiNewton' } _class = _optimMap[name] _m = import_module('ase.optimize') return getattr(_m, _class) # def setup_gpts(self): self.gpts = h2gpts(self.inp.get('spacing') or 0.20, self.atoms.get_cell(), idiv=self.inp.get('idiv') or 8) def filename(self, force_id=None, extension='txt'): '''to correctly support this: stateFile support must be completed as it isn't just add "_0" to __str__() for now ''' base = self.__str__() if self.custom_id is not None: base = base + '_' + self.custom_id if force_id is None: _fn = base + '_0.' + extension else: _fn = '{}_{}.{}'.format(base, force_id, extension) return _fn # @property def xyz(self): return self.inp['xyz'] # @property def spacegroup(self): '''international ID of determined spacegroup''' sg = spglib.get_symmetry_dataset(spglib.refine_cell( self.atoms))['international'] return sg.replace('/', ':') # def __str__(self): '''get execution name - to be used for log filename Components: - Number atoms - Volume - Spacegroup number - Functional name ''' if self.name is None: self.name = '{}_{}_{}_{}'.format( len(self.atoms), int(self.atoms.get_volume() * 100), self.spacegroup, self.functional) _gpaw = os.environ['GPAW'] _pwd = os.environ['PWD'] self.procname = '{} ({})'.format(self.name, _pwd.replace(_gpaw + '/', '')) setproctitle(self.procname) return self.name ## def print(self, *args, **kwargs): '''print wrapper - initial step before logger''' r = print(*args, *kwargs) return r # def pretty(self): '''pretty print original parameters''' _p = pformat(self.inp) self.print(_p) # def json(self, filename): '''load parameters from json''' with open(filename, 'rb') as f: s = f.read() j = json.loads(s) return j # def run(self, force_id=None, runtype=None): ''' perform execution - only calc or optim supported now ''' _lastStart = time.time() if runtype is None and self.runtype is 'calc': _f = self.calc else: _f = self.optim # calculate try: result = _f(force_id) finally: _lastEnd = time.time() self.lastElapsed = _lastEnd - _lastStart return result # def calc(self, force_id=None, functional=None): '''prepare and launch execution force_id: use this id for filename generation ''' # Allow to override the funtional _fnl = functional or self.functional # generate filenames _fn = self.filename(force_id) _dn = self.filename(force_id, extension=self.dataExtension) self.print('Calculation with {} log and Input: {}.'.format( _fn, self.inputFileName)) if self.nbands is None: _calc = GPAW(xc=_fnl, txt=self.filename(force_id), mixer=self.mixer(), maxiter=self.max_iterations, gpts=self.gpts) else: _calc = GPAW(xc=_fnl, txt=self.filename(force_id), nbands=self.nbands, mixer=self.mixer(), maxiter=self.max_iterations, gpts=self.gpts) self.atoms.set_calculator(_calc) self.atoms.get_potential_energy() # write out wavefunctions _calc.write(_dn, 'all') return _calc ##### def optim(self, force_id=None, functional=None): '''prepare and launch execution force_id: use this id for filename generation ''' _optim_name = self.inp.get('optimizer') or 'QuasiNewton' optimizer = self.get_optimizer(_optim_name) # Allow to override the funtional _fnl = functional or self.functional # generate filenames _fn = self.filename(force_id) _dn = self.filename(force_id, extension=self.dataExtension) self.print('Optimization with {} log and Input: {}.'.format( _fn, self.inputFileName)) if self.nbands is None: _calc = GPAW(xc=_fnl, txt=self.filename(force_id), mixer=self.mixer(), maxiter=self.max_iterations, gpts=self.gpts) else: _calc = GPAW(xc=_fnl, txt=self.filename(force_id), nbands=self.nbands, mixer=self.mixer(), maxiter=self.max_iterations, gpts=self.gpts) self.atoms.set_calculator(_calc) self.opt = optimizer(self.atoms, trajectory=self.__str__() + '_emt.traj') self.opt.run(fmax=0.05) # write out wavefunctions _calc.write(_dn, 'all') return _calc
from ase.optimize import BFGS from ase.atoms import Atoms from ase.calculators.crystal import CRYSTAL geom = Atoms('OHH', positions=[(0, 0, 0), (1, 0, 0), (0, 1, 0)]) geom.set_calculator( CRYSTAL(label='water', guess=True, basis='sto-3g', xc='PBE', otherkeys=[ 'scfdir', 'anderson', ['maxcycles', '500'], ['toldee', '6'], ['tolinteg', '7 7 7 7 14'], ['fmixing', '90'] ])) opt = BFGS(geom) opt.run(fmax=0.05) final_energy = geom.get_potential_energy() assert abs(final_energy + 2047.34531091) < 1.0
from gpaw import GPAW from ase.atoms import Atoms atoms = Atoms("C", positions=[[0.0, 0.0, 0.0]]) atoms.center(vacuum=4.0) print(atoms) calc = GPAW(txt="-", hund=True) from ase.units import Hartree atoms.set_calculator(calc) Etot = atoms.get_potential_energy() # convert from eV to Ha print("Etot = %18.10f Ha\n" % (Etot / Hartree))
def test_gfn1_xtb_0d(): """Test ASE interface to GFN1-xTB""" thr = 1.0e-5 atoms = Atoms( symbols='CHOCH2CH4CH2OH', positions=np.array([ [1.578385, 0.147690, 0.343809], [1.394750, 0.012968, 1.413545], [1.359929, -1.086203, -0.359782], [0.653845, 1.215099, -0.221322], [1.057827, 2.180283, 0.093924], [0.729693, 1.184864, -1.311438], [-0.817334, 1.152127, 0.208156], [-1.303525, 2.065738, -0.145828], [-0.883765, 1.159762, 1.299260], [1.984120, -1.734446, -0.021385], [2.616286, 0.458948, 0.206544], [-1.627725, -0.034052, -0.311301], [-2.684229, 0.151015, -0.118566], [-1.501868, -0.118146, -1.397506], [-1.324262, -1.260154, 0.333377], [-0.417651, -1.475314, 0.076637], ]), ) forces = np.array([[-0.37070590, -0.51067739, -0.27981764], [-0.04339461, -0.09290876, 0.22940156], [0.11141234, 0.46678720, 0.24552625], [0.04255709, 0.19019316, -0.23531997], [-0.01897377, 0.10810803, 0.05314982], [-0.07150720, -0.05182148, -0.08413638], [0.06631826, 0.10587709, 0.29833479], [-0.01062355, 0.02301460, -0.04964730], [0.06610108, -0.02724994, 0.09234280], [0.06519070, -0.19311773, -0.01152205], [0.23879786, 0.09871398, -0.04009526], [-0.04381577, -0.49997745, 0.08672818], [-0.23259608, 0.13735636, 0.06783414], [0.08297636, -0.09566973, -0.20602954], [-0.23686052, 0.57454371, -0.17194215], [0.35512370, -0.23317164, 0.00519275]]) charges = np.array([ 0.19494678, 0.01759972, -0.57108503, -0.05371086, 0.02458495, 0.03915074, -0.06889977, 0.02521441, 0.03350058, 0.34380081, 0.01442221, 0.20053193, 0.02454679, 0.01011690, -0.58349877, 0.34877861, ]) dipole_moment = np.array([0.76943477, 0.33021928, 0.05670150]) atoms.calc = XTB(method="GFN1-xTB") assert approx(atoms.get_potential_energy(), thr) == -632.7363734598027 assert approx(atoms.get_forces(), thr) == forces assert approx(atoms.get_charges(), thr) == charges assert approx(atoms.get_dipole_moment(), thr) == dipole_moment
from ase.atoms import Atoms from ase.optimize.lbfgs import LBFGS # First test to make sure Gaussian works calc = Gaussian(method='pbepbe', basis='sto-3g', force='force', nproc=1, chk='water.chk', label='water') calc.clean() water = Atoms('OHH', positions=[(0., 0. ,0. ), (1. ,0. ,0. ), (0. ,1. ,0. )], calculator=calc) opt = LBFGS(water) opt.run(fmax=0.05) forces = water.get_forces() energy = water.get_potential_energy() positions = water.get_positions() # Then test the IO routines from ase.io import read water2 = read('water/water.log') forces2 = water2.get_forces() energy2 = water2.get_potential_energy() positions2 = water2.get_positions() assert (energy - energy2) < 1e-14 assert (forces.all() - forces2.all()) < 1e-14 assert (positions.all() - positions2.all()) < 1e-14
def test_gfn2_xtb_0d(): """Test ASE interface to GFN2-xTB""" thr = 1.0e-5 atoms = Atoms( symbols='CHOCH2CH4CH2OH', positions=np.array([ [1.578385, 0.147690, 0.343809], [1.394750, 0.012968, 1.413545], [1.359929, -1.086203, -0.359782], [0.653845, 1.215099, -0.221322], [1.057827, 2.180283, 0.093924], [0.729693, 1.184864, -1.311438], [-0.817334, 1.152127, 0.208156], [-1.303525, 2.065738, -0.145828], [-0.883765, 1.159762, 1.299260], [1.984120, -1.734446, -0.021385], [2.616286, 0.458948, 0.206544], [-1.627725, -0.034052, -0.311301], [-2.684229, 0.151015, -0.118566], [-1.501868, -0.118146, -1.397506], [-1.324262, -1.260154, 0.333377], [-0.417651, -1.475314, 0.076637], ]), ) forces = np.array([ [-0.28561158, -0.48592026, -0.28392327], [-0.05526158, -0.07403455, 0.09665539], [0.18824950, 0.39170140, 0.33881928], [-0.01166074, 0.24653252, -0.16779606], [-0.05367503, 0.03368063, 0.05115422], [-0.09605262, -0.10000389, 0.01097286], [0.09423300, 0.18573616, 0.17797438], [0.02651896, -0.04073849, -0.03655980], [0.07886359, -0.05806479, 0.00649360], [-0.00780520, -0.07979390, -0.03175697], [0.13328595, 0.03209283, -0.04638514], [0.08197778, -0.39157299, 0.12107401], [-0.11453823, -0.01485088, 0.09974068], [0.09786017, -0.09130861, -0.05738675], [-0.26643400, 0.47603727, -0.27856705], [0.19005003, -0.02949244, -0.00050938], ]) charges = np.array([ 0.08239823, 0.03066406, -0.44606929, -0.06139043, 0.03610596, 0.05389499, -0.06991855, 0.03384415, 0.04665524, 0.28688538, 0.02246569, 0.08251610, 0.03810481, 0.01883776, -0.46691965, 0.31192554, ]) dipole_moment = np.array([0.62120710, 0.28006659, 0.04465985]) calc = XTB(method="GFN2-xTB", atoms=atoms) assert approx(atoms.get_potential_energy(), abs=thr) == -592.6794366990786 assert approx(atoms.get_forces(), abs=thr) == forces assert approx(atoms.get_charges(), abs=thr) == charges assert approx(atoms.get_dipole_moment(), abs=thr) == dipole_moment atoms.calc.set( accuracy=0.1, electronic_temperature=500.0, max_iterations=20, solvent="ch2cl2", ) assert approx(atoms.get_potential_energy(), abs=thr) == -592.9940608761889