-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaceAnalysisOnly.py
363 lines (294 loc) · 12.2 KB
/
spaceAnalysisOnly.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
###!/usr/bin/python
##loading shell commands
import os
import os.path
import glob
import sys
import shutil
import time
from scipy import fftpack
#import scipy.fftpack
# from PyQt4.QtGui import *
import struct
# from scipy import *
import numpy as np
import math
# from evtk.hl import gridToVTK
# from matplotlib import *
# from pylab import *
# import matplotlib as plt
# from matplotlib.ticker import MultipleLocator, FormatStrFormatter
# ---
def time_filter(loctime, tmax):
"""add envelope time"""
return 0.5*(1-math.cos(2*np.pi*loctime/tmax))
class mygrid:
def __init__(self):
self.init=True
self.Nx=None
self.Ny=None
self.Nz=None
self.Nt=None
self.totNr=None
self.totNk=None
self.Lx=1
self.Ly=1
self.Lz=1
self.Lt=1
self.x=None
self.y=None
self.z=None
self.t=None
self.dx=1
self.dy=1
self.dz=1
self.dt=1
self.Nkt=None
self.Nkx=None
self.Nky=None
self.Lkx=1
self.Lky=1
self.Lkz=1
self.Lkt=1
self.kx=None
self.ky=None
self.kz=None
self.kt=None
self.dkx=1
self.dky=1
self.dkz=1
self.dkt=1
class FieldAnalysis:
""" la mia classe"""
def __init__(self, filename):
self.grid=mygrid()
self.filename = filename
self.totalEnergyFunction = 0
self.totalEnergyFFT = 0
self.init_values()
self.print_parameters()
self.set_frequency()
self.print_frequency()
self.collect_data()
def collect_data(self):
self.alldata = np.zeros((self.grid.Nz, self.grid.Ny, self.grid.Nx, self.Nc))
self.alldata = self.analize_field(self.filename)
def analize_field(self, filename, analize=False):
f = open(filename, 'rb')
# - endianness 0:small - 1:big -#
endianness = struct.unpack('i', f.read(4))[0]
# - global grid dim -#
nx = struct.unpack('i', f.read(4))[0]
ny = struct.unpack('i', f.read(4))[0]
nz = struct.unpack('i', f.read(4))[0]
self.grid.Nx = nx
self.grid.Ny = ny
self.grid.Nz = nz
ntot = nx * ny * nz
# - processor grid -#
npx = struct.unpack('i', f.read(4))[0]
npy = struct.unpack('i', f.read(4))[0]
npz = struct.unpack('i', f.read(4))[0]
nproc = npx * npy * npz
# - field components -#
nc = struct.unpack('i', f.read(4))[0]
self.grid.Nx = nx
self.grid.Ny = ny
self.grid.Nz = nz
self.Nc = nc
print nx, ny, nz, nc
# - grid -> X -#
# x = np.zeros((Nx))
# for i in range(0,Nx):
x = struct.unpack('f' * nx, f.read(4 * nx))
# - grid -> Y -#
y = struct.unpack('f' * ny, f.read(4 * ny))
self.x = x
# - grid -> Z -#
z = struct.unpack('f' * nz, f.read(4 * nz))
self.x = x
self.y = y
self.z = z
# - loop on processors -#
F = np.zeros((nz, ny, nx, nc))
print "nprocs=", nproc
counter = 0
prog = 0.0
if(analize):
return 0
for nprocessor in range(0, nproc):
print "proc = ", nprocessor
# -processor dims -#
i0 = struct.unpack('i', f.read(4))[0]
j0 = struct.unpack('i', f.read(4))[0]
k0 = struct.unpack('i', f.read(4))[0]
li0 = struct.unpack('i', f.read(4))[0]
lj0 = struct.unpack('i', f.read(4))[0]
lk0 = struct.unpack('i', f.read(4))[0]
# print '>>> ',i0,j0,k0,li0,lj0,lk0
NN = li0 * lj0 * lk0 * nc
array = np.array(struct.unpack('f' * NN, f.read(4 * NN))).reshape(lk0, lj0, li0, nc)
for k in range(0, lk0):
for j in range(0, lj0):
for i in range(0, li0):
for c in range(0, nc):
F[k + k0, j + j0, i + i0, c] = array[k, j, i, c]
counter += li0
prog = counter * (100.0 / ntot)
# np.savetxt( nameOutFile ,F[0,:,:,component],fmt='%15.14e')
f.close()
# print "done"
return F
def init_values(self):
self.analize_field(self.filename, analize=True)
if self.grid.Nx > 1:
self.grid.dx = self.x[1] - self.x[0]
else:
self.grid.dx = 0
if self.grid.Ny > 1:
self.grid.dy = self.y[1] - self.y[0]
else:
self.grid.dy = 0
if self.grid.Nz > 1:
self.grid.dz = self.z[1] - self.z[0]
else:
self.grid.dz = 0
self.grid.Lx = self.grid.dx * self.grid.Nx
self.grid.Ly = self.grid.dy * self.grid.Ny
self.grid.Lz = self.grid.dz * self.grid.Nz
self.grid.totNr = self.grid.Nx * self.grid.Ny * self.grid.Nz
def print_parameters(self):
print ("Start Analysis:")
print ("filename = %s" %self.filename)
print ("SIZE: [ Lx, Ly, Lz ] = [ %.3f, %.3f, %.3f ]" % (self.grid.Lx, self.grid.Ly, self.grid.Lz))
print (" Np : [ Nx, Ny, Nz ] = [ %d, %d, %d ]" % (self.grid.Nx, self.grid.Ny, self.grid.Nz))
def set_frequency(self):
self.grid.kx = np.fft.fftfreq(self.grid.Nx,d=self.grid.dx)
self.grid.ky = np.fft.fftfreq(self.grid.Ny,d=self.grid.dy)
self.grid.kx = np.fft.fftshift(self.grid.kx)
self.grid.ky = np.fft.fftshift(self.grid.ky)
#self.grid.kt = np.fft.fftshift(self.grid.kt)
self.grid.Nkx = self.grid.kx.size
self.grid.Nky = self.grid.ky.size
self.grid.totNk = self.grid.Nkx * self.grid.Nky
self.grid.dkx = 1 / self.grid.Lx
self.grid.dky = 1 / self.grid.Ly
self.grid.Lkx = self.grid.dkx * (self.grid.Nx / 2)
self.grid.Lky = self.grid.dky * (self.grid.Ny / 2)
self.grid.kz = 0
self.grid.dkz = 0
self.grid.Lkz = self.grid.dkz * (self.grid.Nz / 2)
def print_frequency(self):
print ("SIZE: [ Lkx, Lky, Lkz ] = [ %6.3f, %6.3f, %6.3f ]" % (self.grid.Lkx, self.grid.Lky, self.grid.Lkz))
print (" dk : [ dkx, dky, dkz ] = [ %6.3f, %6.3f, %6.3f ]" % (self.grid.dkx, self.grid.dky, self.grid.dkz))
def do_fft(self, zposition, comp):
print ("ready for fft3D...")
dataselect = self.alldata[zposition, :, :, comp]
self.trasf3D = np.fft.fftn(dataselect, axes=(0,1))
# self.trasf3D = self.trasf3D/math.sqrt(self.grid.Nx*self.grid.Ny)
self.shiftedTrasf3D = np.fft.fftshift(self.trasf3D, axes=(0,1))
print ("DONE fft3D")
def do_inversefft(self):
print ("ready for ifft3D...")
self.mynewData = np.fft.ifftn(self.trasf3D, axes=(1,2))
self.mynewData = self.mynewData*math.sqrt(self.grid.Nx*self.grid.Ny)
print ("DONE ifft3D")
def saveffttxt(self, varname, kxmin=-40, kxmax=0, kymin=-10, kymax=40):
name = ("%s-2D-fft.txt" % (varname))
print ("ready for printin on %s..." %(name))
ikmin = np.maximum((int)((kxmin - self.grid.kx[0])/self.grid.dkx), 0)
ikmax = np.minimum((int)((kxmax - self.grid.kx[0])/self.grid.dkx), self.grid.Nkx)
jkmin = np.maximum((int)((kymin - self.grid.ky[0])/self.grid.dky), 0)
jkmax = np.minimum((int)((kymax - self.grid.ky[0])/self.grid.dky), self.grid.Nky)
print "self.grid.Nkx = ", self.grid.Nkx, " self.grid.Nky = ", self.grid.Nky
print "ikmin = ", ikmin, " ikmax = ", ikmax, " jkmin = ", jkmin, " jkmax = ", jkmax
f1 = open(name, 'w')
for j in range(jkmin, jkmax):
for i in range(ikmin, ikmax):
f1.write("%.3e, %.3e, %.3e\n" % (self.grid.kx[i], self.grid.ky[j], (np.absolute(self.shiftedTrasf3D[j, i]))**2 ))
# np.savetxt( "kx-omega.txt" ,np.real(self.trasf[:,:,0]),fmt='%15.14e')
f1.close()
print ("DONE for printin on %s..." %(name))
def saveNewData(self, varname):
name = ("%s-x-y.txt" % (varname))
f1 = open(name, 'w')
for j in range(0, self.grid.Ny):
for i in range(0, self.grid.Nx):
f1.write("%.3e, %.3e, %.3e\n" % (self.x[i], self.y[j], (self.mynewData[itime,j, i]) ) )
f1.write("\n")
f1.close()
def getEnergyAtOmega(self, omegain):
ifreqin = int(round(omegain/self.grid.dkt))
print ">>>>>>>ifreqin=" + str(ifreqin) + " omegain=" + str(omegain)
omegaout = self.grid.kt[ifreqin]
print " omegaout=" + str(omegaout)
factor = 8.0/(3.0*self.grid.Nt)*(self.grid.dx * self.grid.dy*2) / (8*math.pi)
energyAtOmega=0
for t in range(0, self.grid.Nkt/2):
if abs(t-ifreqin)<=1:
energyAtOmega += np.tensordot(self.trasf3D[t,:, :],self.trasf3D[t,:, :].conjugate(),axes=2)
if t > 0:
energyAtOmega += np.tensordot(self.trasf3D[self.grid.Nkt-t,:, :],self.trasf3D[self.grid.Nkt-t,:, :].conjugate(),axes=2)
return np.absolute(energyAtOmega)*factor
def createCone(self,nmax,phimin, phimax, nbin):
print ("creating conditions for the cone...")
self.nbin = nbin
self.plotCone = np.zeros(self.nbin)
self.nmax = nmax
self.nmin = 0
self.dkn = nmax*1.0/self.nbin
self.kns = np.arange(0, self.nmax, self.dkn)
self.gradphimin = phimin
self.gradphimax = phimax
self.phimin = (phimin + 90.0)/180.*np.pi
self.phimax = (phimax + 90.0)/180.*np.pi
print "phimin = ", phimin, " phimax = ", phimax, " nmax = ", nmax
print "self.phimin = ", self.phimin, " self.phimax = ", self.phimax, " self.nmax = ", self.nmax
def analiseCone(self):
print ("start analysis of the cone...")
print "self.phimin = ", self.phimin, " self.phimax = ", self.phimax, " self.nmax = ", self.nmax
self.plotCone[:] = 0
for j in range(0, self.grid.Nky):
ky = self.grid.ky[j]
for i in range(0, self.grid.Nkx):
kx = self.grid.kx[i]
phi = np.arctan2(ky, kx)
if self.phimin <= phi <= self.phimax:
kr = math.sqrt(kx*kx + ky*ky)
ikn = int(kr/self.dkn + 0.5)
if ikn < self.nbin:
self.plotCone[ikn] += ((np.absolute(self.shiftedTrasf3D[j, i]))**2)
print ("DONE")
def analiseConeAndPrint(self, varname, kxmin=-40, kxmax=0, kymin=-10, kymax=40):
print ("start analysis of the cone and print new 2D file...")
ikmin = np.maximum((int)((kxmin - self.grid.kx[0])/self.grid.dkx), 0)
ikmax = np.minimum((int)((kxmax - self.grid.kx[0])/self.grid.dkx), self.grid.Nkx)
jkmin = np.maximum((int)((kymin - self.grid.ky[0])/self.grid.dky), 0)
jkmax = np.minimum((int)((kymax - self.grid.ky[0])/self.grid.dky), self.grid.Nky)
print "self.grid.Nkx = ", self.grid.Nkx, " self.grid.Nky = ", self.grid.Nky
print "ikmin = ", ikmin, " ikmax = ", ikmax, " jkmin = ", jkmin, " jkmax = ", jkmax
print "self.phimin = ", self.phimin, " self.phimax = ", self.phimax, " self.nmax = ", self.nmax
name = ("%s-%.1f-%.1f-2DFFT.txt" % (varname, self.gradphimin, self.gradphimax))
f1 = open(name, 'w')
self.plotCone[:] = 0
for j in range(jkmin, jkmax):
ky = self.grid.ky[j]
for i in range(ikmin, ikmax):
kx = self.grid.kx[i]
phi = np.arctan2(ky, kx)
if (phi >= self.phimin) and (phi <= self.phimax):
kr = math.sqrt(kx*kx + ky*ky)
ikn = int(kr/self.dkn + 0.5)
if ikn < self.nbin:
self.plotCone[ikn] += ((np.absolute(self.shiftedTrasf3D[j, i]))**2)
f1.write("%.3e, %.3e, %.3e\n" % (kx, ky, (np.absolute(self.shiftedTrasf3D[j, i]))**2 ) )
else:
f1.write("%.3e, %.3e, %.3e\n" % (kx, ky, 0 ))
print ("DONE")
def printConeAnalysis(self, varname):
print ("print analysis...")
name = ("%s-%.1f-%.1f.txt" % (varname, self.gradphimin, self.gradphimax))
f1 = open(name, 'w')
for i in range(0,self.nbin):
f1.write("%.3e, %.3e\n" % (self.kns[i], self.plotCone[i]) )
print ("done")