source: trunk/examples/advanced/microbeam/src/MicrobeamEMField.cc @ 810

Last change on this file since 810 was 807, checked in by garnier, 16 years ago

update

  • Property svn:executable set to *
File size: 14.1 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26// -------------------------------------------------------------------
27// $Id: MicrobeamEMField.cc,v 1.6 2007/07/06 06:52:54 sincerti Exp $
28// -------------------------------------------------------------------
29
30#include "MicrobeamEMField.hh"
31
32MicrobeamEMField::MicrobeamEMField() 
33{   
34}
35
36void MicrobeamEMField::GetFieldValue(const double point[4], double *Bfield ) const
37{ 
38  // Magnetic field
39  Bfield[0] = 0;
40  Bfield[1] = 0;
41  Bfield[2] = 0;
42 
43  // Electric field
44  Bfield[3] = 0;
45  Bfield[4] = 0;
46  Bfield[5] = 0;
47
48  G4double Bx = 0;
49  G4double By = 0;
50  G4double Bz = 0;
51   
52  G4double x = point[0];
53  G4double y = point[1];
54  G4double z = point[2];
55
56// ***********************
57// AIFIRA SWITCHING MAGNET
58// ***********************
59 
60   // MAGNETIC FIELD VALUE FOR 3 MeV ALPHAS
61  G4double switchingField = 0.0589768635 * tesla ;
62 
63  // BEAM START
64  G4double beamStart = -10*m;
65
66  // RADIUS
67  G4double Rp = 0.698*m;
68
69  // ENTRANCE POSITION AFTER ANALYSIS MAGNET
70  G4double zS = 975*mm;
71 
72  // POLE GAP
73  G4double D = 31.8*mm;
74 
75  // FRINGING FIELD
76
77  G4double fieldBoundary, wc0, wc1, wc2, wc3, limitMinEntrance, limitMaxEntrance, limitMinExit, limitMaxExit;
78
79  limitMinEntrance = beamStart+zS-4*D;
80  limitMaxEntrance = beamStart+zS+4*D;
81  limitMinExit =Rp-4*D;
82  limitMaxExit =Rp+4*D; 
83   
84  wc0 = 0.3835;
85  wc1 = 2.388;
86  wc2 = -0.8171;
87  wc3 = 0.200;
88
89  fieldBoundary=0.62;
90
91  G4double ws, largeS, h, dhdlargeS, dhds, dlargeSds, dsdz, dsdx, zs0, Rs0, xcenter, zcenter;
92 
93// - ENTRANCE OF SWITCHING MAGNET
94
95if ( (z >= limitMinEntrance) && (z < limitMaxEntrance) ) 
96{
97  zs0 = fieldBoundary*D;
98  ws = (-z+beamStart+zS-zs0)/D;
99  dsdz = -1/D;
100  dsdx = 0;
101
102  largeS = wc0 + wc1*ws + wc2*ws*ws + wc3*ws*ws*ws;
103  h = 1./(1.+std::exp(largeS));
104  dhdlargeS = -std::exp(largeS)*h*h; 
105  dlargeSds = wc1+ 2*wc2*ws + 3*wc3*ws*ws;
106  dhds = dhdlargeS * dlargeSds;
107     
108  By = switchingField * h ;
109  Bx = y*switchingField*dhds*dsdx;
110  Bz = y*switchingField*dhds*dsdz;
111
112}
113
114// - HEART OF SWITCHING MAGNET   
115               
116 if ( 
117        (z >= limitMaxEntrance) 
118   &&  (( x*x + (z -(beamStart+zS))*(z -(beamStart+zS)) < limitMinExit*limitMinExit)) 
119    )   
120{
121   Bx=0; 
122   By = switchingField; 
123   Bz=0;
124}                                           
125       
126// - EXIT OF SWITCHING MAGNET
127
128if ( 
129        (z >= limitMaxEntrance) 
130   &&   (( x*x + (z -(beamStart+zS))*(z -(beamStart+zS))) >= limitMinExit*limitMinExit) 
131   &&   (( x*x + (z -(beamStart+zS))*(z -(beamStart+zS))) < limitMaxExit*limitMaxExit)
132
133    )   
134{
135
136  xcenter = 0;
137  zcenter =  beamStart+zS;
138 
139  Rs0 = Rp + D*fieldBoundary;
140  ws = (std::sqrt((z-zcenter)*(z-zcenter)+(x-xcenter)*(x-xcenter)) - Rs0)/D;
141       
142  dsdz = (1/D)*(z-zcenter)/std::sqrt((z-zcenter)*(z-zcenter)+(x-xcenter)*(x-xcenter));
143  dsdx = (1/D)*(x-xcenter)/std::sqrt((z-zcenter)*(z-zcenter)+(x-xcenter)*(x-xcenter));
144
145  largeS = wc0 + wc1*ws + wc2*ws*ws + wc3*ws*ws*ws;
146  h = 1./(1.+std::exp(largeS));
147  dhdlargeS = -std::exp(largeS)*h*h; 
148  dlargeSds = wc1+ 2*wc2*ws + 3*wc3*ws*ws;
149  dhds = dhdlargeS * dlargeSds;
150     
151  By = switchingField * h ;
152  Bx = y*switchingField*dhds*dsdx;
153  Bz = y*switchingField*dhds*dsdz;
154
155}
156
157// **************************
158// MICROBEAM LINE QUADRUPOLES
159// **************************
160 
161  // MICROBEAM LINE ANGLE
162  G4double lineAngle = -10*deg;
163 
164  // X POSITION OF FIRST QUADRUPOLE
165  G4double lineX = -1295.59*mm;
166
167  // Z POSITION OF FIRST QUADRUPOLE
168  G4double lineZ = -1327*mm;
169
170  // Adjust magnetic zone absolute position
171  lineX = lineX + 5.24*micrometer*std::cos(-lineAngle); // 5.24 = 1.3 + 3.94 micrometer (cf. DetectorConstruction)
172  lineZ = lineZ + 5.24*micrometer*std::sin(-lineAngle);
173       
174  // QUADRUPOLE HALF LENGTH
175  G4double quadHalfLength = 75*mm;
176 
177  // QUADRUPOLE SPACING
178  G4double quadSpacing = 40*mm;
179 
180  // QUADRUPOLE CENTER COORDINATES
181  G4double xoprime, zoprime;
182 
183if (z>=-1400*mm & z <-200*mm)
184{
185  Bx=0; By=0; Bz=0;
186 
187  // FRINGING FILED CONSTANTS
188  G4double c0[4], c1[4], c2[4], z1[4], z2[4], a0[4], gradient[4];
189 
190  // QUADRUPOLE 1
191  c0[0] = -5.;
192  c1[0] = 2.5;
193  c2[0] = -0.1;
194  z1[0] = 60*mm;
195  z2[0] = 130*mm;
196  a0[0] = 10*mm;
197  gradient[0] = 3.406526 *tesla/m;
198
199  // QUADRUPOLE 2
200  c0[1] = -5.;
201  c1[1] = 2.5;
202  c2[1] = -0.1;
203  z1[1] = 60*mm;
204  z2[1] = 130*mm;
205  a0[1] = 10*mm;
206  gradient[1] = -8.505263 *tesla/m;
207
208  // QUADRUPOLE 3
209  c0[2] = -5.;
210  c1[2] = 2.5;
211  c2[2] = -0.1;
212  z1[2] = 60*mm;
213  z2[2] = 130*mm;
214  a0[2] = 10*mm;
215  gradient[2] = 8.505263 *tesla/m;
216
217  // QUADRUPOLE 4
218  c0[3] = -5.;
219  c1[3] = 2.5;
220  c2[3] = -0.1;
221  z1[3] = 60*mm;
222  z2[3] = 130*mm;
223  a0[3] = 10*mm;
224  gradient[3] = -3.406526*tesla/m;
225
226  // FIELD CREATED BY A QUADRUPOLE IN ITS LOCAL FRAME
227  G4double Bx_local,By_local,Bz_local;
228  Bx_local = 0; By_local = 0; Bz_local = 0;
229 
230  // FIELD CREATED BY A QUADRUPOOLE IN WORLD FRAME
231  G4double Bx_quad,By_quad,Bz_quad;
232  Bx_quad = 0; By_quad=0; Bz_quad=0;
233 
234  // QUADRUPOLE FRAME
235  G4double x_local,y_local,z_local;
236  x_local= 0; y_local=0; z_local=0;
237
238  G4double s = 0;
239  G4double G0, G1, G2, G3;
240  G4double K0, K1, K2, K3;
241  G4double P0, P1, P2, P3, cte;
242
243  K0=0;
244  K1=0;
245  K2=0;
246  K3=0;
247  P0=0;
248  P1=0;
249  P2=0;
250  P3=0;
251  G0=0;
252  G1=0;
253  G2=0;
254  G3=0;
255  cte=0;
256
257  G4bool largeScattering=false;
258 
259  for (G4int i=0;i<4; i++) 
260  {
261 
262         if (i==0) 
263                {       xoprime = lineX + quadHalfLength*std::sin(lineAngle);
264                        zoprime = lineZ + quadHalfLength*std::cos(lineAngle);
265
266                        x_local = (x - xoprime) * std::cos (lineAngle) - (z - zoprime) * std::sin (lineAngle); 
267                        y_local = y; 
268                        z_local = (z - zoprime) * std::cos (lineAngle) + (x - xoprime) * std::sin (lineAngle); 
269                        if (std::sqrt(x_local*x_local+y_local*y_local)>a0[i]) largeScattering=true;
270
271                }
272                 
273         if (i==1) 
274                {       xoprime = lineX + (3*quadHalfLength+quadSpacing)*std::sin(lineAngle);
275                        zoprime = lineZ + (3*quadHalfLength+quadSpacing)*std::cos(lineAngle);
276
277                        x_local = (x - xoprime) * std::cos (lineAngle) - (z - zoprime) * std::sin (lineAngle); 
278                        y_local = y; 
279                        z_local = (z - zoprime) * std::cos (lineAngle) + (x - xoprime) * std::sin (lineAngle); 
280                        if (std::sqrt(x_local*x_local+y_local*y_local)>a0[i]) largeScattering=true;
281                }
282
283         if (i==2) 
284                {       xoprime = lineX + (5*quadHalfLength+2*quadSpacing)*std::sin(lineAngle);
285                        zoprime = lineZ + (5*quadHalfLength+2*quadSpacing)*std::cos(lineAngle);
286
287                        x_local = (x - xoprime) * std::cos (lineAngle) - (z - zoprime) * std::sin (lineAngle); 
288                        y_local = y; 
289                        z_local = (z - zoprime) * std::cos (lineAngle) + (x - xoprime) * std::sin (lineAngle); 
290                        if (std::sqrt(x_local*x_local+y_local*y_local)>a0[i]) largeScattering=true;
291                }
292         
293         if (i==3) 
294                {       xoprime = lineX + (7*quadHalfLength+3*quadSpacing)*std::sin(lineAngle);
295                        zoprime = lineZ + (7*quadHalfLength+3*quadSpacing)*std::cos(lineAngle);
296
297                        x_local = (x - xoprime) * std::cos (lineAngle) - (z - zoprime) * std::sin (lineAngle); 
298                        y_local = y; 
299                        z_local = (z - zoprime) * std::cos (lineAngle) + (x - xoprime) * std::sin (lineAngle); 
300                        if (std::sqrt(x_local*x_local+y_local*y_local)>a0[i]) largeScattering=true;
301                }
302
303         
304          if ( z_local < -z2[i] )
305         {
306          G0=0;
307          G1=0;
308          G2=0;
309          G3=0;
310         }
311         
312         if ( z_local > z2[i] )
313         {
314          G0=0;
315          G1=0;
316          G2=0;
317          G3=0;
318         }
319
320         if ( (z_local>=-z1[i]) & (z_local<=z1[i]) ) 
321         {
322          G0=gradient[i];
323          G1=0;
324          G2=0;
325          G3=0;
326         }
327         
328         if ( ((z_local>=-z2[i]) & (z_local<-z1[i])) ||  ((z_local>z1[i]) & (z_local<=z2[i])) ) 
329         {
330
331         s = ( z_local - z1[i]) / a0[i] ;
332         if (z_local<-z1[i]) s = ( - z_local - z1[i]) / a0[i] ;
333
334
335         P0 = c0[i]+c1[i]*s+c2[i]*s*s;
336
337         P1 = c1[i]/a0[i]+2*c2[i]*(z_local-z1[i])/a0[i]/a0[i];
338         if (z_local<-z1[i])  P1 = -c1[i]/a0[i]+2*c2[i]*(z_local+z1[i])/a0[i]/a0[i];
339
340         P2 = 2*c2[i]/a0[i]/a0[i];
341
342         P3 = 0;
343
344         cte = 1 + std::exp(c0[i]);
345
346         K1 = -cte*P1*std::exp(P0)/( (1+std::exp(P0))*(1+std::exp(P0)) );
347
348         K2 = -cte*std::exp(P0)*(
349          P2/( (1+std::exp(P0))*(1+std::exp(P0)) )
350         +2*P1*K1/(1+std::exp(P0))/cte
351         +P1*P1/(1+std::exp(P0))/(1+std::exp(P0))
352         );
353 
354         K3 = -cte*std::exp(P0)*(
355         (3*P2*P1+P1*P1*P1)/(1+std::exp(P0))/(1+std::exp(P0))
356         +4*K1*(P1*P1+P2)/(1+std::exp(P0))/cte
357         +2*P1*(K1*K1/cte/cte+K2/(1+std::exp(P0))/cte)
358          );
359         
360         G0 = gradient[i]*cte/(1+std::exp(P0));
361         G1 = gradient[i]*K1;
362         G2 = gradient[i]*K2;
363         G3 = gradient[i]*K3;
364
365         }
366         
367         // PROTECTION AGAINST LARGE SCATTERING
368
369         if ( largeScattering ) 
370         {
371          G0=0;
372          G1=0;
373          G2=0;
374          G3=0;
375         }
376
377         // MAGNETIC FIELD COMPUTATION FOR EACH QUADRUPOLE
378         
379         Bx_local = y_local*(G0-(1./12)*(3*x_local*x_local+y_local*y_local)*G2);
380         By_local = x_local*(G0-(1./12)*(3*y_local*y_local+x_local*x_local)*G2);
381         Bz_local = x_local*y_local*(G1-(1./12)*(x_local*x_local+y_local*y_local)*G3);
382
383         Bx_quad = Bz_local*std::sin(lineAngle)+Bx_local*std::cos(lineAngle);
384         By_quad = By_local;
385         Bz_quad = Bz_local*std::cos(lineAngle)-Bx_local*std::sin(lineAngle);
386
387         // TOTAL MAGNETIC FIELD
388         
389         Bx = Bx + Bx_quad ;
390         By = By + By_quad ;
391         Bz = Bz + Bz_quad ;
392
393  } // LOOP ON QUADRUPOLES
394
395     
396} // END OF QUADRUPLET
397
398  Bfield[0] = Bx;
399  Bfield[1] = By;
400  Bfield[2] = Bz;
401
402// *****************************************
403// ELECTRIC FIELD CREATED BY SCANNING PLATES
404// *****************************************
405
406  Bfield[3] = 0;
407  Bfield[4] = 0;
408  Bfield[5] = 0;
409
410  // POSITION OF EXIT OF LAST QUAD WHERE THE SCANNING PLATES START
411
412  G4double electricPlateWidth1 = 5 * mm;
413  G4double electricPlateWidth2 = 5 * mm;
414  G4double electricPlateLength1 = 36 * mm;
415  G4double electricPlateLength2 = 34 * mm;
416  G4double electricPlateGap = 5 * mm;
417  G4double electricPlateSpacing1 = 3 * mm;
418  G4double electricPlateSpacing2 = 4 * mm;
419
420  // APPLY VOLTAGE HERE IN VOLTS (no electrostatic deflection here)
421  G4double electricPlateVoltage1 = 0 * volt;
422  G4double electricPlateVoltage2 = 0 * volt;
423
424  G4double electricFieldPlate1 = electricPlateVoltage1 / electricPlateSpacing1 ;
425  G4double electricFieldPlate2 = electricPlateVoltage2 / electricPlateSpacing2 ;
426
427  G4double  beginFirstZoneX = lineX + (8*quadHalfLength+3*quadSpacing)*std::sin(lineAngle);
428  G4double  beginFirstZoneZ = lineZ + (8*quadHalfLength+3*quadSpacing)*std::cos(lineAngle);
429
430  G4double  beginSecondZoneX = lineX + (8*quadHalfLength+3*quadSpacing+electricPlateLength1+electricPlateGap)*std::sin(lineAngle);
431  G4double  beginSecondZoneZ = lineZ + (8*quadHalfLength+3*quadSpacing+electricPlateLength1+electricPlateGap)*std::cos(lineAngle);
432
433  G4double xA, zA, xB, zB, xC, zC, xD, zD;
434  G4double slope1, cte1, slope2, cte2, slope3, cte3, slope4, cte4;
435 
436  // WARNING : lineAngle < 0
437
438  // FIRST PLATES
439 
440  xA = beginFirstZoneX + std::cos(lineAngle)*electricPlateSpacing1/2;
441  zA = beginFirstZoneZ - std::sin(lineAngle)*electricPlateSpacing1/2;
442
443  xB = xA + std::sin(lineAngle)*electricPlateLength1; 
444  zB = zA + std::cos(lineAngle)*electricPlateLength1;
445 
446  xC = xB - std::cos(lineAngle)*electricPlateSpacing1;
447  zC = zB + std::sin(lineAngle)*electricPlateSpacing1;
448
449  xD = xC - std::sin(lineAngle)*electricPlateLength1; 
450  zD = zC - std::cos(lineAngle)*electricPlateLength1;
451 
452  slope1 = (xB-xA)/(zB-zA);
453  cte1 = xA - slope1 * zA;
454 
455  slope2 = (xC-xB)/(zC-zB);
456  cte2 = xB - slope2 * zB;
457 
458  slope3 = (xD-xC)/(zD-zC);
459  cte3 = xC - slope3 * zC;
460 
461  slope4 = (xA-xD)/(zA-zD);
462  cte4 = xD - slope4 * zD;
463 
464   
465  if 
466  (
467     x <= slope1 * z + cte1
468  && x >= slope3 * z + cte3
469  && x <= slope4 * z + cte4
470  && x >= slope2 * z + cte2   
471  && std::abs(y)<=electricPlateWidth1/2
472  ) 
473
474  {
475      Bfield[3] = electricFieldPlate1*std::cos(lineAngle);
476      Bfield[4] = 0;
477      Bfield[5] = -electricFieldPlate1*std::sin(lineAngle);
478 
479  }
480     
481  // SECOND PLATES
482     
483  xA = beginSecondZoneX + std::cos(lineAngle)*electricPlateWidth2/2;
484  zA = beginSecondZoneZ - std::sin(lineAngle)*electricPlateWidth2/2;
485
486  xB = xA + std::sin(lineAngle)*electricPlateLength2; 
487  zB = zA + std::cos(lineAngle)*electricPlateLength2;
488 
489  xC = xB - std::cos(lineAngle)*electricPlateWidth2;
490  zC = zB + std::sin(lineAngle)*electricPlateWidth2;
491
492  xD = xC - std::sin(lineAngle)*electricPlateLength2; 
493  zD = zC - std::cos(lineAngle)*electricPlateLength2;
494 
495  slope1 = (xB-xA)/(zB-zA);
496  cte1 = xA - slope1 * zA;
497 
498  slope2 = (xC-xB)/(zC-zB);
499  cte2 = xB - slope2 * zB;
500 
501  slope3 = (xD-xC)/(zD-zC);
502  cte3 = xC - slope3 * zC;
503 
504  slope4 = (xA-xD)/(zA-zD);
505  cte4 = xD - slope4 * zD;
506
507  if 
508  (     
509     x <= slope1 * z + cte1
510  && x >= slope3 * z + cte3
511  && x <= slope4 * z + cte4
512  && x >= slope2 * z + cte2   
513  && std::abs(y)<=electricPlateSpacing2/2
514  )
515
516  { 
517      Bfield[3] = 0;
518      Bfield[4] = electricFieldPlate2;
519      Bfield[5] = 0;
520  }
521
522// ZERO FIELD REGIONS
523
524if (
525(Bfield[0]==0. &
526Bfield[1]==0. &
527Bfield[2]==0. &
528Bfield[4]==0. &
529Bfield[5]==0. &
530Bfield[6]==0. )
531)
532{
533
534G4FieldManager *pFieldMgr;
535pFieldMgr = G4TransportationManager::GetTransportationManager()->GetFieldManager();
536pFieldMgr = NULL;
537
538}
539
540//
541
542
543}
544
Note: See TracBrowser for help on using the repository browser.