def test_flatten(self): """Tests the flattening.""" # Unflattened desc = SineMatrix(n_atoms_max=5, permutation="none", flatten=False) cm = desc.create(H2O) self.assertEqual(cm.shape, (5, 5)) # Flattened desc = SineMatrix(n_atoms_max=5, permutation="none", flatten=True) cm = desc.create(H2O) self.assertEqual(cm.shape, (25, ))
def test_exceptions(self): """Tests different invalid parameters that should raise an exception. """ with self.assertRaises(ValueError): SineMatrix(n_atoms_max=5, permutation="unknown") with self.assertRaises(ValueError): SineMatrix(n_atoms_max=-1) with self.assertRaises(ValueError): sm = SineMatrix(n_atoms_max=2) sm.create([HHe, H2O])
def test_parallel_dense(self): """Tests creating dense output parallelly.""" samples = [bulk("NaCl", "rocksalt", a=5.64), bulk("Cu", "fcc", a=3.6)] desc = SineMatrix(n_atoms_max=5, permutation="none", flatten=True, sparse=False) n_features = desc.get_number_of_features() # Determining number of jobs based on the amount of CPUs desc.create(system=samples, n_jobs=-1, only_physical_cores=False) desc.create(system=samples, n_jobs=-1, only_physical_cores=True) # Multiple systems, serial job output = desc.create( system=samples, n_jobs=1, ) assumed = np.empty((2, n_features)) assumed[0, :] = desc.create(samples[0]) assumed[1, :] = desc.create(samples[1]) self.assertTrue(np.allclose(output, assumed)) # Multiple systems, parallel job output = desc.create( system=samples, n_jobs=2, ) assumed = np.empty((2, n_features)) assumed[0, :] = desc.create(samples[0]) assumed[1, :] = desc.create(samples[1]) self.assertTrue(np.allclose(output, assumed)) # Non-flattened output desc = SineMatrix(n_atoms_max=5, permutation="none", flatten=False, sparse=False) output = desc.create( system=samples, n_jobs=2, ) assumed = np.empty((2, 5, 5)) assumed[0] = desc.create(samples[0]) assumed[1] = desc.create(samples[1]) self.assertTrue(np.allclose(np.array(output), assumed))
def test_sparse(self): """Tests the sparse matrix creation.""" # Dense desc = SineMatrix(n_atoms_max=5, permutation="none", flatten=True, sparse=False) vec = desc.create(H2O) self.assertTrue(type(vec) == np.ndarray) # Sparse desc = SineMatrix(n_atoms_max=5, permutation="none", flatten=True, sparse=True) vec = desc.create(H2O) self.assertTrue(type(vec) == sparse.COO)
def test_unit_cells(self): """Tests if arbitrary unit cells are accepted""" desc = SineMatrix(n_atoms_max=3, permutation="none", flatten=False) molecule = H2O.copy() molecule.set_cell([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]) with self.assertRaises(ValueError): nocell = desc.create(molecule) molecule.set_pbc(True) molecule.set_cell([[20.0, 0.0, 0.0], [0.0, 30.0, 0.0], [0.0, 0.0, 40.0]]) largecell = desc.create(molecule) molecule.set_cell([[2.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 2.0]]) cubic_cell = desc.create(molecule) molecule.set_cell([[0.0, 2.0, 2.0], [2.0, 0.0, 2.0], [2.0, 2.0, 0.0]]) triclinic_smallcell = desc.create(molecule)
def test_features(self): """Tests that the correct features are present in the desciptor. """ desc = SineMatrix(n_atoms_max=2, permutation="none", flatten=False) # Test that without cell the matrix cannot be calculated system = Atoms( positions=[[0, 0, 0], [1.0, 1.0, 1.0]], symbols=["H", "H"], ) with self.assertRaises(ValueError): desc.create(system) # Test that periodic boundaries are considered by seeing that an atom # in the origin is replicated to the corners system = Atoms( cell=[ [10, 10, 0], [0, 10, 0], [0, 0, 10], ], scaled_positions=[[0, 0, 0], [1.0, 1.0, 1.0]], symbols=["H", "H"], pbc=True, ) # from ase.visualize import view # view(system) matrix = desc.create(system) # The interaction between atoms 1 and 2 should be infinite due to # periodic boundaries. self.assertEqual(matrix[0, 1], float("Inf")) # The interaction of an atom with itself is always 0.5*Z**2.4 atomic_numbers = system.get_atomic_numbers() for i, i_diag in enumerate(np.diag(matrix)): self.assertEqual(i_diag, 0.5 * atomic_numbers[i]**2.4)
def test_parallel_sparse(self): """Tests creating sparse output parallelly. """ # Test indices samples = [bulk("NaCl", "rocksalt", a=5.64), bulk('Cu', 'fcc', a=3.6)] desc = SineMatrix(n_atoms_max=5, permutation="none", flatten=True, sparse=True) n_features = desc.get_number_of_features() # Multiple systems, serial job output = desc.create( system=samples, n_jobs=1, ).toarray() assumed = np.empty((2, n_features)) assumed[0, :] = desc.create(samples[0]).toarray() assumed[1, :] = desc.create(samples[1]).toarray() self.assertTrue(np.allclose(output, assumed)) # Multiple systems, parallel job output = desc.create( system=samples, n_jobs=2, ).toarray() assumed = np.empty((2, n_features)) assumed[0, :] = desc.create(samples[0]).toarray() assumed[1, :] = desc.create(samples[1]).toarray() self.assertTrue(np.allclose(output, assumed)) # Non-flattened output desc = SineMatrix(n_atoms_max=5, permutation="none", flatten=False, sparse=True) output = [ x.toarray() for x in desc.create( system=samples, n_jobs=2, ) ] assumed = np.empty((2, 5, 5)) assumed[0] = desc.create(samples[0]).toarray() assumed[1] = desc.create(samples[1]).toarray() self.assertTrue(np.allclose(np.array(output), assumed))
from dscribe.descriptors import SineMatrix # Setting up the sine matrix descriptor sm = SineMatrix(n_atoms_max=6, permutation="sorted_l2", sparse=False, flatten=True) # Creation from ase.build import bulk # NaCl crystal created as an ASE.Atoms nacl = bulk("NaCl", "rocksalt", a=5.64) # Create output for the system nacl_sine = sm.create(nacl) # Create output for multiple system al = bulk("Al", "fcc", a=4.046) fe = bulk("Fe", "bcc", a=2.856) samples = [nacl, al, fe] sine_matrices = sm.create(samples) # Serial sine_matrices = sm.create(samples, n_jobs=2) # Parallel # Visualization import numpy as np from ase import Atoms import matplotlib.pyplot as mpl from mpl_toolkits.axes_grid1 import make_axes_locatable # FCC aluminum crystal
from ase.spacegroup import crystal from dscribe.descriptors import SineMatrix # Define atomic structures skutterudite = crystal(('Co', 'Sb'), basis=[(0.25, 0.25, 0.25), (0.0, 0.335, 0.158)], spacegroup=204, cellpar=[9.04, 9.04, 9.04, 90, 90, 90]) nacl = bulk("NaCl", "rocksalt", a=5.64) al = bulk("Al", "fcc", a=4.046) fe = bulk("Fe", "bcc", a=2.856) samples_bulk = [skutterudite, nacl, al, fe] # Setup descriptor sm_desc = SineMatrix(n_atoms_max=35, permutation="sorted_l2", sparse=False, flatten=True) # Create single descriptor sine_matrix = sm_desc.create(skutterudite) print("Sine matrix for skutterudite:\n", sine_matrix) # Create multiple descriptors sine_matrices = sm_desc.create(samples_bulk) print("List of Sine matrices:\n", sine_matrices)
def create(system): desc = SineMatrix(n_atoms_max=3, permutation="sorted_l2", flatten=True) return desc.create(system)