|
1 | 1 |
|
2 | 2 | import re |
| 3 | +import fractions |
3 | 4 | import cStringIO |
4 | 5 |
|
5 | 6 | from ase.atoms import Atoms |
@@ -87,30 +88,45 @@ def symmetrize(ase_obj, accuracy=1E-03): |
87 | 88 | return None, 'Unrecognized sites or invalid site symmetry in structure' |
88 | 89 |
|
89 | 90 |
|
90 | | -def get_formula(ase_obj): |
91 | | - formula_sequence = ['Fr','Cs','Rb','K','Na','Li', 'Be','Mg','Ca','Sr','Ba','Ra', 'Sc','Y','La','Ce','Pr','Nd','Pm','Sm','Eu','Gd','Tb','Dy','Ho','Er','Tm','Yb', 'Ac','Th','Pa','U','Np','Pu', 'Ti','Zr','Hf', 'V','Nb','Ta', 'Cr','Mo','W', 'Fe','Ru','Os', 'Co','Rh','Ir', 'Mn','Tc','Re', 'Ni','Pd','Pt', 'Cu','Ag','Au', 'Zn','Cd','Hg', 'B','Al','Ga','In','Tl', 'Pb','Sn','Ge','Si','C', 'N','P','As','Sb','Bi', 'H', 'Po','Te','Se','S','O', 'At','I','Br','Cl','F', 'He','Ne','Ar','Kr','Xe','Rn'] |
| 91 | +FORMULA_SEQUENCE = ['Fr','Cs','Rb','K','Na','Li', 'Be','Mg','Ca','Sr','Ba','Ra', 'Sc','Y','La','Ce','Pr','Nd','Pm','Sm','Eu','Gd','Tb','Dy','Ho','Er','Tm','Yb', 'Ac','Th','Pa','U','Np','Pu', 'Ti','Zr','Hf', 'V','Nb','Ta', 'Cr','Mo','W', 'Fe','Ru','Os', 'Co','Rh','Ir', 'Mn','Tc','Re', 'Ni','Pd','Pt', 'Cu','Ag','Au', 'Zn','Cd','Hg', 'B','Al','Ga','In','Tl', 'Pb','Sn','Ge','Si','C', 'N','P','As','Sb','Bi', 'H', 'Po','Te','Se','S','O', 'At','I','Br','Cl','F', 'He','Ne','Ar','Kr','Xe','Rn'] |
92 | 92 |
|
93 | | - labels = {} |
94 | | - types = [] |
95 | | - count = 0 |
| 93 | +def get_formula(ase_obj, find_gcd=True): |
| 94 | + parsed_formula = {} |
96 | 95 |
|
97 | | - for k, label in enumerate(ase_obj.get_chemical_symbols()): |
98 | | - if label not in labels: |
99 | | - labels[label] = count |
100 | | - types.append([k+1]) |
101 | | - count += 1 |
| 96 | + for label in ase_obj.get_chemical_symbols(): |
| 97 | + if label not in parsed_formula: |
| 98 | + parsed_formula[label] = 1 |
102 | 99 | else: |
103 | | - types[ labels[label] ].append(k+1) |
| 100 | + parsed_formula[label] += 1 |
104 | 101 |
|
105 | | - atoms = labels.keys() |
106 | | - atoms = [x for x in formula_sequence if x in atoms] + [x for x in atoms if x not in formula_sequence] |
| 102 | + expanded = reduce(fractions.gcd, parsed_formula.values()) if find_gcd else 1 |
| 103 | + if expanded > 1: |
| 104 | + parsed_formula = {el: int(content / float(expanded)) |
| 105 | + for el, content in parsed_formula.items()} |
| 106 | + |
| 107 | + atoms = parsed_formula.keys() |
| 108 | + atoms = [x for x in FORMULA_SEQUENCE if x in atoms] + [x for x in atoms if x not in FORMULA_SEQUENCE] |
107 | 109 | formula = '' |
108 | 110 | for atom in atoms: |
109 | | - n = len(types[labels[atom]]) |
110 | | - if n == 1: |
111 | | - n = '' |
112 | | - else: |
113 | | - n = str(n) |
114 | | - formula += atom + n |
| 111 | + index = parsed_formula[atom] |
| 112 | + index = '' if index == 1 else str(index) |
| 113 | + formula += atom + index |
115 | 114 |
|
116 | 115 | return formula |
| 116 | + |
| 117 | + |
| 118 | +def sgn_to_crsystem(number): |
| 119 | + if 195 <= number <= 230: |
| 120 | + return 'cubic' |
| 121 | + elif 168 <= number <= 194: |
| 122 | + return 'hexagonal' |
| 123 | + elif 143 <= number <= 167: |
| 124 | + return 'trigonal' |
| 125 | + elif 75 <= number <= 142: |
| 126 | + return 'tetragonal' |
| 127 | + elif 16 <= number <= 74: |
| 128 | + return 'orthorhombic' |
| 129 | + elif 3 <= number <= 15: |
| 130 | + return 'monoclinic' |
| 131 | + else: |
| 132 | + return 'triclinic' |
0 commit comments