Skip to content

Commit

Permalink
assertRaises and MuAIRSS batch test #48
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-austin committed Apr 12, 2023
1 parent 25bef5e commit 632b05b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 15 deletions.
4 changes: 2 additions & 2 deletions pymuonsuite/test/test_castep.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def test_read(self):
reader = ReadWriteCastep()
# test that we do not get any result for trying to read
# an empty folder:
with self.assertRaises(IOError) as e:
with self.assertRaises(OSError) as e:
reader.read(folder, sname)
self.assertTrue("no such file or directory" in e)
self.assertIn("No such file or directory", str(e.exception))

folder = os.path.join(_TESTDATA_DIR, sname)
# tests castep file being read:
Expand Down
17 changes: 17 additions & 0 deletions pymuonsuite/test/test_data/Si2/batch/Si2.cell
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%block lattice_cart
2.6954645 2.6954645 0.0
2.6954645 0.0 2.6954645
0.0 2.6954645 2.6954645
%endblock lattice_cart

%block positions_frac
Si 0.00 0.00 0.00
Si 0.25 0.25 0.25
%endblock positions_frac

%block kpoints_list
0.25 0.25 0.25 0.25
0.5 0.5 0.25 0.75
%endblock kpoints_list

symmetry_generate
2 changes: 1 addition & 1 deletion pymuonsuite/test/test_data/Si2/script-uep
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
target="muon-airss-out-uep/uep/"
target=$1
for folder in "$target"*
do
pm-uep-opt $folder/$(basename $folder).yaml
Expand Down
2 changes: 1 addition & 1 deletion pymuonsuite/test/test_dftb.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_read(self):
# an empty folder:
with self.assertRaises(OSError) as e:
reader.read(folder)
self.assertTrue("no such file or directory" in e)
self.assertIn("No geo_end.gen file found in", str(e.exception))

folder = os.path.join(
folder,
Expand Down
62 changes: 59 additions & 3 deletions pymuonsuite/test/test_muairss.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,57 @@ class TestMuairss(unittest.TestCase):
def setUp(self):
_clean_testdata_dir()

def testBatchUEP(self):
try:
yaml_file = os.path.join(_TESTDATA_DIR, "Si2-muairss-uep.yaml")
cell_file = os.path.join(_TESTDATA_DIR, "batch")
input_params = load_input_file(yaml_file, MuAirssSchema)

# Run Muairss write:
sys.argv[1:] = ["-tw", cell_file, yaml_file]
os.chdir(_TESTDATA_DIR)
run_muairss()
# Check all folders contain a yaml file
for (rootDir, subDirs, files) in os.walk("muon-airss-out-uep/Si2/uep/"):
for s in subDirs:
expected_file = os.path.join(
"muon-airss-out-uep/Si2/uep/" + s, s + ".yaml"
)
self.assertTrue(os.path.exists(expected_file))
params = load_input_file(expected_file, UEPOptSchema)
self.assertEqual(params["geom_steps"], input_params["geom_steps"])
self.assertEqual(params["opt_tol"], input_params["geom_force_tol"])
self.assertEqual(params["gw_factor"], input_params["uep_gw_factor"])

# Run UEP
if platform.system() == "Windows":
script_path = os.path.join(_TESTDATA_DIR, "script-uep-windows.ps1")
subprocess.run(["powershell", "-File", os.path.normpath(script_path)])
else:
subprocess.run(
[
os.path.join(_TESTDATA_DIR, "script-uep"),
"muon-airss-out-uep/Si2/uep/",
]
)

# Check all folders contain UEP file
for (rootDir, subDirs, files) in os.walk("muon-airss-out-uep/Si2/uep/"):
for s in subDirs:
expected_file = os.path.join(
"muon-airss-out-uep/Si2/uep/" + s, s + ".uep"
)
self.assertTrue(os.path.exists(expected_file))

sys.argv[1:] = [cell_file, yaml_file]
run_muairss()

self.assertTrue(os.path.exists("Si2_clusters.txt"))
self.assertTrue(os.path.exists("Si2_Si2_uep_clusters.dat"))
finally:
# Remove all created files and folders
_clean_testdata_dir()

def testUEP(self):
try:
yaml_file = os.path.join(_TESTDATA_DIR, "Si2-muairss-uep.yaml")
Expand All @@ -85,9 +136,14 @@ def testUEP(self):
# Run UEP
if platform.system() == "Windows":
script_path = os.path.join(_TESTDATA_DIR, "script-uep-windows.ps1")
subprocess.call(["powershell", "-File", os.path.normpath(script_path)])
subprocess.run(["powershell", "-File", os.path.normpath(script_path)])
else:
subprocess.call(os.path.join(_TESTDATA_DIR, "script-uep"))
subprocess.run(
[
os.path.join(_TESTDATA_DIR, "script-uep"),
"muon-airss-out-uep/uep/",
]
)

# Check all folders contain UEP file
for (rootDir, subDirs, files) in os.walk("muon-airss-out-uep/uep/"):
Expand Down Expand Up @@ -225,7 +281,7 @@ def testDFTB(self):

# Run DFTB
if _RUN_DFTB:
subprocess.call(os.path.join(_TESTDATA_DIR, "script-dftb"))
subprocess.run(os.path.join(_TESTDATA_DIR, "script-dftb"))
else:
yaml_file = os.path.join(_TESTDATA_DIR, "Si2-muairss-dftb-read.yaml")

Expand Down
14 changes: 6 additions & 8 deletions pymuonsuite/test/test_uep.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def test_read(self):
reader = ReadWriteUEP()
# test that we do not get any result for trying to read
# an empty folder:
try:
with self.assertRaises(OSError) as e:
reader.read(folder, sname)
except Exception as e:
print(e)
self.assertIn("could not read UEP file in", str(e.exception))

folder = os.path.join(_TESTDATA_DIR, "Si2/uep-result")
# tests uep file being read, and compares structure to
# that in the xyz file - these should be equal
Expand Down Expand Up @@ -117,11 +117,9 @@ def test_write(self):
params["charged"] = False

reader = ReadWriteUEP(params=params)

reader.write(atoms, output_folder)

except Exception as e:
print(e)
with self.assertRaises(RuntimeError) as e:
reader.write(atoms, output_folder)
self.assertIn("Can't use UEP method for neutral system", str(e.exception))
finally:
shutil.rmtree("test_save")

Expand Down

0 comments on commit 632b05b

Please sign in to comment.