本文整理汇总了Python中diffpy.Structure.Structure.lattice方法的典型用法代码示例。如果您正苦于以下问题:Python Structure.lattice方法的具体用法?Python Structure.lattice怎么用?Python Structure.lattice使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类diffpy.Structure.Structure
的用法示例。
在下文中一共展示了Structure.lattice方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parseLines
# 需要导入模块: from diffpy.Structure import Structure [as 别名]
# 或者: from diffpy.Structure.Structure import lattice [as 别名]
def parseLines(self, lines):
"""Parse list of lines in XYZ format.
Return Structure object or raise StructureFormatError.
"""
linefields = [l.split() for l in lines]
# prepare output structure
stru = Structure()
# find first valid record
start = 0
for field in linefields:
if len(field) == 0 or field[0] == "#":
start += 1
else:
break
# first valid line gives number of atoms
try:
lfs = linefields[start]
w1 = linefields[start][0]
if len(lfs) == 1 and str(int(w1)) == w1:
p_natoms = int(w1)
#try to get lattice vectors from description line
try:
latticeVecs = map(float, linefields[start+1])
assert len(latticeVecs)==9
reshaped = [latticeVecs[0:3], latticeVecs[3:6], latticeVecs[6:9]]
stru.lattice = Lattice(base=reshaped)
needsDescription = True
except:
needsDescription = False
stru.description = lines[start+1].strip()
start += 2
else:
emsg = ("%d: invalid XYZ format, missing number of atoms" %
(start + 1))
raise StructureFormatError(emsg)
except (IndexError, ValueError):
exc_type, exc_value, exc_traceback = sys.exc_info()
emsg = ("%d: invalid XYZ format, missing number of atoms" %
(start + 1))
raise StructureFormatError, emsg, exc_traceback
# find the last valid record
stop = len(lines)
while stop > start and len(linefields[stop-1]) == 0:
stop -= 1
# get out for empty structure
if p_natoms == 0 or start >= stop:
return stru
# here we have at least one valid record line
nfields = len(linefields[start])
if nfields != 4 and nfields != 5:
emsg = "%d: invalid XYZ format, expected 4 or 5 columns" % (start + 1)
raise StructureFormatError(emsg)
# now try to read all record lines
try:
p_nl = start
for fields in linefields[start:] :
p_nl += 1
if fields == []:
continue
elif len(fields) != 4 and len(fields) !=5:
emsg = ('%d: all lines must have ' +
'a symbol, position, and optionally charge') % p_nl
raise StructureFormatError(emsg)
symbol = fields[0]
symbol = symbol[0].upper() + symbol[1:].lower()
xyz = [ float(f) for f in fields[1:4] ]
if len(fields)==5:
charge = float(fields[4])
else:
charge = 0.0
stru.addNewAtom(symbol, xyz=xyz)
stru.getLastAtom().charge=charge
except ValueError:
exc_type, exc_value, exc_traceback = sys.exc_info()
emsg = "%d: invalid number format" % p_nl
raise StructureFormatError, emsg, exc_traceback
# finally check if all the atoms have been read
if p_natoms is not None and len(stru) != p_natoms:
emsg = "expected %d atoms, read %d" % (p_natoms, len(stru))
raise StructureFormatError(emsg)
# if needsDescription:
# stru.generateDescription()
return stru