Example #1
0
    def _evaluate_single(
        self,
        positions: torch.Tensor,
        evaluate_force=True,
        evaluate_energy=True,
    ):
        from xtb.interface import Calculator, XTBException
        from xtb.utils import get_method, get_solvent
        positions = _nm2bohr(positions)
        energy, force = None, None
        try:
            calc = Calculator(get_method(self.method), self.numbers, positions)
            calc.set_solvent(get_solvent(self.solvent))
            calc.set_verbosity(self.verbosity)
            calc.set_electronic_temperature(self.temperature)
            try:
                res = calc.singlepoint()
            except XTBException:
                # Try with higher temperature
                calc.set_electronic_temperature(10 * self.temperature)
                res = calc.singlepoint()
                calc.set_electronic_temperature(self.temperature)
                res = calc.singlepoint(res)
            if evaluate_energy:
                energy = _hartree2kbt(res.get_energy(), self.temperature)
            if evaluate_force:
                force = _hartree_per_bohr2kbt_per_nm(-res.get_gradient(),
                                                     self.temperature)
            assert not np.isnan(energy)
            assert not np.isnan(force).any()
        except XTBException as e:
            if self.err_handling == "error":
                raise e
            elif self.err_handling == "warning":
                warnings.warn(f"Caught exception in xtb. "
                              f"Returning infinite energy and zero force. "
                              f"Original exception: {e}")
                force = np.zeros_like(positions)
                energy = np.infty
            elif self.err_handling == "ignore":
                force = np.zeros_like(positions)
                energy = np.infty
        except AssertionError:
            force[np.isnan(force)] = 0.
            energy = np.infty
            if self.err_handling in ["error", "warning"]:
                warnings.warn(
                    "Found nan in xtb force or energy. Returning infinite energy and zero force."
                )

        return energy, force
Example #2
0
def test_gfn2_xtb_0d():
    """check if the GFN2-xTB interface is working correctly."""
    thr = 1.0e-7
    thr2 = 1.0e-6

    numbers = np.array(
        [6, 7, 6, 7, 6, 6, 6, 8, 7, 6, 8, 7, 6, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    )
    positions = np.array(
        [
            [ 2.02799738646442, 0.09231312124713,-0.14310895950963],
            [ 4.75011007621000, 0.02373496014051,-0.14324124033844],
            [ 6.33434307654413, 2.07098865582721,-0.14235306905930],
            [ 8.72860718071825, 1.38002919517619,-0.14265542523943],
            [ 8.65318821103610,-1.19324866489847,-0.14231527453678],
            [ 6.23857175648671,-2.08353643730276,-0.14218299370797],
            [ 5.63266886875962,-4.69950321056008,-0.13940509630299],
            [ 3.44931709749015,-5.48092386085491,-0.14318454855466],
            [ 7.77508917214346,-6.24427872938674,-0.13107140408805],
            [10.30229550927022,-5.39739796609292,-0.13672168520430],
            [12.07410272485492,-6.91573621641911,-0.13666499342053],
            [10.70038521493902,-2.79078533715849,-0.14148379504141],
            [13.24597858727017,-1.76969072232377,-0.14218299370797],
            [ 7.40891694074004,-8.95905928176407,-0.11636933482904],
            [ 1.38702118184179, 2.05575746325296,-0.14178615122154],
            [ 1.34622199478497,-0.86356704498496, 1.55590600570783],
            [ 1.34624089204623,-0.86133716815647,-1.84340893849267],
            [ 5.65596919189118, 4.00172183859480,-0.14131371969009],
            [14.67430918222276,-3.26230980007732,-0.14344911021228],
            [13.50897177220290,-0.60815166181684, 1.54898960808727],
            [13.50780014200488,-0.60614855212345,-1.83214617078268],
            [ 5.41408424778406,-9.49239668625902,-0.11022772492007],
            [ 8.31919801555568,-9.74947502841788, 1.56539243085954],
            [ 8.31511620712388,-9.76854236502758,-1.79108242206824],
        ],
        order="F",
    )
    gradient = np.array(
        [
            [-4.48702270e-03,-7.11501681e-04, 4.42250727e-06],
            [ 5.63520998e-03,-2.63277841e-02,-5.47551032e-05],
            [ 4.94394513e-03, 1.91697172e-02, 5.20296937e-05],
            [ 4.14320227e-03, 3.78985927e-03,-3.26124506e-05],
            [-3.44924840e-02,-8.30763633e-03,-3.85476373e-05],
            [ 6.09858493e-03,-4.02776651e-03, 3.46142461e-05],
            [ 1.74698961e-02, 7.91501928e-03, 3.75246600e-05],
            [-1.44268345e-02, 7.07857171e-03,-1.12175048e-04],
            [-5.07088926e-04,-1.13149559e-02, 7.28999985e-05],
            [-1.55778036e-02, 1.26994854e-02,-2.82633017e-05],
            [ 2.84123935e-02,-2.38401320e-02,-3.96858051e-05],
            [ 2.52730535e-03, 1.36557434e-02,-1.07970323e-05],
            [-1.61957397e-03,-2.96924390e-03, 2.89329075e-06],
            [-6.51526117e-03, 7.90714240e-03,-5.83689564e-05],
            [-1.45365262e-03, 2.78387473e-03, 4.39889933e-06],
            [ 2.59676642e-03, 9.07269292e-04, 3.98184821e-03],
            [ 2.59860253e-03, 9.08300767e-04,-3.98462262e-03],
            [-3.77425616e-03, 8.36833530e-03, 2.89789639e-05],
            [ 7.86820850e-03, 2.13957196e-03, 7.31459251e-07],
            [ 9.32145702e-04,-1.65668033e-04, 3.24917573e-03],
            [ 9.57211265e-04,-1.41846051e-04,-3.25368821e-03],
            [-2.06937754e-03,-9.28913451e-03, 1.03587348e-04],
            [ 3.58598494e-04,-9.82977790e-05, 2.38378001e-03],
            [ 3.81284918e-04,-1.28923994e-04,-2.34336886e-03],
        ]
    )
    charges = np.array([
        -0.05445590, -0.00457526,  0.08391889, -0.27870751,  0.11914924,
        -0.02621044,  0.26115960, -0.44071824, -0.10804747,  0.30411699,
        -0.44083760, -0.07457706, -0.04790859, -0.03738239,  0.06457802,
         0.08293905,  0.08296802,  0.05698136,  0.09025556,  0.07152988,
         0.07159003,  0.08590674,  0.06906357,  0.06926350,
    ])

    calc = Calculator(Param.GFN2xTB, numbers, positions)
    calc.set_verbosity(VERBOSITY_MINIMAL)
    assert calc.check() == 0

    calc.set_accuracy(1.0)
    calc.set_max_iterations(50)
    calc.set_electronic_temperature(300.0)

    res = calc.singlepoint()

    assert approx(res.get_energy(), abs=thr) == -42.14746312757416
    assert approx(res.get_gradient(), abs=thr2) == gradient
    assert approx(res.get_charges(), abs=thr2) == charges