Пример #1
0
    def write(self, target="atoms.h5", **kwargs):
        """Writes an atoms object to file.

        Args:
            target (str): The path to the target file. Default is "atoms.h5".
        """
        frmt = target.split('.')[-1]
        if frmt == "h5" or frmt == "hdf5":
            from matdb.io import save_dict_to_h5
            with h5py.File(target, "w") as hf:
                data = self.to_dict()
                save_dict_to_h5(hf, data, '/')
        else:
            io.write(target, self, **kwargs)
Пример #2
0
    def write(self, target, **kwargs):
        """Writes an atoms object to file.

        Args:
            target (str): The path to the target file.
            kwargs (dict): A dictionary of key word args to pass to the ase  write function.
        """

        frmt = target.split('.')[-1]
        if frmt == "h5" or frmt == "hdf5":
            from matdb.io import save_dict_to_h5
            with h5py.File(target, "w") as hf:
                for atom in self:
                    data = atom.to_dict()
                    hf.create_group("atom_{}".format(data["uuid"]))
                    save_dict_to_h5(hf, data, "/atom_{}/".format(data["uuid"]))
        else:
            io.write(target, self)
Пример #3
0
def _fix_precomp(db, purge=False):
    """Reconstitutes the `pre_comp_atoms.h5` file by copying the existing
    `atoms.h5`, and applying some modifications to the atoms object.

    Args:
        db (matdb.database.Group): a group to create pre_comp files for.
        purge (bool): when True, remove the `atoms.h5` files after the pre-comp
          file is created.
    """
    for aid, apath in db.configs.items():
        target = path.join(apath, "atoms.h5")
        with h5py.File(target, "r") as hf:
            data = load_dict_from_h5(hf)
        data["pbc"] = np.array([1, 1, 1], dtype=bool)

        newtarg = path.join(apath, "pre_comp_atoms.h5")
        with h5py.File(newtarg, "w") as hf:
            save_dict_to_h5(hf, data, '/')

        if path.isfile(newtarg) and purge:
            remove(target)
Пример #4
0
def test_hdf5_in_out():
    """Tests the writing of dictionaries to hdf5 and reading back out.
    """

    import h5py
    from matdb.io import load_dict_from_h5, save_dict_to_h5
    from os import remove

    dict_1_in = {
        "a": {
            "B": np.int64(1),
            "C": np.int64(3),
            "D": {
                "temp": np.array([10, 11, 12])
            }
        },
        "n": np.array([3, 2]),
        "t": np.int64(5)
    }
    dict_2_in = {"a": np.int64(10), "b": np.array([1, 2, 10])}

    hf = h5py.File("temp.h5", "w")
    save_dict_to_h5(hf, dict_1_in, "/")
    hf.close()

    hf = h5py.File("temp.h5", "r")
    out = load_dict_from_h5(hf)
    hf.close()
    assert compare_nested_dicts(dict_1_in, out)
    remove("temp.h5")

    hf = h5py.File("temp.h5", "w")
    save_dict_to_h5(hf, dict_2_in, "/")
    hf.close()

    hf = h5py.File("temp.h5", "r")
    out = load_dict_from_h5(hf)
    hf.close()
    assert compare_dicts(dict_2_in, out)
    remove("temp.h5")

    hf = h5py.File("temp.h5", "w")

    with pytest.raises(ValueError):
        save_dict_to_h5(hf, {"a": remove}, "/")
    hf.close()
    remove("temp.h5")
Пример #5
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]