forked from stanfordhpccenter/HTR-solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeXdmfFile.py
executable file
·237 lines (195 loc) · 9.39 KB
/
makeXdmfFile.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
226
227
228
229
230
231
232
233
234
235
236
237
import os
import shutil
import sys
import h5py
import subprocess
import argparse
import glob
XMF_HEADER = """<?xml version="1.0" ?>
<!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" []>
<Xdmf xmlns:xi="http://www.w3.org/2003/XInclude" Version="3.0">
<Domain Name="Fluid">
<Grid Name="FluidTimeSeries" GridType="Collection" CollectionType="Temporal">
"""
XMF_SOLUTION_HEADER = """
<Grid Name="@NAME" GridType="Collection" CollectionType="Spatial">
<Time Value="@TIMESTEP"/>
"""
XMF_TILE_HEADER = """
<Grid Name="Tile @N" GridType="Uniform">
<Topology TopologyType="3DRectMesh" Dimensions="@XPOINTS @YPOINTS @ZPOINTS"></Topology>
<Geometry GeometryType="VXVYVZ">
<DataItem Dimensions="@ZPOINTS" NumberType="Float" Precision="8" Format="HDF">@HDF_FILE:/ZFaceCoordinates</DataItem>
<DataItem Dimensions="@YPOINTS" NumberType="Float" Precision="8" Format="HDF">@HDF_FILE:/YFaceCoordinates</DataItem>
<DataItem Dimensions="@XPOINTS" NumberType="Float" Precision="8" Format="HDF">@HDF_FILE:/XFaceCoordinates</DataItem>
</Geometry>
"""
XMF_SOLUTION_SCALAR= """
<Attribute Name="@NAME" AttributeType="Scalar" Center="Cell">
<DataItem Dimensions="@CELLS" NumberType="Float" Precision="8" Format="HDF">@HDF_FILE:/@NAME</DataItem>
</Attribute>
"""
XMF_SOLUTION_SCALAR2= """
<Attribute Name="@NAME" AttributeType="Scalar" Center="Cell">
<DataItem Dimensions="@CELLS" NumberType="Float" Precision="8" Format="HDF">@HDF_FILE:/@GROUP/@NAME</DataItem>
</Attribute>
"""
XMF_SOLUTION_VECTOR= """
<Attribute Name="@NAME" AttributeType="Vector" Center="Cell">
<DataItem Dimensions="@CELLS 3" NumberType="Float" Precision="8" Format="HDF">@HDF_FILE:/@GROUP/@NAME</DataItem>
</Attribute>
"""
XMF_TILE_FOOTER = """
</Grid>
"""
XMF_SOLUTION_FOOTER = """
</Grid>
"""
XMF_FOOTER = """
</Grid>
</Domain>
</Xdmf>
"""
def progressbar(it, prefix="", suffix = '', decimals = 1, length = 100, fill = '-', file=sys.stdout):
count = len(it)
def show(j):
percent = ("{0:." + str(decimals) + "f}").format(100 * (j / float(count)))
filledLength = int(length * j // count)
bar = fill * filledLength + '-' * (length - filledLength)
file.write("\r%s |%s| %s%% %s" % (prefix, bar, percent, suffix))
file.flush()
show(0)
for i, item in enumerate(it):
yield i, item
show(i+1)
file.write("\n")
file.flush()
parser = argparse.ArgumentParser()
parser.add_argument('--sampledir', nargs='?', const='.', default='.',
help='directory with all the simulation output')
parser.add_argument('--debugOut', default=False, action='store_true',
help='flag to process debug output of the simulation')
args = parser.parse_args()
sample_dir = os.path.abspath(args.sampledir)
# Make directories for viz data
out_dir = os.path.abspath("viz_data")
grid_dir = os.path.join(out_dir,"grid")
if not os.path.exists(grid_dir):
os.makedirs(grid_dir)
if not os.path.exists(out_dir):
os.makedirs(out_dir)
# Check for solution snapshots
if (args.debugOut):
snapshots = glob.glob(os.path.join(sample_dir,"debugOut"))
else:
snapshots = glob.glob(os.path.join(sample_dir,"fluid_iter*"))
if (len(snapshots) == 0): assert False, "No solution files provided"
# sort files by timestep
snapshots.sort(key=lambda x: x[len(os.path.join(sample_dir, "fluid_iter")):])
with open('out_fluid.xmf', 'w') as xmf_out:
xmf_out.write(XMF_HEADER)
for i, sn in progressbar(snapshots, "Converting:"):
# We still need to convert H5T_ARRAYs to numpy vectors...
# TODO: get rid of this step as soon as you find out how to read H5T_ARRAYs with XDMF
hdf_sol = None
solutionfile = os.path.join(out_dir,sn.split('/')[-1])+".hdf"
if not os.path.exists(solutionfile):
hdf_sol = h5py.File(solutionfile, "w")
####################################################################################
tiles = glob.glob(os.path.join(sn,"*hdf"))
# Load attributes
hdf_in = h5py.File(tiles[0], 'r')
simTime = hdf_in.attrs.get("simTime")
SpeciesNames = hdf_in.attrs.get("SpeciesNames")
hdf_in.close()
xmf_out.write(XMF_SOLUTION_HEADER
.replace('@NAME','%s'% sn.split('/')[-1])
.replace('@TIMESTEP','%.8g'% simTime))
# Set reference to each tile
for j, tl in enumerate(tiles):
hdf_in = h5py.File(tl, 'r')
# Sanity check
assert simTime == hdf_in.attrs.get("simTime")
assert (SpeciesNames == hdf_in.attrs.get("SpeciesNames")).all()
# Extract domain size.
nx = hdf_in['pressure'].shape[0]
ny = hdf_in['pressure'].shape[1]
nz = hdf_in['pressure'].shape[2]
# Generate grid file if it does not exist
gridfile = os.path.join(grid_dir, tl.split('/')[-1])
if not os.path.exists(gridfile):
with h5py.File(gridfile, "w") as hdf_grid:
centerCoordinates = hdf_in["centerCoordinates"][:][:,:,:,:]
cellWidth = hdf_in["cellWidth"][:][:,:,:,:]
XFaceCoordinates = []
for i in range(nx):
XFaceCoordinates.append(centerCoordinates[i,0,0,2]-0.5*cellWidth[i,0,0,2])
XFaceCoordinates.append(centerCoordinates[nx-1,0,0,2]+0.5*cellWidth[nx-1,0,0,2])
YFaceCoordinates = []
for i in range(ny):
YFaceCoordinates.append(centerCoordinates[0,i,0,1]-0.5*cellWidth[0,i,0,1])
YFaceCoordinates.append(centerCoordinates[0,ny-1,0,1]+0.5*cellWidth[0,ny-1,0,1])
ZFaceCoordinates = []
for i in range(nz):
ZFaceCoordinates.append(centerCoordinates[0,0,i,0]-0.5*cellWidth[0,0,i,0])
ZFaceCoordinates.append(centerCoordinates[0,0,nz-1,0]+0.5*cellWidth[0,0,nz-1,0])
hdf_grid["XFaceCoordinates"] = XFaceCoordinates
hdf_grid["YFaceCoordinates"] = YFaceCoordinates
hdf_grid["ZFaceCoordinates"] = ZFaceCoordinates
# We still need to convert H5T_ARRAYs to numpy vectors...
# TODO: get rid of this step as soon as you find out how to read H5T_ARRAYs with XDMF
group = tl.split('/')[-1][0:-4]
if hdf_sol != None:
hdf_sol[group+"/velocity"] = hdf_in["velocity"][:][:,:,:,:]
for isp, sp in enumerate(SpeciesNames):
hdf_sol[group+"/X_"+sp.decode()] = hdf_in['MolarFracs'][:][:,:,:,isp]
####################################################################################
xmf_out.write(XMF_TILE_HEADER
.replace("@N", "%s" % tl.split('/')[-1][0:-4])
.replace("@XPOINTS", "%s" % str(nx+1))
.replace("@YPOINTS", "%s" % str(ny+1))
.replace("@ZPOINTS", "%s" % str(nz+1))
.replace("@HDF_FILE", gridfile))
xmf_out.write(XMF_SOLUTION_SCALAR
.replace("@NAME", "%s" % "pressure")
.replace("@HDF_FILE", "%s" % tl)
.replace("@CELLS", "%s %s %s" % (nx,ny,nz)))
xmf_out.write(XMF_SOLUTION_SCALAR
.replace("@NAME","%s" % "rho")
.replace("@HDF_FILE", "%s" % tl)
.replace("@CELLS", "%s %s %s" % (nx,ny,nz)))
xmf_out.write(XMF_SOLUTION_SCALAR
.replace("@NAME","%s" % "temperature")
.replace("@HDF_FILE", "%s" % tl)
.replace("@CELLS", "%s %s %s" % (nx,ny,nz)))
for sp in SpeciesNames:
xmf_out.write(XMF_SOLUTION_SCALAR2
.replace("@NAME","%s" % "X_"+sp.decode())
.replace("@GROUP", "%s" % group)
.replace("@HDF_FILE", "%s" % solutionfile)
#.replace("@HDF_FILE", "%s" % tl)
.replace('@CELLS', '%s %s %s' % (nx,ny,nz)))
xmf_out.write(XMF_SOLUTION_VECTOR
.replace("@NAME", "%s" % "velocity")
.replace("@GROUP", "%s" % group)
.replace("@HDF_FILE", "%s" % solutionfile)
#.replace("@HDF_FILE", "%s" % tl)
.replace("@CELLS", "%s %s %s" % (nx,ny,nz)))
if (args.debugOut):
xmf_out.write(XMF_SOLUTION_SCALAR
.replace("@NAME","%s" % "shockSensorX")
.replace("@HDF_FILE", "%s" % tl)
.replace("@CELLS", "%s %s %s" % (nx,ny,nz)))
xmf_out.write(XMF_SOLUTION_SCALAR
.replace("@NAME","%s" % "shockSensorY")
.replace("@HDF_FILE", "%s" % tl)
.replace("@CELLS", "%s %s %s" % (nx,ny,nz)))
xmf_out.write(XMF_SOLUTION_SCALAR
.replace("@NAME","%s" % "shockSensorZ")
.replace("@HDF_FILE", "%s" % tl)
.replace("@CELLS", "%s %s %s" % (nx,ny,nz)))
xmf_out.write(XMF_TILE_FOOTER)
hdf_in.close()
xmf_out.write(XMF_SOLUTION_FOOTER)
if hdf_sol != None: hdf_sol.close()
xmf_out.write(XMF_FOOTER)