/
potential.py
463 lines (410 loc) · 16.6 KB
/
potential.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# coding: utf-8
# Copyright (c) Max-Planck-Institut für Eisenforschung GmbH - Computational Materials Design (CM) Department
# Distributed under the terms of "New BSD License", see the LICENSE file.
import os
import posixpath
import numpy as np
import pandas
import tables
from pyiron.base.generic.parameters import GenericParameters
from pyiron.base.settings.generic import Settings
from pyiron.atomistics.job.potentials import PotentialAbstract
__author__ = "Jan Janssen"
__copyright__ = (
"Copyright 2019, Max-Planck-Institut für Eisenforschung GmbH - "
"Computational Materials Design (CM) Department"
)
__version__ = "1.0"
__maintainer__ = "Jan Janssen"
__email__ = "janssen@mpie.de"
__status__ = "development"
__date__ = "Sep 1, 2017"
s = Settings()
class VaspPotentialAbstract(PotentialAbstract):
"""
Args:
potential_df:
default_df:
selected_atoms:
"""
def __init__(self, potential_df=None, default_df=None, selected_atoms=None):
if potential_df is None:
potential_df = self._get_potential_df(
plugin_name="vasp",
file_name_lst={"potentials_vasp.csv"},
backward_compatibility_name="vasppotentials",
)
super(VaspPotentialAbstract, self).__init__(
potential_df=potential_df,
default_df=default_df,
selected_atoms=selected_atoms,
)
def default(self):
if self._default_df is not None:
return pandas.concat(
[
self._potential_df[
(
self._potential_df["Name"]
== self._default_df.loc[atom].values[0]
)
]
for atom in self._selected_atoms
]
)
return None
def find_default(self, element):
if isinstance(element, set):
element = element
elif isinstance(element, list):
element = set(element)
elif isinstance(element, str):
element = set([element])
else:
raise TypeError("Only, str, list and set supported!")
element_lst = list(element)
if self._default_df is not None:
merged_lst = list(set(self._selected_atoms + element_lst))
return pandas.concat(
[
self._potential_df[
(
self._potential_df["Name"]
== self._default_df.loc[atom].values[0]
)
]
for atom in merged_lst
]
)
return None
def find(self, element):
if isinstance(element, set):
element = element
elif isinstance(element, list):
element = set(element)
elif isinstance(element, str):
element = set([element])
else:
raise TypeError("Only, str, list and set supported!")
element_lst = list(element)
merged_lst = list(set(self._selected_atoms + element_lst))
return pandas.concat(
[super(VaspPotentialAbstract, self).find({atom}) for atom in merged_lst]
)
def list(self):
if len(self._selected_atoms) != 0:
return pandas.concat(
[
super(VaspPotentialAbstract, self).find({atom})
for atom in self._selected_atoms
]
)
else:
return pandas.DataFrame({})
def list_potential_names(self):
df = self.list()
if len(df) != 0:
return list(self.list()["Name"])
else:
return []
@staticmethod
def _return_potential_file(file_name):
for resource_path in s.resource_paths:
resource_path_potcar = os.path.join(
resource_path, "vasp", "potentials", file_name
)
if os.path.exists(resource_path_potcar):
return resource_path_potcar
return None
def __dir__(self):
return [val.replace("-", "_") for val in self.list_potential_names()]
def __getitem__(self, item):
item_replace = item.replace("_gga_pbe", "-gga-pbe").replace("_lda", "-lda")
if item_replace in self.list_potential_names():
df = self.list()
return self._return_potential_file(
file_name=list(df[df["Name"] == item_replace]["Filename"])[0][0]
)
selected_atoms = self._selected_atoms + [item]
return VaspPotentialAbstract(
potential_df=self._potential_df,
default_df=self._default_df,
selected_atoms=selected_atoms,
)
class VaspPotentialFile(VaspPotentialAbstract):
"""
The Potential class is derived from the PotentialAbstract class, but instead of loading the potentials from a list,
the potentials are loaded from a file.
Args:
xc (str): Exchange correlation functional ['PBE', 'LDA']
"""
def __init__(self, xc=None, selected_atoms=None):
potential_df = self._get_potential_df(
plugin_name="vasp",
file_name_lst={"potentials_vasp.csv"},
backward_compatibility_name="vasppotentials",
)
if xc == "PBE":
default_df = self._get_potential_default_df(
plugin_name="vasp",
file_name_lst={"potentials_vasp_pbe_default.csv"},
backward_compatibility_name="defaultvasppbe",
)
potential_df = potential_df[(potential_df["Model"] == "gga-pbe")]
elif xc == "GGA":
default_df = self._get_potential_default_df(
plugin_name="vasp",
file_name_lst={"potentials_vasp_pbe_default.csv"},
backward_compatibility_name="defaultvasppbe",
)
potential_df = potential_df[(potential_df["Model"] == "gga-pbe")]
elif xc == "LDA":
default_df = self._get_potential_default_df(
plugin_name="vasp",
file_name_lst={"potentials_vasp_lda_default.csv"},
backward_compatibility_name="defaultvasplda",
)
potential_df = potential_df[(potential_df["Model"] == "lda")]
else:
raise ValueError(
'The exchange correlation functional has to be set and it can either be "LDA" or "PBE"'
)
super(VaspPotentialFile, self).__init__(
potential_df=potential_df,
default_df=default_df,
selected_atoms=selected_atoms,
)
def add_new_element(self, parent_element, new_element):
"""
Adding a new user defined element with a different POTCAR file. It is assumed that the file exists
Args:
parent_element (str): Parent element
new_element (str): Name of the new element (the name of the folder where the new POTCAR file exists
"""
ds = self.find_default(element=parent_element)
ds["Species"].values[0][0] = new_element
path_list = ds["Filename"].values[0][0].split("/")
path_list[-2] = new_element
name_list = ds["Name"].values[0].split("-")
name_list[0] = new_element
ds["Name"].values[0] = "-".join(name_list)
ds["Filename"].values[0][0] = "/".join(path_list)
self._potential_df = self._potential_df.append(ds)
ds = pandas.Series()
ds.name = new_element
ds["Name"] = "-".join(name_list)
self._default_df = self._default_df.append(ds)
class VaspPotential(object):
"""
The Potential class is derived from the PotentialAbstract class, but instead of loading the potentials from a list,
the potentials are loaded from a file.
Args:
path (str): path to the potential list
"""
def __init__(self, selected_atoms=None):
self.pbe = VaspPotentialFile(xc="PBE", selected_atoms=selected_atoms)
self.lda = VaspPotentialFile(xc="LDA", selected_atoms=selected_atoms)
class VaspPotentialSetter(object):
def __init__(self, element_lst):
super(VaspPotentialSetter, self).__setattr__("_element_lst", element_lst)
super(VaspPotentialSetter, self).__setattr__(
"_potential_dict", {el: None for el in element_lst}
)
def __getattr__(self, item):
if item in self._element_lst:
return item
else:
raise AttributeError
def __setattr__(self, key, value):
if key in self._element_lst:
self._potential_dict[key] = value
else:
raise AttributeError
def to_dict(self):
return self._potential_dict
def __repr__(self):
return self._potential_dict.__repr__()
def find_potential_file(file_name=None, xc=None, path=None, pot_path_dict=None):
if path is not None:
for resource_path in s.resource_paths:
if os.path.exists(os.path.join(resource_path, "vasp", "potentials", path)):
return os.path.join(resource_path, "vasp", "potentials", path)
elif xc is not None and file_name is not None:
for resource_path in s.resource_paths:
if os.path.exists(
os.path.join(resource_path, "vasp", "potentials", pot_path_dict[xc])
):
resource_path = os.path.join(
resource_path, "vasp", "potentials", pot_path_dict[xc]
)
if "potentials" in resource_path:
for path, folder_lst, file_lst in os.walk(resource_path):
if file_name in file_lst:
return os.path.join(path, file_name)
raise ValueError("Either the filename or the functional has to be defined.")
def get_enmax_among_species(symbol_lst, return_list=False, xc="PBE"):
"""
Given a list of species symbols, finds the largest applicable encut.
Args:
symbol_lst (list): The list of species symbols.
return_list (bool): Whether to return the list of all ENMAX values (in the same order as `species_lst` along with
the largest value). (Default is False.)
xc ("GGA"/"PBE"/"LDA"): The exchange correlation functional for which the POTCARs were generated. (Default is "PBE".)
Returns:
(float): The largest ENMAX among the POTCAR files for all the species.
[optional](list): The ENMAX value corresponding to each species.
"""
pot_path_dict = Potcar.pot_path_dict
enmax_lst = []
vpf = VaspPotentialFile(xc=xc)
for symbol in symbol_lst:
potcar_file = find_potential_file(
path=vpf.find_default(symbol)['Filename'].values[0][0],
pot_path_dict=pot_path_dict
)
with open(potcar_file) as pf:
for i, line in enumerate(pf):
if i == 14:
encut_str = line.split()[2][:-1]
enmax_lst.append(float(encut_str))
break
if return_list:
return max(enmax_lst), enmax_lst
else:
return max(enmax_lst)
class Potcar(GenericParameters):
pot_path_dict = {"GGA": "paw-gga-pbe", "PBE": "paw-gga-pbe", "LDA": "paw-lda"}
def __init__(self, input_file_name=None, table_name="potcar"):
GenericParameters.__init__(
self,
input_file_name=input_file_name,
table_name=table_name,
val_only=False,
comment_char="#",
)
self._structure = None
self.electrons_per_atom_lst = list()
self.max_cutoff_lst = list()
self.el_path_lst = list()
self.el_path_dict = dict()
self.modified_elements = dict()
def potcar_set_structure(self, structure, modified_elements):
self._structure = structure
self._set_default_path_dict()
self._set_potential_paths()
self.modified_elements = modified_elements
def modify(self, **modify):
if "xc" in modify:
xc_type = modify["xc"]
self._set_default_path_dict()
if xc_type not in self.pot_path_dict:
raise ValueError("xc type not implemented: " + xc_type)
GenericParameters.modify(self, **modify)
if self._structure is not None:
self._set_potential_paths()
def _set_default_path_dict(self):
if self._structure is None:
return
vasp_potentials = VaspPotentialFile(xc=self.get("xc"))
for i, el_obj in enumerate(self._structure.get_species_objects()):
if isinstance(el_obj.Parent, str):
el = el_obj.Parent
else:
el = el_obj.Abbreviation
if isinstance(el_obj.tags, dict):
if "pseudo_potcar_file" in el_obj.tags.keys():
new_element = el_obj.tags["pseudo_potcar_file"]
vasp_potentials.add_new_element(
parent_element=el, new_element=new_element
)
key = vasp_potentials.find_default(el).Species.values[0][0]
val = vasp_potentials.find_default(el).Name.values[0]
self[key] = val
def _set_potential_paths(self):
element_list = (
self._structure.get_species_symbols()
) # .ElementList.getSpecies()
object_list = self._structure.get_species_objects()
s.logger.debug("element list: {0}".format(element_list))
self.el_path_lst = list()
try:
xc = self.get("xc")
except tables.exceptions.NoSuchNodeError:
xc = self.get("xc")
s.logger.debug("XC: {0}".format(xc))
vasp_potentials = VaspPotentialFile(xc=xc)
for i, el_obj in enumerate(object_list):
if isinstance(el_obj.Parent, str):
el = el_obj.Parent
else:
el = el_obj.Abbreviation
if (
isinstance(el_obj.tags, dict)
and "pseudo_potcar_file" in el_obj.tags.keys()
):
new_element = el_obj.tags["pseudo_potcar_file"]
vasp_potentials.add_new_element(
parent_element=el, new_element=new_element
)
el_path = find_potential_file(
path=vasp_potentials.find_default(new_element)["Filename"].values[
0
][0],
pot_path_dict=self.pot_path_dict,
)
if not (os.path.isfile(el_path)):
raise ValueError("such a file does not exist in the pp directory")
else:
el_path = find_potential_file(
path=vasp_potentials.find_default(el)["Filename"].values[0][0],
pot_path_dict=self.pot_path_dict,
)
if not (os.path.isfile(el_path)):
raise AssertionError()
pot_name = "pot_" + str(i)
if pot_name in self._dataset["Parameter"]:
try:
ind = self._dataset["Parameter"].index(pot_name)
except (ValueError, IndexError):
indices = np.core.defchararray.find(
self._dataset["Parameter"], pot_name
)
ind = np.where(indices == 0)[0][0]
self._dataset["Value"][ind] = el_path
self._dataset["Comment"][ind] = ""
else:
self._dataset["Parameter"].append("pot_" + str(i))
self._dataset["Value"].append(el_path)
self._dataset["Comment"].append("")
if el_obj.Abbreviation in self.modified_elements.keys():
self.el_path_lst.append(self.modified_elements[el_obj.Abbreviation])
else:
self.el_path_lst.append(el_path)
def write_file(self, file_name, cwd=None):
"""
Args:
file_name:
cwd:
Returns:
"""
self.electrons_per_atom_lst = list()
self.max_cutoff_lst = list()
self._set_potential_paths()
if cwd is not None:
file_name = posixpath.join(cwd, file_name)
f = open(file_name, "w")
for el_file in self.el_path_lst:
with open(el_file) as pot_file:
for i, line in enumerate(pot_file):
f.write(line)
if i == 1:
self.electrons_per_atom_lst.append(int(float(line)))
elif i == 14:
mystr = line.split()[2][:-1]
self.max_cutoff_lst.append(float(mystr))
f.close()
def load_default(self):
file_content = """\
xc GGA # LDA, GGA
"""
self.load_string(file_content)