def AgCu(tmpdir): """Test the functionality of the substitution group for a small seed configuration where the number of possible random combinations is limited and repition is more likely. Attributes: atoms_seed(matdb.atoms.Atoms): The seed atoms objech configuration for db generation. """ from matdb.utility import relpath from matdb.database import Controller from shutil import copy target = relpath("./tests/AgCu/matdb.yml") dbdir = str(tmpdir.join("agcu_db")) mkdir(dbdir) copyonce(target, path.join(dbdir, "matdb.yml")) target = path.join(dbdir, "matdb") # We need to copy the POSCAR from the testing directory to tempdir. POSCAR = relpath("./tests/AgCu/Ag1Cu5") mkdir(path.join(dbdir, "seed")) copy(POSCAR, path.join(dbdir, "seed", "Ag1Cu5")) if path.isfile("matdb.yml"): remove("matdb.yml") symlink("{}.yml".format(target), "matdb.yml") result = Controller(target, dbdir) return result
def test_ran_seed(tmpdir): """Tests that the random seed gets set by the controller properly. """ from matdb.utility import relpath from matdb.database import Controller target = relpath("./tests/general/ran_seed") dbdir = str(tmpdir.join("general")) mkdir(dbdir) cntrl = Controller(target, dbdir) splits = {"A": .5, "B": .75} root = str(tmpdir.join("legacy")) if not path.isdir(root): mkdir(root) folder = relpath("./tests/data/legacy") result = LegacyDatabase("AgPd-50", root, cntrl, splits, folder, "p-50-*.xyz", "ph", energy="dft_energy", force="dft_force", virial="dft_virial") assert result.ran_seed == 100
def Pd(tmpdir): from matdb.utility import relpath, copyonce from matdb.database import Controller from os import mkdir, symlink, remove, path target = relpath("./tests/Pd/matdb.yml") dbdir = str(tmpdir.join("pd_db")) mkdir(dbdir) copyonce(target, path.join(dbdir, "matdb.yml")) target = path.join(dbdir, "matdb") #We need to copy the POSCAR over from the testing directory to the temporary #one. from shutil import copy POSCAR = relpath("./tests/Pd/POSCAR") mkdir(path.join(dbdir, "seed")) copy(POSCAR, path.join(dbdir, "seed", "Pd")) # the source file `matdb.yml` linked to might be gone, that left `matdb.yml` not an valid "file" # we need to get rid if it anyway try: remove("matdb.yml") except: pass symlink("{}.yml".format(target), "matdb.yml") result = Controller("matdb", dbdir) remove("matdb.yml") result = Controller(target, dbdir) return result
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_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 Act(tmpdir): target = relpath("./tests/AgPd/matdb.yml") dbdir = str(tmpdir.join("active_db")) mkdir(dbdir) copyonce(target, path.join(dbdir, "matdb.yml")) target = path.join(dbdir, "matdb") seed_root = path.join(dbdir, "seed") if not path.isdir(seed_root): mkdir(seed_root) for i in range(1, 4): cfg_target = path.join(seed_root, "Pd{0}".format(i)) cfg_source = path.join(_get_reporoot(), "tests", "database", "files", "Pd", "POSCAR{0}".format(i)) copyonce(cfg_source, cfg_target) cntrl = Controller(target, dbdir) db = Database("active", dbdir, cntrl, [{"type": "active.Active"}], {}, 0) tcntrl = TController(db=db, root=dbdir, fits={}) dbargs = {"root": dbdir, "parent": db, "calculator": tcntrl.db.calculator} result = Active(**dbargs) return result
def test_errors(tmpdir, rendb): """Tests raising of exceptions for badly named parameters. """ root = str(tmpdir.join("legacy")) if not path.isdir(root): mkdir(root) folder = relpath("./tests/data/legacy") splits = None with pytest.raises(ValueError): LegacyDatabase("errors", root, None, splits, folder, "r-50-*.xyz") with pytest.raises(ValueError): LegacyDatabase("errors", root, None, splits, folder, "r-50-*.xyz", energy="dft_energy") with pytest.raises(ValueError): LegacyDatabase("errors", root, None, splits, folder, "r-50-*.xyz", energy="dft_energy", force="dft_force")
def test_rel_path(): """Tests the relative path. """ from matdb.utility import relpath temp = relpath("./tests") assert "matdb/tests" in temp
def test_cfg(tmpdir): """Tests conversion of MTP's CFG format to XYZ. """ target = str(tmpdir.join("cfg.xyz")) model = AtomsList(relpath("tests/files/io_convert/atoms.xyz")) conv = cfg_to_xyz(relpath("tests/files/io_convert/atoms.cfg"), target, species=[46, 47]) for a, b in zip(model, conv): assert np.allclose(a.get_positions(), b.get_positions()) assert np.allclose(a.get_forces(), b.calc.results["forces"]) assert np.allclose(a.get_stress(), b.calc.results["stress"], atol=1e-4, rtol=1e-3) assert a.get_total_energy() == b.calc.results["energy"]
def test_execute(Act): """Tetsts the execute of the Active database. """ add_configs(Act, 1) jobfile = path.join(Act.root,"jobfile.sh") assert not path.isfile(jobfile) Act.setup() assert len(Act.last_iteration) == 3 assert Act.is_setup() assert not Act.ready() assert path.isfile(jobfile) # test the recovery.sh. But without a "failures" file jobfile = path.join(Act.root,"recovery.sh") assert not path.isfile(jobfile) Act.jobfile(recovery=True) assert not path.isfile(jobfile) # test the recovery.sh with a "failures" file with open(path.join(Act.root,"failures"), 'w') as f: f.write("Not an actual failure.") Act.jobfile(recovery=True) assert path.isfile(jobfile) # dryrun assert not Act.is_executing() assert Act.can_extract() assert Act.execute(dryrun=True) assert not Act.is_executing() assert Act.can_extract() assert Act.execute() # We need to fake some VASP output so that we can cleanup the # database and get the rset src = relpath("./tests/data/Pd/complete/OUTCAR__DynMatrix_phonon_Pd_dim-2.00") dbfolder = Act.root for j in range(1,4): dest = path.join(dbfolder,"Ac.{}".format(j),"OUTCAR") symlink(src,dest) dbfolder = Act.root for j in range(1,4): src = path.join(dbfolder,"Ac.{}".format(j),"POSCAR") dest = path.join(dbfolder,"Ac.{}".format(j),"CONTCAR") symlink(src,dest) assert not Act.is_executing() assert Act.can_extract() # execute should return false, because one of them can be extracted assert not Act.execute() assert Act.nconfigs == 3 # the job never get actually executed because we are using sbatch on personal device for this test assert len(Act.fitting_configs) == 0
def Pd_db(tmpdir): from matdb.utility import relpath, reporoot from matdb.database import Controller from os import mkdir target = relpath("./tests/Pd/matdb") dbdir = str(tmpdir.join("pd_db")) mkdir(dbdir) #We need to copy the POSCAR over from the testing directory to the temporary #one. from shutil import copy POSCAR = relpath("./tests/Pd/POSCAR") copy(POSCAR, dbdir) Pd = Controller(target, dbdir) Pd.setup() #First, we need to copy the FORCE_SETS and total_dos.dat files so that we #don't have to recompile those (they are tested elsewhere). from matdb.utility import symlink troot = path.join(reporoot, "tests", "data", "Pd", "dynmatrix") files = ["FORCE_SETS", "total_dos.dat", "mesh.yaml"] for seq in Pd.find("Pd.phonon-*.dynmatrix"): for filename in files: target = path.join(seq.root, "phonopy", filename) source = path.join(troot, "{0}__{1}".format(filename, seq.parent.name)) symlink(target, source) Pd.cleanup() Pd.setup() files = ["OUTCAR", "output.xyz"] seq = Pd["Pd.modulate.modulations"] troot = path.join(reporoot, "tests", "data", "Pd", "modulations") for i in range(1, 6): key = "M.{0:d}".format(i) for filename in files: target = path.join(seq.root, key, filename) source = path.join(troot, "{0}__{1}".format(filename, key)) symlink(target, source) return Pd
def test_vasp_xyz(tmpdir): """Tests vasp to xyz function. """ from matdb.io import vasp_to_xyz from os import remove target = "atoms.xyz" model = vasp_to_xyz(relpath("tests/files/io_convert/"), target, properties=["species", "pos", "z"], parameters=["energy", "virial"], config_type="temp") assert model model = vasp_to_xyz(relpath("tests/files/io_convert/"), target, config_type="temp") assert model
def Pd_no_seeds(tmpdir): target = relpath("./tests/Pd/matdb_no_seeds.yml") dbdir = str(tmpdir.join("manual_no_seeds_db")) mkdir(dbdir) copyonce(target, path.join(dbdir, "matdb.yml")) target = path.join(dbdir, "matdb") cntrl = Controller(target, dbdir) return cntrl
def legDB(tmpdir): from matdb.utility import relpath, reporoot from matdb.database import Controller from os import mkdir target = relpath("./tests/legacy/matdb") dbdir = str(tmpdir.join("legacy")) mkdir(dbdir) return Controller(target, tmpdir=dbdir)
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. """ from matdb.utility import symlink, relpath 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]]) kwargs = { "kpoints": { "method": "mueller", "mindistance": 50 }, "potcars": { "directory": "./tests/vasp", "xc": "pbe", "versions": { "Si": '05Jan2001' } } } calc = Vasp(atm, target, str(tmpdir), 0, **kwargs) assert not calc.can_extract("def") assert not calc.can_extract(target) symlink(path.join(calc.folder, "OUTCAR"), relpath("tests/files/VASP/OUTCAR_incomplete")) assert not calc.can_extract(target) assert calc.is_executing(target) symlink(path.join(calc.folder, "OUTCAR"), relpath("tests/files/VASP/OUTCAR_complete")) assert calc.can_extract(target) assert not calc.is_executing(target)
def Pd_split(tmpdir): from matdb.utility import relpath, copyonce from matdb.database import Controller from os import mkdir, path target = relpath("./tests/Pd/matdb_split.yml") dbdir = str(tmpdir.join("pd_db_splits")) mkdir(dbdir) copyonce(target, path.join(dbdir, "matdb.yml")) target = path.join(dbdir, "matdb") from shutil import copy POSCAR = relpath("./tests/Pd/POSCAR") mkdir(path.join(dbdir, "seed")) copy(POSCAR, path.join(dbdir, "seed", "Pd-1")) copy(POSCAR, path.join(dbdir, "seed", "Pd-2")) copy(POSCAR, path.join(dbdir, "seed", "Pd-3")) copy(POSCAR, path.join(dbdir, "seed", "Pd-4")) copy(POSCAR, path.join(dbdir, "seed", "Pd-5")) result = Controller(target, dbdir) return result
def CoNiTi(tmpdir): from matdb.utility import relpath, copyonce from matdb.database import Controller from os import mkdir, symlink, remove target = relpath("./tests/files/CoNiTi.yml") dbdir = str(tmpdir.join("coniti_db")) mkdir(dbdir) copyonce(target, path.join(dbdir, "matdb.yml")) target = path.join(dbdir, "matdb") result = Controller(target, dbdir) return result
def Pd_copy(tmpdir): from matdb.utility import relpath, copyonce from matdb.database import Controller from os import path, remove, mkdir target = relpath("./tests/Pd/matdb_copy.yml") dbdir = str(tmpdir.join("pd_db_copy")) mkdir(dbdir) copyonce(target, path.join(dbdir, "matdb.yml")) target = path.join(dbdir, "matdb") from shutil import copy POSCAR = relpath("./tests/Pd/POSCAR") if path.isfile("matdb_copy.yml"): remove("matdb_copy.yml") result = Controller(target, dbdir) mkdir(path.join(dbdir, "seed")) copy(POSCAR, path.join(dbdir, "seed", "Pd")) result = Controller(target, dbdir) return result
def mtpdb(tmpdir): from matdb.utility import relpath, reporoot, copyonce from matdb.database import Controller from os import mkdir, path target = relpath("./tests/mtp/CoWV.yml") dbdir = str(tmpdir.join("mlp_tests")) mkdir(dbdir) copyonce(target, path.join(dbdir, "matdb.yml")) target = path.join(dbdir,"matdb") cntrl = Controller(target, tmpdir=dbdir) return cntrl
def test_read(tmpdir): """Tests the read function of the QE calculator. """ 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) symlink(path.join(calc.folder, "test.xml"), relpath("tests/qe/complete.xml")) output = calc._read(path.join(calc.folder, "test.xml")) assert output["convergence"] == 4.068079462655824e-7 assert np.allclose(output["atoms"], [0, 0, 0]) assert np.allclose(output["cell"], [[-3.75, 0, 3.75], [0, 3.75, 3.75], [-3.75, 3.75, 0]]) assert output["etot"] == -1.975055613913407e1 assert np.allclose(output["forces"], [0, 0, 0]) assert np.allclose(output["stress"], [[ 1.578434139006113e-4, -1.219727444046192e-19, -9.486769009248164e-20 ], [ -1.490777987167569e-19, 1.578434139006113e-4, 9.486769009248164e-20 ], [-6.776263578034403e-20, 1.219727444046192e-19, 1.578434139006113e-4]]) assert calc.version == '6.2 (svn rev. 14038)'
def Act(tmpdir): target = relpath("./tests/AgPd/matdb.yml") dbdir = str(tmpdir.join("active_db")) mkdir(dbdir) copyonce(target, path.join(dbdir, "matdb.yml")) target = path.join(dbdir,"matdb") cntrl = Controller(target, dbdir) db = Database("active", dbdir, cntrl, [{"type":"active.Active"}], {}, 0) tcntrl = TController(db=db, root=dbdir, fits={}) dbargs = {"root": dbdir, "parent": db, "calculator": tcntrl.db.calculator} result = Active(**dbargs) return result
def globals_setup(new_root): """Sets up the globals for the calculator instance. """ from matdb.io import read from matdb.calculators.utility import paths, set_paths target = relpath("./tests/AgPd/matdb_qe") config = path.expanduser(path.abspath(target)) if path.isabs(config): root, config = path.split(config) else: root, config = path.dirname(config), config configyml = read(root, config) configyml["root"] = new_root set_paths(configyml)
def phondb(tmpdir): """Returns a legacy database for the split-up phonon-50 AgPd database. """ splits = {"A": .5, "B": .75} root = str(tmpdir.join("legacy")) if not path.isdir(root): mkdir(root) folder = relpath("./tests/data/legacy") return LegacyDatabase("AgPd-50", root, None, splits, folder, "p-50-*.xyz", "ph", energy="dft_energy", force="dft_force", virial="dft_virial")
def rendb(tmpdir): """Returns a legacy database for the *renamed* phonon-50 AgPd database. """ splits = {"0": .3, "1": .6} root = str(tmpdir.join("legacy")) if not path.isdir(root): mkdir(root) folder = relpath("./tests/data/legacy") return LegacyDatabase("R-50", root, None, splits, folder, "r-50-*.xyz", "re", energy="energy", force="force", virial="virial", limit=80)
def test_get_calc_hashes(): """Tests the get_calculator_hashes function.""" from matdb.calculators.utility import get_calculator_hashes key = "matdb" target = relpath("./tests/AgPd/matdb") config = path.expanduser(path.abspath(target)) if path.isabs(config): root, config = path.split(config) else: root, config = path.dirname(config), config configyml = read(root, config) bc = "" cp = {} get_calculator_hashes(key, configyml, bc, cp) assert "vasp" in cp assert len(cp["vasp"]) == 2 for k, v in cp["vasp"].items(): assert "tests/vasp" in v
def Pd(tmpdir): target = relpath("./tests/Pd/matdb.yml") dbdir = str(tmpdir.join("manual_db")) mkdir(dbdir) copyonce(target, path.join(dbdir, "matdb.yml")) seed_root = path.join(dbdir, "seed") if not path.isdir(seed_root): mkdir(seed_root) for i in range(1, 4): cfg_target = path.join(seed_root, "Pd{0}".format(i)) cfg_source = path.join(_get_reporoot(), "tests", "database", "files", "Pd", "POSCAR{0}".format(i)) copyonce(cfg_source, cfg_target) target = path.join(dbdir, "matdb") cntrl = Controller(target, dbdir) return cntrl
def test_set_paths(): """Tests the setting of the global paths.""" from matdb.calculators.utility import paths, set_paths target = relpath("./tests/AgPd/matdb") config = path.expanduser(path.abspath(target)) if path.isabs(config): root, config = path.split(config) else: root, config = path.dirname(config), config configyml = read(root, config) set_paths(configyml) name = configyml["title"].strip().replace(' ', '_') namehash = str(sha1(name.encode("ASCII")).hexdigest()) assert namehash in paths assert "vasp" in paths[namehash] for k, v in paths[namehash]["vasp"].items(): assert "tests/vasp" in v
def test_build_calc(): """Tests the setting of the calc builder.""" from matdb.calculators.utility import build_calc from matdb.io import read from matdb.calculators.utility import paths, set_paths from matdb.utility import _set_config_paths _set_config_paths("AgPd_Enumerated", '.') target = relpath("./tests/AgPd/matdb") config = path.expanduser(path.abspath(target)) if path.isabs(config): root, config = path.split(config) else: root, config = path.dirname(config), config configyml = read(root, config) set_paths(configyml) kwargs = { "kpoints": { "rmin": 50 }, "potcars": { "directory": "./tests/vasp", "xc": "pbe" } } res = build_calc("Vasp", None, **kwargs) assert res.key == "vasp" res = None res = build_calc("Vasp", '.', **kwargs) assert res.key == "vasp"
def test_extract_force_sets(tmpdir): """Tests the extract_force_sets and extract_farce_constants functions. """ from matdb.calculators.vasp import extract_force_sets, extract_force_constants from matdb.utility import relpath, symlink phonon_dir = str(tmpdir.join("phonopy")) mkdir(phonon_dir) configs = {"1": str(tmpdir)} res = extract_force_sets(configs, phonon_dir) assert compare_nested_dicts(res, {"error": ""}) symlink(path.join(str(tmpdir), "vasprun.xml"), relpath("tests/files/VASP/vasprun.xml_complete")) res = extract_force_sets(configs, phonon_dir) assert res["error"] == [] res = extract_force_constants(configs, phonon_dir) assert res["error"] == []