def test_extract(tmpdir): """Tests the extract method and cleanup method. """ target = str(tmpdir.join("Qe")) atm = Atoms("AlPd", positions=[[0, 0, 0], [0.5, 0.5, 0.5]]) kwargs = { "potcars": { "directory": path.join(reporoot, "tests/qe"), "potentials": { "Al": "Al.pbe-n-kjpaw_psl.1.0.0.UPF", "Pd": "Pd_ONCV_PBE-1.0.upf" }, "versions": { "Al": ["2.0.1", ".5.1"], "Pd": ["2.0.1", "2.1.1"] } }, "kpoints": { "method": "kspacing", "spacing": 0.1, "offset": 1 }, "input_data": { "control": { "calculation": "relax", "prefix": "test" } } } calc = Qe(atm, target, '.', 0, **kwargs) calc.create() symlink(path.join(calc.folder, "test.xml"), relpath("tests/qe/complete.xml")) mkdir(path.join(calc.folder, "test.save")) calc.extract(target) assert hasattr(calc.atoms, "qe_force") assert hasattr(calc.atoms, "qe_stress") assert hasattr(calc.atoms, "qe_energy") assert calc.atoms.qe_energy is not None assert calc.atoms.qe_stress is not None assert calc.atoms.qe_force is not None touch(path.join(calc.folder, "test.save", "paw.txt")) calc.cleanup(target, clean_level="light") assert not path.isfile(path.join(calc.folder, "test.save", "paw.txt")) touch(path.join(calc.folder, "test.save", "charge-density.dat")) calc.cleanup(target) assert not path.isfile( path.join(calc.folder, "test.save", "charge-density.dat")) calc.cleanup(target, clean_level="aggressive") assert not path.isfile(path.join(calc.folder, "test.xml")) assert not path.isdir(path.join(calc.folder, "test.save"))
def test_can_extract(tmpdir): """Tests the can_extract method. We'll also test is_executing at the same time. """ target = str(tmpdir.join("Qe")) atm = Atoms("AlPd", positions=[[0, 0, 0], [0.5, 0.5, 0.5]]) kwargs = { "potcars": { "directory": path.join(reporoot, "tests/qe"), "potentials": { "Al": "Al.pbe-n-kjpaw_psl.1.0.0.UPF", "Pd": "Pd_ONCV_PBE-1.0.upf" }, "versions": { "Al": ["2.0.1", ".5.1"], "Pd": ["2.0.1", "2.1.1"] } }, "kpoints": { "method": "kspacing", "spacing": 0.1, "offset": 1 }, "input_data": { "control": { "calculation": "relax", "prefix": "test" } } } calc = Qe(atm, target, '.', 0, **kwargs) calc.create() assert not calc.can_extract("def") assert not calc.can_extract(target) assert not calc.is_executing(target) touch(path.join(calc.folder, "CRASH")) assert not calc.can_extract(target) remove(path.join(calc.folder, "CRASH")) symlink(path.join(calc.folder, "pwscf.xml"), relpath("tests/qe/complete.xml")) mkdir(path.join(calc.folder, "pwscf.save")) assert calc.can_extract(target) remove(path.join(calc.folder, "test.xml")) mkdir(path.join(calc.folder, "pwscf.save")) symlink(path.join(calc.folder, "pwscf.xml"), relpath("tests/qe/fail.xml")) assert not calc.can_extract(target)
def test_prot_to_cfg(): """Tests the conversion of a prototype structure to a cfg file. """ from matdb.fitting.mtp import _prot_to_cfg from matdb.utility import _get_reporoot, touch, chdir from os import path, remove import tarfile template_root = path.join(_get_reporoot(), "matdb", "templates") if not path.isdir(path.join(template_root, "uniqueUnaries")): with chdir(template_root): tarf = "prototypes.tar.gz" tar = tarfile.open(tarf, "r:gz") tar.extractall() tar.close() source = path.join(template_root, "uniqueUnaries", "A10_POSCAR.orig") species = ["Al"] relax_file = "relax.cfg" type_map = {0:0} root = "." min_atoms = 1 max_atoms = None touch(relax_file) _prot_to_cfg(source, species, relax_file, type_map, root, min_atoms, max_atoms) assert path.isfile(relax_file) lc = 0 with open(relax_file, "r") as f: for line in f: lc += 1 assert lc == 12 source = path.join(template_root, "uniqueBinaries", "b31_POSCAR.orig") species = ["Al", "Cu"] relax_file = "relax.cfg" type_map = {0:0, 1:1} root = "." min_atoms = 1 max_atoms = 4 _prot_to_cfg(source, species, relax_file, type_map, root, min_atoms, max_atoms) lc = 0 with open(relax_file, "r") as f: for line in f: lc += 1 assert lc == 12 remove(relax_file)
def test_copyonce(): """Tests the copyonce method in utility. """ from matdb.utility import copyonce, touch from os import remove, path touch("temp1.txt") copyonce("temp1.txt","temp2.txt") assert path.isfile("temp2.txt") remove("temp1.txt") remove("temp2.txt")
def test_make_input_files(mtpdb): """Tests the creation of various input files. """ from os import path, mkdir from matdb.utility import touch, _get_reporoot, copyonce from matdb.atoms import Atoms mtpfit = mtpdb.trainers.fits['CoWV_mtp'].sequences['CoWV_mtp'].steps['mtp'] touch(path.join(mtpfit.root, "to-relax.cfg")) mtpfit._make_pot_initial() assert path.isfile(path.join(mtpfit.root, "pot.mtp")) with open(path.join(mtpfit.root, "pot.mtp"), "r") as f: for line in f: assert not "{{" in line and not "}}" in line mtpfit._make_relax_ini() assert path.isfile(path.join(mtpfit.root, "relax.ini")) with open(path.join(mtpfit.root, "relax.ini"), "r") as f: for line in f: assert not "{{" in line and not "}}" in line mtpfit._setup_to_relax_cfg() assert path.isfile(path.join(mtpfit.root, "to_relax.json")) with open(path.join(mtpfit.root, "to_relax.json")) as f: to_relax_dict = json.load(f) assert to_relax_dict["phenum_args"] == {"config":"t", "species":mtpfit.species, "structures": "all", "debug": False, "example": False, "displace":0.0, "mink":True, "outfile":path.join(mtpfit.root,"to-relax.cfg"), "verbose": None, "rattle":0.0, "mapping": None} assert to_relax_dict["crystals"] == mtpfit.crystals_to_relax assert to_relax_dict["species"] == mtpfit.species assert to_relax_dict["min_atoms"] == mtpfit.relax_min_atoms assert to_relax_dict["max_atoms"] == mtpfit.relax_max_atoms assert to_relax_dict["root"] == mtpfit.root mtpfit.species = ["Co", "Ni", "Ti", "W", "Li"] assert mtpfit._setup_to_relax_cfg() is None
def run(args): """Runs the matdb setup and cleanup to produce database files. """ if args is None: return cmd = args["cmd"] print(cmd) keyword = cmd[0].strip() print(keyword) if keyword == "calc-grade": touch("state.mvs") touch("temp1.cfg") elif keyword == "select-add": template_root = path.join(_get_reporoot(), "tests", "fitting", "files") touch("new_training.cfg") # src = path.join(template_root,"mtp_new_training.cfg") # dest = "new_training.cfg" # copyonce(src, dest) elif keyword == "convert-cfg": template_root = path.join(_get_reporoot(), "tests", "mtp", "training") for i in range(1, 11): src = path.join(template_root, "POSCAR{0}".format(i)) dest = "POSCAR{0}".format(i) copyonce(src, dest)
def test_extract(tmpdir): """Tests the extract method and cleanup method. """ from matdb.utility import symlink, relpath, touch from matdb.utility import _set_config_paths _set_config_paths("AgPd_Enumerated", str(tmpdir)) target = str(tmpdir.join("Vasp")) globals_setup(target) atm = Atoms("Si", positions=[[0, 0, 0]], cell=[1, 1, 1]) kwargs = { "ibrion": 5, "nsw": 1, "kpoints": { "method": "mueller", "mindistance": 50 }, "potcars": { "directory": "./tests/vasp", "xc": "pbe", "versions": { "Si": '05Jan2001' } } } calc = Vasp(atm, target, str(tmpdir), 0, **kwargs) calc.write_input(atm, target) symlink(path.join(calc.folder, "OUTCAR"), relpath("tests/files/VASP/OUTCAR_complete")) symlink(path.join(calc.folder, "CONTCAR"), path.join(calc.folder, "POSCAR")) calc.extract(target) assert hasattr(calc.atoms, calc.force_name) assert hasattr(calc.atoms, calc.virial_name) assert hasattr(calc.atoms, calc.energy_name) assert calc.atoms.vasp_energy is not None assert calc.atoms.vasp_virial is not None assert calc.atoms.vasp_force is not None touch(path.join(calc.folder, "CHG")) calc.cleanup(target, clean_level="light") assert not path.isfile(path.join(calc.folder, "CHG")) touch(path.join(calc.folder, "CHGCAR")) calc.cleanup(target) assert not path.isfile(path.join(calc.folder, "CHGCAR")) touch(path.join(calc.folder, "vasprun.xml")) calc.cleanup(target, clean_level="aggressive") assert not path.isfile(path.join(calc.folder, "OUTCAR")) assert not path.isfile(path.join(calc.folder, "vasprun.xml")) symlink(path.join(calc.folder, "CONTCAR"), path.join(calc.folder, "POSCAR")) symlink(path.join(calc.folder, "OUTCAR"), relpath("tests/files/VASP/OUTCAR_incomplete")) assert not calc.extract(target)
def test_compare_tree(tmpdir): """Tests the folder comparison method. """ from os import mkdir from matdb.utility import touch, compare_tree test_dir = str(tmpdir.join("comp_tree")) mkdir(test_dir) touch(path.join(test_dir,"compute.pkl")) touch(path.join(test_dir,"jobfile.sh")) mkdir(path.join(test_dir,"phonopy")) touch(path.join(test_dir,"phonopy","POSCAR")) folders = { "__files__": ["compute.pkl","jobfile.sh"], "phonopy": { "__files__": ["POSCAR"] }, } compare_tree(test_dir,folders)
def create_to_relax(setup_args): """Creates the to-relax.cfg file from the passed in args dictionary. Args: setup_args (dict): A dictionary containing the arguments needed to construct the potential. """ args = setup_args["phenum_args"] species = np.unique(setup_args["species"]) crystals = setup_args["crystals"] min_atoms = setup_args["min_atoms"] max_atoms = setup_args["max_atoms"] root = setup_args["root"] touch(args["outfile"]) for crystal in crystals: prot_map = { 0: "uniqueUnaries", 1: "uniqueBinaries", 2: "uniqueTernaries" } if crystal == "prototypes": # The prototypes are saved into the file prototypes.tar.gz, if # this is the first time prototypes has been run we need to unpack it. template_root = path.join(_get_reporoot(), "matdb", "templates") if not path.isdir(path.join(template_root, "uniqueUnaries")): with chdir(template_root): tarf = "prototypes.tar.gz" tar = tarfile.open(tarf, "r:gz") tar.extractall() tar.close() for size in range(len(species)): cand_path = path.join(template_root, prot_map[size]) structures = glob("{0}/*".format(cand_path)) perms = [list(i) for i in permutations(species, r=size + 1)] for fpath, perm in product(structures, perms): type_map = {} for i, s in enumerate(species): if s in perm: type_map[perm.index(s)] = i _prot_to_cfg(fpath, perm, args["outfile"], type_map, root, min_atoms, max_atoms) else: # eventually we'll want to replace this with an actual # enumeration over the options the user specifies. infile = path.join( _get_reporoot(), "matdb", "templates", "struct_enum.out_{0}_{1}".format(len(species), crystal)) expected_min_atoms = 1 if crystal == "hcp": min_atoms = 2 expected_min_atoms = 2 if min_atoms != expected_min_atoms or max_atoms is not None: with open(infile, "r") as f: min_num = None max_num = None last_num = 1 past_start = False for line in f: if line.split()[0] == "start": past_start = True continue if past_start: data = line.strip().split() lab = data[-2] last_num = int(data[0]) if len(lab) == min_atoms and min_num is None: min_num = int(data[0]) if len(lab) > max_atoms and max_num is None: max_num = int(data[0]) break # In case in the input file, there is no configuration # that has the number of atoms bigger than max_atoms, # the max_num will be set to the number in the last # line. if max_num is None: max_num = last_num args["structures"] = range(min_num, max_num) args["input"] = infile _make_structures(args)
#!/usr/bin/python """This is a stub for vasp to enable unit testing. It creates empty files that mimic the ones VASP actually creates when it executes. We don't include the OUTCAR because we want to be able to simulate VASP failing as well, if the OUTCAR is needed then we simply copy an existing OUTCAR from `tests/data`. We're copying the POSCAR to the CONTCAR so that the cleanup methods can grab the 'relaxed' atomic positions. """ from matdb.utility import touch, symlink from os import path files = ["CONTCAR", "WAVECAR", "CHGCAR"] for fname in files: touch(fname) if path.isfile("POSCAR"): symlink("CONTCAR", "POSCAR") print("vasp.4.6.35 3Apr08 complex \n")
def test_command_functions(mtpdb): """All the commands are iterative, so we'll have to test them one after the other. """ #first build the database directory and the initial database of #structures. from matdb.utility import copyonce, _get_reporoot, touch from os import path, mkdir, remove, rename from matdb.atoms import Atoms seed_root = path.join(mtpdb.root, "seed") mkdir(seed_root) templates = path.join(_get_reporoot(), "tests", "mtp", "training") for i in range(1,11): target = path.join(seed_root, "vasp.{0}".format(i)) source = path.join(templates, "POSCAR{0}".format(i)) copyonce(source, target) mtpfit = mtpdb.trainers.fits['CoWV_mtp'].sequences['CoWV_mtp'].steps['mtp'] cntrl_root = mtpdb.root db_root = path.join(cntrl_root, "Manual", "test.manual") mtpdb.setup() files = ["OUTCAR{}", "CONTCAR{}"] for i in range(1,11): target = path.join(db_root, "vasp.{0}".format(i), "S1.1") for f in files: copyonce(path.join(templates, f.format(i)), path.join(target, f.format(''))) mtpdb.extract() touch(path.join(mtpfit.root, "new_training.cfg")) touch(path.join(mtpfit.root, "relaxed.cfg")) # With the initial database setup we can now run the mtp commands. # First test the initial training setup script cmd_template = mtpfit.command() target = path.join(mtpfit.root, "status.txt") bc = path.join(mtpfit.root, "status.txt_train") copyonce(target,bc) assert path.isfile(target) with open(target, "r") as f: line = f.read() assert line.strip() == "relax_setup 1 0" assert path.isfile(path.join(mtpfit.root, "relax.ini")) assert path.isfile(path.join(mtpfit.root, "pot.mtp")) assert cmd_template == mtpfit._train_template() # Now we setup for the test of the relaxation_setup copyonce(path.join(mtpfit.root, "pot.mtp"), path.join(mtpfit.root, "Trained.mtp_")) cmd_template = mtpfit.command() assert "matdb_mtp_to_relax.py" in cmd_template with open(target, "r") as f: line = f.read() assert line.strip() == "relax 1 0" rename(bc, target) copyonce(target,bc) copyonce(path.join(mtpfit.root, "pot.mtp"), path.join(mtpfit.root, "Trained.mtp_")) touch(path.join(mtpfit.root, "unrelaxed.cfg")) mtpfit.use_unrelaxed = True cmd_template = mtpfit.command() assert cmd_template == mtpfit._relax_template() with open(target, "r") as f: line = f.read() assert line.strip() == "select 1 0" mtpfit.use_unrelaxed = False rename(bc, target) copyonce(path.join(mtpfit.root, "pot.mtp"), path.join(mtpfit.root, "Trained.mtp_")) touch(path.join(mtpfit.root, "candidate.cfg")) cmd_template = mtpfit.command() assert cmd_template == mtpfit._relax_template() with open(target, "r") as f: line = f.read() assert line.strip() == "select 1 0" # Now we can test the select step for i in range(72): touch(path.join(mtpfit.root, "candidate.cfg_{0}".format(i))) cmd_template = mtpfit.command() assert path.isfile(path.join(mtpfit.root, "candidate.cfg")) with open(target, "r") as f: line = f.read() assert line.strip() == "add 1 0"