00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00029
00030
00031
00032
00033
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
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
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
00099
00100
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
00136
00137 SetAdjointPrimaryRunAndStackingActions();
00138 SetRestOfAdjointActions();
00139
00140
00141
00142 theAdjointPrimaryGeneratorAction->UpdateListOfPrimaryParticles();
00143
00144 adjoint_sim_mode=true;
00145
00146 ID_of_last_particle_that_reach_the_ext_source=0;
00147
00148
00149
00150
00151 nb_evt_of_last_run =nb_evt;
00152 G4RunManager::GetRunManager()->BeamOn(theAdjointPrimaryGeneratorAction->GetNbOfAdjointPrimaryTypes()*2*nb_evt);
00153
00154
00155
00156 ResetRestOfUserActions();
00157 ResetUserPrimaryRunAndStackingActions();
00158 adjoint_sim_mode=false;
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 }
00183
00184 void G4AdjointSimManager::SetRestOfAdjointActions()
00185 {
00186 G4RunManager* theRunManager = G4RunManager::GetRunManager();
00187
00188 if (!user_action_already_defined) DefineUserActions();
00189
00190
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
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
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
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
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
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
00435
00436
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