From 632b05bf341caf5d830aef71631272bf981b2e4d Mon Sep 17 00:00:00 2001 From: Patrick Austin Date: Wed, 12 Apr 2023 16:17:38 +0000 Subject: [PATCH] assertRaises and MuAIRSS batch test #48 --- pymuonsuite/test/test_castep.py | 4 +- pymuonsuite/test/test_data/Si2/batch/Si2.cell | 17 +++++ pymuonsuite/test/test_data/Si2/script-uep | 2 +- pymuonsuite/test/test_dftb.py | 2 +- pymuonsuite/test/test_muairss.py | 62 ++++++++++++++++++- pymuonsuite/test/test_uep.py | 14 ++--- 6 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 pymuonsuite/test/test_data/Si2/batch/Si2.cell diff --git a/pymuonsuite/test/test_castep.py b/pymuonsuite/test/test_castep.py index ba5f78ff..1edd56b5 100644 --- a/pymuonsuite/test/test_castep.py +++ b/pymuonsuite/test/test_castep.py @@ -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: diff --git a/pymuonsuite/test/test_data/Si2/batch/Si2.cell b/pymuonsuite/test/test_data/Si2/batch/Si2.cell new file mode 100644 index 00000000..f37c3e62 --- /dev/null +++ b/pymuonsuite/test/test_data/Si2/batch/Si2.cell @@ -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 diff --git a/pymuonsuite/test/test_data/Si2/script-uep b/pymuonsuite/test/test_data/Si2/script-uep index 4ce7cc71..e0e73b29 100755 --- a/pymuonsuite/test/test_data/Si2/script-uep +++ b/pymuonsuite/test/test_data/Si2/script-uep @@ -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 diff --git a/pymuonsuite/test/test_dftb.py b/pymuonsuite/test/test_dftb.py index cfe48f42..d3c2eb20 100644 --- a/pymuonsuite/test/test_dftb.py +++ b/pymuonsuite/test/test_dftb.py @@ -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, diff --git a/pymuonsuite/test/test_muairss.py b/pymuonsuite/test/test_muairss.py index 1daf1dcf..91c8413c 100644 --- a/pymuonsuite/test/test_muairss.py +++ b/pymuonsuite/test/test_muairss.py @@ -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") @@ -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/"): @@ -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") diff --git a/pymuonsuite/test/test_uep.py b/pymuonsuite/test/test_uep.py index 8b977e45..695b0256 100644 --- a/pymuonsuite/test/test_uep.py +++ b/pymuonsuite/test/test_uep.py @@ -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 @@ -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")