-
Notifications
You must be signed in to change notification settings - Fork 0
/
qiProf.py
218 lines (189 loc) · 6.68 KB
/
qiProf.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
import numpy as np
from mpi4py import MPI
from filereadpar1 import parfread
import os.path
from os import path
import time
import sys
import pickle
comm = MPI.COMM_WORLD
me = comm.Get_rank()
size = comm.Get_size()
expir = 'STD'
Reynolds = 360
if (size % 2) != 0 and size != 1:
if me == 0:
print('# of MPI ranks must be even at this time', file=sys.stderr)
comm.barrier()
sys.exit(1)
if me == 0:
if expir == 'STD':
nph = 1
f = open('./RE{1}/file_{0}_{1}_1'.format(expir,Reynolds))
files = f.readlines()
nfiles = len(files)
else:
nph = 32
files = ['']*3200
for j in range(32):
for i in range(100):
f = open('./RE{1}/file_{0}_{1}_{2}'.format(expir,Reynolds,j+1))
fl = f.readlines()
ng = j + i*32
files[ng] = fl[i]
else:
files = None
nph = None
files = comm.bcast(files,root=0)
nph = comm.bcast(nph,root=0)
nfiles = int(len(files)/1)
count = 1
if expir == 'STD':
skip = 4
else:
skip = 32
nby = 2
for ph in range(nph):
# load the mean fields
savename = './RE{0}/mflds_{1}_{2}.pkl'.format(Reynolds,expir,ph)
count = 1
if path.exists(savename) == False:
for i in range(ph,nfiles,skip):
tind = time.time()
out = parfread(comm,files[i].strip(),me,size,nby)
e = time.time() - tind
if me == 0:
print('Time to load file # {0}: {1}'.format(i,e),flush=True)
tind = time.time()
if i == 0:
ind = 0
x = out[3]
y = out[4]
z = out[5]
[nz, ny, nx] = x.shape
nflds = 3
else:
vx = out[0]
vy = out[1]
vz = out[2]
t = np.arctan2(y,x)
r = np.sqrt(x**2 + y**2)
vr = out[0]*np.cos(t) + out[1]*np.sin(t)
vt =-out[0]*np.sin(t) + out[1]*np.cos(t)
vz = out[2]
out[0][:,:,:] = vz[:,:,:]
out[1][:,:,:] = vr[:,:,:]
out[2][:,:,:] = vt[:,:,:]
if count == 1:
stats = np.zeros((nflds,ny),dtype=np.double)
beta = 1/count
alpha = 1 - beta
stats[0,:] = stats[0,:]*alpha + beta*np.mean(out[0],axis=(0,2))
stats[1,:] = stats[1,:]*alpha + beta*np.mean(out[1],axis=(0,2))
stats[2,:] = stats[2,:]*alpha + beta*np.mean(out[2],axis=(0,2))
count+=1
statsT0 = np.zeros((nflds,ny*nby),dtype=np.double)
statsT1 = statsT0.copy()
iy = (me % nby)*ny
ey = iy + ny
statsT0[:,iy:ey] = stats
comm.Reduce([statsT0,MPI.DOUBLE],[statsT1,MPI.DOUBLE])
statsT1 = statsT1/(size/nby)
if me == 0:
print(ph,'writing data: {0}'.format(savename),flush=True)
with open(savename,'wb') as f:
pickle.dump([statsT1],f,protocol=-1)
comm.barrier()
else:
if me == 0:
print(ph,'opening: {0}'.format(savename),flush=True)
with open(savename,'rb') as f:
statsT = pickle.load(f)
statsT = statsT[0]
else:
statsT = None
statsT= comm.bcast(statsT,root=0)
nflds,ngy = statsT.shape
ny = ngy // nby
iy = (me % nby)*ny
ey = iy+ny
stats = statsT[:,iy:ey]
# begin calculating the quadrant analysis
statsQi = np.zeros((nflds,4,ny),dtype = np.double)
nQi = np.zeros((nflds,4,ny),dtype = np.double)
for i in range(ph,nfiles,skip):
tind = time.time()
out = parfread(comm,files[i].strip(),me,size,nby)
e = time.time() - tind
if me == 0:
print('Time to load file # {0}: {1}'.format(i,e),flush=True)
tind = time.time()
if i == 0:
ind = 0
x = out[3]
y = out[4]
z = out[5]
[nz, ny, nx] = x.shape
nflds = 3
else:
vx = out[0]
vy = out[1]
vz = out[2]
t = np.arctan2(y,x)
r = np.sqrt(x**2 + y**2)
vr = out[0]*np.cos(t) + out[1]*np.sin(t)
vt =-out[0]*np.sin(t) + out[1]*np.cos(t)
vz = out[2]
out[0][:,:,:] = vz[:,:,:]
out[1][:,:,:] = vr[:,:,:]
out[2][:,:,:] = vt[:,:,:]
# subtract the long time/phase average mean from the velocity field
for ifld in range(nflds):
out[ifld][:,:,:] = out[ifld][:,:,:] - stats[ifld,np.newaxis,:,np.newaxis]
# loop over the velocity fields calculating the mean contribution to eache velocity pair
for ifld1 in range(nflds):
# choose second component
if ifld1 == 0:
# if streamwise then calculate < u'v' >_qi
ifld2 = 1
elif ifld1 == 1:
# if radial then calculate < v'w' >_qi
ifld2 = 2
else:
# if azimuthal then calculate < w'u' >_qi
ifld2 = 0
mm1 = (out[ifld1] >= 0).astype(np.double)
mm2 = (out[ifld2] >= 0).astype(np.double)
for m2 in range(2):
for m1 in range(2):
# loop over masks assuming ifld1 is the x-axis
md1 = (1-mm1)*m1 + mm1*(1-m1)
md2 = (1-mm2)*m2 + mm2*(1-m2)
# q = 0,1,2,3
# consider u' v'
# m1 = 0, m2 = 0; q = 0 -> u' > 0, v' < 0: Q_4
# m1 = 1, m2 = 0; q = 1 -> u' < 0, v' < 0: Q_3
# m1 = 0, m2 = 1; q = 2 -> u' > 0, v' > 0: Q_1
# m1 = 0, m2 = 1; q = 3 -> u' < 0, v' > 0: Q_2
q = m1 + 2*m2
nQi[ifld1,q,:] += (
np.sum(md1*md2,axis=(0,2))
)
statsQi[ifld1,q,:] += (
np.sum(md1*md2*out[ifld1]*out[ifld2],axis=(0,2))
)
statsQiG0 = np.zeros((nflds,4,ny*nby),dtype=np.double)
statsQiG1 = statsQiG0.copy()
nQiG0 = np.zeros((nflds,4,ny*nby),dtype=np.double)
nQiG1 = nQiG0.copy()
iy = (me % nby)*ny
ey = iy + ny
statsQiG0[:,:,iy:ey] = statsQi
nQiG0[:,:,iy:ey] = nQi
comm.Reduce([statsQiG0,MPI.DOUBLE],[statsQiG1,MPI.DOUBLE])
comm.Reduce([nQiG0,MPI.DOUBLE],[nQiG1,MPI.DOUBLE])
savename = './RE{0}/statsQi_{1}_{2}.pkl'.format(Reynolds,expir,ph)
if me == 0:
print(ph,'writing quadrants: {0}'.format(savename),flush=True)
with open(savename,'wb') as f:
pickle.dump([statsQiG1,nQiG1],f,protocol=-1)