def test_get_bond_order(self): site1 = Site("C", [0, 0, 0]) site2 = Site("H", [0, 0, 1.08]) self.assertAlmostEqual(CovalentBond(site1, site2).get_bond_order(), 1) bond = CovalentBond(Site("C", [0, 0, 0]), Site("Br", [0, 0, 2])) self.assertAlmostEqual(bond.get_bond_order(0.5, 1.9), 0.894736842105263)
def setUp(self): self.ordered_site = Site("Fe", [0.25, 0.35, 0.45]) self.disordered_site = Site({"Fe": 0.5, "Mn": 0.5}, [0.25, 0.35, 0.45]) self.propertied_site = Site("Fe2+", [0.25, 0.35, 0.45], {'magmom': 5.1, 'charge': 4.2}) self.dummy_site = Site("X", [0, 0, 0])
def setUp(self): self.ordered_site = Site("Fe", [0.25, 0.35, 0.45]) self.disordered_site = Site({"Fe": 0.5, "Mn": 0.5}, [0.25, 0.35, 0.45]) self.propertied_site = Site("Fe2+", [0.25, 0.35, 0.45], { 'magmom': 5.1, 'charge': 4.2 })
def get_size(self, cell): ''' Returns the diameter of a wire structure, defined as the maximum distance between atoms projected to the x-y plane. Precondition: the cell has already been put into wire format (c lattice vector is parallel to z-axis and a and b lattice vectors in the x-y plane), and all sites are located inside the cell (i.e., have fractional coordinates between 0 and 1). Args: cell: the Cell whose size to get ''' max_distance = 0 for site_i in cell.sites: # make Site versions of each PeriodicSite so that the computed # distance won't include periodic images non_periodic_site_i = Site( site_i.species_and_occu, [site_i.coords[0], site_i.coords[1], 0.0]) for site_j in cell.sites: non_periodic_site_j = Site( site_j.species_and_occu, [site_j.coords[0], site_j.coords[1], 0.0]) distance = non_periodic_site_i.distance(non_periodic_site_j) if distance > max_distance: max_distance = distance return max_distance
class SiteTest(PymatgenTest): def setUp(self): self.ordered_site = Site("Fe", [0.25, 0.35, 0.45]) self.disordered_site = Site({"Fe": 0.5, "Mn": 0.5}, [0.25, 0.35, 0.45]) self.propertied_site = Site("Fe2+", [0.25, 0.35, 0.45], { 'magmom': 5.1, 'charge': 4.2 }) self.dummy_site = Site("X", [0, 0, 0]) def test_properties(self): self.assertRaises(AttributeError, getattr, self.disordered_site, 'specie') self.assertIsInstance(self.ordered_site.specie, Element) self.assertEqual(self.propertied_site.properties["magmom"], 5.1) self.assertEqual(self.propertied_site.properties["charge"], 4.2) def test_to_from_dict(self): d = self.disordered_site.as_dict() site = Site.from_dict(d) self.assertEqual(site, self.disordered_site) self.assertNotEqual(site, self.ordered_site) d = self.propertied_site.as_dict() site = Site.from_dict(d) self.assertEqual(site.properties["magmom"], 5.1) self.assertEqual(site.properties["charge"], 4.2) d = self.dummy_site.as_dict() site = Site.from_dict(d) self.assertEqual(site.species, self.dummy_site.species) def test_hash(self): self.assertEqual(self.ordered_site.__hash__(), 26) self.assertEqual(self.disordered_site.__hash__(), 51) def test_cmp(self): self.assertTrue(self.ordered_site > self.disordered_site) def test_distance(self): osite = self.ordered_site self.assertAlmostEqual(np.linalg.norm([0.25, 0.35, 0.45]), osite.distance_from_point([0, 0, 0])) self.assertAlmostEqual(osite.distance(self.disordered_site), 0) def test_pickle(self): o = pickle.dumps(self.propertied_site) self.assertEqual(pickle.loads(o), self.propertied_site) def test_setters(self): self.disordered_site.species = "Cu" self.assertEqual(self.disordered_site.species, Composition("Cu")) self.disordered_site.x = 1.25 self.disordered_site.y = 1.35 self.assertEqual(self.disordered_site.coords[0], 1.25) self.assertEqual(self.disordered_site.coords[1], 1.35) def set_bad_species(): self.disordered_site.species = {"Cu": 0.5, "Gd": 0.6} self.assertRaises(ValueError, set_bad_species)
class SiteTest(PymatgenTest): def setUp(self): self.ordered_site = Site("Fe", [0.25, 0.35, 0.45]) self.disordered_site = Site({"Fe": 0.5, "Mn": 0.5}, [0.25, 0.35, 0.45]) self.propertied_site = Site("Fe2+", [0.25, 0.35, 0.45], {'magmom': 5.1, 'charge': 4.2}) self.dummy_site = Site("X", [0, 0, 0]) def test_properties(self): self.assertRaises(AttributeError, getattr, self.disordered_site, 'specie') self.assertIsInstance(self.ordered_site.specie, Element) self.assertEqual(self.propertied_site.properties["magmom"], 5.1) self.assertEqual(self.propertied_site.properties["charge"], 4.2) def test_to_from_dict(self): d = self.disordered_site.as_dict() site = Site.from_dict(d) self.assertEqual(site, self.disordered_site) self.assertNotEqual(site, self.ordered_site) d = self.propertied_site.as_dict() site = Site.from_dict(d) self.assertEqual(site.properties["magmom"], 5.1) self.assertEqual(site.properties["charge"], 4.2) d = self.dummy_site.as_dict() site = Site.from_dict(d) self.assertEqual(site.species, self.dummy_site.species) def test_hash(self): self.assertEqual(self.ordered_site.__hash__(), 26) self.assertEqual(self.disordered_site.__hash__(), 51) def test_cmp(self): self.assertTrue(self.ordered_site > self.disordered_site) def test_distance(self): osite = self.ordered_site self.assertAlmostEqual(np.linalg.norm([0.25, 0.35, 0.45]), osite.distance_from_point([0, 0, 0])) self.assertAlmostEqual(osite.distance(self.disordered_site), 0) def test_pickle(self): o = pickle.dumps(self.propertied_site) self.assertEqual(pickle.loads(o), self.propertied_site) def test_setters(self): self.disordered_site.species = "Cu" self.assertEqual(self.disordered_site.species, Composition("Cu")) self.disordered_site.x = 1.25 self.disordered_site.y = 1.35 self.assertEqual(self.disordered_site.coords[0], 1.25) self.assertEqual(self.disordered_site.coords[1], 1.35) def set_bad_species(): self.disordered_site.species = {"Cu": 0.5, "Gd": 0.6} self.assertRaises(ValueError, set_bad_species)
def test_to_from_dict(self): d = self.disordered_site.to_dict site = Site.from_dict(d) self.assertEqual(site, self.disordered_site) self.assertNotEqual(site, self.ordered_site) d = self.propertied_site.to_dict site = Site.from_dict(d) self.assertEqual(site.magmom, 5.1) self.assertEqual(site.charge, 4.2)
def test_is_bonded(self): site1 = Site("C", [0, 0, 0]) site2 = Site("H", [0, 0, 1]) self.assertTrue(CovalentBond.is_bonded(site1, site2)) site2 = Site("H", [0, 0, 1.5]) self.assertFalse(CovalentBond.is_bonded(site1, site2)) site1 = Site("U", [0, 0, 0]) self.assertRaises(ValueError, CovalentBond.is_bonded, site1, site2) self.assertTrue(CovalentBond.is_bonded(site1, site2, default_bl=2))
def setUp(self): self.ordered_site = Site("Fe", [0.25, 0.35, 0.45]) self.disordered_site = Site({"Fe": 0.5, "Mn": 0.5}, [0.25, 0.35, 0.45]) self.propertied_site = Site("Fe2+", [0.25, 0.35, 0.45], {'magmom': 5.1, 'charge': 4.2}) self.propertied_magmomvector_site = Site("Fe2+", [0.25, 0.35, 0.45], {'magmom': Magmom([2.6, 2.6, 3.5]), 'charge': 4.2}) self.dummy_site = Site("X", [0, 0, 0])
def test_to_from_dict(self): d = self.disordered_site.as_dict() site = Site.from_dict(d) self.assertEqual(site, self.disordered_site) self.assertNotEqual(site, self.ordered_site) d = self.propertied_site.as_dict() site = Site.from_dict(d) self.assertEqual(site.magmom, 5.1) self.assertEqual(site.charge, 4.2) d = self.dummy_site.as_dict() site = Site.from_dict(d) self.assertEqual(site.species_and_occu, self.dummy_site.species_and_occu)
def test_to_from_dict(self): d = self.disordered_site.as_dict() site = Site.from_dict(d) self.assertEqual(site, self.disordered_site) self.assertNotEqual(site, self.ordered_site) d = self.propertied_site.as_dict() site = Site.from_dict(d) self.assertEqual(site.properties["magmom"], 5.1) self.assertEqual(site.properties["charge"], 4.2) d = self.dummy_site.as_dict() site = Site.from_dict(d) self.assertEqual(site.species, self.dummy_site.species)
def get_site_location_by_key(psites: [PeriodicSite], key='label'): """ loc[key] = 'bone'/'sidechain' must have 'imol' 'disg' 'siteid' and <key> assigned """ res = {} k = lambda x: x.properties['imol'] psites.sort(key=k) for imol, group in groupby(psites, key=k): obc_sites = [] for ps in group: obc_sites.append( Site(ps.species_string, ps.coords, properties=ps.properties)) try: molconformer = MolConformer.from_sites( obc_sites, siteids=[s.properties['siteid'] for s in obc_sites]) except ConformerError: raise DisorderParserError( 'cannot init legit conformer to identify site location') for sid in molconformer.siteids: site = molconformer.get_site_byid(sid) if molconformer.backbone is None: res[site.properties[key]] = 'sidechain' else: if sid in molconformer.backbone.siteids: res[site.properties[key]] = 'bone' else: res[site.properties[key]] = 'sidechain' return res
def insert_site(self, i, species, coords, validate_proximity=True, properties=None): """ Insert a site to the structure. Args: i: Index to insert site. species: Species of inserted site. coords: Coordinates of inserted site. validate_proximity: Whether to check if inserted site is too close to an existing site. Defaults to True. """ new_site = Site(species, coords, properties=properties) if validate_proximity: for site in self._sites: if site.distance(new_site) < self.DISTANCE_TOLERANCE: raise ValueError("New site is too close to an existing " "site!") self._sites.insert(i, new_site)
class SiteTest(PymatgenTest): def setUp(self): self.ordered_site = Site("Fe", [0.25, 0.35, 0.45]) self.disordered_site = Site({"Fe": 0.5, "Mn": 0.5}, [0.25, 0.35, 0.45]) self.propertied_site = Site("Fe2+", [0.25, 0.35, 0.45], { 'magmom': 5.1, 'charge': 4.2 }) self.dummy_site = Site("X", [0, 0, 0]) def test_properties(self): self.assertRaises(AttributeError, getattr, self.disordered_site, 'specie') self.assertIsInstance(self.ordered_site.specie, Element) self.assertEqual(self.propertied_site.magmom, 5.1) self.assertEqual(self.propertied_site.charge, 4.2) def test_to_from_dict(self): d = self.disordered_site.as_dict() site = Site.from_dict(d) self.assertEqual(site, self.disordered_site) self.assertNotEqual(site, self.ordered_site) d = self.propertied_site.as_dict() site = Site.from_dict(d) self.assertEqual(site.magmom, 5.1) self.assertEqual(site.charge, 4.2) d = self.dummy_site.as_dict() site = Site.from_dict(d) self.assertEqual(site.species_and_occu, self.dummy_site.species_and_occu) def test_hash(self): self.assertEqual(self.ordered_site.__hash__(), 26) self.assertEqual(self.disordered_site.__hash__(), 51) def test_cmp(self): self.assertTrue(self.ordered_site > self.disordered_site) def test_distance(self): osite = self.ordered_site self.assertAlmostEqual(np.linalg.norm([0.25, 0.35, 0.45]), osite.distance_from_point([0, 0, 0])) self.assertAlmostEqual(osite.distance(self.disordered_site), 0) def test_pickle(self): o = pickle.dumps(self.propertied_site) self.assertEqual(pickle.loads(o), self.propertied_site)
class SiteTest(PymatgenTest): def setUp(self): self.ordered_site = Site("Fe", [0.25, 0.35, 0.45]) self.disordered_site = Site({"Fe": 0.5, "Mn": 0.5}, [0.25, 0.35, 0.45]) self.propertied_site = Site("Fe2+", [0.25, 0.35, 0.45], {'magmom': 5.1, 'charge': 4.2}) self.dummy_site = Site("X", [0, 0, 0]) def test_properties(self): self.assertRaises(AttributeError, getattr, self.disordered_site, 'specie') self.assertIsInstance(self.ordered_site.specie, Element) self.assertEqual(self.propertied_site.magmom, 5.1) self.assertEqual(self.propertied_site.charge, 4.2) def test_to_from_dict(self): d = self.disordered_site.as_dict() site = Site.from_dict(d) self.assertEqual(site, self.disordered_site) self.assertNotEqual(site, self.ordered_site) d = self.propertied_site.as_dict() site = Site.from_dict(d) self.assertEqual(site.magmom, 5.1) self.assertEqual(site.charge, 4.2) d = self.dummy_site.as_dict() site = Site.from_dict(d) self.assertEqual(site.species_and_occu, self.dummy_site.species_and_occu) def test_hash(self): self.assertEqual(self.ordered_site.__hash__(), 26) self.assertEqual(self.disordered_site.__hash__(), 51) def test_cmp(self): self.assertTrue(self.ordered_site > self.disordered_site) def test_distance(self): osite = self.ordered_site self.assertAlmostEqual(np.linalg.norm([0.25, 0.35, 0.45]), osite.distance_from_point([0, 0, 0])) self.assertAlmostEqual(osite.distance(self.disordered_site), 0) def test_pickle(self): o = pickle.dumps(self.propertied_site) self.assertEqual(pickle.loads(o), self.propertied_site)
class SiteTest(unittest.TestCase): def setUp(self): self.ordered_site = Site(Element("Fe"), [0.25, 0.35, 0.45]) self.disordered_site = Site({Element("Fe"): 0.5, Element("Mn"): 0.5}, [0.25, 0.35, 0.45]) self.propertied_site = Site(Specie("Fe", 2), [0.25, 0.35, 0.45], {'magmom': 5.1, 'charge': 4.2}) def test_init(self): self.assertRaises(ValueError, Site, Specie("Fe", 2), [0.25, 0.35, 0.45], {'mag': 5.1}) def test_properties(self): self.assertRaises(AttributeError, getattr, self.disordered_site, 'specie') self.assertIsInstance(self.ordered_site.specie, Element) self.assertEqual(self.propertied_site.magmom, 5.1) self.assertEqual(self.propertied_site.charge, 4.2) def test_to_from_dict(self): d = self.disordered_site.to_dict site = Site.from_dict(d) self.assertEqual(site, self.disordered_site) self.assertNotEqual(site, self.ordered_site) d = self.propertied_site.to_dict site = Site.from_dict(d) self.assertEqual(site.magmom, 5.1) self.assertEqual(site.charge, 4.2) def test_hash(self): self.assertEqual(self.ordered_site.__hash__(), 26) self.assertEqual(self.disordered_site.__hash__(), 25.5) def test_cmp(self): self.assertTrue(self.ordered_site > self.disordered_site) def test_distance(self): osite = self.ordered_site self.assertAlmostEqual(np.linalg.norm([0.25, 0.35, 0.45]), osite.distance_from_point([0, 0, 0])) self.assertAlmostEqual(osite.distance(self.disordered_site), 0) def test_pickle(self): o = pickle.dumps(self.propertied_site) self.assertEqual(pickle.loads(o), self.propertied_site)
def __init__(self, fname): """ Initialize the cube object and store the data as self.data Args: fname (str): filename of the cube to read """ f = zopen(fname, "rt") # skip header lines for i in range(2): f.readline() # number of atoms followed by the position of the origin of the volumetric data line = f.readline().split() self.natoms = int(line[0]) self.origin = np.array(list(map(float, line[1:]))) # The number of voxels along each axis (x, y, z) followed by the axis vector. line = f.readline().split() self.NX = int(line[0]) self.X = np.array([bohr_to_angstrom * float(l) for l in line[1:]]) self.dX = np.linalg.norm(self.X) line = f.readline().split() self.NY = int(line[0]) self.Y = np.array([bohr_to_angstrom * float(l) for l in line[1:]]) self.dY = np.linalg.norm(self.Y) line = f.readline().split() self.NZ = int(line[0]) self.Z = np.array([bohr_to_angstrom * float(l) for l in line[1:]]) self.dZ = np.linalg.norm(self.Z) self.voxel_volume = abs(np.dot(np.cross(self.X, self.Y), self.Z)) self.volume = abs(np.dot(np.cross(self.X.dot(self.NZ), self.Y.dot(self.NY)), self.Z.dot(self.NZ))) # The last section in the header is one line for each atom consisting of 5 numbers, # the first is the atom number, second is charge, # the last three are the x,y,z coordinates of the atom center. self.sites = [] for i in range(self.natoms): line = f.readline().split() self.sites.append(Site(line[0], np.multiply(bohr_to_angstrom, list(map(float, line[2:]))))) self.structure = Structure( lattice=[self.X * self.NX, self.Y * self.NY, self.Z * self.NZ], species=[s.specie for s in self.sites], coords=[s.coords for s in self.sites], coords_are_cartesian=True, ) # Volumetric data self.data = np.reshape(np.array(f.read().split()).astype(float), (self.NX, self.NY, self.NZ))
def reset_positions(self, coors): """ reset the coordinates """ from pymatgen.core.sites import Site if len(coors) != len(self.mol._sites): raise ValueError("number of atoms is inconsistent!") else: for i, coor in enumerate(coors): _site = self.mol._sites[i] new_site = Site(_site.species, coor, properties=_site.properties) self.mol._sites[i] = new_site
def test_to_from_dict(self): d = self.site2.to_dict site = PeriodicSite.from_dict(d) self.assertEqual(site, self.site2) self.assertNotEqual(site, self.site) d = self.propertied_site.to_dict site = Site.from_dict(d) self.assertEqual(site.magmom, 5.1) self.assertEqual(site.charge, 4.2) site3 = PeriodicSite({"Si": 0.5, "Fe": 0.5}, [0, 0, 0], self.lattice) d = site3.to_dict site = PeriodicSite.from_dict(d) self.assertEqual(site.species_and_occu, site3.species_and_occu)
def replace_site(self, index, species_n_occu): """ Replace a single site. Takes either a species or a dict of occus. Args: index: The index of the site in the _sites list species: A species object. """ self._sites[index] = Site(species_n_occu, self._sites[index].coords, properties=self._sites[index].properties)
def get_size(self, cell): ''' Returns the diameter of a cluster structure, defined as the maximum distance between atoms in the cell. Precondition: all sites are located inside the cell (i.e., have fractional coordinates between 0 and 1) Args: cell: the Cell whose size to get ''' max_distance = 0 for site_i in cell.sites: # make Site versions of each PeriodicSite so that the computed # distance won't include periodic images non_periodic_site_i = Site(site_i.species_and_occu, site_i.coords) for site_j in cell.sites: non_periodic_site_j = Site(site_j.species_and_occu, site_j.coords) distance = non_periodic_site_i.distance(non_periodic_site_j) if distance > max_distance: max_distance = distance return max_distance
def translate_sites(self, indices, vector): """ Translate specific sites by some vector, keeping the sites within the unit cell. Args: sites: List of site indices on which to perform the translation. vector: Translation vector for sites. """ for i in indices: site = self._sites[i] new_site = Site(site.species_and_occu, site.coords + vector, properties=site.properties) self._sites[i] = new_site
def remove_species(self, species): """ Remove all occurrences of a species from a molecule. Args: species: Species to remove. """ new_sites = [] for site in self._sites: new_sp_occu = { sp: amt for sp, amt in site.species_and_occu.items() if sp not in species } if len(new_sp_occu) > 0: new_sites.append( Site(new_sp_occu, site.coords, properties=site.properties)) self._sites = new_sites
def add_site_property(self, property_name, values): """ Adds a property to a site. Args: property_name: The name of the property to add. values: A sequence of values. Must be same length as number of sites. """ if len(values) != len(self._sites): raise ValueError("Values must be same length as sites.") for i in xrange(len(self._sites)): site = self._sites[i] props = site.properties if not props: props = {} props[property_name] = values[i] self._sites[i] = Site(site.species_and_occu, site.coords, properties=props)
def mod_site(site): new_atom_occu = dict() for sp, amt in site.species_and_occu.items(): if sp in species_mapping: if isinstance(species_mapping[sp], (Element, Specie)): if species_mapping[sp] in new_atom_occu: new_atom_occu[species_mapping[sp]] += amt else: new_atom_occu[species_mapping[sp]] = amt elif isinstance(species_mapping[sp], dict): for new_sp, new_amt in species_mapping[sp].items(): if new_sp in new_atom_occu: new_atom_occu[new_sp] += amt * new_amt else: new_atom_occu[new_sp] = amt * new_amt else: if sp in new_atom_occu: new_atom_occu[sp] += amt else: new_atom_occu[sp] = amt return Site(new_atom_occu, site.coords, properties=site.properties)
def test_str(self): site1 = Site("C", [0, 0, 0]) site2 = Site("H", [0, 0.7, 0.6]) self.assertIsNotNone(CovalentBond(site1, site2))
def setUp(self): self.ordered_site = Site("Fe", [0.25, 0.35, 0.45]) self.disordered_site = Site({"Fe": 0.5, "Mn": 0.5}, [0.25, 0.35, 0.45]) self.propertied_site = Site("Fe2+", [0.25, 0.35, 0.45], {"magmom": 5.1, "charge": 4.2})
def test_length(self): site1 = Site("C", [0, 0, 0]) site2 = Site("H", [0, 0.7, 0.6]) self.assertAlmostEqual( CovalentBond(site1, site2).length, 0.92195444572928864)
def setUp(self): self.ordered_site = Site(Element("Fe"), [0.25, 0.35, 0.45]) self.disordered_site = Site({Element("Fe"): 0.5, Element("Mn"): 0.5}, [0.25, 0.35, 0.45]) self.propertied_site = Site(Specie("Fe", 2), [0.25, 0.35, 0.45], {'magmom': 5.1, 'charge': 4.2})
def test_is_bonded(self): site1 = Site("C", [0, 0, 0]) site2 = Site("H", [0, 0, 1]) self.assertTrue(CovalentBond.is_bonded(site1, site2)) site2 = Site("H", [0, 0, 1.5]) self.assertFalse(CovalentBond.is_bonded(site1, site2))