From e8315d7341d31f0cca6e37d1e58bb3cd118b30b0 Mon Sep 17 00:00:00 2001 From: Hao-Jen You <65796567+Youhaojen@users.noreply.github.com> Date: Sat, 14 Sep 2024 01:43:14 +0800 Subject: [PATCH 1/2] fix "ValueError: could not convert string to float: 'ATOMIC_POSITIONS'" Signed-off-by: Hao-Jen You <65796567+Youhaojen@users.noreply.github.com> --- dpdata/qe/scf.py | 73 ++++++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/dpdata/qe/scf.py b/dpdata/qe/scf.py index f86708605..4044a2dc7 100755 --- a/dpdata/qe/scf.py +++ b/dpdata/qe/scf.py @@ -12,47 +12,65 @@ kbar2evperang3 = 1e3 / 1.602176621e6 -def get_block(lines, keyword, skip=0): - ret = [] - for idx, ii in enumerate(lines): - if keyword in ii: - blk_idx = idx + 1 + skip - while len(lines[blk_idx]) == 0: - blk_idx += 1 - while len(lines[blk_idx]) != 0 and blk_idx != len(lines): - ret.append(lines[blk_idx]) - blk_idx += 1 +def get_block(lines, start_marker): + start_idx = None + for idx, line in enumerate(lines): + if start_marker in line: + start_idx = idx + 1 break - return ret - + if start_idx is None: + raise RuntimeError(f"{start_marker} not found in the input lines.") + + block = [] + for line in lines[start_idx:]: + if line.strip() == "" or line.strip().startswith("&"): + break + block.append(line.strip()) + return block + +def get_block(lines, start_marker, skip=0): + start_idx = None + for idx, line in enumerate(lines): + if start_marker in line: + start_idx = idx + 1 + skip + break + if start_idx is None: + raise RuntimeError(f"{start_marker} not found in the input lines.") + + block = [] + for line in lines[start_idx:]: + if line.strip() == "" or line.strip().startswith("&"): + break + block.append(line.strip()) + return block def get_cell(lines): - ret = [] - for idx, ii in enumerate(lines): - if "ibrav" in ii: + for idx, line in enumerate(lines): + if "ibrav" in line: + ibrav = int(line.replace(",", "").split("=")[-1]) break - blk = lines[idx : idx + 2] - ibrav = int(blk[0].replace(",", "").split("=")[-1]) + else: + raise RuntimeError("ibrav not found in the input lines.") + if ibrav == 0: - for iline in lines: - if "CELL_PARAMETERS" in iline and "angstrom" not in iline.lower(): + for line in lines: + if "CELL_PARAMETERS" in line and "angstrom" not in line.lower(): raise RuntimeError( "CELL_PARAMETERS must be written in Angstrom. Other units are not supported yet." ) blk = get_block(lines, "CELL_PARAMETERS") - for ii in blk: - ret.append([float(jj) for jj in ii.split()[0:3]]) + ret = [] + for line in blk: + ret.append([float(value) for value in line.split()[0:3]]) ret = np.array(ret) elif ibrav == 1: a = None - for iline in lines: - line = iline.replace("=", " ").replace(",", "").split() + for line in lines: + line = line.replace("=", " ").replace(",", "").split() if len(line) >= 2 and "a" == line[0]: - # print("line = ", line) a = float(line[1]) if len(line) >= 2 and "celldm(1)" == line[0]: a = float(line[1]) * bohr2ang - # print("a = ", a) if not a: raise RuntimeError("parameter 'a' or 'celldm(1)' cannot be found.") ret = np.array([[a, 0.0, 0.0], [0.0, a, 0.0], [0.0, 0.0, a]]) @@ -60,7 +78,6 @@ def get_cell(lines): raise RuntimeError("ibrav > 1 not supported yet.") return ret - def get_coords(lines, cell): coord = [] atom_symbol_list = [] @@ -100,7 +117,6 @@ def get_coords(lines, cell): return list(atom_names), atom_numbs, atom_types, coord - def get_energy(lines): energy = None for ii in lines: @@ -108,7 +124,6 @@ def get_energy(lines): energy = ry2ev * float(ii.split("=")[1].split()[0]) return energy - def get_force(lines, natoms): blk = get_block(lines, "Forces acting on atoms", skip=1) ret = [] @@ -119,7 +134,6 @@ def get_force(lines, natoms): ret *= ry2ev / bohr2ang return ret - def get_stress(lines): blk = get_block(lines, "total stress") if len(blk) == 0: @@ -131,7 +145,6 @@ def get_stress(lines): ret *= kbar2evperang3 return ret - def get_frame(fname): if isinstance(fname, str): path_out = fname From 1c853121d438eb4e9be417d0e4f5d024a063f5a4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 13 Sep 2024 17:44:09 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dpdata/qe/scf.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/dpdata/qe/scf.py b/dpdata/qe/scf.py index 4044a2dc7..8c5299d07 100755 --- a/dpdata/qe/scf.py +++ b/dpdata/qe/scf.py @@ -20,7 +20,7 @@ def get_block(lines, start_marker): break if start_idx is None: raise RuntimeError(f"{start_marker} not found in the input lines.") - + block = [] for line in lines[start_idx:]: if line.strip() == "" or line.strip().startswith("&"): @@ -28,6 +28,7 @@ def get_block(lines, start_marker): block.append(line.strip()) return block + def get_block(lines, start_marker, skip=0): start_idx = None for idx, line in enumerate(lines): @@ -36,7 +37,7 @@ def get_block(lines, start_marker, skip=0): break if start_idx is None: raise RuntimeError(f"{start_marker} not found in the input lines.") - + block = [] for line in lines[start_idx:]: if line.strip() == "" or line.strip().startswith("&"): @@ -44,6 +45,7 @@ def get_block(lines, start_marker, skip=0): block.append(line.strip()) return block + def get_cell(lines): for idx, line in enumerate(lines): if "ibrav" in line: @@ -51,7 +53,7 @@ def get_cell(lines): break else: raise RuntimeError("ibrav not found in the input lines.") - + if ibrav == 0: for line in lines: if "CELL_PARAMETERS" in line and "angstrom" not in line.lower(): @@ -78,6 +80,7 @@ def get_cell(lines): raise RuntimeError("ibrav > 1 not supported yet.") return ret + def get_coords(lines, cell): coord = [] atom_symbol_list = [] @@ -117,6 +120,7 @@ def get_coords(lines, cell): return list(atom_names), atom_numbs, atom_types, coord + def get_energy(lines): energy = None for ii in lines: @@ -124,6 +128,7 @@ def get_energy(lines): energy = ry2ev * float(ii.split("=")[1].split()[0]) return energy + def get_force(lines, natoms): blk = get_block(lines, "Forces acting on atoms", skip=1) ret = [] @@ -134,6 +139,7 @@ def get_force(lines, natoms): ret *= ry2ev / bohr2ang return ret + def get_stress(lines): blk = get_block(lines, "total stress") if len(blk) == 0: @@ -145,6 +151,7 @@ def get_stress(lines): ret *= kbar2evperang3 return ret + def get_frame(fname): if isinstance(fname, str): path_out = fname