pytorch implementation of dftd2 & dftd3

Overview

torch-dftd

pytorch implementation of dftd2 [1] & dftd3 [2, 3]

Install

# Install from pypi
pip install torch-dftd

# Install from source (for developers)
git clone https://github.com/pfnet-research/torch-dftd
pip install -e .

Quick start

from ase.build import molecule
from torch_dftd.torch_dftd3_calculator import TorchDFTD3Calculator

atoms = molecule("CH3CH2OCH3")
# device="cuda:0" for fast GPU computation.
calc = TorchDFTD3Calculator(atoms=atoms, device="cpu", damping="bj")

energy = atoms.get_potential_energy()
forces = atoms.get_forces()

print(f"energy {energy} eV")
print(f"forces {forces}")

Dependency

The library is tested under following environment.

  • python: 3.6
  • CUDA: 10.2
torch==1.5.1
ase==3.21.1
# Below is only for 3-body term
cupy-cuda102==8.6.0
pytorch-pfn-extras==0.3.2

Development tips

Formatting & Linting

pysen is used to format the python code of this repository.
You can simply run below to get your code formatted :)

# Format the code
$ pysen run format
# Check the code format
$ pysen run lint

CUDA Kernel function implementation with cupy

cupy supports users to implement CUDA kernels within python code, and it can be easily linked with pytorch tensor calculations.
Element wise kernel is implemented and used in some pytorch functions to accelerate speed with GPU.

See document for details about user defined kernel.

Citation

Please always cite original paper of DFT-D2 [1] or DFT-D3 [2, 3], if you used this software for your publication.

DFT-D2:
[1] S. Grimme, J. Comput. Chem, 27 (2006), 1787-1799. DOI: 10.1002/jcc.20495

DFT-D3:
[2] S. Grimme, J. Antony, S. Ehrlich and H. Krieg, J. Chem. Phys, 132 (2010), 154104. DOI: 10.1063/1.3382344

If BJ-damping is used in DFT-D3:
[3] S. Grimme, S. Ehrlich and L. Goerigk, J. Comput. Chem, 32 (2011), 1456-1465. DOI: 10.1002/jcc.21759

