/scratch0/jsnuveri/BDSIM/BDSIMgit/bdsim/src/BDSBunchHalo.cc

00001 #include "BDSBunchHalo.hh"
00002 #include "BDSDebug.hh"
00003 
00004 BDSBunchHalo::BDSBunchHalo() : BDSBunchInterface(), betaX(0.0), betaY(0.0), alphaX(0.0), alphaY(0.0), emitX(0.0), emitY(0.0), gammaX(0.0), gammaY(0.0), envelopeX(0.0), envelopeY(0.0), envelopeXp(0.0), envelopeYp(0.0) {
00005   FlatGen  = new CLHEP::RandFlat(*CLHEP::HepRandom::getTheEngine());  
00006   weightParameter=1.0;
00007 }
00008 
00009 BDSBunchHalo::BDSBunchHalo(G4double betaXIn,      G4double betaYIn, 
00010                            G4double alphaXIn,     G4double alphaYIn,
00011                            G4double emitXIn,      G4double emitYIn,
00012                            G4double envelopeXIn,  G4double envelopeYIn,
00013                            G4double envelopeXpIn, G4double envelopeYpIn,
00014                            G4double X0In,         G4double Y0In,       G4double Z0In,   G4double T0In, 
00015                            G4double Xp0In,        G4double Yp0In,      G4double Zp0In,                       
00016                            G4double sigmaTIn,     G4double sigmaEIn) : BDSBunchInterface(X0In,Y0In,Z0In,T0In,Xp0In,Yp0In,Zp0In,sigmaTIn,sigmaEIn), betaX(betaXIn), betaY(betaYIn), alphaX(alphaXIn), alphaY(alphaYIn), emitX(emitXIn), emitY(emitYIn),   envelopeX(envelopeXIn), envelopeY(envelopeYIn), envelopeXp(envelopeXpIn), envelopeYp(envelopeYpIn) 
00017 {
00018   gammaX = 0.0;
00019   gammaY = 0.0;
00020 
00021 #ifdef BDSDEBUG
00022   G4cout << __METHOD_NAME__ << G4endl;
00023 #endif
00024   FlatGen  = new CLHEP::RandFlat(*CLHEP::HepRandom::getTheEngine());
00025   weightParameter=1.0;
00026 }
00027 
00028 BDSBunchHalo::~BDSBunchHalo() 
00029 { 
00030 #ifdef BDSDEBUG 
00031   G4cout << __METHOD_NAME__ << G4endl;
00032 #endif
00033   delete FlatGen; 
00034 }
00035 
00036 void  BDSBunchHalo::SetOptions(struct Options &opt) { 
00037 
00038   BDSBunchInterface::SetOptions(opt);
00039   SetBetaX(opt.betx);
00040   SetBetaY(opt.bety);
00041   SetAlphaX(opt.alfx);
00042   SetAlphaY(opt.alfy);
00043   SetEmitX(opt.emitx);
00044   SetEmitY(opt.emity);  
00045   gammaX = (1.0+alphaX*alphaX)/betaX;
00046   gammaY = (1.0+alphaY*alphaY)/betaY;  
00047   SetEnvelopeX(opt.envelopeX); 
00048   SetEnvelopeY(opt.envelopeY);
00049   SetEnvelopeXp(opt.envelopeXp);
00050   SetEnvelopeYp(opt.envelopeYp); 
00051   SetWeightParameter(opt.haloPSWeightParameter);
00052   SetWeightFunction(opt.haloPSWeightFunction);
00053 }
00054 
00055 void BDSBunchHalo::GetNextParticle(G4double& x0, G4double& y0, G4double& z0, 
00056                                    G4double& xp, G4double& yp, G4double& zp,
00057                                    G4double& t , G4double&  E, G4double& weight) {
00058 
00059   // Central orbit 
00060   x0 = X0  * CLHEP::m;
00061   y0 = Y0  * CLHEP::m;
00062   z0 = Z0  * CLHEP::m;
00063   xp = Xp0 * CLHEP::rad;
00064   yp = Yp0 * CLHEP::rad;
00065   z0 = Z0  * CLHEP::m; 
00066 
00067   //  z0 += (T0 - envelopeT * (1.-2.*FlatGen->shoot())) * CLHEP::c_light * CLHEP::s;
00068   z0 += 0;
00069 
00070   while(true) {
00071     // Flat 2x2d phase space
00072     G4double dx  = envelopeX  * (1-2*FlatGen->shoot()) * CLHEP::m;
00073     G4double dy  = envelopeY  * (1-2*FlatGen->shoot()) * CLHEP::m;
00074     G4double dxp = envelopeXp * (1-2*FlatGen->shoot()) * CLHEP::rad;
00075     G4double dyp = envelopeYp * (1-2*FlatGen->shoot()) * CLHEP::rad;
00076 
00077     // compute single particle emittance 
00078     double emitXSp = gammaX*pow(dx,2) + 2.*alphaX*dx*dxp + betaX*pow(dxp,2);
00079     double emitYSp = gammaY*pow(dy,2) + 2.*alphaY*dy*dyp + betaY*pow(dyp,2);
00080     
00081 #ifdef BDSDEBUG
00082     G4cout << "phase space> " << dx << " " << dy << " " << dxp << " " << dyp << G4endl;
00083     G4cout << "emittance> " << emitXSp << " " << emitX << " " << emitYSp << " " << emitY << G4endl;
00084 #endif
00085 
00086     // check if particle is within normal beam core, if so continue generation
00087     if (emitXSp < emitX || emitYSp <emitY) { 
00088 #ifdef BDSDEBUG
00089       G4cout << "continue> " << G4endl;
00090 #endif
00091       continue;
00092     } 
00093     else {
00094       // determine weight, initialise 1 so always passes
00095       double wx = 1.0;
00096       double wy = 1.0;
00097       if(weightFunction == "flat" || weightFunction == "") { 
00098         wx = 1.0;
00099         wy = 1.0;
00100       }
00101       else if (weightFunction == "oneoverr") { 
00102         wx = pow(emitX/fabs(emitXSp),weightParameter);
00103         wy = pow(emitY/fabs(emitYSp),weightParameter);  
00104       }
00105       else if (weightFunction == "exp") {
00106         wx = exp(-(emitXSp-emitX)/(emitX*weightParameter));
00107         wy = exp(-(emitYSp-emitY)/(emitY*weightParameter));
00108       }
00109       
00110 #ifdef BDSDEBUG
00111       G4cout << emitXSp/emitX << " " << emitYSp/emitY << " " << wx << " " << wy << G4endl;
00112 #endif
00113       // reject
00114       if(FlatGen->shoot() > wx && FlatGen->shoot() > wy) 
00115         continue;
00116 
00117       // add to reference orbit 
00118       x0 += dx;
00119       y0 += dy;
00120       xp += dxp;
00121       yp += dyp;
00122       
00123       zp = CalculateZp(xp,yp,Zp0);
00124       t = 0 * CLHEP::s;
00125       E = BDSGlobalConstants::Instance()->GetParticleKineticEnergy();
00126 
00127 #ifdef BDSDEBUG
00128       G4cout << "selected> " << dx << " " << dy << " " << dxp << " " << dyp << G4endl;
00129 #endif
00130      
00131       weight = 1.0;
00132       return;
00133     }
00134   }
00135 }

Generated on 28 Jun 2015 for BDSIM by  doxygen 1.4.7