-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_jsheet_null.py
317 lines (283 loc) · 10.5 KB
/
get_jsheet_null.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
"""
driver of the whole pipe line
for derive jsheet properties
"""
#import matplotlib
#import matplotlib.pyplot as plt
import numpy as np
#import athena4_read as ath
import athena_read as ath
import cPickle as pickle
import time
import multiprocessing as mp
from itertools import product
import sys
from sklearn.decomposition import PCA
import scipy.ndimage.measurements as measurements
import scipy.ndimage.morphology as morphology
from hessian import hessian
def dimensions_jsheet(num):
js = jlist_sorted[num]
locs = np.array(js.keys())
xlocs = np.array(zip(z[locs[:,0]],y[locs[:,1]],x[locs[:,2]]))
if (len(xlocs[:,0])<nlim):
#print 'reach < ',nlim
return [0,0,0,(0,0,0),(0,0,0)]
else:
#(1) num of cells in given sheet
ncell = len(js)
#(2) find j_max
maxj = np.sqrt(np.max(np.array(js.values())))
idx_maxj = np.argmax(np.array(js.values()))
#(3) compute cell averaged dissipation \epsilon
eps = np.average(eta*np.array(js.values()))
#(4) compute (lambda,xi,l)
# pca.fit(locs)
pca = PCA(n_components=3).fit(xlocs)
i1 = pca.components_[0]; i2= pca.components_[1]; i3 = pca.components_[2]
nlocs = len(locs[:,0])
d1 = np.array([xlocs[i,:].dot(i1) for i in range(0,nlocs)])
d2 = np.array([xlocs[i,:].dot(i2) for i in range(0,nlocs)])
#d3 = np.array([xlocs[i,:].dot(i3) for i in range(0,len(locs[:,0]))])
# dimensions of the sheet
L1 = np.max([(max(d1)-min(d1)),dx])
L2 = np.max([(max(d2)-min(d2)),dx])
### method 5: using Minkowski functionals where
### thickness is ~ volume/surface_area
L3 = 0.0
volume = ncell*dv
surface = 0.0
locset = set(js.keys())
refset0 = locset.copy() # xy,yz faces
refset1 = locset.copy() # xz face
for item in locset:
(iz,iy,ix) = (item[0],item[1],item[2])
for i in range(-1,2,2):
refset0.add((iz,iy,ix+i))
refset0.add((iz+i,iy,ix))
refset1.add((iz,iy+i,ix))
nface0 = len(refset0.difference(locset))
nface1 = len(refset1.difference(locset))
surface = nface0*dx*dy+nface1*dx*dz
L3 = volume*3.0/surface
#print 'num=',num,' jsheet ncell, volume,surface,L3= ',ncell, volume,surface,L3
### method 1:
### projection onto i3: overestimate
#L3 = np.max([(max(d3)-min(d3)),dx])
### method 2:
### estimate thickness by volume/area: underestimate as sheet might
### be sparse.
##vol = ncells*dx**3*2
##L3 = np.max([vol/(L1*L2),dx])
#### method 3:
#### local projection i3 centered on jmax: still overestimate(?)
#### similar as method4 except use i3 instead of k
##i3 = [1,0,0]
#rad = np.array([np.linalg.norm(np.cross(xlocs[i,:],i3)) for i in range(0,nlocs)])
#idx_dis = [i for i in range(0,nlocs) if (np.abs(rad[i]-rad[idx_maxj]) <= 2.0*dx)]
#ndis = len(idx_dis)
##d3 = np.array(sorted([xlocs[i,:].dot(i3) for i in idx_dis]))
#d3 = np.array(sorted([xlocs[i,:].dot(i3) for i in idx_dis]))
#d_maxj = xlocs[idx_maxj,:].dot(i3)
#idx_maxj = (np.abs(d3-d_maxj)).argmin()
#lower,upper = 0,ndis-1
#for i in range(idx_maxj,1,-1):
# if np.abs(d3[i-1]-d3[i]) >dx*1.414:
# lower = i
# break
#for i in range(idx_maxj,ndis-2,1):
# if np.abs(d3[i+1]-d3[i]) >dx*1.414:
# upper = i
# break
#
##L3 = np.max([(max(d3)-min(d3)),dx])
#L3 = np.abs(d3[upper]-d3[lower])
#L3 = np.max([L3,dx])
### method 4:
### local projection of the fastest slope (k) centered on jmax
### 0) first get estimate of k using 3d hession
### 1) first get distance from the unit vector k for all cells
### 2) then pick out all the cells which have distance |d-d(jmax)|
### smaller than 2 cells;
### 3) separate out the physically connected features within the above
### cylinder selection;
### 4) do the projection among those connected cells and measure the
### maximum distance
# ncol,nstep = 5,2 # nstep=ncol/2
# jbox = np.zeros([ncol,ncol,ncol])
# loclist = [(locs[i,0],locs[i,1],locs[i,2]) for i in range(0,nlocs)]
# knd,jnd,ind = locs[idx_maxj,0],locs[idx_maxj,1],locs[idx_maxj,2]
# for i in range(0,ncol):
# for j in range(0,ncol):
# for k in range(0,ncol):
# tnd = (knd-nstep+k,jnd-nstep+j,ind-nstep+i)
# if tnd in loclist:
# jbox[k,j,i] = js[tnd]
# #print 'in jsheet # ',num, ' jbox= ',jbox
# hess_jbox = hessian(jbox)
# hess_jmax = hess_jbox[:,:,nstep,nstep,nstep]
# w,v = np.linalg.eig(hess_jmax)
# major = 0
# for i in range(1,3):
# if np.abs(w[i])>np.abs(w[i-1]):
# major = i
# hvec = v[:,major]
# #print 'hvec = ',hvec
# ##projection along hvec and then find the thickness
# rad = np.array([np.linalg.norm(np.cross(xlocs[i,:],hvec)) for i in range(0,nlocs)])
# idx_dis = [i for i in range(0,nlocs) if (np.abs(rad[i]-rad[idx_maxj]) <= 1.0*dx)] #2.0*dx)]
# ndis = len(idx_dis)
# #d3 = np.array(sorted([xlocs[i,:].dot(i3) for i in idx_dis]))
# d3 = np.array(sorted([xlocs[i,:].dot(hvec) for i in idx_dis]))
# d_maxj = xlocs[idx_maxj,:].dot(hvec)
# idx_maxj = (np.abs(d3-d_maxj)).argmin()
# lower,upper = 0,ndis-1
# for i in range(idx_maxj,1,-1):
# if np.abs(d3[i-1]-d3[i]) >dx*1.414:
# lower = i
# break
# for i in range(idx_maxj,ndis-2,1):
# if np.abs(d3[i+1]-d3[i]) >dx*1.414:
# upper = i
# break
#
# #L3 = np.max([(max(d3)-min(d3)),dx])
# L3 = np.abs(d3[upper]-d3[lower])
# L3 = np.max([L3,dx])
# i3 = hvec
return [ncell,maxj,eps,(L1,L2,L3),(i1,i2,i3)]
def dimensions_jnull(num):
js = jlist_null[num]
locs = np.array(js.keys())
xlocs = np.array(zip(z[locs[:,0]],y[locs[:,1]],x[locs[:,2]]))
if (len(xlocs[:,0])<nlim):
#print 'reach < ',nlim
return [0,0,0,(0,0,0),(0,0,0)]
else:
#(1) num of cells in given sheet
ncell = len(js)
#(2) find j_max
maxj = np.sqrt(np.max(np.array(js.values())))
idx_maxj = np.argmax(np.array(js.values()))
#(3) compute cell averaged dissipation \epsilon
eps = np.average(eta*np.array(js.values()))
#(4) compute (lambda,xi,l)
# pca.fit(locs)
pca = PCA(n_components=3).fit(xlocs)
i1 = pca.components_[0]; i2= pca.components_[1]; i3 = pca.components_[2]
nlocs = len(locs[:,0])
d1 = np.array([xlocs[i,:].dot(i1) for i in range(0,nlocs)])
d2 = np.array([xlocs[i,:].dot(i2) for i in range(0,nlocs)])
#d3 = np.array([xlocs[i,:].dot(i3) for i in range(0,len(locs[:,0]))])
# dimensions of the sheet
L1 = np.max([(max(d1)-min(d1)),dx])
L2 = np.max([(max(d2)-min(d2)),dx])
### method 5: using Minkowski functionals where
### thickness is ~ volume/surface_area
L3 = 0.0
volume = ncell*dv
surface = 0.0
locset = set(js.keys())
refset0 = locset.copy() # xy,yz faces
refset1 = locset.copy() # xz face
for item in locset:
(iz,iy,ix) = (item[0],item[1],item[2])
for i in range(-1,2,2):
refset0.add((iz,iy,ix+i))
refset0.add((iz+i,iy,ix))
refset1.add((iz,iy+i,ix))
nface0 = len(refset0.difference(locset))
nface1 = len(refset1.difference(locset))
surface = nface0*dx*dy+nface1*dx*dz
L3 = volume*3.0/surface
return [ncell,maxj,eps,(L1,L2,L3),(i1,i2,i3)]
def get_xcenter(x1f,x2f,x3f):
x = x1f+0.5*(x1f[1]-x1f[0])
y = x2f+0.5*(x2f[1]-x2f[0])
z = x3f+0.5*(x3f[1]-x3f[0])
return x[:-1],y[:-1],z[:-1]
def loadData(fname):
jlist_sorted = pickle.load( open(fname, "rb" ) )
return jlist_sorted
def loadNull(fname):
null = pickle.load( open(fname, "rb" ) )
return null
def identifier(jlist,nlist):
j_null=[]
#j_nonu = []
null_copy = nlist[:]
for i in range(0,len(jlist)):
points = set(null_copy).intersection(jlist[i].keys())
if bool(points): # found identity
j_null.append(jlist[i])
#else: # no identity
# j_nonu +=jlist[i]
null_copy=[x for x in null_copy if x not in list(points)]
return j_null #, j_nonu
if __name__=='__main__':
if len( sys.argv ) < 4:
print "calling sequence: python get_jsheet_prop.py targ,ts,te,tstride,[eta=2.5e-4],[nlim=8]"
exit( )
targ = sys.argv[1]
#targ = x2y4z1r64pm1re4000
ts,te,tstride = int(sys.argv[2]),int(sys.argv[3]),int(sys.argv[4])
fdir = '/tigress/jiming/reconnect/athena/bin/'
basename = 'Unstra.out2.'
t,grid = ath.athdf(fdir+targ+'/'+basename+str(ts).zfill(5)+'.athdf',quantities=['x1f','x2f','x3f'])
x,y,z = get_xcenter(grid['x1f'],grid['x2f'],grid['x3f'])
dx = x[1]-x[0]
dy = y[1]-y[0]
dz = z[1]-z[0]
dv = dx*dy*dz
if len(sys.argv)>=6:
eta = float(sys.argv[5])
else:
eta = 2.5e-4
if len(sys.argv)>=7:
nlim = int(sys.argv[6])
else:
nlim = 8
print 'nlim = ',nlim
print 'eta = ',eta
ncells=[]; jmax=[];diss=[];size=[];orient=[]
for frame in np.arange(ts,te,tstride):
# load jlist
fname = fdir+targ+'/'+basename+str(frame).zfill(5)+'.athdf.jlist.p'
jlist_sorted = loadData(fname)
nlist = len(jlist_sorted)
pca = PCA(n_components=3)
# load null pts
fname = fdir+targ+'/'+basename+str(frame).zfill(5)+'.athdf.null.p'
null = loadNull(fname)
#print 'len(jlist_sorted) = ',len(jlist_sorted)
#print 'len(null) = ',len(null)
# separate sheet with null-pts and w/o null-pts
jlist_null = identifier(jlist_sorted,null)
# only consider sheet with more than nlim grid points
nlist = len(jlist_null)
nlist = np.max([i for i in range(nlist) if len(jlist_null[i])>=nlim])
print 'nlist=',nlist
#create a pool and map the target function with multi-arguments
tstart = time.time()
npr = 8
p = mp.Pool(processes=npr)
result = p.map(dimensions_jnull,range(nlist),chunksize=npr)
p.close()
p.join()
tend = time.time()
print 'time spent with ',str(npr), ' processors = ', tend-tstart, ' seconds'
ncells0 = [result[i][0] for i in range(nlist)]
jmax0 = [result[i][1] for i in range(nlist)]
diss0 = [result[i][2] for i in range(nlist)]
size0 = [result[i][3] for i in range(nlist)]
orient0 = [result[i][4] for i in range(nlist)]
pname = fdir+targ+'/'+'jprop_null.'+str(frame).zfill(5)+'.p'
pickle.dump([ncells0,jmax0,diss0,size0,orient0], open(pname, "wb" ),2)
ncells += ncells0 #[result[i][0] for i in range(nlist)]
jmax += jmax0 #[result[i][1] for i in range(nlist)]
diss += diss0 #[result[i][2] for i in range(nlist)]
size += size0 #[result[i][3] for i in range(nlist)]
orient += orient0 #[result[i][4] for i in range(nlist)]
pname = fdir+targ+'/'+'jprop_null_ts='+str(ts)+'_te='+str(te)+'.p'
pickle.dump([ncells,jmax,diss,size,orient], open(pname, "wb" ),2)
# end of the script