def test_rad_sf(self): with unittest.mock.patch.dict("os.environ", {"CML_SCRATCH": str(self.tmpdir)}): from cmlkit.representation.sf import SymmetryFunctions from cmlkit import Dataset, runner_path print(runner_path) data = Dataset( z=np.array([[1, 1]]), r=np.array([[[0.0, 0.0, 0.0], [2.0, 0.0, 0.0]]]) ) for i in range(5): eta = np.random.random() mu = np.random.random() cutoff = 5.0 sf = SymmetryFunctions( [1], universal=[{"rad": {"eta": eta, "mu": mu, "cutoff": cutoff}}] ) computed = sf(data).ragged np.testing.assert_almost_equal( computed[0][0][0], rad_sf(2.0, eta, mu) * fc(2.0, cutoff) ) np.testing.assert_almost_equal( computed[0][1][0], rad_sf(2.0, eta, mu) * fc(2.0, cutoff) )
def test_parametrized_sf(self): data = Dataset(z=np.array([[1, 1]]), r=np.array([[[0.0, 0.0, 0.0], [2.0, 0.0, 0.0]]])) cutoff = 5.0 sf = SymmetryFunctions([1], sfs=[{ "rad_centered": { "n": 3 } }], cutoff=cutoff) delta = (cutoff - 1.5) / 2 computed = sf(data) print(computed) np.testing.assert_almost_equal(computed[0][0][0], fc(2.0, cutoff)) np.testing.assert_almost_equal( computed[0][0][1], rad_sf(2.0, 0.5 / (0.5 + 0 * delta)**2, 0.0) * fc(2.0, cutoff), ) np.testing.assert_almost_equal( computed[0][0][2], rad_sf(2.0, 0.5 / (0.5 + 1 * delta)**2, 0.0) * fc(2.0, cutoff), ) np.testing.assert_almost_equal( computed[0][0][3], rad_sf(2.0, 0.5 / (0.5 + 2 * delta)**2, 0.0) * fc(2.0, cutoff), )
def test_rad_sf(self): data = Dataset(z=np.array([[1, 1]]), r=np.array([[[0.0, 0.0, 0.0], [2.0, 0.0, 0.0]]])) for i in range(5): eta = np.random.random() mu = np.random.random() cutoff = 6.0 sf = SymmetryFunctions(elems=[1], cutoff=cutoff, sfs=[{ "rad": { "eta": eta, "mu": mu } }]) computed = sf(data) print(computed.shape) np.testing.assert_almost_equal(computed[0][0][0], fc(2.0, cutoff)) np.testing.assert_almost_equal( computed[0][0][1], rad_sf(2.0, eta, mu) * fc(2.0, cutoff)) np.testing.assert_almost_equal(computed[0][1][0], fc(2.0, cutoff)) np.testing.assert_almost_equal( computed[0][1][1], rad_sf(2.0, eta, mu) * fc(2.0, cutoff))
def test_mbtr2_chunked(self): # naughty -- this is testing functionality that is common # to ALL representations! data = Dataset( z=np.array([[0, 0], [0, 0], [0, 0]]), r=np.array([[[0.0, 0.0, 0.0], [2.0, 0.0, 0.0]], [[0.0, 0.0, 0.0], [2.0, 2.0, 3.0]], [[0.0, 3.0, 0.0], [2.0, 1.0, 0.0]]]) ) mbtr_chunked = MBTR2( start=0, stop=1, num=3, geomf="1/distance", weightf="unity", broadening=0.001, eindexf="noreversals", aindexf="noreversals", elems=[0], flatten=True, context={"chunk_size": 2} ) mbtr = MBTR2( start=0, stop=1, num=3, geomf="1/distance", weightf="unity", broadening=0.001, eindexf="noreversals", aindexf="noreversals", elems=[0], flatten=True, context={"chunk_size": None} ) np.testing.assert_array_equal(mbtr_chunked(data).array, mbtr(data).array)
def test_ang_sf(self): with unittest.mock.patch.dict("os.environ", {"CML_SCRATCH": str(self.tmpdir)}): from cmlkit.representation.sf import SymmetryFunctions from cmlkit import Dataset, runner_path print(runner_path) data = Dataset( z=np.array([[1, 1, 1]]), r=np.array([[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]]), ) for i in range(5): eta = np.random.random() zeta = np.random.random() cutoff = 5.0 lambd = 1.0 + np.random.random() * 0.3 sf = SymmetryFunctions( [1], universal=[ { "ang": { "eta": eta, "zeta": zeta, "cutoff": cutoff, "lambd": lambd, } } ], ) computed = sf(data).ragged print(computed) np.testing.assert_almost_equal( computed[0][0][0], ang_sf(np.pi / 2, 1.0, 1.0, np.sqrt(2.0), lambd, zeta, eta) * fc(1.0, cutoff) * fc(1.0, cutoff) * fc(np.sqrt(2.0), cutoff), ) np.testing.assert_almost_equal( computed[0][1][0], ang_sf(np.pi / 4, 1.0, 1.0, np.sqrt(2.0), lambd, zeta, eta) * fc(1.0, cutoff) * fc(1.0, cutoff) * fc(np.sqrt(2.0), cutoff), )
def setUp(self): self.n = 100 self.n_atoms = np.random.randint(1, high=10, size=self.n) r = [5 * np.random.random((na, 3)) for na in self.n_atoms] self.r = np.array(r, dtype=object) self.z = np.array( [np.random.randint(1, high=3, size=na) for na in self.n_atoms], dtype=object ) self.data = Dataset(z=self.z, r=self.r) self.tmpdir = (pathlib.Path(__file__) / "..").resolve() / "tmp_test_composed" self.tmpdir.mkdir(exist_ok=True)
def test_ang_sf(self): data = Dataset( z=np.array([[1, 1, 1]]), r=np.array([[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]]), ) for i in range(5): eta = np.random.random() zeta = np.random.random() cutoff = 5.0 lambd = 1.0 + np.random.random() * 0.3 sf = SymmetryFunctions( [1], sfs=[{ "ang": { "eta": eta, "zeta": zeta, "lambd": lambd } }], cutoff=cutoff, ) computed = sf(data) print(computed) np.testing.assert_almost_equal(computed[0][0][0], fc(1.0, cutoff) + fc(1.0, cutoff)) np.testing.assert_almost_equal( computed[0][0][1], ang_sf(np.pi / 2, 1.0, 1.0, np.sqrt(2.0), lambd, zeta, eta) * fc(1.0, cutoff) * fc(1.0, cutoff) * fc(np.sqrt(2.0), cutoff), ) np.testing.assert_almost_equal( computed[0][1][0], fc(1.0, cutoff) + fc(np.sqrt(2.0), cutoff)) np.testing.assert_almost_equal( computed[0][1][1], ang_sf(np.pi / 4, 1.0, 1.0, np.sqrt(2.0), lambd, zeta, eta) * fc(1.0, cutoff) * fc(1.0, cutoff) * fc(np.sqrt(2.0), cutoff), )
def test_smoke(self): # since quippy is impossible to install, # we can't test it here. # but we can at least check that things are not TOO broken with unittest.mock.patch.dict( "os.environ", { "CML_QUIPPY_PYTHONPATH": str(self.tmpdir), "CML_QUIPPY_PYTHON_EXE": "", "CML_SCRATCH": str(self.tmpdir), }, ): res = np.array([[np.array([1.0]), np.array([2.0])]], dtype=object) def fake_output(*args, **kwargs): outfolder = list(self.tmpdir.glob("soap_*"))[0] np.save(outfolder / "out", res) return "", "" fake_task = unittest.mock.MagicMock(side_effect=fake_output) with unittest.mock.patch( "cmlkit.representation.soap.quippy_interface.run_task", fake_task): from cmlkit.representation.soap import SOAP from cmlkit import Dataset data = Dataset( z=np.array([[1, 1]]), r=np.array([[[0.0, 0.0, 0.0], [2.0, 0.0, 0.0]]]), ) soap = SOAP(elems=[1], sigma=0.1, n_max=2, l_max=3, cutoff=5.0) computed = soap(data).ragged np.testing.assert_array_equal(computed, res)
def test_rad_sf_periodic(self): data = Dataset( z=np.array([[1, 1]]), r=np.array([[[0.0, 0.0, 0.0], [2.0, 0.0, 0.0]]]), b=np.array([[[8.0, 0, 0], [0, 8.0, 0], [0, 0, 8.0]]]), ) # just testing that nothing is broken; cutoff is smaller than cell for i in range(5): eta = np.random.random() mu = np.random.random() cutoff = 6.0 sf = SymmetryFunctions(elems=[1], cutoff=cutoff, sfs=[{ "rad": { "eta": eta, "mu": mu } }]) computed = sf(data) print(computed.shape) np.testing.assert_almost_equal(computed[0][0][0], fc(2.0, cutoff)) np.testing.assert_almost_equal( computed[0][0][1], rad_sf(2.0, eta, mu) * fc(2.0, cutoff)) np.testing.assert_almost_equal(computed[0][1][0], fc(2.0, cutoff)) np.testing.assert_almost_equal( computed[0][1][1], rad_sf(2.0, eta, mu) * fc(2.0, cutoff))
def test_parametrized_sf(self): with unittest.mock.patch.dict("os.environ", {"CML_SCRATCH": str(self.tmpdir)}): from cmlkit.representation.sf import SymmetryFunctions from cmlkit import Dataset, runner_path print(runner_path) data = Dataset( z=np.array([[1, 1]]), r=np.array([[[0.0, 0.0, 0.0], [2.0, 0.0, 0.0]]]) ) cutoff = 5.0 sf = SymmetryFunctions( [1], universal=[{"rad_centered": {"n": 3, "cutoff": cutoff}}] ) delta = (cutoff - 1.5) / 2 computed = sf(data).ragged # note that the ordering of the results is not the same as # the one you'd expect -- runner internally reorders. # I can't be bothered to find out how for now. np.testing.assert_almost_equal( computed[0][0][2], rad_sf(2.0, 0.5 / (0.5 + 0 * delta) ** 2, 0.0) * fc(2.0, cutoff), ) np.testing.assert_almost_equal( computed[0][0][1], rad_sf(2.0, 0.5 / (0.5 + 1 * delta) ** 2, 0.0) * fc(2.0, cutoff), ) np.testing.assert_almost_equal( computed[0][0][0], rad_sf(2.0, 0.5 / (0.5 + 2 * delta) ** 2, 0.0) * fc(2.0, cutoff), )
def test_rad_sf_multiple_elements(self): data = Dataset( z=np.array([[1, 2, 3]]), r=np.array([[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]]), ) for i in range(5): eta = np.random.random() mu = np.random.random() cutoff = 5.0 sf = SymmetryFunctions([1, 2, 3], sfs=[{ "rad": { "eta": eta, "mu": mu } }], cutoff=cutoff) computed = sf(data) print(computed) print(computed.shape) # Atom 0 # Other Z = 1 np.testing.assert_almost_equal(computed[0][0][0], 0.0) np.testing.assert_almost_equal(computed[0][0][1], 0.0) # Other Z = 2 np.testing.assert_almost_equal(computed[0][0][2], fc(1.0, cutoff)) np.testing.assert_almost_equal( computed[0][0][3], rad_sf(1.0, eta, mu) * fc(1.0, cutoff)) # Other Z = 3 np.testing.assert_almost_equal(computed[0][0][4], fc(1.0, cutoff)) np.testing.assert_almost_equal( computed[0][0][5], rad_sf(1.0, eta, mu) * fc(1.0, cutoff)) # Atom 1 # Other Z = 1 np.testing.assert_almost_equal(computed[0][1][6 + 0], fc(1.0, cutoff)) np.testing.assert_almost_equal( computed[0][1][6 + 1], rad_sf(1.0, eta, mu) * fc(1.0, cutoff)) # Other Z = 2 np.testing.assert_almost_equal(computed[0][1][6 + 2], 0.0) np.testing.assert_almost_equal(computed[0][1][6 + 3], 0.0) # Other Z = 3 np.testing.assert_almost_equal(computed[0][1][6 + 4], fc(np.sqrt(2.0), cutoff)) np.testing.assert_almost_equal( computed[0][1][6 + 5], rad_sf(np.sqrt(2.0), eta, mu) * fc(np.sqrt(2.0), cutoff), ) # Atom 2 # Other Z = 1 np.testing.assert_almost_equal(computed[0][2][12 + 0], fc(1.0, cutoff)) np.testing.assert_almost_equal( computed[0][2][12 + 1], rad_sf(1.0, eta, mu) * fc(1.0, cutoff)) # Other Z = 2 np.testing.assert_almost_equal(computed[0][2][12 + 2], fc(np.sqrt(2.0), cutoff)) np.testing.assert_almost_equal( computed[0][2][12 + 3], rad_sf(np.sqrt(2.0), eta, mu) * fc(np.sqrt(2.0), cutoff), ) # Other Z = 3 np.testing.assert_almost_equal(computed[0][2][12 + 4], 0.0) np.testing.assert_almost_equal(computed[0][2][12 + 5], 0.0)
def setUp(self): self.data = Dataset( z=np.array([[1, 2, 2]]), r=np.array([[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [2.0, 0.0, 0.0]]]), )
def setUp(self): self.data = Dataset(z=np.array([[0, 1]]), r=np.array([[[1, 2, 3], [1, 2, 3]]]))
def setUp(self): self.data = Dataset( z=np.array([[0, 0, 0]]), r=np.array([[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]]), )
def setUp(self): self.data = Dataset(z=np.array([[2, 3]]), r=np.array([[[0, 0, 0], [2.0, 0, 0]]]))