[英]Filter XYZ txt file by temperature in Python (data from Finite Element)
我在一个文本文件中有一些有限元结果,其中包括每个节点的 X、Y、Z 和温度。 我想使用 matplotlib 绘制这个 3D 数据,但首先需要过滤掉所有无用的数据。 我将如何编写代码来导入文本文件,读取 XYZ 和 T,并找到高于给定 Temp 的每个相应位置? (例如,模拟域从 22 C 开始,但我想过滤掉 XYZ 中给出的任何低于 1237 C 的节点 - 任何低于 1237 的 XYZ 都没有用)。 现在,将结果导出为 txt 文件就可以了。
文本文件有点挑剔 - 有时每行中的数据用空格分隔,有时用制表符分隔
此外,我认为需要三角剖分,因为节点不是在统一网格上间隔开的。
import numpy as np
### read in each line from txt file ###
### X (mm), Y (mm), Z (mm), T (C) ###
### for encoding, try Windows-1252, utf-8, or ascii ###
matrix = np.loadtxt('xyzt.txt', skiprows = (1), usecols = (1,2,3,4), encoding = 'Windows-1252')
### filter out any temperature data below given threshold ###
threshold = 1238
print(matrix)
matrix[:,3] > threshold
matrix[matrix[:,3] > threshold,:]
我从下面的txt文件中附上了几行
Node Number X Location (mm) Y Location (mm) Z Location (mm) Temperature (∞C)
1 0. 0.15 -0.15 1235.8
2 1.5 0.15 -0.15 1234.4
3 3.e-003 0.15 -0.15 1235.8
4 6.e-003 0.15 -0.15 1235.8
5 9.e-003 0.15 -0.15 1235.9
6 1.2e-002 0.15 -0.15 1235.9
7 1.5e-002 0.15 -0.15 1235.9
8 1.8e-002 0.15 -0.15 1235.9
9 2.1e-002 0.15 -0.15 1236.
10 2.4e-002 0.15 -0.15 1236.
11 2.7e-002 0.15 -0.15 1236.1
12 3.e-002 0.15 -0.15 1236.1
13 3.3e-002 0.15 -0.15 1236.2
14 3.6e-002 0.15 -0.15 1236.3
15 3.9e-002 0.15 -0.15 1236.3
16 4.2e-002 0.15 -0.15 1236.4
17 4.5e-002 0.15 -0.15 1236.5
18 4.8e-002 0.15 -0.15 1236.6
19 5.1e-002 0.15 -0.15 1236.7
20 5.4e-002 0.15 -0.15 1236.8
21 5.7e-002 0.15 -0.15 1236.9
22 6.e-002 0.15 -0.15 1237.
23 6.3e-002 0.15 -0.15 1237.2
24 6.6e-002 0.15 -0.15 1237.3
25 6.9e-002 0.15 -0.15 1237.4
26 7.2e-002 0.15 -0.15 1237.6
27 7.5e-002 0.15 -0.15 1237.7
28 7.8e-002 0.15 -0.15 1237.8
29 8.1e-002 0.15 -0.15 1238.
30 8.4e-002 0.15 -0.15 1238.1
31 8.7e-002 0.15 -0.15 1238.3
32 9.e-002 0.15 -0.15 1238.5
33 9.3e-002 0.15 -0.15 1238.6
34 9.6e-002 0.15 -0.15 1238.8
35 9.9e-002 0.15 -0.15 1239.
36 0.102 0.15 -0.15 1239.2
37 0.105 0.15 -0.15 1239.4
38 0.108 0.15 -0.15 1239.6
39 0.111 0.15 -0.15 1239.8
40 0.114 0.15 -0.15 1240.
顺便说一句,这就是我最终的结果。 这也是一个相当有效的代码。
import numpy as np
from scipy.interpolate import LinearNDInterpolator as LI
import matplotlib.pyplot as plt
input_file = '/Users/jaroh/Documents/Thermal_Modeling/xyztemp.txt'
xyzt = np.loadtxt(input_file, skiprows = (1), usecols = (1,2,3,4), encoding = 'Windows-1252')
xyz = xyzt[0:, 0:3]
x = xyzt[0:, 0]
y = xyzt[0:, 1]
z = xyzt[0:, 2]
T = xyzt[0:, 3]
x2 = np.linspace(min(x), max(x), 100)
y2 = np.linspace(min(y), max(y), 100)
z2 = np.linspace(min(z), max(z), 100)
indices = np.random.randint(low=0, high = 307259, size = 30000)
xyz1 = [xyz[i,:] for i in indices]
T1 = [T[i] for i in indices]
xx, yy, zz = np.meshgrid(x2, y2, z2, indexing ='ij')
newcoords = np.vstack([xx.ravel(), yy.ravel(), zz.ravel()]).T
interp = LI(xyz1, T1, fill_value=0) # interpolate Z over X and Y
newvals = interp(newcoords)
newmat = np.array(newvals).reshape((len(x2), len(y2), len(z2)))
plt.imshow(newmat[:,:,50])
plt.colorbar()
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.