/
ExpandCSTProfile.py
251 lines (175 loc) · 5.4 KB
/
ExpandCSTProfile.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
#!/usr/bin/env python
#
# Jeff Kaplan 5/2011
#
from matplotlib import pyplot as mpl
from numpy import*
from dataFunction import *
from dataFuncVsTime import *
from scipy import special as sp
cheb = sp.eval_chebyt
P= sp.legendre
pi = arccos(0.)*2.
from JeffSpec import *
from IntegrateTabulatedData import *
import sys
filename = "D0res800x800.dat"
file = open(filename,'r')
print "First Line:"
print file.readline()
line=file.readline()
ns,nu=line.split()
print "Number of radial points, ns: ", ns
print "Number of mu, i.e. cos(theta) points, nu: ", nu
line=file.readline()
radequat,npoly=line.split()
line=file.readline()
comega,rotfunc=line.split()
print "Radequat (avg radius?): ", radequat
data=[]
rhoEquator=[]
rsEquator=[]
numLines=0
for line in file:
data.append(line.split())
#if mu = 1.0, theta = Pi/2
#print data[numLines][2]
if float(data[numLines][1])==1.0 and float(data[numLines][2]) != 0.0:
rsEquator.append(float(data[numLines][0]))
rhoEquator.append(float(data[numLines][2]))
numLines+=1
print "TotalLines: ", numLines, "\t ns*nu: ", int(ns)*int(nu)
#trim off last entry
rsEquator.pop()
rhoEquator.pop()
rs = array(rsEquator)
rhos = array(rhoEquator)
n = len(rs)
rhoFunc= dataFunction()
rhoFunc.set_numPoints(n)
rhoFunc.setPoints(rs)
rhoFunc.setData(rhos)
#sys.exit()
print rhoFunc.data
expandInLogXspace=0
funcExpanding = log(rhoFunc.data + 1.0e-2)
exmin=rhoFunc.points[0]
exmax=rhoFunc.points[len(rhoFunc.points) -1]
eps = 0.0# rhoFunc.points[1] - rhoFunc.points[0]
print "eps: ", eps
xs =xtointerval(rhoFunc.points,exmin-eps,exmax+eps)
realxs=xtointerval(rhoFunc.points,exmin,exmax)
if expandInLogXspace:
eps = log(rhoFunc.points[1]) - log(rhoFunc.points[0])
xs =xtointerval(log(rhoFunc.points),log(exmin)-eps,log(exmax)+eps)
realxs=xtointerval(log(rhoFunc.points),log(exmin),log(exmax))
fdeos = []
for i in range(len(realxs)-1):
fdeos.append((funcExpanding[i+1]-funcExpanding[i])/(realxs[i+1]-realxs[i]))
fdeos.append(fdeos[len(rhoFunc.points)-2])
#print trapezoidIntegrateFromPoints(xs,funcExpanding)
#### N is number of terms in expansion
N = 56
fks=[]
for i in range(N):
ci = 1.0
if (i == N-1 ):
ci = 2.0
chebi = []
for j in range(len(xs)):
chebi.append(
funcExpanding *cheb(i,xs[j])
)
fks.append(
trapezoidIntegrateFromPoints(xs,
funcExpanding / (1.0-xs*xs)**(1./2.)
* cheb(i,xs) * (ci)/pi
)
)
################### TEST INTEGRATOR #######################
#
# tests integration of function f(x)=x
#
testxs = arange(0.0, 1.0, 1e-2)
testintegrand = []
for i in range(len(testxs)):
portion = testxs[0:i]
testintegrand.append (
trapezoidIntegrateFromPoints(portion, portion)
)
#mpl.plot(testxs,testintegrand-1./2.*testxs*testxs)
#mpl.show()
# done with test integration
#######################
##########################
#Test chebbies
# for i in range(N):
# mpl.plot(xs,cheb(i,xs))
# mpl.show()
#
#############################
def interpFunc(x):
func =funcExpanding
index =index_from_value(x,realxs )
a = realxs[index -1]
b = realxs[index]
fa = func[index-1]
fb = func[index]
value = fa + (fb - fa)/(b-a) * (x -a)
#print x, xs[index]
return value
print interpFunc(.5)
pseudofks= calc_fks(interpFunc,N-1)
print pseudofks
print "COEFFICIENTS:", fks
pseudopoints = matrixTransform(linalg.inv(dft_matrix(N)),pseudofks)
cpoints = -cos(arange(0,N)*pi/(N-1))
dpseudopoints = matrixTransform(deriv_matrix(N),pseudopoints)
dpseudofks = matrixTransform(dft_matrix(N), dpseudopoints)
d2pseudopoints = matrixTransform((deriv_matrix(N)), dpseudopoints)
d2pseudofks = matrixTransform(dft_matrix(N),d2pseudopoints)
## appears det deriv matrix is nearly 0, so inversion is bad
## so we can't integrate by inverting the deriv matrix =(
print "dpseudofks"
print dpseudofks
print
print "d2pseudopoints:"
print d2pseudopoints
ys=[]
ypseudo=[]
dypseudo=[]
d2pseudo = []
fdpseudoys=[0.0]
count = 0
for x in xs:
ys.append(NthPartialSum(x,N, fks))
ypseudo.append(NthPartialSum(x,N, pseudofks))
dypseudo.append(NthPartialSum(x,N, dpseudofks))
d2pseudo.append(NthPartialSum(x,N, d2pseudofks))
if count >= 1.0:
fdpseudoys.append(( ys[count] - ys[count-1] )/ (xs[count] - xs[count-1] ))
count +=1;
mpl.plot(xs, exp(funcExpanding) ,xs,exp(ypseudo))#, xs,ys,cpoints,pseudopoints)
mpl.legend(["data","psedo-interp"])#,"galerk-interp","pseudo-points"])
mpl.grid(True)
mpl.show()
mpl.plot(xs, funcExpanding ,xs,ypseudo)#, xs,ys,cpoints,pseudopoints)
mpl.legend(["data","psedo-interp"])#,"galerk-interp","pseudo-points"])
mpl.grid(True)
mpl.show()
mpl.plot(xs, funcExpanding - ypseudo)#, xs,ys,cpoints,pseudopoints)
mpl.legend(["Diff between data & pseudo-interp"])#,"galerk-interp","pseudo-points"])
mpl.grid(True)
mpl.show()
# mpl.plot(xs, d2pseudo, cpoints, d2pseudopoints )#, xs,ys,cpoints,pseudopoints)
# mpl.legend(["data","d2psedo-interp"])#,"galerk-interp","pseudo-points"])
# #mpl.show()
# #mpl.semilogx(rhoFunc.points,rhoFunc.data)
# mpl.grid(True)
# mpl.show()
mpl.plot(cpoints,dpseudopoints,xs,dypseudo,realxs,fdeos)
mpl.legend(["dpseudo-pts","dpseudo-interp","finite-difference"])
mpl.grid(True)
mpl.show()
mpl.semilogy(range(N),absolute(pseudofks),range(N), absolute(fks))
mpl.show()