Geant4  10.03
G4AdjointSimManager.cc
Go to the documentation of this file.
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 // $Id: G4AdjointSimManager.cc 98735 2016-08-09 10:54:06Z gcosmo $
27 //
29 // Class Name: G4AdjointCrossSurfChecker
30 // Author: L. Desorgher
31 // Organisation: SpaceIT GmbH
32 // Contract: ESA contract 21435/08/NL/AT
33 // Customer: ESA/ESTEC
35 
36 #include "G4AdjointSimManager.hh"
37 #include "G4Run.hh"
38 #include "G4RunManager.hh"
39 
40 #include "G4UserEventAction.hh"
42 #include "G4UserTrackingAction.hh"
43 #include "G4UserSteppingAction.hh"
44 #include "G4UserStackingAction.hh"
45 #include "G4UserRunAction.hh"
46 
51 
52 #include "G4AdjointSimMessenger.hh"
53 
55 
56 #include "G4ParticleTable.hh"
57 #include "G4PhysicsLogVector.hh"
58 /*
59 #ifdef G4MULTITHREADED
60 #include "G4MTAdjointSimManager.hh"
61 #endif
62 */
63 
65 //
67 
69 //
71  fUserRunAction(0), fUserEventAction(0),fUserPrimaryGeneratorAction(0),
72  fUserTrackingAction(0), fUserSteppingAction(0), fUserStackingAction(0),
73  theAdjointRunAction(0), theAdjointEventAction(0),
74  adjoint_tracking_mode(false),last_ekin(0),last_ekin_nuc(0),
75  last_cos_th(0),last_fwd_part_PDGEncoding(0),last_fwd_part_index(0),
76  last_weight(0), ID_of_last_particle_that_reach_the_ext_source(0),
77  nb_evt_of_last_run(0),area_of_the_adjoint_source(0),theAdjointPrimaryWeight(0)
78 {
79  //Create adjoint actions;
80  //----------------------
87  //Create messenger
88  //----------------
90 
92  use_user_StackingAction = false;
94 
95  adjoint_sim_mode = false;
96 
98 
99  nb_nuc=1.;
100 
101  welcome_message =true;
102 
103  //Define user action and set this class instance as RunAction
104  //----------------
105  //DefineUserActions();
106  //G4RunManager* theRunManager = G4RunManager::GetRunManager();
107 
108  //theRunManager->G4RunManager::SetUserAction(this);
109 /*
110 #ifdef G4MULTITHREADED
111 
112  if (theRunManager->GetRunManagerType() == G4RunManager::workerRM){
113  G4cout<<"Here"<<std::endl;
114  //G4MTAdjointSimManager::GetInstance()->RegisterLocalManager(this);
115  G4cout<<"Here1"<<std::endl;
116  }
117 #endif
118 */
119 }
121 //
123 {
130  if (theMessenger) delete theMessenger;
131 }
133 //
135 {
136  if (instance == 0) instance = new G4AdjointSimManager;
137  return instance;
138 }
140 //
142 { if (G4RunManager::GetRunManager()->GetRunManagerType() != G4RunManager::sequentialRM) return; //only for sequential mode
143  if (welcome_message) {
144  G4cout<<"****************************************************************"<<std::endl;
145  G4cout<<"*** Geant4 Reverse/Adjoint Monte Carlo mode ***"<<std::endl;
146  G4cout<<"*** Author: L.Desorgher ***"<<std::endl;
147  G4cout<<"*** Company: SpaceIT GmbH, Bern, Switzerland ***"<<std::endl;
148  G4cout<<"*** Sponsored by: ESA/ESTEC contract contract 21435/08/NL/AT ***"<<std::endl;
149  G4cout<<"****************************************************************"<<std::endl;
150  welcome_message=false;
151  }
152 
153  //Switch to adjoint simulation mode
154  //---------------------------------------------------------
156 
157  //Make the run
158  //------------
159 
160  nb_evt_of_last_run =nb_evt;
162  //G4RunManager::GetRunManager()->BeamOn(theAdjointPrimaryGeneratorAction->GetNbOfAdjointPrimaryTypes()*2*nb_evt);
163 
164  //Back to Fwd Simulation Mode
165  //--------------------------------
167 
168  /*
169  //Register the weight vector
170  //--------------------------
171  std::ofstream FileOutputElectronWeight("ElectronWeight.txt", std::ios::out);
172  FileOutputElectronWeight<<std::setiosflags(std::ios::scientific);
173  FileOutputElectronWeight<<std::setprecision(6);
174  G4bool aBool = electron_last_weight_vector->Store(FileOutputElectronWeight, true);
175  FileOutputElectronWeight.close();
176 
177  std::ofstream FileOutputProtonWeight("ProtonWeight.txt", std::ios::out);
178  FileOutputProtonWeight<<std::setiosflags(std::ios::scientific);
179  FileOutputProtonWeight<<std::setprecision(6);
180  aBool = proton_last_weight_vector->Store(FileOutputProtonWeight, true);
181  FileOutputProtonWeight.close();
182 
183  std::ofstream FileOutputGammaWeight("GammaWeight.txt", std::ios::out);
184  FileOutputGammaWeight<<std::setiosflags(std::ios::scientific);
185  FileOutputGammaWeight<<std::setprecision(6);
186  aBool = gamma_last_weight_vector->Store(FileOutputGammaWeight, true);
187  FileOutputGammaWeight.close();
188  */
189 }
191 //
193 {
194  G4RunManager* theRunManager = G4RunManager::GetRunManager();
195 
197 
198  //Replace the user action by the adjoint actions
199  //-------------------------------------------------
200 
201  theRunManager->G4RunManager::SetUserAction(theAdjointEventAction);
202  theRunManager->G4RunManager::SetUserAction(theAdjointSteppingAction);
203  theRunManager->G4RunManager::SetUserAction(theAdjointTrackingAction);
204 
205 }
207 //
209 { //Replace the user defined actions by the adjoint actions
210  //---------------------------------------------------------
212 
213  //Update the list of primaries
214  //-----------------------------
216  adjoint_sim_mode=true;
218 }
220 //
222 { //Restore the user defined actions
223  //--------------------------------
225  adjoint_sim_mode=false;
226 }
227 
229 //
231 {
232  G4RunManager* theRunManager = G4RunManager::GetRunManager();
233 
235 
236 
237  //Replace the user action by the adjoint actions
238  //-------------------------------------------------
239  theRunManager->G4RunManager::SetUserAction(this);
240  theRunManager->G4RunManager::SetUserAction(theAdjointPrimaryGeneratorAction);
241  theRunManager->G4RunManager::SetUserAction(theAdjointStackingAction);
244  theRunManager->G4RunManager::SetUserAction(theAdjointEventAction);
245  theRunManager->G4RunManager::SetUserAction(theAdjointSteppingAction);
246  theRunManager->G4RunManager::SetUserAction(theAdjointTrackingAction);
249 }
251 //
253 {
254  G4RunManager* theRunManager = G4RunManager::GetRunManager();
255 
257 
258  //Replace the user action by the adjoint actions
259  //-------------------------------------------------
260 
261  theRunManager->G4RunManager::SetUserAction(theAdjointRunAction);
262  theRunManager->G4RunManager::SetUserAction(theAdjointPrimaryGeneratorAction);
263  theRunManager->G4RunManager::SetUserAction(theAdjointStackingAction);
266 }
268 //
270 {
271  G4RunManager* theRunManager = G4RunManager::GetRunManager();
272 
273  //Restore the user defined actions
274  //-------------------------------
275  theRunManager->G4RunManager::SetUserAction(fUserRunAction);
276  theRunManager->G4RunManager::SetUserAction(fUserEventAction);
277  theRunManager->G4RunManager::SetUserAction(fUserSteppingAction);
278  theRunManager->G4RunManager::SetUserAction(fUserTrackingAction);
279  theRunManager->G4RunManager::SetUserAction(fUserPrimaryGeneratorAction);
280  theRunManager->G4RunManager::SetUserAction(fUserStackingAction);
281 }
283 //
285 {
286  G4RunManager* theRunManager = G4RunManager::GetRunManager();
287 
288  //Restore the user defined actions
289  //-------------------------------
290 
291  theRunManager->G4RunManager::SetUserAction(fUserEventAction);
292  theRunManager->G4RunManager::SetUserAction(fUserSteppingAction);
293  theRunManager->G4RunManager::SetUserAction(fUserTrackingAction);
294 }
295 
297 //
299 {
300  G4RunManager* theRunManager = G4RunManager::GetRunManager();
301  //Restore the user defined actions
302  //-------------------------------
303  theRunManager->G4RunManager::SetUserAction(fUserRunAction);
304  theRunManager->G4RunManager::SetUserAction(fUserPrimaryGeneratorAction);
305  theRunManager->G4RunManager::SetUserAction(fUserStackingAction);
306 }
308 //
310 {
311  G4RunManager* theRunManager = G4RunManager::GetRunManager();
312  fUserTrackingAction= const_cast<G4UserTrackingAction* >( theRunManager->GetUserTrackingAction() );
313  fUserEventAction= const_cast<G4UserEventAction* >( theRunManager->GetUserEventAction() );
314  fUserSteppingAction= const_cast<G4UserSteppingAction* >( theRunManager->GetUserSteppingAction() );
317  fUserRunAction= const_cast<G4UserRunAction*>( theRunManager->GetUserRunAction() );
318  fUserStackingAction= const_cast<G4UserStackingAction* >( theRunManager->GetUserStackingAction() );
320 }
322 //
325 }
327 //
329 {
330  adjoint_tracking_mode = aBool;
331 
332  if (adjoint_tracking_mode) {
336 
337  }
338  else {
339 
345  }
347  }
348 }
350 //
352 {
354 }
355 
357 //
358 std::vector<G4ParticleDefinition*>* G4AdjointSimManager::GetListOfPrimaryFwdParticles()
359 {
361 }
363 //
365 {
367 }
368 
370 //
373 }
374 
376 //
379 }
381 //
384 }
386 //
389 }
391 //
394 }
396 //
399 }
401 //
404 }
406 //
409 }
410 
412 //
415 }
417 //
419 {
421 }
423 //
426 }
427 
429 //
431 {
437 
438  last_fwd_part_name= aPartDef->GetParticleName();
439 
441 
443 
444  std::vector<G4ParticleDefinition*>* aList = theAdjointPrimaryGeneratorAction->GetListOfPrimaryFwdParticles();
446  size_t i=0;
447  while(i<aList->size() && last_fwd_part_index<0) {
448  if ((*aList)[i]->GetParticleName() == last_fwd_part_name) last_fwd_part_index=i;
449  i++;
450  }
451 
454  if (aPartDef->GetParticleType() == "adjoint_nucleus") {
455  nb_nuc=double(aPartDef->GetBaryonNumber());
457  }
458 
460 
461 
462 
463  last_pos_vec.push_back(last_pos);
465  last_ekin_vec.push_back(last_ekin);
466  last_ekin_nuc_vec.push_back(last_ekin_nuc);
467  last_cos_th_vec.push_back(last_cos_th);
468  last_weight_vec.push_back(last_weight);
473 
474 
475 
476 
477 
478 
479  /* G4PhysicsLogVector* theWeightVector=0;
480  if (last_fwd_part_name =="e-") theWeightVector=electron_last_weight_vector;
481  else if (last_fwd_part_name =="gamma") theWeightVector=gamma_last_weight_vector;
482  else if (last_fwd_part_name =="proton") theWeightVector=proton_last_weight_vector;
483 
484  if (theWeightVector){
485 
486  size_t ind = size_t(std::log10(last_weight/theAdjointPrimaryWeight)*10. + 200);
487  G4double low_val =theWeightVector->GetLowEdgeEnergy(ind);
488  G4bool aBool = true;
489  G4double bin_weight = theWeightVector->GetValue(low_val, aBool)+1.;
490  theWeightVector->PutValue(ind, bin_weight);
491  }
492  */
493  /*if ((last_weight/theAdjointPrimaryWeight)>1.) last_weight*=1000. ;
494  else if ( (last_weight/theAdjointPrimaryWeight)>0.1) last_weight*=100. ;
495  else if ( (last_weight/theAdjointPrimaryWeight)>0.01) last_weight*=10. ;*/
496 
497 
498  //G4cout <<"Last Weight "<<last_weight<<'\t'<<theAdjointPrimaryWeight<<'\t'<<last_weight/theAdjointPrimaryWeight<<std::endl;
499  /*if (last_weight/theAdjointPrimaryWeight >10.) {
500  G4cout<<"Warning a weight increase by a factor : "<<last_weight/theAdjointPrimaryWeight<<std::endl;
501  }
502  */
503 
504 
505 }
507 //
509 {
510  G4double area;
511  return G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurface("ExternalSource", radius, pos, area);
512 }
514 //
516 {
517  G4double area;
518  G4ThreeVector center;
519  return G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurfaceWithCenterAtTheCenterOfAVolume( "ExternalSource", radius, volume_name,center, area);
520 }
522 //
524 {
525  G4double area;
526  return G4AdjointCrossSurfChecker::GetInstance()->AddanExtSurfaceOfAvolume( "ExternalSource", volume_name,area);
527 }
529 //
531 {
533 }
535 //
537 {
538  G4double area;
539  G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurface("AdjointSource", radius, pos, area);
542  return aBool;
543 }
545 //
547 {
548  G4double area;
549  G4ThreeVector center;
550  G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurfaceWithCenterAtTheCenterOfAVolume( "AdjointSource", radius, volume_name,center, area);
553  return aBool;
554 }
556 //
558 {
559  G4double area;
560  G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddanExtSurfaceOfAvolume( "AdjointSource", volume_name,area);
562  if (aBool) {
564  }
565  return aBool;
566 }
568 //
570 {
572 }
574 //
576 {
578 }
580 //
582 {
584 }
586 //
588 {
590 }
592 //
593 /*void G4AdjointSimManager::SetPrimaryIon(G4int Z, G4int A)
594 {
595  theAdjointPrimaryGeneratorAction->SetPrimaryIon(Z, A);
596 }
597 */
599 //
601 {
602  theAdjointPrimaryGeneratorAction->SetPrimaryIon(adjointIon, fwdIon);
603 }
605 //
607 {
609 }
611 //
613 {
614  theAdjointPrimaryWeight = aWeight;
616 }
617 
619 //
621 {
622  theAdjointEventAction = anAction;
623 }
625 //
627 {
629 }
631 //
633 {
635 }
636 
638 //
640 {
641  theAdjointRunAction=anAction;
642 }
644 //
646 {
648 }
650 //
652 {
654 }
656 //
658 {
660 }
662 //
664 {
665 /*
666  if (!adjoint_sim_mode){
667  if(fUserRunAction) fUserRunAction->BeginOfRunAction(aRun);
668  }
669  else {
670  if (theAdjointRunAction) theAdjointRunAction->BeginOfRunAction(aRun);
671  }
672  */
674 }
676 //
678 {if (!adjoint_sim_mode){
680  }
682 /*
683 #ifdef G4MULTITHREADED
684  if (G4RunManager::GetRunManager()->GetRunManagerType() == G4RunManager::workerRM){
685  if (adjoint_sim_mode) BackToFwdSimulationMode();
686  }
687 #endif
688 */
689 
690 }
692 //
695 }
696 
697 
static G4AdjointSimManager * GetInstance()
G4int GetLastFwdParticleIndex(size_t i=0)
static G4ThreadLocal G4AdjointSimManager * instance
const G4VUserPrimaryGeneratorAction * GetUserPrimaryGeneratorAction() const
G4double GetCosthAtEndOfLastAdjointTrack(size_t i=0)
G4ParticleDefinition * FindParticle(G4int PDGEncoding)
G4int GetFwdParticleIndexAtEndOfLastAdjointTrack(size_t i=0)
std::vector< G4ParticleDefinition * > * GetListOfPrimaryFwdParticles()
G4bool AddaSphericalSurface(const G4String &SurfaceName, G4double radius, G4ThreeVector pos, G4double &area)
G4bool GetDidAdjParticleReachTheExtSource()
std::vector< G4int > ID_of_last_particle_that_reach_the_ext_source_vec
void SetAdjointSourceEmax(G4double Emax)
std::vector< G4ThreeVector > last_pos_vec
CLHEP::Hep3Vector G4ThreeVector
G4double GetEkinNucAtEndOfLastAdjointTrack(size_t i=0)
G4UserStackingAction * fUserStackingAction
G4String & remove(str_size)
std::vector< G4double > last_weight_vec
void SetAdjointStackingAction(G4UserStackingAction *anAction)
G4ThreeVector GetPositionAtEndOfLastAdjointTrack(size_t i=0)
virtual void EndOfRunAction(const G4Run *aRun)
G4AdjointSteppingAction * theAdjointSteppingAction
G4ThreeVector GetDirectionAtEndOfLastAdjointTrack(size_t i=0)
virtual void BeamOn(G4int n_event, const char *macroFile=0, G4int n_select=-1)
G4bool DefineSphericalAdjointSourceWithCentreAtTheCentreOfAVolume(G4double radius, const G4String &volume_name)
const G4String & GetFwdParticleNameAtEndOfLastAdjointTrack()
std::vector< G4int > last_fwd_part_index_vec
void SetUserAdjointStackingAction(G4UserStackingAction *anAction)
G4AdjointTrackingAction * theAdjointTrackingAction
G4bool DefineSphericalAdjointSource(G4double radius, G4ThreeVector pos)
void SetListOfPrimaryFwdParticles(std::vector< G4ParticleDefinition * > *aListOfParticles)
G4ParticleDefinition * GetLastGeneratedFwdPrimaryParticle()
G4VUserPrimaryGeneratorAction * fUserPrimaryGeneratorAction
#define G4ThreadLocal
Definition: tls.hh:89
G4double GetCosthAtEndOfLastAdjointTrack(size_t i=0)
int G4int
Definition: G4Types.hh:78
const G4UserSteppingAction * GetUserSteppingAction() const
const G4String & GetParticleName() const
virtual void EndOfRunAction(const G4Run *aRun)
void SetAdjointSourceEmin(G4double Emin)
G4UserRunAction * fUserRunAction
G4bool AddaSphericalSurfaceWithCenterAtTheCenterOfAVolume(const G4String &SurfaceName, G4double radius, const G4String &volume_name, G4ThreeVector &center, G4double &area)
void SetPrimaryIon(G4ParticleDefinition *adjointIon, G4ParticleDefinition *fwdIon)
G4UserEventAction * fUserEventAction
void ConsiderParticleAsPrimary(const G4String &particle_name)
G4GLOB_DLL std::ostream G4cout
void SetAdjointTrackingMode(G4bool aBool)
const G4UserStackingAction * GetUserStackingAction() const
G4UserTrackingAction * fUserTrackingAction
virtual void BeginOfRunAction(const G4Run *aRun)
G4bool DefineAdjointSourceOnTheExtSurfaceOfAVolume(const G4String &volume_name)
bool G4bool
Definition: G4Types.hh:79
void SetNbOfPrimaryFwdGammasPerEvent(G4int)
G4UserRunAction * theAdjointRunAction
std::vector< G4double > last_cos_th_vec
G4double GetEkinNucAtEndOfLastAdjointTrack(size_t i=0)
Definition: G4Run.hh:46
const G4UserEventAction * GetUserEventAction() const
G4int GetFwdParticlePDGEncodingAtEndOfLastAdjointTrack(size_t i=0)
void RunAdjointSimulation(G4int nb_evt)
const G4UserTrackingAction * GetUserTrackingAction() const
const G4String & GetParticleType() const
size_t GetNbOfAdointTracksReachingTheExternalSurface()
void SetUserForwardSteppingAction(G4UserSteppingAction *anAction)
G4UserEventAction * theAdjointEventAction
G4AdjointStackingAction * theAdjointStackingAction
G4double GetWeightAtEndOfLastAdjointTrack(size_t i=0)
G4bool DefineSphericalExtSourceWithCentreAtTheCentreOfAVolume(G4double radius, const G4String &volume_name)
const G4String & GetFwdParticleNameAtEndOfLastAdjointTrack()
void SetPrimaryIon(G4ParticleDefinition *adjointIon, G4ParticleDefinition *fwdIon)
const G4String & GetPrimaryIonName()
G4AdjointSimMessenger * theMessenger
G4bool DefineSphericalExtSource(G4double radius, G4ThreeVector pos)
static G4AdjointCrossSurfChecker * GetInstance()
void SetPrimWeight(G4double weight)
G4ParticleDefinition * GetLastPartDef()
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:79
G4double GetEkinAtEndOfLastAdjointTrack(size_t i=0)
std::vector< G4double > last_ekin_nuc_vec
void SetExtSourceEMax(G4double Emax)
void SetAdjointRunAction(G4UserRunAction *anAction)
G4bool AddanExtSurfaceOfAvolume(const G4String &SurfaceName, const G4String &volume_name, G4double &area)
void SetAdjointEventAction(G4UserEventAction *anAction)
static G4ParticleTable * GetParticleTable()
G4UserSteppingAction * fUserSteppingAction
G4ThreeVector GetPositionAtEndOfLastAdjointTrack(size_t i=0)
void NeglectParticleAsPrimary(const G4String &particle_name)
void ConsiderParticleAsPrimary(const G4String &particle_name)
void SetAdjointPrimarySourceOnAnExtSurfaceOfAVolume(const G4String &volume_name)
std::vector< G4ParticleDefinition * > * GetListOfPrimaryFwdParticles()
virtual void BeginOfRunAction(const G4Run *aRun)
static const G4double Emin
G4int GetFwdParticlePDGEncodingAtEndOfLastAdjointTrack(size_t i=0)
void RegisterAdjointPrimaryWeight(G4double aWeight)
G4bool DefineExtSourceOnTheExtSurfaceOfAVolume(const G4String &volume_name)
static const G4double Emax
void SetNbAdjointPrimaryElectronsPerEvent(G4int)
void SetNbAdjointPrimaryGammasPerEvent(G4int)
void SetUserAdjointSteppingAction(G4UserSteppingAction *anAction)
G4ParticleDefinition * GetLastGeneratedFwdPrimaryParticle()
const G4UserRunAction * GetUserRunAction() const
void SetUserFwdStackingAction(G4UserStackingAction *anAction)
double G4double
Definition: G4Types.hh:76
void SetUserForwardTrackingAction(G4UserTrackingAction *anAction)
void SetExtSourceEmax(G4double Emax)
std::vector< G4ThreeVector > last_direction_vec
void SetSphericalAdjointPrimarySource(G4double radius, G4ThreeVector pos)
G4ThreeVector GetDirectionAtEndOfLastAdjointTrack(size_t i=0)
std::vector< G4double > last_ekin_vec
void SetAdjointPrimaryRunAndStackingActions()
G4AdjointPrimaryGeneratorAction * theAdjointPrimaryGeneratorAction
G4int ID_of_last_particle_that_reach_the_ext_source
void SetAdjointSteppingAction(G4UserSteppingAction *anAction)
G4double GetEkinAtEndOfLastAdjointTrack(size_t i=0)
G4double GetWeightAtEndOfLastAdjointTrack(size_t i=0)
std::vector< G4int > last_fwd_part_PDGEncoding_vec
static const G4double pos
size_t GetNbOfAdointTracksReachingTheExternalSurface()
void NeglectParticleAsPrimary(const G4String &particle_name)
void ResetUserPrimaryRunAndStackingActions()