-
Notifications
You must be signed in to change notification settings - Fork 1
/
Read_vtk.py
120 lines (85 loc) · 2.94 KB
/
Read_vtk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import matplotlib.pyplot as plt
import re
# Optional style sheet for beautiful plots
plt.style.use('./large_plot.mplstyle')
#plt.style.use('C:/Users/chenc/Documents/Python_plot_formats/large_plot.mplstyle')
from matplotlib import cm
import matplotlib
np.set_printoptions(precision=5)
np.set_printoptions(suppress=True)
# In[58]:
def vtk_to_coord(inputfile):
x = re.split (r"\.", inputfile)
print ("Frame:", x[2])
filename2 = "tmp-coordinates.txt"
file2 = open(filename2, 'w')
with open(inputfile) as fp:
nPoints = 0
file2.write("#Coordinates\n")
coords = False
cnt = 0
for line in fp:
# convert string to floats
#
if (coords == True and cnt < nPoints):
x = re.split ('\s+', line)
#print (x)
for i in range (len(x)):
if(re.match ('[0-9]|NaN|-',x[i])):
file2.write('%E\t'% float (x[i]))
file2.write('\n')
cnt += 1
if re.match (r'\APOINTS', line):
print (line)
x = re.split ('\s+', line)
nPoints = int (x[1])
print ("Number of points:", nPoints)
coords = True
#print (nPoints)
return nPoints
# In[60]:
def vtk_to_dir(inputfile, nPoints):
x = re.split (r"\.", inputfile)
print ("Frame:", x[2])
filename2 = "tmp-directors.txt"
file2 = open(filename2, 'w')
with open(inputfile) as fp:
file2.write("#Directors\n")
coords = False
cnt = 0
for line in fp:
# convert string to floats
#
if (coords == True and cnt < (nPoints +2) ):
x = re.split ('\s+', line)
#print (x)
for i in range (len(x)):
if(re.match ('[0-9]|NaN|-',x[i])):
file2.write('%E\t'% float (x[i]))
file2.write('\n')
cnt += 1
if re.match (r'\Adirector', line):
print (line)
print ("Number of points:", nPoints)
coords = True
return nPoints
# In[17]:
if __name__ == "__main__":
directory2 = "./Original_Director_Field/"
frames= np.loadtxt("tmp-frames.txt", dtype = np.int32)
for frame in frames:
fname = "./vtks/stretch."+str(frame)+".vtk"
#Write temporary files
nPoints = vtk_to_coord(fname)
nPoints = vtk_to_dir(fname, nPoints)
#Write DirectorField file
coords = np.loadtxt("tmp-coordinates.txt")
directors = np.loadtxt("tmp-directors.txt")
X = np.hstack([coords,directors])
top = "Frame-"+str(frame)+"-Original director file"
filename1 = directory2+"Frame-"+str(frame) + "-DirectorField.txt"
np.savetxt(filename1, X, fmt='%.4f', delimiter= '\t',header=top)