Skip to content

Commit

Permalink
avoid module level variable
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Sep 30, 2024
1 parent 2ad48e6 commit 73ddbd6
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions tests/io/cp2k/test_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,6 @@

TEST_DIR = f"{TEST_FILES_DIR}/io/cp2k"

si_struct = Structure(
lattice=[[0, 2.734364, 2.734364], [2.734364, 0, 2.734364], [2.734364, 2.734364, 0]],
species=["Si", "Si"],
coords=[[0, 0, 0], [0.25, 0.25, 0.25]],
)

nonsense_struct = Structure(
lattice=[[-1.0, -10.0, -100.0], [0.1, 0.01, 0.001], [7.0, 11.0, 21.0]],
species=["H"],
coords=[[-1, -1, -1]],
)

ch_mol = Molecule(species=["C", "H"], coords=[[0, 0, 0], [1, 1, 1]])

BASIS_FILE_STR = """
H SZV-MOLOPT-GTH SZV-MOLOPT-GTH-q1
Expand All @@ -52,26 +39,22 @@
0.066918004004 0.037148121400
0.021708243634 -0.001125195500
"""
ALL_HYDROGEN_STR = """


class TestBasisAndPotential(PymatgenTest):
all_hydrogen_str = """
H ALLELECTRON ALL
1 0 0
0.20000000 0
0.20000000 0
"""
POT_HYDROGEN_STR = """

pot_hydrogen_str = """
H GTH-PBE-q1 GTH-PBE
1
0.20000000 2 -4.17890044 0.72446331
0.20000000 2 -4.17890044 0.72446331
0
"""
CP2K_INPUT_STR = """
&GLOBAL
RUN_TYPE ENERGY
PROJECT_NAME CP2K ! default name
&END
"""


class TestBasisAndPotential(PymatgenTest):
def test_basis_info(self):
# Ensure basis metadata can be read from string
basis_info = BasisInfo.from_str("cc-pc-DZVP-MOLOPT-q1-SCAN")
Expand Down Expand Up @@ -135,20 +118,20 @@ def test_basis(self):

def test_potentials(self):
# Ensure cp2k formatted string can be read for data correctly
h_all_elec = GthPotential.from_str(ALL_HYDROGEN_STR)
h_all_elec = GthPotential.from_str(self.all_hydrogen_str)
assert h_all_elec.potential == "All Electron"
pot = GthPotential.from_str(POT_HYDROGEN_STR)
pot = GthPotential.from_str(self.pot_hydrogen_str)
assert pot.potential == "Pseudopotential"
assert pot.r_loc == approx(0.2)
assert pot.nexp_ppl == approx(2)
assert_allclose(pot.c_exp_ppl, [-4.17890044, 0.72446331])

# Basis file can read from strings
pot_file = PotentialFile.from_str(POT_HYDROGEN_STR)
pot_file = PotentialFile.from_str(self.pot_hydrogen_str)
assert pot_file.objects[0] == pot

pot_file_path = self.tmp_path / "potential-file"
pot_file_path.write_text(POT_HYDROGEN_STR)
pot_file_path.write_text(self.pot_hydrogen_str)
pot_from_file = PotentialFile.from_file(pot_file_path)
assert pot_file != pot_from_file # unequal because pot_from_file has filename != None

Expand All @@ -160,11 +143,32 @@ def test_potentials(self):


class TestCp2kInput(PymatgenTest):
si_struct = Structure(
lattice=[[0, 2.734364, 2.734364], [2.734364, 0, 2.734364], [2.734364, 2.734364, 0]],
species=["Si", "Si"],
coords=[[0, 0, 0], [0.25, 0.25, 0.25]],
)

invalid_struct = Structure(
lattice=[[-1.0, -10.0, -100.0], [0.1, 0.01, 0.001], [7.0, 11.0, 21.0]],
species=["H"],
coords=[[-1, -1, -1]],
)

ch_mol = Molecule(species=["C", "H"], coords=[[0, 0, 0], [1, 1, 1]])

cp2k_input_str = """
&GLOBAL
RUN_TYPE ENERGY
PROJECT_NAME CP2K ! default name
&END
"""

def setUp(self):
self.ci = Cp2kInput.from_file(f"{TEST_DIR}/cp2k.inp")

def test_basic_sections(self):
cp2k_input = Cp2kInput.from_str(CP2K_INPUT_STR)
cp2k_input = Cp2kInput.from_str(self.cp2k_input_str)
assert cp2k_input["GLOBAL"]["RUN_TYPE"] == Keyword("RUN_TYPE", "energy")
assert cp2k_input["GLOBAL"]["PROJECT_NAME"].description == "default name"
self.assert_msonable(cp2k_input)
Expand All @@ -190,13 +194,13 @@ def test_basic_keywords(self):
assert "[Ha]" in kwd.get_str()

def test_coords(self):
for struct in [nonsense_struct, si_struct, ch_mol]:
for struct in (self.invalid_struct, self.si_struct, self.ch_mol):
coords = Coord(struct)
for val in coords.keywords.values():
assert isinstance(val, Keyword | KeywordList)

def test_kind(self):
for struct in [nonsense_struct, si_struct, ch_mol]:
for struct in (self.invalid_struct, self.si_struct, self.ch_mol):
for spec in struct.species:
assert spec == Kind(spec).specie

Expand Down Expand Up @@ -246,7 +250,7 @@ def test_preprocessor(self):
assert self.ci["FORCE_EVAL"]["DFT"]["SCF"]["MAX_SCF"] == Keyword("MAX_SCF", 1)

def test_mongo(self):
cp2k_input = Cp2kInput.from_str(CP2K_INPUT_STR)
cp2k_input = Cp2kInput.from_str(self.cp2k_input_str)
cp2k_input.inc({"GLOBAL": {"TEST": 1}})
assert cp2k_input["global"]["test"] == Keyword("TEST", 1)

Expand Down

0 comments on commit 73ddbd6

Please sign in to comment.