class ComputedReactionTest(unittest.TestCase):

    def setUp(self):
        d = [{"correction": 0.0, "data": {}, "energy": -108.56492362,
              "parameters": {}, "composition": {"Li": 54}},
             {"correction": 0.0, "data": {}, "energy": -577.94689128,
              "parameters": {}, "composition": {"O": 32, "Li": 64}},
             {"correction": 0.0, "data": {}, "energy": -17.02844794,
              "parameters": {}, "composition": {"O": 2}},
             {"correction": 0.0, "data": {}, "energy": -959.64693323,
              "parameters": {}, "composition": {"O": 72, "Li": 72}}]
        entries = []
        for e in d:
            entries.append(ComputedEntry.from_dict(e))
        rcts = list(filter(lambda e: e.composition.reduced_formula in ["Li", "O2"],
                      entries))
        prods = list(filter(lambda e: e.composition.reduced_formula == "Li2O2",
                       entries))

        self.rxn = ComputedReaction(rcts, prods)

    def test_calculated_reaction_energy(self):
        self.assertAlmostEqual(self.rxn.calculated_reaction_energy,
                               - 5.60748821935)

    def test_init(self):
        self.assertEqual(str(self.rxn), "1.000 O2 + 2.000 Li -> 1.000 Li2O2")

    def test_to_from_dict(self):
        d = self.rxn.as_dict()
        new_rxn = ComputedReaction.from_dict(d)
        self.assertEqual(str(new_rxn), "1.000 O2 + 2.000 Li -> 1.000 Li2O2")
Exemplo n.º 2
0
class ComputedReactionTest(unittest.TestCase):
    def setUp(self):
        d = [{
            "correction": 0.0,
            "data": {},
            "energy": -108.56492362,
            "parameters": {},
            "composition": {
                "Li": 54
            }
        }, {
            "correction": 0.0,
            "data": {},
            "energy": -577.94689128,
            "parameters": {},
            "composition": {
                "O": 32,
                "Li": 64
            }
        }, {
            "correction": 0.0,
            "data": {},
            "energy": -17.02844794,
            "parameters": {},
            "composition": {
                "O": 2
            }
        }, {
            "correction": 0.0,
            "data": {},
            "energy": -959.64693323,
            "parameters": {},
            "composition": {
                "O": 72,
                "Li": 72
            }
        }]
        entries = []
        for e in d:
            entries.append(ComputedEntry.from_dict(e))
        rcts = list(
            filter(lambda e: e.composition.reduced_formula in ["Li", "O2"],
                   entries))
        prods = list(
            filter(lambda e: e.composition.reduced_formula == "Li2O2",
                   entries))

        self.rxn = ComputedReaction(rcts, prods)

    def test_calculated_reaction_energy(self):
        self.assertAlmostEqual(self.rxn.calculated_reaction_energy,
                               -5.60748821935)

    def test_init(self):
        self.assertEqual(str(self.rxn), "1.000 O2 + 2.000 Li -> 1.000 Li2O2")

    def test_to_from_dict(self):
        d = self.rxn.as_dict()
        new_rxn = ComputedReaction.from_dict(d)
        self.assertEqual(str(new_rxn), "1.000 O2 + 2.000 Li -> 1.000 Li2O2")
