-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ax-Basic-Constant_Distribution.py
410 lines (293 loc) · 9.55 KB
/
Ax-Basic-Constant_Distribution.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 13 11:17:19 2016
@author: Eric Schmidt
"""
import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft
from scipy.integrate import quad
save_plots = 1
through_contact = 1 # Set to 1 for positrons that passed through matter
fit = 2 # 1 for linear, 2 for quadratic
Ne = 2153
N = 1000
T = 1/10
TT = 3
t = np.linspace(0.0, N*T, N)
tf = np.linspace(0.0,1.0/(2.0*T),N/2)
w = ((6/5)*np.pi)*10**6 # (rad/s) Oscillating frequency
phi_x = -np.pi/8 # (rad) Phase-offset of x-position mean oscillations
phi_sigma = np.pi/2 # (rad) Phase-offset of beam distribution width oscillations
x_mid = .1 # (m) Middle of the acceptance function
x_0 = 0 # (m) Initial beam distribution mean
sigma_0 = 14.5*10**-3 # (m) Initial beam distribution width
D_0 = 80*10**-3 # (m) Maximum physical width of beam
A_x = 2*10**-3 # (m) Initial beam distribution mean oscillation amplitude
A_sigma = 5.5*10**-3 # (m) Initial beam distribution width oscillation amplitude
if through_contact == 1:
if fit == 1:
k_0 = 0.762043 # () Constant term
k_1 = -0.5931 # () Linear term
if fit == 2:
k_0 = 0.760101 # () Constant term
k_1 = -0.609356 # () Linear term
k_2 = 7.97297 # () Quadratic term
else:
if fit == 1:
k_0 = 0.793666 # () Constant term
k_1 = -1.277 # () Linear term
if fit == 2:
k_0 = 0.790605 # () Constant term
k_1 = -1.31183 # () Linear term
k_2 = 13.1419 # () Quadratic term
#phi_x = np.pi
phi_D = 0
#x_mid = 10
#x_0 = 10
#A_x = 2
#D_0 = 3
#A_D = 2
#k_1 = 0.05
def main():
# xlinear(noplot=0)
# sigmalinear(noplot=0)
# combinedLinear()
#
# xquad(noplot=0)
# sigmaquad(noplot=0)
combinedQuad()
def xlinear(noplot):
# Initialize variables
AD = np.zeros(len(t))
x_B = x_0 + A_x*np.cos(w*t + phi_x)
D = None
D = D_0
a = np.zeros(len(t))
ul = x_B + D/2
ll = x_B - D/2
i = 0
while i < len(t):
a[i] = N/(ul[i] - ll[i])
temp1 = quad(integrandADlinear, ll[i], ul[i], args=(a[i],x_B[i]))
AD[i] = temp1[0]
# Nt = quad(integrandN, ll[i], ul[i], args=(a[i],x_B[i],D))
# print(Nt[0])
i = i + 1
title = "Postion vs. Time"
ylabel = "$x_m$"
save_title = "CF_Linear_Position"
stitle = "Constant Distribution: Linear Position"
if noplot==0:
plotSingle(AD,x_B,ylabel,title,save_title,stitle)
return AD
def sigmalinear(noplot):
AD = np.zeros(len(t))
a = np.zeros(len(t))
D = (D_0-A_sigma) + (A_sigma)*np.cos(w*t + phi_D)
x_B = None
x_B = x_0
ul = x_B + D/2
ll = x_B - D/2
i = 0
while i < len(t):
a[i] = N/(ul[i] - ll[i])
temp1 = quad(integrandADlinear, ll[i], ul[i], args=(a[i],x_B))
AD[i] = temp1[0]
# Nt = quad(integrandN, ll[i], ul[i], args=(a[i],x_B,D[i]))
# print(Nt[0])
i = i + 1
title = "Sigma vs. Time"
ylabel = "$\sigma$"
save_title = "CF_Linear_Sigma"
stitle = "Constant Distribution: Linear Sigma"
if noplot==0:
plotSingle(AD,D,ylabel,title,save_title,stitle)
return AD
def combinedLinear():
AD = np.zeros(len(t))
a = np.zeros(len(t))
x_B = x_0 + A_x*np.cos(w*t + phi_x)
D = (D_0-A_sigma) + (A_sigma)*np.cos(w*t + phi_D)
ll = x_B - D/2
ul = x_B + D/2
i = 0
while i < len(t):
a[i] = N/(ul[i] - ll[i])
temp = quad(integrandADlinear, ll[i], ul[i], args=(a[i],x_B[i]))
AD[i] = temp[0]
# Nt = quad(integrandN, ll[i], ul[i], args=(a[i],x_B[i],D[i]))
# print(Nt[0])
i = i + 1
datax = xlinear(1)
datasig = sigmalinear(1)
save_title = "CF_Linear_Combined"
stitle = "Constant Distribution: Linear Combined"
plotCombined(AD, save_title, datax, datasig, stitle)
def xquad(noplot):
AD = np.zeros(len(t))
x_B = x_0 + A_x*np.cos(w*t + phi_x)
D = None
D = D_0
ul = x_B + D/2
ll = x_B - D/2
a = np.zeros(len(t))
i = 0
while i < len(t):
a[i] = N/(ul[i] - ll[i])
temp = quad(integrandADquad, ll[i], ul[i], args=(a[i],x_B[i]))
AD[i] = temp[0]
# return a*k_1*x**2
# return a*((-(x - x_mid)**2 + (x_mid)**2)/((x_mid)**2))
# return (a*(np.sqrt(2*np.pi*sigma**2)**(-1))*np.exp(-((x-x_B)**2)/(2*sigma**2)))*(k_1*x**2)
# Nt = quad(integrandN, ll[i], ul[i], args=(a[i],x_B[i],D))
# print(x_B[i])
# print(ll[i])
# print(ul[i])
# print(a[i])
# print(Nt[0])
# print(D)
# print(AD[i])
i = i + 1
title = "Postion vs. Time"
ylabel = "$x_m$"
save_title = "CF_Quad_Position"
stitle = "Constant Distribution: Quadratic Position"
if noplot==0:
plotSingle(AD,x_B,ylabel,title,save_title,stitle)
return AD
def sigmaquad(noplot):
AD = np.zeros(len(t))
a = np.zeros(len(t))
D = (D_0-A_sigma) + (A_sigma)*np.cos(w*t + phi_D)
x_B = None
x_B = x_0
ul = x_B + D/2
ll = x_B - D/2
i = 0
while i < len(t):
a[i] = N/(ul[i] - ll[i])
temp = quad(integrandADquad, ll[i], ul[i], args=(a[i],x_B))
AD[i] = temp[0]
# Nt = quad(integrandN, ll[i], ul[i], args=(a[i],x_B,D[i]))
# print(x_B)
# print(ll[i])
# print(ul[i])
# print(a[i])
# print(Nt[0])
# print(D[i])
# print(AD[i])
i = i + 1
title = "Sigma vs. Time"
ylabel = "$\sigma$"
save_title = "CF_Quad_Sigma"
stitle = "Constant Distribution: Quadratic Sigma"
if noplot==0:
plotSingle(AD,D,ylabel,title,save_title,stitle)
return AD
def combinedQuad():
AD = np.zeros(len(t))
a = np.zeros(len(t))
x_B = x_0 + A_x*np.cos(w*t + phi_x)
D = (D_0-A_sigma) + (A_sigma)*np.cos(w*t + phi_D)
ll = x_B - D/2
ul = x_B + D/2
i = 0
while i < len(t):
a[i] = N/(ul[i] - ll[i])
temp = quad(integrandADquad, ll[i], ul[i], args=(a[i],x_B[i]))
AD[i] = temp[0]
# Nt = quad(integrandN, ll[i], ul[i], args=(a[i],x_B[i],D[i]))
# print(Nt[0])
i = i + 1
datax = xquad(noplot = 1)
datasig = sigmaquad(noplot = 1)
save_title = "CF_Quad_Combined"
stitle = "Constant Distribution: Quadratic Combined"
plotCombined(AD,save_title,datax,datasig,stitle)
#def integranda(x, x_B, D):
# return 1
def integrandN(x, a, x_B, D):
return a
def integrandADlinear(x, a, x_B):
# return a*x_B*k_0*x
return a*(k_0 + k_1*x)
def integrandADquad(x, a, x_B):
# return a*x_B*k_1*(x-x_mid)**2
return a*(k_0 + k_1*x + k_2*x**2*100)
def plotSingle(AD,data,ylabel,title,save_title,stitle):
n = 1
fig = plt.figure(n)
st = fig.suptitle("%s"%stitle, fontsize="x-large")
n = n + 1
plot1 = plt.subplot(2,1,1)
plt.plot(t,data)
plt.ylabel("%s"%(ylabel))
plt.title("%s"%(title))
plot2 = plt.subplot(2,1,2)
plt.plot(t,AD)
plt.ylabel("$N_D$")
plt.xlabel("Time ($\mu$s)")
plt.title("Particles Detected vs. Time")
plt.tight_layout()
plot1.locator_params(axis='y',nbins=5)
plot2.locator_params(axis='y',nbins=5)
st.set_y(1.0)
fig.subplots_adjust(top=0.85)
if save_plots == 1:
plt.savefig('Plots/%s.png'%(save_title), bbox_inches='tight', dpi=300)
fig = plt.figure(n)
st = fig.suptitle("%s"%stitle, fontsize="x-large")
n = n + 1
plt.subplot(1,1,1)
Af = fft(AD)
plt.plot(tf[1:], 2/N * np.abs(Af[:N/2])[1:])
plt.xlim(0,2)
plt.xlabel("Frequency (MHz)")
plt.title("DFT of Particles Detected")
st.set_y(1.0)
fig.subplots_adjust(top=0.85)
if save_plots == 1:
plt.savefig('Plots/%s-DFT.png'%(save_title), bbox_inches='tight',
dpi=300)
def plotCombined(AD,save_title,datax,datasig,stitle):
n = 1
fig = plt.figure(n)
st = fig.suptitle("%s"%stitle, fontsize="x-large")
n = n + 1
plot1 = plt.subplot(3,1,1)
plt.plot(t,datax)
plt.ylabel("$N_D$")
plt.title("Particles Detected from changing $x_m$")
plot2 = plt.subplot(3,1,2)
plt.plot(t,datasig)
plt.ylabel("$N_D$")
plt.title("Particles Detected from changing $\sigma$")
plot3 = plt.subplot(3,1,3)
plt.plot(t,AD)
plt.ylabel("$N_D$")
plt.xlabel("Time ($\mu$s)")
plt.title("Particles Detected vs. Time")
plt.tight_layout()
plot1.locator_params(axis='y',nbins=5)
plot2.locator_params(axis='y',nbins=5)
plot3.locator_params(axis='y',nbins=5)
st.set_y(1.0)
fig.subplots_adjust(top=0.85)
if save_plots == 1:
plt.savefig('Plots/%s.png'%(save_title), bbox_inches='tight', dpi=300)
fig = plt.figure(n)
st = fig.suptitle("%s"%stitle, fontsize="x-large")
n = n + 1
plt.subplot(1,1,1)
Af = fft(AD)
plt.plot(tf[1:], 2/N * np.abs(Af[:N/2])[1:])
plt.xlim(0,2)
# plt.ylim(0,2)
plt.xlabel("Frequency (MHz)")
plt.title("DFT of Particles Detected")
st.set_y(1.0)
fig.subplots_adjust(top=0.85)
if save_plots == 1:
plt.savefig('Plots/%s-DFT.png'%(save_title), bbox_inches='tight', dpi=300)
main()