-
Notifications
You must be signed in to change notification settings - Fork 379
/
Copy pathwrite_pview_file.py
225 lines (183 loc) · 8.13 KB
/
write_pview_file.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import numpy as np
import struct
import os
def write_paraview_file_structmesh(fname,xp,yp,ccdata,ncdata):
outfile=open(fname,'w')
Npx=xp.shape[1]
Npy=xp.shape[0]
zero=0
one=1
outfile.write("<?xml version=\"1.0\"?>\n")
outfile.write("<VTKFile type=\"StructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">\n")
outfile.write("<StructuredGrid WholeExtent=\"%d %d %d %d %d %d\">\n"%(one,Npx,one,Npy,one,one))
#piece extent is useful when writing in parallel
#it corresponds to extents for each processor in global coordinates, eg: 1-2 is proc 0, 3-4 is proc 0
outfile.write("<Piece Extent=\"%d %d %d %d %d %d\">\n"%(one,Npx,one,Npy,one,one))
outfile.write("<PointData>\n")
n_ncdata=ncdata.shape[0]
if(n_ncdata > 0):
for ndataset in range(n_ncdata):
outfile.write("<DataArray type=\"Float32\" Name=\"node_data%d\" format=\"ascii\">\n"%(ndataset))
for i in range(ncdata.shape[1]):
outfile.write("%e\t"%(ncdata[ndataset][i]))
outfile.write("\n</DataArray>\n")
outfile.write("</PointData>\n")
outfile.write("<CellData>\n")
n_ccdata=ccdata.shape[0]
if(n_ccdata > 0):
for ndataset in range(n_ccdata):
outfile.write("<DataArray type=\"Float32\" Name=\"cell_data%d\" format=\"ascii\">\n"%(ndataset))
for i in range(ccdata.shape[1]):
outfile.write("%e\t"%(ccdata[ndataset][i]))
outfile.write("\n</DataArray>\n")
outfile.write("</CellData>\n")
outfile.write("<Points>\n")
outfile.write("<DataArray type=\"Float32\" NumberOfComponents=\"3\" format=\"ascii\">\n")
for j in range(Npy):
for i in range(Npx):
outfile.write("%e\t%e\t%e\t"%(xp[j][i],yp[j][i],zero))
outfile.write("\n</DataArray>\n")
outfile.write("</Points>\n")
outfile.write("</Piece>\n")
outfile.write("</StructuredGrid>\n")
outfile.write("</VTKFile>")
def write_paraview_file_unst_trimesh(fname,pts,conn,ccdata,ncdata):
outfile=open(fname,'w')
Npts=pts.shape[0]
Ntri=conn.shape[0]
zero=0
outfile.write("<?xml version=\"1.0\"?>\n")
outfile.write("<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">\n")
outfile.write("<UnstructuredGrid>\n")
outfile.write("<Piece NumberOfPoints=\"%d\" NumberOfCells=\"%d\">\n"%(Npts,Ntri))
outfile.write("<PointData>\n")
n_ncdata=ncdata.shape[0]
if(n_ncdata > 0):
for ndataset in range(n_ncdata):
outfile.write("<DataArray type=\"Float32\" Name=\"Point_data%d\" format=\"ascii\">\n"%(ndataset))
for i in range(ncdata.shape[1]):
outfile.write("%e "%(ncdata[ndataset][i]))
outfile.write("\n</DataArray>\n")
outfile.write("</PointData>\n")
outfile.write("<CellData>\n")
n_ccdata=ccdata.shape[0]
if(n_ccdata > 0):
for ndataset in range(n_ccdata):
outfile.write("<DataArray type=\"Float32\" Name=\"Cell_data%d\" format=\"ascii\">\n"%(ndataset))
for i in range(ccdata.shape[1]):
outfile.write("%e "%(ccdata[ndataset][i]))
outfile.write("\n</DataArray>\n")
outfile.write("</CellData>\n")
outfile.write("<Points>\n")
outfile.write("<DataArray type=\"Float32\" Name=\"Points\" NumberOfComponents=\"3\" format=\"ascii\">\n")
for i in range(Npts):
outfile.write("%e\t%e\t%e\t"%(pts[i][0],pts[i][1],zero))
outfile.write("\n</DataArray>\n")
outfile.write("</Points>\n")
outfile.write("<Cells>\n")
outfile.write("<DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">\n")
for i in range(Ntri):
for j in range(3):
outfile.write("%d "%(conn[i][j]))
outfile.write("\n</DataArray>\n")
outfile.write("<DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">\n")
for i in range(Ntri):
offs=3*(i+1)
outfile.write("%d "%(offs))
outfile.write("\n</DataArray>\n")
outfile.write("<DataArray type=\"Int64\" Name=\"types\" format=\"ascii\">\n")
for i in range(Ntri):
tri_type=5
outfile.write("%d "%(tri_type))
outfile.write("\n</DataArray>\n")
#note, different types are
#triangle - 5
#quad - 9
#tetrahedron - 10
#hexahedron - 12
#prism - 13
#pyramid - 14
outfile.write("</Cells>\n")
outfile.write("</Piece>\n")
outfile.write("</UnstructuredGrid>\n")
outfile.write("</VTKFile>\n")
outfile.close()
def write_paraview_file_cartmesh(fname,dx,prob_lo,N,ncdata,ccdata):
one=1
outfile=open(fname,'w')
outfile.write("<?xml version=\"1.0\"?>\n")
outfile.write("<VTKFile type=\"RectilinearGrid\" version=\"0.1\" byte_order=\"LittleEndian\">\n")
outfile.write("<RectilinearGrid WholeExtent=\"%d\t%d\t%d\t%d\t%d\t%d\">\n"%(one,N[0],one,N[1],one,one))
outfile.write("<Piece Extent=\"%d\t%d\t%d\t%d\t%d\t%d\">\n"%(one,N[0],one,N[1],one,one))
outfile.write("<PointData>\n")
n_ncdata=ncdata.shape[0]
if(n_ncdata > 0):
for ndataset in range(n_ncdata):
outfile.write("<DataArray type=\"Float32\" Name=\"Point_data%d\" format=\"ascii\">\n"%(ndataset))
for i in range(ncdata.shape[1]):
outfile.write("%e "%(ncdata[ndataset][i]))
outfile.write("\n</DataArray>\n")
outfile.write("</PointData>\n")
outfile.write("<CellData>\n")
n_ccdata=ccdata.shape[0]
if(n_ccdata > 0):
for ndataset in range(n_ccdata):
outfile.write("<DataArray type=\"Float32\" Name=\"Cell_data%d\" format=\"ascii\">\n"%(ndataset))
for i in range(ccdata.shape[1]):
outfile.write("%e "%(ccdata[ndataset][i]))
outfile.write("\n</DataArray>\n")
outfile.write("</CellData>\n")
outfile.write("<Coordinates>\n")
outfile.write("<DataArray type=\"Float32\" Name=\"X\" format=\"ascii\">\n")
for i in range(N[0]):
outfile.write("%e\t"%(prob_lo[0]+i*dx[0]))
outfile.write("\n</DataArray>\n")
outfile.write("<DataArray type=\"Float32\" Name=\"Y\" format=\"ascii\">\n")
for i in range(N[1]):
outfile.write("%e\t"%(prob_lo[1]+i*dx[1]))
outfile.write("\n</DataArray>\n")
outfile.write("<DataArray type=\"Float32\" Name=\"Z\" format=\"ascii\">\n")
outfile.write("%e\t"%(0.0))
outfile.write("\n</DataArray>\n")
outfile.write("</Coordinates>\n")
outfile.write("</Piece>\n")
outfile.write("</RectilinearGrid>\n")
outfile.write("</VTKFile>")
outfile.close()
def write_paraview_file_particles(fname,pts,ncdata):
outfile=open(fname,'w')
Npts=pts.shape[0]
outfile.write("<?xml version=\"1.0\"?>\n")
outfile.write("<VTKFile type=\"PolyData\" version=\"0.1\" byte_order=\"LittleEndian\" header_type=\"UInt64\">\n")
outfile.write("<PolyData>\n")
outfile.write("<Piece NumberOfPoints=\"%d\" NumberOfVerts=\"0\" NumberOfLines=\"0\" NumberOfStrips=\"0\" NumberOfPolys=\"0\">\n"%(Npts))
outfile.write("<PointData>\n")
n_ncdata=ncdata.shape[0]
if(n_ncdata > 0):
for ndataset in range(n_ncdata):
outfile.write("<DataArray type=\"Float32\" Name=\"Point_data%d\" format=\"ascii\">\n"%(ndataset))
for i in range(ncdata.shape[1]):
outfile.write("%e "%(ncdata[ndataset][i]))
outfile.write("\n</DataArray>\n")
outfile.write("</PointData>\n")
outfile.write("<Points>\n")
outfile.write("<DataArray type=\"Float32\" Name=\"Points\" NumberOfComponents=\"3\" format=\"appended\" offset=\"0\">\n")
outfile.write("</DataArray>\n")
outfile.write("</Points>\n")
outfile.write("</Piece>\n")
outfile.write("</PolyData>\n")
# write AppendedData in special format
# https://vtk.org/Wiki/VTK_XML_Formats#Uncompressed_Data
outfile.write("<AppendedData encoding=\"raw\">\n")
outfile.write("\t_")
outfile.close()
with open(fname, 'ab') as binfile:
arrays_to_write = [pts]
for arr in arrays_to_write:
float_arr = arr.astype(np.float32)
binfile.write(struct.pack('<L', arr.nbytes))
binfile.write(float_arr.tobytes('C'))
outfile = open(fname, 'a')
outfile.write("\n</AppendedData>")
outfile.write("</VTKFile>\n")
outfile.close()