|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# encoding: utf-8 |
| 3 | + |
| 4 | +""" |
| 5 | +Look up atom energy corrections (AEC) and bond additivity corrections (BAC) |
| 6 | +from the RMG quantum corrections database for a given level of theory. |
| 7 | +
|
| 8 | +Run from the RMG conda environment so that Arkane is importable. |
| 9 | +
|
| 10 | +Usage:: |
| 11 | +
|
| 12 | + python get_qm_corrections.py input.yaml output.yaml |
| 13 | +
|
| 14 | +Input YAML format:: |
| 15 | +
|
| 16 | + matched_key: "LevelOfTheory(method='cbsqb3',software='gaussian')" |
| 17 | + bac_type: p # 'p', 'm', or null |
| 18 | +
|
| 19 | +Output YAML format:: |
| 20 | +
|
| 21 | + aec: |
| 22 | + H: -0.499818 |
| 23 | + C: -37.78794 |
| 24 | + bac: |
| 25 | + C-H: -0.06 |
| 26 | +""" |
| 27 | + |
| 28 | +import re |
| 29 | +import sys |
| 30 | + |
| 31 | +from arkane.encorr.data import atom_energies, pbac, mbac |
| 32 | +from arkane.modelchem import LevelOfTheory |
| 33 | + |
| 34 | +from common import read_yaml_file, save_yaml_file |
| 35 | + |
| 36 | + |
| 37 | +def _lot_from_string(lot_str): |
| 38 | + """Construct a LevelOfTheory from its repr string.""" |
| 39 | + # Extract keyword arguments from e.g. "LevelOfTheory(method='cbsqb3',software='gaussian')" |
| 40 | + kwargs = dict(re.findall(r"(\w+)\s*=\s*'([^']*)'", lot_str)) |
| 41 | + return LevelOfTheory(**kwargs) |
| 42 | + |
| 43 | + |
| 44 | +def main(input_path, output_path): |
| 45 | + """Look up AEC and BAC for the given level of theory key.""" |
| 46 | + params = read_yaml_file(input_path) or {} |
| 47 | + matched_key = params.get('matched_key') |
| 48 | + bac_type = params.get('bac_type') |
| 49 | + |
| 50 | + result = {'aec': None, 'bac': None} |
| 51 | + |
| 52 | + if not matched_key: |
| 53 | + save_yaml_file(output_path, result) |
| 54 | + return |
| 55 | + |
| 56 | + lot = _lot_from_string(matched_key) |
| 57 | + |
| 58 | + aec = atom_energies.get(lot) |
| 59 | + if aec is not None: |
| 60 | + result['aec'] = {str(k): float(v) for k, v in aec.items()} |
| 61 | + |
| 62 | + if bac_type in ('p', 'm'): |
| 63 | + bac_dict = pbac if bac_type == 'p' else mbac |
| 64 | + bac = bac_dict.get(lot) |
| 65 | + if bac is not None: |
| 66 | + result['bac'] = {str(k): float(v) for k, v in bac.items()} |
| 67 | + |
| 68 | + save_yaml_file(output_path, result) |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == '__main__': |
| 72 | + if len(sys.argv) != 3: |
| 73 | + print(f'Usage: {sys.argv[0]} input.yaml output.yaml', file=sys.stderr) |
| 74 | + sys.exit(1) |
| 75 | + main(sys.argv[1], sys.argv[2]) |
0 commit comments