Skip to content

Commit db45f4b

Browse files
wladererjanosh
andauthored
Enforce zval to be an integer to avoid improper syntax in .cri file … (#3502)
* Enforce zval to be an integer to avoid improper syntax in .cri file #3501 * test with chgcar and zpsp to ensure zval is formatted as int --------- Co-authored-by: Janosh Riebesell <[email protected]>
1 parent e6e818d commit db45f4b

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

pymatgen/command_line/critic2_caller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def from_chgcar(
195195
if chgcar:
196196
input_script += ["load int.CHGCAR id chg_int", "integrable chg_int"]
197197
if zpsp:
198-
zpsp_str = f" zpsp {' '.join(f'{symbol} {zval}' for symbol, zval in zpsp.items())}"
198+
zpsp_str = f" zpsp {' '.join(f'{symbol} {int(zval)}' for symbol, zval in zpsp.items())}"
199199
input_script[-2] += zpsp_str
200200

201201
# Command to run automatic analysis

tests/command_line/test_critic2_caller.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import os
43
import unittest
54
from shutil import which
65

@@ -21,10 +20,8 @@
2120
@unittest.skipIf(not which("critic2"), "critic2 executable not present")
2221
class TestCritic2Caller(unittest.TestCase):
2322
def test_from_path(self):
24-
# uses chgcars
25-
test_dir = f"{TEST_FILES_DIR}/bader"
26-
27-
c2c = Critic2Caller.from_path(test_dir)
23+
# uses CHGCARs
24+
c2c = Critic2Caller.from_path(f"{TEST_FILES_DIR}/bader")
2825

2926
# check we have some results!
3027
assert len(c2c._stdout) >= 500
@@ -51,36 +48,39 @@ def test_from_path(self):
5148

5249
# alternatively, can also set when we do the analysis, but note that this will change
5350
# the analysis performed since augmentation charges are added in core regions
54-
c2c = Critic2Caller.from_path(test_dir, zpsp={"Fe": 8.0, "O": 6.0})
51+
c2c = Critic2Caller.from_path(f"{TEST_FILES_DIR}/bader", zpsp={"Fe": 8.0, "O": 6.0})
5552

5653
# check yt integration
5754
assert c2o.structure.site_properties["bader_volume"][0] == approx(66.0148355)
5855
assert c2o.structure.site_properties["bader_charge"][0] == approx(12.2229131)
5956
assert c2o.structure.site_properties["bader_charge_transfer"][0] == approx(4.2229131)
6057

6158
def test_from_structure(self):
62-
# uses promolecular density
63-
structure = Structure.from_file(
64-
os.path.join(
65-
TEST_FILES_DIR,
66-
"critic2/MoS2.cif",
67-
)
68-
)
59+
# uses pro-molecular density
60+
structure = Structure.from_file(f"{TEST_FILES_DIR}/critic2/MoS2.cif")
6961

7062
c2c = Critic2Caller.from_chgcar(structure)
7163

7264
# check we have some results!
7365
assert len(c2c._stdout) >= 500
7466

67+
# test with chgcar and zpsp to ensure zval is formatted as int
68+
# https://github.com/materialsproject/pymatgen/issues/3501
69+
c2c = Critic2Caller.from_chgcar(
70+
structure, zpsp={"Mo": 6.0, "S": 6.0}, chgcar=f"{TEST_FILES_DIR}/bader/CHGCAR.gz"
71+
)
72+
73+
assert "ERROR : load int.CHGCAR id chg_int zpsp Mo 6 S 6" in c2c._input_script
74+
7575

7676
class TestCritic2Analysis(unittest.TestCase):
7777
def setUp(self):
7878
stdout_file = f"{TEST_FILES_DIR}/critic2/MoS2_critic2_stdout.txt"
7979
stdout_file_new_format = f"{TEST_FILES_DIR}/critic2/MoS2_critic2_stdout_new_format.txt"
80-
with open(stdout_file) as f:
81-
reference_stdout = f.read()
82-
with open(stdout_file_new_format) as f:
83-
reference_stdout_new_format = f.read()
80+
with open(stdout_file) as file:
81+
reference_stdout = file.read()
82+
with open(stdout_file_new_format) as file:
83+
reference_stdout_new_format = file.read()
8484

8585
structure = Structure.from_file(f"{TEST_FILES_DIR}/critic2/MoS2.cif")
8686

0 commit comments

Comments
 (0)