From ccf1fc116290a8be7768f184394b8fc7201e2a2a Mon Sep 17 00:00:00 2001 From: Yixiao Chen Date: Mon, 3 Jun 2019 16:46:15 -0400 Subject: [PATCH] add function to handle fractional coordinates in lammps dump file --- dpdata/lammps/dump.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/dpdata/lammps/dump.py b/dpdata/lammps/dump.py index 6d8185e17..1af637d35 100644 --- a/dpdata/lammps/dump.py +++ b/dpdata/lammps/dump.py @@ -67,6 +67,30 @@ def get_posi(lines) : posis = np.array(posis) return posis[:,1:4] +def get_posi_frac(lines) : + blk, head = _get_block(lines, 'ATOMS') + keys = head.split() + id_idx = keys.index('id') - 2 + xidx = keys.index('xs') - 2 + yidx = keys.index('ys') - 2 + zidx = keys.index('zs') - 2 + sel = (xidx, yidx, zidx) + posis = [] + for ii in blk : + words = ii.split() + posis.append([float(words[id_idx]), float(words[xidx]), float(words[yidx]), float(words[zidx])]) + posis.sort() + posis = np.array(posis) + return posis[:,1:4] + +def safe_get_posi(lines, cell): + try: + posis = get_posi(lines) + except ValueError: + fposis = get_posi_frac(lines) + posis = fposis @ cell + return posis + def get_dumpbox(lines) : blk, h = _get_block(lines, 'BOX BOUNDS') bounds = np.zeros([3,2]) @@ -149,12 +173,12 @@ def system_data(lines, type_map = None, type_idx_zero = True) : system['cells'] = [np.array(cell)] natoms = sum(system['atom_numbs']) system['atom_types'] = get_atype(lines, type_idx_zero = type_idx_zero) - system['coords'] = [get_posi(lines) - np.array(orig)] + system['coords'] = [safe_get_posi(lines, cell) - np.array(orig)] for ii in range(1, len(array_lines)) : bounds, tilt = get_dumpbox(array_lines[ii]) orig, cell = dumpbox2box(bounds, tilt) system['cells'].append(cell) - system['coords'].append(get_posi(array_lines[ii]) - np.array(orig)) + system['coords'].append(safe_get_posi(lines, cell) - np.array(orig)) system['cells'] = np.array(system['cells']) system['coords'] = np.array(system['coords']) return system