west_fetch_input.py 6.85 KB
Newer Older
Marco Govoni's avatar
Marco Govoni committed
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/python3

from __future__ import print_function
import sys
from os import path, remove
import yaml
import json

rytoev = 13.6056980659

#########################
# 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 # dynamically set to the number of electrons 
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 ] # dynamically set to the actual number of q 
# wfreq_control
default["wfreq_control"] = {}
default["wfreq_control"]["wfreq_calculation"] = "XWGQ"
default["wfreq_control"]["n_pdep_eigen_to_use"] = 1 # dynamically set to the number of electrons
default["wfreq_control"]["qp_bandrange"] = [1, 2]
default["wfreq_control"]["macropol_calculation"] = 'N'
default["wfreq_control"]["n_lanczos"] = 30
default["wfreq_control"]["n_imfreq"] = 128 
default["wfreq_control"]["n_refreq"] = 272  
default["wfreq_control"]["ecut_imfreq"] = 25. # dynamically set to ecutrho
default["wfreq_control"]["ecut_refreq"] = 2. 
default["wfreq_control"]["wfreq_eta"] = 0.05 / rytoev
default["wfreq_control"]["n_secant_maxiter"] = 21
default["wfreq_control"]["trev_secant"] = 0.05 / rytoev
default["wfreq_control"]["l_enable_lanczos"] = True
default["wfreq_control"]["l_enable_gwetot"] = False
default["wfreq_control"]["o_restart_time"] = 0.
default["wfreq_control"]["ecut_spectralf"] = [-2., 1.] 
default["wfreq_control"]["n_spectralf"] = 204
# westpp_control
default["westpp_control"] = {}
default["westpp_control"]["westpp_calculation"] = "r"
default["westpp_control"]["westpp_range"] = [1, 2]
default["westpp_control"]["westpp_format"] = "C"
default["westpp_control"]["westpp_sign"] = False
default["westpp_control"]["westpp_n_pdep_eigen_to_use"] = 1
default["westpp_control"]["westpp_r0"] = [0., 0., 0.]
default["westpp_control"]["westpp_nr"] = 100
default["westpp_control"]["westpp_rmax"] = 1.
default["westpp_control"]["westpp_epsinfty"] = 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) ]
       #
       assert("nelec") in kwargs.keys()
       nelec = kwargs["nelec"] 
       default[key]["n_pdep_eigen"] = int(nelec)
    #
    if key == "wfreq_control" :
       #
       assert("nelec") in kwargs.keys()
       nelec = kwargs["nelec"] 
       default[key]["n_pdep_eigen_to_use"] = int(nelec)
       #
       assert("ecutrho") in kwargs.keys()
       ecutrho = kwargs["ecutrho"] 
       default[key]["ecut_imfreq"] = ecutrho

################
# OPEN & PARSE #
################

def open_and_parse_file(fileName="west.in") :
    """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)
    #
    return data

##############
# CHECK DICT #
##############

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``

    """
    #
    data = {}
    #
    for key in default_data.keys() : 
        if key in parsed_data.keys() : 
           data[key] = parsed_data[key]
        else : 
           data[key] = default_data[key]
    #
    return data

###########
# SUPPORT #
###########

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)

#########
# PRINT #
#########

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()

#############
# INTERFACE #
#############

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

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

    """
    #
    fileName = args[0]
    keyword = args[1] 
    verbose = args[2]
    #
    # Assign static & dynamical defaults 
    # 
    default_data = default[keyword]
    update_default_values(keyword,kwargs) 
    #
    # Read input file  
    #
    input_data = open_and_parse_file(fileName)
    if keyword in input_data.keys() :
       parsed_data = input_data[keyword]
    else : 
       parsed_data = {}
    #
    # Compare defaults and input variables 
    #
    data = check_dict( parsed_data, default_data )
    #
    # Print
    #
    if (verbose) : 
       print_dict(keyword, data)
    #
    return data

########
# TEST #
########

def test() :
    #
    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 # this is a comment
   unknown_key : value # this line will be read but not passed 
""")
    #
    read_keyword_from_file(fileName,"input_west",True)
    read_keyword_from_file(fileName,"wstat_control",True,nq=20,nelec=10)
    read_keyword_from_file(fileName,"wfreq_control",True,nelec=10,ecutrho=30.)
    read_keyword_from_file(fileName,"westpp_control",True)
    #
    remove(fileName)

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