source: ETALON/CLIO/CTR+gfw/GFWnew/GFWnew.m @ 383

Last change on this file since 383 was 383, checked in by hodnevuc, 8 years ago
File size: 14.8 KB
Line 
1 function  [nu_Hz,SP_spec]=GFWnew(var)
2% close all; clear all; clc
3%% Configuration of calculation
4%PrSelect == what profile to use: 1 --from file; 2 - assymmetric gaussian
5%P_file -- name and path to file generated with ASTRA (use TakeData.m script on svn=> CLIO/Scripts to take data from ASTRA generated files)
6%PlotFunc:
7%   1 -- Form factor
8%   2 -- Single electron yield from Theta
9%   3 -- dI_N/dOmega (SP spetrum for whole bunch)
10%   4 -- Bunch profile
11%   5 -- dI(nu)/dnu (SP spetrum for whole bunch)
12% var='PrSelect=1; P_file=''../p20.mat''; PlotFunc=[1 1 1 1 1]; ';
13 %% Physical constants
14c=299792458;%m/s speed of ligth
15qe = 1.602176487e-19*c*10;%Electron charge, statColumns
16%% Parameters block (by default)
17T=1;%Transmissivity (from gun exit to experimental position)
18m=1;%Order of radiation
19lgr_mm=180;%Grating length, mm
20wid_mm=40;%Grating width, mm
21el_mm=8;%Grating period, mm
22alpha1=30;%Blaze angle, degrees
23gam=100;%relativistic gamma
24Qbunch_nC=1.2e-9;%Bunch charge, nC (if T=1,than chagre at experiment position, if not, than charge at the exit of gun)
25x0_mm=6; %Position of beam centroid above grating, mm
26fwhm_x_mm=2; %FWHM_X  of Gaussian beam at the centre of grating, mm
27fwhm_y_mm=2; %FWHM_Y  of Gaussian beam at the centre of grating, mm
28pulse_len_ps=2.5;%Nominal bunch duration (FWHM), ps
29BAd_mm=300;% beam - aperture distance ,  mm
30Aed_mm=25;%Aperture entrance diameter,  mm
31%All angles are in radians
32phiStop=atan(Aed_mm/2/BAd_mm);% phi go from 0 to phiStop
33% ThetaStart=pi/2-atan(Aed/2/BAd);
34% ThetaStop=pi/2+atan(Aed/2/BAd);
35ThetaStart=alpha1/180*pi;%initial theta angle
36ThetaStop=pi-pi/10;%final theta angle
37ThetaDiv=200;%steps in theta
38phiDiv=10;%steps in phi
39
40if (~isempty(var))
41disp(var);
42eval(var);
43end %if
44
45%save all parameters
46name=regexprep(var,'.mat','');
47name=regexprep(name,'../','');
48name=regexprep(name,';','');
49save([ name '.mat'])
50%% Profile selection
51if PrSelect==1
52    FFTspec=1;
53  if  exist(P_file,'file')==2;
54    load([P_file]);
55  else
56      disp('Set correct file name!')
57  end
58else
59    FFTspec=1;%how to calculate form factor: 1 -- FFT; 2 -- only fro assymetric gaussian
60%Gaussian
61epsl=0.5;                                                           %Asymmetry factor
62sigma_t= pulse_len_ps/(sqrt(2*log(2))*(1+epsl))*1e-3;  %The sigma of the leading edge    ns
63
64nu=(1:1e+10:3e+13).*1e-9;%Ghz
65
66sigma_L= sigma_t*epsl;
67sigma_H= sigma_t;
68time=(1:(length(nu)*2))*(1/(length(nu)*2*(nu(2)-nu(1))));
69xc=mean(time);
70G1=exp(-(time-xc).^2./(2*sigma_L^2));
71G2=exp(-(time-xc).^2./(2*sigma_H^2));
72G1(time>xc)=0;
73G2(time<xc)=0;
74Profile=(G1+G2)./sum(G1+G2);
75end
76
77%% Calculation block for additional parameters
78 n_e_t=round(Qbunch_nC/1.6e-19*T);              %Total number of electrons in the bunch
79% n_e_t=1e+11;
80SolAng=pi*((Aed_mm/2)^2)/(BAd_mm^2); %solid angle from beam to aperture
81% Grating
82alpha1=alpha1/180*pi;
83el1=el_mm*(cos(alpha1)^2);
84el2=el_mm-el1;
85hi=abs(el1*tan(alpha1));
86alpha2=-(pi/2-alpha1);
87% Beam
88beta=sqrt(gam^2 - 1)/gam;
89sigma_x= fwhm_x_mm/(2*sqrt(2*log(2)));
90sigma_y= fwhm_y_mm/(2*sqrt(2*log(2)));
91% N_e above grating
92lol=x0_mm-5*sigma_x;                                       
93if (lol<hi) lol=1.001*hi; end
94upl=x0_mm+5*sigma_x;
95GAU = @(x) exp(-(x-x0_mm).*(x-x0_mm)/(2*sigma_x*sigma_x));
96res1 = integral(GAU,lol,upl)/(sigma_x*sqrt(2*pi));%part of the beam is above the grating
97disp([num2str(res1) ' of the beam above the grating'])
98n_e=n_e_t*res1;                                                 %Number of electrons above the grating
99
100%% Estimation grating factor R^2, coherent & incoherent integrals
101%%
102%
103% $$W_1 |_{x_0=0}= \sum \frac{q^2\omega^3 L l }{4\pi^2|m|c^3}| n  \times n \times \sum G(...)|^2$$
104%
105% In this block we calculate
106%%
107%
108% $$| n  \times n \times \sum G(...)|^2 $$
109%
110% where other terms (except L - grating length) are in f1, f2, f4. In general:
111%%
112%
113% $$W_N=W_1 |_{x_0=0}(NS_{inc}+N^2S_{coh}) $$
114%
115% where s_coh and s_inc are:
116%%
117%
118% $$S_{inc}=\int_h^{\infty}dxX(x)$$
119%
120% $$S_{coh}=|\int_h^{\infty}dxX(x)\tilde{Y}(ky)\tilde{T}(\omega)|^2 $$
121%
122% for now (in this block)
123%%
124%
125% $$S^*_{inc}=\int_h^{\infty}dxX(x)*| n  \times n \times \sum G(...)|^2\\ $$
126%
127% $$S^*_{coh}=\int_h^{\infty}dxX(x)*| n  \times n \times \sum G(...)|  $$
128%
129par = struct('Theta',NaN,'phi',NaN,'m',m,'alpha1',alpha1,'facet',NaN,'gamma',gam,'x0',x0_mm,'l',el_mm,'w',wid_mm);
130for i=1:ThetaDiv%theta
131thet(i)=(ThetaStart+(ThetaStop-ThetaStart)*(i-1)/(ThetaDiv-1))/pi*180;
132theta=thet(i)*pi/180;
133for ii=1:phiDiv;%phi
134phi=(phiStop/(phiDiv-1))*(ii-1);
135
136for ind=1:21;%x0
137xv=lol+ (ind-1)*0.5*sigma_x;%height above grating
138
139par.phi=phi;
140par.Theta=theta;
141par.x0=xv;
142par.facet=1;
143
144[res1,res2,res3,res4]=QUx(par,50,200);
145        G1x=(res1*tan(alpha1) +1i*res2*tan(alpha1))/(el_mm*pi);
146        G1z=(res1+1i*res2)/(el_mm*pi);
147        G1y=(res3*tan(alpha1)-1i*res4*tan(alpha1))/(el_mm*pi);     
148
149par.facet=2; 
150
151[res1,res2,res3,res4]=QUx(par,50,200);
152        G2x=(res1*tan(alpha2) +1i*res2*tan(alpha2))/(el_mm*pi);
153        G2z=(res1+1i*res2)/(el_mm*pi);
154        G2y=(res3*tan(alpha2)-1i*res4*tan(alpha2))/(el_mm*pi);
155   
156      Gx= G1x+ G2x;
157      Gy= G1y+G2y;
158      Gz= G1z+G2z;
159      G=[Gx Gy Gz];
160     
161nv=complex([ sin(theta)*cos(phi) sin(theta)*sin(phi) cos(theta)]);   %dir. unit vector
162nvp=complex([cos(theta)*cos(phi) cos(theta)*sin(phi) -sin(theta)]);%1st pol. unit vector
163nvv=complex([-sin(phi) cos(phi) 0]);%2nd pol. unit vector
164
165    a1t=(G*nvp')*conj(G*nvp');% square of magn. in first polarisation
166    a2t=(G*nvv')*conj(G*nvv');% square of magn. in second polarisation
167   
168      f5c=a1t+a2t;              %  total
169
170       if (ind==10)%
171       single_elec(i,ii)= real(f5c);
172       single_elec_p1(i,ii)= real(a1t);
173       single_elec_p2(i,ii)= real(a2t) ;   
174       end         %   /*store the single-electron R^2 */ 
175   
176      x0_bin(ind)= xv;
177      inc_bin(ind)= real(f5c)*exp(-(xv-x0_mm)*(xv-x0_mm)/(2*sigma_x*sigma_x));
178      inc_bin_p1(ind)= real(a1t)*exp(-(xv-x0_mm)*(xv-x0_mm)/(2*sigma_x*sigma_x));
179      inc_bin_p2(ind)= real(a2t)*exp(-(xv-x0_mm)*(xv-x0_mm)/(2*sigma_x*sigma_x));
180      coh_bin(ind)= sqrt(real(f5c))*exp(-(xv-x0_mm)*(xv-x0_mm)/(2*sigma_x*sigma_x));
181      coh_bin_p1(ind)= sqrt(real(a1t))*exp(-(xv-x0_mm)*(xv-x0_mm)/(2*sigma_x*sigma_x));
182      coh_bin_p2(ind)= sqrt(real(a2t))*exp(-(xv-x0_mm)*(xv-x0_mm)/(2*sigma_x*sigma_x));
183end %of x0
184%  integral over hight from grating
185toler=1000;
186dist1=(x0_bin(end)-x0_bin(1))/toler;
187dist=x0_bin(1):dist1:x0_bin(end);
188incans=pchip(x0_bin,inc_bin,dist);
189s_inc_x(i,ii)=   trapz(dist,incans)/(sqrt(2*pi)*sigma_x);
190cohans=pchip(x0_bin,coh_bin,dist);
191s_coh_x(i,ii)= ( trapz(dist,cohans)/(sqrt(2*pi)*sigma_x))^2;
192
193incans=pchip(x0_bin,inc_bin_p1,dist);
194s_inc_x_p1(i,ii)=    trapz(dist,incans)/(sqrt(2*pi)*sigma_x);
195cohans=pchip(x0_bin,coh_bin_p1,dist);
196s_coh_x_p1(i,ii)= ( trapz(dist,cohans)/(sqrt(2*pi)*sigma_x))^2;
197
198incans=pchip(x0_bin,inc_bin_p2,dist);
199s_inc_x_p2(i,ii)=   trapz(dist,incans)/(sqrt(2*pi)*sigma_x);
200cohans=pchip(x0_bin,coh_bin_p2,dist);
201s_coh_x_p2(i,ii)= (trapz(dist,cohans)/(sqrt(2*pi)*sigma_x))^2;
202
203end%of phi
204end%of theta
205%% Form factor
206% form factor is calculated numericaly by FFT
207SpectrumFFT=abs(fft(Profile)).^2;%Fourier transform
208SpectrumHalf=SpectrumFFT(1:round(length(SpectrumFFT)/2));%only half of FT spectrum hold usefull information
209Frequency=(1:1:length(SpectrumFFT))./(time(2)-time(1))/length(SpectrumFFT)*1e-3;%THz
210FreqHalf=Frequency(1:round(length(Frequency)/2));%half of frequency range
211SpectrumHalf=SpectrumHalf./max(SpectrumHalf);%normalization of Spectrum
212Nmax=find(FreqHalf>=2.5,1);%2.5 THz is more than enoght
213% Interpolation is applyed to decrease error in calculation (freq step in FFT is to wide for this calculation )
214FreqInter=FreqHalf(1):((FreqHalf(2)-FreqHalf(1))*1e-3):FreqHalf(Nmax);%interpolation diapason
215SpecInter=pchip( FreqHalf(1:Nmax),SpectrumHalf(1:Nmax),FreqInter);%interpolation
216if PlotFunc(1)==1
217figure(1)
218plot(FreqInter,SpecInter,'-k','linew',2)
219hold on
220grid on
221set(gca,'fontsize',16)
222ylabel('F(\nu)')
223xlabel('\nu, [THz]')
224legend('Form factor')
225end
226 %% I_N calculation
227f1=2*pi*qe*qe*1e-7;                                           % in Joules
228f2=100/(el_mm^2);
229f4=(m^2)*(beta./(1-beta*cos(thet*pi/180))).^3;
230phi=(phiStop/(phiDiv-1))*((1:phiDiv)-1);
231%lam_e=lam*beta*gam/(2*pi*sqrt(1+(beta*gam*sin(theta)*sin(phi0))^2));
232lam= el_mm*(1/beta-cos(thet*pi/180))/m;%mm
233
234for i=1:ThetaDiv%theta
235for ii=1:phiDiv;%phi
236ky=(2*pi/lam(i))*sin(thet(i)*pi/180)*sin(phi(ii));
237om=2*pi*0.3/lam(i);                          %c=0.3 mm/ps, hence om is in ps^-1                         
238%calculate longitudinal coherence effects
239%Form factor   
240if FFTspec==1
241 s_coh_long=SpecInter(find(2*pi*FreqInter>=om,1));
242 if isempty(s_coh_long)
243     s_coh_long=0;%ACHTUNG!!!
244     disp('fail to find Form Factro value')
245 end
246else
247    %this work with new versions of MATLAB (2015a for example)
248u1= om*sigma_t;
249u2= om*sigma_t*epsl;
250 f_z_c=(exp(-(u1*u1)/2)+ epsl*exp(-(u2*u2)/2))/(1+epsl);
251 f_z_s=-2/(sqrt(pi)*(1+epsl))*(dawson(u2/sqrt(2))*epsl-dawson(u1/sqrt(2)));
252 s_coh_long=f_z_c.^2+f_z_s.^2;
253end
254%%
255
256Y2=exp(-((ky*sigma_y)^2));       % Ysquare
257
258s_coh_xy= Y2*s_coh_x(i,ii);
259s_coh_xy_p1= Y2*s_coh_x_p1(i,ii);
260s_coh_xy_p2= Y2*s_coh_x_p2(i,ii);
261FFact(i)=s_coh_long;
262bin(i,1)=thet(i);
263bin(i,ii+1)= f1*f2*f4(i)*(n_e*s_inc_x(i,ii)+ n_e*n_e*s_coh_xy*s_coh_long);
264bin_p1(i,ii+1)= f1*f2*f4(i)*(n_e*s_inc_x_p1(i,ii)+ n_e*n_e*s_coh_xy_p1*s_coh_long);
265bin_p2(i,ii+1)= f1*f2*f4(i)*(n_e*s_inc_x_p2(i,ii)+ n_e*n_e*s_coh_xy_p2*s_coh_long);
266bin_single(i,1)= thet(i);                     % store for single electron yield
267bin_single(i,ii+1)= f1*f2*f4(i)*single_elec(i,ii);
268bin_single_p1(i,ii+1)= f1*f2*f4(i)*single_elec_p1(i,ii);
269bin_single_p2(i,ii+1)= f1*f2*f4(i)*single_elec_p2(i,ii);
270end% of ii (phi)           
271end % of i (theta)
272
273if PlotFunc(1)==1
274figure(1)
275hold on
276plot(c./(lam*1e-3)*1e-12,FFact,'or','linew',2)
277legend('Form factor','Used points')
278end
279%%
280% At this point calculation of
281%
282% $$W_N=W_1 |_{x_0=0}(NS_{inc}+N^2S_{coh}) $$
283%
284% is complete. But there is dependence from angles. So next step is
285% integration
286%% Interration and averaging
287philmt=phiDiv+1;%number of steps +1
288%phiDiv + 2 -- average over phi
289for i=1:ThetaDiv%theta
290bin(i,phiDiv+2)=(bin(i,2)+ 2*sum(bin(i,3:philmt)))/(2*(philmt-1)-1);    %    col. 9 is the store for AVERAGE dE over all ACCEPTED phi's; in J/sr/cm     
291bin_p1(i,phiDiv+2)=(bin_p1(i,2)+ 2*sum(bin_p1(i,3:philmt)))/(2*(philmt-1)-1);     %   ditto for pol. 1
292bin_p2(i,phiDiv+2)=(bin_p2(i,2)+ 2*sum(bin_p2(i,3:philmt)))/(2*(philmt-1)-1);     %   and for pol. 2
293bin_single(i,phiDiv+2)= (bin_single(i,2)+ 2*sum(bin_single(i,3:philmt)))/(2*(philmt-1)-1);
294bin_single_p1(i,phiDiv+2)= (bin_single_p1(i,2)+ 2*sum(bin_single_p1(i,3:philmt)))/(2*(philmt-1)-1);
295bin_single_p2(i,phiDiv+2)= (bin_single_p2(i,2)+ 2*sum(bin_single_p2(i,3:philmt)))/(2*(philmt-1)-1);
296end
297
298%phiDiv + 3 -- integral
299% integrate over phi
300d_theta=(ThetaStop-ThetaStart)/(ThetaDiv-1);                             % d_theta expressed in rad
301d_phi= (phiStop/(phiDiv-1));                  %d_phi expressed in rad
302for i=1:ThetaDiv%theta
303
304     theta=(ThetaStart+(ThetaStop-ThetaStart)*(i-1)/(ThetaDiv-1));
305     
306     sum0 = sin(theta)*d_theta*d_phi*sum(bin(i,3:(phiDiv+1)));   
307     sum_p1 =sin(theta)*d_theta*d_phi*sum(bin_p1(i,3:(phiDiv+1)));   %pol. 1 only
308     sum_p2 =sin(theta)*d_theta*d_phi*sum(bin_p2(i,3:(phiDiv+1)));   %and pol. 2
309     sum_single =sin(theta)*d_theta*d_phi*sum(bin_single(i,3:(phiDiv+1)));   
310     sum_single_p1 = sin(theta)*d_theta*d_phi*sum(bin_single_p1(i,3:(phiDiv+1)));   
311     sum_single_p2 =  sin(theta)*d_theta*d_phi*sum(bin_single_p2(i,3:(phiDiv+1)));       
312           
313    bin(i,phiDiv+3)= sin(theta)*d_theta*d_phi*bin(i,2)+ 2*sum0;
314    bin_p1(i,phiDiv+3) = sin(theta)*d_theta*d_phi*bin_p1(i,2)+ 2*sum_p1;%  /* pol. 1 only */
315    bin_p2(i,phiDiv+3) = sin(theta)*d_theta*d_phi*bin_p2(i,2)+ 2*sum_p2; % /*  and pol. 2 */
316    bin_single(i,phiDiv+3) = sin(theta)*d_theta*d_phi*bin_single(i,2) + 2*sum_single;   
317    bin_single_p1(i,phiDiv+3) = sin(theta)*d_theta*d_phi*bin_single_p1(i,2) + 2*sum_single_p1;
318    bin_single_p2(i,phiDiv+3) = sin(theta)*d_theta*d_phi*bin_single_p2(i,2) + 2*sum_single_p2;
319end
320 
321% now integrate over theta
322 sum1=sum(bin(1:ThetaDiv,phiDiv+3));       
323 sum3=sum(bin_p1(1:ThetaDiv,phiDiv+3));   
324 sum4=sum(bin_p2(1:ThetaDiv,phiDiv+3));   
325 sum2=sum(bin_single(1:ThetaDiv,phiDiv+3));   
326
327disp(['Rough estimate of total radiated energy (single electron) over hemisphere =' num2str(sum2*0.1*lgr_mm) ' J']);%/* no need for x2, since +-phi has been taken into account*/
328disp(['Rough estimate of total radiated energy (bunch) over hemisphere =' num2str(sum1*0.1*lgr_mm) ' J']);%/* no need for x2, since +-phi has been taken into account*/
329percent=sum1*0.1*lgr_mm/(n_e_t*1.602177e-19*(gam-1)*0.511*1e+6);
330disp(['This is equal to ' num2str(percent) ' of the energy in the bunch'])
331disp(['Energy in polarization 1= ' num2str(sum3*0.1*lgr_mm) ' J'])
332disp(['Energy in polarization 2= ' num2str(sum4*0.1*lgr_mm) ' J'])
333
334lgr_e_mm = abs(Aed_mm/sin(thet(i)/180*pi));           % variable effective grating length, determined basically by the cone entrance diameter of XXXmm*/
335
336sum_tr = bin(:,phiDiv+2) * 0.1 * lgr_e_mm * SolAng;   % /*variable grating length,  now in J*/
337sum_tr_single = bin_single(:,phiDiv+2) * 0.1 * lgr_e_mm * SolAng;
338sum_tr_p1 = bin_p1(:,phiDiv+2) * 0.1 * lgr_e_mm * SolAng;
339sum_tr_p2 = bin_p2(:,phiDiv+2) * 0.1 * lgr_e_mm * SolAng;
340sum_tr_single_p1 = bin_single_p1(:,phiDiv+2) * 0.1 * lgr_e_mm * SolAng;
341sum_tr_single_p2 = bin_single_p2(:,phiDiv+2) * 0.1 * lgr_e_mm * SolAng;
342 
343% KV. Precise calculation over phi
344phiNew=phi(1):((phi(2)-phi(1))*1e-3):phi(end);
345for i=1:ThetaDiv%theta
346        IntergalPhi=sum(pchip(phi,single_elec(i,:),phiNew))*(phiNew(2)-phiNew(1));
347        I1(i)=f1*f2*f4(i)*IntergalPhi;%interal over phi (0 to phiMax)
348end
349nu_Hz=c./(lam*1e-3);%Hz
350SP_spec=I1*0.1*lgr_e_mm*(el_mm/m*1e-3)*2.*(lam/nu_Hz)*1e+18;%muJ/THz
351
352%% Plots
353if PlotFunc(2)==1
354 figure(2)
355 plot(thet,bin_single(:,phiDiv+2),'k','linew',2)
356 set(gca,'fontsize',16)
357ylabel('dI_1/d\Omega, [J sr^{-1}cm^{-1}]')
358xlabel('\Theta, [deg]')
359title('Singe electron yield')
360 grid on
361end
362
363if PlotFunc(3)==1
364figure(3)
365plot(thet,sum_tr,'r','linew',2)
366 set(gca,'fontsize',16)
367ylabel('dI/sin(\Theta)d\Theta, [J]')
368xlabel('\Theta, [deg]')
369title('SP spectrum')
370 grid on
371end
372
373if PlotFunc(4)==1
374figure(4)
375plot(time,Profile,'r','linew',2)
376 set(gca,'fontsize',16)
377ylabel('Amplitude, [a.u.]')
378xlabel('t, [ns]')
379title('Temporal bunch profile')
380 grid on
381end
382
383if PlotFunc(5)==1
384figure(5)
385semilogy(nu_Hz*1e-12,SP_spec,'r','linew',2)
386 hold on
387%  load CTR.mat
388% semilogy(nu*1e-12,IntergdOm*1e+18,'k','linew',2)
389set(gca,'fontsize',16)
390ylabel('Energy, [\muJ/THz]')
391xlabel('\nu, [THz]')
392title('SP spectrum')
393% legend('SP','CTR')
394 grid on
395xlim([min(nu_Hz) max(nu_Hz)].*1e-12)
396end
397
Note: See TracBrowser for help on using the repository browser.