-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.py
More file actions
102 lines (84 loc) · 2.97 KB
/
reader.py
File metadata and controls
102 lines (84 loc) · 2.97 KB
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
import logging
import re
from argparse import ArgumentParser
from configparser import ConfigParser
from os import path
logger = logging.getLogger('curlywaddle')
formatter = logging.Formatter('{levelname:8}:{filename}:{funcName}:{lineno}: {message}', style='{')
def parse_config(config) -> dict:
"""
Parses information from configuration
:param config: ConfigParser
:return: dict
"""
if config.getboolean('GENERAL', 'debug'):
debug = 10
else:
debug = 0
xc = config.get('GENERAL', 'xc')
# KPOINTS section
kpts_text = config.get('GENERAL', 'kpoints')
kpoints = [list(map(int, point.split()))
for point in kpts_text.strip().split('\n')]
if len(kpoints) == 1:
kpoints = kpoints[0]
# INCAR section
incar = dict()
for name, value in config['INCAR'].items():
vals = []
for v in re.split('\s*,?\s+', value):
if re.match('[-+]?\d+$', v):
v = int(v)
elif re.match('(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$', v):
v = float(v)
# vasp incar uses `.` around booleans, e.g. .False.
elif v.lower().strip('. ') in ['yes', 'no', 'on', 'off', 'true', 'false', '1', '0']:
v = v.lower().strip('. ') in ('yes', 'on', 'true', '1')
vals.append(v)
if len(vals) == 1:
vals = vals[0]
incar[name] = vals
res = dict(kpts=kpoints,
xc=xc,
debug=debug,
**incar)
return res
def read_config(fname: str):
defaults = dict(GENERAL={'debug': False,
'xc': 'pbe'})
parser = ConfigParser(allow_no_value=True)
parser.read_dict(defaults)
parser.read(fname)
config = parse_config(parser)
return config
def get_args(argv=''):
parser = ArgumentParser()
parser.add_argument('slab')
parser.add_argument('ads', nargs='?', default='O', choices=['O', 'OH'])
parser.add_argument('-f', '--format')
parser.add_argument('-p', '--poscar_only', action='store_true',
help='writes POSCAR only, does not run')
parser.add_argument('-c', '--config', default='config.ini')
parser.add_argument('--debug', action='store_true')
if argv:
if isinstance(argv, str):
argv = argv.split()
elif not hasattr(argv, '__iter__'):
raise TypeError(f'argv must be `str` or iterable, not {type(argv)}')
args = parser.parse_args(argv)
else:
# get arguments from terminal
args = parser.parse_args()
if args.debug:
log_level = logging.DEBUG
else:
log_level = logging.INFO
logger.setLevel(log_level)
fh = logging.FileHandler('out.log')
fh.setFormatter(formatter)
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
logger.debug(f'args:{vars(args)}')
if not args.poscar_only:
assert path.isfile(args.config), f'config: `{args.config}` must be an existing file'
return args