Geant4  10.02.p02
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 86968 2014-11-21 11:52:04Z 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  //----------------
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  //Replace the user action by the adjoint actions
237  //-------------------------------------------------
238 
239  theRunManager->G4RunManager::SetUserAction(theAdjointPrimaryGeneratorAction);
240  theRunManager->G4RunManager::SetUserAction(theAdjointStackingAction);
243  theRunManager->G4RunManager::SetUserAction(theAdjointEventAction);
244  theRunManager->G4RunManager::SetUserAction(theAdjointSteppingAction);
245  theRunManager->G4RunManager::SetUserAction(theAdjointTrackingAction);
248 }
250 //
252 {
253  G4RunManager* theRunManager = G4RunManager::GetRunManager();
254 
256 
257  //Replace the user action by the adjoint actions
258  //-------------------------------------------------
259 
260  theRunManager->G4RunManager::SetUserAction(theAdjointRunAction);
261  theRunManager->G4RunManager::SetUserAction(theAdjointPrimaryGeneratorAction);
262  theRunManager->G4RunManager::SetUserAction(theAdjointStackingAction);
265 }
267 //
269 {
270  G4RunManager* theRunManager = G4RunManager::GetRunManager();
271 
272  //Restore the user defined actions
273  //-------------------------------
274 
275  theRunManager->G4RunManager::SetUserAction(fUserEventAction);
276  theRunManager->G4RunManager::SetUserAction(fUserSteppingAction);
277  theRunManager->G4RunManager::SetUserAction(fUserTrackingAction);
278  theRunManager->G4RunManager::SetUserAction(fUserPrimaryGeneratorAction);
279  theRunManager->G4RunManager::SetUserAction(fUserStackingAction);
280 }
282 //
284 {
285  G4RunManager* theRunManager = G4RunManager::GetRunManager();
286 
287  //Restore the user defined actions
288  //-------------------------------
289 
290  theRunManager->G4RunManager::SetUserAction(fUserEventAction);
291  theRunManager->G4RunManager::SetUserAction(fUserSteppingAction);
292  theRunManager->G4RunManager::SetUserAction(fUserTrackingAction);
293 }
294 
296 //
298 {
299  G4RunManager* theRunManager = G4RunManager::GetRunManager();
300  //Restore the user defined actions
301  //-------------------------------
302  theRunManager->G4RunManager::SetUserAction(fUserRunAction);
303  theRunManager->G4RunManager::SetUserAction(fUserPrimaryGeneratorAction);
304  theRunManager->G4RunManager::SetUserAction(fUserStackingAction);
305 }
307 //
309 {
310  G4RunManager* theRunManager = G4RunManager::GetRunManager();
311  fUserTrackingAction= const_cast<G4UserTrackingAction* >( theRunManager->GetUserTrackingAction() );
312  fUserEventAction= const_cast<G4UserEventAction* >( theRunManager->GetUserEventAction() );
313  fUserSteppingAction= const_cast<G4UserSteppingAction* >( theRunManager->GetUserSteppingAction() );
316  fUserRunAction= const_cast<G4UserRunAction*>( theRunManager->GetUserRunAction() );
317  fUserStackingAction= const_cast<G4UserStackingAction* >( theRunManager->GetUserStackingAction() );
319 }
321 //
324 }
326 //
328 {
329  adjoint_tracking_mode = aBool;
330 
331  if (adjoint_tracking_mode) {
335 
336  }
337  else {
338 
344  }
346  }
347 }
349 //
351 {
353 }
355 //
356 std::vector<G4ParticleDefinition*>* G4AdjointSimManager::GetListOfPrimaryFwdParticles()
357 {
359 }
361 //
363 {
365 }
366 
368 //
371 }
372 
374 //
377 }
379 //
382 }
384 //
387 }
389 //
392 }
394 //
397 }
399 //
402 }
404 //
407 }
408 
410 //
413 }
414 
416 //
418 {
424 
425  last_fwd_part_name= aPartDef->GetParticleName();
426 
428 
430 
431  std::vector<G4ParticleDefinition*>* aList = theAdjointPrimaryGeneratorAction->GetListOfPrimaryFwdParticles();
433  size_t i=0;
434  while(i<aList->size() && last_fwd_part_index<0) {
435  if ((*aList)[i]->GetParticleName() == last_fwd_part_name) last_fwd_part_index=i;
436  i++;
437  }
438 
441  if (aPartDef->GetParticleType() == "adjoint_nucleus") {
442  nb_nuc=double(aPartDef->GetBaryonNumber());
444  }
445 
447 
448  /* G4PhysicsLogVector* theWeightVector=0;
449  if (last_fwd_part_name =="e-") theWeightVector=electron_last_weight_vector;
450  else if (last_fwd_part_name =="gamma") theWeightVector=gamma_last_weight_vector;
451  else if (last_fwd_part_name =="proton") theWeightVector=proton_last_weight_vector;
452 
453  if (theWeightVector){
454 
455  size_t ind = size_t(std::log10(last_weight/theAdjointPrimaryWeight)*10. + 200);
456  G4double low_val =theWeightVector->GetLowEdgeEnergy(ind);
457  G4bool aBool = true;
458  G4double bin_weight = theWeightVector->GetValue(low_val, aBool)+1.;
459  theWeightVector->PutValue(ind, bin_weight);
460  }
461  */
462  /*if ((last_weight/theAdjointPrimaryWeight)>1.) last_weight*=1000. ;
463  else if ( (last_weight/theAdjointPrimaryWeight)>0.1) last_weight*=100. ;
464  else if ( (last_weight/theAdjointPrimaryWeight)>0.01) last_weight*=10. ;*/
465 
466 
467  //G4cout <<"Last Weight "<<last_weight<<'\t'<<theAdjointPrimaryWeight<<'\t'<<last_weight/theAdjointPrimaryWeight<<std::endl;
468  /*if (last_weight/theAdjointPrimaryWeight >10.) {
469  G4cout<<"Warning a weight increase by a factor : "<<last_weight/theAdjointPrimaryWeight<<std::endl;
470  }
471  */
472 
474 }
476 //
478 {
479  G4double area;
480  return G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurface("ExternalSource", radius, pos, area);
481 }
483 //
485 {
486  G4double area;
487  G4ThreeVector center;
488  return G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurfaceWithCenterAtTheCenterOfAVolume( "ExternalSource", radius, volume_name,center, area);
489 }
491 //
493 {
494  G4double area;
495  return G4AdjointCrossSurfChecker::GetInstance()->AddanExtSurfaceOfAvolume( "ExternalSource", volume_name,area);
496 }
498 //
500 {
502 }
504 //
506 {
507  G4double area;
508  G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurface("AdjointSource", radius, pos, area);
511  return aBool;
512 }
514 //
516 {
517  G4double area;
518  G4ThreeVector center;
519  G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurfaceWithCenterAtTheCenterOfAVolume( "AdjointSource", radius, volume_name,center, area);
522  return aBool;
523 }
525 //
527 {
528  G4double area;
529  G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddanExtSurfaceOfAvolume( "AdjointSource", volume_name,area);
531  if (aBool) {
533  }
534  return aBool;
535 }
537 //
539 {
541 }
543 //
545 {
547 }
549 //
551 {
553 }
555 //
557 {
559 }
561 //
562 /*void G4AdjointSimManager::SetPrimaryIon(G4int Z, G4int A)
563 {
564  theAdjointPrimaryGeneratorAction->SetPrimaryIon(Z, A);
565 }
566 */
568 //
570 {
571  theAdjointPrimaryGeneratorAction->SetPrimaryIon(adjointIon, fwdIon);
572 }
574 //
576 {
578 }
580 //
582 {
583  theAdjointPrimaryWeight = aWeight;
585 }
586 
588 //
590 {
591  theAdjointEventAction = anAction;
592 }
594 //
596 {
598 }
600 //
602 {
604 }
605 
607 //
609 {
610  theAdjointRunAction=anAction;
611 }
613 //
615 {
617 }
619 //
621 {
622 
623  if (!adjoint_sim_mode){
625  }
626  else {
628  }
629 }
631 //
633 {if (!adjoint_sim_mode){
635  }
637 /*
638 #ifdef G4MULTITHREADED
639  if (G4RunManager::GetRunManager()->GetRunManagerType() == G4RunManager::workerRM){
640  if (adjoint_sim_mode) BackToFwdSimulationMode();
641  }
642 #endif
643 */
644 
645 }
static G4AdjointSimManager * GetInstance()
static G4ThreadLocal G4AdjointSimManager * instance
const G4VUserPrimaryGeneratorAction * GetUserPrimaryGeneratorAction() const
G4ParticleDefinition * FindParticle(G4int PDGEncoding)
std::vector< G4ParticleDefinition * > * GetListOfPrimaryFwdParticles()
G4bool AddaSphericalSurface(const G4String &SurfaceName, G4double radius, G4ThreeVector pos, G4double &area)
G4bool GetDidAdjParticleReachTheExtSource()
void SetAdjointSourceEmax(G4double Emax)
CLHEP::Hep3Vector G4ThreeVector
G4UserStackingAction * fUserStackingAction
G4String & remove(str_size)
void SetAdjointStackingAction(G4UserStackingAction *anAction)
virtual void EndOfRunAction(const G4Run *aRun)
G4AdjointSteppingAction * theAdjointSteppingAction
G4int GetFwdParticlePDGEncodingAtEndOfLastAdjointTrack()
virtual void BeamOn(G4int n_event, const char *macroFile=0, G4int n_select=-1)
G4bool DefineSphericalAdjointSourceWithCentreAtTheCentreOfAVolume(G4double radius, const G4String &volume_name)
G4int GetFwdParticlePDGEncodingAtEndOfLastAdjointTrack()
const G4String & GetFwdParticleNameAtEndOfLastAdjointTrack()
void SetUserAdjointStackingAction(G4UserStackingAction *anAction)
G4AdjointTrackingAction * theAdjointTrackingAction
G4bool DefineSphericalAdjointSource(G4double radius, G4ThreeVector pos)
void SetListOfPrimaryFwdParticles(std::vector< G4ParticleDefinition * > *aListOfParticles)
G4VUserPrimaryGeneratorAction * fUserPrimaryGeneratorAction
#define G4ThreadLocal
Definition: tls.hh:89
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)
G4double GetEkinAtEndOfLastAdjointTrack()
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
Definition: G4Run.hh:46
const G4UserEventAction * GetUserEventAction() const
void RunAdjointSimulation(G4int nb_evt)
G4double GetCosthAtEndOfLastAdjointTrack()
const G4UserTrackingAction * GetUserTrackingAction() const
const G4String & GetParticleType() const
void SetUserForwardSteppingAction(G4UserSteppingAction *anAction)
G4UserEventAction * theAdjointEventAction
G4AdjointStackingAction * theAdjointStackingAction
G4bool DefineSphericalExtSourceWithCentreAtTheCentreOfAVolume(G4double radius, const G4String &volume_name)
const G4String & GetFwdParticleNameAtEndOfLastAdjointTrack()
void SetPrimaryIon(G4ParticleDefinition *adjointIon, G4ParticleDefinition *fwdIon)
const G4String & GetPrimaryIonName()
G4AdjointSimMessenger * theMessenger
G4ThreeVector GetDirectionAtEndOfLastAdjointTrack()
G4bool DefineSphericalExtSource(G4double radius, G4ThreeVector pos)
static G4AdjointCrossSurfChecker * GetInstance()
void SetPrimWeight(G4double weight)
G4ParticleDefinition * GetLastPartDef()
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:79
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
G4double GetEkinNucAtEndOfLastAdjointTrack()
void NeglectParticleAsPrimary(const G4String &particle_name)
void ConsiderParticleAsPrimary(const G4String &particle_name)
void SetAdjointPrimarySourceOnAnExtSurfaceOfAVolume(const G4String &volume_name)
G4double GetWeightAtEndOfLastAdjointTrack()
std::vector< G4ParticleDefinition * > * GetListOfPrimaryFwdParticles()
virtual void BeginOfRunAction(const G4Run *aRun)
static const G4double Emin
void RegisterAdjointPrimaryWeight(G4double aWeight)
G4bool DefineExtSourceOnTheExtSurfaceOfAVolume(const G4String &volume_name)
static const G4double Emax
void SetUserAdjointSteppingAction(G4UserSteppingAction *anAction)
const G4UserRunAction * GetUserRunAction() const
void SetUserFwdStackingAction(G4UserStackingAction *anAction)
double G4double
Definition: G4Types.hh:76
G4ThreeVector GetPositionAtEndOfLastAdjointTrack()
void SetUserForwardTrackingAction(G4UserTrackingAction *anAction)
void SetExtSourceEmax(G4double Emax)
void SetSphericalAdjointPrimarySource(G4double radius, G4ThreeVector pos)
void SetAdjointPrimaryRunAndStackingActions()
G4AdjointPrimaryGeneratorAction * theAdjointPrimaryGeneratorAction
G4ThreeVector GetPositionAtEndOfLastAdjointTrack()
G4int ID_of_last_particle_that_reach_the_ext_source
void SetAdjointSteppingAction(G4UserSteppingAction *anAction)
G4int GetFwdParticleIndexAtEndOfLastAdjointTrack()
static const G4double pos
G4ThreeVector GetDirectionAtEndOfLastAdjointTrack()
void NeglectParticleAsPrimary(const G4String &particle_name)
void ResetUserPrimaryRunAndStackingActions()