• 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.89
/pymatgen/electronic_structure/tests/test_core.py
1
# Copyright (c) Pymatgen Development Team.
2
# Distributed under the terms of the MIT License.
3

4

5
from __future__ import annotations
1✔
6

7
import unittest
1✔
8

9
import numpy as np
1✔
10
import pytest
1✔
11

12
from pymatgen.core import Lattice
1✔
13
from pymatgen.electronic_structure.core import Magmom, Orbital, Spin
1✔
14

15

16
class SpinTest(unittest.TestCase):
1✔
17
    def test_init(self):
1✔
18
        assert int(Spin.up) == 1
1✔
19
        assert int(Spin.down) == -1
1✔
20

21
    def test_from_int(self):
1✔
22
        assert Spin(1) == Spin.up
1✔
23
        assert Spin(-1) == Spin.down
1✔
24
        with pytest.raises(ValueError):
1✔
25
            Spin(0)
1✔
26

27
    def test_cached(self):
1✔
28
        assert id(Spin(1)) == id(Spin.up)
1✔
29

30

31
class OrbitalTest(unittest.TestCase):
1✔
32
    def test_init(self):
1✔
33
        for orb in Orbital:
1✔
34
            assert Orbital(orb.value) == orb
1✔
35
        with pytest.raises(ValueError):
1✔
36
            Orbital(100)
1✔
37

38
    def test_cached(self):
1✔
39
        assert id(Orbital(0)) == id(Orbital.s)
1✔
40

41

42
class MagmomTest(unittest.TestCase):
1✔
43
    def test_init(self):
1✔
44
        # backwards compatibility for scalar-like magmoms
45
        magmom = Magmom(2.0)
1✔
46
        assert float(magmom) == 2.0
1✔
47
        # backwards compatibility for list-like magmoms
48
        magmom2 = Magmom([1, 2, 3])
1✔
49
        assert list(magmom2) == [1, 2, 3]
1✔
50
        assert magmom2.global_moment.tolist() == [1, 2, 3]
1✔
51
        # non-default saxis, normalized internally
52
        magmom3 = Magmom([1, 2, 3], saxis=[1, 1, 1])
1✔
53
        assert np.allclose(magmom3.saxis, [np.sqrt(1 / 3.0)] * 3)
1✔
54
        # test construction from known global moment and desired, non-default saxis
55
        magmom4 = Magmom.from_global_moment_and_saxis([1, 2, 3], saxis=[1, 0, 0])
1✔
56
        assert np.allclose(magmom4.moment, [-3, 2, 1])
1✔
57
        # test global moments with non-default saxis
58
        magmom5 = Magmom([-3, 2, 1], saxis=[1, 0, 0])
1✔
59
        assert np.allclose(magmom5.global_moment, [1, 2, 3])
1✔
60

61
    def test_get_moments(self):
1✔
62
        # simple cases
63
        magmom_along_x = Magmom([1, 0, 0])
1✔
64
        assert np.allclose(magmom_along_x.get_moment(saxis=[1, 0, 0]), [0, 0, 1])
1✔
65

66
        magmom_along_y = Magmom([0, 1, 0])
1✔
67
        assert np.allclose(magmom_along_y.get_moment(saxis=[0, 1, 0]), [0, 0, 1])
1✔
68

69
        # test transformations
70
        magmoms = [
1✔
71
            [0, 0, 0],
72
            [0, 0, 1],
73
            [0, 0, -1],
74
            [1, 2, 3],
75
            [-1, 2, 3],
76
            [-1, -2, -3],
77
        ]
78

79
        for magmom in magmoms:
1✔
80
            magmom1 = Magmom(magmom)
1✔
81
            # transform to non-default saxis
82
            magmom2 = magmom1.get_00t_magmom_with_xyz_saxis()
1✔
83
            # and back to default saxis
84
            magmom3 = magmom2.get_xyz_magmom_with_001_saxis()
