Пример #1
0
def test_can_execute(tmpdir):
    """Tests the can_execute method.
    """
    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, 0, 0], [0, 1, 0], [0, 0, 1]])
    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_execute(target)
    calc.write_input(atm, directory=target)

    assert not calc.can_execute("def")
    assert calc.can_execute(target)
Пример #2
0
def test_to_dict(tmpdir):
    """Tests the conversion of atoms to dictionaries.
    """
    
    from matdb.calculators import Vasp
    from matdb.atoms import Atoms as Atoms
    from matdb.utility import _set_config_paths

    _set_config_paths("AgPd_Enumerated", str(tmpdir))
    target = str(tmpdir.join("atoms_dict"))
    globals_setup(target)

    if not path.isdir(target):
        mkdir(target)

    atSi = Atoms("Si8",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75],[0,0.5,0.5],[0.25,0.75,0.75]],
                 cell=[5.43,5.43,5.43])

    kwargs = {"encut":400, "kpoints": {"rmin": 50},
              "potcars":{"xc": "pbe", "directory": "./tests/vasp", "versions": {"Si": "05Jan2001"}}}
    
    calc = Vasp(atSi, target, '.', 0, **kwargs)

    atSi.set_calculator(calc)
    atSi.group_uuid = "123456"
    Sidict = atSi.to_dict()

    
    assert "calc" in Sidict
    assert "calc_kwargs" in Sidict
    assert Sidict["calc_kwargs"]["encut"] == 400
    assert Sidict["group_uuid"] == "123456"
    assert "potcars" in Sidict["calc_kwargs"]
    assert "kpoints" in Sidict["calc_kwargs"]
Пример #3
0
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)
Пример #4
0
def test_write_input(tmpdir):
    """Tests the writing of the input files. 
    """
    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, 0, 0], [0, 1, 0], [0, 0, 1]])
    kwargs = {
        "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, directory=target)
    assert path.isfile(path.join(calc.folder, "POTCAR"))
    assert path.isfile(path.join(calc.folder, "INCAR"))
    assert path.isfile(path.join(calc.folder, "POSCAR"))
    assert path.isfile(path.join(calc.folder, "KPOINTS"))

    kwargs = {
        "potcars": {
            "directory": "./tests/vasp",
            "xc": "pbe",
            "versions": {
                "Si": '05Jan2001'
            }
        }
    }
    calc = Vasp(atm, target, str(tmpdir), 0, **kwargs)

    calc.write_input(atm, directory=target)
    assert path.isfile(path.join(calc.folder, "POTCAR"))
    assert path.isfile(path.join(calc.folder, "INCAR"))
    assert path.isfile(path.join(calc.folder, "POSCAR"))
    assert path.isfile(path.join(calc.folder, "KPOINTS"))

    calc.create()
    assert path.isfile(path.join(calc.folder, "POTCAR"))
    assert path.isfile(path.join(calc.folder, "INCAR"))
    assert path.isfile(path.join(calc.folder, "POSCAR"))
    assert path.isfile(path.join(calc.folder, "KPOINTS"))
Пример #5
0
def test_remote_read(tmpdir):
    """Tests the reading in of a atoms.h5 file from another directory."""
    from matdb.atoms import Atoms
    from matdb.utility import _set_config_paths, reporoot
    from os import path

    _set_config_paths("AgPd_Enumerated", str(tmpdir))
    target = str(tmpdir.join("to_hdf5"))
    globals_setup(target)
    at = Atoms()
    at.read(target=path.join(reporoot, "tests", "files", "test.h5"))
    assert isinstance(at, Atoms)
Пример #6
0
def test_hdf5(tmpdir):
    """Tests whether an atoms object with calculated parameters can be saved to
    JSON and then restored.
    """
    from matdb.calculators import Vasp
    from matdb.atoms import Atoms
    from matdb.utility import _set_config_paths

    _set_config_paths("AgPd_Enumerated", str(tmpdir))
    target = str(tmpdir.join("to_hdf5"))
    globals_setup(target)
    if not path.isdir(target):
        mkdir(target)

    atSi = Atoms("Si8",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75],[0,0.5,0.5],[0.25,0.75,0.75]],
                 cell=[5.43,5.43,5.43])
    kwargs = {"kpoints":{"rmin":50}, "potcars": {"directory":"./tests/vasp",
                                                 "versions":{"Si": '05Jan2001'}}, "xc":"pbe"}    
    potSW = Vasp(atSi, target, str(tmpdir), 0, **kwargs)
    atSi.set_calculator(potSW)
    atSi.add_property("vasp_force", [[-18057.59589857, -18057.59589857, -18057.59589857],
         [ -2997.55626529,  -2997.55626529,  -2997.55626529],
         [  3044.17916471,   3044.17916471, -34130.71583118],
         [ 18969.1757571 ,  18969.1757571 ,  11159.15815145],
         [  3044.17916471, -34130.71583118,   3044.17916471],
         [ 18969.1757571 ,  11159.15815145,  18969.1757571 ],
         [-34130.71583118,   3044.17916471,   3044.17916471],
         [ 11159.15815145,  18969.1757571 ,  18969.1757571 ]])
    atSi.add_param("vasp_energy", 25360.504084423999)
    atSi.add_param("vasp_virial", [[ 33538.34327189,  11045.88697112,  11045.88697112],
         [ 11045.88697112,  33538.34327189,  11045.88697112],
         [ 11045.88697112,  11045.88697112,  33538.34327189]])
    atSi.properties["rand"] = np.random.randint(0, 100, 8)
    atSi.write(target=path.join(target,"temp.h5"))
    atR = Atoms()
    atR.read(target=path.join(target,"temp.h5"))

    # check that the calculator got transfered properly.
    assert type(atSi.calc) == type(atR.calc)
    assert atSi.calc.args == atR.calc.args
    assert atSi.calc.kwargs == atR.calc.kwargs

    # check that the other properties got transfered properly.
    assert atR.vasp_energy == atSi.vasp_energy
    assert isinstance(atR, Atoms)
    assert np.allclose(atR.vasp_force, atSi.vasp_force)
    assert np.allclose(atR.vasp_virial, atSi.vasp_virial)
    assert np.allclose(atR.properties["rand"], atSi.properties["rand"])
    assert np.allclose(atR.positions, atSi.positions)
    remove(path.join(target,"temp.h5"))
