-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_final.py
385 lines (295 loc) · 11.7 KB
/
plot_final.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
from scipy import *
from scipy import linalg
from scipy import fftpack
from numpy.fft import fftshift, ifftshift
from scipy.fftpack import dct as dct
from pylab import *
import cPickle as pickle
import ConfigParser
import argparse
import h5py
argparser = argparse.ArgumentParser()
argparser.add_argument("-p", "--path", type=str, default=".",
help='specify the directory containing the data')
argparser.add_argument("-N", type=int, default=None,
help='Override Number of Fourier modes given in the config file')
argparser.add_argument("-M", type=int, default=None,
help='Override Number of Chebyshev modes in the config file')
argparser.add_argument("-Re", type=float, default=None,
help="Override Reynold's number in the config file")
argparser.add_argument("-b", type=float, default=None,
help='Override beta of the config file')
argparser.add_argument("-Wi", type=float, default=None,
help='Override Weissenberg number of the config file')
argparser.add_argument("-kx", type=float, default=None,
help='Override wavenumber of the config file')
argparser.add_argument("-Newt", action='store_true',
help='Newtonian data flag')
args = argparser.parse_args()
N = args.N
M = args.M
Re = args.Re
beta = args.b
Wi = args.Wi
kx = args.kx
config = ConfigParser.RawConfigParser()
fp = open(args.path + '/config.cfg')
config.readfp(fp)
if N == None : N = config.getint('General', 'N')
if M == None : M = config.getint('General', 'M')
if kx == None : kx = config.getfloat('General', 'kx')
if Re == None : Re = config.getfloat('General', 'Re')
if Wi == None : Wi = config.getfloat('General', 'Wi')
if beta == None : beta = config.getfloat('General', 'beta')
dt = config.getfloat('Time Iteration', 'dt')
totTime = config.getfloat('Time Iteration', 'totTime')
numFrames = config.getint('Time Iteration', 'numFrames')
dealiasing = config.getboolean('Time Iteration', 'Dealiasing')
Nf = 2*N
Mf = 2*M
fp.close()
numTimeSteps = int(totTime / dt)
kwargs = {'N': N, 'M': M, 'Nf': Nf, 'Mf':Mf,
'Re': Re,'Wi': Wi, 'beta': beta, 'kx': kx,'time':
totTime, 'dt':dt, 'dealiasing':dealiasing }
if args.Newt:
#inFileName = args.path + "/traj_psi.h5".format()
inFileName = args.path + "/final.h5".format()
twsFileName = args.path + "/pf-N{N}-M{M}-kx{kx}-Re{Re}.pickle".format(**kwargs)
else:
#inFileName = args.path + "/traj.h5".format()
#inFileName = args.path + "/initial_linear.h5".format()
#inFileName2 = "./initial_full.h5".format()
#inFileName = args.path + "/linear_final.h5".format()
#inFileName2 = "./full_final.h5".format()
inFileName = args.path + "/output/final.h5".format()
print 'Reading from: ', inFileName
#twsFileName = args.path + "/pf-N{N}-M{M}-kx{kx}-Re{Re}-b{beta}-Wi{Wi}.pickle".format(**kwargs)
CNSTS = kwargs
def load_hdf5_snapshot_visco(fp, time):
dataset_id = "/t{0:f}".format(time)
print dataset_id
psi = array(fp[dataset_id+"/psi"])
cxx = array(fp[dataset_id+"/cxx"])
cyy = array(fp[dataset_id+"/cyy"])
cxy = array(fp[dataset_id+"/cxy"])
return psi, cxx, cyy, cxy
def load_hdf5_snapshot(fp, time):
dataset_id = "/t{0:f}".format(time)
print dataset_id
inarr = array(fp[dataset_id])
return inarr
def forward_cheb_transform(GLreal, CNSTS):
"""
Use a real FFT to transform a single array from the Gauss-Labatto grid to
Chebyshev polynomials.
Note, this uses a real FFT therefore you must apply the transformations in
the other directions before this one, otherwise you will loose the data from
the imaginary parts.
"""
M = CNSTS['M']
Mf = CNSTS['Mf']
Ly = CNSTS['Ly']
tmp = dct(real(GLreal), type=1)
out = zeros(M, dtype='complex')
# Renormalise and divide by c_k to convert to Chebyshev polynomials
out[0] = (0.5/(Mf-1.0)) * tmp[0]
out[1:M-1] = (1.0/(Mf-1.0)) * tmp[1:M-1]
if dealiasing:
out[M-1] = (1.0/(Mf-1.0)) * tmp[M-1]
else:
out[M-1] = (0.5/(Mf-1.0)) * tmp[M-1]
return out
def backward_cheb_transform(cSpec, CNSTS):
"""
Use a real FFT to transform a single array of Chebyshev polynomials to the
Gauss-Labatto grid.
"""
M = CNSTS['M']
# Define the temporary vector for the transformation
tmp = zeros(Mf)
# The first half contains the vector on the Gauss-Labatto points * c_k
tmp[0] = real(cSpec[0])
tmp[1:M] = 0.5*real(cSpec[1:M])
tmp[Mf-1] = 2*tmp[Mf-1]
out = dct(tmp, type=1)
tmp[0] = imag(cSpec[0])
tmp[1:M] = 0.5*imag(cSpec[1:M])
tmp[Mf-1] = 2*tmp[Mf-1]
out += dct(tmp, type=1) * 1.j
return out[0:Mf]
def stupid_transform_i(GLspec, CNSTS):
"""
apply the Chebyshev transform the stupid way.
"""
M = CNSTS['M']
Mf = CNSTS['Mf']
out = zeros(Mf, dtype='complex')
for i in range(Mf):
out[i] += GLspec[0]
for j in range(1,M-1):
out[i] += GLspec[j]*cos(pi*i*j/(Mf-1))
out[i] += GLspec[M-1]*cos(pi*i)
del i,j
return out
def plot_comp_modes(arr_ti, arr_true, phase_factor, arrname, time, CNSTS):
fig=gcf()
fig.suptitle(
'${0}$ linear red, full in green at {1}'.format(arrname, time))
subplot_indices= {0:231, 1:232, 2:233, 3:234, 4:235, 5:236}
for n in range(2):
ti_mode = stupid_transform_i(arr_ti[:, n], CNSTS)
tws_mode = stupid_transform_i(arr_true[:, n]*(phase_factor**n), CNSTS)
# plot graph
splt =subplot(subplot_indices[n])
plot(y, real(tws_mode), 'g')
plot(y, real(ti_mode), 'r', linewidth=2.0)
title('{0}'.format(n))
xlabel('y')
ylabel('$Re[{0}_{1}]$'.format(arrname, n))
for n in range(2):
ti_mode = stupid_transform_i(arr_ti[:, n], CNSTS)
tws_mode = stupid_transform_i(arr_true[:, n]*(phase_factor**n), CNSTS)
# plot graph
splt =subplot(subplot_indices[(3+n)])
plot(y, imag(tws_mode), 'g')
plot(y, imag(ti_mode), 'r', linewidth=2.0)
title('{0}'.format(n))
xlabel('y')
ylabel('$Im[{0}_{1}]$'.format(arrname, n))
#fig.tight_layout()
if arrname == "\psi" :
savefig(args.path + "/modal_comparison_{0}.pdf".format("psi"))
else:
savefig(args.path + "/modal_comparison_{0}.pdf".format(arrname))
fig.clf()
def plot_modes(arr_ti, arrname, time, CNSTS):
fig=gcf()
fig.suptitle(
'${0}$ time iteration red at t = {1}'.format(arrname, time))
subplot_indices= {0:231, 1:232, 2:233, 3:234, 4:235, 5:236}
for n in range(3):
ti_mode = stupid_transform_i(arr_ti[:, n], CNSTS)
# plot graph
splt =subplot(subplot_indices[n])
splt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
splt.yaxis.major.formatter._useMathText = True
plot(y, real(ti_mode), 'r', linewidth=2.0)
title('{0}'.format(n))
xlabel('y')
ylabel('$Re[{0}_{1}]$'.format(arrname, n))
if n ==1:
if arrname == "\psi" :
savetxt(args.path + "/{0}_mode1.dat".format("psi"),
real(vstack((y, ti_mode)).T))
else:
savetxt(args.path + "/{0}_mode1.dat".format(arrname),
real(vstack((y, ti_mode)).T))
for n in range(3):
ti_mode = stupid_transform_i(arr_ti[:, n], CNSTS)
# plot graph
splt =subplot(subplot_indices[(3+n)])
splt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
splt.yaxis.major.formatter._useMathText = True
plot(y, imag(ti_mode), 'r', linewidth=2.0)
title('{0}'.format(n))
xlabel('y')
ylabel('$Im[{0}_{1}]$'.format(arrname, n))
#fig.tight_layout()
if arrname == "\psi" :
savefig(args.path + "/modes_{0}.pdf".format("psi"))
else:
savefig(args.path + "/modes_{0}.pdf".format(arrname))
fig.clf()
##### MAIN ######
print"=====================================\n"
print "Settings:"
print """------------------------------------
N \t\t= {N}
M \t\t= {M}
Re \t\t= {Re}
kx \t\t= {kx}
dt\t\t= {dt}
totTime\t\t= {t}
NumTimeSteps\t= {NT}
------------------------------------
""".format(N=N, M=M, kx=kx, Re=Re, dt=dt, NT=numTimeSteps, t=totTime)
time = totTime
f = h5py.File(inFileName, "r")
#f2 = h5py.File(inFileName2, "r")
if args.Newt:
psi = array(f["/psi"])
psi_ti = psi_ti.reshape((2*N+1, M)).T
#psi_true, Nu = pickle.load(open(twsFileName, 'r'))
else:
#psi_ti, cxx_ti, cyy_ti, cxy_ti = load_hdf5_snapshot_visco(f, time)
psi_ti = array(f["/psi"])
cxx_ti = array(f["/cxx"])
cyy_ti = array(f["/cyy"])
cxy_ti = array(f["/cxy"])
#psi_ti = psi_ti.reshape((2*N+1, M)).T
#cxx_ti = cxx_ti.reshape((2*N+1, M)).T
#cyy_ti = cyy_ti.reshape((2*N+1, M)).T
#cxy_ti = cxy_ti.reshape((2*N+1, M)).T
psi_ti = psi_ti.reshape((N+1, M)).T
psi_ti = hstack((psi_ti, conj(psi_ti[:, N:0:-1])))
cxx_ti = cxx_ti.reshape((N+1, M)).T
cxx_ti = hstack((cxx_ti, conj(cxx_ti[:, N:0:-1])))
cyy_ti = cyy_ti.reshape((N+1, M)).T
cyy_ti = hstack((cyy_ti, conj(cyy_ti[:, N:0:-1])))
cxy_ti = cxy_ti.reshape((N+1, M)).T
cxy_ti = hstack((cxy_ti, conj(cxy_ti[:, N:0:-1])))
##psi_true, cxx_true, cyy_true, cxy_true, Nu = pickle.load(open(twsFileName, 'r'))
#psi_true = array(f2["/psi"])
#cxx_true = array(f2["/cxx"])
#cyy_true = array(f2["/cyy"])
#cxy_true = array(f2["/cxy"])
##psi_true = psi_true.reshape((2*N+1, M)).T
##cxx_true = cxx_true.reshape((2*N+1, M)).T
##cyy_true = cyy_true.reshape((2*N+1, M)).T
##cxy_true = cxy_true.reshape((2*N+1, M)).T
#psi_true = psi_true.reshape((N+1, M)).T
#psi_true = hstack((psi_true, conj(psi_true[:, N:0:-1])))
#cxx_true = cxx_true.reshape((N+1, M)).T
#cxx_true = hstack((cxx_true, conj(cxx_true[:, N:0:-1])))
#cyy_true = cyy_true.reshape((N+1, M)).T
#cyy_true = hstack((cyy_true, conj(cyy_true[:, N:0:-1])))
#cxy_true = cxy_true.reshape((N+1, M)).T
#cxy_true = hstack((cxy_true, conj(cxy_true[:, N:0:-1])))
f.close()
#psi_true = psi_true.reshape(2*N+1, M).T
#psi_true = ifftshift(psi_true, axes=1)
#cxx_true = cxx_true.reshape(2*N+1, M).T
#cxx_true = ifftshift(cxx_true, axes=1)
#cyy_true = cyy_true.reshape(2*N+1, M).T
#cyy_true = ifftshift(cyy_true, axes=1)
#cxy_true = cxy_true.reshape(2*N+1, M).T
#cxy_true = ifftshift(cxy_true, axes=1)
# Choose the value of the streamfunction at a point for 1st mode,
# psi_1(0) = 1
PSIr1 = stupid_transform_i(real(psi_ti[:, 1]), CNSTS) +\
stupid_transform_i(imag(psi_ti[:, 1]), CNSTS)*1.j
# calculate a phase factor 1 / (psi_1(0)) such that the streamfunction is real
phase_factor = 1./PSIr1[Mf/2]
# scale the phase factor so that it is just a phase with no amplitude,
phase_factor = phase_factor / sqrt(phase_factor*conj(phase_factor))
for n in range(1,N+1):
psi_ti[:, n] = phase_factor**n*psi_ti[:, n]
psi_ti[:, 2*N+1-n] = phase_factor**(-n)*psi_ti[:, 2*N+1-n]
cxx_ti[:, n] = phase_factor**n*cxx_ti[:, n]
cxx_ti[:, 2*N+1-n] = phase_factor**(-n)*cxx_ti[:, 2*N+1-n]
cyy_ti[:, n] = phase_factor**n*cyy_ti[:, n]
cyy_ti[:, 2*N+1-n] = phase_factor**(-n)*cyy_ti[:, 2*N+1-n]
cxy_ti[:, n] = phase_factor**n*cxy_ti[:, n]
cxy_ti[:, 2*N+1-n] = phase_factor**(-n)*cxy_ti[:, 2*N+1-n]
y = cos(pi*arange(Mf)/(Mf-1))
#y = 2.0*arange(Mf)/(Mf-1.) -1
# Compare mode by mode
#plot_comp_modes(psi_ti, psi_true, phase_factor, "\psi", time, CNSTS)
#plot_comp_modes(cxx_ti, cxx_true, phase_factor, "cxx", time, CNSTS)
#plot_comp_modes(cyy_ti, cyy_true, phase_factor, "cyy", time, CNSTS)
#plot_comp_modes(cxy_ti, cxy_true, phase_factor, "cxy", time, CNSTS)
plot_modes(psi_ti, "\psi", time, CNSTS)
plot_modes(cxx_ti, "cxx", time, CNSTS)
plot_modes(cyy_ti, "cyy", time, CNSTS)
plot_modes(cxy_ti, "cxy", time, CNSTS)