【发布时间】:2015-05-20 18:06:03
【问题描述】:
我正在对 CFD 数据进行后处理(对坐标应用旋转)。为此,我正在执行以下操作:
-读取文件
-将数据存储到结构化数组中
-处理数据(进行计算)
-写一个新文件
它可以工作,但每个文件需要 7 秒。我有 (15000 * 4) 个文件要继续...
for i in range(0,len(file_count)):
#Source folder with original files
os.chdir(path+'\\'+folder_source_location)
#Generate file names
file_name = file_source_begin+("%0"+str(ndigit)+"d") % file_count[i]+"_tec.dat"
#Read the file
Data = read_tecUNS(file_name)
#New data set modified
Data_new = Data
#Translation
Data["node"]["X"]+=translator_plane2RotCenter[0] #The += is important or the Data won't be affected by the translation
Data["node"]["Y"]+=translator_plane2RotCenter[1]
Data["node"]["Z"]+=translator_plane2RotCenter[2]
#Rotation
Y_temp = Data["node"]["Y"]*cos(theta_rot_rad)-Data["node"]["Z"]*sin(theta_rot_rad)
Z_temp = Data["node"]["Y"]*sin(theta_rot_rad)+Data["node"]["Z"]*cos(theta_rot_rad)
Data_new["node"]["Y"]=Y_temp
Data_new["node"]["Z"]=np.mean(Z_temp) #Due to rounding, the Z values are not exactly the same. The mean avoid that.
#Write the new file
os.chdir(path+'\\'+folder_source_location+'\\'+"Output")
write_tecplot(file_name,Data_new)
您有什么改进的想法吗?我考虑过线程化写作,但我不确定它会改进什么。
以下是阅读/计算/写作时间的示例:
The output folder already exists. The data in it will be erased
StartReading B--0.000018_tec.dat in progress. - 0.001s elapsed
EndReading B--0.000018_tec.dat in progress. - 0.433s elapsed
StartWriting B--0.000018_tec.dat in progress. - 0.435s elapsed
EndWriting B--0.000018_tec.dat in progress. - 7.585s elapsed
StartReading B--0.000036_tec.dat in progress. - 7.586s elapsed
EndReading B--0.000036_tec.dat in progress. - 7.697s elapsed
StartWriting B--0.000036_tec.dat in progress. - 7.697s elapsed
EndWriting B--0.000036_tec.dat in progress. - 13.472s elapsed
还有一个脚本和一个示例来尝试更鲁莽的:
http://s000.tinyupload.com/index.php?file_id=80589646527340633700
【问题讨论】:
-
你没有向我们展示
write_tecplot!确定这是最重要的一点吗? -
^ 不仅如此,我们也看不到
read_tecUNS()方法... -
由于阅读时间在0.1-0.4s之间,我认为这不是最关键的工作。无论如何,该功能相当长且丑陋(太长而无法发布),但可以在 Sample 包中找到! :)
标签: python optimization time writing