Skip to content

Compatibility issue with Pytorch #110

@SingletC

Description

@SingletC

I found this strange behavior of tblite in Python if PyTorch was imported.

import torch
from tblite.ase import TBLite
from ase.atoms import Atoms
import numpy as np
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]),
)
atoms.calc = TBLite(method="GFN1-xTB")
atoms.get_potential_energy()  # result in eV

I got below output and it will never converge.

------------------------------------------------------------
  cycle        total energy    energy error   density error
------------------------------------------------------------
      1     -220145.1130691  -2.2014835E+05   2.2662454E+01
      2     -40994493.39613  -4.0774348E+07   2.5360774E+01
      3     -41798970.97528  -8.0447758E+05   2.1118388E+01
      4     -41231779.00001   5.6719198E+05   4.2775653E+00
      5     -41176517.54710   5.5261453E+04   2.1617182E+00
      6     -40929947.88949   2.4656966E+05   2.0241658E+00
      7     -40916228.61312   1.3719276E+04   1.5423235E+00
      8     -41175248.31226  -2.5901970E+05   1.8033720E+00
      9     -41075903.51087   9.9344801E+04   1.8529595E+00
     10     -41214458.36624  -1.3855486E+05   1.6252884E+00
     11     -41186215.84712   2.8242519E+04   1.2174420E+00
     12     -41076999.56984   1.0921628E+05   1.5036796E+00
     13     -40914319.21158   1.6268036E+05   1.0555325E+00
     14     -41803486.23037  -8.8916702E+05   1.1167128E+00
     15     -41425955.91671   3.7753031E+05   7.6022145E-01
     16     -41304348.50592   1.2160741E+05   1.2073868E+00
     17     -41771834.81548  -4.6748631E+05   9.9171739E-01
     18     -41090587.65317   6.8124716E+05   9.7361783E-01
     19     -41803487.78510  -7.1290013E+05   9.0392384E-01
     20     -40980774.06874   8.2271372E+05   8.5913280E-01
     21     -41803489.59767  -8.2271553E+05   5.3896947E-01
     22     -39982953.84887   1.8205357E+06   9.5528544E-01
     23     -41305192.47564  -1.3222386E+06   6.9177270E-01
     24     -41268485.27003   3.6707206E+04   5.5739283E-01
     25     -41478496.45580  -2.1001119E+05   6.3010293E-01
     26     -40702421.14412   7.7607531E+05   7.1690218E-01
     27     -39651186.85413   1.0512343E+06   1.2553164E+00
     28     -40949242.17474  -1.2980553E+06   6.1052683E-01
     29     -41001412.34763  -5.2170173E+04   5.5212886E-01
     30     -41073869.01141  -7.2456664E+04   7.7515769E-01
     31     -41737795.31617  -6.6392630E+05   1.9723712E-01
     32     -41801158.60198  -6.3363286E+04   2.2661286E-01
     33     -41661436.55195   1.3972205E+05   4.3782619E-01
     34     -41674543.69094  -1.3107139E+04   3.5255131E-01
     35     -41173591.11277   5.0095258E+05   4.4963589E-01
     36     -41788959.06234  -6.1536795E+05   2.8875074E-01
     37     -41797570.46348  -8.6114011E+03   2.4509764E-01
     38     -41668462.37089   1.2910809E+05   4.0788906E-01
     39     -41803490.37610  -1.3502801E+05   2.4518633E-01
     40     -41803490.44519  -6.9086663E-02   1.9533992E-01
     41     -41703372.46342   1.0011798E+05   3.2203525E-01
...

change the order of import solve the issue:

from tblite.ase import TBLite
import torch
from ase.atoms import Atoms
import numpy as np
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]),
)
atoms.calc = TBLite(method="GFN1-xTB")
atoms.get_potential_energy()  # result in eV
------------------------------------------------------------
  cycle        total energy    energy error   density error
------------------------------------------------------------
      1     -44.18466375638  -4.4584172E+01   9.5746031E-01
      2     -46.17349195524  -1.9888282E+00   3.7709276E-01
      3     -46.19352636164  -2.0034406E-02   2.1480769E-01
      4     -46.18634789943   7.1784622E-03   6.1688940E-02
      5     -46.19681129475  -1.0463395E-02   3.3123492E-03
      6     -46.19683700903  -2.5714277E-05   1.4081482E-03
      7     -46.19684187755  -4.8685184E-06   4.2127827E-04
      8     -46.19684228849  -4.1094205E-07   1.2814394E-04
      9     -46.19684233075  -4.2263878E-08   5.6634669E-06
------------------------------------------------------------
 total:                                   0.550 sec
Out[6]: -1257.0801097690023

my environment is python3.9
conda-forge installed tblite 0.3.0
I tested both Torch version 1.8.1+cu102 and 2.0.1+cpu

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions