Geant4  10.00.p01
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 76245 2013-11-08 11:14:32Z 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 {
75  //instance =this;
76  G4cout<<"G4AdjointSimManager::G4AdjointSimManager() Here"<<std::endl;
77  //Create adjoint actions;
78  //----------------------
83 
84  //Create messenger
85  //----------------
87 
89  use_user_StackingAction = false;
90 
91  adjoint_sim_mode = false;
92 
94 
95  nb_nuc=1.;
96 
97  welcome_message =true;
98 
99  //Define user action and set this class instance as RunAction
100  //----------------
102  G4RunManager* theRunManager = G4RunManager::GetRunManager();
103  theRunManager->SetUserAction(this);
104 /*
105 #ifdef G4MULTITHREADED
106 
107  if (theRunManager->GetRunManagerType() == G4RunManager::workerRM){
108  G4cout<<"Here"<<std::endl;
109  //G4MTAdjointSimManager::GetInstance()->RegisterLocalManager(this);
110  G4cout<<"Here1"<<std::endl;
111  }
112 #endif
113 */
114 }
116 //
118 {
125  if (theMessenger) delete theMessenger;
126 }
128 //
130 {
131  if (instance == 0) instance = new G4AdjointSimManager;
132  return instance;
133 }
135 //
137 { if (G4RunManager::GetRunManager()->GetRunManagerType() != G4RunManager::sequentialRM) return; //only for sequential mode
138  if (welcome_message) {
139  G4cout<<"****************************************************************"<<std::endl;
140  G4cout<<"*** Geant4 Reverse/Adjoint Monte Carlo mode ***"<<std::endl;
141  G4cout<<"*** Author: L.Desorgher ***"<<std::endl;
142  G4cout<<"*** Company: SpaceIT GmbH, Bern, Switzerland ***"<<std::endl;
143  G4cout<<"*** Sponsored by: ESA/ESTEC contract contract 21435/08/NL/AT ***"<<std::endl;
144  G4cout<<"****************************************************************"<<std::endl;
145  welcome_message=false;
146  }
147 
148  //Switch to adjoint simulation mode
149  //---------------------------------------------------------
151 
152  //Make the run
153  //------------
154 
155  nb_evt_of_last_run =nb_evt;
157  //G4RunManager::GetRunManager()->BeamOn(theAdjointPrimaryGeneratorAction->GetNbOfAdjointPrimaryTypes()*2*nb_evt);
158 
159  //Back to Fwd Simulation Mode
160  //--------------------------------
162 
163  /*
164  //Register the weight vector
165  //--------------------------
166  std::ofstream FileOutputElectronWeight("ElectronWeight.txt", std::ios::out);
167  FileOutputElectronWeight<<std::setiosflags(std::ios::scientific);
168  FileOutputElectronWeight<<std::setprecision(6);
169  G4bool aBool = electron_last_weight_vector->Store(FileOutputElectronWeight, true);
170  FileOutputElectronWeight.close();
171 
172  std::ofstream FileOutputProtonWeight("ProtonWeight.txt", std::ios::out);
173  FileOutputProtonWeight<<std::setiosflags(std::ios::scientific);
174  FileOutputProtonWeight<<std::setprecision(6);
175  aBool = proton_last_weight_vector->Store(FileOutputProtonWeight, true);
176  FileOutputProtonWeight.close();
177 
178  std::ofstream FileOutputGammaWeight("GammaWeight.txt", std::ios::out);
179  FileOutputGammaWeight<<std::setiosflags(std::ios::scientific);
180  FileOutputGammaWeight<<std::setprecision(6);
181  aBool = gamma_last_weight_vector->Store(FileOutputGammaWeight, true);
182  FileOutputGammaWeight.close();
183  */
184 }
186 //
188 {
189  G4RunManager* theRunManager = G4RunManager::GetRunManager();
190 
192 
193  //Replace the user action by the adjoint actions
194  //-------------------------------------------------
195 
196  theRunManager->SetUserAction(theAdjointEventAction);
197  theRunManager->SetUserAction(theAdjointSteppingAction);
198  theRunManager->SetUserAction(theAdjointTrackingAction);
199 
200 }
202 //
204 { //Replace the user defined actions by the adjoint actions
205  //---------------------------------------------------------
207 
208  //Update the list of primaries
209  //-----------------------------
211  adjoint_sim_mode=true;
213 }
215 //
217 { //Restore the user defined actions
218  //--------------------------------
220  adjoint_sim_mode=false;
221 }
222 
224 //
226 {
227  G4RunManager* theRunManager = G4RunManager::GetRunManager();
228 
230 
231  //Replace the user action by the adjoint actions
232  //-------------------------------------------------
233 
235  theRunManager->SetUserAction(theAdjointStackingAction);
239  theRunManager->SetUserAction(theAdjointSteppingAction);
240  theRunManager->SetUserAction(theAdjointTrackingAction);
241 }
243 //
245 {
246  G4RunManager* theRunManager = G4RunManager::GetRunManager();
247 
249 
250  //Replace the user action by the adjoint actions
251  //-------------------------------------------------
252 
253  theRunManager->SetUserAction(theAdjointRunAction);
255  theRunManager->SetUserAction(theAdjointStackingAction);
258 }
260 //
262 {
263  G4RunManager* theRunManager = G4RunManager::GetRunManager();
264 
265  //Restore the user defined actions
266  //-------------------------------
267 
268  theRunManager->SetUserAction(fUserEventAction);
269  theRunManager->SetUserAction(fUserSteppingAction);
270  theRunManager->SetUserAction(fUserTrackingAction);
272  theRunManager->SetUserAction(fUserStackingAction);
273 }
275 //
277 {
278  G4RunManager* theRunManager = G4RunManager::GetRunManager();
279 
280  //Restore the user defined actions
281  //-------------------------------
282 
283  theRunManager->SetUserAction(fUserEventAction);
284  theRunManager->SetUserAction(fUserSteppingAction);
285  theRunManager->SetUserAction(fUserTrackingAction);
286 }
287 
289 //
291 {
292  G4RunManager* theRunManager = G4RunManager::GetRunManager();
293  //Restore the user defined actions
294  //-------------------------------
295  theRunManager->SetUserAction(fUserRunAction);
297  theRunManager->SetUserAction(fUserStackingAction);
298 }
300 //
302 {
303  G4RunManager* theRunManager = G4RunManager::GetRunManager();
304  fUserTrackingAction= const_cast<G4UserTrackingAction* >( theRunManager->GetUserTrackingAction() );
305  fUserEventAction= const_cast<G4UserEventAction* >( theRunManager->GetUserEventAction() );
306  fUserSteppingAction= const_cast<G4UserSteppingAction* >( theRunManager->GetUserSteppingAction() );
309  fUserRunAction= const_cast<G4UserRunAction*>( theRunManager->GetUserRunAction() );
310  fUserStackingAction= const_cast<G4UserStackingAction* >( theRunManager->GetUserStackingAction() );
312 }
314 //
316 {
317  adjoint_tracking_mode = aBool;
318 
319  if (adjoint_tracking_mode) {
323 
324  }
325  else {
326 
332  }
334  }
335 }
337 //
339 {
341 }
343 //
344 std::vector<G4ParticleDefinition*> G4AdjointSimManager::GetListOfPrimaryFwdParticles()
345 {
347 }
349 //
351 {
353 }
354 
356 //
359 }
360 
362 //
365 }
367 //
370 }
372 //
375 }
377 //
380 }
382 //
385 }
387 //
390 }
392 //
395 }
396 
398 //
400 {
406 
407  last_fwd_part_name= aPartDef->GetParticleName();
408 
410 
412 
413  std::vector<G4ParticleDefinition*> aList = theAdjointPrimaryGeneratorAction->GetListOfPrimaryFwdParticles();
415  size_t i=0;
416  while(i<aList.size() && last_fwd_part_index<0) {
417  if (aList[i]->GetParticleName() == last_fwd_part_name) last_fwd_part_index=i;
418  i++;
419  }
420 
423  if (aPartDef->GetParticleType() == "adjoint_nucleus") {
424  nb_nuc=double(aPartDef->GetBaryonNumber());
426  }
427 
429 
430  /* G4PhysicsLogVector* theWeightVector=0;
431  if (last_fwd_part_name =="e-") theWeightVector=electron_last_weight_vector;
432  else if (last_fwd_part_name =="gamma") theWeightVector=gamma_last_weight_vector;
433  else if (last_fwd_part_name =="proton") theWeightVector=proton_last_weight_vector;
434 
435  if (theWeightVector){
436 
437  size_t ind = size_t(std::log10(last_weight/theAdjointPrimaryWeight)*10. + 200);
438  G4double low_val =theWeightVector->GetLowEdgeEnergy(ind);
439  G4bool aBool = true;
440  G4double bin_weight = theWeightVector->GetValue(low_val, aBool)+1.;
441  theWeightVector->PutValue(ind, bin_weight);
442  }
443  */
444  /*if ((last_weight/theAdjointPrimaryWeight)>1.) last_weight*=1000. ;
445  else if ( (last_weight/theAdjointPrimaryWeight)>0.1) last_weight*=100. ;
446  else if ( (last_weight/theAdjointPrimaryWeight)>0.01) last_weight*=10. ;*/
447 
448 
449  //G4cout <<"Last Weight "<<last_weight<<'\t'<<theAdjointPrimaryWeight<<'\t'<<last_weight/theAdjointPrimaryWeight<<std::endl;
450  /*if (last_weight/theAdjointPrimaryWeight >10.) {
451  G4cout<<"Warning a weight increase by a factor : "<<last_weight/theAdjointPrimaryWeight<<std::endl;
452  }
453  */
454 
456 }
458 //
460 {
461  G4double area;
462  return G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurface("ExternalSource", radius, pos, area);
463 }
465 //
467 {
468  G4double area;
469  G4ThreeVector center;
470  return G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurfaceWithCenterAtTheCenterOfAVolume( "ExternalSource", radius, volume_name,center, area);
471 }
473 //
475 {
476  G4double area;
477  return G4AdjointCrossSurfChecker::GetInstance()->AddanExtSurfaceOfAvolume( "ExternalSource", volume_name,area);
478 }
480 //
482 {
484 }
486 //
488 {
489  G4double area;
490  G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurface("AdjointSource", radius, pos, area);
493  return aBool;
494 }
496 //
498 {
499  G4double area;
500  G4ThreeVector center;
501  G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurfaceWithCenterAtTheCenterOfAVolume( "AdjointSource", radius, volume_name,center, area);
504  return aBool;
505 }
507 //
509 {
510  G4double area;
511  G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddanExtSurfaceOfAvolume( "AdjointSource", volume_name,area);
513  if (aBool) {
515  }
516  return aBool;
517 }
519 //
521 {
523 }
525 //
527 {
529 }
531 //
533 {
535 }
537 //
539 {
541 }
543 //
544 /*void G4AdjointSimManager::SetPrimaryIon(G4int Z, G4int A)
545 {
546  theAdjointPrimaryGeneratorAction->SetPrimaryIon(Z, A);
547 }
548 */
550 //
552 {
553  theAdjointPrimaryGeneratorAction->SetPrimaryIon(adjointIon, fwdIon);
554 }
556 //
558 {
560 }
562 //
564 {
565  theAdjointPrimaryWeight = aWeight;
567 }
568 
570 //
572 {
573  theAdjointEventAction = anAction;
574 }
576 //
578 {
580 }
582 //
584 {
586 }
587 
589 //
591 {
592  theAdjointRunAction=anAction;
593 }
595 //
597 //
599 {
600 G4cout<<"G4AdjointSimManager::BeginOfRunAction"<<std::endl;
601 
602  if (!adjoint_sim_mode){
604  }
605  else {
607  }
608 }
610 //
612 {if (!adjoint_sim_mode){
614  }
616 /*
617 #ifdef G4MULTITHREADED
618  if (G4RunManager::GetRunManager()->GetRunManagerType() == G4RunManager::workerRM){
619  if (adjoint_sim_mode) BackToFwdSimulationMode();
620  }
621 #endif
622 */
623 
624 }
static G4AdjointSimManager * GetInstance()
static G4ThreadLocal G4AdjointSimManager * instance
const G4VUserPrimaryGeneratorAction * GetUserPrimaryGeneratorAction() const
G4ParticleDefinition * FindParticle(G4int PDGEncoding)
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)
G4VUserPrimaryGeneratorAction * fUserPrimaryGeneratorAction
#define G4ThreadLocal
Definition: tls.hh:52
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
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:74
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()
std::vector< G4ParticleDefinition * > GetListOfPrimaryFwdParticles()
void NeglectParticleAsPrimary(const G4String &particle_name)
void ConsiderParticleAsPrimary(const G4String &particle_name)
void SetAdjointPrimarySourceOnAnExtSurfaceOfAVolume(const G4String &volume_name)
G4double GetWeightAtEndOfLastAdjointTrack()
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 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)
static const G4double pos
G4ThreeVector GetDirectionAtEndOfLastAdjointTrack()
void NeglectParticleAsPrimary(const G4String &particle_name)
std::vector< G4ParticleDefinition * > GetListOfPrimaryFwdParticles()
virtual void SetUserAction(G4UserRunAction *userAction)
void ResetUserPrimaryRunAndStackingActions()