1✔
85
            assert np.allclose(magmom1.moment, magmom)
1✔
86
            assert np.allclose(magmom1.saxis, [0, 0, 1])
1✔
87
            assert np.allclose(magmom1.get_moment(saxis=magmom1.saxis), magmom1.moment)
1✔
88
            assert np.allclose(magmom1.get_moment(saxis=magmom2.saxis), magmom2.moment)
1✔
89
            assert np.allclose(magmom2.get_moment(saxis=[0, 0, 1]), magmom1.moment)
1✔
90
            assert np.allclose(magmom2.get_moment(saxis=magmom2.saxis), magmom2.moment)
1✔
91
            assert np.allclose(magmom3.moment, magmom1.moment)
1✔
92

93
    def test_is_collinear(self):
1✔
94
        magmoms_list = [
1✔
95
            [0, 0, 0],
96
            [1, 1, 1],
97
            [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
98
            [[0, 0, 1], [0, 0, 1], [0, 0, 1]],
99
            [[0, 0, -1], [0, 0, 1], [0, 0, 1]],
100
            [[2, 2, 2], [-2, -2, -2], [2, 2, 2]],
101
        ]
102
        for magmoms in magmoms_list:
1✔
103
            assert Magmom.are_collinear(magmoms) is True
1✔
104
        ncl_magmoms = [[[0, 0, 1], [0, 0, 1], [1, 2, 3]]]
1✔
105
        assert Magmom.are_collinear(ncl_magmoms) is False
1✔
106

107
    def test_have_consistent_saxis(self):
1✔
108
        magmom1 = Magmom([1, 2, 3])
1✔
109
        magmom2 = Magmom([1, 2, 3])
1✔
110
        magmom3 = Magmom([1, 2, 3], saxis=[0, 0, -1])
1✔
111
        magmom4 = Magmom([1, 2, 3], saxis=[1, 2, 3])
1✔
112
        assert Magmom.have_consistent_saxis([magmom1, magmom2])
1✔
113
        assert not Magmom.have_consistent_saxis([magmom1, magmom3])
1✔
114
        assert not Magmom.have_consistent_saxis([magmom1, magmom4])
1✔
115

116
    def test_get_consistent_set_and_saxis(self):
1✔
117
        magmoms = [1, 1, 2, 2, 0, 0, 2]
1✔
118
        magmoms, saxis = Magmom.get_consistent_set_and_saxis(magmoms)
1✔
119
        assert np.allclose(saxis, [0, 0, 1])
1✔
120

121
        magmoms = [[0, 0, 0], [1, 1, 1], [2, 2, 2]]
1✔
122
        magmoms, saxis = Magmom.get_consistent_set_and_saxis(magmoms)
1✔
123
        assert np.allclose(saxis, [np.sqrt(1 / 3.0)] * 3)
1✔
124

125
    def test_relative_to_crystal_axes(self):
1✔
126
        lattice = Lattice.from_parameters(5, 10, 5, 90, 110, 90)
1✔
127
        moment = [1, 0, 2]
1✔
128
        magmom = Magmom.from_moment_relative_to_crystal_axes(moment, lattice)
1✔
129
        assert np.allclose(magmom.moment, [0.93969262, 0.0, 1.65797986])
1✔
130
        assert np.allclose(magmom.get_moment_relative_to_crystal_axes(lattice), moment)
1✔
131

132
    def test_equality(self):
1✔
133
        assert Magmom([1, 1, 1]) == Magmom([1, 1, 1])
1✔
134
        assert not Magmom([1, 1, 2]) == Magmom([1, 1, 1])
1✔
135
        assert Magmom([0, 0, 10]) == 10
1✔
136

137
    def test_negative(self):
1✔
138
        assert -Magmom([1, 2, 3]) == Magmom([-1, -2, -3])
1✔
139

140

141
if __name__ == "__main__":
1✔
142
    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