Comments
  • [WIP] Cell-related gradient modifications

    [WIP] Cell-related gradient modifications

    I found that the current implementation has several performance issues regarding gradient wrt. cell. This PR modifies that. Since the changes are relatively much, I will put some comments.

    Change summary:

    • Use shift for gradient instead of cell.
    • shift is now length scale instead cell unit.
    • Calculate Voigt notation style stress directly

    Also, this PR contains bugfix related to sked cell.

    bug enhancement 
    opened by So-Takamoto 1
  • Raise Error with single atom inputs.

    Raise Error with single atom inputs.

    When the length of atoms is 1, the routine raises error.

    from ase.build import molecule
    from ase.calculators.dftd3 import DFTD3
    from torch_dftd.torch_dftd3_calculator import TorchDFTD3Calculator
    
    if __name__ == "__main__":
        atoms = molecule("H")
        # device="cuda:0" for fast GPU computation.
        calc = TorchDFTD3Calculator(atoms=atoms, device="cpu", damping="bj")
    
        energy = atoms.get_potential_energy()
        forces = atoms.get_forces()
    
        print(f"energy {energy} eV")
        print(f"forces {forces}")
    
    
    Traceback (most recent call last):
      File "quick.py", line 12, in <module>
        energy = atoms.get_potential_energy()
      File "/home/ahayashi/envs/dftd/lib/python3.8/site-packages/ase/atoms.py", line 731, in get_potential_energy
        energy = self._calc.get_potential_energy(self)
      File "/home/ahayashi/envs/dftd/lib/python3.8/site-packages/ase/calculators/calculator.py", line 709, in get_potential_energy
        energy = self.get_property('energy', atoms)
      File "/home/ahayashi/torch-dftd/torch_dftd/torch_dftd3_calculator.py", line 141, in get_property
        dftd3_result = Calculator.get_property(self, name, atoms, allow_calculation)
      File "/home/ahayashi/envs/dftd/lib/python3.8/site-packages/ase/calculators/calculator.py", line 737, in get_property
        self.calculate(atoms, [name], system_changes)
      File "/home/ahayashi/torch-dftd/torch_dftd/torch_dftd3_calculator.py", line 119, in calculate
        results = self.dftd_module.calc_energy(**input_dicts, damping=self.damping)[0]
      File "/home/ahayashi/torch-dftd/torch_dftd/nn/base_dftd_module.py", line 75, in calc_energy
        E_disp = self.calc_energy_batch(
      File "/home/ahayashi/torch-dftd/torch_dftd/nn/dftd3_module.py", line 86, in calc_energy_batch
        E_disp = d3_autoev * edisp(
      File "/home/ahayashi/torch-dftd/torch_dftd/functions/dftd3.py", line 189, in edisp
        c6 = _getc6(Zi, Zj, nci, ncj, c6ab=c6ab, k3=k3)  # c6 coefficients
      File "/home/ahayashi/torch-dftd/torch_dftd/functions/dftd3.py", line 97, in _getc6
        k3_rnc = torch.where(cn0 > 0.0, k3 * r, -1.0e20 * torch.ones_like(r)).view(n_edges, -1)
    RuntimeError: cannot reshape tensor of 0 elements into shape [0, -1] because the unspecified dimension size -1 can be any value and is ambiguous
    
    opened by AkihideHayashi 1
  • use shift for gradient calculation instead of cell

    use shift for gradient calculation instead of cell

    I found that the current implementation has several performance issues regarding gradient wrt. cell. This PR modifies it. Since the changes are relatively much, I will put some comments.

    Change summary:

    • Use shift for gradient instead of cell.
    • shift is now length scale instead cell unit.
    • Calculate Voigt notation style stress directly

    Also, this PR contains bugfix related to sked cell.

    bug enhancement 
    opened by So-Takamoto 0
  • Bugfix: batch calculation with abc=True

    Bugfix: batch calculation with abc=True

    I found that test function test_calc_energy_force_stress_device_batch_abc unintentionally ignores abc argument.

    This PR modified related implementation to work it.

    In addition, corner case correspondence when the total number of atom is zero is also added. (n_graphs cannot be calculated from batch_edge when len(batch_edge) == 0.)

    bug 
    opened by So-Takamoto 0
  • Fixed a bug for inputs with 0 adjacencies.

    Fixed a bug for inputs with 0 adjacencies.

    The _gettc6 routine now works correctly even when the number of adjacencies is 0. Instead of calling calc_neighbor_by_pymatgen when the number of atoms is 0 and the periodic boundary condition, it now return edge_index, S for adjacency 0. In my environment, using the result of torch.sum for the size of torch.zeros caused an error, so I changed it to cast the result of sum to int.

    bug 
    opened by AkihideHayashi 0
  •  Bug in test for stress

    Bug in test for stress

    In test_torch_dftd3_calculator.py/_assert_energy_force_stress_equal, there is a code below.

        if np.all(atoms.pbc == np.array([True, True, True])):
            s1 = atoms.get_stress()
            s2 = atoms.get_stress()
            assert np.allclose(s1, s2, atol=1e-5, rtol=1e-5)
    

    This code cannot compare the results of stresses of calc1 and calc2. Both s1 and s2 are the stress of calc2.

    opened by AkihideHayashi 0
Releases(v0.3.0)
  • v0.3.0(Apr 25, 2022)

    This is the release note of v0.3.0.

    Highlights

    • use shift for gradient calculation instead of cell #13 (Thank you @So-Takamoto )
      • It includes 1. speed up of stress calculation for batch atoms, and 2. bug fix for stress calculation when cell is skewed.
    Source code(tar.gz)
    Source code(zip)
  • v0.2.0(Sep 4, 2021)

    This is the release note of v0.2.0.

    Highlights

    • Add PFP citation in README.md #2
    • Use pymatgen for pbc neighbor search speed up #3

    Bug fixes

    • Fixed a bug for inputs with 0 adjacencies. #6 (Thank you @AkihideHayashi )
    • Remove RuntimeError on no-cupy environment #8 (Thank you @So-Takamoto )
    • Bugfix: batch calculation with abc=True #9 (Thank you @So-Takamoto )

    Others

    • move pysen to develop dependency #10 (Thank you @So-Takamoto )
    Source code(tar.gz)
    Source code(zip)
  • v0.1.0(May 10, 2021)

Owner
null
An essential implementation of BYOL in PyTorch + PyTorch Lightning

Essential BYOL A simple and complete implementation of Bootstrap your own latent: A new approach to self-supervised Learning in PyTorch + PyTorch Ligh

Enrico Fini 48 Sep 27, 2022
RealFormer-Pytorch Implementation of RealFormer using pytorch

RealFormer-Pytorch Implementation of RealFormer using pytorch. Includes comparison with classical Transformer on image classification task (ViT) wrt C

Simo Ryu 90 Dec 8, 2022
A PyTorch implementation of the paper Mixup: Beyond Empirical Risk Minimization in PyTorch

Mixup: Beyond Empirical Risk Minimization in PyTorch This is an unofficial PyTorch implementation of mixup: Beyond Empirical Risk Minimization. The co

Harry Yang 121 Dec 17, 2022
A pytorch implementation of Pytorch-Sketch-RNN

Pytorch-Sketch-RNN A pytorch implementation of https://arxiv.org/abs/1704.03477 In order to draw other things than cats, you will find more drawing da

Alexis David Jacq 172 Dec 12, 2022
PyTorch implementation of Advantage async actor-critic Algorithms (A3C) in PyTorch

Advantage async actor-critic Algorithms (A3C) in PyTorch @inproceedings{mnih2016asynchronous, title={Asynchronous methods for deep reinforcement lea

LEI TAI 111 Dec 8, 2022
Pytorch-diffusion - A basic PyTorch implementation of 'Denoising Diffusion Probabilistic Models'

PyTorch implementation of 'Denoising Diffusion Probabilistic Models' This reposi

Arthur Juliani 76 Jan 7, 2023
Fang Zhonghao 13 Nov 19, 2022
RETRO-pytorch - Implementation of RETRO, Deepmind's Retrieval based Attention net, in Pytorch

RETRO - Pytorch (wip) Implementation of RETRO, Deepmind's Retrieval based Attent

Phil Wang 556 Jan 4, 2023
HashNeRF-pytorch - Pure PyTorch Implementation of NVIDIA paper on Instant Training of Neural Graphics primitives

HashNeRF-pytorch Instant-NGP recently introduced a Multi-resolution Hash Encodin

Yash Sanjay Bhalgat 616 Jan 6, 2023
Generic template to bootstrap your PyTorch project with PyTorch Lightning, Hydra, W&B, and DVC.

NN Template Generic template to bootstrap your PyTorch project. Click on Use this Template and avoid writing boilerplate code for: PyTorch Lightning,

Luca Moschella 520 Dec 30, 2022
A PyTorch Extension: Tools for easy mixed precision and distributed training in Pytorch

This repository holds NVIDIA-maintained utilities to streamline mixed precision and distributed training in Pytorch. Some of the code here will be included in upstream Pytorch eventually. The intention of Apex is to make up-to-date utilities available to users as quickly as possible.

NVIDIA Corporation 6.9k Jan 3, 2023
Objective of the repository is to learn and build machine learning models using Pytorch. 30DaysofML Using Pytorch

30 Days Of Machine Learning Using Pytorch Objective of the repository is to learn and build machine learning models using Pytorch. List of Algorithms

Mayur 119 Nov 24, 2022
Pretrained SOTA Deep Learning models, callbacks and more for research and production with PyTorch Lightning and PyTorch

Pretrained SOTA Deep Learning models, callbacks and more for research and production with PyTorch Lightning and PyTorch

Pytorch Lightning 1.4k Jan 1, 2023
Amazon Forest Computer Vision: Satellite Image tagging code using PyTorch / Keras with lots of PyTorch tricks

Amazon Forest Computer Vision Satellite Image tagging code using PyTorch / Keras Here is a sample of images we had to work with Source: https://www.ka

Mamy Ratsimbazafy 360 Dec 10, 2022
The Incredible PyTorch: a curated list of tutorials, papers, projects, communities and more relating to PyTorch.

This is a curated list of tutorials, projects, libraries, videos, papers, books and anything related to the incredible PyTorch. Feel free to make a pu

Ritchie Ng 9.2k Jan 2, 2023
Amazon Forest Computer Vision: Satellite Image tagging code using PyTorch / Keras with lots of PyTorch tricks

Amazon Forest Computer Vision Satellite Image tagging code using PyTorch / Keras Here is a sample of images we had to work with Source: https://www.ka

Mamy Ratsimbazafy 359 Jan 5, 2023
A bunch of random PyTorch models using PyTorch's C++ frontend

PyTorch Deep Learning Models using the C++ frontend Gettting started Clone the repo 1. https://github.com/mrdvince/pytorchcpp 2. cd fashionmnist or

Vince 0 Jul 13, 2021
PyTorch Autoencoders - Implementing a Variational Autoencoder (VAE) Series in Pytorch.

PyTorch Autoencoders Implementing a Variational Autoencoder (VAE) Series in Pytorch. Inspired by this repository Model List check model paper conferen

Subin An 8 Nov 21, 2022
PyTorch-LIT is the Lite Inference Toolkit (LIT) for PyTorch which focuses on easy and fast inference of large models on end-devices.

PyTorch-LIT PyTorch-LIT is the Lite Inference Toolkit (LIT) for PyTorch which focuses on easy and fast inference of large models on end-devices. With

Amin Rezaei 157 Dec 11, 2022