mirror of
https://github.com/ParticulateFlow/LPP.git
synced 2025-12-08 06:37:46 +00:00
python 3 compatibility for src/*.py files
move imports to top of files add from __future__ import commands module -> subprocesses module print statement -> print() function exec statement -> exec() function xrange -> range map() -> list(map()) StandardError -> Exception integer division: / -> // x.has_key(y) -> y in x sort(): use functools.cmp_to_key type(x) is y -> isinstance(x, y) raw_input -> input change variable names 'list' to avoid clashes with list class
This commit is contained in:
324
src/dump.py
324
src/dump.py
@ -8,6 +8,14 @@
|
||||
|
||||
# dump tool
|
||||
|
||||
# Imports and external programs
|
||||
|
||||
import sys, subprocess, re, glob, types
|
||||
from os import popen
|
||||
from math import * # any function could be used by set()
|
||||
import os
|
||||
import functools
|
||||
|
||||
oneline = "Read, write, manipulate dump files and particle attributes"
|
||||
|
||||
docstr = """
|
||||
@ -180,19 +188,12 @@ d.extra(obj) extract bond/tri/line info from obj
|
||||
# xlo,xhi,ylo,yhi,zlo,zhi = box bounds (float)
|
||||
# atoms[i][j] = 2d array of floats, i = 0 to natoms-1, j = 0 to ncols-1
|
||||
|
||||
# Imports and external programs
|
||||
|
||||
import sys, commands, re, glob, types
|
||||
from os import popen
|
||||
from math import * # any function could be used by set()
|
||||
import os
|
||||
|
||||
try:
|
||||
import numpy as np
|
||||
oldnumeric = False
|
||||
import numpy as np
|
||||
oldnumeric = False
|
||||
except:
|
||||
import Numeric as np
|
||||
oldnumeric = True
|
||||
import Numeric as np
|
||||
oldnumeric = True
|
||||
|
||||
try: from DEFAULTS import PIZZA_GUNZIP
|
||||
except: PIZZA_GUNZIP = "gunzip"
|
||||
@ -228,7 +229,7 @@ class dump:
|
||||
# check whether to output or not
|
||||
if "debugMode" in dictionary: outputfl = dictionary["debugMode"]
|
||||
|
||||
if outputfl: print "number of subprocess:", os.getpid()
|
||||
if outputfl: print("number of subprocess:", os.getpid())
|
||||
|
||||
self.flist = dictionary["filelist"]
|
||||
self.multiprocflag = 1
|
||||
@ -240,7 +241,7 @@ class dump:
|
||||
self.flist = []
|
||||
for word in words: self.flist += glob.glob(word)
|
||||
if len(self.flist) == 0 and len(input) == 1:
|
||||
raise StandardError,"no dump file specified"
|
||||
raise Exception("no dump file specified")
|
||||
|
||||
if len(input) == 1:
|
||||
self.increment = 0
|
||||
@ -261,31 +262,32 @@ class dump:
|
||||
outputfl = True
|
||||
if "output" in kwargs: outputfl = kwargs["output"]
|
||||
|
||||
if outputfl: print "reading dump file..."
|
||||
if outputfl: print("reading dump file...")
|
||||
|
||||
for i, file in enumerate(self.flist):
|
||||
if file[-3:] == ".gz":
|
||||
f = popen("%s -c %s" % (PIZZA_GUNZIP,file),'r')
|
||||
else: f = open(file)
|
||||
else:
|
||||
f = open(file)
|
||||
|
||||
snap = self.read_snapshot(f)
|
||||
while snap:
|
||||
self.snaps.append(snap)
|
||||
if outputfl: print snap.time,
|
||||
if outputfl: print(snap.time,end=' ')
|
||||
self.fileNums.append(snap.time)
|
||||
sys.stdout.flush()
|
||||
snap = self.read_snapshot(f)
|
||||
|
||||
f.close()
|
||||
if outputfl: print
|
||||
if outputfl: print()
|
||||
|
||||
# sort entries by timestep, cull duplicates
|
||||
|
||||
self.snaps.sort(self.compare_time)
|
||||
self.snaps.sort(key = functools.cmp_to_key(self.compare_time))
|
||||
self.fileNums.sort()
|
||||
self.cull()
|
||||
self.nsnaps = len(self.snaps)
|
||||
#print "read %d snapshots" % self.nsnaps
|
||||
#print("read %d snapshots" % self.nsnaps)
|
||||
|
||||
# select all timesteps and atoms
|
||||
|
||||
@ -294,32 +296,32 @@ class dump:
|
||||
# set default names for atom columns if file wasn't self-describing
|
||||
|
||||
if len(self.snaps) == 0:
|
||||
if outputfl: print "no column assignments made"
|
||||
if outputfl: print("no column assignments made")
|
||||
elif len(self.names):
|
||||
if outputfl: print "assigned columns:",self.names2str()
|
||||
if outputfl: print("assigned columns:",self.names2str())
|
||||
else:
|
||||
if outputfl: print "no column assignments made"
|
||||
if outputfl: print("no column assignments made")
|
||||
pass
|
||||
|
||||
# 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")):
|
||||
print "dump scaling status is unknown"
|
||||
if ("x" not in self.names) or \
|
||||
("y" not in self.names) or \
|
||||
("z" not in self.names):
|
||||
print("dump scaling status is unknown")
|
||||
elif self.nsnaps > 0:
|
||||
if self.scale_original == 1: self.unscale()
|
||||
elif self.scale_original == 0:
|
||||
if outputfl: print "dump is already unscaled"
|
||||
if outputfl: print("dump is already unscaled")
|
||||
else:
|
||||
if outputfl: print "dump scaling status is unknown"
|
||||
if outputfl: print("dump scaling status is unknown")
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# read next snapshot from list of files
|
||||
|
||||
def next(self):
|
||||
|
||||
if not self.increment: raise StandardError,"cannot read incrementally"
|
||||
if not self.increment: raise Exception("cannot read incrementally")
|
||||
|
||||
# read next snapshot in current file using eof as pointer
|
||||
# if fail, try next file
|
||||
@ -345,10 +347,11 @@ class dump:
|
||||
# select the new snapshot with all its atoms
|
||||
|
||||
self.snaps.append(snap)
|
||||
self.fileNums.append(snap.time)
|
||||
snap = self.snaps[self.nsnaps]
|
||||
snap.tselect = 1
|
||||
snap.nselect = snap.natoms
|
||||
for i in xrange(snap.natoms): snap.aselect[i] = 1
|
||||
for i in range(snap.natoms): snap.aselect[i] = 1
|
||||
self.nsnaps += 1
|
||||
self.nselect += 1
|
||||
|
||||
@ -411,14 +414,14 @@ class dump:
|
||||
if snap.natoms:
|
||||
words = f.readline().split()
|
||||
ncol = len(words)
|
||||
for i in xrange(1,snap.natoms):
|
||||
for i in range(1,snap.natoms):
|
||||
words += f.readline().split()
|
||||
floats = map(float,words)
|
||||
floats = list(map(float,words))
|
||||
if oldnumeric: atoms = np.zeros((snap.natoms,ncol),np.Float)
|
||||
else: atoms = np.zeros((snap.natoms,ncol),np.float)
|
||||
start = 0
|
||||
stop = ncol
|
||||
for i in xrange(snap.natoms):
|
||||
for i in range(snap.natoms):
|
||||
atoms[i] = floats[start:stop]
|
||||
start = stop
|
||||
stop += ncol
|
||||
@ -433,7 +436,7 @@ class dump:
|
||||
|
||||
def map(self,*pairs):
|
||||
if len(pairs) % 2 != 0:
|
||||
raise StandardError, "dump map() requires pairs of mappings"
|
||||
raise Exception("dump map() requires pairs of mappings")
|
||||
for i in range(0,len(pairs),2):
|
||||
j = i + 1
|
||||
self.names[pairs[j]] = pairs[i]-1
|
||||
@ -445,25 +448,26 @@ class dump:
|
||||
ndel = i = 0
|
||||
while i < self.nsnaps:
|
||||
if not self.snaps[i].tselect:
|
||||
del self.fileNums[i]
|
||||
del self.snaps[i]
|
||||
self.nsnaps -= 1
|
||||
ndel += 1
|
||||
else: i += 1
|
||||
print "%d snapshots deleted" % ndel
|
||||
print "%d snapshots remaining" % self.nsnaps
|
||||
print("%d snapshots deleted" % ndel)
|
||||
print("%d snapshots remaining" % self.nsnaps)
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# scale coords to 0-1 for all snapshots or just one
|
||||
|
||||
def scale(self,*list):
|
||||
if len(list) == 0:
|
||||
print "Scaling dump ..."
|
||||
def scale(self,*dumplist):
|
||||
if len(dumplist) == 0:
|
||||
print("Scaling dump ...")
|
||||
x = self.names["x"]
|
||||
y = self.names["y"]
|
||||
z = self.names["z"]
|
||||
for snap in self.snaps: self.scale_one(snap,x,y,z)
|
||||
else:
|
||||
i = self.findtime(list[0])
|
||||
i = self.findtime(dumplist[0])
|
||||
x = self.names["x"]
|
||||
y = self.names["y"]
|
||||
z = self.names["z"]
|
||||
@ -483,15 +487,15 @@ class dump:
|
||||
# --------------------------------------------------------------------
|
||||
# unscale coords from 0-1 to box size for all snapshots or just one
|
||||
|
||||
def unscale(self,*list):
|
||||
if len(list) == 0:
|
||||
print "Unscaling dump ..."
|
||||
def unscale(self,*dumplist):
|
||||
if len(dumplist) == 0:
|
||||
print("Unscaling dump ...")
|
||||
x = self.names["x"]
|
||||
y = self.names["y"]
|
||||
z = self.names["z"]
|
||||
for snap in self.snaps: self.unscale_one(snap,x,y,z)
|
||||
else:
|
||||
i = self.findtime(list[0])
|
||||
i = self.findtime(dumplist[0])
|
||||
x = self.names["x"]
|
||||
y = self.names["y"]
|
||||
z = self.names["z"]
|
||||
@ -512,7 +516,7 @@ class dump:
|
||||
# wrap coords from outside box to inside
|
||||
|
||||
def wrap(self):
|
||||
print "Wrapping dump ..."
|
||||
print("Wrapping dump ...")
|
||||
|
||||
x = self.names["x"]
|
||||
y = self.names["y"]
|
||||
@ -534,7 +538,7 @@ class dump:
|
||||
# unwrap coords from inside box to outside
|
||||
|
||||
def unwrap(self):
|
||||
print "Unwrapping dump ..."
|
||||
print("Unwrapping dump ...")
|
||||
|
||||
x = self.names["x"]
|
||||
y = self.names["y"]
|
||||
@ -557,7 +561,7 @@ class dump:
|
||||
# if dynamic extra lines or triangles defined, owrap them as well
|
||||
|
||||
def owrap(self,other):
|
||||
print "Wrapping to other ..."
|
||||
print("Wrapping to other ...")
|
||||
|
||||
id = self.names["id"]
|
||||
x = self.names["x"]
|
||||
@ -574,9 +578,9 @@ class dump:
|
||||
zprd = snap.zhi - snap.zlo
|
||||
atoms = snap.atoms
|
||||
ids = {}
|
||||
for i in xrange(snap.natoms):
|
||||
for i in range(snap.natoms):
|
||||
ids[atoms[i][id]] = i
|
||||
for i in xrange(snap.natoms):
|
||||
for i in range(snap.natoms):
|
||||
j = ids[atoms[i][iother]]
|
||||
atoms[i][x] += (atoms[i][ix]-atoms[j][ix])*xprd
|
||||
atoms[i][y] += (atoms[i][iy]-atoms[j][iy])*yprd
|
||||
@ -589,11 +593,11 @@ class dump:
|
||||
# convert column names assignment to a string, in column order
|
||||
|
||||
def names2str(self):
|
||||
ncol = max(self.names.values()) #len(self.snaps[0].atoms[0])
|
||||
pairs = self.names.items()
|
||||
values = self.names.values()
|
||||
pairs = list(self.names.items())
|
||||
values = list(self.names.values())
|
||||
ncol = len(pairs)
|
||||
str = ""
|
||||
for i in xrange(ncol):
|
||||
for i in range(ncol):
|
||||
if i in values: str += pairs[values.index(i)][0] + ' '
|
||||
return str
|
||||
|
||||
@ -602,24 +606,24 @@ class dump:
|
||||
# if arg = string, sort all steps by that column
|
||||
# if arg = numeric, sort atoms in single step
|
||||
|
||||
def sort(self,*list, **kwargs):
|
||||
def sort(self,*tslist, **kwargs):
|
||||
|
||||
# check whether to output or not
|
||||
outputfl = True
|
||||
if "output" in kwargs: outputfl = kwargs["output"]
|
||||
|
||||
if len(list) == 0:
|
||||
if outputfl: print "Sorting selected snapshots ..."
|
||||
if len(tslist) == 0:
|
||||
if outputfl: print("Sorting selected snapshots ...")
|
||||
id = self.names["id"]
|
||||
for snap in self.snaps:
|
||||
if snap.tselect: self.sort_one(snap,id)
|
||||
elif type(list[0]) is types.StringType:
|
||||
if outputfl: print "Sorting selected snapshots by %s ..." % list[0]
|
||||
id = self.names[list[0]]
|
||||
elif isinstance(tslist[0], str):
|
||||
if outputfl: print("Sorting selected snapshots by %s ..." % tslist[0])
|
||||
id = self.names[tslist[0]]
|
||||
for snap in self.snaps:
|
||||
if snap.tselect: self.sort_one(snap,id)
|
||||
else:
|
||||
i = self.findtime(list[0])
|
||||
i = self.findtime(tslist[0])
|
||||
id = self.names["id"]
|
||||
self.sort_one(self.snaps[i],id)
|
||||
|
||||
@ -630,7 +634,7 @@ class dump:
|
||||
atoms = snap.atoms
|
||||
ids = atoms[:,id]
|
||||
ordering = np.argsort(ids)
|
||||
for i in xrange(len(atoms[0])):
|
||||
for i in range(len(atoms[0])):
|
||||
atoms[:,i] = np.take(atoms[:,i],ordering)
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
@ -642,33 +646,33 @@ class dump:
|
||||
else: f = open(file,"a")
|
||||
for snap in self.snaps:
|
||||
if not snap.tselect: continue
|
||||
print snap.time,
|
||||
print(snap.time, end=' ')
|
||||
sys.stdout.flush()
|
||||
|
||||
if header:
|
||||
print >>f,"ITEM: TIMESTEP"
|
||||
print >>f,snap.time
|
||||
print >>f,"ITEM: NUMBER OF ATOMS"
|
||||
print >>f,snap.nselect
|
||||
print >>f,"ITEM: BOX BOUNDS"
|
||||
print >>f,snap.xlo,snap.xhi
|
||||
print >>f,snap.ylo,snap.yhi
|
||||
print >>f,snap.zlo,snap.zhi
|
||||
print >>f,"ITEM: ATOMS",namestr
|
||||
print("ITEM: TIMESTEP", file=f)
|
||||
print(snap.time, file=f)
|
||||
print("ITEM: NUMBER OF ATOMS", file=f)
|
||||
print(snap.nselect, file=f)
|
||||
print("ITEM: BOX BOUNDS", file=f)
|
||||
print(snap.xlo,snap.xhi, file=f)
|
||||
print(snap.ylo,snap.yhi, file=f)
|
||||
print(snap.zlo,snap.zhi, file=f)
|
||||
print("ITEM: ATOMS",namestr, file=f)
|
||||
|
||||
atoms = snap.atoms
|
||||
nvalues = len(atoms[0])
|
||||
for i in xrange(snap.natoms):
|
||||
for i in range(snap.natoms):
|
||||
if not snap.aselect[i]: continue
|
||||
line = ""
|
||||
for j in xrange(nvalues):
|
||||
for j in range(nvalues):
|
||||
if (j < 2):
|
||||
line += str(int(atoms[i][j])) + " "
|
||||
else:
|
||||
line += str(atoms[i][j]) + " "
|
||||
print >>f,line
|
||||
print(line, file=f)
|
||||
f.close()
|
||||
print "\n%d snapshots" % self.nselect
|
||||
print("\n%d snapshots" % self.nselect)
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# write one dump file per snapshot from current selection
|
||||
@ -677,34 +681,34 @@ class dump:
|
||||
if len(self.snaps): namestr = self.names2str()
|
||||
for snap in self.snaps:
|
||||
if not snap.tselect: continue
|
||||
print snap.time,
|
||||
print(snap.time, end=' ')
|
||||
sys.stdout.flush()
|
||||
|
||||
file = root + "." + str(snap.time)
|
||||
f = open(file,"w")
|
||||
print >>f,"ITEM: TIMESTEP"
|
||||
print >>f,snap.time
|
||||
print >>f,"ITEM: NUMBER OF ATOMS"
|
||||
print >>f,snap.nselect
|
||||
print >>f,"ITEM: BOX BOUNDS"
|
||||
print >>f,snap.xlo,snap.xhi
|
||||
print >>f,snap.ylo,snap.yhi
|
||||
print >>f,snap.zlo,snap.zhi
|
||||
print >>f,"ITEM: ATOMS",namestr
|
||||
print("ITEM: TIMESTEP", file=f)
|
||||
print(snap.time, file=f)
|
||||
print("ITEM: NUMBER OF ATOMS", file=f)
|
||||
print(snap.nselect, file=f)
|
||||
print("ITEM: BOX BOUNDS", file=f)
|
||||
print(snap.xlo,snap.xhi, file=f)
|
||||
print(snap.ylo,snap.yhi, file=f)
|
||||
print(snap.zlo,snap.zhi, file=f)
|
||||
print("ITEM: ATOMS",namestr, file=f)
|
||||
|
||||
atoms = snap.atoms
|
||||
nvalues = len(atoms[0])
|
||||
for i in xrange(snap.natoms):
|
||||
for i in range(snap.natoms):
|
||||
if not snap.aselect[i]: continue
|
||||
line = ""
|
||||
for j in xrange(nvalues):
|
||||
for j in range(nvalues):
|
||||
if (j < 2):
|
||||
line += str(int(atoms[i][j])) + " "
|
||||
else:
|
||||
line += str(atoms[i][j]) + " "
|
||||
print >>f,line
|
||||
print(line, file=f)
|
||||
f.close()
|
||||
print "\n%d snapshots" % self.nselect
|
||||
print("\n%d snapshots" % self.nselect)
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# find min/max across all selected snapshots/atoms for a particular column
|
||||
@ -716,7 +720,7 @@ class dump:
|
||||
for snap in self.snaps:
|
||||
if not snap.tselect: continue
|
||||
atoms = snap.atoms
|
||||
for i in xrange(snap.natoms):
|
||||
for i in range(snap.natoms):
|
||||
if not snap.aselect[i]: continue
|
||||
if atoms[i][icol] < min: min = atoms[i][icol]
|
||||
if atoms[i][icol] > max: max = atoms[i][icol]
|
||||
@ -726,15 +730,15 @@ class dump:
|
||||
# set a column value via an equation for all selected snapshots
|
||||
|
||||
def set(self,eq):
|
||||
print "Setting ..."
|
||||
print("Setting ...")
|
||||
pattern = "\$\w*"
|
||||
list = re.findall(pattern,eq)
|
||||
eqlist = re.findall(pattern,eq)
|
||||
|
||||
lhs = list[0][1:]
|
||||
if not self.names.has_key(lhs):
|
||||
lhs = eqlist[0][1:]
|
||||
if lhs not in self.names:
|
||||
self.newcolumn(lhs)
|
||||
|
||||
for item in list:
|
||||
for item in eqlist:
|
||||
name = item[1:]
|
||||
column = self.names[name]
|
||||
insert = "snap.atoms[i][%d]" % (column)
|
||||
@ -743,25 +747,25 @@ class dump:
|
||||
|
||||
for snap in self.snaps:
|
||||
if not snap.tselect: continue
|
||||
for i in xrange(snap.natoms):
|
||||
if snap.aselect[i]: exec ceq
|
||||
for i in range(snap.natoms):
|
||||
if snap.aselect[i]: exec(ceq)
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# set a column value via an input vec for all selected snapshots/atoms
|
||||
|
||||
def setv(self,colname,vec):
|
||||
print "Setting ..."
|
||||
if not self.names.has_key(colname):
|
||||
print("Setting ...")
|
||||
if colname not in self.names:
|
||||
self.newcolumn(colname)
|
||||
icol = self.names[colname]
|
||||
|
||||
for snap in self.snaps:
|
||||
if not snap.tselect: continue
|
||||
if snap.nselect != len(vec):
|
||||
raise StandardError,"vec length does not match # of selected atoms"
|
||||
raise Exception("vec length does not match # of selected atoms")
|
||||
atoms = snap.atoms
|
||||
m = 0
|
||||
for i in xrange(snap.natoms):
|
||||
for i in range(snap.natoms):
|
||||
if snap.aselect[i]:
|
||||
atoms[i][icol] = vec[m]
|
||||
m += 1
|
||||
@ -774,12 +778,12 @@ class dump:
|
||||
icol = self.names[col]
|
||||
id = self.names["id"]
|
||||
ids = {}
|
||||
for i in xrange(self.snaps[istep].natoms):
|
||||
for i in range(self.snaps[istep].natoms):
|
||||
ids[self.snaps[istep].atoms[i][id]] = i
|
||||
for snap in self.snaps:
|
||||
if not snap.tselect: continue
|
||||
atoms = snap.atoms
|
||||
for i in xrange(snap.natoms):
|
||||
for i in range(snap.natoms):
|
||||
if not snap.aselect[i]: continue
|
||||
j = ids[atoms[i][id]]
|
||||
atoms[i][icol] = self.snaps[istep].atoms[j][icol]
|
||||
@ -789,18 +793,18 @@ class dump:
|
||||
|
||||
def spread(self,old,n,new):
|
||||
iold = self.names[old]
|
||||
if not self.names.has_key(new): self.newcolumn(new)
|
||||
if new not in self.names: self.newcolumn(new)
|
||||
inew = self.names[new]
|
||||
|
||||
min,max = self.minmax(old)
|
||||
print "min/max = ",min,max
|
||||
print("min/max = ",min,max)
|
||||
|
||||
gap = max - min
|
||||
invdelta = n/gap
|
||||
for snap in self.snaps:
|
||||
if not snap.tselect: continue
|
||||
atoms = snap.atoms
|
||||
for i in xrange(snap.natoms):
|
||||
for i in range(snap.natoms):
|
||||
if not snap.aselect[i]: continue
|
||||
ivalue = int((atoms[i][iold] - min) * invdelta) + 1
|
||||
if ivalue > n: ivalue = n
|
||||
@ -822,12 +826,12 @@ class dump:
|
||||
# --------------------------------------------------------------------
|
||||
# extract vector(s) of values for atom ID n at each selected timestep
|
||||
|
||||
def atom(self,n,*list):
|
||||
if len(list) == 0:
|
||||
raise StandardError, "no columns specified"
|
||||
def atom(self,n,*tslist):
|
||||
if len(tslist) == 0:
|
||||
raise Exception("no columns specified")
|
||||
columns = []
|
||||
values = []
|
||||
for name in list:
|
||||
for name in tslist:
|
||||
columns.append(self.names[name])
|
||||
values.append(self.nselect * [0])
|
||||
ncol = len(columns)
|
||||
@ -837,40 +841,40 @@ class dump:
|
||||
for snap in self.snaps:
|
||||
if not snap.tselect: continue
|
||||
atoms = snap.atoms
|
||||
for i in xrange(snap.natoms):
|
||||
for i in range(snap.natoms):
|
||||
if atoms[i][id] == n: break
|
||||
if atoms[i][id] != n:
|
||||
raise StandardError, "could not find atom ID in snapshot"
|
||||
for j in xrange(ncol):
|
||||
raise Exception("could not find atom ID in snapshot")
|
||||
for j in range(ncol):
|
||||
values[j][m] = atoms[i][columns[j]]
|
||||
m += 1
|
||||
|
||||
if len(list) == 1: return values[0]
|
||||
if len(tslist) == 1: return values[0]
|
||||
else: return values
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# extract vector(s) of values for selected atoms at chosen timestep
|
||||
|
||||
def vecs(self,n,*list):
|
||||
def vecs(self,n,*tslist):
|
||||
snap = self.snaps[self.findtime(n)]
|
||||
|
||||
if len(list) == 0:
|
||||
raise StandardError, "no columns specified"
|
||||
if len(tslist) == 0:
|
||||
raise Exception("no columns specified")
|
||||
columns = []
|
||||
values = []
|
||||
for name in list:
|
||||
for name in tslist:
|
||||
columns.append(self.names[name])
|
||||
values.append(snap.nselect * [0])
|
||||
ncol = len(columns)
|
||||
|
||||
m = 0
|
||||
for i in xrange(snap.natoms):
|
||||
for i in range(snap.natoms):
|
||||
if not snap.aselect[i]: continue
|
||||
for j in xrange(ncol):
|
||||
for j in range(ncol):
|
||||
values[j][m] = snap.atoms[i][columns[j]]
|
||||
m += 1
|
||||
|
||||
if len(list) == 1: return values[0]
|
||||
if len(tslist) == 1: return values[0]
|
||||
else: return values
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
@ -915,7 +919,7 @@ class dump:
|
||||
def iterator(self,flag):
|
||||
start = 0
|
||||
if flag: start = self.iterate + 1
|
||||
for i in xrange(start,self.nsnaps):
|
||||
for i in range(start,self.nsnaps):
|
||||
if self.snaps[i].tselect:
|
||||
self.iterate = i
|
||||
return i,self.snaps[i].time,1
|
||||
@ -951,7 +955,7 @@ class dump:
|
||||
# need Numeric/Numpy mode here
|
||||
|
||||
atoms = []
|
||||
for i in xrange(snap.natoms):
|
||||
for i in range(snap.natoms):
|
||||
if not snap.aselect[i]: continue
|
||||
atom = snap.atoms[i]
|
||||
atoms.append([atom[id],atom[type],atom[x],atom[y],atom[z]])
|
||||
@ -970,7 +974,7 @@ class dump:
|
||||
elif self.bondflag == 2:
|
||||
tmp1,tmp2,tmp3,bondlist,tmp4,tmp5 = self.objextra.viz(time,1)
|
||||
alist = {}
|
||||
for i in xrange(len(atoms)): alist[int(atoms[i][0])] = i
|
||||
for i in range(len(atoms)): alist[int(atoms[i][0])] = i
|
||||
for bond in bondlist:
|
||||
try:
|
||||
i = alist[bond[2]]
|
||||
@ -1004,9 +1008,9 @@ class dump:
|
||||
# --------------------------------------------------------------------
|
||||
|
||||
def findtime(self,n):
|
||||
for i in xrange(self.nsnaps):
|
||||
for i in range(self.nsnaps):
|
||||
if self.snaps[i].time == n: return i
|
||||
raise StandardError, "no step %d exists" % n
|
||||
raise Exception("no step %d exists" % n)
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# return maximum box size across all selected snapshots
|
||||
@ -1033,7 +1037,7 @@ class dump:
|
||||
for snap in self.snaps:
|
||||
if not snap.tselect: continue
|
||||
atoms = snap.atoms
|
||||
for i in xrange(snap.natoms):
|
||||
for i in range(snap.natoms):
|
||||
if not snap.aselect[i]: continue
|
||||
if atoms[i][icol] > max: max = atoms[i][icol]
|
||||
return int(max)
|
||||
@ -1046,7 +1050,7 @@ class dump:
|
||||
|
||||
# data object, grab bonds statically
|
||||
|
||||
if type(arg) is types.InstanceType and ".data" in str(arg.__class__):
|
||||
if isinstance(arg, object) and ".data" in str(arg.__class__):
|
||||
self.bondflag = 0
|
||||
try:
|
||||
bondlist = []
|
||||
@ -1059,11 +1063,11 @@ class dump:
|
||||
self.bondflag = 1
|
||||
self.bondlist = bondlist
|
||||
except:
|
||||
raise StandardError,"could not extract bonds from data object"
|
||||
raise Exception("could not extract bonds from data object")
|
||||
|
||||
# cdata object, grab tris and lines statically
|
||||
|
||||
elif type(arg) is types.InstanceType and ".cdata" in str(arg.__class__):
|
||||
elif isinstance(arg, object) and ".cdata" in str(arg.__class__):
|
||||
self.triflag = self.lineflag = 0
|
||||
try:
|
||||
tmp,tmp,tmp,tmp,tris,lines = arg.viz(0)
|
||||
@ -1074,34 +1078,34 @@ class dump:
|
||||
self.lineflag = 1
|
||||
self.linelist = lines
|
||||
except:
|
||||
raise StandardError,"could not extract tris/lines from cdata object"
|
||||
raise Exception("could not extract tris/lines from cdata object")
|
||||
|
||||
# mdump object, grab tris dynamically
|
||||
|
||||
elif type(arg) is types.InstanceType and ".mdump" in str(arg.__class__):
|
||||
elif isinstance(arg, object) and ".mdump" in str(arg.__class__):
|
||||
self.triflag = 2
|
||||
self.objextra = arg
|
||||
|
||||
# bdump object, grab bonds dynamically
|
||||
|
||||
elif type(arg) is types.InstanceType and ".bdump" in str(arg.__class__):
|
||||
elif isinstance(arg, object) and ".bdump" in str(arg.__class__):
|
||||
self.bondflag = 2
|
||||
self.objextra = arg
|
||||
|
||||
# ldump object, grab tris dynamically
|
||||
|
||||
elif type(arg) is types.InstanceType and ".ldump" in str(arg.__class__):
|
||||
elif isinstance(arg, object) and ".ldump" in str(arg.__class__):
|
||||
self.lineflag = 2
|
||||
self.objextra = arg
|
||||
|
||||
# tdump object, grab tris dynamically
|
||||
|
||||
elif type(arg) is types.InstanceType and ".tdump" in str(arg.__class__):
|
||||
elif isinstance(arg, object) and ".tdump" in str(arg.__class__):
|
||||
self.triflag = 2
|
||||
self.objextra = arg
|
||||
|
||||
else:
|
||||
raise StandardError,"unrecognized argument to dump.extra()"
|
||||
raise Exception("unrecognized argument to dump.extra()")
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
|
||||
@ -1140,7 +1144,7 @@ class tselect:
|
||||
snap.tselect = 1
|
||||
data.nselect = len(data.snaps)
|
||||
data.aselect.all()
|
||||
if outputfl: print "%d snapshots selected out of %d" % (data.nselect,data.nsnaps)
|
||||
if outputfl: print("%d snapshots selected out of %d" % (data.nselect,data.nsnaps))
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
|
||||
@ -1152,7 +1156,7 @@ class tselect:
|
||||
data.snaps[i].tselect = 1
|
||||
data.nselect = 1
|
||||
data.aselect.all()
|
||||
print "%d snapshots selected out of %d" % (data.nselect,data.nsnaps)
|
||||
print("%d snapshots selected out of %d" % (data.nselect,data.nsnaps))
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
|
||||
@ -1161,7 +1165,7 @@ class tselect:
|
||||
for snap in data.snaps:
|
||||
snap.tselect = 0
|
||||
data.nselect = 0
|
||||
print "%d snapshots selected out of %d" % (data.nselect,data.nsnaps)
|
||||
print("%d snapshots selected out of %d" % (data.nselect,data.nsnaps))
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
|
||||
@ -1177,7 +1181,7 @@ class tselect:
|
||||
snap.tselect = 0
|
||||
data.nselect -= 1
|
||||
data.aselect.all()
|
||||
print "%d snapshots selected out of %d" % (data.nselect,data.nsnaps)
|
||||
print("%d snapshots selected out of %d" % (data.nselect,data.nsnaps))
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
|
||||
@ -1186,14 +1190,16 @@ class tselect:
|
||||
snaps = data.snaps
|
||||
cmd = "flag = " + teststr.replace("$t","snaps[i].time")
|
||||
ccmd = compile(cmd,'','single')
|
||||
for i in xrange(data.nsnaps):
|
||||
for i in range(data.nsnaps):
|
||||
if not snaps[i].tselect: continue
|
||||
exec ccmd
|
||||
ldict = {'data':data,'snaps':snaps,'i':i}
|
||||
exec(ccmd,globals(),ldict)
|
||||
flag = ldict['flag']
|
||||
if not flag:
|
||||
snaps[i].tselect = 0
|
||||
data.nselect -= 1
|
||||
data.aselect.all()
|
||||
print "%d snapshots selected out of %d" % (data.nselect,data.nsnaps)
|
||||
print("%d snapshots selected out of %d" % (data.nselect,data.nsnaps))
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# atom selection class
|
||||
@ -1210,12 +1216,12 @@ class aselect:
|
||||
if len(args) == 0: # all selected timesteps
|
||||
for snap in data.snaps:
|
||||
if not snap.tselect: continue
|
||||
for i in xrange(snap.natoms): snap.aselect[i] = 1
|
||||
for i in range(snap.natoms): snap.aselect[i] = 1
|
||||
snap.nselect = snap.natoms
|
||||
else: # one timestep
|
||||
n = data.findtime(args[0])
|
||||
snap = data.snaps[n]
|
||||
for i in xrange(snap.natoms): snap.aselect[i] = 1
|
||||
for i in range(snap.natoms): snap.aselect[i] = 1
|
||||
snap.nselect = snap.natoms
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
@ -1226,8 +1232,8 @@ class aselect:
|
||||
# replace all $var with snap.atoms references and compile test string
|
||||
|
||||
pattern = "\$\w*"
|
||||
list = re.findall(pattern,teststr)
|
||||
for item in list:
|
||||
testlist = re.findall(pattern,teststr)
|
||||
for item in testlist:
|
||||
name = item[1:]
|
||||
column = data.names[name]
|
||||
insert = "snap.atoms[i][%d]" % column
|
||||
@ -1238,29 +1244,33 @@ class aselect:
|
||||
if len(args) == 0: # all selected timesteps
|
||||
for snap in data.snaps:
|
||||
if not snap.tselect: continue
|
||||
for i in xrange(snap.natoms):
|
||||
for i in range(snap.natoms):
|
||||
if not snap.aselect[i]: continue
|
||||
exec ccmd
|
||||
ldict = {'snap':snap,'i':i}
|
||||
exec(ccmd,globals(),ldict)
|
||||
flag = ldict['flag']
|
||||
if not flag:
|
||||
snap.aselect[i] = 0
|
||||
snap.nselect -= 1
|
||||
for i in xrange(data.nsnaps):
|
||||
for i in range(data.nsnaps):
|
||||
if data.snaps[i].tselect:
|
||||
print "%d atoms of %d selected in first step %d" % \
|
||||
(data.snaps[i].nselect,data.snaps[i].natoms,data.snaps[i].time)
|
||||
print("%d atoms of %d selected in first step %d" % \
|
||||
(data.snaps[i].nselect,data.snaps[i].natoms,data.snaps[i].time))
|
||||
break
|
||||
for i in xrange(data.nsnaps-1,-1,-1):
|
||||
for i in range(data.nsnaps-1,-1,-1):
|
||||
if data.snaps[i].tselect:
|
||||
print "%d atoms of %d selected in last step %d" % \
|
||||
(data.snaps[i].nselect,data.snaps[i].natoms,data.snaps[i].time)
|
||||
print("%d atoms of %d selected in last step %d" % \
|
||||
(data.snaps[i].nselect,data.snaps[i].natoms,data.snaps[i].time))
|
||||
break
|
||||
|
||||
else: # one timestep
|
||||
n = data.findtime(args[0])
|
||||
snap = data.snaps[n]
|
||||
for i in xrange(snap.natoms):
|
||||
for i in range(snap.natoms):
|
||||
if not snap.aselect[i]: continue
|
||||
exec ccmd
|
||||
ldict = {'snap':snap,'i':i}
|
||||
exec(ccmd,globals(),ldict)
|
||||
flag = ldict['flag']
|
||||
if not flag:
|
||||
snap.aselect[i] = 0
|
||||
snap.nselect -= 1
|
||||
|
||||
Reference in New Issue
Block a user