-
Notifications
You must be signed in to change notification settings - Fork 839
/
pmg_config.py
executable file
·216 lines (188 loc) · 6.48 KB
/
pmg_config.py
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
#!/usr/bin/env python
# coding: utf-8
# Copyright (c) Pymatgen Development Team.
# Distributed under the terms of the MIT License.
"""
Implementation for `pmg config` CLI.
"""
import glob
import os
import shutil
import subprocess
import sys
from urllib.request import urlretrieve
from monty.serialization import dumpfn, loadfn
from pymatgen.core import SETTINGS_FILE
def setup_potcars(args):
"""
Setup POTCAR directirt,
:param args: args from command.
"""
pspdir, targetdir = [os.path.abspath(d) for d in args.potcar_dirs]
try:
os.makedirs(targetdir)
except OSError:
r = input("Destination directory exists. Continue (y/n)? ")
if r != "y":
print("Exiting ...")
sys.exit(0)
print("Generating pymatgen resources directory...")
name_mappings = {
"potpaw_PBE": "POT_GGA_PAW_PBE",
"potpaw_PBE_52": "POT_GGA_PAW_PBE_52",
"potpaw_PBE_54": "POT_GGA_PAW_PBE_54",
"potpaw_PBE.52": "POT_GGA_PAW_PBE_52",
"potpaw_PBE.54": "POT_GGA_PAW_PBE_54",
"potpaw_LDA": "POT_LDA_PAW",
"potpaw_LDA.52": "POT_LDA_PAW_52",
"potpaw_LDA.54": "POT_LDA_PAW_54",
"potpaw_LDA_52": "POT_LDA_PAW_52",
"potpaw_LDA_54": "POT_LDA_PAW_54",
"potUSPP_LDA": "POT_LDA_US",
"potpaw_GGA": "POT_GGA_PAW_PW91",
"potUSPP_GGA": "POT_GGA_US_PW91",
}
for (parent, subdirs, files) in os.walk(pspdir):
basename = os.path.basename(parent)
basename = name_mappings.get(basename, basename)
for subdir in subdirs:
filenames = glob.glob(os.path.join(parent, subdir, "POTCAR*"))
if len(filenames) > 0:
try:
basedir = os.path.join(targetdir, basename)
if not os.path.exists(basedir):
os.makedirs(basedir)
fname = filenames[0]
dest = os.path.join(basedir, os.path.basename(fname))
shutil.copy(fname, dest)
ext = fname.split(".")[-1]
if ext.upper() in ["Z", "GZ"]:
subprocess.Popen(["gunzip", dest]).communicate()
elif ext.upper() in ["BZ2"]:
subprocess.Popen(["bunzip2", dest]).communicate()
if subdir == "Osmium":
subdir = "Os"
dest = os.path.join(basedir, "POTCAR.{}".format(subdir))
shutil.move(os.path.join(basedir, "POTCAR"), dest)
subprocess.Popen(["gzip", "-f", dest]).communicate()
except Exception as ex:
print("An error has occured. Message is %s. Trying to " "continue... " % str(ex))
print("")
print(
"PSP resources directory generated. It is recommended that you "
"run 'pmg config --add PMG_VASP_PSP_DIR %s'" % os.path.abspath(targetdir)
)
print("Start a new terminal to ensure that your environment variables " "are properly set.")
def build_enum(fortran_command="gfortran"):
"""
Build enum.
:param fortran_command:
"""
currdir = os.getcwd()
state = True
try:
subprocess.call(["git", "clone", "--recursive", "https://github.com/msg-byu/enumlib.git"])
os.chdir(os.path.join(currdir, "enumlib", "symlib", "src"))
os.environ["F90"] = fortran_command
subprocess.call(["make"])
enumpath = os.path.join(currdir, "enumlib", "src")
os.chdir(enumpath)
subprocess.call(["make"])
for f in ["enum.x", "makestr.x"]:
subprocess.call(["make", f])
shutil.copy(f, os.path.join("..", ".."))
except Exception as ex:
print(str(ex))
state = False
finally:
os.chdir(currdir)
shutil.rmtree("enumlib")
return state
def build_bader(fortran_command="gfortran"):
"""
Build bader package.
:param fortran_command:
"""
bader_url = "http://theory.cm.utexas.edu/henkelman/code/bader/download/bader.tar.gz"
currdir = os.getcwd()
state = True
try:
urlretrieve(bader_url, "bader.tar.gz")
subprocess.call(["tar", "-zxf", "bader.tar.gz"])
os.chdir("bader")
subprocess.call(["cp", "makefile.osx_" + fortran_command, "makefile"])
subprocess.call(["make"])
shutil.copy("bader", os.path.join("..", "bader_exe"))
os.chdir("..")
shutil.rmtree("bader")
os.remove("bader.tar.gz")
shutil.move("bader_exe", "bader")
except Exception as ex:
print(str(ex))
state = False
finally:
os.chdir(currdir)
return state
def install_software(args):
"""
Install all optional external software.
:param args:
"""
try:
subprocess.call(["ifort", "--version"])
print("Found ifort")
fortran_command = "ifort"
except Exception:
try:
subprocess.call(["gfortran", "--version"])
print("Found gfortran")
fortran_command = "gfortran"
except Exception as ex:
print(str(ex))
print("No fortran compiler found.")
sys.exit(-1)
enum = None
bader = None
if args.install == "enumlib":
print("Building enumlib")
enum = build_enum(fortran_command)
print("")
elif args.install == "bader":
print("Building bader")
bader = build_bader(fortran_command)
print("")
if bader or enum:
print(
"Please add {} to your PATH or move the executables multinum.x, "
"makestr.x and/or bader to a location in your PATH.".format(os.path.abspath("."))
)
print("")
def add_config_var(args):
"""
Add configuration args.
:param args:
"""
d = {}
if os.path.exists(SETTINGS_FILE):
shutil.copy(SETTINGS_FILE, SETTINGS_FILE + ".bak")
print("Existing %s backed up to %s" % (SETTINGS_FILE, SETTINGS_FILE + ".bak"))
d = loadfn(SETTINGS_FILE)
toks = args.var_spec
if len(toks) % 2 != 0:
print("Bad variable specification!")
sys.exit(-1)
for i in range(int(len(toks) / 2)):
d[toks[2 * i]] = toks[2 * i + 1]
dumpfn(d, SETTINGS_FILE, default_flow_style=False)
print("New %s written!" % (SETTINGS_FILE))
def configure_pmg(args):
"""
Handle configure command.
:param args:
"""
if args.potcar_dirs:
setup_potcars(args)
elif args.install:
install_software(args)
elif args.var_spec:
add_config_var(args)