Esempio n. 1
0
def test_gfn2xtb_solvation():
    """Use GFN2-xTB/GBSA for a mindless molecule"""
    thr = 1.0e-7

    numbers = np.array([
        1, 9, 15, 13, 1, 1, 13, 5, 3, 15, 8, 1, 1, 5, 16, 1,
    ])
    positions = np.array([
        [-2.14132037405479, -1.34402701877044, -2.32492500904728],
        [ 4.46671289205392, -2.04800110524830,  0.44422406067087],
        [-4.92212517643478, -1.73734240529793,  0.96890323821450],
        [-1.30966093045696, -0.52977363497805,  3.44453452239668],
        [-4.34208759006189, -4.30470270977329,  0.39887431726215],
        [ 0.61788392767516,  2.62484136683297, -3.28228926932647],
        [ 4.23562873444840, -1.68839322682951, -3.53824299552792],
        [ 2.23130060612446,  1.93579813100155, -1.80384647554323],
        [-2.32285463652832,  2.90603947535842, -1.39684847191937],
        [ 2.34557941578250,  2.86074312333371,  1.82827238641666],
        [-3.66431367659153, -0.42910188232667, -1.81957402856634],
        [-0.34927881505446, -1.75988134003940,  5.98017466326572],
        [ 0.29500802281217, -2.00226104143537,  0.53023447931897],
        [ 2.10449364205058, -0.56741404446633,  0.30975625014335],
        [-1.59355304432499,  3.69176153150419,  2.87878226787916],
        [ 4.34858700256050,  2.39171478113440, -2.61802993563738],
    ])
    dipole = np.array(
        [ 0.24089806056398366, -0.8387798297446885, -2.490982140104827]
    )
    gradient = np.array([
        [ 0.00665983765172852, -0.000306100468371254,  0.002975729588167250],
        [-0.00075554832215171,  0.001103331394141163,  0.002917549694785815],
        [ 0.00103709572973848, -0.000861474437293449,  0.005249091862608099],
        [ 0.00345933752444154, -0.004301298519638866, -0.003786850195834586],
        [-0.00078541037746589, -0.004659889270757375,  0.000757596433662243],
        [ 0.00322292779086688, -0.003572675716601181, -0.004746876147864423],
        [-0.00513395549346115,  0.011449011590338731,  0.002505538991042167],
        [-0.00574879905769782,  0.004579255119010918, -0.002779923711244377],
        [ 0.00241040419407409,  0.003121180180444150,  0.003515870816111050],
        [-0.00021150434069988, -0.002620960429434571, -0.008691267383737740],
        [-0.00323129790918273,  0.005268717780263234, -0.010531374769279896],
        [ 0.00203571944238303, -0.004757311655869700,  0.005097227197421218],
        [-0.00606663933320002,  0.003597297542372022, -0.002467813364587243],
        [ 0.00024286212984382, -0.009680057140546466,  0.014999309475018330],
        [-0.00090824770994144,  0.004745736486598619, -0.001923154458354601],
        [ 0.00377321808072424, -0.003104762454655897, -0.003090654027913303],
    ])

    calc = Calculator(Param.GFN2xTB, numbers, positions)

    res = calc.singlepoint()

    assert approx(res.get_energy(), abs=thr) == -25.0841508410945

    calc.set_solvent(Solvent.ch2cl2)

    res = calc.singlepoint(res)

    assert approx(res.get_energy(), abs=thr) == -25.11573992702904
    assert approx(res.get_dipole(), abs=thr) == dipole
    assert approx(res.get_gradient(), abs=thr) == gradient
Esempio n. 2
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
Esempio n. 3
0
def test_gfn1xtb_solvation():
    """Use GFN1-xTB/GBSA for a mindless molecule"""
    thr = 1.0e-7

    numbers = np.array([
        1, 1, 6, 5, 1, 15, 8, 17, 13, 15, 5, 1, 9, 15, 1, 15,
    ])
    positions = np.array([
        [ 2.79274810283778,  3.82998228828316, -2.79287054959216],
        [-1.43447454186833,  0.43418729987882,  5.53854345129809],
        [-3.26268343665218, -2.50644032426151, -1.56631149351046],
        [ 2.14548759959147, -0.88798018953965, -2.24592534506187],
        [-4.30233097423181, -3.93631518670031, -0.48930754109119],
        [ 0.06107643564880, -3.82467931731366, -2.22333344469482],
        [ 0.41168550401858,  0.58105573172764,  5.56854609916143],
        [ 4.41363836635653,  3.92515871809283,  2.57961724984000],
        [ 1.33707758998700,  1.40194471661647,  1.97530004949523],
        [ 3.08342709834868,  1.72520024666801, -4.42666116106828],
        [-3.02346932078505,  0.04438199934191, -0.27636197425010],
        [ 1.11508390868455, -0.97617412809198,  6.25462847718180],
        [ 0.61938955433011,  2.17903547389232, -6.21279842416963],
        [-2.67491681346835,  3.00175899761859,  1.05038813614845],
        [-4.13181080289514, -2.34226739863660, -3.44356159392859],
        [ 2.85007173009739, -2.64884892757600,  0.71010806424206],
        ])
    dipole = np.array(
        [-1.7019679049596192, -2.3705248395254794, 2.3242138027827446]
    )
    gradient = np.array([
        [ 0.009559998512721449,  0.003160492649991202,  0.002493263404035152],
        [-0.014057002659599450,  0.001307143355963449, -0.006896995523674975],
        [ 0.003153682694711128,  0.000577646576754350,  0.003242310422859749],
        [ 0.003450958779103499, -0.003371154526376105,  0.006833209066374420],
        [-0.001015432627909870, -0.003296949677104424,  0.001316980539002961],
        [-0.006952278070976579,  0.004741461728853791, -0.010835641525482725],
        [ 0.004676820346825542,  0.013884337727247118, -0.001369508087834928],
        [-0.008836453325485083, -0.008509582724068617, -0.004439799174447864],
        [ 0.014459308335089790,  0.005276511880967742,  0.004903442725946663],
        [-0.036094040309768836, -0.003785312815195147,  0.006658677483987865],
        [ 0.008136405754401969,  0.004504379636843624,  0.003321663239613836],
        [ 0.008869982110786701, -0.011001116814485038, -0.001446882216395328],
        [ 0.016590179303752735, -0.004068517105666850,  0.002429055885660720],
        [-0.007907876155693029, -0.003802464713817357, -0.003873086023200218],
        [-0.000245758070628337,  0.000875351650743832, -0.003291567698024252],
        [ 0.006211505382668360,  0.003507773169348530,  0.000954877481578866],
    ])

    calc = Calculator(Param.GFN1xTB, numbers, positions)

    calc.set_solvent(Solvent.methanol)

    res = calc.singlepoint()

    assert approx(res.get_energy(), abs=thr) == -33.66717660261584
    assert approx(res.get_dipole(), abs=thr) == dipole
    assert approx(res.get_gradient(), abs=thr) == gradient

    calc.set_solvent()

    res = calc.singlepoint(res, copy=True)

    assert approx(res.get_energy(), abs=thr) == -33.63768565903155