/home/cern/BDSIM_new/src/BDSMultipoleMagField.cc

00001 
00007 //==============================================================
00008 
00009 
00010 
00011 #include "BDSGlobalConstants.hh" // must be first in include list
00012 
00013 #include "BDSMultipoleMagField.hh"
00014 
00015 #include "G4Navigator.hh"
00016 #include "G4TransportationManager.hh"
00017 
00018 ofstream testf;
00019 
00020 
00021 BDSMultipoleMagField::BDSMultipoleMagField(list<G4double> knl, list<G4double> ksl)
00022 {
00023   //G4cout<<"Creating BDSMultipleMagField"<<G4endl;
00024  
00025   //G4cout<<"size->"<<knl.size()<<G4endl;
00026 
00027   //G4double P0 = BDSGlobals->GetBeamTotalEnergy();
00028   
00029   // compute magnetic rigidity brho
00030   // formula: B(Tesla)*rho(m) = p(GeV)/(0.299792458 * |charge(e)|)
00031   G4double charge = BDSGlobals->GetParticleDefinition()->GetPDGCharge();
00032   G4double momentum = BDSGlobals->GetBeamMomentum();
00033   G4double brho = ( (momentum/GeV) / (0.299792458 * fabs(charge))) * (tesla*m);
00034 
00035   bnl = knl;
00036   bsl = ksl;
00037 
00038   list<G4double>::iterator it;
00039   list<G4double>::iterator its;
00040   
00041   for(it=bnl.begin(), its=bsl.begin();it!=bnl.end();it++, its++)
00042     {
00043       //G4cout<<"was : "<<(*it)<<G4endl;
00044       (*it) *= (brho*tesla );
00045       //G4cout<<"is : "<<(*it)<<G4endl;
00046       (*its) *= (brho*tesla );
00047     }
00048 
00049   testf.open("field.txt");
00050 
00051   G4double pt[4];
00052   G4double b[3];
00053 
00054   for(G4double x=-1*cm;x<1*cm;x+=0.1*mm)
00055     for(G4double y=-1*cm;y<1*cm;y+=0.1*mm){
00056       pt[0]= x;
00057       pt[1]= y;
00058       pt[2]= pt[3]=0;
00059       GetFieldValue(pt,b);
00060       testf<<pt[0]<<" "<<pt[1]<<" "<<b[0]/tesla<<" "<<b[1]/tesla<<" "<<b[2]/tesla<<G4endl;
00061     }
00062 
00063 
00064 }
00065 
00066 BDSMultipoleMagField::~BDSMultipoleMagField(){}
00067 
00068 void BDSMultipoleMagField::GetFieldValue( const G4double Point[4],
00069                        G4double *Bfield ) const
00070 {
00071   G4Navigator* MulNavigator=
00072     G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking();
00073   
00074   G4ThreeVector LocalR, GlobalR;
00075   
00076   GlobalR.setX(Point[0]);
00077   GlobalR.setY(Point[1]);
00078   GlobalR.setZ(Point[2]);
00079   //LocalR = QuadNavigator->GetCurrentLocalCoordinate();
00080   G4AffineTransform GlobalAffine=MulNavigator->GetGlobalToLocalTransform();
00081   LocalR=GlobalAffine.TransformPoint(GlobalR); 
00082  
00083 
00084   Bfield[0]=0;
00085   Bfield[1]=0;
00086 
00087 
00088   G4double br = 0;
00089   G4double bphi = 0;
00090   
00091 
00092   list<G4double>::const_iterator it;
00093   list<G4double>::const_iterator its;
00094 
00095   //G4cout<<"Called GetFieldValue "<<Point[0]<<" "<<Point[1]<<" "<<Point[2]<<G4endl;
00096   //G4cout<<"Called GetFieldValue "<<LocalR[0]<<" "<<LocalR[1]<<" "<<LocalR[2]<<//"br="G4endl;
00097 
00098   G4double r = sqrt(LocalR[0]*LocalR[0] + LocalR[1]*LocalR[1]);
00099   G4double phi;
00100   if(fabs(r)>1.e-11*m) phi = asin(LocalR[1]/r);
00101   else phi = 0; // don't care
00102                                            
00103 
00104   //G4cout<<"Called GetFieldValue "<<LocalR[0]<<" "<<LocalR[1]<<" "<<LocalR[2]<<
00105   // " r="<<r<<" Phi="<<phi<<G4endl;
00106 
00107   G4int order=0;
00108   G4double fact = -1;
00109 
00110 
00111   // I want to use the strange convention of dipole coeff. with opposite sign - 
00112   // then it is the same sign as angle 
00113 
00114   for(it=bnl.begin(), its=bsl.begin();it!=bnl.end();it++, its++)
00115     {
00116       order++;
00117       br += (*it) * pow(r,order-1) * sin(order*phi) / fact;
00118       bphi += (*it) * pow(r,order-1) * cos(order*phi)/ fact;
00119 
00120       if(order==1) {br *= -1; bphi *=-1; }
00121 
00122       fact *= (order*m);
00123     }
00124   
00125   //G4cout<<"order="<<order<<
00126   //  " br="<<br<<" bphi="<<bphi<<G4endl;
00127 
00128 
00129   Bfield[0]=( br*cos(phi)-bphi*sin(phi) );
00130   Bfield[1]=( br*sin(phi)+bphi*cos(phi) );
00131   
00132 
00133   Bfield[2]=0;
00134 
00135   Bfield[3]=0;
00136   Bfield[4]=0;
00137   Bfield[5]=0;
00138 
00139   // factor of 2 is actually 2-factorial.
00140 }
00141 
00142 
00143 

Generated on Wed Mar 5 17:25:22 2008 for BDSIM by  doxygen 1.5.3