Пример #7
0
def test_read_atoms(tmpdir):
    """Tests the reading of atoms objects from files.
    """
    
    from matdb.calculators import Vasp
    from matdb.atoms import Atoms as Atoms
    from matdb.utility import _set_config_paths

    _set_config_paths("AgPd_Enumerated", str(tmpdir))
    target = str(tmpdir.join("read_atoms"))
    globals_setup(target)

    if not path.isdir(target):
        mkdir(target)

    atSi = Atoms("Si8",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75],[0,0.5,0.5],[0.25,0.75,0.75]],
                 cell=[5.43,5.43,5.43])

    kwargs = {"encut":400, "kpoints": {"rmin": 50},
              "potcars":{"xc": "pbe", "directory": "./tests/vasp", "versions": {"Si": "05Jan2001"}}}

    calc = Vasp(atSi, target, '.', 0, **kwargs)
    atSi.set_calculator(calc)

    atSi.set_calculator(calc)
    atSi.add_property("vasp_force", [[-18057.59589857, -18057.59589857, -18057.59589857],
         [ -2997.55626529,  -2997.55626529,  -2997.55626529],
         [  3044.17916471,   3044.17916471, -34130.71583118],
         [ 18969.1757571 ,  18969.1757571 ,  11159.15815145],
         [  3044.17916471, -34130.71583118,   3044.17916471],
         [ 18969.1757571 ,  11159.15815145,  18969.1757571 ],
         [-34130.71583118,   3044.17916471,   3044.17916471],
         [ 11159.15815145,  18969.1757571 ,  18969.1757571 ]])
    atSi.add_param("vasp_energy", 25360.504084423999)
    atSi.add_param("vasp_virial", [[ 33538.34327189,  11045.88697112,  11045.88697112],
         [ 11045.88697112,  33538.34327189,  11045.88697112],
         [ 11045.88697112,  11045.88697112,  33538.34327189]])
    atSi.group_uuid = "123456"

    temp = path.join(target,"temp.h5")
    atSi.write(temp)

    atR = Atoms(temp)
    
    assert atR.calc.name == "Vasp"
    assert hasattr(atR.calc,"potcars")
    assert atR.calc.kwargs["encut"] == 400
    assert np.allclose(atR.positions,[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75],[0,0.5,0.5],[0.25,0.75,0.75]])
