From f9663566b7bb2021aaf76dead30549aa84e4f076 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sun, 9 Jul 2023 19:06:50 -0400 Subject: [PATCH 1/4] support fparam/aparam in dp model-devi --- deepmd/infer/model_devi.py | 28 ++++++- deepmd/utils/data.py | 2 +- .../system_fparam_aparam/set.000/aparam.npy | Bin 0 -> 176 bytes .../system_fparam_aparam/set.000/box.npy | Bin 0 -> 200 bytes .../system_fparam_aparam/set.000/coord.npy | Bin 0 -> 272 bytes .../system_fparam_aparam/set.000/energy.npy | Bin 0 -> 136 bytes .../system_fparam_aparam/set.000/force.npy | Bin 0 -> 272 bytes .../system_fparam_aparam/set.000/fparam.npy | Bin 0 -> 136 bytes source/tests/system_fparam_aparam/type.raw | 6 ++ .../tests/system_fparam_aparam/type_map.raw | 1 + source/tests/test_model_devi.py | 69 ++++++++++++++++++ 11 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 source/tests/system_fparam_aparam/set.000/aparam.npy create mode 100644 source/tests/system_fparam_aparam/set.000/box.npy create mode 100644 source/tests/system_fparam_aparam/set.000/coord.npy create mode 100644 source/tests/system_fparam_aparam/set.000/energy.npy create mode 100644 source/tests/system_fparam_aparam/set.000/force.npy create mode 100644 source/tests/system_fparam_aparam/set.000/fparam.npy create mode 100644 source/tests/system_fparam_aparam/type.raw create mode 100644 source/tests/system_fparam_aparam/type_map.raw diff --git a/deepmd/infer/model_devi.py b/deepmd/infer/model_devi.py index bc80ac78f6..2f5fc02e05 100644 --- a/deepmd/infer/model_devi.py +++ b/deepmd/infer/model_devi.py @@ -1,6 +1,7 @@ # SPDX-License-Identifier: LGPL-3.0-or-later from typing import ( Tuple, + Optional, ) import numpy as np @@ -145,6 +146,8 @@ def calc_model_devi( fname=None, frequency=1, mixed_type=False, + fparam : Optional[np.ndarray] = None, + aparam : Optional[np.ndarray] = None, ): """Python interface to calculate model deviation. @@ -164,6 +167,10 @@ def calc_model_devi( Steps between frames (if the system is given by molecular dynamics engine), default 1 mixed_type : bool Whether the input atype is in mixed_type format or not + fparam : numpy.ndarray + frame specific parameters + aparam : numpy.ndarray + atomic specific parameters Returns ------- @@ -191,6 +198,8 @@ def calc_model_devi( coord, box, atype, + fparam=fparam, + aparam=aparam, mixed_type=mixed_type, ) energies.append(ret[0] / natom) @@ -248,9 +257,18 @@ def make_model_devi( if len(all_sys) == 0: raise RuntimeError("Did not find valid system") devis_coll = [] + + first_dp = dp_models[0] + for system in all_sys: # create data-system dp_data = DeepmdData(system, set_prefix, shuffle_test=False, type_map=tmap) + if first_dp.get_dim_fparam() > 0: + dp_data.add( + "fparam", first_dp.get_dim_fparam(), atomic=False, must=True, high_prec=False + ) + if first_dp.get_dim_aparam() > 0: + dp_data.add("aparam", first_dp.get_dim_aparam(), atomic=True, must=True, high_prec=False) mixed_type = dp_data.mixed_type data_sets = [dp_data._load_set(set_name) for set_name in dp_data.dirs] @@ -265,7 +283,15 @@ def make_model_devi( atype = data["type"][0] if not dp_data.pbc: box = None - devi = calc_model_devi(coord, box, atype, dp_models, mixed_type=mixed_type) + if first_dp.get_dim_fparam() > 0: + fparam = data["fparam"] + else: + fparam = None + if first_dp.get_dim_aparam() > 0: + aparam = data["aparam"] + else: + aparam = None + devi = calc_model_devi(coord, box, atype, dp_models, mixed_type=mixed_type, fparam=fparam, aparam=aparam) nframes_tot += coord.shape[0] devis.append(devi) devis = np.vstack(devis) diff --git a/deepmd/utils/data.py b/deepmd/utils/data.py index 24042444c8..8442f84156 100644 --- a/deepmd/utils/data.py +++ b/deepmd/utils/data.py @@ -82,7 +82,7 @@ def __init__( self.pbc = self._check_pbc(root) # enforce type_map if necessary self.enforce_type_map = False - if type_map is not None and self.type_map is not None: + if type_map is not None and self.type_map is not None and len(type_map): if not self.mixed_type: atom_type_ = [ type_map.index(self.type_map[ii]) for ii in self.atom_type diff --git a/source/tests/system_fparam_aparam/set.000/aparam.npy b/source/tests/system_fparam_aparam/set.000/aparam.npy new file mode 100644 index 0000000000000000000000000000000000000000..da59918dad40fcc05a13c7a781855e2780b5b9da GIT binary patch literal 176 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlV+i=qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= fXCxM+0{I$-Itpf*ItsN4WCN~QGeKadJ+U+ZA|5u( literal 0 HcmV?d00001 diff --git a/source/tests/system_fparam_aparam/set.000/box.npy b/source/tests/system_fparam_aparam/set.000/box.npy new file mode 100644 index 0000000000000000000000000000000000000000..f70e95b6e772550e087d1aba097f26b3b380704e GIT binary patch literal 200 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlV+i=qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= hXCxM+0{I$-ItrGWItsN4WCJb+Ffeg|(6~s#@&G_89MJ#( literal 0 HcmV?d00001 diff --git a/source/tests/system_fparam_aparam/set.000/coord.npy b/source/tests/system_fparam_aparam/set.000/coord.npy new file mode 100644 index 0000000000000000000000000000000000000000..27a06e944251c351ad110c8c69cc764c2fcd84db GIT binary patch literal 272 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlV+i=qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= zXCxM+0{I$-ItqpsnmP)#3giN=`6m;X{f%5|zg$iK+xCJ@_C<$GYa?gvv_GBq Date: Sun, 9 Jul 2023 23:07:37 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- deepmd/infer/model_devi.py | 32 +++++++++++++++++++++++++------- source/tests/test_model_devi.py | 4 +--- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/deepmd/infer/model_devi.py b/deepmd/infer/model_devi.py index 2f5fc02e05..0274384188 100644 --- a/deepmd/infer/model_devi.py +++ b/deepmd/infer/model_devi.py @@ -1,7 +1,7 @@ # SPDX-License-Identifier: LGPL-3.0-or-later from typing import ( - Tuple, Optional, + Tuple, ) import numpy as np @@ -146,8 +146,8 @@ def calc_model_devi( fname=None, frequency=1, mixed_type=False, - fparam : Optional[np.ndarray] = None, - aparam : Optional[np.ndarray] = None, + fparam: Optional[np.ndarray] = None, + aparam: Optional[np.ndarray] = None, ): """Python interface to calculate model deviation. @@ -265,10 +265,20 @@ def make_model_devi( dp_data = DeepmdData(system, set_prefix, shuffle_test=False, type_map=tmap) if first_dp.get_dim_fparam() > 0: dp_data.add( - "fparam", first_dp.get_dim_fparam(), atomic=False, must=True, high_prec=False - ) + "fparam", + first_dp.get_dim_fparam(), + atomic=False, + must=True, + high_prec=False, + ) if first_dp.get_dim_aparam() > 0: - dp_data.add("aparam", first_dp.get_dim_aparam(), atomic=True, must=True, high_prec=False) + dp_data.add( + "aparam", + first_dp.get_dim_aparam(), + atomic=True, + must=True, + high_prec=False, + ) mixed_type = dp_data.mixed_type data_sets = [dp_data._load_set(set_name) for set_name in dp_data.dirs] @@ -291,7 +301,15 @@ def make_model_devi( aparam = data["aparam"] else: aparam = None - devi = calc_model_devi(coord, box, atype, dp_models, mixed_type=mixed_type, fparam=fparam, aparam=aparam) + devi = calc_model_devi( + coord, + box, + atype, + dp_models, + mixed_type=mixed_type, + fparam=fparam, + aparam=aparam, + ) nframes_tot += coord.shape[0] devis.append(devi) devis = np.vstack(devis) diff --git a/source/tests/test_model_devi.py b/source/tests/test_model_devi.py index e25449b1f8..5249365187 100644 --- a/source/tests/test_model_devi.py +++ b/source/tests/test_model_devi.py @@ -12,7 +12,6 @@ from deepmd.infer.model_devi import ( make_model_devi, ) -from deepmd.common import data_requirement sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) from common import ( @@ -96,6 +95,7 @@ def tearDown(self): class TestMakeModelDeviFparamAparam(unittest.TestCase): """Ensure dp model_devi accepts fparam and aparam.""" + @classmethod def setUpClass(cls): cls.pbtxts = [ @@ -112,7 +112,6 @@ def tearDownClass(cls): os.remove(pb) cls.graphs = None - def setUp(self): gen_data() self.data_dir = "system_fparam_aparam" @@ -128,7 +127,6 @@ def setUp(self): self.fparam = np.repeat([0.25852028], self.box.size / 9) self.aparam = np.repeat(self.fparam, self.atype.size) - def test_calc_model_devi(self): model_devi = calc_model_devi( self.coord, From 42603095d5e44e4efdbc7d3f310ecb3488660502 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sun, 9 Jul 2023 19:56:50 -0400 Subject: [PATCH 3/4] fix tests --- source/tests/common.py | 32 +++++++++--------- .../system_fparam_aparam/set.000/aparam.npy | Bin 176 -> 0 bytes .../system_fparam_aparam/set.000/box.npy | Bin 200 -> 0 bytes .../system_fparam_aparam/set.000/coord.npy | Bin 272 -> 0 bytes .../system_fparam_aparam/set.000/energy.npy | Bin 136 -> 0 bytes .../system_fparam_aparam/set.000/force.npy | Bin 272 -> 0 bytes .../system_fparam_aparam/set.000/fparam.npy | Bin 136 -> 0 bytes source/tests/system_fparam_aparam/type.raw | 6 ---- .../tests/system_fparam_aparam/type_map.raw | 1 - source/tests/test_model_devi.py | 13 ++++--- 10 files changed, 24 insertions(+), 28 deletions(-) delete mode 100644 source/tests/system_fparam_aparam/set.000/aparam.npy delete mode 100644 source/tests/system_fparam_aparam/set.000/box.npy delete mode 100644 source/tests/system_fparam_aparam/set.000/coord.npy delete mode 100644 source/tests/system_fparam_aparam/set.000/energy.npy delete mode 100644 source/tests/system_fparam_aparam/set.000/force.npy delete mode 100644 source/tests/system_fparam_aparam/set.000/fparam.npy delete mode 100644 source/tests/system_fparam_aparam/type.raw delete mode 100644 source/tests/system_fparam_aparam/type_map.raw diff --git a/source/tests/common.py b/source/tests/common.py index e1788bb942..6680c936d2 100644 --- a/source/tests/common.py +++ b/source/tests/common.py @@ -41,8 +41,8 @@ def del_data(): shutil.rmtree("system_mixed_type") -def gen_data_type_specific(nframes=1): - tmpdata = Data(rand_pert=0.1, seed=1, nframes=nframes) +def gen_data_type_specific(nframes=1, dim_fparam=2): + tmpdata = Data(rand_pert=0.1, seed=1, nframes=nframes, dim_fparam=dim_fparam) sys = dpdata.LabeledSystem() sys.data["atom_names"] = ["foo", "bar"] sys.data["coords"] = tmpdata.coord @@ -56,11 +56,11 @@ def gen_data_type_specific(nframes=1): sys.data["forces"] = np.zeros([nframes, natoms, 3]) sys.to_deepmd_npy("system", prec=np.float64) np.save("system/set.000/fparam.npy", tmpdata.fparam) - np.save("system/set.000/aparam.npy", tmpdata.aparam.reshape([nframes, natoms, 2])) + np.save("system/set.000/aparam.npy", tmpdata.aparam.reshape([nframes, natoms, dim_fparam])) -def gen_data_mixed_type(nframes=1): - tmpdata = Data(rand_pert=0.1, seed=1, nframes=nframes) +def gen_data_mixed_type(nframes=1, dim_fparam=2): + tmpdata = Data(rand_pert=0.1, seed=1, nframes=nframes, dim_fparam=dim_fparam) sys = dpdata.LabeledSystem() real_type_map = ["foo", "bar"] sys.data["atom_names"] = ["X"] @@ -82,12 +82,12 @@ def gen_data_mixed_type(nframes=1): np.save("system_mixed_type/set.000/fparam.npy", tmpdata.fparam) np.save( "system_mixed_type/set.000/aparam.npy", - tmpdata.aparam.reshape([nframes, natoms, 2]), + tmpdata.aparam.reshape([nframes, natoms, dim_fparam]), ) -def gen_data_virtual_type(nframes=1, nghost=4): - tmpdata = Data(rand_pert=0.1, seed=1, nframes=nframes) +def gen_data_virtual_type(nframes=1, nghost=4, dim_fparam=2): + tmpdata = Data(rand_pert=0.1, seed=1, nframes=nframes, dim_fparam=dim_fparam) sys = dpdata.LabeledSystem() real_type_map = ["foo", "bar"] sys.data["atom_names"] = ["X"] @@ -129,25 +129,25 @@ def gen_data_virtual_type(nframes=1, nghost=4): "system_mixed_type/set.000/aparam.npy", np.concatenate( [ - tmpdata.aparam.reshape([nframes, natoms, 2]), - np.zeros([nframes, nghost, 2]), + tmpdata.aparam.reshape([nframes, natoms, dim_fparam]), + np.zeros([nframes, nghost, dim_fparam]), ], axis=1, ), ) -def gen_data(nframes=1, mixed_type=False, virtual_type=False): +def gen_data(nframes=1, mixed_type=False, virtual_type=False, dim_fparam=2): if not mixed_type: - gen_data_type_specific(nframes) + gen_data_type_specific(nframes, dim_fparam=dim_fparam) elif virtual_type: - gen_data_virtual_type(nframes) + gen_data_virtual_type(nframes, dim_fparam=dim_fparam) else: - gen_data_mixed_type(nframes) + gen_data_mixed_type(nframes, dim_fparam=dim_fparam) class Data: - def __init__(self, rand_pert=0.1, seed=1, box_scale=20, nframes=1): + def __init__(self, rand_pert=0.1, seed=1, box_scale=20, nframes=1, dim_fparam=2): coord = [ [0.0, 0.0, 0.1], [1.1, 0.0, 0.1], @@ -161,7 +161,7 @@ def __init__(self, rand_pert=0.1, seed=1, box_scale=20, nframes=1): self.coord = self._copy_nframes(self.coord) dp_random.seed(seed) self.coord += rand_pert * dp_random.random(self.coord.shape) - self.fparam = np.array([[0.1, 0.2]]) + self.fparam = ((np.arange(dim_fparam) + 1) * 0.1).reshape(1, dim_fparam) self.aparam = np.tile(self.fparam, [1, 6]) self.fparam = self._copy_nframes(self.fparam) self.aparam = self._copy_nframes(self.aparam) diff --git a/source/tests/system_fparam_aparam/set.000/aparam.npy b/source/tests/system_fparam_aparam/set.000/aparam.npy deleted file mode 100644 index da59918dad40fcc05a13c7a781855e2780b5b9da..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 176 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlV+i=qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= fXCxM+0{I$-Itpf*ItsN4WCN~QGeKadJ+U+ZA|5u( diff --git a/source/tests/system_fparam_aparam/set.000/box.npy b/source/tests/system_fparam_aparam/set.000/box.npy deleted file mode 100644 index f70e95b6e772550e087d1aba097f26b3b380704e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 200 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlV+i=qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= hXCxM+0{I$-ItrGWItsN4WCJb+Ffeg|(6~s#@&G_89MJ#( diff --git a/source/tests/system_fparam_aparam/set.000/coord.npy b/source/tests/system_fparam_aparam/set.000/coord.npy deleted file mode 100644 index 27a06e944251c351ad110c8c69cc764c2fcd84db..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 272 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlV+i=qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= zXCxM+0{I$-ItqpsnmP)#3giN=`6m;X{f%5|zg$iK+xCJ@_C<$GYa?gvv_GBq Date: Sun, 9 Jul 2023 23:57:22 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- source/tests/common.py | 5 ++++- source/tests/test_model_devi.py | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/source/tests/common.py b/source/tests/common.py index 6680c936d2..12723e52fc 100644 --- a/source/tests/common.py +++ b/source/tests/common.py @@ -56,7 +56,10 @@ def gen_data_type_specific(nframes=1, dim_fparam=2): sys.data["forces"] = np.zeros([nframes, natoms, 3]) sys.to_deepmd_npy("system", prec=np.float64) np.save("system/set.000/fparam.npy", tmpdata.fparam) - np.save("system/set.000/aparam.npy", tmpdata.aparam.reshape([nframes, natoms, dim_fparam])) + np.save( + "system/set.000/aparam.npy", + tmpdata.aparam.reshape([nframes, natoms, dim_fparam]), + ) def gen_data_mixed_type(nframes=1, dim_fparam=2): diff --git a/source/tests/test_model_devi.py b/source/tests/test_model_devi.py index 3abd25f45a..b1c5ec8ead 100644 --- a/source/tests/test_model_devi.py +++ b/source/tests/test_model_devi.py @@ -128,7 +128,9 @@ def setUp(self): self.expect = np.zeros(8) nframes = self.box.size // 9 self.fparam = np.repeat([0.25852028], nframes).reshape((nframes, 1)) - self.aparam = np.repeat(self.fparam, self.atype.size).reshape((nframes, self.atype.size, 1)) + self.aparam = np.repeat(self.fparam, self.atype.size).reshape( + (nframes, self.atype.size, 1) + ) def test_calc_model_devi(self): model_devi = calc_model_devi(