def test_relax_single(self): sb = StructureBuilder() atoms, *_ = sb.get_structure("Si", "diamond") print(atoms) m_calc = MaterCalc(atoms=atoms, base_dir="../../tmp/Si-class/") res = m_calc.relax(fmax=0.002) # Very tight limit! self.assertTrue(res)
def test_bs(self): sb = StructureBuilder() atoms, *_ = sb.get_structure("C", "diamond") # print(atoms) base_dir = os.path.join(os.path.dirname(__file__), "../../tmp/C-class/") m_calc = MaterCalc(atoms=atoms, base_dir=base_dir) self.assertTrue(m_calc.relax(fmax=0.002)) # Very tight limit! self.assertTrue(m_calc.ground_state()) # get the PBE BS lattice_type = get_cellinfo(m_calc.atoms.cell).lattice self.assertTrue(lattice_type in special_paths.keys()) kpts_bs = dict(path=special_paths[lattice_type], npoints=120) # HSE06 base generate gs_file = os.path.join(base_dir, "gs.gpw") _calc = GPAW(restart=gs_file) atoms = _calc.atoms.copy() calc = GPAW(**_calc.parameters) calc.set(kpts=dict(gamma=True, density=4)) # low density calculations calc.atoms = atoms del _calc calc.get_potential_energy() calc.write(os.path.join(base_dir, "hse.gpw"), mode="all") calc = GPAW(restart=os.path.join(base_dir, "hse.gpw"), txt=None) ns = calc.get_number_of_spins() nk = len(calc.get_ibz_k_points()) nbands = calc.get_number_of_bands() eigen_pbe = numpy.array([[calc.get_eigenvalues(spin=s, kpt=k) \ for k in range(nk)]\ for s in range(ns)]) parprint("EIGEN_PBE", eigen_pbe.shape) vxc_pbe = vxc(calc, "PBE") parprint("VXC_PBE", vxc_pbe.shape) # world.barrier() # HSE06 now calc_hse = EXX(os.path.join(base_dir, "hse.gpw"), xc="HSE06", bands=[0, nbands]) calc_hse.calculate() vxc_hse = calc_hse.get_eigenvalue_contributions() parprint(vxc_hse.shape) parprint(vxc_hse) eigen_hse = eigen_pbe - vxc_pbe + vxc_hse # HSE bandgap from just kpts bg_hse_min, *_ = bandgap(eigenvalues=eigen_hse, efermi=calc.get_fermi_level(), direct=False) bg_hse_dir, *_ = bandgap(eigenvalues=eigen_hse, efermi=calc.get_fermi_level(), direct=True) parprint("HSE: E_min \t E_dir") parprint("{:.3f}\t{:.3f}".format(bg_hse_min, bg_hse_dir)) """
def test_single(self): sb = StructureBuilder() atoms, *_ = sb.get_structure("Si", "diamond") base_dir = os.path.join(os.path.dirname(__file__), "../../tmp/Si-eps-test/") m_calc = MaterCalc(atoms=atoms, base_dir=base_dir) # PBE parprint(m_calc.check_status())
def test_gs_single(self): sb = StructureBuilder() atoms, *_ = sb.get_structure("Si", "diamond") print(atoms) m_calc = MaterCalc(atoms=atoms, base_dir="../../tmp/Si-class/") self.assertTrue(m_calc.relax(fmax=0.002)) self.assertTrue(m_calc.ground_state())
def test_index(self): """Materials from index? """ # Are indexes from 2 instances the same? sb = StructureBuilder() n = sb.count maters = sb.from_index(range(n)) failed = [m for m in maters if False in m.pbc] self.assertEqual(len(failed), 0)
def test_index(self): """Are indexes from 2 instances the same? """ sb1 = StructureBuilder() sb2 = StructureBuilder() len1 = sb1.count len2 = sb2.count self.assertEqual(len1, len2) diff = [i for i in range(len1) \ if sb1.entries[i] != sb2.entries[i]] # [print(sb1.entries[i], sb2.entries[i]) for i in range(len1)] self.assertEqual(len(diff), 0)
def test_build_entries(self): """Get all entries from StructureBuilder """ sb = StructureBuilder() entries = sb.entries # [print(l) for l in entries] self.assertGreater(len(entries), 0)
def test_simple_build(self): """Generate all materials within the entries """ sb = StructureBuilder() entries = sb.entries failed = [] for e in entries: try: mater = sb._convert_struct(e) except Exception as err: failed.append((e["formula"], e["prototype"], err)) if len(failed) > 0: parprint(len(failed), failed) else: parprint("All structure passed!") self.assertEqual(len(failed), 0)
def test_single(self): sb = StructureBuilder() atoms, *_ = sb.get_structure("Si", "diamond") base_dir = os.path.join(os.path.dirname(__file__), "../../tmp/Si-eps-test/") m_calc = MaterCalc(atoms=atoms, base_dir=base_dir) self.assertTrue(m_calc.relax(fmax=0.002)) self.assertTrue(m_calc.ground_state()) # PBE Eg_min, Eg_dir, bs = m_calc.bandgap(method="PBE") parprint(Eg_min, Eg_dir) self.assertLess(abs(Eg_min - 0.61), 0.15) Eg_min, Eg_dir, bs = m_calc.bandgap(method="GLLB") parprint(Eg_min, Eg_dir) self.assertLess(abs(Eg_min - 1.1), 0.15) self.assertTrue(m_calc.excited_state()) self.assertTrue(m_calc.dielectric())
def test_relax_single(self): sb = StructureBuilder() atoms, *_ = sb.get_structure("C", "diamond") base_dir = os.path.join(os.path.dirname(__file__), "../../tmp/C-module-test/") m_calc = MaterCalc(atoms=atoms, base_dir=base_dir) self.assertTrue(m_calc.relax(fmax=0.002)) self.assertTrue(m_calc.ground_state()) # PBE Eg_min, Eg_dir, bs = m_calc.bandgap("PBE") Eg_gllb_min, Eg_gllb_dir, bs = m_calc.bandgap("GLLB") with open("PBE-gap.npz", "wb") as f: numpy.savez(f, Eg_min=Eg_min, Eg_dir=Eg_dir, **bs) parprint("{:.3f}\t{:.3f}".format(Eg_min, Eg_dir)) parprint("PBE Gap: min \t dir") # GLLB parprint("GLLB Gap: min \t dir") parprint("{:.3f}\t{:.3f}".format(Eg_gllb_min, Eg_gllb_dir))
def run_single(formula, prototype, root="/cluster/scratch/ttian/bulk", clean=False): prototype = convert_name(prototype)[0] name = "{}-{}".format(formula, prototype) base_dir = join(root, name) # Directory manipulation if rank == 0: if clean: shutil.rmtree(base_dir, ignore_errors=True) if not exists(base_dir): os.makedirs(base_dir) world.barrier() sb = StructureBuilder() atoms, *_ = sb.get_structure(formula, prototype) m_calc = MaterCalc(atoms=atoms, base_dir=base_dir) if prototype != "perovskite": m_calc.relax(fmax=0.002) else: # m_calc.relax(fmax=0.02, method="UCF") m_calc.relax(skip=True) m_calc.ground_state() if prototype != "perovskite": eg_min, eg_dir, *_ = m_calc.bandgap(method="pbe") parprint("PBE min/dir: {:.3f}\t{:.3f}".format(eg_min, eg_dir)) eg_min, eg_dir, *_ = m_calc.bandgap(method="gllb") parprint("GLLB min/dir: {:.3f}\t{:.3f}".format(eg_min, eg_dir)) else: pass # eg_min, eg_dir, *_ = m_calc.bandgap(method="pbe", skip=True) # parprint("PBE min/dir: {:.3f}\t{:.3f}".format(eg_min, eg_dir)) # eg_min, eg_dir, *_ = m_calc.bandgap(method="gllb", skip=True) # parprint("GLLB min/dir: {:.3f}\t{:.3f}".format(eg_min, eg_dir)) # Use db values m_calc.excited_state() m_calc.dielectric(method="rpa") return 0
def run_single(name="Si", prototype=None): sb = StructureBuilder() mater = sb.get_structure(formula=name, prototype=prototype) parprint("Before", mater) if len(mater) != 1: return mater = mater[0] base_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), "../../tmp/", "{}-{}/".format(name, prototype)) if rank == 0: if not os.path.exists(base_dir): os.makedirs(base_dir) world.barrier() calc = GPAW( mode=dict(name="pw", ecut=800), occupations=dict(name="fermi-dirac", width=0.01), basis="dzp", xc="PBE", kpts=dict(gamma=True, density=4.0), # Very rough k-density txt=os.path.join(base_dir, "{}-{}.txt".format(name, prototype))) mater.set_calculator(calc) sf = StrainFilter(mater) traj_filename = os.path.join(base_dir, "{}-{}.traj".format(name, prototype)) log_filename = os.path.join(base_dir, "{}-{}.log".format(name, prototype)) # Overwrite file with open(traj_filename, "w") as _: pass with open(log_filename, "w") as _: pass opt_strain = ase.optimize.BFGS(sf, trajectory=traj_filename, logfile=log_filename) opt_force = ase.optimize.BFGS(mater, trajectory=traj_filename, logfile=log_filename) opt_strain.run(fmax=0.02) # opt_force.run(fmax=0.02) parprint("After", mater)
def test_build_by_name(self): """Build materials by name and prototype """ sb = StructureBuilder() test_samples = [("AlAs", "Z"), ("ZnO", "z"), ("ZnO", "W"), ("ZnO", "R"), ("TlCl", "C"), ("HgO", None), ("Si3N4", "other"), ("Si3N4", "unknown"), ("C", "D")] failed = [] for s in test_samples: formula, prototype = s try: mat = sb.get_structure(formula, prototype) if len(mat) == 0: failed.append((formula, prototype, "zero-len")) except Exception as e: failed.append((formula, prototype, e)) if len(failed) > 0: parprint(len(failed), failed) else: parprint("All structure passed!") self.assertEqual(len(failed), 0)
def test_relax_single(self): sb = StructureBuilder() atoms, *_ = sb.get_structure("C", "diamond") # print(atoms) m_calc = MaterCalc(atoms=atoms, base_dir="../../tmp/C-class/") m_calc.relax(fmax=0.002) # Very tight limit! m_calc.ground_state() # self.assertTrue(res) base_dir = "../../tmp/C-class/" gpw_name = os.path.join(base_dir, "gs.gpw") self.assertTrue(os.path.exists(gpw_name)) calc = GPAW(restart=gpw_name, txt="gp.txt") # PBE bandgap bg_pbe_min, *_ = bandgap(calc, direct=False) bg_pbe_dir, *_ = bandgap(calc, direct=True) # gllbsc calc_gllb = GPAW(**calc.parameters) calc_gllb.atoms = calc.atoms calc_gllb.set(xc="GLLBSC", txt="gllb.txt") calc_gllb.get_potential_energy() # SC calculation response = calc_gllb.hamiltonian.xc.xcs["RESPONSE"] response.calculate_delta_xc() EKs, Dxc = response.calculate_delta_xc_perturbation() gllb_gap = EKs + Dxc parprint("Eg-PBE-min, Eg-PBE-dir, Eg-gllb") parprint(bg_pbe_min, bg_pbe_dir, gllb_gap) # Use kpts? ibz_kpts = calc.get_ibz_k_points() e_kn = numpy.array([calc.get_eigenvalues(kpt=k) \ for k in range(len(ibz_kpts))]) efermi = calc.get_fermi_level() e_kn[e_kn > efermi] += Dxc gllb_gap_min = bandgap(eigenvalues=e_kn, efermi=efermi, direct=False) gllb_gap_dir = bandgap(eigenvalues=e_kn, efermi=efermi, direct=True) parprint("Efermi", efermi) parprint("Eg-gllb-min, Eg-gllb-dir") parprint(gllb_gap_min, gllb_gap_dir)
def main(root_dir="/cluster/scratch/ttian/bulk/"): sb = StructureBuilder() entries = sb.entries all_data = [] for entry in entries: formula = entry["formula"] prototype = entry["prototype"] single_data = get_single_data(root_dir, formula, prototype) if single_data is not None: all_data.append(single_data) f_save = join(root_dir, "bulk_eps.npz") numpy.savez_compressed(f_save, data=all_data) print("done!")
def main(): sb = StructureBuilder() entries = sb.entries for entry in entries: formula = entry["formula"] prototype = entry["prototype"] base_dir = os.path.join(root, "{}-{}".format(formula, prototype)) mc = MaterCalc(atoms=None, base_dir=base_dir) if all([value for key, value in mc.check_status().items()]): print("{}-{} calculation done!".format(formula, prototype)) else: sub_job(formula=formula, prototype=prototype) return True
def main(): sb = StructureBuilder() entries = sb.entries finished = 0 for entry in entries: formula = entry["formula"] prototype = entry["prototype"] base_dir = os.path.join(root, "{}-{}".format(formula, prototype)) mc = MaterCalc(atoms=None, base_dir=base_dir) if all([value for key, value in mc.check_status().items()]): finished += 1 else: print("{}-{}:".format(formula, prototype), mc.check_status()) print("Finished jobs {}".format(finished)) return True
def main(begin=0, end=None): sb = StructureBuilder() assert begin >= 0 entries = sb.entries if end is None: end = len(entries) candidates = entries[begin:end] # End is not calculated! results_all = [] for i, line in enumerate(candidates): name = line["formula"] prototype = line["prototype"] res_single = run_single(sb, name, prototype) for entry in res_single: results_all.append(entry) parprint(type(results_all), results_all) world.barrier() if rank == 0: with open( os.path.join(cur_dir, "../../tmp/", "relax_result_{}-{}.json".format(begin, end)), "w") as f: json.dump(results_all, f) return
def test_bs(self): sb = StructureBuilder() atoms, *_ = sb.get_structure("Si", "diamond") # print(atoms) base_dir = os.path.join(os.path.dirname(__file__), "../../tmp/Si-class/") m_calc = MaterCalc(atoms=atoms, base_dir=base_dir) self.assertTrue(m_calc.relax(fmax=0.002)) # Very tight limit! self.assertTrue(m_calc.ground_state()) # get the PBE BS lattice_type = get_cellinfo(m_calc.atoms.cell).lattice self.assertTrue(lattice_type in special_paths.keys()) kpts_bs = dict(path=special_paths[lattice_type], npoints=120) gpw_name = os.path.join(base_dir, "gs.gpw") self.assertTrue(os.path.exists(gpw_name)) calc_bs = GPAW(restart=gpw_name, kpts=kpts_bs, fixdensity=True, symmetry='off', txt=os.path.join(base_dir, "pbe-bs.txt")) calc_bs.get_potential_energy() # PBE bandgap bg_pbe_min, *_ = bandgap(calc_bs, direct=False) bg_pbe_dir, *_ = bandgap(calc_bs, direct=True) calc_bs.write(os.path.join(base_dir, "pbe-bs.gpw")) bs_pbe = calc_bs.band_structure() bs_pbe.plot(emin=-10, emax=10, filename=os.path.join(base_dir, "pbe-bs.png")) # get the gllbsc by steps calc_ = GPAW(restart=gpw_name) calc_gllb = GPAW(**calc_.parameters) calc_gllb.set(xc="GLLBSC", txt=os.path.join(base_dir, "gllb-gs.txt")) calc_gllb.atoms = calc_.atoms del calc_ calc_gllb.get_potential_energy() # SC calculation calc_gllb.write("gllb-gs.gpw") calc_gllb_bs = GPAW(restart="gllb-gs.gpw", kpts=kpts_bs, fixdensity=True, symmetry="off", txt=os.path.join(base_dir, "gllb-bs.txt")) world.barrier() calc_gllb_bs.get_potential_energy() homolumo = calc_gllb_bs.get_homo_lumo() bg_gllb_ks = homolumo[1] - homolumo[0] response = calc_gllb_bs.hamiltonian.xc.xcs["RESPONSE"] response.calculate_delta_xc(homolumo / Ha) EKs, Dxc = response.calculate_delta_xc_perturbation() bg_gllb_deltaxc = EKs + Dxc ibz_kpts = calc_gllb_bs.get_ibz_k_points() e_kn = numpy.array([calc_gllb_bs.get_eigenvalues(kpt=k) \ for k in range(len(ibz_kpts))]) efermi = calc_gllb_bs.get_fermi_level() e_kn[e_kn > efermi] += Dxc bg_gllb_min, *_ = bandgap(eigenvalues=e_kn, efermi=efermi, direct=False) bg_gllb_dir, *_ = bandgap(eigenvalues=e_kn, efermi=efermi, direct=True) parprint("PBE: E_min \t E_dir") parprint("{:.3f}\t{:.3f}".format(bg_pbe_min, bg_pbe_dir)) parprint("Gllb: EKS \t E_deltaxc") parprint("{:.3f}\t{:.3f}".format(bg_gllb_ks, bg_gllb_deltaxc)) parprint("Gllb: E_min \t E_dir") parprint("{:.3f}\t{:.3f}".format(bg_gllb_min, bg_gllb_dir)) bs_gllb = calc_gllb_bs.band_structure() bs_gllb.energies[bs_gllb.energies > bs_gllb.reference] += Dxc bs_gllb.plot(emin=-10, emax=10, filename=os.path.join(base_dir, "gllb-bs.png")) calc_gllb_bs.write(os.path.join(base_dir, "gllb-bs.gpw"))