def setUp(self): comp = Composition("LiFeO2") entry = PDEntry(comp, 53) self.transformed_entry = TransformedPDEntry( { DummySpecie('Xa'): 1, DummySpecie("Xb"): 1 }, entry)
def transform_entries(self, entries, terminal_compositions): """ Method to transform all entries to the composition coordinate in the terminal compositions. If the entry does not fall within the space defined by the terminal compositions, they are excluded. For example, Li3PO4 is mapped into a Li2O:1.5, P2O5:0.5 composition. The terminal compositions are represented by DummySpecies. Args: entries: Sequence of all input entries terminal_compositions: Terminal compositions of phase space. Returns: Sequence of TransformedPDEntries falling within the phase space. """ new_entries = [] if self.normalize_terminals: fractional_comp = [ c.get_fractional_composition() for c in terminal_compositions ] else: fractional_comp = terminal_compositions #Map terminal compositions to unique dummy species. sp_mapping = collections.OrderedDict() for i, comp in enumerate(fractional_comp): sp_mapping[comp] = DummySpecie("X" + chr(102 + i)) for entry in entries: try: rxn = Reaction(fractional_comp, [entry.composition]) rxn.normalize_to(entry.composition) #We only allow reactions that have positive amounts of #reactants. if all([ rxn.get_coeff(comp) <= CompoundPhaseDiagram.amount_tol for comp in fractional_comp ]): newcomp = { sp_mapping[comp]: -rxn.get_coeff(comp) for comp in fractional_comp } newcomp = { k: v for k, v in newcomp.items() if v > CompoundPhaseDiagram.amount_tol } transformed_entry = \ TransformedPDEntry(Composition(newcomp), entry) new_entries.append(transformed_entry) except ReactionError: #If the reaction can't be balanced, the entry does not fall #into the phase space. We ignore them. pass return new_entries, sp_mapping
class TransformedPDEntryTest(unittest.TestCase): ''' Test all functions using a ficitious entry ''' def setUp(self): comp = Composition("LiFeO2") entry = PDEntry(comp, 53) self.transformed_entry = TransformedPDEntry( { DummySpecie('Xa'): 1, DummySpecie("Xb"): 1 }, entry) def test_get_energy(self): self.assertEqual(self.transformed_entry.energy, 53, "Wrong energy!") self.assertEqual(self.transformed_entry.original_entry.energy, 53.0) def test_get_energy_per_atom(self): self.assertEqual(self.transformed_entry.energy_per_atom, 53.0 / 2) def test_get_name(self): self.assertEqual(self.transformed_entry.name, 'LiFeO2', "Wrong name!") def test_get_composition(self): comp = self.transformed_entry.composition expected_comp = Composition({ DummySpecie('Xa'): 1, DummySpecie('Xb'): 1 }) self.assertEqual(comp, expected_comp, "Wrong composition!") def test_is_element(self): self.assertFalse(self.transformed_entry.is_element) def test_to_from_dict(self): d = self.transformed_entry.as_dict() entry = TransformedPDEntry.from_dict(d) self.assertEqual(entry.name, 'LiFeO2', "Wrong name!") self.assertEqual(entry.energy_per_atom, 53.0 / 2) def test_str(self): self.assertIsNotNone(str(self.transformed_entry))
class TransformedPDEntryTest(unittest.TestCase): ''' Test all functions using a ficitious entry ''' def setUp(self): comp = Composition("LiFeO2") entry = PDEntry(comp, 53) self.transformed_entry = TransformedPDEntry({DummySpecie('Xa'): 1, DummySpecie("Xb"): 1}, entry) def test_get_energy(self): self.assertEqual(self.transformed_entry.energy, 53, "Wrong energy!") self.assertEqual(self.transformed_entry.original_entry.energy, 53.0) def test_get_energy_per_atom(self): self.assertEqual(self.transformed_entry.energy_per_atom, 53.0 / 2) def test_get_name(self): self.assertEqual(self.transformed_entry.name, 'LiFeO2', "Wrong name!") def test_get_composition(self): comp = self.transformed_entry.composition expected_comp = Composition({DummySpecie('Xa'): 1, DummySpecie('Xb'): 1}) self.assertEqual(comp, expected_comp, "Wrong composition!") def test_is_element(self): self.assertFalse(self.transformed_entry.is_element) def test_to_from_dict(self): d = self.transformed_entry.as_dict() entry = TransformedPDEntry.from_dict(d) self.assertEqual(entry.name, 'LiFeO2', "Wrong name!") self.assertEqual(entry.energy_per_atom, 53.0 / 2) def test_str(self): self.assertIsNotNone(str(self.transformed_entry))
def test_to_from_dict(self): d = self.transformed_entry.as_dict() entry = TransformedPDEntry.from_dict(d) self.assertEqual(entry.name, 'LiFeO2', "Wrong name!") self.assertEqual(entry.energy_per_atom, 53.0 / 2)
def setUp(self): comp = Composition("LiFeO2") entry = PDEntry(comp, 53) self.transformed_entry = TransformedPDEntry({DummySpecie('Xa'): 1, DummySpecie("Xb"): 1}, entry)