From 6ba9e77e9779a7d7c76e463292e91b98ed5946bf Mon Sep 17 00:00:00 2001 From: Nick Papior Date: Fri, 15 Jun 2018 08:51:51 +0200 Subject: [PATCH] bug: fixed number reg-exp finding in Siesta interface, fixes #71 The regexp for finding numbers could not find 1. (without a trailing 0). In such cases the tag would never be found and instantiated correctly. Secondly, I have corrected some problems that may occur when units are specified in lower case. Lastly, any unknown unit is now raising an error. This fixes #71. Signed-off-by: Nick Papior --- phonopy/interface/siesta.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/phonopy/interface/siesta.py b/phonopy/interface/siesta.py index 4bceb321..a746b2d9 100644 --- a/phonopy/interface/siesta.py +++ b/phonopy/interface/siesta.py @@ -73,6 +73,7 @@ def parse_set_of_forces(num_atoms, forces_filenames, verbose=True): def read_siesta(filename): siesta_in = SiestaIn(open(filename).read()) numbers = siesta_in._tags["atomicnumbers"] + alat = siesta_in._tags["latticeconstant"] lattice = siesta_in._tags["latticevectors"] positions = siesta_in._tags["atomiccoordinates"] atypes = siesta_in._tags["chemicalspecieslabel"] @@ -82,10 +83,7 @@ def read_siesta(filename): if coordformat == "fractional" or coordformat == "scaledbylatticevectors": cell.set_scaled_positions(positions) elif coordformat == "scaledcartesian": - if siesta_in._tags['latticeconstant'] == 'ang': - cell.set_positions(np.array(positions) / Bohr) - else: - cell.set_positions(np.array(positions)) + cell.set_positions(np.array(positions) * alat) elif coordformat == "notscaledcartesianang" or coordformat == "ang": cell.set_positions(np.array(positions) / Bohr) elif coordformat == "notscaledcartesianbohr" or coordformat == "bohr": @@ -134,7 +132,7 @@ def get_siesta_structure(cell,atypes): return lines class SiestaIn(object): - _num_regex = '([+-]?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?)' + _num_regex = '([+-]?\d+(?:\.\d*)?(?:[eE][-+]?\d+)?)' _tags = { "latticeconstant": 1.0, "latticeconstantunit": None, "chemicalspecieslabel": None, @@ -153,15 +151,18 @@ class SiestaIn(object): - atomic_species """ for tag,value,unit in re.findall( - '([\.A-Za-z]+)\s+?%s(?:[ ]+)?([A-Za-z]+)?' % + '([\.A-Za-z]+)\s+%s\s+([A-Za-z]+)?' % self._num_regex,lines): tag = tag.lower() + unit = unit.lower() if tag == "latticeconstant": - self._tags['latticeconstantunit'] = unit.lower() - if unit == 'Ang': + self._tags['latticeconstantunit'] = unit.capitalize() + if unit == 'ang': self._tags[tag] = float(value) / Bohr - else: + elif unit == 'bohr': self._tags[tag] = float(value) + else: + raise ValueError('Unknown LatticeConstant unit: {}'.format(unit)) for tag,value in re.findall('([\.A-Za-z]+)[ \t]+([a-zA-Z]+)',lines): tag = tag.replace('_','').lower()