def test_converter(transformation, frac_coords): converter = DerivativeMultiLatticeHash(transformation, frac_coords) index = converter.index num_sites = converter.num_sites all_factors = converter.get_all_factors() assert len({tuple(f) for f in all_factors}) == index all_lattice_points = converter.get_lattice_points() assert len({tuple(f) for f in all_lattice_points}) == index all_periodic_sites = converter.get_canonical_sites_list() assert len(set(all_periodic_sites)) == num_sites all_dsites = converter.get_distinct_derivative_sites_list() for dsite in all_dsites: csite, derivative_jimage = converter.hash_derivative_site(dsite, return_image=True) fcoords1 = converter.get_frac_coords(dsite) fcoords2 = ( converter.displacement_set[dsite.site_index] + np.dot(converter.left_inv, csite.factor) + np.dot(converter.hnf, derivative_jimage) ) assert np.allclose(fcoords1, fcoords2) for csite in all_periodic_sites: ind = converter.ravel_canonical_site(csite) csite2 = converter.unravel_to_canonical_site(ind) assert csite2 == csite
def test_converter(): list_transformations = [ np.diag((2, 2, 2)), np.array([[-1, 1, 1], [1, -1, 1], [1, 1, -1]]), np.array([[-1, 1, 1], [1, -1, 1], [-1, -1, 1]]), # negative determinat ] list_frac_coords = [ np.array([[0, 0, 0]]), np.array([[0, 0, 0], [0.5, 0.5, 0.5]]) ] for transformation in list_transformations: for frac_coords in list_frac_coords: converter = DerivativeMultiLatticeHash(transformation, frac_coords) index = converter.index num_sites = converter.num_sites all_factors = converter.get_all_factors() assert len(set([tuple((f)) for f in all_factors])) == index all_lattice_points = converter.get_lattice_points() assert len(set([tuple(f) for f in all_lattice_points])) == index all_periodic_sites = converter.get_canonical_sites_list() assert len(set(all_periodic_sites)) == num_sites all_dsites = converter.get_distinct_derivative_sites_list() for dsite in all_dsites: csite, derivative_jimage = converter.hash_derivative_site( dsite, return_image=True) fcoords1 = converter.get_frac_coords(dsite) fcoords2 = (converter.displacement_set[dsite.site_index] + np.dot(converter.left_inv, csite.factor) + np.dot(converter.hnf, derivative_jimage)) assert np.allclose(fcoords1, fcoords2) for csite in all_periodic_sites: ind = converter.ravel_canonical_site(csite) csite2 = converter.unravel_to_canonical_site(ind) assert csite2 == csite
def __init__( self, base_structure: Structure, dshash: DerivativeMultiLatticeHash, mapping_color_to_species: List[Union[str, Element, Specie, DummySpecie]], additional_species=None, additional_frac_coords=None, ): """ Parameters ---------- base_structure: Structure with only ordering species dshash: mapping_color_to_species: additional_species: list of pymatgen.core.Species, optional species which are nothing to do with ordering additional_frac_coords: np.ndarray, optional fractional coordinates of species which are nothing to do with ordering """ self.base_structure = base_structure self.dshash = dshash self.mapping_color_to_species = mapping_color_to_species self.lattice_matrix = np.dot(self.base_matrix.T, self.dshash.hnf).T # lattice of derivative structure self.lattice = Lattice(self.lattice_matrix) self.canonical_derivative_sites = dshash.get_canonical_and_derivative_sites_list( ) list_coords = [] for _, dsite in self.canonical_derivative_sites: coords = dshash.get_frac_coords(dsite) cart_coords = np.dot(coords, self.base_matrix) list_coords.append(cart_coords) self.list_coords = list_coords self.precomputed_psites = [[ PeriodicSite(sp, coords, self.lattice, coords_are_cartesian=True) for sp in self.mapping_color_to_species ] for coords in self.list_coords] # additional fixed sites self.additional_species = additional_species self.additional_frac_coords = additional_frac_coords if self.additional_species is not None: assert len(self.additional_species) == len( self.additional_frac_coords) self.additional_psites = [] if self.additional_species is not None: lattice_points = self.dshash.get_lattice_points() for sp, disp in zip(self.additional_species, self.additional_frac_coords): cart_coords = [ np.dot(np.array(disp) + np.array(lp), self.base_matrix) for lp in lattice_points ] self.additional_psites.extend([ PeriodicSite(sp, coords, self.lattice, coords_are_cartesian=True) for coords in cart_coords ])