forked from deepmodeling/deepmd-kit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_fitting_invar_fitting.py
176 lines (162 loc) · 5.17 KB
/
test_fitting_invar_fitting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# SPDX-License-Identifier: LGPL-3.0-or-later
import itertools
import unittest
import numpy as np
from deepmd.dpmodel.descriptor import (
DescrptSeA,
)
from deepmd.dpmodel.fitting import (
InvarFitting,
)
from ...seed import (
GLOBAL_SEED,
)
from .case_single_frame_with_nlist import (
TestCaseSingleFrameWithNlist,
)
class TestInvarFitting(unittest.TestCase, TestCaseSingleFrameWithNlist):
def setUp(self):
TestCaseSingleFrameWithNlist.setUp(self)
def test_self_consistency(
self,
):
rng = np.random.default_rng(GLOBAL_SEED)
nf, nloc, nnei = self.nlist.shape
ds = DescrptSeA(self.rcut, self.rcut_smth, self.sel)
dd = ds.call(self.coord_ext, self.atype_ext, self.nlist)
atype = self.atype_ext[:, :nloc]
for (
mixed_types,
od,
nfp,
nap,
et,
) in itertools.product(
[True, False],
[1, 2],
[0, 3],
[0, 4],
[[], [0], [1]],
):
ifn0 = InvarFitting(
"energy",
self.nt,
ds.dim_out,
od,
numb_fparam=nfp,
numb_aparam=nap,
mixed_types=mixed_types,
exclude_types=et,
)
ifn1 = InvarFitting.deserialize(ifn0.serialize())
if nfp > 0:
ifp = rng.normal(size=(self.nf, nfp))
else:
ifp = None
if nap > 0:
iap = rng.normal(size=(self.nf, self.nloc, nap))
else:
iap = None
ret0 = ifn0(dd[0], atype, fparam=ifp, aparam=iap)
ret1 = ifn1(dd[0], atype, fparam=ifp, aparam=iap)
np.testing.assert_allclose(ret0["energy"], ret1["energy"])
sel_set = set(ifn0.get_sel_type())
exclude_set = set(et)
self.assertEqual(sel_set | exclude_set, set(range(self.nt)))
self.assertEqual(sel_set & exclude_set, set())
def test_mask(self):
nf, nloc, nnei = self.nlist.shape
ds = DescrptSeA(self.rcut, self.rcut_smth, self.sel)
dd = ds.call(self.coord_ext, self.atype_ext, self.nlist)
atype = self.atype_ext[:, :nloc]
od = 2
mixed_types = True
# exclude type 1
et = [1]
ifn0 = InvarFitting(
"energy",
self.nt,
ds.dim_out,
od,
mixed_types=mixed_types,
exclude_types=et,
)
ret0 = ifn0(dd[0], atype)
# atom index 2 is of type 1 that is excluded
zero_idx = 2
np.testing.assert_allclose(
ret0["energy"][0, zero_idx, :],
np.zeros_like(ret0["energy"][0, zero_idx, :]),
)
zero_idx = 0
np.testing.assert_allclose(
ret0["energy"][1, zero_idx, :],
np.zeros_like(ret0["energy"][1, zero_idx, :]),
)
def test_self_exception(
self,
):
rng = np.random.default_rng(GLOBAL_SEED)
nf, nloc, nnei = self.nlist.shape
ds = DescrptSeA(self.rcut, self.rcut_smth, self.sel)
dd = ds.call(self.coord_ext, self.atype_ext, self.nlist)
atype = self.atype_ext[:, :nloc]
for (
mixed_types,
od,
nfp,
nap,
) in itertools.product(
[True, False],
[1, 2],
[0, 3],
[0, 4],
):
ifn0 = InvarFitting(
"energy",
self.nt,
ds.dim_out,
od,
numb_fparam=nfp,
numb_aparam=nap,
mixed_types=mixed_types,
)
if nfp > 0:
ifp = rng.normal(size=(self.nf, nfp))
else:
ifp = None
if nap > 0:
iap = rng.normal(size=(self.nf, self.nloc, nap))
else:
iap = None
with self.assertRaises(ValueError) as context:
ret0 = ifn0(dd[0][:, :, :-2], atype, fparam=ifp, aparam=iap)
self.assertIn("input descriptor", context.exception)
if nfp > 0:
ifp = rng.normal(size=(self.nf, nfp - 1))
with self.assertRaises(ValueError) as context:
ret0 = ifn0(dd[0], atype, fparam=ifp, aparam=iap)
self.assertIn("input fparam", context.exception)
if nap > 0:
iap = rng.normal(size=(self.nf, self.nloc, nap - 1))
with self.assertRaises(ValueError) as context:
ifn0(dd[0], atype, fparam=ifp, aparam=iap)
self.assertIn("input aparam", context.exception)
def test_get_set(self):
ifn0 = InvarFitting(
"energy",
self.nt,
3,
1,
)
rng = np.random.default_rng(GLOBAL_SEED)
foo = rng.normal([3, 4])
for ii in [
"bias_atom_e",
"fparam_avg",
"fparam_inv_std",
"aparam_avg",
"aparam_inv_std",
]:
ifn0[ii] = foo
np.testing.assert_allclose(foo, ifn0[ii])