Skip to content

Commit 6b5cdc6

Browse files
authored
Merge pull request #121 from njzjz/ase
add an ASE calculator interface
2 parents 9b6e58d + e69d279 commit 6b5cdc6

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- [Include deepmd in the pair style](#include-deepmd-in-the-pair-style)
3333
- [Long-range interaction](#long-range-interaction)
3434
- [Run path-integral MD with i-PI](#run-path-integral-md-with-i-pi)
35+
- [Use deep potential with ASE](#use-deep-potential-with-ase)
3536
- [Troubleshooting](#troubleshooting)
3637

3738
# About DeePMD-kit
@@ -553,6 +554,30 @@ The option **`graph_file`** provides the file name of the frozen model.
553554

554555
The `dp_ipi` gets the atom names from an [XYZ file](https://en.wikipedia.org/wiki/XYZ_file_format) provided by **`coord_file`** (meanwhile ignores all coordinates in it), and translates the names to atom types by rules provided by **`atom_type`**.
555556

557+
## Use deep potential with ASE
558+
559+
Deep potential can be set up as a calculator with ASE to obtain potential energies and forces.
560+
```python
561+
from ase import Atoms
562+
from deepmd.calculator import DP
563+
564+
water = Atoms('H2O',
565+
positions=[(0.7601, 1.9270, 1),
566+
(1.9575, 1, 1),
567+
(1., 1., 1.)],
568+
cell=[100, 100, 100],
569+
calculator=DP(model="frozen_model.pb"))
570+
print(water.get_potential_energy())
571+
print(water.get_forces())
572+
```
573+
574+
Optimization is also available:
575+
```python
576+
from ase.optimize import BFGS
577+
dyn = BFGS(water)
578+
dyn.run(fmax=1e-6)
579+
print(water.get_positions())
580+
```
556581

557582
# Troubleshooting
558583
In consequence of various differences of computers or systems, problems may occur. Some common circumstances are listed as follows.

source/train/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
configure_file("RunOptions.py.in" "${CMAKE_CURRENT_BINARY_DIR}/RunOptions.py" @ONLY)
44

5-
file(GLOB LIB_PY main.py common.py env.py compat.py Network.py Deep*.py Data.py DataSystem.py Model*.py Descrpt*.py Fitting.py Loss.py LearningRate.py Trainer.py TabInter.py ${CMAKE_CURRENT_BINARY_DIR}/RunOptions.py)
5+
file(GLOB LIB_PY main.py common.py env.py compat.py calculator.py Network.py Deep*.py Data.py DataSystem.py Model*.py Descrpt*.py Fitting.py Loss.py LearningRate.py Trainer.py TabInter.py ${CMAKE_CURRENT_BINARY_DIR}/RunOptions.py)
66

77
file(GLOB CLS_PY Local.py Slurm.py)
88

source/train/calculator.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""An ASE calculator interface.
2+
3+
Example:
4+
```python
5+
from ase import Atoms
6+
from deepmd.calculator import DP
7+
8+
water = Atoms('H2O',
9+
positions=[(0.7601, 1.9270, 1),
10+
(1.9575, 1, 1),
11+
(1., 1., 1.)],
12+
cell=[100, 100, 100],
13+
calculator=DP(model="frozen_model.pb"))
14+
print(water.get_potential_energy())
15+
print(water.get_forces())
16+
```
17+
18+
Optimization is also available:
19+
```python
20+
from ase.optimize import BFGS
21+
dyn = BFGS(water)
22+
dyn.run(fmax=1e-6)
23+
print(water.get_positions())
24+
```
25+
"""
26+
27+
from ase.calculators.calculator import Calculator, all_changes
28+
import deepmd.DeepPot as DeepPot
29+
30+
31+
class DP(Calculator):
32+
name = "DP"
33+
implemented_properties = ["energy", "forces", "stress"]
34+
35+
def __init__(self, model, label="DP", **kwargs):
36+
Calculator.__init__(self, label=label, **kwargs)
37+
self.dp = DeepPot(model)
38+
self.type_dict = dict(zip(self.dp.get_type_map(), range(self.dp.get_ntypes())))
39+
40+
def calculate(self, atoms=None, properties=["energy", "forces", "stress"], system_changes=all_changes):
41+
coord = atoms.get_positions().reshape([1, -1])
42+
cell = atoms.get_cell().reshape([1, -1])
43+
symbols = atoms.get_chemical_symbols()
44+
atype = [self.type_dict[k] for k in symbols]
45+
e, f, v = self.dp.eval(coord, cell, atype)
46+
self.results['energy'] = e[0]
47+
self.results['forces'] = f[0]
48+
self.results['stress'] = v[0]
49+

0 commit comments

Comments
 (0)