MaterSim / PyXtal

A code to generate atomic structure with symmetry
MIT License
269 stars 68 forks source link

_from_pymatgen() #227

Closed XinYu73 closed 1 year ago

XinYu73 commented 1 year ago

def _from_pymatgen(self, struc, tol=1e-3, a_tol=5.0, style='pyxtal', hn=None): """ Load structure from Pymatgen should not be used directly

    Args:
        struc: input pymatgen structure
        tol: symmetry tolerance
        a_tol: angle tolerance
        style: 'pyxtal' or spglib, differing in the choice of origin
        hn: hall_number
    """
    from pyxtal.util import get_symmetrized_pmg
    #import pymatgen.analysis.structure_matcher as sm

    self.valid = True
    try:
        sym_struc, number = get_symmetrized_pmg(struc, tol, a_tol, style, hn)
        #print(sym_struc)
        #import sys; sys.exit()
    except TypeError:
        print("Failed to load the Pymatgen structure")
    #    print(struc)
    #    self.valid = False

    if self.valid:
        d = sym_struc.composition.as_dict()
        species = [key for key in d.keys()]
        numIons = []
        for ele in species:
            numIons.append(int(d[ele]))
        self.numIons = numIons
        self.species = species
        if hn is None:
            self.group = Group(number, style=style)
        else:
            self.group = Group(hn, use_hall=True)
        #print(self.group[0]); import sys; sys.exit()
        matrix, ltype = sym_struc.lattice.matrix, self.group.lattice_type
        self.lattice = Lattice.from_matrix(matrix, ltype=ltype)
        atom_sites = []
        for i, site in enumerate(sym_struc.equivalent_sites):
            pos = site[0].frac_coords
            letter = sym_struc.wyckoff_symbols[i]
            wp = Wyckoff_position.from_group_and_letter(number, letter, style=style, hn=hn)
            specie = site[0].specie.number
            #if wp.index>0: print(wp)
            pos1 = wp.search_generator(pos, self.group[0])
            if pos1 is not None:
                atom_sites.append(atom_site(wp, pos1, specie))
            else:
                break

the default tol for search_generator is 1e-2, if tol used to find equivalent atoms is large like 0.1, the default 1e-2 may be too small

qzhu2017 commented 1 year ago

@XinYu73 Just fixed

XinYu73 commented 1 year ago

thank you