This commit is contained in:
2025-01-04 20:08:03 +08:00
parent 1f8557a918
commit f214f51e12
9 changed files with 585 additions and 0 deletions

74
model/fairchem_router.py Normal file
View File

@@ -0,0 +1,74 @@
from fairchem.core import OCPCalculator
from ase.optimize import FIRE # Import your optimizer of choice
from ase.filters import FrechetCellFilter # to include cell relaxations
from ase.io import read
from pymatgen.core import Structure
from pymatgen.ext.matproj import MPRester
from pymatgen.analysis.phase_diagram import PhaseDiagram, PDEntry
from pymatgen.entries.computed_entries import ComputedStructureEntry
from pymatgen.entries.mixing_scheme import MaterialsProjectDFTMixingScheme
# 创建相图并计算形成能与 above hull energy 的函数
def calculate_phase_diagram_properties(structure, total_energy, api_key):
"""
计算化合物的形成能和 above hull energy
参数:
- formula (str): 化学式 (如 "CsPbBr3")
- total_energy (float): 化合物的总能量 (eV)
- mpr (MPRester): MPRester 实例
返回:
- formation_energy (float): 每个原子的形成能 (eV/atom)
- e_above_hull (float): 每个原子的 above hull energy (eV/atom)
"""
chemsys = structure.chemical_system.split("-")
formula = structure.reduced_formula
with MPRester(api_key) as mpr:
# 获取化学系统中所有的相
# entries = mpr.get_entries_in_chemsys(elements=chemsys, additional_criteria={"thermo_types": ["GGA_GGA+U"]})
entries = mpr.get_entries_in_chemsys(elements=chemsys, additional_criteria={"thermo_types": ["GGA_GGA+U", "R2SCAN"]})
# 创建新计算结构的 PDEntry
pd_entry = PDEntry(composition=formula, energy=total_energy)
# entries.append(pd_entry)
scheme = MaterialsProjectDFTMixingScheme()
entries = scheme.process_entries(entries)
# 创建相图
pd = PhaseDiagram(entries + [pd_entry])
# 计算形成能和 above hull energy
formation_energy = pd.get_form_energy_per_atom(pd_entry)
e_above_hull = pd.get_e_above_hull(pd_entry)
return formation_energy, e_above_hull
atoms = read("/home/ubuntu/sas0/LYT/paper_dataset/mp_cif/CsPbBr3.cif") # Read in an atoms object or create your own structure
calc = OCPCalculator(checkpoint_path="/home/ubuntu/sas0/LYT/paper_dataset/mp_cif/meta_fairchem/eqV2_86M_omat_mp_salex.pt") # Path to downloaded checkpoint
atoms.calc = calc
dyn = FIRE(FrechetCellFilter(atoms))
dyn.run(fmax=0.01)
total_energy = atoms.get_potential_energy()
print("Predicted Total Energy: ", total_energy)
# 保存优化后的结构
atoms.write("optimized_structure.cif") # 保存为 CIF 文件
print("Geometry optimization completed. Optimized structure saved as 'optimized_structure.cif'.")
# 从 ASE 转换为 Pymatgen 结构
optimized_structure = Structure.from_file("optimized_structure.cif")
api_key = "gfBp2in8qxm9Xm2SwLKFwNxDyZvNTAEt"
mpr = MPRester(api_key)
print(f"Chemical Formula: {optimized_structure .composition.reduced_formula}")
formation_energy, e_above_hull = calculate_phase_diagram_properties(
structure=optimized_structure,
total_energy=total_energy,
api_key=api_key
)
print(formation_energy, e_above_hull)
print()