Пример #8
0
def test_Atoms_attributes(tmpdir):
    """Tests the some of the attributes of an atoms object.
    """
    from matdb.calculators import Vasp
    from matdb.atoms import Atoms
    from matdb.utility import _set_config_paths, reporoot
    from os import path

    _set_config_paths("AgPd_Enumerated", str(tmpdir))
    target = str(tmpdir.join("atom_attributes"))
    globals_setup(target)
    if not path.isdir(target):
        mkdir(target)

    at1 = Atoms("Si8",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75],[0,0.5,0.5],[0.25,0.75,0.75]],
                 cell=[5.43,5.43,5.43])
    kwargs = {"kpoints":{"rmin":50}, "potcars": {"directory":"./tests/vasp",
                                                 "versions":{"Si": '05Jan2001'}}, "xc":"pbe"}    
    potSW = Vasp(at1, target, str(tmpdir), 0, **kwargs)
    at1.set_calculator(potSW)
    at1.add_property("vasp_force", [[-18057.59589857, -18057.59589857, -18057.59589857],
         [ -2997.55626529,  -2997.55626529,  -2997.55626529],
         [  3044.17916471,   3044.17916471, -34130.71583118],
         [ 18969.1757571 ,  18969.1757571 ,  11159.15815145],
         [  3044.17916471, -34130.71583118,   3044.17916471],
         [ 18969.1757571 ,  11159.15815145,  18969.1757571 ],
         [-34130.71583118,   3044.17916471,   3044.17916471],
         [ 11159.15815145,  18969.1757571 ,  18969.1757571 ]])
    at1.add_param("vasp_energy", 1234)

    assert at1.get_energy() == 1234

    at1.rm_param("vasp_energy")
    assert not "vasp_energy" in at1.info["params"]
    at1.rm_property("vasp_force")
    assert not "vasp_force" in at1.info["properties"]

    at1.params = {"vasp_energy": 1234}
    assert at1.vasp_energy == 1234

    at1.properties["vasp_force"] = [[-18057.59589857, -18057.59589857, -18057.59589857],
         [ -2997.55626529,  -2997.55626529,  -2997.55626529],
         [  3044.17916471,   3044.17916471, -34130.71583118],
         [ 18969.1757571 ,  18969.1757571 ,  11159.15815145],
         [  3044.17916471, -34130.71583118,   3044.17916471],
         [ 18969.1757571 ,  11159.15815145,  18969.1757571 ],
         [-34130.71583118,   3044.17916471,   3044.17916471],
         [ 11159.15815145,  18969.1757571 ,  18969.1757571 ]]
    assert "vasp_force" in at1.info["properties"]
Пример #9
0
def test_to_dict():
    """Tests the to dict method.
    """
    from matdb.utility import _set_config_paths

    _set_config_paths("AgPd_Enumerated", '.')
    
    atSi = Atoms("Si8",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75],[0,0.5,0.5],[0.25,0.75,0.75]],
                 cell=[5.43,5.43,5.43])

    kwargs  = {}
    args = []

    calc = Aflow(atSi, '$control$', '$control$', 0, *args, **kwargs)
    out = {"folder":'$control$', "ran_seed":0, "contr_dir":'$control$',
           "kwargs":{"entry":None}, "args":[]}
    assert compare_nested_dicts(calc.to_dict(),out)