Exemplo n.º 3
0
class ComputedReactionTest(unittest.TestCase):
    def setUp(self):
        d = [
            {
                "correction": 0.0,
                "data": {},
                "energy": -108.56492362,
                "parameters": {},
                "composition": {
                    "Li": 54
                },
            },
            {
                "correction": 0.0,
                "data": {},
                "energy": -577.94689128,
                "parameters": {},
                "composition": {
                    "O": 32,
                    "Li": 64
                },
            },
            {
                "correction": 0.0,
                "data": {},
                "energy": -17.02844794,
                "parameters": {},
                "composition": {
                    "O": 2
                },
            },
            {
                "correction": 0.0,
                "data": {},
                "energy": -959.64693323,
                "parameters": {},
                "composition": {
                    "O": 72,
                    "Li": 72
                },
            },
        ]
        entries = []
        for e in d:
            entries.append(ComputedEntry.from_dict(e))
        rcts = list(
            filter(lambda e: e.composition.reduced_formula in ["Li", "O2"],
                   entries))
        prods = list(
            filter(lambda e: e.composition.reduced_formula == "Li2O2",
                   entries))

        self.rxn = ComputedReaction(rcts, prods)

    def test_calculated_reaction_energy(self):
        self.assertAlmostEqual(self.rxn.calculated_reaction_energy,
                               -5.60748821935)

    def test_calculated_reaction_energy_uncertainty(self):
        d = [
            {
                "correction": 0.0,
                "data": {},
                "energy": -108.56492362,
                "parameters": {},
                "composition": {
                    "Li": 54
                },
            },
            {
                "correction": 0.0,
                "data": {},
                "energy": -17.02844794,
                "parameters": {},
                "composition": {
                    "O": 2
                },
            },
            {
                '@module':
                'pymatgen.entries.computed_entries',
                '@class':
                'ComputedEntry',
                'energy':
                -38.76889738,
                'composition':
                defaultdict(float, {
                    'Li': 4.0,
                    'O': 4.0
                }),
                'energy_adjustments': [{
                    '@module':
                    'pymatgen.entries.computed_entries',
                    '@class':
                    'ConstantEnergyAdjustment',
                    '@version':
                    '2020.6.8',
                    'value':
                    -1.864,
                    'uncertainty':
                    0.0744,
                    'name':
                    'MP2020 Composition Correction',
                    'cls': {
                        '@module': 'pymatgen.entries.compatibility',
                        '@class': 'MaterialsProject2020Compatibility',
                        '@version': '2020.6.8',
                        'compat_type': 'Advanced',
                        'correct_peroxide': True,
                        'check_potcar_hash': False
                    },
                    'description':
                    'Constant energy adjustment (-1.864 eV)'
                }],
                'parameters': {
                    'run_type': 'GGA',
                    'is_hubbard': False,
                    'pseudo_potential': {
                        'functional': 'PBE',
                        'labels': ['Li_sv', 'O'],
                        'pot_type': 'paw'
                    },
                    'hubbards': {},
                    'potcar_symbols': ['PBE Li_sv', 'PBE O'],
                    'oxide_type': 'peroxide'
                },
                'data': {
                    'oxide_type': 'peroxide'
                },
                'entry_id':
                'mp-841',
                'correction':
                -1.864
            },
        ]
        entries = []
        for e in d:
            entries.append(ComputedEntry.from_dict(e))
        rcts = list(
            filter(lambda e: e.composition.reduced_formula in ["Li", "O2"],
                   entries))
        prods = list(
            filter(lambda e: e.composition.reduced_formula == "Li2O2",
                   entries))

        rxn_with_uncertainty = ComputedReaction(rcts, prods)
        self.assertAlmostEqual(
            rxn_with_uncertainty.calculated_reaction_energy_uncertainty,
            0.5 * 0.0744)

    def test_calculated_reaction_energy_uncertainty_for_no_uncertainty(self):
        # test that reaction_energy_uncertainty property doesn't cause errors
        # when products/reactants have no uncertainties
        self.assertAlmostEqual(self.rxn.calculated_reaction_energy_uncertainty,
                               0)

    def test_calculated_reaction_energy_uncertainty_for_nan(self):
        # test that reaction_energy_uncertainty property is nan when the uncertainty
        # for any product/reactant is nan
        d = [
            {
                "correction": 0.0,
                "data": {},
                "energy": -108.56492362,
                "parameters": {},
                "composition": {
                    "Li": 54
                },
            },
            {
                "correction": 0.0,
                "data": {},
                "energy": -17.02844794,
                "parameters": {},
                "composition": {
                    "O": 2
                },
            },
            {
                '@module':
                'pymatgen.entries.computed_entries',
                '@class':
                'ComputedEntry',
                'energy':
                -38.76889738,
                'composition':
                defaultdict(float, {
                    'Li': 4.0,
                    'O': 4.0
                }),
                'energy_adjustments': [{
                    '@module':
                    'pymatgen.entries.computed_entries',
                    '@class':
                    'ConstantEnergyAdjustment',
                    '@version':
                    '2020.6.8',
                    'value':
                    -1.864,
                    'uncertainty':
                    np.nan,
                    'name':
                    'MP2020 Composition Correction',
                    'cls': {
                        '@module': 'pymatgen.entries.compatibility',
                        '@class': 'MaterialsProject2020Compatibility',
                        '@version': '2020.6.8',
                        'compat_type': 'Advanced',
                        'correct_peroxide': True,
                        'check_potcar_hash': False
                    },
                    'description':
                    'Constant energy adjustment (-1.864 eV)'
                }],
                'parameters': {
                    'run_type': 'GGA',
                    'is_hubbard': False,
                    'pseudo_potential': {
                        'functional': 'PBE',
                        'labels': ['Li_sv', 'O'],
                        'pot_type': 'paw'
                    },
                    'hubbards': {},
                    'potcar_symbols': ['PBE Li_sv', 'PBE O'],
                    'oxide_type': 'peroxide'
                },
                'data': {
                    'oxide_type': 'peroxide'
                },
                'entry_id':
                'mp-841',
                'correction':
                -1.864
            },
        ]
        entries = []
        for e in d:
            entries.append(ComputedEntry.from_dict(e))
        rcts = list(
            filter(lambda e: e.composition.reduced_formula in ["Li", "O2"],
                   entries))
        prods = list(
            filter(lambda e: e.composition.reduced_formula == "Li2O2",
                   entries))

        rxn_with_uncertainty = ComputedReaction(rcts, prods)
        self.assertTrue(
            isnan(rxn_with_uncertainty.calculated_reaction_energy_uncertainty))

    def test_init(self):
        self.assertEqual(str(self.rxn), "2 Li + O2 -> Li2O2")

    def test_to_from_dict(self):
        d = self.rxn.as_dict()
        new_rxn = ComputedReaction.from_dict(d)
        self.assertEqual(str(new_rxn), "2 Li + O2 -> Li2O2")

    def test_all_entries(self):
        for c, e in zip(self.rxn.coeffs, self.rxn.all_entries):
            if c > 0:
                self.assertEqual(e.composition.reduced_formula, "Li2O2")
                self.assertAlmostEqual(e.energy, -959.64693323)
