-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVTKformat.py
More file actions
164 lines (130 loc) · 5.45 KB
/
VTKformat.py
File metadata and controls
164 lines (130 loc) · 5.45 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# This file is used to write vtk format for Bidirectional Growth
# Remember to change the input text files name
import math
import datetime
print datetime.datetime.now()
cellnumber = [] # Cell number: saved in column 6
stepnumber = [] # Step number: saved in column 7
previousnode = [] # Previous node number: saved in column 8
currentnode = [] # Current node number: saved in column 9
r_fwd = [] # r coordinates: saved in column 0
theta_fwd = [] # theta coordinates: saved in column 1
z_fwd = [] # z coordinates: saved in column 2
uniquenumber = []
uniquenumber1 = []
x_fwd = [] # x coordinates: r * cos(theta)
y_fwd = [] # y coordinates: r * sin(theta)
new_row = []
# Open the output from Growth model and save in a list; Remember to change the file's name
with open('pos_bi_fwd_500_20.txt') as f:
content = f.readlines()
# Read all useful information from the list
for line in content:
currentnode.append(line.split()[9])
previousnode.append(line.split()[8])
cellnumber.append(line.split()[6])
uniquenumber.append(line.split()[6] + '-' + line.split()[9]) # Create unique numbers for cell number and current node
uniquenumber1.append(line.split()[6] + '-' + line.split()[8]) # Create unique numbers for cell number and previous node
stepnumber.append(line.split()[7])
r_fwd.append(float(line.split()[0]))
theta_fwd.append(float(line.split()[1]))
z_fwd.append(float(line.split()[2]))
x_fwd = [i * math.cos(j) for i, j in zip(r_fwd,z_fwd)]
y_fwd = [i * math.sin(j) for i,j in zip(r_fwd,z_fwd)]
z_fwd = [i for i in z_fwd]
for i_fwd in uniquenumber1:
new_row.append(uniquenumber.index(i_fwd))
# # Write the connection relation to text file
# with open('result.txt', 'w') as f:
# for i in range(len(new_row)):
# f.write("%s %s \n" % (str(i), str(new_row[i])))
####################### Deal with backward growth ###############################
cellnumber_bw = []
stepnumber_bw = []
currentnode_bw = []
previousnode_bw = []
r_bw = []
theta_bw = []
z_bw = []
uniquenumber_bw = []
uniquenumber1_bw = []
x_bw = []
y_bw = []
new_row_bw = []
with open('pos_bi_bw_500_20.txt') as f: # Remember to change the file name
content_bw = f.readlines()
for line in content_bw:
currentnode_bw.append(line.split()[9])
previousnode_bw.append(line.split()[8])
cellnumber_bw.append(line.split()[6])
uniquenumber_bw.append(line.split()[6] + '-' + line.split()[9])
uniquenumber1_bw.append(line.split()[6] + '-' + line.split()[8])
stepnumber_bw.append(line.split()[7])
r_bw.append(float(line.split()[0]))
theta_bw.append(float(line.split()[1]))
z_bw.append(float(line.split()[2]))
x_bw = [i * math.cos(j) for i, j in zip(r_bw,z_bw)]
y_bw = [i * math.sin(j) for i,j in zip(r_bw,z_bw)]
z_bw = [i for i in z_bw]
for i_bw in uniquenumber1_bw:
new_row_bw.append(uniquenumber_bw.index(i_bw))
# # # Write the connection relation to text file
# # with open('result.txt', 'w') as f:
# # for i in range(len(new_row)):
# # f.write("%s %s \n" % (str(i), str(new_row[i])))
####################### Start to write VTK format ############################
stepnumber = [int(float(i)) for i in stepnumber]
cellnumber = [int(float(i)) for i in cellnumber]
NoStep = max(stepnumber)
NoCell = max(cellnumber)
CellType = 3 # In VTK format, Type 3 is VTK_LINE
counter = 0
while counter < NoStep:
# while counter < 2:
print counter
new_x = []
new_xb = []
new_yb = []
new_y = []
new_z = []
new_zb = []
new_connection = []
new_connectionb = []
for i,s in enumerate(stepnumber):
if s <= counter:
new_x.append(x_fwd[i])
new_xb .append(x_bw[i])
new_y.append(y_fwd[i])
new_yb.append(y_bw[i])
new_z.append(z_fwd[i])
new_zb.append(z_bw[i])
new_connection.append(new_row[i])
new_connectionb.append(new_row_bw[i])
else:
totalx = new_x + new_xb
totaly = new_y + new_yb
totalz = new_z + new_zb
totalconnection = new_connection + [r + len(new_connection) for r in new_connectionb]
with open('Step%s.vtk' %s,'wb') as output:
output.write("# vtk DataFile Version 3.0\n")
output.write("%i Cells\n" %(NoCell+1))
output.write("ASCII\n")
output.write("DATASET UNSTRUCTURED_GRID\n")
output.write("POINTS %d FLOAT\n" % (len(totalx)))
for i in range(len(totalx)):
output.write("%s %s %s\n" % (str(totalx[i]), str(totaly[i]),str(totalz[i])))
output.write(("CELLS %d %d\n" % (len(totalconnection), len(totalconnection) * 3)))
for j in range(len(totalconnection)):
output.write("%d %s %s\n" % (2, str(j), str(totalconnection[j])))
output.write("CELL_TYPES %d\n" % (len(totalconnection)))
for m in range(len(totalconnection)):
output.write("%d\n" % CellType)
output.write("CELL_DATA %d\n" % (len(totalx)))
output.write("SCALARS cell_scalars int 1\nLOOKUP_TABLE default\n")
for n in range(len(new_x)):
output.write("0\n")
for p in range(len(new_xb)):
output.write("1\n")
counter +=1
break
print datetime.datetime.now()