Пример #10
0
def test_Atoms_creation(tmpdir):
    """Tests the initialization of the atoms objcet.
    """
    from matdb.atoms import Atoms
    from ase.atoms import Atoms as aseAtoms
    atSi = Atoms("Si8",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75],[0,0.5,0.5],[0.25,0.75,0.75]],
                 cell=[5.43,5.43,5.43])
    atR = Atoms(atSi)
    assert atR==atSi
    
    atSi = aseAtoms("Si8",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75],[0,0.5,0.5],[0.25,0.75,0.75]],
                 cell=[5.43,5.43,5.43])
    atSi.info['nneightol'] = 10
    atSi.info['cutoff'] = 5
    atSi.info['cutoff_break'] = 10
    atR = Atoms(atSi)

    assert np.allclose(atR.positions,atSi.positions)
    assert np.allclose(atR.cell,atSi.cell)
    assert hasattr(atR,'nneightol')
    assert hasattr(atR,'cutoff')
    assert hasattr(atR,'cutoff_break')
    
    from matdb.utility import _set_config_paths
    _set_config_paths("AgPd_Enumerated", str(tmpdir))
    target = str(tmpdir.join("make_atoms"))
    globals_setup(target)
    if not path.isdir(target):
        mkdir(target)
    atSi = Atoms("Si8",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75],[0,0.5,0.5],[0.25,0.75,0.75]],
                 cell=[5.43,5.43,5.43])
    atSi.write(target=path.join(target,"temp.xyz"))
    atR = Atoms(path.join(target,"temp.xyz"))
    
    assert np.allclose(atR.positions,atSi.positions)
    assert np.allclose(atR.cell,atSi.cell)
    remove(path.join(target,"temp.xyz"))
Пример #11
0
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)
Пример #12
0
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"
Пример #13
0
def test_ase_atoms_conversion(tmpdir):
    """Tests the conversion of an ase atoms objcet to a 'matdb.atoms.Atoms' object. 
    """

    from matdb.atoms import Atoms as matAtoms
    from ase.atoms import Atoms
    from matdb.utility import _set_config_paths

    _set_config_paths("AgPd_Enumerated", str(tmpdir))
    target = str(tmpdir.join("from_ase"))
    globals_setup(target)
    if not path.isdir(target):
        mkdir(target)

    atSi = Atoms("Si8",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75],[0,0.5,0.5],[0.25,0.75,0.75]],
                 cell=[5.43,5.43,5.43])

    aR = matAtoms(atSi)

    assert np.allclose(aR.positions, atSi.positions)
    assert aR.calc == atSi.calc 
Пример #14
0
def test_vasp_setup(tmpdir):
    """Tests Vasp calculator initialization.
    """
    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": {
            "rmin": 50
        },
        "potcars": {
            "directory": "./tests/vasp",
            "versions": {
                "Si": '05Jan2001'
            }
        },
        "xc": "pbe"
    }
    calc = Vasp(atm, target, str(tmpdir), 0, **kwargs)

    assert calc.potcars["xc"] == "pbe"

    kwargs = {
        "kpoints": {
            "rmin": 50
        },
        "potcars": {
            "directory": "./tests/vasp",
            "xc": "pbe",
            "versions": {
                "Si": '05Jan2001'
            }
        }
    }
    calc = Vasp(atm, target, str(tmpdir), 0, **kwargs)

    assert calc.kwargs["xc"] == "pbe"

    stat = calc.set_static(kwargs)
    assert "nsw" in stat and stat["nsw"] == 0

    calc.asis = True
    assert calc.read_convergence()

    with pytest.raises(VersionError):
        kwargs = {
            "kpoints": {
                "rmin": 50
            },
            "potcars": {
                "directory": "./tests/vasp",
                "xc": "pbe"
            }
        }
        calc = Vasp(atm, '.', str(tmpdir), 0, **kwargs)

    with pytest.raises(VersionError):
        kwargs = {
            "kpoints": {
                "rmin": 50
            },
            "potcars": {
                "directory": "./tests/vasp",
                "xc": "pbe",
                "versions": {
                    "Si": '04Jan2001'
                }
            }
        }
        calc = Vasp(atm, '.', str(tmpdir), 0, **kwargs)

    with pytest.raises(VersionError):
        kwargs = {
            "kpoints": {
                "rmin": 50
            },
            "potcars": {
                "directory": "./tests/vasp",
                "xc": "pbe",
                "versions": {
                    "Si": '04Jan2001'
                }
            }
        }
        atm = Atoms("Al", positions=[[0, 0, 0]])
        calc = Vasp(atm, '.', str(tmpdir), 0, **kwargs)

    with pytest.raises(ValueError):
        kwargs = {
            "kpoints": {
                "rmin": 50
            },
            "potcars": {
                "directory": "./tests/vasp"
            }
        }
        calc = Vasp(atm, '.', str(tmpdir), 0, **kwargs)
