• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

materialsproject / pymatgen / 4075885785

pending completion
4075885785

push

github

Shyue Ping Ong
Merge branch 'master' of github.com:materialsproject/pymatgen

96 of 96 new or added lines in 27 files covered. (100.0%)

81013 of 102710 relevant lines covered (78.88%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.94
/pymatgen/alchemy/tests/test_materials.py
1
# Copyright (c) Pymatgen Development Team.
2
# Distributed under the terms of the MIT License.
3

4
from __future__ import annotations
1✔
5

6
import json
1✔
7
import os
1✔
8
import unittest
1✔
9
import warnings
1✔
10

11
import pytest
1✔
12

13
from pymatgen.alchemy.filters import ContainsSpecieFilter
1✔
14
from pymatgen.alchemy.materials import TransformedStructure
1✔
15
from pymatgen.core import SETTINGS
1✔
16
from pymatgen.core.structure import Structure
1✔
17
from pymatgen.io.vasp.sets import MPRelaxSet
1✔
18
from pymatgen.transformations.standard_transformations import (
1✔
19
    PartialRemoveSpecieTransformation,
20
    SubstitutionTransformation,
21
    SupercellTransformation,
22
)
23
from pymatgen.util.provenance import StructureNL
1✔
24
from pymatgen.util.testing import PymatgenTest
1✔
25

26

27
class TransformedStructureTest(PymatgenTest):
1✔
28
    def setUp(self):
1✔
29
        structure = PymatgenTest.get_structure("LiFePO4")
1✔
30
        self.structure = structure
1✔
31
        trans = [SubstitutionTransformation({"Li": "Na"})]
1✔
32
        self.trans = TransformedStructure(structure, trans)
1✔
33

34
    def test_append_transformation(self):
1✔
35
        t = SubstitutionTransformation({"Fe": "Mn"})
1✔
36
        self.trans.append_transformation(t)
1✔
37
        assert "NaMnPO4" == self.trans.final_structure.composition.reduced_formula
1✔
38
        assert len(self.trans.structures) == 3
1✔
39
        coords = []
1✔
40
        coords.append([0, 0, 0])
1✔
41
        coords.append([0.75, 0.5, 0.75])
1✔
42
        lattice = [
1✔
43
            [3.8401979337, 0.00, 0.00],
44
            [1.9200989668, 3.3257101909, 0.00],
45
            [0.00, -2.2171384943, 3.1355090603],
46
        ]
47
        struct = Structure(lattice, ["Si4+", "Si4+"], coords)
1✔
48
        ts = TransformedStructure(struct, [])
1✔
49
        ts.append_transformation(SupercellTransformation.from_scaling_factors(2, 1, 1))
1✔
50
        alt = ts.append_transformation(
1✔
51
            PartialRemoveSpecieTransformation("Si4+", 0.5, algo=PartialRemoveSpecieTransformation.ALGO_COMPLETE), 5
52
        )
53
        assert len(alt) == 2
1✔
54

55
    def test_append_filter(self):
1✔
56
        f3 = ContainsSpecieFilter(["O2-"], strict_compare=True, AND=False)
1✔
57
        self.trans.append_filter(f3)
1✔
58

59
    def test_get_vasp_input(self):
1✔
60
        SETTINGS["PMG_VASP_PSP_DIR"] = PymatgenTest.TEST_FILES_DIR
1✔
61
        potcar = self.trans.get_vasp_input(MPRelaxSet)["POTCAR"]
1✔
62
        assert "Na_pv\nFe_pv\nP\nO" == "\n".join(p.symbol for p in potcar)
1✔
63
        assert len(self.trans.structures) == 2
1✔
64

65
    def test_final_structure(self):
1✔
66
        assert "NaFePO4" == self.trans.final_structure.composition.reduced_formula
1✔
67

68
    def test_from_dict(self):
1✔
69
        d = json.load(open(os.path.join(PymatgenTest.TEST_FILES_DIR, "transformations.json")))
1✔
70
        d["other_parameters"] = {"tags": ["test"]}
1✔
71
        ts = TransformedStructure.from_dict(d)
1✔
72
        ts.other_parameters["author"] = "Will"
1✔
73
        ts.append_transformation(SubstitutionTransformation({"Fe": "Mn"}))
1✔
74
        assert "MnPO4" == ts.final_structure.composition.reduced_formula
1✔
75
        assert ts.other_parameters == {"author": "Will", "tags": ["test"]}
1✔
76

77
    def test_undo_and_redo_last_change(self):
1✔
78
        trans = [
1✔
79
            SubstitutionTransformation({"Li": "Na"}),
80
            SubstitutionTransformation({"Fe": "Mn"}),
81
        ]
82
        ts = TransformedStructure(self.structure, trans)
1✔
83
        assert "NaMnPO4" == ts.final_structure.composition.reduced_formula
1✔
84
        ts.undo_last_change()
1✔
85
        assert "NaFePO4" == ts.final_structure.composition.reduced_formula
1✔
86
        ts.undo_last_change()
1✔
87
        assert "LiFePO4" == ts.final_structure.composition.reduced_formula
1✔
88
        with pytest.raises(IndexError):
1✔
89
            ts.undo_last_change()
1✔
90
        ts.redo_next_change()
1✔
91
        assert "NaFePO4" == ts.final_structure.composition.reduced_formula
1✔
92
        ts.redo_next_change()
1✔
93
        assert "NaMnPO4" == ts.final_structure.composition.reduced_formula
1✔
94
        with pytest.raises(IndexError):
1✔
95
            ts.redo_next_change()
1✔
96
        # Make sure that this works with filters.
97
        f3 = ContainsSpecieFilter(["O2-"], strict_compare=True, AND=False)
1✔
98
        ts.append_filter(f3)
1✔
99
        ts.undo_last_change()
1✔
100
        ts.redo_next_change()
1✔
101

102
    def test_as_dict(self):
1✔
103
        self.trans.set_parameter("author", "will")
1✔
104
        d = self.trans.as_dict()
1✔
105
        assert "last_modified" in d
1✔
106
        assert "history" in d
1✔
107
        assert "author" in d["other_parameters"]
1✔
108
        assert Structure.from_dict(d).formula == "Na4 Fe4 P4 O16"
1✔
109

110
    def test_snl(self):
1✔
111
        self.trans.set_parameter("author", "will")
1✔
112
        with warnings.catch_warnings(record=True) as w:
1✔
113
            warnings.simplefilter("always")
1✔
114
            snl = self.trans.to_snl([("will", "will@test.com")])
1✔
115
            assert len(w) == 1, "Warning not raised on type conversion with other_parameters"
1✔
116
        ts = TransformedStructure.from_snl(snl)
1✔
117
        assert ts.history[-1]["@class"] == "SubstitutionTransformation"
1✔
118

119
        h = ("testname", "testURL", {"test": "testing"})
1✔
120
        snl = StructureNL(ts.final_structure, [("will", "will@test.com")], history=[h])
1✔
121
        snl = TransformedStructure.from_snl(snl).to_snl([("notwill", "notwill@test.com")])
1✔
122
        assert snl.history == [h]
1✔
123
        assert snl.authors == [("notwill", "notwill@test.com")]
1✔
124

125

126
if __name__ == "__main__":
1✔
127
    unittest.main()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc