def test_abipsps(self): """Testing abipsps.py""" if not self.has_matplotlib(): return si_pspnc = abidata.pseudo("14si.pspnc") si_oncv = abidata.pseudo("Si.oncvpsp") env = self.get_env() env.run(self.script, si_pspnc.path, self.loglevel, self.verbose) env.run(self.script, si_pspnc.path, si_oncv.path, self.loglevel, self.verbose, expect_stderr=True)
def test_class(self): pseudo = AbinitPseudoData() pseudo.set_pseudos_from_abinit_input(self.scf_inp) with tempfile.NamedTemporaryFile("wt") as files_file: files_file.write("run.abi\nrun.abo\nin\n\out\ntmp\n") files_file.writelines([ abidata.pseudo("C.oncvpsp").filepath, abidata.pseudo("Ga.oncvpsp").filepath ]) pseudo.set_pseudos_from_files_file(files_file.name, 2)
def test_nc_silicon_gbrv_factory(self): """Testing GBRV work for NC silicon.""" flow = abilab.Flow(workdir=tempfile.mkdtemp()) gbrv_factory = GbrvFactory() extra_abivars = { "mem_test": 0, "fband": 2, "nstep": 100, "paral_kgb": 0, } # Create a tmp pseudo because the flow will add the DOJO_REPORT pseudo = abidata.pseudo("14si.pspnc").as_tmpfile() for struct_type in ("fcc", "bcc"): work = gbrv_factory.relax_and_eos_work(pseudo, struct_type, ecut=3, pawecutdg=None, **extra_abivars) flow.register_work(work) flow.build_and_pickle_dump() isok, results = flow.abivalidate_inputs() assert isok flow.make_scheduler().start() assert flow.all_ok assert pseudo.has_dojo_report flow.rmtree()
def test_nc_silicon_df(self): """Testing df factory for NC silicon.""" flow = abilab.Flow(workdir=tempfile.mkdtemp()) df_factory = DeltaFactory() extra_abivars = { "mem_test": 0, "fband": 2, "nstep": 100, "paral_kgb": 0, } # Create a tmp pseudo because the flow will add the DOJO_REPORT pseudo = abidata.pseudo("14si.pspnc").as_tmpfile() work = df_factory.work_for_pseudo(pseudo, kppa=1, ecut=2, pawecutdg=None, **extra_abivars) flow.register_work(work) flow.build_and_pickle_dump() print("Working in ", flow.workdir) isok, results = flow.abivalidate_inputs() if not isok: print(results) assert isok if not DRY_RUN: flow.make_scheduler().start() assert flow.all_ok assert all(work.finalized for work in flow) assert pseudo.has_dojo_report flow.rmtree()
def itest_deltafactor(fwp, tvars): """Testing the flow used for the computation of the deltafactor.""" # Path of the pseudopotential to test. pseudo = abidata.pseudo("Si.GGA_PBE-JTH-paw.xml").as_tmpfile() flow = abilab.Flow(workdir=fwp.workdir, manager=fwp.manager) # Build the workflow for the computation of the deltafactor. # The workflow will produce a pdf file with the equation of state # and a file deltafactor.txt with the final results in the # outdir directory DELTAFACTOR/work_0/outdir. kppa = 20 # this value is for testing purpose (6570 is the correct one) ecut = 2 pawecutdg = ecut * 2 if pseudo.ispaw else None work = DeltaFactory().work_for_pseudo(pseudo, kppa=kppa, ecut=ecut, pawecutdg=pawecutdg, paral_kgb=tvars.paral_kgb) # Register the workflow. flow.register_work(work) flow.allocate() flow.build_and_pickle_dump() for task in flow[0]: task.start_and_wait() flow.check_status() flow.show_status() assert flow.all_ok assert all(work.finalized for work in flow) results = flow[0].get_results()
def build_flow(options): # Path of the pseudopotential to test. #pseudo = data.pseudo("14si.pspnc") pseudo = data.pseudo("Si.GGA_PBE-JTH-paw.xml") # Working directory (default is the name of the script with '.py' removed and "run_" replaced by "flow_") workdir = options.workdir if not options.workdir: workdir = os.path.basename(__file__).replace(".py", "").replace("run_", "flow_") # Initialize the flow. flow = abilab.Flow(workdir=workdir, manager=options.manager, remove=options.remove) # Build the workflow for the computation of the deltafactor. # The calculation is done with the parameters and the cif files # used in the original paper. We only have to specify # the cutoff energy ecut (Ha) for the pseudopotential. # The workflow will produce a pdf file with the equation of state # and a file deltafactor.txt with the final results in the # outdir directory DELTAFACTOR/work_0/outdir. factory = DeltaFactory() kppa = 6750 # Use this to have the official k-point sampling kppa = 50 # this value is for testing purpose. ecut = 8 pawecutdg = ecut * 2 if pseudo.ispaw else None work = factory.work_for_pseudo(pseudo, accuracy="normal", kppa=kppa, ecut=ecut, pawecutdg=pawecutdg, toldfe=1.e-8, smearing="fermi_dirac:0.0005") # Register the workflow. flow.register_work(work) return flow
def test_lobsterinput(self): """Testing LobsterInput.""" lin = LobsterInput(atom_pairs=[(0, 1), (2, 3)], orbitalwise=True) repr(lin); str(lin) assert lin.to_string(verbose=2) lin.set_basis_functions_from_abinit_pseudos([abidata.pseudo("Al.GGA_PBE-JTH.xml")]) # XML parser does not guarantee order. assert sorted(lin.basis_functions[0].split()) == sorted("Al 3s 3p".split()) lin.write(self.mkdtemp())
def get_pseudos(structure, extension='oncvpsp'): """ returns a list of pseudos names for structure. This list should be fed to abidata.pseudos like abidata.pseudos(get_pseudos(structure)) """ pseudos = [] for element in structure.composition.elements: pseudos.append(abidata.pseudo(str(element) + '.' + extension)) #todo test if the pseudo potential file exists return pseudos
def get_pseudos(structure, extension="oncvpsp"): """ returns a list of pseudos names for structure. This list should be fed to abidata.pseudos like abidata.pseudos(get_pseudos(structure)) """ pseudos = [] for element in structure.composition.elements: pseudos.append(abidata.pseudo(str(element) + "." + extension)) # todo test if the pseudo potential file exists return pseudos
def test_psps_nc_silicon(self): """Very preliminary test for PSPS.nc file with Ga.oncvpsp""" pseudo = abidata.pseudo("Ga.oncvpsp") with pseudo.open_pspsfile(ecut=10) as psps: print(psps) r = psps.reader assert r.usepaw == 0 and r.ntypat == 1 if has_matplotlib(): psps.plot(what="all", show=False) psps.compare(psps, show=False)
def test_psps_nc_silicon(self): """Test PSPS.nc file with Ga.oncvpsp""" pseudo = abidata.pseudo("Ga.oncvpsp") with pseudo.open_pspsfile(ecut=10) as psps: print(psps) r = psps.reader assert r.usepaw == 0 and r.ntypat == 1 if has_matplotlib(): psps.plot(what="all", show=False) psps.compare(psps, show=False)
def itest_gbrv_flow(fwp, tvars): """The the GBRV flow: relaxation + EOS computation.""" factory = GbrvFactory() #pseudo = "si_pbe_v1_abinit.paw" pseudo = abidata.pseudo("Si.GGA_PBE-JTH-paw.xml") ecut = 2 pawecutdg = 2 * ecut if pseudo.ispaw else None flow = abilab.Flow(workdir=fwp.workdir, manager=fwp.manager, pickle_protocol=0) struct_types = ["fcc"] #, "bcc"] for struct_type in struct_types: work = factory.relax_and_eos_work(pseudo, struct_type, ecut, pawecutdg=pawecutdg, paral_kgb=tvars.paral_kgb) flow.register_work(work) flow.allocate() flow.build_and_pickle_dump() fwp.scheduler.add_flow(flow) assert fwp.scheduler.start() == 0 assert not fwp.scheduler.exceptions #work = flow[0] #t0 = work[0] #assert len(work) == 1 #t0.start_and_wait() #flow.check_status() # At this point on_all_ok is called. #assert t0.status == t0.S_OK #assert len(flow) == 2 #assert len(flow[1]) == 9 #assert not flow.all_ok #for task in flow[1]: # task.start_and_wait() flow.check_status() flow.show_status() assert all(work.finalized for work in flow) assert flow.all_ok
def test_helper_functions(self): """Testing AbinitInput helper functions.""" pseudo = abidata.pseudo("14si.pspnc") pseudo_dir = os.path.dirname(pseudo.filepath) inp = AbinitInput(structure=abidata.cif_file("si.cif"), pseudos=pseudo.basename, pseudo_dir=pseudo_dir) nval_atoms = inp.valence_electrons_per_atom assert len(nval_atoms) == 2 assert nval_atoms == [4, 4] inp.set_kmesh(ngkpt=(1, 2, 3), shiftk=(1, 2, 3, 4, 5, 6)) assert inp["kptopt"] == 1 and inp["nshiftk"] == 2 assert inp.uses_ktimereversal inp.set_gamma_sampling() assert inp["kptopt"] == 1 and inp["nshiftk"] == 1 assert np.all(inp["shiftk"] == 0) inp.set_autokmesh(nksmall=2) assert inp["kptopt"] == 1 and np.all(inp["ngkpt"] == [2, 2, 2]) and inp["nshiftk"] == 4 inp.set_kpath(ndivsm=3, kptbounds=None) assert inp["ndivsm"] == 3 and inp["iscf"] == -2 and len(inp["kptbounds"]) == 12 inp.set_qpath(ndivsm=3, qptbounds=None) assert len(inp["ph_qpath"]) == 12 and inp["ph_nqpath"] == 12 and inp["ph_ndivsm"] == 3 inp.set_phdos_qmesh(nqsmall=16, method="tetra") assert inp["ph_intmeth"] == 2 and np.all(inp["ph_ngqpt"] == 16) and np.all(inp["ph_qshift"] == 0) inp.set_kptgw(kptgw=(1, 2, 3, 4, 5, 6), bdgw=(1, 2)) assert inp["nkptgw"] == 2 and np.all(inp["bdgw"].ravel() == np.array(len(inp["kptgw"]) * [1,2]).ravel()) linps = inp.linspace("ecut", 2, 6, num=3, endpoint=True) assert len(linps) == 3 and (linps[0]["ecut"] == 2 and (linps[-1]["ecut"] == 6)) ranginps = inp.arange("ecut", start=3, stop=5, step=1) assert len(ranginps) == 2 and (ranginps[0]["ecut"] == 3 and (ranginps[-1]["ecut"] == 4)) with self.assertRaises(inp.Error): inp.product("ngkpt", "tsmear", [[2, 2, 2], [4, 4, 4]]) prod_inps = inp.product("ngkpt", "tsmear", [[2, 2, 2], [4, 4, 4]], [0.1, 0.2, 0.3]) assert len(prod_inps) == 6 assert prod_inps[0]["ngkpt"] == [2, 2, 2] and prod_inps[0]["tsmear"] == 0.1 assert prod_inps[-1]["ngkpt"] == [4, 4, 4] and prod_inps[-1]["tsmear"] == 0.3 inp["kptopt"] = 4 assert not inp.uses_ktimereversal
def test_expand(self): """ Testing helper function to extend the convergence grid """ self.maxDiff = None spec = get_spec('GW') struc = AbiStructure.from_file(abidata.cif_file("si.cif")) struc.item = 'test' wdir = tempfile.mkdtemp() print('wdir', wdir) os.chdir(wdir) shutil.copyfile(abidata.cif_file("si.cif"), os.path.join(wdir, 'si.cif')) shutil.copyfile(abidata.pseudo("14si.pspnc").path, os.path.join(wdir, 'Si.pspnc')) shutil.copyfile(os.path.join(abidata.dirpath, 'managers', 'shell_manager.yml'), os.path.join(wdir, 'manager.yml')) shutil.copyfile(os.path.join(abidata.dirpath, 'managers', 'scheduler.yml'), os.path.join(wdir, 'scheduler.yml')) try: temp_ABINIT_PS_EXT = os.environ['ABINIT_PS_EXT'] temp_ABINIT_PS = os.environ['ABINIT_PS'] except KeyError: temp_ABINIT_PS_EXT = None temp_ABINIT_PS = None os.environ['ABINIT_PS_EXT'] = '.pspnc' os.environ['ABINIT_PS'] = wdir tests = SingleAbinitGWWork(struc, spec).convs tests_out = {'nscf_nbands': {'test_range': (40,), 'control': 'gap', 'method': 'set_bands', 'level': 'nscf'}, 'ecut': {'test_range': (50, 48, 46, 44), 'control': 'e_ks_max', 'method': 'direct', 'level': 'scf'}, 'ecuteps': {'test_range': (4, 8, 12, 16, 20), 'control': 'gap', 'method': 'direct', 'level': 'sigma'}} self.assertEqual(expand(tests, 1), tests_out) spec.data['code'] = 'VASP' if "VASP_PSP_DIR" in os.environ: spec.update_code_interface() tests = GWG0W0VaspInputSet(struc, spec).convs tests_out = {'ENCUTGW': {'test_range': (200, 400, 600, 800), 'control': 'gap', 'method': 'incar_settings'}} self.assertEqual(expand(tests, 1), tests_out) if temp_ABINIT_PS is not None: os.environ['ABINIT_PS_EXT'] = temp_ABINIT_PS_EXT os.environ['ABINIT_PS'] = temp_ABINIT_PS
def itest_gbrv_flow(fwp, tvars): """The the GBRV flow: relaxation + EOS computation.""" from pseudo_dojo.dojo.dojo_workflows import GbrvFactory factory = GbrvFactory() #pseudo = "si_pbe_v1_abinit.paw" pseudo = abidata.pseudo("Si.GGA_PBE-JTH-paw.xml") ecut = 2 pawecutdg = 2 * ecut if pseudo.ispaw else None flow = abilab.Flow(workdir=fwp.workdir, manager=fwp.manager, pickle_protocol=0) struct_types = ["fcc"] #, "bcc"] for struct_type in struct_types: work = factory.relax_and_eos_work(pseudo, struct_type, ecut, pawecutdg=pawecutdg, paral_kgb=tvars.paral_kgb) flow.register_work(work) flow.allocate() flow.build_and_pickle_dump() fwp.scheduler.add_flow(flow) assert fwp.scheduler.start() assert not fwp.scheduler.exceptions #work = flow[0] #t0 = work[0] #assert len(work) == 1 #t0.start_and_wait() #flow.check_status() # At this point on_all_ok is called. #assert t0.status == t0.S_OK #assert len(flow) == 2 #assert len(flow[1]) == 9 #assert not flow.all_ok #for task in flow[1]: # task.start_and_wait() flow.check_status() flow.show_status() assert all(work.finalized for work in flow) assert flow.all_ok
def test_data_api(self): """Testing abipy.data API.""" ncpseudo_h = abidata.pseudo("01h.pspgth") assert ncpseudo_h.isnc paw_table = abidata.pseudos("1h.paw", "28ni.paw") assert paw_table.allpaw assert os.path.isfile(abidata.cif_file("al.cif")) assert os.path.isfile(abidata.pyscript("plot_bz.py")) d = abidata.get_mp_structures_dict() assert isinstance(d, dict) and d is abidata.get_mp_structures_dict() structure = abidata.structure_from_cif("gan2.cif") assert hasattr(structure, "to_abivars") structure = abidata.structure_from_mpid("mp-4820") assert hasattr(structure, "to_abivars") with self.assertRaises(KeyError): abidata.structure_from_mpid("foobar")
def test_expand(self): """ Testing helper function to extend the convergence grid """ self.maxDiff = None spec = get_spec('GW') struc = AbiStructure.from_file(abidata.cif_file("si.cif")) struc.item = 'test' wdir = tempfile.mkdtemp() print('wdir', wdir) os.chdir(wdir) shutil.copyfile(abidata.cif_file("si.cif"), os.path.join(wdir, 'si.cif')) shutil.copyfile(abidata.pseudo("14si.pspnc").path, os.path.join(wdir, 'Si.pspnc')) shutil.copyfile(os.path.join(abidata.dirpath, 'managers', 'shell_manager.yml'), os.path.join(wdir, 'manager.yml')) shutil.copyfile(os.path.join(abidata.dirpath, 'managers', 'simple_scheduler.yml'), os.path.join(wdir, 'scheduler.yml')) temp_ABINIT_PS_EXT = os.environ.get('ABINIT_PS_EXT', None) temp_ABINIT_PS = os.environ.get('ABINIT_PS', None) os.environ['ABINIT_PS_EXT'] = '.pspnc' os.environ['ABINIT_PS'] = wdir tests = SingleAbinitGWWork(struc, spec).convs tests_out = {'nscf_nbands': {'test_range': (25,), 'control': 'gap', 'method': 'set_bands', 'level': 'nscf'}, 'ecut': {'test_range': (50, 48, 46, 44), 'control': 'e_ks_max', 'method': 'direct', 'level': 'scf'}, 'ecuteps': {'test_range': (4, 6, 8, 10, 12), 'control': 'gap', 'method': 'direct', 'level': 'sigma'}} self.assertEqual(expand(tests, 1), tests_out) spec.data['code'] = 'VASP' if temp_ABINIT_PS is not None: os.environ['ABINIT_PS_EXT'] = temp_ABINIT_PS_EXT os.environ['ABINIT_PS'] = temp_ABINIT_PS
def delta_flow(workdir="tmp_deltafactor"): # Path of the pseudopotential to test. #pseudo = data.pseudo("14si.pspnc") pseudo = data.pseudo("Si.GGA_PBE-JTH-paw.xml") # Manager used to submit the jobs. manager = abilab.TaskManager.from_user_config() # Initialize the flow. # FIXME Abistructure is not pickleable with protocol -1 flow = abilab.AbinitFlow(workdir=workdir, manager=manager, pickle_protocol=0) # Build the workflow for the computation of the deltafactor. # The calculation is done with the parameters and the cif files # used in the original paper. We only have to specify # the cutoff energy ecut (Ha) for the pseudopotential. # The workflow will produce a pdf file with the equation of state # and a file deltafactor.txt with the final results in the # outdir directory DELTAFACTOR/work_0/outdir. factory = DeltaFactory() kppa = 6750 # Use this to have the official k-point sampling kppa = 50 # this value is for testing purpose. #extra = {} ecut = 8 pawecutdg = ecut * 2 #if pseudo.ispaw else None work = factory.work_for_pseudo(pseudo, accuracy="normal", kppa=kppa, ecut=ecut, pawecutdg=pawecutdg, toldfe=1.e-8, smearing="fermi_dirac:0.0005") # Register the workflow. flow.register_work(work) return flow.allocate()
def itest_deltafactor(fwp, tvars): """Test the flow used for the computation of the deltafactor.""" # Path of the pseudopotential to test. pseudo = abidata.pseudo("Si.GGA_PBE-JTH-paw.xml") flow = abilab.Flow(workdir=fwp.workdir, manager=fwp.manager) # Build the workflow for the computation of the deltafactor. # The workflow will produce a pdf file with the equation of state # and a file deltafactor.txt with the final results in the # outdir directory DELTAFACTOR/work_0/outdir. kppa = 20 # this value is for testing purpose (6570 is the correct one) ecut = 2 pawecutdg = ecut * 2 if pseudo.ispaw else None work = DeltaFactory().work_for_pseudo(pseudo, kppa=kppa, ecut=ecut, pawecutdg=pawecutdg, paral_kgb=tvars.paral_kgb) # Register the workflow. flow.register_work(work) flow.allocate() flow.build_and_pickle_dump() for task in flow[0]: task.start_and_wait() flow.check_status() flow.show_status() assert flow.all_ok assert all(work.finalized for work in flow) results = flow[0].get_results()
def test_api(self): """Testing MultiDataset API.""" structure = abilab.Structure.from_file(abidata.cif_file("si.cif")) pseudo = abidata.pseudo("14si.pspnc") pseudo_dir = os.path.dirname(pseudo.filepath) multi = MultiDataset(structure=structure, pseudos=pseudo) with self.assertRaises(ValueError): MultiDataset(structure=structure, pseudos=pseudo, ndtset=-1) multi = MultiDataset(structure=structure, pseudos=pseudo.basename, pseudo_dir=pseudo_dir) assert len(multi) == 1 and multi.ndtset == 1 assert multi.isnc for i, inp in enumerate(multi): assert list(inp.keys()) == list(multi[i].keys()) multi.addnew_from(0) assert multi.ndtset == 2 and multi[0] is not multi[1] assert multi[0].structure == multi[1].structure assert multi[0].structure is not multi[1].structure multi.set_vars(ecut=2) assert all(inp["ecut"] == 2 for inp in multi) self.assert_equal(multi.get("ecut"), [2, 2]) df = multi.get_vars_dataframe("ecut", "foobar") assert "ecut" in df self.assert_equal(df["ecut"].values, [2, 2]) multi[1].set_vars(ecut=1) assert multi[0]["ecut"] == 2 and multi[1]["ecut"] == 1 self.assert_equal(multi.get("ecut"), [2, 1]) self.assert_equal(multi.get("foo", "default"), ["default", "default"]) multi[1].set_vars(paral_kgb=1) assert "paral_kgb" not in multi[0] self.assert_equal(multi.get("paral_kgb"), [None, 1]) pert_structure = structure.copy() pert_structure.perturb(distance=0.1) assert structure != pert_structure assert multi.set_structure(structure) == multi.ndtset * [structure] assert all(s == structure for s in multi.structure) assert multi.has_same_structures multi[1].set_structure(pert_structure) assert multi[0].structure != multi[1].structure and multi[ 1].structure == pert_structure assert not multi.has_same_structures split = multi.split_datasets() assert len(split) == 2 and all(split[i] == multi[i] for i in range(multi.ndtset)) repr(multi) str(multi) assert multi.to_string(mode="text") assert multi._repr_html_() inp.write(filepath=self.tmpfileindir("run.abi")) multi.write(filepath=self.tmpfileindir("run.abi")) new_multi = MultiDataset.from_inputs([inp for inp in multi]) assert new_multi.ndtset == multi.ndtset assert new_multi.structure == multi.structure for old_inp, new_inp in zip(multi, new_multi): assert old_inp is not new_inp self.assertDictEqual(old_inp.as_dict(), new_inp.as_dict()) ref_input = multi[0] new_multi = MultiDataset.replicate_input(input=ref_input, ndtset=4) assert new_multi.ndtset == 4 for inp in new_multi: assert ref_input is not inp self.assertDictEqual(ref_input.as_dict(), inp.as_dict()) # Compatible with Pickle and MSONable? self.serialize_with_pickle(multi, test_eq=False) #self.assertMSONable(multi) # Test tags new_multi.add_tags([GROUND_STATE, RELAX], [0, 2]) assert len(new_multi[0].tags) == 2 sub_multi = new_multi.filter_by_tags(GROUND_STATE) assert len(sub_multi) == 2 new_multi.remove_tags([RELAX]) assert len(new_multi[0].tags) == 1
def test_helper_functions(self): """Testing AbinitInput helper functions.""" pseudo = abidata.pseudo("14si.pspnc") pseudo_dir = os.path.dirname(pseudo.filepath) inp = AbinitInput(structure=abidata.cif_file("si.cif"), pseudos=pseudo.basename, pseudo_dir=pseudo_dir) nval_atoms = inp.valence_electrons_per_atom assert len(nval_atoms) == 2 assert nval_atoms == [4, 4] inp.set_kmesh(ngkpt=(1, 2, 3), shiftk=(1, 2, 3, 4, 5, 6)) assert inp["kptopt"] == 1 and inp["nshiftk"] == 2 assert inp.uses_ktimereversal inp.set_gamma_sampling() assert inp["kptopt"] == 1 and inp["nshiftk"] == 1 assert np.all(inp["shiftk"] == 0) inp.set_autokmesh(nksmall=2) assert inp["kptopt"] == 1 and np.all( inp["ngkpt"] == [2, 2, 2]) and inp["nshiftk"] == 4 inp.set_kpath(ndivsm=3, kptbounds=None) assert inp["ndivsm"] == 3 and inp["iscf"] == -2 and len( inp["kptbounds"]) == 12 inp.set_qpath(ndivsm=3, qptbounds=None) assert len(inp["ph_qpath"] ) == 12 and inp["ph_nqpath"] == 12 and inp["ph_ndivsm"] == 3 inp.set_phdos_qmesh(nqsmall=16, method="tetra") assert inp["ph_intmeth"] == 2 and np.all( inp["ph_ngqpt"] == 16) and np.all(inp["ph_qshift"] == 0) inp.set_kptgw(kptgw=(1, 2, 3, 4, 5, 6), bdgw=(1, 2)) assert inp["nkptgw"] == 2 and np.all( inp["bdgw"].ravel() == np.array(len(inp["kptgw"]) * [1, 2]).ravel()) linps = inp.linspace("ecut", 2, 6, num=3, endpoint=True) assert len(linps) == 3 and (linps[0]["ecut"] == 2 and (linps[-1]["ecut"] == 6)) ranginps = inp.arange("ecut", start=3, stop=5, step=1) assert len(ranginps) == 2 and (ranginps[0]["ecut"] == 3 and (ranginps[-1]["ecut"] == 4)) with self.assertRaises(inp.Error): inp.product("ngkpt", "tsmear", [[2, 2, 2], [4, 4, 4]]) prod_inps = inp.product("ngkpt", "tsmear", [[2, 2, 2], [4, 4, 4]], [0.1, 0.2, 0.3]) assert len(prod_inps) == 6 assert prod_inps[0]["ngkpt"] == [2, 2, 2 ] and prod_inps[0]["tsmear"] == 0.1 assert prod_inps[-1]["ngkpt"] == [4, 4, 4 ] and prod_inps[-1]["tsmear"] == 0.3 inp["kptopt"] = 4 assert not inp.uses_ktimereversal
def test_setup(self): """ Testing the main functions called in the abiGWsetup script """ spec_in = get_spec('GW') self.assertIsInstance(spec_in, GWSpecs) self.assert_equal(spec_in.test(), 0) self.assert_equal(len(spec_in.to_dict()), 10) self.assert_equal(spec_in.get_code(), 'ABINIT') spec_in.data['source'] = 'cif' self.assert_equal( spec_in.hash_str(), "code:ABINIT;source:cifjobs:[u'prep', u'G0W0'];mode:ceci;functional:PBE;kp_grid_dens:500prec:m;tol:0.0001;test:False;converge:False" ) wdir = tempfile.mkdtemp() base = os.getcwd() print('wdir', wdir) os.chdir(wdir) shutil.copyfile(abidata.cif_file("si.cif"), os.path.join(wdir, 'si.cif')) shutil.copyfile( abidata.pseudo("14si.pspnc").path, os.path.join(wdir, 'Si.pspnc')) shutil.copyfile( os.path.join(abidata.dirpath, 'managers', 'shell_manager.yml'), os.path.join(wdir, 'manager.yml')) shutil.copyfile( os.path.join(abidata.dirpath, 'managers', 'scheduler.yml'), os.path.join(wdir, 'scheduler.yml')) try: temp_ABINIT_PS_EXT = os.environ['ABINIT_PS_EXT'] temp_ABINIT_PS = os.environ['ABINIT_PS'] except KeyError: temp_ABINIT_PS_EXT = None temp_ABINIT_PS = None os.environ['ABINIT_PS_EXT'] = '.pspnc' os.environ['ABINIT_PS'] = wdir spec_in.data['source'] = 'cif' print('dirpath', abidata.dirpath) spec_in.write_to_file('spec.in') self.assertTrue(os.path.isfile(os.path.join(wdir, 'spec.in'))) # broken due to strategy refactoring # spec_in.loop_structures('i') os.chdir(base) shutil.rmtree(wdir) if temp_ABINIT_PS is not None: os.environ['ABINIT_PS_EXT'] = temp_ABINIT_PS_EXT os.environ['ABINIT_PS'] = temp_ABINIT_PS
def test_setup_single(self): """ Testing the main functions called in the abiGWsetup script for a single shot calculation """ spec_in = get_spec('GW') self.assertIsInstance(spec_in, GWSpecs) self.assert_equal(spec_in.test(), 0) self.assert_equal(len(spec_in.to_dict()), 10) self.assert_equal(spec_in.get_code(), 'ABINIT') spec_in.data['source'] = 'cif' self.assert_equal(spec_in.hash_str(), "code:ABINIT;source:cifjobs:['prep', 'G0W0'];mode:ceci;functional:PBE;kp_grid_dens:500prec:m;tol:0.0001;test:False;converge:False") wdir = tempfile.mkdtemp() print('wdir', wdir) os.chdir(wdir) shutil.copyfile(abidata.cif_file("si.cif"), os.path.join(wdir, 'si.cif')) shutil.copyfile(abidata.pseudo("14si.pspnc").path, os.path.join(wdir, 'Si.pspnc')) shutil.copyfile(os.path.join(abidata.dirpath, 'managers', 'shell_manager.yml'), os.path.join(wdir, 'manager.yml')) shutil.copyfile(os.path.join(abidata.dirpath, 'managers', 'simple_scheduler.yml'), os.path.join(wdir, 'scheduler.yml')) temp_ABINIT_PS_EXT = os.environ.get('ABINIT_PS_EXT', None) temp_ABINIT_PS = os.environ.get('ABINIT_PS', None) os.environ['ABINIT_PS_EXT'] = '.pspnc' os.environ['ABINIT_PS'] = wdir spec_in.data['source'] = 'cif' print('dirpath', abidata.dirpath) spec_in.write_to_file('spec.in') self.assertTrue(os.path.isfile(os.path.join(wdir, 'spec.in'))) spec_in.loop_structures('i') if temp_ABINIT_PS is not None: os.environ['ABINIT_PS_EXT'] = temp_ABINIT_PS_EXT os.environ['ABINIT_PS'] = temp_ABINIT_PS ls = os.listdir('.') print(ls) self.assertEqual(ls, [u'job_collection', u'manager.yml', u'scheduler.yml', u'si.cif', u'Si.pspnc', u'Si_si.cif', u'spec.in']) ls2 = os.listdir('Si_si.cif/') print(ls2) self.assertEqual(ls2, [u'.nodeid', u'__AbinitFlow__.pickle', u'indata', u'outdata', u'tmpdata', u'w0']) ls3 = os.listdir('Si_si.cif/w0/') print(ls3) self.assertEqual(ls3, [u'indata', u'outdata', u't0', u't1', u't2', u't3', u'tmpdata']) with open('Si_si.cif/w0/t0/run.abi') as f: abi = f.readlines() for x in [' ecut 44\n', ' tolvrs 1e-08\n', ' kptopt 1\n', ' ngkpt 6 6 6\n', ' nshiftk 1\n', ' nband 26\n']: self.assertIn(x, abi) with open('Si_si.cif/w0/t1/run.abi') as f: abi = f.readlines() for x in [' ecut 44\n', ' tolwfr 1e-17\n', ' kptopt 1\n', ' iscf -2\n']: self.assertIn(x, abi) with open('Si_si.cif/w0/t2/run.abi') as f: abi = f.readlines() for x in [' kptopt 1\n', ' shiftk 0 0 0\n', ' ngkpt 6 6 6\n', ' nshiftk 1\n', ' iscf -2\n', ' nband 60\n', ' nsppol 1\n', ' ecut 44\n', ' toldfe 1e-08\n',' gwmem 10\n', ' inclvkb 2\n', '\n', ' symsigma 1\n', ' ecuteps 8\n']: self.assertIn(x, abi) with open('Si_si.cif/w0/t3/run.abi') as f: abi = f.readlines() for x in [' kptopt 1\n', ' shiftk 0 0 0\n', ' ngkpt 6 6 6\n', ' nshiftk 1\n', ' iscf -2\n', ' nband 60\n', ' nsppol 1\n', ' ecut 44\n', ' toldfe 1e-08\n', ' gwmem 10\n', ' inclvkb 2\n', '\n', ' symsigma 1\n', ' ecutsigx 44\n', ' ecuteps 8\n', ' gw_qprange 2\n', ' ppmodel 1\n', ' gwcalctyp 00\n']: self.assertIn(x, abi)
def test_SingleAbinitGWWork(self): """ Testing the concrete SingelAbinitGWWork class """ struc = AbiStructure.from_file(abidata.cif_file("si.cif")) struc.item = 'test' wdir = tempfile.mkdtemp() #wdir = '.' shutil.copyfile(abidata.cif_file("si.cif"), os.path.join(wdir, 'si.cif')) shutil.copyfile( abidata.pseudo("14si.pspnc").path, os.path.join(wdir, 'Si.pspnc')) shutil.copyfile( os.path.join(abidata.dirpath, 'managers', 'shell_manager_nompi.yml'), os.path.join(wdir, 'manager.yml')) shutil.copyfile( os.path.join(abidata.dirpath, 'managers', 'scheduler.yml'), os.path.join(wdir, 'scheduler.yml')) try: temp_ABINIT_PS_EXT = os.environ['ABINIT_PS_EXT'] temp_ABINIT_PS = os.environ['ABINIT_PS'] except KeyError: temp_ABINIT_PS_EXT = None temp_ABINIT_PS = None os.environ['ABINIT_PS_EXT'] = '.pspnc' os.environ['ABINIT_PS'] = wdir self.assertIsInstance(struc, AbiStructure) spec = get_spec('GW') spec.data['kp_grid_dens'] = 100 spec.data['kp_in'] = -100 with open(os.path.join(wdir, 'extra_abivars'), 'w') as f: f.write('{"ecut": 8, "ecutsigx": 8}') work = SingleAbinitGWWork(struc, spec) self.assertEqual(len(work.CONVS), 3) conv_strings = ['method', 'control', 'level'] for test in work.CONVS: self.assertIsInstance(work.CONVS[test]['test_range'], tuple) for item in conv_strings: self.assertIsInstance(work.CONVS[test][item], unicode) self.assertEqual(work.work_dir, 'Si_test') self.assertEqual(len(work.pseudo_table), 1) self.assertEqual(work.bands_fac, 1) self.assertEqual(work.get_electrons(struc), 8) self.assertEqual(work.get_bands(struc), 6) self.assertGreater(work.get_bands(struc), work.get_electrons(struc) / 2, 'More electrons than bands, very bad.') flow = work.create() print(work.work_dir) print(flow.workdir) print(flow[0].workdir) self.assertIsInstance(flow, Flow) self.assertEqual(len(flow), 1) # one work self.assertEqual(len(flow[0]), 4) # with 4 tasks # self.assertEqual(flow.workdir, 'Si') self.assertEqual(flow.build_and_pickle_dump(), 0) # some more tests flow.rmtree() spec = get_spec('GW') spec.data['converge'] = True struc.item = 'converge' work = SingleAbinitGWWork(struc, spec) flow = work.create() self.assertEqual(len(flow[0]), 45) self.assertEqual(flow[0][0].__class__, ScfTask) self.assertEqual(flow[0][1].__class__, ScfTask) self.assertEqual(flow[0][2].__class__, ScfTask) self.assertEqual(flow[0][3].__class__, ScfTask) self.assertEqual(flow[0][4].__class__, NscfTask) self.assertEqual(flow[0][5].__class__, ScrTask) self.assertEqual(flow[0][6].__class__, SigmaTask) ecuts = [ dict(task.input.as_dict()['abi_args'])['ecut'] for task in flow[0] ] print('ecuts:', ecuts) # it is essential that the first four are diffent, this is for the convergence study of ecut, and that # after that is stays the same self.assertEqual(ecuts, [ 50, 48, 46, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44 ]) nbands = [ dict(task.input.as_dict()['abi_args'])['nband'] for task in flow[0] ] print('nbands:', nbands) # the firs 4 should be 'low' these are self consistent # the fifth should be the maximum of what follows # the 6th and on should always be pairs that are the same, they are combinations of scr and sigma tasks self.assertEqual(nbands, [ 26, 26, 26, 26, 180, 30, 30, 60, 60, 120, 120, 180, 180, 30, 30, 60, 60, 120, 120, 180, 180, 30, 30, 60, 60, 120, 120, 180, 180, 30, 30, 60, 60, 120, 120, 180, 180, 30, 30, 60, 60, 120, 120, 180, 180 ]) ecuteps = [ dict(task.input.as_dict()['abi_args']).get('ecuteps', None) for task in flow[0] ] print('ecuteps:', ecuteps) self.assertEqual(ecuteps, [ None, None, None, None, None, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 20, 20, 20, 20 ]) inplens = [len(task.input.as_dict()['abi_args']) for task in flow[0]] print(inplens) self.assertEqual(inplens, [ 17, 17, 17, 17, 18, 27, 30, 27, 30, 27, 30, 27, 30, 27, 30, 27, 30, 27, 30, 27, 30, 27, 30, 27, 30, 27, 30, 27, 30, 27, 30, 27, 30, 27, 30, 27, 30, 27, 30, 27, 30, 27, 30, 27, 30 ]) ngkpts = [ dict(task.input.as_dict()['abi_args'])['ngkpt'] for task in flow[0] ] for ngkpt in ngkpts: self.assertEqual(ngkpt, [2, 2, 2]) self.assertEqual(flow.build_and_pickle_dump(), 0) # some more tests flow.rmtree() if temp_ABINIT_PS is not None: os.environ['ABINIT_PS_EXT'] = temp_ABINIT_PS_EXT os.environ['ABINIT_PS'] = temp_ABINIT_PS
def test_SingleAbinitGWWork(self): """ Testing the concrete SingelAbinitGWWork class """ struc = AbiStructure.from_file(abidata.cif_file("si.cif")) struc.item = 'test' wdir = '.' shutil.copyfile(abidata.cif_file("si.cif"), os.path.join(wdir, 'si.cif')) shutil.copyfile(abidata.pseudo("14si.pspnc").path, os.path.join(wdir, 'Si.pspnc')) shutil.copyfile(os.path.join(abidata.dirpath, 'managers', 'shell_manager_nompi.yml'), os.path.join(wdir, 'manager.yml')) shutil.copyfile(os.path.join(abidata.dirpath, 'managers', 'scheduler.yml'), os.path.join(wdir, 'scheduler.yml')) try: temp_ABINIT_PS_EXT = os.environ['ABINIT_PS_EXT'] temp_ABINIT_PS = os.environ['ABINIT_PS'] except KeyError: temp_ABINIT_PS_EXT = None temp_ABINIT_PS = None os.environ['ABINIT_PS_EXT'] = '.pspnc' os.environ['ABINIT_PS'] = wdir self.assertIsInstance(struc, AbiStructure) spec = get_spec('GW') spec.data['kp_grid_dens'] = 100 spec.data['kp_in'] = -100 with open('extra_abivars', 'w') as f: f.write('{"ecut": 8, "ecutsigx": 8}') work = SingleAbinitGWWork(struc, spec) self.assertEqual(len(work.CONVS), 3) conv_strings = ['method', 'control', 'level'] for test in work.CONVS: self.assertIsInstance(work.CONVS[test]['test_range'], tuple) for item in conv_strings: self.assertIsInstance(work.CONVS[test][item], unicode) self.assertEqual(work.work_dir, 'Si_test') self.assertEqual(len(work.pseudo_table), 1) self.assertEqual(work.bands_fac, 1) self.assertEqual(work.get_electrons(struc), 8) self.assertEqual(work.get_bands(struc), 6) self.assertGreater(work.get_bands(struc), work.get_electrons(struc) / 2, 'More electrons than bands, very bad.') flow = work.create() print(work.work_dir) print(flow.workdir) print(flow[0].workdir) self.assertIsInstance(flow, Flow) self.assertEqual(len(flow), 1) # one work self.assertEqual(len(flow[0]), 4) # with 4 tasks # self.assertEqual(flow.workdir, 'Si') self.assertEqual(flow.build_and_pickle_dump(), 0) # some more tests flow.rmtree() spec = get_spec('GW') spec.data['converge'] = True struc.item = 'converge' work = SingleAbinitGWWork(struc, spec) flow = work.create() self.assertEqual(len(flow[0]), 45) self.assertEqual(flow.build_and_pickle_dump(), 0) # some more tests flow.rmtree() if temp_ABINIT_PS is not None: os.environ['ABINIT_PS_EXT'] = temp_ABINIT_PS_EXT os.environ['ABINIT_PS'] = temp_ABINIT_PS
def test_expand(self): """ Testing helper function to extend the convergence grid """ self.maxDiff = None spec = get_spec('GW') struc = AbiStructure.from_file(abidata.cif_file("si.cif")) struc.item = 'test' wdir = tempfile.mkdtemp() print('wdir', wdir) os.chdir(wdir) shutil.copyfile(abidata.cif_file("si.cif"), os.path.join(wdir, 'si.cif')) shutil.copyfile( abidata.pseudo("14si.pspnc").path, os.path.join(wdir, 'Si.pspnc')) shutil.copyfile( os.path.join(abidata.dirpath, 'managers', 'shell_manager.yml'), os.path.join(wdir, 'manager.yml')) shutil.copyfile( os.path.join(abidata.dirpath, 'managers', 'scheduler.yml'), os.path.join(wdir, 'scheduler.yml')) try: temp_ABINIT_PS_EXT = os.environ['ABINIT_PS_EXT'] temp_ABINIT_PS = os.environ['ABINIT_PS'] except KeyError: temp_ABINIT_PS_EXT = None temp_ABINIT_PS = None os.environ['ABINIT_PS_EXT'] = '.pspnc' os.environ['ABINIT_PS'] = wdir tests = SingleAbinitGWWork(struc, spec).convs tests_out = { 'nscf_nbands': { 'test_range': (40, ), 'control': 'gap', 'method': 'set_bands', 'level': 'nscf' }, 'ecut': { 'test_range': (50, 48, 46, 44), 'control': 'e_ks_max', 'method': 'direct', 'level': 'scf' }, 'ecuteps': { 'test_range': (4, 8, 12, 16, 20), 'control': 'gap', 'method': 'direct', 'level': 'sigma' } } self.assertEqual(expand(tests, 1), tests_out) spec.data['code'] = 'VASP' if "VASP_PSP_DIR" in os.environ: spec.update_code_interface() tests = GWG0W0VaspInputSet(struc, spec).convs tests_out = { 'ENCUTGW': { 'test_range': (200, 400, 600, 800), 'control': 'gap', 'method': 'incar_settings' } } self.assertEqual(expand(tests, 1), tests_out) if temp_ABINIT_PS is not None: os.environ['ABINIT_PS_EXT'] = temp_ABINIT_PS_EXT os.environ['ABINIT_PS'] = temp_ABINIT_PS
def test_api(self): """Testing MultiDataset API.""" structure = abilab.Structure.from_file(abidata.cif_file("si.cif")) pseudo = abidata.pseudo("14si.pspnc") pseudo_dir = os.path.dirname(pseudo.filepath) multi = MultiDataset(structure=structure, pseudos=pseudo) with self.assertRaises(ValueError): MultiDataset(structure=structure, pseudos=pseudo, ndtset=-1) multi = MultiDataset(structure=structure, pseudos=pseudo.basename, pseudo_dir=pseudo_dir) assert len(multi) == 1 and multi.ndtset == 1 assert multi.isnc for i, inp in enumerate(multi): assert list(inp.keys()) == list(multi[i].keys()) multi.addnew_from(0) assert multi.ndtset == 2 and multi[0] is not multi[1] assert multi[0].structure == multi[1].structure assert multi[0].structure is not multi[1].structure multi.set_vars(ecut=2) assert all(inp["ecut"] == 2 for inp in multi) self.assert_equal(multi.get("ecut"), [2, 2]) df = multi.get_vars_dataframe("ecut", "foobar") assert "ecut" in df self.assert_equal(df["ecut"].values, [2, 2]) multi[1].set_vars(ecut=1) assert multi[0]["ecut"] == 2 and multi[1]["ecut"] == 1 self.assert_equal(multi.get("ecut"), [2, 1]) self.assert_equal(multi.get("foo", "default"), ["default", "default"]) multi[1].set_vars(paral_kgb=1) assert "paral_kgb" not in multi[0] self.assert_equal(multi.get("paral_kgb"), [None, 1]) pert_structure = structure.copy() pert_structure.perturb(distance=0.1) assert structure != pert_structure assert multi.set_structure(structure) == multi.ndtset * [structure] assert all(s == structure for s in multi.structure) assert multi.has_same_structures multi[1].set_structure(pert_structure) assert multi[0].structure != multi[1].structure and multi[1].structure == pert_structure assert not multi.has_same_structures split = multi.split_datasets() assert len(split) == 2 and all(split[i] == multi[i] for i in range(multi.ndtset)) repr(multi); str(multi) assert multi.to_string(mode="text") assert multi._repr_html_() inp.write(filepath=self.tmpfileindir("run.abi")) multi.write(filepath=self.tmpfileindir("run.abi")) new_multi = MultiDataset.from_inputs([inp for inp in multi]) assert new_multi.ndtset == multi.ndtset assert new_multi.structure == multi.structure for old_inp, new_inp in zip(multi, new_multi): assert old_inp is not new_inp self.assertDictEqual(old_inp.as_dict(), new_inp.as_dict()) ref_input = multi[0] new_multi = MultiDataset.replicate_input(input=ref_input, ndtset=4) assert new_multi.ndtset == 4 for inp in new_multi: assert ref_input is not inp self.assertDictEqual(ref_input.as_dict(), inp.as_dict()) # Compatible with Pickle and MSONable? self.serialize_with_pickle(multi, test_eq=False) #self.assertMSONable(multi) # Test tags new_multi.add_tags([GROUND_STATE, RELAX], [0,2]) assert len(new_multi[0].tags) == 2 sub_multi = new_multi.filter_by_tags(GROUND_STATE) assert len(sub_multi) == 2 new_multi.remove_tags([RELAX]) assert len(new_multi[0].tags) == 1
def pseudos(self): return [abidata.pseudo("14si.pspnc")]
def test_SingleAbinitGWWork(self): """ Testing the concrete SingelAbinitGWWork class """ struc = AbiStructure.from_file(abidata.cif_file("si.cif")) struc.item = 'test' wdir = tempfile.mkdtemp() #wdir = '.' shutil.copyfile(abidata.cif_file("si.cif"), os.path.join(wdir, 'si.cif')) shutil.copyfile(abidata.pseudo("14si.pspnc").path, os.path.join(wdir, 'Si.pspnc')) shutil.copyfile(os.path.join(abidata.dirpath, 'managers', 'shell_manager_nompi.yml'), os.path.join(wdir, 'manager.yml')) shutil.copyfile(os.path.join(abidata.dirpath, 'managers', 'scheduler.yml'), os.path.join(wdir, 'scheduler.yml')) try: temp_ABINIT_PS_EXT = os.environ['ABINIT_PS_EXT'] temp_ABINIT_PS = os.environ['ABINIT_PS'] except KeyError: temp_ABINIT_PS_EXT = None temp_ABINIT_PS = None os.environ['ABINIT_PS_EXT'] = '.pspnc' os.environ['ABINIT_PS'] = wdir self.assertIsInstance(struc, AbiStructure) spec = get_spec('GW') spec.data['kp_grid_dens'] = 100 spec.data['kp_in'] = -100 with open(os.path.join(wdir, 'extra_abivars'), 'w') as f: f.write('{"ecut": 8, "ecutsigx": 8}') work = SingleAbinitGWWork(struc, spec) self.assertEqual(len(work.CONVS), 3) conv_strings = ['method', 'control', 'level'] for test in work.CONVS: self.assertIsInstance(work.CONVS[test]['test_range'], tuple) for item in conv_strings: self.assertIsInstance(work.CONVS[test][item], unicode) self.assertEqual(work.work_dir, 'Si_test') self.assertEqual(len(work.pseudo_table), 1) self.assertEqual(work.bands_fac, 1) self.assertEqual(work.get_electrons(struc), 8) self.assertEqual(work.get_bands(struc), 6) self.assertGreater(work.get_bands(struc), work.get_electrons(struc) / 2, 'More electrons than bands, very bad.') flow = work.create() print(work.work_dir) print(flow.workdir) print(flow[0].workdir) self.assertIsInstance(flow, Flow) self.assertEqual(len(flow), 1) # one work self.assertEqual(len(flow[0]), 4) # with 4 tasks # self.assertEqual(flow.workdir, 'Si') self.assertEqual(flow.build_and_pickle_dump(), 0) # some more tests flow.rmtree() spec = get_spec('GW') spec.data['converge'] = True struc.item = 'converge' work = SingleAbinitGWWork(struc, spec) flow = work.create() self.assertEqual(len(flow[0]), 45) self.assertEqual(flow.build_and_pickle_dump(), 0) # some more tests flow.rmtree() if temp_ABINIT_PS is not None: os.environ['ABINIT_PS_EXT'] = temp_ABINIT_PS_EXT os.environ['ABINIT_PS'] = temp_ABINIT_PS