make dump.py files consistent and improve python 2/3 portability

This commit is contained in:
Axel Kohlmeyer
2022-08-17 16:15:03 -04:00
parent 1b979be939
commit 7b9b056c98
3 changed files with 86 additions and 79 deletions

View File

@ -16,15 +16,15 @@ oneline = "Read, write, manipulate dump files and particle attributes"
docstr = """
d = dump("dump.one") read in one or more dump files
d = dump("dump.1 dump.2.gz") can be gzipped
d = dump("dump.*") wildcard expands to multiple files
d = dump("dump.*",0) two args = store filenames, but don't read
d = dump("dump.1 dump.2.gz") can be gzipped
d = dump("dump.*") wildcard expands to multiple files
d = dump("dump.*",0) two args = store filenames, but don't read
incomplete and duplicate snapshots are deleted
if atoms have 5 or 8 columns, assign id,type,x,y,z (ix,iy,iz)
atoms will be unscaled if stored in files as scaled
time = d.next() read next snapshot from dump files
time = d.next() read next snapshot from dump files
used with 2-argument constructor to allow reading snapshots one-at-a-time
snapshot will be skipped only if another snapshot has same time stamp
@ -36,20 +36,20 @@ d.map(1,"id",3,"x") assign names to atom columns (1-N)
not needed if dump file is self-describing
d.tselect.all() select all timesteps
d.tselect.one(N) select only timestep N
d.tselect.none() deselect all timesteps
d.tselect.skip(M) select every Mth step
d.tselect.all() select all timesteps
d.tselect.one(N) select only timestep N
d.tselect.none() deselect all timesteps
d.tselect.skip(M) select every Mth step
d.tselect.test("$t >= 100 and $t < 10000") select matching timesteps
d.delete() delete non-selected timesteps
d.delete() delete non-selected timesteps
selecting a timestep also selects all atoms in the timestep
skip() and test() only select from currently selected timesteps
test() uses a Python Boolean expression with $t for timestep value
Python comparison syntax: == != < > <= >= and or
d.aselect.all() select all atoms in all steps
d.aselect.all(N) select all atoms in one step
d.aselect.all() select all atoms in all steps
d.aselect.all(N) select all atoms in one step
d.aselect.test("$id > 100 and $type == 2") select match atoms in all steps
d.aselect.test("$id > 100 and $type == 2",N) select matching atoms in one step
@ -60,24 +60,24 @@ d.aselect.test("$id > 100 and $type == 2",N) select matching atoms in one step
Python comparison syntax: == != < > <= >= and or
$name must end with a space
d.write("file") write selected steps/atoms to dump file
d.write("file",head,app) write selected steps/atoms to dump file
d.scatter("tmp") write selected steps/atoms to multiple files
d.write("file") write selected steps/atoms to dump file
d.write("file",head,app) write selected steps/atoms to dump file
d.scatter("tmp") write selected steps/atoms to multiple files
write() can be specified with 2 additional flags
headd = 0/1 for no/yes snapshot header, app = 0/1 for write vs append
scatter() files are given timestep suffix: e.g. tmp.0, tmp.100, etc
d.scale() scale x,y,z to 0-1 for all timesteps
d.scale(100) scale atom coords for timestep N
d.unscale() unscale x,y,z to box size to all timesteps
d.unscale(1000) unscale atom coords for timestep N
d.wrap() wrap x,y,z into periodic box via ix,iy,iz
d.unwrap() unwrap x,y,z out of box via ix,iy,iz
d.owrap("other") wrap x,y,z to same image as another atom
d.sort() sort atoms by atom ID in all selected steps
d.sort("x") sort atoms by column value in all steps
d.sort(1000) sort atoms in timestep N
d.scale() scale x,y,z to 0-1 for all timesteps
d.scale(100) scale atom coords for timestep N
d.unscale() unscale x,y,z to box size to all timesteps
d.unscale(1000) unscale atom coords for timestep N
d.wrap() wrap x,y,z into periodic box via ix,iy,iz
d.unwrap() unwrap x,y,z out of box via ix,iy,iz
d.owrap("other") wrap x,y,z to same image as another atom
d.sort() sort atoms by atom ID in all selected steps
d.sort("x") sort atoms by column value in all steps
d.sort(1000) sort atoms in timestep N
scale(), unscale(), wrap(), unwrap(), owrap() operate on all steps and atoms
wrap(), unwrap(), owrap() require ix,iy,iz be defined
@ -89,8 +89,8 @@ d.sort(1000) sort atoms in timestep N
m1,m2 = d.minmax("type") find min/max values for a column
d.set("$ke = $vx * $vx + $vy * $vy") set a column to a computed value
d.setv("type",vector) set a column to a vector of values
d.spread("ke",N,"color") 2nd col = N ints spread over 1st col
d.clone(1000,"color") clone timestep N values to other steps
d.spread("ke",N,"color") 2nd col = N ints spread over 1st col
d.clone(1000,"color") clone timestep N values to other steps
minmax() operates on selected timesteps and atoms
set() operates on selected timesteps and atoms
@ -111,7 +111,7 @@ d.clone(1000,"color") clone timestep N values to other steps
values at every timestep are set to value at timestep N for that atom ID
useful for propagating a color map
t = d.time() return vector of selected timestep values
t = d.time() return vector of selected timestep values
fx,fy,... = d.atom(100,"fx","fy",...) return vector(s) for atom ID N
fx,fy,... = d.vecs(1000,"fx","fy",...) return vector(s) for timestep N
@ -121,8 +121,8 @@ fx,fy,... = d.vecs(1000,"fx","fy",...) return vector(s) for timestep N
index,time,flag = d.iterator(0/1) loop over dump snapshots
time,box,atoms,bonds,tris = d.viz(index) return list of viz objects
d.atype = "color" set column returned as "type" by viz
d.extra("dump.bond") read bond list from dump file
d.extra(data) extract bond/tri/line list from data
d.extra("dump.bond") read bond list from dump file
d.extra(data) extract bond/tri/line list from data
iterator() loops over selected timesteps
iterator() called with arg = 0 first time, with arg = 1 on subsequent calls
@ -148,6 +148,7 @@ d.extra(data) extract bond/tri/line list from data
# 12/09, David Hart (SNL): allow use of NumPy or Numeric
# 03/17, Richard Berger (Temple U): improve Python 3 compatibility,
# simplify read_snapshot by using reshape
# 08/22, Axel Kohlmeyer (Temple U): remove Numeric, more Python 2/3 compatibility
# ToDo list
# try to optimize this line in read_snap: words += f.readline().split()
@ -261,7 +262,7 @@ class dump:
snap = self.read_snapshot(f)
while snap:
self.snaps.append(snap)
print(snap.time,end='')
print(snap.time,end=' ')
sys.stdout.flush()
snap = self.read_snapshot(f)
@ -298,9 +299,9 @@ class dump:
# if snapshots are scaled, unscale them
if (not self.names.has_key("x")) or \
(not self.names.has_key("y")) or \
(not self.names.has_key("z")):
if (not "x" in self.names) or \
(not "y" in self.names) or \
(not "z" in self.names):
print("no unscaling could be performed")
elif self.nsnaps > 0:
if self.scaled(self.nsnaps-1): self.unscale()
@ -356,13 +357,13 @@ class dump:
try:
snap = Snap()
item = f.readline()
snap.time = int(f.readline().decode().split()[0]) # just grab 1st field
snap.time = int(f.readline().split()[0]) # just grab 1st field
item = f.readline()
snap.natoms = int(f.readline().decode())
snap.natoms = int(f.readline())
snap.aselect = np.zeros(snap.natoms)
item = f.readline().decode()
item = f.readline()
words = f.readline().split()
snap.xlo,snap.xhi = float(words[0]),float(words[1])
words = f.readline().split()
@ -370,7 +371,7 @@ class dump:
words = f.readline().split()
snap.zlo,snap.zhi = float(words[0]),float(words[1])
item = f.readline().decode()
item = f.readline()
if len(self.names) == 0:
words = item.split()[2:]
if len(words):
@ -384,10 +385,10 @@ class dump:
else: self.names[words[i]] = i
if snap.natoms:
words = f.readline().decode().split()
words = f.readline().split()
ncol = len(words)
for i in range(1,snap.natoms):
words += f.readline().decode().split()
words += f.readline().split()
floats = map(float,words)
atom_data = np.array(list(floats),np.float)
@ -579,10 +580,10 @@ class dump:
def names2str(self):
ncol = len(self.snaps[0].atoms[0])
pairs = self.names.items()
values = self.names.values()
str = ""
for i in range(ncol):
if i in values: str += pairs[values.index(i)][0] + ' '
for p in pairs:
if p[1] == i: str += p[0] + ' '
return str
# --------------------------------------------------------------------
@ -625,7 +626,7 @@ class dump:
else: f = open(file,"a")
for snap in self.snaps:
if not snap.tselect: continue
print(snap.time,end='')
print(snap.time,end=' ')
sys.stdout.flush()
if header:
@ -660,7 +661,7 @@ class dump:
if len(self.snaps): namestr = self.names2str()
for snap in self.snaps:
if not snap.tselect: continue
print(snap.time,end='')
print(snap.time,end=' ')
sys.stdout.flush()
file = root + "." + str(snap.time)
@ -714,7 +715,7 @@ class dump:
list = re.findall(pattern,eq)
lhs = list[0][1:]
if not self.names.has_key(lhs):
if not lhs in self.names:
self.newcolumn(lhs)
for item in list:
@ -734,7 +735,7 @@ class dump:
def setv(self,colname,vec):
print("Setting ...")
if not self.names.has_key(colname):
if not colname in self.names:
self.newcolumn(colname)
icol = self.names[colname]
@ -772,7 +773,7 @@ class dump:
def spread(self,old,n,new):
iold = self.names[old]
if not self.names.has_key(new): self.newcolumn(new)
if not new in self.names: self.newcolumn(new)
inew = self.names[new]
min,max = self.minmax(old)

View File

@ -287,7 +287,7 @@ class pdbfile:
if len(self.files):
for atom in atoms:
id = atom[0]
if self.atomlines.has_key(id):
if id in self.atomlines:
(begin,end) = self.atomlines[id]
line = "%s%8.3f%8.3f%8.3f%s" % (begin,atom[2],atom[3],atom[4],end)
print(line,file=f,end='')