• 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

70.15
/pymatgen/command_line/tests/test_critic2_caller.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 os
1✔
7
import unittest
1✔
8
from shutil import which
1✔
9

10
from pytest import approx
1✔
11

12
from pymatgen.command_line.critic2_caller import Critic2Analysis, Critic2Caller
1✔
13
from pymatgen.core.structure import Structure
1✔
14
from pymatgen.util.testing import PymatgenTest
1✔
15

16
__author__ = "Matthew Horton"
1✔
17
__version__ = "0.1"
1✔
18
__maintainer__ = "Matthew Horton"
1✔
19
__email__ = "mkhorton@lbl.gov"
1✔
20
__status__ = "Production"
1✔
21
__date__ = "July 2017"
1✔
22

23

24
@unittest.skipIf(not which("critic2"), "critic2 executable not present")
1✔
25
class Critic2CallerTest(unittest.TestCase):
1✔
26
    def test_from_path(self):
1✔
27
        # uses chgcars
28
        test_dir = os.path.join(PymatgenTest.TEST_FILES_DIR, "bader")
×
29

30
        c2c = Critic2Caller.from_path(test_dir)
×
31

32
        # check we have some results!
33
        assert len(c2c._stdout) >= 500
×
34

35
        c2o = c2c.output
×
36

37
        # check we get our structure graph
38
        sg = c2o.structure_graph(include_critical_points=None)
×
39
        assert sg.get_coordination_of_site(0) == 4
×
40

41
        # check yt integration
42
        assert c2o.structure.site_properties["bader_volume"][0] == approx(66.0148355)
×
43
        assert c2o.structure.site_properties["bader_charge"][0] == approx(12.2229131)
×
44

45
        # test zpsp functionality
46
        # this is normally picked up from POTCARs, but since POTCARs not checked in with the
47
        # test suite, setting manually here
48
        c2o_dict = c2o.as_dict()
×
49
        c2o_dict["zpsp"] = {"Fe": 8.0, "O": 6.0}
×
50
        c2o = Critic2Analysis.from_dict(c2o_dict)
×
51
        # note: these values don't seem sensible physically, but seem to be correct with
52
        # respect to the input files (possibly bad/underconverged source data)
53
        assert c2o.structure.site_properties["bader_charge_transfer"][0] == approx(4.2229131)
×
54

55
        # alternatively, can also set when we do the analysis, but note that this will change
56
        # the analysis performed since augmentation charges are added in core regions
57
        c2c = Critic2Caller.from_path(test_dir, zpsp={"Fe": 8.0, "O": 6.0})
×
58

59
        # check yt integration
60
        assert c2o.structure.site_properties["bader_volume"][0] == approx(66.0148355)
×
61
        assert c2o.structure.site_properties["bader_charge"][0] == approx(12.2229131)
×
62
        assert c2o.structure.site_properties["bader_charge_transfer"][0] == approx(4.2229131)
×
63

64
    def test_from_structure(self):
1✔
65
        # uses promolecular density
66
        structure = Structure.from_file(
×
67
            os.path.join(
68
                PymatgenTest.TEST_FILES_DIR,
69
                "critic2/MoS2.cif",
70
            )
71
        )
72

73
        c2c = Critic2Caller.from_chgcar(structure)
×
74

75
        # check we have some results!
76
        assert len(c2c._stdout) >= 500
×
77

78

79
class Critic2AnalysisTest(unittest.TestCase):
1✔
80
    _multiprocess_shared_ = True
1✔
81

82
    def setUp(self):
1✔
83
        stdout_file = os.path.join(PymatgenTest.TEST_FILES_DIR, "critic2/MoS2_critic2_stdout.txt")
1✔
84
        stdout_file_new_format = os.path.join(PymatgenTest.TEST_FILES_DIR, "critic2/MoS2_critic2_stdout_new_format.txt")
1✔
85
        with open(stdout_file) as f:
1✔
86
            reference_stdout = f.read()
1✔
87
        with open(stdout_file_new_format) as f:
1✔
88
            reference_stdout_new_format = f.read()
1✔
89

90
        structure = Structure.from_file(os.path.join(PymatgenTest.TEST_FILES_DIR, "critic2/MoS2.cif"))
1✔
91

92
        self.c2o = Critic2Analysis(structure, reference_stdout)
1✔
93
        self.c2o_new_format = Critic2Analysis(structure, reference_stdout_new_format)
1✔
94

95
    def test_properties_to_from_dict(self):
1✔
96
        assert len(self.c2o.critical_points) == 6
1✔
97
        assert len(self.c2o.nodes) == 14
1✔
98
        assert len(self.c2o.edges) == 10
1✔
99

100
        assert len(self.c2o_new_format.critical_points) == 6
1✔
101
        assert len(self.c2o_new_format.nodes) == 14
1✔
102
        assert len(self.c2o_new_format.edges) == 10
1✔
103

104
        # reference dictionary for c2o.critical_points[0].as_dict()
105
        # {'@class': 'CriticalPoint',
106
        #  '@module': 'pymatgen.command_line.critic2_caller',
107
        #  'coords': None,
108
        #  'field': 93848.0413,
109
        #  'field_gradient': 0.0,
110
        #  'field_hessian': [[-2593274446000.0, -3.873587547e-19, -1.704530713e-08],
111
        #                    [-3.873587547e-19, -2593274446000.0, 1.386877485e-18],
112
        #                    [-1.704530713e-08, 1.386877485e-18, -2593274446000.0]],
113
        #  'frac_coords': [0.333333, 0.666667, 0.213295],
114
        #  'index': 0,
115
        #  'multiplicity': 1.0,
116
        #  'point_group': 'D3h',
117
        #  'type': < CriticalPointType.nucleus: 'nucleus' >}
118

119
        assert str(self.c2o.critical_points[0].type) == "CriticalPointType.nucleus"
1✔
120

121
        # test connectivity
122
        assert self.c2o.edges[3] == {"from_idx": 1, "from_lvec": (0, 0, 0), "to_idx": 0, "to_lvec": (1, 0, 0)}
1✔
123
        # test as/from dict
124
        d = self.c2o.as_dict()
1✔
125
        assert set(d) == {
1✔
126
            "@module",
127
            "@class",
128
            "@version",
129
            "structure",
130
            "stdout",
131
            "stderr",
132
            "cpreport",
133
            "yt",
134
            "zpsp",
135
        }
136
        self.c2o.from_dict(d)
1✔
137

138
    def test_graph_output(self):
1✔
139
        sg = self.c2o.structure_graph()
1✔
140
        assert str(sg.structure[3].specie) == "Xbcp"
1✔
141
        assert set(list(sg.graph.edges(data=True))[0][2]) == {
1✔
142
            "to_jimage",
143
            "weight",
144
            "field",
145
            "laplacian",
146
            "ellipticity",
147
            "frac_coords",
148
        }
149

150

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