2015-02-13 22:24:48 +01:00
|
|
|
r"""
|
2015-11-20 18:54:51 +01:00
|
|
|
vasp.inpconfig
|
|
|
|
==============
|
|
|
|
|
2015-02-13 22:24:48 +01:00
|
|
|
Module for parsing and checking an input config-file.
|
|
|
|
"""
|
2015-02-13 21:58:42 +01:00
|
|
|
import ConfigParser
|
|
|
|
import numpy as np
|
|
|
|
import re
|
|
|
|
import sys
|
2015-02-13 22:24:48 +01:00
|
|
|
import itertools as it
|
2015-02-13 21:58:42 +01:00
|
|
|
import vaspio
|
|
|
|
|
|
|
|
def issue_warning(message):
|
|
|
|
"""
|
|
|
|
Issues a warning.
|
|
|
|
"""
|
|
|
|
print
|
|
|
|
print " !!! WARNING !!!: " + message
|
|
|
|
print
|
2015-02-13 22:24:48 +01:00
|
|
|
|
2015-02-13 21:58:42 +01:00
|
|
|
################################################################################
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# class ConfigParameters
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
################################################################################
|
|
|
|
class ConfigParameters:
|
|
|
|
r"""
|
|
|
|
Class responsible for parsing of the input config-file.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
|
|
|
- *sh_required*, *sh_optional* : required and optional parameters of shells
|
|
|
|
- *gr_required*, *gr_optional* : required and optional parameters of groups
|
|
|
|
|
|
|
|
The dictionary contains a mapping of conf-file keywords to
|
|
|
|
a pair of objects:
|
|
|
|
|
|
|
|
1. internal name of a parameter
|
|
|
|
2. function used to convert an input string into data for a given parameter
|
|
|
|
"""
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# __init__()
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
def __init__(self, input_filename, verbosity=1):
|
|
|
|
self.verbosity = verbosity
|
2015-02-13 22:24:48 +01:00
|
|
|
self.cp = ConfigParser.SafeConfigParser()
|
2015-02-13 21:58:42 +01:00
|
|
|
self.cp.readfp(open(input_filename, 'r'))
|
|
|
|
|
2015-02-13 22:24:48 +01:00
|
|
|
self.parameters = {}
|
|
|
|
|
|
|
|
self.sh_required = {
|
|
|
|
'ions': ('ion_list', self.parse_string_ion_list),
|
|
|
|
'lshell': ('lshell', int)}
|
|
|
|
|
|
|
|
self.sh_optional = {
|
2015-11-30 20:24:34 +01:00
|
|
|
'transform': ('tmatrix', lambda s: self.parse_string_tmatrix(s, real=True)),
|
2015-11-11 11:36:09 +01:00
|
|
|
'transfile': ('tmatrices', self.parse_file_tmatrix)}
|
2015-02-13 22:24:48 +01:00
|
|
|
|
|
|
|
self.gr_required = {
|
|
|
|
'shells': ('shells', lambda s: map(int, s.split())),
|
2015-11-11 18:58:38 +01:00
|
|
|
'ewindow': ('ewindow', self.parse_energy_window)}
|
2015-02-13 22:24:48 +01:00
|
|
|
|
|
|
|
self.gr_optional = {
|
2015-02-17 19:19:13 +01:00
|
|
|
'normalize' : ('normalize', self.parse_string_logical, True),
|
2015-11-20 18:10:14 +01:00
|
|
|
'normion' : ('normion', self.parse_string_logical, True)}
|
2015-02-13 22:24:48 +01:00
|
|
|
|
2015-08-12 12:10:31 +02:00
|
|
|
self.gen_optional = {
|
|
|
|
'basename' : ('basename', str, 'vasp'),
|
2015-11-10 16:40:46 +01:00
|
|
|
'efermi' : ('efermi', float),
|
|
|
|
'dosmesh': ('dosmesh', self.parse_string_dosmesh)}
|
2015-02-13 21:58:42 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# Special parsers
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# parse_string_ion_list()
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
def parse_string_ion_list(self, par_str):
|
|
|
|
"""
|
|
|
|
The ion list accepts two formats:
|
|
|
|
1). A list of ion indices according to POSCAR.
|
2015-02-13 22:24:48 +01:00
|
|
|
The list can be defined as a range '9..20'.
|
2015-02-13 21:58:42 +01:00
|
|
|
2). An element name, in which case all ions with
|
|
|
|
this name are included.
|
|
|
|
|
|
|
|
The second option requires an input from POSCAR file.
|
|
|
|
"""
|
2015-02-13 22:24:48 +01:00
|
|
|
# First check if a range is given
|
|
|
|
patt = '([0-9]+)\.\.([0-9]+)'
|
|
|
|
match = re.match(patt, par_str)
|
|
|
|
if match:
|
|
|
|
i1, i2 = tuple(map(int, match.groups()[:2]))
|
|
|
|
mess = "First index of the range must be smaller or equal to the second"
|
|
|
|
assert i1 <= i2, mess
|
2015-11-10 16:40:46 +01:00
|
|
|
# Note that we need to subtract 1 from VASP indices
|
2015-02-13 22:24:48 +01:00
|
|
|
ion_list = np.array(range(i1 - 1, i2))
|
|
|
|
else:
|
|
|
|
# Check if a set of indices is given
|
|
|
|
try:
|
|
|
|
l_tmp = map(int, par_str.split())
|
|
|
|
l_tmp.sort()
|
2015-02-13 21:58:42 +01:00
|
|
|
# Subtract 1 so that VASP indices (starting with 1) are converted
|
|
|
|
# to Python indices (starting with 0)
|
2015-02-13 22:24:48 +01:00
|
|
|
ion_list = np.array(l_tmp) - 1
|
|
|
|
except ValueError:
|
|
|
|
err_msg = "Only an option with a list of ion indices is implemented"
|
|
|
|
raise NotImplementedError(err_msg)
|
|
|
|
|
|
|
|
err_mess = "Lowest ion index is smaller than 1 in '%s'"%(par_str)
|
|
|
|
assert np.all(ion_list >= 0), err_mess
|
2015-02-13 21:58:42 +01:00
|
|
|
|
|
|
|
return ion_list
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# parse_string_logical()
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
def parse_string_logical(self, par_str):
|
|
|
|
"""
|
|
|
|
Logical parameters are given by string 'True' or 'False'
|
|
|
|
(case does not matter). In fact, only the first symbol matters so that
|
|
|
|
one can write 'T' or 'F'.
|
|
|
|
"""
|
2015-02-13 22:24:48 +01:00
|
|
|
first_char = par_str[0].lower()
|
|
|
|
assert first_char in 'tf', "Logical parameters should be given by either 'True' or 'False'"
|
|
|
|
return first_char == 't'
|
|
|
|
|
2015-11-11 18:58:38 +01:00
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# parse_energy_window()
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
def parse_energy_window(self, par_str):
|
|
|
|
"""
|
|
|
|
Energy window is given by two floats, with the first one being smaller
|
|
|
|
than the second one.
|
|
|
|
"""
|
|
|
|
ftmp = map(float, par_str.split())
|
|
|
|
assert len(ftmp) == 2, "EWINDOW must be specified by exactly two floats"
|
|
|
|
assert ftmp[0] < ftmp[1], "The first float in EWINDOW must be smaller than the second one"
|
|
|
|
return tuple(ftmp)
|
|
|
|
|
2015-02-13 22:24:48 +01:00
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# parse_string_tmatrix()
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
def parse_string_tmatrix(self, par_str, real):
|
|
|
|
"""
|
|
|
|
Transformation matrix is defined as a set of rows separated
|
|
|
|
by a new line symbol.
|
|
|
|
"""
|
|
|
|
str_rows = par_str.split('\n')
|
|
|
|
try:
|
|
|
|
rows = [map(float, s.split()) for s in str_rows]
|
|
|
|
except ValueError:
|
|
|
|
err_mess = "Cannot parse a matrix string:\n%s"%(par_str)
|
|
|
|
raise ValueError(err_mess)
|
|
|
|
|
|
|
|
nr = len(rows)
|
|
|
|
nm = len(rows[0])
|
|
|
|
|
|
|
|
err_mess = "Number of columns must be the same:\n%s"%(par_str)
|
|
|
|
for row in rows:
|
|
|
|
assert len(row) == nm, err_mess
|
|
|
|
|
|
|
|
if real:
|
|
|
|
mat = np.array(rows)
|
|
|
|
else:
|
|
|
|
err_mess = "Complex matrix must contain 2*M values:\n%s"%(par_str)
|
|
|
|
assert 2 * (nm / 2) == nm, err_mess
|
|
|
|
|
|
|
|
tmp = np.array(rows, dtype=np.complex128)
|
|
|
|
mat = tmp[:, 0::2] + 1.0j * tmp[:, 1::2]
|
|
|
|
|
|
|
|
return mat
|
2015-02-13 21:58:42 +01:00
|
|
|
|
2015-11-11 11:36:09 +01:00
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# parse_file_tmatrix()
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
def parse_file_tmatrix(self, filename):
|
|
|
|
"""
|
|
|
|
Parses a file 'filename' containing transformation matrices
|
|
|
|
for each ion. The parser returns a raw matrix that will be
|
|
|
|
interpreted elsewhere because the interpretation depends on
|
|
|
|
shell parameters.
|
|
|
|
"""
|
|
|
|
tmatrices = np.loadtxt(filename)
|
|
|
|
return tmatrices
|
|
|
|
|
2015-02-13 21:58:42 +01:00
|
|
|
################################################################################
|
2015-11-10 16:40:46 +01:00
|
|
|
#
|
|
|
|
# parse_string_ion_list()
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
def parse_string_dosmesh(self, par_str):
|
|
|
|
"""
|
|
|
|
Two formats are accepted:
|
|
|
|
|
2015-11-11 11:36:09 +01:00
|
|
|
1. Two floats (energy range) and an integer (number of energy points).
|
2015-11-10 16:40:46 +01:00
|
|
|
|
2015-11-11 11:36:09 +01:00
|
|
|
2. One integer (number of energy points). In this case the energy
|
|
|
|
range is taken to be equal to EMIN, EMAX of a shell.
|
2015-11-10 16:40:46 +01:00
|
|
|
|
|
|
|
The parser returns a dictionary:
|
|
|
|
{'n_points': int,
|
|
|
|
'emin': float,
|
|
|
|
'emax': float}
|
|
|
|
|
|
|
|
If the second option is used, 'emin' and 'emax' are undefined
|
|
|
|
and set to 'nan'.
|
|
|
|
"""
|
|
|
|
stmp = par_str.split()
|
|
|
|
if len(stmp) == 3:
|
|
|
|
emin, emax = float(stmp[0]), float(stmp[1])
|
|
|
|
n_points = int(stmp[2])
|
|
|
|
elif len(stmp) == 1:
|
|
|
|
n_points = int(stmp[0])
|
|
|
|
emin = emax = float('nan')
|
|
|
|
else:
|
|
|
|
err_mess = "DOSMESH must be either 'EMIN EMAX NPOINTS' or 'NPOINTS'"
|
|
|
|
raise ValueError(err_mess)
|
|
|
|
|
|
|
|
dos_pars = {
|
|
|
|
'n_points': n_points,
|
|
|
|
'emin': emin,
|
|
|
|
'emax': emax}
|
|
|
|
|
|
|
|
return dos_pars
|
|
|
|
|
|
|
|
################################################################################
|
2015-02-13 21:58:42 +01:00
|
|
|
#
|
|
|
|
# parse_parameter_set()
|
|
|
|
#
|
|
|
|
################################################################################
|
2015-02-17 19:19:13 +01:00
|
|
|
def parse_parameter_set(self, section, param_set, exception=False, defaults=True):
|
2015-02-13 21:58:42 +01:00
|
|
|
"""
|
|
|
|
Parses required or optional parameter set from a section.
|
|
|
|
For required parameters `exception=True` must be set.
|
|
|
|
"""
|
|
|
|
parsed = {}
|
|
|
|
for par in param_set.keys():
|
2015-02-17 19:19:13 +01:00
|
|
|
key = param_set[par][0]
|
2015-02-13 21:58:42 +01:00
|
|
|
try:
|
|
|
|
par_str = self.cp.get(section, par)
|
2015-08-12 12:10:31 +02:00
|
|
|
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
|
2015-02-13 21:58:42 +01:00
|
|
|
if exception:
|
|
|
|
message = "Required parameter '%s' not found in section [%s]"%(par, section)
|
2015-02-13 22:24:48 +01:00
|
|
|
raise Exception(message)
|
2015-02-13 21:58:42 +01:00
|
|
|
else:
|
2015-02-17 19:19:13 +01:00
|
|
|
# Use the default value if there is one
|
|
|
|
if defaults and len(param_set[par]) > 2:
|
|
|
|
parsed[key] = param_set[par][2]
|
2015-02-13 21:58:42 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
if self.verbosity > 0:
|
|
|
|
print " %s = %s"%(par, par_str)
|
|
|
|
|
|
|
|
parse_fun = param_set[par][1]
|
|
|
|
parsed[key] = parse_fun(par_str)
|
|
|
|
|
|
|
|
return parsed
|
|
|
|
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# parse_shells()
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
def parse_shells(self):
|
|
|
|
"""
|
|
|
|
Parses all [Shell] sections.
|
|
|
|
"""
|
|
|
|
# Find all [Shell] sections
|
|
|
|
# (note that ConfigParser transforms all names to lower case)
|
|
|
|
sections = self.cp.sections()
|
|
|
|
|
2015-02-13 22:24:48 +01:00
|
|
|
sh_patt1 = re.compile('shell +.*', re.IGNORECASE)
|
|
|
|
sec_shells = filter(sh_patt1.match, sections)
|
2015-02-13 21:58:42 +01:00
|
|
|
|
|
|
|
self.nshells = len(sec_shells)
|
|
|
|
assert self.nshells > 0, "No projected shells found in the input file"
|
|
|
|
|
|
|
|
if self.verbosity > 0:
|
|
|
|
print
|
|
|
|
if self.nshells > 1:
|
|
|
|
print " Found %i projected shells"%(self.nshells)
|
|
|
|
else:
|
|
|
|
print " Found 1 projected shell"
|
|
|
|
|
|
|
|
# Get shell indices
|
2015-02-13 22:24:48 +01:00
|
|
|
sh_patt2 = re.compile('shell +([0-9]*)$', re.IGNORECASE)
|
2015-02-13 21:58:42 +01:00
|
|
|
try:
|
2015-02-13 22:24:48 +01:00
|
|
|
get_ind = lambda s: int(sh_patt2.match(s).groups()[0])
|
2015-02-13 21:58:42 +01:00
|
|
|
sh_inds = map(get_ind, sec_shells)
|
2015-02-13 22:24:48 +01:00
|
|
|
except (ValueError, AttributeError):
|
2015-02-13 21:58:42 +01:00
|
|
|
raise ValueError("Failed to extract shell indices from a list: %s"%(sec_shells))
|
|
|
|
|
2015-02-13 22:24:48 +01:00
|
|
|
self.sh_sections = {ind: sec for ind, sec in it.izip(sh_inds, sec_shells)}
|
2015-02-13 21:58:42 +01:00
|
|
|
|
|
|
|
# Check that all indices are unique
|
2015-02-13 22:24:48 +01:00
|
|
|
# In principle redundant because the list of sections will contain only unique names
|
2015-02-13 21:58:42 +01:00
|
|
|
assert len(sh_inds) == len(set(sh_inds)), "There must be no shell with the same index!"
|
|
|
|
|
|
|
|
# Ideally, indices should run from 1 to <nshells>
|
|
|
|
# If it's not the case, issue a warning
|
|
|
|
sh_inds.sort()
|
|
|
|
if sh_inds != range(1, len(sh_inds) + 1):
|
|
|
|
issue_warning("Shell indices are not uniform or not starting from 1. "
|
2015-02-13 22:24:48 +01:00
|
|
|
"This might be an indication of a incorrect setup.")
|
2015-02-13 21:58:42 +01:00
|
|
|
|
2015-02-15 16:42:48 +01:00
|
|
|
# Parse shell parameters and put them into a list sorted according to the original indices
|
|
|
|
self.shells = []
|
2015-02-13 21:58:42 +01:00
|
|
|
for ind in sh_inds:
|
2015-02-15 16:42:48 +01:00
|
|
|
shell = {}
|
|
|
|
# Store the original user-defined index
|
|
|
|
shell['user_index'] = ind
|
2015-02-13 21:58:42 +01:00
|
|
|
section = self.sh_sections[ind]
|
|
|
|
|
|
|
|
if self.verbosity > 0:
|
|
|
|
print
|
2015-02-16 11:30:31 +01:00
|
|
|
print " Shell parameters:"
|
|
|
|
# Shell required parameters
|
2015-02-13 21:58:42 +01:00
|
|
|
parsed = self.parse_parameter_set(section, self.sh_required, exception=True)
|
2015-02-15 16:42:48 +01:00
|
|
|
shell.update(parsed)
|
2015-02-13 21:58:42 +01:00
|
|
|
|
|
|
|
# Shell optional parameters
|
|
|
|
parsed = self.parse_parameter_set(section, self.sh_optional, exception=False)
|
2015-02-15 16:42:48 +01:00
|
|
|
shell.update(parsed)
|
2015-02-13 21:58:42 +01:00
|
|
|
|
|
|
|
# Group required parameters
|
|
|
|
# Must be given if no group is explicitly specified
|
|
|
|
# If in conflict with the [Group] section, the latter has a priority
|
|
|
|
parsed = self.parse_parameter_set(section, self.gr_required, exception=False)
|
2015-02-15 16:42:48 +01:00
|
|
|
shell.update(parsed)
|
2015-02-13 21:58:42 +01:00
|
|
|
|
|
|
|
# Group optional parameters
|
2015-02-17 19:19:13 +01:00
|
|
|
parsed = self.parse_parameter_set(section, self.gr_optional, exception=False, defaults=False)
|
2015-02-15 16:42:48 +01:00
|
|
|
shell.update(parsed)
|
|
|
|
|
|
|
|
self.shells.append(shell)
|
2015-02-13 21:58:42 +01:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# parse_groups()
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
def parse_groups(self):
|
|
|
|
"""
|
|
|
|
Parses [Group] sections.
|
|
|
|
"""
|
|
|
|
# Find group sections
|
|
|
|
sections = self.cp.sections()
|
|
|
|
|
2015-02-13 23:22:26 +01:00
|
|
|
gr_patt = re.compile('group +(.*)', re.IGNORECASE)
|
2015-02-13 22:24:48 +01:00
|
|
|
sec_groups = filter(gr_patt.match, sections)
|
2015-02-13 21:58:42 +01:00
|
|
|
|
|
|
|
self.ngroups = len(sec_groups)
|
|
|
|
|
2015-02-15 16:42:48 +01:00
|
|
|
self.groups = []
|
|
|
|
# Parse group parameters
|
|
|
|
for section in sec_groups:
|
|
|
|
group = {}
|
|
|
|
|
|
|
|
# Extract group index (FIXME: do we really need it?)
|
|
|
|
gr_patt2 = re.compile('group +([0-9]*)$', re.IGNORECASE)
|
|
|
|
try:
|
|
|
|
gr_ind = int(gr_patt2.match(section).groups()[0])
|
|
|
|
except (ValueError, AttributeError):
|
|
|
|
raise ValueError("Failed to extract group index from a group name: %s"%(section))
|
|
|
|
group['index'] = gr_ind
|
|
|
|
|
|
|
|
if self.verbosity > 0:
|
|
|
|
print
|
2015-02-16 11:30:31 +01:00
|
|
|
print " Group parameters:"
|
|
|
|
# Group required parameters
|
2015-02-15 16:42:48 +01:00
|
|
|
parsed = self.parse_parameter_set(section, self.gr_required, exception=True)
|
|
|
|
group.update(parsed)
|
|
|
|
|
|
|
|
# Group optional parameters
|
|
|
|
parsed = self.parse_parameter_set(section, self.gr_optional, exception=False)
|
|
|
|
group.update(parsed)
|
|
|
|
|
|
|
|
self.groups.append(group)
|
|
|
|
|
|
|
|
# Sort groups according to indices defined in the config-file
|
|
|
|
if self.ngroups > 0:
|
|
|
|
self.groups.sort(key=lambda g: g['index'])
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# groups_shells_consistency()
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
def groups_shells_consistency(self):
|
|
|
|
"""
|
|
|
|
Ensures consistency between groups and shells.
|
|
|
|
In particular:
|
|
|
|
- if no groups are explicitly defined and only shell is defined create
|
|
|
|
a group automatically
|
|
|
|
- check the existance of all shells referenced in the groups
|
|
|
|
- check that all shells are referenced in the groups
|
|
|
|
"""
|
2015-02-13 21:58:42 +01:00
|
|
|
# Special case: no groups is defined
|
|
|
|
if self.ngroups == 0:
|
|
|
|
# Check that 'nshells = 1'
|
|
|
|
assert self.nshells == 1, "At least one group must be defined if there are more than one shells."
|
|
|
|
|
|
|
|
# Otherwise create a single group taking group information from [Shell] section
|
2015-02-15 16:42:48 +01:00
|
|
|
self.groups.append({})
|
2015-02-19 15:04:43 +01:00
|
|
|
self.groups[0]['index'] = '1'
|
2015-02-13 21:58:42 +01:00
|
|
|
# Check that the single '[Shell]' section contains enough information
|
2015-02-16 11:54:54 +01:00
|
|
|
# (required group parameters except 'shells')
|
2015-02-13 21:58:42 +01:00
|
|
|
# and move it to the `groups` dictionary
|
2015-02-16 11:54:54 +01:00
|
|
|
sh_gr_required = dict(self.gr_required)
|
|
|
|
sh_gr_required.pop('shells')
|
2015-02-13 21:58:42 +01:00
|
|
|
try:
|
2015-02-16 11:54:54 +01:00
|
|
|
for par in sh_gr_required.keys():
|
|
|
|
key = sh_gr_required[par][0]
|
2015-02-15 16:42:48 +01:00
|
|
|
value = self.shells[0].pop(key)
|
2015-02-13 21:58:42 +01:00
|
|
|
self.groups[0][key] = value
|
|
|
|
except KeyError:
|
|
|
|
message = "One [Shell] section is specified but no explicit [Group] section is provided."
|
|
|
|
message += " In this case the [Shell] section must contain all required group information.\n"
|
2015-02-16 11:54:54 +01:00
|
|
|
message += " Required parameters are: %s"%(sh_gr_required.keys())
|
2015-02-13 21:58:42 +01:00
|
|
|
raise KeyError(message)
|
|
|
|
|
|
|
|
# Do the same for optional group parameters, but do not raise an exception
|
2015-02-15 16:42:48 +01:00
|
|
|
for par in self.gr_optional.keys():
|
2015-02-13 21:58:42 +01:00
|
|
|
try:
|
2015-02-15 16:42:48 +01:00
|
|
|
key = self.gr_optional[par][0]
|
2015-02-19 15:04:43 +01:00
|
|
|
value = self.shells[0].pop(key)
|
2015-02-13 21:58:42 +01:00
|
|
|
self.groups[0][key] = value
|
|
|
|
except KeyError:
|
2015-02-19 17:25:03 +01:00
|
|
|
if len(self.gr_optional[par]) > 2:
|
|
|
|
self.groups[0][key] = self.gr_optional[par][2]
|
2015-02-13 21:58:42 +01:00
|
|
|
continue
|
2015-02-15 16:42:48 +01:00
|
|
|
# Add the index of the single shell into the group
|
2015-02-19 15:04:43 +01:00
|
|
|
self.groups[0].update({'shells': [1]})
|
2015-02-15 16:42:48 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# Consistency checks
|
|
|
|
#
|
|
|
|
# Check the existance of shells referenced in the groups
|
|
|
|
def find_shell_by_user_index(uindex):
|
|
|
|
for ind, shell in enumerate(self.shells):
|
|
|
|
if shell['user_index'] == uindex:
|
2015-02-16 12:02:11 +01:00
|
|
|
return ind, shell
|
2015-02-15 16:42:48 +01:00
|
|
|
raise KeyError
|
|
|
|
|
2015-02-16 12:25:10 +01:00
|
|
|
sh_all_inds = []
|
2015-02-15 16:42:48 +01:00
|
|
|
for group in self.groups:
|
|
|
|
gr_shells = group['shells']
|
2015-02-16 12:25:10 +01:00
|
|
|
sh_inds = []
|
2015-02-15 16:42:48 +01:00
|
|
|
for user_ind in gr_shells:
|
|
|
|
try:
|
|
|
|
ind, shell = find_shell_by_user_index(user_ind)
|
|
|
|
except KeyError:
|
2015-02-16 12:02:11 +01:00
|
|
|
raise Exception("Shell %i referenced in group '%s' does not exist"%(user_ind, group['index']))
|
2015-02-15 16:42:48 +01:00
|
|
|
sh_inds.append(ind)
|
|
|
|
|
2015-02-16 12:25:10 +01:00
|
|
|
# If [Shell] section contains (potentially conflicting) group parameters
|
|
|
|
# remove them and issue a warning.
|
|
|
|
#
|
2015-02-15 16:42:48 +01:00
|
|
|
# First, required group parameters
|
|
|
|
for par in self.gr_required.keys():
|
|
|
|
try:
|
|
|
|
key = self.gr_required[par][0]
|
|
|
|
value = shell.pop(key)
|
|
|
|
mess = (" Redundant group parameter '%s' in [Shell] section"
|
|
|
|
" %i is discarded"%(par, user_ind))
|
|
|
|
issue_warning(mess)
|
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Second, optional group parameters
|
|
|
|
for par in self.gr_optional.keys():
|
|
|
|
try:
|
|
|
|
key = self.gr_optional[par][0]
|
|
|
|
value = shell.pop(key)
|
|
|
|
mess = (" Redundant group parameter '%s' in [Shell] section"
|
|
|
|
" %i is discarded"%(par, user_ind))
|
|
|
|
issue_warning(mess)
|
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
|
2015-02-16 12:25:10 +01:00
|
|
|
sh_all_inds += sh_inds
|
|
|
|
# Replace user shell indices with internal ones
|
|
|
|
group['shells'] = sh_inds
|
|
|
|
|
|
|
|
sh_refs_used = list(set(sh_all_inds))
|
2015-02-15 16:42:48 +01:00
|
|
|
sh_refs_used.sort()
|
|
|
|
|
|
|
|
# Check that all shells are referenced in the groups
|
|
|
|
assert sh_refs_used == range(self.nshells), "Some shells are not inside any of the groups"
|
|
|
|
|
|
|
|
|
2015-02-15 16:59:51 +01:00
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# parse_general()
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
def parse_general(self):
|
|
|
|
"""
|
|
|
|
Parses [General] section.
|
|
|
|
"""
|
2015-08-12 12:10:31 +02:00
|
|
|
self.general = {}
|
|
|
|
sections = self.cp.sections()
|
|
|
|
gen_section = filter(lambda s: s.lower() == 'general', sections)
|
|
|
|
# If no [General] section is found parse a dummy section name to the parser
|
|
|
|
# to reset parameters to their default values
|
|
|
|
if len(gen_section) > 1:
|
|
|
|
raise Exception("More than one section [General] is found")
|
|
|
|
if len(gen_section) == 0:
|
|
|
|
gen_section = 'general'
|
|
|
|
gen_section = gen_section[0]
|
|
|
|
parsed = self.parse_parameter_set(gen_section, self.gen_optional, exception=False)
|
|
|
|
self.general.update(parsed)
|
2015-02-13 21:58:42 +01:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
#
|
2015-02-16 12:25:10 +01:00
|
|
|
# Main parser function
|
2015-02-13 21:58:42 +01:00
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
def parse_input(self):
|
|
|
|
"""
|
|
|
|
Parses input conf-file.
|
|
|
|
"""
|
2015-02-15 16:59:51 +01:00
|
|
|
self.parse_general()
|
2015-02-13 21:58:42 +01:00
|
|
|
self.parse_shells()
|
|
|
|
self.parse_groups()
|
|
|
|
|
2015-02-15 16:42:48 +01:00
|
|
|
self.groups_shells_consistency()
|
2015-02-13 21:58:42 +01:00
|
|
|
|
2015-02-16 17:14:24 +01:00
|
|
|
#
|
|
|
|
# Obsolete part
|
|
|
|
#
|
2015-02-13 21:58:42 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
narg = len(sys.argv)
|
|
|
|
if narg < 2:
|
|
|
|
raise SystemExit(" Usage: python pyconf.py <conf-file> [<path-to-vasp-calcultaion>]")
|
|
|
|
else:
|
|
|
|
filename = sys.argv[1]
|
|
|
|
if narg > 2:
|
|
|
|
vasp_dir = sys.argv[2]
|
|
|
|
if vasp_dir[-1] != '/':
|
|
|
|
vasp_dir += '/'
|
|
|
|
else:
|
|
|
|
vasp_dir = './'
|
|
|
|
|
|
|
|
|
|
|
|
# plocar = vaspio.Plocar()
|
|
|
|
# plocar.from_file(vasp_dir)
|
|
|
|
# poscar = vaspio.Poscar()
|
|
|
|
# poscar.from_file(vasp_dir)
|
|
|
|
# kpoints = vaspio.Kpoints()
|
|
|
|
# kpoints.from_file(vasp_dir)
|
|
|
|
eigenval = vaspio.Eigenval()
|
|
|
|
eigenval.from_file(vasp_dir)
|
|
|
|
doscar = vaspio.Doscar()
|
|
|
|
doscar.from_file(vasp_dir)
|
|
|
|
# pars = parse_input(filename)
|
|
|
|
|