|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +def orca_ipfitting(orca_input, omega_l, omega_r, omega_tol=1.0e-3, maxiter=20): |
| 4 | + import os, re, subprocess, shutil |
| 5 | + |
| 6 | + inp_omega_decimal = 6 |
| 7 | + inp_omega_fmt = '%%.%df' % (inp_omega_decimal,) |
| 8 | + |
| 9 | + omegas = [] |
| 10 | + types = [] |
| 11 | + E0s = [] |
| 12 | + E1s = [] |
| 13 | + IPs = [] |
| 14 | + kIPs = [] |
| 15 | + |
| 16 | + def inp_acc_omega(omega): |
| 17 | + return round(omega, inp_omega_decimal) |
| 18 | + |
| 19 | + def nearest_omega(omega, min_diff=None): |
| 20 | + old_omega = None |
| 21 | + for og in omegas: |
| 22 | + diff = abs(og - omega) |
| 23 | + if min_diff is None or diff < min_diff: |
| 24 | + old_omega, min_diff = og, diff |
| 25 | + return old_omega |
| 26 | + |
| 27 | + def gen_fgbw_suffix(omega): |
| 28 | + return '_' + inp_omega_fmt % (omega,) |
| 29 | + |
| 30 | + def gen_inp(chg, spin, omega, finp): |
| 31 | + with open(orca_input, 'r') as f: |
| 32 | + txt = f.read() |
| 33 | + xyz, xyzfile = True, True |
| 34 | + txt_new, na = re.subn('\*\s*xyzfile\s+-?[0-9]+\s+-?[0-9]+', '*xyzfile %d %d' % (chg, spin), txt, re.I) |
| 35 | + if na == 0: |
| 36 | + txt_new, nb = re.subn('\*\s*xyz\s+-?[0-9]+\s+-?[0-9]+', '*xyz %d %d' % (chg, spin), txt, re.I) |
| 37 | + if nb == 0: |
| 38 | + raise ValueError('Fail to renew charge and spin!') |
| 39 | + txt_new, n = re.subn('RangeSepMu\s+[^\s]*', 'RangeSepMu %.6f' % (omega), txt_new, re.I) |
| 40 | + if n == 0: |
| 41 | + raise ValueError('Fail to renew omega!') |
| 42 | + with open(finp, 'w') as f: |
| 43 | + f.write(txt_new) |
| 44 | + |
| 45 | + def get_tote(flog): |
| 46 | + tote = None |
| 47 | + with open(flog, 'r') as f: |
| 48 | + for line in f.readlines(): |
| 49 | + if line.startswith('FINAL SINGLE POINT ENERGY'): |
| 50 | + tote = float(line.strip().split()[-1]) |
| 51 | + break |
| 52 | + return tote |
| 53 | + |
| 54 | + def get_homo(flog): |
| 55 | + homo = None |
| 56 | + with open(flog, 'r') as f: |
| 57 | + lines = f.readlines() |
| 58 | + start = None |
| 59 | + for i, line in enumerate(lines): |
| 60 | + if line.startswith('ORBITAL ENERGIES'): |
| 61 | + start = i + 4 |
| 62 | + break |
| 63 | + occ, lastocc, e, laste = 0.0, 0.0, 0.0, 0.0 |
| 64 | + for i, line in enumerate(lines[start:]): |
| 65 | + occ, e = [float(x) for x in line.strip().split()[1:3]] |
| 66 | + if round(occ) == 0 and round(lastocc) == 2: |
| 67 | + homo = laste |
| 68 | + break |
| 69 | + lastocc, laste = occ, e |
| 70 | + return homo |
| 71 | + |
| 72 | + def calc_neutral(omega): |
| 73 | + finp, flog, fgbw = 'neutral.inp', 'neutral.log', 'neutral.gbw' |
| 74 | + gen_inp(0, 1, omega, finp) |
| 75 | + old_omega = nearest_omega(omega) |
| 76 | + if old_omega is not None: |
| 77 | + old_fgbw = fgbw + gen_fgbw_suffix(old_omega) |
| 78 | + shutil.copyfile(old_fgbw, fgbw) |
| 79 | + with open(flog, 'w') as f: |
| 80 | + exitcode = subprocess.call([orca_exec, finp], stdout=f, stderr=f) |
| 81 | + if exitcode != 0: |
| 82 | + raise RuntimeError('ORCA terminated abnormally!') |
| 83 | + E, E_HOMO = get_tote(flog), get_homo(flog) |
| 84 | + new_fgbw = fgbw + gen_fgbw_suffix(omega) |
| 85 | + shutil.copyfile(fgbw, new_fgbw) |
| 86 | + |
| 87 | + return E, E_HOMO |
| 88 | + |
| 89 | + def calc_cation(omega): |
| 90 | + finp, flog, fgbw = 'cation.inp', 'cation.log', 'cation.gbw' |
| 91 | + gen_inp(1, 2, omega, finp) |
| 92 | + old_omega = nearest_omega(omega, min_diff=0.05) |
| 93 | + if old_omega is not None: |
| 94 | + old_fgbw = fgbw + gen_fgbw_suffix(old_omega) |
| 95 | + else: |
| 96 | + old_fgbw = 'neutral.gbw' |
| 97 | + shutil.copyfile(old_fgbw, fgbw) |
| 98 | + with open(flog, 'w') as f: |
| 99 | + exitcode = subprocess.call([orca_exec, finp], stdout=f, stderr=f) |
| 100 | + if exitcode != 0: |
| 101 | + raise RuntimeError('ORCA terminated abnormally!') |
| 102 | + |
| 103 | + E = get_tote(flog) |
| 104 | + new_fgbw = fgbw + gen_fgbw_suffix(omega) |
| 105 | + shutil.copyfile(fgbw, new_fgbw) |
| 106 | + |
| 107 | + return E |
| 108 | + |
| 109 | + orca_root = os.environ.get('ORCA_ROOT', None) |
| 110 | + if orca_root is None: |
| 111 | + raise ValueError('"ORCA_ROOT" is not set!') |
| 112 | + orca_exec = orca_root + '/orca' |
| 113 | + if not os.access(orca_exec, os.X_OK): |
| 114 | + raise ValueError('"%s" is not executable!') |
| 115 | + |
| 116 | + E0r, E_HOMOr = calc_neutral(omega_r) |
| 117 | + E1r = calc_cation(omega_r) |
| 118 | + |
| 119 | + IPr, kIPr = E1r - E0r, -E_HOMOr |
| 120 | + delta_r = IPr - kIPr |
| 121 | + |
| 122 | + if IPr > kIPr: |
| 123 | + raise ValueError("""\n***IP Fitting Error: Right Omega limit should have kIP > IP: {} !> {}""".format(kIPr, IPr)) |
| 124 | + |
| 125 | + omegas.append(omega_r) |
| 126 | + types.append('Right Limit') |
| 127 | + E0s.append(E0r) |
| 128 | + E1s.append(E1r) |
| 129 | + IPs.append(IPr) |
| 130 | + kIPs.append(kIPr) |
| 131 | + |
| 132 | + E0l, E_HOMOl = calc_neutral(omega_l) |
| 133 | + E1l = calc_cation(omega_l) |
| 134 | + |
| 135 | + IPl, kIPl = E1l - E0l, -E_HOMOl |
| 136 | + delta_l = IPl - kIPl |
| 137 | + |
| 138 | + if IPl < kIPl: |
| 139 | + raise ValueError("""\n***IP Fitting Error: Left Omega limit should have kIP < IP: {} !< {}""".format(kIPl, IPl)) |
| 140 | + |
| 141 | + omegas.append(omega_l) |
| 142 | + types.append('Left Limit') |
| 143 | + E0s.append(E0l) |
| 144 | + E1s.append(E1l) |
| 145 | + IPs.append(IPl) |
| 146 | + kIPs.append(kIPl) |
| 147 | + |
| 148 | + converged = False |
| 149 | + repeat_l = 0 |
| 150 | + repeat_r = 0 |
| 151 | + for step in range(maxiter): |
| 152 | + # Regula Falsi (modified) |
| 153 | + if repeat_l > 1: |
| 154 | + delta_l /= 2.0 |
| 155 | + if repeat_r > 1: |
| 156 | + delta_r /= 2.0 |
| 157 | + omega = - (omega_r - omega_l) / (delta_r - delta_l) * delta_l + omega_l |
| 158 | + if inp_acc_omega(omega) == inp_acc_omega(omega_r) or inp_acc_omega(omega) == inp_acc_omega(omega_l): |
| 159 | + converged = True |
| 160 | + break |
| 161 | + |
| 162 | + E0, E_HOMO = calc_neutral(omega) |
| 163 | + E1 = calc_cation(omega) |
| 164 | + |
| 165 | + IP, kIP = E1 - E0, -E_HOMO |
| 166 | + delta = IP - kIP |
| 167 | + |
| 168 | + if kIP > IP: |
| 169 | + omega_r = omega |
| 170 | + E0r = E0 |
| 171 | + E1r = E1 |
| 172 | + IPr = IP |
| 173 | + kIPr = kIP |
| 174 | + delta_r = delta |
| 175 | + repeat_r = 0 |
| 176 | + repeat_l += 1 |
| 177 | + else: |
| 178 | + omega_l = omega |
| 179 | + E0l = E0 |
| 180 | + E1l = E1 |
| 181 | + IPl = IP |
| 182 | + kIPl = kIP |
| 183 | + delta_l = delta |
| 184 | + repeat_l = 0 |
| 185 | + repeat_r += 1 |
| 186 | + |
| 187 | + omegas.append(omega) |
| 188 | + types.append('Regula-Falsi') |
| 189 | + E0s.append(E0) |
| 190 | + E1s.append(E1) |
| 191 | + IPs.append(IP) |
| 192 | + kIPs.append(kIP) |
| 193 | + |
| 194 | + # Termination |
| 195 | + if abs(omega_l - omega_r) < omega_tol: |
| 196 | + converged = True |
| 197 | + break |
| 198 | + |
| 199 | + print(""" => Regula Falsi Iterations <=\n""") |
| 200 | + print(""" %3s %10s %14s %14s %14s %s""" % ('N','Omega','IP','kIP','Delta','Type')) |
| 201 | + for k in range(len(omegas)): |
| 202 | + print(""" %3d %10.6F %14.6E %14.6E %14.6E %s""" % |
| 203 | + (k + 1, omegas[k], IPs[k], kIPs[k], IPs[k] - kIPs[k], types[k])) |
| 204 | + |
| 205 | + if converged: |
| 206 | + print("""\n IP Fitting Converged""") |
| 207 | + print(""" Final omega = %10.6F""" % ((omega_l + omega_r) / 2)) |
| 208 | + |
| 209 | + else: |
| 210 | + raise ValueError("""IP Fitting """, step + 1) |
| 211 | + |
| 212 | + return ((omega_l + omega_r) / 2) |
| 213 | + |
| 214 | +if __name__ == '__main__': |
| 215 | + import argparse |
| 216 | + |
| 217 | + parser = argparse.ArgumentParser() |
| 218 | + parser.add_argument('--input', type=str, required=True, help='sample input file') |
| 219 | + parser.add_argument('--min', type=float, required=True, help='minimum omega') |
| 220 | + parser.add_argument('--max', type=float, required=True, help='maximum omega') |
| 221 | + parser.add_argument('--tol', type=float, required=False, help='tolerance of omega', default=0.001) |
| 222 | + parser.add_argument('--maxiter', type=int, required=False, help='max number of iterations', default=20) |
| 223 | + |
| 224 | + args = parser.parse_args() |
| 225 | + |
| 226 | + orca_ipfitting(args.input, args.min, args.max, args.tol, args.maxiter) |
| 227 | + |
0 commit comments