-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
80 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import unittest | ||
import numpy as np | ||
import torch | ||
from deepmd.pt.utils import ( | ||
env, | ||
) | ||
from deepmd.pt.model.task.polarizability import PolarFittingNet | ||
from deepmd.tf.fit.polar import PolarFittingSeA | ||
from deepmd.pt.utils.utils import ( | ||
to_numpy_array, | ||
) | ||
|
||
class TestConsistency(unittest.TestCase): | ||
def setUp(self) -> None: | ||
types = torch.randint(0,4,(1,5), device=env.DEVICE) | ||
types = torch.cat((types,types,types), dim=0) | ||
ntypes = 4 | ||
atomic_polarizability = torch.rand(3,5,9) | ||
polarizability = torch.rand(3,9) | ||
find_polarizability = torch.rand(1) | ||
find_atomic_polarizability = torch.rand(1) | ||
self.sampled = [{"type": types, | ||
"find_atomic_polarizability": find_atomic_polarizability, | ||
"atomic_polarizability": atomic_polarizability, | ||
"polarizability": polarizability, | ||
"find_polarizability": find_polarizability}] | ||
self.all_stat = {k: [v.numpy()] for d in self.sampled for k, v in d.items()} | ||
self.tfpolar = PolarFittingSeA( | ||
ntypes=ntypes, | ||
dim_descrpt=1, | ||
embedding_width=1, | ||
sel_type=[i for i in range(ntypes)], | ||
) | ||
self.ptpolar = PolarFittingNet( | ||
ntypes=ntypes, | ||
dim_descrpt=1, | ||
embedding_width=1, | ||
) | ||
|
||
|
||
def test_atomic_consistency(self): | ||
self.tfpolar.compute_output_stats(self.all_stat) | ||
tfbias = self.tfpolar.constant_matrix | ||
self.ptpolar.compute_output_stats(self.sampled) | ||
ptbias = self.ptpolar.constant_matrix | ||
print(tfbias, to_numpy_array(ptbias)) | ||
np.testing.assert_allclose(tfbias, to_numpy_array(ptbias)) | ||
|
||
def test_global_consistency(self): | ||
self.sampled[0]["find_atomic_polarizability"] = -1 | ||
self.sampled[0]["polarizability"] = self.sampled[0]["atomic_polarizability"].sum(dim=1) | ||
self.all_stat["find_atomic_polarizability"] = [-1] | ||
self.all_stat["polarizability"] = [(self.all_stat["atomic_polarizability"][0]).sum(axis=1)] | ||
self.tfpolar.compute_output_stats(self.all_stat) | ||
tfbias = self.tfpolar.constant_matrix | ||
self.ptpolar.compute_output_stats(self.sampled) | ||
ptbias = self.ptpolar.constant_matrix | ||
print(tfbias, to_numpy_array(ptbias)) | ||
np.testing.assert_allclose(tfbias, to_numpy_array(ptbias)) |