Exemplo n.º 4
0
class ComputedReactionTest(unittest.TestCase):
    def setUp(self):
        d = [
            {
                "correction": 0.0,
                "data": {},
                "energy": -108.56492362,
                "parameters": {},
                "composition": {"Li": 54},
            },
            {
                "correction": 0.0,
                "data": {},
                "energy": -577.94689128,
                "parameters": {},
                "composition": {"O": 32, "Li": 64},
            },
            {
                "correction": 0.0,
                "data": {},
                "energy": -17.02844794,
                "parameters": {},
                "composition": {"O": 2},
            },
            {
                "correction": 0.0,
                "data": {},
                "energy": -959.64693323,
                "parameters": {},
                "composition": {"O": 72, "Li": 72},
            },
        ]
        entries = []
        for e in d:
            entries.append(ComputedEntry.from_dict(e))
        rcts = list(
            filter(lambda e: e.composition.reduced_formula in ["Li", "O2"], entries)
        )
        prods = list(
            filter(lambda e: e.composition.reduced_formula == "Li2O2", entries)
        )

        self.rxn = ComputedReaction(rcts, prods)

    def test_calculated_reaction_energy(self):
        self.assertAlmostEqual(self.rxn.calculated_reaction_energy, -5.60748821935)

    def test_calculated_reaction_energy_uncertainty(self):
        d = [
            {
                "correction": 0.0,
                "data": {},
                "energy": -108.56492362,
                "parameters": {},
                "composition": {"Li": 54},
            },
            {
                "correction": 0.0,
                "data": {},
                "energy": -17.02844794,
                "parameters": {},
                "composition": {"O": 2},
            },
            {
                "@module": "pymatgen.entries.computed_entries",
                "@class": "ComputedEntry",
                "energy": -38.76889738,
                "composition": defaultdict(float, {"Li": 4.0, "O": 4.0}),
                "energy_adjustments": [
                    {
                        "@module": "pymatgen.entries.computed_entries",
                        "@class": "ConstantEnergyAdjustment",
                        "@version": "2020.6.8",
                        "value": -1.864,
                        "uncertainty": 0.0744,
                        "name": "MP2020 Composition Correction",
                        "cls": {
                            "@module": "pymatgen.entries.compatibility",
                            "@class": "MaterialsProject2020Compatibility",
                            "@version": "2020.6.8",
                            "compat_type": "Advanced",
                            "correct_peroxide": True,
                            "check_potcar_hash": False,
                        },
                        "description": "Constant energy adjustment (-1.864 eV)",
                    }
                ],
                "parameters": {
                    "run_type": "GGA",
                    "is_hubbard": False,
                    "pseudo_potential": {
                        "functional": "PBE",
                        "labels": ["Li_sv", "O"],
                        "pot_type": "paw",
                    },
                    "hubbards": {},
                    "potcar_symbols": ["PBE Li_sv", "PBE O"],
                    "oxide_type": "peroxide",
                },
                "data": {"oxide_type": "peroxide"},
                "entry_id": "mp-841",
                "correction": -1.864,
            },
        ]
        entries = []
        for e in d:
            entries.append(ComputedEntry.from_dict(e))
        rcts = list(
            filter(lambda e: e.composition.reduced_formula in ["Li", "O2"], entries)
        )
        prods = list(
            filter(lambda e: e.composition.reduced_formula == "Li2O2", entries)
        )

        rxn_with_uncertainty = ComputedReaction(rcts, prods)
        self.assertAlmostEqual(
            rxn_with_uncertainty.calculated_reaction_energy_uncertainty, 0.5 * 0.0744
        )

    def test_calculated_reaction_energy_uncertainty_for_no_uncertainty(self):
        # test that reaction_energy_uncertainty property doesn't cause errors
        # when products/reactants have no uncertainties
        self.assertAlmostEqual(self.rxn.calculated_reaction_energy_uncertainty, 0)

    def test_calculated_reaction_energy_uncertainty_for_nan(self):
        # test that reaction_energy_uncertainty property is nan when the uncertainty
        # for any product/reactant is nan
        d = [
            {
                "correction": 0.0,
                "data": {},
                "energy": -108.56492362,
                "parameters": {},
                "composition": {"Li": 54},
            },
            {
                "correction": 0.0,
                "data": {},
                "energy": -17.02844794,
                "parameters": {},
                "composition": {"O": 2},
            },
            {
                "@module": "pymatgen.entries.computed_entries",
                "@class": "ComputedEntry",
                "energy": -38.76889738,
                "composition": defaultdict(float, {"Li": 4.0, "O": 4.0}),
                "energy_adjustments": [
                    {
                        "@module": "pymatgen.entries.computed_entries",
                        "@class": "ConstantEnergyAdjustment",
                        "@version": "2020.6.8",
                        "value": -1.864,
                        "uncertainty": np.nan,
                        "name": "MP2020 Composition Correction",
                        "cls": {
                            "@module": "pymatgen.entries.compatibility",
                            "@class": "MaterialsProject2020Compatibility",
                            "@version": "2020.6.8",
                            "compat_type": "Advanced",
                            "correct_peroxide": True,
                            "check_potcar_hash": False,
                        },
                        "description": "Constant energy adjustment (-1.864 eV)",
                    }
                ],
                "parameters": {
                    "run_type": "GGA",
                    "is_hubbard": False,
                    "pseudo_potential": {
                        "functional": "PBE",
                        "labels": ["Li_sv", "O"],
                        "pot_type": "paw",
                    },
                    "hubbards": {},
                    "potcar_symbols": ["PBE Li_sv", "PBE O"],
                    "oxide_type": "peroxide",
                },
                "data": {"oxide_type": "peroxide"},
                "entry_id": "mp-841",
                "correction": -1.864,
            },
        ]
        entries = []
        for e in d:
            entries.append(ComputedEntry.from_dict(e))
        rcts = list(
            filter(lambda e: e.composition.reduced_formula in ["Li", "O2"], entries)
        )
        prods = list(
            filter(lambda e: e.composition.reduced_formula == "Li2O2", entries)
        )

        rxn_with_uncertainty = ComputedReaction(rcts, prods)
        self.assertTrue(
            isnan(rxn_with_uncertainty.calculated_reaction_energy_uncertainty)
        )

    def test_init(self):
        self.assertEqual(str(self.rxn), "2 Li + O2 -> Li2O2")

    def test_to_from_dict(self):
        d = self.rxn.as_dict()
        new_rxn = ComputedReaction.from_dict(d)
        self.assertEqual(str(new_rxn), "2 Li + O2 -> Li2O2")

    def test_all_entries(self):
        for c, e in zip(self.rxn.coeffs, self.rxn.all_entries):
            if c > 0:
                self.assertEqual(e.composition.reduced_formula, "Li2O2")
                self.assertAlmostEqual(e.energy, -959.64693323)