fetch_input.py 4.15 KB
Newer Older
Marco Govoni's avatar
Marco Govoni committed
1
2
3
#!/usr/bin/python

# File: fetch_input.py
Marco Govoni's avatar
Marco Govoni committed
4
# Test: python3 fetch_input.py
Marco Govoni's avatar
Marco Govoni committed
5
6
7
8
9
10
11

from __future__ import print_function
import sys
import os
import yaml
import json

Marco Govoni's avatar
Marco Govoni committed
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
#########################
# STATIC DEFAULT VALUES #
#########################

default = {}
# input_west
default["input_west"] = {}
default["input_west"]["qe_prefix"] = "pwscf"
default["input_west"]["west_prefix"] = "west"
default["input_west"]["outdir"] = "./"
# wstat_control
default["wstat_control"] = {}
default["wstat_control"]["wstat_calculation"] = "S"
default["wstat_control"]["n_pdep_eigen"] = 1
default["wstat_control"]["n_pdep_times"] = 4
default["wstat_control"]["n_pdep_maxiter"] = 100
default["wstat_control"]["n_dfpt_maxiter"] = 250
default["wstat_control"]["n_pdep_read_from_file"] = 0
default["wstat_control"]["trev_pdep"] = 1.e-3
default["wstat_control"]["trev_pdep_rel"] = 1.e-1
default["wstat_control"]["tr2_dfpt"] = 1.e-12
default["wstat_control"]["l_kinetic_only"] = False
default["wstat_control"]["l_minimize_exx_if_active"] = False
default["wstat_control"]["l_use_ecutrho"] = False
default["wstat_control"]["qlist"] = [ 1 ]

############################
# DYNAMICAL DEFAULT VALUES #
############################

def update_default_values(key,kwargs) :
    assert key in default.keys()
    #
    if key == "wstat_control" :
       assert("nq") in kwargs.keys()
       nq = kwargs["nq"] 
       default[key]["qlist"] = [ i+1 for i in range(nq) ]

def open_and_parse_file(fileName="west.in") :
Marco Govoni's avatar
Marco Govoni committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    """Opens a file and parses it using the YAML sintax 

    :param fileName: name of the file
    :type fileName: ``string``
    :return: parsed data
    :rtype: ``dict``

    """
    data = {}
    try : 
       with open(fileName, 'r') as stream:
           try:
              data = yaml.load(stream,Loader=yaml.SafeLoader)
           except:
              print("Cannot parse file")
    except : 
       print("Cannot open file : ",fileName)
Marco Govoni's avatar
Marco Govoni committed
68
    #
Marco Govoni's avatar
Marco Govoni committed
69
70
    return data

Marco Govoni's avatar
Marco Govoni committed
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
def print_bar(prefix="",nmarks=92) : 
    """Prints bar.

    :param prefix: prefix
    :type prefix: ``string``
    :param nmarks: number of marks
    :type nmarks: ``int``
    """
    #
    s = prefix
    for i in range(nmarks) : 
       s+="-"
    print(s)

def check_dict(parsed_data={}, default_data={}) : 
    """Check data: returns a dictionary with the same keys of default_data. If keys are matching, values of default_data are replaced with those of parsed_data. 

    :param parsed_data: parsed data 
    :type parsed_data: ``dict``
    :param default_data: default data 
    :type default_data: ``dict``
    :return: checked data
    :rtype: ``dict``

    """
Marco Govoni's avatar
Marco Govoni committed
96
97
98
    #
    data = {}
    #
Marco Govoni's avatar
Marco Govoni committed
99
100
101
102
103
104
    for key in default_data.keys() : 
        if key in parsed_data.keys() : 
           data[key] = parsed_data[key]
        else : 
           data[key] = default_data[key]
    #
Marco Govoni's avatar
Marco Govoni committed
105
106
    return data

Marco Govoni's avatar
Marco Govoni committed
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
133
134
135
136
137
def print_dict(title="input_west", data={}) : 
    """Prints data.  

    :param title: title
    :type title: ``string``
    :param data: data to print
    :type default_data: ``dict``

    """
    #
    nmarks = 92
    nspaces = 5 
    s = ""
    for i in range(nspaces) : 
       s+=" "
    #
    print_bar(s,nmarks)
    print(s+"I/O Summary : "+str(title))
    print_bar(s,nmarks)
    for key in data.keys() :
       print(s+key,"=",data[key])
    print_bar(s,nmarks)
    sys.stdout.flush()

def read_keyword_from_file(*args, **kwargs):
    """Read keyword from file  

    :return: read data
    :rtype: ``dict``

    """
Marco Govoni's avatar
Marco Govoni committed
138
139
    #
    fileName = args[0]
Marco Govoni's avatar
Marco Govoni committed
140
141
142
143
    keyword = args[1] 
    #
    parsed_data = open_and_parse_file(fileName)[keyword]
    default_data = default[keyword]
Marco Govoni's avatar
Marco Govoni committed
144
    #
Marco Govoni's avatar
Marco Govoni committed
145
    update_default_values(keyword,kwargs) 
Marco Govoni's avatar
Marco Govoni committed
146
    #
Marco Govoni's avatar
Marco Govoni committed
147
    data = check_dict( parsed_data, default_data )
Marco Govoni's avatar
Marco Govoni committed
148
    #
Marco Govoni's avatar
Marco Govoni committed
149
150
151
    print_dict(keyword, data)
    #
    return data
Marco Govoni's avatar
Marco Govoni committed
152

Marco Govoni's avatar
Marco Govoni committed
153
def test() :
Marco Govoni's avatar
Marco Govoni committed
154
    #
Marco Govoni's avatar
Marco Govoni committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    fileName = "west.in"
    #
    with open(fileName, "w") as file :
       file.write("""
input_west : 
   qe_prefix : molecule
   west_prefix : molecule
   outdir : "./"
wstat_control : 
   wstat_calculation : R
""")
    #
    read_keyword_from_file(fileName,"input_west")
    read_keyword_from_file(fileName,"wstat_control",nq=20)
    #
    os.remove(fileName)

if __name__ == "__main__":
    # execute only if run as a script
    test()