Пример #15
0
def test_AtomsList_creation(tmpdir):
    """Tests the creation of the AtomsList object. 
    """
    from matdb.calculators import Vasp
    from matdb.atoms import Atoms, AtomsList
    from matdb.utility import _set_config_paths

    _set_config_paths("AgPd_Enumerated", str(tmpdir))
    target = str(tmpdir.join("make_AtomsList"))
    globals_setup(target)

    if not path.isdir(target):
        mkdir(target)
    kwargs = {"kpoints":{"rmin":50}, "potcars": {"directory":"./tests/vasp",
                                                 "versions":{"Si": '05Jan2001'}}, "xc":"pbe"}    

        
    at1 = Atoms("Si8",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75],[0,0.5,0.5],[0.25,0.75,0.75]],
                 cell=[5.43,5.43,5.43],info={"rand":10})
    potSW = Vasp(at1, target, str(tmpdir), 0, **kwargs)
    at1.set_calculator(potSW)
    at1.add_property("vasp_force", [[-18057.59589857, -18057.59589857, -18057.59589857],
         [ -2997.55626529,  -2997.55626529,  -2997.55626529],
         [  3044.17916471,   3044.17916471, -34130.71583118],
         [ 18969.1757571 ,  18969.1757571 ,  11159.15815145],
         [  3044.17916471, -34130.71583118,   3044.17916471],
         [ 18969.1757571 ,  11159.15815145,  18969.1757571 ],
         [-34130.71583118,   3044.17916471,   3044.17916471],
         [ 11159.15815145,  18969.1757571 ,  18969.1757571 ]])
    at1.add_param("vasp_energy", 1234)
    
    at2 = Atoms("S6",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75]],
                 cell=[6.43,5.43,4.43],info={"rand":10},calculator=potSW)
    kwargs = {"kpoints":{"rmin":50}, "potcars": {"directory":"./tests/vasp",
                                                 "versions":{"S": '06Sep2000'}}, "xc":"pbe"}    
    potSW = Vasp(at2, target, str(tmpdir), 0, **kwargs)
    at2.set_calculator(potSW)
    at2.add_property("vasp_force", np.random.randint(0, 100, (6,3)))
    at2.add_param("vasp_energy", 4321)
    
    al1 = AtomsList([at1,at2])
    
    assert len(al1) == 2

    at1.write(target=path.join(target,"temp1.h5"))
    at2.write(target=path.join(target,"temp2.h5"))

    al2 = AtomsList([path.join(target,"temp1.h5"),path.join(target,"temp2.h5")])
    assert len(al2) == 2
    assert isinstance(al2[0],Atoms)

    empty_list = AtomsList([])
    assert len(empty_list) == 0

    al3 = AtomsList(at1)
    assert al3[0] == at1

    al4 = AtomsList(path.join(target,"temp1.h5"))
    assert len(al4) == 1
    assert isinstance(al4[0],Atoms)
    assert al4[0].vasp_energy == at1.vasp_energy
