0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 load ../../../LATTICES/perfmach78.mat
0018 arc=THERING;
0019 multbend=0;
0020 nameG4bllat='ESRFlat';
0021
0022 sector=1;
0023
0024
0025 format longe
0026
0027 StepLength=10;
0028
0029 Nevents=1;
0030 disp(['writing G4BL file to simulate: ' num2str(Nevents) ' e-'])
0031 det=0;
0032 SR=0;
0033 RINGFLAG=1;
0034 fringON=0;
0035
0036 TotLength=findspos(arc,length(arc)+1);
0037
0038 beamstart=0;
0039
0040
0041
0042
0043 sX=1*1e-3;
0044 sY=1*1e-3;
0045 sXp=0*1e-6;
0046 sYp=0*1e-6;
0047
0048 killval=1;
0049
0050
0051 P_0=sqrt((E0/1e9)^2-(0.511/1e6)^2);
0052 particle='e-';
0053
0054 sxt=1;
0055 quadH=-1;
0056
0057 Brho=3.3356409519815205*P_0;
0058
0059
0060 bigmondo=[...
0061 ...
0062 ];
0063
0064 ShynRadstarter=[
0065 '#### no net displacement beam generator for radiation.\n'...
0066 'genericbend B1 fieldLength=2 fieldWidth=300 fieldHeight=300 ironLength=2 ironWidth=330 ironHeight=330 fringe=0 By=0.1 ironColor=0.5,.0,.0 kill=1 \n'...
0067 'genericbend B2 fieldLength=1 fieldWidth=300 fieldHeight=300 ironLength=1 ironWidth=330 ironHeight=330 fringe=0 By=-0.1 ironColor=1,.0,.0 kill=1 \n'...
0068 'genericbend B3 fieldLength=1 fieldWidth=300 fieldHeight=300 ironLength=1 ironWidth=330 ironHeight=330 fringe=0 By=-0.1 ironColor=0.5,.0,.0 kill=1 \n'...
0069 'genericbend B4 fieldLength=1 fieldWidth=300 fieldHeight=300 ironLength=1 ironWidth=330 ironHeight=330 fringe=0 By=0.1 ironColor=1,.0,.0 kill=1\n'...
0070 '\n'...
0071 'place B1 z=0\n'...
0072 'place B2 z=1.5\n'...
0073 'place B3 z=2.5\n'...
0074 'place B4 z=3.5\n'...
0075 ];
0076
0077
0078 posbeam=[ ' beam gaussian particle=' particle ...
0079 ' beamZ=' num2str(beamstart) ...
0080 ' beamX=' num2str(+0*1.532845188e-07,15) ...
0081 ' beamXp=' num2str(+0*1.683548689e-07,15) ...
0082 ' rotation=Y' num2str(0*180)...
0083 ' nEvents=' num2str(Nevents) ' '...
0084 ' sigmaX=' num2str(sX,15) ...
0085 ' sigmaY=' num2str(sY,15)...
0086 ' sigmaXp=' num2str(sXp,15)...
0087 ' sigmaYp=' num2str(sYp,15) ' '...
0088 ' meanMomentum=' num2str(P_0*1000,15) ' sigmaP=0'...
0089 ' meanT=0 sigmaT=0 \n'...
0090 'trackcolor reference=1,1,1 \n'...
0091 ShynRadstarter...
0092 ];
0093
0094 head=['physics QGSP synchrotronRadiation=' num2str(SR) '\n'...
0095 'start x=0 y=0 z=' num2str(beamstart) ' radiusCut=0.001 ring=' num2str(RINGFLAG) '\n'...
0096 'reference particle=' particle ' referenceMomentum=' num2str(P_0*1000,15) ' noEloss=1'...
0097 ' beamXp=0 beamYp=0 '...
0098 ' tuneMomentum=' num2str(P_0,15) '\n'...
0099 'box BeamVis width=200.0 height=200.0 length=0.1 material=Vacuum color=1,0,0 kill=1 \n'...
0100 'trace nTrace=' num2str(2*Nevents) ' oneNTuple=1 format=root \n'...
0101 ' param eventTimeLimit=60*8\n'...
0102 ' trackcuts keep=e+,e-,gamma maxTime=1000000*10\n' ...
0103 ];
0104
0105
0106
0107 defQ= ['virtualdetector Det radius=10.0 length=' num2str(2*StepLength)...
0108 ' color=0,.2,.0 referenceParticle=1 format=rootExtended\n'];
0109 defS='';
0110 defB='';
0111 ord=1;
0112 plac={};
0113 defined={};
0114 d=1;
0115
0116
0117 quadr=unique([findcells(arc,'Class','Quadrupole') findcells(arc,'BetaCode','QP') ]);
0118 sextu=unique([findcells(arc,'Class','Sextupole') findcells(arc,'BetaCode','SX') ]);
0119 dipol=unique([findcells(arc,'Class','Dipole') findcells(arc,'BetaCode','DI') ]);
0120 octup=unique([findcells(arc,'Class','Octupole') findcells(arc,'BetaCode','OC') ]);
0121
0122 for i=1:length(quadr)
0123
0124
0125
0126 S=findspos(arc,quadr(i))+arc{quadr(i)}.('Length')/2;
0127 N=arc{quadr(i)}.('FamName');
0128 L=arc{quadr(i)}.('Length');
0129 K=arc{quadr(i)}.('PolynomB')(2);
0130
0131 if sum(strcmp(N,defined))==0;
0132 inrad=60;
0133 outrad=70;
0134
0135
0136 defQ=[ defQ ...
0137 ' genericquad ' N ' '...
0138 ' fieldLength=' num2str(L*1000,15) ...
0139 ' apertureRadius=' num2str(inrad,15) ' '...
0140 ' ironRadius=' num2str(outrad,15) ' '...
0141 ' ironLength=' num2str(L*1000,15) ' '...
0142 ' ironColor=0/255,191/255,255/255 kill=' num2str(killval) ' '...
0143 ' fringe=' num2str(0*fringON) ...
0144 ' gradient=' num2str(quadH*K*Brho,15) ' '...
0145 ' maxStep=' num2str(StepLength/2) ...
0146 '\n'];
0147
0148
0149 defined{d}=N;
0150 d=d+1;
0151 end
0152
0153 if (ord/2-floor(ord/2))==(det-1)
0154 plac{ord}=['place ' N ' parent= z=' num2str(S*1000,15)...
0155 ' coordinates=centerline'...
0156 '\n'...
0157 'place ' 'Det' ...
0158 ' rename=DET# '...
0159 ' parent= z=' num2str((S)*1000)...
0160 ' coordinates=centerline '...
0161 '\n'];
0162 else
0163 plac{ord}=['place ' N ' parent= z=' num2str(S*1000,15)...
0164 ' coordinates=centerline'...
0165 '\n'];
0166 end
0167
0168 ord=ord+1;
0169 end
0170
0171 for i=1:length(sextu)
0172
0173
0174 S=findspos(arc,sextu(i))+arc{sextu(i)}.('Length')/2;
0175 N=arc{sextu(i)}.('FamName');
0176 L=arc{sextu(i)}.('Length');
0177
0178 K=2*arc{sextu(i)}.('PolynomB')(3);
0179
0180 if sum(strcmp(N,defined))==0;
0181 inrad=60;
0182 outrad=70;
0183
0184
0185 defS=[ defS ...
0186 ' multipole ' N ' '...
0187 ' fieldLength=' num2str(L*1000-1e6,15) ...
0188 ' apertureRadius=' num2str(inrad,15) ' '...
0189 ' ironRadius=' num2str(outrad,15) ' '...
0190 ' ironLength=' num2str(L*1000,15) ' '...
0191 ' ironColor=173/255,255/255,47/255 kill=' num2str(killval) ' '...
0192 ' fringe=' num2str(0*fringON) ...
0193 ' maxStep=' num2str(StepLength/2) ...
0194 ' sextupole=' num2str(sxt*K*Brho,15) ' '...
0195 '\n'];
0196
0197 defined{d}=N;
0198 d=d+1;
0199 end
0200
0201
0202 plac{ord}=['place ' N ' parent= z=' num2str(S*1000,15)...
0203 ' coordinates=centerline '...
0204 '\n'];
0205 ord=ord+1;
0206
0207 end
0208
0209 totA=0;
0210 for i=1:length(dipol)
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221 S=findspos(arc,dipol(i))+arc{dipol(i)}.('Length')/2;
0222 N=arc{dipol(i)}.('FamName');
0223 L=arc{dipol(i)}.('Length');
0224
0225 A=arc{dipol(i)}.('BendingAngle');
0226 K=quadH*arc{dipol(i)}.('PolynomB')(2);
0227
0228 tmpmultibend=multbend;
0229 if K==0
0230 multbend=0;
0231 end
0232
0233 if multbend==0
0234 split=1;
0235 else
0236 split=1;
0237 end
0238 L=L/split;
0239 A=A/split;
0240 K=K/split;
0241 totA=totA+A;
0242
0243 if sum(strcmp(N,defined))==0
0244 Fw=240*1.0;
0245 Fh=70;
0246 Iw=10+Fw;
0247 Ih=30+Fh;
0248
0249 defB=[ defB ...
0250 '\n param B' num2str(i) N '=' num2str(-Brho*A/L,15) '\n'...
0251 ];
0252 if multbend==0
0253 if sector==0
0254 defB=[ defB ...
0255 'genericbend ' N ' '...
0256 ' fieldLength=' num2str(L*1000/split,15) ...
0257 ' fieldWidth=' num2str(Fw,15) ' '...
0258 ' fieldHeight=' num2str(Fh,15) ' '...
0259 ' ironLength=' num2str(L*1*1000/split,15) ...
0260 ' ironWidth=' num2str(Iw,15) ' '...
0261 ' ironHeight=' num2str(Ih,15) ' '...
0262 ' maxStep=' num2str(StepLength) ...
0263 ' By=' num2str(Brho*A/L,15)...
0264 ' ironColor=1,.0,.0 kill=' num2str(killval) ' '...
0265 '\n'...
0266 ];
0267
0268 elseif sector==1
0269
0270 defB=[ defB ...
0271 'idealsectorbend ' N ' '...
0272 ' fieldCenterRadius=' num2str((L/A)*1000,15) ' '...
0273 ' fieldOuterRadius=' num2str((L/A)*1000+250,15) ' '...
0274 ' fieldInnerRadius=' num2str((L/A)*1000-250,15) ' '...
0275 ' fieldHeight=' num2str(Fh,15) ' '...
0276 ' ironOuterRadius=' num2str((L/A)*1000+270,15) ...
0277 ' ironInnerRadius=' num2str((L/A)*1000-270,15) ' '...
0278 ' ironHeight=' num2str(Ih,15) ' '...
0279 ' maxStep=' num2str(StepLength) ...
0280 ' angle=' num2str(A*180/pi,15)...
0281 ' By=' num2str(Brho*A/L,15)...
0282 ' ironColor=1,.0,.0 kill=' num2str(killval) ' '...
0283 '\n'...
0284 ];
0285 end
0286
0287 elseif multbend==1
0288
0289 inrad=60;
0290 outrad=70;
0291
0292 L=L * (2*sin(A/2))/A;
0293
0294
0295 defB=[ defB ...
0296 'multipole ' N ' '...
0297 ' fieldLength=' num2str(L*1000,15) ...
0298 ' ironLength=' num2str(L*1000,15) ...
0299 ' ironRadius=' num2str(outrad,15) ...
0300 ' apertureRadius=' num2str(inrad,15) ' '...
0301 ' fringe=' num2str(0*fringON) ...
0302 ' maxStep=' num2str(StepLength,15) ...
0303 ' dipole=' num2str(Brho*A/L,15)...
0304 ' quadrupole=' num2str(Brho*K,15)...
0305 ' ironColor=1,.0,.0 kill=' num2str(killval) ' '...
0306 '\n'...
0307 ];
0308
0309
0310 defined{d}=N;
0311 d=d+1;
0312
0313 elseif multbend==2
0314
0315 inrad=450;
0316 outrad=500;
0317
0318 L=L * (2*sin(A/2))/A;
0319
0320
0321 defB=[ defB ...
0322 ' genericquad ' N ' '...
0323 ' fieldLength=' num2str(L*1000,15) ...
0324 ' apertureRadius=' num2str(inrad,15) ' '...
0325 ' ironRadius=' num2str(outrad,15) ' '...
0326 ' ironLength=' num2str(L*1000,15) ' '...
0327 ' ironColor=0/255,191/255,255/255 kill=' num2str(killval) ' '...
0328 ' fringe=' num2str(0*fringON) ...
0329 ' gradient=' num2str(K*Brho,15) ' '...
0330 ' maxStep=' num2str(StepLength/2) ...
0331 ' ironColor=1,.0,.0 kill=' num2str(killval) ' '...
0332 '\n'...
0333 ];
0334
0335
0336 defined{d}=N;
0337 d=d+1;
0338
0339 end
0340 end
0341
0342 disp(totA)
0343
0344 dipposstr=[];
0345
0346 for spl=1:split
0347 S=findspos(arc,dipol(i))+(L*(spl-1/2));
0348 if multbend==1
0349
0350 dipposstr=[ dipposstr ' \n'...
0351 'place ' N ' parent= z=0+' num2str(0) '+0+' num2str(S*1000,15) ...
0352 ' x=' num2str(0*L/2*tan(A/2)*1000,15) ''...
0353 ' coordinates=centerline'...
0354 ' rotation=Y' num2str(0*A*180/pi/2,15)...
0355 ...
0356 '\n' ...
0357 ' corner C' num2str(i) ' z=0+' num2str(0) '+0+' num2str((S)*1000,15) ...
0358 ' rotation=Y' num2str(A*180/pi,15) ' radiusCut=100 radius=' num2str((L/A)*1000,15) '\n'...
0359 '\n'
0360 ...
0361 ...
0362 ...
0363 ];
0364 elseif multbend==2
0365
0366 dipposstr=[ dipposstr ' \n'...
0367 'place ' N ' parent= z=0+' num2str(0) '+0+' num2str(S*1000,15) ...
0368 ' x=' num2str(-A/(K*L)*1000,15) ''...
0369 ' coordinates=centerline'...
0370 ' rotation=Y' num2str(A*180/pi/2,15)...
0371 ...
0372 '\n' ...
0373 ...
0374 ...
0375 ...
0376 ' cornerarc' ' z=0+' num2str(0) '+0+' num2str((S)*1000,15) ...
0377 ' angle=' num2str(A*180/pi,15) ' centerRadius=' num2str((L/A)*1000,15) '\n'...
0378 '\n'...
0379 ];
0380
0381 elseif multbend==0
0382 if sector==0
0383
0384 dipposstr=[ dipposstr ' \n'...
0385 'place ' N ' parent= z=0+' num2str(0) '+0+' num2str(S*1000,15) ...
0386 ' x=' num2str(L/2*tan(A/2)*1000,15) ''...
0387 ' coordinates=centerline'...
0388 ' rotation=Y' num2str(A*180/pi/2,15)...
0389 ...
0390 '\n' ...
0391 ' cornerarc' ' z=0+' num2str(0) '+0+' num2str((S-L/2)*1000,15) ...
0392 ' angle=' num2str(A*180/pi,15) ' centerRadius=' num2str((L/A)*1000,15) '\n'...
0393 '\n'...
0394 ];
0395
0396 elseif sector==1
0397
0398 S=findspos(arc,dipol(i))+(L*(spl-1));
0399
0400
0401 dipposstr=[ dipposstr ' \n'...
0402 'place ' N ' parent= z=0+' num2str(0) '+0+' num2str(S*1000,15) ...
0403 ' x=' num2str(0*L/2*tan(A/2)*1000,15) ''...
0404 ' coordinates=centerline'...
0405 ' rotation=Y' num2str(0*(A*180/pi)/2,15)...
0406 ...
0407 '\n' ...
0408 ' cornerarc' ' z=0+' num2str(0) '+0+' num2str((S)*1000,15) ...
0409 ' angle=' num2str(A*180/pi,15) ' centerRadius=' num2str((L/A)*1000,15) '\n'...
0410 '\n'...
0411 ];
0412 end
0413 end
0414 end
0415 plac{ord}=dipposstr;
0416 ord=ord+1;
0417 multbend=tmpmultibend;
0418
0419 end
0420
0421 TotLength
0422
0423 RF_on=0;
0424
0425
0426
0427 RF=['pillbox RFC maxGradient=' num2str(RF_on*0.6/0.501777,15) ...
0428 ' color=0.5,0.5,0 frequency=0.4760054439 innerLength=501.777 '...
0429 ' phaseAcc=0.424389 wallThick=100 innerRadius=1000\n'];
0430 RF1pos=0*1000;
0431 plac{ord}=[' # place RFC z=' num2str(RF1pos) '\n'];
0432
0433 ippos=0;
0434 Dumppos=beamstart-1;
0435
0436 positions=[findspos(arc,quadr)'; findspos(arc,sextu)'; findspos(arc,dipol)'; (RF1pos-TotLength)/1000; (beamstart-TotLength)/1000;(TotLength/2)/1000;TotLength*2];
0437
0438 posString=[];
0439 for i=1:length(positions)-1
0440 posString=[posString num2str(positions(i)*1000) ','];
0441 end
0442 posString=[posString num2str(positions(i)*1000)];
0443
0444 twissH=['profile ' ...
0445 'zloop=' num2str(beamstart*0) ':' num2str(TotLength*3) ':1000 '...
0446 ...
0447 ' file=twissLER particle=' particle ...
0448 ' coordinates=centerline\n'];
0449
0450
0451
0452
0453 dump=['tubs DUMP innerRadius=0.01 outerRadius=300 length=10 kill=1 material=Cu color=0.2,0.2,0\n '...
0454 ];
0455
0456 plac{ord+1}=['# place ip parent= z=' num2str(ippos,15) ' \n' ];
0457 plac{ord+2}=[...
0458 posbeam 'place BeamVis z=' num2str(beamstart,15) ' y=101 \n'
0459 ];
0460 plac{ord+3}=['# place DUMP parent= z=' num2str(Dumppos,15) ' \n'];
0461
0462
0463 [s,ind]=sort([findspos(arc,quadr)'; findspos(arc,sextu)'; findspos(arc,dipol)'; RF1pos/1000;beamstart/1000;Dumppos/1000;TotLength/1000]);
0464
0465
0466 R=[head defQ defS defB RF dump];
0467 RPlace=[plac{ind}];
0468
0469
0470 outtext=char([...
0471 bigmondo ...
0472 ...
0473 R ...
0474 RPlace ...
0475 ' particlecolor e-=0,0,1 e+=1,0,0 \n'...
0476 ...
0477 'g4ui when=4 ''/vis/viewer/set/viewpointThetaPhi 90 90''\n'...
0478 'g4ui when=4 ''/vis/viewer/set/style wireframe''\n'...
0479 'g4ui when=4 ''/run/beamOn ' num2str(Nevents*2) ' ''\n'...
0480 ...
0481 ]);
0482
0483 out=fopen(nameG4bllat,'w+');
0484 fprintf(out,outtext);
0485
0486 fclose('all');
0487
0488
0489
0490
0491
0492 disp('--- DONE ---')