Peano
Loading...
Searching...
No Matches
ModeCalc.py
Go to the documentation of this file.
1import numpy as np
2import matplotlib.pyplot as plt
3from scipy import io
4import os
5import time
6import glob
7
8pi=np.pi
9
10def arctan2_2pi(yy,xx):
11 tem=np.arctan2(yy,xx)
12 for i in range(len(tem)):
13 if tem[i]<0: tem[i]+=2*np.pi
14 return tem
15
16def sph_design(func, t=5, author='Hardin', **kwargs):
17 if author == 'WomersleySym':
18 if not glob.glob('PointDistFiles/sphdesigns/WomersleySym/ss%03d.*'%t):
19 return (float('nan'),float('nan'))
20 coord = np.loadtxt(glob.glob('PointDistFiles/sphdesigns/WomersleySym/ss%03d.*'%t)[0])
21 elif author == 'WomersleyNonSym':
22 if not glob.glob('PointDistFiles/sphdesigns/WomersleyNonSym/sf%03d.*'%t):
23 return (float('nan'),float('nan'))
24 coord = np.loadtxt(glob.glob('PointDistFiles/sphdesigns/WomersleyNonSym/sf%03d.*'%t)[0])
25 else:
26 if not glob.glob('PointDistFiles/sphdesigns/HardinSloane/hs%03.*.txt'%t):
27 return (float('nan'),float('nan'))
28 coord = np.loadtxt(sorted(glob.glob('PointDistFiles/sphdesigns/HardinSloane/hs%03d.txt'%t))[0])
29 xx = coord[:,0]; yy = coord[:,1]; zz = coord[:,2]
30 theta = np.arccos(zz)
31 phi = arctan2_2pi(yy,xx)
32 N = len(theta)
33 I = 0
34 for (i,x) in enumerate(phi):
35 I += func(theta[i],phi[i],**kwargs)
36 I = 4*np.pi*I/N # Normalisation
37 return (I,N)
38
39def gauss_legendre(func, N=40, x_low=0, x_up=pi, **kwargs):
40 (x_sample, w) = np.polynomial.legendre.leggauss(N)
41 # Transform to [x_low,x_up]
42 x_sample = (x_up-x_low)*x_sample/2 + (x_up + x_low)/2
43 I = 0
44 for (i,x) in enumerate(x_sample):
45 I += w[i]*func(x, **kwargs)
46 I = (x_up-x_low)/2 * I
47 return I
48
49
50def trapezoidal(func, N=40, x_low=0, x_up=pi, **kwargs):
51 x_sample = np.linspace(x_low,x_up,N)
52 f = np.zeros(N)
53 for (i,x) in enumerate(x_sample):
54 f[i] = func(x, **kwargs)
55 I = np.trapz(f,x_sample)
56 return I
57
58
59def prod_quad(func, N=20, M=40, **kwargs):
60
61 def func_sphere(phi,theta,**kwargs): return func(phi,theta,**kwargs)*np.sin(phi)
62
63 I = trapezoidal(lambda x: gauss_legendre(func_sphere, N=N,x_low=0,x_up=pi,theta=x,**kwargs),
64 N=M,x_low=0,x_up=2*pi)
65
66 return I/(4*pi)
67
68def file_len(fname):
69 with open(fname) as f:
70 for i, l in enumerate(f):
71 pass
72 return i + 1
73
74
77scheme="t-design"
78
79
80if scheme=="Gauss_Legendre":
81 file_name="zz.csv"
82elif scheme=="t-design":
83 file_name="t-de.csv"
84
85f=open(file_name)
86print("Scheme: "+scheme+" datafile: "+file_name)
87
88#skip the description line
89dat=f.readlines()[1:]
90
91#first find out the number of timesteps
92tstep=0
93told=1e6
94for line in dat:
95 tem=list(map(float,line.split(', ')))
96 tnew=tem[0]
97 if (told-tnew)<0:
98 break
99 else:
100 tstep+=1
101 told=tnew
102
103print("timesteps: "+str(tstep))
104
105#second, find out how many tracer recorded here.
106N_tracer=0
107ID1=-100; ID2=-100
108coors=[]
109for line in dat:
110 tem=list(map(float,line.split(', ')))
111 if (not tem[1]==ID1) or (not tem[2]==ID2) and (not tem[6]==0):
112 ID1=tem[1]; ID2=tem[2]
113 N_tracer+=1
114 coor=[tem[3],tem[4],tem[5]]
115 coors.append(coor)
116
117coors=np.array(coors)
118print("valid tracers: "+str(len(coors)))
119
120#create the real data set
121data_set=np.zeros((N_tracer,tstep,7))
122
123ID1=0; ID2=1
124t_count=0 #counting the tsteps
125N_count=0 #counting the tracer
126for line in dat:
127 tem=list(map(float,line.split(', ')))
128 if (tem[1]==ID1) and (tem[2]==ID2):
129 data_set[N_count][t_count][0]=tem[0]; #timestep
130 data_set[N_count][t_count][1]=tem[3]; data_set[N_count][t_count][2]=tem[4]; data_set[N_count][t_count][3]=tem[5] #coordinates
131 data_set[N_count][t_count][4]=tem[6]; data_set[N_count][t_count][5]=tem[7] #psi4 Re and Im
132 t_count+=1
133 else:
134 ID1=tem[1]; ID2=tem[2]
135 N_count+=1
136 t_count=0
137 data_set[N_count][t_count][0]=tem[0]; #timestep
138 data_set[N_count][t_count][1]=tem[3]; data_set[N_count][t_count][2]=tem[4]; data_set[N_count][t_count][3]=tem[5] #coordinates
139 data_set[N_count][t_count][4]=tem[6]; data_set[N_count][t_count][5]=tem[7] #psi4 Re and Im
140 t_count+=1
141
142
144
145if scheme=="Gauss_Legendre":
146 (thetas,ws)= np.polynomial.legendre.leggauss(20)
147 zs=np.cos(np.pi*thetas/2 + np.pi/2)*0.4
148 zip_iterator=zip(zs,ws)
149 w_dict=dict(zip_iterator)
150
151 #print(w_dict)
152
153 for n in range(N_tracer):
154 for z in zs:
155 if abs(data_set[n][0][3]-z)<1e-4:
156 data_set[n,:,6]=w_dict[z] #weight
157
158 #for t in range(tstep) : print(any(data_set[:,t,6]==0))
159 #print(data_set[80])
160elif scheme=="t-design":
161 for n in range(N_tracer):
162 data_set[n,:,6]=1
163 #for t in range(tstep) : print(any(data_set[:,t,6]==0))
164
165
166
169l_mode=2; m_mode=2;
170for t in range(tstep):
171 for n in range(N_tracer):
172 x=data_set[n][t][1]; y=data_set[n][t][2]; z=data_set[n][t][3];
173 p4re=data_set[n][t][4]; p4im=data_set[n][t][5]
174 #we calculate the triangle function directly to reduce numerical error
175 sintheta=(x**2+y**2)**0.5/(x**2+y**2+z**2)**0.5; costheta=z/(x**2+y**2+z**2)**0.5;
176 if x**2+y**2==0: x+=1e-6;
177 sinphi=y/(x**2+y**2)**0.5; cosphi=x/(x**2+y**2)**0.5
178 cos2phi=2*cosphi**2-1; sin2phi=2*sinphi*cosphi
179 #mode 22
180 if l_mode==2 and m_mode==2:
181 data_set[n][t][4]=(p4re*cos2phi-p4im*sin2phi)*(1+costheta)**2*(5/64/np.pi)**0.5
182 data_set[n][t][5]=(p4re*sin2phi-p4im*cos2phi)*(1+costheta)**2*(5/64/np.pi)**0.5
183 #data_set[n][t][4]=p4re*4*sintheta
184 if scheme=="Gauss_Legendre":
185 data_set[n][t][4]*=sintheta;
186 data_set[n][t][5]*=sintheta;
187
188
190ModeRe=np.zeros(tstep)
191ModeIm=np.zeros(tstep)
192if scheme=="Gauss_Legendre":
193 for t in range(tstep):
194 for n in range(N_tracer):
195 w=(1.0/40)*(2*np.pi)*data_set[n][t][6]*(np.pi/2.0)
196 x=data_set[n][t][1]; y=data_set[n][t][2]; z=data_set[n][t][3];
197 ModeRe[t]+=w*data_set[n][t][4]#notice we do not need to do this when we add things above
198 #if t==0 :
199 #print(data_set[n][t][4])
200 ModeIm[t]+=w*data_set[n][t][5]
201elif scheme=="t-design":
202 for t in range(tstep):
203 for n in range(N_tracer):
204 w=(1.0/N_tracer)*(4*np.pi)
205 ModeRe[t]+=w*data_set[n][t][4] #notice we do not need to do this when we add things above
206 #if t==0 :
207 #print(data_set[n][t][4])
208 ModeIm[t]+=w*data_set[n][t][5]
209
210#plt.plot(data_set[0,:,0],ModeRe[:]/4/np.pi)
211#plt.show()
212print(ModeRe/4/np.pi)
213print(ModeIm/4/np.pi)
214
215
216
217
218
219
220'''
221z=coors[:,2]
222zz=list(set(z))
223zzz=[]
224print("z coors number: "+str(len(zz))+" "+str(zz))
225for z3 in zz:
226 if len(z[z==z3])<40:
227 print(z3,len(z[z==z3]))
228 zzz.append(z3)
229
230
231plt.figure(figsize=(10,10),dpi=80)
232for p in coors:
233 if p[2] in zzz:
234 plt.plot(p[0],p[1],'o',color='blue')
235plt.show()
236'''
237'''
238I_exa=np.pi**2
239#print(I_exa)
240tnum=51
241t_list=np.array(range(tnum))
242I_num=np.zeros(tnum)
243N_num=np.zeros(tnum)
244
245def f(theta,phi):
246 return np.sin(theta)
247 #return 1
248
249#(I,N)=sph_design(f,21,author='WomersleySym')
250
251#print(I-I_exa,N)
252
253for (i,t) in enumerate(t_list):
254 (I_num[i],N_num[i])=sph_design(f,t,author='WomersleySym')
255
256I_num=I_num[~np.isnan(N_num)]
257N_num=N_num[~np.isnan(N_num)]
258#print(arctan2_2pi([1,1,-1,-1],[1,-1,-1,1])/np.pi)
259
260#print(I_num-I_exa)
261#print(N_num)
262#plt.plot(N_num,abs(I_num-I_exa)/I_exa)
263#plt.yscale("log")
264#plt.show()
265
266print(prod_quad(f,10,20)*4*pi-I_exa)
267'''
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
trapezoidal(func, N=40, x_low=0, x_up=pi, **kwargs)
Gauss 1D integration.
Definition ModeCalc.py:50
arctan2_2pi(yy, xx)
Definition ModeCalc.py:10
file_len(fname)
Definition ModeCalc.py:68
gauss_legendre(func, N=40, x_low=0, x_up=pi, **kwargs)
Gauss-Legendre 1D integration.
Definition ModeCalc.py:39
sph_design(func, t=5, author='Hardin', **kwargs)
Definition ModeCalc.py:16
prod_quad(func, N=20, M=40, **kwargs)
Gaussian product quadrature using Trapezoidal for azimuthal direction and Gauss-Legendre for polar an...
Definition ModeCalc.py:59