- Timestamp:
- Apr 18, 2016, 12:36:31 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
CLIO/CTR+gfw/GFWnew/GFWnew.m
r471 r490 1 function [nu_Hz,SP_spec_1,SP_spec_N,lam,E_total,sum_tr,thet]=GFWnew(var) 2 3 % single_elec 4 % close all; clear all; clc 1 %SP space charge model, spectrum calculation 2 % close all; clear all; clc 3 % var='PrSelect=1; P_file=''../p20.mat''; PlotFunc=[1 1 1 1 1]; '; 4 function [nu_Hz,SP_spec_1,SP_spec_N,lam,E_total,sum_tr,thet]=GFWnew(var) 5 %% Output parameters 6 %nu_Hz -- frequency in Hz 7 %SP_spec_1 -- single electron yield from frequency, J/THz 8 %SP_spec_N -- whole spectrum from frequency, J/THz 9 %lam -- wavelength in mm 10 %E_total -- total energy emitted, J 11 %sum_tr -- spectrum from Theta, J 12 %thet -- observation angle, Thetadegrees 5 13 %% Configuration of calculation 6 14 %PrSelect == what profile to use: 1 --from file; 2 - assymmetric gaussian … … 12 20 % 4 -- Bunch profile 13 21 % 5 -- dI(nu)/dnu (SP spetrum for signle elctron bunch) 14 % var='PrSelect=1; P_file=''../p20.mat''; PlotFunc=[1 1 1 1 1]; ';15 22 PrSelect=2; 16 23 PlotFunc=[0 0 0 0 0]; … … 46 53 ThetaDiv=10;%steps in theta 47 54 phiDiv=3;%steps in phi 48 55 saveflag=0;%set to 1 if you want to save model parameters 49 56 if (~isempty(var)) 50 strread(var,'%s','delimiter',' 57 strread(var,'%s','delimiter',';') 51 58 eval(var); 52 59 end %if 53 60 54 61 %save all parameters 62 if (saveflag) 55 63 name=regexprep(var,'.mat',''); 56 64 name=regexprep(name,'[../''[]]',''); 57 65 name=regexprep(name,'[; ]','_'); 58 % save([ name 'SP.mat']) 59 %% Profile selection 60 if PrSelect==1 61 FFTspec=1; 62 if exist(P_file,'file')==2; 63 load([P_file]); 64 else 65 disp('Set correct file name!') 66 end 67 else 68 FFTspec=1;%how to calculate form factor: 1 -- FFT; 2 -- only fro assymetric gaussian 69 %Gaussian 70 sigma_t= pulse_len_ps/(sqrt(2*log(2))*(1+epsl))*1e-3; %The sigma of the leading edge ns 71 72 nu=(1:1e+10:3e+13).*1e-9;%Ghz 73 74 sigma_L= sigma_t*epsl; 75 sigma_H= sigma_t; 76 time=(1:(length(nu)*2))*(1/(length(nu)*2*(nu(2)-nu(1)))); 77 xc=mean(time); 78 G1=exp(-(time-xc).^2./(2*sigma_L^2)); 79 G2=exp(-(time-xc).^2./(2*sigma_H^2)); 80 G1(time>xc)=0; 81 G2(time<xc)=0; 82 Profile=(G1+G2)./sum(G1+G2); 66 save([ name 'SP.mat']) 83 67 end 84 68 … … 106 90 disp([num2str(res1) ' of the beam above the grating']) 107 91 n_e=n_e_t*res1; %Number of electrons above the grating 108 % save([ name '_additional_SP.mat']) 92 numax=c/(el_mm*(1/beta-cos(ThetaStart))/m*1e-3);%max freq in Hz 93 94 if (saveflag) 95 save([ name '_additional_SP.mat']) 96 end 97 %% Profile selection 98 if PrSelect==1 99 FFTspec=1; 100 if exist(P_file,'file')==2; 101 load([P_file]); 102 else 103 error('Set correct file name!') 104 end 105 else 106 FFTspec=1;%how to calculate form factor: 1 -- FFT; 2 -- only fro assymetric gaussian 107 %Gaussian 108 sigma_t= pulse_len_ps/(sqrt(2*log(2))*(1+epsl))*1e-3; %The sigma of the leading edge ns 109 nu=(1:1e+10:(2*numax)).*1e-9;%Ghz 110 111 sigma_L= sigma_t*epsl; 112 sigma_H= sigma_t; 113 time=(1:(length(nu)*2))*(1/(length(nu)*2*(nu(2)-nu(1)))); 114 xc=mean(time); 115 G1=exp(-(time-xc).^2./(2*sigma_L^2)); 116 G2=exp(-(time-xc).^2./(2*sigma_H^2)); 117 G1(time>xc)=0; 118 G2(time<xc)=0; 119 Profile=(G1+G2)./sum(G1+G2); 120 end 121 if isnan(sum(Profile)) 122 error('Increase pulselength or decrease grating pitch') 123 end 124 %% Form factor 125 % form factor is calculated numericaly by FFT 126 SpectrumFFT=abs(fft(Profile)).^2;%Fourier transform 127 SpectrumHalf=SpectrumFFT(1:round(length(SpectrumFFT)/2));%only half of FT spectrum hold usefull information 128 Frequency=(0:(length(SpectrumFFT)-1))./(time(2)-time(1))/length(SpectrumFFT)*1e-3;%THz 129 FreqHalf=Frequency(1:round(length(Frequency)/2));%half of frequency range 130 SpectrumHalf=SpectrumHalf./max(SpectrumHalf);%normalization of Spectrum 131 Nmax=find(FreqHalf>=numax*1e-12,1);%xxx THz is more than enoght 132 % Interpolation is applyed to decrease error in calculation (freq step in FFT is to wide for this calculation ) 133 FreqInter=FreqHalf(1):((FreqHalf(2)-FreqHalf(1))*1e-3):FreqHalf(Nmax);%interpolation diapason 134 SpecInter=pchip( FreqHalf(1:Nmax),SpectrumHalf(1:Nmax),FreqInter);%interpolation 135 if PlotFunc(1)==1 136 figure(1) 137 plot(FreqInter,SpecInter,'-k','linew',2) 138 hold on 139 grid on 140 set(gca,'fontsize',16) 141 ylabel('F(\nu)') 142 xlabel('\nu, [THz]') 143 legend('Form factor') 144 end 109 145 %% Estimation grating factor R^2, coherent & incoherent integrals 110 146 %% … … 213 249 end%of phi 214 250 end%of theta 215 %% Form factor 216 % form factor is calculated numericaly by FFT 217 SpectrumFFT=abs(fft(Profile)).^2;%Fourier transform 218 SpectrumHalf=SpectrumFFT(1:round(length(SpectrumFFT)/2));%only half of FT spectrum hold usefull information 219 Frequency=(0:(length(SpectrumFFT)-1))./(time(2)-time(1))/length(SpectrumFFT)*1e-3;%THz 220 FreqHalf=Frequency(1:round(length(Frequency)/2));%half of frequency range 221 SpectrumHalf=SpectrumHalf./max(SpectrumHalf);%normalization of Spectrum 222 Nmax=find(FreqHalf>=2.5,1);%2.5 THz is more than enoght 223 % Interpolation is applyed to decrease error in calculation (freq step in FFT is to wide for this calculation ) 224 FreqInter=FreqHalf(1):((FreqHalf(2)-FreqHalf(1))*1e-3):FreqHalf(Nmax);%interpolation diapason 225 SpecInter=pchip( FreqHalf(1:Nmax),SpectrumHalf(1:Nmax),FreqInter);%interpolation 226 if PlotFunc(1)==1 227 figure(1) 228 plot(FreqInter,SpecInter,'-k','linew',2) 229 hold on 230 grid on 231 set(gca,'fontsize',16) 232 ylabel('F(\nu)') 233 xlabel('\nu, [THz]') 234 legend('Form factor') 235 end 251 236 252 %% I_N calculation 237 253 f1=2*pi*qe*qe*1e-7; % in Joules
Note: See TracChangeset
for help on using the changeset viewer.