我有一个粘贴在下面的python代码。它可以很好地满足我的需要。您会注意到我加载了一个转储文件。如何遍历所有具有* .dump结束模式的转储文件,并让每个新文件仅向输出文件添加新的数据列?本质上,我想添加一个循环,这样就不必手动为每个转储文件重新编写代码。
from ovito.io import *
from ovito.data import *
from ovito.modifiers import *
import numpy as np
node = import_file("../200eV.dump",multiple_frames = True)
# Perform Wigner-Seitz analysis:
ws = WignerSeitzAnalysisModifier(
per_type_occupancies = True,
eliminate_cell_deformation = True)
ws.reference.load("../../../WS_Ref/ws.dump")
node.modifiers.append(ws)
# Define a modifier function that selects sites of type A=1 which
# are occupied by exactly one atom of type B=2.
def modify(frame, input, output):
# Retrieve the two-dimensional Numpy array with the site occupancy numbers.
occupancies = input.particle_properties['Occupancy'].array
# Get the site types as additional input:
site_type = input.particle_properties.particle_type.array
# Calculate total occupancy of every site:
total_occupancy = np.sum(occupancies, axis=1)
# Set up a particle selection by creating the Selection property:
selection1 = (site_type == 1) & (occupancies[:,0] == 0) & (occupancies[:,1] == 0)
output.attributes['Ca_Vac'] = np.count_nonzero(selection1)
# Insert Python modifier into the data pipeline.
node.modifiers.append(PythonScriptModifier(function = modify))
# Let OVITO do the computation and export the number of identified
# antisites as a function of simulation time to a text file:
export_file(node, "defects_200.txt", "txt",
columns = ['Timestep', 'Ca_Vac'],
multiple_frames = True)
答案 0 :(得分:1)
import numpy as np
from ovito.data import *
from ovito.io import *
from ovito.modifiers import *
ws = WignerSeitzAnalysisModifier(
per_type_occupancies=True,
eliminate_cell_deformation=True)
ws.reference.load("../../../WS_Ref/ws.dump")
def modify(frame, input, output):
occupancies = input.particle_properties['Occupancy'].array
site_type = input.particle_properties.particle_type.array
total_occupancy = np.sum(occupancies, axis=1) # you are also not using, also not using the frame parameter
selection1 = (site_type == 1) & (occupancies[:, 0] == 0) & (occupancies[:, 1] == 0)
output.attributes['Ca_Vac'] = np.count_nonzero(selection1)
import glob
for file in glob.glob('../*.glob'):
node = import_file(file, multiple_frames=True)
node.modifiers.append(ws)
node.modifiers.append(PythonScriptModifier(function=modify))
export_file(
node, "defects_200.txt", "txt",
columns=['Timestep', 'Ca_Vac'],
multiple_frames=True
)
我不知道这是我能想到的最好的方法,我希望它能起作用!
根据OP的要求添加此部分。
for index, file in enumerate(glob.glob('../*.glob')):
node = import_file(file, multiple_frames=True)
node.modifiers.append(ws)
node.modifiers.append(PythonScriptModifier(function=modify))
export_file(
node, "defects_{}.txt".format(index), "txt",
columns=['Timestep', 'Ca_Vac'],
multiple_frames=True
)
再次,这是关于库如何工作的猜测,并且只有在原始代码产生defetcs_200.txt
作为结果时才能起作用。
答案 1 :(得分:0)
尝试使用python glob软件包。
>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']