Пример #16
0
def test_atoms_to_cfg(tmpdir):
    """Tests the writing of an atoms object to cfg format. 
    """
    from matdb.io import atoms_to_cfg
    from matdb.atoms import Atoms
    from os import path, remove
    from ase.calculators.singlepoint import SinglePointCalculator
    from matdb.calculators import Vasp
    from matdb.utility import _set_config_paths

    atSi = Atoms("Si8",
                 positions=[[0, 0, 0], [0.25, 0.25, 0.25], [0.5, 0.5, 0],
                            [0.75, 0.75, 0.25], [0.5, 0, 0.5],
                            [0.75, 0.25, 0.75], [0, 0.5, 0.5],
                            [0.25, 0.75, 0.75]],
                 cell=[5.43, 5.43, 5.43])

    target = "train.cfg"

    atoms_to_cfg(atSi, target)

    assert path.isfile(target)

    remove(target)

    energy = 10.0
    forces = [[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [5, 5, 5], [6, 6, 6],
              [7, 7, 7], [8, 8, 8]]
    virial = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

    atSi.calc = SinglePointCalculator(atSi,
                                      energy=energy,
                                      forces=np.array(forces),
                                      stress=virial)

    atSi.add_property("energy", energy)
    atSi.add_property("virial", virial)
    atSi.add_param("force", forces)
    atoms_to_cfg(atSi, target)

    assert path.isfile(target)

    remove(target)

    atSi = Atoms("Si8",
                 positions=[[0, 0, 0], [0.25, 0.25, 0.25], [0.5, 0.5, 0],
                            [0.75, 0.75, 0.25], [0.5, 0, 0.5],
                            [0.75, 0.25, 0.75], [0, 0.5, 0.5],
                            [0.25, 0.75, 0.75]],
                 cell=[5.43, 5.43, 5.43])

    type_map = {0: 1}

    kwargs = {
        "kpoints": {
            "rmin": 50
        },
        "potcars": {
            "directory": "./tests/vasp",
            "versions": {
                "Si": "05Jan2001"
            }
        },
        "xc": "pbe"
    }
    _set_config_paths("test", tmpdir)
    atSi.calc = Vasp(atSi, str(tmpdir.join("Vasp")), str(tmpdir), 0, **kwargs)

    atoms_to_cfg(atSi, target, type_map=type_map, config_id="test")
    assert path.isfile(target)

    remove(target)
Пример #17
0
def test_write_potcar(tmpdir):
    """Tests the writing of the POTCAR file. 
    """
    from matdb.utility import _set_config_paths
    from hashlib import sha1

    _set_config_paths("AgPd_Enumerated", str(tmpdir))
    target = str(tmpdir.join("Vasp"))
    globals_setup(target)

    atm = Atoms("Si",
                positions=[[0, 0, 0]],
                cell=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])
    kwargs = {
        "kpoints": {
            "rmin": 50
        },
        "potcars": {
            "directory": "./tests/vasp",
            "xc": "pbe",
            "versions": {
                "Si": '05Jan2001'
            }
        }
    }
    calc = Vasp(atm, target, str(tmpdir), 0, **kwargs)

    calc._write_potcar()
    this_potcar = str(
        sha1("{0}{1}".format("Si", "05Jan2001").encode()).hexdigest())
    assert path.isfile(path.join(calc.contr_dir, "POTCARS", this_potcar))
    assert path.isfile(path.join(calc.folder, "POTCAR"))
    remove(path.join(calc.contr_dir, "POTCARS", this_potcar))

    kwargs = {
        "kpoints": {
            "method": "mueller",
            "mindistance": 50
        },
        "potcars": {
            "directory": "./tests/vasp",
            "xc": "pbe",
            "versions": {
                "Si": '05Jan2001'
            }
        }
    }
    calc = Vasp(atm, target, str(tmpdir), 0, **kwargs)
    calc._write_potcar()
    assert path.isfile(path.join(calc.contr_dir, "POTCARS", this_potcar))
    calc._write_potcar()
    assert path.isfile(path.join(calc.folder, "POTCAR"))

    # check POTCAR reuse
    calc = Vasp(atm, target, str(tmpdir), 0, **kwargs)

    with pytest.raises(SpeciesError):
        kwargs = {
            "kpoints": {
                "rmin": 50
            },
            "potcars": {
                "directory": "./tests/vasp",
                "xc": "pbe",
                "versions": {
                    "Si": '05Jan2001'
                }
            }
        }
        symlink(
            path.join(calc.contr_dir, "POTCARS", calc.this_potcar),
            path.join(reporoot, "tests", "vasp", "potpaw_PBE", "Ag", "POTCAR"))
        calc = Vasp(atm, '.', str(tmpdir), 0, **kwargs)

    with pytest.raises(VersionError):
        kwargs = {
            "kpoints": {
                "rmin": 50
            },
            "potcars": {
                "directory": "./tests/vasp",
                "xc": "pbe",
                "versions": {
                    "Si": '05Jan2001'
                }
            }
        }
        symlink(
            path.join(calc.contr_dir, "POTCARS", calc.this_potcar),
            path.join(reporoot, "tests", "vasp", "potpaw_PBE", "Si_sv_GW",
                      "POTCAR"))
        atm = Atoms("Si",
                    positions=[[0, 0, 0]],
                    cell=[[1, 0, 0], [0, 1, 0], [0, 0, 1]])
        calc = Vasp(atm, '.', str(tmpdir), 0, **kwargs)
