本文整理汇总了Python中pymatgen.core.composition.Composition.as_dict方法的典型用法代码示例。如果您正苦于以下问题:Python Composition.as_dict方法的具体用法?Python Composition.as_dict怎么用?Python Composition.as_dict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymatgen.core.composition.Composition
的用法示例。
在下文中一共展示了Composition.as_dict方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_from_dict
# 需要导入模块: from pymatgen.core.composition import Composition [as 别名]
# 或者: from pymatgen.core.composition.Composition import as_dict [as 别名]
def test_from_dict(self):
sym_dict = {"Fe": 6, "O": 8}
self.assertEqual(
Composition.from_dict(sym_dict).reduced_formula, "Fe3O4", "Creation form sym_amount dictionary failed!"
)
comp = Composition({"Fe2+": 2, "Fe3+": 4, "O2-": 8})
comp2 = Composition.from_dict(comp.as_dict())
self.assertEqual(comp, comp2)
示例2: to_reduced_dict
# 需要导入模块: from pymatgen.core.composition import Composition [as 别名]
# 或者: from pymatgen.core.composition.Composition import as_dict [as 别名]
def to_reduced_dict(self):
"""
Returns:
dict with element symbol and reduced amount e.g.,
{"Fe": 2.0, "O":3.0}.
"""
reduced_formula = self._composition.reduced_formula
c = Composition(reduced_formula)
d = c.as_dict()
d['charge'] = self._charge
return d
示例3: ComputedEntry
# 需要导入模块: from pymatgen.core.composition import Composition [as 别名]
# 或者: from pymatgen.core.composition.Composition import as_dict [as 别名]
class ComputedEntry(PDEntry, MSONable):
"""
An lightweight ComputedEntry object containing key computed data
for many purposes. Extends a PDEntry so that it can be used for phase
diagram generation. The difference between a ComputedEntry and a standard
PDEntry is that it includes additional parameters like a correction and
run_parameters.
"""
def __init__(self, composition, energy, correction=0.0, parameters=None,
data=None, entry_id=None, attribute=None):
"""
Initializes a ComputedEntry.
Args:
composition (Composition): Composition of the entry. For
flexibility, this can take the form of all the typical input
taken by a Composition, including a {symbol: amt} dict,
a string formula, and others.
energy (float): Energy of the entry. Usually the final calculated
energy from VASP or other electronic structure codes.
correction (float): A correction to be applied to the energy.
This is used to modify the energy for certain analyses.
Defaults to 0.0.
parameters (dict): An optional dict of parameters associated with
the entry. Defaults to None.
data (dict): An optional dict of any additional data associated
with the entry. Defaults to None.
entry_id (obj): An optional id to uniquely identify the entry.
attribute: Optional attribute of the entry. This can be used to
specify that the entry is a newly found compound, or to specify
a particular label for the entry, or else ... Used for further
analysis and plotting purposes. An attribute can be anything
but must be MSONable.
"""
self.uncorrected_energy = energy
self.composition = Composition(composition)
self.correction = correction
self.parameters = parameters if parameters else {}
self.data = data if data else {}
self.entry_id = entry_id
self.name = self.composition.reduced_formula
self.attribute = attribute
@property
def energy(self):
"""
Returns the *corrected* energy of the entry.
"""
return self.uncorrected_energy + self.correction
def __repr__(self):
output = ["ComputedEntry {}".format(self.composition.formula),
"Energy = {:.4f}".format(self.uncorrected_energy),
"Correction = {:.4f}".format(self.correction), "Parameters:"]
for k, v in self.parameters.items():
output.append("{} = {}".format(k, v))
output.append("Data:")
for k, v in self.data.items():
output.append("{} = {}".format(k, v))
return "\n".join(output)
def __str__(self):
return self.__repr__()
@classmethod
def from_dict(cls, d):
dec = MontyDecoder()
return cls(d["composition"], d["energy"], d["correction"],
dec.process_decoded(d.get("parameters", {})),
dec.process_decoded(d.get("data", {})),
entry_id=d.get("entry_id", None),
attribute=d["attribute"] if "attribute" in d else None)
def as_dict(self):
return {"@module": self.__class__.__module__,
"@class": self.__class__.__name__,
"energy": self.uncorrected_energy,
"composition": self.composition.as_dict(),
"correction": self.correction,
"parameters": json.loads(json.dumps(self.parameters,
cls=MontyEncoder)),
"data": json.loads(json.dumps(self.data, cls=MontyEncoder)),
"entry_id": self.entry_id,
"attribute": self.attribute}
示例4: Ion
# 需要导入模块: from pymatgen.core.composition import Composition [as 别名]
# 或者: from pymatgen.core.composition.Composition import as_dict [as 别名]
#.........这里部分代码省略.........
chg_str = "[{}-]".format(formula_double_format(abs(charge),
False))
else:
chg_str = "(aq)"
return reduced_formula + chg_str
@property
def alphabetical_formula(self):
"""
Returns a reduced formula string with appended charge
"""
alph_formula = self._composition.alphabetical_formula
chg_str = ""
if self._charge > 0:
chg_str = " +" + formula_double_format(self._charge, False)
elif self._charge < 0:
chg_str = " " + formula_double_format(self._charge, False)
return alph_formula + chg_str
@property
def charge(self):
"""
Charge of the ion
"""
return self._charge
@property
def composition(self):
"""
Return composition object
"""
return self._composition
def as_dict(self):
"""
Returns:
dict with composition, as well as charge
"""
d = self._composition.as_dict()
d['charge'] = self._charge
return d
@classmethod
def from_dict(cls, d):
"""
Generates an ion object from a dict created by as_dict().
Args:
d:
{symbol: amount} dict.
"""
# composition = Composition.from_dict(d['composition'])
charge = d['charge']
composition = Composition({i: d[i] for i in d if i != 'charge'})
return Ion(composition, charge)
@property
def to_reduced_dict(self):
"""
Returns:
dict with element symbol and reduced amount e.g.,
{"Fe": 2.0, "O":3.0}.
"""
reduced_formula = self._composition.reduced_formula
c = Composition(reduced_formula)
d = c.as_dict()
示例5: PDEntry
# 需要导入模块: from pymatgen.core.composition import Composition [as 别名]
# 或者: from pymatgen.core.composition.Composition import as_dict [as 别名]
class PDEntry(PMGSONable):
"""
An object encompassing all relevant data for phase diagrams.
.. attribute:: name
A name for the entry. This is the string shown in the phase diagrams.
By default, this is the reduced formula for the composition, but can be
set to some other string for display purposes.
Args:
comp: Composition as a pymatgen.core.structure.Composition
energy: Energy for composition.
name: Optional parameter to name the entry. Defaults to the reduced
chemical formula.
attribute: Optional attribute of the entry. This can be used to
specify that the entry is a newly found compound, or to specify a
particular label for the entry, or else ... Used for further
analysis and plotting purposes. An attribute can be anything
but must be PMGSONable.
"""
def __init__(self, composition, energy, name=None, attribute=None):
self.energy = energy
self.composition = Composition(composition)
self.name = name if name else self.composition.reduced_formula
self.attribute = attribute
@property
def energy_per_atom(self):
"""
Returns the final energy per atom.
"""
return self.energy / self.composition.num_atoms
@property
def is_element(self):
"""
True if the entry is an element.
"""
return self.composition.is_element
def __repr__(self):
return "PDEntry : {} with energy = {:.4f}".format(self.composition,
self.energy)
def __str__(self):
return self.__repr__()
def as_dict(self):
return {"@module": self.__class__.__module__,
"@class": self.__class__.__name__,
"composition": self.composition.as_dict(),
"energy": self.energy,
"name": self.name,
"attribute": self.attribute}
@classmethod
def from_dict(cls, d):
return cls(Composition(d["composition"]), d["energy"], d["name"],
d["attribute"] if "attribute" in d else None)