Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def parse_tok(t):
if re.match(r"\w+-\d+", t):
return {"task_id": t}
elif "-" in t:
elements = [parse_sym(sym) for sym in t.split("-")]
chemsyss = []
for cs in itertools.product(*elements):
if len(set(cs)) == len(cs):
# Check for valid symbols
cs = [Element(s).symbol for s in cs]
chemsyss.append("-".join(sorted(cs)))
return {"chemsys": {"$in": chemsyss}}
else:
all_formulas = set()
explicit_els = []
wild_card_els = []
for sym in re.findall(
r"(\*[\.\d]*|\{.*\}[\.\d]*|[A-Z][a-z]*)[\.\d]*", t):
if ("*" in sym) or ("{" in sym):
wild_card_els.append(sym)
else:
m = re.match(r"([A-Z][a-z]*)[\.\d]*", sym)
explicit_els.append(m.group(1))
nelements = len(wild_card_els) + len(set(explicit_els))
parts = re.split(r"(\*|\{.*\})", t)
parts = [parse_sym(s) for s in parts if s != ""]
E.g., The electronic structure for Fe is represented as:
[(1, "s", 2), (2, "s", 2), (2, "p", 6), (3, "s", 2), (3, "p", 6),
(3, "d", 6), (4, "s", 2)]
"""
estr = self._data["Electronic structure"]
def parse_orbital(orbstr):
m = re.match(r"(\d+)([spdfg]+)<sup>(\d+)</sup>", orbstr)
if m:
return int(m.group(1)), m.group(2), int(m.group(3))
return orbstr
data = [parse_orbital(s) for s in estr.split(".")]
if data[0][0] == "[":
sym = data[0].replace("[", "").replace("]", "")
data = Element(sym).full_electronic_structure + data[1:]
return data
"""
am_1, am_2, tm_1, tm_2 = None, None, None, None
compstr_spl = [''.join(g) for _, g in groupby(str(compstr), str.isalpha)]
for l in range(len(compstr_spl)):
try:
if ptable.Element(compstr_spl[l]).is_alkaline or ptable.Element(
compstr_spl[l]).is_alkali or ptable.Element(compstr_spl[l]).is_rare_earth_metal:
if am_1 is None:
am_1 = [compstr_spl[l], float(compstr_spl[l + 1])]
elif am_2 is None:
am_2 = [compstr_spl[l], float(compstr_spl[l + 1])]
if ptable.Element(compstr_spl[l]).is_transition_metal and not (
ptable.Element(compstr_spl[l]).is_rare_earth_metal):
if tm_1 is None:
tm_1 = [compstr_spl[l], float(compstr_spl[l + 1])]
elif tm_2 is None:
tm_2 = [compstr_spl[l], float(compstr_spl[l + 1])]
# stoichiometries raise ValueErrors in pymatgen .is_alkaline etc., ignore these errors and skip that entry
except ValueError:
pass
return am_1, am_2, tm_1, tm_2
def is_element(txt):
"""
Checks if the string is a chemical symbol.
:param txt: input string
:return: True or False
"""
try:
Element(txt)
return True
except ValueError:
return False
def from_composition_and_pd(comp, pd, working_ion_symbol="Li"):
"""
Convenience constructor to make a ConversionElectrode from a
composition and a phase diagram.
Args:
comp:
Starting composition for ConversionElectrode, e.g.,
Composition("FeF3")
pd:
A PhaseDiagram of the relevant system (e.g., Li-Fe-F)
working_ion_symbol:
Element symbol of working ion. Defaults to Li.
"""
working_ion = Element(working_ion_symbol)
entry = None
working_ion_entry = None
for e in pd.stable_entries:
if e.composition.reduced_formula == comp.reduced_formula:
entry = e
elif e.is_element and \
e.composition.reduced_formula == working_ion_symbol:
working_ion_entry = e
if not entry:
raise ValueError("Not stable compound found at composition {}."
.format(comp))
profile = pd.get_element_profile(working_ion, comp)
# Need to reverse because voltage goes form most charged to most
# discharged.
def is_element(txt):
"""Checks if the string is a chemical symbol.
Args:
txt: The input string.
Returns:
True if the string is a chemical symbol, e.g. Hg, Fe, V, etc. False otherwise.
"""
try:
Element(txt)
return True
except ValueError:
return False
else:
if len(structure.sites) < 45:
structure.make_supercell(2)
# Create a dict of sites as keys and lists of their
# bonded neighbors as values.
sites = structure.sites
bonds = {}
for site in sites:
bonds[site] = []
for i in range(len(sites)):
site_1 = sites[i]
for site_2 in sites[i+1:]:
if (site_1.distance(site_2) <
float(Element(site_1.specie).atomic_radius
+ Element(site_2.specie).atomic_radius) * 1.1):
bonds[site_1].append(site_2)
bonds[site_2].append(site_1)
# Assimilate all bonded atoms in a cluster; terminate
# when it stops growing.
cluster_terminated = False
while not cluster_terminated:
original_cluster_size = len(bonds[sites[0]])
for site in bonds[sites[0]]:
bonds[sites[0]] += [
s for s in bonds[site] if s not in bonds[sites[0]]]
if len(bonds[sites[0]]) == original_cluster_size:
cluster_terminated = True
original_cluster = bonds[sites[0]]
am_1, am_2, tm_1, tm_2 = None, None, None, None
compstr_spl = ["".join(g) for _, g in groupby(str(compstr), str.isalpha)]
for l in range(len(compstr_spl)):
try:
if (
ptable.Element(compstr_spl[l]).is_alkaline
or ptable.Element(compstr_spl[l]).is_alkali
or ptable.Element(compstr_spl[l]).is_rare_earth_metal
):
if am_1 is None:
am_1 = [compstr_spl[l], float(compstr_spl[l + 1])]
elif am_2 is None:
am_2 = [compstr_spl[l], float(compstr_spl[l + 1])]
if ptable.Element(compstr_spl[l]).is_transition_metal and not (
ptable.Element(compstr_spl[l]).is_rare_earth_metal
):
if tm_1 is None:
tm_1 = [compstr_spl[l], float(compstr_spl[l + 1])]
elif tm_2 is None:
tm_2 = [compstr_spl[l], float(compstr_spl[l + 1])]
# stoichiometries raise ValueErrors in pymatgen .is_alkaline etc., ignore these errors and skip that entry
except ValueError:
pass
return am_1, am_2, tm_1, tm_2
def max_electronegativity(self):
"""
returns the maximum pairwise electronegativity difference
"""
maximum = 0
for e1, e2 in combinations(self.elements, 2):
if abs(Element(e1).X - Element(e2).X) > maximum:
maximum = abs(Element(e1).X - Element(e2).X)
return maximum
entries.append(myPDEntry)
pd = PhaseDiagram(entries)
#plotter = PDPlotter(gppd)
#plotter.show()
ppda = PDAnalyzer(pd)
eabove=ppda.get_decomp_and_e_above_hull(myPDEntry)
print "Energy above hull: ", eabove[1]
print "Decomposition: ", eabove[0]
return eabove
else: #Grand potential phase diagram
orange = np.arange(ostart, oend+ostep, ostep) #add ostep because otherwise the range ends before oend
for o_chem_pot in orange:
entries = list(chemsys_entries)
myGrandPDEntry = GrandPotPDEntry(myPDEntry,{Element('O'): float(o_chem_pot)}) #need grand pot pd entry for GPPD
entries.append(myGrandPDEntry)
gppd = GrandPotentialPhaseDiagram(entries,{Element('O'): float(o_chem_pot)})
gppda = PDAnalyzer(gppd)
geabove=gppda.get_decomp_and_e_above_hull(myGrandPDEntry, True)
print "******** Decomposition for mu_O = %s eV ********" % o_chem_pot
print "%30s%1.4f" % ("mu_O: ",o_chem_pot)
print "%30s%1.4f" % ("Energy above hull (eV): ",geabove[1])
decomp=geabove[0]
#print "Decomp: ", decomp
print "%30s" % "Decomposition: "
for dkey in decomp.keys():
print "%30s:%1.4f" % (dkey.composition,decomp[dkey])
return