Пример #18
0
def test_to_dict(tmpdir):
    """Tests the calculator to_dict method.
    """
    from matdb.utility import _set_config_paths

    _set_config_paths("AgPd_Enumerated", str(tmpdir))
    target = str(tmpdir.join("Vasp"))
    globals_setup(target)

    atm = Atoms("AgPd",
                positions=[[0, 0, 0], [0.25, 0.25, 0.25]],
                cell=[1, 1, 1])
    kwargs = {
        "ibrion": 5,
        "xc": "pbe",
        "nsw": 1,
        "kpoints": {
            "method": "mueller",
            "mindistance": 30
        },
        "potcars": {
            "directory": '156def0ed29f1a908d5a7b4eae006c672e7b0ff1',
            "xc": "pbe",
            "versions": {
                "Ag": '09Dec2005',
                "Pd": '04Jan2005'
            },
            "setups": {
                "Ag": "_pv"
            }
        },
        "exec_path": None
    }
    calc = Vasp(atm, '$control$/Vasp', '$control$', 0, **kwargs)

    calc_dict = calc.to_dict()

    kwargs1 = {
        "ibrion": 5,
        "xc": "pbe",
        "nsw": 1,
        "kpoints": {
            "method": "mueller",
            "mindistance": 30
        },
        "potcars": {
            "directory": '156def0ed29f1a908d5a7b4eae006c672e7b0ff1',
            "xc": "pbe",
            "versions": {
                "Ag": '09Dec2005',
                "Pd": '04Jan2005'
            },
            "setups": {
                "Ag": "_pv"
            }
        }
    }
    kwargs2 = {
        "ibrion": 5,
        "xc": "pbe",
        "nsw": 1,
        "kpoints": {
            "method": "mueller",
            "mindistance": 30
        },
        "potcars": {
            "directory": '03526af4c062e11236b794c0b260a2571cfe12d6',
            "xc": "pbe",
            "versions": {
                "Ag": '09Dec2005',
                "Pd": '04Jan2005'
            },
            "setups": {
                "Ag": "_pv"
            }
        }
    }

    out1 = {
        "folder": '$control$/Vasp',
        "ran_seed": 0,
        "contr_dir": '$control$',
        "kwargs": kwargs1,
        "args": ()
    }  #, "version": "vasp.4.6.35"}
    out2 = {
        "folder": '$control$/Vasp',
        "ran_seed": 0,
        "contr_dir": '$control$',
        "kwargs": kwargs2,
        "args": ()
    }  #, "version": "vasp.4.6.35"}

    # "version" appears in calc_dict on local device
    if "version" in calc_dict:
        out1["version"] = calc_dict["version"]
        out2["version"] = calc_dict["version"]

    assert compare_nested_dicts(calc_dict, out1) or compare_nested_dicts(
        calc_dict, out2)

    calc_dict = calc.to_dict()
    out1 = {
        "folder": '$control$/Vasp',
        "ran_seed": 0,
        "contr_dir": '$control$',
        "kwargs": kwargs1,
        "args": (),
        "version": ""
    }
    out2 = {
        "folder": '$control$/Vasp',
        "ran_seed": 0,
        "contr_dir": '$control$',
        "kwargs": kwargs2,
        "args": (),
        "version": ""
    }
    assert compare_nested_dicts(calc_dict, out1) or compare_nested_dicts(
        calc_dict, out2)
