Skip to content

Commit 9c24606

Browse files
authored
Merge pull request #5 from deepmodeling/devel
devel, up to date
2 parents 036a31a + 0de1276 commit 9c24606

File tree

8 files changed

+106
-11
lines changed

8 files changed

+106
-11
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/lib/include/NNPInter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ class NNPInterModelDevi
200200
void compute_std_f (vector<VALUETYPE> & std,
201201
const vector<VALUETYPE> & avg,
202202
const vector<vector<VALUETYPE> >& xx);
203+
void compute_relative_std_f (vector<VALUETYPE> & std,
204+
const vector<VALUETYPE> & avg,
205+
const VALUETYPE eps);
203206
private:
204207
unsigned numb_models;
205208
vector<Session*> sessions;

source/lib/src/NNPInter.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,3 +1690,22 @@ compute_std_f (vector<VALUETYPE> & std,
16901690
}
16911691
}
16921692

1693+
void
1694+
NNPInterModelDevi::
1695+
compute_relative_std_f (vector<VALUETYPE> &std,
1696+
const vector<VALUETYPE> &avg,
1697+
const VALUETYPE eps)
1698+
{
1699+
unsigned nloc = std.size();
1700+
for (unsigned ii = 0; ii < nloc; ++ii){
1701+
const VALUETYPE * tmp_avg = &(avg[ii*3]);
1702+
VALUETYPE vdiff[3];
1703+
vdiff[0] = tmp_avg[0];
1704+
vdiff[1] = tmp_avg[1];
1705+
vdiff[2] = tmp_avg[2];
1706+
VALUETYPE f_norm = sqrt(MathUtilities::dot(vdiff, vdiff));
1707+
// relative std = std/(abs(f)+eps)
1708+
std[ii] /= f_norm + eps;
1709+
}
1710+
}
1711+

source/lmp/pair_nnp.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,9 @@ void PairNNP::compute(int eflag, int vflag)
450450
vector<double> tmp_avg_f;
451451
nnp_inter_model_devi.compute_avg (tmp_avg_f, all_force);
452452
nnp_inter_model_devi.compute_std_f (std_f, tmp_avg_f, all_force);
453+
if (out_rel == 1){
454+
nnp_inter_model_devi.compute_relative_std_f (std_f, tmp_avg_f, eps);
455+
}
453456
#else
454457
vector<float> tmp_avg_f_, std_f_;
455458
for (unsigned ii = 0; ii < all_force_.size(); ++ii){
@@ -461,6 +464,9 @@ void PairNNP::compute(int eflag, int vflag)
461464
nnp_inter_model_devi.compute_std_f (std_f_, tmp_avg_f_, all_force_);
462465
std_f.resize(std_f_.size());
463466
for (int dd = 0; dd < std_f_.size(); ++dd) std_f[dd] = std_f_[dd];
467+
if (out_rel == 1){
468+
nnp_inter_model_devi.compute_relative_std_f (std_f, tmp_avg_f_, eps);
469+
}
464470
#endif
465471
double min = numeric_limits<double>::max(), max = 0, avg = 0;
466472
ana_st(max, min, avg, std_f, nlocal);
@@ -514,16 +520,7 @@ void PairNNP::compute(int eflag, int vflag)
514520
// 1. If the atom_style is not atomic (e.g. charge), the order of std_f is different from that of atom ids.
515521
// 2. std_f is not gathered by MPI.
516522
for (int dd = 0; dd < all_nlocal; ++dd) {
517-
if (out_rel == 1){
518-
// relative std = std/(abs(f)+1)
519-
#ifdef HIGH_PREC
520-
fp << " " << setw(18) << std_f[dd] / (fabs(tmp_avg_f[dd]) + eps);
521-
#else
522-
fp << " " << setw(18) << std_f[dd] / (fabsf(tmp_avg_f_[dd]) + eps);
523-
#endif
524-
} else {
525523
fp << " " << setw(18) << std_f[dd];
526-
}
527524
}
528525
}
529526
fp << endl;

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/DescrptSeAR.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def build (self,
7272
reuse = None):
7373
# dout
7474
self.dout_a = self.descrpt_a.build(coord_, atype_, natoms, box, mesh, davg[0], dstd[0], suffix=suffix+'_a', reuse=reuse)
75-
self.dout_r = self.descrpt_r.build(coord_, atype_, natoms, box, mesh, davg[1], dstd[1], suffix=suffix+'_r', reuse=reuse)
75+
self.dout_r = self.descrpt_r.build(coord_, atype_, natoms, box, mesh, davg[1], dstd[1], suffix=suffix, reuse=reuse)
7676
self.dout_a = tf.reshape(self.dout_a, [-1, self.descrpt_a.get_dim_out()])
7777
self.dout_r = tf.reshape(self.dout_r, [-1, self.descrpt_r.get_dim_out()])
7878
self.dout = tf.concat([self.dout_a, self.dout_r], axis = 1)

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+

source/train/common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
data_requirement = {}
24

35
def add_data_requirement(key,

0 commit comments

Comments
 (0)