Koulb's picture
Add files using upload-large-folder tool
a65f762 verified
#!/usr/bin/env python
"""
Run QE calculations for all displaced configs and band structure folders.
Runs:
- SCF + pw2bgw for each data/disp-XX/
- SCF + pw2bgw + bands + bands.x for data/bands/uc/ and data/bands/sc/
If params.json has cluster.run_sh set, wraps each job for cluster submission.
Otherwise runs locally with mpirun.
Usage: python run.py [params.json]
"""
import glob
import json
import os
import subprocess
import sys
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
def load_params(path=None):
if path is None:
path = os.path.join(SCRIPT_DIR, 'params.json')
with open(path) as f:
return json.load(f)
def run_qe(cmd_args, cwd, qe_setup, np, log_file, env=None):
"""Run a QE command with mpirun, sourcing qe_setup first."""
mpi = ['mpirun', '-np', str(np)]
bash_cmd = f"source {qe_setup} 2>/dev/null; " + \
' '.join(mpi + cmd_args + [f'> {log_file} 2>&1'])
subprocess.run(['bash', '-c', bash_cmd], cwd=cwd, check=True)
def run_scf(scf_dir, qe_setup, np):
xml = os.path.join(scf_dir, 'diamond.xml')
if os.path.exists(xml):
print(f" SCF already done: {scf_dir}")
return
print(f" Running SCF in {scf_dir}")
run_qe(['pw.x', '<', 'pw.in'], scf_dir, qe_setup, np, 'pw.out')
def run_pw2bgw(scf_dir, qe_setup, np):
vsc = os.path.join(scf_dir, 'VSC')
if os.path.exists(vsc):
print(f" pw2bgw already done: {scf_dir}")
return
print(f" Running pw2bgw in {scf_dir}")
run_qe(['pw2bgw.x', '<', 'pw2bgw.in'], scf_dir, qe_setup, np, 'pw2bgw.out')
def run_bands(scf_dir, qe_setup, np):
gnu = os.path.join(scf_dir, 'bands.dat.gnu')
if os.path.exists(gnu):
print(f" Bands already done: {scf_dir}")
return
print(f" Running bands in {scf_dir}")
run_qe(['pw.x', '<', 'bands.in'], scf_dir, qe_setup, np, 'bands.out')
# bands post-processing
bash_cmd = f"source {qe_setup} 2>/dev/null; bands.x < bands_pp.in > bands_pp.out 2>&1"
subprocess.run(['bash', '-c', bash_cmd], cwd=scf_dir, check=True)
def submit_cluster(script_path, cwd):
"""Submit to cluster using the provided run_sh script."""
subprocess.run(['bash', script_path], cwd=cwd, check=True)
def main():
params_path = sys.argv[1] if len(sys.argv) > 1 else \
os.path.join(SCRIPT_DIR, 'params.json')
params = load_params(params_path)
data_dir = os.path.join(SCRIPT_DIR, 'data')
qe_setup = params['paths']['qe_setup']
np_qe = params['execution']['mpi_np']
run_sh = params['execution'].get('run_sh')
if not os.path.isdir(data_dir):
print("data/ not found – run prepare.py first")
sys.exit(1)
# -------------------------------------------------------
# Displaced configurations: SCF + pw2bgw
# -------------------------------------------------------
disp_dirs = sorted(glob.glob(os.path.join(data_dir, 'disp-*')))
print(f"Processing {len(disp_dirs)} displaced configurations...")
for disp_dir in disp_dirs:
label = os.path.basename(disp_dir)
scf_dir = os.path.join(disp_dir, 'scf')
recon_done = os.path.join(disp_dir, 'reconstruction',
'aohamiltonian', 'hamiltonians.h5')
if os.path.exists(recon_done):
print(f" [{label}] Already fully done, skipping")
continue
print(f" [{label}]")
if run_sh:
submit_cluster(run_sh, disp_dir)
else:
run_scf(scf_dir, qe_setup, np_qe)
run_pw2bgw(scf_dir, qe_setup, np_qe)
# -------------------------------------------------------
# Band structure folders: SCF + pw2bgw + bands
# -------------------------------------------------------
for cell_label in ('uc', 'sc'):
bands_dir = os.path.join(data_dir, 'bands', cell_label)
if not os.path.isdir(bands_dir):
continue
scf_dir = os.path.join(bands_dir, 'scf')
print(f"\nProcessing bands/{cell_label}...")
if run_sh:
submit_cluster(run_sh, bands_dir)
else:
run_scf(scf_dir, qe_setup, np_qe)
run_pw2bgw(scf_dir, qe_setup, np_qe)
if os.path.exists(os.path.join(scf_dir, 'bands.in')):
run_bands(scf_dir, qe_setup, np_qe)
print("\nrun.py done. Run reconstruct.py next.")
if __name__ == '__main__':
main()