def test_batch_relax(self): """ Batch relax structures from file to file. """ shutil.copy(REAL_PATH + "data/structures/LiC.res", "_LiC.res") shutil.copy(REAL_PATH + "data/LiC_tests/LiC.cell", ".") shutil.copy(REAL_PATH + "data/LiC_tests/LiC.param", ".") shutil.copy(REAL_PATH + "data/pspots/Li_00PBE.usp", ".") shutil.copy(REAL_PATH + "data/pspots/C_00PBE.usp", ".") runner = BatchRun( seed=["LiC"], debug=False, no_reopt=True, verbosity=VERBOSITY, ncores=NCORES, executable=EXECUTABLE, ) runner.spawn(join=False) completed_exists = isfile("completed/_LiC.res") cruft = glob.glob("_LiC*") cruft_doesnt_exist = bool(len(cruft)) num = reset_job_folder() self.assertEqual(num, 0) self.assertTrue(completed_exists, "couldn't find output file!") self.assertFalse(cruft_doesnt_exist, "found some cruft {}".format(cruft))
def test_convergence_runner(self): """ Check that convergence tests run to completion. """ shutil.copy(REAL_PATH + "data/structures/Li.res", "_LiAs_testcase.res") shutil.copy(REAL_PATH + "data/LiAs_tests/LiAs_scf.cell", ".") shutil.copy(REAL_PATH + "data/LiAs_tests/LiAs_scf.param", ".") shutil.copy(REAL_PATH + "data/pspots/Li_00PBE.usp", ".") shutil.copy(REAL_PATH + "data/pspots/As_00PBE.usp", ".") with open("kpt.conv", "w") as f: f.write("0.08\n") f.write("0.07") with open("cutoff.conv", "w") as f: f.write("300\n") f.write("400") runner = BatchRun( seed=["LiAs_scf"], debug=False, conv_cutoff=True, conv_kpt=True, verbosity=VERBOSITY, ncores=NCORES, nprocesses=2, executable=EXECUTABLE, ) runner.spawn(join=False) dirs_exist = [ isdir(_dir) for _dir in ["completed_kpts", "completed_cutoff"] ] bad_castep_exist = isdir("bad_castep") files_that_should_not_exist = [ "_LiAs_testcase_300eV.res", "_LiAs_testcase_400eV.res", "_LiAs_testcase_0.08A.res", "_LiAs_testcase_0.07A.res", ] do_bad_files_exist = [ isfile(_file) for _file in files_that_should_not_exist ] results = [ "completed_cutoff/_LiAs_testcase_{}eV.castep".format(cutoff) for cutoff in [300, 400] ] results += [ "completed_kpts/_LiAs_testcase_{}A.castep".format(kpts) for kpts in [0.08, 0.07] ] files_exist = [isfile(_file) for _file in results] self.assertTrue(all(dirs_exist)) self.assertFalse(bad_castep_exist) self.assertTrue(all(files_exist)) self.assertFalse(any(do_bad_files_exist))
def test_generic_batch_threaded(self): """ Run a calculation in generic mode, check that res files are cycled over and not repeated by multiple threads. Multiple threads competing for the same job. """ executable = "echo" files = glob.glob(REAL_PATH + "/data/structures/*.res")[0:2] for _file in files: shutil.copy(_file, ".") runner = BatchRun( seed="*.res", debug=False, mode="generic", verbosity=4, ncores=1, nprocesses=2, executable=executable, ) runner.spawn(join=True) completed_files_exist = isfile("completed/" + _file.split("/")[-1]) txt_files_exist = all( [isfile(_file) for _file in ["jobs.txt", "finished_cleanly.txt"]] ) dirs = ["completed", "input", "logs"] dirs_exist = all([isdir(_dir) for _dir in dirs]) logs = glob.glob("logs/*.log") num_logs = len(logs) log_lines = [] for log in logs: with open(log, "r") as f: log_lines.append(len(f.readlines())) with open("jobs.txt", "r") as f: jobs_len = len(f.readlines()) self.assertEqual(num_logs, len(files), msg="Not enough log files!") for f in files: self.assertTrue(os.path.isfile("input/{}".format(f.split("/")[-1]))) self.assertTrue( all(lines > 5 for lines in log_lines), msg="Log files were too short!" ) self.assertEqual(jobs_len, len(files)) self.assertTrue(dirs_exist) self.assertTrue(completed_files_exist) self.assertTrue(txt_files_exist)
def test_batch_nothing_todo(self): """ Check that nothing is done when there's nothing to do... """ for file in glob.glob(REAL_PATH + "data/nothing_to_do/*.*"): shutil.copy(file, ".") runner = BatchRun( seed=["LiAs"], debug=False, no_reopt=True, verbosity=VERBOSITY, ncores=2, nprocesses=2, executable=EXECUTABLE, ) start = time.time() runner.spawn() elapsed = time.time() - start self.assertTrue(elapsed < 10, "Sluggish to quit!")
def test_failed_compute_dir_scf(self): """ Check that using a garbage path for compute dir causes a safe crash. """ shutil.copy(REAL_PATH + "data/structures/Li.res", "_Li.res") shutil.copy(REAL_PATH + "data/structures/LiC.res", "_LiC.res") shutil.copy(REAL_PATH + "data/pspots/Li_00PBE.usp", ".") shutil.copy(REAL_PATH + "data/pspots/C_00PBE.usp", ".") shutil.copy(REAL_PATH + "data/fail_scf/LiC_scf.cell", ".") shutil.copy(REAL_PATH + "data/fail_scf/LiC_scf.param", ".") runner = BatchRun( seed=["LiC_scf"], debug=False, no_reopt=True, scratch_prefix="/this/drive/doesnt/exist", verbosity=VERBOSITY, ncores=NCORES, executable=EXECUTABLE, ) with self.assertRaises(CriticalError): runner.spawn()
def test_batch_failed_scf(self): """ Check that SCF failures don't kill everything... """ shutil.copy(REAL_PATH + "data/structures/Li.res", "_Li.res") shutil.copy(REAL_PATH + "data/structures/LiC.res", "_LiC.res") shutil.copy(REAL_PATH + "data/pspots/Li_00PBE.usp", ".") shutil.copy(REAL_PATH + "data/pspots/C_00PBE.usp", ".") shutil.copy(REAL_PATH + "data/fail_scf/LiC_scf.cell", ".") shutil.copy(REAL_PATH + "data/fail_scf/LiC_scf.param", ".") runner = BatchRun( seed=["LiC_scf"], debug=False, no_reopt=True, verbosity=VERBOSITY, ncores=NCORES, executable=EXECUTABLE, ) runner.spawn() completed_folder_exists = isdir("completed") bad_castep_folder_exists = isdir("bad_castep") seeds = ["_Li.res", "_LiC.res"] output_files_exist = all( [isfile("bad_castep/{}".format(seed)) for seed in seeds]) cruft = glob.glob("_Li*") cruft_doesnt_exist = bool(len(cruft)) compute_dir_exist = isdir(HOSTNAME) num = reset_job_folder() self.assertEqual(num, 0) self.assertFalse(completed_folder_exists, "couldn't find output file!") self.assertTrue(bad_castep_folder_exists, "couldn't find bad_castep") self.assertTrue(output_files_exist, "couldn't find both outputs") self.assertFalse(cruft_doesnt_exist, "found some cruft {}".format(cruft)) self.assertFalse(compute_dir_exist, "Compute dir not cleaned up!")
def test_missing_basics(self): """" Check that run3 falls over when e.g. xc_functional is missing. """ for file in glob.glob(REAL_PATH + "data/misisng_basics/*.*"): shutil.copy(file, ".") tests = ["missing_cutoff", "missing_kpts", "missing_xc", "missing_pspot"] errors = [False for test in tests] for ind, test in enumerate(tests): try: runner = BatchRun( seed=["LiC_" + test], debug=False, no_reopt=True, verbosity=VERBOSITY, executable=EXECUTABLE, ) runner.spawn() except InputError: errors[ind] = True self.assertTrue(all(errors))
def test_res_name_collision(self): """ Check that run3 safely falls over if there is a file called <seed>.res. """ shutil.copy(REAL_PATH + "data/file_collision/LiAs.cell", ".") shutil.copy(REAL_PATH + "data/file_collision/LiAs.res", ".") shutil.copy(REAL_PATH + "data/file_collision/LiAs.param", ".") failed_safely = False try: runner = BatchRun( seed=["LiAs"], debug=False, no_reopt=True, verbosity=VERBOSITY, ncores=2, nprocesses=2, executable=EXECUTABLE, ) runner.spawn() except InputError: failed_safely = True self.assertTrue(failed_safely)
def test_batch_max_walltime_threaded(self): """ Check that WallTimeErrors do kill everything... """ shutil.copy(REAL_PATH + "data/structures/LiAs_testcase.res", ".") shutil.copy(REAL_PATH + "data/structures/LiAs_testcase_bad.res", ".") shutil.copy(REAL_PATH + "data/pspots/Li_00PBE.usp", ".") shutil.copy(REAL_PATH + "data/pspots/As_00PBE.usp", ".") shutil.copy(REAL_PATH + "data/max_walltime/LiAs.cell", ".") shutil.copy(REAL_PATH + "data/max_walltime/LiAs.param", ".") runner = BatchRun( seed=["LiAs"], debug=False, no_reopt=True, verbosity=VERBOSITY, ncores=2, nprocesses=2, executable=EXECUTABLE, max_walltime=15, polltime=10, ) with self.assertRaises(WalltimeError): runner.spawn() castep_exists = isfile("LiAs_testcase.castep") bad_castep_exists = isfile("LiAs_testcase_bad.castep") res_exists = isfile("LiAs_testcase.res") and isfile( "LiAs_testcase_bad.res") lock_exists = isfile("LiAs_testcase.res.lock") or isfile( "LiAs_testcase_bad.res.lock") compute_dir_exist = isdir(HOSTNAME) self.assertTrue(castep_exists, "Could not find castep file!") self.assertTrue(bad_castep_exists, "Could not find bad castep file!") self.assertTrue(res_exists, "Could not find res file!") self.assertFalse(compute_dir_exist, "Compute dir not cleaned up!") self.assertFalse(lock_exists, "Lock file was not deleted!")
def test_batch_queues(self): """ Test the scraping of queuing environments. """ shutil.copy(REAL_PATH + "data/structures/LiC.res", "_LiC.res") shutil.copy(REAL_PATH + "data/LiC_tests/LiC.cell", ".") shutil.copy(REAL_PATH + "data/LiC_tests/LiC.param", ".") shutil.copy(REAL_PATH + "data/pspots/Li_00PBE.usp", ".") shutil.copy(REAL_PATH + "data/pspots/C_00PBE.usp", ".") slurm_env = { "SLURM_NTASKS": "120", "SLURM_JOB_ID": "123456", "SLURM_ARRAY_TASK_ID": "123123", "SLURM_MEM_PER_CPU": "1024", "SLURM_RANDOM_STRING": "hello", "blah": "abc", } pbs_env = { "PBS_TASKNUM": "120", "PBS_JOB_ID": "999", "PBS_ARRAYID": "123123" } old_env = copy.deepcopy(os.environ) os.environ.update(slurm_env) runner = BatchRun( seed=["LiC"], debug=False, ncores=NCORES, verbosity=VERBOSITY, executable=EXECUTABLE, ) self.assertEqual(runner.args["ncores"], NCORES) self.assertEqual(type(runner.queue_mgr), SlurmQueueManager) self.assertEqual(runner.maxmem, 1024 * 120) self.assertEqual(runner.queue_mgr.max_memory, 1024 * 120) self.assertEqual(runner.queue_mgr.array_id, 123123) self.assertEqual(runner.queue_mgr.env["SLURM_RANDOM_STRING"], "hello") self.assertTrue("blah" not in runner.queue_mgr.env) runner = BatchRun( seed=["LiC"], debug=False, ncores=None, verbosity=VERBOSITY, executable=EXECUTABLE, ) self.assertEqual(runner.args["ncores"], 120) self.assertEqual(type(runner.queue_mgr), SlurmQueueManager) self.assertEqual(runner.maxmem, 1024 * 120) self.assertEqual(runner.queue_mgr.max_memory, 1024 * 120) self.assertEqual(runner.queue_mgr.array_id, 123123) os.environ = copy.deepcopy(old_env) os.environ.update(pbs_env) runner = BatchRun( seed=["LiC"], debug=False, ncores=None, verbosity=VERBOSITY, executable=EXECUTABLE, ) print(runner.queue_mgr) self.assertEqual(runner.args["ncores"], 120) self.assertEqual(type(runner.queue_mgr), PBSQueueManager) self.assertEqual(runner.maxmem, None) self.assertEqual(runner.queue_mgr.max_memory, None) self.assertEqual(runner.queue_mgr.array_id, 123123) os.environ.update(slurm_env) with self.assertRaises(RuntimeError): runner = BatchRun( seed=["LiC"], debug=False, ncores=None, verbosity=VERBOSITY, executable=EXECUTABLE, ) os.environ = copy.deepcopy(old_env)