Filter XYZ txt file by temperature in Python (data from Finite Element)

Question:

I have some finite element results in a text file that includes X, Y, Z, and temperature at each node. I would like to plot this 3D data using matplotlib, but first need to filter out all the data that is of no use. How would I write a code to import the text file, read in XYZ and T, and find each respective position that is above a given Temp? (e.g. the simulation domain starts out at 22 C, but I want to filter out any node given in XYZ that is lower than 1237 C – any XYZ lower than 1237 is not useful). For now, having the results exported as a txt file would be fine.

The text file is a bit finicky – sometimes the data in each row is separated by spaces and sometimes by tabs

Also I believe triangulation will be needed seeing as the nodes are not spaced on a uniform grid.

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,:]

I attached a few lines from the txt file below

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.
Asked By: jaroh24

||

Answers:

Here’s what I ended up btw. It’s a fairly efficient code too.

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) # 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()
Answered By: jaroh24
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.