1from __future__ import print_function
2import re, sys
3
4info = """
5This script collects the relevant simulation parameters and generates a
6set of input options for `fft_test.x` that replicate the execution of
7vloc_psi done in the production run.
8This simplifies the identification of the optimal fft tasking paramete.
9
10Usage: python run_test.py pw_out_file
11"""
12
13match_alat = re.compile(r'lattice parameter \(alat\)\s+=\s+([+-]?([0-9]*[.])?[0-9]+)')
14match_nbnd = re.compile(r'number of Kohn-Sham states=\s+([+-]?([0-9]*[.])?[0-9]+)')
15match_ecutwfc = re.compile(r'kinetic-energy cutoff\s+=\s+([+-]?([0-9]*[.])?[0-9]+)')
16match_ecutrho = re.compile(r'charge density cutoff\s+=\s+([+-]?([0-9]*[.])?[0-9]+)')
17match_k = re.compile(r'number of k points=\s+(\d+)')
18
19
20if __name__ == "__main__":
21    if len(sys.argv) <= 1:
22        print(info)
23    else:
24        with open(sys.argv[1],'r') as f:
25            data = f.read(30000)
26            gamma = False
27            maxk = ''
28            alat = match_alat.findall(data)
29            nbnd = match_nbnd.findall(data)
30            ewfc = match_ecutwfc.findall(data)
31            erho = match_ecutrho.findall(data)
32            if len(alat[0]) == 0 or len(nbnd[0]) == 0 or len(ewfc[0]) == 0 or len(erho[0]) == 0:
33                print("Could not parse file. Sorry.")
34            alat = alat[0][0]; nbnd=nbnd[0][0]; ewfc=ewfc[0][0];erho=erho[0][0]
35            a1 = []
36            a2 = []
37            a3 = []
38            lines = data.splitlines()
39            for i, line in enumerate(lines):
40                if 'gamma-point specific algorithms are used' in line:
41                    gamma = True
42                if 'crystal axes' in line:
43                    a1 = [float(x)*float(alat) for x in lines[i+1].split()[3:6]]
44                    a2 = [float(x)*float(alat) for x in lines[i+2].split()[3:6]]
45                    a3 = [float(x)*float(alat) for x in lines[i+3].split()[3:6]]
46                if 'number of k points' in line:
47                    nk = int(match_k.findall(line)[0])
48                    if '2pi/alat' in lines[i+1]:
49                        nrm2 = 0
50                        for k in range(nk):
51                            kn, v = re.findall(r"\(([-\d\s\.]*)\)", lines[i+k+2])
52                            v2 = [float(x)**2 for x in v.split()]
53                            if sum(v2) > nrm2:
54                                maxk=v
55
56            print ("To analize performances run with:")
57            buf = ("mpirun -np X ./fft_test.x -ntg Y -ecutwfc {ewfc} -ecutrho {erho} " + \
58                  "-av1 {av1} -av2 {av2} -av3 {av3} -nbnd {nbnd} -gamma {gamma}").format(\
59                       ewfc=ewfc, erho=erho, \
60                       av1=' '.join([str(x) for x in a1]), \
61                       av2=' '.join([str(x) for x in a2]), \
62                       av3=' '.join([str(x) for x in a3]), \
63                       nbnd=nbnd, gamma=('.true.' if gamma else '.false.'))
64            if maxk:
65                buf += " -kmax "+maxk
66            print(buf)
67
68