def test_Atoms_get_energy(): """Tests get_energy method. """ from matdb.atoms import Atoms 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]) at1.add_param("vasp_energy", 4532) assert at1.get_energy() == 4532
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"))
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]])
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"]
def test_safeupdate(): """Tests the safe_update method in utility. """ from matdb.atoms import Atoms from matdb.utility import safe_update al = Atoms("Si",positions=[[0,0,0]]) al.add_param("energy",None) kv = {"positions":[[0.5,0.5,0.5]],"energy":10} safe_update(al,kv) assert np.allclose(al.positions,[[0,0,0]]) assert al.energy == 10
def test_AtomsList_sort(): """Tests the method AtomsList.sort """ from matdb.atoms import Atoms, AtomsList 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}) 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}) at3 = Atoms("CNi",positions=[[0,0,0],[0.5,0.5,0.5]], info={"rand":8}) at4 = Atoms("CoV",positions=[[0,0,0],[0.25,0.5,0.25]], info={"rand":8}) at1.add_param("vasp_energy", 25361.504084423999) at2.add_param("vasp_energy", 25362.504084423999) at3.add_param("vasp_energy", 25363.504084423999) at4.add_param("vasp_energy", 25364.504084423999) al1 = AtomsList([at4,at2,at1,at3]) #This is to test __getitem__ al2 = al1[0:2] assert len(al2) == 2 al1.sort(key=len) assert al1[0].get_chemical_formula() == "CoV" assert al1[1].get_chemical_formula() == "CNi" assert al1[2].get_chemical_formula() == "S6" assert al1[3].get_chemical_formula() == "Si8" al1.sort(attr="vasp_energy") assert al1[0].get_chemical_formula() == "Si8" assert al1[1].get_chemical_formula() == "S6" assert al1[2].get_chemical_formula() == "CNi" assert al1[3].get_chemical_formula() == "CoV" al1.sort(attr="vasp_energy", reverse=True) assert al1[0].get_chemical_formula() == "CoV" assert al1[1].get_chemical_formula() == "CNi" assert al1[2].get_chemical_formula() == "S6" assert al1[3].get_chemical_formula() == "Si8"
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
def test_split(Act): """Tests the split function. """ from matdb.atoms import Atoms, AtomsList 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}) 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}) at3 = Atoms("CNi", positions=[[0, 0, 0], [0.5, 0.5, 0.5]], info={"rand": 8}) at4 = Atoms("CoV", positions=[[0, 0, 0], [0.25, 0.5, 0.25]], info={"rand": 8}) at1.add_param("vasp_energy", 25361.504084423999) at2.add_param("vasp_energy", 25362.504084423999) at3.add_param("vasp_energy", 25363.504084423999) at4.add_param("vasp_energy", 25364.504084423999) al = [at4, at2, at1, at3] splits = {'A': 0.4, 'B': 0.2} splitroot = path.join(Act.root, "split") mkdir(splitroot) file_targets = { "train": Act.database.train_file, "holdout": Act.database.holdout_file, "super": Act.database.super_file } # the split directory should be empty assert len(listdir(splitroot)) == 0 # split with an empty splits split(al, {}, file_targets, splitroot, ran_seed=1, recalc=1) # the split directory should still be empty assert len(listdir(splitroot)) == 0 # split with an empty atom list split([], splits, file_targets, splitroot, ran_seed=1, recalc=1) # the split directory should have 2 entries assert len(listdir(splitroot)) == 2 # split split(al, splits, file_targets, splitroot, ran_seed=1) # now we should have all the splitted files assert path.exists(path.join(splitroot, Act.database.train_file('A'))) assert path.exists(path.join(splitroot, Act.database.holdout_file('A'))) assert path.exists(path.join(splitroot, Act.database.super_file('A'))) assert path.exists(path.join(splitroot, Act.database.train_file('B'))) assert path.exists(path.join(splitroot, Act.database.holdout_file('B'))) assert path.exists(path.join(splitroot, Act.database.super_file('B'))) # the split directory should have 2 entries assert len(listdir(splitroot)) == 2 # split again with recalc split(al, splits, file_targets, splitroot, ran_seed=1, recalc=1) assert glob( path.join(Act.database.root, "splits", "active", "A_*-train.h5")) assert glob( path.join(Act.database.root, "splits", "active", "A_*-holdout.h5")) assert glob( path.join(Act.database.root, "splits", "active", "A_*-super.h5")) assert glob( path.join(Act.database.root, "splits", "active", "B_*-train.h5")) assert glob( path.join(Act.database.root, "splits", "active", "B_*-holdout.h5")) assert glob( path.join(Act.database.root, "splits", "active", "B_*-super.h5")) # the split directory should have 2 entries assert len(listdir(splitroot)) == 2
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)