File size: 4,425 Bytes
a65f762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/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()