Skip to content

Commit 4f3ee8e

Browse files
committed
get type_map from the model; add readme
1 parent 0f5ce53 commit 4f3ee8e

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
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/calculator.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
(1.9575, 1, 1),
1111
(1., 1., 1.)],
1212
cell=[100, 100, 100],
13-
calculator=DP(model="frozen_model.pb", type_map=['O', 'H']))
13+
calculator=DP(model="frozen_model.pb"))
1414
print(water.get_potential_energy())
1515
print(water.get_forces())
1616
```
@@ -25,23 +25,22 @@
2525
"""
2626

2727
from ase.calculators.calculator import Calculator, all_changes
28-
import deepmd.DeepPot as DPinference
28+
import deepmd.DeepPot as DeepPot
2929

3030

3131
class DP(Calculator):
3232
name = "DP"
3333
implemented_properties = ["energy", "forces", "stress"]
3434

35-
def __init__(self, model, type_map, label="DP", **kwargs):
35+
def __init__(self, model, label="DP", **kwargs):
3636
Calculator.__init__(self, label=label, **kwargs)
37-
self.dp = DPinference(model)
38-
self.type_map = type_map
37+
self.dp = DeepPot(model)
3938

4039
def calculate(self, atoms=None, properties=["energy", "forces", "stress"], system_changes=all_changes):
4140
coord = atoms.get_positions().reshape([1, -1])
4241
cell = atoms.get_cell().reshape([1, -1])
4342
symbols = atoms.get_chemical_symbols()
44-
type_dict = dict(zip(self.type_map, range(len(self.type_map))))
43+
type_dict = dict(zip(self.dp.get_type_map(), range(self.dp.get_ntypes())))
4544
atype = [type_dict[k] for k in symbols]
4645
e, f, v = self.dp.eval(coord, cell, atype)
4746
self.results['energy'] = e[0]

0 commit comments

Comments
 (0)