Пример #19
0
def test_reading_multiple_files(tmpdir):
    """Tests the reading in of multiple atoms objects to an AtomsList.
    """
    
    from matdb.calculators import Vasp
    from matdb.atoms import Atoms as Atoms, AtomsList
    from matdb.io import save_dict_to_h5
    import h5py
    from matdb.utility import _set_config_paths

    _set_config_paths("AgPd_Enumerated", str(tmpdir))
    target = str(tmpdir.join("read_atoms2"))
    globals_setup(target)

    if not path.isdir(target):
        mkdir(target)

    atSi = Atoms("Si8",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75],[0,0.5,0.5],[0.25,0.75,0.75]],
                 cell=[5.43,5.43,5.43])

    kwargs = {"encut":400, "kpoints": {"rmin": 50},
              "potcars":{"xc": "pbe", "directory": "./tests/vasp", "versions": {"Si": "05Jan2001"}}}
    
    calc = Vasp(atSi, target, '.', 0, **kwargs)
    atSi.set_calculator(calc)

    temp = path.join(target,"temp.h5")
    atSi.write(temp)    

    atSi2 = Atoms("Si8",positions=[[0,0,0],[0.25,0.25,0.25],[0.5,0.5,0],[0.75,0.75,0.25],
                                  [0.5,0,0.5],[0.75,0.25,0.75],[0,0.5,0.5],[0.25,0.75,0.75]],
                 cell=[6.43,6.43,6.43])

    kwargs = {"encut":600, "kpoints": {"rmin": 50},
              "potcars":{"xc": "pbe", "directory": "./tests/vasp", "versions": {"Si": "05Jan2001"}}}
    
    calc = Vasp(atSi2, target, '.', 0, **kwargs)
    atSi2.set_calculator(calc)
    temp2 = path.join(target,"temp2.h5")
    atSi2.write(temp2)

    atRL = AtomsList([temp,temp2])

    assert len(atRL) == 2
    assert atRL[0].calc.kwargs["encut"] != atRL[1].calc.kwargs["encut"]
    assert atRL[1].calc.kwargs["encut"] in [400,600]
    assert atRL[0].calc.kwargs["encut"] in [400,600]

    atom_dict = {"atom_1":temp, "atom_2": temp2}

    temp3 = path.join(target,"temp3.h5")
    with h5py.File(temp3,"w") as hf:
        save_dict_to_h5(hf,atom_dict,'/')

    atRL = AtomsList(temp3)

    assert len(atRL) == 2
    assert atRL[0].calc.kwargs["encut"] != atRL[1].calc.kwargs["encut"]
    assert atRL[1].calc.kwargs["encut"] in [400,600]
    assert atRL[0].calc.kwargs["encut"] in [400,600]
Пример #20
0
def test_to_dict(tmpdir):
    """Tests the calculator to_dict method.
    """
    from matdb.utility import _set_config_paths

    _set_config_paths("AgPd_Enumerated", str(tmpdir))
    target = str(tmpdir.join("Qe"))
    globals_setup(target)

    atm = Atoms("AlPd", positions=[[0, 0, 0], [0.5, 0.5, 0.5]])
    kwargs = {
        "potcars": {
            "directory": "./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, '$control$/Qe', '$control$', 0, **kwargs)
    symlink(path.join(calc.folder, "test.xml"),
            relpath("tests/qe/complete.xml"))
    calc.extract(target)

    calc_dict = calc.to_dict()

    kwargs = {
        "potcars": {
            "directory": "156def0ed29f1a908d5a7b4eae006c672e7b0ff1",
            "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"
            },
            'tprnfor': True,
            'tstress': True
        }
    }
    out = {
        "folder": '$control$/Qe',
        "ran_seed": 0,
        "contr_dir": '$control$',
        "kwargs": kwargs,
        "args": (),
        "version": "6.2 (svn rev. 14038)"
    }
    assert compare_nested_dicts(calc_dict, out)