G4AdjointSimManager.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 // $Id$
00027 //
00029 //      Class Name:     G4AdjointCrossSurfChecker
00030 //      Author:         L. Desorgher
00031 //      Organisation:   SpaceIT GmbH
00032 //      Contract:       ESA contract 21435/08/NL/AT
00033 //      Customer:       ESA/ESTEC
00035 
00036 #include "G4AdjointSimManager.hh"
00037 #include "G4Run.hh"
00038 #include "G4RunManager.hh"
00039 
00040 #include "G4UserEventAction.hh"
00041 #include "G4VUserPrimaryGeneratorAction.hh"
00042 #include "G4UserTrackingAction.hh"
00043 #include "G4UserSteppingAction.hh"
00044 #include "G4UserStackingAction.hh"
00045 #include "G4UserRunAction.hh"
00046 
00047 #include "G4AdjointPrimaryGeneratorAction.hh"
00048 #include "G4AdjointSteppingAction.hh"
00049 #include "G4AdjointStackingAction.hh"
00050 
00051 #include "G4AdjointSimMessenger.hh"
00052 
00053 #include "G4AdjointCrossSurfChecker.hh"
00054 
00055 #include "G4ParticleTable.hh"
00056 #include "G4PhysicsLogVector.hh"
00057 
00059 //
00060 G4AdjointSimManager* G4AdjointSimManager::instance = 0;
00061 
00063 //
00064 G4AdjointSimManager::G4AdjointSimManager()
00065 { 
00066  //Create adjoint actions;
00067  //----------------------
00068  
00069  theAdjointRunAction = 0; 
00070  theAdjointPrimaryGeneratorAction = new G4AdjointPrimaryGeneratorAction(); 
00071  theAdjointSteppingAction = new G4AdjointSteppingAction();
00072  theAdjointEventAction = 0;
00073  theAdjointTrackingAction = 0;  
00074  theAdjointStackingAction = new G4AdjointStackingAction();
00075 
00076  //Create messenger
00077  //----------------
00078   theMessenger = new G4AdjointSimMessenger(this);
00079   
00080   user_action_already_defined=false;
00081   use_user_StackingAction = false; 
00082 
00083   fUserTrackingAction= 0;
00084   fUserEventAction= 0; 
00085   fUserSteppingAction= 0;
00086   fUserPrimaryGeneratorAction= 0;
00087   fUserRunAction= 0;
00088   fUserStackingAction= 0;
00089   
00090   adjoint_sim_mode = false;
00091   
00092   normalisation_mode=3;
00093   
00094   nb_nuc=1.;
00095   
00096   welcome_message =true;
00097   
00098   /*electron_last_weight_vector = new G4PhysicsLogVector(1.e-20,1.e20,400);
00099   proton_last_weight_vector  = new  G4PhysicsLogVector(1.e-20,1.e20,400);
00100   gamma_last_weight_vector = new    G4PhysicsLogVector(1.e-20,1.e20,400);*/ 
00101 }
00103 //
00104 G4AdjointSimManager::~G4AdjointSimManager()
00105 { 
00106   if (theAdjointRunAction) delete theAdjointRunAction;
00107   if (theAdjointPrimaryGeneratorAction) delete theAdjointPrimaryGeneratorAction;
00108   if (theAdjointSteppingAction) delete theAdjointSteppingAction;
00109   if (theAdjointEventAction) delete theAdjointEventAction;
00110   if (theAdjointTrackingAction) delete theAdjointTrackingAction;
00111   if (theAdjointStackingAction) delete theAdjointStackingAction;
00112   if (theMessenger) delete theMessenger;
00113 }
00115 //
00116 G4AdjointSimManager* G4AdjointSimManager::GetInstance()
00117 {
00118   if (instance == 0) instance = new G4AdjointSimManager;
00119   return instance;
00120 }
00122 //
00123 void G4AdjointSimManager::RunAdjointSimulation(G4int nb_evt)
00124 { 
00125   if (welcome_message) {
00126         G4cout<<"****************************************************************"<<std::endl;
00127         G4cout<<"*** Geant4 Reverse/Adjoint Monte Carlo mode                  ***"<<std::endl;
00128         G4cout<<"*** Author:    L.Desorgher                                   ***"<<std::endl;
00129         G4cout<<"*** Company:   SpaceIT GmbH, Bern, Switzerland               ***"<<std::endl;
00130         G4cout<<"*** Sponsored by: ESA/ESTEC contract contract 21435/08/NL/AT ***"<<std::endl;  
00131         G4cout<<"****************************************************************"<<std::endl;
00132         welcome_message=false;
00133   }     
00134   
00135   //Replace the user defined actions by the adjoint actions
00136   //---------------------------------------------------------
00137   SetAdjointPrimaryRunAndStackingActions();
00138   SetRestOfAdjointActions();
00139   
00140   //Update the list of primaries
00141   //-----------------------------
00142   theAdjointPrimaryGeneratorAction->UpdateListOfPrimaryParticles();
00143 
00144   adjoint_sim_mode=true;
00145   
00146   ID_of_last_particle_that_reach_the_ext_source=0;
00147   
00148   //Make the run
00149   //------------
00150   
00151   nb_evt_of_last_run =nb_evt;
00152   G4RunManager::GetRunManager()->BeamOn(theAdjointPrimaryGeneratorAction->GetNbOfAdjointPrimaryTypes()*2*nb_evt);
00153 
00154   //Restore the user defined actions
00155   //--------------------------------
00156   ResetRestOfUserActions(); 
00157   ResetUserPrimaryRunAndStackingActions();
00158   adjoint_sim_mode=false; 
00159 
00160   /*
00161   //Register the weight vector
00162   //--------------------------
00163   std::ofstream FileOutputElectronWeight("ElectronWeight.txt", std::ios::out);
00164   FileOutputElectronWeight<<std::setiosflags(std::ios::scientific);
00165   FileOutputElectronWeight<<std::setprecision(6);
00166   G4bool aBool = electron_last_weight_vector->Store(FileOutputElectronWeight, true);
00167   FileOutputElectronWeight.close();
00168   
00169   std::ofstream FileOutputProtonWeight("ProtonWeight.txt", std::ios::out);
00170   FileOutputProtonWeight<<std::setiosflags(std::ios::scientific);
00171   FileOutputProtonWeight<<std::setprecision(6);
00172   aBool = proton_last_weight_vector->Store(FileOutputProtonWeight, true);
00173   FileOutputProtonWeight.close();
00174   
00175   std::ofstream FileOutputGammaWeight("GammaWeight.txt", std::ios::out);
00176   FileOutputGammaWeight<<std::setiosflags(std::ios::scientific);
00177   FileOutputGammaWeight<<std::setprecision(6);
00178   aBool = gamma_last_weight_vector->Store(FileOutputGammaWeight, true);
00179   FileOutputGammaWeight.close();
00180   */  
00181 }
00183 //
00184 void G4AdjointSimManager::SetRestOfAdjointActions()
00185 {  
00186   G4RunManager* theRunManager =  G4RunManager::GetRunManager();
00187   
00188   if (!user_action_already_defined) DefineUserActions();
00189  
00190  //Replace the user action by the adjoint actions
00191  //------------------------------------------------- 
00192   
00193   theRunManager->SetUserAction(theAdjointEventAction);
00194   theRunManager->SetUserAction(theAdjointSteppingAction);
00195   theRunManager->SetUserAction(theAdjointTrackingAction);  
00196 }
00198 //
00199 void G4AdjointSimManager::SetAdjointPrimaryRunAndStackingActions()
00200 {  
00201   G4RunManager* theRunManager =  G4RunManager::GetRunManager();
00202   
00203   if (!user_action_already_defined) DefineUserActions();
00204   
00205  //Replace the user action by the adjoint actions
00206  //------------------------------------------------- 
00207   
00208   theRunManager->SetUserAction(theAdjointRunAction);
00209   theRunManager->SetUserAction(theAdjointPrimaryGeneratorAction);
00210   theRunManager->SetUserAction(theAdjointStackingAction);
00211   if (use_user_StackingAction)  theAdjointStackingAction->SetUserFwdStackingAction(fUserStackingAction);
00212   else theAdjointStackingAction->SetUserFwdStackingAction(0);
00213 }
00215 //
00216 void G4AdjointSimManager::ResetRestOfUserActions()
00217 {
00218   G4RunManager* theRunManager =  G4RunManager::GetRunManager();
00219   
00220   //Restore the user defined actions
00221   //-------------------------------
00222  
00223   theRunManager->SetUserAction(fUserEventAction);
00224   theRunManager->SetUserAction(fUserSteppingAction);
00225   theRunManager->SetUserAction(fUserTrackingAction);
00226 }
00227 
00229 //
00230 void G4AdjointSimManager::ResetUserPrimaryRunAndStackingActions()
00231 { 
00232   G4RunManager* theRunManager =  G4RunManager::GetRunManager();
00233   //Restore the user defined actions
00234   //-------------------------------
00235   theRunManager->SetUserAction(fUserRunAction);
00236   theRunManager->SetUserAction(fUserPrimaryGeneratorAction); 
00237   theRunManager->SetUserAction(fUserStackingAction);
00238 }
00240 //
00241 void G4AdjointSimManager::DefineUserActions()
00242 { 
00243    G4RunManager* theRunManager =  G4RunManager::GetRunManager();
00244    fUserTrackingAction= const_cast<G4UserTrackingAction* >( theRunManager->GetUserTrackingAction() );
00245    fUserEventAction= const_cast<G4UserEventAction* >( theRunManager->GetUserEventAction() ); 
00246    fUserSteppingAction= const_cast<G4UserSteppingAction* >( theRunManager->GetUserSteppingAction() );
00247    fUserPrimaryGeneratorAction= const_cast<G4VUserPrimaryGeneratorAction* >( theRunManager->GetUserPrimaryGeneratorAction() );
00248    fUserRunAction= const_cast<G4UserRunAction*>( theRunManager->GetUserRunAction() );
00249    fUserStackingAction= const_cast<G4UserStackingAction* >( theRunManager->GetUserStackingAction() );
00250    user_action_already_defined=true;    
00251 }
00253 //
00254 void G4AdjointSimManager::SetAdjointTrackingMode(G4bool aBool)
00255 {
00256   adjoint_tracking_mode = aBool;
00257  
00258   if (adjoint_tracking_mode) {
00259         SetRestOfAdjointActions();
00260         theAdjointStackingAction->SetAdjointMode(true);
00261         theAdjointStackingAction->SetKillTracks(false);
00262         
00263   }     
00264   else {
00265         
00266         ResetRestOfUserActions();
00267         theAdjointStackingAction->SetAdjointMode(false);
00268         if (GetDidAdjParticleReachTheExtSource()){
00269                 theAdjointStackingAction->SetKillTracks(false);
00270                 RegisterAtEndOfAdjointTrack();
00271         }
00272         else theAdjointStackingAction->SetKillTracks(true);
00273   }     
00274 }
00276 //
00277 G4bool G4AdjointSimManager::GetDidAdjParticleReachTheExtSource()
00278 {
00279   return theAdjointSteppingAction->GetDidAdjParticleReachTheExtSource();
00280 }
00282 //
00283 std::vector<G4ParticleDefinition*>  G4AdjointSimManager::GetListOfPrimaryFwdParticles()
00284 {
00285   return theAdjointPrimaryGeneratorAction->GetListOfPrimaryFwdParticles();
00286 }
00288 //
00289 void G4AdjointSimManager::RegisterAtEndOfAdjointTrack()
00290 {
00291   last_pos = theAdjointSteppingAction->GetLastPosition();  
00292   last_direction = theAdjointSteppingAction->GetLastMomentum();
00293   last_direction /=last_direction.mag();
00294   last_cos_th =  last_direction.z();
00295   G4ParticleDefinition* aPartDef= theAdjointSteppingAction->GetLastPartDef(); 
00296         
00297   last_fwd_part_name= aPartDef->GetParticleName();
00298  
00299   last_fwd_part_name.remove(0,4);
00300   
00301   last_fwd_part_PDGEncoding=G4ParticleTable::GetParticleTable()->FindParticle(last_fwd_part_name)->GetPDGEncoding();
00302         
00303   std::vector<G4ParticleDefinition*> aList = theAdjointPrimaryGeneratorAction->GetListOfPrimaryFwdParticles();
00304   last_fwd_part_index=-1;
00305   size_t i=0;
00306   while(i<aList.size() && last_fwd_part_index<0) {
00307         if (aList[i]->GetParticleName() == last_fwd_part_name) last_fwd_part_index=i;
00308         i++;
00309   }
00310   
00311   last_ekin = theAdjointSteppingAction->GetLastEkin();
00312   last_ekin_nuc = last_ekin;
00313   if (aPartDef->GetParticleType() == "adjoint_nucleus") {
00314         nb_nuc=double(aPartDef->GetBaryonNumber());
00315         last_ekin_nuc /=nb_nuc;
00316   }
00317 
00318   last_weight = theAdjointSteppingAction->GetLastWeight(); 
00319   
00320   /* G4PhysicsLogVector* theWeightVector=0;
00321   if (last_fwd_part_name =="e-")  theWeightVector=electron_last_weight_vector;
00322   else if (last_fwd_part_name =="gamma") theWeightVector=gamma_last_weight_vector;
00323   else if (last_fwd_part_name =="proton") theWeightVector=proton_last_weight_vector;
00324   
00325   if (theWeightVector){
00326 
00327         size_t ind =  size_t(std::log10(last_weight/theAdjointPrimaryWeight)*10. + 200);
00328         G4double low_val =theWeightVector->GetLowEdgeEnergy(ind);
00329         G4bool aBool = true;
00330         G4double bin_weight = theWeightVector->GetValue(low_val, aBool)+1.;
00331         theWeightVector->PutValue(ind, bin_weight);
00332   }
00333   */
00334   /*if ((last_weight/theAdjointPrimaryWeight)>1.) last_weight*=1000. ;
00335   else if ( (last_weight/theAdjointPrimaryWeight)>0.1) last_weight*=100. ;
00336   else if ( (last_weight/theAdjointPrimaryWeight)>0.01) last_weight*=10. ;*/
00337   
00338   
00339   //G4cout <<"Last Weight "<<last_weight<<'\t'<<theAdjointPrimaryWeight<<'\t'<<last_weight/theAdjointPrimaryWeight<<std::endl;
00340   /*if (last_weight/theAdjointPrimaryWeight >10.) {
00341         G4cout<<"Warning a weight increase by a factor : "<<last_weight/theAdjointPrimaryWeight<<std::endl;
00342   }
00343   */
00344  
00345    ID_of_last_particle_that_reach_the_ext_source++;
00346 }
00348 //
00349 G4bool  G4AdjointSimManager::DefineSphericalExtSource(G4double radius, G4ThreeVector pos)
00350 {       
00351    G4double area;
00352    return G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurface("ExternalSource", radius, pos, area);
00353 }
00355 //
00356 G4bool  G4AdjointSimManager::DefineSphericalExtSourceWithCentreAtTheCentreOfAVolume(G4double radius, const G4String& volume_name)
00357 {
00358    G4double area;
00359    G4ThreeVector center;
00360    return G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurfaceWithCenterAtTheCenterOfAVolume( "ExternalSource", radius, volume_name,center, area);
00361 }
00363 //
00364 G4bool  G4AdjointSimManager::DefineExtSourceOnTheExtSurfaceOfAVolume(const G4String& volume_name)
00365 {
00366    G4double area;
00367    return G4AdjointCrossSurfChecker::GetInstance()->AddanExtSurfaceOfAvolume( "ExternalSource", volume_name,area);
00368 }
00370 //
00371 void  G4AdjointSimManager::SetExtSourceEmax(G4double Emax)
00372 {
00373   theAdjointSteppingAction->SetExtSourceEMax(Emax);
00374 }
00376 //
00377 G4bool  G4AdjointSimManager::DefineSphericalAdjointSource(G4double radius, G4ThreeVector pos)
00378 {       
00379    G4double area;
00380    G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurface("AdjointSource", radius, pos, area); 
00381    theAdjointPrimaryGeneratorAction->SetSphericalAdjointPrimarySource(radius, pos); 
00382    area_of_the_adjoint_source=area;
00383    return aBool;        
00384 }
00386 //
00387 G4bool  G4AdjointSimManager::DefineSphericalAdjointSourceWithCentreAtTheCentreOfAVolume(G4double radius, const G4String& volume_name)
00388 {
00389    G4double area;
00390    G4ThreeVector center;
00391    G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurfaceWithCenterAtTheCenterOfAVolume( "AdjointSource", radius, volume_name,center, area);
00392    theAdjointPrimaryGeneratorAction->SetSphericalAdjointPrimarySource(radius, center);
00393    area_of_the_adjoint_source=area;
00394    return aBool;
00395 }
00397 //
00398 G4bool  G4AdjointSimManager::DefineAdjointSourceOnTheExtSurfaceOfAVolume(const G4String& volume_name)
00399 {
00400    G4double area;
00401    G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddanExtSurfaceOfAvolume( "AdjointSource", volume_name,area); 
00402    area_of_the_adjoint_source=area; 
00403    if (aBool) { 
00404         theAdjointPrimaryGeneratorAction->SetAdjointPrimarySourceOnAnExtSurfaceOfAVolume(volume_name);
00405    }
00406    return aBool;
00407 }
00409 //
00410 void G4AdjointSimManager::SetAdjointSourceEmin(G4double Emin)
00411 {
00412   theAdjointPrimaryGeneratorAction->SetEmin(Emin);
00413 }
00415 //
00416 void G4AdjointSimManager::SetAdjointSourceEmax(G4double Emax)
00417 {
00418   theAdjointPrimaryGeneratorAction->SetEmax(Emax);
00419 }
00421 //
00422 void G4AdjointSimManager::ConsiderParticleAsPrimary(const G4String& particle_name)
00423 {
00424   theAdjointPrimaryGeneratorAction->ConsiderParticleAsPrimary(particle_name);
00425 }
00427 //
00428 void G4AdjointSimManager::NeglectParticleAsPrimary(const G4String& particle_name)
00429 {
00430   theAdjointPrimaryGeneratorAction->NeglectParticleAsPrimary(particle_name);
00431 }
00433 //
00434 /*void G4AdjointSimManager::SetPrimaryIon(G4int Z, G4int A)
00435 {
00436   theAdjointPrimaryGeneratorAction->SetPrimaryIon(Z, A);
00437 }
00438 */
00440 //
00441 void G4AdjointSimManager::SetPrimaryIon(G4ParticleDefinition* adjointIon, G4ParticleDefinition* fwdIon)
00442 {
00443   theAdjointPrimaryGeneratorAction->SetPrimaryIon(adjointIon, fwdIon);
00444 }
00446 //
00447 const G4String& G4AdjointSimManager::GetPrimaryIonName()
00448 {
00449   return theAdjointPrimaryGeneratorAction->GetPrimaryIonName();
00450 }
00452 //
00453 void G4AdjointSimManager::RegisterAdjointPrimaryWeight(G4double aWeight)
00454 {
00455   theAdjointPrimaryWeight = aWeight;
00456   theAdjointSteppingAction->SetPrimWeight(aWeight);
00457 } 
00458 
00460 //
00461 void G4AdjointSimManager::SetAdjointEventAction(G4UserEventAction* anAction)
00462 {
00463   theAdjointEventAction = anAction;
00464 }
00466 //
00467 void G4AdjointSimManager::SetAdjointSteppingAction(G4UserSteppingAction* anAction)
00468 {
00469   theAdjointSteppingAction->SetUserAdjointSteppingAction(anAction);
00470 }
00472 //
00473 void G4AdjointSimManager::SetAdjointStackingAction(G4UserStackingAction* anAction)
00474 {
00475   theAdjointStackingAction->SetUserAdjointStackingAction(anAction);
00476 }
00478 //
00479 void G4AdjointSimManager::SetAdjointTrackingAction(G4UserTrackingAction* anAction)
00480 {
00481   theAdjointTrackingAction=anAction;
00482 }
00484 //
00485 void G4AdjointSimManager::SetAdjointRunAction(G4UserRunAction* anAction)
00486 {
00487   theAdjointRunAction=anAction;
00488 } 
00490 //  

Generated on Mon May 27 17:47:38 2013 for Geant4 by  doxygen 1.4.7